stm8l15x_dac.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_dac.c
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  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. * The device integrates two 12-bit Digital Analog Converters that can
  19. * be used independently or simultaneously (dual mode):
  20. * 1- DAC channel1 with DAC_OUT1 (PF0) as output
  21. * 1- DAC channel2 with DAC_OUT2 (PF1) as output
  22. *
  23. * Digital to Analog conversion can be non-triggered using DAC_Trigger_None
  24. * and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register using
  25. * DAC_SetChannel1Data()/DAC_SetChannel2Data.
  26. *
  27. * Digital to Analog conversion can be triggered by:
  28. * 1- External event: PE4 using DAC_Trigger_Ext.
  29. * This pin (PE4) must be configured in input mode.
  30. *
  31. * 2- Timers TRGO: TIM4, TIM5
  32. * (DAC_Trigger_T4_TRGO, DAC_Trigger_T5_TRGO)
  33. * The timer TRGO event should be selected using TIMx_SelectOutputTrigger()
  34. *
  35. * 3- Software using DAC_Trigger_Software
  36. *
  37. * Each DAC channel integrates an output buffer that can be used to
  38. * reduce the output impedance, and to drive external loads directly
  39. * without having to add an external operational amplifier.
  40. *
  41. * Refer to the device datasheet for more details about output impedance
  42. * value with and without output buffer.
  43. *
  44. * Both DAC channels can be used to generate
  45. * 1- Noise wave using DAC_Wave_Noise
  46. * 2- Triangle wave using DAC_Wave_Triangle
  47. *
  48. *
  49. * The DAC data format can be:
  50. * 1- 8-bit right alignment using DAC_Align_8b_R
  51. * 2- 12-bit left alignment using DAC_Align_12b_L
  52. * 3- 12-bit right alignment using DAC_Align_12b_R
  53. *
  54. * The analog output voltage on each DAC channel pin is determined
  55. * by the following equation: DAC_OUTx = VREF+ * DOR / 4095
  56. * with DOR is the Data Output Register
  57. * VEF+ is the input voltage reference (refer to the device datasheet)
  58. * e.g. To set DAC_OUT1 to 0.7V, use
  59. * DAC_SetChannel1Data(DAC_Align_12b_R, 868);
  60. * Assuming that VREF+ = 3.3, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
  61. *
  62. * A DMA1 request can be generated when an external trigger (but not
  63. * a software trigger) occurs if DMA1 requests are enabled using
  64. * DAC_DMACmd()
  65. * DMA1 requests are mapped as following:
  66. * 1- DAC channel1 is mapped on DMA1 channel3 which must be already
  67. * configured
  68. * 2- DAC channel2 is mapped on DMA1 channel1 which must be already
  69. * configured
  70. *
  71. * ===================================================================
  72. * How to use this driver
  73. * ===================================================================
  74. * - DAC clock must be enabled to get write access to DAC registers using
  75. * CLK_PeripheralClockConfig(CLK_Peripheral_DAC, ENABLE)
  76. * - Configure DAC_OUTx (DAC_OUT1: PF0, DAC_OUT2: PF1) in analog mode.
  77. * - Configure the DAC channel using DAC_Init()
  78. * - Enable the DAC channel using DAC_Cmd()
  79. *
  80. * @endverbatim
  81. *
  82. ******************************************************************************
  83. * @attention
  84. *
  85. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  86. *
  87. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  88. * You may not use this file except in compliance with the License.
  89. * You may obtain a copy of the License at:
  90. *
  91. * http://www.st.com/software_license_agreement_liberty_v2
  92. *
  93. * Unless required by applicable law or agreed to in writing, software
  94. * distributed under the License is distributed on an "AS IS" BASIS,
  95. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  96. * See the License for the specific language governing permissions and
  97. * limitations under the License.
  98. *
  99. ******************************************************************************
  100. */
  101. /* Includes ------------------------------------------------------------------*/
  102. #include "stm8l15x_dac.h"
  103. /** @addtogroup STM8L15x_StdPeriph_Driver
  104. * @{
  105. */
  106. /** @defgroup DAC
  107. * @brief DAC driver modules
  108. * @{
  109. */
  110. /* Private typedef -----------------------------------------------------------*/
  111. /* Private define ------------------------------------------------------------*/
  112. /* Private macro -------------------------------------------------------------*/
  113. /* Private variables ---------------------------------------------------------*/
  114. /* Private function prototypes -----------------------------------------------*/
  115. /* Private functions ---------------------------------------------------------*/
  116. /** @defgroup DAC_Private_Functions
  117. * @{
  118. */
  119. /** @defgroup DAC_Group1 DAC channels configuration
  120. * @brief DAC channels configuration: trigger, output buffer, data format
  121. *
  122. @verbatim
  123. ===============================================================================
  124. DAC channels configuration: trigger, output buffer, data format
  125. ===============================================================================
  126. @endverbatim
  127. * @{
  128. */
  129. /**
  130. * @brief Deinitializes the DAC peripheral registers to their default reset values.
  131. * @param None
  132. * @retval None
  133. */
  134. void DAC_DeInit(void)
  135. {
  136. /* Set Channel1 the Configuration registers to their reset values */
  137. DAC->CH1CR1 = DAC_CR1_RESET_VALUE;
  138. DAC->CH1CR2 = DAC_CR2_RESET_VALUE;
  139. /* Set Channel2 the Configuration registers to their reset values */
  140. DAC->CH2CR1 = DAC_CR1_RESET_VALUE;
  141. DAC->CH2CR2 = DAC_CR2_RESET_VALUE;
  142. /* Set the Software Trigger configuration registers to their reset values */
  143. DAC->SWTRIGR = DAC_SWTRIGR_RESET_VALUE;
  144. /* Set the Status registers to their reset values */
  145. DAC->SR = (uint8_t)~DAC_SR_RESET_VALUE;
  146. /* Set the Channel1 Data holding registers to their reset values */
  147. DAC->CH1RDHRH = DAC_RDHRH_RESET_VALUE;
  148. DAC->CH1RDHRL = DAC_RDHRL_RESET_VALUE;
  149. DAC->CH1LDHRH = DAC_LDHRH_RESET_VALUE;
  150. DAC->CH1LDHRL = DAC_LDHRL_RESET_VALUE;
  151. DAC->CH1DHR8 = DAC_DHR8_RESET_VALUE;
  152. /* Set the Channel2 Data holding registers to their reset values */
  153. DAC->CH2RDHRH = DAC_RDHRH_RESET_VALUE;
  154. DAC->CH2RDHRL = DAC_RDHRL_RESET_VALUE;
  155. DAC->CH2LDHRH = DAC_LDHRH_RESET_VALUE;
  156. DAC->CH2LDHRL = DAC_LDHRL_RESET_VALUE;
  157. DAC->CH2DHR8 = DAC_DHR8_RESET_VALUE;
  158. /* Set the Dual mode 12bit Right Data holding registers to their reset values */
  159. DAC->DCH1RDHRH = DAC_RDHRH_RESET_VALUE;
  160. DAC->DCH1RDHRL = DAC_RDHRL_RESET_VALUE;
  161. DAC->DCH2RDHRH = DAC_RDHRH_RESET_VALUE;
  162. DAC->DCH2RDHRL = DAC_RDHRL_RESET_VALUE;
  163. /* Set the Dual mode 12bit Left Data holding registers to their reset values */
  164. DAC->DCH1LDHRH = DAC_LDHRH_RESET_VALUE;
  165. DAC->DCH1LDHRL = DAC_LDHRL_RESET_VALUE;
  166. DAC->DCH2LDHRH = DAC_LDHRH_RESET_VALUE;
  167. DAC->DCH2LDHRL = DAC_LDHRL_RESET_VALUE;
  168. /* Set the Dual mode 8bit Data holding registers to their reset values */
  169. DAC->DCH1DHR8 = DAC_DHR8_RESET_VALUE;
  170. DAC->DCH2DHR8 = DAC_DHR8_RESET_VALUE;
  171. }
  172. /**
  173. * @brief Initializes the DAC according to the specified parameters.
  174. * @param DAC_Channel: the selected DAC channel.
  175. * This parameter can be one of the following values:
  176. * @arg DAC_Channel_1: DAC Channel1 selected
  177. * @arg DAC_Channel_2: DAC Channel2 selected
  178. * @param DAC_Trigger : the selected DAC trigger.
  179. * This parameter can be one of the following values:
  180. * @arg DAC_Trigger_None: DAC trigger None
  181. * @arg DAC_Trigger_T4_TRGO: DAC trigger TIM4 TRGO
  182. * @arg DAC_Trigger_T5_TRGO: DAC trigger TIM5 TRGO
  183. * @arg DAC_Trigger_Ext: DAC trigger External Trigger (PE4)
  184. * @arg DAC_Trigger_Software: DAC trigger software
  185. * @param DAC_OutputBuffer : the status of DAC load Buffer
  186. * This parameter can be one of the following values:
  187. * @arg DAC_OutputBuffer_Enable: DAC output buffer Enabled
  188. * @arg DAC_OutputBuffer_Disable: DAC output buffer Disabled
  189. * @retval None
  190. */
  191. void DAC_Init(DAC_Channel_TypeDef DAC_Channel,
  192. DAC_Trigger_TypeDef DAC_Trigger,
  193. DAC_OutputBuffer_TypeDef DAC_OutputBuffer)
  194. {
  195. uint8_t tmpreg = 0;
  196. uint16_t tmpreg2 = 0;
  197. /* Check the DAC parameters */
  198. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  199. assert_param(IS_DAC_TRIGGER(DAC_Trigger));
  200. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_OutputBuffer));
  201. /* Get the DAC CHxCR1 value */
  202. tmpreg2 = (uint16_t)((uint8_t)((uint8_t)DAC_Channel << 1));
  203. tmpreg = *(uint8_t*)((uint16_t)(DAC_BASE + CR1_Offset + tmpreg2));
  204. /* Clear BOFFx, TENx, TSELx bits */
  205. tmpreg &= (uint8_t)~(DAC_CR1_BOFF | DAC_CR1_TEN | DAC_CR1_TSEL );
  206. /* Set BOFFx bit according to DAC_OutputBuffer value */
  207. tmpreg |= (uint8_t)(DAC_OutputBuffer);
  208. /* Configure for the selected DAC channel trigger*/
  209. if (DAC_Trigger != DAC_Trigger_None)
  210. {
  211. /* Set TSELx and TEN bits according to DAC_Trigger value */
  212. tmpreg |= (uint8_t)(DAC_CR1_TEN | DAC_Trigger) ;
  213. }
  214. /* Write to DAC CHxCR1 */
  215. *(uint8_t*)((uint16_t)(DAC_BASE + CR1_Offset + tmpreg2)) = (uint8_t)tmpreg;
  216. }
  217. /**
  218. * @brief Enables or disables the specified DAC channel.
  219. * @param DAC_Channel: the selected DAC channel.
  220. * This parameter can be one of the following values:
  221. * @arg DAC_Channel_1: DAC Channel1 selected
  222. * @arg DAC_Channel_2: DAC Channel2 selected
  223. * @param NewState: new state of the DAC channel.
  224. * This parameter can be: ENABLE or DISABLE.
  225. * @note When the DAC channel is enabled the trigger source can no more
  226. * be modified.
  227. * @retval None
  228. */
  229. void DAC_Cmd(DAC_Channel_TypeDef DAC_Channel, FunctionalState NewState)
  230. {
  231. uint16_t cr1addr = 0;
  232. /* Check the parameters */
  233. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  234. assert_param(IS_FUNCTIONAL_STATE(NewState));
  235. /* Find CHxCR1 register Address */
  236. cr1addr = DAC_BASE + CR1_Offset + (uint8_t)((uint8_t)DAC_Channel << 1);
  237. if (NewState != DISABLE)
  238. {
  239. /* Enable the selected DAC channel */
  240. (*(uint8_t*)(cr1addr)) |= DAC_CR1_EN;
  241. }
  242. else
  243. {
  244. /* Disable the selected DAC channel */
  245. (*(uint8_t*)(cr1addr)) &= (uint8_t) ~(DAC_CR1_EN);
  246. }
  247. }
  248. /**
  249. * @brief Enables or disables the selected DAC channel software trigger.
  250. * @param DAC_Channel: the selected DAC channel.
  251. * This parameter can be one of the following values:
  252. * @arg DAC_Channel_1: DAC Channel1 selected
  253. * @arg DAC_Channel_2: DAC Channel2 selected
  254. * @param NewState: new state of the selected DAC channel software trigger.
  255. * This parameter can be: ENABLE or DISABLE.
  256. * @retval None.
  257. */
  258. void DAC_SoftwareTriggerCmd(DAC_Channel_TypeDef DAC_Channel, FunctionalState NewState)
  259. {
  260. /* Check the parameters */
  261. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  262. assert_param(IS_FUNCTIONAL_STATE(NewState));
  263. if (NewState != DISABLE)
  264. {
  265. /* Enable software trigger for the selected DAC channel */
  266. DAC->SWTRIGR |= (uint8_t)(DAC_SWTRIGR_SWTRIG1 << DAC_Channel);
  267. }
  268. else
  269. {
  270. /* Disable software trigger for the selected DAC channel */
  271. DAC->SWTRIGR &= (uint8_t)~((uint8_t)(DAC_SWTRIGR_SWTRIG1 << DAC_Channel));
  272. }
  273. }
  274. /**
  275. * @brief Enables or disables simultaneously the two DAC channels software
  276. * triggers.
  277. * @param NewState: new state of the DAC channels software triggers.
  278. * This parameter can be: ENABLE or DISABLE.
  279. * @retval None.
  280. */
  281. void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)
  282. {
  283. /* Check the parameters */
  284. assert_param(IS_FUNCTIONAL_STATE(NewState));
  285. if (NewState != DISABLE)
  286. {
  287. /* Enable software trigger for both DAC channels */
  288. DAC->SWTRIGR |= (DAC_SWTRIGR_SWTRIG1 | DAC_SWTRIGR_SWTRIG2) ;
  289. }
  290. else
  291. {
  292. /* Disable software trigger for both DAC channels */
  293. DAC->SWTRIGR &= (uint8_t)~(DAC_SWTRIGR_SWTRIG1 | DAC_SWTRIGR_SWTRIG2);
  294. }
  295. }
  296. /**
  297. * @brief Enables or disables the selected DAC channel wave generation.
  298. * @param DAC_Channel: the selected DAC channel.
  299. * This parameter can be one of the following values:
  300. * @arg DAC_Channel_1: DAC Channel1 selected
  301. * @arg DAC_Channel_2: DAC Channel2 selected
  302. * @param DAC_Wave: Specifies the wave type to enable or disable.
  303. * This parameter can be one of the following values:
  304. * @arg DAC_Wave_Noise: noise wave generation
  305. * @arg DAC_Wave_Triangle: triangle wave generation
  306. * @param NewState: new state of the selected DAC channel wave generation.
  307. * This parameter can be: ENABLE or DISABLE.
  308. * @retval None
  309. */
  310. void DAC_WaveGenerationCmd(DAC_Channel_TypeDef DAC_Channel,
  311. DAC_Wave_TypeDef DAC_Wave,
  312. FunctionalState NewState)
  313. {
  314. uint8_t tmpreg = 0;
  315. /* Check the DAC parameters */
  316. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  317. assert_param(IS_DAC_WAVE(DAC_Wave));
  318. assert_param(IS_FUNCTIONAL_STATE(NewState));
  319. /* Get the DAC CHxCR1 value & Clear WAVEN bits */
  320. tmpreg = (uint8_t)((*(uint8_t*)(uint16_t)(DAC_BASE + CR1_Offset + (uint8_t)((uint8_t)DAC_Channel << 1))) & (uint8_t)~(DAC_CR1_WAVEN));
  321. if (NewState != DISABLE)
  322. {
  323. tmpreg |= (uint8_t)(DAC_Wave);
  324. }
  325. /* Write to DAC CHxCR1 */
  326. (*(uint8_t*) (uint16_t)(DAC_BASE + CR1_Offset + (uint8_t)((uint8_t)DAC_Channel << 1))) = tmpreg;
  327. }
  328. /**
  329. * @brief Select DAC Noise Wave Generation LFSR according to the specified parameters.
  330. * @param DAC_Channel: the selected DAC channel.
  331. * This parameter can be one of the following values:
  332. * @arg DAC_Channel_1: DAC Channel1 selected
  333. * @arg DAC_Channel_2: DAC Channel2 selected
  334. * @param DAC_LFSRUnmask : the selected unmasked bit.
  335. * This parameter can be one of the following values:
  336. * @arg DAC_LFSRUnmask_Bit0: Noise LFSR Unmask 1 LSB
  337. * @arg DAC_LFSRUnmask_Bits1_0: Noise LFSR Unmask 2 LSB
  338. * @arg DAC_LFSRUnmask_Bit2_0: Noise LFSR Unmask 3 LSB
  339. * @arg DAC_LFSRUnmask_Bit3_0: Noise LFSR Unmask 4 LSB
  340. * @arg DAC_LFSRUnmask_Bit4_0: Noise LFSR Unmask 5 LSB
  341. * @arg DAC_LFSRUnmask_Bit5_0: Noise LFSR Unmask 6 LSB
  342. * @arg DAC_LFSRUnmask_Bit6_0: Noise LFSR Unmask 7 LSB
  343. * @arg DAC_LFSRUnmask_Bit7_0: Noise LFSR Unmask 8 LSB
  344. * @arg DAC_LFSRUnmask_Bit8_0: Noise LFSR Unmask 9 LSB
  345. * @arg DAC_LFSRUnmask_Bit9_0: Noise LFSR Unmask 10 LSB
  346. * @arg DAC_LFSRUnmask_Bit10_0: Noise LFSR Unmask 11 LSB
  347. * @arg DAC_LFSRUnmask_Bit11_0: Noise LFSR Unmask 12 LSB
  348. * @retval None
  349. */
  350. void DAC_SetNoiseWaveLFSR(DAC_Channel_TypeDef DAC_Channel, DAC_LFSRUnmask_TypeDef DAC_LFSRUnmask)
  351. {
  352. uint8_t tmpreg = 0;
  353. uint16_t cr2addr = 0;
  354. /* Check the DAC parameters */
  355. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  356. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_LFSRUnmask));
  357. /* Get the DAC CHxCR2 value & Clear MAMPx bits */
  358. cr2addr = (uint16_t)(DAC_BASE + CR2_Offset + (uint8_t)((uint8_t)DAC_Channel << 1));
  359. tmpreg = (uint8_t)((*(uint8_t*)(cr2addr)) & (uint8_t)~(DAC_CR2_MAMPx));
  360. /* Write to DAC CHxCR2 */
  361. (*(uint8_t*)(cr2addr)) = (uint8_t)( tmpreg | DAC_LFSRUnmask);
  362. }
  363. /**
  364. * @brief Select DAC Triangle Wave Generation Amplitude according to the specified parameters.
  365. * @param DAC_Channel: the selected DAC channel.
  366. * This parameter can be one of the following values:
  367. * @arg DAC_Channel_1: DAC Channel1 selected
  368. * @arg DAC_Channel_2: DAC Channel2 selected
  369. * @param DAC_TriangleAmplitude : the selected Amplitude
  370. * This parameter can be one of the following values:
  371. * @arg DAC_TriangleAmplitude_1: Triangle Amplitude = Vref.(1/4096)
  372. * @arg DAC_TriangleAmplitude_3: Triangle Amplitude = Vref.(3/4096)
  373. * @arg DAC_TriangleAmplitude_7: Triangle Amplitude = Vref.(7/4096)
  374. * @arg DAC_TriangleAmplitude_15: Triangle Amplitude = Vref.(15/4096)
  375. * @arg DAC_TriangleAmplitude_31: Triangle Amplitude = Vref.(31/4096)
  376. * @arg DAC_TriangleAmplitude_63: Triangle Amplitude = Vref.(63/4096)
  377. * @arg DAC_TriangleAmplitude_127: Triangle Amplitude = Vref.(127/4096)
  378. * @arg DAC_TriangleAmplitude_255: Triangle Amplitude = Vref.(255/4096)
  379. * @arg DAC_TriangleAmplitude_511: Triangle Amplitude = Vref.(511/4096)
  380. * @arg DAC_TriangleAmplitude_1023: Triangle Amplitude = Vref.(1023/4096)
  381. * @arg DAC_TriangleAmplitude_2047: Triangle Amplitude = Vref.(2047/4096)
  382. * @arg DAC_TriangleAmplitude_4095: Triangle Amplitude = Vref.(4095/4096)
  383. * @retval None
  384. */
  385. void DAC_SetTriangleWaveAmplitude(DAC_Channel_TypeDef DAC_Channel, DAC_TriangleAmplitude_TypeDef DAC_TriangleAmplitude)
  386. {
  387. uint8_t tmpreg = 0;
  388. uint16_t cr2addr = 0;
  389. /* Check the DAC parameters */
  390. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  391. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_TriangleAmplitude));
  392. /* Get the DAC CHxCR2 value & Clear MAMPx bits */
  393. cr2addr = (uint16_t)(DAC_BASE + CR2_Offset + (uint8_t)((uint8_t)DAC_Channel << 1));
  394. tmpreg = (uint8_t)((*(uint8_t*)(cr2addr)) & (uint8_t)~(DAC_CR2_MAMPx));
  395. /* Write to DAC CHxCR2 */
  396. (*(uint8_t*)(cr2addr)) = (uint8_t)( tmpreg | DAC_TriangleAmplitude);
  397. }
  398. /**
  399. * @brief Set the specified data holding register value for DAC channel1.
  400. * @param DAC_Align: Specifies the data alignment for DAC channel1.
  401. * This parameter can be one of the following values:
  402. * @arg DAC_Align_8b_R: 8bit right data alignment selected
  403. * @arg DAC_Align_12b_L: 12bit left data alignment selected
  404. * @arg DAC_Align_12b_R: 12bit right data alignment selected
  405. * @param Data : Data to be loaded in the selected data holding register.
  406. * @retval None.
  407. */
  408. void DAC_SetChannel1Data(DAC_Align_TypeDef DAC_Align, uint16_t DAC_Data)
  409. {
  410. /* Check the parameters */
  411. assert_param(IS_DAC_ALIGN(DAC_Align));
  412. if (DAC_Align != DAC_Align_8b_R)
  413. {
  414. /* Set the DAC channel1 selected data holding register */
  415. *(uint8_t*)((uint16_t)(DAC_BASE + CH1RDHRH_Offset + DAC_Align )) = (uint8_t)(((uint16_t)DAC_Data) >> 8);
  416. *(uint8_t*)((uint16_t)(DAC_BASE + CH1RDHRH_Offset + 1 + DAC_Align )) = (uint8_t)DAC_Data;
  417. }
  418. else
  419. {
  420. /* Check the parameter */
  421. assert_param(IS_DAC_DATA_08R(DAC_Data));
  422. /* Set the DAC channel1 selected data holding register */
  423. DAC->CH1DHR8 = (uint8_t)(DAC_Data);
  424. }
  425. }
  426. /**
  427. * @brief Set the specified data holding register value for DAC channel2.
  428. * @param DAC_Align: Specifies the data alignment for DAC channel2.
  429. * This parameter can be one of the following values:
  430. * @arg DAC_Align_8b_R: 8bit right data alignment selected
  431. * @arg DAC_Align_12b_L: 12bit left data alignment selected
  432. * @arg DAC_Align_12b_R: 12bit right data alignment selected
  433. * @param Data : Data to be loaded in the selected data holding register.
  434. * @retval None.
  435. */
  436. void DAC_SetChannel2Data(DAC_Align_TypeDef DAC_Align, uint16_t DAC_Data)
  437. {
  438. /* Check the parameters */
  439. assert_param(IS_DAC_ALIGN(DAC_Align));
  440. if (DAC_Align != DAC_Align_8b_R)
  441. {
  442. /* Set the DAC channel2 selected data holding register */
  443. *(uint8_t*)((uint16_t)(DAC_BASE + CH2RDHRH_Offset + DAC_Align )) = (uint8_t)(((uint16_t)DAC_Data) >> 8);
  444. *(uint8_t*)((uint16_t)(DAC_BASE + CH2RDHRH_Offset + 1 + DAC_Align )) = (uint8_t)DAC_Data;
  445. }
  446. else
  447. {
  448. /* Check the parameter */
  449. assert_param(IS_DAC_DATA_08R(DAC_Data));
  450. /* Set the DAC channel2 selected data holding register */
  451. DAC->CH2DHR8 = (uint8_t)(DAC_Data);
  452. }
  453. }
  454. /**
  455. * @brief Set the specified data holding register value for dual channel DAC.
  456. * @param DAC_Align: Specifies the data alignment for dual channel DAC.
  457. * This parameter can be one of the following values:
  458. * @arg DAC_Align_8b_R: 8bit right data alignment selected
  459. * @arg DAC_Align_12b_L: 12bit left data alignment selected
  460. * @arg DAC_Align_12b_R: 12bit right data alignment selected
  461. * @param Data2: Data for DAC Channel2 to be loaded in the selected data
  462. * holding register.
  463. * @param Data1: Data for DAC Channel1 to be loaded in the selected data
  464. * holding register.
  465. * @note In dual mode, a unique register access is required to write in both
  466. * DAC channels at the same time.
  467. * @retval None.
  468. */
  469. void DAC_SetDualChannelData(DAC_Align_TypeDef DAC_Align, uint16_t DAC_Data2, uint16_t DAC_Data1)
  470. {
  471. uint16_t dchxrdhrhaddr = 0;
  472. /* Check the parameters */
  473. assert_param(IS_DAC_ALIGN(DAC_Align));
  474. if (DAC_Align != DAC_Align_8b_R)
  475. {
  476. /* Identify the DCHxRDHRH address*/
  477. dchxrdhrhaddr = (uint16_t)(DAC_BASE + DCH1RDHRH_Offset + DAC_Align);
  478. /* Set the DAC channels Dual data holding registers */
  479. *(uint8_t*)(uint16_t)dchxrdhrhaddr = (uint8_t)(((uint16_t)DAC_Data1) >> 8);
  480. *(uint8_t*)(uint16_t)(dchxrdhrhaddr + 1) = (uint8_t)DAC_Data1;
  481. *(uint8_t*)(uint16_t)(dchxrdhrhaddr + 2) = (uint8_t)(((uint16_t)DAC_Data2) >> 8);
  482. *(uint8_t*)(uint16_t)(dchxrdhrhaddr + 3) = (uint8_t)DAC_Data2;
  483. }
  484. else
  485. {
  486. /* Check the parameter */
  487. assert_param(IS_DAC_DATA_08R(DAC_Data1 | DAC_Data2));
  488. /* Set the DAC channels Dual data holding registers */
  489. DAC->DCH1DHR8 = (uint8_t)(DAC_Data1);
  490. DAC->DCH2DHR8 = (uint8_t)(DAC_Data2);
  491. }
  492. }
  493. /**
  494. * @brief Returns the last data output value of the selected DAC channel.
  495. * @param DAC_Channel: the selected DAC channel.
  496. * This parameter can be one of the following values:
  497. * @arg DAC_Channel_1: DAC Channel1 selected
  498. * @arg DAC_Channel_2: DAC Channel2 selected
  499. * @retval The selected DAC channel data output value.
  500. */
  501. uint16_t DAC_GetDataOutputValue(DAC_Channel_TypeDef DAC_Channel)
  502. {
  503. uint16_t outputdata = 0;
  504. uint16_t tmp = 0;
  505. /* Check the parameters */
  506. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  507. if ( DAC_Channel == DAC_Channel_1)
  508. {
  509. /* Returns the DAC channel data output register value */
  510. tmp = (uint16_t)((uint16_t)DAC->CH1DORH << 8);
  511. outputdata = (uint16_t)(tmp | (DAC->CH1DORL));
  512. }
  513. else
  514. {
  515. /* Returns the DAC channel data output register value */
  516. tmp = (uint16_t)((uint16_t)DAC->CH2DORH << 8);
  517. outputdata = (uint16_t)(tmp | (DAC->CH2DORL));
  518. }
  519. /* return the selected DAC channel data output value.*/
  520. return (uint16_t)outputdata;
  521. }
  522. /**
  523. * @}
  524. */
  525. /** @defgroup DAC_Group2 DMA management functions
  526. * @brief DMA management functions
  527. *
  528. @verbatim
  529. ===============================================================================
  530. DMA management function
  531. ===============================================================================
  532. @endverbatim
  533. * @{
  534. */
  535. /**
  536. * @brief Enables or disables the specified DAC channel DMA request.
  537. * When enabled DMA1 is generated when an external trigger occurs
  538. * @param DAC_Channel: the selected DAC channel.
  539. * This parameter can be one of the following values:
  540. * @arg DAC_Channel_1: DAC Channel1 selected
  541. * @arg DAC_Channel_2: DAC Channel2 selected
  542. * @param NewState: new state of the selected DAC channel DMA request.
  543. * This parameter can be: ENABLE or DISABLE.
  544. * The DAC channel1 (channel2) is mapped on DMA1 channel3 (channel1) which
  545. * must be already configured.
  546. * @retval None
  547. */
  548. void DAC_DMACmd(DAC_Channel_TypeDef DAC_Channel, FunctionalState NewState)
  549. {
  550. uint16_t cr2addr = 0;
  551. /* Check the parameters */
  552. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  553. assert_param(IS_FUNCTIONAL_STATE(NewState));
  554. /* Find CHxCR2 register Address */
  555. cr2addr = DAC_BASE + CR2_Offset + (uint8_t)((uint8_t)DAC_Channel << 1);
  556. if (NewState != DISABLE)
  557. {
  558. /* Enable the selected DAC channel DMA request */
  559. (*(uint8_t*)(cr2addr)) |= DAC_CR2_DMAEN;
  560. }
  561. else
  562. {
  563. /* Disable the selected DAC channel DMA request */
  564. (*(uint8_t*)(cr2addr)) &= (uint8_t)~(DAC_CR2_DMAEN);
  565. }
  566. }
  567. /**
  568. * @}
  569. */
  570. /** @defgroup DAC_Group3 Interrupts and flags management functions
  571. * @brief Interrupts and flags management functions
  572. *
  573. @verbatim
  574. ===============================================================================
  575. Interrupts and flags management functions
  576. ===============================================================================
  577. @endverbatim
  578. * @{
  579. */
  580. /**
  581. * @brief Enables or disables the specified DAC interrupts.
  582. * @param DAC_Channel: the selected DAC channel.
  583. * This parameter can be one of the following values:
  584. * @arg DAC_Channel_1: DAC Channel1 selected
  585. * @arg DAC_Channel_2: DAC Channel2 selected
  586. * @param DAC_IT: specifies the DAC interrupt sources to be enabled or disabled.
  587. * This parameter can be the following values:
  588. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  589. * @note The DMA underrun occurs when a second external trigger arrives before
  590. * the acknowledgement for the first external trigger is received (first request).
  591. * @param NewState: new state of the specified DAC interrupts.
  592. * This parameter can be: ENABLE or DISABLE.
  593. * @retval None
  594. */
  595. void DAC_ITConfig(DAC_Channel_TypeDef DAC_Channel, DAC_IT_TypeDef DAC_IT, FunctionalState NewState)
  596. {
  597. uint16_t cr2addr = 0;
  598. /* Check the parameters */
  599. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  600. assert_param(IS_FUNCTIONAL_STATE(NewState));
  601. assert_param(IS_DAC_IT(DAC_IT));
  602. /* Find CHxCR2 register Address */
  603. cr2addr = DAC_BASE + CR2_Offset + (uint8_t)((uint8_t)DAC_Channel << 1);
  604. if (NewState != DISABLE)
  605. {
  606. /* Enable the selected DAC interrupts */
  607. (*(uint8_t*)(cr2addr)) |= (uint8_t)(DAC_IT);
  608. }
  609. else
  610. {
  611. /* Disable the selected DAC interrupts */
  612. (*(uint8_t*)(cr2addr)) &= (uint8_t)(~(DAC_IT));
  613. }
  614. }
  615. /**
  616. * @brief Checks whether the specified DAC flag is set or not.
  617. * @param DAC_Channel: thee selected DAC channel.
  618. * This parameter can be one of the following values:
  619. * @arg DAC_Channel_1: DAC Channel1 selected
  620. * @arg DAC_Channel_2: DAC Channel2 selected
  621. * @param DAC_FLAG: specifies the flag to check.
  622. * This parameter can be only of the following value:
  623. * @arg DAC_FLAG_DMAUDR: DMA underrun flag
  624. * @note The DMA underrun occurs when a second external trigger arrives before
  625. * the acknowledgement for the first external trigger is received (first request).
  626. * @retval The new state of DAC_FLAG (SET or RESET).
  627. */
  628. FlagStatus DAC_GetFlagStatus(DAC_Channel_TypeDef DAC_Channel, DAC_FLAG_TypeDef DAC_FLAG)
  629. {
  630. FlagStatus flagstatus = RESET;
  631. uint8_t flag = 0;
  632. /* Check the parameters */
  633. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  634. assert_param(IS_DAC_FLAG(DAC_FLAG));
  635. flag = (uint8_t)(DAC_FLAG << DAC_Channel);
  636. /* Check the status of the specified DAC flag */
  637. if ((DAC->SR & flag ) != (uint8_t)RESET)
  638. {
  639. /* DAC FLAG is set */
  640. flagstatus = SET;
  641. }
  642. else
  643. {
  644. /* DAC FLAG is reset */
  645. flagstatus = RESET;
  646. }
  647. /* Return the DAC FLAG status */
  648. return flagstatus;
  649. }
  650. /**
  651. * @brief Clears the DAC channel's pending flags.
  652. * @param DAC_Channel: the selected DAC channel.
  653. * This parameter can be one of the following values:
  654. * @arg DAC_Channel_1: DAC Channel1 selected
  655. * @arg DAC_Channel_2: DAC Channel2 selected
  656. * @param DAC_FLAG: specifies the flag to clear.
  657. * This parameter can be of the following value:
  658. * @arg DAC_FLAG_DMAUDR: DMA underrun flag
  659. * @retval None
  660. */
  661. void DAC_ClearFlag(DAC_Channel_TypeDef DAC_Channel, DAC_FLAG_TypeDef DAC_FLAG)
  662. {
  663. uint8_t flag = 0;
  664. /* Check the parameters */
  665. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  666. assert_param(IS_DAC_FLAG(DAC_FLAG));
  667. /* identify the selected flag*/
  668. flag = (uint8_t)(DAC_FLAG << DAC_Channel);
  669. /* Clear the selected DAC flag */
  670. DAC->SR = (uint8_t)(~flag);
  671. }
  672. /**
  673. * @brief Checks whether the specified DAC interrupt has occurred or not.
  674. * @param DAC_Channel: the selected DAC channel.
  675. * This parameter can be one of the following values:
  676. * @arg DAC_Channel_1: DAC Channel1 selected
  677. * @arg DAC_Channel_2: DAC Channel2 selected
  678. * @param DAC_IT: specifies the DAC interrupt source to check.
  679. * This parameter can be the following values:
  680. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  681. * @note The DMA underrun occurs when a second external trigger arrives before
  682. * the acknowledgement for the first external trigger is received (first request).
  683. * @retval The new state of DAC_IT (SET or RESET).
  684. */
  685. ITStatus DAC_GetITStatus(DAC_Channel_TypeDef DAC_Channel, DAC_IT_TypeDef DAC_IT)
  686. {
  687. ITStatus itstatus = RESET;
  688. uint8_t enablestatus = 0;
  689. uint8_t flagstatus = 0;
  690. uint8_t tempreg = 0;
  691. /* Check the parameters */
  692. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  693. assert_param(IS_DAC_IT(DAC_IT));
  694. /* identify the status of the IT and its correspondent flag*/
  695. tempreg = *(uint8_t*)(uint16_t)(DAC_BASE + CR2_Offset + (uint8_t)((uint8_t)DAC_Channel << 2));
  696. enablestatus = (uint8_t)( tempreg & (uint8_t)((uint8_t)DAC_IT << DAC_Channel));
  697. flagstatus = (uint8_t)(DAC->SR & (uint8_t)(DAC_IT >> ((uint8_t)0x05 - DAC_Channel)));
  698. /* Check the status of the specified DAC interrupt */
  699. if (((flagstatus) != (uint8_t)RESET) && enablestatus)
  700. {
  701. /* DAC IT is set */
  702. itstatus = SET;
  703. }
  704. else
  705. {
  706. /* DAC IT is reset */
  707. itstatus = RESET;
  708. }
  709. /* Return the DAC IT status */
  710. return itstatus;
  711. }
  712. /**
  713. * @brief Clears the DAC channel's interrupt pending bits.
  714. * @param DAC_Channel: the selected DAC channel.
  715. * This parameter can be one of the following values:
  716. * @arg DAC_Channel_1: DAC Channel1 selected
  717. * @arg DAC_Channel_2: DAC Channel2 selected
  718. * @param DAC_IT: specifies the DAC interrupt pending bit to clear.
  719. * This parameter can be the following values:
  720. * @arg DAC_IT_DMAUDR: DMA underrun interrupt mask
  721. * @retval None
  722. */
  723. void DAC_ClearITPendingBit(DAC_Channel_TypeDef DAC_Channel, DAC_IT_TypeDef DAC_IT)
  724. {
  725. /* Check the parameters */
  726. assert_param(IS_DAC_CHANNEL(DAC_Channel));
  727. assert_param(IS_DAC_IT(DAC_IT));
  728. /* Clear the selected DAC interrupt pending bits */
  729. DAC->SR = (uint8_t)~(uint8_t)((uint8_t)DAC_IT >> (0x05 - DAC_Channel));
  730. }
  731. /**
  732. * @}
  733. */
  734. /**
  735. * @}
  736. */
  737. /**
  738. * @}
  739. */
  740. /**
  741. * @}
  742. */
  743. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/