1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_hal_dac_ex.c
4   * @author  MCD Application Team
5   * @brief   DAC HAL module driver.
6   *          This file provides firmware functions to manage the extended
7   *          functionalities of the DAC peripheral.
8   *
9   *
10   ******************************************************************************
11   * @attention
12   *
13   * Copyright (c) 2017 STMicroelectronics.
14   * All rights reserved.
15   *
16   * This software is licensed under terms that can be found in the LICENSE file
17   * in the root directory of this software component.
18   * If no LICENSE file comes with this software, it is provided AS-IS.
19   *
20   ******************************************************************************
21   @verbatim
22   ==============================================================================
23                       ##### How to use this driver #####
24   ==============================================================================
25     [..]
26      *** Dual mode IO operation ***
27      ==============================
28       (+) When Dual mode is enabled (i.e. DAC Channel1 and Channel2 are used simultaneously) :
29           Use HAL_DACEx_DualGetValue() to get digital data to be converted and use
30           HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in
31           Channel 1 and Channel 2.
32 
33      *** Signal generation operation ***
34      ===================================
35       (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal.
36       (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal.
37 
38       (+) HAL_DACEx_SelfCalibrate to calibrate one DAC channel.
39       (+) HAL_DACEx_SetUserTrimming to set user trimming value.
40       (+) HAL_DACEx_GetTrimOffset to retrieve trimming value (factory setting
41           after reset, user setting if HAL_DACEx_SetUserTrimming have been used
42           at least one time after reset).
43 
44  @endverbatim
45   ******************************************************************************
46   */
47 
48 
49 /* Includes ------------------------------------------------------------------*/
50 #include "stm32l4xx_hal.h"
51 
52 /** @addtogroup STM32L4xx_HAL_Driver
53   * @{
54   */
55 
56 #ifdef HAL_DAC_MODULE_ENABLED
57 
58 #if defined(DAC1)
59 
60 /** @defgroup DACEx DACEx
61   * @brief DAC Extended HAL module driver
62   * @{
63   */
64 
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 /* Private macro -------------------------------------------------------------*/
68 /* Private variables ---------------------------------------------------------*/
69 /* Private function prototypes -----------------------------------------------*/
70 /* Exported functions --------------------------------------------------------*/
71 
72 /** @defgroup DACEx_Exported_Functions DACEx Exported Functions
73   * @{
74   */
75 
76 /** @defgroup DACEx_Exported_Functions_Group2 IO operation functions
77  *  @brief    Extended IO operation functions
78  *
79 @verbatim
80   ==============================================================================
81                  ##### Extended features functions #####
82   ==============================================================================
83     [..]  This section provides functions allowing to:
84       (+) Start conversion.
85       (+) Stop conversion.
86       (+) Start conversion and enable DMA transfer.
87       (+) Stop conversion and disable DMA transfer.
88       (+) Get result of conversion.
89       (+) Get result of dual mode conversion.
90 
91 @endverbatim
92   * @{
93   */
94 
95 /**
96   * @brief  Enable or disable the selected DAC channel wave generation.
97   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
98   *         the configuration information for the specified DAC.
99   * @param  Channel The selected DAC channel.
100   *          This parameter can be one of the following values:
101   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
102   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
103   * @param  Amplitude Select max triangle amplitude.
104   *          This parameter can be one of the following values:
105   *            @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
106   *            @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
107   *            @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
108   *            @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
109   *            @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
110   *            @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
111   *            @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
112   *            @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
113   *            @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
114   *            @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
115   *            @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
116   *            @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
117   * @retval HAL status
118   */
HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t Amplitude)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 
144 /**
145   * @brief  Enable or disable the selected DAC channel wave generation.
146   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
147   *         the configuration information for the specified DAC.
148   * @param  Channel The selected DAC channel.
149   *          This parameter can be one of the following values:
150   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
151   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
152   * @param  Amplitude Unmask DAC channel LFSR for noise wave generation.
153   *          This parameter can be one of the following values:
154   *            @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
155   *            @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
156   *            @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
157   *            @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
158   *            @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
159   *            @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
160   *            @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
161   *            @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
162   *            @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
163   *            @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
164   *            @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
165   *            @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
166   * @retval HAL status
167   */
HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t Amplitude)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 (STM32L4P5xx) || defined (STM32L4Q5xx) || \
196     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
197 
198 /**
199   * @brief  Set the specified data holding register value for dual DAC channel.
200   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
201   *               the configuration information for the specified DAC.
202   * @param  Alignment Specifies the data alignment for dual channel DAC.
203   *          This parameter can be one of the following values:
204   *            DAC_ALIGN_8B_R: 8bit right data alignment selected
205   *            DAC_ALIGN_12B_L: 12bit left data alignment selected
206   *            DAC_ALIGN_12B_R: 12bit right data alignment selected
207   * @param  Data1 Data for DAC Channel1 to be loaded in the selected data holding register.
208   * @param  Data2 Data for DAC Channel2 to be loaded in the selected data  holding register.
209   * @note   In dual mode, a unique register access is required to write in both
210   *          DAC channels at the same time.
211   * @retval HAL status
212   */
HAL_DACEx_DualSetValue(DAC_HandleTypeDef * hdac,uint32_t Alignment,uint32_t Data1,uint32_t Data2)213 HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
214 {
215   uint32_t data;
216   uint32_t tmp;
217 
218   /* Check the parameters */
219   assert_param(IS_DAC_ALIGN(Alignment));
220   assert_param(IS_DAC_DATA(Data1));
221   assert_param(IS_DAC_DATA(Data2));
222 
223   /* Calculate and set dual DAC data holding register value */
224   if (Alignment == DAC_ALIGN_8B_R)
225   {
226     data = ((uint32_t)Data2 << 8U) | Data1;
227   }
228   else
229   {
230     data = ((uint32_t)Data2 << 16U) | Data1;
231   }
232 
233   tmp = (uint32_t)hdac->Instance;
234   tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
235 
236   /* Set the dual DAC selected data holding register */
237   *(__IO uint32_t *)tmp = data;
238 
239   /* Return function status */
240   return HAL_OK;
241 }
242 
243 /**
244   * @brief  Conversion complete callback in non-blocking mode for Channel2.
245   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
246   *         the configuration information for the specified DAC.
247   * @retval None
248   */
HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef * hdac)249 __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
250 {
251   /* Prevent unused argument(s) compilation warning */
252   UNUSED(hdac);
253 
254   /* NOTE : This function should not be modified, when the callback is needed,
255             the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
256    */
257 }
258 
259 /**
260   * @brief  Conversion half DMA transfer callback in non-blocking mode for Channel2.
261   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
262   *         the configuration information for the specified DAC.
263   * @retval None
264   */
HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef * hdac)265 __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac)
266 {
267   /* Prevent unused argument(s) compilation warning */
268   UNUSED(hdac);
269 
270   /* NOTE : This function should not be modified, when the callback is needed,
271             the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
272    */
273 }
274 
275 /**
276   * @brief  Error DAC callback for Channel2.
277   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
278   *         the configuration information for the specified DAC.
279   * @retval None
280   */
HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef * hdac)281 __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
282 {
283   /* Prevent unused argument(s) compilation warning */
284   UNUSED(hdac);
285 
286   /* NOTE : This function should not be modified, when the callback is needed,
287             the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
288    */
289 }
290 
291 /**
292   * @brief  DMA underrun DAC callback for Channel2.
293   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
294   *         the configuration information for the specified DAC.
295   * @retval None
296   */
HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef * hdac)297 __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
298 {
299   /* Prevent unused argument(s) compilation warning */
300   UNUSED(hdac);
301 
302   /* NOTE : This function should not be modified, when the callback is needed,
303             the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
304    */
305 }
306 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
307         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
308         /* STM32L4P5xx STM32L4Q5xx                                                             */
309         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
310 
311 /**
312   * @brief  Run the self calibration of one DAC channel.
313   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
314   *         the configuration information for the specified DAC.
315   * @param  sConfig DAC channel configuration structure.
316   * @param  Channel The selected DAC channel.
317   *          This parameter can be one of the following values:
318   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
319   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
320   * @retval Updates DAC_TrimmingValue. , DAC_UserTrimming set to DAC_UserTrimming
321   * @retval HAL status
322   * @note   Calibration runs about 7 ms.
323   */
324 
HAL_DACEx_SelfCalibrate(DAC_HandleTypeDef * hdac,DAC_ChannelConfTypeDef * sConfig,uint32_t Channel)325 HAL_StatusTypeDef HAL_DACEx_SelfCalibrate(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
326 {
327   HAL_StatusTypeDef status = HAL_OK;
328 
329   __IO uint32_t tmp;
330   uint32_t trimmingvalue;
331   uint32_t delta;
332 
333   /* store/restore channel configuration structure purpose */
334   uint32_t oldmodeconfiguration;
335 
336   /* Check the parameters */
337   assert_param(IS_DAC_CHANNEL(Channel));
338 
339  /* Check the DAC handle allocation */
340  /* Check if DAC running */
341   if (hdac == NULL)
342   {
343     status = HAL_ERROR;
344   }
345   else if (hdac->State == HAL_DAC_STATE_BUSY)
346   {
347     status = HAL_ERROR;
348   }
349   else
350   {
351     /* Process locked */
352     __HAL_LOCK(hdac);
353 
354     /* Store configuration */
355     oldmodeconfiguration = (hdac->Instance->MCR & (DAC_MCR_MODE1 << (Channel & 0x10UL)));
356 
357     /* Disable the selected DAC channel */
358     CLEAR_BIT((hdac->Instance->CR), (DAC_CR_EN1 << (Channel & 0x10UL)));
359 
360     /* Set mode in MCR  for calibration */
361     MODIFY_REG(hdac->Instance->MCR, (DAC_MCR_MODE1 << (Channel & 0x10UL)), 0U);
362 
363     /* Set DAC Channel1 DHR register to the middle value */
364     tmp = (uint32_t)hdac->Instance;
365 
366 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
367     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
368     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
369     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
370     if(Channel == DAC_CHANNEL_1)
371     {
372       tmp += DAC_DHR12R1_ALIGNMENT(DAC_ALIGN_12B_R);
373     }
374     else
375     {
376       tmp += DAC_DHR12R2_ALIGNMENT(DAC_ALIGN_12B_R);
377     }
378 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
379         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
380         /* STM32L4P5xx STM32L4Q5xx                                                             */
381         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
382 #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
383     tmp += DAC_DHR12R1_ALIGNMENT(DAC_ALIGN_12B_R);
384 #endif /* STM32L451xx STM32L452xx STM32L462xx */
385     *(__IO uint32_t *) tmp = 0x0800U;
386 
387     /* Enable the selected DAC channel calibration */
388     /* i.e. set DAC_CR_CENx bit */
389     SET_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
390 
391     /* Init trimming counter */
392     /* Medium value */
393     trimmingvalue = 16U;
394     delta = 8U;
395     while (delta != 0U)
396     {
397       /* Set candidate trimming */
398       MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
399 
400       /* tOFFTRIMmax delay x ms as per datasheet (electrical characteristics */
401       /* i.e. minimum time needed between two calibration steps */
402       HAL_Delay(1);
403 
404       if ((hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL))) == (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL)))
405       {
406         /* DAC_SR_CAL_FLAGx is HIGH try higher trimming */
407         trimmingvalue -= delta;
408       }
409       else
410       {
411         /* DAC_SR_CAL_FLAGx is LOW try lower trimming */
412         trimmingvalue += delta;
413       }
414       delta >>= 1U;
415     }
416 
417     /* Still need to check if right calibration is current value or one step below */
418     /* Indeed the first value that causes the DAC_SR_CAL_FLAGx bit to change from 0 to 1  */
419     /* Set candidate trimming */
420     MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
421 
422     /* tOFFTRIMmax delay x ms as per datasheet (electrical characteristics */
423     /* i.e. minimum time needed between two calibration steps */
424     HAL_Delay(1U);
425 
426     if ((hdac->Instance->SR & (DAC_SR_CAL_FLAG1 << (Channel & 0x10UL))) == 0UL)
427     {
428       /* OPAMP_CSR_OUTCAL is actually one value more */
429       trimmingvalue++;
430       /* Set right trimming */
431       MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (trimmingvalue << (Channel & 0x10UL)));
432     }
433 
434     /* Disable the selected DAC channel calibration */
435     /* i.e. clear DAC_CR_CENx bit */
436     CLEAR_BIT((hdac->Instance->CR), (DAC_CR_CEN1 << (Channel & 0x10UL)));
437 
438     sConfig->DAC_TrimmingValue = trimmingvalue;
439     sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
440 
441     /* Restore configuration */
442     MODIFY_REG(hdac->Instance->MCR, (DAC_MCR_MODE1 << (Channel & 0x10UL)), oldmodeconfiguration);
443 
444     /* Process unlocked */
445     __HAL_UNLOCK(hdac);
446   }
447 
448   return status;
449 }
450 
451 /**
452   * @brief  Set the trimming mode and trimming value (user trimming mode applied).
453   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
454   *         the configuration information for the specified DAC.
455   * @param  sConfig DAC configuration structure updated with new DAC trimming value.
456   * @param  Channel The selected DAC channel.
457   *          This parameter can be one of the following values:
458   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
459   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
460   * @param  NewTrimmingValue DAC new trimming value
461   * @retval HAL status
462   */
463 
HAL_DACEx_SetUserTrimming(DAC_HandleTypeDef * hdac,DAC_ChannelConfTypeDef * sConfig,uint32_t Channel,uint32_t NewTrimmingValue)464 HAL_StatusTypeDef HAL_DACEx_SetUserTrimming(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel,
465                                             uint32_t NewTrimmingValue)
466 {
467   HAL_StatusTypeDef status = HAL_OK;
468 
469   /* Check the parameters */
470   assert_param(IS_DAC_CHANNEL(Channel));
471   assert_param(IS_DAC_NEWTRIMMINGVALUE(NewTrimmingValue));
472 
473  /* Check the DAC handle allocation */
474   if (hdac == NULL)
475   {
476     status = HAL_ERROR;
477   }
478   else
479   {
480     /* Process locked */
481     __HAL_LOCK(hdac);
482 
483     /* Set new trimming */
484     MODIFY_REG(hdac->Instance->CCR, (DAC_CCR_OTRIM1 << (Channel & 0x10UL)), (NewTrimmingValue << (Channel & 0x10UL)));
485 
486     /* Update trimming mode */
487     sConfig->DAC_UserTrimming = DAC_TRIMMING_USER;
488     sConfig->DAC_TrimmingValue = NewTrimmingValue;
489 
490     /* Process unlocked */
491     __HAL_UNLOCK(hdac);
492   }
493   return status;
494 }
495 
496 /**
497   * @brief  Return the DAC trimming value.
498   * @param  hdac DAC handle
499   * @param  Channel The selected DAC channel.
500   *          This parameter can be one of the following values:
501   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
502   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
503   * @retval Trimming value : range: 0->31
504   *
505  */
506 
HAL_DACEx_GetTrimOffset(DAC_HandleTypeDef * hdac,uint32_t Channel)507 uint32_t HAL_DACEx_GetTrimOffset(DAC_HandleTypeDef *hdac, uint32_t Channel)
508 {
509     /* Check the parameter */
510     assert_param(IS_DAC_CHANNEL(Channel));
511 
512     /* Retrieve trimming  */
513   return ((hdac->Instance->CCR & (DAC_CCR_OTRIM1 << (Channel & 0x10UL))) >> (Channel & 0x10UL));
514 }
515 
516 /**
517   * @}
518   */
519 
520 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
521     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
522     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
523     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
524 
525 /** @defgroup DACEx_Exported_Functions_Group3 Peripheral Control functions
526  *  @brief    Extended Peripheral Control functions
527  *
528 @verbatim
529   ==============================================================================
530              ##### Peripheral Control functions #####
531   ==============================================================================
532     [..]  This section provides functions allowing to:
533       (+) Set the specified data holding register value for DAC channel.
534 
535 @endverbatim
536   * @{
537   */
538 
539 /**
540   * @brief  Return the last data output value of the selected DAC channel.
541   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
542   *         the configuration information for the specified DAC.
543   * @retval The selected DAC channel data output value.
544   */
HAL_DACEx_DualGetValue(DAC_HandleTypeDef * hdac)545 uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef *hdac)
546 {
547   uint32_t tmp = 0U;
548 
549   tmp |= hdac->Instance->DOR1;
550 
551   tmp |= hdac->Instance->DOR2 << 16U;
552 
553   /* Returns the DAC channel data output register value */
554   return tmp;
555 }
556 
557 /**
558   * @}
559   */
560 
561 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
562         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
563         /* STM32L4P5xx STM32L4Q5xx                                                             */
564         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
565 
566 /**
567   * @}
568   */
569 
570 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
571     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
572     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
573     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
574 
575 /* Private functions ---------------------------------------------------------*/
576 /** @defgroup DACEx_Private_Functions DACEx private functions
577  *  @brief    Extended private functions
578    * @{
579   */
580 
581 /**
582   * @brief  DMA conversion complete callback.
583   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
584   *                the configuration information for the specified DMA module.
585   * @retval None
586   */
DAC_DMAConvCpltCh2(DMA_HandleTypeDef * hdma)587 void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
588 {
589   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
590 
591 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
592   hdac->ConvCpltCallbackCh2(hdac);
593 #else
594   HAL_DACEx_ConvCpltCallbackCh2(hdac);
595 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
596 
597   hdac->State = HAL_DAC_STATE_READY;
598 }
599 
600 /**
601   * @brief  DMA half transfer complete callback.
602   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
603   *                the configuration information for the specified DMA module.
604   * @retval None
605   */
DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef * hdma)606 void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
607 {
608   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
609   /* Conversion complete callback */
610 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
611   hdac->ConvHalfCpltCallbackCh2(hdac);
612 #else
613   HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
614 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
615 }
616 
617 /**
618   * @brief  DMA error callback.
619   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
620   *                the configuration information for the specified DMA module.
621   * @retval None
622   */
DAC_DMAErrorCh2(DMA_HandleTypeDef * hdma)623 void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
624 {
625   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
626 
627   /* Set DAC error code to DMA error */
628   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
629 
630 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
631   hdac->ErrorCallbackCh2(hdac);
632 #else
633   HAL_DACEx_ErrorCallbackCh2(hdac);
634 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
635 
636   hdac->State = HAL_DAC_STATE_READY;
637 }
638 
639 /**
640   * @}
641   */
642 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
643         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
644         /* STM32L4P5xx STM32L4Q5xx                                                             */
645         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
646 
647 /**
648   * @}
649   */
650 
651 #endif /* DAC1 */
652 
653 #endif /* HAL_DAC_MODULE_ENABLED */
654 
655 /**
656   * @}
657   */
658 
659