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