STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_dac_ex.c
Go to the documentation of this file.
1 
49 /* Includes ------------------------------------------------------------------*/
50 #include "stm32l4xx_hal.h"
51 
56 #ifdef HAL_DAC_MODULE_ENABLED
57 
58 #if defined(DAC1)
59 
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 /* Private macro -------------------------------------------------------------*/
68 /* Private variables ---------------------------------------------------------*/
69 /* Private function prototypes -----------------------------------------------*/
70 /* Exported functions --------------------------------------------------------*/
71 
119 HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
120 {
121  /* Check the parameters */
122  assert_param(IS_DAC_CHANNEL(Channel));
123  assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
124 
125  /* Process locked */
126  __HAL_LOCK(hdac);
127 
128  /* Change DAC state */
129  hdac->State = HAL_DAC_STATE_BUSY;
130 
131  /* Enable the triangle wave generation for the selected DAC channel */
132  MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL), (DAC_CR_WAVE1_1 | Amplitude) << (Channel & 0x10UL));
133 
134  /* Change DAC state */
135  hdac->State = HAL_DAC_STATE_READY;
136 
137  /* Process unlocked */
138  __HAL_UNLOCK(hdac);
139 
140  /* Return function status */
141  return HAL_OK;
142 }
143 
168 HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
169 {
170  /* Check the parameters */
171  assert_param(IS_DAC_CHANNEL(Channel));
172  assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
173 
174  /* Process locked */
175  __HAL_LOCK(hdac);
176 
177  /* Change DAC state */
178  hdac->State = HAL_DAC_STATE_BUSY;
179 
180  /* Enable the noise wave generation for the selected DAC channel */
181  MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL), (DAC_CR_WAVE1_0 | Amplitude) << (Channel & 0x10UL));
182 
183  /* Change DAC state */
184  hdac->State = HAL_DAC_STATE_READY;
185 
186  /* Process unlocked */
187  __HAL_UNLOCK(hdac);
188 
189  /* Return function status */
190  return HAL_OK;
191 }
192 
193 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
194  defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
195  defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
196 
212 HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
213 {
214  uint32_t data;
215  uint32_t tmp;
216 
217  /* Check the parameters */
218  assert_param(IS_DAC_ALIGN(Alignment));
219  assert_param(IS_DAC_DATA(Data1));
220  assert_param(IS_DAC_DATA(Data2));
221 
222  /* Calculate and set dual DAC data holding register value */
223  if (Alignment == DAC_ALIGN_8B_R)
224  {
225  data = ((uint32_t)Data2 << 8U) | Data1;
226  }
227  else
228  {
229  data = ((uint32_t)Data2 << 16U) | Data1;
230  }
231 
232  tmp = (uint32_t)hdac->Instance;
233  tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
234 
235  /* Set the dual DAC selected data holding register */
236  *(__IO uint32_t *)tmp = data;
237 
238  /* Return function status */
239  return HAL_OK;
240 }
241 
249 {
250  /* Prevent unused argument(s) compilation warning */
251  UNUSED(hdac);
252 
253  /* NOTE : This function should not be modified, when the callback is needed,
254  the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
255  */
256 }
257 
265 {
266  /* Prevent unused argument(s) compilation warning */
267  UNUSED(hdac);
268 
269  /* NOTE : This function should not be modified, when the callback is needed,
270  the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
271  */
272 }
273 
281 {
282  /* Prevent unused argument(s) compilation warning */
283  UNUSED(hdac);
284 
285  /* NOTE : This function should not be modified, when the callback is needed,
286  the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
287  */
288 }
289 
297 {
298  /* Prevent unused argument(s) compilation warning */
299  UNUSED(hdac);
300 
301  /* NOTE : This function should not be modified, when the callback is needed,
302  the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
303  */
304 }
305 #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
306  /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
307  /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
308 
323 HAL_StatusTypeDef HAL_DACEx_SelfCalibrate(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
324 {
325  HAL_StatusTypeDef status = HAL_OK;
326 
327  __IO uint32_t tmp;
328  uint32_t trimmingvalue;
329  uint32_t delta;
330 
331  /* store/restore channel configuration structure purpose */
332  uint32_t oldmodeconfiguration;
333 
334  /* Check the parameters */
335  assert_param(IS_DAC_CHANNEL(Channel));
336 
337  /* Check the DAC handle allocation */
338  /* Check if DAC running */
339  if (hdac == NULL)
340  {
341  status = HAL_ERROR;
342  }
343  else if (hdac->State == HAL_DAC_STATE_BUSY)
344  {
345  status = HAL_ERROR;
346  }
347  else
348  {
349  /* Process locked */
350  __HAL_LOCK(hdac);
351 
352  /* Store configuration */
353  oldmodeconfiguration = (hdac->Instance->MCR & (DAC_MCR_MODE1 << (Channel & 0x10UL)));
354 
355  /* Disable the selected DAC channel */
356  CLEAR_BIT((hdac->Instance->CR), (DAC_CR_EN1 << (Channel & 0x10UL)));
357 
358  /* Set mode in MCR for calibration */
359  MODIFY_REG(hdac->Instance->MCR, (DAC_MCR_MODE1 << (Channel & 0x10UL)), 0U);
360 
361  /* Set DAC Channel1 DHR register to the middle value */
362  tmp = (uint32_t)hdac->Instance;
363 
364 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
365  defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
366  defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
367  if(Channel == DAC_CHANNEL_1)
368  {
369  tmp += DAC_DHR12R1_ALIGNMENT(DAC_ALIGN_12B_R);
370  }
371  else
372  {
373  tmp += DAC_DHR12R2_ALIGNMENT(DAC_ALIGN_12B_R);
374  }
375 #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
376  /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
377  /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
378 #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
379  tmp += DAC_DHR12R1_ALIGNMENT(DAC_ALIGN_12B_R);
380 #endif /* STM32L451xx STM32L452xx STM32L462xx */
381  *(__IO uint32_t *) tmp = 0x0800U;
382 
383  /* Enable the selected DAC channel calibration */
384  /* i.e. set DAC_CR_CENx bit */
385  SET_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
386 
387  /* Init trimming counter */
388  /* Medium value */
389  trimmingvalue = 16U;
390  delta = 8U;
391  while (delta != 0U)
392  {
393  /* Set candidate trimming */
394  MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
395 
396  /* tOFFTRIMmax delay x ms as per datasheet (electrical characteristics */
397  /* i.e. minimum time needed between two calibration steps */
398  HAL_Delay(1);
399 
400  if ((hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL))) == (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL)))
401  {
402  /* DAC_SR_CAL_FLAGx is HIGH try higher trimming */
403  trimmingvalue -= delta;
404  }
405  else
406  {
407  /* DAC_SR_CAL_FLAGx is LOW try lower trimming */
408  trimmingvalue += delta;
409  }
410  delta >>= 1U;
411  }
412 
413  /* Still need to check if right calibration is current value or one step below */
414  /* Indeed the first value that causes the DAC_SR_CAL_FLAGx bit to change from 0 to 1 */
415  /* Set candidate trimming */
416  MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
417 
418  /* tOFFTRIMmax delay x ms as per datasheet (electrical characteristics */
419  /* i.e. minimum time needed between two calibration steps */
420  HAL_Delay(1U);
421 
422  if ((hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL))) == 0UL)
423  {
424  /* OPAMP_CSR_OUTCAL is actually one value more */
425  trimmingvalue++;
426  /* Set right trimming */
427  MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
428  }
429 
430  /* Disable the selected DAC channel calibration */
431  /* i.e. clear DAC_CR_CENx bit */
432  CLEAR_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
433 
434  sConfig->DAC_TrimmingValue = trimmingvalue;
435  sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
436 
437  /* Restore configuration */
438  MODIFY_REG(hdac->Instance->MCR, (DAC_MCR_MODE1 << (Channel & 0x10UL)), oldmodeconfiguration);
439 
440  /* Process unlocked */
441  __HAL_UNLOCK(hdac);
442  }
443 
444  return status;
445 }
446 
460 HAL_StatusTypeDef HAL_DACEx_SetUserTrimming(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel,
461  uint32_t NewTrimmingValue)
462 {
463  HAL_StatusTypeDef status = HAL_OK;
464 
465  /* Check the parameters */
466  assert_param(IS_DAC_CHANNEL(Channel));
467  assert_param(IS_DAC_NEWTRIMMINGVALUE(NewTrimmingValue));
468 
469  /* Check the DAC handle allocation */
470  if (hdac == NULL)
471  {
472  status = HAL_ERROR;
473  }
474  else
475  {
476  /* Process locked */
477  __HAL_LOCK(hdac);
478 
479  /* Set new trimming */
480  MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (NewTrimmingValue << (Channel & 0x10UL)));
481 
482  /* Update trimming mode */
483  sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
484  sConfig->DAC_TrimmingValue = NewTrimmingValue;
485 
486  /* Process unlocked */
487  __HAL_UNLOCK(hdac);
488  }
489  return status;
490 }
491 
503 uint32_t HAL_DACEx_GetTrimOffset(DAC_HandleTypeDef *hdac, uint32_t Channel)
504 {
505  /* Check the parameter */
506  assert_param(IS_DAC_CHANNEL(Channel));
507 
508  /* Retrieve trimming */
509  return ((hdac->Instance->CCR & (DAC_CCR_OTRIM1 << (Channel & 0x10UL))) >> (Channel & 0x10UL));
510 }
511 
516 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
517  defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
518  defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
519 
541 {
542  uint32_t tmp = 0U;
543 
544  tmp |= hdac->Instance->DOR1;
545 
546  tmp |= hdac->Instance->DOR2 << 16U;
547 
548  /* Returns the DAC channel data output register value */
549  return tmp;
550 }
551 
556 #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
557  /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
558  /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
559 
564 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
565  defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
566  defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
567 
568 /* Private functions ---------------------------------------------------------*/
581 {
582  DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
583 
584 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
585  hdac->ConvCpltCallbackCh2(hdac);
586 #else
588 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
589 
590  hdac->State = HAL_DAC_STATE_READY;
591 }
592 
600 {
601  DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
602  /* Conversion complete callback */
603 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
604  hdac->ConvHalfCpltCallbackCh2(hdac);
605 #else
607 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
608 }
609 
617 {
618  DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
619 
620  /* Set DAC error code to DMA error */
621  hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
622 
623 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
624  hdac->ErrorCallbackCh2(hdac);
625 #else
627 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
628 
629  hdac->State = HAL_DAC_STATE_READY;
630 }
631 
635 #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
636  /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
637  /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
638 
643 #endif /* DAC1 */
644 
645 #endif /* HAL_DAC_MODULE_ENABLED */
646 
651 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
struct __DAC_HandleTypeDef else typedef struct endif DAC_HandleTypeDef
DAC handle Structure definition.
__weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
Conversion complete callback in non-blocking mode for Channel2.
DMA handle Structure definition.
HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
Enable or disable the selected DAC channel wave generation.
This file contains all the functions prototypes for the HAL module driver.
__weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac)
Conversion half DMA transfer callback in non-blocking mode for Channel2.
DAC Configuration regular Channel structure definition.
void HAL_Delay(uint32_t Delay)
This function provides minimum delay (in milliseconds) based on variable incremented.
void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
DMA error callback.
__HAL_UNLOCK(hrtc)
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
Enable or disable the selected DAC channel wave generation.
return HAL_OK
uint32_t HAL_DACEx_GetTrimOffset(DAC_HandleTypeDef *hdac, uint32_t Channel)
Return the DAC trimming value.
HAL_StatusTypeDef HAL_DACEx_SelfCalibrate(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
Run the self calibration of one DAC channel.
HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
Set the specified data holding register value for dual DAC channel.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
__weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
DMA underrun DAC callback for Channel2.
void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
DMA conversion complete callback.
void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
DMA half transfer complete callback.
uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef *hdac)
Return the last data output value of the selected DAC channel.
HAL_StatusTypeDef HAL_DACEx_SetUserTrimming(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel, uint32_t NewTrimmingValue)
Set the trimming mode and trimming value (user trimming mode applied).
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
__weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
Error DAC callback for Channel2.