gd32f3x0_dac.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*!
  2. \file gd32f3x0_dac.h
  3. \brief definitions for the DAC
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #ifdef GD32F350
  10. #ifndef GD32F3X0_DAC_H
  11. #define GD32F3X0_DAC_H
  12. #include "gd32f3x0.h"
  13. /* DAC definitions */
  14. #define DAC DAC_BASE
  15. /* registers definitions */
  16. #define DAC_CTL REG32(DAC + 0x00U) /*!< DAC control register */
  17. #define DAC_SWT REG32(DAC + 0x04U) /*!< DAC software trigger register */
  18. #define DAC_R12DH REG32(DAC + 0x08U) /*!< DAC 12-bit right-aligned data holding register */
  19. #define DAC_L12DH REG32(DAC + 0x0CU) /*!< DAC 12-bit left-aligned data holding register */
  20. #define DAC_R8DH REG32(DAC + 0x10U) /*!< DAC 8-bit right-aligned data holding register */
  21. #define DAC_DO REG32(DAC + 0x2CU) /*!< DAC output data register */
  22. #define DAC_STAT REG32(DAC + 0x34U) /*!< DAC status register */
  23. /* bits definitions */
  24. /* DAC_CTL */
  25. #define DAC_CTL_DEN BIT(0) /*!< DAC enable/disable bit */
  26. #define DAC_CTL_DBOFF BIT(1) /*!< DAC output buffer turn on/turn off bit */
  27. #define DAC_CTL_DTEN BIT(2) /*!< DAC trigger enable/disable bit */
  28. #define DAC_CTL_DTSEL BITS(3,5) /*!< DAC trigger source selection enable/disable bits */
  29. #define DAC_CTL_DWM BITS(6,7) /*!< DAC noise wave mode */
  30. #define DAC_CTL_DWBW BITS(8,11) /*!< DAC noise wave bit width */
  31. #define DAC_CTL_DDMAEN BIT(12) /*!< DAC DMA enable/disable bit */
  32. #define DAC_CTL_DDUDRIE BIT(13) /*!< DAC DMA underrun interrupt enable/disable bit */
  33. /* DAC_SWT */
  34. #define DAC_SWT_SWTR BIT(0) /*!< DAC software trigger bit,cleared by hardware */
  35. /* DAC_R12DH */
  36. #define DAC_R12DH_DAC_DH BITS(0,11) /*!< DAC 12-bit right-aligned data bits */
  37. /* DAC_L12DH */
  38. #define DAC_L12DH_DAC_DH BITS(4,15) /*!< DAC 12-bit left-aligned data bits */
  39. /* DAC_R8DH */
  40. #define DAC_R8DH_DAC_DH BITS(0,7) /*!< DAC 8-bit right-aligned data bits */
  41. /* DAC_DO */
  42. #define DAC_DO_DAC_DO BITS(0,11) /*!< DAC 12-bit output data bits */
  43. /* DAC_STAT */
  44. #define DAC_STAT_DDUDR BIT(13) /*!< DAC DMA underrun flag */
  45. /* constants definitions */
  46. /* DAC trigger source */
  47. #define CTL_DTSEL(regval) (BITS(3,5) & ((uint32_t)(regval) << 3))
  48. #define DAC_TRIGGER_T5_TRGO CTL_DTSEL(0) /*!< TIMER5 TRGO */
  49. #define DAC_TRIGGER_T2_TRGO CTL_DTSEL(1) /*!< TIMER2 TRGO */
  50. #define DAC_TRIGGER_T14_TRGO CTL_DTSEL(3) /*!< TIMER14 TRGO */
  51. #define DAC_TRIGGER_T1_TRGO CTL_DTSEL(4) /*!< TIMER1 TRGO */
  52. #define DAC_TRIGGER_EXTI_9 CTL_DTSEL(6) /*!< EXTI interrupt line9 event */
  53. #define DAC_TRIGGER_SOFTWARE CTL_DTSEL(7) /*!< software trigger */
  54. /* DAC noise wave mode */
  55. #define CTL_DWM(regval) (BITS(6,7) & ((uint32_t)(regval) << 6))
  56. #define DAC_WAVE_DISABLE CTL_DWM(0) /*!< wave disable */
  57. #define DAC_WAVE_MODE_LFSR CTL_DWM(1) /*!< LFSR noise mode */
  58. #define DAC_WAVE_MODE_TRIANGLE CTL_DWM(2) /*!< triangle noise mode */
  59. /* DAC noise wave bit width */
  60. #define DWBW(regval) (BITS(8,11) & ((uint32_t)(regval) << 8))
  61. #define DAC_WAVE_BIT_WIDTH_1 DWBW(0) /*!< bit width of the wave signal is 1 */
  62. #define DAC_WAVE_BIT_WIDTH_2 DWBW(1) /*!< bit width of the wave signal is 2 */
  63. #define DAC_WAVE_BIT_WIDTH_3 DWBW(2) /*!< bit width of the wave signal is 3 */
  64. #define DAC_WAVE_BIT_WIDTH_4 DWBW(3) /*!< bit width of the wave signal is 4 */
  65. #define DAC_WAVE_BIT_WIDTH_5 DWBW(4) /*!< bit width of the wave signal is 5 */
  66. #define DAC_WAVE_BIT_WIDTH_6 DWBW(5) /*!< bit width of the wave signal is 6 */
  67. #define DAC_WAVE_BIT_WIDTH_7 DWBW(6) /*!< bit width of the wave signal is 7 */
  68. #define DAC_WAVE_BIT_WIDTH_8 DWBW(7) /*!< bit width of the wave signal is 8 */
  69. #define DAC_WAVE_BIT_WIDTH_9 DWBW(8) /*!< bit width of the wave signal is 9 */
  70. #define DAC_WAVE_BIT_WIDTH_10 DWBW(9) /*!< bit width of the wave signal is 10 */
  71. #define DAC_WAVE_BIT_WIDTH_11 DWBW(10) /*!< bit width of the wave signal is 11 */
  72. #define DAC_WAVE_BIT_WIDTH_12 DWBW(11) /*!< bit width of the wave signal is 12 */
  73. /* unmask LFSR bits in DAC LFSR noise mode */
  74. #define DAC_LFSR_BIT0 DAC_WAVE_BIT_WIDTH_1 /*!< unmask the LFSR bit0 */
  75. #define DAC_LFSR_BITS1_0 DAC_WAVE_BIT_WIDTH_2 /*!< unmask the LFSR bits[1:0] */
  76. #define DAC_LFSR_BITS2_0 DAC_WAVE_BIT_WIDTH_3 /*!< unmask the LFSR bits[2:0] */
  77. #define DAC_LFSR_BITS3_0 DAC_WAVE_BIT_WIDTH_4 /*!< unmask the LFSR bits[3:0] */
  78. #define DAC_LFSR_BITS4_0 DAC_WAVE_BIT_WIDTH_5 /*!< unmask the LFSR bits[4:0] */
  79. #define DAC_LFSR_BITS5_0 DAC_WAVE_BIT_WIDTH_6 /*!< unmask the LFSR bits[5:0] */
  80. #define DAC_LFSR_BITS6_0 DAC_WAVE_BIT_WIDTH_7 /*!< unmask the LFSR bits[6:0] */
  81. #define DAC_LFSR_BITS7_0 DAC_WAVE_BIT_WIDTH_8 /*!< unmask the LFSR bits[7:0] */
  82. #define DAC_LFSR_BITS8_0 DAC_WAVE_BIT_WIDTH_9 /*!< unmask the LFSR bits[8:0] */
  83. #define DAC_LFSR_BITS9_0 DAC_WAVE_BIT_WIDTH_10 /*!< unmask the LFSR bits[9:0] */
  84. #define DAC_LFSR_BITS10_0 DAC_WAVE_BIT_WIDTH_11 /*!< unmask the LFSR bits[10:0] */
  85. #define DAC_LFSR_BITS11_0 DAC_WAVE_BIT_WIDTH_12 /*!< unmask the LFSR bits[11:0] */
  86. /* triangle amplitude in DAC triangle noise mode */
  87. #define DAC_TRIANGLE_AMPLITUDE_1 DAC_WAVE_BIT_WIDTH_1 /*!< triangle amplitude is 1 */
  88. #define DAC_TRIANGLE_AMPLITUDE_3 DAC_WAVE_BIT_WIDTH_2 /*!< triangle amplitude is 3 */
  89. #define DAC_TRIANGLE_AMPLITUDE_7 DAC_WAVE_BIT_WIDTH_3 /*!< triangle amplitude is 7 */
  90. #define DAC_TRIANGLE_AMPLITUDE_15 DAC_WAVE_BIT_WIDTH_4 /*!< triangle amplitude is 15 */
  91. #define DAC_TRIANGLE_AMPLITUDE_31 DAC_WAVE_BIT_WIDTH_5 /*!< triangle amplitude is 31 */
  92. #define DAC_TRIANGLE_AMPLITUDE_63 DAC_WAVE_BIT_WIDTH_6 /*!< triangle amplitude is 63 */
  93. #define DAC_TRIANGLE_AMPLITUDE_127 DAC_WAVE_BIT_WIDTH_7 /*!< triangle amplitude is 127 */
  94. #define DAC_TRIANGLE_AMPLITUDE_255 DAC_WAVE_BIT_WIDTH_8 /*!< triangle amplitude is 255 */
  95. #define DAC_TRIANGLE_AMPLITUDE_511 DAC_WAVE_BIT_WIDTH_9 /*!< triangle amplitude is 511 */
  96. #define DAC_TRIANGLE_AMPLITUDE_1023 DAC_WAVE_BIT_WIDTH_10 /*!< triangle amplitude is 1023 */
  97. #define DAC_TRIANGLE_AMPLITUDE_2047 DAC_WAVE_BIT_WIDTH_11 /*!< triangle amplitude is 2047 */
  98. #define DAC_TRIANGLE_AMPLITUDE_4095 DAC_WAVE_BIT_WIDTH_12 /*!< triangle amplitude is 4095 */
  99. /* DAC data alignment */
  100. #define DATA_ALIGN(regval) (BITS(0,1) & ((uint32_t)(regval) << 0))
  101. #define DAC_ALIGN_12B_R DATA_ALIGN(0) /*!< data right 12b alignment */
  102. #define DAC_ALIGN_12B_L DATA_ALIGN(1) /*!< data left 12b alignment */
  103. #define DAC_ALIGN_8B_R DATA_ALIGN(2) /*!< data right 8b alignment */
  104. /* function declarations */
  105. /* deinitialize DAC */
  106. void dac_deinit(void);
  107. /* enable DAC */
  108. void dac_enable(void);
  109. /* disable DAC */
  110. void dac_disable(void);
  111. /* enable DAC DMA */
  112. void dac_dma_enable(void);
  113. /* disable DAC DMA */
  114. void dac_dma_disable(void);
  115. /* enable DAC output buffer */
  116. void dac_output_buffer_enable(void);
  117. /* disable DAC output buffer */
  118. void dac_output_buffer_disable(void);
  119. /* enable DAC trigger */
  120. void dac_trigger_enable(void);
  121. /* disable DAC trigger */
  122. void dac_trigger_disable(void);
  123. /* enable DAC software trigger */
  124. void dac_software_trigger_enable(void);
  125. /* disable DAC software trigger */
  126. void dac_software_trigger_disable(void);
  127. /* enable DAC interrupt(DAC DMA underrun interrupt) */
  128. void dac_interrupt_enable(void);
  129. /* disable DAC interrupt(DAC DMA underrun interrupt) */
  130. void dac_interrupt_disable(void);
  131. /* configure DAC trigger source */
  132. void dac_trigger_source_config(uint32_t triggersource);
  133. /* configure DAC wave mode */
  134. void dac_wave_mode_config(uint32_t wave_mode);
  135. /* configure DAC wave bit width */
  136. void dac_wave_bit_width_config(uint32_t bit_width);
  137. /* configure DAC LFSR noise mode */
  138. void dac_lfsr_noise_config(uint32_t unmask_bits);
  139. /* configure DAC triangle noise mode */
  140. void dac_triangle_noise_config(uint32_t amplitude);
  141. /* get the last data output value */
  142. uint16_t dac_output_value_get(void);
  143. /* get the specified DAC flag(DAC DMA underrun flag) */
  144. FlagStatus dac_flag_get(void);
  145. /* clear the specified DAC flag(DAC DMA underrun flag) */
  146. void dac_flag_clear(void);
  147. /* get the specified DAC interrupt flag(DAC DMA underrun interrupt flag) */
  148. FlagStatus dac_interrupt_flag_get(void);
  149. /* clear the specified DAC interrupt flag(DAC DMA underrun interrupt flag) */
  150. void dac_interrupt_flag_clear(void);
  151. /* set DAC data holding register value */
  152. void dac_data_set(uint32_t dac_align, uint16_t data);
  153. #endif /* GD32F3X0_DAC_H */
  154. #endif /* GD32F350 */