gd32f30x_dac.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*!
  2. \file gd32f30x_dac.h
  3. \brief definitions for the DAC
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-02-10, V1.0.1, firmware for GD32F30x
  8. */
  9. #ifndef GD32F30X_DAC_H
  10. #define GD32F30X_DAC_H
  11. #include "gd32f30x.h"
  12. /* DACx(x=0,1) definitions */
  13. #define DAC DAC_BASE
  14. #define DAC0 0U
  15. #define DAC1 1U
  16. /* registers definitions */
  17. #define DAC_CTL REG32(DAC + 0x00U) /*!< DAC control register */
  18. #define DAC_SWT REG32(DAC + 0x04U) /*!< DAC software trigger register */
  19. #define DAC0_R12DH REG32(DAC + 0x08U) /*!< DAC0 12-bit right-aligned data holding register */
  20. #define DAC0_L12DH REG32(DAC + 0x0CU) /*!< DAC0 12-bit left-aligned data holding register */
  21. #define DAC0_R8DH REG32(DAC + 0x10U) /*!< DAC0 8-bit right-aligned data holding register */
  22. #define DAC1_R12DH REG32(DAC + 0x14U) /*!< DAC1 12-bit right-aligned data holding register */
  23. #define DAC1_L12DH REG32(DAC + 0x18U) /*!< DAC1 12-bit left-aligned data holding register */
  24. #define DAC1_R8DH REG32(DAC + 0x1CU) /*!< DAC1 8-bit right-aligned data holding register */
  25. #define DACC_R12DH REG32(DAC + 0x20U) /*!< DAC concurrent mode 12-bit right-aligned data holding register */
  26. #define DACC_L12DH REG32(DAC + 0x24U) /*!< DAC concurrent mode 12-bit left-aligned data holding register */
  27. #define DACC_R8DH REG32(DAC + 0x28U) /*!< DAC concurrent mode 8-bit right-aligned data holding register */
  28. #define DAC0_DO REG32(DAC + 0x2CU) /*!< DAC0 data output register */
  29. #define DAC1_DO REG32(DAC + 0x30U) /*!< DAC1 data output register */
  30. /* bits definitions */
  31. /* DAC_CTL */
  32. #define DAC_CTL_DEN0 BIT(0) /*!< DAC0 enable/disable bit */
  33. #define DAC_CTL_DBOFF0 BIT(1) /*!< DAC0 output buffer turn on/turn off bit */
  34. #define DAC_CTL_DTEN0 BIT(2) /*!< DAC0 trigger enable/disable bit */
  35. #define DAC_CTL_DTSEL0 BITS(3,5) /*!< DAC0 trigger source selection enable/disable bits */
  36. #define DAC_CTL_DWM0 BITS(6,7) /*!< DAC0 noise wave mode */
  37. #define DAC_CTL_DWBW0 BITS(8,11) /*!< DAC0 noise wave bit width */
  38. #define DAC_CTL_DDMAEN0 BIT(12) /*!< DAC0 DMA enable/disable bit */
  39. #define DAC_CTL_DEN1 BIT(16) /*!< DAC1 enable/disable bit */
  40. #define DAC_CTL_DBOFF1 BIT(17) /*!< DAC1 output buffer turn on/turn off bit */
  41. #define DAC_CTL_DTEN1 BIT(18) /*!< DAC1 trigger enable/disable bit */
  42. #define DAC_CTL_DTSEL1 BITS(19,21) /*!< DAC1 trigger source selection enable/disable bits */
  43. #define DAC_CTL_DWM1 BITS(22,23) /*!< DAC1 noise wave mode */
  44. #define DAC_CTL_DWBW1 BITS(24,27) /*!< DAC1 noise wave bit width */
  45. #define DAC_CTL_DDMAEN1 BIT(28) /*!< DAC1 DMA enable/disable bit */
  46. /* DAC_SWT */
  47. #define DAC_SWT_SWTR0 BIT(0) /*!< DAC0 software trigger bit, cleared by hardware */
  48. #define DAC_SWT_SWTR1 BIT(1) /*!< DAC1 software trigger bit, cleared by hardware */
  49. /* DAC0_R12DH */
  50. #define DAC0_R12DH_DAC0_DH BITS(0,11) /*!< DAC0 12-bit right-aligned data bits */
  51. /* DAC0_L12DH */
  52. #define DAC0_L12DH_DAC0_DH BITS(4,15) /*!< DAC0 12-bit left-aligned data bits */
  53. /* DAC0_R8DH */
  54. #define DAC0_R8DH_DAC0_DH BITS(0,7) /*!< DAC0 8-bit right-aligned data bits */
  55. /* DAC1_R12DH */
  56. #define DAC1_R12DH_DAC1_DH BITS(0,11) /*!< DAC1 12-bit right-aligned data bits */
  57. /* DAC1_L12DH */
  58. #define DAC1_L12DH_DAC1_DH BITS(4,15) /*!< DAC1 12-bit left-aligned data bits */
  59. /* DAC1_R8DH */
  60. #define DAC1_R8DH_DAC1_DH BITS(0,7) /*!< DAC1 8-bit right-aligned data bits */
  61. /* DACC_R12DH */
  62. #define DACC_R12DH_DAC0_DH BITS(0,11) /*!< DAC concurrent mode DAC0 12-bit right-aligned data bits */
  63. #define DACC_R12DH_DAC1_DH BITS(16,27) /*!< DAC concurrent mode DAC1 12-bit right-aligned data bits */
  64. /* DACC_L12DH */
  65. #define DACC_L12DH_DAC0_DH BITS(4,15) /*!< DAC concurrent mode DAC0 12-bit left-aligned data bits */
  66. #define DACC_L12DH_DAC1_DH BITS(20,31) /*!< DAC concurrent mode DAC1 12-bit left-aligned data bits */
  67. /* DACC_R8DH */
  68. #define DACC_R8DH_DAC0_DH BITS(0,7) /*!< DAC concurrent mode DAC0 8-bit right-aligned data bits */
  69. #define DACC_R8DH_DAC1_DH BITS(8,15) /*!< DAC concurrent mode DAC1 8-bit right-aligned data bits */
  70. /* DAC0_DO */
  71. #define DAC0_DO_DAC0_DO BITS(0,11) /*!< DAC0 12-bit output data bits */
  72. /* DAC1_DO */
  73. #define DAC1_DO_DAC1_DO BITS(0,11) /*!< DAC1 12-bit output data bits */
  74. /* constants definitions */
  75. /* DAC trigger source */
  76. #define CTL_DTSEL(regval) (BITS(3,5) & ((uint32_t)(regval) << 3))
  77. #define DAC_TRIGGER_T5_TRGO CTL_DTSEL(0) /*!< TIMER5 TRGO */
  78. #if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
  79. #define DAC_TRIGGER_T7_TRGO CTL_DTSEL(1) /*!< TIMER7 TRGO */
  80. #elif defined(GD32F30X_CL)
  81. #define DAC_TRIGGER_T2_TRGO CTL_DTSEL(1) /*!< TIMER2 TRGO */
  82. #endif /* GD32F30X_HD and GD32F30X_XD */
  83. #define DAC_TRIGGER_T6_TRGO CTL_DTSEL(2) /*!< TIMER6 TRGO */
  84. #define DAC_TRIGGER_T4_TRGO CTL_DTSEL(3) /*!< TIMER4 TRGO */
  85. #define DAC_TRIGGER_T1_TRGO CTL_DTSEL(4) /*!< TIMER1 TRGO */
  86. #define DAC_TRIGGER_T3_TRGO CTL_DTSEL(5) /*!< TIMER3 TRGO */
  87. #define DAC_TRIGGER_EXTI_9 CTL_DTSEL(6) /*!< EXTI interrupt line9 event */
  88. #define DAC_TRIGGER_SOFTWARE CTL_DTSEL(7) /*!< software trigger */
  89. /* DAC noise wave mode */
  90. #define CTL_DWM(regval) (BITS(6,7) & ((uint32_t)(regval) << 6))
  91. #define DAC_WAVE_DISABLE CTL_DWM(0) /*!< wave disable */
  92. #define DAC_WAVE_MODE_LFSR CTL_DWM(1) /*!< LFSR noise mode */
  93. #define DAC_WAVE_MODE_TRIANGLE CTL_DWM(2) /*!< triangle noise mode */
  94. /* DAC noise wave bit width */
  95. #define DWBW(regval) (BITS(8,11) & ((uint32_t)(regval) << 8))
  96. #define DAC_WAVE_BIT_WIDTH_1 DWBW(0) /*!< bit width of the wave signal is 1 */
  97. #define DAC_WAVE_BIT_WIDTH_2 DWBW(1) /*!< bit width of the wave signal is 2 */
  98. #define DAC_WAVE_BIT_WIDTH_3 DWBW(2) /*!< bit width of the wave signal is 3 */
  99. #define DAC_WAVE_BIT_WIDTH_4 DWBW(3) /*!< bit width of the wave signal is 4 */
  100. #define DAC_WAVE_BIT_WIDTH_5 DWBW(4) /*!< bit width of the wave signal is 5 */
  101. #define DAC_WAVE_BIT_WIDTH_6 DWBW(5) /*!< bit width of the wave signal is 6 */
  102. #define DAC_WAVE_BIT_WIDTH_7 DWBW(6) /*!< bit width of the wave signal is 7 */
  103. #define DAC_WAVE_BIT_WIDTH_8 DWBW(7) /*!< bit width of the wave signal is 8 */
  104. #define DAC_WAVE_BIT_WIDTH_9 DWBW(8) /*!< bit width of the wave signal is 9 */
  105. #define DAC_WAVE_BIT_WIDTH_10 DWBW(9) /*!< bit width of the wave signal is 10 */
  106. #define DAC_WAVE_BIT_WIDTH_11 DWBW(10) /*!< bit width of the wave signal is 11 */
  107. #define DAC_WAVE_BIT_WIDTH_12 DWBW(11) /*!< bit width of the wave signal is 12 */
  108. /* unmask LFSR bits in DAC LFSR noise mode */
  109. #define DAC_LFSR_BIT0 DAC_WAVE_BIT_WIDTH_1 /*!< unmask the LFSR bit0 */
  110. #define DAC_LFSR_BITS1_0 DAC_WAVE_BIT_WIDTH_2 /*!< unmask the LFSR bits[1:0] */
  111. #define DAC_LFSR_BITS2_0 DAC_WAVE_BIT_WIDTH_3 /*!< unmask the LFSR bits[2:0] */
  112. #define DAC_LFSR_BITS3_0 DAC_WAVE_BIT_WIDTH_4 /*!< unmask the LFSR bits[3:0] */
  113. #define DAC_LFSR_BITS4_0 DAC_WAVE_BIT_WIDTH_5 /*!< unmask the LFSR bits[4:0] */
  114. #define DAC_LFSR_BITS5_0 DAC_WAVE_BIT_WIDTH_6 /*!< unmask the LFSR bits[5:0] */
  115. #define DAC_LFSR_BITS6_0 DAC_WAVE_BIT_WIDTH_7 /*!< unmask the LFSR bits[6:0] */
  116. #define DAC_LFSR_BITS7_0 DAC_WAVE_BIT_WIDTH_8 /*!< unmask the LFSR bits[7:0] */
  117. #define DAC_LFSR_BITS8_0 DAC_WAVE_BIT_WIDTH_9 /*!< unmask the LFSR bits[8:0] */
  118. #define DAC_LFSR_BITS9_0 DAC_WAVE_BIT_WIDTH_10 /*!< unmask the LFSR bits[9:0] */
  119. #define DAC_LFSR_BITS10_0 DAC_WAVE_BIT_WIDTH_11 /*!< unmask the LFSR bits[10:0] */
  120. #define DAC_LFSR_BITS11_0 DAC_WAVE_BIT_WIDTH_12 /*!< unmask the LFSR bits[11:0] */
  121. /* triangle amplitude in DAC triangle noise mode */
  122. #define DAC_TRIANGLE_AMPLITUDE_1 DAC_WAVE_BIT_WIDTH_1 /*!< triangle amplitude is 1 */
  123. #define DAC_TRIANGLE_AMPLITUDE_3 DAC_WAVE_BIT_WIDTH_2 /*!< triangle amplitude is 3 */
  124. #define DAC_TRIANGLE_AMPLITUDE_7 DAC_WAVE_BIT_WIDTH_3 /*!< triangle amplitude is 7 */
  125. #define DAC_TRIANGLE_AMPLITUDE_15 DAC_WAVE_BIT_WIDTH_4 /*!< triangle amplitude is 15 */
  126. #define DAC_TRIANGLE_AMPLITUDE_31 DAC_WAVE_BIT_WIDTH_5 /*!< triangle amplitude is 31 */
  127. #define DAC_TRIANGLE_AMPLITUDE_63 DAC_WAVE_BIT_WIDTH_6 /*!< triangle amplitude is 63 */
  128. #define DAC_TRIANGLE_AMPLITUDE_127 DAC_WAVE_BIT_WIDTH_7 /*!< triangle amplitude is 127 */
  129. #define DAC_TRIANGLE_AMPLITUDE_255 DAC_WAVE_BIT_WIDTH_8 /*!< triangle amplitude is 255 */
  130. #define DAC_TRIANGLE_AMPLITUDE_511 DAC_WAVE_BIT_WIDTH_9 /*!< triangle amplitude is 511 */
  131. #define DAC_TRIANGLE_AMPLITUDE_1023 DAC_WAVE_BIT_WIDTH_10 /*!< triangle amplitude is 1023 */
  132. #define DAC_TRIANGLE_AMPLITUDE_2047 DAC_WAVE_BIT_WIDTH_11 /*!< triangle amplitude is 2047 */
  133. #define DAC_TRIANGLE_AMPLITUDE_4095 DAC_WAVE_BIT_WIDTH_12 /*!< triangle amplitude is 4095 */
  134. /* DAC data alignment */
  135. #define DATA_ALIGN(regval) (BITS(0,1) & ((uint32_t)(regval) << 0))
  136. #define DAC_ALIGN_12B_R DATA_ALIGN(0) /*!< data right 12b alignment */
  137. #define DAC_ALIGN_12B_L DATA_ALIGN(1) /*!< data left 12b alignment */
  138. #define DAC_ALIGN_8B_R DATA_ALIGN(2) /*!< data right 8b alignment */
  139. /* function declarations */
  140. /* deinitialize DAC */
  141. void dac_deinit(void);
  142. /* enable DAC */
  143. void dac_enable(uint32_t dac_periph);
  144. /* disable DAC */
  145. void dac_disable(uint32_t dac_periph);
  146. /* enable DAC DMA */
  147. void dac_dma_enable(uint32_t dac_periph);
  148. /* disable DAC DMA */
  149. void dac_dma_disable(uint32_t dac_periph);
  150. /* enable DAC output buffer */
  151. void dac_output_buffer_enable(uint32_t dac_periph);
  152. /* disable DAC output buffer */
  153. void dac_output_buffer_disable(uint32_t dac_periph);
  154. /* enable DAC trigger */
  155. void dac_trigger_enable(uint32_t dac_periph);
  156. /* disable DAC trigger */
  157. void dac_trigger_disable(uint32_t dac_periph);
  158. /* enable DAC software trigger */
  159. void dac_software_trigger_enable(uint32_t dac_periph);
  160. /* disable DAC software trigger */
  161. void dac_software_trigger_disable(uint32_t dac_periph);
  162. /* configure DAC trigger source */
  163. void dac_trigger_source_config(uint32_t dac_periph, uint32_t triggersource);
  164. /* configure DAC wave mode */
  165. void dac_wave_mode_config(uint32_t dac_periph, uint32_t wave_mode);
  166. /* configure DAC wave bit width */
  167. void dac_wave_bit_width_config(uint32_t dac_periph, uint32_t bit_width);
  168. /* configure DAC LFSR noise mode */
  169. void dac_lfsr_noise_config(uint32_t dac_periph, uint32_t unmask_bits);
  170. /* configure DAC triangle noise mode */
  171. void dac_triangle_noise_config(uint32_t dac_periph, uint32_t amplitude);
  172. /* get the last data output value */
  173. uint16_t dac_output_value_get(uint32_t dac_periph);
  174. /* set DAC data holding register value */
  175. void dac_data_set(uint32_t dac_periph, uint32_t dac_align, uint16_t data);
  176. /* set DAC concurrent mode data holding register value */
  177. void dac_concurrent_data_set(uint32_t dac_align, uint16_t data0, uint16_t data1);
  178. /* enable DAC concurrent mode */
  179. void dac_concurrent_enable(void);
  180. /* disable DAC concurrent mode */
  181. void dac_concurrent_disable(void);
  182. /* enable DAC concurrent software trigger */
  183. void dac_concurrent_software_trigger_enable(void);
  184. /* disable DAC concurrent software trigger */
  185. void dac_concurrent_software_trigger_disable(void);
  186. /* enable DAC concurrent buffer function */
  187. void dac_concurrent_output_buffer_enable(void);
  188. /* disable DAC concurrent buffer function */
  189. void dac_concurrent_output_buffer_disable(void);
  190. #endif /* GD32F30X_DAC_H */