gd32f1x0_dac.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*!
  2. \file gd32f1x0_dac.h
  3. \brief definitions for the DAC
  4. \version 2014-12-26, V1.0.0, platform GD32F1x0(x=3,5)
  5. \version 2016-01-15, V2.0.0, platform GD32F1x0(x=3,5,7,9)
  6. \version 2016-04-30, V3.0.0, firmware update for GD32F1x0(x=3,5,7,9)
  7. \version 2017-06-19, V3.1.0, firmware update for GD32F1x0(x=3,5,7,9)
  8. \version 2019-11-20, V3.2.0, firmware update for GD32F1x0(x=3,5,7,9)
  9. */
  10. /*
  11. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  12. Redistribution and use in source and binary forms, with or without modification,
  13. are permitted provided that the following conditions are met:
  14. 1. Redistributions of source code must retain the above copyright notice, this
  15. list of conditions and the following disclaimer.
  16. 2. Redistributions in binary form must reproduce the above copyright notice,
  17. this list of conditions and the following disclaimer in the documentation
  18. and/or other materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its contributors
  20. may be used to endorse or promote products derived from this software without
  21. specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. OF SUCH DAMAGE.
  32. */
  33. #ifndef GD32F1X0_DAC_H
  34. #define GD32F1X0_DAC_H
  35. #include "gd32f1x0.h"
  36. /* DACx(x=0,1) definitions */
  37. #define DAC DAC_BASE
  38. #define DAC0 0U
  39. #ifdef GD32F170_190
  40. #define DAC1 1U
  41. #endif /* GD32F170_190 */
  42. /* registers definitions */
  43. #define DAC_CTL REG32(DAC + 0x00U) /*!< DAC control register */
  44. #define DAC_SWT REG32(DAC + 0x04U) /*!< DAC software trigger register */
  45. #define DAC0_R12DH REG32(DAC + 0x08U) /*!< DAC0 12-bit right-aligned data holding register */
  46. #define DAC0_L12DH REG32(DAC + 0x0CU) /*!< DAC0 12-bit left-aligned data holding register */
  47. #define DAC0_R8DH REG32(DAC + 0x10U) /*!< DAC0 8-bit right-aligned data holding register */
  48. #ifdef GD32F170_190
  49. #define DAC1_R12DH REG32(DAC + 0x14U) /*!< DAC1 12-bit right-aligned data holding register */
  50. #define DAC1_L12DH REG32(DAC + 0x18U) /*!< DAC1 12-bit left-aligned data holding register */
  51. #define DAC1_R8DH REG32(DAC + 0x1CU) /*!< DAC1 8-bit right-aligned data holding register */
  52. #define DACC_R12DH REG32(DAC + 0x20U) /*!< DAC concurrent mode 12-bit right-aligned data holding register */
  53. #define DACC_L12DH REG32(DAC + 0x24U) /*!< DAC concurrent mode 12-bit left-aligned data holding register */
  54. #define DACC_R8DH REG32(DAC + 0x28U) /*!< DAC concurrent mode 8-bit right-aligned data holding register */
  55. #endif /* GD32F170_190 */
  56. #define DAC0_DO REG32(DAC + 0x2CU) /*!< DAC0 output data register */
  57. #ifdef GD32F170_190
  58. #define DAC1_DO REG32(DAC + 0x30U) /*!< DAC1 output data register */
  59. #endif /* GD32F170_190 */
  60. #define DAC_STAT REG32(DAC + 0x34U) /*!< DAC status register */
  61. /* bits definitions */
  62. /* DAC_CTL */
  63. #define DAC_CTL_DEN0 BIT(0) /*!< DAC0 enable/disable bit */
  64. #define DAC_CTL_DBOFF0 BIT(1) /*!< DAC0 output buffer turn on/turn off bit */
  65. #define DAC_CTL_DTEN0 BIT(2) /*!< DAC0 trigger enable/disable bit */
  66. #define DAC_CTL_DTSEL0 BITS(3,5) /*!< DAC0 trigger source selection enable/disable bits */
  67. #define DAC_CTL_DDMAEN0 BIT(12) /*!< DAC0 DMA enable/disanle bit */
  68. #define DAC_CTL_DDUDRIE0 BIT(13) /*!< DAC0 DMA underrun Interrupt enable/disable bit */
  69. #ifdef GD32F170_190
  70. #define DAC_CTL_DEN1 BIT(16) /*!< DAC1 enable/disable bit */
  71. #define DAC_CTL_DBOFF1 BIT(17) /*!< DAC1 output buffer turn on/turn off bit */
  72. #define DAC_CTL_DTEN1 BIT(18) /*!< DAC1 trigger enable/disable bit */
  73. #define DAC_CTL_DTSEL1 BITS(19,21) /*!< DAC1 trigger source selection enable/disable bits */
  74. #define DAC_CTL_DDMAEN1 BIT(28) /*!< DAC1 DMA enable/disable bit */
  75. #define DAC_CTL_DDUDRIE1 BIT(29) /*!< DAC1 DMA underrun interrupt enable/disable bit */
  76. #endif /* GD32F170_190 */
  77. /* DAC_SWT */
  78. #define DAC_SWT_SWTR0 BIT(0) /*!< DAC0 software trigger bit,cleared by hardware */
  79. #ifdef GD32F170_190
  80. #define DAC_SWT_SWTR1 BIT(1) /*!< DAC1 software trigger bit,cleared by hardware */
  81. #endif /* GD32F170_190 */
  82. /* DAC0_R12DH */
  83. #define DAC0_R12DH_DAC0_DH BITS(0,11) /*!< DAC0 12-bit right-aligned data bits */
  84. /* DAC0_L12DH */
  85. #define DAC0_L12DH_DAC0_DH BITS(4,15) /*!< DAC0 12-bit left-aligned data bits */
  86. /* DAC0_R8DH */
  87. #define DAC0_R8DH_DAC0_DH BITS(0,7) /*!< DAC0 8-bit right-aligned data bits */
  88. #ifdef GD32F170_190
  89. /* DAC1_R12DH */
  90. #define DAC1_R12DH_DAC1_DH BITS(0,11) /*!< DAC1 12-bit right-aligned data bits */
  91. /* DAC1_L12DH */
  92. #define DAC1_L12DH_DAC1_DH BITS(4,15) /*!< DAC1 12-bit left-aligned data bits */
  93. /* DAC1_R8DH */
  94. #define DAC1_R8DH_DAC1_DH BITS(0,7) /*!< DAC1 8-bit right-aligned data bits */
  95. /* DACC_R12DH */
  96. #define DACC_R12DH_DAC0_DH BITS(0,11) /*!< DAC concurrent mode DAC0 12-bit right-aligned data bits */
  97. #define DACC_R12DH_DAC1_DH BITS(16,27) /*!< DAC concurrent mode DAC1 12-bit right-aligned data bits */
  98. /* DACC_L12DH */
  99. #define DACC_L12DH_DAC0_DH BITS(4,15) /*!< DAC concurrent mode DAC0 12-bit left-aligned data bits */
  100. #define DACC_L12DH_DAC1_DH BITS(20,31) /*!< DAC concurrent mode DAC1 12-bit left-aligned data bits */
  101. /* DACC_R8DH */
  102. #define DACC_R8DH_DAC0_DH BITS(0,7) /*!< DAC concurrent mode DAC0 8-bit right-aligned data bits */
  103. #define DACC_R8DH_DAC1_DH BITS(8,15) /*!< DAC concurrent mode DAC1 8-bit right-aligned data bits */
  104. #endif /* GD32F170_190 */
  105. /* DAC0_DO */
  106. #define DAC0_DO_DAC0_DO BITS(0,11) /*!< DAC0 12-bit output data bits */
  107. #ifdef GD32F170_190
  108. /* DAC1_DO */
  109. #define DAC1_DO_DAC1_DO BITS(0,11) /*!< DAC1 12-bit output data bits */
  110. #endif /* GD32F170_190 */
  111. /* DAC_STAT */
  112. #define DAC_STAT_DDUDR0 BIT(13) /*!< DAC0 DMA underrun flag */
  113. #ifdef GD32F170_190
  114. #define DAC_STAT_DDUDR1 BIT(29) /*!< DAC1 DMA underrun flag */
  115. #endif /* GD32F170_190 */
  116. /* constants definitions */
  117. /* DAC trigger source */
  118. #define CTL_DTSEL(regval) (BITS(3,5) & ((uint32_t)(regval) << 3))
  119. #define DAC_TRIGGER_T5_TRGO CTL_DTSEL(0) /*!< TIMER5 TRGO */
  120. #define DAC_TRIGGER_T2_TRGO CTL_DTSEL(1) /*!< TIMER2 TRGO */
  121. #define DAC_TRIGGER_T14_TRGO CTL_DTSEL(3) /*!< TIMER14 TRGO */
  122. #define DAC_TRIGGER_T1_TRGO CTL_DTSEL(4) /*!< TIMER1 TRGO */
  123. #define DAC_TRIGGER_EXTI_9 CTL_DTSEL(6) /*!< EXTI interrupt line9 event */
  124. #define DAC_TRIGGER_SOFTWARE CTL_DTSEL(7) /*!< software trigger */
  125. /* dac data alignment */
  126. #define DATA_ALIGN(regval) (BITS(0,1) & ((uint32_t)(regval) << 0))
  127. #define DAC_ALIGN_12B_R DATA_ALIGN(0) /*!< data right 12b alignment */
  128. #define DAC_ALIGN_12B_L DATA_ALIGN(1) /*!< data left 12b alignment */
  129. #define DAC_ALIGN_8B_R DATA_ALIGN(2) /*!< data right 8b alignment */
  130. /* function declarations */
  131. /* deinit DAC */
  132. void dac_deinit(void);
  133. /* enable DAC0 function */
  134. void dac0_enable(void);
  135. /* disable DAC0 function */
  136. void dac0_disable(void);
  137. /* enable DAC0 DMA function */
  138. void dac0_dma_enable(void);
  139. /* disable DAC0 DMA function */
  140. void dac0_dma_disable(void);
  141. /* enable DAC0 output buffer function */
  142. void dac0_output_buffer_enable(void);
  143. /* disable DAC0 output buffer function */
  144. void dac0_output_buffer_disable(void);
  145. /* enable DAC0 trigger function */
  146. void dac0_trigger_enable(void);
  147. /* disable DAC0 trigger function */
  148. void dac0_trigger_disable(void);
  149. /* enable DAC0 software trigger function */
  150. void dac0_software_trigger_enable(void);
  151. /* disable DAC0 software trigger function */
  152. void dac0_software_trigger_disable(void);
  153. /* enable DAC0 interrupt(DAC0 DMA underrun interrupt) */
  154. void dac0_interrupt_enable(void);
  155. /* disable DAC0 interrupt(DAC0 DMA underrun interrupt) */
  156. void dac0_interrupt_disable(void);
  157. /* set DAC0 tgigger source function */
  158. void dac0_trigger_source_config(uint32_t triggersource);
  159. /* get the last data output value */
  160. uint16_t dac0_output_value_get(void);
  161. /* get the specified DAC0 flag(DAC0 DMA underrun flag) */
  162. FlagStatus dac0_flag_get(void);
  163. /* clear the specified DAC0 flag(DAC0 DMA underrun flag) */
  164. void dac0_flag_clear(void);
  165. /* get the specified DAC0 interrupt flag(DAC0 DMA underrun interrupt flag) */
  166. FlagStatus dac0_interrupt_flag_get(void);
  167. /* clear the specified DAC0 interrupt flag(DAC0 DMA underrun interrupt flag) */
  168. void dac0_interrupt_flag_clear(void);
  169. /* set DAC0 data holding register value */
  170. void dac0_data_set(uint32_t dac_align, uint16_t data);
  171. #ifdef GD32F170_190
  172. /* enable DAC */
  173. void dac_enable(uint32_t dac_periph);
  174. /* disable DAC */
  175. void dac_disable(uint32_t dac_periph);
  176. /* enable DAC DMA */
  177. void dac_dma_enable(uint32_t dac_periph);
  178. /* disable DAC DMA */
  179. void dac_dma_disable(uint32_t dac_periph);
  180. /* enable DAC output buffer */
  181. void dac_output_buffer_enable(uint32_t dac_periph);
  182. /* disable DAC output buffer */
  183. void dac_output_buffer_disable(uint32_t dac_periph);
  184. /* enable DAC trigger */
  185. void dac_trigger_enable(uint32_t dac_periph);
  186. /* disable DAC trigger */
  187. void dac_trigger_disable(uint32_t dac_periph);
  188. /* enable DAC software trigger */
  189. void dac_software_trigger_enable(uint32_t dac_periph);
  190. /* disable DAC software trigger */
  191. void dac_software_trigger_disable(uint32_t dac_periph);
  192. /* enable DAC interrupt(DAC0 DMA underrun interrupt) */
  193. void dac_interrupt_enable(uint32_t dac_periph);
  194. /* disable DAC interrupt(DAC0 DMA underrun interrupt) */
  195. void dac_interrupt_disable(uint32_t dac_periph);
  196. /* set DAC tgigger source */
  197. void dac_trigger_source_config(uint32_t dac_periph,uint32_t triggersource);
  198. /* get the last data output value */
  199. uint16_t dac_output_value_get(uint32_t dac_periph);
  200. /* get the specified DAC flag(DAC DMA underrun flag) */
  201. FlagStatus dac_flag_get(uint32_t dac_periph);
  202. /* clear the specified DAC flag(DAC DMA underrun flag) */
  203. void dac_flag_clear(uint32_t dac_periph);
  204. /* get the specified DAC interrupt flag(DAC DMA underrun interrupt flag) */
  205. FlagStatus dac_interrupt_flag_get(uint32_t dac_periph);
  206. /* clear the specified DAC interrupt flag(DAC DMA underrun interrupt flag) */
  207. void dac_interrupt_flag_clear(uint32_t dac_periph);
  208. /* enable DAC concurrent mode */
  209. void dac_concurrent_enable(void);
  210. /* disable DAC concurrent mode */
  211. void dac_concurrent_disable(void);
  212. /* enable DAC concurrent software trigger */
  213. void dac_concurrent_software_trigger_enable(void);
  214. /* disable DAC concurrent software trigger */
  215. void dac_concurrent_software_trigger_disable(void);
  216. /* enable DAC concurrent buffer */
  217. void dac_concurrent_output_buffer_enable(void);
  218. /* disable DAC concurrent buffer */
  219. void dac_concurrent_output_buffer_disable(void);
  220. /* enable DAC concurrent interrupt */
  221. void dac_concurrent_interrupt_enable(void);
  222. /* disable DAC concurrent interrupt */
  223. void dac_concurrent_interrupt_disable(void);
  224. /* set DAC data holding register value */
  225. void dac_data_set(uint32_t dac_periph, uint32_t dac_align, uint16_t data);
  226. /* set DAC concurrent mode data holding register value */
  227. void dac_concurrent_data_set(uint32_t dac_align, uint16_t data1, uint16_t data2);
  228. #endif /* GD32F170_190 */
  229. #endif /* GD32F1X0_DAC_H */