Merge pull request #93 from zyp/master
[fw/stlink] / example / libs_stm / src / stm32l1xx / stm32l1xx_dac.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_dac.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    31-December-2010\r
7   * @brief   This file provides firmware functions to manage the following \r
8   *          functionalities of the Digital-to-Analog Converter (DAC) peripheral: \r
9   *           - DAC channels configuration: trigger, output buffer, data format\r
10   *           - DMA management      \r
11   *           - Interrupts and flags management\r
12   *\r
13   *  @verbatim\r
14   *    \r
15   *          ===================================================================\r
16   *                             DAC Peripheral features\r
17   *          ===================================================================\r
18   *          The device integrates two 12-bit Digital Analog Converters that can \r
19   *          be used independently or simultaneously (dual mode):\r
20   *            1- DAC channel1 with DAC_OUT1 (PA4) as output\r
21   *            1- DAC channel2 with DAC_OUT2 (PA5) as output\r
22   *\r
23   *          Digital to Analog conversion can be non-triggered using DAC_Trigger_None\r
24   *          and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register using \r
25   *          DAC_SetChannel1Data()/DAC_SetChannel2Data.\r
26   *   \r
27   *         Digital to Analog conversion can be triggered by:\r
28   *             1- External event: EXTI Line 9 (any GPIOx_Pin9) using DAC_Trigger_Ext_IT9.\r
29   *                The used pin (GPIOx_Pin9) must be configured in input mode.\r
30   *\r
31   *             2- Timers TRGO: TIM2, TIM4, TIM6, TIM7 and TIM9 \r
32   *                (DAC_Trigger_T2_TRGO, DAC_Trigger_T4_TRGO...)\r
33   *                The timer TRGO event should be selected using TIM_SelectOutputTrigger()\r
34   *\r
35   *             3- Software using DAC_Trigger_Software\r
36   *\r
37   *          Each DAC channel integrates an output buffer that can be used to \r
38   *          reduce the output impedance, and to drive external loads directly\r
39   *          without having to add an external operational amplifier.\r
40   *          To enable, the output buffer use  \r
41   *              DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;\r
42   *          \r
43   *          Refer to the device datasheet for more details about output impedance\r
44   *          value with and without output buffer.\r
45   *\r
46   *          Both DAC channels can be used to generate\r
47   *             1- Noise wave using DAC_WaveGeneration_Noise\r
48   *             2- Triangle wave using DAC_WaveGeneration_Triangle\r
49   *        \r
50   *          Wave generation can be disabled using DAC_WaveGeneration_None\r
51   *\r
52   *          The DAC data format can be:\r
53   *             1- 8-bit right alignment using DAC_Align_8b_R\r
54   *             2- 12-bit left alignment using DAC_Align_12b_L\r
55   *             3- 12-bit right alignment using DAC_Align_12b_R\r
56   *\r
57   *          The analog output voltage on each DAC channel pin is determined\r
58   *          by the following equation: DAC_OUTx = VREF+ * DOR / 4095\r
59   *             with  DOR is the Data Output Register\r
60   *                   VEF+ is the input voltage reference (refer to the device datasheet)\r
61   *          e.g. To set DAC_OUT1 to 0.7V, use\r
62   *            DAC_SetChannel1Data(DAC_Align_12b_R, 868);\r
63   *          Assuming that VREF+ = 3.3, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V\r
64   *\r
65   *          A DMA1 request can be generated when an external trigger (but not\r
66   *          a software trigger) occurs if DMA1 requests are enabled using\r
67   *          DAC_DMACmd()\r
68   *          DMA1 requests are mapped as following:\r
69   *             1- DAC channel1 is mapped on DMA1 channel3 which must be already \r
70   *                configured\r
71   *             2- DAC channel2 is mapped on DMA1 channel4 which must be already \r
72   *                configured\r
73   *\r
74   *          ===================================================================      \r
75   *                              How to use this driver \r
76   *          ===================================================================          \r
77   *            - DAC APB clock must be enabled to get write access to DAC\r
78   *              registers using\r
79   *              RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE)\r
80   *            - Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.\r
81   *            - Configure the DAC channel using DAC_Init()\r
82   *            - Enable the DAC channel using DAC_Cmd()\r
83   * \r
84   *  @endverbatim\r
85   *    \r
86   ******************************************************************************\r
87   * @attention\r
88   *\r
89   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
90   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
91   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
92   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
93   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
94   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
95   *\r
96   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
97   ******************************************************************************  \r
98   */ \r
99 \r
100 /* Includes ------------------------------------------------------------------*/\r
101 #include "stm32l1xx_dac.h"\r
102 #include "stm32l1xx_rcc.h"\r
103 \r
104 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
105   * @{\r
106   */\r
107 \r
108 /** @defgroup DAC \r
109   * @brief DAC driver modules\r
110   * @{\r
111   */ \r
112 \r
113 /* Private typedef -----------------------------------------------------------*/\r
114 /* Private define ------------------------------------------------------------*/\r
115 /* CR register Mask */\r
116 #define CR_CLEAR_MASK              ((uint32_t)0x00000FFE)\r
117 \r
118 /* DAC Dual Channels SWTRIG masks */\r
119 #define DUAL_SWTRIG_SET            ((uint32_t)0x00000003)\r
120 #define DUAL_SWTRIG_RESET          ((uint32_t)0xFFFFFFFC)\r
121 \r
122 /* DHR registers offsets */\r
123 #define DHR12R1_OFFSET             ((uint32_t)0x00000008)\r
124 #define DHR12R2_OFFSET             ((uint32_t)0x00000014)\r
125 #define DHR12RD_OFFSET             ((uint32_t)0x00000020)\r
126 \r
127 /* DOR register offset */\r
128 #define DOR_OFFSET                 ((uint32_t)0x0000002C)\r
129 \r
130 /* Private macro -------------------------------------------------------------*/\r
131 /* Private variables ---------------------------------------------------------*/\r
132 /* Private function prototypes -----------------------------------------------*/\r
133 /* Private functions ---------------------------------------------------------*/\r
134 \r
135 /** @defgroup DAC_Private_Functions\r
136   * @{\r
137   */ \r
138 \r
139 /** @defgroup DAC_Group1 DAC channels configuration\r
140  *  @brief   DAC channels configuration: trigger, output buffer, data format \r
141  *\r
142 @verbatim   \r
143  ===============================================================================\r
144           DAC channels configuration: trigger, output buffer, data format\r
145  ===============================================================================  \r
146 \r
147 @endverbatim\r
148   * @{\r
149   */\r
150 \r
151 /**\r
152   * @brief  Deinitializes the DAC peripheral registers to their default reset values.\r
153   * @param  None\r
154   * @retval None\r
155   */\r
156 void DAC_DeInit(void)\r
157 {\r
158   /* Enable DAC reset state */\r
159   RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);\r
160   /* Release DAC from reset state */\r
161   RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);\r
162 }\r
163 \r
164 /**\r
165   * @brief  Initializes the DAC peripheral according to the specified \r
166   *         parameters in the DAC_InitStruct.\r
167   * @param  DAC_Channel: the selected DAC channel. \r
168   *   This parameter can be one of the following values:\r
169   *     @arg DAC_Channel_1: DAC Channel1 selected\r
170   *     @arg DAC_Channel_2: DAC Channel2 selected\r
171   * @param  DAC_InitStruct: pointer to a DAC_InitTypeDef structure that\r
172   *         contains the configuration information for the specified DAC channel.\r
173   *         DAC_Trigger selects the trigger source: EXTI Line 9, TIM2, TIM4....\r
174   *         DAC_WaveGeneration selects the waveform to be generated: noise, triangle\r
175   *         DAC_LFSRUnmask_TriangleAmplitude \r
176   *             defines the LFSR when noise waveform is selected by DAC_WaveGeneration\r
177   *             or defines the amplitude of the triangle waveform when it is \r
178   *             selected by DAC_WaveGeneration\r
179   *         DAC_OutputBuffer enables/disables the output buffer on DAC_OUTx\r
180   * @retval None\r
181   */\r
182 void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct)\r
183 {\r
184   uint32_t tmpreg1 = 0, tmpreg2 = 0;\r
185 \r
186   /* Check the DAC parameters */\r
187   assert_param(IS_DAC_TRIGGER(DAC_InitStruct->DAC_Trigger));\r
188   assert_param(IS_DAC_GENERATE_WAVE(DAC_InitStruct->DAC_WaveGeneration));\r
189   assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude));\r
190   assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->DAC_OutputBuffer));\r
191 \r
192 /*---------------------------- DAC CR Configuration --------------------------*/\r
193   /* Get the DAC CR value */\r
194   tmpreg1 = DAC->CR;\r
195   /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */\r
196   tmpreg1 &= ~(CR_CLEAR_MASK << DAC_Channel);\r
197   /* Configure for the selected DAC channel: buffer output, trigger, wave generation,\r
198      mask/amplitude for wave generation */\r
199   /* Set TSELx and TENx bits according to DAC_Trigger value */\r
200   /* Set WAVEx bits according to DAC_WaveGeneration value */\r
201   /* Set MAMPx bits according to DAC_LFSRUnmask_TriangleAmplitude value */ \r
202   /* Set BOFFx bit according to DAC_OutputBuffer value */   \r
203   tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration |\r
204              DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | DAC_InitStruct->DAC_OutputBuffer);\r
205   /* Calculate CR register value depending on DAC_Channel */\r
206   tmpreg1 |= tmpreg2 << DAC_Channel;\r
207   /* Write to DAC CR */\r
208   DAC->CR = tmpreg1;\r
209 }\r
210 \r
211 /**\r
212   * @brief  Fills each DAC_InitStruct member with its default value.\r
213   * @param  DAC_InitStruct : pointer to a DAC_InitTypeDef structure which will \r
214   *         be initialized.\r
215   * @retval None\r
216   */\r
217 void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct)\r
218 {\r
219 /*--------------- Reset DAC init structure parameters values -----------------*/\r
220   /* Initialize the DAC_Trigger member */\r
221   DAC_InitStruct->DAC_Trigger = DAC_Trigger_None;\r
222   /* Initialize the DAC_WaveGeneration member */\r
223   DAC_InitStruct->DAC_WaveGeneration = DAC_WaveGeneration_None;\r
224   /* Initialize the DAC_LFSRUnmask_TriangleAmplitude member */\r
225   DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;\r
226   /* Initialize the DAC_OutputBuffer member */\r
227   DAC_InitStruct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;\r
228 }\r
229 \r
230 /**\r
231   * @brief  Enables or disables the specified DAC channel.\r
232   * @param  DAC_Channel: The selected DAC channel. \r
233   *   This parameter can be one of the following values:\r
234   *     @arg DAC_Channel_1: DAC Channel1 selected\r
235   *     @arg DAC_Channel_2: DAC Channel2 selected\r
236   * @param  NewState: new state of the DAC channel. \r
237   *      This parameter can be: ENABLE or DISABLE.\r
238   * @note When the DAC channel is enabled the trigger source can no more\r
239   *       be modified.\r
240   * @retval None\r
241   */\r
242 void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState)\r
243 {\r
244   /* Check the parameters */\r
245   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
246   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
247 \r
248   if (NewState != DISABLE)\r
249   {\r
250     /* Enable the selected DAC channel */\r
251     DAC->CR |= (DAC_CR_EN1 << DAC_Channel);\r
252   }\r
253   else\r
254   {\r
255     /* Disable the selected DAC channel */\r
256     DAC->CR &= (~(DAC_CR_EN1 << DAC_Channel));\r
257   }\r
258 }\r
259 \r
260 /**\r
261   * @brief  Enables or disables the selected DAC channel software trigger.\r
262   * @param  DAC_Channel: the selected DAC channel. \r
263   *   This parameter can be one of the following values:\r
264   *     @arg DAC_Channel_1: DAC Channel1 selected\r
265   *     @arg DAC_Channel_2: DAC Channel2 selected\r
266   * @param  NewState: new state of the selected DAC channel software trigger.\r
267   *   This parameter can be: ENABLE or DISABLE.\r
268   * @retval None\r
269   */\r
270 void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState)\r
271 {\r
272   /* Check the parameters */\r
273   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
274   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
275 \r
276   if (NewState != DISABLE)\r
277   {\r
278     /* Enable software trigger for the selected DAC channel */\r
279     DAC->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4);\r
280   }\r
281   else\r
282   {\r
283     /* Disable software trigger for the selected DAC channel */\r
284     DAC->SWTRIGR &= ~((uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4));\r
285   }\r
286 }\r
287 \r
288 /**\r
289   * @brief  Enables or disables simultaneously the two DAC channels software\r
290   *         triggers.\r
291   * @param  NewState: new state of the DAC channels software triggers.\r
292   *   This parameter can be: ENABLE or DISABLE.\r
293   * @retval None\r
294   */\r
295 void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)\r
296 {\r
297   /* Check the parameters */\r
298   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
299 \r
300   if (NewState != DISABLE)\r
301   {\r
302     /* Enable software trigger for both DAC channels */\r
303     DAC->SWTRIGR |= DUAL_SWTRIG_SET;\r
304   }\r
305   else\r
306   {\r
307     /* Disable software trigger for both DAC channels */\r
308     DAC->SWTRIGR &= DUAL_SWTRIG_RESET;\r
309   }\r
310 }\r
311 \r
312 /**\r
313   * @brief  Enables or disables the selected DAC channel wave generation.\r
314   * @param  DAC_Channel: the selected DAC channel. \r
315   *   This parameter can be one of the following values:\r
316   *     @arg DAC_Channel_1: DAC Channel1 selected\r
317   *     @arg DAC_Channel_2: DAC Channel2 selected\r
318   * @param  DAC_Wave: Specifies the wave type to enable or disable.\r
319   *   This parameter can be one of the following values:\r
320   *     @arg DAC_Wave_Noise: noise wave generation\r
321   *     @arg DAC_Wave_Triangle: triangle wave generation\r
322   * @param  NewState: new state of the selected DAC channel wave generation.\r
323   *   This parameter can be: ENABLE or DISABLE.\r
324   * @note   \r
325   * @retval None\r
326   */\r
327 void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState)\r
328 {\r
329   /* Check the parameters */\r
330   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
331   assert_param(IS_DAC_WAVE(DAC_Wave)); \r
332   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
333 \r
334   if (NewState != DISABLE)\r
335   {\r
336     /* Enable the selected wave generation for the selected DAC channel */\r
337     DAC->CR |= DAC_Wave << DAC_Channel;\r
338   }\r
339   else\r
340   {\r
341     /* Disable the selected wave generation for the selected DAC channel */\r
342     DAC->CR &= ~(DAC_Wave << DAC_Channel);\r
343   }\r
344 }\r
345 \r
346 /**\r
347   * @brief  Set the specified data holding register value for DAC channel1.\r
348   * @param  DAC_Align: Specifies the data alignment for DAC channel1.\r
349   *   This parameter can be one of the following values:\r
350   *     @arg DAC_Align_8b_R: 8bit right data alignment selected\r
351   *     @arg DAC_Align_12b_L: 12bit left data alignment selected\r
352   *     @arg DAC_Align_12b_R: 12bit right data alignment selected\r
353   * @param  Data : Data to be loaded in the selected data holding register.\r
354   * @retval None\r
355   */\r
356 void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)\r
357 {  \r
358   __IO uint32_t tmp = 0;\r
359   \r
360   /* Check the parameters */\r
361   assert_param(IS_DAC_ALIGN(DAC_Align));\r
362   assert_param(IS_DAC_DATA(Data));\r
363   \r
364   tmp = (uint32_t)DAC_BASE; \r
365   tmp += DHR12R1_OFFSET + DAC_Align;\r
366 \r
367   /* Set the DAC channel1 selected data holding register */\r
368   *(__IO uint32_t *) tmp = Data;\r
369 }\r
370 \r
371 /**\r
372   * @brief  Set the specified data holding register value for DAC channel2.\r
373   * @param  DAC_Align: Specifies the data alignment for DAC channel2.\r
374   *   This parameter can be one of the following values:\r
375   *     @arg DAC_Align_8b_R: 8bit right data alignment selected\r
376   *     @arg DAC_Align_12b_L: 12bit left data alignment selected\r
377   *     @arg DAC_Align_12b_R: 12bit right data alignment selected\r
378   * @param  Data : Data to be loaded in the selected data holding register.\r
379   * @retval None\r
380   */\r
381 void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data)\r
382 {\r
383   __IO uint32_t tmp = 0;\r
384 \r
385   /* Check the parameters */\r
386   assert_param(IS_DAC_ALIGN(DAC_Align));\r
387   assert_param(IS_DAC_DATA(Data));\r
388   \r
389   tmp = (uint32_t)DAC_BASE;\r
390   tmp += DHR12R2_OFFSET + DAC_Align;\r
391 \r
392   /* Set the DAC channel2 selected data holding register */\r
393   *(__IO uint32_t *)tmp = Data;\r
394 }\r
395 \r
396 /**\r
397   * @brief  Set the specified data holding register value for dual channel DAC.\r
398   * @param  DAC_Align: Specifies the data alignment for dual channel DAC.\r
399   *   This parameter can be one of the following values:\r
400   *     @arg DAC_Align_8b_R: 8bit right data alignment selected\r
401   *     @arg DAC_Align_12b_L: 12bit left data alignment selected\r
402   *     @arg DAC_Align_12b_R: 12bit right data alignment selected\r
403   * @param  Data2: Data for DAC Channel2 to be loaded in the selected data \r
404   *         holding register.\r
405   * @param  Data1: Data for DAC Channel1 to be loaded in the selected data \r
406   *         holding register.\r
407   * @note In dual mode, a unique register access is required to write in both\r
408   *       DAC channels at the same time.\r
409   * @retval None\r
410   */\r
411 void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1)\r
412 {\r
413   uint32_t data = 0, tmp = 0;\r
414   \r
415   /* Check the parameters */\r
416   assert_param(IS_DAC_ALIGN(DAC_Align));\r
417   assert_param(IS_DAC_DATA(Data1));\r
418   assert_param(IS_DAC_DATA(Data2));\r
419   \r
420   /* Calculate and set dual DAC data holding register value */\r
421   if (DAC_Align == DAC_Align_8b_R)\r
422   {\r
423     data = ((uint32_t)Data2 << 8) | Data1; \r
424   }\r
425   else\r
426   {\r
427     data = ((uint32_t)Data2 << 16) | Data1;\r
428   }\r
429   \r
430   tmp = (uint32_t)DAC_BASE;\r
431   tmp += DHR12RD_OFFSET + DAC_Align;\r
432 \r
433   /* Set the dual DAC selected data holding register */\r
434   *(__IO uint32_t *)tmp = data;\r
435 }\r
436 \r
437 /**\r
438   * @brief  Returns the last data output value of the selected DAC channel.\r
439   * @param  DAC_Channel: the selected DAC channel. \r
440   *   This parameter can be one of the following values:\r
441   *     @arg DAC_Channel_1: DAC Channel1 selected\r
442   *     @arg DAC_Channel_2: DAC Channel2 selected\r
443   * @retval The selected DAC channel data output value.\r
444   */\r
445 uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)\r
446 {\r
447   __IO uint32_t tmp = 0;\r
448   \r
449   /* Check the parameters */\r
450   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
451   \r
452   tmp = (uint32_t) DAC_BASE ;\r
453   tmp += DOR_OFFSET + ((uint32_t)DAC_Channel >> 2);\r
454   \r
455   /* Returns the DAC channel data output register value */\r
456   return (uint16_t) (*(__IO uint32_t*) tmp);\r
457 }\r
458 \r
459 /**\r
460   * @}\r
461   */\r
462 \r
463 /** @defgroup DAC_Group2 DMA management functions\r
464  *  @brief   DMA management functions\r
465  *\r
466 @verbatim   \r
467  ===============================================================================\r
468                           DMA management functions\r
469  ===============================================================================  \r
470 \r
471 @endverbatim\r
472   * @{\r
473   */\r
474 \r
475 /**\r
476   * @brief  Enables or disables the specified DAC channel DMA request.\r
477   *         When enabled DMA1 is generated when an external trigger (EXTI Line9,\r
478   *         TIM2, TIM4, TIM6, TIM7 or TIM9  but not a software trigger) occurs\r
479   * @param  DAC_Channel: the selected DAC channel.\r
480   *   This parameter can be one of the following values:\r
481   *     @arg DAC_Channel_1: DAC Channel1 selected\r
482   *     @arg DAC_Channel_2: DAC Channel2 selected\r
483   * @param  NewState: new state of the selected DAC channel DMA request.\r
484   *   This parameter can be: ENABLE or DISABLE.\r
485   *  The DAC channel1 (channel2) is mapped on DMA1 channel3 (channel4) which \r
486   *  must be already configured. \r
487   * @retval None\r
488   */\r
489 void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState)\r
490 {\r
491   /* Check the parameters */\r
492   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
493   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
494 \r
495   if (NewState != DISABLE)\r
496   {\r
497     /* Enable the selected DAC channel DMA request */\r
498     DAC->CR |= (DAC_CR_DMAEN1 << DAC_Channel);\r
499   }\r
500   else\r
501   {\r
502     /* Disable the selected DAC channel DMA request */\r
503     DAC->CR &= (~(DAC_CR_DMAEN1 << DAC_Channel));\r
504   }\r
505 }\r
506 \r
507 /**\r
508   * @}\r
509   */\r
510 \r
511 /** @defgroup DAC_Group3 Interrupts and flags management functions\r
512  *  @brief   Interrupts and flags management functions\r
513  *\r
514 @verbatim   \r
515  ===============================================================================\r
516                    Interrupts and flags management functions\r
517  ===============================================================================  \r
518 \r
519 @endverbatim\r
520   * @{\r
521   */\r
522 \r
523 /**\r
524   * @brief  Enables or disables the specified DAC interrupts.\r
525   * @param  DAC_Channel: the selected DAC channel. \r
526   *   This parameter can be one of the following values:\r
527   *     @arg DAC_Channel_1: DAC Channel1 selected\r
528   *     @arg DAC_Channel_2: DAC Channel2 selected\r
529   * @param  DAC_IT: specifies the DAC interrupt sources to be enabled or disabled. \r
530   *   This parameter can be the following values:\r
531   *     @arg DAC_IT_DMAUDR: DMA underrun interrupt mask\r
532   * @note The DMA underrun occurs when a second external trigger arrives before\r
533   *       the acknowledgement for the first external trigger is received (first request).\r
534   * @param  NewState: new state of the specified DAC interrupts.\r
535   *   This parameter can be: ENABLE or DISABLE.\r
536   * @retval None\r
537   */ \r
538 void DAC_ITConfig(uint32_t DAC_Channel, uint32_t DAC_IT, FunctionalState NewState)  \r
539 {\r
540   /* Check the parameters */\r
541   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
542   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
543   assert_param(IS_DAC_IT(DAC_IT)); \r
544 \r
545   if (NewState != DISABLE)\r
546   {\r
547     /* Enable the selected DAC interrupts */\r
548     DAC->CR |=  (DAC_IT << DAC_Channel);\r
549   }\r
550   else\r
551   {\r
552     /* Disable the selected DAC interrupts */\r
553     DAC->CR &= (~(uint32_t)(DAC_IT << DAC_Channel));\r
554   }\r
555 }\r
556 \r
557 /**\r
558   * @brief  Checks whether the specified DAC flag is set or not.\r
559   * @param  DAC_Channel: thee selected DAC channel. \r
560   *   This parameter can be one of the following values:\r
561   *     @arg DAC_Channel_1: DAC Channel1 selected\r
562   *     @arg DAC_Channel_2: DAC Channel2 selected\r
563   * @param  DAC_FLAG: specifies the flag to check. \r
564   *   This parameter can be only of the following value:\r
565   *     @arg DAC_FLAG_DMAUDR: DMA underrun flag\r
566   * @note The DMA underrun occurs when a second external trigger arrives before\r
567   *       the acknowledgement for the first external trigger is received (first request).\r
568   * @retval The new state of DAC_FLAG (SET or RESET).\r
569   */\r
570 FlagStatus DAC_GetFlagStatus(uint32_t DAC_Channel, uint32_t DAC_FLAG)\r
571 {\r
572   FlagStatus bitstatus = RESET;\r
573   /* Check the parameters */\r
574   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
575   assert_param(IS_DAC_FLAG(DAC_FLAG));\r
576 \r
577   /* Check the status of the specified DAC flag */\r
578   if ((DAC->SR & (DAC_FLAG << DAC_Channel)) != (uint8_t)RESET)\r
579   {\r
580     /* DAC_FLAG is set */\r
581     bitstatus = SET;\r
582   }\r
583   else\r
584   {\r
585     /* DAC_FLAG is reset */\r
586     bitstatus = RESET;\r
587   }\r
588   /* Return the DAC_FLAG status */\r
589   return  bitstatus;\r
590 }\r
591 \r
592 /**\r
593   * @brief  Clears the DAC channel's pending flags.\r
594   * @param  DAC_Channel: the selected DAC channel. \r
595   *   This parameter can be one of the following values:\r
596   *     @arg DAC_Channel_1: DAC Channel1 selected\r
597   *     @arg DAC_Channel_2: DAC Channel2 selected\r
598   * @param  DAC_FLAG: specifies the flag to clear. \r
599   *   This parameter can be of the following value:\r
600   *     @arg DAC_FLAG_DMAUDR: DMA underrun flag                          \r
601   * @retval None\r
602   */\r
603 void DAC_ClearFlag(uint32_t DAC_Channel, uint32_t DAC_FLAG)\r
604 {\r
605   /* Check the parameters */\r
606   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
607   assert_param(IS_DAC_FLAG(DAC_FLAG));\r
608 \r
609   /* Clear the selected DAC flags */\r
610   DAC->SR = (DAC_FLAG << DAC_Channel);\r
611 }\r
612 \r
613 /**\r
614   * @brief  Checks whether the specified DAC interrupt has occurred or not.\r
615   * @param  DAC_Channel: the selected DAC channel. \r
616   *   This parameter can be one of the following values:\r
617   *     @arg DAC_Channel_1: DAC Channel1 selected\r
618   *     @arg DAC_Channel_2: DAC Channel2 selected\r
619   * @param  DAC_IT: specifies the DAC interrupt source to check. \r
620   *   This parameter can be the following values:\r
621   *     @arg DAC_IT_DMAUDR: DMA underrun interrupt mask\r
622   * @note The DMA underrun occurs when a second external trigger arrives before\r
623   *       the acknowledgement for the first external trigger is received (first request).\r
624   * @retval The new state of DAC_IT (SET or RESET).\r
625   */\r
626 ITStatus DAC_GetITStatus(uint32_t DAC_Channel, uint32_t DAC_IT)\r
627 {\r
628   ITStatus bitstatus = RESET;\r
629   uint32_t enablestatus = 0;\r
630   \r
631   /* Check the parameters */\r
632   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
633   assert_param(IS_DAC_IT(DAC_IT));\r
634 \r
635   /* Get the DAC_IT enable bit status */\r
636   enablestatus = (DAC->CR & (DAC_IT << DAC_Channel)) ;\r
637   \r
638   /* Check the status of the specified DAC interrupt */\r
639   if (((DAC->SR & (DAC_IT << DAC_Channel)) != (uint32_t)RESET) && enablestatus)\r
640   {\r
641     /* DAC_IT is set */\r
642     bitstatus = SET;\r
643   }\r
644   else\r
645   {\r
646     /* DAC_IT is reset */\r
647     bitstatus = RESET;\r
648   }\r
649   /* Return the DAC_IT status */\r
650   return  bitstatus;\r
651 }\r
652 \r
653 /**\r
654   * @brief  Clears the DAC channel\92s interrupt pending bits.\r
655   * @param  DAC_Channel: the selected DAC channel. \r
656   *   This parameter can be one of the following values:\r
657   *     @arg DAC_Channel_1: DAC Channel1 selected\r
658   *     @arg DAC_Channel_2: DAC Channel2 selected\r
659   * @param  DAC_IT: specifies the DAC interrupt pending bit to clear.\r
660   *   This parameter can be the following values:\r
661   *     @arg DAC_IT_DMAUDR: DMA underrun interrupt mask                         \r
662   * @retval None\r
663   */\r
664 void DAC_ClearITPendingBit(uint32_t DAC_Channel, uint32_t DAC_IT)\r
665 {\r
666   /* Check the parameters */\r
667   assert_param(IS_DAC_CHANNEL(DAC_Channel));\r
668   assert_param(IS_DAC_IT(DAC_IT)); \r
669 \r
670   /* Clear the selected DAC interrupt pending bits */\r
671   DAC->SR = (DAC_IT << DAC_Channel);\r
672 }\r
673 \r
674 /**\r
675   * @}\r
676   */\r
677 \r
678 /**\r
679   * @}\r
680   */ \r
681 \r
682 /**\r
683   * @}\r
684   */ \r
685 \r
686 /**\r
687   * @}\r
688   */ \r
689 \r
690 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r