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