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