2 ******************************************************************************
3 * @file stm32f1xx_hal_dac.c
4 * @author MCD Application Team
7 * @brief DAC HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Digital to Analog Converter (DAC) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State and Errors functions
17 ==============================================================================
18 ##### DAC Peripheral features #####
19 ==============================================================================
24 The device integrates two 12-bit Digital Analog Converters that can
25 be used independently or simultaneously (dual mode):
26 (#) DAC channel1 with DAC_OUT1 (PA4) as output
27 (#) DAC channel2 with DAC_OUT2 (PA5) as output
32 Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
33 and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
35 Digital to Analog conversion can be triggered by:
36 (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
37 The used pin (GPIOx_PIN_9) must be configured in input mode.
39 (#) Timers TRGO: TIM2, TIM4, TIM6, TIM7
40 For STM32F10x connectivity line devices and STM32F100x devices: TIM3
41 For STM32F10x high-density and XL-density devices: TIM8
42 For STM32F100x high-density value line devices: TIM15 as
44 (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
46 (#) Software using DAC_TRIGGER_SOFTWARE
48 *** DAC Buffer mode feature ***
49 ===============================
51 Each DAC channel integrates an output buffer that can be used to
52 reduce the output impedance, and to drive external loads directly
53 without having to add an external operational amplifier.
54 To enable, the output buffer use
55 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
57 (@) Refer to the device datasheet for more details about output
58 impedance value with and without output buffer.
60 *** DAC connect feature ***
61 ===============================
63 Each DAC channel can be connected internally.
65 sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
67 *** GPIO configurations guidelines ***
70 When a DAC channel is used (ex channel1 on PA4) and the other is not
71 (ex channel1 on PA5 is configured in Analog and disabled).
72 Channel1 may disturb channel2 as coupling effect.
73 Note that there is no coupling on channel2 as soon as channel2 is turned on.
74 Coupling on adjacent channel could be avoided as follows:
75 when unused PA5 is configured as INPUT PULL-UP or DOWN.
76 PA5 is configured in ANALOG just before it is turned on.
78 *** DAC wave generation feature ***
79 ===================================
81 Both DAC channels can be used to generate
82 (#) Noise wave using HAL_DACEx_NoiseWaveGenerate()
83 (#) Triangle wave using HAL_DACEx_TriangleWaveGenerate()
85 *** DAC data format ***
86 =======================
88 The DAC data format can be:
89 (#) 8-bit right alignment using DAC_ALIGN_8B_R
90 (#) 12-bit left alignment using DAC_ALIGN_12B_L
91 (#) 12-bit right alignment using DAC_ALIGN_12B_R
93 *** DAC data value to voltage correspondance ***
94 ================================================
96 The analog output voltage on each DAC channel pin is determined
97 by the following equation:
99 DAC_OUTx = VREF+ * DOR / 4095
100 (+) with DOR is the Data Output Register
102 VEF+ is the input voltage reference (refer to the device datasheet)
104 e.g. To set DAC_OUT1 to 0.7V, use
105 (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
108 =====================
110 A DMA1 request can be generated when an external trigger (but not
111 a software trigger) occurs if DMA1 requests are enabled using
114 DMA requests are mapped as following:
116 For STM32F100x low-density, medium-density, high-density with DAC
118 mapped on DMA1 channel3 which must be
120 For STM32F100x high-density without DAC DMA remap and other
122 mapped on DMA2 channel3 which must be
125 For STM32F100x low-density, medium-density, high-density with DAC
127 mapped on DMA1 channel4 which must be
129 For STM32F100x high-density without DAC DMA remap and other
131 mapped on DMA2 channel4 which must be
134 ##### How to use this driver #####
135 ==============================================================================
137 (+) DAC APB clock must be enabled to get write access to DAC
138 registers using HAL_DAC_Init()
139 (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
140 (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
141 (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
143 *** Polling mode IO operation ***
144 =================================
146 (+) Start the DAC peripheral using HAL_DAC_Start()
147 (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
148 (+) Stop the DAC peripheral using HAL_DAC_Stop()
150 *** DMA mode IO operation ***
151 ==============================
153 (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
154 of data to be transferred at each end of conversion
155 (+) At the middle of data transfer HAL_DACEx_ConvHalfCpltCallbackCh1()or HAL_DACEx_ConvHalfCpltCallbackCh2()
156 function is executed and user can add his own code by customization of function pointer
157 HAL_DAC_ConvHalfCpltCallbackCh1 or HAL_DAC_ConvHalfCpltCallbackCh2
158 (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
159 function is executed and user can add his own code by customization of function pointer
160 HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
161 (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() or HAL_DACEx_ErrorCallbackCh2() function is executed and user can
162 add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1 or HAL_DACEx_ErrorCallbackCh2
163 (+) For STM32F100x devices with specific feature: DMA underrun.
164 In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
165 HAL_DAC_DMAUnderrunCallbackCh1()or HAL_DACEx_DMAUnderrunCallbackCh2()
166 function is executed and user can add his own code by customization of function pointer
167 HAL_DAC_DMAUnderrunCallbackCh1 or HAL_DACEx_DMAUnderrunCallbackCh2
168 add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
169 (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
171 *** DAC HAL driver macros list ***
172 =============================================
174 Below the list of most used macros in DAC HAL driver.
176 (+) __HAL_DAC_ENABLE : Enable the DAC peripheral (For STM32F100x devices with specific feature: DMA underrun)
177 (+) __HAL_DAC_DISABLE : Disable the DAC peripheral (For STM32F100x devices with specific feature: DMA underrun)
178 (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags (For STM32F100x devices with specific feature: DMA underrun)
179 (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status (For STM32F100x devices with specific feature: DMA underrun)
182 (@) You can refer to the DAC HAL driver header file for more useful macros
185 ******************************************************************************
188 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
190 * Redistribution and use in source and binary forms, with or without modification,
191 * are permitted provided that the following conditions are met:
192 * 1. Redistributions of source code must retain the above copyright notice,
193 * this list of conditions and the following disclaimer.
194 * 2. Redistributions in binary form must reproduce the above copyright notice,
195 * this list of conditions and the following disclaimer in the documentation
196 * and/or other materials provided with the distribution.
197 * 3. Neither the name of STMicroelectronics nor the names of its contributors
198 * may be used to endorse or promote products derived from this software
199 * without specific prior written permission.
201 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
202 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
203 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
204 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
205 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
206 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
207 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
208 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
209 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
210 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
212 ******************************************************************************
216 /* Includes ------------------------------------------------------------------*/
217 #include "stm32f1xx_hal.h"
219 /** @addtogroup STM32F1xx_HAL_Driver
223 /** @defgroup DAC DAC
224 * @brief DAC driver modules
228 #ifdef HAL_DAC_MODULE_ENABLED
229 #if defined (STM32F100xB) || defined (STM32F100xE) || defined (STM32F101xE) || defined (STM32F101xG) || defined (STM32F103xE) || defined (STM32F103xG) || defined (STM32F105xC) || defined (STM32F107xC)
231 /* Private typedef -----------------------------------------------------------*/
232 /* Private define ------------------------------------------------------------*/
233 /* Private macro -------------------------------------------------------------*/
234 /* Private variables ---------------------------------------------------------*/
235 /* Private function prototypes -----------------------------------------------*/
236 /* Exported functions -------------------------------------------------------*/
238 /** @defgroup DAC_Exported_Functions DAC Exported Functions
242 /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
243 * @brief Initialization and Configuration functions
246 ==============================================================================
247 ##### Initialization and de-initialization functions #####
248 ==============================================================================
249 [..] This section provides functions allowing to:
250 (+) Initialize and configure the DAC.
251 (+) De-initialize the DAC.
258 * @brief Initializes the DAC peripheral according to the specified parameters
259 * in the DAC_InitStruct.
260 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
261 * the configuration information for the specified DAC.
264 HAL_StatusTypeDef
HAL_DAC_Init(DAC_HandleTypeDef
* hdac
)
266 /* Check DAC handle */
271 /* Check the parameters */
272 assert_param(IS_DAC_ALL_INSTANCE(hdac
->Instance
));
274 if(hdac
->State
== HAL_DAC_STATE_RESET
)
276 /* Allocate lock resource and initialize it */
277 hdac
->Lock
= HAL_UNLOCKED
;
279 /* Init the low level hardware */
280 HAL_DAC_MspInit(hdac
);
283 /* Initialize the DAC state*/
284 hdac
->State
= HAL_DAC_STATE_BUSY
;
286 /* Set DAC error code to none */
287 hdac
->ErrorCode
= HAL_DAC_ERROR_NONE
;
289 /* Initialize the DAC state*/
290 hdac
->State
= HAL_DAC_STATE_READY
;
292 /* Return function status */
297 * @brief Deinitializes the DAC peripheral registers to their default reset values.
298 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
299 * the configuration information for the specified DAC.
302 HAL_StatusTypeDef
HAL_DAC_DeInit(DAC_HandleTypeDef
* hdac
)
304 /* Check DAC handle */
310 /* Check the parameters */
311 assert_param(IS_DAC_ALL_INSTANCE(hdac
->Instance
));
313 /* Change DAC state */
314 hdac
->State
= HAL_DAC_STATE_BUSY
;
316 /* DeInit the low level hardware */
317 HAL_DAC_MspDeInit(hdac
);
319 /* Set DAC error code to none */
320 hdac
->ErrorCode
= HAL_DAC_ERROR_NONE
;
322 /* Change DAC state */
323 hdac
->State
= HAL_DAC_STATE_RESET
;
328 /* Return function status */
333 * @brief Initializes the DAC MSP.
334 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
335 * the configuration information for the specified DAC.
338 __weak
void HAL_DAC_MspInit(DAC_HandleTypeDef
* hdac
)
340 /* Prevent unused argument(s) compilation warning */
342 /* NOTE : This function Should not be modified, when the callback is needed,
343 the HAL_DAC_MspInit could be implemented in the user file
348 * @brief DeInitializes the DAC MSP.
349 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
350 * the configuration information for the specified DAC.
353 __weak
void HAL_DAC_MspDeInit(DAC_HandleTypeDef
* hdac
)
355 /* Prevent unused argument(s) compilation warning */
357 /* NOTE : This function Should not be modified, when the callback is needed,
358 the HAL_DAC_MspDeInit could be implemented in the user file
366 /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
367 * @brief IO operation functions
370 ==============================================================================
371 ##### IO operation functions #####
372 ==============================================================================
373 [..] This section provides functions allowing to:
374 (+) Start conversion.
376 (+) Start conversion and enable DMA transfer.
377 (+) Stop conversion and disable DMA transfer.
378 (+) Get result of conversion.
385 * @brief Enables DAC and starts conversion of channel.
386 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
387 * the configuration information for the specified DAC.
388 * @param Channel: The selected DAC channel.
389 * This parameter can be one of the following values:
390 * @arg DAC_CHANNEL_1: DAC Channel1 selected
391 * @arg DAC_CHANNEL_2: DAC Channel2 selected
394 HAL_StatusTypeDef
HAL_DAC_Start(DAC_HandleTypeDef
* hdac
, uint32_t Channel
)
396 /* Check the parameters */
397 assert_param(IS_DAC_CHANNEL(Channel
));
402 /* Change DAC state */
403 hdac
->State
= HAL_DAC_STATE_BUSY
;
405 /* Enable the Peripharal */
406 __HAL_DAC_ENABLE(hdac
, Channel
);
408 if(Channel
== DAC_CHANNEL_1
)
410 /* Check if software trigger enabled */
411 if((hdac
->Instance
->CR
& (DAC_CR_TEN1
| DAC_CR_TSEL1
)) == (DAC_CR_TEN1
| DAC_CR_TSEL1
))
413 /* Enable the selected DAC software conversion */
414 SET_BIT(hdac
->Instance
->SWTRIGR
, DAC_SWTRIGR_SWTRIG1
);
419 /* Check if software trigger enabled */
420 if((hdac
->Instance
->CR
& (DAC_CR_TEN2
| DAC_CR_TSEL2
)) == (DAC_CR_TEN2
| DAC_CR_TSEL2
))
422 /* Enable the selected DAC software conversion*/
423 SET_BIT(hdac
->Instance
->SWTRIGR
, DAC_SWTRIGR_SWTRIG2
);
427 /* Change DAC state */
428 hdac
->State
= HAL_DAC_STATE_READY
;
430 /* Process unlocked */
433 /* Return function status */
438 * @brief Disables DAC and stop conversion of channel.
439 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
440 * the configuration information for the specified DAC.
441 * @param Channel: The selected DAC channel.
442 * This parameter can be one of the following values:
443 * @arg DAC_CHANNEL_1: DAC Channel1 selected
444 * @arg DAC_CHANNEL_2: DAC Channel2 selected
447 HAL_StatusTypeDef
HAL_DAC_Stop(DAC_HandleTypeDef
* hdac
, uint32_t Channel
)
449 /* Check the parameters */
450 assert_param(IS_DAC_CHANNEL(Channel
));
452 /* Disable the Peripheral */
453 __HAL_DAC_DISABLE(hdac
, Channel
);
455 /* Change DAC state */
456 hdac
->State
= HAL_DAC_STATE_READY
;
458 /* Return function status */
463 * @brief Enables DAC and starts conversion of channel.
464 * Note: For STM32F100x devices with specific feature: DMA underrun.
465 * On these devices, this function enables the interruption of DMA
467 * (refer to redefinition of this function in DAC extended file)
468 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
469 * the configuration information for the specified DAC.
470 * @param Channel: The selected DAC channel.
471 * This parameter can be one of the following values:
472 * @arg DAC_CHANNEL_1: DAC Channel1 selected
473 * @arg DAC_CHANNEL_2: DAC Channel2 selected
474 * @param pData: The Source memory Buffer address.
475 * @param Length: The length of data to be transferred from memory to DAC peripheral
476 * @param Alignment: Specifies the data alignment for DAC channel.
477 * This parameter can be one of the following values:
478 * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
479 * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
480 * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
483 __weak HAL_StatusTypeDef
HAL_DAC_Start_DMA(DAC_HandleTypeDef
* hdac
, uint32_t Channel
, uint32_t* pData
, uint32_t Length
, uint32_t Alignment
)
485 uint32_t tmpreg
= 0U;
487 /* Check the parameters */
488 assert_param(IS_DAC_CHANNEL(Channel
));
489 assert_param(IS_DAC_ALIGN(Alignment
));
494 /* Change DAC state */
495 hdac
->State
= HAL_DAC_STATE_BUSY
;
497 if(Channel
== DAC_CHANNEL_1
)
499 /* Set the DMA transfer complete callback for channel1 */
500 hdac
->DMA_Handle1
->XferCpltCallback
= DAC_DMAConvCpltCh1
;
502 /* Set the DMA half transfer complete callback for channel1 */
503 hdac
->DMA_Handle1
->XferHalfCpltCallback
= DAC_DMAHalfConvCpltCh1
;
505 /* Set the DMA error callback for channel1 */
506 hdac
->DMA_Handle1
->XferErrorCallback
= DAC_DMAErrorCh1
;
508 /* Enable the selected DAC channel1 DMA request */
509 SET_BIT(hdac
->Instance
->CR
, DAC_CR_DMAEN1
);
511 /* Case of use of channel 1 */
514 case DAC_ALIGN_12B_R
:
515 /* Get DHR12R1 address */
516 tmpreg
= (uint32_t)&hdac
->Instance
->DHR12R1
;
518 case DAC_ALIGN_12B_L
:
519 /* Get DHR12L1 address */
520 tmpreg
= (uint32_t)&hdac
->Instance
->DHR12L1
;
523 /* Get DHR8R1 address */
524 tmpreg
= (uint32_t)&hdac
->Instance
->DHR8R1
;
532 /* Set the DMA transfer complete callback for channel2 */
533 hdac
->DMA_Handle2
->XferCpltCallback
= DAC_DMAConvCpltCh2
;
535 /* Set the DMA half transfer complete callback for channel2 */
536 hdac
->DMA_Handle2
->XferHalfCpltCallback
= DAC_DMAHalfConvCpltCh2
;
538 /* Set the DMA error callback for channel2 */
539 hdac
->DMA_Handle2
->XferErrorCallback
= DAC_DMAErrorCh2
;
541 /* Enable the selected DAC channel2 DMA request */
542 SET_BIT(hdac
->Instance
->CR
, DAC_CR_DMAEN2
);
544 /* Case of use of channel 2 */
547 case DAC_ALIGN_12B_R
:
548 /* Get DHR12R2 address */
549 tmpreg
= (uint32_t)&hdac
->Instance
->DHR12R2
;
551 case DAC_ALIGN_12B_L
:
552 /* Get DHR12L2 address */
553 tmpreg
= (uint32_t)&hdac
->Instance
->DHR12L2
;
556 /* Get DHR8R2 address */
557 tmpreg
= (uint32_t)&hdac
->Instance
->DHR8R2
;
564 /* Enable the DMA channel */
565 if(Channel
== DAC_CHANNEL_1
)
567 /* Enable the DMA channel */
568 HAL_DMA_Start_IT(hdac
->DMA_Handle1
, (uint32_t)pData
, tmpreg
, Length
);
572 /* Enable the DMA channel */
573 HAL_DMA_Start_IT(hdac
->DMA_Handle2
, (uint32_t)pData
, tmpreg
, Length
);
576 /* Process Unlocked */
579 /* Enable the Peripharal */
580 __HAL_DAC_ENABLE(hdac
, Channel
);
582 /* Return function status */
587 * @brief Disables DAC and stop conversion of channel.
588 * Note: For STM32F100x devices with specific feature: DMA underrun.
589 * On these devices, this function disables the interruption of DMA
591 * (refer to redefinition of this function in DAC extended file)
592 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
593 * the configuration information for the specified DAC.
594 * @param Channel: The selected DAC channel.
595 * This parameter can be one of the following values:
596 * @arg DAC_CHANNEL_1: DAC Channel1 selected
597 * @arg DAC_CHANNEL_2: DAC Channel2 selected
600 __weak HAL_StatusTypeDef
HAL_DAC_Stop_DMA(DAC_HandleTypeDef
* hdac
, uint32_t Channel
)
602 HAL_StatusTypeDef status
= HAL_OK
;
604 /* Check the parameters */
605 assert_param(IS_DAC_CHANNEL(Channel
));
607 /* Disable the selected DAC channel DMA request */
608 CLEAR_BIT(hdac
->Instance
->CR
, DAC_CR_DMAEN1
<< Channel
);
610 /* Disable the Peripharal */
611 __HAL_DAC_DISABLE(hdac
, Channel
);
613 /* Disable the DMA Channel */
614 /* Channel1 is used */
615 if (Channel
== DAC_CHANNEL_1
)
617 status
= HAL_DMA_Abort(hdac
->DMA_Handle1
);
619 else /* Channel2 is used for */
621 status
= HAL_DMA_Abort(hdac
->DMA_Handle2
);
624 /* Check if DMA Channel effectively disabled */
625 if (status
!= HAL_OK
)
627 /* Update ADC state machine to error */
628 hdac
->State
= HAL_DAC_STATE_ERROR
;
632 /* Change DAC state */
633 hdac
->State
= HAL_DAC_STATE_READY
;
636 /* Return function status */
641 * @brief Returns the last data output value of the selected DAC channel.
642 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
643 * the configuration information for the specified DAC.
644 * @param Channel: The selected DAC channel.
645 * This parameter can be one of the following values:
646 * @arg DAC_CHANNEL_1: DAC Channel1 selected
647 * @arg DAC_CHANNEL_2: DAC Channel2 selected
648 * @retval The selected DAC channel data output value.
650 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef
* hdac
, uint32_t Channel
)
652 /* Check the parameters */
653 assert_param(IS_DAC_CHANNEL(Channel
));
655 /* Returns the DAC channel data output register value */
656 if(Channel
== DAC_CHANNEL_1
)
658 return hdac
->Instance
->DOR1
;
662 return hdac
->Instance
->DOR2
;
667 * @brief Conversion complete callback in non blocking mode for Channel1
668 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
669 * the configuration information for the specified DAC.
672 __weak
void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef
* hdac
)
674 /* Prevent unused argument(s) compilation warning */
676 /* NOTE : This function Should not be modified, when the callback is needed,
677 the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
682 * @brief Conversion half DMA transfer callback in non blocking mode for Channel1
683 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
684 * the configuration information for the specified DAC.
687 __weak
void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef
* hdac
)
689 /* Prevent unused argument(s) compilation warning */
691 /* NOTE : This function Should not be modified, when the callback is needed,
692 the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
697 * @brief Error DAC callback for Channel1.
698 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
699 * the configuration information for the specified DAC.
702 __weak
void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef
*hdac
)
704 /* Prevent unused argument(s) compilation warning */
706 /* NOTE : This function Should not be modified, when the callback is needed,
707 the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
715 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
716 * @brief Peripheral Control functions
719 ==============================================================================
720 ##### Peripheral Control functions #####
721 ==============================================================================
722 [..] This section provides functions allowing to:
723 (+) Configure channels.
724 (+) Set the specified data holding register value for DAC channel.
731 * @brief Configures the selected DAC channel.
732 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
733 * the configuration information for the specified DAC.
734 * @param sConfig: DAC configuration structure.
735 * @param Channel: The selected DAC channel.
736 * This parameter can be one of the following values:
737 * @arg DAC_CHANNEL_1: DAC Channel1 selected
738 * @arg DAC_CHANNEL_2: DAC Channel2 selected
741 HAL_StatusTypeDef
HAL_DAC_ConfigChannel(DAC_HandleTypeDef
* hdac
, DAC_ChannelConfTypeDef
* sConfig
, uint32_t Channel
)
743 uint32_t tmpreg1
= 0U;
745 /* Check the DAC parameters */
746 assert_param(IS_DAC_TRIGGER(sConfig
->DAC_Trigger
));
747 assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig
->DAC_OutputBuffer
));
748 assert_param(IS_DAC_CHANNEL(Channel
));
753 /* Change DAC state */
754 hdac
->State
= HAL_DAC_STATE_BUSY
;
756 /* Configure for the selected DAC channel: buffer output, trigger */
757 /* Set TSELx and TENx bits according to DAC_Trigger value */
758 /* Set BOFFx bit according to DAC_OutputBuffer value */
759 SET_BIT(tmpreg1
, (sConfig
->DAC_Trigger
| sConfig
->DAC_OutputBuffer
));
761 /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
762 /* Calculate CR register value depending on DAC_Channel */
763 MODIFY_REG(hdac
->Instance
->CR
,
764 ((uint32_t)(DAC_CR_MAMP1
| DAC_CR_WAVE1
| DAC_CR_TSEL1
| DAC_CR_TEN1
| DAC_CR_BOFF1
)) << Channel
,
767 /* Disable wave generation */
768 hdac
->Instance
->CR
&= ~(DAC_CR_WAVE1
<< Channel
);
770 /* Change DAC state */
771 hdac
->State
= HAL_DAC_STATE_READY
;
773 /* Process unlocked */
776 /* Return function status */
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.
796 HAL_StatusTypeDef
HAL_DAC_SetValue(DAC_HandleTypeDef
* hdac
, uint32_t Channel
, uint32_t Alignment
, uint32_t Data
)
798 __IO
uint32_t tmp
= 0U;
800 /* Check the parameters */
801 assert_param(IS_DAC_CHANNEL(Channel
));
802 assert_param(IS_DAC_ALIGN(Alignment
));
803 assert_param(IS_DAC_DATA(Data
));
805 tmp
= (uint32_t)hdac
->Instance
;
806 if(Channel
== DAC_CHANNEL_1
)
808 tmp
+= DAC_DHR12R1_ALIGNMENT(Alignment
);
812 tmp
+= DAC_DHR12R2_ALIGNMENT(Alignment
);
815 /* Set the DAC channel selected data holding register */
816 *(__IO
uint32_t *) tmp
= Data
;
818 /* Return function status */
826 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
827 * @brief Peripheral State and Errors functions
830 ==============================================================================
831 ##### Peripheral State and Errors functions #####
832 ==============================================================================
834 This subsection provides functions allowing to
835 (+) Check the DAC state.
836 (+) Check the DAC Errors.
843 * @brief return the DAC state
844 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
845 * the configuration information for the specified DAC.
848 HAL_DAC_StateTypeDef
HAL_DAC_GetState(DAC_HandleTypeDef
* hdac
)
850 /* Return DAC state */
856 * @brief Return the DAC error code
857 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
858 * the configuration information for the specified DAC.
859 * @retval DAC Error Code
861 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef
*hdac
)
863 return hdac
->ErrorCode
;
874 /** @addtogroup DAC_Private_Functions
879 * @brief DMA conversion complete callback.
880 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
881 * the configuration information for the specified DMA module.
884 void DAC_DMAConvCpltCh1(DMA_HandleTypeDef
*hdma
)
886 DAC_HandleTypeDef
* hdac
= ( DAC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
888 HAL_DAC_ConvCpltCallbackCh1(hdac
);
890 hdac
->State
= HAL_DAC_STATE_READY
;
894 * @brief DMA half transfer complete callback.
895 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
896 * the configuration information for the specified DMA module.
899 void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef
*hdma
)
901 DAC_HandleTypeDef
* hdac
= ( DAC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
902 /* Conversion complete callback */
903 HAL_DAC_ConvHalfCpltCallbackCh1(hdac
);
907 * @brief DMA error callback
908 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
909 * the configuration information for the specified DMA module.
912 void DAC_DMAErrorCh1(DMA_HandleTypeDef
*hdma
)
914 DAC_HandleTypeDef
* hdac
= ( DAC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
916 /* Set DAC error code to DMA error */
917 hdac
->ErrorCode
|= HAL_DAC_ERROR_DMA
;
919 HAL_DAC_ErrorCallbackCh1(hdac
);
921 hdac
->State
= HAL_DAC_STATE_READY
;
928 #endif /* STM32F100xB || STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
929 #endif /* HAL_DAC_MODULE_ENABLED */
939 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/