stm32f0xx_dac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_dac.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 23-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 channel 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. [..] The device integrates one 12-bit Digital Analog Converters refered as
  19. DAC channel1 with DAC_OUT1 (PA4) as output
  20. [..] Digital to Analog conversion can be non-triggered using DAC_Trigger_None
  21. and DAC_OUT1 is available once writing to DHRx register using
  22. DAC_SetChannel1Data().
  23. [..] Digital to Analog conversion can be triggered by:
  24. (#) External event: EXTI Line 9 (any GPIOx_Pin9) using DAC_Trigger_Ext_IT9.
  25. The used pin (GPIOx_Pin9) must be configured in input mode.
  26. (#) Timers TRGO: TIM2, TIM3, TIM6 and TIM15
  27. (DAC_Trigger_T2_TRGO, DAC_Trigger_T3_TRGO...)
  28. The timer TRGO event should be selected using TIM_SelectOutputTrigger()
  29. (#) Software using DAC_Trigger_Software
  30. [..] The DAC channel 1 integrates an output buffer that can be used to
  31. reduce the output impedance, and to drive external loads directly
  32. without having to add an external operational amplifier.
  33. To enable the output buffer use
  34. DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  35. [..] Refer to the device datasheet for more details about output impedance
  36. value with and without output buffer.
  37. [..] The DAC data format can be:
  38. (#) 8-bit right alignment using DAC_Align_8b_R
  39. (#) 12-bit left alignment using DAC_Align_12b_L
  40. (#) 12-bit right alignment using DAC_Align_12b_R
  41. [..] The analog output voltage on each DAC channel pin is determined
  42. by the following equation: DAC_OUTx = VREF+ * DOR / 4095
  43. with DOR is the Data Output Register
  44. VEF+ is the input voltage reference (refer to the device datasheet)
  45. e.g. To set DAC_OUT1 to 0.7V, use
  46. DAC_SetChannel1Data(DAC_Align_12b_R, 868);
  47. Assuming that VREF+ = 3.3, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
  48. [..] A DMA1 request can be generated when an external trigger (but not
  49. a software trigger) occurs if DMA1 requests are enabled using
  50. DAC_DMACmd()
  51. DMA1 requests are mapped as following:
  52. (+) DAC channel1 is mapped on DMA1 channel3 which must be already
  53. configured
  54. ##### How to use this driver #####
  55. ===============================================================================
  56. [..]
  57. (+) Enable DAC APB1 clock to get write access to DAC registers
  58. using RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE)
  59. (+) Configure DAC_OUT1 (DAC_OUT1: PA4) in analog mode
  60. using GPIO_Init() function
  61. (+) Configure the DAC channel using DAC_Init()
  62. (+) Enable the DAC channel using DAC_Cmd()
  63. @endverbatim
  64. *
  65. ******************************************************************************
  66. * @attention
  67. *
  68. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  69. *
  70. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  71. * You may not use this file except in compliance with the License.
  72. * You may obtain a copy of the License at:
  73. *
  74. * http://www.st.com/software_license_agreement_liberty_v2
  75. *
  76. * Unless required by applicable law or agreed to in writing, software
  77. * distributed under the License is distributed on an "AS IS" BASIS,
  78. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  79. * See the License for the specific language governing permissions and
  80. * limitations under the License.
  81. *
  82. ******************************************************************************
  83. */
  84. /* Includes ------------------------------------------------------------------*/
  85. #include "stm32f0xx_dac.h"
  86. /** @addtogroup STM32F0xx_StdPeriph_Driver
  87. * @{
  88. */
  89. /** @defgroup DAC
  90. * @brief DAC driver modules
  91. * @{
  92. */
  93. /* Private typedef -----------------------------------------------------------*/
  94. /* Private define ------------------------------------------------------------*/
  95. /* CR register Mask */
  96. #define CR_CLEAR_MASK ((uint32_t)0x0000003E)
  97. /* DHR registers offsets */
  98. #define DHR12R1_OFFSET ((uint32_t)0x00000008)
  99. /* DOR register offset */
  100. #define DOR_OFFSET ((uint32_t)0x0000002C)
  101. /* Private macro -------------------------------------------------------------*/
  102. /* Private variables ---------------------------------------------------------*/
  103. /* Private function prototypes -----------------------------------------------*/
  104. /* Private functions ---------------------------------------------------------*/
  105. /** @defgroup DAC_Private_Functions
  106. * @{
  107. */
  108. /** @defgroup DAC_Group1 DAC channels configuration
  109. * @brief DAC channels configuration: trigger, output buffer, data format
  110. *
  111. @verbatim
  112. ===============================================================================
  113. ##### DAC channels configuration: trigger, output buffer, data format #####
  114. ===============================================================================
  115. @endverbatim
  116. * @{
  117. */
  118. /**
  119. * @brief Deinitializes the DAC peripheral registers to their default reset values.
  120. * @param None
  121. * @retval None
  122. */
  123. void DAC_DeInit(void)
  124. {
  125. /* Enable DAC reset state */
  126. RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);
  127. /* Release DAC from reset state */
  128. RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);
  129. }
  130. /**
  131. * @brief Initializes the DAC peripheral according to the specified
  132. * parameters in the DAC_InitStruct.
  133. * @param DAC_Channel: the selected DAC channel.
  134. * This parameter can be one of the following values:
  135. * @arg DAC_Channel_1: DAC Channel1 selected
  136. * @param DAC_InitStruct: pointer to a DAC_InitTypeDef structure that
  137. * contains the configuration information for the specified DAC channel.
  138. *
  139. * @retval None
  140. */
  141. void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct)
  142. {
  143. uint32_t tmpreg1 = 0, tmpreg2 = 0;
  144. /* Check the DAC parameters */
  145. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  146. assert_param(IS_DAC_TRIGGER(DAC_InitStruct->DAC_Trigger));
  147. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->DAC_OutputBuffer));
  148. /*---------------------------- DAC CR Configuration ------------------------*/
  149. /* Get the DAC CR value */
  150. tmpreg1 = DAC->CR;
  151. /* Clear BOFFx, TENx, TSELx bits */
  152. tmpreg1 &= ~(CR_CLEAR_MASK << DAC_Channel);
  153. /* Configure for the selected DAC channel: buffer output, trigger */
  154. /* Set TSELx and TENx bits according to DAC_Trigger value */
  155. /* Set BOFFx bit according to DAC_OutputBuffer value */
  156. tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_OutputBuffer);
  157. /* Calculate CR register value depending on DAC_Channel */
  158. tmpreg1 |= tmpreg2 << DAC_Channel;
  159. /* Write to DAC CR */
  160. DAC->CR = tmpreg1;
  161. }
  162. /**
  163. * @brief Fills each DAC_InitStruct member with its default value.
  164. * @param DAC_InitStruct : pointer to a DAC_InitTypeDef structure which will
  165. * be initialized.
  166. * @retval None
  167. */
  168. void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct)
  169. {
  170. /*--------------- Reset DAC init structure parameters values -----------------*/
  171. /* Initialize the DAC_Trigger member */
  172. DAC_InitStruct->DAC_Trigger = DAC_Trigger_None;
  173. /* Initialize the DAC_OutputBuffer member */
  174. DAC_InitStruct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  175. }
  176. /**
  177. * @brief Enables or disables the specified DAC channel.
  178. * @param DAC_Channel: The selected DAC channel.
  179. * This parameter can be one of the following values:
  180. * @arg DAC_Channel_1: DAC Channel1 selected
  181. * @param NewState: new state of the DAC channel.
  182. * This parameter can be: ENABLE or DISABLE.
  183. * @note When the DAC channel is enabled the trigger source can no more
  184. * be modified.
  185. * @retval None
  186. */
  187. void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState)
  188. {
  189. /* Check the parameters */
  190. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  191. assert_param(IS_FUNCTIONAL_STATE(NewState));
  192. if (NewState != DISABLE)
  193. {
  194. /* Enable the selected DAC channel */
  195. DAC->CR |= (DAC_CR_EN1 << DAC_Channel);
  196. }
  197. else
  198. {
  199. /* Disable the selected DAC channel */
  200. DAC->CR &= (~(DAC_CR_EN1 << DAC_Channel));
  201. }
  202. }
  203. /**
  204. * @brief Enables or disables the selected DAC channel software trigger.
  205. * @param DAC_Channel: the selected DAC channel.
  206. * This parameter can be one of the following values:
  207. * @arg DAC_Channel_1: DAC Channel1 selected
  208. * @param NewState: new state of the selected DAC channel software trigger.
  209. * This parameter can be: ENABLE or DISABLE.
  210. * @retval None
  211. */
  212. void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState)
  213. {
  214. /* Check the parameters */
  215. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  216. assert_param(IS_FUNCTIONAL_STATE(NewState));
  217. if (NewState != DISABLE)
  218. {
  219. /* Enable software trigger for the selected DAC channel */
  220. DAC->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4);
  221. }
  222. else
  223. {
  224. /* Disable software trigger for the selected DAC channel */
  225. DAC->SWTRIGR &= ~((uint32_t)DAC_SWTRIGR_SWTRIG1 << (DAC_Channel >> 4));
  226. }
  227. }
  228. /**
  229. * @brief Set the specified data holding register value for DAC channel1.
  230. * @param DAC_Align: Specifies the data alignment for DAC channel1.
  231. * This parameter can be one of the following values:
  232. * @arg DAC_Align_8b_R: 8bit right data alignment selected
  233. * @arg DAC_Align_12b_L: 12bit left data alignment selected
  234. * @arg DAC_Align_12b_R: 12bit right data alignment selected
  235. * @param Data : Data to be loaded in the selected data holding register.
  236. * @retval None
  237. */
  238. void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)
  239. {
  240. __IO uint32_t tmp = 0;
  241. /* Check the parameters */
  242. assert_param(IS_DAC_ALIGN(DAC_Align));
  243. assert_param(IS_DAC_DATA(Data));
  244. tmp = (uint32_t)DAC_BASE;
  245. tmp += DHR12R1_OFFSET + DAC_Align;
  246. /* Set the DAC channel1 selected data holding register */
  247. *(__IO uint32_t *) tmp = Data;
  248. }
  249. /**
  250. * @brief Returns the last data output value of the selected DAC channel.
  251. * @param DAC_Channel: the selected DAC channel.
  252. * This parameter can be one of the following values:
  253. * @arg DAC_Channel_1: DAC Channel1 selected
  254. * @retval The selected DAC channel data output value.
  255. */
  256. uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)
  257. {
  258. __IO uint32_t tmp = 0;
  259. /* Check the parameters */
  260. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  261. tmp = (uint32_t) DAC_BASE ;
  262. tmp += DOR_OFFSET + ((uint32_t)DAC_Channel >> 2);
  263. /* Returns the DAC channel data output register value */
  264. return (uint16_t) (*(__IO uint32_t*) tmp);
  265. }
  266. /**
  267. * @}
  268. */
  269. /** @defgroup DAC_Group2 DMA management functions
  270. * @brief DMA management functions
  271. *
  272. @verbatim
  273. ===============================================================================
  274. ##### DMA management functions #####
  275. ===============================================================================
  276. @endverbatim
  277. * @{
  278. */
  279. /**
  280. * @brief Enables or disables the specified DAC channel DMA request.
  281. * When enabled DMA1 is generated when an external trigger (EXTI Line9,
  282. * TIM2, TIM3, TIM6 or TIM15 but not a software trigger) occurs
  283. * @param DAC_Channel: the selected DAC channel.
  284. * This parameter can be one of the following values:
  285. * @arg DAC_Channel_1: DAC Channel1 selected
  286. * @param NewState: new state of the selected DAC channel DMA request.
  287. * This parameter can be: ENABLE or DISABLE.
  288. * The DAC channel1 is mapped on DMA1 channel3 which must be already configured.
  289. * @retval None
  290. */
  291. void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState)
  292. {
  293. /* Check the parameters */
  294. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  295. assert_param(IS_FUNCTIONAL_STATE(NewState));
  296. if (NewState != DISABLE)
  297. {
  298. /* Enable the selected DAC channel DMA request */
  299. DAC->CR |= (DAC_CR_DMAEN1 << DAC_Channel);
  300. }
  301. else
  302. {
  303. /* Disable the selected DAC channel DMA request */
  304. DAC->CR &= (~(DAC_CR_DMAEN1 << DAC_Channel));
  305. }
  306. }
  307. /**
  308. * @}
  309. */
  310. /** @defgroup DAC_Group3 Interrupts and flags management functions
  311. * @brief Interrupts and flags management functions
  312. *
  313. @verbatim
  314. ===============================================================================
  315. ##### Interrupts and flags management functions #####
  316. ===============================================================================
  317. @endverbatim
  318. * @{
  319. */
  320. /**
  321. * @brief Enables or disables the specified DAC interrupts.
  322. * @param DAC_Channel: the selected DAC channel.
  323. * This parameter can be one of the following values:
  324. * @arg DAC_Channel_1: DAC Channel1 selected
  325. * @param DAC_IT: specifies the DAC interrupt sources to be enabled or disabled.
  326. * This parameter can be the following values:
  327. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  328. * @note The DMA underrun occurs when a second external trigger arrives before
  329. * the acknowledgement for the first external trigger is received (first request).
  330. * @param NewState: new state of the specified DAC interrupts.
  331. * This parameter can be: ENABLE or DISABLE.
  332. * @retval None
  333. */
  334. void DAC_ITConfig(uint32_t DAC_Channel, uint32_t DAC_IT, FunctionalState NewState)
  335. {
  336. /* Check the parameters */
  337. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  338. assert_param(IS_FUNCTIONAL_STATE(NewState));
  339. assert_param(IS_DAC_IT(DAC_IT));
  340. if (NewState != DISABLE)
  341. {
  342. /* Enable the selected DAC interrupts */
  343. DAC->CR |= (DAC_IT << DAC_Channel);
  344. }
  345. else
  346. {
  347. /* Disable the selected DAC interrupts */
  348. DAC->CR &= (~(uint32_t)(DAC_IT << DAC_Channel));
  349. }
  350. }
  351. /**
  352. * @brief Checks whether the specified DAC flag is set or not.
  353. * @param DAC_Channel: thee selected DAC channel.
  354. * This parameter can be one of the following values:
  355. * @arg DAC_Channel_1: DAC Channel1 selected
  356. * @param DAC_FLAG: specifies the flag to check.
  357. * This parameter can be only of the following value:
  358. * @arg DAC_FLAG_DMAUDR: DMA underrun flag
  359. * @note The DMA underrun occurs when a second external trigger arrives before
  360. * the acknowledgement for the first external trigger is received (first request).
  361. * @retval The new state of DAC_FLAG (SET or RESET).
  362. */
  363. FlagStatus DAC_GetFlagStatus(uint32_t DAC_Channel, uint32_t DAC_FLAG)
  364. {
  365. FlagStatus bitstatus = RESET;
  366. /* Check the parameters */
  367. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  368. assert_param(IS_DAC_FLAG(DAC_FLAG));
  369. /* Check the status of the specified DAC flag */
  370. if ((DAC->SR & (DAC_FLAG << DAC_Channel)) != (uint8_t)RESET)
  371. {
  372. /* DAC_FLAG is set */
  373. bitstatus = SET;
  374. }
  375. else
  376. {
  377. /* DAC_FLAG is reset */
  378. bitstatus = RESET;
  379. }
  380. /* Return the DAC_FLAG status */
  381. return bitstatus;
  382. }
  383. /**
  384. * @brief Clears the DAC channel's pending flags.
  385. * @param DAC_Channel: the selected DAC channel.
  386. * This parameter can be one of the following values:
  387. * @arg DAC_Channel_1: DAC Channel1 selected
  388. * @param DAC_FLAG: specifies the flag to clear.
  389. * This parameter can be of the following value:
  390. * @arg DAC_FLAG_DMAUDR: DMA underrun flag
  391. * @retval None
  392. */
  393. void DAC_ClearFlag(uint32_t DAC_Channel, uint32_t DAC_FLAG)
  394. {
  395. /* Check the parameters */
  396. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  397. assert_param(IS_DAC_FLAG(DAC_FLAG));
  398. /* Clear the selected DAC flags */
  399. DAC->SR = (DAC_FLAG << DAC_Channel);
  400. }
  401. /**
  402. * @brief Checks whether the specified DAC interrupt has occurred or not.
  403. * @param DAC_Channel: the selected DAC channel.
  404. * This parameter can be one of the following values:
  405. * @arg DAC_Channel_1: DAC Channel1 selected
  406. * @param DAC_IT: specifies the DAC interrupt source to check.
  407. * This parameter can be the following values:
  408. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  409. * @note The DMA underrun occurs when a second external trigger arrives before
  410. * the acknowledgement for the first external trigger is received (first request).
  411. * @retval The new state of DAC_IT (SET or RESET).
  412. */
  413. ITStatus DAC_GetITStatus(uint32_t DAC_Channel, uint32_t DAC_IT)
  414. {
  415. ITStatus bitstatus = RESET;
  416. uint32_t enablestatus = 0;
  417. /* Check the parameters */
  418. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  419. assert_param(IS_DAC_IT(DAC_IT));
  420. /* Get the DAC_IT enable bit status */
  421. enablestatus = (DAC->CR & (DAC_IT << DAC_Channel)) ;
  422. /* Check the status of the specified DAC interrupt */
  423. if (((DAC->SR & (DAC_IT << DAC_Channel)) != (uint32_t)RESET) && enablestatus)
  424. {
  425. /* DAC_IT is set */
  426. bitstatus = SET;
  427. }
  428. else
  429. {
  430. /* DAC_IT is reset */
  431. bitstatus = RESET;
  432. }
  433. /* Return the DAC_IT status */
  434. return bitstatus;
  435. }
  436. /**
  437. * @brief Clears the DAC channel's interrupt pending bits.
  438. * @param DAC_Channel: the selected DAC channel.
  439. * This parameter can be one of the following values:
  440. * @arg DAC_Channel_1: DAC Channel1 selected
  441. * @param DAC_IT: specifies the DAC interrupt pending bit to clear.
  442. * This parameter can be the following values:
  443. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  444. * @retval None
  445. */
  446. void DAC_ClearITPendingBit(uint32_t DAC_Channel, uint32_t DAC_IT)
  447. {
  448. /* Check the parameters */
  449. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  450. assert_param(IS_DAC_IT(DAC_IT));
  451. /* Clear the selected DAC interrupt pending bits */
  452. DAC->SR = (DAC_IT << DAC_Channel);
  453. }
  454. /**
  455. * @}
  456. */
  457. /**
  458. * @}
  459. */
  460. /**
  461. * @}
  462. */
  463. /**
  464. * @}
  465. */
  466. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/