at32f403a_407_dma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /**
  2. **************************************************************************
  3. * @file at32f403a_407_dma.c
  4. * @brief contains all the functions for the dma firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. #include "at32f403a_407_conf.h"
  25. /** @addtogroup AT32F403A_407_periph_driver
  26. * @{
  27. */
  28. /** @defgroup DMA
  29. * @brief DMA driver modules
  30. * @{
  31. */
  32. #ifdef DMA_MODULE_ENABLED
  33. /** @defgroup DMA_private_functions
  34. * @{
  35. */
  36. /**
  37. * @brief reset the dmax channely registers.
  38. * @param dmax_channely:
  39. * this parameter can be one of the following values:
  40. * - DMA1_CHANNEL1
  41. * - DMA1_CHANNEL2
  42. * - DMA1_CHANNEL3
  43. * - DMA1_CHANNEL4
  44. * - DMA1_CHANNEL5
  45. * - DMA1_CHANNEL6
  46. * - DMA1_CHANNEL7
  47. * - DMA2_CHANNEL1
  48. * - DMA2_CHANNEL2
  49. * - DMA2_CHANNEL3
  50. * - DMA2_CHANNEL4
  51. * - DMA2_CHANNEL5
  52. * - DMA2_CHANNEL6
  53. * - DMA2_CHANNEL7
  54. * @retval none
  55. */
  56. void dma_reset(dma_channel_type* dmax_channely)
  57. {
  58. uint32_t temp = 0;
  59. dmax_channely->ctrl_bit.chen = FALSE;
  60. dmax_channely->ctrl = 0;
  61. dmax_channely->dtcnt = 0;
  62. dmax_channely->paddr = 0;
  63. dmax_channely->maddr = 0;
  64. temp = (uint32_t)dmax_channely;
  65. if((temp & 0x4ff) < 0x408)
  66. {
  67. /* dma1 channel */
  68. DMA1->clr |= (uint32_t)(0x0f << ((((temp & 0xff) - 0x08) / 0x14) * 4));
  69. }
  70. else if((temp & 0x4ff) < 0x488)
  71. {
  72. /* dma2 channel */
  73. DMA2->clr |= (uint32_t)(0x0f << ((((temp & 0xff) - 0x08) / 0x14) * 4));
  74. }
  75. }
  76. /**
  77. * @brief set the number of data to be transferred
  78. * @param dmax_channely:
  79. * this parameter can be one of the following values:
  80. * - DMA1_CHANNEL1
  81. * - DMA1_CHANNEL2
  82. * - DMA1_CHANNEL3
  83. * - DMA1_CHANNEL4
  84. * - DMA1_CHANNEL5
  85. * - DMA1_CHANNEL6
  86. * - DMA1_CHANNEL7
  87. * - DMA2_CHANNEL1
  88. * - DMA2_CHANNEL2
  89. * - DMA2_CHANNEL3
  90. * - DMA2_CHANNEL4
  91. * - DMA2_CHANNEL5
  92. * - DMA2_CHANNEL6
  93. * - DMA2_CHANNEL7
  94. * @param data_number: the number of data to be transferred(0x0000~0xFFFF)
  95. * transfer.
  96. * @retval none.
  97. */
  98. void dma_data_number_set(dma_channel_type* dmax_channely, uint16_t data_number)
  99. {
  100. dmax_channely->dtcnt = data_number;
  101. }
  102. /**
  103. * @brief get number of data from dtcnt register
  104. * @param dmax_channely:
  105. * this parameter can be one of the following values:
  106. * - DMA1_CHANNEL1
  107. * - DMA1_CHANNEL2
  108. * - DMA1_CHANNEL3
  109. * - DMA1_CHANNEL4
  110. * - DMA1_CHANNEL5
  111. * - DMA1_CHANNEL6
  112. * - DMA1_CHANNEL7
  113. * - DMA2_CHANNEL1
  114. * - DMA2_CHANNEL2
  115. * - DMA2_CHANNEL3
  116. * - DMA2_CHANNEL4
  117. * - DMA2_CHANNEL5
  118. * - DMA2_CHANNEL6
  119. * - DMA2_CHANNEL7
  120. * @retval the number of data.
  121. */
  122. uint16_t dma_data_number_get(dma_channel_type* dmax_channely)
  123. {
  124. return (uint16_t)dmax_channely->dtcnt;
  125. }
  126. /**
  127. * @brief enable or disable dma interrupt
  128. * @param dmax_channely:
  129. * this parameter can be one of the following values:
  130. * - DMA1_CHANNEL1
  131. * - DMA1_CHANNEL2
  132. * - DMA1_CHANNEL3
  133. * - DMA1_CHANNEL4
  134. * - DMA1_CHANNEL5
  135. * - DMA1_CHANNEL6
  136. * - DMA1_CHANNEL7
  137. * - DMA2_CHANNEL1
  138. * - DMA2_CHANNEL2
  139. * - DMA2_CHANNEL3
  140. * - DMA2_CHANNEL4
  141. * - DMA2_CHANNEL5
  142. * - DMA2_CHANNEL6
  143. * - DMA2_CHANNEL7
  144. * @param dma_int:
  145. * this parameter can be any combination of the following values:
  146. * - DMA_FDT_INT
  147. * - DMA_HDT_INT
  148. * - DMA_DTERR_INT
  149. * @param new_state (TRUE or FALSE)
  150. * @retval none
  151. */
  152. void dma_interrupt_enable(dma_channel_type* dmax_channely, uint32_t dma_int, confirm_state new_state)
  153. {
  154. if (new_state != FALSE)
  155. {
  156. dmax_channely->ctrl |= dma_int;
  157. }
  158. else
  159. {
  160. dmax_channely->ctrl &= ~dma_int;
  161. }
  162. }
  163. /**
  164. * @brief enable or disable dma channely
  165. * @param dmax_channely:
  166. * this parameter can be one of the following values:
  167. * - DMA1_CHANNEL1
  168. * - DMA1_CHANNEL2
  169. * - DMA1_CHANNEL3
  170. * - DMA1_CHANNEL4
  171. * - DMA1_CHANNEL5
  172. * - DMA1_CHANNEL6
  173. * - DMA1_CHANNEL7
  174. * - DMA2_CHANNEL1
  175. * - DMA2_CHANNEL2
  176. * - DMA2_CHANNEL3
  177. * - DMA2_CHANNEL4
  178. * - DMA2_CHANNEL5
  179. * - DMA2_CHANNEL6
  180. * - DMA2_CHANNEL7
  181. * @param new_state (TRUE or FALSE)
  182. * @retval None
  183. */
  184. void dma_channel_enable(dma_channel_type* dmax_channely, confirm_state new_state)
  185. {
  186. dmax_channely->ctrl_bit.chen = new_state;
  187. }
  188. /**
  189. * @brief initialize the dma_x flexible function according to the specified parameters.
  190. * @param dma_x:
  191. * this parameter can be one of the following values:
  192. * - DMA1
  193. * - DMA2
  194. * @param flex_channelx:
  195. * this parameter can be one of the following values:
  196. * - FLEX_CHANNEL1
  197. * - FLEX_CHANNEL2
  198. * - FLEX_CHANNEL3
  199. * - FLEX_CHANNEL4
  200. * - FLEX_CHANNEL5
  201. * - FLEX_CHANNEL6
  202. * - FLEX_CHANNEL7
  203. * @param flexible_request: every peripheral have specified hardware_id.
  204. * this parameter can be one of the following values:
  205. * - DMA_FLEXIBLE_ADC1 - DMA_FLEXIBLE_ADC3 - DMA_FLEXIBLE_DAC1 - DMA_FLEXIBLE_DAC2
  206. * - DMA_FLEXIBLE_SPI1_RX - DMA_FLEXIBLE_SPI1_TX - DMA_FLEXIBLE_SPI2_RX - DMA_FLEXIBLE_SPI2_TX
  207. * - DMA_FLEXIBLE_SPI3_RX - DMA_FLEXIBLE_SPI3_TX - DMA_FLEXIBLE_SPI4_RX - DMA_FLEXIBLE_SPI4_TX
  208. * - DMA_FLEXIBLE_I2S2EXT_RX - DMA_FLEXIBLE_I2S2EXT_TX - DMA_FLEXIBLE_I2S3EXT_RX - DMA_FLEXIBLE_I2S3EXT_TX
  209. * - DMA_FLEXIBLE_UART1_RX - DMA_FLEXIBLE_UART1_TX - DMA_FLEXIBLE_UART2_RX - DMA_FLEXIBLE_UART2_TX
  210. * - DMA_FLEXIBLE_UART3_RX - DMA_FLEXIBLE_UART3_TX - DMA_FLEXIBLE_UART4_RX - DMA_FLEXIBLE_UART4_TX
  211. * - DMA_FLEXIBLE_UART5_RX - DMA_FLEXIBLE_UART5_TX - DMA_FLEXIBLE_UART6_RX - DMA_FLEXIBLE_UART6_TX
  212. * - DMA_FLEXIBLE_UART7_RX - DMA_FLEXIBLE_UART7_TX - DMA_FLEXIBLE_UART8_RX - DMA_FLEXIBLE_UART8_TX
  213. * - DMA_FLEXIBLE_I2C1_RX - DMA_FLEXIBLE_I2C1_TX - DMA_FLEXIBLE_I2C2_RX - DMA_FLEXIBLE_I2C2_TX
  214. * - DMA_FLEXIBLE_I2C3_RX - DMA_FLEXIBLE_I2C3_TX - DMA_FLEXIBLE_SDIO1 - DMA_FLEXIBLE_SDIO2
  215. * - DMA_FLEXIBLE_TMR1_TRIG - DMA_FLEXIBLE_TMR1_HALL - DMA_FLEXIBLE_TMR1_OVERFLOW- DMA_FLEXIBLE_TMR1_CH1
  216. * - DMA_FLEXIBLE_TMR1_CH2 - DMA_FLEXIBLE_TMR1_CH3 - DMA_FLEXIBLE_TMR1_CH4 - DMA_FLEXIBLE_TMR2_TRIG
  217. * - DMA_FLEXIBLE_TMR2_OVERFLOW- DMA_FLEXIBLE_TMR2_CH1 - DMA_FLEXIBLE_TMR2_CH2 - DMA_FLEXIBLE_TMR2_CH3
  218. * - DMA_FLEXIBLE_TMR2_CH4 - DMA_FLEXIBLE_TMR3_TRIG - DMA_FLEXIBLE_TMR3_OVERFLOW- DMA_FLEXIBLE_TMR3_CH1
  219. * - DMA_FLEXIBLE_TMR3_CH2 - DMA_FLEXIBLE_TMR3_CH3 - DMA_FLEXIBLE_TMR3_CH4 - DMA_FLEXIBLE_TMR4_TRIG
  220. * - DMA_FLEXIBLE_TMR4_OVERFLOW- DMA_FLEXIBLE_TMR4_CH1 - DMA_FLEXIBLE_TMR4_CH2 - DMA_FLEXIBLE_TMR4_CH3
  221. * - DMA_FLEXIBLE_TMR4_CH4 - DMA_FLEXIBLE_TMR5_TRIG - DMA_FLEXIBLE_TMR5_OVERFLOW- DMA_FLEXIBLE_TMR5_CH1
  222. * - DMA_FLEXIBLE_TMR5_CH2 - DMA_FLEXIBLE_TMR5_CH3 - DMA_FLEXIBLE_TMR5_CH4 - DMA_FLEXIBLE_TMR6_OVERFLOW
  223. * - DMA_FLEXIBLE_TMR7_OVERFLOW- DMA_FLEXIBLE_TMR8_TRIG - DMA_FLEXIBLE_TMR8_HALL - DMA_FLEXIBLE_TMR8_OVERFLOW
  224. * - DMA_FLEXIBLE_TMR8_CH1 - DMA_FLEXIBLE_TMR8_CH2 - DMA_FLEXIBLE_TMR8_CH3 - DMA_FLEXIBLE_TMR8_CH4
  225. * @retval none
  226. */
  227. void dma_flexible_config(dma_type* dma_x, uint8_t flex_channelx, dma_flexible_request_type flexible_request)
  228. {
  229. if(dma_x->src_sel1_bit.dma_flex_en == RESET)
  230. {
  231. dma_x->src_sel1_bit.dma_flex_en = TRUE;
  232. }
  233. if(flex_channelx == FLEX_CHANNEL1)
  234. {
  235. dma_x->src_sel0_bit.ch1_src = flexible_request;
  236. }
  237. else if(flex_channelx == FLEX_CHANNEL2)
  238. {
  239. dma_x->src_sel0_bit.ch2_src = flexible_request;
  240. }
  241. else if(flex_channelx == FLEX_CHANNEL3)
  242. {
  243. dma_x->src_sel0_bit.ch3_src = flexible_request;
  244. }
  245. else if(flex_channelx == FLEX_CHANNEL4)
  246. {
  247. dma_x->src_sel0_bit.ch4_src = flexible_request;
  248. }
  249. else if(flex_channelx == FLEX_CHANNEL5)
  250. {
  251. dma_x->src_sel1_bit.ch5_src = flexible_request;
  252. }
  253. else if(flex_channelx == FLEX_CHANNEL6)
  254. {
  255. dma_x->src_sel1_bit.ch6_src = flexible_request;
  256. }
  257. else
  258. {
  259. if(flex_channelx == FLEX_CHANNEL7)
  260. {
  261. dma_x->src_sel1_bit.ch7_src = flexible_request;
  262. }
  263. }
  264. }
  265. /**
  266. * @brief get dma flag
  267. * @param dmax_flag
  268. * this parameter can be one of the following values:
  269. * - DMA1_GL1_FLAG - DMA1_FDT1_FLAG - DMA1_HDT1_FLAG - DMA1_DTERR1_FLAG
  270. * - DMA1_GL2_FLAG - DMA1_FDT2_FLAG - DMA1_HDT2_FLAG - DMA1_DTERR2_FLAG
  271. * - DMA1_GL3_FLAG - DMA1_FDT3_FLAG - DMA1_HDT3_FLAG - DMA1_DTERR3_FLAG
  272. * - DMA1_GL4_FLAG - DMA1_FDT4_FLAG - DMA1_HDT4_FLAG - DMA1_DTERR4_FLAG
  273. * - DMA1_GL5_FLAG - DMA1_FDT5_FLAG - DMA1_HDT5_FLAG - DMA1_DTERR5_FLAG
  274. * - DMA1_GL6_FLAG - DMA1_FDT6_FLAG - DMA1_HDT6_FLAG - DMA1_DTERR6_FLAG
  275. * - DMA1_GL7_FLAG - DMA1_FDT7_FLAG - DMA1_HDT7_FLAG - DMA1_DTERR7_FLAG
  276. * - DMA2_GL1_FLAG - DMA2_FDT1_FLAG - DMA2_HDT1_FLAG - DMA2_DTERR1_FLAG
  277. * - DMA2_GL2_FLAG - DMA2_FDT2_FLAG - DMA2_HDT2_FLAG - DMA2_DTERR2_FLAG
  278. * - DMA2_GL3_FLAG - DMA2_FDT3_FLAG - DMA2_HDT3_FLAG - DMA2_DTERR3_FLAG
  279. * - DMA2_GL4_FLAG - DMA2_FDT4_FLAG - DMA2_HDT4_FLAG - DMA2_DTERR4_FLAG
  280. * - DMA2_GL5_FLAG - DMA2_FDT5_FLAG - DMA2_HDT5_FLAG - DMA2_DTERR5_FLAG
  281. * - DMA2_GL6_FLAG - DMA2_FDT6_FLAG - DMA2_HDT6_FLAG - DMA2_DTERR6_FLAG
  282. * - DMA2_GL7_FLAG - DMA2_FDT7_FLAG - DMA2_HDT7_FLAG - DMA2_DTERR7_FLAG
  283. * @retval state of dma flag
  284. */
  285. flag_status dma_flag_get(uint32_t dmax_flag)
  286. {
  287. flag_status status = RESET;
  288. uint32_t temp = 0;
  289. if(dmax_flag > 0x10000000)
  290. {
  291. temp = DMA2->sts;
  292. }
  293. else
  294. {
  295. temp = DMA1->sts;
  296. }
  297. if ((temp & dmax_flag) != (uint16_t)RESET)
  298. {
  299. status = SET;
  300. }
  301. else
  302. {
  303. status = RESET;
  304. }
  305. return status;
  306. }
  307. /**
  308. * @brief clear dma flag
  309. * @param dmax_flag
  310. * this parameter can be one of the following values:
  311. * - DMA1_GL1_FLAG - DMA1_FDT1_FLAG - DMA1_HDT1_FLAG - DMA1_DTERR1_FLAG
  312. * - DMA1_GL2_FLAG - DMA1_FDT2_FLAG - DMA1_HDT2_FLAG - DMA1_DTERR2_FLAG
  313. * - DMA1_GL3_FLAG - DMA1_FDT3_FLAG - DMA1_HDT3_FLAG - DMA1_DTERR3_FLAG
  314. * - DMA1_GL4_FLAG - DMA1_FDT4_FLAG - DMA1_HDT4_FLAG - DMA1_DTERR4_FLAG
  315. * - DMA1_GL5_FLAG - DMA1_FDT5_FLAG - DMA1_HDT5_FLAG - DMA1_DTERR5_FLAG
  316. * - DMA1_GL6_FLAG - DMA1_FDT6_FLAG - DMA1_HDT6_FLAG - DMA1_DTERR6_FLAG
  317. * - DMA1_GL7_FLAG - DMA1_FDT7_FLAG - DMA1_HDT7_FLAG - DMA1_DTERR7_FLAG
  318. * - DMA2_GL1_FLAG - DMA2_FDT1_FLAG - DMA2_HDT1_FLAG - DMA2_DTERR1_FLAG
  319. * - DMA2_GL2_FLAG - DMA2_FDT2_FLAG - DMA2_HDT2_FLAG - DMA2_DTERR2_FLAG
  320. * - DMA2_GL3_FLAG - DMA2_FDT3_FLAG - DMA2_HDT3_FLAG - DMA2_DTERR3_FLAG
  321. * - DMA2_GL4_FLAG - DMA2_FDT4_FLAG - DMA2_HDT4_FLAG - DMA2_DTERR4_FLAG
  322. * - DMA2_GL5_FLAG - DMA2_FDT5_FLAG - DMA2_HDT5_FLAG - DMA2_DTERR5_FLAG
  323. * - DMA2_GL6_FLAG - DMA2_FDT6_FLAG - DMA2_HDT6_FLAG - DMA2_DTERR6_FLAG
  324. * - DMA2_GL7_FLAG - DMA2_FDT7_FLAG - DMA2_HDT7_FLAG - DMA2_DTERR7_FLAG
  325. * @retval none
  326. */
  327. void dma_flag_clear(uint32_t dmax_flag)
  328. {
  329. if(dmax_flag > 0x10000000)
  330. {
  331. DMA2->clr = (uint32_t)(dmax_flag & 0x0FFFFFFF);
  332. }
  333. else
  334. {
  335. DMA1->clr = dmax_flag;
  336. }
  337. }
  338. /**
  339. * @brief dma init config with its default value.
  340. * @param dma_init_struct : pointer to a dma_init_type structure which will
  341. * be initialized.
  342. * @retval none
  343. */
  344. void dma_default_para_init(dma_init_type* dma_init_struct)
  345. {
  346. dma_init_struct->peripheral_base_addr = 0x0;
  347. dma_init_struct->memory_base_addr = 0x0;
  348. dma_init_struct->direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
  349. dma_init_struct->buffer_size = 0x0;
  350. dma_init_struct->peripheral_inc_enable = FALSE;
  351. dma_init_struct->memory_inc_enable = FALSE;
  352. dma_init_struct->peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
  353. dma_init_struct->memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
  354. dma_init_struct->loop_mode_enable = FALSE;
  355. dma_init_struct->priority = DMA_PRIORITY_LOW;
  356. }
  357. /**
  358. * @brief dma init
  359. * @param dmax_channely:
  360. * this parameter can be one of the following values:
  361. * - DMA1_CHANNEL1
  362. * - DMA1_CHANNEL2
  363. * - DMA1_CHANNEL3
  364. * - DMA1_CHANNEL4
  365. * - DMA1_CHANNEL5
  366. * - DMA1_CHANNEL6
  367. * - DMA1_CHANNEL7
  368. * - DMA2_CHANNEL1
  369. * - DMA2_CHANNEL2
  370. * - DMA2_CHANNEL3
  371. * - DMA2_CHANNEL4
  372. * - DMA2_CHANNEL5
  373. * - DMA2_CHANNEL6
  374. * - DMA2_CHANNEL7
  375. * @param dma_initstruct : pointer to a dma_init_type structure.
  376. * @retval none
  377. */
  378. void dma_init(dma_channel_type* dmax_channely, dma_init_type* dma_init_struct)
  379. {
  380. /* clear ctrl register dtd bit and m2m bit */
  381. dmax_channely->ctrl &= 0xbfef;
  382. dmax_channely->ctrl |= dma_init_struct->direction;
  383. dmax_channely->ctrl_bit.chpl = dma_init_struct->priority;
  384. dmax_channely->ctrl_bit.mwidth = dma_init_struct->memory_data_width;
  385. dmax_channely->ctrl_bit.pwidth = dma_init_struct->peripheral_data_width;
  386. dmax_channely->ctrl_bit.mincm = dma_init_struct->memory_inc_enable;
  387. dmax_channely->ctrl_bit.pincm = dma_init_struct->peripheral_inc_enable;
  388. dmax_channely->ctrl_bit.lm = dma_init_struct->loop_mode_enable;
  389. dmax_channely->dtcnt = dma_init_struct->buffer_size;
  390. dmax_channely->paddr = dma_init_struct->peripheral_base_addr;
  391. dmax_channely->maddr = dma_init_struct->memory_base_addr;
  392. }
  393. /**
  394. * @}
  395. */
  396. #endif
  397. /**
  398. * @}
  399. */
  400. /**
  401. * @}
  402. */