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