gd32f10x_dac.c 16 KB

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