2 ******************************************************************************
\r
3 * @file stm32f10x_dac.c
\r
4 * @author MCD Application Team
\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 2010 STMicroelectronics</center></h2>
\r
21 /* Includes ------------------------------------------------------------------*/
\r
22 #include "stm32f10x_dac.h"
\r
23 #include "stm32f10x_rcc.h"
\r
25 /** @addtogroup STM32F10x_StdPeriph_Driver
\r
30 * @brief DAC driver modules
\r
34 /** @defgroup DAC_Private_TypesDefinitions
\r
42 /** @defgroup DAC_Private_Defines
\r
47 #define CR_EN_Set ((uint32_t)0x00000001)
\r
49 /* DAC DMAEN mask */
\r
50 #define CR_DMAEN_Set ((uint32_t)0x00001000)
\r
52 /* CR register Mask */
\r
53 #define CR_CLEAR_Mask ((uint32_t)0x00000FFE)
\r
55 /* DAC SWTRIG mask */
\r
56 #define SWTRIGR_SWTRIG_Set ((uint32_t)0x00000001)
\r
58 /* DAC Dual Channels SWTRIG masks */
\r
59 #define DUAL_SWTRIG_Set ((uint32_t)0x00000003)
\r
60 #define DUAL_SWTRIG_Reset ((uint32_t)0xFFFFFFFC)
\r
62 /* DHR registers offsets */
\r
63 #define DHR12R1_Offset ((uint32_t)0x00000008)
\r
64 #define DHR12R2_Offset ((uint32_t)0x00000014)
\r
65 #define DHR12RD_Offset ((uint32_t)0x00000020)
\r
67 /* DOR register offset */
\r
68 #define DOR_Offset ((uint32_t)0x0000002C)
\r
73 /** @defgroup DAC_Private_Macros
\r
81 /** @defgroup DAC_Private_Variables
\r
89 /** @defgroup DAC_Private_FunctionPrototypes
\r
97 /** @defgroup DAC_Private_Functions
\r
102 * @brief Deinitializes the DAC peripheral registers to their default reset values.
\r
106 void DAC_DeInit(void)
\r
108 /* Enable DAC reset state */
\r
109 RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);
\r
110 /* Release DAC from reset state */
\r
111 RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);
\r
115 * @brief Initializes the DAC peripheral according to the specified
\r
116 * parameters in the DAC_InitStruct.
\r
117 * @param DAC_Channel: the selected DAC channel.
\r
118 * This parameter can be one of the following values:
\r
119 * @arg DAC_Channel_1: DAC Channel1 selected
\r
120 * @arg DAC_Channel_2: DAC Channel2 selected
\r
121 * @param DAC_InitStruct: pointer to a DAC_InitTypeDef structure that
\r
122 * contains the configuration information for the specified DAC channel.
\r
125 void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct)
\r
127 uint32_t tmpreg1 = 0, tmpreg2 = 0;
\r
128 /* Check the DAC parameters */
\r
129 assert_param(IS_DAC_TRIGGER(DAC_InitStruct->DAC_Trigger));
\r
130 assert_param(IS_DAC_GENERATE_WAVE(DAC_InitStruct->DAC_WaveGeneration));
\r
131 assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude));
\r
132 assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->DAC_OutputBuffer));
\r
133 /*---------------------------- DAC CR Configuration --------------------------*/
\r
134 /* Get the DAC CR value */
\r
136 /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
\r
137 tmpreg1 &= ~(CR_CLEAR_Mask << DAC_Channel);
\r
138 /* Configure for the selected DAC channel: buffer output, trigger, wave genration,
\r
139 mask/amplitude for wave genration */
\r
140 /* Set TSELx and TENx bits according to DAC_Trigger value */
\r
141 /* Set WAVEx bits according to DAC_WaveGeneration value */
\r
142 /* Set MAMPx bits according to DAC_LFSRUnmask_TriangleAmplitude value */
\r
143 /* Set BOFFx bit according to DAC_OutputBuffer value */
\r
144 tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration |
\r
145 DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | DAC_InitStruct->DAC_OutputBuffer);
\r
146 /* Calculate CR register value depending on DAC_Channel */
\r
147 tmpreg1 |= tmpreg2 << DAC_Channel;
\r
148 /* Write to DAC CR */
\r
153 * @brief Fills each DAC_InitStruct member with its default value.
\r
154 * @param DAC_InitStruct : pointer to a DAC_InitTypeDef structure which will
\r
158 void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct)
\r
160 /*--------------- Reset DAC init structure parameters values -----------------*/
\r
161 /* Initialize the DAC_Trigger member */
\r
162 DAC_InitStruct->DAC_Trigger = DAC_Trigger_None;
\r
163 /* Initialize the DAC_WaveGeneration member */
\r
164 DAC_InitStruct->DAC_WaveGeneration = DAC_WaveGeneration_None;
\r
165 /* Initialize the DAC_LFSRUnmask_TriangleAmplitude member */
\r
166 DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
\r
167 /* Initialize the DAC_OutputBuffer member */
\r
168 DAC_InitStruct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;
\r
172 * @brief Enables or disables the specified DAC channel.
\r
173 * @param DAC_Channel: the selected DAC channel.
\r
174 * This parameter can be one of the following values:
\r
175 * @arg DAC_Channel_1: DAC Channel1 selected
\r
176 * @arg DAC_Channel_2: DAC Channel2 selected
\r
177 * @param NewState: new state of the DAC channel.
\r
178 * This parameter can be: ENABLE or DISABLE.
\r
181 void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState)
\r
183 /* Check the parameters */
\r
184 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
185 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
186 if (NewState != DISABLE)
\r
188 /* Enable the selected DAC channel */
\r
189 DAC->CR |= CR_EN_Set << DAC_Channel;
\r
193 /* Disable the selected DAC channel */
\r
194 DAC->CR &= ~(CR_EN_Set << DAC_Channel);
\r
197 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL)
\r
199 * @brief Enables or disables the specified DAC interrupts.
\r
200 * @param DAC_Channel: the selected DAC channel.
\r
201 * This parameter can be one of the following values:
\r
202 * @arg DAC_Channel_1: DAC Channel1 selected
\r
203 * @arg DAC_Channel_2: DAC Channel2 selected
\r
204 * @param DAC_IT: specifies the DAC interrupt sources to be enabled or disabled.
\r
205 * This parameter can be the following values:
\r
206 * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
\r
207 * @param NewState: new state of the specified DAC interrupts.
\r
208 * This parameter can be: ENABLE or DISABLE.
\r
211 void DAC_ITConfig(uint32_t DAC_Channel, uint32_t DAC_IT, FunctionalState NewState)
\r
213 /* Check the parameters */
\r
214 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
215 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
216 assert_param(IS_DAC_IT(DAC_IT));
\r
218 if (NewState != DISABLE)
\r
220 /* Enable the selected DAC interrupts */
\r
221 DAC->CR |= (DAC_IT << DAC_Channel);
\r
225 /* Disable the selected DAC interrupts */
\r
226 DAC->CR &= (~(uint32_t)(DAC_IT << DAC_Channel));
\r
232 * @brief Enables or disables the specified DAC channel DMA request.
\r
233 * @param DAC_Channel: the selected DAC channel.
\r
234 * This parameter can be one of the following values:
\r
235 * @arg DAC_Channel_1: DAC Channel1 selected
\r
236 * @arg DAC_Channel_2: DAC Channel2 selected
\r
237 * @param NewState: new state of the selected DAC channel DMA request.
\r
238 * This parameter can be: ENABLE or DISABLE.
\r
241 void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState)
\r
243 /* Check the parameters */
\r
244 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
245 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
246 if (NewState != DISABLE)
\r
248 /* Enable the selected DAC channel DMA request */
\r
249 DAC->CR |= CR_DMAEN_Set << DAC_Channel;
\r
253 /* Disable the selected DAC channel DMA request */
\r
254 DAC->CR &= ~(CR_DMAEN_Set << DAC_Channel);
\r
259 * @brief Enables or disables the selected DAC channel software trigger.
\r
260 * @param DAC_Channel: the selected DAC channel.
\r
261 * This parameter can be one of the following values:
\r
262 * @arg DAC_Channel_1: DAC Channel1 selected
\r
263 * @arg DAC_Channel_2: DAC Channel2 selected
\r
264 * @param NewState: new state of the selected DAC channel software trigger.
\r
265 * This parameter can be: ENABLE or DISABLE.
\r
268 void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState)
\r
270 /* Check the parameters */
\r
271 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
272 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
273 if (NewState != DISABLE)
\r
275 /* Enable software trigger for the selected DAC channel */
\r
276 DAC->SWTRIGR |= SWTRIGR_SWTRIG_Set << (DAC_Channel >> 4);
\r
280 /* Disable software trigger for the selected DAC channel */
\r
281 DAC->SWTRIGR &= ~(SWTRIGR_SWTRIG_Set << (DAC_Channel >> 4));
\r
286 * @brief Enables or disables simultaneously the two DAC channels software
\r
288 * @param NewState: new state of the DAC channels software triggers.
\r
289 * This parameter can be: ENABLE or DISABLE.
\r
292 void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)
\r
294 /* Check the parameters */
\r
295 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
296 if (NewState != DISABLE)
\r
298 /* Enable software trigger for both DAC channels */
\r
299 DAC->SWTRIGR |= DUAL_SWTRIG_Set ;
\r
303 /* Disable software trigger for both DAC channels */
\r
304 DAC->SWTRIGR &= DUAL_SWTRIG_Reset;
\r
309 * @brief Enables or disables the selected DAC channel wave generation.
\r
310 * @param DAC_Channel: the selected DAC channel.
\r
311 * This parameter can be one of the following values:
\r
312 * @arg DAC_Channel_1: DAC Channel1 selected
\r
313 * @arg DAC_Channel_2: DAC Channel2 selected
\r
314 * @param DAC_Wave: Specifies the wave type to enable or disable.
\r
315 * This parameter can be one of the following values:
\r
316 * @arg DAC_Wave_Noise: noise wave generation
\r
317 * @arg DAC_Wave_Triangle: triangle wave generation
\r
318 * @param NewState: new state of the selected DAC channel wave generation.
\r
319 * This parameter can be: ENABLE or DISABLE.
\r
322 void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState)
\r
324 /* Check the parameters */
\r
325 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
326 assert_param(IS_DAC_WAVE(DAC_Wave));
\r
327 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
328 if (NewState != DISABLE)
\r
330 /* Enable the selected wave generation for the selected DAC channel */
\r
331 DAC->CR |= DAC_Wave << DAC_Channel;
\r
335 /* Disable the selected wave generation for the selected DAC channel */
\r
336 DAC->CR &= ~(DAC_Wave << DAC_Channel);
\r
341 * @brief Set the specified data holding register value for DAC channel1.
\r
342 * @param DAC_Align: Specifies the data alignement for DAC channel1.
\r
343 * This parameter can be one of the following values:
\r
344 * @arg DAC_Align_8b_R: 8bit right data alignement selected
\r
345 * @arg DAC_Align_12b_L: 12bit left data alignement selected
\r
346 * @arg DAC_Align_12b_R: 12bit right data alignement selected
\r
347 * @param Data : Data to be loaded in the selected data holding register.
\r
350 void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)
\r
352 __IO uint32_t tmp = 0;
\r
354 /* Check the parameters */
\r
355 assert_param(IS_DAC_ALIGN(DAC_Align));
\r
356 assert_param(IS_DAC_DATA(Data));
\r
358 tmp = (uint32_t)DAC_BASE;
\r
359 tmp += DHR12R1_Offset + DAC_Align;
\r
361 /* Set the DAC channel1 selected data holding register */
\r
362 *(__IO uint32_t *) tmp = Data;
\r
366 * @brief Set the specified data holding register value for DAC channel2.
\r
367 * @param DAC_Align: Specifies the data alignement for DAC channel2.
\r
368 * This parameter can be one of the following values:
\r
369 * @arg DAC_Align_8b_R: 8bit right data alignement selected
\r
370 * @arg DAC_Align_12b_L: 12bit left data alignement selected
\r
371 * @arg DAC_Align_12b_R: 12bit right data alignement selected
\r
372 * @param Data : Data to be loaded in the selected data holding register.
\r
375 void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data)
\r
377 __IO uint32_t tmp = 0;
\r
379 /* Check the parameters */
\r
380 assert_param(IS_DAC_ALIGN(DAC_Align));
\r
381 assert_param(IS_DAC_DATA(Data));
\r
383 tmp = (uint32_t)DAC_BASE;
\r
384 tmp += DHR12R2_Offset + DAC_Align;
\r
386 /* Set the DAC channel2 selected data holding register */
\r
387 *(__IO uint32_t *)tmp = Data;
\r
391 * @brief Set the specified data holding register value for dual channel
\r
393 * @param DAC_Align: Specifies the data alignement for dual channel DAC.
\r
394 * This parameter can be one of the following values:
\r
395 * @arg DAC_Align_8b_R: 8bit right data alignement selected
\r
396 * @arg DAC_Align_12b_L: 12bit left data alignement selected
\r
397 * @arg DAC_Align_12b_R: 12bit right data alignement selected
\r
398 * @param Data2: Data for DAC Channel2 to be loaded in the selected data
\r
399 * holding register.
\r
400 * @param Data1: Data for DAC Channel1 to be loaded in the selected data
\r
401 * holding register.
\r
404 void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1)
\r
406 uint32_t data = 0, tmp = 0;
\r
408 /* Check the parameters */
\r
409 assert_param(IS_DAC_ALIGN(DAC_Align));
\r
410 assert_param(IS_DAC_DATA(Data1));
\r
411 assert_param(IS_DAC_DATA(Data2));
\r
413 /* Calculate and set dual DAC data holding register value */
\r
414 if (DAC_Align == DAC_Align_8b_R)
\r
416 data = ((uint32_t)Data2 << 8) | Data1;
\r
420 data = ((uint32_t)Data2 << 16) | Data1;
\r
423 tmp = (uint32_t)DAC_BASE;
\r
424 tmp += DHR12RD_Offset + DAC_Align;
\r
426 /* Set the dual DAC selected data holding register */
\r
427 *(__IO uint32_t *)tmp = data;
\r
431 * @brief Returns the last data output value of the selected DAC cahnnel.
\r
432 * @param DAC_Channel: the selected DAC channel.
\r
433 * This parameter can be one of the following values:
\r
434 * @arg DAC_Channel_1: DAC Channel1 selected
\r
435 * @arg DAC_Channel_2: DAC Channel2 selected
\r
436 * @retval The selected DAC channel data output value.
\r
438 uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)
\r
440 __IO uint32_t tmp = 0;
\r
442 /* Check the parameters */
\r
443 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
445 tmp = (uint32_t) DAC_BASE ;
\r
446 tmp += DOR_Offset + ((uint32_t)DAC_Channel >> 2);
\r
448 /* Returns the DAC channel data output register value */
\r
449 return (uint16_t) (*(__IO uint32_t*) tmp);
\r
452 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL)
\r
454 * @brief Checks whether the specified DAC flag is set or not.
\r
455 * @param DAC_Channel: thee selected DAC channel.
\r
456 * This parameter can be one of the following values:
\r
457 * @arg DAC_Channel_1: DAC Channel1 selected
\r
458 * @arg DAC_Channel_2: DAC Channel2 selected
\r
459 * @param DAC_FLAG: specifies the flag to check.
\r
460 * This parameter can be only of the following value:
\r
461 * @arg DAC_FLAG_DMAUDR: DMA underrun flag
\r
462 * @retval The new state of DAC_FLAG (SET or RESET).
\r
464 FlagStatus DAC_GetFlagStatus(uint32_t DAC_Channel, uint32_t DAC_FLAG)
\r
466 FlagStatus bitstatus = RESET;
\r
467 /* Check the parameters */
\r
468 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
469 assert_param(IS_DAC_FLAG(DAC_FLAG));
\r
471 /* Check the status of the specified DAC flag */
\r
472 if ((DAC->SR & (DAC_FLAG << DAC_Channel)) != (uint8_t)RESET)
\r
474 /* DAC_FLAG is set */
\r
479 /* DAC_FLAG is reset */
\r
482 /* Return the DAC_FLAG status */
\r
487 * @brief Clears the DAC channelx's pending flags.
\r
488 * @param DAC_Channel: the selected DAC channel.
\r
489 * This parameter can be one of the following values:
\r
490 * @arg DAC_Channel_1: DAC Channel1 selected
\r
491 * @arg DAC_Channel_2: DAC Channel2 selected
\r
492 * @param DAC_FLAG: specifies the flag to clear.
\r
493 * This parameter can be of the following value:
\r
494 * @arg DAC_FLAG_DMAUDR: DMA underrun flag
\r
497 void DAC_ClearFlag(uint32_t DAC_Channel, uint32_t DAC_FLAG)
\r
499 /* Check the parameters */
\r
500 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
501 assert_param(IS_DAC_FLAG(DAC_FLAG));
\r
503 /* Clear the selected DAC flags */
\r
504 DAC->SR = (DAC_FLAG << DAC_Channel);
\r
508 * @brief Checks whether the specified DAC interrupt has occurred or not.
\r
509 * @param DAC_Channel: the selected DAC channel.
\r
510 * This parameter can be one of the following values:
\r
511 * @arg DAC_Channel_1: DAC Channel1 selected
\r
512 * @arg DAC_Channel_2: DAC Channel2 selected
\r
513 * @param DAC_IT: specifies the DAC interrupt source to check.
\r
514 * This parameter can be the following values:
\r
515 * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
\r
516 * @retval The new state of DAC_IT (SET or RESET).
\r
518 ITStatus DAC_GetITStatus(uint32_t DAC_Channel, uint32_t DAC_IT)
\r
520 ITStatus bitstatus = RESET;
\r
521 uint32_t enablestatus = 0;
\r
523 /* Check the parameters */
\r
524 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
525 assert_param(IS_DAC_IT(DAC_IT));
\r
527 /* Get the DAC_IT enable bit status */
\r
528 enablestatus = (DAC->CR & (DAC_IT << DAC_Channel)) ;
\r
530 /* Check the status of the specified DAC interrupt */
\r
531 if (((DAC->SR & (DAC_IT << DAC_Channel)) != (uint32_t)RESET) && enablestatus)
\r
533 /* DAC_IT is set */
\r
538 /* DAC_IT is reset */
\r
541 /* Return the DAC_IT status */
\r
546 * @brief Clears the DAC channelx
\92s interrupt pending bits.
\r
547 * @param DAC_Channel: the selected DAC channel.
\r
548 * This parameter can be one of the following values:
\r
549 * @arg DAC_Channel_1: DAC Channel1 selected
\r
550 * @arg DAC_Channel_2: DAC Channel2 selected
\r
551 * @param DAC_IT: specifies the DAC interrupt pending bit to clear.
\r
552 * This parameter can be the following values:
\r
553 * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
\r
556 void DAC_ClearITPendingBit(uint32_t DAC_Channel, uint32_t DAC_IT)
\r
558 /* Check the parameters */
\r
559 assert_param(IS_DAC_CHANNEL(DAC_Channel));
\r
560 assert_param(IS_DAC_IT(DAC_IT));
\r
562 /* Clear the selected DAC interrupt pending bits */
\r
563 DAC->SR = (DAC_IT << DAC_Channel);
\r
579 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r