gd32e23x_dma.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*!
  2. \file gd32e23x_dma.h
  3. \brief definitions for the DMA
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. */
  7. /*
  8. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #ifndef GD32E23X_DMA_H
  31. #define GD32E23X_DMA_H
  32. #include "gd32e23x.h"
  33. /* DMA definitions */
  34. #define DMA DMA_BASE /*!< DMA base address */
  35. /* registers definitions */
  36. #define DMA_INTF REG32(DMA + 0x00U) /*!< DMA interrupt flag register */
  37. #define DMA_INTC REG32(DMA + 0x04U) /*!< DMA interrupt flag clear register */
  38. #define DMA_CH0CTL REG32(DMA + 0x08U) /*!< DMA channel 0 control register */
  39. #define DMA_CH0CNT REG32(DMA + 0x0CU) /*!< DMA channel 0 counter register */
  40. #define DMA_CH0PADDR REG32(DMA + 0x10U) /*!< DMA channel 0 peripheral base address register */
  41. #define DMA_CH0MADDR REG32(DMA + 0x14U) /*!< DMA channel 0 memory base address register */
  42. #define DMA_CH1CTL REG32(DMA + 0x1CU) /*!< DMA channel 1 control register */
  43. #define DMA_CH1CNT REG32(DMA + 0x20U) /*!< DMA channel 1 counter register */
  44. #define DMA_CH1PADDR REG32(DMA + 0x24U) /*!< DMA channel 1 peripheral base address register */
  45. #define DMA_CH1MADDR REG32(DMA + 0x28U) /*!< DMA channel 1 memory base address register */
  46. #define DMA_CH2CTL REG32(DMA + 0x30U) /*!< DMA channel 2 control register */
  47. #define DMA_CH2CNT REG32(DMA + 0x34U) /*!< DMA channel 2 counter register */
  48. #define DMA_CH2PADDR REG32(DMA + 0x38U) /*!< DMA channel 2 peripheral base address register */
  49. #define DMA_CH2MADDR REG32(DMA + 0x3CU) /*!< DMA channel 2 memory base address register */
  50. #define DMA_CH3CTL REG32(DMA + 0x44U) /*!< DMA channel 3 control register */
  51. #define DMA_CH3CNT REG32(DMA + 0x48U) /*!< DMA channel 3 counter register */
  52. #define DMA_CH3PADDR REG32(DMA + 0x4CU) /*!< DMA channel 3 peripheral base address register */
  53. #define DMA_CH3MADDR REG32(DMA + 0x50U) /*!< DMA channel 3 memory base address register */
  54. #define DMA_CH4CTL REG32(DMA + 0x58U) /*!< DMA channel 4 control register */
  55. #define DMA_CH4CNT REG32(DMA + 0x5CU) /*!< DMA channel 4 counter register */
  56. #define DMA_CH4PADDR REG32(DMA + 0x60U) /*!< DMA channel 4 peripheral base address register */
  57. #define DMA_CH4MADDR REG32(DMA + 0x64U) /*!< DMA channel 4 memory base address register */
  58. /* bits definitions */
  59. /* DMA_INTF */
  60. #define DMA_INTF_GIF BIT(0) /*!< global interrupt flag of channel */
  61. #define DMA_INTF_FTFIF BIT(1) /*!< full transfer finish flag of channel */
  62. #define DMA_INTF_HTFIF BIT(2) /*!< half transfer finish flag of channel */
  63. #define DMA_INTF_ERRIF BIT(3) /*!< error flag of channel */
  64. /* DMA_INTC */
  65. #define DMA_INTC_GIFC BIT(0) /*!< clear global interrupt flag of channel */
  66. #define DMA_INTC_FTFIFC BIT(1) /*!< clear transfer finish flag of channel */
  67. #define DMA_INTC_HTFIFC BIT(2) /*!< clear half transfer finish flag of channel */
  68. #define DMA_INTC_ERRIFC BIT(3) /*!< clear error flag of channel */
  69. /* DMA_CHxCTL,x=0..4 */
  70. #define DMA_CHXCTL_CHEN BIT(0) /*!< channel x enable */
  71. #define DMA_CHXCTL_FTFIE BIT(1) /*!< enable bit for channel x transfer complete interrupt */
  72. #define DMA_CHXCTL_HTFIE BIT(2) /*!< enable bit for channel x transfer half complete interrupt */
  73. #define DMA_CHXCTL_ERRIE BIT(3) /*!< enable bit for channel x error interrupt */
  74. #define DMA_CHXCTL_DIR BIT(4) /*!< direction of the data transfer on the channel */
  75. #define DMA_CHXCTL_CMEN BIT(5) /*!< circulation mode */
  76. #define DMA_CHXCTL_PNAGA BIT(6) /*!< next address generation algorithm of peripheral */
  77. #define DMA_CHXCTL_MNAGA BIT(7) /*!< next address generation algorithm of memory */
  78. #define DMA_CHXCTL_PWIDTH BITS(8,9) /*!< transfer data size of peripheral */
  79. #define DMA_CHXCTL_MWIDTH BITS(10,11) /*!< transfer data size of memory */
  80. #define DMA_CHXCTL_PRIO BITS(12,13) /*!< priority level of channelx */
  81. #define DMA_CHXCTL_M2M BIT(14) /*!< memory to memory mode */
  82. /* DMA_CHxCNT,x=0..4 */
  83. #define DMA_CHXCNT_CNT BITS(0,15) /*!< transfer counter */
  84. /* DMA_CHxPADDR,x=0..4 */
  85. #define DMA_CHXPADDR_PADDR BITS(0,31) /*!< peripheral base address */
  86. /* DMA_CHxMADDR,x=0..4 */
  87. #define DMA_CHXMADDR_MADDR BITS(0,31) /*!< memory base address */
  88. /* constants definitions */
  89. /* DMA channel select */
  90. typedef enum
  91. {
  92. DMA_CH0 = 0, /*!< DMA Channel0 */
  93. DMA_CH1, /*!< DMA Channel1 */
  94. DMA_CH2, /*!< DMA Channel2 */
  95. DMA_CH3, /*!< DMA Channel3 */
  96. DMA_CH4, /*!< DMA Channel4 */
  97. } dma_channel_enum;
  98. /* DMA initialize struct */
  99. typedef struct
  100. {
  101. uint32_t periph_addr; /*!< peripheral base address */
  102. uint32_t periph_width; /*!< transfer data size of peripheral */
  103. uint32_t memory_addr; /*!< memory base address */
  104. uint32_t memory_width; /*!< transfer data size of memory */
  105. uint32_t number; /*!< channel transfer number */
  106. uint32_t priority; /*!< channel priority level */
  107. uint8_t periph_inc; /*!< peripheral increasing mode */
  108. uint8_t memory_inc; /*!< memory increasing mode */
  109. uint8_t direction; /*!< channel data transfer direction */
  110. } dma_parameter_struct;
  111. /* DMA reset value */
  112. #define DMA_CHCTL_RESET_VALUE ((uint32_t)0x00000000U) /*!< the reset value of DMA channel CHXCTL register */
  113. #define DMA_CHCNT_RESET_VALUE ((uint32_t)0x00000000U) /*!< the reset value of DMA channel CHXCNT register */
  114. #define DMA_CHPADDR_RESET_VALUE ((uint32_t)0x00000000U) /*!< the reset value of DMA channel CHXPADDR register */
  115. #define DMA_CHMADDR_RESET_VALUE ((uint32_t)0x00000000U) /*!< the reset value of DMA channel CHXMADDR register */
  116. #define DMA_CHINTF_RESET_VALUE (DMA_INTF_GIF | DMA_INTF_FTFIF | \
  117. DMA_INTF_HTFIF | DMA_INTF_ERRIF)
  118. #define DMA_FLAG_ADD(flag,shift) ((flag) << ((uint32_t)(shift) * 4U)) /*!< DMA channel flag shift */
  119. /* DMA_CHCTL base address */
  120. #define DMA_CHXCTL_BASE (DMA + 0x08U) /*!< the base address of DMA channel CHXCTL register */
  121. #define DMA_CHXCNT_BASE (DMA + 0x0CU) /*!< the base address of DMA channel CHXCNT register */
  122. #define DMA_CHXPADDR_BASE (DMA + 0x10U) /*!< the base address of DMA channel CHXPADDR register */
  123. #define DMA_CHXMADDR_BASE (DMA + 0x14U) /*!< the base address of DMA channel CHXMADDR register */
  124. /* DMA channel shift bit */
  125. #define DMA_CHCTL(channel) REG32(DMA_CHXCTL_BASE + 0x14U * (uint32_t)(channel)) /*!< the address of DMA channel CHXCTL register */
  126. #define DMA_CHCNT(channel) REG32(DMA_CHXCNT_BASE + 0x14U * (uint32_t)(channel)) /*!< the address of DMA channel CHXCNT register */
  127. #define DMA_CHPADDR(channel) REG32(DMA_CHXPADDR_BASE + 0x14U * (uint32_t)(channel)) /*!< the address of DMA channel CHXPADDR register */
  128. #define DMA_CHMADDR(channel) REG32(DMA_CHXMADDR_BASE + 0x14U * (uint32_t)(channel)) /*!< the address of DMA channel CHXMADDR register */
  129. /* DMA_INTF register */
  130. /* interrupt flag bits */
  131. #define DMA_INT_FLAG_G DMA_INTF_GIF /*!< global interrupt flag of channel */
  132. #define DMA_INT_FLAG_FTF DMA_INTF_FTFIF /*!< full transfer finish interrupt flag of channel */
  133. #define DMA_INT_FLAG_HTF DMA_INTF_HTFIF /*!< half transfer finish interrupt flag of channel */
  134. #define DMA_INT_FLAG_ERR DMA_INTF_ERRIF /*!< error interrupt flag of channel */
  135. /* flag bits */
  136. #define DMA_FLAG_G DMA_INTF_GIF /*!< global interrupt flag of channel */
  137. #define DMA_FLAG_FTF DMA_INTF_FTFIF /*!< full transfer finish flag of channel */
  138. #define DMA_FLAG_HTF DMA_INTF_HTFIF /*!< half transfer finish flag of channel */
  139. #define DMA_FLAG_ERR DMA_INTF_ERRIF /*!< error flag of channel */
  140. /* DMA_CHxCTL register */
  141. /* interrupt enable bits */
  142. #define DMA_INT_FTF DMA_CHXCTL_FTFIE /*!< enable bit for channel full transfer finish interrupt */
  143. #define DMA_INT_HTF DMA_CHXCTL_HTFIE /*!< enable bit for channel half transfer finish interrupt */
  144. #define DMA_INT_ERR DMA_CHXCTL_ERRIE /*!< enable bit for channel error interrupt */
  145. /* transfer direction */
  146. #define DMA_PERIPHERAL_TO_MEMORY ((uint8_t)0x00U) /*!< read from peripheral and write to memory */
  147. #define DMA_MEMORY_TO_PERIPHERAL ((uint8_t)0x01U) /*!< read from memory and write to peripheral */
  148. /* peripheral increasing mode */
  149. #define DMA_PERIPH_INCREASE_DISABLE ((uint8_t)0x00U) /*!< next address of peripheral is fixed address mode */
  150. #define DMA_PERIPH_INCREASE_ENABLE ((uint8_t)0x01U) /*!< next address of peripheral is increasing address mode */
  151. /* memory increasing mode */
  152. #define DMA_MEMORY_INCREASE_DISABLE ((uint8_t)0x00U) /*!< next address of memory is fixed address mode */
  153. #define DMA_MEMORY_INCREASE_ENABLE ((uint8_t)0x01U) /*!< next address of memory is increasing address mode */
  154. /* transfer data size of peripheral */
  155. #define CHCTL_PWIDTH(regval) (BITS(8,9) & ((regval) << 8)) /*!< transfer data size of peripheral */
  156. #define DMA_PERIPHERAL_WIDTH_8BIT CHCTL_PWIDTH(0U) /*!< transfer data size of peripheral is 8-bit */
  157. #define DMA_PERIPHERAL_WIDTH_16BIT CHCTL_PWIDTH(1U) /*!< transfer data size of peripheral is 16-bit */
  158. #define DMA_PERIPHERAL_WIDTH_32BIT CHCTL_PWIDTH(2U) /*!< transfer data size of peripheral is 32-bit */
  159. /* transfer data size of memory */
  160. #define CHCTL_MWIDTH(regval) (BITS(10,11) & ((regval) << 10)) /*!< transfer data size of memory */
  161. #define DMA_MEMORY_WIDTH_8BIT CHCTL_MWIDTH(0U) /*!< transfer data size of memory is 8-bit */
  162. #define DMA_MEMORY_WIDTH_16BIT CHCTL_MWIDTH(1U) /*!< transfer data size of memory is 16-bit */
  163. #define DMA_MEMORY_WIDTH_32BIT CHCTL_MWIDTH(2U) /*!< transfer data size of memory is 32-bit */
  164. /* channel priority level */
  165. #define CHCTL_PRIO(regval) (BITS(12,13) & ((regval) << 12)) /*!< DMA channel priority level */
  166. #define DMA_PRIORITY_LOW CHCTL_PRIO(0U) /*!< low priority */
  167. #define DMA_PRIORITY_MEDIUM CHCTL_PRIO(1U) /*!< medium priority */
  168. #define DMA_PRIORITY_HIGH CHCTL_PRIO(2U) /*!< high priority */
  169. #define DMA_PRIORITY_ULTRA_HIGH CHCTL_PRIO(3U) /*!< ultra high priority */
  170. /* DMA_CHxCNT register */
  171. /* transfer counter */
  172. #define DMA_CHANNEL_CNT_MASK DMA_CHXCNT_CNT
  173. /* function declarations */
  174. /* deinitialize DMA a channel registers */
  175. void dma_deinit(dma_channel_enum channelx);
  176. /* initialize the parameters of DMA struct with the default values */
  177. void dma_struct_para_init(dma_parameter_struct* init_struct);
  178. /* initialize DMA channel */
  179. void dma_init(dma_channel_enum channelx, dma_parameter_struct* init_struct);
  180. /* enable DMA circulation mode */
  181. void dma_circulation_enable(dma_channel_enum channelx);
  182. /* disable DMA circulation mode */
  183. void dma_circulation_disable(dma_channel_enum channelx);
  184. /* enable memory to memory mode */
  185. void dma_memory_to_memory_enable(dma_channel_enum channelx);
  186. /* disable memory to memory mode */
  187. void dma_memory_to_memory_disable(dma_channel_enum channelx);
  188. /* enable DMA channel */
  189. void dma_channel_enable(dma_channel_enum channelx);
  190. /* disable DMA channel */
  191. void dma_channel_disable(dma_channel_enum channelx);
  192. /* set DMA peripheral base address */
  193. void dma_periph_address_config(dma_channel_enum channelx, uint32_t address);
  194. /* set DMA memory base address */
  195. void dma_memory_address_config(dma_channel_enum channelx, uint32_t address);
  196. /* set the number of remaining data to be transferred by the DMA */
  197. void dma_transfer_number_config(dma_channel_enum channelx, uint32_t number);
  198. /* get the number of remaining data to be transferred by the DMA */
  199. uint32_t dma_transfer_number_get(dma_channel_enum channelx);
  200. /* configure priority level of DMA channel */
  201. void dma_priority_config(dma_channel_enum channelx, uint32_t priority);
  202. /* configure transfer data size of memory */
  203. void dma_memory_width_config (dma_channel_enum channelx, uint32_t mwidth);
  204. /* configure transfer data size of peripheral */
  205. void dma_periph_width_config (dma_channel_enum channelx, uint32_t pwidth);
  206. /* enable next address increasement algorithm of memory */
  207. void dma_memory_increase_enable(dma_channel_enum channelx);
  208. /* disable next address increasement algorithm of memory */
  209. void dma_memory_increase_disable(dma_channel_enum channelx);
  210. /* enable next address increasement algorithm of peripheral */
  211. void dma_periph_increase_enable(dma_channel_enum channelx);
  212. /* disable next address increasement algorithm of peripheral */
  213. void dma_periph_increase_disable(dma_channel_enum channelx);
  214. /* configure the direction of data transfer on the channel */
  215. void dma_transfer_direction_config(dma_channel_enum channelx, uint8_t direction);
  216. /* check DMA flag is set or not */
  217. FlagStatus dma_flag_get(dma_channel_enum channelx, uint32_t flag);
  218. /* clear DMA a channel flag */
  219. void dma_flag_clear(dma_channel_enum channelx, uint32_t flag);
  220. /* check DMA flag and interrupt enable bit is set or not */
  221. FlagStatus dma_interrupt_flag_get(dma_channel_enum channelx, uint32_t flag);
  222. /* clear DMA a channel flag */
  223. void dma_interrupt_flag_clear(dma_channel_enum channelx, uint32_t flag);
  224. /* enable DMA interrupt */
  225. void dma_interrupt_enable(dma_channel_enum channelx, uint32_t source);
  226. /* disable DMA interrupt */
  227. void dma_interrupt_disable(dma_channel_enum channelx, uint32_t source);
  228. #endif /* GD32E23X_DMA_H */