1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_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   *           + Peripheral Control functions
10   *           + Peripheral State and Errors functions
11   *
12   *
13   ******************************************************************************
14   * @attention
15   *
16   * Copyright (c) 2017 STMicroelectronics.
17   * All rights reserved.
18   *
19   * This software is licensed under terms that can be found in the LICENSE file
20   * in the root directory of this software component.
21   * If no LICENSE file comes with this software, it is provided AS-IS.
22   *
23   ******************************************************************************
24   @verbatim
25   ==============================================================================
26                       ##### DAC Peripheral features #####
27   ==============================================================================
28     [..]
29       *** DAC Channels ***
30       ====================
31     [..]
32     STM32L4 devices integrate one or two 12-bit Digital Analog Converters
33     (i.e. one or 2 channel(s))
34     1 channel : STM32L451xx STM32L452xx STM32L462xx
35     2 channels: STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx
36                 STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx
37                 STM32L4P5xx STM32L4Q5xx
38                 STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx
39 
40     When 2 channels are available, the 2 converters (i.e. channel1 & channel2)
41     can be used independently or simultaneously (dual mode):
42       (#) DAC channel1 with DAC_OUT1 (PA4) as output or connected to on-chip
43           peripherals.
44       (#) Whenever present, DAC channel2 with DAC_OUT2 (PA5) as output
45           or connected to on-chip peripherals.
46 
47       *** DAC Triggers ***
48       ====================
49     [..]
50     Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
51     and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
52     [..]
53     Digital to Analog conversion can be triggered by:
54       (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
55           The used pin (GPIOx_PIN_9) must be configured in input mode.
56 
57       (#) Timers TRGO: TIM2, TIM3, TIM4, TIM5, TIM6 and TIM7
58           (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T3_TRGO...)
59 
60       (#) Software using DAC_TRIGGER_SOFTWARE
61 
62       *** DAC Buffer mode feature ***
63       ===============================
64       [..]
65       Each DAC channel integrates an output buffer that can be used to
66       reduce the output impedance, and to drive external loads directly
67       without having to add an external operational amplifier.
68       To enable, the output buffer use
69       sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
70       [..]
71       (@) Refer to the device datasheet for more details about output
72           impedance value with and without output buffer.
73 
74       *** DAC connect feature ***
75       ===============================
76       [..]
77       Each DAC channel can be connected internally.
78       To connect, use
79       sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
80 
81       *** GPIO configurations guidelines ***
82       =====================
83       [..]
84       When a DAC channel is used (ex channel1 on PA4) and the other is not
85       (ex channel2 on PA5 is configured in Analog and disabled).
86       Channel1 may disturb channel2 as coupling effect.
87       Note that there is no coupling on channel2 as soon as channel2 is turned on.
88       Coupling on adjacent channel could be avoided as follows:
89       when unused PA5 is configured as INPUT PULL-UP or DOWN.
90       PA5 is configured in ANALOG just before it is turned on.
91 
92       *** DAC Sample and Hold feature ***
93       ========================
94       [..]
95       For each converter, 2 modes are supported: normal mode and
96       "sample and hold" mode (i.e. low power mode).
97       In the sample and hold mode, the DAC core converts data, then holds the
98       converted voltage on a capacitor. When not converting, the DAC cores and
99       buffer are completely turned off between samples and the DAC output is
100       tri-stated, therefore  reducing the overall power consumption. A new
101       stabilization period is needed before each new conversion.
102 
103       The sample and hold allow setting internal or external voltage @
104       low power consumption cost (output value can be at any given rate either
105       by CPU or DMA).
106 
107       The Sample and hold block and registers uses either LSI & run in
108       several power modes: run mode, sleep mode, low power run, low power sleep
109       mode & stop1 mode.
110 
111       Low power stop1 mode allows only static conversion.
112 
113       To enable Sample and Hold mode
114       Enable LSI using HAL_RCC_OscConfig with RCC_OSCILLATORTYPE_LSI &
115       RCC_LSI_ON parameters.
116 
117       Use DAC_InitStructure.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_ENABLE;
118          & DAC_ChannelConfTypeDef.DAC_SampleAndHoldConfig.DAC_SampleTime,
119            DAC_HoldTime & DAC_RefreshTime;
120 
121        *** DAC calibration feature ***
122        ===================================
123       [..]
124        (#)  The 2 converters (channel1 & channel2) provide calibration capabilities.
125        (++) Calibration aims at correcting some offset of output buffer.
126        (++) The DAC uses either factory calibration settings OR user defined
127            calibration (trimming) settings (i.e. trimming mode).
128        (++) The user defined settings can be figured out using self calibration
129            handled by HAL_DACEx_SelfCalibrate.
130        (++) HAL_DACEx_SelfCalibrate:
131        (+++) Runs automatically the calibration.
132        (+++) Enables the user trimming mode
133        (+++) Updates a structure with trimming values with fresh calibration
134             results.
135             The user may store the calibration results for larger
136             (ex monitoring the trimming as a function of temperature
137             for instance)
138 
139        *** DAC wave generation feature ***
140        ===================================
141        [..]
142        Both DAC channels can be used to generate
143          (#) Noise wave
144          (#) Triangle wave
145 
146        *** DAC data format ***
147        =======================
148        [..]
149        The DAC data format can be:
150          (#) 8-bit right alignment using DAC_ALIGN_8B_R
151          (#) 12-bit left alignment using DAC_ALIGN_12B_L
152          (#) 12-bit right alignment using DAC_ALIGN_12B_R
153 
154        *** DAC data value to voltage correspondence ***
155        ================================================
156        [..]
157        The analog output voltage on each DAC channel pin is determined
158        by the following equation:
159        [..]
160        DAC_OUTx = VREF+ * DOR / 4095
161        (+) with  DOR is the Data Output Register
162        [..]
163           VEF+ is the input voltage reference (refer to the device datasheet)
164        [..]
165         e.g. To set DAC_OUT1 to 0.7V, use
166        (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
167 
168        *** DMA requests ***
169        =====================
170        [..]
171        A DMA1 request can be generated when an external trigger (but not a software trigger)
172        occurs if DMA1 requests are enabled using HAL_DAC_Start_DMA().
173        DMA requests are mapped as following:
174        (#) When DMAMUX is NOT present:
175            DMA1 requests are mapped as following:
176              (+) DAC channel1 mapped on DMA1 request 6 / channel3
177              (+) DAC channel2 mapped on DMA1 request 5 / channel4
178            DMA2 requests are mapped as following:
179              (+) DAC channel1 mapped on DMA2 request 3 / channel4
180              (+) DAC channel2 mapped on DMA2 request 3 / channel5
181        (#) When DMAMUX is present:
182              (+) DAC channel1 mapped on DMA1/DMA2 request 6 (can be any DMA channel)
183              (+) DAC channel2 mapped on DMA1/DMA2 request 7 (can be any DMA channel)
184 
185        *** High frequency interface mode ***
186        =====================================
187        [..]
188        The high frequency interface informs DAC instance about the bus frequency in use.
189        It is mandatory information for DAC (as internal timing of DAC is bus frequency dependent)
190        provided thanks to parameter DAC_HighFrequency handled in HAL_DAC_ConfigChannel () function.
191        Use of DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC value of DAC_HighFrequency is recommended
192        function figured out the correct setting.
193        The high frequency mode is same for all converters of a same DAC instance. Either same
194        parameter DAC_HighFrequency is used for all DAC converters or again self
195        DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC detection parameter.
196 
197      [..]
198     (@) For Dual mode and specific signal (Triangle and noise) generation please
199         refer to Extended Features Driver description
200 
201                       ##### How to use this driver #####
202   ==============================================================================
203     [..]
204       (+) DAC APB clock must be enabled to get write access to DAC
205           registers using HAL_DAC_Init()
206       (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
207       (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
208       (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA() functions.
209 
210      *** Calibration mode IO operation ***
211      ======================================
212      [..]
213        (+) Retrieve the factory trimming (calibration settings) using HAL_DACEx_GetTrimOffset()
214        (+) Run the calibration using HAL_DACEx_SelfCalibrate()
215        (+) Update the trimming while DAC running using HAL_DACEx_SetUserTrimming()
216 
217      *** Polling mode IO operation ***
218      =================================
219      [..]
220        (+) Start the DAC peripheral using HAL_DAC_Start()
221        (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
222        (+) Stop the DAC peripheral using HAL_DAC_Stop()
223 
224      *** DMA mode IO operation ***
225      ==============================
226      [..]
227        (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
228            of data to be transferred at each end of conversion
229            First issued trigger will start the conversion of the value previously set by HAL_DAC_SetValue().
230        (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
231            function is executed and user can add his own code by customization of function pointer
232            HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
233        (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
234            function is executed and user can add his own code by customization of function pointer
235            HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
236        (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
237             add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
238        (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
239            HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2()
240            function is executed and user can add his own code by customization of function pointer
241            HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2() and
242            add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1()
243        (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
244 
245     *** Callback registration ***
246     =============================================
247     [..]
248       The compilation define  USE_HAL_DAC_REGISTER_CALLBACKS when set to 1
249       allows the user to configure dynamically the driver callbacks.
250 
251     Use Functions HAL_DAC_RegisterCallback() to register a user callback,
252       it allows to register following callbacks:
253       (+) ConvCpltCallbackCh1     : callback when a half transfer is completed on Ch1.
254       (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
255       (+) ErrorCallbackCh1        : callback when an error occurs on Ch1.
256       (+) DMAUnderrunCallbackCh1  : callback when an underrun error occurs on Ch1.
257       (+) ConvCpltCallbackCh2     : callback when a half transfer is completed on Ch2.
258       (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
259       (+) ErrorCallbackCh2        : callback when an error occurs on Ch2.
260       (+) DMAUnderrunCallbackCh2  : callback when an underrun error occurs on Ch2.
261       (+) MspInitCallback         : DAC MspInit.
262       (+) MspDeInitCallback       : DAC MspdeInit.
263       This function takes as parameters the HAL peripheral handle, the Callback ID
264       and a pointer to the user callback function.
265 
266     Use function HAL_DAC_UnRegisterCallback() to reset a callback to the default
267       weak (surcharged) function. It allows to reset following callbacks:
268       (+) ConvCpltCallbackCh1     : callback when a half transfer is completed on Ch1.
269       (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
270       (+) ErrorCallbackCh1        : callback when an error occurs on Ch1.
271       (+) DMAUnderrunCallbackCh1  : callback when an underrun error occurs on Ch1.
272       (+) ConvCpltCallbackCh2     : callback when a half transfer is completed on Ch2.
273       (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
274       (+) ErrorCallbackCh2        : callback when an error occurs on Ch2.
275       (+) DMAUnderrunCallbackCh2  : callback when an underrun error occurs on Ch2.
276       (+) MspInitCallback         : DAC MspInit.
277       (+) MspDeInitCallback       : DAC MspdeInit.
278       (+) All Callbacks
279       This function) takes as parameters the HAL peripheral handle and the Callback ID.
280 
281       By default, after the HAL_DAC_Init and if the state is HAL_DAC_STATE_RESET
282       all callbacks are reset to the corresponding legacy weak (surcharged) functions.
283       Exception done for MspInit and MspDeInit callbacks that are respectively
284       reset to the legacy weak (surcharged) functions in the HAL_DAC_Init
285       and HAL_DAC_DeInit only when these callbacks are null (not registered beforehand).
286       If not, MspInit or MspDeInit are not null, the HAL_DAC_Init and HAL_DAC_DeInit
287       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
288 
289       Callbacks can be registered/unregistered in READY state only.
290       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
291       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
292       during the Init/DeInit.
293       In that case first register the MspInit/MspDeInit user callbacks
294       using HAL_DAC_RegisterCallback before calling HAL_DAC_DeInit
295       or HAL_DAC_Init function.
296 
297       When The compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or
298       not defined, the callback registering feature is not available
299       and weak (surcharged) callbacks are used.
300 
301      *** DAC HAL driver macros list ***
302      =============================================
303      [..]
304        Below the list of most used macros in DAC HAL driver.
305 
306       (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
307       (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
308       (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
309       (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
310 
311      [..]
312       (@) You can refer to the DAC HAL driver header file for more useful macros
313 
314  @endverbatim
315   ******************************************************************************
316   */
317 
318 /* Includes ------------------------------------------------------------------*/
319 #include "stm32l4xx_hal.h"
320 
321 /** @addtogroup STM32L4xx_HAL_Driver
322   * @{
323   */
324 
325 #ifdef HAL_DAC_MODULE_ENABLED
326 #if defined(DAC1)
327 
328   /** @defgroup DAC DAC
329   * @brief DAC driver modules
330   * @{
331   */
332 
333 /* Private typedef -----------------------------------------------------------*/
334 /* Private define ------------------------------------------------------------*/
335 /* Private constants ---------------------------------------------------------*/
336 /** @addtogroup DAC_Private_Constants DAC Private Constants
337   * @{
338   */
339 #define TIMEOUT_DAC_CALIBCONFIG        1U         /* 1   ms        */
340 #define HFSEL_ENABLE_THRESHOLD_80MHZ   80000000U  /* 80 MHz        */
341 
342 /**
343   * @}
344   */
345 
346 /* Private macro -------------------------------------------------------------*/
347 /* Private variables ---------------------------------------------------------*/
348 /* Private function prototypes -----------------------------------------------*/
349 /* Exported functions -------------------------------------------------------*/
350 
351 /** @defgroup DAC_Exported_Functions DAC Exported Functions
352   * @{
353   */
354 
355 /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
356  *  @brief    Initialization and Configuration functions
357  *
358 @verbatim
359   ==============================================================================
360               ##### Initialization and de-initialization functions #####
361   ==============================================================================
362     [..]  This section provides functions allowing to:
363       (+) Initialize and configure the DAC.
364       (+) De-initialize the DAC.
365 
366 @endverbatim
367   * @{
368   */
369 
370 /**
371   * @brief  Initialize the DAC peripheral according to the specified parameters
372   *         in the DAC_InitStruct and initialize the associated handle.
373   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
374   *         the configuration information for the specified DAC.
375   * @retval HAL status
376   */
HAL_DAC_Init(DAC_HandleTypeDef * hdac)377 HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef *hdac)
378 {
379   /* Check DAC handle */
380   if (hdac == NULL)
381   {
382      return HAL_ERROR;
383   }
384   /* Check the parameters */
385   assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
386 
387   if (hdac->State == HAL_DAC_STATE_RESET)
388   {
389 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
390     /* Init the DAC Callback settings */
391     hdac->ConvCpltCallbackCh1           = HAL_DAC_ConvCpltCallbackCh1;
392     hdac->ConvHalfCpltCallbackCh1       = HAL_DAC_ConvHalfCpltCallbackCh1;
393     hdac->ErrorCallbackCh1              = HAL_DAC_ErrorCallbackCh1;
394     hdac->DMAUnderrunCallbackCh1        = HAL_DAC_DMAUnderrunCallbackCh1;
395 
396 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
397     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
398     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
399     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
400     hdac->ConvCpltCallbackCh2           = HAL_DACEx_ConvCpltCallbackCh2;
401     hdac->ConvHalfCpltCallbackCh2       = HAL_DACEx_ConvHalfCpltCallbackCh2;
402     hdac->ErrorCallbackCh2              = HAL_DACEx_ErrorCallbackCh2;
403     hdac->DMAUnderrunCallbackCh2        = HAL_DACEx_DMAUnderrunCallbackCh2;
404 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
405         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
406         /* STM32L4P5xx STM32L4Q5xx                                                             */
407         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
408 
409     if (hdac->MspInitCallback == NULL)
410     {
411       hdac->MspInitCallback             = HAL_DAC_MspInit;
412     }
413 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
414 
415     /* Allocate lock resource and initialize it */
416     hdac->Lock = HAL_UNLOCKED;
417 
418 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
419     /* Init the low level hardware */
420     hdac->MspInitCallback(hdac);
421 #else
422     /* Init the low level hardware */
423     HAL_DAC_MspInit(hdac);
424 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
425   }
426 
427   /* Initialize the DAC state*/
428   hdac->State = HAL_DAC_STATE_BUSY;
429 
430   /* Set DAC error code to none */
431   hdac->ErrorCode = HAL_DAC_ERROR_NONE;
432 
433   /* Initialize the DAC state*/
434   hdac->State = HAL_DAC_STATE_READY;
435 
436   /* Return function status */
437   return HAL_OK;
438 }
439 
440 /**
441   * @brief  Deinitialize the DAC peripheral registers to their default reset values.
442   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
443   *         the configuration information for the specified DAC.
444   * @retval HAL status
445   */
HAL_DAC_DeInit(DAC_HandleTypeDef * hdac)446 HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef *hdac)
447 {
448   /* Check DAC handle */
449   if (hdac == NULL)
450   {
451     return HAL_ERROR;
452   }
453 
454   /* Check the parameters */
455   assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
456 
457   /* Change DAC state */
458   hdac->State = HAL_DAC_STATE_BUSY;
459 
460 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
461   if (hdac->MspDeInitCallback == NULL)
462   {
463     hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
464   }
465   /* DeInit the low level hardware */
466   hdac->MspDeInitCallback(hdac);
467 #else
468   /* DeInit the low level hardware */
469   HAL_DAC_MspDeInit(hdac);
470 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
471 
472   /* Set DAC error code to none */
473   hdac->ErrorCode = HAL_DAC_ERROR_NONE;
474 
475   /* Change DAC state */
476   hdac->State = HAL_DAC_STATE_RESET;
477 
478   /* Release Lock */
479   __HAL_UNLOCK(hdac);
480 
481   /* Return function status */
482   return HAL_OK;
483 }
484 
485 /**
486   * @brief  Initialize the DAC MSP.
487   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
488   *         the configuration information for the specified DAC.
489   * @retval None
490   */
HAL_DAC_MspInit(DAC_HandleTypeDef * hdac)491 __weak void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
492 {
493   /* Prevent unused argument(s) compilation warning */
494   UNUSED(hdac);
495 
496   /* NOTE : This function should not be modified, when the callback is needed,
497             the HAL_DAC_MspInit could be implemented in the user file
498    */
499 }
500 
501 /**
502   * @brief  DeInitialize the DAC MSP.
503   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
504   *         the configuration information for the specified DAC.
505   * @retval None
506   */
HAL_DAC_MspDeInit(DAC_HandleTypeDef * hdac)507 __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
508 {
509   /* Prevent unused argument(s) compilation warning */
510   UNUSED(hdac);
511 
512   /* NOTE : This function should not be modified, when the callback is needed,
513             the HAL_DAC_MspDeInit could be implemented in the user file
514    */
515 }
516 
517 /**
518   * @}
519   */
520 
521 /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
522  *  @brief    IO operation functions
523  *
524 @verbatim
525   ==============================================================================
526              ##### IO operation functions #####
527   ==============================================================================
528     [..]  This section provides functions allowing to:
529       (+) Start conversion.
530       (+) Stop conversion.
531       (+) Start conversion and enable DMA transfer.
532       (+) Stop conversion and disable DMA transfer.
533       (+) Get result of conversion.
534 
535 @endverbatim
536   * @{
537   */
538 
539 /**
540   * @brief  Enables DAC and starts conversion of channel.
541   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
542   *         the configuration information for the specified DAC.
543   * @param  Channel The selected DAC channel.
544   *          This parameter can be one of the following values:
545   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
546   *            @arg DAC_CHANNEL_2: DAC Channel2 selected (when supported)
547   * @retval HAL status
548   */
HAL_DAC_Start(DAC_HandleTypeDef * hdac,uint32_t Channel)549 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef *hdac, uint32_t Channel)
550 {
551   /* Check the parameters */
552   assert_param(IS_DAC_CHANNEL(Channel));
553 
554   /* Process locked */
555   __HAL_LOCK(hdac);
556 
557   /* Change DAC state */
558   hdac->State = HAL_DAC_STATE_BUSY;
559 
560   /* Enable the Peripheral */
561   __HAL_DAC_ENABLE(hdac, Channel);
562 
563 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
564   if (Channel == DAC_CHANNEL_1)
565   {
566     /* Check if software trigger enabled */
567     if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
568     {
569       /* Enable the selected DAC software conversion */
570       SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
571     }
572   }
573   else
574   {
575     /* Check if software trigger enabled */
576     if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << (Channel & 0x10UL)))
577     {
578       /* Enable the selected DAC software conversion*/
579       SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
580     }
581   }
582 
583 #endif /* STM32L4P5xx STM32L4Q5xx STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx                                     */
584 
585 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
586     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx)
587   if(Channel == DAC_CHANNEL_1)
588   {
589     /* Check if software trigger enabled */
590     if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_CR_TEN1)
591     {
592       /* Enable the selected DAC software conversion */
593       SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
594     }
595   }
596   else
597   {
598     /* Check if software trigger enabled */
599     if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == DAC_CR_TEN2)
600     {
601       /* Enable the selected DAC software conversion*/
602       SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
603     }
604   }
605 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
606         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
607 
608 
609 #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
610   /* Check if software trigger enabled */
611   if((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == (DAC_CR_TEN1 | DAC_CR_TSEL1))
612   {
613     /* Enable the selected DAC software conversion */
614     SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
615   }
616 #endif /* STM32L451xx STM32L452xx STM32L462xx */
617   /* Change DAC state */
618   hdac->State = HAL_DAC_STATE_READY;
619 
620   /* Process unlocked */
621   __HAL_UNLOCK(hdac);
622 
623   /* Return function status */
624   return HAL_OK;
625 }
626 
627 /**
628   * @brief  Disables DAC and stop conversion of channel.
629   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
630   *         the configuration information for the specified DAC.
631   * @param  Channel The selected DAC channel.
632   *          This parameter can be one of the following values:
633   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
634   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
635   * @retval HAL status
636   */
HAL_DAC_Stop(DAC_HandleTypeDef * hdac,uint32_t Channel)637 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef *hdac, uint32_t Channel)
638 {
639   /* Check the parameters */
640   assert_param(IS_DAC_CHANNEL(Channel));
641 
642   /* Disable the Peripheral */
643   __HAL_DAC_DISABLE(hdac, Channel);
644 
645   /* Change DAC state */
646   hdac->State = HAL_DAC_STATE_READY;
647 
648   /* Return function status */
649   return HAL_OK;
650 }
651 
652 #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
653 /**
654   * @brief  Enables DAC and starts conversion of channel.
655   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
656   *         the configuration information for the specified DAC.
657   * @param  Channel The selected DAC channel.
658   *          This parameter can be one of the following values:
659   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
660   * @param  pData The destination peripheral Buffer address.
661   * @param  Length The length of data to be transferred from memory to DAC peripheral
662   * @param  Alignment Specifies the data alignment for DAC channel.
663   *          This parameter can be one of the following values:
664   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
665   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
666   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
667   * @retval HAL status
668   */
HAL_DAC_Start_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t * pData,uint32_t Length,uint32_t Alignment)669 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t *pData, uint32_t Length,
670                                     uint32_t Alignment)
671 {
672   HAL_StatusTypeDef status;
673   uint32_t tmpreg = 0U;
674 
675   /* Check the parameters */
676   assert_param(IS_DAC_CHANNEL(Channel));
677   assert_param(IS_DAC_ALIGN(Alignment));
678 
679   /* Process locked */
680   __HAL_LOCK(hdac);
681 
682   /* Change DAC state */
683   hdac->State = HAL_DAC_STATE_BUSY;
684 
685   /* Set the DMA transfer complete callback for channel1 */
686   hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
687 
688   /* Set the DMA half transfer complete callback for channel1 */
689   hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
690 
691   /* Set the DMA error callback for channel1 */
692   hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
693 
694   /* Enable the selected DAC channel1 DMA request */
695   SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
696 
697   /* Case of use of channel 1 */
698   switch (Alignment)
699   {
700     case DAC_ALIGN_12B_R:
701       /* Get DHR12R1 address */
702       tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
703       break;
704     case DAC_ALIGN_12B_L:
705       /* Get DHR12L1 address */
706       tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
707       break;
708     case DAC_ALIGN_8B_R:
709       /* Get DHR8R1 address */
710       tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
711       break;
712     default:
713       break;
714   }
715 
716   /* Enable the DMA channel */
717   /* Enable the DAC DMA underrun interrupt */
718   __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
719 
720   /* Enable the DMA channel */
721   status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
722 
723   /* Process Unlocked */
724   __HAL_UNLOCK(hdac);
725 
726   if (status == HAL_OK)
727   {
728     /* Enable the Peripheral */
729     __HAL_DAC_ENABLE(hdac, Channel);
730   }
731   else
732   {
733     hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
734   }
735 
736   /* Return function status */
737   return status;
738 }
739 #endif /* STM32L451xx STM32L452xx STM32L462xx */
740 
741 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
742     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
743     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
744     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
745 
746 /**
747   * @brief  Enables DAC and starts conversion of channel.
748   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
749   *         the configuration information for the specified DAC.
750   * @param  Channel The selected DAC channel.
751   *          This parameter can be one of the following values:
752   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
753   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
754   * @param  pData The destination peripheral Buffer address.
755   * @param  Length The length of data to be transferred from memory to DAC peripheral
756   * @param  Alignment Specifies the data alignment for DAC channel.
757   *          This parameter can be one of the following values:
758   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
759   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
760   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
761   * @retval HAL status
762   */
HAL_DAC_Start_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t * pData,uint32_t Length,uint32_t Alignment)763 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t *pData, uint32_t Length,
764                                     uint32_t Alignment)
765 {
766   HAL_StatusTypeDef status;
767   uint32_t tmpreg = 0U;
768 
769   /* Check the parameters */
770   assert_param(IS_DAC_CHANNEL(Channel));
771   assert_param(IS_DAC_ALIGN(Alignment));
772 
773   /* Process locked */
774   __HAL_LOCK(hdac);
775 
776   /* Change DAC state */
777   hdac->State = HAL_DAC_STATE_BUSY;
778 
779   if (Channel == DAC_CHANNEL_1)
780   {
781     /* Set the DMA transfer complete callback for channel1 */
782     hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
783 
784     /* Set the DMA half transfer complete callback for channel1 */
785     hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
786 
787     /* Set the DMA error callback for channel1 */
788     hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
789 
790     /* Enable the selected DAC channel1 DMA request */
791     SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
792 
793     /* Case of use of channel 1 */
794     switch (Alignment)
795     {
796       case DAC_ALIGN_12B_R:
797         /* Get DHR12R1 address */
798         tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
799         break;
800       case DAC_ALIGN_12B_L:
801         /* Get DHR12L1 address */
802         tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
803         break;
804       case DAC_ALIGN_8B_R:
805         /* Get DHR8R1 address */
806         tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
807         break;
808       default:
809         break;
810     }
811   }
812   else
813   {
814     /* Set the DMA transfer complete callback for channel2 */
815     hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
816 
817     /* Set the DMA half transfer complete callback for channel2 */
818     hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
819 
820     /* Set the DMA error callback for channel2 */
821     hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
822 
823     /* Enable the selected DAC channel2 DMA request */
824     SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
825 
826     /* Case of use of channel 2 */
827     switch (Alignment)
828     {
829       case DAC_ALIGN_12B_R:
830         /* Get DHR12R2 address */
831         tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
832         break;
833       case DAC_ALIGN_12B_L:
834         /* Get DHR12L2 address */
835         tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
836         break;
837       case DAC_ALIGN_8B_R:
838         /* Get DHR8R2 address */
839         tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
840         break;
841       default:
842         break;
843     }
844   }
845 
846   /* Enable the DMA channel */
847   if (Channel == DAC_CHANNEL_1)
848   {
849     /* Enable the DAC DMA underrun interrupt */
850     __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
851 
852     /* Enable the DMA channel */
853     status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
854   }
855   else
856   {
857     /* Enable the DAC DMA underrun interrupt */
858     __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
859 
860     /* Enable the DMA channel */
861     status = HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
862   }
863 
864   /* Process Unlocked */
865   __HAL_UNLOCK(hdac);
866 
867   if (status == HAL_OK)
868   {
869     /* Enable the Peripheral */
870     __HAL_DAC_ENABLE(hdac, Channel);
871   }
872   else
873   {
874     hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
875   }
876 
877   /* Return function status */
878   return status;
879 }
880 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
881         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
882         /* STM32L4P5xx STM32L4Q5xx                                                             */
883         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
884 
885 /**
886   * @brief  Disables DAC and stop conversion of channel.
887   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
888   *         the configuration information for the specified DAC.
889   * @param  Channel The selected DAC channel.
890   *          This parameter can be one of the following values:
891   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
892   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
893   * @retval HAL status
894   */
HAL_DAC_Stop_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel)895 HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel)
896 {
897   /* Check the parameters */
898   assert_param(IS_DAC_CHANNEL(Channel));
899 
900   /* Disable the selected DAC channel DMA request */
901   hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << (Channel & 0x10UL));
902 
903   /* Disable the Peripheral */
904   __HAL_DAC_DISABLE(hdac, Channel);
905 
906   /* Disable the DMA channel */
907 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
908     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
909     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
910     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
911   /* Channel1 is used */
912   if (Channel == DAC_CHANNEL_1)
913   {
914     /* Disable the DMA channel */
915     (void)HAL_DMA_Abort(hdac->DMA_Handle1);
916 
917     /* Disable the DAC DMA underrun interrupt */
918     __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
919   }
920   else /* Channel2 is used for */
921   {
922     /* Disable the DMA channel */
923     (void)HAL_DMA_Abort(hdac->DMA_Handle2);
924 
925     /* Disable the DAC DMA underrun interrupt */
926     __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR2);
927   }
928 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
929         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
930         /* STM32L4P5xx STM32L4Q5xx                                                             */
931         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
932 
933 #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
934   /* Disable the DMA channel */
935   (void)HAL_DMA_Abort(hdac->DMA_Handle1);
936 
937   /* Disable the DAC DMA underrun interrupt */
938   __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
939 #endif /* STM32L451xx STM32L452xx STM32L462xx */
940 
941   /* Return function status */
942   return HAL_OK;
943 }
944 
945 /* DAC channel 2 is available on top of DAC channel 1 in */
946 /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
947 /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
948 
949 /**
950   * @brief  Handles DAC interrupt request
951   *         This function uses the interruption of DMA
952   *         underrun.
953   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
954   *         the configuration information for the specified DAC.
955   * @retval None
956   */
HAL_DAC_IRQHandler(DAC_HandleTypeDef * hdac)957 void HAL_DAC_IRQHandler(DAC_HandleTypeDef *hdac)
958 {
959   if (__HAL_DAC_GET_IT_SOURCE(hdac, DAC_IT_DMAUDR1))
960   {
961     /* Check underrun flag of DAC channel 1 */
962     if (__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
963     {
964       /* Change DAC state to error state */
965       hdac->State = HAL_DAC_STATE_ERROR;
966 
967       /* Set DAC error code to chanel1 DMA underrun error */
968       SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH1);
969 
970       /* Clear the underrun flag */
971       __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR1);
972 
973       /* Disable the selected DAC channel1 DMA request */
974       CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
975 
976       /* Error callback */
977 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
978       hdac->DMAUnderrunCallbackCh1(hdac);
979 #else
980       HAL_DAC_DMAUnderrunCallbackCh1(hdac);
981 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
982     }
983   }
984 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
985     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
986     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
987     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
988   if(__HAL_DAC_GET_IT_SOURCE(hdac, DAC_IT_DMAUDR2))
989   {
990     /* Check underrun flag of DAC channel 2 */
991     if (__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
992     {
993       /* Change DAC state to error state */
994       hdac->State = HAL_DAC_STATE_ERROR;
995 
996       /* Set DAC error code to channel2 DMA underrun error */
997       SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH2);
998 
999       /* Clear the underrun flag */
1000       __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR2);
1001 
1002       /* Disable the selected DAC channel2 DMA request */
1003       CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
1004 
1005       /* Error callback */
1006 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1007       hdac->DMAUnderrunCallbackCh2(hdac);
1008 #else
1009       HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
1010 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1011     }
1012   }
1013 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
1014         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
1015         /* STM32L4P5xx STM32L4Q5xx                                                             */
1016         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
1017 }
1018 
1019 /**
1020   * @brief  Set the specified data holding register value for DAC channel.
1021   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1022   *         the configuration information for the specified DAC.
1023   * @param  Channel The selected DAC channel.
1024   *          This parameter can be one of the following values:
1025   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
1026   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
1027   * @param  Alignment Specifies the data alignment.
1028   *          This parameter can be one of the following values:
1029   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
1030   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
1031   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
1032   * @param  Data Data to be loaded in the selected data holding register.
1033   * @retval HAL status
1034   */
HAL_DAC_SetValue(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t Alignment,uint32_t Data)1035 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
1036 {
1037   __IO uint32_t tmp = 0;
1038 
1039   /* Check the parameters */
1040   assert_param(IS_DAC_CHANNEL(Channel));
1041   assert_param(IS_DAC_ALIGN(Alignment));
1042   assert_param(IS_DAC_DATA(Data));
1043 
1044   tmp = (uint32_t)hdac->Instance;
1045   if (Channel == DAC_CHANNEL_1)
1046   {
1047     tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
1048   }
1049   else
1050   {
1051     tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
1052   }
1053 
1054   /* Set the DAC channel selected data holding register */
1055   *(__IO uint32_t *) tmp = Data;
1056 
1057   /* Return function status */
1058   return HAL_OK;
1059 }
1060 
1061 /**
1062   * @brief  Conversion complete callback in non-blocking mode for Channel1
1063   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1064   *         the configuration information for the specified DAC.
1065   * @retval None
1066   */
HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef * hdac)1067 __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
1068 {
1069   /* Prevent unused argument(s) compilation warning */
1070   UNUSED(hdac);
1071 
1072   /* NOTE : This function should not be modified, when the callback is needed,
1073             the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
1074    */
1075 }
1076 
1077 /**
1078   * @brief  Conversion half DMA transfer callback in non-blocking mode for Channel1
1079   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1080   *         the configuration information for the specified DAC.
1081   * @retval None
1082   */
HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef * hdac)1083 __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
1084 {
1085   /* Prevent unused argument(s) compilation warning */
1086   UNUSED(hdac);
1087 
1088   /* NOTE : This function should not be modified, when the callback is needed,
1089             the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
1090    */
1091 }
1092 
1093 /**
1094   * @brief  Error DAC callback for Channel1.
1095   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1096   *         the configuration information for the specified DAC.
1097   * @retval None
1098   */
HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef * hdac)1099 __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
1100 {
1101   /* Prevent unused argument(s) compilation warning */
1102   UNUSED(hdac);
1103 
1104   /* NOTE : This function should not be modified, when the callback is needed,
1105             the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
1106    */
1107 }
1108 
1109 /**
1110   * @brief  DMA underrun DAC callback for channel1.
1111   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1112   *         the configuration information for the specified DAC.
1113   * @retval None
1114   */
HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef * hdac)1115 __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
1116 {
1117   /* Prevent unused argument(s) compilation warning */
1118   UNUSED(hdac);
1119 
1120   /* NOTE : This function should not be modified, when the callback is needed,
1121             the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
1122    */
1123 }
1124 
1125 /**
1126   * @}
1127   */
1128 
1129 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
1130  *  @brief    Peripheral Control functions
1131  *
1132 @verbatim
1133   ==============================================================================
1134              ##### Peripheral Control functions #####
1135   ==============================================================================
1136     [..]  This section provides functions allowing to:
1137       (+) Configure channels.
1138       (+) Set the specified data holding register value for DAC channel.
1139 
1140 @endverbatim
1141   * @{
1142   */
1143 
1144 /**
1145   * @brief  Returns the last data output value of the selected DAC channel.
1146   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1147   *         the configuration information for the specified DAC.
1148   * @param  Channel The selected DAC channel.
1149   *          This parameter can be one of the following values:
1150   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
1151   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
1152   * @retval The selected DAC channel data output value.
1153   */
HAL_DAC_GetValue(DAC_HandleTypeDef * hdac,uint32_t Channel)1154 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef *hdac, uint32_t Channel)
1155 {
1156   /* Check the parameters */
1157   assert_param(IS_DAC_CHANNEL(Channel));
1158 
1159   /* Returns the DAC channel data output register value */
1160 #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
1161   /* Prevent unused argument(s) compilation warning if no assert_param check */
1162   UNUSED(Channel);
1163 
1164   return hdac->Instance->DOR1;
1165 #endif /* STM32L451xx STM32L452xx STM32L462xx */
1166 
1167 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
1168     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
1169     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
1170     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1171   if(Channel == DAC_CHANNEL_1)
1172   {
1173     return hdac->Instance->DOR1;
1174   }
1175   else
1176   {
1177     return hdac->Instance->DOR2;
1178   }
1179 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
1180         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
1181         /* STM32L4P5xx STM32L4Q5xx                                                             */
1182         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
1183 }
1184 
1185 /**
1186   * @brief  Configures the selected DAC channel.
1187   * @note   By calling this function, the high frequency interface mode (HFSEL bits)
1188   *         will be set. This parameter scope is the DAC instance. As the function
1189   *         is called for each channel, the @ref DAC_HighFrequency of @arg sConfig
1190   *         must be the same at each call.
1191   *         (or DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC self detect).
1192   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1193   *         the configuration information for the specified DAC.
1194   * @param  sConfig DAC configuration structure.
1195   * @param  Channel The selected DAC channel.
1196   *          This parameter can be one of the following values:
1197   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
1198   *            @arg DAC_CHANNEL_2: DAC Channel2 selected (Whenever present)
1199   * @retval HAL status
1200   */
HAL_DAC_ConfigChannel(DAC_HandleTypeDef * hdac,DAC_ChannelConfTypeDef * sConfig,uint32_t Channel)1201 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
1202 {
1203   uint32_t tmpreg1;
1204   uint32_t tmpreg2;
1205   uint32_t tickstart = 0U;
1206 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1207   uint32_t hclkfreq;
1208 #endif /* STM32L4P5xx STM32L4Q5xx STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
1209 
1210   /* Check the DAC parameters */
1211 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1212   assert_param(IS_DAC_HIGH_FREQUENCY_MODE(sConfig->DAC_HighFrequency));
1213 #endif /* STM32L4P5xx STM32L4Q5xx STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
1214   assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
1215   assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
1216   assert_param(IS_DAC_CHIP_CONNECTION(sConfig->DAC_ConnectOnChipPeripheral));
1217   assert_param(IS_DAC_TRIMMING(sConfig->DAC_UserTrimming));
1218   if ((sConfig->DAC_UserTrimming) == DAC_TRIMMING_USER)
1219   {
1220     assert_param(IS_DAC_TRIMMINGVALUE(sConfig->DAC_TrimmingValue));
1221   }
1222   assert_param(IS_DAC_SAMPLEANDHOLD(sConfig->DAC_SampleAndHold));
1223   if ((sConfig->DAC_SampleAndHold) == DAC_SAMPLEANDHOLD_ENABLE)
1224   {
1225     assert_param(IS_DAC_SAMPLETIME(sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime));
1226     assert_param(IS_DAC_HOLDTIME(sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime));
1227     assert_param(IS_DAC_REFRESHTIME(sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime));
1228   }
1229   assert_param(IS_DAC_CHANNEL(Channel));
1230 
1231   /* Process locked */
1232   __HAL_LOCK(hdac);
1233 
1234   /* Change DAC state */
1235   hdac->State = HAL_DAC_STATE_BUSY;
1236 
1237   if (sConfig->DAC_SampleAndHold == DAC_SAMPLEANDHOLD_ENABLE)
1238   /* Sample on old configuration */
1239   {
1240     /* Get timeout */
1241     tickstart = HAL_GetTick();
1242 
1243     if (Channel == DAC_CHANNEL_1)
1244     {
1245 
1246       /* SHSR1 can be written when BWST1 is cleared */
1247       while (((hdac->Instance->SR) & DAC_SR_BWST1) != 0UL)
1248       {
1249         /* Check for the Timeout */
1250         if ((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
1251         {
1252           /* Update error code */
1253           SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
1254 
1255           /* Change the DMA state */
1256           hdac->State = HAL_DAC_STATE_TIMEOUT;
1257 
1258           return HAL_TIMEOUT;
1259         }
1260       }
1261       HAL_Delay(1);
1262       hdac->Instance->SHSR1 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
1263     }
1264 #if !defined (STM32L451xx) & !defined (STM32L452xx) & !defined (STM32L462xx)
1265     else /* Channel 2 */
1266     {
1267       /* SHSR2 can be written when BWST2 is cleared */
1268 
1269       while (((hdac->Instance->SR) & DAC_SR_BWST2) != 0UL)
1270       {
1271         /* Check for the Timeout */
1272         if ((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
1273         {
1274           /* Update error code */
1275           SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
1276 
1277           /* Change the DMA state */
1278           hdac->State = HAL_DAC_STATE_TIMEOUT;
1279 
1280           return HAL_TIMEOUT;
1281         }
1282       }
1283       HAL_Delay(1U);
1284       hdac->Instance->SHSR2 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
1285     }
1286 #endif /* STM32L451xx STM32L452xx STM32L462xx */
1287 
1288     /* HoldTime */
1289     MODIFY_REG(hdac->Instance->SHHR, DAC_SHHR_THOLD1 << (Channel & 0x10UL), (sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime) << (Channel & 0x10UL));
1290     /* RefreshTime */
1291     MODIFY_REG(hdac->Instance->SHRR, DAC_SHRR_TREFRESH1 << (Channel & 0x10UL), (sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime) << (Channel & 0x10UL));
1292   }
1293 
1294   if (sConfig->DAC_UserTrimming == DAC_TRIMMING_USER)
1295   /* USER TRIMMING */
1296   {
1297     /* Get the DAC CCR value */
1298     tmpreg1 = hdac->Instance->CCR;
1299     /* Clear trimming value */
1300     tmpreg1 &= ~(((uint32_t)(DAC_CCR_OTRIM1)) << (Channel & 0x10UL));
1301     /* Configure for the selected trimming offset */
1302     tmpreg2 = sConfig->DAC_TrimmingValue;
1303     /* Calculate CCR register value depending on DAC_Channel */
1304     tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
1305     /* Write to DAC CCR */
1306     hdac->Instance->CCR = tmpreg1;
1307   }
1308   /* else factory trimming is used (factory setting are available at reset)*/
1309   /* SW Nothing has nothing to do */
1310 
1311   /* Get the DAC MCR value */
1312   tmpreg1 = hdac->Instance->MCR;
1313   /* Clear DAC_MCR_MODEx bits */
1314   tmpreg1 &= ~(((uint32_t)(DAC_MCR_MODE1)) << (Channel & 0x10UL));
1315   /* Configure for the selected DAC channel: mode, buffer output & on chip peripheral connect */
1316   tmpreg2 = (sConfig->DAC_SampleAndHold | sConfig->DAC_OutputBuffer | sConfig->DAC_ConnectOnChipPeripheral);
1317   /* Calculate MCR register value depending on DAC_Channel */
1318   tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
1319   /* Write to DAC MCR */
1320   hdac->Instance->MCR = tmpreg1;
1321 
1322   /* DAC in normal operating mode hence clear DAC_CR_CENx bit */
1323   CLEAR_BIT(hdac->Instance->CR, DAC_CR_CEN1 << (Channel & 0x10UL));
1324 
1325   /* Get the DAC CR value */
1326   tmpreg1 = hdac->Instance->CR;
1327   /* Clear TENx, TSELx, WAVEx and MAMPx bits */
1328   tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1)) << (Channel & 0x10UL));
1329   /* Configure for the selected DAC channel: trigger */
1330   /* Set TSELx and TENx bits according to DAC_Trigger value */
1331   tmpreg2 = sConfig->DAC_Trigger;
1332   /* Calculate CR register value depending on DAC_Channel */
1333   tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
1334 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1335   if (DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_80MHZ == sConfig->DAC_HighFrequency)
1336   {
1337     tmpreg1 |= DAC_CR_HFSEL;
1338   }
1339   else
1340   {
1341     if (DAC_HIGH_FREQUENCY_INTERFACE_MODE_DISABLE == sConfig->DAC_HighFrequency)
1342     {
1343       tmpreg1 &= ~(DAC_CR_HFSEL);
1344     }
1345     else /* Automatic selection */
1346     {
1347       hclkfreq = HAL_RCC_GetHCLKFreq();
1348       if (hclkfreq > HFSEL_ENABLE_THRESHOLD_80MHZ)
1349       {
1350         /* High frequency enable when HCLK frequency higher than 80   */
1351          tmpreg1 |= DAC_CR_HFSEL;
1352       }
1353       else
1354       {
1355         /* High frequency disable when HCLK frequency higher than 80  */
1356         tmpreg1 &= ~(DAC_CR_HFSEL);
1357       }
1358     }
1359   }
1360 
1361 #endif /* STM32L4P5xx STM32L4Q5xx STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
1362 
1363   /* Write to DAC CR */
1364   hdac->Instance->CR = tmpreg1;
1365   /* Disable wave generation */
1366   hdac->Instance->CR &= ~(DAC_CR_WAVE1 << (Channel & 0x10UL));
1367 
1368   /* Change DAC state */
1369   hdac->State = HAL_DAC_STATE_READY;
1370 
1371   /* Process unlocked */
1372   __HAL_UNLOCK(hdac);
1373 
1374   /* Return function status */
1375   return HAL_OK;
1376 }
1377 
1378 /**
1379   * @}
1380   */
1381 
1382 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
1383  *  @brief   Peripheral State and Errors functions
1384  *
1385 @verbatim
1386   ==============================================================================
1387             ##### Peripheral State and Errors functions #####
1388   ==============================================================================
1389     [..]
1390     This subsection provides functions allowing to
1391       (+) Check the DAC state.
1392       (+) Check the DAC Errors.
1393 
1394 @endverbatim
1395   * @{
1396   */
1397 
1398 /**
1399   * @brief  return the DAC handle state
1400   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1401   *         the configuration information for the specified DAC.
1402   * @retval HAL state
1403   */
HAL_DAC_GetState(DAC_HandleTypeDef * hdac)1404 HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef *hdac)
1405 {
1406   /* Return DAC handle state */
1407   return hdac->State;
1408 }
1409 
1410 
1411 /**
1412   * @brief  Return the DAC error code
1413   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1414   *         the configuration information for the specified DAC.
1415   * @retval DAC Error Code
1416   */
HAL_DAC_GetError(DAC_HandleTypeDef * hdac)1417 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
1418 {
1419   return hdac->ErrorCode;
1420 }
1421 
1422 /**
1423   * @}
1424   */
1425 
1426 /**
1427   * @}
1428   */
1429 
1430 /** @addtogroup DAC_Exported_Functions
1431   * @{
1432   */
1433 
1434 /** @addtogroup DAC_Exported_Functions_Group1
1435   * @{
1436   */
1437 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1438 /**
1439   * @brief  Register a User DAC Callback
1440   *         To be used instead of the weak (surcharged) predefined callback
1441   * @param  hdac DAC handle
1442   * @param  CallbackID ID of the callback to be registered
1443   *         This parameter can be one of the following values:
1444   *          @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK   DAC Error Callback ID
1445   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID       DAC CH1 Complete Callback ID
1446   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID  DAC CH1 Half Complete Callback ID
1447   *          @arg @ref HAL_DAC_CH1_ERROR_ID             DAC CH1 Error Callback ID
1448   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID       DAC CH1 UnderRun Callback ID
1449   *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID       DAC CH2 Complete Callback ID
1450   *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID  DAC CH2 Half Complete Callback ID
1451   *          @arg @ref HAL_DAC_CH2_ERROR_ID             DAC CH2 Error Callback ID
1452   *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID       DAC CH2 UnderRun Callback ID
1453   *          @arg @ref HAL_DAC_MSPINIT_CB_ID            DAC MSP Init Callback ID
1454   *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID          DAC MSP DeInit Callback ID
1455   *
1456   * @param  pCallback pointer to the Callback function
1457   * @retval status
1458   */
HAL_DAC_RegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID,pDAC_CallbackTypeDef pCallback)1459 HAL_StatusTypeDef HAL_DAC_RegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID,
1460                                            pDAC_CallbackTypeDef pCallback)
1461 {
1462   HAL_StatusTypeDef status = HAL_OK;
1463 
1464   if (pCallback == NULL)
1465   {
1466     /* Update the error code */
1467     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1468     return HAL_ERROR;
1469   }
1470 
1471   /* Process locked */
1472   __HAL_LOCK(hdac);
1473 
1474   if (hdac->State == HAL_DAC_STATE_READY)
1475   {
1476     switch (CallbackID)
1477     {
1478     case HAL_DAC_CH1_COMPLETE_CB_ID :
1479       hdac->ConvCpltCallbackCh1 = pCallback;
1480       break;
1481     case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1482       hdac->ConvHalfCpltCallbackCh1 = pCallback;
1483       break;
1484     case HAL_DAC_CH1_ERROR_ID :
1485       hdac->ErrorCallbackCh1 = pCallback;
1486       break;
1487     case HAL_DAC_CH1_UNDERRUN_CB_ID :
1488       hdac->DMAUnderrunCallbackCh1 = pCallback;
1489       break;
1490     case HAL_DAC_CH2_COMPLETE_CB_ID :
1491       hdac->ConvCpltCallbackCh2 = pCallback;
1492       break;
1493     case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1494       hdac->ConvHalfCpltCallbackCh2 = pCallback;
1495       break;
1496     case HAL_DAC_CH2_ERROR_ID :
1497       hdac->ErrorCallbackCh2 = pCallback;
1498       break;
1499     case HAL_DAC_CH2_UNDERRUN_CB_ID :
1500       hdac->DMAUnderrunCallbackCh2 = pCallback;
1501       break;
1502     case HAL_DAC_MSPINIT_CB_ID :
1503       hdac->MspInitCallback = pCallback;
1504       break;
1505     case HAL_DAC_MSPDEINIT_CB_ID :
1506       hdac->MspDeInitCallback = pCallback;
1507       break;
1508     default :
1509       /* Update the error code */
1510       hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1511       /* update return status */
1512       status =  HAL_ERROR;
1513       break;
1514     }
1515   }
1516   else if (hdac->State == HAL_DAC_STATE_RESET)
1517   {
1518     switch (CallbackID)
1519     {
1520     case HAL_DAC_MSPINIT_CB_ID :
1521       hdac->MspInitCallback = pCallback;
1522       break;
1523     case HAL_DAC_MSPDEINIT_CB_ID :
1524       hdac->MspDeInitCallback = pCallback;
1525       break;
1526     default :
1527       /* Update the error code */
1528       hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1529       /* update return status */
1530       status =  HAL_ERROR;
1531       break;
1532     }
1533   }
1534   else
1535   {
1536     /* Update the error code */
1537     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1538     /* update return status */
1539     status =  HAL_ERROR;
1540   }
1541 
1542   /* Release Lock */
1543   __HAL_UNLOCK(hdac);
1544   return status;
1545 }
1546 
1547 /**
1548   * @brief  Unregister a User DAC Callback
1549   *         DAC Callback is redirected to the weak (surcharged) predefined callback
1550   * @param  hdac DAC handle
1551   * @param  CallbackID ID of the callback to be unregistered
1552   *         This parameter can be one of the following values:
1553   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID          DAC CH1 transfer Complete Callback ID
1554   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID     DAC CH1 Half Complete Callback ID
1555   *          @arg @ref HAL_DAC_CH1_ERROR_ID                DAC CH1 Error Callback ID
1556   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID          DAC CH1 UnderRun Callback ID
1557   *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID          DAC CH2 Complete Callback ID
1558   *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID     DAC CH2 Half Complete Callback ID
1559   *          @arg @ref HAL_DAC_CH2_ERROR_ID                DAC CH2 Error Callback ID
1560   *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID          DAC CH2 UnderRun Callback ID
1561   *          @arg @ref HAL_DAC_MSPINIT_CB_ID               DAC MSP Init Callback ID
1562   *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID             DAC MSP DeInit Callback ID
1563   *          @arg @ref HAL_DAC_ALL_CB_ID                   DAC All callbacks
1564   * @retval status
1565   */
HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID)1566 HAL_StatusTypeDef HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
1567 {
1568   HAL_StatusTypeDef status = HAL_OK;
1569 
1570   /* Process locked */
1571   __HAL_LOCK(hdac);
1572 
1573   if (hdac->State == HAL_DAC_STATE_READY)
1574   {
1575     switch (CallbackID)
1576     {
1577     case HAL_DAC_CH1_COMPLETE_CB_ID :
1578       hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1579       break;
1580     case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1581       hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1582       break;
1583     case HAL_DAC_CH1_ERROR_ID :
1584       hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1585       break;
1586     case HAL_DAC_CH1_UNDERRUN_CB_ID :
1587       hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1588       break;
1589 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
1590     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
1591     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
1592     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1593     case HAL_DAC_CH2_COMPLETE_CB_ID :
1594       hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1595       break;
1596     case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1597       hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1598       break;
1599     case HAL_DAC_CH2_ERROR_ID :
1600       hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1601       break;
1602     case HAL_DAC_CH2_UNDERRUN_CB_ID :
1603       hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1604       break;
1605 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
1606         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
1607         /* STM32L4P5xx STM32L4Q5xx                                                             */
1608         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
1609     case HAL_DAC_MSPINIT_CB_ID :
1610       hdac->MspInitCallback = HAL_DAC_MspInit;
1611       break;
1612     case HAL_DAC_MSPDEINIT_CB_ID :
1613       hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1614       break;
1615     case HAL_DAC_ALL_CB_ID :
1616       hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1617       hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1618       hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1619       hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1620 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
1621     defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
1622     defined (STM32L4P5xx) || defined (STM32L4Q5xx) || \
1623     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1624       hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1625       hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1626       hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1627       hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1628 #endif  /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx                         */
1629         /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
1630         /* STM32L4P5xx STM32L4Q5xx                                                             */
1631         /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx             */
1632       hdac->MspInitCallback = HAL_DAC_MspInit;
1633       hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1634       break;
1635     default :
1636       /* Update the error code */
1637       hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1638       /* update return status */
1639       status =  HAL_ERROR;
1640       break;
1641     }
1642   }
1643   else if (hdac->State == HAL_DAC_STATE_RESET)
1644   {
1645     switch (CallbackID)
1646     {
1647     case HAL_DAC_MSPINIT_CB_ID :
1648       hdac->MspInitCallback = HAL_DAC_MspInit;
1649       break;
1650     case HAL_DAC_MSPDEINIT_CB_ID :
1651       hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1652       break;
1653     default :
1654       /* Update the error code */
1655       hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1656       /* update return status */
1657       status =  HAL_ERROR;
1658       break;
1659     }
1660   }
1661   else
1662   {
1663     /* Update the error code */
1664     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1665     /* update return status */
1666     status =  HAL_ERROR;
1667   }
1668 
1669   /* Release Lock */
1670   __HAL_UNLOCK(hdac);
1671   return status;
1672 }
1673 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1674 
1675 /**
1676   * @}
1677   */
1678 
1679 /**
1680   * @}
1681   */
1682 
1683 /** @addtogroup DAC_Private_Functions
1684   * @{
1685   */
1686 
1687 /**
1688   * @brief  DMA conversion complete callback.
1689   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1690   *                the configuration information for the specified DMA module.
1691   * @retval None
1692   */
DAC_DMAConvCpltCh1(DMA_HandleTypeDef * hdma)1693 void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
1694 {
1695   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1696 
1697 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1698   hdac->ConvCpltCallbackCh1(hdac);
1699 #else
1700   HAL_DAC_ConvCpltCallbackCh1(hdac);
1701 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1702 
1703   hdac->State = HAL_DAC_STATE_READY;
1704 }
1705 
1706 /**
1707   * @brief  DMA half transfer complete callback.
1708   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1709   *                the configuration information for the specified DMA module.
1710   * @retval None
1711   */
DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef * hdma)1712 void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
1713 {
1714   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1715   /* Conversion complete callback */
1716 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1717   hdac->ConvHalfCpltCallbackCh1(hdac);
1718 #else
1719   HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
1720 #endif  /* USE_HAL_DAC_REGISTER_CALLBACKS */
1721 }
1722 
1723 /**
1724   * @brief  DMA error callback
1725   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1726   *                the configuration information for the specified DMA module.
1727   * @retval None
1728   */
DAC_DMAErrorCh1(DMA_HandleTypeDef * hdma)1729 void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
1730 {
1731   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1732 
1733   /* Set DAC error code to DMA error */
1734   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
1735 
1736 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1737   hdac->ErrorCallbackCh1(hdac);
1738 #else
1739   HAL_DAC_ErrorCallbackCh1(hdac);
1740 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1741 
1742   hdac->State = HAL_DAC_STATE_READY;
1743 }
1744 
1745 /**
1746   * @}
1747   */
1748 
1749 /**
1750   * @}
1751   */
1752 
1753 #endif /* DAC1 */
1754 
1755 #endif /* HAL_DAC_MODULE_ENABLED */
1756 
1757 /**
1758   * @}
1759   */
1760 
1761