gd32f30x_dma.h 19 KB

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