gd32f30x_dac.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*!
  2. \file gd32f30x_dac.h
  3. \brief definitions for the DAC
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #ifndef GD32F30X_DAC_H
  30. #define GD32F30X_DAC_H
  31. #include "gd32f30x.h"
  32. /* DACx(x=0) definitions */
  33. #define DAC0 (DAC_BASE)
  34. /* registers definitions */
  35. #define DAC_CTL0(dacx) REG32((dacx) + 0x00000000U) /*!< DACx control register 0 */
  36. #define DAC_SWT(dacx) REG32((dacx) + 0x00000004U) /*!< DACx software trigger register */
  37. #define DAC_OUT0_R12DH(dacx) REG32((dacx) + 0x00000008U) /*!< DACx_OUT0 12-bit right-aligned data holding register */
  38. #define DAC_OUT0_L12DH(dacx) REG32((dacx) + 0x0000000CU) /*!< DACx_OUT0 12-bit left-aligned data holding register */
  39. #define DAC_OUT0_R8DH(dacx) REG32((dacx) + 0x00000010U) /*!< DACx_OUT0 8-bit right-aligned data holding register */
  40. #define DAC_OUT1_R12DH(dacx) REG32((dacx) + 0x00000014U) /*!< DACx_OUT1 12-bit right-aligned data holding register */
  41. #define DAC_OUT1_L12DH(dacx) REG32((dacx) + 0x00000018U) /*!< DACx_OUT1 12-bit left-aligned data holding register */
  42. #define DAC_OUT1_R8DH(dacx) REG32((dacx) + 0x0000001CU) /*!< DACx_OUT1 8-bit right-aligned data holding register */
  43. #define DACC_R12DH(dacx) REG32((dacx) + 0x00000020U) /*!< DACx concurrent mode 12-bit right-aligned data holding register */
  44. #define DACC_L12DH(dacx) REG32((dacx) + 0x00000024U) /*!< DACx concurrent mode 12-bit left-aligned data holding register */
  45. #define DACC_R8DH(dacx) REG32((dacx) + 0x00000028U) /*!< DACx concurrent mode 8-bit right-aligned data holding register */
  46. #define DAC_OUT0_DO(dacx) REG32((dacx) + 0x0000002CU) /*!< DACx_OUT0 data output register */
  47. #define DAC_OUT1_DO(dacx) REG32((dacx) + 0x00000030U) /*!< DACx_OUT1 data output register */
  48. /* bits definitions */
  49. /* DAC_CTL0 */
  50. #define DAC_CTL0_DEN0 BIT(0) /*!< DACx_OUT0 enable */
  51. #define DAC_CTL0_DBOFF0 BIT(1) /*!< DACx_OUT0 output buffer turn off */
  52. #define DAC_CTL0_DTEN0 BIT(2) /*!< DACx_OUT0 trigger enable */
  53. #define DAC_CTL0_DTSEL0 BITS(3,5) /*!< DACx_OUT0 trigger selection */
  54. #define DAC_CTL0_DWM0 BITS(6,7) /*!< DACx_OUT0 noise wave mode */
  55. #define DAC_CTL0_DWBW0 BITS(8,11) /*!< DACx_OUT0 noise wave bit width */
  56. #define DAC_CTL0_DDMAEN0 BIT(12) /*!< DACx_OUT0 DMA enable */
  57. #define DAC_CTL0_DEN1 BIT(16) /*!< DACx_OUT1 enable */
  58. #define DAC_CTL0_DBOFF1 BIT(17) /*!< DACx_OUT1 output buffer turn off */
  59. #define DAC_CTL0_DTEN1 BIT(18) /*!< DACx_OUT1 trigger enable */
  60. #define DAC_CTL0_DTSEL1 BITS(19,21) /*!< DACx_OUT1 trigger selection */
  61. #define DAC_CTL0_DWM1 BITS(22,23) /*!< DACx_OUT1 noise wave mode */
  62. #define DAC_CTL0_DWBW1 BITS(24,27) /*!< DACx_OUT1 noise wave bit width */
  63. #define DAC_CTL0_DDMAEN1 BIT(28) /*!< DACx_OUT1 DMA enable */
  64. /* DAC_SWT */
  65. #define DAC_SWT_SWTR0 BIT(0) /*!< DACx_OUT0 software trigger */
  66. #define DAC_SWT_SWTR1 BIT(1) /*!< DACx_OUT1 software trigger */
  67. /* DAC_OUT0_R12DH */
  68. #define DAC_OUT0_DH_R12 BITS(0,11) /*!< DACx_OUT0 12-bit right-aligned data */
  69. /* DAC_OUT0_L12DH */
  70. #define DAC_OUT0_DH_L12 BITS(4,15) /*!< DACx_OUT0 12-bit left-aligned data */
  71. /* DAC_OUT0_R8DH */
  72. #define DAC_OUT0_DH_R8 BITS(0,7) /*!< DACx_OUT0 8-bit right-aligned data */
  73. /* DAC_OUT1_R12DH */
  74. #define DAC_OUT1_DH_R12 BITS(0,11) /*!< DACx_OUT1 12-bit right-aligned data */
  75. /* DAC_OUT1_L12DH */
  76. #define DAC_OUT1_DH_L12 BITS(4,15) /*!< DACx_OUT1 12-bit left-aligned data */
  77. /* DAC_OUT1_R8DH */
  78. #define DAC_OUT1_DH_R8 BITS(0,7) /*!< DACx_OUT1 8-bit right-aligned data */
  79. /* DACC_R12DH */
  80. #define DACC_OUT0_DH_R12 BITS(0,11) /*!< DAC concurrent mode DACx_OUT0 12-bit right-aligned data */
  81. #define DACC_OUT1_DH_R12 BITS(16,27) /*!< DAC concurrent mode DACx_OUT1 12-bit right-aligned data */
  82. /* DACC_L12DH */
  83. #define DACC_OUT0_DH_L12 BITS(4,15) /*!< DAC concurrent mode DACx_OUT0 12-bit left-aligned data */
  84. #define DACC_OUT1_DH_L12 BITS(20,31) /*!< DAC concurrent mode DACx_OUT1 12-bit left-aligned data */
  85. /* DACC_R8DH */
  86. #define DACC_OUT0_DH_R8 BITS(0,7) /*!< DAC concurrent mode DACx_OUT0 8-bit right-aligned data */
  87. #define DACC_OUT1_DH_R8 BITS(8,15) /*!< DAC concurrent mode DACx_OUT1 8-bit right-aligned data */
  88. /* DAC_OUT0_DO */
  89. #define DAC_OUT0_DO_BITS BITS(0,11) /*!< DACx_OUT0 12-bit output data */
  90. /* DAC_OUT1_DO */
  91. #define DAC_OUT1_DO_BITS BITS(0,11) /*!< DACx_OUT1 12-bit output data */
  92. /* constants definitions */
  93. /* DAC trigger source */
  94. #define CTL0_DTSEL(regval) (BITS(3,5) & ((uint32_t)(regval) << 3))
  95. #define DAC_TRIGGER_T5_TRGO CTL0_DTSEL(0) /*!< TIMER5 TRGO */
  96. #if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
  97. #define DAC_TRIGGER_T7_TRGO CTL0_DTSEL(1) /*!< TIMER7 TRGO */
  98. #elif defined(GD32F30X_CL)
  99. #define DAC_TRIGGER_T2_TRGO CTL0_DTSEL(1) /*!< TIMER2 TRGO */
  100. #endif /* GD32F30X_HD and GD32F30X_XD */
  101. #define DAC_TRIGGER_T6_TRGO CTL0_DTSEL(2) /*!< TIMER6 TRGO */
  102. #define DAC_TRIGGER_T4_TRGO CTL0_DTSEL(3) /*!< TIMER4 TRGO */
  103. #define DAC_TRIGGER_T1_TRGO CTL0_DTSEL(4) /*!< TIMER1 TRGO */
  104. #define DAC_TRIGGER_T3_TRGO CTL0_DTSEL(5) /*!< TIMER3 TRGO */
  105. #define DAC_TRIGGER_EXTI_9 CTL0_DTSEL(6) /*!< EXTI interrupt line9 event */
  106. #define DAC_TRIGGER_SOFTWARE CTL0_DTSEL(7) /*!< software trigger */
  107. /* DAC noise wave mode */
  108. #define CTL0_DWM(regval) (BITS(6,7) & ((uint32_t)(regval) << 6))
  109. #define DAC_WAVE_DISABLE CTL0_DWM(0) /*!< wave disabled */
  110. #define DAC_WAVE_MODE_LFSR CTL0_DWM(1) /*!< LFSR noise mode */
  111. #define DAC_WAVE_MODE_TRIANGLE CTL0_DWM(2) /*!< triangle noise mode */
  112. /* DAC noise wave bit width */
  113. #define DWBW(regval) (BITS(8, 11) & ((uint32_t)(regval) << 8))
  114. #define DAC_WAVE_BIT_WIDTH_1 DWBW(0) /*!< bit width of the wave signal is 1 */
  115. #define DAC_WAVE_BIT_WIDTH_2 DWBW(1) /*!< bit width of the wave signal is 2 */
  116. #define DAC_WAVE_BIT_WIDTH_3 DWBW(2) /*!< bit width of the wave signal is 3 */
  117. #define DAC_WAVE_BIT_WIDTH_4 DWBW(3) /*!< bit width of the wave signal is 4 */
  118. #define DAC_WAVE_BIT_WIDTH_5 DWBW(4) /*!< bit width of the wave signal is 5 */
  119. #define DAC_WAVE_BIT_WIDTH_6 DWBW(5) /*!< bit width of the wave signal is 6 */
  120. #define DAC_WAVE_BIT_WIDTH_7 DWBW(6) /*!< bit width of the wave signal is 7 */
  121. #define DAC_WAVE_BIT_WIDTH_8 DWBW(7) /*!< bit width of the wave signal is 8 */
  122. #define DAC_WAVE_BIT_WIDTH_9 DWBW(8) /*!< bit width of the wave signal is 9 */
  123. #define DAC_WAVE_BIT_WIDTH_10 DWBW(9) /*!< bit width of the wave signal is 10 */
  124. #define DAC_WAVE_BIT_WIDTH_11 DWBW(10) /*!< bit width of the wave signal is 11 */
  125. #define DAC_WAVE_BIT_WIDTH_12 DWBW(11) /*!< bit width of the wave signal is 12 */
  126. /* unmask LFSR bits in DAC LFSR noise mode */
  127. #define DAC_LFSR_BIT0 DAC_WAVE_BIT_WIDTH_1 /*!< unmask the LFSR bit0 */
  128. #define DAC_LFSR_BITS1_0 DAC_WAVE_BIT_WIDTH_2 /*!< unmask the LFSR bits[1:0] */
  129. #define DAC_LFSR_BITS2_0 DAC_WAVE_BIT_WIDTH_3 /*!< unmask the LFSR bits[2:0] */
  130. #define DAC_LFSR_BITS3_0 DAC_WAVE_BIT_WIDTH_4 /*!< unmask the LFSR bits[3:0] */
  131. #define DAC_LFSR_BITS4_0 DAC_WAVE_BIT_WIDTH_5 /*!< unmask the LFSR bits[4:0] */
  132. #define DAC_LFSR_BITS5_0 DAC_WAVE_BIT_WIDTH_6 /*!< unmask the LFSR bits[5:0] */
  133. #define DAC_LFSR_BITS6_0 DAC_WAVE_BIT_WIDTH_7 /*!< unmask the LFSR bits[6:0] */
  134. #define DAC_LFSR_BITS7_0 DAC_WAVE_BIT_WIDTH_8 /*!< unmask the LFSR bits[7:0] */
  135. #define DAC_LFSR_BITS8_0 DAC_WAVE_BIT_WIDTH_9 /*!< unmask the LFSR bits[8:0] */
  136. #define DAC_LFSR_BITS9_0 DAC_WAVE_BIT_WIDTH_10 /*!< unmask the LFSR bits[9:0] */
  137. #define DAC_LFSR_BITS10_0 DAC_WAVE_BIT_WIDTH_11 /*!< unmask the LFSR bits[10:0] */
  138. #define DAC_LFSR_BITS11_0 DAC_WAVE_BIT_WIDTH_12 /*!< unmask the LFSR bits[11:0] */
  139. /* triangle amplitude in DAC triangle noise mode */
  140. #define DAC_TRIANGLE_AMPLITUDE_1 DAC_WAVE_BIT_WIDTH_1 /*!< triangle amplitude is 1 */
  141. #define DAC_TRIANGLE_AMPLITUDE_3 DAC_WAVE_BIT_WIDTH_2 /*!< triangle amplitude is 3 */
  142. #define DAC_TRIANGLE_AMPLITUDE_7 DAC_WAVE_BIT_WIDTH_3 /*!< triangle amplitude is 7 */
  143. #define DAC_TRIANGLE_AMPLITUDE_15 DAC_WAVE_BIT_WIDTH_4 /*!< triangle amplitude is 15 */
  144. #define DAC_TRIANGLE_AMPLITUDE_31 DAC_WAVE_BIT_WIDTH_5 /*!< triangle amplitude is 31 */
  145. #define DAC_TRIANGLE_AMPLITUDE_63 DAC_WAVE_BIT_WIDTH_6 /*!< triangle amplitude is 63 */
  146. #define DAC_TRIANGLE_AMPLITUDE_127 DAC_WAVE_BIT_WIDTH_7 /*!< triangle amplitude is 127 */
  147. #define DAC_TRIANGLE_AMPLITUDE_255 DAC_WAVE_BIT_WIDTH_8 /*!< triangle amplitude is 255 */
  148. #define DAC_TRIANGLE_AMPLITUDE_511 DAC_WAVE_BIT_WIDTH_9 /*!< triangle amplitude is 511 */
  149. #define DAC_TRIANGLE_AMPLITUDE_1023 DAC_WAVE_BIT_WIDTH_10 /*!< triangle amplitude is 1023 */
  150. #define DAC_TRIANGLE_AMPLITUDE_2047 DAC_WAVE_BIT_WIDTH_11 /*!< triangle amplitude is 2047 */
  151. #define DAC_TRIANGLE_AMPLITUDE_4095 DAC_WAVE_BIT_WIDTH_12 /*!< triangle amplitude is 4095 */
  152. /* DAC data alignment */
  153. #define DATA_ALIGN(regval) (BITS(0,1) & ((uint32_t)(regval) << 0))
  154. #define DAC_ALIGN_12B_R DATA_ALIGN(0) /*!< 12-bit right-aligned data */
  155. #define DAC_ALIGN_12B_L DATA_ALIGN(1) /*!< 12-bit left-aligned data */
  156. #define DAC_ALIGN_8B_R DATA_ALIGN(2) /*!< 8-bit right-aligned data */
  157. /* DAC output channel definitions */
  158. #define DAC_OUT0 ((uint8_t)0x00U) /*!< DACx_OUT0 channel */
  159. #define DAC_OUT1 ((uint8_t)0x01U) /*!< DACx_OUT1 channel */
  160. /* function declarations */
  161. /* DAC initialization functions */
  162. /* deinitialize DAC */
  163. void dac_deinit(uint32_t dac_periph);
  164. /* enable DAC */
  165. void dac_enable(uint32_t dac_periph, uint8_t dac_out);
  166. /* disable DAC */
  167. void dac_disable(uint32_t dac_periph, uint8_t dac_out);
  168. /* enable DAC DMA function */
  169. void dac_dma_enable(uint32_t dac_periph, uint8_t dac_out);
  170. /* disable DAC DMA function */
  171. void dac_dma_disable(uint32_t dac_periph, uint8_t dac_out);
  172. /* DAC buffer functions */
  173. /* enable DAC output buffer */
  174. void dac_output_buffer_enable(uint32_t dac_periph, uint8_t dac_out);
  175. /* disable DAC output buffer */
  176. void dac_output_buffer_disable(uint32_t dac_periph, uint8_t dac_out);
  177. /* read and write operation functions */
  178. /* get DAC output value */
  179. uint16_t dac_output_value_get(uint32_t dac_periph, uint8_t dac_out);
  180. /* set DAC data holding register value */
  181. void dac_data_set(uint32_t dac_periph, uint8_t dac_out, uint32_t dac_align, uint16_t data);
  182. /* DAC trigger configuration */
  183. /* enable DAC trigger */
  184. void dac_trigger_enable(uint32_t dac_periph, uint8_t dac_out);
  185. /* disable DAC trigger */
  186. void dac_trigger_disable(uint32_t dac_periph, uint8_t dac_out);
  187. /* configure DAC trigger source */
  188. void dac_trigger_source_config(uint32_t dac_periph, uint8_t dac_out, uint32_t triggersource);
  189. /* enable DAC software trigger */
  190. void dac_software_trigger_enable(uint32_t dac_periph, uint8_t dac_out);
  191. /* DAC wave mode configuration */
  192. /* configure DAC wave mode */
  193. void dac_wave_mode_config(uint32_t dac_periph, uint8_t dac_out, uint32_t wave_mode);
  194. /* configure DAC LFSR noise mode */
  195. void dac_lfsr_noise_config(uint32_t dac_periph, uint8_t dac_out, uint32_t unmask_bits);
  196. /* configure DAC triangle noise mode */
  197. void dac_triangle_noise_config(uint32_t dac_periph, uint8_t dac_out, uint32_t amplitude);
  198. /* DAC concurrent mode configuration */
  199. /* enable DAC concurrent mode */
  200. void dac_concurrent_enable(uint32_t dac_periph);
  201. /* disable DAC concurrent mode */
  202. void dac_concurrent_disable(uint32_t dac_periph);
  203. /* enable DAC concurrent software trigger */
  204. void dac_concurrent_software_trigger_enable(uint32_t dac_periph);
  205. /* enable DAC concurrent buffer function */
  206. void dac_concurrent_output_buffer_enable(uint32_t dac_periph);
  207. /* disable DAC concurrent buffer function */
  208. void dac_concurrent_output_buffer_disable(uint32_t dac_periph);
  209. /* set DAC concurrent mode data holding register value */
  210. void dac_concurrent_data_set(uint32_t dac_periph, uint32_t dac_align, uint16_t data0, uint16_t data1);
  211. #endif /* GD32F30X_DAC_H */