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