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