gd32f3x0_dma.h 17 KB

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