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 (overridden) 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 (overridden) functions.
185       Exception done for MspInit and MspDeInit callbacks that are respectively
186       reset to the legacy weak (overridden) 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 (overridden) 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 the DAC peripheral 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 the DAC peripheral 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 DAC peripheral handle */
436   if (hdac == NULL)
437   {
438     return HAL_ERROR;
439   }
440 
441   /* Check the parameters */
442   assert_param(IS_DAC_CHANNEL(Channel));
443 
444   /* Process locked */
445   __HAL_LOCK(hdac);
446 
447   /* Change DAC state */
448   hdac->State = HAL_DAC_STATE_BUSY;
449 
450   /* Enable the Peripheral */
451   __HAL_DAC_ENABLE(hdac, Channel);
452 
453   if (Channel == DAC_CHANNEL_1)
454   {
455     /* Check if software trigger enabled */
456     if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
457     {
458       /* Enable the selected DAC software conversion */
459       SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
460     }
461   }
462 
463   else
464   {
465     /* Check if software trigger enabled */
466     if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << (Channel & 0x10UL)))
467     {
468       /* Enable the selected DAC software conversion*/
469       SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
470     }
471   }
472 
473 
474   /* Change DAC state */
475   hdac->State = HAL_DAC_STATE_READY;
476 
477   /* Process unlocked */
478   __HAL_UNLOCK(hdac);
479 
480   /* Return function status */
481   return HAL_OK;
482 }
483 
484 /**
485   * @brief  Disables DAC and stop conversion of channel.
486   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
487   *         the configuration information for the specified DAC.
488   * @param  Channel The selected DAC channel.
489   *          This parameter can be one of the following values:
490   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
491   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
492   * @retval HAL status
493   */
HAL_DAC_Stop(DAC_HandleTypeDef * hdac,uint32_t Channel)494 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef *hdac, uint32_t Channel)
495 {
496   /* Check the DAC peripheral handle */
497   if (hdac == NULL)
498   {
499     return HAL_ERROR;
500   }
501 
502   /* Check the parameters */
503   assert_param(IS_DAC_CHANNEL(Channel));
504 
505   /* Disable the Peripheral */
506   __HAL_DAC_DISABLE(hdac, Channel);
507 
508   /* Change DAC state */
509   hdac->State = HAL_DAC_STATE_READY;
510 
511   /* Return function status */
512   return HAL_OK;
513 }
514 
515 /**
516   * @brief  Enables DAC and starts conversion of channel.
517   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
518   *         the configuration information for the specified DAC.
519   * @param  Channel The selected DAC channel.
520   *          This parameter can be one of the following values:
521   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
522   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
523   * @param  pData The source Buffer address.
524   * @param  Length The length of data to be transferred from memory to DAC peripheral
525   * @param  Alignment Specifies the data alignment for DAC channel.
526   *          This parameter can be one of the following values:
527   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
528   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
529   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
530   * @retval HAL status
531   */
HAL_DAC_Start_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel,const uint32_t * pData,uint32_t Length,uint32_t Alignment)532 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, const uint32_t *pData, uint32_t Length,
533                                     uint32_t Alignment)
534 {
535   HAL_StatusTypeDef status;
536   uint32_t tmpreg;
537 
538   /* Check the DAC peripheral handle */
539   if (hdac == NULL)
540   {
541     return HAL_ERROR;
542   }
543 
544   /* Check the parameters */
545   assert_param(IS_DAC_CHANNEL(Channel));
546   assert_param(IS_DAC_ALIGN(Alignment));
547 
548   /* Process locked */
549   __HAL_LOCK(hdac);
550 
551   /* Change DAC state */
552   hdac->State = HAL_DAC_STATE_BUSY;
553 
554   if (Channel == DAC_CHANNEL_1)
555   {
556     /* Set the DMA transfer complete callback for channel1 */
557     hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
558 
559     /* Set the DMA half transfer complete callback for channel1 */
560     hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
561 
562     /* Set the DMA error callback for channel1 */
563     hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
564 
565     /* Enable the selected DAC channel1 DMA request */
566     SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
567 
568     /* Case of use of channel 1 */
569     switch (Alignment)
570     {
571       case DAC_ALIGN_12B_R:
572         /* Get DHR12R1 address */
573         tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
574         break;
575       case DAC_ALIGN_12B_L:
576         /* Get DHR12L1 address */
577         tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
578         break;
579       default: /* case DAC_ALIGN_8B_R */
580         /* Get DHR8R1 address */
581         tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
582         break;
583     }
584   }
585 
586   else
587   {
588     /* Set the DMA transfer complete callback for channel2 */
589     hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
590 
591     /* Set the DMA half transfer complete callback for channel2 */
592     hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
593 
594     /* Set the DMA error callback for channel2 */
595     hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
596 
597     /* Enable the selected DAC channel2 DMA request */
598     SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
599 
600     /* Case of use of channel 2 */
601     switch (Alignment)
602     {
603       case DAC_ALIGN_12B_R:
604         /* Get DHR12R2 address */
605         tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
606         break;
607       case DAC_ALIGN_12B_L:
608         /* Get DHR12L2 address */
609         tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
610         break;
611       default: /* case DAC_ALIGN_8B_R */
612         /* Get DHR8R2 address */
613         tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
614         break;
615     }
616   }
617 
618   if (Channel == DAC_CHANNEL_1)
619   {
620     /* Enable the DAC DMA underrun interrupt */
621     __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
622 
623     /* Enable the DMA Stream */
624     status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
625   }
626 
627   else
628   {
629     /* Enable the DAC DMA underrun interrupt */
630     __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
631 
632     /* Enable the DMA Stream */
633     status = HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
634   }
635 
636 
637   /* Process Unlocked */
638   __HAL_UNLOCK(hdac);
639 
640   if (status == HAL_OK)
641   {
642     /* Enable the Peripheral */
643     __HAL_DAC_ENABLE(hdac, Channel);
644   }
645   else
646   {
647     hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
648   }
649 
650   /* Return function status */
651   return status;
652 }
653 
654 /**
655   * @brief  Disables DAC and stop conversion of channel.
656   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
657   *         the configuration information for the specified DAC.
658   * @param  Channel The selected DAC channel.
659   *          This parameter can be one of the following values:
660   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
661   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
662   * @retval HAL status
663   */
HAL_DAC_Stop_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel)664 HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel)
665 {
666   /* Check the DAC peripheral handle */
667   if (hdac == NULL)
668   {
669     return HAL_ERROR;
670   }
671 
672   /* Check the parameters */
673   assert_param(IS_DAC_CHANNEL(Channel));
674 
675   /* Disable the selected DAC channel DMA request */
676   hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << (Channel & 0x10UL));
677 
678   /* Disable the Peripheral */
679   __HAL_DAC_DISABLE(hdac, Channel);
680 
681   /* Disable the DMA Stream */
682 
683   /* Channel1 is used */
684   if (Channel == DAC_CHANNEL_1)
685   {
686     /* Disable the DMA Stream */
687     (void)HAL_DMA_Abort(hdac->DMA_Handle1);
688 
689     /* Disable the DAC DMA underrun interrupt */
690     __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
691   }
692 
693   else /* Channel2 is used for */
694   {
695     /* Disable the DMA Stream */
696     (void)HAL_DMA_Abort(hdac->DMA_Handle2);
697 
698     /* Disable the DAC DMA underrun interrupt */
699     __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR2);
700   }
701 
702 
703   /* Change DAC state */
704   hdac->State = HAL_DAC_STATE_READY;
705 
706   /* Return function status */
707   return HAL_OK;
708 }
709 
710 /**
711   * @brief  Handles DAC interrupt request
712   *         This function uses the interruption of DMA
713   *         underrun.
714   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
715   *         the configuration information for the specified DAC.
716   * @retval None
717   */
HAL_DAC_IRQHandler(DAC_HandleTypeDef * hdac)718 void HAL_DAC_IRQHandler(DAC_HandleTypeDef *hdac)
719 {
720   uint32_t itsource = hdac->Instance->CR;
721   uint32_t itflag   = hdac->Instance->SR;
722 
723   if ((itsource & DAC_IT_DMAUDR1) == DAC_IT_DMAUDR1)
724   {
725     /* Check underrun flag of DAC channel 1 */
726     if ((itflag & DAC_FLAG_DMAUDR1) == DAC_FLAG_DMAUDR1)
727     {
728       /* Change DAC state to error state */
729       hdac->State = HAL_DAC_STATE_ERROR;
730 
731       /* Set DAC error code to channel1 DMA underrun error */
732       SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH1);
733 
734       /* Clear the underrun flag */
735       __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR1);
736 
737       /* Disable the selected DAC channel1 DMA request */
738       __HAL_DAC_DISABLE_IT(hdac, DAC_CR_DMAEN1);
739 
740       /* Error callback */
741 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
742       hdac->DMAUnderrunCallbackCh1(hdac);
743 #else
744       HAL_DAC_DMAUnderrunCallbackCh1(hdac);
745 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
746     }
747   }
748 
749 
750   if ((itsource & DAC_IT_DMAUDR2) == DAC_IT_DMAUDR2)
751   {
752     /* Check underrun flag of DAC channel 2 */
753     if ((itflag & DAC_FLAG_DMAUDR2) == DAC_FLAG_DMAUDR2)
754     {
755       /* Change DAC state to error state */
756       hdac->State = HAL_DAC_STATE_ERROR;
757 
758       /* Set DAC error code to channel2 DMA underrun error */
759       SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH2);
760 
761       /* Clear the underrun flag */
762       __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR2);
763 
764       /* Disable the selected DAC channel2 DMA request */
765       __HAL_DAC_DISABLE_IT(hdac, DAC_CR_DMAEN2);
766 
767       /* Error callback */
768 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
769       hdac->DMAUnderrunCallbackCh2(hdac);
770 #else
771       HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
772 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
773     }
774   }
775 
776 }
777 
778 /**
779   * @brief  Set the specified data holding register value for DAC channel.
780   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
781   *         the configuration information for the specified DAC.
782   * @param  Channel The selected DAC channel.
783   *          This parameter can be one of the following values:
784   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
785   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
786   * @param  Alignment Specifies the data alignment.
787   *          This parameter can be one of the following values:
788   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
789   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
790   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
791   * @param  Data Data to be loaded in the selected data holding register.
792   * @retval HAL status
793   */
HAL_DAC_SetValue(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t Alignment,uint32_t Data)794 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
795 {
796   __IO uint32_t tmp = 0UL;
797 
798   /* Check the DAC peripheral handle */
799   if (hdac == NULL)
800   {
801     return HAL_ERROR;
802   }
803 
804   /* Check the parameters */
805   assert_param(IS_DAC_CHANNEL(Channel));
806   assert_param(IS_DAC_ALIGN(Alignment));
807   assert_param(IS_DAC_DATA(Data));
808 
809   tmp = (uint32_t)hdac->Instance;
810   if (Channel == DAC_CHANNEL_1)
811   {
812     tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
813   }
814 
815   else
816   {
817     tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
818   }
819 
820 
821   /* Set the DAC channel selected data holding register */
822   *(__IO uint32_t *) tmp = Data;
823 
824   /* Return function status */
825   return HAL_OK;
826 }
827 
828 /**
829   * @brief  Conversion complete callback in non-blocking mode for Channel1
830   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
831   *         the configuration information for the specified DAC.
832   * @retval None
833   */
HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef * hdac)834 __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
835 {
836   /* Prevent unused argument(s) compilation warning */
837   UNUSED(hdac);
838 
839   /* NOTE : This function should not be modified, when the callback is needed,
840             the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
841    */
842 }
843 
844 /**
845   * @brief  Conversion half DMA transfer callback in non-blocking mode for Channel1
846   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
847   *         the configuration information for the specified DAC.
848   * @retval None
849   */
HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef * hdac)850 __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
851 {
852   /* Prevent unused argument(s) compilation warning */
853   UNUSED(hdac);
854 
855   /* NOTE : This function should not be modified, when the callback is needed,
856             the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
857    */
858 }
859 
860 /**
861   * @brief  Error DAC callback for Channel1.
862   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
863   *         the configuration information for the specified DAC.
864   * @retval None
865   */
HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef * hdac)866 __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
867 {
868   /* Prevent unused argument(s) compilation warning */
869   UNUSED(hdac);
870 
871   /* NOTE : This function should not be modified, when the callback is needed,
872             the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
873    */
874 }
875 
876 /**
877   * @brief  DMA underrun DAC callback for channel1.
878   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
879   *         the configuration information for the specified DAC.
880   * @retval None
881   */
HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef * hdac)882 __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
883 {
884   /* Prevent unused argument(s) compilation warning */
885   UNUSED(hdac);
886 
887   /* NOTE : This function should not be modified, when the callback is needed,
888             the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
889    */
890 }
891 
892 /**
893   * @}
894   */
895 
896 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
897   *  @brief    Peripheral Control functions
898   *
899 @verbatim
900   ==============================================================================
901              ##### Peripheral Control functions #####
902   ==============================================================================
903     [..]  This section provides functions allowing to:
904       (+) Configure channels.
905       (+) Set the specified data holding register value for DAC channel.
906 
907 @endverbatim
908   * @{
909   */
910 
911 /**
912   * @brief  Returns the last data output value of the selected DAC channel.
913   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
914   *         the configuration information for the specified DAC.
915   * @param  Channel The selected DAC channel.
916   *          This parameter can be one of the following values:
917   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
918   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
919   * @retval The selected DAC channel data output value.
920   */
HAL_DAC_GetValue(const DAC_HandleTypeDef * hdac,uint32_t Channel)921 uint32_t HAL_DAC_GetValue(const DAC_HandleTypeDef *hdac, uint32_t Channel)
922 {
923   uint32_t result;
924 
925   /* Check the DAC peripheral handle */
926   assert_param(hdac != NULL);
927 
928   /* Check the parameters */
929   assert_param(IS_DAC_CHANNEL(Channel));
930 
931   if (Channel == DAC_CHANNEL_1)
932   {
933     result = hdac->Instance->DOR1;
934   }
935 
936   else
937   {
938     result = hdac->Instance->DOR2;
939   }
940 
941   /* Returns the DAC channel data output register value */
942   return result;
943 }
944 
945 /**
946   * @brief  Configures the selected DAC channel.
947   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
948   *         the configuration information for the specified DAC.
949   * @param  sConfig DAC configuration structure.
950   * @param  Channel The selected DAC channel.
951   *          This parameter can be one of the following values:
952   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
953   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
954   * @retval HAL status
955   */
HAL_DAC_ConfigChannel(DAC_HandleTypeDef * hdac,const DAC_ChannelConfTypeDef * sConfig,uint32_t Channel)956 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef *hdac,
957                                         const DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
958 {
959   HAL_StatusTypeDef status = HAL_OK;
960   uint32_t tmpreg1;
961   uint32_t tmpreg2;
962 
963   /* Check the DAC peripheral handle and channel configuration struct */
964   if ((hdac == NULL) || (sConfig == NULL))
965   {
966     return HAL_ERROR;
967   }
968 
969   /* Check the DAC parameters */
970   assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
971   assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
972   assert_param(IS_DAC_CHANNEL(Channel));
973 
974   /* Process locked */
975   __HAL_LOCK(hdac);
976 
977   /* Change DAC state */
978   hdac->State = HAL_DAC_STATE_BUSY;
979 
980   /* Get the DAC CR value */
981   tmpreg1 = hdac->Instance->CR;
982   /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
983   tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1))
984                << (Channel & 0x10UL));
985   /* Configure for the selected DAC channel: buffer output, trigger */
986   /* Set TSELx and TENx bits according to DAC_Trigger value */
987   /* Set BOFFx bit according to DAC_OutputBuffer value */
988   tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
989   /* Calculate CR register value depending on DAC_Channel */
990   tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
991   /* Write to DAC CR */
992   hdac->Instance->CR = tmpreg1;
993   /* Disable wave generation */
994   CLEAR_BIT(hdac->Instance->CR, (DAC_CR_WAVE1 << (Channel & 0x10UL)));
995 
996   /* Change DAC state */
997   hdac->State = HAL_DAC_STATE_READY;
998 
999   /* Process unlocked */
1000   __HAL_UNLOCK(hdac);
1001 
1002   /* Return function status */
1003   return status;
1004 }
1005 
1006 /**
1007   * @}
1008   */
1009 
1010 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
1011   *  @brief   Peripheral State and Errors functions
1012   *
1013 @verbatim
1014   ==============================================================================
1015             ##### Peripheral State and Errors functions #####
1016   ==============================================================================
1017     [..]
1018     This subsection provides functions allowing to
1019       (+) Check the DAC state.
1020       (+) Check the DAC Errors.
1021 
1022 @endverbatim
1023   * @{
1024   */
1025 
1026 /**
1027   * @brief  return the DAC handle state
1028   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1029   *         the configuration information for the specified DAC.
1030   * @retval HAL state
1031   */
HAL_DAC_GetState(const DAC_HandleTypeDef * hdac)1032 HAL_DAC_StateTypeDef HAL_DAC_GetState(const DAC_HandleTypeDef *hdac)
1033 {
1034   /* Return DAC handle state */
1035   return hdac->State;
1036 }
1037 
1038 
1039 /**
1040   * @brief  Return the DAC error code
1041   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1042   *         the configuration information for the specified DAC.
1043   * @retval DAC Error Code
1044   */
HAL_DAC_GetError(const DAC_HandleTypeDef * hdac)1045 uint32_t HAL_DAC_GetError(const DAC_HandleTypeDef *hdac)
1046 {
1047   return hdac->ErrorCode;
1048 }
1049 
1050 /**
1051   * @}
1052   */
1053 
1054 /**
1055   * @}
1056   */
1057 
1058 /** @addtogroup DAC_Exported_Functions
1059   * @{
1060   */
1061 
1062 /** @addtogroup DAC_Exported_Functions_Group1
1063   * @{
1064   */
1065 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1066 /**
1067   * @brief  Register a User DAC Callback
1068   *         To be used instead of the weak (overridden) predefined callback
1069   * @note   The HAL_DAC_RegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to register
1070   *         callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
1071   * @param  hdac DAC handle
1072   * @param  CallbackID ID of the callback to be registered
1073   *         This parameter can be one of the following values:
1074   *          @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK   DAC Error Callback ID
1075   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID       DAC CH1 Complete Callback ID
1076   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID  DAC CH1 Half Complete Callback ID
1077   *          @arg @ref HAL_DAC_CH1_ERROR_ID             DAC CH1 Error Callback ID
1078   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID       DAC CH1 UnderRun Callback ID
1079   *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID       DAC CH2 Complete Callback ID
1080   *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID  DAC CH2 Half Complete Callback ID
1081   *          @arg @ref HAL_DAC_CH2_ERROR_ID             DAC CH2 Error Callback ID
1082   *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID       DAC CH2 UnderRun Callback ID
1083   *          @arg @ref HAL_DAC_MSPINIT_CB_ID            DAC MSP Init Callback ID
1084   *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID          DAC MSP DeInit Callback ID
1085   *
1086   * @param  pCallback pointer to the Callback function
1087   * @retval status
1088   */
HAL_DAC_RegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID,pDAC_CallbackTypeDef pCallback)1089 HAL_StatusTypeDef HAL_DAC_RegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID,
1090                                            pDAC_CallbackTypeDef pCallback)
1091 {
1092   HAL_StatusTypeDef status = HAL_OK;
1093 
1094   /* Check the DAC peripheral handle */
1095   if (hdac == NULL)
1096   {
1097     return HAL_ERROR;
1098   }
1099 
1100   if (pCallback == NULL)
1101   {
1102     /* Update the error code */
1103     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1104     return HAL_ERROR;
1105   }
1106 
1107   if (hdac->State == HAL_DAC_STATE_READY)
1108   {
1109     switch (CallbackID)
1110     {
1111       case HAL_DAC_CH1_COMPLETE_CB_ID :
1112         hdac->ConvCpltCallbackCh1 = pCallback;
1113         break;
1114       case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1115         hdac->ConvHalfCpltCallbackCh1 = pCallback;
1116         break;
1117       case HAL_DAC_CH1_ERROR_ID :
1118         hdac->ErrorCallbackCh1 = pCallback;
1119         break;
1120       case HAL_DAC_CH1_UNDERRUN_CB_ID :
1121         hdac->DMAUnderrunCallbackCh1 = pCallback;
1122         break;
1123 
1124       case HAL_DAC_CH2_COMPLETE_CB_ID :
1125         hdac->ConvCpltCallbackCh2 = pCallback;
1126         break;
1127       case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1128         hdac->ConvHalfCpltCallbackCh2 = pCallback;
1129         break;
1130       case HAL_DAC_CH2_ERROR_ID :
1131         hdac->ErrorCallbackCh2 = pCallback;
1132         break;
1133       case HAL_DAC_CH2_UNDERRUN_CB_ID :
1134         hdac->DMAUnderrunCallbackCh2 = pCallback;
1135         break;
1136 
1137       case HAL_DAC_MSPINIT_CB_ID :
1138         hdac->MspInitCallback = pCallback;
1139         break;
1140       case HAL_DAC_MSPDEINIT_CB_ID :
1141         hdac->MspDeInitCallback = pCallback;
1142         break;
1143       default :
1144         /* Update the error code */
1145         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1146         /* update return status */
1147         status =  HAL_ERROR;
1148         break;
1149     }
1150   }
1151   else if (hdac->State == HAL_DAC_STATE_RESET)
1152   {
1153     switch (CallbackID)
1154     {
1155       case HAL_DAC_MSPINIT_CB_ID :
1156         hdac->MspInitCallback = pCallback;
1157         break;
1158       case HAL_DAC_MSPDEINIT_CB_ID :
1159         hdac->MspDeInitCallback = pCallback;
1160         break;
1161       default :
1162         /* Update the error code */
1163         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1164         /* update return status */
1165         status =  HAL_ERROR;
1166         break;
1167     }
1168   }
1169   else
1170   {
1171     /* Update the error code */
1172     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1173     /* update return status */
1174     status =  HAL_ERROR;
1175   }
1176 
1177   return status;
1178 }
1179 
1180 /**
1181   * @brief  Unregister a User DAC Callback
1182   *         DAC Callback is redirected to the weak (overridden) predefined callback
1183   * @note   The HAL_DAC_UnRegisterCallback() may be called before HAL_DAC_Init() in HAL_DAC_STATE_RESET to un-register
1184   *         callbacks for HAL_DAC_MSPINIT_CB_ID and HAL_DAC_MSPDEINIT_CB_ID
1185   * @param  hdac DAC handle
1186   * @param  CallbackID ID of the callback to be unregistered
1187   *         This parameter can be one of the following values:
1188   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID          DAC CH1 transfer Complete Callback ID
1189   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID     DAC CH1 Half Complete Callback ID
1190   *          @arg @ref HAL_DAC_CH1_ERROR_ID                DAC CH1 Error Callback ID
1191   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID          DAC CH1 UnderRun Callback ID
1192   *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID          DAC CH2 Complete Callback ID
1193   *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID     DAC CH2 Half Complete Callback ID
1194   *          @arg @ref HAL_DAC_CH2_ERROR_ID                DAC CH2 Error Callback ID
1195   *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID          DAC CH2 UnderRun Callback ID
1196   *          @arg @ref HAL_DAC_MSPINIT_CB_ID               DAC MSP Init Callback ID
1197   *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID             DAC MSP DeInit Callback ID
1198   *          @arg @ref HAL_DAC_ALL_CB_ID                   DAC All callbacks
1199   * @retval status
1200   */
HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID)1201 HAL_StatusTypeDef HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
1202 {
1203   HAL_StatusTypeDef status = HAL_OK;
1204 
1205   /* Check the DAC peripheral handle */
1206   if (hdac == NULL)
1207   {
1208     return HAL_ERROR;
1209   }
1210 
1211   if (hdac->State == HAL_DAC_STATE_READY)
1212   {
1213     switch (CallbackID)
1214     {
1215       case HAL_DAC_CH1_COMPLETE_CB_ID :
1216         hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1217         break;
1218       case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1219         hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1220         break;
1221       case HAL_DAC_CH1_ERROR_ID :
1222         hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1223         break;
1224       case HAL_DAC_CH1_UNDERRUN_CB_ID :
1225         hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1226         break;
1227 
1228       case HAL_DAC_CH2_COMPLETE_CB_ID :
1229         hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1230         break;
1231       case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1232         hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1233         break;
1234       case HAL_DAC_CH2_ERROR_ID :
1235         hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1236         break;
1237       case HAL_DAC_CH2_UNDERRUN_CB_ID :
1238         hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1239         break;
1240 
1241       case HAL_DAC_MSPINIT_CB_ID :
1242         hdac->MspInitCallback = HAL_DAC_MspInit;
1243         break;
1244       case HAL_DAC_MSPDEINIT_CB_ID :
1245         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1246         break;
1247       case HAL_DAC_ALL_CB_ID :
1248         hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1249         hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1250         hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1251         hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1252 
1253         hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1254         hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1255         hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1256         hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1257 
1258         hdac->MspInitCallback = HAL_DAC_MspInit;
1259         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1260         break;
1261       default :
1262         /* Update the error code */
1263         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1264         /* update return status */
1265         status =  HAL_ERROR;
1266         break;
1267     }
1268   }
1269   else if (hdac->State == HAL_DAC_STATE_RESET)
1270   {
1271     switch (CallbackID)
1272     {
1273       case HAL_DAC_MSPINIT_CB_ID :
1274         hdac->MspInitCallback = HAL_DAC_MspInit;
1275         break;
1276       case HAL_DAC_MSPDEINIT_CB_ID :
1277         hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1278         break;
1279       default :
1280         /* Update the error code */
1281         hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1282         /* update return status */
1283         status =  HAL_ERROR;
1284         break;
1285     }
1286   }
1287   else
1288   {
1289     /* Update the error code */
1290     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1291     /* update return status */
1292     status =  HAL_ERROR;
1293   }
1294 
1295   return status;
1296 }
1297 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1298 
1299 /**
1300   * @}
1301   */
1302 
1303 /**
1304   * @}
1305   */
1306 
1307 /** @addtogroup DAC_Private_Functions
1308   * @{
1309   */
1310 
1311 /**
1312   * @brief  DMA conversion complete callback.
1313   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1314   *                the configuration information for the specified DMA module.
1315   * @retval None
1316   */
DAC_DMAConvCpltCh1(DMA_HandleTypeDef * hdma)1317 void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
1318 {
1319   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1320 
1321 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1322   hdac->ConvCpltCallbackCh1(hdac);
1323 #else
1324   HAL_DAC_ConvCpltCallbackCh1(hdac);
1325 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1326 
1327   hdac->State = HAL_DAC_STATE_READY;
1328 }
1329 
1330 /**
1331   * @brief  DMA half transfer complete callback.
1332   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1333   *                the configuration information for the specified DMA module.
1334   * @retval None
1335   */
DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef * hdma)1336 void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
1337 {
1338   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1339   /* Conversion complete callback */
1340 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1341   hdac->ConvHalfCpltCallbackCh1(hdac);
1342 #else
1343   HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
1344 #endif  /* USE_HAL_DAC_REGISTER_CALLBACKS */
1345 }
1346 
1347 /**
1348   * @brief  DMA error callback
1349   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1350   *                the configuration information for the specified DMA module.
1351   * @retval None
1352   */
DAC_DMAErrorCh1(DMA_HandleTypeDef * hdma)1353 void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
1354 {
1355   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1356 
1357   /* Set DAC error code to DMA error */
1358   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
1359 
1360 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1361   hdac->ErrorCallbackCh1(hdac);
1362 #else
1363   HAL_DAC_ErrorCallbackCh1(hdac);
1364 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1365 
1366   hdac->State = HAL_DAC_STATE_READY;
1367 }
1368 
1369 /**
1370   * @}
1371   */
1372 
1373 /**
1374   * @}
1375   */
1376 
1377 #endif /* DAC */
1378 
1379 #endif /* HAL_DAC_MODULE_ENABLED */
1380 /**
1381   * @}
1382   */
1383