gd32f30x_dma.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*!
  2. \file gd32f30x_dma.h
  3. \brief definitions for the DMA
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-02-10, V1.0.1, firmware for GD32F30x
  8. */
  9. #ifndef GD32F30X_DMA_H
  10. #define GD32F30X_DMA_H
  11. #include "gd32f30x.h"
  12. /* DMA definitions */
  13. #define DMA0 (DMA_BASE) /*!< DMA0 base address */
  14. #define DMA1 (DMA_BASE + 0x0400U) /*!< DMA1 base address */
  15. /* registers definitions */
  16. #define DMA_INTF(dmax) REG32((dmax) + 0x00U) /*!< DMA interrupt flag register */
  17. #define DMA_INTC(dmax) REG32((dmax) + 0x04U) /*!< DMA interrupt flag clear register */
  18. #define DMA_CH0CTL(dmax) REG32((dmax) + 0x08U) /*!< DMA channel 0 control register */
  19. #define DMA_CH0CNT(dmax) REG32((dmax) + 0x0CU) /*!< DMA channel 0 counter register */
  20. #define DMA_CH0PADDR(dmax) REG32((dmax) + 0x10U) /*!< DMA channel 0 peripheral base address register */
  21. #define DMA_CH0MADDR(dmax) REG32((dmax) + 0x14U) /*!< DMA channel 0 memory base address register */
  22. #define DMA_CH1CTL(dmax) REG32((dmax) + 0x1CU) /*!< DMA channel 1 control register */
  23. #define DMA_CH1CNT(dmax) REG32((dmax) + 0x20U) /*!< DMA channel 1 counter register */
  24. #define DMA_CH1PADDR(dmax) REG32((dmax) + 0x24U) /*!< DMA channel 1 peripheral base address register */
  25. #define DMA_CH1MADDR(dmax) REG32((dmax) + 0x28U) /*!< DMA channel 1 memory base address register */
  26. #define DMA_CH2CTL(dmax) REG32((dmax) + 0x30U) /*!< DMA channel 2 control register */
  27. #define DMA_CH2CNT(dmax) REG32((dmax) + 0x34U) /*!< DMA channel 2 counter register */
  28. #define DMA_CH2PADDR(dmax) REG32((dmax) + 0x38U) /*!< DMA channel 2 peripheral base address register */
  29. #define DMA_CH2MADDR(dmax) REG32((dmax) + 0x3CU) /*!< DMA channel 2 memory base address register */
  30. #define DMA_CH3CTL(dmax) REG32((dmax) + 0x44U) /*!< DMA channel 3 control register */
  31. #define DMA_CH3CNT(dmax) REG32((dmax) + 0x48U) /*!< DMA channel 3 counter register */
  32. #define DMA_CH3PADDR(dmax) REG32((dmax) + 0x4CU) /*!< DMA channel 3 peripheral base address register */
  33. #define DMA_CH3MADDR(dmax) REG32((dmax) + 0x50U) /*!< DMA channel 3 memory base address register */
  34. #define DMA_CH4CTL(dmax) REG32((dmax) + 0x58U) /*!< DMA channel 4 control register */
  35. #define DMA_CH4CNT(dmax) REG32((dmax) + 0x5CU) /*!< DMA channel 4 counter register */
  36. #define DMA_CH4PADDR(dmax) REG32((dmax) + 0x60U) /*!< DMA channel 4 peripheral base address register */
  37. #define DMA_CH4MADDR(dmax) REG32((dmax) + 0x64U) /*!< DMA channel 4 memory base address register */
  38. #define DMA_CH5CTL(dmax) REG32((dmax) + 0x6CU) /*!< DMA channel 5 control register */
  39. #define DMA_CH5CNT(dmax) REG32((dmax) + 0x70U) /*!< DMA channel 5 counter register */
  40. #define DMA_CH5PADDR(dmax) REG32((dmax) + 0x74U) /*!< DMA channel 5 peripheral base address register */
  41. #define DMA_CH5MADDR(dmax) REG32((dmax) + 0x78U) /*!< DMA channel 5 memory base address register */
  42. #define DMA_CH6CTL(dmax) REG32((dmax) + 0x80U) /*!< DMA channel 6 control register */
  43. #define DMA_CH6CNT(dmax) REG32((dmax) + 0x84U) /*!< DMA channel 6 counter register */
  44. #define DMA_CH6PADDR(dmax) REG32((dmax) + 0x88U) /*!< DMA channel 6 peripheral base address register */
  45. #define DMA_CH6MADDR(dmax) REG32((dmax) + 0x8CU) /*!< DMA channel 6 memory base address register */
  46. /* bits definitions */
  47. /* DMA_INTF */
  48. #define DMA_INTF_GIF BIT(0) /*!< global interrupt flag of channel */
  49. #define DMA_INTF_FTFIF BIT(1) /*!< full transfer finish flag of channel */
  50. #define DMA_INTF_HTFIF BIT(2) /*!< half transfer finish flag of channel */
  51. #define DMA_INTF_ERRIF BIT(3) /*!< error flag of channel */
  52. /* DMA_INTC */
  53. #define DMA_INTC_GIFC BIT(0) /*!< clear global interrupt flag of channel */
  54. #define DMA_INTC_FTFIFC BIT(1) /*!< clear transfer finish flag of channel */
  55. #define DMA_INTC_HTFIFC BIT(2) /*!< clear half transfer finish flag of channel */
  56. #define DMA_INTC_ERRIFC BIT(3) /*!< clear error flag of channel */
  57. /* DMA_CHxCTL, x=0..6 */
  58. #define DMA_CHXCTL_CHEN BIT(0) /*!< channel enable */
  59. #define DMA_CHXCTL_FTFIE BIT(1) /*!< enable bit for channel full transfer finish interrupt */
  60. #define DMA_CHXCTL_HTFIE BIT(2) /*!< enable bit for channel half transfer finish interrupt */
  61. #define DMA_CHXCTL_ERRIE BIT(3) /*!< enable bit for channel error interrupt */
  62. #define DMA_CHXCTL_DIR BIT(4) /*!< transfer direction */
  63. #define DMA_CHXCTL_CMEN BIT(5) /*!< circular mode enable */
  64. #define DMA_CHXCTL_PNAGA BIT(6) /*!< next address generation algorithm of peripheral */
  65. #define DMA_CHXCTL_MNAGA BIT(7) /*!< next address generation algorithm of memory */
  66. #define DMA_CHXCTL_PWIDTH BITS(8,9) /*!< transfer data width of peripheral */
  67. #define DMA_CHXCTL_MWIDTH BITS(10,11) /*!< transfer data width of memory */
  68. #define DMA_CHXCTL_PRIO BITS(12,13) /*!< priority level */
  69. #define DMA_CHXCTL_M2M BIT(14) /*!< memory to memory mode */
  70. /* DMA_CHxCNT,x=0..6 */
  71. #define DMA_CHXCNT_CNT BITS(0,15) /*!< transfer counter */
  72. /* DMA_CHxPADDR,x=0..6 */
  73. #define DMA_CHXPADDR_PADDR BITS(0,31) /*!< peripheral base address */
  74. /* DMA_CHxMADDR,x=0..6 */
  75. #define DMA_CHXMADDR_MADDR BITS(0,31) /*!< memory base address */
  76. /* constants definitions */
  77. /* DMA channel select */
  78. typedef enum
  79. {
  80. DMA_CH0 = 0, /*!< DMA Channel0 */
  81. DMA_CH1, /*!< DMA Channel1 */
  82. DMA_CH2, /*!< DMA Channel2 */
  83. DMA_CH3, /*!< DMA Channel3 */
  84. DMA_CH4, /*!< DMA Channel4 */
  85. DMA_CH5, /*!< DMA Channel5 */
  86. DMA_CH6 /*!< DMA Channel6 */
  87. } dma_channel_enum;
  88. /* DMA initialize struct */
  89. typedef struct
  90. {
  91. uint32_t periph_addr; /*!< peripheral base address */
  92. uint32_t periph_width; /*!< transfer data size of peripheral */
  93. uint32_t memory_addr; /*!< memory base address */
  94. uint32_t memory_width; /*!< transfer data size of memory */
  95. uint32_t number; /*!< channel transfer number */
  96. uint32_t priority; /*!< channel priority level */
  97. uint8_t periph_inc; /*!< peripheral increasing mode */
  98. uint8_t memory_inc; /*!< memory increasing mode */
  99. uint8_t direction; /*!< channel data transfer direction */
  100. } dma_parameter_struct;
  101. #define DMA_FLAG_ADD(flag, shift) ((flag) << ((shift) * 4U)) /*!< DMA channel flag shift */
  102. /* DMA_register address */
  103. #define DMA_CHCTL(dma, channel) REG32(((dma) + 0x08U) + 0x14U * (uint32_t)(channel)) /*!< the address of DMA channel CHXCTL register */
  104. #define DMA_CHCNT(dma, channel) REG32(((dma) + 0x0CU) + 0x14U * (uint32_t)(channel)) /*!< the address of DMA channel CHXCNT register */
  105. #define DMA_CHPADDR(dma, channel) REG32(((dma) + 0x10U) + 0x14U * (uint32_t)(channel)) /*!< the address of DMA channel CHXPADDR register */
  106. #define DMA_CHMADDR(dma, channel) REG32(((dma) + 0x14U) + 0x14U * (uint32_t)(channel)) /*!< the address of DMA channel CHXMADDR register */
  107. /* DMA reset value */
  108. #define DMA_CHCTL_RESET_VALUE ((uint32_t)0x00000000U) /*!< the reset value of DMA channel CHXCTL register */
  109. #define DMA_CHCNT_RESET_VALUE ((uint32_t)0x00000000U) /*!< the reset value of DMA channel CHXCNT register */
  110. #define DMA_CHPADDR_RESET_VALUE ((uint32_t)0x00000000U) /*!< the reset value of DMA channel CHXPADDR register */
  111. #define DMA_CHMADDR_RESET_VALUE ((uint32_t)0x00000000U) /*!< the reset value of DMA channel CHXMADDR register */
  112. #define DMA_CHINTF_RESET_VALUE (DMA_INTF_GIF | DMA_INTF_FTFIF | \
  113. DMA_INTF_HTFIF | DMA_INTF_ERRIF) /*!< clear DMA channel DMA_INTF register */
  114. /* DMA_INTF register */
  115. /* interrupt flag bits */
  116. #define DMA_INT_FLAG_G DMA_INTF_GIF /*!< global interrupt flag of channel */
  117. #define DMA_INT_FLAG_FTF DMA_INTF_FTFIF /*!< full transfer finish interrupt flag of channel */
  118. #define DMA_INT_FLAG_HTF DMA_INTF_HTFIF /*!< half transfer finish interrupt flag of channel */
  119. #define DMA_INT_FLAG_ERR DMA_INTF_ERRIF /*!< error interrupt flag of channel */
  120. /* flag bits */
  121. #define DMA_FLAG_G DMA_INTF_GIF /*!< global interrupt flag of channel */
  122. #define DMA_FLAG_FTF DMA_INTF_FTFIF /*!< full transfer finish flag of channel */
  123. #define DMA_FLAG_HTF DMA_INTF_HTFIF /*!< half transfer finish flag of channel */
  124. #define DMA_FLAG_ERR DMA_INTF_ERRIF /*!< error flag of channel */
  125. /* DMA_CHxCTL register */
  126. /* interrupt enable bits */
  127. #define DMA_INT_FTF DMA_CHXCTL_FTFIE /*!< enable bit for channel full transfer finish interrupt */
  128. #define DMA_INT_HTF DMA_CHXCTL_HTFIE /*!< enable bit for channel half transfer finish interrupt */
  129. #define DMA_INT_ERR DMA_CHXCTL_ERRIE /*!< enable bit for channel error interrupt */
  130. /* transfer direction */
  131. #define DMA_PERIPHERAL_TO_MEMORY ((uint32_t)0x00000000U) /*!< read from peripheral and write to memory */
  132. #define DMA_MEMORY_TO_PERIPHERAL ((uint32_t)0x00000001U) /*!< read from memory and write to peripheral */
  133. /* circular mode */
  134. #define DMA_CIRCULAR_MODE_DISABLE ((uint32_t)0x00000000U) /*!< circular mode disable */
  135. #define DMA_CIRCULAR_MODE_ENABLE ((uint32_t)0x00000001U) /*!< circular mode enable */
  136. /* peripheral increasing mode */
  137. #define DMA_PERIPH_INCREASE_DISABLE ((uint32_t)0x00000000U) /*!< next address of peripheral is fixed address mode */
  138. #define DMA_PERIPH_INCREASE_ENABLE ((uint32_t)0x00000001U) /*!< next address of peripheral is increasing address mode */
  139. /* memory increasing mode */
  140. #define DMA_MEMORY_INCREASE_DISABLE ((uint32_t)0x00000000U) /*!< next address of memory is fixed address mode */
  141. #define DMA_MEMORY_INCREASE_ENABLE ((uint32_t)0x00000001U) /*!< next address of memory is increasing address mode */
  142. /* transfer data size of peripheral */
  143. #define CHCTL_PWIDTH(regval) (BITS(8,9) & ((regval) << 8)) /*!< transfer data size of peripheral */
  144. #define DMA_PERIPHERAL_WIDTH_8BIT CHCTL_PWIDTH(0) /*!< transfer data size of peripheral is 8-bit */
  145. #define DMA_PERIPHERAL_WIDTH_16BIT CHCTL_PWIDTH(1) /*!< transfer data size of peripheral is 16-bit */
  146. #define DMA_PERIPHERAL_WIDTH_32BIT CHCTL_PWIDTH(2) /*!< transfer data size of peripheral is 32-bit */
  147. /* transfer data size of memory */
  148. #define CHCTL_MWIDTH(regval) (BITS(10,11) & ((regval) << 10)) /*!< transfer data size of memory */
  149. #define DMA_MEMORY_WIDTH_8BIT CHCTL_MWIDTH(0) /*!< transfer data size of memory is 8-bit */
  150. #define DMA_MEMORY_WIDTH_16BIT CHCTL_MWIDTH(1) /*!< transfer data size of memory is 16-bit */
  151. #define DMA_MEMORY_WIDTH_32BIT CHCTL_MWIDTH(2) /*!< transfer data size of memory is 32-bit */
  152. /* channel priority level */
  153. #define CHCTL_PRIO(regval) (BITS(12,13) & ((regval) << 12)) /*!< DMA channel priority level */
  154. #define DMA_PRIORITY_LOW CHCTL_PRIO(0) /*!< low priority */
  155. #define DMA_PRIORITY_MEDIUM CHCTL_PRIO(1) /*!< medium priority */
  156. #define DMA_PRIORITY_HIGH CHCTL_PRIO(2) /*!< high priority */
  157. #define DMA_PRIORITY_ULTRA_HIGH CHCTL_PRIO(3) /*!< ultra high priority */
  158. /* memory to memory mode */
  159. #define DMA_MEMORY_TO_MEMORY_DISABLE ((uint32_t)0x00000000U)
  160. #define DMA_MEMORY_TO_MEMORY_ENABLE ((uint32_t)0x00000001U)
  161. /* DMA_CHxCNT register */
  162. /* transfer counter */
  163. #define DMA_CHANNEL_CNT_MASK DMA_CHXCNT_CNT /*!< transfer counter mask */
  164. /* function declarations */
  165. /* deinitialize DMA a channel registers */
  166. void dma_deinit(uint32_t dma_periph, dma_channel_enum channelx);
  167. /* initialize DMA channel */
  168. void dma_init(uint32_t dma_periph, dma_channel_enum channelx, dma_parameter_struct init_struct);
  169. /* enable DMA circulation mode */
  170. void dma_circulation_enable(uint32_t dma_periph, dma_channel_enum channelx);
  171. /* disable DMA circulation mode */
  172. void dma_circulation_disable(uint32_t dma_periph, dma_channel_enum channelx);
  173. /* enable memory to memory mode */
  174. void dma_memory_to_memory_enable(uint32_t dma_periph, dma_channel_enum channelx);
  175. /* disable memory to memory mode */
  176. void dma_memory_to_memory_disable(uint32_t dma_periph, dma_channel_enum channelx);
  177. /* enable DMA channel */
  178. void dma_channel_enable(uint32_t dma_periph, dma_channel_enum channelx);
  179. /* disable DMA channel */
  180. void dma_channel_disable(uint32_t dma_periph, dma_channel_enum channelx);
  181. /* set DMA peripheral base address */
  182. void dma_periph_address_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t address);
  183. /* set DMA Memory base address */
  184. void dma_memory_address_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t address);
  185. /* set the number of remaining data to be transferred by the DMA */
  186. void dma_transfer_number_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t number);
  187. /* get the number of remaining data to be transferred by the DMA */
  188. uint32_t dma_transfer_number_get(uint32_t dma_periph, dma_channel_enum channelx);
  189. /* configure priority level of DMA channel */
  190. void dma_priority_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t priority);
  191. /* configure transfer data size of memory */
  192. void dma_memory_width_config (uint32_t dma_periph, dma_channel_enum channelx, uint32_t mwidth);
  193. /* configure transfer data size of peripheral */
  194. void dma_periph_width_config (uint32_t dma_periph, dma_channel_enum channelx, uint32_t pwidth);
  195. /* enable next address increasement algorithm of memory */
  196. void dma_memory_increase_enable(uint32_t dma_periph, dma_channel_enum channelx);
  197. /* disable next address increasement algorithm of memory */
  198. void dma_memory_increase_disable(uint32_t dma_periph, dma_channel_enum channelx);
  199. /* enable next address increasement algorithm of peripheral */
  200. void dma_periph_increase_enable(uint32_t dma_periph, dma_channel_enum channelx);
  201. /* disable next address increasement algorithm of peripheral */
  202. void dma_periph_increase_disable(uint32_t dma_periph, dma_channel_enum channelx);
  203. /* configure the direction of data transfer on the channel */
  204. void dma_transfer_direction_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t direction);
  205. /* check DMA flag is set or not */
  206. FlagStatus dma_flag_get(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag);
  207. /* clear DMA a channel flag */
  208. void dma_flag_clear(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag);
  209. /* check DMA flag and interrupt enable bit is set or not */
  210. FlagStatus dma_interrupt_flag_get(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag);
  211. /* clear DMA a channel flag */
  212. void dma_interrupt_flag_clear(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag);
  213. /* enable DMA interrupt */
  214. void dma_interrupt_enable(uint32_t dma_periph, dma_channel_enum channelx, uint32_t source);
  215. /* disable DMA interrupt */
  216. void dma_interrupt_disable(uint32_t dma_periph, dma_channel_enum channelx, uint32_t source);
  217. #endif /* GD32F30X_DMA_H */