1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_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) 2016 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     STM32L0 devices integrate one or two 12-bit Digital Analog Converters
33     (i.e. one or 2 channel(s))
34     1 channel : STM32L05x STM32L06x devices
35     2 channels: STM32L07x STM32L08x devices
36 
37     When 2 channels are available, the 2 converters (i.e. channel1 & channel2)
38     can be used independently or simultaneously (dual mode):
39       (#) DAC channel1 with DAC_OUT1 (PA4) as output
40       (#) DAC channel2 with DAC_OUT2 (PA5) as output (STM32L07x/STM32L08x only)
41       (#) Channel1 & channel2 can be used independently or simultaneously in dual mode (STM32L07x/STM32L08x only)
42 
43       *** DAC Triggers ***
44       ====================
45     [..]
46     Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
47     and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
48     [..]
49     Digital to Analog conversion can be triggered by:
50       (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
51           The used pin (GPIOx_PIN_9) must be configured in input mode.
52 
53       (#) Timers TRGO:
54           STM32L05x/STM32L06x : TIM2, TIM6 and TIM21
55           STM32L07x/STM32L08x : TIM2, TIM3, TIM6, TIM7 and TIM21
56           (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T6_TRGO...)
57 
58       (#) Software using DAC_TRIGGER_SOFTWARE
59 
60       *** DAC Buffer mode feature ***
61       ===============================
62       [..]
63       Each DAC channel integrates an output buffer that can be used to
64       reduce the output impedance, and to drive external loads directly
65       without having to add an external operational amplifier.
66       To enable, the output buffer use
67       sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
68       [..]
69       (@) Refer to the device datasheet for more details about output
70           impedance value with and without output buffer.
71 
72        *** DAC wave generation feature ***
73        ===================================
74        [..]
75        Both DAC channels can be used to generate
76          (#) Noise wave using HAL_DACEx_NoiseWaveGenerate()
77          (#) Triangle wave using HAL_DACEx_TriangleWaveGenerate()
78 
79        *** DAC data format ***
80        =======================
81        [..]
82        The DAC data format can be:
83          (#) 8-bit right alignment using DAC_ALIGN_8B_R
84          (#) 12-bit left alignment using DAC_ALIGN_12B_L
85          (#) 12-bit right alignment using DAC_ALIGN_12B_R
86 
87        *** DAC data value to voltage correspondence ***
88        ================================================
89        [..]
90        The analog output voltage on each DAC channel pin is determined
91        by the following equation:
92        [..]
93        DAC_OUTx = VREF+ * DOR / 4095
94        (+) with  DOR is the Data Output Register
95        [..]
96           VEF+ is the input voltage reference (refer to the device datasheet)
97        [..]
98         e.g. To set DAC_OUT1 to 0.7V, use
99        (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
100 
101        *** DMA requests  ***
102        =====================
103        [..]
104        A DMA1 request can be generated when an external trigger (but not a software trigger)
105        occurs if DMA1 requests are enabled using HAL_DAC_Start_DMA().
106        DMA1 requests are mapped as following:
107          (#) DAC channel1 : mapped on DMA1 Request9 channel2 which must be
108              already configured
109          (#) DAC channel2 : mapped on DMA1 Request15 channel4 which must be
110              already configured (STM32L07x/STM32L08x only)
111 
112     -@- For Dual mode (STM32L07x/STM32L08x only) and specific signal (Triangle and noise) generation please
113         refer to Extension Features Driver description
114 
115 
116                       ##### How to use this driver #####
117   ==============================================================================
118     [..]
119       (+) DAC APB clock must be enabled to get write access to DAC
120           registers using HAL_DAC_Init()
121       (+) Configure DAC_OUT1: PA4 in analog mode.
122       (+) Configure DAC_OUT2: PA5 in analog mode (STM32L07x/STM32L08x only).
123       (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
124       (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
125 
126      *** Polling mode IO operation ***
127      =================================
128      [..]
129        (+) Start the DAC peripheral using HAL_DAC_Start()
130        (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
131        (+) Stop the DAC peripheral using HAL_DAC_Stop()
132 
133      *** DMA mode IO operation ***
134      ==============================
135      [..]
136        (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
137            of data to be transferred at each end of conversion
138        (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1()or HAL_DAC_ConvHalfCpltCallbackCh2()
139            function is executed and user can add his own code by customization of function pointer
140            HAL_DAC_ConvHalfCpltCallbackCh1 or HAL_DAC_ConvHalfCpltCallbackCh2
141        (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
142            function is executed and user can add his own code by customization of function pointer
143            HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
144        (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
145            add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
146        (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
147            HAL_DAC_DMAUnderrunCallbackCh1()or HAL_DAC_DMAUnderrunCallbackCh2()
148            function is executed and user can add his own code by customization of function pointer
149            HAL_DAC_DMAUnderrunCallbackCh1 or HAL_DAC_DMAUnderrunCallbackCh2
150            add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
151        (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
152 
153     *** Callback registration ***
154     =============================================
155     [..]
156       The compilation define  USE_HAL_DAC_REGISTER_CALLBACKS when set to 1
157       allows the user to configure dynamically the driver callbacks.
158 
159     Use Functions HAL_DAC_RegisterCallback() to register a user callback,
160       it allows to register following callbacks:
161       (+) ConvCpltCallbackCh1     : callback when a half transfer is completed on Ch1.
162       (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
163       (+) ErrorCallbackCh1        : callback when an error occurs on Ch1.
164       (+) DMAUnderrunCallbackCh1  : callback when an underrun error occurs on Ch1.
165       (+) ConvCpltCallbackCh2     : callback when a half transfer is completed on Ch2.
166       (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
167       (+) ErrorCallbackCh2        : callback when an error occurs on Ch2.
168       (+) DMAUnderrunCallbackCh2  : callback when an underrun error occurs on Ch2.
169       (+) MspInitCallback         : DAC MspInit.
170       (+) MspDeInitCallback       : DAC MspdeInit.
171       This function takes as parameters the HAL peripheral handle, the Callback ID
172       and a pointer to the user callback function.
173 
174     Use function HAL_DAC_UnRegisterCallback() to reset a callback to the default
175       weak (overridden) function. It allows to reset following callbacks:
176       (+) ConvCpltCallbackCh1     : callback when a half transfer is completed on Ch1.
177       (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
178       (+) ErrorCallbackCh1        : callback when an error occurs on Ch1.
179       (+) DMAUnderrunCallbackCh1  : callback when an underrun error occurs on Ch1.
180       (+) ConvCpltCallbackCh2     : callback when a half transfer is completed on Ch2.
181       (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
182       (+) ErrorCallbackCh2        : callback when an error occurs on Ch2.
183       (+) DMAUnderrunCallbackCh2  : callback when an underrun error occurs on Ch2.
184       (+) MspInitCallback         : DAC MspInit.
185       (+) MspDeInitCallback       : DAC MspdeInit.
186       (+) All Callbacks
187       This function) takes as parameters the HAL peripheral handle and the Callback ID.
188 
189       By default, after the HAL_DAC_Init and if the state is HAL_DAC_STATE_RESET
190       all callbacks are reset to the corresponding legacy weak (overridden) functions.
191       Exception done for MspInit and MspDeInit callbacks that are respectively
192       reset to the legacy weak (overridden) functions in the HAL_DAC_Init
193       and  HAL_DAC_DeInit only when these callbacks are null (not registered beforehand).
194       If not, MspInit or MspDeInit are not null, the HAL_DAC_Init and HAL_DAC_DeInit
195       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
196 
197       Callbacks can be registered/unregistered in READY state only.
198       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
199       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
200       during the Init/DeInit.
201       In that case first register the MspInit/MspDeInit user callbacks
202       using HAL_DAC_RegisterCallback before calling HAL_DAC_DeInit
203       or HAL_DAC_Init function.
204 
205       When The compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or
206       not defined, the callback registering feature is not available
207       and weak (overridden) callbacks are used.
208 
209      *** DAC HAL driver macros list ***
210      =============================================
211      [..]
212        Below the list of most used macros in DAC HAL driver.
213 
214       (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
215       (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
216       (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
217       (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
218 
219      [..]
220       (@) You can refer to the DAC HAL driver header file for more useful macros
221 
222  @endverbatim
223   ******************************************************************************
224   */
225 
226 
227 #if !defined (STM32L010xB) && !defined (STM32L010x8) && !defined (STM32L010x6) && !defined (STM32L010x4) && !defined (STM32L011xx) && !defined (STM32L021xx) && !defined (STM32L031xx) && !defined (STM32L041xx) && !defined (STM32L051xx) && !defined (STM32L071xx) && !defined (STM32L081xx)
228 /* Includes ------------------------------------------------------------------*/
229 #include "stm32l0xx_hal.h"
230 
231 #ifdef HAL_DAC_MODULE_ENABLED
232 /** @addtogroup STM32L0xx_HAL_Driver
233   * @{
234   */
235 
236 /** @addtogroup DAC
237   * @brief DAC driver modules
238   * @{
239   */
240 
241 
242 /* Private typedef -----------------------------------------------------------*/
243 /* Private define ------------------------------------------------------------*/
244 /* Private macro -------------------------------------------------------------*/
245 /* Private variables ---------------------------------------------------------*/
246 /* Private function prototypes -----------------------------------------------*/
247 
248 /* Private functions ---------------------------------------------------------*/
249 
250 /** @addtogroup DAC_Exported_Functions
251   * @{
252   */
253 
254 /** @addtogroup DAC_Exported_Functions_Group1
255  *  @brief    Initialization and Configuration functions
256  *
257 @verbatim
258   ==============================================================================
259               ##### Initialization and de-initialization functions #####
260   ==============================================================================
261     [..]  This section provides functions allowing to:
262       (+) Initialize and configure the DAC.
263       (+) De-initialize the DAC.
264 
265 @endverbatim
266   * @{
267   */
268 
269 /**
270   * @brief  Initialize the DAC peripheral according to the specified parameters
271   *         in the DAC_InitStruct and initialize the associated handle.
272   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
273   *         the configuration information for the specified DAC.
274   * @retval HAL status
275   */
HAL_DAC_Init(DAC_HandleTypeDef * hdac)276 HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
277 {
278   /* Check DAC handle */
279   if(hdac == NULL)
280   {
281      return HAL_ERROR;
282   }
283   /* Check the parameters */
284   assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
285 
286   if(hdac->State == HAL_DAC_STATE_RESET)
287   {
288 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
289     /* Init the DAC Callback settings */
290     hdac->ConvCpltCallbackCh1           = HAL_DAC_ConvCpltCallbackCh1;
291     hdac->ConvHalfCpltCallbackCh1       = HAL_DAC_ConvHalfCpltCallbackCh1;
292     hdac->ErrorCallbackCh1              = HAL_DAC_ErrorCallbackCh1;
293     hdac->DMAUnderrunCallbackCh1        = HAL_DAC_DMAUnderrunCallbackCh1;
294 
295 #if defined (DAC_CHANNEL2_SUPPORT)
296     hdac->ConvCpltCallbackCh2           = HAL_DACEx_ConvCpltCallbackCh2;
297     hdac->ConvHalfCpltCallbackCh2       = HAL_DACEx_ConvHalfCpltCallbackCh2;
298     hdac->ErrorCallbackCh2              = HAL_DACEx_ErrorCallbackCh2;
299     hdac->DMAUnderrunCallbackCh2        = HAL_DACEx_DMAUnderrunCallbackCh2;
300 #endif /* DAC_CHANNEL2_SUPPORT */
301 
302     if(hdac->MspInitCallback == NULL)
303     {
304       hdac->MspInitCallback               = HAL_DAC_MspInit;
305     }
306 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
307 
308     /* Allocate lock resource and initialize it */
309     hdac->Lock = HAL_UNLOCKED;
310 
311 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
312     /* Init the low level hardware */
313     hdac->MspInitCallback(hdac);
314 #else
315     /* Init the low level hardware */
316     HAL_DAC_MspInit(hdac);
317 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
318   }
319 
320   /* Initialize the DAC state*/
321   hdac->State = HAL_DAC_STATE_BUSY;
322 
323   /* Set DAC error code to none */
324   hdac->ErrorCode = HAL_DAC_ERROR_NONE;
325 
326   /* Initialize the DAC state*/
327   hdac->State = HAL_DAC_STATE_READY;
328 
329   /* Return function status */
330   return HAL_OK;
331 }
332 
333 /**
334   * @brief  Deinitialize the DAC peripheral registers to their default reset values.
335   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
336   *         the configuration information for the specified DAC.
337   * @retval HAL status
338   */
HAL_DAC_DeInit(DAC_HandleTypeDef * hdac)339 HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
340 {
341   /* Check DAC handle */
342   if(hdac == NULL)
343   {
344      return HAL_ERROR;
345   }
346 
347   /* Check the parameters */
348   assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
349 
350   /* Change DAC state */
351   hdac->State = HAL_DAC_STATE_BUSY;
352 
353 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
354   if(hdac->MspDeInitCallback == NULL)
355   {
356     hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
357   }
358   /* DeInit the low level hardware */
359   hdac->MspDeInitCallback(hdac);
360 #else
361   /* DeInit the low level hardware */
362   HAL_DAC_MspDeInit(hdac);
363 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
364 
365   /* Set DAC error code to none */
366   hdac->ErrorCode = HAL_DAC_ERROR_NONE;
367 
368   /* Change DAC state */
369   hdac->State = HAL_DAC_STATE_RESET;
370 
371   /* Release Lock */
372   __HAL_UNLOCK(hdac);
373 
374   /* Return function status */
375   return HAL_OK;
376 }
377 
378 /**
379   * @brief  Initialize the DAC MSP.
380   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
381   *         the configuration information for the specified DAC.
382   * @retval None
383   */
HAL_DAC_MspInit(DAC_HandleTypeDef * hdac)384 __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
385 {
386   /* Prevent unused argument(s) compilation warning */
387   UNUSED(hdac);
388 
389   /* NOTE : This function should not be modified, when the callback is needed,
390             the HAL_DAC_MspInit could be implemented in the user file
391    */
392 }
393 
394 /**
395   * @brief  DeInitialize the DAC MSP.
396   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
397   *         the configuration information for the specified DAC.
398   * @retval None
399   */
HAL_DAC_MspDeInit(DAC_HandleTypeDef * hdac)400 __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
401 {
402   /* Prevent unused argument(s) compilation warning */
403   UNUSED(hdac);
404 
405   /* NOTE : This function should not be modified, when the callback is needed,
406             the HAL_DAC_MspDeInit could be implemented in the user file
407    */
408 }
409 
410 /**
411   * @}
412   */
413 
414 /** @addtogroup DAC_Exported_Functions_Group2
415  *  @brief    IO operation functions
416  *
417 @verbatim
418   ==============================================================================
419              ##### IO operation functions #####
420   ==============================================================================
421     [..]  This section provides functions allowing to:
422       (+) Start conversion.
423       (+) Stop conversion.
424       (+) Start conversion and enable DMA transfer.
425       (+) Stop conversion and disable DMA transfer.
426       (+) Get result of conversion.
427       (+) Get result of dual mode conversion (STM32L07xx/STM32L08xx only)
428 
429 @endverbatim
430   * @{
431   */
432 
433 /**
434   * @brief  Enables DAC and starts conversion of channel.
435   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
436   *         the configuration information for the specified DAC.
437   * @param  Channel The selected DAC channel.
438   *          This parameter can be one of the following values:
439   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
440   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
441   * @retval HAL status
442   */
HAL_DAC_Start(DAC_HandleTypeDef * hdac,uint32_t Channel)443 __weak HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
444 {
445   /* Prevent unused argument(s) compilation warning */
446   UNUSED(hdac);
447   UNUSED(Channel);
448 
449   /* Note : This function is defined into this file for library reference. */
450   /*        Function content is located into file stm32l0xx_hal_dac_ex.c   */
451 
452   /* Return function status */
453   return HAL_OK;
454 }
455 
456 /**
457   * @brief  Disables DAC and stop conversion of channel.
458   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
459   *         the configuration information for the specified DAC.
460   * @param  Channel The selected DAC channel.
461   *          This parameter can be one of the following values:
462   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
463   *            @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
464   * @retval HAL status
465   */
HAL_DAC_Stop(DAC_HandleTypeDef * hdac,uint32_t Channel)466 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
467 {
468   /* Check the parameters */
469   assert_param(IS_DAC_CHANNEL(Channel));
470 
471   /* Disable the Peripheral */
472   __HAL_DAC_DISABLE(hdac, Channel);
473 
474   /* Change DAC state */
475   hdac->State = HAL_DAC_STATE_READY;
476 
477   /* Return function status */
478   return HAL_OK;
479 }
480 
481 /**
482   * @brief  Enables DAC and starts conversion of channel using DMA.
483   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
484   *         the configuration information for the specified DAC.
485   * @param  Channel The selected DAC channel.
486   *          This parameter can be one of the following values:
487   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
488   *            @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
489   * @param  pData The destination peripheral Buffer address.
490   * @param  Length The length of data to be transferred from memory to DAC peripheral
491   * @param  Alignment Specifies the data alignment for DAC channel.
492   *          This parameter can be one of the following values:
493   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
494   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
495   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
496   * @retval HAL status
497   */
HAL_DAC_Start_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t * pData,uint32_t Length,uint32_t Alignment)498 __weak HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
499 {
500   /* Prevent unused argument(s) compilation warning */
501   UNUSED(hdac);
502   UNUSED(Channel);
503   UNUSED(pData);
504   UNUSED(Length);
505   UNUSED(Alignment);
506 
507   /* Note : This function is defined into this file for library reference. */
508   /*        Function content is located into file stm32l0xx_hal_dac_ex.c   */
509 
510   /* Return function status */
511   return HAL_OK;
512 }
513 
514 /**
515   * @brief  Disables DAC and stop conversion of channel.
516   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
517   *         the configuration information for the specified DAC.
518   * @param  Channel The selected DAC channel.
519   *          This parameter can be one of the following values:
520   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
521   *            @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
522   * @retval HAL status
523   */
HAL_DAC_Stop_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel)524 __weak HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
525 {
526   /* Prevent unused argument(s) compilation warning */
527   UNUSED(hdac);
528   UNUSED(Channel);
529 
530   /* Note : This function is defined into this file for library reference. */
531   /*        Function content is located into file stm32l0xx_hal_dac_ex.c   */
532 
533   /* Return function status */
534   return HAL_OK;
535 }
536 
537 /**
538   * @brief  Returns the last data output value of the selected DAC 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 (STM32L07x/STM32L08x only)
545   * @retval The selected DAC channel data output value.
546   */
HAL_DAC_GetValue(DAC_HandleTypeDef * hdac,uint32_t Channel)547 __weak uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
548 {
549   /* Prevent unused argument(s) compilation warning */
550   UNUSED(hdac);
551   UNUSED(Channel);
552 
553   /* Note : This function is defined into this file for library reference. */
554   /*        Function content is located into file stm32l0xx_hal_dac_ex.c   */
555 
556   /* Return function status */
557   return 0U;
558 }
559 
560 /**
561   * @brief  Handles DAC interrupt request
562   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
563   *         the configuration information for the specified DAC.
564   * @retval None
565   */
HAL_DAC_IRQHandler(DAC_HandleTypeDef * hdac)566 __weak void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
567 {
568   /* Prevent unused argument(s) compilation warning */
569   UNUSED(hdac);
570 
571   /* Note : This function is defined into this file for library reference. */
572   /*        Function content is located into file stm32l0xx_hal_dac_ex.c   */
573 
574 }
575 
576 /**
577   * @brief  Conversion complete callback in non-blocking mode for Channel1
578   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
579   *         the configuration information for the specified DAC.
580   * @retval None
581   */
HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef * hdac)582 __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
583 {
584   /* Prevent unused argument(s) compilation warning */
585   UNUSED(hdac);
586 
587   /* NOTE : This function should not be modified, when the callback is needed,
588             the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
589    */
590 }
591 
592 /**
593   * @brief  Conversion half DMA transfer callback in non-blocking mode for Channel1
594   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
595   *         the configuration information for the specified DAC.
596   * @retval None
597   */
HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef * hdac)598 __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
599 {
600   /* Prevent unused argument(s) compilation warning */
601   UNUSED(hdac);
602 
603   /* NOTE : This function should not be modified, when the callback is needed,
604             the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
605    */
606 }
607 
608 /**
609   * @brief  Error DAC callback for Channel1.
610   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
611   *         the configuration information for the specified DAC.
612   * @retval None
613   */
HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef * hdac)614 __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
615 {
616   /* Prevent unused argument(s) compilation warning */
617   UNUSED(hdac);
618 
619   /* NOTE : This function should not be modified, when the callback is needed,
620             the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
621    */
622 }
623 
624 /**
625   * @brief  DMA underrun DAC callback for channel1.
626   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
627   *         the configuration information for the specified DAC.
628   * @retval None
629   */
HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef * hdac)630 __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
631 {
632   /* Prevent unused argument(s) compilation warning */
633   UNUSED(hdac);
634 
635   /* NOTE : This function should not be modified, when the callback is needed,
636             the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
637    */
638 }
639 
640 /**
641   * @}
642   */
643 
644 /** @addtogroup DAC_Exported_Functions_Group3
645  *  @brief    Peripheral Control functions
646  *
647 @verbatim
648   ==============================================================================
649              ##### Peripheral Control functions #####
650   ==============================================================================
651     [..]  This section provides functions allowing to:
652       (+) Configure channels.
653       (+) Set the specified data holding register value for DAC channel.
654 
655 @endverbatim
656   * @{
657   */
658 
659 /**
660   * @brief  Configures the selected DAC channel.
661   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
662   *         the configuration information for the specified DAC.
663   * @param  sConfig DAC configuration structure.
664   * @param  Channel The selected DAC channel.
665   *          This parameter can be one of the following values:
666   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
667   *            @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
668   * @retval HAL status
669   */
HAL_DAC_ConfigChannel(DAC_HandleTypeDef * hdac,DAC_ChannelConfTypeDef * sConfig,uint32_t Channel)670 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
671 {
672   uint32_t tmpreg1 = 0U, tmpreg2 = 0U;
673 
674   /* Check the DAC parameters */
675   assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
676   assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
677   assert_param(IS_DAC_CHANNEL(Channel));
678 
679   /* Process locked */
680   __HAL_LOCK(hdac);
681 
682   /* Change DAC state */
683   hdac->State = HAL_DAC_STATE_BUSY;
684 
685   /* Get the DAC CR value */
686   tmpreg1 = hdac->Instance->CR;
687   /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
688   tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
689   /* Configure for the selected DAC channel: buffer output, trigger */
690   /* Set TSELx and TENx bits according to DAC_Trigger value */
691   /* Set BOFFx bit according to DAC_OutputBuffer value */
692   tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
693   /* Calculate CR register value depending on DAC_Channel */
694   tmpreg1 |= tmpreg2 << Channel;
695   /* Write to DAC CR */
696   hdac->Instance->CR = tmpreg1;
697   /* Disable wave generation */
698   CLEAR_BIT(hdac->Instance->CR, (DAC_CR_WAVE1 << Channel));
699   /* Change DAC state */
700   hdac->State = HAL_DAC_STATE_READY;
701 
702   /* Process unlocked */
703   __HAL_UNLOCK(hdac);
704 
705   /* Return function status */
706   return HAL_OK;
707 }
708 
709 /**
710   * @}
711   */
712 
713 /** @addtogroup DAC_Exported_Functions_Group4
714  *  @brief   Peripheral State and Errors functions
715  *
716 @verbatim
717   ==============================================================================
718             ##### Peripheral State and Errors functions #####
719   ==============================================================================
720     [..]
721     This subsection provides functions allowing to
722       (+) Check the DAC state.
723       (+) Check the DAC Errors.
724 
725 @endverbatim
726   * @{
727   */
728 
729 /**
730   * @brief  return the DAC handle state
731   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
732   *         the configuration information for the specified DAC.
733   * @retval HAL state
734   */
HAL_DAC_GetState(DAC_HandleTypeDef * hdac)735 HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
736 {
737   /* Return DAC handle state */
738   return hdac->State;
739 }
740 
741 
742 /**
743   * @brief  Return the DAC error code
744   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
745   *         the configuration information for the specified DAC.
746   * @retval DAC Error Code
747   */
HAL_DAC_GetError(DAC_HandleTypeDef * hdac)748 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
749 {
750   return hdac->ErrorCode;
751 }
752 
753 /**
754   * @brief  Set the specified data holding register value for DAC channel.
755   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
756   *         the configuration information for the specified DAC.
757   * @param  Channel The selected DAC channel.
758   *          This parameter can be one of the following values:
759   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
760   *            @arg DAC_CHANNEL_2: DAC Channel2 selected (STM32L07x/STM32L08x only)
761   * @param  Alignment Specifies the data alignment.
762   *          This parameter can be one of the following values:
763   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
764   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
765   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
766   * @param  Data Data to be loaded in the selected data holding register.
767   * @retval HAL status
768   */
HAL_DAC_SetValue(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t Alignment,uint32_t Data)769 __weak HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
770 {
771   /* Prevent unused argument(s) compilation warning */
772   UNUSED(hdac);
773   UNUSED(Channel);
774   UNUSED(Alignment);
775   UNUSED(Data);
776   /* Note : This function is defined into this file for library reference. */
777   /*        Function content is located into file stm32l0xx_hal_dac_ex.c   */
778 
779   /* Return function status */
780   return HAL_OK;
781 }
782 /**
783   * @}
784   */
785 
786 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
787 /**
788   * @brief  Register a User DAC Callback
789   *         To be used instead of the weak (overridden) predefined callback
790   * @param hdac DAC handle
791   * @param CallbackId ID of the callback to be registered
792   *        This parameter can be one of the following values:
793   *          @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK   DAC Error Callback ID
794   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID       DAC CH1 Complete Callback ID
795   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID  DAC CH1 Half Complete Callback ID
796   *          @arg @ref HAL_DAC_CH1_ERROR_ID             DAC CH1 Error Callback ID
797   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID       DAC CH1 UnderRun Callback ID
798   *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID       DAC CH2 Complete Callback ID
799   *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID  DAC CH2 Half Complete Callback ID
800   *          @arg @ref HAL_DAC_CH2_ERROR_ID             DAC CH2 Error Callback ID
801   *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID       DAC CH2 UnderRun Callback ID
802   *          @arg @ref HAL_DAC_MSP_INIT_CB_ID           DAC MSP Init Callback ID
803   *          @arg @ref HAL_DAC_MSP_DEINIT_CB_ID         DAC MSP DeInit Callback ID
804   *
805     * @param pCallback pointer to the Callback function
806   * @retval status
807   */
HAL_DAC_RegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackId,pDAC_CallbackTypeDef pCallback)808 HAL_StatusTypeDef HAL_DAC_RegisterCallback (DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackId, pDAC_CallbackTypeDef pCallback)
809 {
810   HAL_StatusTypeDef status = HAL_OK;
811 
812   if(pCallback == NULL)
813   {
814     /* Update the error code */
815     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
816     return HAL_ERROR;
817   }
818 
819   /* Process locked */
820   __HAL_LOCK(hdac);
821 
822   if(hdac->State == HAL_DAC_STATE_READY)
823   {
824     switch (CallbackId)
825     {
826     case HAL_DAC_CH1_COMPLETE_CB_ID :
827       hdac->ConvCpltCallbackCh1 = pCallback;
828       break;
829     case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
830       hdac->ConvHalfCpltCallbackCh1 = pCallback;
831       break;
832     case HAL_DAC_CH1_ERROR_ID :
833       hdac->ErrorCallbackCh1 = pCallback;
834       break;
835     case HAL_DAC_CH1_UNDERRUN_CB_ID :
836       hdac->DMAUnderrunCallbackCh1 = pCallback;
837       break;
838     case HAL_DAC_CH2_COMPLETE_CB_ID :
839       hdac->ConvCpltCallbackCh2 = pCallback;
840       break;
841     case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
842       hdac->ConvHalfCpltCallbackCh2 = pCallback;
843       break;
844     case HAL_DAC_CH2_ERROR_ID :
845       hdac->ErrorCallbackCh2 = pCallback;
846       break;
847     case HAL_DAC_CH2_UNDERRUN_CB_ID :
848       hdac->DMAUnderrunCallbackCh2 = pCallback;
849       break;
850     case HAL_DAC_MSP_INIT_CB_ID :
851       hdac->MspInitCallback = pCallback;
852       break;
853     case HAL_DAC_MSP_DEINIT_CB_ID :
854       hdac->MspDeInitCallback = pCallback;
855       break;
856     default :
857       /* Update the error code */
858       hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
859       /* update return status */
860       status =  HAL_ERROR;
861       break;
862     }
863   }
864   else if (hdac->State == HAL_DAC_STATE_RESET)
865   {
866     switch (CallbackId)
867     {
868     case HAL_DAC_MSP_INIT_CB_ID :
869       hdac->MspInitCallback = pCallback;
870       break;
871     case HAL_DAC_MSP_DEINIT_CB_ID :
872       hdac->MspDeInitCallback = pCallback;
873       break;
874     default :
875       /* Update the error code */
876       hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
877       /* update return status */
878       status =  HAL_ERROR;
879       break;
880     }
881   }
882   else
883   {
884     /* Update the error code */
885     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
886     /* update return status */
887     status =  HAL_ERROR;
888   }
889 
890   /* Release Lock */
891   __HAL_UNLOCK(hdac);
892   return status;
893 }
894 
895 /**
896   * @brief  Unregister a User DAC Callback
897   *         DAC Callback is redirected to the weak (overridden) predefined callback
898   * @param hdac DAC handle
899   * @param  CallbackID ID of the callback to be unregistered
900   *        This parameter can be one of the following values:
901   *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID          DAC CH1 transfer Complete Callback ID
902   *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID     DAC CH1 Half Complete Callback ID
903   *          @arg @ref HAL_DAC_CH1_ERROR_ID                DAC CH1 Error Callback ID
904   *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID          DAC CH1 UnderRun Callback ID
905   *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID          DAC CH2 Complete Callback ID
906   *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID     DAC CH2 Half Complete Callback ID
907   *          @arg @ref HAL_DAC_CH2_ERROR_ID                DAC CH2 Error Callback ID
908   *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID          DAC CH2 UnderRun Callback ID
909   *          @arg @ref HAL_DAC_MSP_INIT_CB_ID              DAC MSP Init Callback ID
910   *          @arg @ref HAL_DAC_MSP_DEINIT_CB_ID            DAC MSP DeInit Callback ID
911   *          @arg @ref HAL_DAC_ALL_CB_ID                   DAC All callbacks
912   * @retval status
913   */
HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID)914 HAL_StatusTypeDef HAL_DAC_UnRegisterCallback (DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
915 {
916   HAL_StatusTypeDef status = HAL_OK;
917 
918   /* Process locked */
919   __HAL_LOCK(hdac);
920 
921   if(hdac->State == HAL_DAC_STATE_READY)
922   {
923     switch (CallbackID)
924     {
925     case HAL_DAC_CH1_COMPLETE_CB_ID :
926       hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
927       break;
928     case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
929       hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
930       break;
931     case HAL_DAC_CH1_ERROR_ID :
932       hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
933       break;
934     case HAL_DAC_CH1_UNDERRUN_CB_ID :
935       hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
936       break;
937 #if defined (DAC_CHANNEL2_SUPPORT)
938     case HAL_DAC_CH2_COMPLETE_CB_ID :
939       hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
940       break;
941     case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
942       hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
943       break;
944     case HAL_DAC_CH2_ERROR_ID :
945       hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
946       break;
947     case HAL_DAC_CH2_UNDERRUN_CB_ID :
948       hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
949       break;
950 #endif /* DAC_CHANNEL2_SUPPORT */
951     case HAL_DAC_MSP_INIT_CB_ID :
952       hdac->MspInitCallback = HAL_DAC_MspInit;
953       break;
954     case HAL_DAC_MSP_DEINIT_CB_ID :
955       hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
956       break;
957     case HAL_DAC_ALL_CB_ID :
958       hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
959       hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
960       hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
961       hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
962 #if defined (DAC_CHANNEL2_SUPPORT)
963       hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
964       hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
965       hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
966       hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
967 #endif /* DAC_CHANNEL2_SUPPORT */
968       hdac->MspInitCallback = HAL_DAC_MspInit;
969       hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
970       break;
971     default :
972       /* Update the error code */
973       hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
974       /* update return status */
975       status =  HAL_ERROR;
976       break;
977     }
978   }
979   else if (hdac->State == HAL_DAC_STATE_RESET)
980   {
981     switch (CallbackID)
982     {
983     case HAL_DAC_MSP_INIT_CB_ID :
984       hdac->MspInitCallback = HAL_DAC_MspInit;
985       break;
986     case HAL_DAC_MSP_DEINIT_CB_ID :
987       hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
988       break;
989     default :
990       /* Update the error code */
991       hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
992       /* update return status */
993       status =  HAL_ERROR;
994       break;
995     }
996   }
997   else
998   {
999     /* Update the error code */
1000     hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1001     /* update return status */
1002     status =  HAL_ERROR;
1003   }
1004 
1005   /* Release Lock */
1006   __HAL_UNLOCK(hdac);
1007   return status;
1008 }
1009 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1010 
1011 /**
1012   * @}
1013   */
1014 
1015 /**
1016   * @}
1017   */
1018 
1019 /**
1020   * @}
1021   */
1022 #endif /* HAL_DAC_MODULE_ENABLED */
1023 #endif /* !STM32L010xB && !STM32L010x8 && !STM32L010x6 && !STM32L010x4 && !STM32L011xx && !STM32L021xx && !STM32L031xx && !STM32L041xx && !STM32L051xx !STM32L071xx&& !STM32L081xx */
1024 
1025 
1026