stm32f4xx_dac.c 26 KB

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