ald_dma.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_dma.h
  5. * @brief DMA module Library.
  6. *
  7. * @version V1.0
  8. * @date 09 Nov 2017
  9. * @author AE Team
  10. * @note
  11. *
  12. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  13. *
  14. *********************************************************************************
  15. */
  16. #ifndef __ALD_DMA_H__
  17. #define __ALD_DMA_H__
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "utils.h"
  22. /** @addtogroup ES32FXXX_ALD
  23. * @{
  24. */
  25. /** @addtogroup DMA
  26. * @{
  27. */
  28. /**
  29. * @defgroup DMA_Public_Macros DMA Public Macros
  30. * @{
  31. */
  32. #define DMA_CH_COUNT 6
  33. #define DMA_ERR 31
  34. /**
  35. * @}
  36. */
  37. /**
  38. * @defgroup DMA_Public_Types DMA Public Types
  39. * @{
  40. */
  41. /**
  42. * @brief Input source to DMA channel
  43. */
  44. typedef enum {
  45. DMA_MSEL_NONE = 0x0, /**< NONE */
  46. DMA_MSEL_GPIO = 0x1, /**< GPIO */
  47. DMA_MSEL_CRYPT = 0x2, /**< CRYPT */
  48. DMA_MSEL_ACMP = 0x3, /**< ACMP */
  49. DMA_MSEL_DAC0 = 0x4, /**< DAC0 */
  50. DMA_MSEL_ADC0 = 0x6, /**< ADC0 */
  51. DMA_MSEL_CRC = 0x7, /**< CRC */
  52. DMA_MSEL_UART0 = 0x8, /**< UART0 */
  53. DMA_MSEL_UART1 = 0x9, /**< UART1 */
  54. DMA_MSEL_UART2 = 0xA, /**< UART2 */
  55. DMA_MSEL_UART3 = 0xB, /**< UART3 */
  56. DMA_MSEL_USART0 = 0xC, /**< USART0 */
  57. DMA_MSEL_USART1 = 0xD, /**< USART1 */
  58. DMA_MSEL_SPI0 = 0xE, /**< SPI0 */
  59. DMA_MSEL_SPI1 = 0xF, /**< SPI1 */
  60. DMA_MSEL_I2C0 = 0x10, /**< I2C0 */
  61. DMA_MSEL_I2C1 = 0x11, /**< I2C1 */
  62. DMA_MSEL_TIMER0 = 0x12, /**< TIMER0 */
  63. DMA_MSEL_TIMER1 = 0x13, /**< TIMER1 */
  64. DMA_MSEL_TIMER2 = 0x14, /**< TIMER2 */
  65. DMA_MSEL_TIMER3 = 0x15, /**< TIMER3 */
  66. DMA_MSEL_RTC = 0x16, /**< RTC */
  67. DMA_MSEL_LPTIM0 = 0x17, /**< LPTIM0 */
  68. DMA_MSEL_LPUART0 = 0x18, /**< LPUART0 */
  69. DMA_MSEL_DMA = 0x19, /**< DMA */
  70. DMA_MSEL_SPI2 = 0x1A, /**< SPI2 */
  71. DMA_MSEL_TIMER4 = 0x1B, /**< TIMER4 */
  72. DMA_MSEL_TIMER5 = 0x1C, /**< TIMER5 */
  73. DMA_MSEL_TIMER6 = 0x1D, /**< TIMER6 */
  74. DMA_MSEL_TIMER7 = 0x1E, /**< TIMER7 */
  75. DMA_MSEL_ADC1 = 0x1F, /**< ADC1 */
  76. DMA_MSEL_PIS = 0x20, /**< PIS */
  77. DMA_MSEL_TRNG = 0x21, /**< TRNG */
  78. } dma_msel_t;
  79. /**
  80. * @brief Input signal to DMA channel
  81. */
  82. typedef enum {
  83. DMA_MSIGSEL_NONE = 0x0, /**< NONE */
  84. DMA_MSIGSEL_EXTI_0 = 0x0, /**< External interrupt 0 */
  85. DMA_MSIGSEL_EXTI_1 = 0x1, /**< External interrupt 1 */
  86. DMA_MSIGSEL_EXTI_2 = 0x2, /**< External interrupt 2 */
  87. DMA_MSIGSEL_EXTI_3 = 0x3, /**< External interrupt 3 */
  88. DMA_MSIGSEL_EXTI_4 = 0x4, /**< External interrupt 4 */
  89. DMA_MSIGSEL_EXTI_5 = 0x5, /**< External interrupt 5 */
  90. DMA_MSIGSEL_EXTI_6 = 0x6, /**< External interrupt 6 */
  91. DMA_MSIGSEL_EXTI_7 = 0x7, /**< External interrupt 7 */
  92. DMA_MSIGSEL_EXTI_8 = 0x8, /**< External interrupt 8 */
  93. DMA_MSIGSEL_EXTI_9 = 0x9, /**< External interrupt 9 */
  94. DMA_MSIGSEL_EXTI_10 = 0xA, /**< External interrupt 10 */
  95. DMA_MSIGSEL_EXTI_11 = 0xB, /**< External interrupt 11 */
  96. DMA_MSIGSEL_EXTI_12 = 0xC, /**< External interrupt 12 */
  97. DMA_MSIGSEL_EXTI_13 = 0xD, /**< External interrupt 13 */
  98. DMA_MSIGSEL_EXTI_14 = 0xE, /**< External interrupt 14 */
  99. DMA_MSIGSEL_EXTI_15 = 0xF, /**< External interrupt 15 */
  100. DMA_MSIGSEL_CRYPT_WRITE = 0x0, /**< CRYPT write mode */
  101. DMA_MSIGSEL_CRYPT_READ = 0x1, /**< CRYPT read mode */
  102. DMA_MSIGSEL_CALC_WRITE = 0x0, /**< CALC write mode */
  103. DMA_MSIGSEL_CALC_READ = 0x1, /**< CALC read mode */
  104. DMA_MSIGSEL_DAC0_CH0 = 0x0, /**< DAC0 channel 0 complete */
  105. DMA_MSIGSEL_DAC0_CH1 = 0x1, /**< DAC0 channel 1 complete */
  106. DMA_MSIGSEL_ADC = 0x0, /**< ADC mode */
  107. DMA_MSIGSEL_UART_TXEMPTY = 0x0, /**< UART transmit */
  108. DMA_MSIGSEL_UART_RNR = 0x1, /**< UART receive */
  109. DMA_MSIGSEL_USART_RNR = 0x0, /**< USART reveive */
  110. DMA_MSIGSEL_USART_TXEMPTY = 0x1, /**< USART transmit */
  111. DMA_MSIGSEL_SPI_RNR = 0x0, /**< SPI receive */
  112. DMA_MSIGSEL_SPI_TXEMPTY = 0x1, /**< SPI transmit */
  113. DMA_MSIGSEL_I2C_RNR = 0x0, /**< I2C receive */
  114. DMA_MSIGSEL_I2C_TXEMPTY = 0x1, /**< I2C transmit */
  115. DMA_MSIGSEL_TIMER_CH1 = 0x0, /**< TIM channal 1 */
  116. DMA_MSIGSEL_TIMER_CH2 = 0x1, /**< TIM channal 2 */
  117. DMA_MSIGSEL_TIMER_CH3 = 0x2, /**< TIM channal 3 */
  118. DMA_MSIGSEL_TIMER_CH4 = 0x3, /**< TIM channal 4 */
  119. DMA_MSIGSEL_TIMER_TRI = 0x4, /**< TIM trigger */
  120. DMA_MSIGSEL_TIMER_COMP = 0x5, /**< TIM compare */
  121. DMA_MSIGSEL_TIMER_UPDATE = 0x6, /**< TIM update */
  122. DMA_MSIGSEL_LPUART_RNR = 0x0, /**< LPUART receive */
  123. DMA_MSIGSEL_LPUART_TXEMPTY = 0x1, /**< LPUART transmit */
  124. DMA_MSIGSEL_PIS_CH0 = 0x0, /**< PIS channal 0 */
  125. DMA_MSIGSEL_PIS_CH1 = 0x1, /**< PIS channal 1 */
  126. DMA_MSIGSEL_PIS_CH2 = 0x2, /**< PIS channal 2 */
  127. DMA_MSIGSEL_PIS_CH3 = 0x3, /**< PIS channal 3 */
  128. DMA_MSIGSEL_PIS_CH4 = 0x4, /**< PIS channal 4 */
  129. DMA_MSIGSEL_PIS_CH5 = 0x5, /**< PIS channal 5 */
  130. DMA_MSIGSEL_PIS_CH6 = 0x6, /**< PIS channal 6 */
  131. DMA_MSIGSEL_PIS_CH7 = 0x7, /**< PIS channal 7 */
  132. DMA_MSIGSEL_PIS_CH8 = 0x8, /**< PIS channal 8 */
  133. DMA_MSIGSEL_PIS_CH9 = 0x9, /**< PIS channal 9 */
  134. DMA_MSIGSEL_PIS_CH10 = 0xA, /**< PIS channal 10 */
  135. DMA_MSIGSEL_PIS_CH11 = 0xB, /**< PIS channal 11 */
  136. DMA_MSIGSEL_PIS_CH12 = 0xC, /**< PIS channal 12 */
  137. DMA_MSIGSEL_PIS_CH13 = 0xD, /**< PIS channal 13 */
  138. DMA_MSIGSEL_PIS_CH14 = 0xE, /**< PIS channal 14 */
  139. DMA_MSIGSEL_PIS_CH15 = 0xF, /**< PIS channal 15 */
  140. } dma_msigsel_t;
  141. /**
  142. * @brief DMA Descriptor control type
  143. */
  144. typedef union {
  145. struct {
  146. uint32_t cycle_ctrl :3; /**< DMA operating mode @ref dma_cycle_ctrl_t */
  147. uint32_t next_useburst :1; /**< Uses the alternate data structure when complete a DMA cycle */
  148. uint32_t n_minus_1 :10; /**< Represent the total number of DMA transfers that DMA cycle contains. */
  149. uint32_t R_power :4; /**< Control how many DMA transfers can occur before re-arbitrates. @ref dma_arbiter_config_t */
  150. uint32_t src_prot_ctrl :3; /**< Control the state of HPROT when reads the source data. */
  151. uint32_t dst_prot_ctrl :3; /**< Control the state of HPROT when writes the destination data */
  152. uint32_t src_size :2; /**< Source data size @ref dma_data_size_t */
  153. uint32_t src_inc :2; /**< Control the source address increment. @ref dma_data_inc_t */
  154. uint32_t dst_size :2; /**< Destination data size. @ref dma_data_size_t */
  155. uint32_t dst_inc :2; /**< Destination address increment. @ref dma_data_inc_t */
  156. };
  157. uint32_t word;
  158. } dma_ctrl_t;
  159. /**
  160. * @brief Channel control data structure
  161. */
  162. typedef struct {
  163. void *src; /**< Source data end pointer */
  164. void *dst; /**< Destination data end pointer */
  165. dma_ctrl_t ctrl; /**< Control data configuration @ref dma_ctrl_t */
  166. uint32_t use; /**< Reserve for user */
  167. } dma_descriptor_t;
  168. /**
  169. * @brief data increment
  170. */
  171. typedef enum {
  172. DMA_DATA_INC_BYTE = 0x0, /**< Address increment by byte */
  173. DMA_DATA_INC_HALFWORD = 0x1, /**< Address increment by halfword */
  174. DMA_DATA_INC_WORD = 0x2, /**< Address increment by word */
  175. DMA_DATA_INC_NONE = 0x3, /**< No increment */
  176. } dma_data_inc_t;
  177. /**
  178. * @brief Data size
  179. */
  180. typedef enum {
  181. DMA_DATA_SIZE_BYTE = 0x0, /**< Byte */
  182. DMA_DATA_SIZE_HALFWORD = 0x1, /**< Halfword */
  183. DMA_DATA_SIZE_WORD = 0x2, /**< Word */
  184. } dma_data_size_t;
  185. /**
  186. * @brief The operating mode of the DMA cycle
  187. */
  188. typedef enum {
  189. DMA_CYCLE_CTRL_NONE = 0x0, /**< Stop */
  190. DMA_CYCLE_CTRL_BASIC = 0x1, /**< Basic */
  191. DMA_CYCLE_CTRL_AUTO = 0x2, /**< Auto-request */
  192. DMA_CYCLE_CTRL_PINGPONG = 0x3, /**< Ping-pong */
  193. DMA_CYCLE_CTRL_MEM_SCATTER_GATHER = 0x4, /**< Memory scatter/gather */
  194. DMA_CYCLE_CTRL_PER_SCATTER_GATHER = 0x6, /**< Peripheral scatter/gather */
  195. } dma_cycle_ctrl_t;
  196. /**
  197. * @brief Control how many DMA transfers can occur
  198. * before the controller re-arbitrates
  199. */
  200. typedef enum {
  201. DMA_R_POWER_1 = 0x0, /**< Arbitrates after each DMA transfer */
  202. DMA_R_POWER_2 = 0x1, /**< Arbitrates after 2 DMA transfer */
  203. DMA_R_POWER_4 = 0x2, /**< Arbitrates after 4 DMA transfer */
  204. DMA_R_POWER_8 = 0x3, /**< Arbitrates after 8 DMA transfer */
  205. DMA_R_POWER_16 = 0x4, /**< Arbitrates after 16 DMA transfer */
  206. DMA_R_POWER_32 = 0x5, /**< Arbitrates after 32 DMA transfer */
  207. DMA_R_POWER_64 = 0x6, /**< Arbitrates after 64 DMA transfer */
  208. DMA_R_POWER_128 = 0x7, /**< Arbitrates after 128 DMA transfer */
  209. DMA_R_POWER_256 = 0x8, /**< Arbitrates after 256 DMA transfer */
  210. DMA_R_POWER_512 = 0x9, /**< Arbitrates after 512 DMA transfer */
  211. DMA_R_POWER_1024 = 0xA, /**< Arbitrates after 1024 DMA transfer */
  212. } dma_arbiter_config_t;
  213. /**
  214. * @brief Callback function pointer and param
  215. */
  216. typedef struct {
  217. void (*cplt_cbk)(void *arg); /**< DMA transfers complete callback */
  218. void (*err_cbk)(void* arg); /**< DMA occurs error callback */
  219. void *cplt_arg; /**< The parameter of cplt_cbk() */
  220. void *err_arg; /**< The parameter of err_cbk() */
  221. } dma_call_back_t;
  222. /**
  223. * @brief DMA channal configure structure
  224. */
  225. typedef struct {
  226. void *src; /**< Source data begin pointer */
  227. void *dst; /**< Destination data begin pointer */
  228. uint16_t size; /**< The total number of DMA transfers that DMA cycle contains */
  229. dma_data_size_t data_width; /**< Data width, @ref dma_data_size_t */
  230. dma_data_inc_t src_inc; /**< Source increment type. @ref dma_data_inc_t */
  231. dma_data_inc_t dst_inc; /**< Destination increment type. @ref dma_data_inc_t */
  232. dma_arbiter_config_t R_power; /**< Control how many DMA transfers can occur before re-arbitrates. @ref dma_arbiter_config_t */
  233. type_func_t primary; /**< Use primary descriptor or alternate descriptor */
  234. type_func_t burst; /**< Uses the alternate data structure when complete a DMA cycle */
  235. type_func_t high_prio; /**< High priority or default priority */
  236. type_func_t iterrupt; /**< Enable/disable interrupt */
  237. dma_msel_t msel; /**< Input source to DMA channel @ref dma_msel_t */
  238. dma_msigsel_t msigsel; /**< Input signal to DMA channel @ref dma_msigsel_t */
  239. uint8_t channel; /**< Channel index */
  240. } dma_config_t;
  241. /**
  242. * @brief DMA handle structure definition
  243. */
  244. typedef struct {
  245. DMA_TypeDef *perh; /**< DMA registers base address */
  246. dma_config_t config; /**< Channel configure structure. @ref dma_config_t */
  247. void (*cplt_cbk)(void *arg); /**< DMA transfers complete callback */
  248. void (*err_cbk)(void *arg); /**< DMA bus occurs error callback */
  249. void *cplt_arg; /**< The parameter of cplt_cbk() */
  250. void *err_arg; /**< The parameter of err_cbk() */
  251. } dma_handle_t;
  252. /**
  253. * @}
  254. */
  255. /**
  256. * @defgroup DMA_Private_Macros DMA Private Macros
  257. * @{
  258. */
  259. #define IS_DMA_MSEL_TYPE(x) ((x) <= DMA_MSEL_TRNG)
  260. #define IS_DMA_MSIGSEL_TYPE(x) ((x) <= 0xF)
  261. #define IS_DMA_DATAINC_TYPE(x) (((x) == DMA_DATA_INC_BYTE) || \
  262. ((x) == DMA_DATA_INC_HALFWORD) || \
  263. ((x) == DMA_DATA_INC_WORD) || \
  264. ((x) == DMA_DATA_INC_NONE))
  265. #define IS_DMA_DATASIZE_TYPE(x) (((x) == DMA_DATA_SIZE_BYTE) || \
  266. ((x) == DMA_DATA_SIZE_HALFWORD) || \
  267. ((x) == DMA_DATA_SIZE_WORD))
  268. #define IS_CYCLECTRL_TYPE(x) (((x) == DMA_CYCLE_CTRL_NONE) || \
  269. ((x) == DMA_CYCLE_CTRL_BASIC) || \
  270. ((x) == DMA_CYCLE_CTRL_AUTO) || \
  271. ((x) == DMA_CYCLE_CTRL_PINGPONG) || \
  272. ((x) == DMA_CYCLE_CTRL_MEM_SCATTER_GATHER) || \
  273. ((x) == DMA_CYCLE_CTRL_PER_SCATTER_GATHER))
  274. #define IS_DMA_ARBITERCONFIG_TYPE(x) (((x) == DMA_R_POWER_1) || \
  275. ((x) == DMA_R_POWER_2) || \
  276. ((x) == DMA_R_POWER_4) || \
  277. ((x) == DMA_R_POWER_8) || \
  278. ((x) == DMA_R_POWER_16) || \
  279. ((x) == DMA_R_POWER_32) || \
  280. ((x) == DMA_R_POWER_64) || \
  281. ((x) == DMA_R_POWER_128) || \
  282. ((x) == DMA_R_POWER_256) || \
  283. ((x) == DMA_R_POWER_512) || \
  284. ((x) == DMA_R_POWER_1024))
  285. #define IS_DMA(x) ((x) == DMA0)
  286. #define IS_DMA_CHANNEL(x) ((x) <= 5)
  287. #define IS_DMA_DATA_SIZE(x) ((x) <= 1024)
  288. #define IS_DMA_IT_TYPE(x) (((x) <= 5) || ((x) == 31))
  289. /**
  290. * @}
  291. */
  292. /**
  293. * @addtogroup DMA_Public_Functions
  294. * @{
  295. */
  296. /** @addtogroup DMA_Public_Functions_Group1
  297. * @{
  298. */
  299. /* Initialization functions */
  300. extern void dma_reset(DMA_TypeDef *DMAx);
  301. extern void dma_init(DMA_TypeDef *DMAx);
  302. extern void dma_config_struct(dma_config_t *p);
  303. /**
  304. * @}
  305. */
  306. /** @addtogroup DMA_Public_Functions_Group2
  307. * @{
  308. */
  309. /* Configure DMA channel functions */
  310. extern void dma_config_auto(dma_handle_t *hperh);
  311. extern void dma_restart_auto(dma_handle_t *hperh, void *src, void *dst, uint16_t size);
  312. extern void dma_config_auto_easy(DMA_TypeDef *DMAx, void *src, void *dst,
  313. uint16_t size, uint8_t channel, void (*cbk)(void *arg));
  314. extern void dma_config_basic(dma_handle_t *hperh);
  315. extern void dma_restart_basic(dma_handle_t *hperh, void *src, void *dst, uint16_t size);
  316. extern void dma_config_basic_easy(DMA_TypeDef *DMAx, void *src, void *dst, uint16_t size, dma_msel_t msel,
  317. dma_msigsel_t msigsel, uint8_t channel, void (*cbk)(void *arg));
  318. /**
  319. * @}
  320. */
  321. /** @addtogroup DMA_Public_Functions_Group3
  322. * @{
  323. */
  324. /* DMA control functions */
  325. extern void dma_channel_config(DMA_TypeDef *DMAx, uint8_t channel, type_func_t state);
  326. extern void dma_interrupt_config(DMA_TypeDef *DMAx, uint8_t channel, type_func_t state);
  327. extern it_status_t dma_get_it_status(DMA_TypeDef *DMAx, uint8_t channel);
  328. extern flag_status_t dma_get_flag_status(DMA_TypeDef *DMAx, uint8_t channel);
  329. extern void dma_clear_flag_status(DMA_TypeDef *DMAx, uint8_t channel);
  330. void dma0_irq_cbk(void);
  331. /**
  332. * @}
  333. */
  334. /**
  335. * @}
  336. */
  337. /**
  338. * @}
  339. */
  340. /**
  341. * @}
  342. */
  343. #ifdef __cplusplus
  344. }
  345. #endif
  346. #endif /*__ALD_DMA_H__ */