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   if (hdac == NULL)
942   {
943     return HAL_ERROR;
944   }
945 
946   /* Check the parameters */
947   assert_param(IS_DAC_CHANNEL(Channel));
948 
949   if (Channel == DAC_CHANNEL_1)
950   {
951     result = hdac->Instance->DOR1;
952   }
953 
954   else
955   {
956     result = hdac->Instance->DOR2;
957   }
958 
959   /* Returns the DAC channel data output register value */
960   return result;
961 }
962 
963 /**
964   * @brief  Configures the selected DAC channel.
965   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
966   *         the configuration information for the specified DAC.
967   * @param  sConfig DAC configuration structure.
968   * @param  Channel The selected DAC channel.
969   *          This parameter can be one of the following values:
970   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
971   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
972   * @retval HAL status
973   */
HAL_DAC_ConfigChannel(DAC_HandleTypeDef * hdac,const DAC_ChannelConfTypeDef * sConfig,uint32_t Channel)974 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef *hdac,
975                                         const DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
976 {
977   HAL_StatusTypeDef status = HAL_OK;
978   uint32_t tmpreg1;
979   uint32_t tmpreg2;
980 
981   /* Check the DAC peripheral handle and channel configuration struct */
982   if ((hdac == NULL) || (sConfig == NULL))
983   {
984     return HAL_ERROR;
985   }
986 
987   /* Check the DAC parameters */
988   assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
989   assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
990   assert_param(IS_DAC_CHANNEL(Channel));
991 
992   /* Process locked */
993   __HAL_LOCK(hdac);
994 
995   /* Change DAC state */
996   hdac->State = HAL_DAC_STATE_BUSY;
997 
998   /* Get the DAC CR value */
999   tmpreg1 = hdac->Instance->CR;
1000   /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
1001   tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1))
1002                << (Channel & 0x10UL));
1003   /* Configure for the selected DAC channel: buffer output, trigger */
1004   /* Set TSELx and TENx bits according to DAC_Trigger value */
1005   /* Set BOFFx bit according to DAC_OutputBuffer value */
1006   tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
1007   /* Calculate CR register value depending on DAC_Channel */
1008   tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
1009   /* Write to DAC CR */
1010   hdac->Instance->CR = tmpreg1;
1011   /* Disable wave generation */
1012   CLEAR_BIT(hdac->Instance->CR, (DAC_CR_WAVE1 << (Channel & 0x10UL)));
1013 
1014   /* Change DAC state */
1015   hdac->State = HAL_DAC_STATE_READY;
1016 
1017   /* Process unlocked */
1018   __HAL_UNLOCK(hdac);
1019 
1020   /* Return function status */
1021   return status;
1022 }
1023 
1024 /**
1025   * @}
1026   */
1027 
1028 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
1029   *  @brief   Peripheral State and Errors functions
1030   *
1031 @verbatim
1032   ==============================================================================
1033             ##### Peripheral State and Errors functions #####
1034   ==============================================================================
1035     [..]
1036     This subsection provides functions allowing to
1037       (+) Check the DAC state.
1038       (+) Check the DAC Errors.
1039 
1040 @endverbatim
1041   * @{
1042   */
1043 
1044 /**
1045   * @brief  return the DAC handle state
1046   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1047   *         the configuration information for the specified DAC.
1048   * @retval HAL state
1049   */
HAL_DAC_GetState(const DAC_HandleTypeDef * hdac)1050 HAL_DAC_StateTypeDef HAL_DAC_GetState(const DAC_HandleTypeDef *hdac)
1051 {
1052   /* Return DAC handle state */
1053   return hdac->State;
1054 }
1055 
1056 
1057 /**
1058   * @brief  Return the DAC error code
1059   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1060   *         the configuration information for the specified DAC.
1061   * @retval DAC Error Code
1062   */
HAL_DAC_GetError(const DAC_HandleTypeDef * hdac)1063 uint32_t HAL_DAC_GetError(const DAC_HandleTypeDef *hdac)
1064 {
1065   return hdac->ErrorCode;
1066 }
1067 
1068 /**
1069   * @}
1070   */
1071 
1072 /**
1073   * @}
1074   */
1075 
1076 /** @addtogroup DAC_Exported_Functions
1077   * @{
1078   */
1079 
1080 /** @addtogroup DAC_Exported_Functions_Group1
1081   * @{
1082   */
1083 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1084 /**
1085   * @brief  Register a User DAC Callback
1086   *         To be used instead of the weak (overridden) predefined callback
1087   * @note   The HAL_DAC_RegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to register
1088   *         callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
1089   * @param  hdac DAC handle
1090   * @param  CallbackID ID of the callback to be registered
1091   *         This parameter can be one of the following values:
1092   *          @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK   DAC Error Callback ID
1093   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID       DAC CH1 Complete Callback ID
1094   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID  DAC CH1 Half Complete Callback ID
1095   *          @arg @ref HAL_DAC_CH1_ERROR_ID             DAC CH1 Error Callback ID
1096   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID       DAC CH1 UnderRun Callback ID
1097   *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID       DAC CH2 Complete Callback ID
1098   *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID  DAC CH2 Half Complete Callback ID
1099   *          @arg @ref HAL_DAC_CH2_ERROR_ID             DAC CH2 Error Callback ID
1100   *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID       DAC CH2 UnderRun Callback ID
1101   *          @arg @ref HAL_DAC_MSPINIT_CB_ID            DAC MSP Init Callback ID
1102   *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID          DAC MSP DeInit Callback ID
1103   *
1104   * @param  pCallback pointer to the Callback function
1105   * @retval status
1106   */
HAL_DAC_RegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID,pDAC_CallbackTypeDef pCallback)1107 HAL_StatusTypeDef HAL_DAC_RegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID,
1108                                            pDAC_CallbackTypeDef pCallback)
1109 {
1110   HAL_StatusTypeDef status = HAL_OK;
1111 
1112   /* Check the DAC peripheral handle */
1113   if (hdac == NULL)
1114   {
1115     return HAL_ERROR;
1116   }
1117 
1118   if (pCallback == NULL)
1119   {
1120     /* Update the error code */
1121     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1122     return HAL_ERROR;
1123   }
1124 
1125   if (hdac->State == HAL_DAC_STATE_READY)
1126   {
1127     switch (CallbackID)
1128     {
1129       case HAL_DAC_CH1_COMPLETE_CB_ID :
1130         hdac->ConvCpltCallbackCh1 = pCallback;
1131         break;
1132       case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1133         hdac->ConvHalfCpltCallbackCh1 = pCallback;
1134         break;
1135       case HAL_DAC_CH1_ERROR_ID :
1136         hdac->ErrorCallbackCh1 = pCallback;
1137         break;
1138       case HAL_DAC_CH1_UNDERRUN_CB_ID :
1139         hdac->DMAUnderrunCallbackCh1 = pCallback;
1140         break;
1141 
1142       case HAL_DAC_CH2_COMPLETE_CB_ID :
1143         hdac->ConvCpltCallbackCh2 = pCallback;
1144         break;
1145       case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1146         hdac->ConvHalfCpltCallbackCh2 = pCallback;
1147         break;
1148       case HAL_DAC_CH2_ERROR_ID :
1149         hdac->ErrorCallbackCh2 = pCallback;
1150         break;
1151       case HAL_DAC_CH2_UNDERRUN_CB_ID :
1152         hdac->DMAUnderrunCallbackCh2 = pCallback;
1153         break;
1154 
1155       case HAL_DAC_MSPINIT_CB_ID :
1156         hdac->MspInitCallback = pCallback;
1157         break;
1158       case HAL_DAC_MSPDEINIT_CB_ID :
1159         hdac->MspDeInitCallback = pCallback;
1160         break;
1161       default :
1162         /* Update the error code */
1163         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1164         /* update return status */
1165         status =  HAL_ERROR;
1166         break;
1167     }
1168   }
1169   else if (hdac->State == HAL_DAC_STATE_RESET)
1170   {
1171     switch (CallbackID)
1172     {
1173       case HAL_DAC_MSPINIT_CB_ID :
1174         hdac->MspInitCallback = pCallback;
1175         break;
1176       case HAL_DAC_MSPDEINIT_CB_ID :
1177         hdac->MspDeInitCallback = pCallback;
1178         break;
1179       default :
1180         /* Update the error code */
1181         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1182         /* update return status */
1183         status =  HAL_ERROR;
1184         break;
1185     }
1186   }
1187   else
1188   {
1189     /* Update the error code */
1190     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1191     /* update return status */
1192     status =  HAL_ERROR;
1193   }
1194 
1195   return status;
1196 }
1197 
1198 /**
1199   * @brief  Unregister a User DAC Callback
1200   *         DAC Callback is redirected to the weak (overridden) predefined callback
1201   * @note   The HAL_DAC_UnRegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to un-register
1202   *         callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
1203   * @param  hdac DAC handle
1204   * @param  CallbackID ID of the callback to be unregistered
1205   *         This parameter can be one of the following values:
1206   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID          DAC CH1 transfer Complete Callback ID
1207   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID     DAC CH1 Half Complete Callback ID
1208   *          @arg @ref HAL_DAC_CH1_ERROR_ID                DAC CH1 Error Callback ID
1209   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID          DAC CH1 UnderRun Callback ID
1210   *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID          DAC CH2 Complete Callback ID
1211   *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID     DAC CH2 Half Complete Callback ID
1212   *          @arg @ref HAL_DAC_CH2_ERROR_ID                DAC CH2 Error Callback ID
1213   *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID          DAC CH2 UnderRun Callback ID
1214   *          @arg @ref HAL_DAC_MSPINIT_CB_ID               DAC MSP Init Callback ID
1215   *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID             DAC MSP DeInit Callback ID
1216   *          @arg @ref HAL_DAC_ALL_CB_ID                   DAC All callbacks
1217   * @retval status
1218   */
HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID)1219 HAL_StatusTypeDef HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
1220 {
1221   HAL_StatusTypeDef status = HAL_OK;
1222 
1223   /* Check the DAC peripheral handle */
1224   if (hdac == NULL)
1225   {
1226     return HAL_ERROR;
1227   }
1228 
1229   if (hdac->State == HAL_DAC_STATE_READY)
1230   {
1231     switch (CallbackID)
1232     {
1233       case HAL_DAC_CH1_COMPLETE_CB_ID :
1234         hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1235         break;
1236       case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1237         hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1238         break;
1239       case HAL_DAC_CH1_ERROR_ID :
1240         hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1241         break;
1242       case HAL_DAC_CH1_UNDERRUN_CB_ID :
1243         hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1244         break;
1245 
1246       case HAL_DAC_CH2_COMPLETE_CB_ID :
1247         hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1248         break;
1249       case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1250         hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1251         break;
1252       case HAL_DAC_CH2_ERROR_ID :
1253         hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1254         break;
1255       case HAL_DAC_CH2_UNDERRUN_CB_ID :
1256         hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1257         break;
1258 
1259       case HAL_DAC_MSPINIT_CB_ID :
1260         hdac->MspInitCallback = HAL_DAC_MspInit;
1261         break;
1262       case HAL_DAC_MSPDEINIT_CB_ID :
1263         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1264         break;
1265       case HAL_DAC_ALL_CB_ID :
1266         hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1267         hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1268         hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1269         hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1270 
1271         hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1272         hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1273         hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1274         hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1275 
1276         hdac->MspInitCallback = HAL_DAC_MspInit;
1277         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1278         break;
1279       default :
1280         /* Update the error code */
1281         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1282         /* update return status */
1283         status =  HAL_ERROR;
1284         break;
1285     }
1286   }
1287   else if (hdac->State == HAL_DAC_STATE_RESET)
1288   {
1289     switch (CallbackID)
1290     {
1291       case HAL_DAC_MSPINIT_CB_ID :
1292         hdac->MspInitCallback = HAL_DAC_MspInit;
1293         break;
1294       case HAL_DAC_MSPDEINIT_CB_ID :
1295         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1296         break;
1297       default :
1298         /* Update the error code */
1299         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1300         /* update return status */
1301         status =  HAL_ERROR;
1302         break;
1303     }
1304   }
1305   else
1306   {
1307     /* Update the error code */
1308     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1309     /* update return status */
1310     status =  HAL_ERROR;
1311   }
1312 
1313   return status;
1314 }
1315 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1316 
1317 /**
1318   * @}
1319   */
1320 
1321 /**
1322   * @}
1323   */
1324 
1325 /** @addtogroup DAC_Private_Functions
1326   * @{
1327   */
1328 
1329 /**
1330   * @brief  DMA conversion complete callback.
1331   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1332   *                the configuration information for the specified DMA module.
1333   * @retval None
1334   */
DAC_DMAConvCpltCh1(DMA_HandleTypeDef * hdma)1335 void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
1336 {
1337   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1338 
1339 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1340   hdac->ConvCpltCallbackCh1(hdac);
1341 #else
1342   HAL_DAC_ConvCpltCallbackCh1(hdac);
1343 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1344 
1345   hdac->State = HAL_DAC_STATE_READY;
1346 }
1347 
1348 /**
1349   * @brief  DMA half transfer complete callback.
1350   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1351   *                the configuration information for the specified DMA module.
1352   * @retval None
1353   */
DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef * hdma)1354 void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
1355 {
1356   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1357   /* Conversion complete callback */
1358 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1359   hdac->ConvHalfCpltCallbackCh1(hdac);
1360 #else
1361   HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
1362 #endif  /* USE_HAL_DAC_REGISTER_CALLBACKS */
1363 }
1364 
1365 /**
1366   * @brief  DMA error callback
1367   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1368   *                the configuration information for the specified DMA module.
1369   * @retval None
1370   */
DAC_DMAErrorCh1(DMA_HandleTypeDef * hdma)1371 void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
1372 {
1373   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1374 
1375   /* Set DAC error code to DMA error */
1376   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
1377 
1378 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1379   hdac->ErrorCallbackCh1(hdac);
1380 #else
1381   HAL_DAC_ErrorCallbackCh1(hdac);
1382 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1383 
1384   hdac->State = HAL_DAC_STATE_READY;
1385 }
1386 
1387 /**
1388   * @}
1389   */
1390 
1391 /**
1392   * @}
1393   */
1394 
1395 #endif /* DAC */
1396 
1397 #endif /* HAL_DAC_MODULE_ENABLED */
1398 /**
1399   * @}
1400   */
1401