1 /**
2   ******************************************************************************
3   * @file    stm32wlxx_hal_dac.c
4   * @author  MCD Application Team
5   * @brief   DAC HAL module driver.
6   *         This file provides firmware functions to manage the following
7   *         functionalities of the Digital to Analog Converter (DAC) peripheral:
8   *           + Initialization and de-initialization functions
9   *           + IO operation functions
10   *           + Peripheral Control functions
11   *           + Peripheral State and Errors functions
12   *
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2020 STMicroelectronics.
18   * All rights reserved.
19   *
20   * This software is licensed under terms that can be found in the LICENSE file
21   * in the root directory of this software component.
22   * If no LICENSE file comes with this software, it is provided AS-IS.
23   *
24   ******************************************************************************
25   @verbatim
26   ==============================================================================
27                       ##### DAC Peripheral features #####
28   ==============================================================================
29     [..]
30       *** DAC Channels ***
31       ====================
32     [..]
33     STM32WL devices integrate one 12-bit Digital Analog Converter
34 
35     The single converters (i.e. channel1)
36     can be used:
37       (#) DAC channel1 with DAC_OUT1 (PA10) as output or connected to on-chip
38           peripherals (ex. timers, ADC).
39 
40       *** DAC Triggers ***
41       ====================
42     [..]
43     Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
44     and DAC_OUT1 is available once writing to DHRx register.
45     [..]
46     Digital to Analog conversion can be triggered by:
47       (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
48           The used pin (GPIOx_PIN_9) must be configured in input mode.
49 
50       (#) Timers TRGO: TIM1, TIM2, LPTIM1, LPTIM2, LPTIM3
51           (DAC_TRIGGER_T1_TRGO, DAC_TRIGGER_T2_TRGO...)
52 
53       (#) Software using DAC_TRIGGER_SOFTWARE
54 
55       *** DAC Buffer mode feature ***
56       ===============================
57       [..]
58       Each DAC channel integrates an output buffer that can be used to
59       reduce the output impedance, and to drive external loads directly
60       without having to add an external operational amplifier.
61       To enable, the output buffer use
62       sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
63       [..]
64       (@) Refer to the device datasheet for more details about output
65           impedance value with and without output buffer.
66 
67       *** GPIO configurations guidelines ***
68       =====================
69       [..]
70       When a DAC channel is used (ex channel1 on PA4) and the other is not
71       (ex channel2 on PA5 is configured in Analog and disabled).
72       Channel1 may disturb channel2 as coupling effect.
73       Note that there is no coupling on channel2 as soon as channel2 is turned on.
74       Coupling on adjacent channel could be avoided as follows:
75       when unused PA5 is configured as INPUT PULL-UP or DOWN.
76       PA5 is configured in ANALOG just before it is turned on.
77 
78       *** DAC Sample and Hold feature ***
79       ========================
80       [..]
81       For each converter, 2 modes are supported: normal mode and
82       "sample and hold" mode (i.e. low power mode).
83       In the sample and hold mode, the DAC core converts data, then holds the
84       converted voltage on a capacitor. When not converting, the DAC cores and
85       buffer are completely turned off between samples and the DAC output is
86       tri-stated, therefore  reducing the overall power consumption. A new
87       stabilization period is needed before each new conversion.
88 
89       The sample and hold allow setting internal or external voltage @
90       low power consumption cost (output value can be at any given rate either
91       by CPU or DMA).
92 
93       The Sample and hold block and registers uses either LSI & run in
94       several power modes: run mode, sleep mode, low power run, low power sleep
95       mode & stop1 mode.
96 
97       Low power stop1 mode allows only static conversion.
98 
99       To enable Sample and Hold mode
100       Enable LSI using HAL_RCC_OscConfig with RCC_OSCILLATORTYPE_LSI &
101       RCC_LSI_ON parameters.
102 
103       Use DAC_InitStructure.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_ENABLE;
104          & DAC_ChannelConfTypeDef.DAC_SampleAndHoldConfig.DAC_SampleTime,
105            DAC_HoldTime & DAC_RefreshTime;
106 
107        *** DAC calibration feature ***
108        ===================================
109       [..]
110        (#)  The 2 converters (channel1 & channel2) provide calibration capabilities.
111        (++) Calibration aims at correcting some offset of output buffer.
112        (++) The DAC uses either factory calibration settings OR user defined
113            calibration (trimming) settings (i.e. trimming mode).
114        (++) The user defined settings can be figured out using self calibration
115            handled by HAL_DACEx_SelfCalibrate.
116        (++) HAL_DACEx_SelfCalibrate:
117        (+++) Runs automatically the calibration.
118        (+++) Enables the user trimming mode
119        (+++) Updates a structure with trimming values with fresh calibration
120             results.
121             The user may store the calibration results for larger
122             (ex monitoring the trimming as a function of temperature
123             for instance)
124 
125        *** DAC wave generation feature ***
126        ===================================
127        [..]
128        Both DAC channels can be used to generate
129          (#) Noise wave
130          (#) Triangle wave
131 
132        *** DAC data format ***
133        =======================
134        [..]
135        The DAC data format can be:
136          (#) 8-bit right alignment using DAC_ALIGN_8B_R
137          (#) 12-bit left alignment using DAC_ALIGN_12B_L
138          (#) 12-bit right alignment using DAC_ALIGN_12B_R
139 
140        *** DAC data value to voltage correspondence ***
141        ================================================
142        [..]
143        The analog output voltage on each DAC channel pin is determined
144        by the following equation:
145        [..]
146        DAC_OUTx = VREF+ * DOR / 4095
147        (+) with  DOR is the Data Output Register
148        [..]
149           VREF+ is the input voltage reference (refer to the device datasheet)
150        [..]
151         e.g. To set DAC_OUT1 to 0.7V, use
152        (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
153 
154        *** DMA requests ***
155        =====================
156        [..]
157        A DMA request can be generated when an external trigger (but not a software trigger)
158        occurs if DMA1 requests are enabled using HAL_DAC_Start_DMA().
159        DMA requests are mapped as following:
160       (#) DAC channel1: mapped on DMA request 6 (can be any DMA channel)
161 
162      [..]
163     (@) For Dual mode and specific signal (Triangle and noise) generation please
164         refer to Extended Features Driver description
165 
166                       ##### How to use this driver #####
167   ==============================================================================
168     [..]
169       (+) DAC APB clock must be enabled to get write access to DAC
170           registers using HAL_DAC_Init()
171       (+) Configure DAC_OUTx (DAC_OUT1: PA10) in analog mode.
172       (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
173       (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA() functions.
174 
175      *** Calibration mode IO operation ***
176      ======================================
177      [..]
178        (+) Retrieve the factory trimming (calibration settings) using HAL_DACEx_GetTrimOffset()
179        (+) Run the calibration using HAL_DACEx_SelfCalibrate()
180        (+) Update the trimming while DAC running using HAL_DACEx_SetUserTrimming()
181 
182      *** Polling mode IO operation ***
183      =================================
184      [..]
185        (+) Start the DAC peripheral using HAL_DAC_Start()
186        (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
187        (+) Stop the DAC peripheral using HAL_DAC_Stop()
188 
189      *** DMA mode IO operation ***
190      ==============================
191      [..]
192        (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
193            of data to be transferred at each end of conversion
194            First issued trigger will start the conversion of the value previously set by HAL_DAC_SetValue().
195        (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1()
196            function is executed and user can add his own code by customization of function pointer
197            HAL_DAC_ConvHalfCpltCallbackCh1()
198        (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()
199            function is executed and user can add his own code by customization of function pointer
200            HAL_DAC_ConvCpltCallbackCh1()
201        (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
202             add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
203        (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
204            HAL_DAC_DMAUnderrunCallbackCh1()
205            function is executed and user can add his own code by customization of function pointer
206            HAL_DAC_DMAUnderrunCallbackCh1() and
207            add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1()
208        (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
209 
210     *** Callback registration ***
211     =============================================
212     [..]
213       The compilation define  USE_HAL_DAC_REGISTER_CALLBACKS when set to 1
214       allows the user to configure dynamically the driver callbacks.
215 
216     Use Functions @ref HAL_DAC_RegisterCallback() to register a user callback,
217       it allows to register following callbacks:
218       (+) ConvCpltCallbackCh1     : callback when a half transfer is completed on Ch1.
219       (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
220       (+) ErrorCallbackCh1        : callback when an error occurs on Ch1.
221       (+) DMAUnderrunCallbackCh1  : callback when an underrun error occurs on Ch1.
222       (+) MspInitCallback         : DAC MspInit.
223       (+) MspDeInitCallback       : DAC MspdeInit.
224       This function takes as parameters the HAL peripheral handle, the Callback ID
225       and a pointer to the user callback function.
226 
227     Use function @ref HAL_DAC_UnRegisterCallback() to reset a callback to the default
228       weak (surcharged) function. It allows to reset following callbacks:
229       (+) ConvCpltCallbackCh1     : callback when a half transfer is completed on Ch1.
230       (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
231       (+) ErrorCallbackCh1        : callback when an error occurs on Ch1.
232       (+) DMAUnderrunCallbackCh1  : callback when an underrun error occurs on Ch1.
233       (+) MspInitCallback         : DAC MspInit.
234       (+) MspDeInitCallback       : DAC MspdeInit.
235       (+) All Callbacks
236       This function) takes as parameters the HAL peripheral handle and the Callback ID.
237 
238       By default, after the @ref HAL_DAC_Init and if the state is HAL_DAC_STATE_RESET
239       all callbacks are reset to the corresponding legacy weak (surcharged) functions.
240       Exception done for MspInit and MspDeInit callbacks that are respectively
241       reset to the legacy weak (surcharged) functions in the @ref HAL_DAC_Init
242       and @ref  HAL_DAC_DeInit only when these callbacks are null (not registered beforehand).
243       If not, MspInit or MspDeInit are not null, the @ref HAL_DAC_Init and @ref HAL_DAC_DeInit
244       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
245 
246       Callbacks can be registered/unregistered in READY state only.
247       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
248       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
249       during the Init/DeInit.
250       In that case first register the MspInit/MspDeInit user callbacks
251       using @ref HAL_DAC_RegisterCallback before calling @ref HAL_DAC_DeInit
252       or @ref HAL_DAC_Init function.
253 
254       When The compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or
255       not defined, the callback registering feature is not available
256       and weak (surcharged) callbacks are used.
257 
258      *** DAC HAL driver macros list ***
259      =============================================
260      [..]
261        Below the list of most used macros in DAC HAL driver.
262 
263       (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
264       (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
265       (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
266       (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
267 
268      [..]
269       (@) You can refer to the DAC HAL driver header file for more useful macros
270 
271 @endverbatim
272   ******************************************************************************
273   */
274 
275 /* Includes ------------------------------------------------------------------*/
276 #include "stm32wlxx_hal.h"
277 
278 /** @addtogroup STM32WLxx_HAL_Driver
279   * @{
280   */
281 
282 #ifdef HAL_DAC_MODULE_ENABLED
283 #if defined(DAC)
284 
285 /** @defgroup DAC DAC
286   * @brief DAC driver modules
287   * @{
288   */
289 
290 /* Private typedef -----------------------------------------------------------*/
291 /* Private define ------------------------------------------------------------*/
292 /* Private constants ---------------------------------------------------------*/
293 /** @addtogroup DAC_Private_Constants DAC Private Constants
294   * @{
295   */
296 #define TIMEOUT_DAC_CALIBCONFIG        1U         /* 1   ms        */
297 
298 /**
299   * @}
300   */
301 
302 /* Private macro -------------------------------------------------------------*/
303 /* Private variables ---------------------------------------------------------*/
304 /* Private function prototypes -----------------------------------------------*/
305 /* Exported functions -------------------------------------------------------*/
306 
307 /** @defgroup DAC_Exported_Functions DAC Exported Functions
308   * @{
309   */
310 
311 /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
312   *  @brief    Initialization and Configuration functions
313   *
314 @verbatim
315   ==============================================================================
316               ##### Initialization and de-initialization functions #####
317   ==============================================================================
318     [..]  This section provides functions allowing to:
319       (+) Initialize and configure the DAC.
320       (+) De-initialize the DAC.
321 
322 @endverbatim
323   * @{
324   */
325 
326 /**
327   * @brief  Initialize the DAC peripheral according to the specified parameters
328   *         in the DAC_InitStruct and initialize the associated handle.
329   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
330   *         the configuration information for the specified DAC.
331   * @retval HAL status
332   */
HAL_DAC_Init(DAC_HandleTypeDef * hdac)333 HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef *hdac)
334 {
335   /* Check DAC handle */
336   if (hdac == NULL)
337   {
338     return HAL_ERROR;
339   }
340   /* Check the parameters */
341   assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
342 
343   if (hdac->State == HAL_DAC_STATE_RESET)
344   {
345 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
346     /* Init the DAC Callback settings */
347     hdac->ConvCpltCallbackCh1           = HAL_DAC_ConvCpltCallbackCh1;
348     hdac->ConvHalfCpltCallbackCh1       = HAL_DAC_ConvHalfCpltCallbackCh1;
349     hdac->ErrorCallbackCh1              = HAL_DAC_ErrorCallbackCh1;
350     hdac->DMAUnderrunCallbackCh1        = HAL_DAC_DMAUnderrunCallbackCh1;
351     if (hdac->MspInitCallback == NULL)
352     {
353       hdac->MspInitCallback             = HAL_DAC_MspInit;
354     }
355 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
356 
357     /* Allocate lock resource and initialize it */
358     hdac->Lock = HAL_UNLOCKED;
359 
360 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
361     /* Init the low level hardware */
362     hdac->MspInitCallback(hdac);
363 #else
364     /* Init the low level hardware */
365     HAL_DAC_MspInit(hdac);
366 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
367   }
368 
369   /* Initialize the DAC state*/
370   hdac->State = HAL_DAC_STATE_BUSY;
371 
372   /* Set DAC error code to none */
373   hdac->ErrorCode = HAL_DAC_ERROR_NONE;
374 
375   /* Initialize the DAC state*/
376   hdac->State = HAL_DAC_STATE_READY;
377 
378   /* Return function status */
379   return HAL_OK;
380 }
381 
382 /**
383   * @brief  Deinitialize the DAC peripheral registers to their default reset values.
384   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
385   *         the configuration information for the specified DAC.
386   * @retval HAL status
387   */
HAL_DAC_DeInit(DAC_HandleTypeDef * hdac)388 HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef *hdac)
389 {
390   /* Check DAC handle */
391   if (hdac == NULL)
392   {
393     return HAL_ERROR;
394   }
395 
396   /* Check the parameters */
397   assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
398 
399   /* Change DAC state */
400   hdac->State = HAL_DAC_STATE_BUSY;
401 
402 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
403   if (hdac->MspDeInitCallback == NULL)
404   {
405     hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
406   }
407   /* DeInit the low level hardware */
408   hdac->MspDeInitCallback(hdac);
409 #else
410   /* DeInit the low level hardware */
411   HAL_DAC_MspDeInit(hdac);
412 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
413 
414   /* Set DAC error code to none */
415   hdac->ErrorCode = HAL_DAC_ERROR_NONE;
416 
417   /* Change DAC state */
418   hdac->State = HAL_DAC_STATE_RESET;
419 
420   /* Release Lock */
421   __HAL_UNLOCK(hdac);
422 
423   /* Return function status */
424   return HAL_OK;
425 }
426 
427 /**
428   * @brief  Initialize the DAC MSP.
429   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
430   *         the configuration information for the specified DAC.
431   * @retval None
432   */
HAL_DAC_MspInit(DAC_HandleTypeDef * hdac)433 __weak void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
434 {
435   /* Prevent unused argument(s) compilation warning */
436   UNUSED(hdac);
437 
438   /* NOTE : This function should not be modified, when the callback is needed,
439             the HAL_DAC_MspInit could be implemented in the user file
440    */
441 }
442 
443 /**
444   * @brief  DeInitialize the DAC MSP.
445   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
446   *         the configuration information for the specified DAC.
447   * @retval None
448   */
HAL_DAC_MspDeInit(DAC_HandleTypeDef * hdac)449 __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
450 {
451   /* Prevent unused argument(s) compilation warning */
452   UNUSED(hdac);
453 
454   /* NOTE : This function should not be modified, when the callback is needed,
455             the HAL_DAC_MspDeInit could be implemented in the user file
456    */
457 }
458 
459 /**
460   * @}
461   */
462 
463 /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
464   *  @brief    IO operation functions
465   *
466 @verbatim
467   ==============================================================================
468              ##### IO operation functions #####
469   ==============================================================================
470     [..]  This section provides functions allowing to:
471       (+) Start conversion.
472       (+) Stop conversion.
473       (+) Start conversion and enable DMA transfer.
474       (+) Stop conversion and disable DMA transfer.
475       (+) Get result of conversion.
476 
477 @endverbatim
478   * @{
479   */
480 
481 /**
482   * @brief  Enables DAC and starts conversion of channel.
483   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
484   *         the configuration information for the specified DAC.
485   * @param  Channel The selected DAC channel.
486   *          This parameter can be one of the following values:
487   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
488   * @retval HAL status
489   */
HAL_DAC_Start(DAC_HandleTypeDef * hdac,uint32_t Channel)490 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef *hdac, uint32_t Channel)
491 {
492   /* Check the parameters */
493   assert_param(IS_DAC_CHANNEL(Channel));
494 
495   /* Process locked */
496   __HAL_LOCK(hdac);
497 
498   /* Change DAC state */
499   hdac->State = HAL_DAC_STATE_BUSY;
500 
501   /* Enable the Peripheral */
502   __HAL_DAC_ENABLE(hdac, Channel);
503 
504   /* Check if software trigger enabled */
505   if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
506   {
507     /* Enable the selected DAC software conversion */
508     SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
509   }
510 
511   /* Change DAC state */
512   hdac->State = HAL_DAC_STATE_READY;
513 
514   /* Process unlocked */
515   __HAL_UNLOCK(hdac);
516 
517   /* Return function status */
518   return HAL_OK;
519 }
520 
521 /**
522   * @brief  Disables DAC and stop conversion of channel.
523   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
524   *         the configuration information for the specified DAC.
525   * @param  Channel The selected DAC channel.
526   *          This parameter can be one of the following values:
527   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
528   * @retval HAL status
529   */
HAL_DAC_Stop(DAC_HandleTypeDef * hdac,uint32_t Channel)530 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef *hdac, uint32_t Channel)
531 {
532   /* Check the parameters */
533   assert_param(IS_DAC_CHANNEL(Channel));
534 
535   /* Disable the Peripheral */
536   __HAL_DAC_DISABLE(hdac, Channel);
537 
538   /* Change DAC state */
539   hdac->State = HAL_DAC_STATE_READY;
540 
541   /* Return function status */
542   return HAL_OK;
543 }
544 
545 /**
546   * @brief  Enables DAC and starts conversion of channel.
547   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
548   *         the configuration information for the specified DAC.
549   * @param  Channel The selected DAC channel.
550   *          This parameter can be one of the following values:
551   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
552   * @param  pData The source Buffer address.
553   * @param  Length The length of data to be transferred from memory to DAC peripheral
554   * @param  Alignment Specifies the data alignment for DAC channel.
555   *          This parameter can be one of the following values:
556   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
557   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
558   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
559   * @retval HAL status
560   */
HAL_DAC_Start_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel,const uint32_t * pData,uint32_t Length,uint32_t Alignment)561 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, const uint32_t *pData, uint32_t Length,
562                                     uint32_t Alignment)
563 {
564   HAL_StatusTypeDef status;
565   uint32_t tmpreg = 0U;
566 
567   /* Check the parameters */
568   assert_param(IS_DAC_CHANNEL(Channel));
569   assert_param(IS_DAC_ALIGN(Alignment));
570 
571   /* Process locked */
572   __HAL_LOCK(hdac);
573 
574   /* Change DAC state */
575   hdac->State = HAL_DAC_STATE_BUSY;
576 
577   if (Channel == DAC_CHANNEL_1)
578   {
579     /* Set the DMA transfer complete callback for channel1 */
580     hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
581 
582     /* Set the DMA half transfer complete callback for channel1 */
583     hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
584 
585     /* Set the DMA error callback for channel1 */
586     hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
587 
588     /* Enable the selected DAC channel1 DMA request */
589     SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
590 
591     /* Case of use of channel 1 */
592     switch (Alignment)
593     {
594       case DAC_ALIGN_12B_R:
595         /* Get DHR12R1 address */
596         tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
597         break;
598       case DAC_ALIGN_12B_L:
599         /* Get DHR12L1 address */
600         tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
601         break;
602       default: /* case DAC_ALIGN_8B_R */
603         /* Get DHR8R1 address */
604         tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
605         break;
606     }
607   }
608 
609   /* Enable the DAC DMA underrun interrupt */
610   __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
611 
612   /* Enable the DMA channel */
613   status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
614 
615   /* Process Unlocked */
616   __HAL_UNLOCK(hdac);
617 
618   if (status == HAL_OK)
619   {
620     /* Enable the Peripheral */
621     __HAL_DAC_ENABLE(hdac, Channel);
622   }
623   else
624   {
625     hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
626   }
627 
628   /* Return function status */
629   return status;
630 }
631 
632 /**
633   * @brief  Disables DAC and stop conversion of channel.
634   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
635   *         the configuration information for the specified DAC.
636   * @param  Channel The selected DAC channel.
637   *          This parameter can be one of the following values:
638   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
639   * @retval HAL status
640   */
HAL_DAC_Stop_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel)641 HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel)
642 {
643   /* Check the parameters */
644   assert_param(IS_DAC_CHANNEL(Channel));
645 
646   /* Disable the selected DAC channel DMA request */
647   hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << (Channel & 0x10UL));
648 
649   /* Disable the Peripheral */
650   __HAL_DAC_DISABLE(hdac, Channel);
651 
652   /* Disable the DMA channel */
653 
654   /* Disable the DMA channel */
655   (void)HAL_DMA_Abort(hdac->DMA_Handle1);
656 
657   /* Disable the DAC DMA underrun interrupt */
658   __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
659 
660   /* Change DAC state */
661   hdac->State = HAL_DAC_STATE_READY;
662 
663   /* Return function status */
664   return HAL_OK;
665 }
666 
667 /**
668   * @brief  Handles DAC interrupt request
669   *         This function uses the interruption of DMA
670   *         underrun.
671   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
672   *         the configuration information for the specified DAC.
673   * @retval None
674   */
HAL_DAC_IRQHandler(DAC_HandleTypeDef * hdac)675 void HAL_DAC_IRQHandler(DAC_HandleTypeDef *hdac)
676 {
677   uint32_t itsource = hdac->Instance->CR;
678   uint32_t itflag   = hdac->Instance->SR;
679 
680   if ((itsource & DAC_IT_DMAUDR1) == DAC_IT_DMAUDR1)
681   {
682     /* Check underrun flag of DAC channel 1 */
683     if ((itflag & DAC_FLAG_DMAUDR1) == DAC_FLAG_DMAUDR1)
684     {
685       /* Change DAC state to error state */
686       hdac->State = HAL_DAC_STATE_ERROR;
687 
688       /* Set DAC error code to channel1 DMA underrun error */
689       SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH1);
690 
691       /* Clear the underrun flag */
692       __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR1);
693 
694       /* Disable the selected DAC channel1 DMA request */
695       __HAL_DAC_DISABLE_IT(hdac, DAC_CR_DMAEN1);
696 
697       /* Error callback */
698 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
699       hdac->DMAUnderrunCallbackCh1(hdac);
700 #else
701       HAL_DAC_DMAUnderrunCallbackCh1(hdac);
702 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
703     }
704   }
705 }
706 
707 /**
708   * @brief  Set the specified data holding register value for DAC channel.
709   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
710   *         the configuration information for the specified DAC.
711   * @param  Channel The selected DAC channel.
712   *          This parameter can be one of the following values:
713   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
714   * @param  Alignment Specifies the data alignment.
715   *          This parameter can be one of the following values:
716   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
717   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
718   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
719   * @param  Data Data to be loaded in the selected data holding register.
720   * @retval HAL status
721   */
HAL_DAC_SetValue(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t Alignment,uint32_t Data)722 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
723 {
724   __IO uint32_t tmp = 0UL;
725 
726   /* Check the parameters */
727   assert_param(IS_DAC_CHANNEL(Channel));
728   assert_param(IS_DAC_ALIGN(Alignment));
729   assert_param(IS_DAC_DATA(Data));
730 
731   tmp = (uint32_t)hdac->Instance;
732   if (Channel == DAC_CHANNEL_1)
733   {
734     tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
735   }
736 
737   /* Set the DAC channel selected data holding register */
738   *(__IO uint32_t *) tmp = Data;
739 
740   /* Return function status */
741   return HAL_OK;
742 }
743 
744 /**
745   * @brief  Conversion complete callback in non-blocking mode for Channel1
746   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
747   *         the configuration information for the specified DAC.
748   * @retval None
749   */
HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef * hdac)750 __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
751 {
752   /* Prevent unused argument(s) compilation warning */
753   UNUSED(hdac);
754 
755   /* NOTE : This function should not be modified, when the callback is needed,
756             the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
757    */
758 }
759 
760 /**
761   * @brief  Conversion half DMA transfer callback in non-blocking mode for Channel1
762   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
763   *         the configuration information for the specified DAC.
764   * @retval None
765   */
HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef * hdac)766 __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
767 {
768   /* Prevent unused argument(s) compilation warning */
769   UNUSED(hdac);
770 
771   /* NOTE : This function should not be modified, when the callback is needed,
772             the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
773    */
774 }
775 
776 /**
777   * @brief  Error DAC callback for Channel1.
778   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
779   *         the configuration information for the specified DAC.
780   * @retval None
781   */
HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef * hdac)782 __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
783 {
784   /* Prevent unused argument(s) compilation warning */
785   UNUSED(hdac);
786 
787   /* NOTE : This function should not be modified, when the callback is needed,
788             the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
789    */
790 }
791 
792 /**
793   * @brief  DMA underrun DAC callback for channel1.
794   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
795   *         the configuration information for the specified DAC.
796   * @retval None
797   */
HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef * hdac)798 __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
799 {
800   /* Prevent unused argument(s) compilation warning */
801   UNUSED(hdac);
802 
803   /* NOTE : This function should not be modified, when the callback is needed,
804             the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
805    */
806 }
807 
808 /**
809   * @}
810   */
811 
812 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
813   *  @brief    Peripheral Control functions
814   *
815 @verbatim
816   ==============================================================================
817              ##### Peripheral Control functions #####
818   ==============================================================================
819     [..]  This section provides functions allowing to:
820       (+) Configure channels.
821       (+) Set the specified data holding register value for DAC channel.
822 
823 @endverbatim
824   * @{
825   */
826 
827 /**
828   * @brief  Returns the last data output value of the selected DAC channel.
829   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
830   *         the configuration information for the specified DAC.
831   * @param  Channel The selected DAC channel.
832   *          This parameter can be one of the following values:
833   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
834   * @retval The selected DAC channel data output value.
835   */
HAL_DAC_GetValue(const DAC_HandleTypeDef * hdac,uint32_t Channel)836 uint32_t HAL_DAC_GetValue(const DAC_HandleTypeDef *hdac, uint32_t Channel)
837 {
838   uint32_t result;
839 
840   /* Check the parameters */
841   assert_param(IS_DAC_CHANNEL(Channel));
842 
843   result = hdac->Instance->DOR1;
844 
845   /* Returns the DAC channel data output register value */
846   return result;
847 }
848 
849 /**
850   * @brief  Configures the selected DAC channel.
851   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
852   *         the configuration information for the specified DAC.
853   * @param  sConfig DAC configuration structure.
854   * @param  Channel The selected DAC channel.
855   *          This parameter can be one of the following values:
856   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
857   * @retval HAL status
858   */
HAL_DAC_ConfigChannel(DAC_HandleTypeDef * hdac,const DAC_ChannelConfTypeDef * sConfig,uint32_t Channel)859 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef *hdac,
860                                         const DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
861 {
862   HAL_StatusTypeDef status = HAL_OK;
863   uint32_t tmpreg1;
864   uint32_t tmpreg2;
865   uint32_t tickstart;
866 
867   /* Check the DAC parameters */
868   assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
869   assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
870   assert_param(IS_DAC_CHIP_CONNECTION(sConfig->DAC_ConnectOnChipPeripheral));
871   assert_param(IS_DAC_TRIMMING(sConfig->DAC_UserTrimming));
872   if ((sConfig->DAC_UserTrimming) == DAC_TRIMMING_USER)
873   {
874     assert_param(IS_DAC_TRIMMINGVALUE(sConfig->DAC_TrimmingValue));
875   }
876   assert_param(IS_DAC_SAMPLEANDHOLD(sConfig->DAC_SampleAndHold));
877   if ((sConfig->DAC_SampleAndHold) == DAC_SAMPLEANDHOLD_ENABLE)
878   {
879     assert_param(IS_DAC_SAMPLETIME(sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime));
880     assert_param(IS_DAC_HOLDTIME(sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime));
881     assert_param(IS_DAC_REFRESHTIME(sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime));
882   }
883   assert_param(IS_DAC_CHANNEL(Channel));
884 
885   /* Process locked */
886   __HAL_LOCK(hdac);
887 
888   /* Change DAC state */
889   hdac->State = HAL_DAC_STATE_BUSY;
890 
891   /* Sample and hold configuration */
892   if (sConfig->DAC_SampleAndHold == DAC_SAMPLEANDHOLD_ENABLE)
893   {
894     /* Get timeout */
895     tickstart = HAL_GetTick();
896 
897     /* SHSR1 can be written when BWST1 is cleared */
898     while (((hdac->Instance->SR) & DAC_SR_BWST1) != 0UL)
899     {
900       /* Check for the Timeout */
901       if ((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
902       {
903         /* New check to avoid false timeout detection in case of preemption */
904         if (((hdac->Instance->SR) & DAC_SR_BWST1) != 0UL)
905         {
906           /* Update error code */
907           SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
908 
909           /* Change the DMA state */
910           hdac->State = HAL_DAC_STATE_TIMEOUT;
911 
912           return HAL_TIMEOUT;
913         }
914       }
915     }
916     hdac->Instance->SHSR1 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
917 
918     /* HoldTime */
919     MODIFY_REG(hdac->Instance->SHHR, DAC_SHHR_THOLD1 << (Channel & 0x10UL),
920                (sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime) << (Channel & 0x10UL));
921     /* RefreshTime */
922     MODIFY_REG(hdac->Instance->SHRR, DAC_SHRR_TREFRESH1 << (Channel & 0x10UL),
923                (sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime) << (Channel & 0x10UL));
924   }
925 
926   if (sConfig->DAC_UserTrimming == DAC_TRIMMING_USER)
927     /* USER TRIMMING */
928   {
929     /* Get the DAC CCR value */
930     tmpreg1 = hdac->Instance->CCR;
931     /* Clear trimming value */
932     tmpreg1 &= ~(((uint32_t)(DAC_CCR_OTRIM1)) << (Channel & 0x10UL));
933     /* Configure for the selected trimming offset */
934     tmpreg2 = sConfig->DAC_TrimmingValue;
935     /* Calculate CCR register value depending on DAC_Channel */
936     tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
937     /* Write to DAC CCR */
938     hdac->Instance->CCR = tmpreg1;
939   }
940   /* else factory trimming is used (factory setting are available at reset)*/
941   /* SW Nothing has nothing to do */
942 
943   /* Get the DAC MCR value */
944   tmpreg1 = hdac->Instance->MCR;
945   /* Clear DAC_MCR_MODEx bits */
946   tmpreg1 &= ~(((uint32_t)(DAC_MCR_MODE1)) << (Channel & 0x10UL));
947   /* Configure for the selected DAC channel: mode, buffer output & on chip peripheral connect */
948   tmpreg2 = (sConfig->DAC_SampleAndHold | sConfig->DAC_OutputBuffer | sConfig->DAC_ConnectOnChipPeripheral);
949   /* Calculate MCR register value depending on DAC_Channel */
950   tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
951   /* Write to DAC MCR */
952   hdac->Instance->MCR = tmpreg1;
953 
954   /* DAC in normal operating mode hence clear DAC_CR_CENx bit */
955   CLEAR_BIT(hdac->Instance->CR, DAC_CR_CEN1 << (Channel & 0x10UL));
956 
957   /* Get the DAC CR value */
958   tmpreg1 = hdac->Instance->CR;
959   /* Clear TENx, TSELx, WAVEx and MAMPx bits */
960   tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1)) << (Channel & 0x10UL));
961   /* Configure for the selected DAC channel: trigger */
962   /* Set TSELx and TENx bits according to DAC_Trigger value */
963   tmpreg2 = sConfig->DAC_Trigger;
964   /* Calculate CR register value depending on DAC_Channel */
965   tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
966   /* Write to DAC CR */
967   hdac->Instance->CR = tmpreg1;
968   /* Disable wave generation */
969   CLEAR_BIT(hdac->Instance->CR, (DAC_CR_WAVE1 << (Channel & 0x10UL)));
970 
971   /* Change DAC state */
972   hdac->State = HAL_DAC_STATE_READY;
973 
974   /* Process unlocked */
975   __HAL_UNLOCK(hdac);
976 
977   /* Return function status */
978   return status;
979 }
980 
981 /**
982   * @}
983   */
984 
985 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
986   *  @brief   Peripheral State and Errors functions
987   *
988 @verbatim
989   ==============================================================================
990             ##### Peripheral State and Errors functions #####
991   ==============================================================================
992     [..]
993     This subsection provides functions allowing to
994       (+) Check the DAC state.
995       (+) Check the DAC Errors.
996 
997 @endverbatim
998   * @{
999   */
1000 
1001 /**
1002   * @brief  return the DAC handle state
1003   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1004   *         the configuration information for the specified DAC.
1005   * @retval HAL state
1006   */
HAL_DAC_GetState(const DAC_HandleTypeDef * hdac)1007 HAL_DAC_StateTypeDef HAL_DAC_GetState(const DAC_HandleTypeDef *hdac)
1008 {
1009   /* Return DAC handle state */
1010   return hdac->State;
1011 }
1012 
1013 
1014 /**
1015   * @brief  Return the DAC error code
1016   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1017   *         the configuration information for the specified DAC.
1018   * @retval DAC Error Code
1019   */
HAL_DAC_GetError(const DAC_HandleTypeDef * hdac)1020 uint32_t HAL_DAC_GetError(const DAC_HandleTypeDef *hdac)
1021 {
1022   return hdac->ErrorCode;
1023 }
1024 
1025 /**
1026   * @}
1027   */
1028 
1029 /**
1030   * @}
1031   */
1032 
1033 /** @addtogroup DAC_Exported_Functions
1034   * @{
1035   */
1036 
1037 /** @addtogroup DAC_Exported_Functions_Group1
1038   * @{
1039   */
1040 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1041 /**
1042   * @brief  Register a User DAC Callback
1043   *         To be used instead of the weak (surcharged) predefined callback
1044   * @note   The HAL_DAC_RegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to register
1045   *         callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
1046   * @param  hdac DAC handle
1047   * @param  CallbackID ID of the callback to be registered
1048   *         This parameter can be one of the following values:
1049   *          @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK   DAC Error Callback ID
1050   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID       DAC CH1 Complete Callback ID
1051   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID  DAC CH1 Half Complete Callback ID
1052   *          @arg @ref HAL_DAC_CH1_ERROR_ID             DAC CH1 Error Callback ID
1053   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID       DAC CH1 UnderRun Callback ID
1054   *          @arg @ref HAL_DAC_MSPINIT_CB_ID            DAC MSP Init Callback ID
1055   *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID          DAC MSP DeInit Callback ID
1056   *
1057   * @param  pCallback pointer to the Callback function
1058   * @retval status
1059   */
HAL_DAC_RegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID,pDAC_CallbackTypeDef pCallback)1060 HAL_StatusTypeDef HAL_DAC_RegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID,
1061                                            pDAC_CallbackTypeDef pCallback)
1062 {
1063   HAL_StatusTypeDef status = HAL_OK;
1064 
1065   if (pCallback == NULL)
1066   {
1067     /* Update the error code */
1068     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1069     return HAL_ERROR;
1070   }
1071 
1072   if (hdac->State == HAL_DAC_STATE_READY)
1073   {
1074     switch (CallbackID)
1075     {
1076       case HAL_DAC_CH1_COMPLETE_CB_ID :
1077         hdac->ConvCpltCallbackCh1 = pCallback;
1078         break;
1079       case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1080         hdac->ConvHalfCpltCallbackCh1 = pCallback;
1081         break;
1082       case HAL_DAC_CH1_ERROR_ID :
1083         hdac->ErrorCallbackCh1 = pCallback;
1084         break;
1085       case HAL_DAC_CH1_UNDERRUN_CB_ID :
1086         hdac->DMAUnderrunCallbackCh1 = pCallback;
1087         break;
1088       case HAL_DAC_MSPINIT_CB_ID :
1089         hdac->MspInitCallback = pCallback;
1090         break;
1091       case HAL_DAC_MSPDEINIT_CB_ID :
1092         hdac->MspDeInitCallback = pCallback;
1093         break;
1094       default :
1095         /* Update the error code */
1096         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1097         /* update return status */
1098         status =  HAL_ERROR;
1099         break;
1100     }
1101   }
1102   else if (hdac->State == HAL_DAC_STATE_RESET)
1103   {
1104     switch (CallbackID)
1105     {
1106       case HAL_DAC_MSPINIT_CB_ID :
1107         hdac->MspInitCallback = pCallback;
1108         break;
1109       case HAL_DAC_MSPDEINIT_CB_ID :
1110         hdac->MspDeInitCallback = pCallback;
1111         break;
1112       default :
1113         /* Update the error code */
1114         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1115         /* update return status */
1116         status =  HAL_ERROR;
1117         break;
1118     }
1119   }
1120   else
1121   {
1122     /* Update the error code */
1123     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1124     /* update return status */
1125     status =  HAL_ERROR;
1126   }
1127 
1128   return status;
1129 }
1130 
1131 /**
1132   * @brief  Unregister a User DAC Callback
1133   *         DAC Callback is redirected to the weak (surcharged) predefined callback
1134   * @note   The HAL_DAC_UnRegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to un-register
1135   *         callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
1136   * @param  hdac DAC handle
1137   * @param  CallbackID ID of the callback to be unregistered
1138   *         This parameter can be one of the following values:
1139   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID          DAC CH1 transfer Complete Callback ID
1140   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID     DAC CH1 Half Complete Callback ID
1141   *          @arg @ref HAL_DAC_CH1_ERROR_ID                DAC CH1 Error Callback ID
1142   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID          DAC CH1 UnderRun Callback ID
1143   *          @arg @ref HAL_DAC_MSPINIT_CB_ID               DAC MSP Init Callback ID
1144   *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID             DAC MSP DeInit Callback ID
1145   *          @arg @ref HAL_DAC_ALL_CB_ID                   DAC All callbacks
1146   * @retval status
1147   */
HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID)1148 HAL_StatusTypeDef HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
1149 {
1150   HAL_StatusTypeDef status = HAL_OK;
1151 
1152   if (hdac->State == HAL_DAC_STATE_READY)
1153   {
1154     switch (CallbackID)
1155     {
1156       case HAL_DAC_CH1_COMPLETE_CB_ID :
1157         hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1158         break;
1159       case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1160         hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1161         break;
1162       case HAL_DAC_CH1_ERROR_ID :
1163         hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1164         break;
1165       case HAL_DAC_CH1_UNDERRUN_CB_ID :
1166         hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1167         break;
1168       case HAL_DAC_MSPINIT_CB_ID :
1169         hdac->MspInitCallback = HAL_DAC_MspInit;
1170         break;
1171       case HAL_DAC_MSPDEINIT_CB_ID :
1172         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1173         break;
1174       case HAL_DAC_ALL_CB_ID :
1175         hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1176         hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1177         hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1178         hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1179         hdac->MspInitCallback = HAL_DAC_MspInit;
1180         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1181         break;
1182       default :
1183         /* Update the error code */
1184         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1185         /* update return status */
1186         status =  HAL_ERROR;
1187         break;
1188     }
1189   }
1190   else if (hdac->State == HAL_DAC_STATE_RESET)
1191   {
1192     switch (CallbackID)
1193     {
1194       case HAL_DAC_MSPINIT_CB_ID :
1195         hdac->MspInitCallback = HAL_DAC_MspInit;
1196         break;
1197       case HAL_DAC_MSPDEINIT_CB_ID :
1198         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1199         break;
1200       default :
1201         /* Update the error code */
1202         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1203         /* update return status */
1204         status =  HAL_ERROR;
1205         break;
1206     }
1207   }
1208   else
1209   {
1210     /* Update the error code */
1211     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1212     /* update return status */
1213     status =  HAL_ERROR;
1214   }
1215 
1216   return status;
1217 }
1218 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1219 
1220 /**
1221   * @}
1222   */
1223 
1224 /**
1225   * @}
1226   */
1227 
1228 /** @addtogroup DAC_Private_Functions
1229   * @{
1230   */
1231 
1232 /**
1233   * @brief  DMA conversion complete callback.
1234   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1235   *                the configuration information for the specified DMA module.
1236   * @retval None
1237   */
DAC_DMAConvCpltCh1(DMA_HandleTypeDef * hdma)1238 void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
1239 {
1240   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1241 
1242 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1243   hdac->ConvCpltCallbackCh1(hdac);
1244 #else
1245   HAL_DAC_ConvCpltCallbackCh1(hdac);
1246 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1247 
1248   hdac->State = HAL_DAC_STATE_READY;
1249 }
1250 
1251 /**
1252   * @brief  DMA half transfer complete callback.
1253   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1254   *                the configuration information for the specified DMA module.
1255   * @retval None
1256   */
DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef * hdma)1257 void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
1258 {
1259   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1260   /* Conversion complete callback */
1261 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1262   hdac->ConvHalfCpltCallbackCh1(hdac);
1263 #else
1264   HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
1265 #endif  /* USE_HAL_DAC_REGISTER_CALLBACKS */
1266 }
1267 
1268 /**
1269   * @brief  DMA error callback
1270   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1271   *                the configuration information for the specified DMA module.
1272   * @retval None
1273   */
DAC_DMAErrorCh1(DMA_HandleTypeDef * hdma)1274 void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
1275 {
1276   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1277 
1278   /* Set DAC error code to DMA error */
1279   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
1280 
1281 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1282   hdac->ErrorCallbackCh1(hdac);
1283 #else
1284   HAL_DAC_ErrorCallbackCh1(hdac);
1285 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1286 
1287   hdac->State = HAL_DAC_STATE_READY;
1288 }
1289 
1290 /**
1291   * @}
1292   */
1293 
1294 /**
1295   * @}
1296   */
1297 
1298 #endif /* DAC */
1299 
1300 #endif /* HAL_DAC_MODULE_ENABLED */
1301 /**
1302   * @}
1303   */
1304