2 ******************************************************************************
3 * @file stm32f3xx_hal_dac_ex.c
4 * @author MCD Application Team
7 * @brief Extended DAC HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Power Controller (DAC) peripheral:
11 * + Initialization and de-initialization functions
12 * + Peripheral Control functions
16 ******************************************************************************
19 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
21 * Redistribution and use in source and binary forms, with or without modification,
22 * are permitted provided that the following conditions are met:
23 * 1. Redistributions of source code must retain the above copyright notice,
24 * this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
28 * 3. Neither the name of STMicroelectronics nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
40 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 ******************************************************************************
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32f3xx_hal.h"
49 /** @addtogroup STM32F3xx_HAL_Driver
53 #ifdef HAL_DAC_MODULE_ENABLED
55 /** @defgroup DACEx DAC Extended HAL module driver
56 * @brief DAC HAL module driver
60 /* Private typedef -----------------------------------------------------------*/
61 /* Private define ------------------------------------------------------------*/
62 /* Private macro -------------------------------------------------------------*/
63 /* Private variables ---------------------------------------------------------*/
64 /* Private function prototypes -----------------------------------------------*/
65 /** @defgroup DACEx_Private_Functions DAC Extended Private Functions
68 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
69 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
70 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma);
72 #if defined(STM32F303xE) || defined(STM32F398xx) || \
73 defined(STM32F303xC) || defined(STM32F358xx) || \
74 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
75 defined(STM32F373xC) || defined(STM32F378xx)
76 /* DAC channel 2 is available on top of DAC channel 1 */
77 static void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma);
78 static void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma);
79 static void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma);
80 #endif /* STM32F303xE || STM32F398xx || */
81 /* STM32F303xC || STM32F358xx || */
82 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
83 /* STM32F373xC || STM32F378xx */
92 /** @addtogroup DAC DAC HAL module driver
93 * @brief DAC HAL module driver
97 /* Exported functions ---------------------------------------------------------*/
98 /** @addtogroup DAC_Exported_Functions DAC Exported Functions
102 /** @addtogroup DAC_Exported_Functions_Group3 Peripheral Control functions
103 * @brief Peripheral Control functions
106 ==============================================================================
107 ##### Peripheral Control functions #####
108 ==============================================================================
109 [..] This section provides functions allowing to:
110 (+) Set the specified data holding register value for DAC channel.
117 * @brief Set the specified data holding register value for DAC channel.
118 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
119 * the configuration information for the specified DAC.
120 * @param channel: The selected DAC channel.
121 * @param alignment: Specifies the data alignment for DAC channel.
122 * This parameter can be one of the following values:
123 * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
124 * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
125 * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
126 * @param data: Data to be loaded in the selected data holding register.
129 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t alignment, uint32_t data)
131 __IO uint32_t tmp = 0;
133 /* Check the parameters */
134 assert_param(IS_DAC_CHANNEL(channel));
135 assert_param(IS_DAC_ALIGN(alignment));
136 assert_param(IS_DAC_DATA(data));
138 tmp = (uint32_t) (hdac->Instance);
140 /* DAC 1 has 1 or 2 channels - no DAC2 */
141 /* DAC 1 has 2 channels 1 & 2 - DAC 2 has one channel 1 */
143 if(channel == DAC_CHANNEL_1)
145 tmp += __HAL_DHR12R1_ALIGNEMENT(alignment);
147 #if defined(STM32F303xE) || defined(STM32F398xx) || \
148 defined(STM32F303xC) || defined(STM32F358xx) || \
149 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
150 defined(STM32F373xC) || defined(STM32F378xx)
151 else /* channel = DAC_CHANNEL_2 */
153 tmp += __HAL_DHR12R2_ALIGNEMENT(alignment);
155 #endif /* STM32F303xE || STM32F398xx || */
156 /* STM32F303xC || STM32F358xx || */
157 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
158 /* STM32F373xC || STM32F378xx */
160 /* Set the DAC channel1 selected data holding register */
161 *(__IO uint32_t *) tmp = data;
163 /* Return function status */
171 /** @addtogroup DAC_Exported_Functions_Group2 Input and Output operation functions
172 * @brief IO operation functions
175 ==============================================================================
176 ##### IO operation functions #####
177 ==============================================================================
178 [..] This section provides functions allowing to:
179 (+) Start conversion.
181 (+) Start conversion and enable DMA transfer.
182 (+) Stop conversion and disable DMA transfer.
183 (+) Get result of conversion.
189 /* DAC 1 has 2 channels 1 & 2 - DAC 2 has one channel 1 */
190 #if defined(STM32F303xE) || defined(STM32F398xx) || \
191 defined(STM32F303xC) || defined(STM32F358xx) || \
192 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
193 defined(STM32F373xC) || defined(STM32F378xx)
194 /* DAC 1 has 2 channels 1 & 2 */
197 * @brief Enables DAC and starts conversion of channel.
198 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
199 * the configuration information for the specified DAC.
200 * @param channel: The selected DAC channel.
201 * This parameter can be one of the following values:
202 * @arg DAC_CHANNEL_1: DAC1 Channel1 or DAC2 Channel1 selected
203 * @arg DAC_CHANNEL_2: DAC1 Channel2 selected
207 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t channel)
209 /* Check the parameters */
210 assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
215 /* Change DAC state */
216 hdac->State = HAL_DAC_STATE_BUSY;
218 /* Enable the Peripharal */
219 __HAL_DAC_ENABLE(hdac, channel);
221 if(channel == DAC_CHANNEL_1)
223 /* Check if software trigger enabled */
224 if(((hdac->Instance->CR & DAC_CR_TEN1) == DAC_CR_TEN1) && ((hdac->Instance->CR & DAC_CR_TSEL1) == DAC_CR_TSEL1))
226 /* Enable the selected DAC software conversion */
227 hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
232 /* Check if software trigger enabled */
233 if(((hdac->Instance->CR & DAC_CR_TEN2) == DAC_CR_TEN2) && ((hdac->Instance->CR & DAC_CR_TSEL2) == DAC_CR_TSEL2))
235 /* Enable the selected DAC software conversion*/
236 hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
240 /* Change DAC state */
241 hdac->State = HAL_DAC_STATE_READY;
243 /* Process unlocked */
246 /* Return function status */
249 #endif /* STM32F303xE || STM32F398xx || */
250 /* STM32F303xC || STM32F358xx || */
251 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
252 /* STM32F373xC || STM32F378xx */
254 #if defined(STM32F302xE) || \
255 defined(STM32F302xC) || \
256 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
257 /* DAC 1 has 1 channels 1 */
260 * @brief Enables DAC and starts conversion of channel.
261 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
262 * the configuration information for the specified DAC.
263 * @param channel: The selected DAC channel.
264 * This parameter can be one of the following values:
265 * @arg DAC_CHANNEL_1: DAC1 Channel1 selected
269 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t channel)
271 /* Check the parameters */
272 assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
277 /* Change DAC state */
278 hdac->State = HAL_DAC_STATE_BUSY;
280 /* Enable the Peripharal */
281 __HAL_DAC_ENABLE(hdac, channel);
283 /* Check if software trigger enabled */
284 if(((hdac->Instance->CR & DAC_CR_TEN1) == DAC_CR_TEN1) && ((hdac->Instance->CR & DAC_CR_TSEL1) == DAC_CR_TSEL1))
286 /* Enable the selected DAC software conversion */
287 hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
290 /* Change DAC state */
291 hdac->State = HAL_DAC_STATE_READY;
293 /* Process unlocked */
296 /* Return function status */
299 #endif /* STM32F302xE || */
301 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
303 /* DAC 1 has 2 channels 1 & 2 - DAC 2 has one channel 1 */
304 #if defined(STM32F303xE) || defined(STM32F398xx) || \
305 defined(STM32F303xC) || defined(STM32F358xx) || \
306 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
307 defined(STM32F373xC) || defined(STM32F378xx)
309 /* DAC 1 has 2 channels 1 & 2 */
312 * @brief Enables DAC and starts conversion of channel.
313 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
314 * the configuration information for the specified DAC.
315 * @param channel: The selected DAC channel.
316 * This parameter can be one of the following values:
317 * @arg DAC_CHANNEL_1: DAC1 Channel1 selected
318 * @arg DAC_CHANNEL_2: DAC1 Channel2 selected
319 * @param pData: The destination peripheral Buffer address.
320 * @param Length: The length of data to be transferred from memory to DAC peripheral
321 * @param alignment: Specifies the data alignment for DAC channel.
322 * This parameter can be one of the following values:
323 * @arg DAC_Align_8b_R: 8bit right data alignment selected
324 * @arg DAC_Align_12b_L: 12bit left data alignment selected
325 * @arg DAC_Align_12b_R: 12bit right data alignment selected
329 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t* pData, uint32_t Length, uint32_t alignment)
333 /* Check the parameters */
334 assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
335 assert_param(IS_DAC_ALIGN(alignment));
340 /* Change DAC state */
341 hdac->State = HAL_DAC_STATE_BUSY;
343 if(channel == DAC_CHANNEL_1)
345 /* Set the DMA transfer complete callback for channel1 */
346 hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
348 /* Set the DMA half transfer complete callback for channel1 */
349 hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
351 /* Set the DMA error callback for channel1 */
352 hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
356 /* Set the DMA transfer complete callback for channel2 */
357 hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
359 /* Set the DMA half transfer complete callback for channel2 */
360 hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
362 /* Set the DMA error callback for channel2 */
363 hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
366 if(channel == DAC_CHANNEL_1)
368 /* Enable the selected DAC channel1 DMA request */
369 hdac->Instance->CR |= DAC_CR_DMAEN1;
371 /* Case of use of channel 1 */
374 case DAC_ALIGN_12B_R:
375 /* Get DHR12R1 address */
376 tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
378 case DAC_ALIGN_12B_L:
379 /* Get DHR12L1 address */
380 tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
383 /* Get DHR8R1 address */
384 tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
392 /* Enable the selected DAC channel2 DMA request */
393 hdac->Instance->CR |= DAC_CR_DMAEN2;
395 /* Case of use of channel 2 */
398 case DAC_ALIGN_12B_R:
399 /* Get DHR12R2 address */
400 tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
402 case DAC_ALIGN_12B_L:
403 /* Get DHR12L2 address */
404 tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
407 /* Get DHR8R2 address */
408 tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
415 /* Enable the DMA Channel */
416 if(channel == DAC_CHANNEL_1)
418 /* Enable the DAC DMA underrun interrupt */
419 __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
421 /* Enable the DMA Channel */
422 HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
426 /* Enable the DAC DMA underrun interrupt */
427 __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
429 /* Enable the DMA Channel */
430 HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
433 /* Enable the Peripheral */
434 __HAL_DAC_ENABLE(hdac, channel);
436 /* Process Unlocked */
439 /* Return function status */
442 #endif /* STM32F303xE || STM32F398xx || */
443 /* STM32F303xC || STM32F358xx || */
444 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
445 /* STM32F373xC || STM32F378xx */
447 #if defined(STM32F302xE) || \
448 defined(STM32F302xC) || \
449 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
450 /* DAC 1 has 1 channels 1 */
453 * @brief Enables DAC and starts conversion of channel.
454 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
455 * the configuration information for the specified DAC.
456 * @param channel: The selected DAC channel.
457 * This parameter can be one of the following values:
458 * @arg DAC_CHANNEL_1: DAC1 Channel1 selected
459 * @param pData: The destination peripheral Buffer address.
460 * @param Length: The length of data to be transferred from memory to DAC peripheral
461 * @param alignment: Specifies the data alignment for DAC channel.
462 * This parameter can be one of the following values:
463 * @arg DAC_Align_8b_R: 8bit right data alignment selected
464 * @arg DAC_Align_12b_L: 12bit left data alignment selected
465 * @arg DAC_Align_12b_R: 12bit right data alignment selected
469 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t* pData, uint32_t Length, uint32_t alignment)
473 /* Check the parameters */
474 assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
475 assert_param(IS_DAC_ALIGN(alignment));
480 /* Change DAC state */
481 hdac->State = HAL_DAC_STATE_BUSY;
483 /* Set the DMA transfer complete callback for channel1 */
484 hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
486 /* Set the DMA half transfer complete callback for channel1 */
487 hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
489 /* Set the DMA error callback for channel1 */
490 hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
492 /* Enable the selected DAC channel1 DMA request */
493 hdac->Instance->CR |= DAC_CR_DMAEN1;
495 /* Case of use of channel 1 */
498 case DAC_ALIGN_12B_R:
499 /* Get DHR12R1 address */
500 tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
502 case DAC_ALIGN_12B_L:
503 /* Get DHR12L1 address */
504 tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
507 /* Get DHR8R1 address */
508 tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
514 /* Enable the DMA Channel */
515 /* Enable the DAC DMA underrun interrupt */
516 __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
518 /* Enable the DMA Channel */
519 HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
521 /* Enable the Peripheral */
522 __HAL_DAC_ENABLE(hdac, channel);
524 /* Process Unlocked */
527 /* Return function status */
530 #endif /* STM32F302xE || */
532 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
534 /* DAC 1 has 2 channels 1 & 2 - DAC 2 has one channel 1 */
535 #if defined(STM32F303xE) || defined(STM32F398xx) || \
536 defined(STM32F303xC) || defined(STM32F358xx) || \
537 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
538 defined(STM32F373xC) || defined(STM32F378xx)
540 /* DAC 1 has 2 channels 1 & 2 */
543 * @brief Returns the last data output value of the selected DAC channel.
544 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
545 * the configuration information for the specified DAC.
546 * @param channel: The selected DAC channel.
547 * This parameter can be one of the following values:
548 * @arg DAC_CHANNEL_1: DAC1 Channel1 or DAC2 Channel1 selected
549 * @arg DAC_CHANNEL_2: DAC1 Channel2 selected
550 * @retval The selected DAC channel data output value.
555 * @brief Returns the last data output value of the selected DAC channel.
556 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
557 * the configuration information for the specified DAC.
558 * @param channel: The selected DAC channel.
559 * This parameter can be one of the following values:
560 * @arg DAC_CHANNEL_1: DAC1 Channel1 selected
561 * @arg DAC_CHANNEL_2: DAC1 Channel2 selected
562 * @retval The selected DAC channel data output value.
564 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t channel)
566 /* Check the parameters */
567 assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
569 /* Returns the DAC channel data output register value */
570 if(channel == DAC_CHANNEL_1)
572 return hdac->Instance->DOR1;
574 else /* channel = DAC_CHANNEL_2 */
576 return hdac->Instance->DOR2;
580 #endif /* STM32F303xE || STM32F398xx || */
581 /* STM32F303xC || STM32F358xx || */
582 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
583 /* STM32F373xC || STM32F378xx */
585 #if defined(STM32F302xE) || \
586 defined(STM32F302xC) || \
587 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
588 /* DAC 1 has 1 channel (channel 1) */
590 * @brief Returns the last data output value of the selected DAC channel.
591 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
592 * the configuration information for the specified DAC.
593 * @param channel: The selected DAC channel.
594 * This parameter can be one of the following values:
595 * @arg DAC_CHANNEL_1: DAC1 Channel1 selected
596 * @retval The selected DAC channel data output value.
598 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t channel)
600 /* Check the parameters */
601 assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
603 /* Returns the DAC channel data output register value */
604 return hdac->Instance->DOR1;
606 #endif /* STM32F302xE || */
608 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
610 #if defined(STM32F302xE) || \
611 defined(STM32F302xC) || \
612 defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
613 /* DAC channel 2 is NOT available. Only DAC channel 1 is available */
616 * @brief Handles DAC interrupt request
617 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
618 * the configuration information for the specified DAC.
622 void HAL_DAC_IRQHandler(struct __DAC_HandleTypeDef* hdac)
624 /* Check Overrun flag */
625 if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
627 /* Change DAC state to error state */
628 hdac->State = HAL_DAC_STATE_ERROR;
630 /* Set DAC error code to chanel1 DMA underrun error */
631 hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
633 /* Clear the underrun flag */
634 __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
636 /* Disable the selected DAC channel1 DMA request */
637 hdac->Instance->CR &= ~DAC_CR_DMAEN1;
640 HAL_DAC_DMAUnderrunCallbackCh1(hdac);
643 #endif /* STM32F302xE || */
645 /* STM32F301x8 || STM32F302x8 || STM32F318xx */
647 #if defined(STM32F303xE) || defined(STM32F398xx) || \
648 defined(STM32F303xC) || defined(STM32F358xx) || \
649 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
650 defined(STM32F373xC) || defined(STM32F378xx)
651 /* DAC channel 2 is available on top of DAC channel 1 */
654 * @brief Handles DAC interrupt request
655 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
656 * the configuration information for the specified DAC.
659 void HAL_DAC_IRQHandler(struct __DAC_HandleTypeDef* hdac)
661 /* Check Overrun flag */
662 if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
664 /* Change DAC state to error state */
665 hdac->State = HAL_DAC_STATE_ERROR;
667 /* Set DAC error code to chanel1 DMA underrun error */
668 hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
670 /* Clear the underrun flag */
671 __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
673 /* Disable the selected DAC channel1 DMA request */
674 hdac->Instance->CR &= ~DAC_CR_DMAEN1;
677 HAL_DAC_DMAUnderrunCallbackCh1(hdac);
681 if (__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
683 /* Change DAC state to error state */
684 hdac->State = HAL_DAC_STATE_ERROR;
686 /* Set DAC error code to channel2 DMA underrun error */
687 hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH2;
689 /* Clear the underrun flag */
690 __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
692 /* Disable the selected DAC channel1 DMA request */
693 hdac->Instance->CR &= ~DAC_CR_DMAEN2;
696 HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
700 #endif /* STM32F303xE || STM32F398xx || */
701 /* STM32F303xC || STM32F358xx || */
702 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
703 /* STM32F373xC || STM32F378xx */
717 /** @addtogroup DACEx
718 * @brief DACEx Extended HAL module driver
721 /* Exported functions ---------------------------------------------------------*/
723 /** @addtogroup DACEx_Exported_Functions DAC Extended Exported Functions
727 #if defined(STM32F303xE) || defined(STM32F398xx) || \
728 defined(STM32F303xC) || defined(STM32F358xx) || \
729 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
730 defined(STM32F373xC) || defined(STM32F378xx)
731 /* DAC channel 2 is present in DAC 1 */
733 * @brief Set the specified data holding register value for dual DAC channel.
734 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
735 * the configuration information for the specified DAC.
736 * @param alignment: Specifies the data alignment for dual channel DAC.
737 * This parameter can be one of the following values:
738 * @arg DAC_Align_8b_R: 8bit right data alignment selected
739 * @arg DAC_Align_12b_L: 12bit left data alignment selected
740 * @arg DAC_Align_12b_R: 12bit right data alignment selected
741 * @param data2: Data for DAC Channel2 to be loaded in the selected data holding register.
742 * @param data1: Data for DAC Channel1 to be loaded in the selected data holding register.
743 * @note In dual mode, a unique register access is required to write in both
744 * DAC channels at the same time.
747 HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef* hdac, uint32_t alignment, uint32_t data1, uint32_t data2)
749 uint32_t data = 0, tmp = 0;
751 /* Check the parameters */
752 assert_param(IS_DAC_ALIGN(alignment));
753 assert_param(IS_DAC_DATA(data1));
754 assert_param(IS_DAC_DATA(data2));
756 /* Calculate and set dual DAC data holding register value */
757 if (alignment == DAC_ALIGN_8B_R)
759 data = ((uint32_t)data2 << 8) | data1;
763 data = ((uint32_t)data2 << 16) | data1;
766 tmp = (uint32_t) (hdac->Instance);
767 tmp += __HAL_DHR12RD_ALIGNEMENT(alignment);
769 /* Set the dual DAC selected data holding register */
770 *(__IO uint32_t *)tmp = data;
772 /* Return function status */
775 #endif /* STM32F303xE || STM32F398xx || */
776 /* STM32F303xC || STM32F358xx || */
777 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
778 /* STM32F373xC || STM32F378xx */
781 * @brief Returns the last data output value of the selected DAC channel.
782 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
783 * the configuration information for the specified DAC.
784 * @retval The selected DAC channel data output value.
786 uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef* hdac)
790 tmp |= hdac->Instance->DOR1;
792 #if defined(STM32F303xE) || defined(STM32F398xx) || \
793 defined(STM32F303xC) || defined(STM32F358xx) || \
794 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
795 defined(STM32F373xC) || defined(STM32F378xx)
796 /* DAC channel 2 is present in DAC 1 */
797 tmp |= hdac->Instance->DOR2 << 16;
798 #endif /* STM32F303xE || STM32F398xx || */
799 /* STM32F303xC || STM32F358xx || */
800 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
801 /* STM32F373xC || STM32F378xx */
803 /* Returns the DAC channel data output register value */
808 * @brief Enables or disables the selected DAC channel wave generation.
809 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
810 * the configuration information for the specified DAC.
811 * @param channel: The selected DAC channel.
812 * This parameter can be one of the following values:
813 * @arg DAC_CHANNEL_1: DAC1 Channel1 selected
814 * @arg DAC_CHANNEL_2: DAC1 Channel2 selected
815 * @param Amplitude: Select max triangle amplitude.
816 * This parameter can be one of the following values:
817 * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
818 * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
819 * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
820 * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
821 * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
822 * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
823 * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
824 * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
825 * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
826 * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
827 * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
828 * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
829 * @note Wave generation is not available in DAC2.
832 HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t Amplitude)
834 /* Check the parameters */
835 assert_param(IS_DAC_CHANNEL(channel));
836 assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
841 /* Change DAC state */
842 hdac->State = HAL_DAC_STATE_BUSY;
844 /* Enable the selected wave generation for the selected DAC channel */
845 hdac->Instance->CR |= (DAC_WAVE_TRIANGLE | Amplitude) << channel;
847 /* Change DAC state */
848 hdac->State = HAL_DAC_STATE_READY;
850 /* Process unlocked */
853 /* Return function status */
858 * @brief Enables or disables the selected DAC channel wave generation.
859 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
860 * the configuration information for the specified DAC.
861 * @param channel: The selected DAC channel.
862 * This parameter can be one of the following values:
863 * @arg DAC_CHANNEL_1: DAC1 Channel1 selected
864 * @arg DAC_CHANNEL_2: DAC1 Channel2 selected
865 * @param Amplitude: Unmask DAC channel LFSR for noise wave generation.
866 * This parameter can be one of the following values:
867 * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
868 * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
869 * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
870 * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
871 * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
872 * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
873 * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
874 * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
875 * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
876 * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
877 * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
878 * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
881 HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t Amplitude)
883 /* Check the parameters */
884 assert_param(IS_DAC_CHANNEL(channel));
885 assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
890 /* Change DAC state */
891 hdac->State = HAL_DAC_STATE_BUSY;
893 /* Enable the selected wave generation for the selected DAC channel */
894 hdac->Instance->CR |= (DAC_WAVE_NOISE | Amplitude) << channel;
896 /* Change DAC state */
897 hdac->State = HAL_DAC_STATE_READY;
899 /* Process unlocked */
902 /* Return function status */
907 * @brief Conversion complete callback in non blocking mode for Channel2
908 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
909 * the configuration information for the specified DAC.
912 __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef* hdac)
914 /* NOTE : This function Should not be modified, when the callback is needed,
915 the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
920 * @brief Conversion half DMA transfer callback in non blocking mode for Channel2
921 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
922 * the configuration information for the specified DAC.
925 __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef* hdac)
927 /* NOTE : This function Should not be modified, when the callback is needed,
928 the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
933 * @brief Error DAC callback for Channel2.
934 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
935 * the configuration information for the specified DAC.
938 __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
940 /* NOTE : This function Should not be modified, when the callback is needed,
941 the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
946 * @brief DMA underrun DAC callback for channel2.
947 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
948 * the configuration information for the specified DAC.
951 __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
953 /* NOTE : This function Should not be modified, when the callback is needed,
954 the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
959 * @brief DMA conversion complete callback.
960 * @param hdma: pointer to DMA handle.
963 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
965 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
967 HAL_DAC_ConvCpltCallbackCh1(hdac);
969 hdac->State= HAL_DAC_STATE_READY;
973 * @brief DMA half transfer complete callback.
974 * @param hdma: pointer to DMA handle.
977 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
979 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
980 /* Conversion complete callback */
981 HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
985 * @brief DMA error callback
986 * @param hdma: pointer to DMA handle.
989 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
991 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
993 /* Set DAC error code to DMA error */
994 hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
996 HAL_DAC_ErrorCallbackCh1(hdac);
998 hdac->State= HAL_DAC_STATE_READY;
1001 #if defined(STM32F303xE) || defined(STM32F398xx) || \
1002 defined(STM32F303xC) || defined(STM32F358xx) || \
1003 defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
1004 defined(STM32F373xC) || defined(STM32F378xx)
1005 /* DAC channel 2 is available on top of DAC channel 1 */
1008 * @brief DMA conversion complete callback.
1009 * @param hdma: pointer to DMA handle.
1012 static void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
1014 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1016 HAL_DACEx_ConvCpltCallbackCh2(hdac);
1018 hdac->State= HAL_DAC_STATE_READY;
1022 * @brief DMA half transfer complete callback.
1023 * @param hdma: pointer to DMA handle.
1026 static void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
1028 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1029 /* Conversion complete callback */
1030 HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
1034 * @brief DMA error callback
1035 * @param hdma: pointer to DMA handle.
1038 static void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
1040 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1042 /* Set DAC error code to DMA error */
1043 hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
1045 HAL_DACEx_ErrorCallbackCh2(hdac);
1047 hdac->State= HAL_DAC_STATE_READY;
1049 #endif /* STM32F303xE || STM32F398xx || */
1050 /* STM32F303xC || STM32F358xx || */
1051 /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
1052 /* STM32F373xC || STM32F378xx */
1062 #endif /* HAL_DAC_MODULE_ENABLED */
1067 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/