gd32f30x_dac.c 16 KB

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