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