gd32f3x0_dac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*!
  2. \file gd32f3x0_dac.c
  3. \brief DAC driver
  4. \version 2017-06-06, V1.0.0, firmware for GD32F3x0
  5. \version 2019-06-01, V2.0.0, firmware for GD32F3x0
  6. */
  7. /*
  8. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #ifdef GD32F350
  31. #include "gd32f3x0_dac.h"
  32. /*!
  33. \brief deinitialize DAC
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void dac_deinit(void)
  39. {
  40. rcu_periph_reset_enable(RCU_DACRST);
  41. rcu_periph_reset_disable(RCU_DACRST);
  42. }
  43. /*!
  44. \brief enable DAC
  45. \param[in] none
  46. \param[out] none
  47. \retval none
  48. */
  49. void dac_enable(void)
  50. {
  51. DAC_CTL |= DAC_CTL_DEN;
  52. }
  53. /*!
  54. \brief disable DAC
  55. \param[in] none
  56. \param[out] none
  57. \retval none
  58. */
  59. void dac_disable(void)
  60. {
  61. DAC_CTL &= ~DAC_CTL_DEN;
  62. }
  63. /*!
  64. \brief enable DAC DMA
  65. \param[in] none
  66. \param[out] none
  67. \retval none
  68. */
  69. void dac_dma_enable(void)
  70. {
  71. DAC_CTL |= DAC_CTL_DDMAEN;
  72. }
  73. /*!
  74. \brief disable DAC DMA
  75. \param[in] none
  76. \param[out] none
  77. \retval none
  78. */
  79. void dac_dma_disable(void)
  80. {
  81. DAC_CTL &= ~DAC_CTL_DDMAEN;
  82. }
  83. /*!
  84. \brief enable DAC output buffer
  85. \param[in] none
  86. \param[out] none
  87. \retval none
  88. */
  89. void dac_output_buffer_enable(void)
  90. {
  91. DAC_CTL &= ~DAC_CTL_DBOFF;
  92. }
  93. /*!
  94. \brief disable DAC output buffer
  95. \param[in] none
  96. \param[out] none
  97. \retval none
  98. */
  99. void dac_output_buffer_disable(void)
  100. {
  101. DAC_CTL |= DAC_CTL_DBOFF;
  102. }
  103. /*!
  104. \brief enable DAC trigger
  105. \param[in] none
  106. \param[out] none
  107. \retval none
  108. */
  109. void dac_trigger_enable(void)
  110. {
  111. DAC_CTL |= DAC_CTL_DTEN;
  112. }
  113. /*!
  114. \brief disable DAC trigger
  115. \param[in] none
  116. \param[out] none
  117. \retval none
  118. */
  119. void dac_trigger_disable(void)
  120. {
  121. DAC_CTL &= ~DAC_CTL_DTEN;
  122. }
  123. /*!
  124. \brief enable DAC software trigger
  125. \param[in] none
  126. \param[out] none
  127. \retval none
  128. */
  129. void dac_software_trigger_enable(void)
  130. {
  131. DAC_SWT |= DAC_SWT_SWTR;
  132. }
  133. /*!
  134. \brief disable DAC software trigger
  135. \param[in] none
  136. \param[out] none
  137. \retval none
  138. */
  139. void dac_software_trigger_disable(void)
  140. {
  141. DAC_SWT &= ~DAC_SWT_SWTR;
  142. }
  143. /*!
  144. \brief enable DAC interrupt(DAC DMA underrun interrupt)
  145. \param[in] none
  146. \param[out] none
  147. \retval none
  148. */
  149. void dac_interrupt_enable(void)
  150. {
  151. DAC_CTL |= DAC_CTL_DDUDRIE;
  152. }
  153. /*!
  154. \brief disable DAC interrupt(DAC DMA underrun interrupt)
  155. \param[in] none
  156. \param[out] none
  157. \retval none
  158. */
  159. void dac_interrupt_disable(void)
  160. {
  161. DAC_CTL &= ~DAC_CTL_DDUDRIE;
  162. }
  163. /*!
  164. \brief set DAC tgigger source
  165. \param[in] triggersource: external triggers of DAC
  166. \arg DAC_TRIGGER_T1_TRGO: trigger source is TIMER1 TRGO
  167. \arg DAC_TRIGGER_T2_TRGO: trigger source is TIMER2 TRGO
  168. \arg DAC_TRIGGER_T5_TRGO: trigger source is TIMER5 TRGO
  169. \arg DAC_TRIGGER_T14_TRGO: trigger source is TIMER14 TRGO
  170. \arg DAC_TRIGGER_EXTI_9: trigger source is EXTI interrupt line9 event
  171. \arg DAC_TRIGGER_SOFTWARE: software trigger
  172. \param[out] none
  173. \retval none
  174. */
  175. void dac_trigger_source_config(uint32_t triggersource)
  176. {
  177. DAC_CTL &= ~DAC_CTL_DTSEL;
  178. DAC_CTL |= triggersource;
  179. }
  180. /*!
  181. \brief configure DAC wave mode
  182. \param[in] wave_mode
  183. \arg DAC_WAVE_DISABLE: wave disable
  184. \arg DAC_WAVE_MODE_LFSR: LFSR noise mode
  185. \arg DAC_WAVE_MODE_TRIANGLE: triangle noise mode
  186. \param[out] none
  187. \retval none
  188. */
  189. void dac_wave_mode_config(uint32_t wave_mode)
  190. {
  191. DAC_CTL &= ~DAC_CTL_DWM;
  192. DAC_CTL |= wave_mode;
  193. }
  194. /*!
  195. \brief configure DAC wave bit width
  196. \param[in] bit_width
  197. \arg DAC_WAVE_BIT_WIDTH_1: bit width of the wave signal is 1
  198. \arg DAC_WAVE_BIT_WIDTH_2: bit width of the wave signal is 2
  199. \arg DAC_WAVE_BIT_WIDTH_3: bit width of the wave signal is 3
  200. \arg DAC_WAVE_BIT_WIDTH_4: bit width of the wave signal is 4
  201. \arg DAC_WAVE_BIT_WIDTH_5: bit width of the wave signal is 5
  202. \arg DAC_WAVE_BIT_WIDTH_6: bit width of the wave signal is 6
  203. \arg DAC_WAVE_BIT_WIDTH_7: bit width of the wave signal is 7
  204. \arg DAC_WAVE_BIT_WIDTH_8: bit width of the wave signal is 8
  205. \arg DAC_WAVE_BIT_WIDTH_9: bit width of the wave signal is 9
  206. \arg DAC_WAVE_BIT_WIDTH_10: bit width of the wave signal is 10
  207. \arg DAC_WAVE_BIT_WIDTH_11: bit width of the wave signal is 11
  208. \arg DAC_WAVE_BIT_WIDTH_12: bit width of the wave signal is 12
  209. \param[out] none
  210. \retval none
  211. */
  212. void dac_wave_bit_width_config(uint32_t bit_width)
  213. {
  214. DAC_CTL &= ~DAC_CTL_DWBW;
  215. DAC_CTL |= bit_width;
  216. }
  217. /*!
  218. \brief configure DAC LFSR noise mode
  219. \param[in] unmask_bits
  220. \arg DAC_LFSR_BIT0: unmask the LFSR bit0
  221. \arg DAC_LFSR_BITS1_0: unmask the LFSR bits[1:0]
  222. \arg DAC_LFSR_BITS2_0: unmask the LFSR bits[2:0]
  223. \arg DAC_LFSR_BITS3_0: unmask the LFSR bits[3:0]
  224. \arg DAC_LFSR_BITS4_0: unmask the LFSR bits[4:0]
  225. \arg DAC_LFSR_BITS5_0: unmask the LFSR bits[5:0]
  226. \arg DAC_LFSR_BITS6_0: unmask the LFSR bits[6:0]
  227. \arg DAC_LFSR_BITS7_0: unmask the LFSR bits[7:0]
  228. \arg DAC_LFSR_BITS8_0: unmask the LFSR bits[8:0]
  229. \arg DAC_LFSR_BITS9_0: unmask the LFSR bits[9:0]
  230. \arg DAC_LFSR_BITS10_0: unmask the LFSR bits[10:0]
  231. \arg DAC_LFSR_BITS11_0: unmask the LFSR bits[11:0]
  232. \param[out] none
  233. \retval none
  234. */
  235. void dac_lfsr_noise_config(uint32_t unmask_bits)
  236. {
  237. DAC_CTL &= ~DAC_CTL_DWBW;
  238. DAC_CTL |= unmask_bits;
  239. }
  240. /*!
  241. \brief configure DAC triangle noise mode
  242. \param[in] amplitude
  243. \arg DAC_TRIANGLE_AMPLITUDE_1: triangle amplitude is 1
  244. \arg DAC_TRIANGLE_AMPLITUDE_3: triangle amplitude is 3
  245. \arg DAC_TRIANGLE_AMPLITUDE_7: triangle amplitude is 7
  246. \arg DAC_TRIANGLE_AMPLITUDE_15: triangle amplitude is 15
  247. \arg DAC_TRIANGLE_AMPLITUDE_31: triangle amplitude is 31
  248. \arg DAC_TRIANGLE_AMPLITUDE_63: triangle amplitude is 63
  249. \arg DAC_TRIANGLE_AMPLITUDE_127: triangle amplitude is 127
  250. \arg DAC_TRIANGLE_AMPLITUDE_255: triangle amplitude is 255
  251. \arg DAC_TRIANGLE_AMPLITUDE_511: triangle amplitude is 511
  252. \arg DAC_TRIANGLE_AMPLITUDE_1023: triangle amplitude is 1023
  253. \arg DAC_TRIANGLE_AMPLITUDE_2047: triangle amplitude is 2047
  254. \arg DAC_TRIANGLE_AMPLITUDE_4095: triangle amplitude is 4095
  255. \param[out] none
  256. \retval none
  257. */
  258. void dac_triangle_noise_config(uint32_t amplitude)
  259. {
  260. DAC_CTL &= ~DAC_CTL_DWBW;
  261. DAC_CTL |= amplitude;
  262. }
  263. /*!
  264. \brief get DAC output value
  265. \param[in] none
  266. \param[out] none
  267. \retval DAC output data
  268. */
  269. uint16_t dac_output_value_get(void)
  270. {
  271. uint16_t data = 0U;
  272. data = (uint16_t)DAC_DO;
  273. return data;
  274. }
  275. /*!
  276. \brief get the specified DAC flag(DAC DMA underrun flag)
  277. \param[in] none
  278. \param[out] none
  279. \retval the state of dac bit(SET or RESET)
  280. */
  281. FlagStatus dac_flag_get(void)
  282. {
  283. /* check the DMA underrun flag */
  284. if((uint8_t)RESET != (DAC_STAT & DAC_STAT_DDUDR)){
  285. return SET;
  286. }else{
  287. return RESET;
  288. }
  289. }
  290. /*!
  291. \brief clear the specified DAC flag(DAC DMA underrun flag)
  292. \param[in] none
  293. \param[out] none
  294. \retval none
  295. */
  296. void dac_flag_clear(void)
  297. {
  298. DAC_STAT |= DAC_STAT_DDUDR;
  299. }
  300. /*!
  301. \brief get the specified DAC interrupt flag(DAC DMA underrun interrupt flag)
  302. \param[in] none
  303. \param[out] none
  304. \retval the state of DAC interrupt flag(SET or RESET)
  305. */
  306. FlagStatus dac_interrupt_flag_get(void)
  307. {
  308. FlagStatus temp_flag = RESET;
  309. uint32_t ddudr_flag = 0U, ddudrie_flag = 0U;
  310. /* check the DMA underrun flag and DAC DMA underrun interrupt enable flag */
  311. ddudr_flag = DAC_STAT & DAC_STAT_DDUDR;
  312. ddudrie_flag = DAC_CTL & DAC_CTL_DDUDRIE;
  313. if((RESET != ddudr_flag) && (RESET != ddudrie_flag)){
  314. temp_flag = SET;
  315. }
  316. return temp_flag;
  317. }
  318. /*!
  319. \brief clear the specified DAC interrupt flag(DAC DMA underrun interrupt flag)
  320. \param[in] none
  321. \param[out] none
  322. \retval none
  323. */
  324. void dac_interrupt_flag_clear(void)
  325. {
  326. DAC_STAT |= DAC_STAT_DDUDR;
  327. }
  328. /*!
  329. \brief set DAC data holding register value
  330. \param[in] dac_align
  331. \arg DAC_ALIGN_8B_R: data right 8b alignment
  332. \arg DAC_ALIGN_12B_R: data right 12b alignment
  333. \arg DAC_ALIGN_12B_L: data left 12b alignment
  334. \param[in] data: data to be loaded
  335. \param[out] none
  336. \retval none
  337. */
  338. void dac_data_set(uint32_t dac_align, uint16_t data)
  339. {
  340. switch(dac_align){
  341. /* data right 12b alignment */
  342. case DAC_ALIGN_12B_R:
  343. DAC_R12DH = data;
  344. break;
  345. /* data left 12b alignment */
  346. case DAC_ALIGN_12B_L:
  347. DAC_L12DH = data;
  348. break;
  349. /* data right 8b alignment */
  350. case DAC_ALIGN_8B_R:
  351. DAC_R8DH = data;
  352. break;
  353. default:
  354. break;
  355. }
  356. }
  357. #endif /* GD32F350 */