2 ******************************************************************************
3 * @file stm32l1xx_hal_dac.c
4 * @author MCD Application Team
6 * @date 5-September-2014
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_Pin9) using DAC_Trigger_Ext_IT9.
37 The used pin (GPIOx_Pin9) must be configured in input mode.
39 (#) Timers TRGO: TIM2, TIM4, TIM6, TIM7, TIM9
40 (DAC_Trigger_T2_TRGO, DAC_Trigger_T4_TRGO...)
42 (#) Software using DAC_Trigger_Software
44 *** DAC Buffer mode feature ***
45 ===============================
47 Each DAC channel integrates an output buffer that can be used to
48 reduce the output impedance, and to drive external loads directly
49 without having to add an external operational amplifier.
50 To enable, the output buffer use
51 sConfig.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
53 (@) Refer to the device datasheet for more details about output
54 impedance value with and without output buffer.
56 *** DAC wave generation feature ***
57 ===================================
59 Both DAC channels can be used to generate
63 *** DAC data format ***
64 =======================
66 The DAC data format can be:
67 (#) 8-bit right alignment using DAC_ALIGN_8B_R
68 (#) 12-bit left alignment using DAC_ALIGN_12B_L
69 (#) 12-bit right alignment using DAC_ALIGN_12B_R
71 *** DAC data value to voltage correspondence ***
72 ================================================
74 The analog output voltage on each DAC channel pin is determined
75 by the following equation:
76 DAC_OUTx = VREF+ * DOR / 4095
77 with DOR is the Data Output Register
78 VEF+ is the input voltage reference (refer to the device datasheet)
79 e.g. To set DAC_OUT1 to 0.7V, use
80 Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
85 A DMA1 request can be generated when an external trigger (but not
86 a software trigger) occurs if DMA1 requests are enabled using
89 DMA1 requests are mapped as following:
90 (#) DAC channel1 : mapped on DMA1 channel2 which must be
92 (#) DAC channel2 : mapped on DMA1 channel3 which must be
95 -@- For Dual mode and specific signal (Triangle and noise) generation please
96 refer to Extension Features Driver description
99 ##### How to use this driver #####
100 ==============================================================================
102 (+) DAC APB clock must be enabled to get write access to DAC
103 registers using HAL_DAC_Init()
104 (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
105 (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
106 (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
108 *** Polling mode IO operation ***
109 =================================
111 (+) Start the DAC peripheral using HAL_DAC_Start()
112 (+) To read the DAC last data output value value, use the HAL_DAC_GetValue() function.
113 (+) Stop the DAC peripheral using HAL_DAC_Stop()
115 *** DMA mode IO operation ***
116 ==============================
118 (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
119 of data to be transferred at each end of conversion
120 (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1()or HAL_DAC_ConvHalfCpltCallbackCh2()
121 function is executed and user can add his own code by customization of function pointer
122 HAL_DAC_ConvHalfCpltCallbackCh1 or HAL_DAC_ConvHalfCpltCallbackCh2
123 (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
124 function is executed and user can add his own code by customization of function pointer
125 HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
126 (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
127 add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
128 (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
129 HAL_DAC_DMAUnderrunCallbackCh1()or HAL_DAC_DMAUnderrunCallbackCh2()
130 function is executed and user can add his own code by customization of function pointer
131 HAL_DAC_DMAUnderrunCallbackCh1 or HAL_DAC_DMAUnderrunCallbackCh2
132 add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
133 (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
135 *** DAC HAL driver macros list ***
136 =============================================
138 Below the list of most used macros in DAC HAL driver.
140 (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
141 (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
142 (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
143 (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
146 (@) You can refer to the DAC HAL driver header file for more useful macros
149 ******************************************************************************
152 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
154 * Redistribution and use in source and binary forms, with or without modification,
155 * are permitted provided that the following conditions are met:
156 * 1. Redistributions of source code must retain the above copyright notice,
157 * this list of conditions and the following disclaimer.
158 * 2. Redistributions in binary form must reproduce the above copyright notice,
159 * this list of conditions and the following disclaimer in the documentation
160 * and/or other materials provided with the distribution.
161 * 3. Neither the name of STMicroelectronics nor the names of its contributors
162 * may be used to endorse or promote products derived from this software
163 * without specific prior written permission.
165 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
166 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
168 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
169 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
170 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
171 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
172 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
173 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
174 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
176 ******************************************************************************
180 /* Includes ------------------------------------------------------------------*/
181 #include "stm32l1xx_hal.h"
183 /** @addtogroup STM32L1xx_HAL_Driver
187 /** @defgroup DAC DAC
188 * @brief DAC driver modules
192 #ifdef HAL_DAC_MODULE_ENABLED
194 /* Private typedef -----------------------------------------------------------*/
195 /* Private define ------------------------------------------------------------*/
196 /* Private macro -------------------------------------------------------------*/
197 /* Private variables ---------------------------------------------------------*/
198 /* Private function prototypes -----------------------------------------------*/
199 /** @defgroup DAC_Private_Functions DAC Private Functions
202 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef
*hdma
);
203 static void DAC_DMAErrorCh1(DMA_HandleTypeDef
*hdma
);
204 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef
*hdma
);
209 /* Private functions ---------------------------------------------------------*/
211 /** @defgroup DAC_Exported_Functions DAC Exported Functions
215 /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
216 * @brief Initialization and Configuration functions
219 ==============================================================================
220 ##### Initialization and de-initialization functions #####
221 ==============================================================================
222 [..] This section provides functions allowing to:
223 (+) Initialize and configure the DAC.
224 (+) De-initialize the DAC.
231 * @brief Initializes the DAC peripheral according to the specified parameters
232 * in the DAC_InitStruct.
233 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
234 * the configuration information for the specified DAC.
237 HAL_StatusTypeDef
HAL_DAC_Init(DAC_HandleTypeDef
* hdac
)
239 /* Check DAC handle */
244 /* Check the parameters */
245 assert_param(IS_DAC_ALL_INSTANCE(hdac
->Instance
));
247 if(hdac
->State
== HAL_DAC_STATE_RESET
)
249 /* Init the low level hardware */
250 HAL_DAC_MspInit(hdac
);
253 /* Initialize the DAC state*/
254 hdac
->State
= HAL_DAC_STATE_BUSY
;
256 /* Set DAC error code to none */
257 hdac
->ErrorCode
= HAL_DAC_ERROR_NONE
;
259 /* Initialize the DAC state*/
260 hdac
->State
= HAL_DAC_STATE_READY
;
262 /* Return function status */
267 * @brief Deinitializes the DAC peripheral registers to their default reset values.
268 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
269 * the configuration information for the specified DAC.
272 HAL_StatusTypeDef
HAL_DAC_DeInit(DAC_HandleTypeDef
* hdac
)
274 /* Check DAC handle */
280 /* Check the parameters */
281 assert_param(IS_DAC_ALL_INSTANCE(hdac
->Instance
));
283 /* Change DAC state */
284 hdac
->State
= HAL_DAC_STATE_BUSY
;
286 /* DeInit the low level hardware */
287 HAL_DAC_MspDeInit(hdac
);
289 /* Set DAC error code to none */
290 hdac
->ErrorCode
= HAL_DAC_ERROR_NONE
;
292 /* Change DAC state */
293 hdac
->State
= HAL_DAC_STATE_RESET
;
298 /* Return function status */
303 * @brief Initializes the DAC MSP.
304 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
305 * the configuration information for the specified DAC.
308 __weak
void HAL_DAC_MspInit(DAC_HandleTypeDef
* hdac
)
310 /* NOTE : This function Should not be modified, when the callback is needed,
311 the HAL_DAC_MspInit could be implemented in the user file
316 * @brief DeInitializes the DAC MSP.
317 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
318 * the configuration information for the specified DAC.
321 __weak
void HAL_DAC_MspDeInit(DAC_HandleTypeDef
* hdac
)
323 /* NOTE : This function Should not be modified, when the callback is needed,
324 the HAL_DAC_MspDeInit could be implemented in the user file
332 /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
333 * @brief IO operation functions
336 ==============================================================================
337 ##### IO operation functions #####
338 ==============================================================================
339 [..] This section provides functions allowing to:
340 (+) Start conversion.
342 (+) Start conversion and enable DMA transfer.
343 (+) Stop conversion and disable DMA transfer.
344 (+) Get result of conversion.
351 * @brief Enables DAC and starts conversion of channel.
352 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
353 * the configuration information for the specified DAC.
354 * @param Channel: The selected DAC channel.
355 * This parameter can be one of the following values:
356 * @arg DAC_CHANNEL_1: DAC Channel1 selected
357 * @arg DAC_CHANNEL_2: DAC Channel2 selected
360 HAL_StatusTypeDef
HAL_DAC_Start(DAC_HandleTypeDef
* hdac
, uint32_t Channel
)
362 uint32_t tmp1
= 0, tmp2
= 0;
364 /* Check the parameters */
365 assert_param(IS_DAC_CHANNEL(Channel
));
370 /* Change DAC state */
371 hdac
->State
= HAL_DAC_STATE_BUSY
;
373 /* Enable the Peripharal */
374 __HAL_DAC_ENABLE(hdac
, Channel
);
376 if(Channel
== DAC_CHANNEL_1
)
378 tmp1
= hdac
->Instance
->CR
& DAC_CR_TEN1
;
379 tmp2
= hdac
->Instance
->CR
& DAC_CR_TSEL1
;
380 /* Check if software trigger enabled */
381 if((tmp1
== DAC_CR_TEN1
) && (tmp2
== DAC_CR_TSEL1
))
383 /* Enable the selected DAC software conversion */
384 SET_BIT(hdac
->Instance
->SWTRIGR
, DAC_SWTRIGR_SWTRIG1
);
389 tmp1
= hdac
->Instance
->CR
& DAC_CR_TEN2
;
390 tmp2
= hdac
->Instance
->CR
& DAC_CR_TSEL2
;
391 /* Check if software trigger enabled */
392 if((tmp1
== DAC_CR_TEN2
) && (tmp2
== DAC_CR_TSEL2
))
394 /* Enable the selected DAC software conversion*/
395 SET_BIT(hdac
->Instance
->SWTRIGR
, DAC_SWTRIGR_SWTRIG2
);
399 /* Change DAC state */
400 hdac
->State
= HAL_DAC_STATE_READY
;
402 /* Process unlocked */
405 /* Return function status */
410 * @brief Disables DAC and stop conversion of channel.
411 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
412 * the configuration information for the specified DAC.
413 * @param Channel: The selected DAC channel.
414 * This parameter can be one of the following values:
415 * @arg DAC_CHANNEL_1: DAC Channel1 selected
416 * @arg DAC_CHANNEL_2: DAC Channel2 selected
419 HAL_StatusTypeDef
HAL_DAC_Stop(DAC_HandleTypeDef
* hdac
, uint32_t Channel
)
421 /* Check the parameters */
422 assert_param(IS_DAC_CHANNEL(Channel
));
424 /* Disable the Peripheral */
425 __HAL_DAC_DISABLE(hdac
, Channel
);
427 /* Change DAC state */
428 hdac
->State
= HAL_DAC_STATE_READY
;
430 /* Return function status */
435 * @brief Enables DAC and starts conversion of channel.
436 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
437 * the configuration information for the specified DAC.
438 * @param Channel: The selected DAC channel.
439 * This parameter can be one of the following values:
440 * @arg DAC_CHANNEL_1: DAC Channel1 selected
441 * @arg DAC_CHANNEL_2: DAC Channel2 selected
442 * @param pData: The destination peripheral Buffer address.
443 * @param Length: The length of data to be transferred from memory to DAC peripheral
444 * @param Alignment: Specifies the data alignment for DAC channel.
445 * This parameter can be one of the following values:
446 * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
447 * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
448 * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
451 HAL_StatusTypeDef
HAL_DAC_Start_DMA(DAC_HandleTypeDef
* hdac
, uint32_t Channel
, uint32_t* pData
, uint32_t Length
, uint32_t Alignment
)
455 /* Check the parameters */
456 assert_param(IS_DAC_CHANNEL(Channel
));
457 assert_param(IS_DAC_ALIGN(Alignment
));
462 /* Change DAC state */
463 hdac
->State
= HAL_DAC_STATE_BUSY
;
465 if(Channel
== DAC_CHANNEL_1
)
467 /* Set the DMA transfer complete callback for channel1 */
468 hdac
->DMA_Handle1
->XferCpltCallback
= DAC_DMAConvCpltCh1
;
470 /* Set the DMA half transfer complete callback for channel1 */
471 hdac
->DMA_Handle1
->XferHalfCpltCallback
= DAC_DMAHalfConvCpltCh1
;
473 /* Set the DMA error callback for channel1 */
474 hdac
->DMA_Handle1
->XferErrorCallback
= DAC_DMAErrorCh1
;
476 /* Enable the selected DAC channel1 DMA request */
477 SET_BIT(hdac
->Instance
->CR
, DAC_CR_DMAEN1
);
479 /* Case of use of channel 1 */
482 case DAC_ALIGN_12B_R
:
483 /* Get DHR12R1 address */
484 tmpreg
= (uint32_t)&hdac
->Instance
->DHR12R1
;
486 case DAC_ALIGN_12B_L
:
487 /* Get DHR12L1 address */
488 tmpreg
= (uint32_t)&hdac
->Instance
->DHR12L1
;
491 /* Get DHR8R1 address */
492 tmpreg
= (uint32_t)&hdac
->Instance
->DHR8R1
;
500 /* Set the DMA transfer complete callback for channel2 */
501 hdac
->DMA_Handle2
->XferCpltCallback
= DAC_DMAConvCpltCh2
;
503 /* Set the DMA half transfer complete callback for channel2 */
504 hdac
->DMA_Handle2
->XferHalfCpltCallback
= DAC_DMAHalfConvCpltCh2
;
506 /* Set the DMA error callback for channel2 */
507 hdac
->DMA_Handle2
->XferErrorCallback
= DAC_DMAErrorCh2
;
509 /* Enable the selected DAC channel2 DMA request */
510 SET_BIT(hdac
->Instance
->CR
, DAC_CR_DMAEN2
);
512 /* Case of use of channel 2 */
515 case DAC_ALIGN_12B_R
:
516 /* Get DHR12R2 address */
517 tmpreg
= (uint32_t)&hdac
->Instance
->DHR12R2
;
519 case DAC_ALIGN_12B_L
:
520 /* Get DHR12L2 address */
521 tmpreg
= (uint32_t)&hdac
->Instance
->DHR12L2
;
524 /* Get DHR8R2 address */
525 tmpreg
= (uint32_t)&hdac
->Instance
->DHR8R2
;
532 /* Enable the DMA Stream */
533 if(Channel
== DAC_CHANNEL_1
)
535 /* Enable the DAC DMA underrun interrupt */
536 __HAL_DAC_ENABLE_IT(hdac
, DAC_IT_DMAUDR1
);
538 /* Enable the DMA Stream */
539 HAL_DMA_Start_IT(hdac
->DMA_Handle1
, (uint32_t)pData
, tmpreg
, Length
);
543 /* Enable the DAC DMA underrun interrupt */
544 __HAL_DAC_ENABLE_IT(hdac
, DAC_IT_DMAUDR2
);
546 /* Enable the DMA Stream */
547 HAL_DMA_Start_IT(hdac
->DMA_Handle2
, (uint32_t)pData
, tmpreg
, Length
);
550 /* Enable the Peripharal */
551 __HAL_DAC_ENABLE(hdac
, Channel
);
553 /* Process Unlocked */
556 /* Return function status */
561 * @brief Disables DAC and stop conversion of channel.
562 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
563 * the configuration information for the specified DAC.
564 * @param Channel: The selected DAC channel.
565 * This parameter can be one of the following values:
566 * @arg DAC_CHANNEL_1: DAC Channel1 selected
567 * @arg DAC_CHANNEL_2: DAC Channel2 selected
570 HAL_StatusTypeDef
HAL_DAC_Stop_DMA(DAC_HandleTypeDef
* hdac
, uint32_t Channel
)
572 HAL_StatusTypeDef status
= HAL_OK
;
574 /* Check the parameters */
575 assert_param(IS_DAC_CHANNEL(Channel
));
577 /* Disable the selected DAC channel DMA request */
578 hdac
->Instance
->CR
&= ~(DAC_CR_DMAEN1
<< Channel
);
580 /* Disable the Peripharal */
581 __HAL_DAC_DISABLE(hdac
, Channel
);
583 /* Disable the DMA Channel */
584 /* Channel1 is used */
585 if(Channel
== DAC_CHANNEL_1
)
587 status
= HAL_DMA_Abort(hdac
->DMA_Handle1
);
589 else /* Channel2 is used for */
591 status
= HAL_DMA_Abort(hdac
->DMA_Handle2
);
594 /* Check if DMA Channel effectively disabled */
597 /* Update ADC state machine to error */
598 hdac
->State
= HAL_DAC_STATE_ERROR
;
602 /* Change DAC state */
603 hdac
->State
= HAL_DAC_STATE_READY
;
606 /* Return function status */
611 * @brief Returns the last data output value of the selected DAC channel.
612 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
613 * the configuration information for the specified DAC.
614 * @param Channel: The selected DAC channel.
615 * This parameter can be one of the following values:
616 * @arg DAC_CHANNEL_1: DAC Channel1 selected
617 * @arg DAC_CHANNEL_2: DAC Channel2 selected
618 * @retval The selected DAC channel data output value.
620 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef
* hdac
, uint32_t Channel
)
622 /* Check the parameters */
623 assert_param(IS_DAC_CHANNEL(Channel
));
625 /* Returns the DAC channel data output register value */
626 if(Channel
== DAC_CHANNEL_1
)
628 return hdac
->Instance
->DOR1
;
632 return hdac
->Instance
->DOR2
;
637 * @brief Handles DAC interrupt request
638 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
639 * the configuration information for the specified DAC.
642 void HAL_DAC_IRQHandler(DAC_HandleTypeDef
* hdac
)
644 /* Check underrun flag of DAC channel 1 */
645 if(__HAL_DAC_GET_FLAG(hdac
, DAC_FLAG_DMAUDR1
))
647 /* Change DAC state to error state */
648 hdac
->State
= HAL_DAC_STATE_ERROR
;
650 /* Set DAC error code to chanel1 DMA underrun error */
651 hdac
->ErrorCode
|= HAL_DAC_ERROR_DMAUNDERRUNCH1
;
653 /* Clear the underrun flag */
654 __HAL_DAC_CLEAR_FLAG(hdac
,DAC_FLAG_DMAUDR1
);
656 /* Disable the selected DAC channel1 DMA request */
657 hdac
->Instance
->CR
&= ~DAC_CR_DMAEN1
;
660 HAL_DAC_DMAUnderrunCallbackCh1(hdac
);
663 /* Check underrun flag of DAC channel 2 */
664 if(__HAL_DAC_GET_FLAG(hdac
, DAC_FLAG_DMAUDR2
))
666 /* Change DAC state to error state */
667 hdac
->State
= HAL_DAC_STATE_ERROR
;
669 /* Set DAC error code to channel2 DMA underrun error */
670 hdac
->ErrorCode
|= HAL_DAC_ERROR_DMAUNDERRUNCH2
;
672 /* Clear the underrun flag */
673 __HAL_DAC_CLEAR_FLAG(hdac
,DAC_FLAG_DMAUDR2
);
675 /* Disable the selected DAC channel1 DMA request */
676 hdac
->Instance
->CR
&= ~DAC_CR_DMAEN2
;
679 HAL_DACEx_DMAUnderrunCallbackCh2(hdac
);
684 * @brief Conversion complete callback in non blocking mode for Channel1
685 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
686 * the configuration information for the specified DAC.
689 __weak
void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef
* hdac
)
691 /* NOTE : This function Should not be modified, when the callback is needed,
692 the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
697 * @brief Conversion half DMA transfer callback in non blocking mode 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_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef
* hdac
)
704 /* NOTE : This function Should not be modified, when the callback is needed,
705 the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
710 * @brief Error DAC callback for Channel1.
711 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
712 * the configuration information for the specified DAC.
715 __weak
void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef
*hdac
)
717 /* NOTE : This function Should not be modified, when the callback is needed,
718 the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
723 * @brief DMA underrun DAC callback for channel1.
724 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
725 * the configuration information for the specified DAC.
728 __weak
void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef
*hdac
)
730 /* NOTE : This function Should not be modified, when the callback is needed,
731 the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
739 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
740 * @brief Peripheral Control functions
743 ==============================================================================
744 ##### Peripheral Control functions #####
745 ==============================================================================
746 [..] This section provides functions allowing to:
747 (+) Configure channels.
748 (+) Set the specified data holding register value for DAC channel.
755 * @brief Configures the selected DAC channel.
756 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
757 * the configuration information for the specified DAC.
758 * @param sConfig: DAC configuration structure.
759 * @param Channel: The selected DAC channel.
760 * This parameter can be one of the following values:
761 * @arg DAC_CHANNEL_1: DAC Channel1 selected
762 * @arg DAC_CHANNEL_2: DAC Channel2 selected
765 HAL_StatusTypeDef
HAL_DAC_ConfigChannel(DAC_HandleTypeDef
* hdac
, DAC_ChannelConfTypeDef
* sConfig
, uint32_t Channel
)
767 uint32_t tmpreg1
= 0, tmpreg2
= 0;
769 /* Check the DAC parameters */
770 assert_param(IS_DAC_TRIGGER(sConfig
->DAC_Trigger
));
771 assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig
->DAC_OutputBuffer
));
772 assert_param(IS_DAC_CHANNEL(Channel
));
777 /* Change DAC state */
778 hdac
->State
= HAL_DAC_STATE_BUSY
;
780 /* Get the DAC CR value */
782 /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
783 tmpreg1
&= ~(((uint32_t)(DAC_CR_MAMP1
| DAC_CR_WAVE1
| DAC_CR_TSEL1
| DAC_CR_TEN1
| DAC_CR_BOFF1
)) << Channel
);
784 /* Configure for the selected DAC channel: buffer output, trigger */
785 /* Set TSELx and TENx bits according to DAC_Trigger value */
786 /* Set BOFFx bit according to DAC_OutputBuffer value */
787 tmpreg2
= (sConfig
->DAC_Trigger
| sConfig
->DAC_OutputBuffer
);
788 /* Calculate CR register value depending on DAC_Channel */
789 tmpreg1
|= tmpreg2
<< Channel
;
790 /* Write to DAC CR */
792 /* Disable wave generation */
793 DAC
->CR
&= ~(DAC_CR_WAVE1
<< Channel
);
795 /* Change DAC state */
796 hdac
->State
= HAL_DAC_STATE_READY
;
798 /* Process unlocked */
801 /* Return function status */
806 * @brief Set the specified data holding register value for DAC channel.
807 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
808 * the configuration information for the specified DAC.
809 * @param Channel: The selected DAC channel.
810 * This parameter can be one of the following values:
811 * @arg DAC_CHANNEL_1: DAC Channel1 selected
812 * @arg DAC_CHANNEL_2: DAC Channel2 selected
813 * @param Alignment: Specifies the data alignment.
814 * This parameter can be one of the following values:
815 * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
816 * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
817 * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
818 * @param Data: Data to be loaded in the selected data holding register.
821 HAL_StatusTypeDef
HAL_DAC_SetValue(DAC_HandleTypeDef
* hdac
, uint32_t Channel
, uint32_t Alignment
, uint32_t Data
)
823 __IO
uint32_t tmp
= 0;
825 /* Check the parameters */
826 assert_param(IS_DAC_CHANNEL(Channel
));
827 assert_param(IS_DAC_ALIGN(Alignment
));
828 assert_param(IS_DAC_DATA(Data
));
830 tmp
= (uint32_t)hdac
->Instance
;
831 if(Channel
== DAC_CHANNEL_1
)
833 tmp
+= __HAL_DHR12R1_ALIGNEMENT(Alignment
);
837 tmp
+= __HAL_DHR12R2_ALIGNEMENT(Alignment
);
840 /* Set the DAC channel selected data holding register */
841 *(__IO
uint32_t *) tmp
= Data
;
843 /* Return function status */
851 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
852 * @brief Peripheral State and Errors functions
855 ==============================================================================
856 ##### Peripheral State and Errors functions #####
857 ==============================================================================
859 This subsection provides functions allowing to
860 (+) Check the DAC state.
861 (+) Check the DAC Errors.
868 * @brief return the DAC state
869 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
870 * the configuration information for the specified DAC.
873 HAL_DAC_StateTypeDef
HAL_DAC_GetState(DAC_HandleTypeDef
* hdac
)
875 /* Return DAC state */
881 * @brief Return the DAC error code
882 * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
883 * the configuration information for the specified DAC.
884 * @retval DAC Error Code
886 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef
*hdac
)
888 return hdac
->ErrorCode
;
899 /** @addtogroup DAC_Private_Functions
904 * @brief DMA conversion complete callback.
905 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
906 * the configuration information for the specified DMA module.
909 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef
*hdma
)
911 DAC_HandleTypeDef
* hdac
= ( DAC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
913 HAL_DAC_ConvCpltCallbackCh1(hdac
);
915 hdac
->State
= HAL_DAC_STATE_READY
;
919 * @brief DMA half transfer complete callback.
920 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
921 * the configuration information for the specified DMA module.
924 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef
*hdma
)
926 DAC_HandleTypeDef
* hdac
= ( DAC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
927 /* Conversion complete callback */
928 HAL_DAC_ConvHalfCpltCallbackCh1(hdac
);
932 * @brief DMA error callback
933 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
934 * the configuration information for the specified DMA module.
937 static void DAC_DMAErrorCh1(DMA_HandleTypeDef
*hdma
)
939 DAC_HandleTypeDef
* hdac
= ( DAC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
941 /* Set DAC error code to DMA error */
942 hdac
->ErrorCode
|= HAL_DAC_ERROR_DMA
;
944 HAL_DAC_ErrorCallbackCh1(hdac
);
946 hdac
->State
= HAL_DAC_STATE_READY
;
953 #endif /* HAL_DAC_MODULE_ENABLED */
963 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/