2 ******************************************************************************
\r
3 * @file stm32f10x_dac.c
\r
4 * @author MCD Application Team
\r
6 * @date 11-March-2011
\r
7 * @brief This file provides all the DAC firmware functions.
\r
8 ******************************************************************************
\r
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
\r
19 ******************************************************************************
\r
22 /* Includes ------------------------------------------------------------------*/
\r
23 #include "stm32f10x_dac.h"
\r
24 #include "stm32f10x_rcc.h"
\r
26 /** @addtogroup STM32F10x_StdPeriph_Driver
\r
31 * @brief DAC driver modules
\r
35 /** @defgroup DAC_Private_TypesDefinitions
\r
43 /** @defgroup DAC_Private_Defines
\r
47 /* CR register Mask */
\r
48 #define CR_CLEAR_MASK ((uint32_t)0x00000FFE)
\r
50 /* DAC Dual Channels SWTRIG masks */
\r
51 #define DUAL_SWTRIG_SET ((uint32_t)0x00000003)
\r
52 #define DUAL_SWTRIG_RESET ((uint32_t)0xFFFFFFFC)
\r
54 /* DHR registers offsets */
\r
55 #define DHR12R1_OFFSET ((uint32_t)0x00000008)
\r
56 #define DHR12R2_OFFSET ((uint32_t)0x00000014)
\r
57 #define DHR12RD_OFFSET ((uint32_t)0x00000020)
\r
59 /* DOR register offset */
\r
60 #define DOR_OFFSET ((uint32_t)0x0000002C)
\r
65 /** @defgroup DAC_Private_Macros
\r
73 /** @defgroup DAC_Private_Variables
\r
81 /** @defgroup DAC_Private_FunctionPrototypes
\r
89 /** @defgroup DAC_Private_Functions
\r
94 * @brief Deinitializes the DAC peripheral registers to their default reset values.
\r
98 void DAC_DeInit(void)
\r
100 /* Enable DAC reset state */
\r
101 RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);
\r
102 /* Release DAC from reset state */
\r
103 RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);
\r
107 * @brief Initializes the DAC peripheral according to the specified
\r
108 * parameters in the DAC_InitStruct.
\r
109 * @param DAC_Channel: the selected DAC channel.
\r
110 * This parameter can be one of the following values:
\r
111 * @arg DAC_Channel_1: DAC Channel1 selected
\r
112 * @arg DAC_Channel_2: DAC Channel2 selected
\r
113 * @param DAC_InitStruct: pointer to a DAC_InitTypeDef structure that
\r
114 * contains the configuration information for the specified DAC channel.
\r
117 void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct)
\r
119 uint32_t tmpreg1 = 0, tmpreg2 = 0;
\r
120 /* Check the DAC parameters */
\r
121 assert_param(IS_DAC_TRIGGER(DAC_InitStruct->DAC_Trigger));
\r
122 assert_param(IS_DAC_GENERATE_WAVE(DAC_InitStruct->DAC_WaveGeneration));
\r
123 assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude));
\r
124 assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->DAC_OutputBuffer));
\r
125 /*---------------------------- DAC CR Configuration --------------------------*/
\r
126 /* Get the DAC CR value */
\r
128 /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
\r
129 tmpreg1 &= ~(CR_CLEAR_MASK << DAC_Channel);
\r
130 /* Configure for the selected DAC channel: buffer output, trigger, wave generation,
\r
131 mask/amplitude for wave generation */
\r
132 /* Set TSELx and TENx bits according to DAC_Trigger value */
\r
133 /* Set WAVEx bits according to DAC_WaveGeneration value */
\r
134 /* Set MAMPx bits according to DAC_LFSRUnmask_TriangleAmplitude value */
\r
135 /* Set BOFFx bit according to DAC_OutputBuffer value */
\r
136 tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration |
\r
137 DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | DAC_InitStruct->DAC_OutputBuffer);
\r
138 /* Calculate CR register value depending on DAC_Channel */
\r
139 tmpreg1 |= tmpreg2 << DAC_Channel;
\r
140 /* Write to DAC CR */
\r
145 * @brief Fills each DAC_InitStruct member with its default value.
\r
146 * @param DAC_InitStruct : pointer to a DAC_InitTypeDef structure which will
\r
150 void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct)
\r
152 /*--------------- Reset DAC init structure parameters values -----------------*/
\r
153 /* Initialize the DAC_Trigger member */
\r
154 DAC_InitStruct->DAC_Trigger = DAC_Trigger_None;
\r
155 /* Initialize the DAC_WaveGeneration member */
\r
156 DAC_InitStruct->DAC_WaveGeneration = DAC_WaveGeneration_None;
\r
157 /* Initialize the DAC_LFSRUnmask_TriangleAmplitude member */
\r
158 DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
\r
159 /* Initialize the DAC_OutputBuffer member */
\r
160 DAC_InitStruct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;
\r
164 * @brief Enables or disables the specified DAC channel.
\r
165 * @param DAC_Channel: the selected DAC channel.
\r
166 * This parameter can be one of the following values:
\r
167 * @arg DAC_Channel_1: DAC Channel1 selected
\r
168 * @arg DAC_Channel_2: DAC Channel2 selected
\r
169 * @param NewState: new state of the DAC channel.
\r
170 * This parameter can be: ENABLE or DISABLE.
\r
173 void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState)
\r
175 /* Check the parameters */
\r
176 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
177 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
178 if (NewState != DISABLE)
\r
180 /* Enable the selected DAC channel */
\r
181 DAC->CR |= (DAC_CR_EN1 << DAC_Channel);
\r
185 /* Disable the selected DAC channel */
\r
186 DAC->CR &= ~(DAC_CR_EN1 << DAC_Channel);
\r
189 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
\r
191 * @brief Enables or disables the specified DAC interrupts.
\r
192 * @param DAC_Channel: the selected DAC channel.
\r
193 * This parameter can be one of the following values:
\r
194 * @arg DAC_Channel_1: DAC Channel1 selected
\r
195 * @arg DAC_Channel_2: DAC Channel2 selected
\r
196 * @param DAC_IT: specifies the DAC interrupt sources to be enabled or disabled.
\r
197 * This parameter can be the following values:
\r
198 * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
\r
199 * @param NewState: new state of the specified DAC interrupts.
\r
200 * This parameter can be: ENABLE or DISABLE.
\r
203 void DAC_ITConfig(uint32_t DAC_Channel, uint32_t DAC_IT, FunctionalState NewState)
\r
205 /* Check the parameters */
\r
206 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
207 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
208 assert_param(IS_DAC_IT(DAC_IT));
\r
210 if (NewState != DISABLE)
\r
212 /* Enable the selected DAC interrupts */
\r
213 DAC->CR |= (DAC_IT << DAC_Channel);
\r
217 /* Disable the selected DAC interrupts */
\r
218 DAC->CR &= (~(uint32_t)(DAC_IT << DAC_Channel));
\r
224 * @brief Enables or disables the specified DAC channel DMA request.
\r
225 * @param DAC_Channel: the selected DAC channel.
\r
226 * This parameter can be one of the following values:
\r
227 * @arg DAC_Channel_1: DAC Channel1 selected
\r
228 * @arg DAC_Channel_2: DAC Channel2 selected
\r
229 * @param NewState: new state of the selected DAC channel DMA request.
\r
230 * This parameter can be: ENABLE or DISABLE.
\r
233 void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState)
\r
235 /* Check the parameters */
\r
236 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
237 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
238 if (NewState != DISABLE)
\r
240 /* Enable the selected DAC channel DMA request */
\r
241 DAC->CR |= (DAC_CR_DMAEN1 << DAC_Channel);
\r
245 /* Disable the selected DAC channel DMA request */
\r
246 DAC->CR &= ~(DAC_CR_DMAEN1 << DAC_Channel);
\r
251 * @brief Enables or disables the selected DAC channel software trigger.
\r
252 * @param DAC_Channel: the selected DAC channel.
\r
253 * This parameter can be one of the following values:
\r
254 * @arg DAC_Channel_1: DAC Channel1 selected
\r
255 * @arg DAC_Channel_2: DAC Channel2 selected
\r
256 * @param NewState: new state of the selected DAC channel software trigger.
\r
257 * This parameter can be: ENABLE or DISABLE.
\r
260 void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState)
\r
262 /* Check the parameters */
\r
263 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
264 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
265 if (NewState != DISABLE)
\r
267 /* Enable software trigger for the selected DAC channel */
\r
268 DAC->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4);
\r
272 /* Disable software trigger for the selected DAC channel */
\r
273 DAC->SWTRIGR &= ~((uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4));
\r
278 * @brief Enables or disables simultaneously the two DAC channels software
\r
280 * @param NewState: new state of the DAC channels software triggers.
\r
281 * This parameter can be: ENABLE or DISABLE.
\r
284 void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)
\r
286 /* Check the parameters */
\r
287 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
288 if (NewState != DISABLE)
\r
290 /* Enable software trigger for both DAC channels */
\r
291 DAC->SWTRIGR |= DUAL_SWTRIG_SET ;
\r
295 /* Disable software trigger for both DAC channels */
\r
296 DAC->SWTRIGR &= DUAL_SWTRIG_RESET;
\r
301 * @brief Enables or disables the selected DAC channel wave generation.
\r
302 * @param DAC_Channel: the selected DAC channel.
\r
303 * This parameter can be one of the following values:
\r
304 * @arg DAC_Channel_1: DAC Channel1 selected
\r
305 * @arg DAC_Channel_2: DAC Channel2 selected
\r
306 * @param DAC_Wave: Specifies the wave type to enable or disable.
\r
307 * This parameter can be one of the following values:
\r
308 * @arg DAC_Wave_Noise: noise wave generation
\r
309 * @arg DAC_Wave_Triangle: triangle wave generation
\r
310 * @param NewState: new state of the selected DAC channel wave generation.
\r
311 * This parameter can be: ENABLE or DISABLE.
\r
314 void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState)
\r
316 /* Check the parameters */
\r
317 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
318 assert_param(IS_DAC_WAVE(DAC_Wave));
\r
319 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
320 if (NewState != DISABLE)
\r
322 /* Enable the selected wave generation for the selected DAC channel */
\r
323 DAC->CR |= DAC_Wave << DAC_Channel;
\r
327 /* Disable the selected wave generation for the selected DAC channel */
\r
328 DAC->CR &= ~(DAC_Wave << DAC_Channel);
\r
333 * @brief Set the specified data holding register value for DAC channel1.
\r
334 * @param DAC_Align: Specifies the data alignment for DAC channel1.
\r
335 * This parameter can be one of the following values:
\r
336 * @arg DAC_Align_8b_R: 8bit right data alignment selected
\r
337 * @arg DAC_Align_12b_L: 12bit left data alignment selected
\r
338 * @arg DAC_Align_12b_R: 12bit right data alignment selected
\r
339 * @param Data : Data to be loaded in the selected data holding register.
\r
342 void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)
\r
344 __IO uint32_t tmp = 0;
\r
346 /* Check the parameters */
\r
347 assert_param(IS_DAC_ALIGN(DAC_Align));
\r
348 assert_param(IS_DAC_DATA(Data));
\r
350 tmp = (uint32_t)DAC_BASE;
\r
351 tmp += DHR12R1_OFFSET + DAC_Align;
\r
353 /* Set the DAC channel1 selected data holding register */
\r
354 *(__IO uint32_t *) tmp = Data;
\r
358 * @brief Set the specified data holding register value for DAC channel2.
\r
359 * @param DAC_Align: Specifies the data alignment for DAC channel2.
\r
360 * This parameter can be one of the following values:
\r
361 * @arg DAC_Align_8b_R: 8bit right data alignment selected
\r
362 * @arg DAC_Align_12b_L: 12bit left data alignment selected
\r
363 * @arg DAC_Align_12b_R: 12bit right data alignment selected
\r
364 * @param Data : Data to be loaded in the selected data holding register.
\r
367 void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data)
\r
369 __IO uint32_t tmp = 0;
\r
371 /* Check the parameters */
\r
372 assert_param(IS_DAC_ALIGN(DAC_Align));
\r
373 assert_param(IS_DAC_DATA(Data));
\r
375 tmp = (uint32_t)DAC_BASE;
\r
376 tmp += DHR12R2_OFFSET + DAC_Align;
\r
378 /* Set the DAC channel2 selected data holding register */
\r
379 *(__IO uint32_t *)tmp = Data;
\r
383 * @brief Set the specified data holding register value for dual channel
\r
385 * @param DAC_Align: Specifies the data alignment for dual channel DAC.
\r
386 * This parameter can be one of the following values:
\r
387 * @arg DAC_Align_8b_R: 8bit right data alignment selected
\r
388 * @arg DAC_Align_12b_L: 12bit left data alignment selected
\r
389 * @arg DAC_Align_12b_R: 12bit right data alignment selected
\r
390 * @param Data2: Data for DAC Channel2 to be loaded in the selected data
\r
391 * holding register.
\r
392 * @param Data1: Data for DAC Channel1 to be loaded in the selected data
\r
393 * holding register.
\r
396 void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1)
\r
398 uint32_t data = 0, tmp = 0;
\r
400 /* Check the parameters */
\r
401 assert_param(IS_DAC_ALIGN(DAC_Align));
\r
402 assert_param(IS_DAC_DATA(Data1));
\r
403 assert_param(IS_DAC_DATA(Data2));
\r
405 /* Calculate and set dual DAC data holding register value */
\r
406 if (DAC_Align == DAC_Align_8b_R)
\r
408 data = ((uint32_t)Data2 << 8) | Data1;
\r
412 data = ((uint32_t)Data2 << 16) | Data1;
\r
415 tmp = (uint32_t)DAC_BASE;
\r
416 tmp += DHR12RD_OFFSET + DAC_Align;
\r
418 /* Set the dual DAC selected data holding register */
\r
419 *(__IO uint32_t *)tmp = data;
\r
423 * @brief Returns the last data output value of the selected DAC channel.
\r
424 * @param DAC_Channel: the selected DAC channel.
\r
425 * This parameter can be one of the following values:
\r
426 * @arg DAC_Channel_1: DAC Channel1 selected
\r
427 * @arg DAC_Channel_2: DAC Channel2 selected
\r
428 * @retval The selected DAC channel data output value.
\r
430 uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)
\r
432 __IO uint32_t tmp = 0;
\r
434 /* Check the parameters */
\r
435 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
437 tmp = (uint32_t) DAC_BASE ;
\r
438 tmp += DOR_OFFSET + ((uint32_t)DAC_Channel >> 2);
\r
440 /* Returns the DAC channel data output register value */
\r
441 return (uint16_t) (*(__IO uint32_t*) tmp);
\r
444 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
\r
446 * @brief Checks whether the specified DAC flag is set or not.
\r
447 * @param DAC_Channel: thee selected DAC channel.
\r
448 * This parameter can be one of the following values:
\r
449 * @arg DAC_Channel_1: DAC Channel1 selected
\r
450 * @arg DAC_Channel_2: DAC Channel2 selected
\r
451 * @param DAC_FLAG: specifies the flag to check.
\r
452 * This parameter can be only of the following value:
\r
453 * @arg DAC_FLAG_DMAUDR: DMA underrun flag
\r
454 * @retval The new state of DAC_FLAG (SET or RESET).
\r
456 FlagStatus DAC_GetFlagStatus(uint32_t DAC_Channel, uint32_t DAC_FLAG)
\r
458 FlagStatus bitstatus = RESET;
\r
459 /* Check the parameters */
\r
460 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
461 assert_param(IS_DAC_FLAG(DAC_FLAG));
\r
463 /* Check the status of the specified DAC flag */
\r
464 if ((DAC->SR & (DAC_FLAG << DAC_Channel)) != (uint8_t)RESET)
\r
466 /* DAC_FLAG is set */
\r
471 /* DAC_FLAG is reset */
\r
474 /* Return the DAC_FLAG status */
\r
479 * @brief Clears the DAC channelx's pending flags.
\r
480 * @param DAC_Channel: the selected DAC channel.
\r
481 * This parameter can be one of the following values:
\r
482 * @arg DAC_Channel_1: DAC Channel1 selected
\r
483 * @arg DAC_Channel_2: DAC Channel2 selected
\r
484 * @param DAC_FLAG: specifies the flag to clear.
\r
485 * This parameter can be of the following value:
\r
486 * @arg DAC_FLAG_DMAUDR: DMA underrun flag
\r
489 void DAC_ClearFlag(uint32_t DAC_Channel, uint32_t DAC_FLAG)
\r
491 /* Check the parameters */
\r
492 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
493 assert_param(IS_DAC_FLAG(DAC_FLAG));
\r
495 /* Clear the selected DAC flags */
\r
496 DAC->SR = (DAC_FLAG << DAC_Channel);
\r
500 * @brief Checks whether the specified DAC interrupt has occurred or not.
\r
501 * @param DAC_Channel: the selected DAC channel.
\r
502 * This parameter can be one of the following values:
\r
503 * @arg DAC_Channel_1: DAC Channel1 selected
\r
504 * @arg DAC_Channel_2: DAC Channel2 selected
\r
505 * @param DAC_IT: specifies the DAC interrupt source to check.
\r
506 * This parameter can be the following values:
\r
507 * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
\r
508 * @retval The new state of DAC_IT (SET or RESET).
\r
510 ITStatus DAC_GetITStatus(uint32_t DAC_Channel, uint32_t DAC_IT)
\r
512 ITStatus bitstatus = RESET;
\r
513 uint32_t enablestatus = 0;
\r
515 /* Check the parameters */
\r
516 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
517 assert_param(IS_DAC_IT(DAC_IT));
\r
519 /* Get the DAC_IT enable bit status */
\r
520 enablestatus = (DAC->CR & (DAC_IT << DAC_Channel)) ;
\r
522 /* Check the status of the specified DAC interrupt */
\r
523 if (((DAC->SR & (DAC_IT << DAC_Channel)) != (uint32_t)RESET) && enablestatus)
\r
525 /* DAC_IT is set */
\r
530 /* DAC_IT is reset */
\r
533 /* Return the DAC_IT status */
\r
538 * @brief Clears the DAC channelx's interrupt pending bits.
\r
539 * @param DAC_Channel: the selected DAC channel.
\r
540 * This parameter can be one of the following values:
\r
541 * @arg DAC_Channel_1: DAC Channel1 selected
\r
542 * @arg DAC_Channel_2: DAC Channel2 selected
\r
543 * @param DAC_IT: specifies the DAC interrupt pending bit to clear.
\r
544 * This parameter can be the following values:
\r
545 * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
\r
548 void DAC_ClearITPendingBit(uint32_t DAC_Channel, uint32_t DAC_IT)
\r
550 /* Check the parameters */
\r
551 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
552 assert_param(IS_DAC_IT(DAC_IT));
\r
554 /* Clear the selected DAC interrupt pending bits */
\r
555 DAC->SR = (DAC_IT << DAC_Channel);
\r
571 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
\r