stm32f4xx_dac.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_dac.c
  4. * @author MCD Application Team
  5. * @version V1.0.2
  6. * @date 05-March-2012
  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. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  106. *
  107. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  108. * You may not use this file except in compliance with the License.
  109. * You may obtain a copy of the License at:
  110. *
  111. * http://www.st.com/software_license_agreement_liberty_v2
  112. *
  113. * Unless required by applicable law or agreed to in writing, software
  114. * distributed under the License is distributed on an "AS IS" BASIS,
  115. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  116. * See the License for the specific language governing permissions and
  117. * limitations under the License.
  118. *
  119. ******************************************************************************
  120. */
  121. /* Includes ------------------------------------------------------------------*/
  122. #include "stm32f4xx_dac.h"
  123. #include "stm32f4xx_rcc.h"
  124. /** @addtogroup STM32F4xx_StdPeriph_Driver
  125. * @{
  126. */
  127. /** @defgroup DAC
  128. * @brief DAC driver modules
  129. * @{
  130. */
  131. /* Private typedef -----------------------------------------------------------*/
  132. /* Private define ------------------------------------------------------------*/
  133. /* CR register Mask */
  134. #define CR_CLEAR_MASK ((uint32_t)0x00000FFE)
  135. /* DAC Dual Channels SWTRIG masks */
  136. #define DUAL_SWTRIG_SET ((uint32_t)0x00000003)
  137. #define DUAL_SWTRIG_RESET ((uint32_t)0xFFFFFFFC)
  138. /* DHR registers offsets */
  139. #define DHR12R1_OFFSET ((uint32_t)0x00000008)
  140. #define DHR12R2_OFFSET ((uint32_t)0x00000014)
  141. #define DHR12RD_OFFSET ((uint32_t)0x00000020)
  142. /* DOR register offset */
  143. #define DOR_OFFSET ((uint32_t)0x0000002C)
  144. /* Private macro -------------------------------------------------------------*/
  145. /* Private variables ---------------------------------------------------------*/
  146. /* Private function prototypes -----------------------------------------------*/
  147. /* Private functions ---------------------------------------------------------*/
  148. /** @defgroup DAC_Private_Functions
  149. * @{
  150. */
  151. /** @defgroup DAC_Group1 DAC channels configuration
  152. * @brief DAC channels configuration: trigger, output buffer, data format
  153. *
  154. @verbatim
  155. ===============================================================================
  156. DAC channels configuration: trigger, output buffer, data format
  157. ===============================================================================
  158. @endverbatim
  159. * @{
  160. */
  161. /**
  162. * @brief Deinitializes the DAC peripheral registers to their default reset values.
  163. * @param None
  164. * @retval None
  165. */
  166. void DAC_DeInit(void)
  167. {
  168. /* Enable DAC reset state */
  169. RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);
  170. /* Release DAC from reset state */
  171. RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);
  172. }
  173. /**
  174. * @brief Initializes the DAC peripheral according to the specified parameters
  175. * in the DAC_InitStruct.
  176. * @param DAC_Channel: the selected DAC channel.
  177. * This parameter can be one of the following values:
  178. * @arg DAC_Channel_1: DAC Channel1 selected
  179. * @arg DAC_Channel_2: DAC Channel2 selected
  180. * @param DAC_InitStruct: pointer to a DAC_InitTypeDef structure that contains
  181. * the configuration information for the specified DAC channel.
  182. * @retval None
  183. */
  184. void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct)
  185. {
  186. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  187. /* Check the DAC parameters */
  188. assert_param(IS_DAC_TRIGGER(DAC_InitStruct->DAC_Trigger));
  189. assert_param(IS_DAC_GENERATE_WAVE(DAC_InitStruct->DAC_WaveGeneration));
  190. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude));
  191. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->DAC_OutputBuffer));
  192. /*---------------------------- DAC CR Configuration --------------------------*/
  193. /* Get the DAC CR value */
  194. tmpreg1 = DAC->CR;
  195. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  196. tmpreg1 &= ~(CR_CLEAR_MASK << DAC_Channel);
  197. /* Configure for the selected DAC channel: buffer output, trigger,
  198. wave generation, mask/amplitude for wave generation */
  199. /* Set TSELx and TENx bits according to DAC_Trigger value */
  200. /* Set WAVEx bits according to DAC_WaveGeneration value */
  201. /* Set MAMPx bits according to DAC_LFSRUnmask_TriangleAmplitude value */
  202. /* Set BOFFx bit according to DAC_OutputBuffer value */
  203. tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration |
  204. DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | \
  205. DAC_InitStruct->DAC_OutputBuffer);
  206. /* Calculate CR register value depending on DAC_Channel */
  207. tmpreg1 |= tmpreg2 << DAC_Channel;
  208. /* Write to DAC CR */
  209. DAC->CR = tmpreg1;
  210. }
  211. /**
  212. * @brief Fills each DAC_InitStruct member with its default value.
  213. * @param DAC_InitStruct: pointer to a DAC_InitTypeDef structure which will
  214. * be initialized.
  215. * @retval None
  216. */
  217. void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct)
  218. {
  219. /*--------------- Reset DAC init structure parameters values -----------------*/
  220. /* Initialize the DAC_Trigger member */
  221. DAC_InitStruct->DAC_Trigger = DAC_Trigger_None;
  222. /* Initialize the DAC_WaveGeneration member */
  223. DAC_InitStruct->DAC_WaveGeneration = DAC_WaveGeneration_None;
  224. /* Initialize the DAC_LFSRUnmask_TriangleAmplitude member */
  225. DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
  226. /* Initialize the DAC_OutputBuffer member */
  227. DAC_InitStruct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  228. }
  229. /**
  230. * @brief Enables or disables the specified DAC channel.
  231. * @param DAC_Channel: The selected DAC channel.
  232. * This parameter can be one of the following values:
  233. * @arg DAC_Channel_1: DAC Channel1 selected
  234. * @arg DAC_Channel_2: DAC Channel2 selected
  235. * @param NewState: new state of the DAC channel.
  236. * This parameter can be: ENABLE or DISABLE.
  237. * @note When the DAC channel is enabled the trigger source can no more be modified.
  238. * @retval None
  239. */
  240. void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState)
  241. {
  242. /* Check the parameters */
  243. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  244. assert_param(IS_FUNCTIONAL_STATE(NewState));
  245. if (NewState != DISABLE)
  246. {
  247. /* Enable the selected DAC channel */
  248. DAC->CR |= (DAC_CR_EN1 << DAC_Channel);
  249. }
  250. else
  251. {
  252. /* Disable the selected DAC channel */
  253. DAC->CR &= (~(DAC_CR_EN1 << DAC_Channel));
  254. }
  255. }
  256. /**
  257. * @brief Enables or disables the selected DAC channel software trigger.
  258. * @param DAC_Channel: The selected DAC channel.
  259. * This parameter can be one of the following values:
  260. * @arg DAC_Channel_1: DAC Channel1 selected
  261. * @arg DAC_Channel_2: DAC Channel2 selected
  262. * @param NewState: new state of the selected DAC channel software trigger.
  263. * This parameter can be: ENABLE or DISABLE.
  264. * @retval None
  265. */
  266. void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState)
  267. {
  268. /* Check the parameters */
  269. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  270. assert_param(IS_FUNCTIONAL_STATE(NewState));
  271. if (NewState != DISABLE)
  272. {
  273. /* Enable software trigger for the selected DAC channel */
  274. DAC->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4);
  275. }
  276. else
  277. {
  278. /* Disable software trigger for the selected DAC channel */
  279. DAC->SWTRIGR &= ~((uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4));
  280. }
  281. }
  282. /**
  283. * @brief Enables or disables simultaneously the two DAC channels software triggers.
  284. * @param NewState: new state of the DAC channels software triggers.
  285. * This parameter can be: ENABLE or DISABLE.
  286. * @retval None
  287. */
  288. void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)
  289. {
  290. /* Check the parameters */
  291. assert_param(IS_FUNCTIONAL_STATE(NewState));
  292. if (NewState != DISABLE)
  293. {
  294. /* Enable software trigger for both DAC channels */
  295. DAC->SWTRIGR |= DUAL_SWTRIG_SET;
  296. }
  297. else
  298. {
  299. /* Disable software trigger for both DAC channels */
  300. DAC->SWTRIGR &= DUAL_SWTRIG_RESET;
  301. }
  302. }
  303. /**
  304. * @brief Enables or disables the selected DAC channel wave generation.
  305. * @param DAC_Channel: The selected DAC channel.
  306. * This parameter can be one of the following values:
  307. * @arg DAC_Channel_1: DAC Channel1 selected
  308. * @arg DAC_Channel_2: DAC Channel2 selected
  309. * @param DAC_Wave: specifies the wave type to enable or disable.
  310. * This parameter can be one of the following values:
  311. * @arg DAC_Wave_Noise: noise wave generation
  312. * @arg DAC_Wave_Triangle: triangle wave generation
  313. * @param NewState: new state of the selected DAC channel wave generation.
  314. * This parameter can be: ENABLE or DISABLE.
  315. * @retval None
  316. */
  317. void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState)
  318. {
  319. /* Check the parameters */
  320. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  321. assert_param(IS_DAC_WAVE(DAC_Wave));
  322. assert_param(IS_FUNCTIONAL_STATE(NewState));
  323. if (NewState != DISABLE)
  324. {
  325. /* Enable the selected wave generation for the selected DAC channel */
  326. DAC->CR |= DAC_Wave << DAC_Channel;
  327. }
  328. else
  329. {
  330. /* Disable the selected wave generation for the selected DAC channel */
  331. DAC->CR &= ~(DAC_Wave << DAC_Channel);
  332. }
  333. }
  334. /**
  335. * @brief Set the specified data holding register value for DAC channel1.
  336. * @param DAC_Align: Specifies the data alignment for DAC channel1.
  337. * This parameter can be one of the following values:
  338. * @arg DAC_Align_8b_R: 8bit right data alignment selected
  339. * @arg DAC_Align_12b_L: 12bit left data alignment selected
  340. * @arg DAC_Align_12b_R: 12bit right data alignment selected
  341. * @param Data: Data to be loaded in the selected data holding register.
  342. * @retval None
  343. */
  344. void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)
  345. {
  346. __IO uint32_t tmp = 0;
  347. /* Check the parameters */
  348. assert_param(IS_DAC_ALIGN(DAC_Align));
  349. assert_param(IS_DAC_DATA(Data));
  350. tmp = (uint32_t)DAC_BASE;
  351. tmp += DHR12R1_OFFSET + DAC_Align;
  352. /* Set the DAC channel1 selected data holding register */
  353. *(__IO uint32_t *) tmp = Data;
  354. }
  355. /**
  356. * @brief Set the specified data holding register value for DAC channel2.
  357. * @param DAC_Align: Specifies the data alignment for DAC channel2.
  358. * This parameter can be one of the following values:
  359. * @arg DAC_Align_8b_R: 8bit right data alignment selected
  360. * @arg DAC_Align_12b_L: 12bit left data alignment selected
  361. * @arg DAC_Align_12b_R: 12bit right data alignment selected
  362. * @param Data: Data to be loaded in the selected data holding register.
  363. * @retval None
  364. */
  365. void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data)
  366. {
  367. __IO uint32_t tmp = 0;
  368. /* Check the parameters */
  369. assert_param(IS_DAC_ALIGN(DAC_Align));
  370. assert_param(IS_DAC_DATA(Data));
  371. tmp = (uint32_t)DAC_BASE;
  372. tmp += DHR12R2_OFFSET + DAC_Align;
  373. /* Set the DAC channel2 selected data holding register */
  374. *(__IO uint32_t *)tmp = Data;
  375. }
  376. /**
  377. * @brief Set the specified data holding register value for dual channel DAC.
  378. * @param DAC_Align: Specifies the data alignment for dual channel DAC.
  379. * This parameter can be one of the following values:
  380. * @arg DAC_Align_8b_R: 8bit right data alignment selected
  381. * @arg DAC_Align_12b_L: 12bit left data alignment selected
  382. * @arg DAC_Align_12b_R: 12bit right data alignment selected
  383. * @param Data2: Data for DAC Channel2 to be loaded in the selected data holding register.
  384. * @param Data1: Data for DAC Channel1 to be loaded in the selected data holding register.
  385. * @note In dual mode, a unique register access is required to write in both
  386. * DAC channels at the same time.
  387. * @retval None
  388. */
  389. void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1)
  390. {
  391. uint32_t data = 0, tmp = 0;
  392. /* Check the parameters */
  393. assert_param(IS_DAC_ALIGN(DAC_Align));
  394. assert_param(IS_DAC_DATA(Data1));
  395. assert_param(IS_DAC_DATA(Data2));
  396. /* Calculate and set dual DAC data holding register value */
  397. if (DAC_Align == DAC_Align_8b_R)
  398. {
  399. data = ((uint32_t)Data2 << 8) | Data1;
  400. }
  401. else
  402. {
  403. data = ((uint32_t)Data2 << 16) | Data1;
  404. }
  405. tmp = (uint32_t)DAC_BASE;
  406. tmp += DHR12RD_OFFSET + DAC_Align;
  407. /* Set the dual DAC selected data holding register */
  408. *(__IO uint32_t *)tmp = data;
  409. }
  410. /**
  411. * @brief Returns the last data output value of the selected DAC channel.
  412. * @param DAC_Channel: The selected DAC channel.
  413. * This parameter can be one of the following values:
  414. * @arg DAC_Channel_1: DAC Channel1 selected
  415. * @arg DAC_Channel_2: DAC Channel2 selected
  416. * @retval The selected DAC channel data output value.
  417. */
  418. uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)
  419. {
  420. __IO uint32_t tmp = 0;
  421. /* Check the parameters */
  422. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  423. tmp = (uint32_t) DAC_BASE ;
  424. tmp += DOR_OFFSET + ((uint32_t)DAC_Channel >> 2);
  425. /* Returns the DAC channel data output register value */
  426. return (uint16_t) (*(__IO uint32_t*) tmp);
  427. }
  428. /**
  429. * @}
  430. */
  431. /** @defgroup DAC_Group2 DMA management functions
  432. * @brief DMA management functions
  433. *
  434. @verbatim
  435. ===============================================================================
  436. DMA management functions
  437. ===============================================================================
  438. @endverbatim
  439. * @{
  440. */
  441. /**
  442. * @brief Enables or disables the specified DAC channel DMA request.
  443. * @note When enabled DMA1 is generated when an external trigger (EXTI Line9,
  444. * TIM2, TIM4, TIM5, TIM6, TIM7 or TIM8 but not a software trigger) occurs.
  445. * @param DAC_Channel: The selected DAC channel.
  446. * This parameter can be one of the following values:
  447. * @arg DAC_Channel_1: DAC Channel1 selected
  448. * @arg DAC_Channel_2: DAC Channel2 selected
  449. * @param NewState: new state of the selected DAC channel DMA request.
  450. * This parameter can be: ENABLE or DISABLE.
  451. * @note The DAC channel1 is mapped on DMA1 Stream 5 channel7 which must be
  452. * already configured.
  453. * @note The DAC channel2 is mapped on DMA1 Stream 6 channel7 which must be
  454. * already configured.
  455. * @retval None
  456. */
  457. void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState)
  458. {
  459. /* Check the parameters */
  460. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  461. assert_param(IS_FUNCTIONAL_STATE(NewState));
  462. if (NewState != DISABLE)
  463. {
  464. /* Enable the selected DAC channel DMA request */
  465. DAC->CR |= (DAC_CR_DMAEN1 << DAC_Channel);
  466. }
  467. else
  468. {
  469. /* Disable the selected DAC channel DMA request */
  470. DAC->CR &= (~(DAC_CR_DMAEN1 << DAC_Channel));
  471. }
  472. }
  473. /**
  474. * @}
  475. */
  476. /** @defgroup DAC_Group3 Interrupts and flags management functions
  477. * @brief Interrupts and flags management functions
  478. *
  479. @verbatim
  480. ===============================================================================
  481. Interrupts and flags management functions
  482. ===============================================================================
  483. @endverbatim
  484. * @{
  485. */
  486. /**
  487. * @brief Enables or disables the specified DAC interrupts.
  488. * @param DAC_Channel: The selected DAC channel.
  489. * This parameter can be one of the following values:
  490. * @arg DAC_Channel_1: DAC Channel1 selected
  491. * @arg DAC_Channel_2: DAC Channel2 selected
  492. * @param DAC_IT: specifies the DAC interrupt sources to be enabled or disabled.
  493. * This parameter can be the following values:
  494. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  495. * @note The DMA underrun occurs when a second external trigger arrives before the
  496. * acknowledgement for the first external trigger is received (first request).
  497. * @param NewState: new state of the specified DAC interrupts.
  498. * This parameter can be: ENABLE or DISABLE.
  499. * @retval None
  500. */
  501. void DAC_ITConfig(uint32_t DAC_Channel, uint32_t DAC_IT, FunctionalState NewState)
  502. {
  503. /* Check the parameters */
  504. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  505. assert_param(IS_FUNCTIONAL_STATE(NewState));
  506. assert_param(IS_DAC_IT(DAC_IT));
  507. if (NewState != DISABLE)
  508. {
  509. /* Enable the selected DAC interrupts */
  510. DAC->CR |= (DAC_IT << DAC_Channel);
  511. }
  512. else
  513. {
  514. /* Disable the selected DAC interrupts */
  515. DAC->CR &= (~(uint32_t)(DAC_IT << DAC_Channel));
  516. }
  517. }
  518. /**
  519. * @brief Checks whether the specified DAC flag is set or not.
  520. * @param DAC_Channel: The selected DAC channel.
  521. * This parameter can be one of the following values:
  522. * @arg DAC_Channel_1: DAC Channel1 selected
  523. * @arg DAC_Channel_2: DAC Channel2 selected
  524. * @param DAC_FLAG: specifies the flag to check.
  525. * This parameter can be only of the following value:
  526. * @arg DAC_FLAG_DMAUDR: DMA underrun flag
  527. * @note The DMA underrun occurs when a second external trigger arrives before the
  528. * acknowledgement for the first external trigger is received (first request).
  529. * @retval The new state of DAC_FLAG (SET or RESET).
  530. */
  531. FlagStatus DAC_GetFlagStatus(uint32_t DAC_Channel, uint32_t DAC_FLAG)
  532. {
  533. FlagStatus bitstatus = RESET;
  534. /* Check the parameters */
  535. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  536. assert_param(IS_DAC_FLAG(DAC_FLAG));
  537. /* Check the status of the specified DAC flag */
  538. if ((DAC->SR & (DAC_FLAG << DAC_Channel)) != (uint8_t)RESET)
  539. {
  540. /* DAC_FLAG is set */
  541. bitstatus = SET;
  542. }
  543. else
  544. {
  545. /* DAC_FLAG is reset */
  546. bitstatus = RESET;
  547. }
  548. /* Return the DAC_FLAG status */
  549. return bitstatus;
  550. }
  551. /**
  552. * @brief Clears the DAC channel's pending flags.
  553. * @param DAC_Channel: The selected DAC channel.
  554. * This parameter can be one of the following values:
  555. * @arg DAC_Channel_1: DAC Channel1 selected
  556. * @arg DAC_Channel_2: DAC Channel2 selected
  557. * @param DAC_FLAG: specifies the flag to clear.
  558. * This parameter can be of the following value:
  559. * @arg DAC_FLAG_DMAUDR: DMA underrun flag
  560. * @note The DMA underrun occurs when a second external trigger arrives before the
  561. * acknowledgement for the first external trigger is received (first request).
  562. * @retval None
  563. */
  564. void DAC_ClearFlag(uint32_t DAC_Channel, uint32_t DAC_FLAG)
  565. {
  566. /* Check the parameters */
  567. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  568. assert_param(IS_DAC_FLAG(DAC_FLAG));
  569. /* Clear the selected DAC flags */
  570. DAC->SR = (DAC_FLAG << DAC_Channel);
  571. }
  572. /**
  573. * @brief Checks whether the specified DAC interrupt has occurred or not.
  574. * @param DAC_Channel: The selected DAC channel.
  575. * This parameter can be one of the following values:
  576. * @arg DAC_Channel_1: DAC Channel1 selected
  577. * @arg DAC_Channel_2: DAC Channel2 selected
  578. * @param DAC_IT: specifies the DAC interrupt source to check.
  579. * This parameter can be the following values:
  580. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  581. * @note The DMA underrun occurs when a second external trigger arrives before the
  582. * acknowledgement for the first external trigger is received (first request).
  583. * @retval The new state of DAC_IT (SET or RESET).
  584. */
  585. ITStatus DAC_GetITStatus(uint32_t DAC_Channel, uint32_t DAC_IT)
  586. {
  587. ITStatus bitstatus = RESET;
  588. uint32_t enablestatus = 0;
  589. /* Check the parameters */
  590. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  591. assert_param(IS_DAC_IT(DAC_IT));
  592. /* Get the DAC_IT enable bit status */
  593. enablestatus = (DAC->CR & (DAC_IT << DAC_Channel)) ;
  594. /* Check the status of the specified DAC interrupt */
  595. if (((DAC->SR & (DAC_IT << DAC_Channel)) != (uint32_t)RESET) && enablestatus)
  596. {
  597. /* DAC_IT is set */
  598. bitstatus = SET;
  599. }
  600. else
  601. {
  602. /* DAC_IT is reset */
  603. bitstatus = RESET;
  604. }
  605. /* Return the DAC_IT status */
  606. return bitstatus;
  607. }
  608. /**
  609. * @brief Clears the DAC channel's interrupt pending bits.
  610. * @param DAC_Channel: The selected DAC channel.
  611. * This parameter can be one of the following values:
  612. * @arg DAC_Channel_1: DAC Channel1 selected
  613. * @arg DAC_Channel_2: DAC Channel2 selected
  614. * @param DAC_IT: specifies the DAC interrupt pending bit to clear.
  615. * This parameter can be the following values:
  616. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  617. * @note The DMA underrun occurs when a second external trigger arrives before the
  618. * acknowledgement for the first external trigger is received (first request).
  619. * @retval None
  620. */
  621. void DAC_ClearITPendingBit(uint32_t DAC_Channel, uint32_t DAC_IT)
  622. {
  623. /* Check the parameters */
  624. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  625. assert_param(IS_DAC_IT(DAC_IT));
  626. /* Clear the selected DAC interrupt pending bits */
  627. DAC->SR = (DAC_IT << DAC_Channel);
  628. }
  629. /**
  630. * @}
  631. */
  632. /**
  633. * @}
  634. */
  635. /**
  636. * @}
  637. */
  638. /**
  639. * @}
  640. */
  641. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/