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