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