gd32f3x0_dac.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*!
  2. \file gd32f3x0_dac.c
  3. \brief DAC driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #ifdef GD32F350
  10. #include "gd32f3x0_dac.h"
  11. /*!
  12. \brief deinitialize DAC
  13. \param[in] none
  14. \param[out] none
  15. \retval none
  16. */
  17. void dac_deinit(void)
  18. {
  19. rcu_periph_reset_enable(RCU_DACRST);
  20. rcu_periph_reset_disable(RCU_DACRST);
  21. }
  22. /*!
  23. \brief enable DAC
  24. \param[in] none
  25. \param[out] none
  26. \retval none
  27. */
  28. void dac_enable(void)
  29. {
  30. DAC_CTL |= DAC_CTL_DEN;
  31. }
  32. /*!
  33. \brief disable DAC
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void dac_disable(void)
  39. {
  40. DAC_CTL &= ~DAC_CTL_DEN;
  41. }
  42. /*!
  43. \brief enable DAC DMA
  44. \param[in] none
  45. \param[out] none
  46. \retval none
  47. */
  48. void dac_dma_enable(void)
  49. {
  50. DAC_CTL |= DAC_CTL_DDMAEN;
  51. }
  52. /*!
  53. \brief disable DAC DMA
  54. \param[in] none
  55. \param[out] none
  56. \retval none
  57. */
  58. void dac_dma_disable(void)
  59. {
  60. DAC_CTL &= ~DAC_CTL_DDMAEN;
  61. }
  62. /*!
  63. \brief enable DAC output buffer
  64. \param[in] none
  65. \param[out] none
  66. \retval none
  67. */
  68. void dac_output_buffer_enable(void)
  69. {
  70. DAC_CTL &= ~DAC_CTL_DBOFF;
  71. }
  72. /*!
  73. \brief disable DAC output buffer
  74. \param[in] none
  75. \param[out] none
  76. \retval none
  77. */
  78. void dac_output_buffer_disable(void)
  79. {
  80. DAC_CTL |= DAC_CTL_DBOFF;
  81. }
  82. /*!
  83. \brief enable DAC trigger
  84. \param[in] none
  85. \param[out] none
  86. \retval none
  87. */
  88. void dac_trigger_enable(void)
  89. {
  90. DAC_CTL |= DAC_CTL_DTEN;
  91. }
  92. /*!
  93. \brief disable DAC trigger
  94. \param[in] none
  95. \param[out] none
  96. \retval none
  97. */
  98. void dac_trigger_disable(void)
  99. {
  100. DAC_CTL &= ~DAC_CTL_DTEN;
  101. }
  102. /*!
  103. \brief enable DAC software trigger
  104. \param[in] none
  105. \param[out] none
  106. \retval none
  107. */
  108. void dac_software_trigger_enable(void)
  109. {
  110. DAC_SWT |= DAC_SWT_SWTR;
  111. }
  112. /*!
  113. \brief disable DAC software trigger
  114. \param[in] none
  115. \param[out] none
  116. \retval none
  117. */
  118. void dac_software_trigger_disable(void)
  119. {
  120. DAC_SWT &= ~DAC_SWT_SWTR;
  121. }
  122. /*!
  123. \brief enable DAC interrupt(DAC DMA underrun interrupt)
  124. \param[in] none
  125. \param[out] none
  126. \retval none
  127. */
  128. void dac_interrupt_enable(void)
  129. {
  130. DAC_CTL |= DAC_CTL_DDUDRIE;
  131. }
  132. /*!
  133. \brief disable DAC interrupt(DAC DMA underrun interrupt)
  134. \param[in] none
  135. \param[out] none
  136. \retval none
  137. */
  138. void dac_interrupt_disable(void)
  139. {
  140. DAC_CTL &= ~DAC_CTL_DDUDRIE;
  141. }
  142. /*!
  143. \brief set DAC tgigger source
  144. \param[in] triggersource: external triggers of DAC
  145. \arg DAC_TRIGGER_T1_TRGO: trigger source is TIMER1 TRGO
  146. \arg DAC_TRIGGER_T2_TRGO: trigger source is TIMER2 TRGO
  147. \arg DAC_TRIGGER_T5_TRGO: trigger source is TIMER5 TRGO
  148. \arg DAC_TRIGGER_T14_TRGO: trigger source is TIMER14 TRGO
  149. \arg DAC_TRIGGER_EXTI_IT9: trigger source is EXTI interrupt line9 event
  150. \arg DAC_TRIGGER_SOFTWARE: software trigger
  151. \param[out] none
  152. \retval none
  153. */
  154. void dac_trigger_source_config(uint32_t triggersource)
  155. {
  156. DAC_CTL &= ~DAC_CTL_DTSEL;
  157. DAC_CTL |= triggersource;
  158. }
  159. /*!
  160. \brief configure DAC wave mode
  161. \param[in] wave_mode
  162. \arg DAC_WAVE_DISABLE: wave disable
  163. \arg DAC_WAVE_MODE_LFSR: LFSR noise mode
  164. \arg DAC_WAVE_MODE_TRIANGLE: triangle noise mode
  165. \param[out] none
  166. \retval none
  167. */
  168. void dac_wave_mode_config(uint32_t wave_mode)
  169. {
  170. DAC_CTL &= ~DAC_CTL_DWM;
  171. DAC_CTL |= wave_mode;
  172. }
  173. /*!
  174. \brief configure DAC wave bit width
  175. \param[in] bit_width
  176. \arg DAC_WAVE_BIT_WIDTH_1: bit width of the wave signal is 1
  177. \arg DAC_WAVE_BIT_WIDTH_2: bit width of the wave signal is 2
  178. \arg DAC_WAVE_BIT_WIDTH_3: bit width of the wave signal is 3
  179. \arg DAC_WAVE_BIT_WIDTH_4: bit width of the wave signal is 4
  180. \arg DAC_WAVE_BIT_WIDTH_5: bit width of the wave signal is 5
  181. \arg DAC_WAVE_BIT_WIDTH_6: bit width of the wave signal is 6
  182. \arg DAC_WAVE_BIT_WIDTH_7: bit width of the wave signal is 7
  183. \arg DAC_WAVE_BIT_WIDTH_8: bit width of the wave signal is 8
  184. \arg DAC_WAVE_BIT_WIDTH_9: bit width of the wave signal is 9
  185. \arg DAC_WAVE_BIT_WIDTH_10: bit width of the wave signal is 10
  186. \arg DAC_WAVE_BIT_WIDTH_11: bit width of the wave signal is 11
  187. \arg DAC_WAVE_BIT_WIDTH_12: bit width of the wave signal is 12
  188. \param[out] none
  189. \retval none
  190. */
  191. void dac_wave_bit_width_config(uint32_t bit_width)
  192. {
  193. DAC_CTL &= ~DAC_CTL_DWBW;
  194. DAC_CTL |= bit_width;
  195. }
  196. /*!
  197. \brief configure DAC LFSR noise mode
  198. \param[in] unmask_bits
  199. \arg DAC_LFSR_BIT0: unmask the LFSR bit0
  200. \arg DAC_LFSR_BITS1_0: unmask the LFSR bits[1:0]
  201. \arg DAC_LFSR_BITS2_0: unmask the LFSR bits[2:0]
  202. \arg DAC_LFSR_BITS3_0: unmask the LFSR bits[3:0]
  203. \arg DAC_LFSR_BITS4_0: unmask the LFSR bits[4:0]
  204. \arg DAC_LFSR_BITS5_0: unmask the LFSR bits[5:0]
  205. \arg DAC_LFSR_BITS6_0: unmask the LFSR bits[6:0]
  206. \arg DAC_LFSR_BITS7_0: unmask the LFSR bits[7:0]
  207. \arg DAC_LFSR_BITS8_0: unmask the LFSR bits[8:0]
  208. \arg DAC_LFSR_BITS9_0: unmask the LFSR bits[9:0]
  209. \arg DAC_LFSR_BITS10_0: unmask the LFSR bits[10:0]
  210. \arg DAC_LFSR_BITS11_0: unmask the LFSR bits[11:0]
  211. \param[out] none
  212. \retval none
  213. */
  214. void dac_lfsr_noise_config(uint32_t unmask_bits)
  215. {
  216. DAC_CTL &= ~DAC_CTL_DWBW;
  217. DAC_CTL |= unmask_bits;
  218. }
  219. /*!
  220. \brief configure DAC triangle noise mode
  221. \param[in] amplitude
  222. \arg DAC_TRIANGLE_AMPLITUDE_1: triangle amplitude is 1
  223. \arg DAC_TRIANGLE_AMPLITUDE_3: triangle amplitude is 3
  224. \arg DAC_TRIANGLE_AMPLITUDE_7: triangle amplitude is 7
  225. \arg DAC_TRIANGLE_AMPLITUDE_15: triangle amplitude is 15
  226. \arg DAC_TRIANGLE_AMPLITUDE_31: triangle amplitude is 31
  227. \arg DAC_TRIANGLE_AMPLITUDE_63: triangle amplitude is 63
  228. \arg DAC_TRIANGLE_AMPLITUDE_127: triangle amplitude is 127
  229. \arg DAC_TRIANGLE_AMPLITUDE_255: triangle amplitude is 255
  230. \arg DAC_TRIANGLE_AMPLITUDE_511: triangle amplitude is 511
  231. \arg DAC_TRIANGLE_AMPLITUDE_1023: triangle amplitude is 1023
  232. \arg DAC_TRIANGLE_AMPLITUDE_2047: triangle amplitude is 2047
  233. \arg DAC_TRIANGLE_AMPLITUDE_4095: triangle amplitude is 4095
  234. \param[out] none
  235. \retval none
  236. */
  237. void dac_triangle_noise_config(uint32_t amplitude)
  238. {
  239. DAC_CTL &= ~DAC_CTL_DWBW;
  240. DAC_CTL |= amplitude;
  241. }
  242. /*!
  243. \brief get DAC output value
  244. \param[in] none
  245. \param[out] none
  246. \retval DAC output data
  247. */
  248. uint16_t dac_output_value_get(void)
  249. {
  250. uint16_t data = 0U;
  251. data = (uint16_t)DAC_DO;
  252. return data;
  253. }
  254. /*!
  255. \brief get the specified DAC flag(DAC DMA underrun flag)
  256. \param[in] none
  257. \param[out] none
  258. \retval the state of dac bit(SET or RESET)
  259. */
  260. FlagStatus dac_flag_get(void)
  261. {
  262. /* check the DMA underrun flag */
  263. if((uint8_t)RESET != (DAC_STAT & DAC_STAT_DDUDR)){
  264. return SET;
  265. }else{
  266. return RESET;
  267. }
  268. }
  269. /*!
  270. \brief clear the specified DAC flag(DAC DMA underrun flag)
  271. \param[in] none
  272. \param[out] none
  273. \retval none
  274. */
  275. void dac_flag_clear(void)
  276. {
  277. DAC_STAT &= ~DAC_STAT_DDUDR;
  278. }
  279. /*!
  280. \brief get the specified DAC interrupt flag(DAC DMA underrun interrupt flag)
  281. \param[in] none
  282. \param[out] none
  283. \retval the state of DAC interrupt flag(SET or RESET)
  284. */
  285. FlagStatus dac_interrupt_flag_get(void)
  286. {
  287. FlagStatus temp_flag = RESET;
  288. uint32_t ddudr_flag = 0U, ddudrie_flag = 0U;
  289. /* check the DMA underrun flag and DAC DMA underrun interrupt enable flag */
  290. ddudr_flag = DAC_STAT & DAC_STAT_DDUDR;
  291. ddudrie_flag = DAC_CTL & DAC_CTL_DDUDRIE;
  292. if((RESET != ddudr_flag) && (RESET != ddudrie_flag)){
  293. temp_flag = SET;
  294. }
  295. return temp_flag;
  296. }
  297. /*!
  298. \brief clear the specified DAC interrupt flag(DAC DMA underrun interrupt flag)
  299. \param[in] none
  300. \param[out] none
  301. \retval none
  302. */
  303. void dac_interrupt_flag_clear(void)
  304. {
  305. DAC_CTL &= ~DAC_CTL_DDUDRIE;
  306. }
  307. /*!
  308. \brief set DAC data holding register value
  309. \param[in] dac_align
  310. \arg DAC_ALIGN_8B_R: data right 8b alignment
  311. \arg DAC_ALIGN_12B_R: data right 12b alignment
  312. \arg DAC_ALIGN_12B_L: data left 12b alignment
  313. \param[in] data: data to be loaded
  314. \param[out] none
  315. \retval none
  316. */
  317. void dac_data_set(uint32_t dac_align, uint16_t data)
  318. {
  319. switch(dac_align){
  320. /* data right 12b alignment */
  321. case DAC_ALIGN_12B_R:
  322. DAC_R12DH = data;
  323. break;
  324. /* data left 12b alignment */
  325. case DAC_ALIGN_12B_L:
  326. DAC_L12DH = data;
  327. break;
  328. /* data right 8b alignment */
  329. case DAC_ALIGN_8B_R:
  330. DAC_R8DH = data;
  331. break;
  332. default:
  333. break;
  334. }
  335. }
  336. #endif /* GD32F350 */