1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_dac.c
4 * @author MCD Application Team
5 * @brief DAC HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Digital to Analog Converter (DAC) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State and Errors functions
12 *
13 *
14 @verbatim
15 ==============================================================================
16 ##### DAC Peripheral features #####
17 ==============================================================================
18 [..]
19 *** DAC Channels ***
20 ====================
21 [..]
22 The device integrates two 12-bit Digital Analog Converters that can
23 be used independently or simultaneously (dual mode):
24 (#) DAC channel1 with DAC_OUT1 (PA4) as output
25 (#) DAC channel2 with DAC_OUT2 (PA5) as output
26
27 *** DAC Triggers ***
28 ====================
29 [..]
30 Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
31 and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
32 [..]
33 Digital to Analog conversion can be triggered by:
34 (#) External event: EXTI Line 9 (any GPIOx_Pin9) using DAC_TRIGGER_EXT_IT9.
35 The used pin (GPIOx_Pin9) must be configured in input mode.
36
37 (#) Timers TRGO: TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8
38 (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
39
40 (#) Software using DAC_TRIGGER_SOFTWARE
41
42 *** DAC Buffer mode feature ***
43 ===============================
44 [..]
45 Each DAC channel integrates an output buffer that can be used to
46 reduce the output impedance, and to drive external loads directly
47 without having to add an external operational amplifier.
48 To enable, the output buffer use
49 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
50 [..]
51 (@) Refer to the device datasheet for more details about output
52 impedance value with and without output buffer.
53
54 *** DAC wave generation feature ***
55 ===================================
56 [..]
57 Both DAC channels can be used to generate
58 (#) Noise wave
59 (#) Triangle wave
60
61 *** DAC data format ***
62 =======================
63 [..]
64 The DAC data format can be:
65 (#) 8-bit right alignment using DAC_ALIGN_8B_R
66 (#) 12-bit left alignment using DAC_ALIGN_12B_L
67 (#) 12-bit right alignment using DAC_ALIGN_12B_R
68
69 *** DAC data value to voltage correspondence ***
70 ================================================
71 [..]
72 The analog output voltage on each DAC channel pin is determined
73 by the following equation:
74 DAC_OUTx = VREF+ * DOR / 4095
75 with DOR is the Data Output Register
76 VEF+ is the input voltage reference (refer to the device datasheet)
77 e.g. To set DAC_OUT1 to 0.7V, use
78 Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
79
80 *** DMA requests ***
81 =====================
82 [..]
83 A DMA1 request can be generated when an external trigger (but not
84 a software trigger) occurs if DMA1 requests are enabled using
85 HAL_DAC_Start_DMA()
86 [..]
87 DMA1 requests are mapped as following:
88 (#) DAC channel1 : mapped on DMA1 Stream5 channel7 which must be
89 already configured
90 (#) DAC channel2 : mapped on DMA1 Stream6 channel7 which must be
91 already configured
92
93 -@- For Dual mode and specific signal (Triangle and noise) generation please
94 refer to Extension Features Driver description
95
96
97 ##### How to use this driver #####
98 ==============================================================================
99 [..]
100 (+) DAC APB clock must be enabled to get write access to DAC
101 registers using HAL_DAC_Init()
102 (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
103 (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
104 (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
105
106 *** Polling mode IO operation ***
107 =================================
108 [..]
109 (+) Start the DAC peripheral using HAL_DAC_Start()
110 (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
111 (+) Stop the DAC peripheral using HAL_DAC_Stop()
112
113 *** DMA mode IO operation ***
114 ==============================
115 [..]
116 (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
117 of data to be transferred at each end of conversion
118 (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
119 function is executed and user can add his own code by customization of function pointer
120 HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
121 (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
122 add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
123 (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
124
125 *** Callback registration ***
126 =============================================
127 [..]
128 The compilation define USE_HAL_DAC_REGISTER_CALLBACKS when set to 1
129 allows the user to configure dynamically the driver callbacks.
130
131 Use Functions @ref HAL_DAC_RegisterCallback() to register a user callback,
132 it allows to register following callbacks:
133 (+) ConvCpltCallbackCh1 : callback when a half transfer is completed on Ch1.
134 (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
135 (+) ErrorCallbackCh1 : callback when an error occurs on Ch1.
136 (+) DMAUnderrunCallbackCh1 : callback when an underrun error occurs on Ch1.
137 (+) ConvCpltCallbackCh2 : callback when a half transfer is completed on Ch2.
138 (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
139 (+) ErrorCallbackCh2 : callback when an error occurs on Ch2.
140 (+) DMAUnderrunCallbackCh2 : callback when an underrun error occurs on Ch2.
141 (+) MspInitCallback : DAC MspInit.
142 (+) MspDeInitCallback : DAC MspdeInit.
143 This function takes as parameters the HAL peripheral handle, the Callback ID
144 and a pointer to the user callback function.
145
146 Use function @ref HAL_DAC_UnRegisterCallback() to reset a callback to the default
147 weak (surcharged) function. It allows to reset following callbacks:
148 (+) ConvCpltCallbackCh1 : callback when a half transfer is completed on Ch1.
149 (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
150 (+) ErrorCallbackCh1 : callback when an error occurs on Ch1.
151 (+) DMAUnderrunCallbackCh1 : callback when an underrun error occurs on Ch1.
152 (+) ConvCpltCallbackCh2 : callback when a half transfer is completed on Ch2.
153 (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
154 (+) ErrorCallbackCh2 : callback when an error occurs on Ch2.
155 (+) DMAUnderrunCallbackCh2 : callback when an underrun error occurs on Ch2.
156 (+) MspInitCallback : DAC MspInit.
157 (+) MspDeInitCallback : DAC MspdeInit.
158 (+) All Callbacks
159 This function) takes as parameters the HAL peripheral handle and the Callback ID.
160
161 By default, after the @ref HAL_DAC_Init and if the state is HAL_DAC_STATE_RESET
162 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
163 Exception done for MspInit and MspDeInit callbacks that are respectively
164 reset to the legacy weak (surcharged) functions in the @ref HAL_DAC_Init
165 and @ref HAL_DAC_DeInit only when these callbacks are null (not registered beforehand).
166 If not, MspInit or MspDeInit are not null, the @ref HAL_DAC_Init and @ref HAL_DAC_DeInit
167 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
168
169 Callbacks can be registered/unregistered in READY state only.
170 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
171 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
172 during the Init/DeInit.
173 In that case first register the MspInit/MspDeInit user callbacks
174 using @ref HAL_DAC_RegisterCallback before calling @ref HAL_DAC_DeInit
175 or @ref HAL_DAC_Init function.
176
177 When The compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or
178 not defined, the callback registering feature is not available
179 and weak (surcharged) callbacks are used.
180 *** DAC HAL driver macros list ***
181 =============================================
182 [..]
183 Below the list of most used macros in DAC HAL driver.
184
185 (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
186 (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
187 (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
188 (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
189
190 [..]
191 (@) You can refer to the DAC HAL driver header file for more useful macros
192
193 @endverbatim
194 ******************************************************************************
195 * @attention
196 *
197 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
198 * All rights reserved.</center></h2>
199 *
200 * This software component is licensed by ST under BSD 3-Clause license,
201 * the "License"; You may not use this file except in compliance with the
202 * License. You may obtain a copy of the License at:
203 * opensource.org/licenses/BSD-3-Clause
204 *
205 ******************************************************************************
206 */
207
208
209 /* Includes ------------------------------------------------------------------*/
210 #include "stm32f4xx_hal.h"
211
212 /** @addtogroup STM32F4xx_HAL_Driver
213 * @{
214 */
215
216 /** @defgroup DAC DAC
217 * @brief DAC driver modules
218 * @{
219 */
220
221 #ifdef HAL_DAC_MODULE_ENABLED
222
223 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
224 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
225 defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) ||\
226 defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || defined(STM32F423xx)
227 /* Private typedef -----------------------------------------------------------*/
228 /* Private define ------------------------------------------------------------*/
229 /* Private macro -------------------------------------------------------------*/
230 /* Private variables ---------------------------------------------------------*/
231 /** @addtogroup DAC_Private_Functions
232 * @{
233 */
234 /* Private function prototypes -----------------------------------------------*/
235 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
236 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
237 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma);
238 /**
239 * @}
240 */
241
242 /* Exported functions --------------------------------------------------------*/
243 /** @defgroup DAC_Exported_Functions DAC Exported Functions
244 * @{
245 */
246
247 /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
248 * @brief Initialization and Configuration functions
249 *
250 @verbatim
251 ==============================================================================
252 ##### Initialization and de-initialization functions #####
253 ==============================================================================
254 [..] This section provides functions allowing to:
255 (+) Initialize and configure the DAC.
256 (+) De-initialize the DAC.
257
258 @endverbatim
259 * @{
260 */
261
262 /**
263 * @brief Initializes the DAC peripheral according to the specified parameters
264 * in the DAC_InitStruct.
265 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
266 * the configuration information for the specified DAC.
267 * @retval HAL status
268 */
HAL_DAC_Init(DAC_HandleTypeDef * hdac)269 HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
270 {
271 /* Check DAC handle */
272 if(hdac == NULL)
273 {
274 return HAL_ERROR;
275 }
276 /* Check the parameters */
277 assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
278
279 if(hdac->State == HAL_DAC_STATE_RESET)
280 {
281 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
282 /* Init the DAC Callback settings */
283 hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
284 hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
285 hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
286 hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
287
288 hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
289 hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
290 hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
291 hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
292
293 if(hdac->MspInitCallback == NULL)
294 {
295 hdac->MspInitCallback = HAL_DAC_MspInit;
296 }
297 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
298 /* Allocate lock resource and initialize it */
299 hdac->Lock = HAL_UNLOCKED;
300 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
301 /* Init the low level hardware */
302 hdac->MspInitCallback(hdac);
303 #else
304 /* Init the low level hardware */
305 HAL_DAC_MspInit(hdac);
306 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
307 }
308
309 /* Initialize the DAC state*/
310 hdac->State = HAL_DAC_STATE_BUSY;
311
312 /* Set DAC error code to none */
313 hdac->ErrorCode = HAL_DAC_ERROR_NONE;
314
315 /* Initialize the DAC state*/
316 hdac->State = HAL_DAC_STATE_READY;
317
318 /* Return function status */
319 return HAL_OK;
320 }
321
322 /**
323 * @brief Deinitializes the DAC peripheral registers to their default reset values.
324 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
325 * the configuration information for the specified DAC.
326 * @retval HAL status
327 */
HAL_DAC_DeInit(DAC_HandleTypeDef * hdac)328 HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
329 {
330 /* Check DAC handle */
331 if(hdac == NULL)
332 {
333 return HAL_ERROR;
334 }
335
336 /* Check the parameters */
337 assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
338
339 /* Change DAC state */
340 hdac->State = HAL_DAC_STATE_BUSY;
341
342 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
343 if(hdac->MspDeInitCallback == NULL)
344 {
345 hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
346 }
347 /* DeInit the low level hardware */
348 hdac->MspDeInitCallback(hdac);
349 #else
350 /* DeInit the low level hardware */
351 HAL_DAC_MspDeInit(hdac);
352 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
353
354 /* Set DAC error code to none */
355 hdac->ErrorCode = HAL_DAC_ERROR_NONE;
356
357 /* Change DAC state */
358 hdac->State = HAL_DAC_STATE_RESET;
359
360 /* Release Lock */
361 __HAL_UNLOCK(hdac);
362
363 /* Return function status */
364 return HAL_OK;
365 }
366
367 /**
368 * @brief Initializes the DAC MSP.
369 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
370 * the configuration information for the specified DAC.
371 * @retval None
372 */
HAL_DAC_MspInit(DAC_HandleTypeDef * hdac)373 __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
374 {
375 /* Prevent unused argument(s) compilation warning */
376 UNUSED(hdac);
377 /* NOTE : This function Should not be modified, when the callback is needed,
378 the HAL_DAC_MspInit could be implemented in the user file
379 */
380 }
381
382 /**
383 * @brief DeInitializes the DAC MSP.
384 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
385 * the configuration information for the specified DAC.
386 * @retval None
387 */
HAL_DAC_MspDeInit(DAC_HandleTypeDef * hdac)388 __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
389 {
390 /* Prevent unused argument(s) compilation warning */
391 UNUSED(hdac);
392 /* NOTE : This function Should not be modified, when the callback is needed,
393 the HAL_DAC_MspDeInit could be implemented in the user file
394 */
395 }
396
397 /**
398 * @}
399 */
400
401 /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
402 * @brief IO operation functions
403 *
404 @verbatim
405 ==============================================================================
406 ##### IO operation functions #####
407 ==============================================================================
408 [..] This section provides functions allowing to:
409 (+) Start conversion.
410 (+) Stop conversion.
411 (+) Start conversion and enable DMA transfer.
412 (+) Stop conversion and disable DMA transfer.
413 (+) Get result of conversion.
414
415 @endverbatim
416 * @{
417 */
418
419 /**
420 * @brief Enables DAC and starts conversion of channel.
421 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
422 * the configuration information for the specified DAC.
423 * @param Channel The selected DAC channel.
424 * This parameter can be one of the following values:
425 * @arg DAC_CHANNEL_1: DAC Channel1 selected
426 * @arg DAC_CHANNEL_2: DAC Channel2 selected
427 * @retval HAL status
428 */
HAL_DAC_Start(DAC_HandleTypeDef * hdac,uint32_t Channel)429 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
430 {
431 uint32_t tmp1 = 0U, tmp2 = 0U;
432
433 /* Check the parameters */
434 assert_param(IS_DAC_CHANNEL(Channel));
435
436 /* Process locked */
437 __HAL_LOCK(hdac);
438
439 /* Change DAC state */
440 hdac->State = HAL_DAC_STATE_BUSY;
441
442 /* Enable the Peripheral */
443 __HAL_DAC_ENABLE(hdac, Channel);
444
445 if(Channel == DAC_CHANNEL_1)
446 {
447 tmp1 = hdac->Instance->CR & DAC_CR_TEN1;
448 tmp2 = hdac->Instance->CR & DAC_CR_TSEL1;
449 /* Check if software trigger enabled */
450 if((tmp1 == DAC_CR_TEN1) && (tmp2 == DAC_CR_TSEL1))
451 {
452 /* Enable the selected DAC software conversion */
453 hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
454 }
455 }
456 else
457 {
458 tmp1 = hdac->Instance->CR & DAC_CR_TEN2;
459 tmp2 = hdac->Instance->CR & DAC_CR_TSEL2;
460 /* Check if software trigger enabled */
461 if((tmp1 == DAC_CR_TEN2) && (tmp2 == DAC_CR_TSEL2))
462 {
463 /* Enable the selected DAC software conversion*/
464 hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
465 }
466 }
467
468 /* Change DAC state */
469 hdac->State = HAL_DAC_STATE_READY;
470
471 /* Process unlocked */
472 __HAL_UNLOCK(hdac);
473
474 /* Return function status */
475 return HAL_OK;
476 }
477
478 /**
479 * @brief Disables DAC and stop conversion of channel.
480 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
481 * the configuration information for the specified DAC.
482 * @param Channel The selected DAC channel.
483 * This parameter can be one of the following values:
484 * @arg DAC_CHANNEL_1: DAC Channel1 selected
485 * @arg DAC_CHANNEL_2: DAC Channel2 selected
486 * @retval HAL status
487 */
HAL_DAC_Stop(DAC_HandleTypeDef * hdac,uint32_t Channel)488 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
489 {
490 /* Check the parameters */
491 assert_param(IS_DAC_CHANNEL(Channel));
492
493 /* Disable the Peripheral */
494 __HAL_DAC_DISABLE(hdac, Channel);
495
496 /* Change DAC state */
497 hdac->State = HAL_DAC_STATE_READY;
498
499 /* Return function status */
500 return HAL_OK;
501 }
502
503 /**
504 * @brief Enables DAC and starts 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: DAC Channel1 selected
510 * @arg DAC_CHANNEL_2: DAC Channel2 selected
511 * @param pData The destination peripheral Buffer address.
512 * @param Length The length of data to be transferred from memory to DAC peripheral
513 * @param Alignment Specifies the data alignment for DAC channel.
514 * This parameter can be one of the following values:
515 * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
516 * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
517 * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
518 * @retval HAL status
519 */
HAL_DAC_Start_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t * pData,uint32_t Length,uint32_t Alignment)520 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
521 {
522 uint32_t tmpreg = 0U;
523
524 /* Check the parameters */
525 assert_param(IS_DAC_CHANNEL(Channel));
526 assert_param(IS_DAC_ALIGN(Alignment));
527
528 /* Process locked */
529 __HAL_LOCK(hdac);
530
531 /* Change DAC state */
532 hdac->State = HAL_DAC_STATE_BUSY;
533
534 if(Channel == DAC_CHANNEL_1)
535 {
536 /* Set the DMA transfer complete callback for channel1 */
537 hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
538
539 /* Set the DMA half transfer complete callback for channel1 */
540 hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
541
542 /* Set the DMA error callback for channel1 */
543 hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
544
545 /* Enable the selected DAC channel1 DMA request */
546 hdac->Instance->CR |= DAC_CR_DMAEN1;
547
548 /* Case of use of channel 1 */
549 switch(Alignment)
550 {
551 case DAC_ALIGN_12B_R:
552 /* Get DHR12R1 address */
553 tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
554 break;
555 case DAC_ALIGN_12B_L:
556 /* Get DHR12L1 address */
557 tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
558 break;
559 case DAC_ALIGN_8B_R:
560 /* Get DHR8R1 address */
561 tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
562 break;
563 default:
564 break;
565 }
566 }
567 else
568 {
569 /* Set the DMA transfer complete callback for channel2 */
570 hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
571
572 /* Set the DMA half transfer complete callback for channel2 */
573 hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
574
575 /* Set the DMA error callback for channel2 */
576 hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
577
578 /* Enable the selected DAC channel2 DMA request */
579 hdac->Instance->CR |= DAC_CR_DMAEN2;
580
581 /* Case of use of channel 2 */
582 switch(Alignment)
583 {
584 case DAC_ALIGN_12B_R:
585 /* Get DHR12R2 address */
586 tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
587 break;
588 case DAC_ALIGN_12B_L:
589 /* Get DHR12L2 address */
590 tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
591 break;
592 case DAC_ALIGN_8B_R:
593 /* Get DHR8R2 address */
594 tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
595 break;
596 default:
597 break;
598 }
599 }
600
601 /* Enable the DMA Stream */
602 if(Channel == DAC_CHANNEL_1)
603 {
604 /* Enable the DAC DMA underrun interrupt */
605 __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
606
607 /* Enable the DMA Stream */
608 HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
609 }
610 else
611 {
612 /* Enable the DAC DMA underrun interrupt */
613 __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
614
615 /* Enable the DMA Stream */
616 HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
617 }
618
619 /* Enable the Peripheral */
620 __HAL_DAC_ENABLE(hdac, Channel);
621
622 /* Process Unlocked */
623 __HAL_UNLOCK(hdac);
624
625 /* Return function status */
626 return HAL_OK;
627 }
628
629 /**
630 * @brief Disables DAC and stop conversion of channel.
631 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
632 * the configuration information for the specified DAC.
633 * @param Channel The selected DAC channel.
634 * This parameter can be one of the following values:
635 * @arg DAC_CHANNEL_1: DAC Channel1 selected
636 * @arg DAC_CHANNEL_2: DAC Channel2 selected
637 * @retval HAL status
638 */
HAL_DAC_Stop_DMA(DAC_HandleTypeDef * hdac,uint32_t Channel)639 HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
640 {
641 HAL_StatusTypeDef status = HAL_OK;
642
643 /* Check the parameters */
644 assert_param(IS_DAC_CHANNEL(Channel));
645
646 /* Disable the selected DAC channel DMA request */
647 hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
648
649 /* Disable the Peripheral */
650 __HAL_DAC_DISABLE(hdac, Channel);
651
652 /* Disable the DMA Channel */
653 /* Channel1 is used */
654 if(Channel == DAC_CHANNEL_1)
655 {
656 status = HAL_DMA_Abort(hdac->DMA_Handle1);
657 }
658 else /* Channel2 is used for */
659 {
660 status = HAL_DMA_Abort(hdac->DMA_Handle2);
661 }
662
663 /* Check if DMA Channel effectively disabled */
664 if(status != HAL_OK)
665 {
666 /* Update DAC state machine to error */
667 hdac->State = HAL_DAC_STATE_ERROR;
668 }
669 else
670 {
671 /* Change DAC state */
672 hdac->State = HAL_DAC_STATE_READY;
673 }
674
675 /* Return function status */
676 return status;
677 }
678
679 /**
680 * @brief Returns the last data output value of the selected DAC channel.
681 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
682 * the configuration information for the specified DAC.
683 * @param Channel The selected DAC channel.
684 * This parameter can be one of the following values:
685 * @arg DAC_CHANNEL_1: DAC Channel1 selected
686 * @arg DAC_CHANNEL_2: DAC Channel2 selected
687 * @retval The selected DAC channel data output value.
688 */
HAL_DAC_GetValue(DAC_HandleTypeDef * hdac,uint32_t Channel)689 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
690 {
691 /* Check the parameters */
692 assert_param(IS_DAC_CHANNEL(Channel));
693
694 /* Returns the DAC channel data output register value */
695 if(Channel == DAC_CHANNEL_1)
696 {
697 return hdac->Instance->DOR1;
698 }
699 else
700 {
701 return hdac->Instance->DOR2;
702 }
703 }
704
705 /**
706 * @brief Handles DAC interrupt request
707 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
708 * the configuration information for the specified DAC.
709 * @retval None
710 */
HAL_DAC_IRQHandler(DAC_HandleTypeDef * hdac)711 void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
712 {
713 /* Check underrun channel 1 flag */
714 if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
715 {
716 /* Change DAC state to error state */
717 hdac->State = HAL_DAC_STATE_ERROR;
718
719 /* Set DAC error code to channel1 DMA underrun error */
720 hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
721
722 /* Clear the underrun flag */
723 __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
724
725 /* Disable the selected DAC channel1 DMA request */
726 hdac->Instance->CR &= ~DAC_CR_DMAEN1;
727
728 /* Error callback */
729 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
730 hdac->DMAUnderrunCallbackCh1(hdac);
731 #else
732 HAL_DAC_DMAUnderrunCallbackCh1(hdac);
733 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
734 }
735 /* Check underrun channel 2 flag */
736 if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
737 {
738 /* Change DAC state to error state */
739 hdac->State = HAL_DAC_STATE_ERROR;
740
741 /* Set DAC error code to channel2 DMA underrun error */
742 hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH2;
743
744 /* Clear the underrun flag */
745 __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
746
747 /* Disable the selected DAC channel1 DMA request */
748 hdac->Instance->CR &= ~DAC_CR_DMAEN2;
749
750 /* Error callback */
751 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
752 hdac->DMAUnderrunCallbackCh2(hdac);
753 #else
754 HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
755 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
756 }
757 }
758
759 /**
760 * @brief Conversion complete callback in non blocking mode for Channel1
761 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
762 * the configuration information for the specified DAC.
763 * @retval None
764 */
HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef * hdac)765 __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
766 {
767 /* Prevent unused argument(s) compilation warning */
768 UNUSED(hdac);
769 /* NOTE : This function Should not be modified, when the callback is needed,
770 the HAL_DAC_ConvCpltCallback could be implemented in the user file
771 */
772 }
773
774 /**
775 * @brief Conversion half DMA transfer callback in non blocking mode for Channel1
776 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
777 * the configuration information for the specified DAC.
778 * @retval None
779 */
HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef * hdac)780 __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
781 {
782 /* Prevent unused argument(s) compilation warning */
783 UNUSED(hdac);
784 /* NOTE : This function Should not be modified, when the callback is needed,
785 the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
786 */
787 }
788
789 /**
790 * @brief Error DAC callback for Channel1.
791 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
792 * the configuration information for the specified DAC.
793 * @retval None
794 */
HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef * hdac)795 __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
796 {
797 /* Prevent unused argument(s) compilation warning */
798 UNUSED(hdac);
799 /* NOTE : This function Should not be modified, when the callback is needed,
800 the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
801 */
802 }
803
804 /**
805 * @brief DMA underrun DAC callback for channel1.
806 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
807 * the configuration information for the specified DAC.
808 * @retval None
809 */
HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef * hdac)810 __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
811 {
812 /* Prevent unused argument(s) compilation warning */
813 UNUSED(hdac);
814 /* NOTE : This function Should not be modified, when the callback is needed,
815 the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
816 */
817 }
818
819 /**
820 * @}
821 */
822
823 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
824 * @brief Peripheral Control functions
825 *
826 @verbatim
827 ==============================================================================
828 ##### Peripheral Control functions #####
829 ==============================================================================
830 [..] This section provides functions allowing to:
831 (+) Configure channels.
832 (+) Set the specified data holding register value for DAC channel.
833
834 @endverbatim
835 * @{
836 */
837
838 /**
839 * @brief Configures the selected DAC channel.
840 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
841 * the configuration information for the specified DAC.
842 * @param sConfig DAC configuration structure.
843 * @param Channel The selected DAC channel.
844 * This parameter can be one of the following values:
845 * @arg DAC_CHANNEL_1: DAC Channel1 selected
846 * @arg DAC_CHANNEL_2: DAC Channel2 selected
847 * @retval HAL status
848 */
HAL_DAC_ConfigChannel(DAC_HandleTypeDef * hdac,DAC_ChannelConfTypeDef * sConfig,uint32_t Channel)849 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
850 {
851 uint32_t tmpreg1 = 0U, tmpreg2 = 0U;
852
853 /* Check the DAC parameters */
854 assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
855 assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
856 assert_param(IS_DAC_CHANNEL(Channel));
857
858 /* Process locked */
859 __HAL_LOCK(hdac);
860
861 /* Change DAC state */
862 hdac->State = HAL_DAC_STATE_BUSY;
863
864 /* Get the DAC CR value */
865 tmpreg1 = hdac->Instance->CR;
866 /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
867 tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
868 /* Configure for the selected DAC channel: buffer output, trigger */
869 /* Set TSELx and TENx bits according to DAC_Trigger value */
870 /* Set BOFFx bit according to DAC_OutputBuffer value */
871 tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
872 /* Calculate CR register value depending on DAC_Channel */
873 tmpreg1 |= tmpreg2 << Channel;
874 /* Write to DAC CR */
875 hdac->Instance->CR = tmpreg1;
876 /* Disable wave generation */
877 hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
878
879 /* Change DAC state */
880 hdac->State = HAL_DAC_STATE_READY;
881
882 /* Process unlocked */
883 __HAL_UNLOCK(hdac);
884
885 /* Return function status */
886 return HAL_OK;
887 }
888
889 /**
890 * @brief Set the specified data holding register value for DAC channel.
891 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
892 * the configuration information for the specified DAC.
893 * @param Channel The selected DAC channel.
894 * This parameter can be one of the following values:
895 * @arg DAC_CHANNEL_1: DAC Channel1 selected
896 * @arg DAC_CHANNEL_2: DAC Channel2 selected
897 * @param Alignment Specifies the data alignment.
898 * This parameter can be one of the following values:
899 * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
900 * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
901 * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
902 * @param Data Data to be loaded in the selected data holding register.
903 * @retval HAL status
904 */
HAL_DAC_SetValue(DAC_HandleTypeDef * hdac,uint32_t Channel,uint32_t Alignment,uint32_t Data)905 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
906 {
907 __IO uint32_t tmp = 0U;
908
909 /* Check the parameters */
910 assert_param(IS_DAC_CHANNEL(Channel));
911 assert_param(IS_DAC_ALIGN(Alignment));
912 assert_param(IS_DAC_DATA(Data));
913
914 tmp = (uint32_t)hdac->Instance;
915 if(Channel == DAC_CHANNEL_1)
916 {
917 tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
918 }
919 else
920 {
921 tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
922 }
923
924 /* Set the DAC channel1 selected data holding register */
925 *(__IO uint32_t *) tmp = Data;
926
927 /* Return function status */
928 return HAL_OK;
929 }
930
931 /**
932 * @}
933 */
934
935 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
936 * @brief Peripheral State and Errors functions
937 *
938 @verbatim
939 ==============================================================================
940 ##### Peripheral State and Errors functions #####
941 ==============================================================================
942 [..]
943 This subsection provides functions allowing to
944 (+) Check the DAC state.
945 (+) Check the DAC Errors.
946
947 @endverbatim
948 * @{
949 */
950
951 /**
952 * @brief return the DAC state
953 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
954 * the configuration information for the specified DAC.
955 * @retval HAL state
956 */
HAL_DAC_GetState(DAC_HandleTypeDef * hdac)957 HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
958 {
959 /* Return DAC state */
960 return hdac->State;
961 }
962
963
964 /**
965 * @brief Return the DAC error code
966 * @param hdac pointer to a DAC_HandleTypeDef structure that contains
967 * the configuration information for the specified DAC.
968 * @retval DAC Error Code
969 */
HAL_DAC_GetError(DAC_HandleTypeDef * hdac)970 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
971 {
972 return hdac->ErrorCode;
973 }
974
975 /**
976 * @}
977 */
978
979 /**
980 * @}
981 */
982
983 /** @addtogroup DAC_Exported_Functions
984 * @{
985 */
986
987 /** @addtogroup DAC_Exported_Functions_Group1
988 * @{
989 */
990 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
991 /**
992 * @brief Register a User DAC Callback
993 * To be used instead of the weak (surcharged) predefined callback
994 * @param hdac DAC handle
995 * @param CallbackID ID of the callback to be registered
996 * This parameter can be one of the following values:
997 * @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK DAC Error Callback ID
998 * @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID DAC CH1 Complete Callback ID
999 * @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID DAC CH1 Half Complete Callback ID
1000 * @arg @ref HAL_DAC_CH1_ERROR_ID DAC CH1 Error Callback ID
1001 * @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID DAC CH1 UnderRun Callback ID
1002 * @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID DAC CH2 Complete Callback ID
1003 * @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID DAC CH2 Half Complete Callback ID
1004 * @arg @ref HAL_DAC_CH2_ERROR_ID DAC CH2 Error Callback ID
1005 * @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID DAC CH2 UnderRun Callback ID
1006 * @arg @ref HAL_DAC_MSP_INIT_CB_ID DAC MSP Init Callback ID
1007 * @arg @ref HAL_DAC_MSP_DEINIT_CB_ID DAC MSP DeInit Callback ID
1008 *
1009 * @param pCallback pointer to the Callback function
1010 * @retval status
1011 */
HAL_DAC_RegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID,pDAC_CallbackTypeDef pCallback)1012 HAL_StatusTypeDef HAL_DAC_RegisterCallback (DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID, pDAC_CallbackTypeDef pCallback)
1013 {
1014 HAL_StatusTypeDef status = HAL_OK;
1015
1016 if(pCallback == NULL)
1017 {
1018 /* Update the error code */
1019 hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1020 return HAL_ERROR;
1021 }
1022
1023 /* Process locked */
1024 __HAL_LOCK(hdac);
1025
1026 if(hdac->State == HAL_DAC_STATE_READY)
1027 {
1028 switch (CallbackID)
1029 {
1030 case HAL_DAC_CH1_COMPLETE_CB_ID :
1031 hdac->ConvCpltCallbackCh1 = pCallback;
1032 break;
1033 case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1034 hdac->ConvHalfCpltCallbackCh1 = pCallback;
1035 break;
1036 case HAL_DAC_CH1_ERROR_ID :
1037 hdac->ErrorCallbackCh1 = pCallback;
1038 break;
1039 case HAL_DAC_CH1_UNDERRUN_CB_ID :
1040 hdac->DMAUnderrunCallbackCh1 = pCallback;
1041 break;
1042 case HAL_DAC_CH2_COMPLETE_CB_ID :
1043 hdac->ConvCpltCallbackCh2 = pCallback;
1044 break;
1045 case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1046 hdac->ConvHalfCpltCallbackCh2 = pCallback;
1047 break;
1048 case HAL_DAC_CH2_ERROR_ID :
1049 hdac->ErrorCallbackCh2 = pCallback;
1050 break;
1051 case HAL_DAC_CH2_UNDERRUN_CB_ID :
1052 hdac->DMAUnderrunCallbackCh2 = pCallback;
1053 break;
1054 case HAL_DAC_MSP_INIT_CB_ID :
1055 hdac->MspInitCallback = pCallback;
1056 break;
1057 case HAL_DAC_MSP_DEINIT_CB_ID :
1058 hdac->MspDeInitCallback = pCallback;
1059 break;
1060 default :
1061 /* Update the error code */
1062 hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1063 /* update return status */
1064 status = HAL_ERROR;
1065 break;
1066 }
1067 }
1068 else if (hdac->State == HAL_DAC_STATE_RESET)
1069 {
1070 switch (CallbackID)
1071 {
1072 case HAL_DAC_MSP_INIT_CB_ID :
1073 hdac->MspInitCallback = pCallback;
1074 break;
1075 case HAL_DAC_MSP_DEINIT_CB_ID :
1076 hdac->MspDeInitCallback = pCallback;
1077 break;
1078 default :
1079 /* Update the error code */
1080 hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1081 /* update return status */
1082 status = HAL_ERROR;
1083 break;
1084 }
1085 }
1086 else
1087 {
1088 /* Update the error code */
1089 hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1090 /* update return status */
1091 status = HAL_ERROR;
1092 }
1093
1094 /* Release Lock */
1095 __HAL_UNLOCK(hdac);
1096 return status;
1097 }
1098
1099 /**
1100 * @brief Unregister a User DAC Callback
1101 * DAC Callback is redirected to the weak (surcharged) predefined callback
1102 * @param hdac DAC handle
1103 * @param CallbackID ID of the callback to be unregistered
1104 * This parameter can be one of the following values:
1105 * @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID DAC CH1 tranfer Complete Callback ID
1106 * @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID DAC CH1 Half Complete Callback ID
1107 * @arg @ref HAL_DAC_CH1_ERROR_ID DAC CH1 Error Callback ID
1108 * @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID DAC CH1 UnderRun Callback ID
1109 * @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID DAC CH2 Complete Callback ID
1110 * @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID DAC CH2 Half Complete Callback ID
1111 * @arg @ref HAL_DAC_CH2_ERROR_ID DAC CH2 Error Callback ID
1112 * @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID DAC CH2 UnderRun Callback ID
1113 * @arg @ref HAL_DAC_MSP_INIT_CB_ID DAC MSP Init Callback ID
1114 * @arg @ref HAL_DAC_MSP_DEINIT_CB_ID DAC MSP DeInit Callback ID
1115 * @arg @ref HAL_DAC_ALL_CB_ID DAC All callbacks
1116 * @retval status
1117 */
HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef * hdac,HAL_DAC_CallbackIDTypeDef CallbackID)1118 HAL_StatusTypeDef HAL_DAC_UnRegisterCallback (DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
1119 {
1120 HAL_StatusTypeDef status = HAL_OK;
1121
1122 /* Process locked */
1123 __HAL_LOCK(hdac);
1124
1125 if(hdac->State == HAL_DAC_STATE_READY)
1126 {
1127 switch (CallbackID)
1128 {
1129 case HAL_DAC_CH1_COMPLETE_CB_ID :
1130 hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1131 break;
1132 case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1133 hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1134 break;
1135 case HAL_DAC_CH1_ERROR_ID :
1136 hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1137 break;
1138 case HAL_DAC_CH1_UNDERRUN_CB_ID :
1139 hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1140 break;
1141 case HAL_DAC_CH2_COMPLETE_CB_ID :
1142 hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1143 break;
1144 case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1145 hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1146 break;
1147 case HAL_DAC_CH2_ERROR_ID :
1148 hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1149 break;
1150 case HAL_DAC_CH2_UNDERRUN_CB_ID :
1151 hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1152 break;
1153 case HAL_DAC_MSP_INIT_CB_ID :
1154 hdac->MspInitCallback = HAL_DAC_MspInit;
1155 break;
1156 case HAL_DAC_MSP_DEINIT_CB_ID :
1157 hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1158 break;
1159 case HAL_DAC_ALL_CB_ID :
1160 hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1161 hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1162 hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1163 hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1164 hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1165 hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1166 hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1167 hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1168 hdac->MspInitCallback = HAL_DAC_MspInit;
1169 hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1170 break;
1171 default :
1172 /* Update the error code */
1173 hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1174 /* update return status */
1175 status = HAL_ERROR;
1176 break;
1177 }
1178 }
1179 else if (hdac->State == HAL_DAC_STATE_RESET)
1180 {
1181 switch (CallbackID)
1182 {
1183 case HAL_DAC_MSP_INIT_CB_ID :
1184 hdac->MspInitCallback = HAL_DAC_MspInit;
1185 break;
1186 case HAL_DAC_MSP_DEINIT_CB_ID :
1187 hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1188 break;
1189 default :
1190 /* Update the error code */
1191 hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1192 /* update return status */
1193 status = HAL_ERROR;
1194 break;
1195 }
1196 }
1197 else
1198 {
1199 /* Update the error code */
1200 hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1201 /* update return status */
1202 status = HAL_ERROR;
1203 }
1204
1205 /* Release Lock */
1206 __HAL_UNLOCK(hdac);
1207 return status;
1208 }
1209 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1210
1211 /**
1212 * @}
1213 */
1214
1215 /**
1216 * @}
1217 */
1218
1219 /** @addtogroup DAC_Private_Functions
1220 * @{
1221 */
1222
1223 /**
1224 * @brief DMA conversion complete callback.
1225 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1226 * the configuration information for the specified DMA module.
1227 * @retval None
1228 */
DAC_DMAConvCpltCh1(DMA_HandleTypeDef * hdma)1229 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
1230 {
1231 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1232
1233 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1234 hdac->ConvCpltCallbackCh1(hdac);
1235 #else
1236 HAL_DAC_ConvCpltCallbackCh1(hdac);
1237 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1238
1239 hdac->State= HAL_DAC_STATE_READY;
1240 }
1241
1242 /**
1243 * @brief DMA half transfer complete callback.
1244 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1245 * the configuration information for the specified DMA module.
1246 * @retval None
1247 */
DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef * hdma)1248 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
1249 {
1250 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1251 /* Conversion complete callback */
1252 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1253 hdac->ConvHalfCpltCallbackCh1(hdac);
1254 #else
1255 HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
1256 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1257 }
1258
1259 /**
1260 * @brief DMA error callback
1261 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1262 * the configuration information for the specified DMA module.
1263 * @retval None
1264 */
DAC_DMAErrorCh1(DMA_HandleTypeDef * hdma)1265 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
1266 {
1267 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1268
1269 /* Set DAC error code to DMA error */
1270 hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
1271
1272 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1273 hdac->ErrorCallbackCh1(hdac);
1274 #else
1275 HAL_DAC_ErrorCallbackCh1(hdac);
1276 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1277
1278 hdac->State= HAL_DAC_STATE_READY;
1279 }
1280
1281 /**
1282 * @}
1283 */
1284 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
1285 STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
1286 STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx ||\
1287 STM32F413xx || STM32F423xx */
1288 #endif /* HAL_DAC_MODULE_ENABLED */
1289
1290 /**
1291 * @}
1292 */
1293
1294 /**
1295 * @}
1296 */
1297
1298 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1299