gd32f30x_dac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*!
  2. \file gd32f30x_dac.c
  3. \brief DAC driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-02-10, V1.0.0, firmware for GD32F30x
  8. */
  9. #include "gd32f30x_dac.h"
  10. /*!
  11. \brief deinitialize DAC
  12. \param[in] none
  13. \param[out] none
  14. \retval none
  15. */
  16. void dac_deinit(void)
  17. {
  18. rcu_periph_reset_enable(RCU_DACRST);
  19. rcu_periph_reset_disable(RCU_DACRST);
  20. }
  21. /*!
  22. \brief enable DAC
  23. \param[in] dac_periph
  24. \arg DACx(x =0,1)
  25. \param[out] none
  26. \retval none
  27. */
  28. void dac_enable(uint32_t dac_periph)
  29. {
  30. if(DAC0 == dac_periph){
  31. DAC_CTL |= DAC_CTL_DEN0;
  32. }else{
  33. DAC_CTL |= DAC_CTL_DEN1;
  34. }
  35. }
  36. /*!
  37. \brief disable DAC
  38. \param[in] dac_periph
  39. \arg DACx(x =0,1)
  40. \param[out] none
  41. \retval none
  42. */
  43. void dac_disable(uint32_t dac_periph)
  44. {
  45. if(DAC0 == dac_periph){
  46. DAC_CTL &= ~DAC_CTL_DEN0;
  47. }else{
  48. DAC_CTL &= ~DAC_CTL_DEN1;
  49. }
  50. }
  51. /*!
  52. \brief enable DAC DMA function
  53. \param[in] dac_periph
  54. \arg DACx(x=0,1)
  55. \param[out] none
  56. \retval none
  57. */
  58. void dac_dma_enable(uint32_t dac_periph)
  59. {
  60. if(DAC0 == dac_periph){
  61. DAC_CTL |= DAC_CTL_DDMAEN0;
  62. }else{
  63. DAC_CTL |= DAC_CTL_DDMAEN1;
  64. }
  65. }
  66. /*!
  67. \brief disable DAC DMA function
  68. \param[in] dac_periph
  69. \arg DACx(x=0,1)
  70. \param[out] none
  71. \retval none
  72. */
  73. void dac_dma_disable(uint32_t dac_periph)
  74. {
  75. if(DAC0 == dac_periph){
  76. DAC_CTL &= ~DAC_CTL_DDMAEN0;
  77. }else{
  78. DAC_CTL &= ~DAC_CTL_DDMAEN1;
  79. }
  80. }
  81. /*!
  82. \brief enable DAC output buffer
  83. \param[in] dac_periph
  84. \arg DACx(x =0,1)
  85. \param[out] none
  86. \retval none
  87. */
  88. void dac_output_buffer_enable(uint32_t dac_periph)
  89. {
  90. if(DAC0 == dac_periph){
  91. DAC_CTL &= ~DAC_CTL_DBOFF0;
  92. }else{
  93. DAC_CTL &= ~DAC_CTL_DBOFF1;
  94. }
  95. }
  96. /*!
  97. \brief disable DAC output buffer
  98. \param[in] dac_periph
  99. \arg DACx(x =0,1)
  100. \param[out] none
  101. \retval none
  102. */
  103. void dac_output_buffer_disable(uint32_t dac_periph)
  104. {
  105. if(DAC0 == dac_periph){
  106. DAC_CTL |= DAC_CTL_DBOFF0;
  107. }else{
  108. DAC_CTL |= DAC_CTL_DBOFF1;
  109. }
  110. }
  111. /*!
  112. \brief enable DAC trigger
  113. \param[in] dac_periph
  114. \arg DACx(x =0,1)
  115. \param[out] none
  116. \retval none
  117. */
  118. void dac_trigger_enable(uint32_t dac_periph)
  119. {
  120. if(DAC0 == dac_periph){
  121. DAC_CTL |= DAC_CTL_DTEN0;
  122. }else{
  123. DAC_CTL |= DAC_CTL_DTEN1;
  124. }
  125. }
  126. /*!
  127. \brief disable DAC trigger
  128. \param[in] dac_periph
  129. \arg DACx(x =0,1)
  130. \param[out] none
  131. \retval none
  132. */
  133. void dac_trigger_disable(uint32_t dac_periph)
  134. {
  135. if(DAC0 == dac_periph){
  136. DAC_CTL &= ~DAC_CTL_DTEN0;
  137. }else{
  138. DAC_CTL &= ~DAC_CTL_DTEN1;
  139. }
  140. }
  141. /*!
  142. \brief enable DAC software trigger
  143. \param[in] dac_periph
  144. \arg DACx(x =0,1)
  145. \retval none
  146. */
  147. void dac_software_trigger_enable(uint32_t dac_periph)
  148. {
  149. if(DAC0 == dac_periph){
  150. DAC_SWT |= DAC_SWT_SWTR0;
  151. }else{
  152. DAC_SWT |= DAC_SWT_SWTR1;
  153. }
  154. }
  155. /*!
  156. \brief disable DAC software trigger
  157. \param[in] dac_periph
  158. \arg DACx(x =0,1)
  159. \param[out] none
  160. \retval none
  161. */
  162. void dac_software_trigger_disable(uint32_t dac_periph)
  163. {
  164. if(DAC0 == dac_periph){
  165. DAC_SWT &= ~DAC_SWT_SWTR0;
  166. }else{
  167. DAC_SWT &= ~DAC_SWT_SWTR1;
  168. }
  169. }
  170. /*!
  171. \brief set DAC trigger source
  172. \param[in] dac_periph
  173. \arg DACx(x =0,1)
  174. \param[in] triggersource: external triggers of DAC
  175. \arg DAC_TRIGGER_T1_TRGO: TIMER1 TRGO
  176. \arg DAC_TRIGGER_T2_TRGO: TIMER2 TRGO (for GD32F30X_CL)
  177. \arg DAC_TRIGGER_T3_TRGO: TIMER3 TRGO
  178. \arg DAC_TRIGGER_T4_TRGO: TIMER4 TRGO
  179. \arg DAC_TRIGGER_T5_TRGO: TIMER5 TRGO
  180. \arg DAC_TRIGGER_T6_TRGO: TIMER6 TRGO
  181. \arg DAC_TRIGGER_T7_TRGO: TIMER7 TRGO (for GD32F30X_HD and GD32F30X_XD)
  182. \arg DAC_TRIGGER_EXTI_9: EXTI interrupt line9 event
  183. \arg DAC_TRIGGER_SOFTWARE: software trigger
  184. \param[out] none
  185. \retval none
  186. */
  187. void dac_trigger_source_config(uint32_t dac_periph,uint32_t triggersource)
  188. {
  189. if(DAC0 == dac_periph){
  190. DAC_CTL &= ~DAC_CTL_DTSEL0;
  191. DAC_CTL |= triggersource;
  192. }else{
  193. DAC_CTL &= ~DAC_CTL_DTSEL1;
  194. DAC_CTL |= (triggersource << 16);
  195. }
  196. }
  197. /*!
  198. \brief configure DAC wave mode
  199. \param[in] dac_periph
  200. \arg DACx(x=0,1)
  201. \param[in] wave_mode
  202. \arg DAC_WAVE_DISABLE: wave disable
  203. \arg DAC_WAVE_MODE_LFSR: LFSR noise mode
  204. \arg DAC_WAVE_MODE_TRIANGLE: triangle noise mode
  205. \param[out] none
  206. \retval none
  207. */
  208. void dac_wave_mode_config(uint32_t dac_periph, uint32_t wave_mode)
  209. {
  210. if(DAC0 == dac_periph){
  211. DAC_CTL &= ~DAC_CTL_DWM0;
  212. DAC_CTL |= wave_mode;
  213. }else{
  214. DAC_CTL &= ~DAC_CTL_DWM1;
  215. DAC_CTL |= wave_mode << 16;
  216. }
  217. }
  218. /*!
  219. \brief configure DAC wave bit width
  220. \param[in] dac_periph
  221. \arg DACx(x=0,1)
  222. \param[in] bit_width
  223. \arg DAC_WAVE_BIT_WIDTH_1: bit width of the wave signal is 1
  224. \arg DAC_WAVE_BIT_WIDTH_2: bit width of the wave signal is 2
  225. \arg DAC_WAVE_BIT_WIDTH_3: bit width of the wave signal is 3
  226. \arg DAC_WAVE_BIT_WIDTH_4: bit width of the wave signal is 4
  227. \arg DAC_WAVE_BIT_WIDTH_5: bit width of the wave signal is 5
  228. \arg DAC_WAVE_BIT_WIDTH_6: bit width of the wave signal is 6
  229. \arg DAC_WAVE_BIT_WIDTH_7: bit width of the wave signal is 7
  230. \arg DAC_WAVE_BIT_WIDTH_8: bit width of the wave signal is 8
  231. \arg DAC_WAVE_BIT_WIDTH_9: bit width of the wave signal is 9
  232. \arg DAC_WAVE_BIT_WIDTH_10: bit width of the wave signal is 10
  233. \arg DAC_WAVE_BIT_WIDTH_11: bit width of the wave signal is 11
  234. \arg DAC_WAVE_BIT_WIDTH_12: bit width of the wave signal is 12
  235. \param[out] none
  236. \retval none
  237. */
  238. void dac_wave_bit_width_config(uint32_t dac_periph, uint32_t bit_width)
  239. {
  240. if(DAC0 == dac_periph){
  241. DAC_CTL &= ~DAC_CTL_DWBW0;
  242. DAC_CTL |= bit_width;
  243. }else{
  244. DAC_CTL &= ~DAC_CTL_DWBW1;
  245. DAC_CTL |= bit_width << 16;
  246. }
  247. }
  248. /*!
  249. \brief configure DAC LFSR noise mode
  250. \param[in] dac_periph
  251. \arg DACx(x=0,1)
  252. \param[in] unmask_bits
  253. \arg DAC_LFSR_BIT0: unmask the LFSR bit0
  254. \arg DAC_LFSR_BITS1_0: unmask the LFSR bits[1:0]
  255. \arg DAC_LFSR_BITS2_0: unmask the LFSR bits[2:0]
  256. \arg DAC_LFSR_BITS3_0: unmask the LFSR bits[3:0]
  257. \arg DAC_LFSR_BITS4_0: unmask the LFSR bits[4:0]
  258. \arg DAC_LFSR_BITS5_0: unmask the LFSR bits[5:0]
  259. \arg DAC_LFSR_BITS6_0: unmask the LFSR bits[6:0]
  260. \arg DAC_LFSR_BITS7_0: unmask the LFSR bits[7:0]
  261. \arg DAC_LFSR_BITS8_0: unmask the LFSR bits[8:0]
  262. \arg DAC_LFSR_BITS9_0: unmask the LFSR bits[9:0]
  263. \arg DAC_LFSR_BITS10_0: unmask the LFSR bits[10:0]
  264. \arg DAC_LFSR_BITS11_0: unmask the LFSR bits[11:0]
  265. \param[out] none
  266. \retval none
  267. */
  268. void dac_lfsr_noise_config(uint32_t dac_periph, uint32_t unmask_bits)
  269. {
  270. if(DAC0 == dac_periph){
  271. DAC_CTL &= ~DAC_CTL_DWBW0;
  272. DAC_CTL |= unmask_bits;
  273. }else{
  274. DAC_CTL &= ~DAC_CTL_DWBW1;
  275. DAC_CTL |= unmask_bits << 16;
  276. }
  277. }
  278. /*!
  279. \brief configure DAC triangle noise mode
  280. \param[in] dac_periph
  281. \arg DACx(x=0,1)
  282. \param[in] amplitude
  283. \arg DAC_TRIANGLE_AMPLITUDE_1: triangle amplitude is 1
  284. \arg DAC_TRIANGLE_AMPLITUDE_3: triangle amplitude is 3
  285. \arg DAC_TRIANGLE_AMPLITUDE_7: triangle amplitude is 7
  286. \arg DAC_TRIANGLE_AMPLITUDE_15: triangle amplitude is 15
  287. \arg DAC_TRIANGLE_AMPLITUDE_31: triangle amplitude is 31
  288. \arg DAC_TRIANGLE_AMPLITUDE_63: triangle amplitude is 63
  289. \arg DAC_TRIANGLE_AMPLITUDE_127: triangle amplitude is 127
  290. \arg DAC_TRIANGLE_AMPLITUDE_255: triangle amplitude is 255
  291. \arg DAC_TRIANGLE_AMPLITUDE_511: triangle amplitude is 511
  292. \arg DAC_TRIANGLE_AMPLITUDE_1023: triangle amplitude is 1023
  293. \arg DAC_TRIANGLE_AMPLITUDE_2047: triangle amplitude is 2047
  294. \arg DAC_TRIANGLE_AMPLITUDE_4095: triangle amplitude is 4095
  295. \param[out] none
  296. \retval none
  297. */
  298. void dac_triangle_noise_config(uint32_t dac_periph, uint32_t amplitude)
  299. {
  300. if(DAC0 == dac_periph){
  301. DAC_CTL &= ~DAC_CTL_DWBW0;
  302. DAC_CTL |= amplitude;
  303. }else{
  304. DAC_CTL &= ~DAC_CTL_DWBW1;
  305. DAC_CTL |= amplitude << 16;
  306. }
  307. }
  308. /*!
  309. \brief get DAC output value
  310. \param[in] dac_periph
  311. \arg DACx(x=0,1)
  312. \param[out] none
  313. \retval DAC output data
  314. */
  315. uint16_t dac_output_value_get(uint32_t dac_periph)
  316. {
  317. uint16_t data = 0U;
  318. if(DAC0 == dac_periph){
  319. data = (uint16_t)DAC0_DO;
  320. }else{
  321. data = (uint16_t)DAC1_DO;
  322. }
  323. return data;
  324. }
  325. /*!
  326. \brief enable DAC concurrent mode
  327. \param[in] none
  328. \param[out] none
  329. \retval none
  330. */
  331. void dac_concurrent_enable(void)
  332. {
  333. uint32_t ctl = 0U;
  334. ctl = DAC_CTL_DEN0 | DAC_CTL_DEN1;
  335. DAC_CTL |= (ctl);
  336. }
  337. /*!
  338. \brief disable DAC concurrent mode
  339. \param[in] none
  340. \param[out] none
  341. \retval none
  342. */
  343. void dac_concurrent_disable(void)
  344. {
  345. uint32_t ctl = 0U;
  346. ctl = DAC_CTL_DEN0 | DAC_CTL_DEN1;
  347. DAC_CTL &= (~ctl);
  348. }
  349. /*!
  350. \brief enable DAC concurrent software trigger function
  351. \param[in] none
  352. \param[out] none
  353. \retval none
  354. */
  355. void dac_concurrent_software_trigger_enable(void)
  356. {
  357. uint32_t swt = 0U;
  358. swt = DAC_SWT_SWTR0 | DAC_SWT_SWTR1;
  359. DAC_SWT |= (swt);
  360. }
  361. /*!
  362. \brief disable DAC concurrent software trigger function
  363. \param[in] none
  364. \param[out] none
  365. \retval none
  366. */
  367. void dac_concurrent_software_trigger_disable(void)
  368. {
  369. uint32_t swt = 0U;
  370. swt = DAC_SWT_SWTR0 | DAC_SWT_SWTR1;
  371. DAC_SWT &= (~swt);
  372. }
  373. /*!
  374. \brief enable DAC concurrent buffer function
  375. \param[in] none
  376. \param[out] none
  377. \retval none
  378. */
  379. void dac_concurrent_output_buffer_enable(void)
  380. {
  381. uint32_t ctl = 0U;
  382. ctl = DAC_CTL_DBOFF0 | DAC_CTL_DBOFF1;
  383. DAC_CTL &= (~ctl);
  384. }
  385. /*!
  386. \brief disable DAC concurrent buffer function
  387. \param[in] none
  388. \param[out] none
  389. \retval none
  390. */
  391. void dac_concurrent_output_buffer_disable(void)
  392. {
  393. uint32_t ctl = 0U;
  394. ctl = DAC_CTL_DBOFF0 | DAC_CTL_DBOFF1;
  395. DAC_CTL |= (ctl);
  396. }
  397. /*!
  398. \brief set the DAC specified data holding register value
  399. \param[in] dac_periph
  400. \arg DACx(x=0,1)
  401. \param[in] dac_align
  402. \arg DAC_ALIGN_8B_R: data right 8b alignment
  403. \arg DAC_ALIGN_12B_R: data right 12b alignment
  404. \arg DAC_ALIGN_12B_L: data left 12b alignment
  405. \param[in] data: data to be loaded
  406. \param[out] none
  407. \retval none
  408. */
  409. void dac_data_set(uint32_t dac_periph, uint32_t dac_align, uint16_t data)
  410. {
  411. if(DAC0 == dac_periph){
  412. switch(dac_align){
  413. /* data right 12b alignment */
  414. case DAC_ALIGN_12B_R:
  415. DAC0_R12DH = data;
  416. break;
  417. /* data left 12b alignment */
  418. case DAC_ALIGN_12B_L:
  419. DAC0_L12DH = data;
  420. break;
  421. /* data right 8b alignment */
  422. case DAC_ALIGN_8B_R:
  423. DAC0_R8DH = data;
  424. break;
  425. default:
  426. break;
  427. }
  428. }else{
  429. switch(dac_align){
  430. /* data right 12b alignment */
  431. case DAC_ALIGN_12B_R:
  432. DAC1_R12DH = data;
  433. break;
  434. /* data left 12b alignment */
  435. case DAC_ALIGN_12B_L:
  436. DAC1_L12DH = data;
  437. break;
  438. /* data right 8b alignment */
  439. case DAC_ALIGN_8B_R:
  440. DAC1_R8DH = data;
  441. break;
  442. default:
  443. break;
  444. }
  445. }
  446. }
  447. /*!
  448. \brief set DAC concurrent mode data holding register value
  449. \param[in] dac_align
  450. \arg DAC_ALIGN_8B_R: data right 8b alignment
  451. \arg DAC_ALIGN_12B_R: data right 12b alignment
  452. \arg DAC_ALIGN_12B_L: data left 12b alignment
  453. \param[in] data0: data to be loaded
  454. \param[in] data1: data to be loaded
  455. \param[out] none
  456. \retval none
  457. */
  458. void dac_concurrent_data_set(uint32_t dac_align, uint16_t data0, uint16_t data1)
  459. {
  460. uint32_t data = 0U;
  461. switch(dac_align){
  462. /* data right 12b alignment */
  463. case DAC_ALIGN_12B_R:
  464. data = ((uint32_t)data1 << 16) | data0;
  465. DACC_R12DH = data;
  466. break;
  467. /* data left 12b alignment */
  468. case DAC_ALIGN_12B_L:
  469. data = ((uint32_t)data1 << 16) | data0;
  470. DACC_L12DH = data;
  471. break;
  472. /* data right 8b alignment */
  473. case DAC_ALIGN_8B_R:
  474. data = ((uint32_t)data1 << 8) | data0;
  475. DACC_R8DH = data;
  476. break;
  477. default:
  478. break;
  479. }
  480. }