gd32f4xx_dma.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*!
  2. \file gd32f4xx_dma.c
  3. \brief DMA driver
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #include "gd32f4xx_dma.h"
  10. /* DMA register bit offset */
  11. #define CHXCTL_PERIEN_OFFSET ((uint32_t)25U)
  12. /*!
  13. \brief deinitialize DMA a channel registers
  14. \param[in] dma_periph: DMAx(x=0,1)
  15. \arg DMAx(x=0,1)
  16. \param[in] channelx: specify which DMA channel is deinitialized
  17. \arg DMA_CHx(x=0..7)
  18. \param[out] none
  19. \retval none
  20. */
  21. void dma_deinit(uint32_t dma_periph,dma_channel_enum channelx)
  22. {
  23. /* disable DMA a channel */
  24. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_CHEN;
  25. /* reset DMA channel registers */
  26. DMA_CHCTL(dma_periph,channelx) = DMA_CHCTL_RESET_VALUE;
  27. DMA_CHCNT(dma_periph,channelx) = DMA_CHCNT_RESET_VALUE;
  28. DMA_CHPADDR(dma_periph,channelx) = DMA_CHPADDR_RESET_VALUE;
  29. DMA_CHM0ADDR(dma_periph,channelx) = DMA_CHMADDR_RESET_VALUE;
  30. DMA_CHM1ADDR(dma_periph,channelx) = DMA_CHMADDR_RESET_VALUE;
  31. DMA_CHFCTL(dma_periph,channelx) = DMA_CHFCTL_RESET_VALUE;
  32. if(channelx < DMA_CH4){
  33. DMA_INTC0(dma_periph) |= DMA_FLAG_ADD(DMA_CHINTF_RESET_VALUE,channelx);
  34. }else{
  35. DMA_INTC1(dma_periph) |= DMA_FLAG_ADD(DMA_CHINTF_RESET_VALUE,channelx);
  36. }
  37. }
  38. /*!
  39. \brief initialize DMA single data mode
  40. \param[in] dma_periph: DMAx(x=0,1)
  41. \arg DMAx(x=0,1)
  42. \param[in] channelx: specify which DMA channel is initialized
  43. \arg DMA_CHx(x=0..7)
  44. \param[in] init_struct: the data needed to initialize DMA single data mode
  45. periph_addr: peripheral base address
  46. periph_memory_width: DMA_PERIPH_WIDTH_8BIT,DMA_PERIPH_WIDTH_16BIT,DMA_PERIPH_WIDTH_32BIT
  47. periph_inc: DMA_PERIPH_INCREASE_ENABLE,DMA_PERIPH_INCREASE_DISABLE,DMA_PERIPH_INCREASE_FIX
  48. memory0_addr: memory base address
  49. memory_inc: DMA_MEMORY_INCREASE_ENABLE,DMA_MEMORY_INCREASE_DISABLE
  50. direction: DMA_PERIPH_TO_MEMORY,DMA_MEMORY_TO_PERIPH,DMA_MEMORY_TO_MEMORY
  51. number: the number of remaining data to be transferred by the DMA
  52. priority: DMA_PRIORITY_LOW,DMA_PRIORITY_MEDIUM,DMA_PRIORITY_HIGH,DMA_PRIORITY_ULTRA_HIGH
  53. circular_mode: DMA_CIRCULAR_MODE_ENABLE,DMA_CIRCULAR_MODE_DISABLE
  54. \param[out] none
  55. \retval none
  56. */
  57. void dma_single_data_mode_init(uint32_t dma_periph,dma_channel_enum channelx,dma_single_data_parameter_struct init_struct)
  58. {
  59. uint32_t ctl;
  60. /* select single data mode */
  61. DMA_CHFCTL(dma_periph,channelx) &= ~DMA_CHXFCTL_MDMEN;
  62. /* configure peripheral base address */
  63. DMA_CHPADDR(dma_periph,channelx) = init_struct.periph_addr;
  64. /* configure memory base address */
  65. DMA_CHM0ADDR(dma_periph,channelx) = init_struct.memory0_addr;
  66. /* configure the number of remaining data to be transferred */
  67. DMA_CHCNT(dma_periph,channelx) = init_struct.number;
  68. /* configure peripheral and memory transfer width,channel priotity,transfer mode */
  69. ctl = DMA_CHCTL(dma_periph,channelx);
  70. ctl &= ~(DMA_CHXCTL_PWIDTH | DMA_CHXCTL_MWIDTH | DMA_CHXCTL_PRIO | DMA_CHXCTL_TM);
  71. ctl |= (init_struct.periph_memory_width | (init_struct.periph_memory_width << 2) | init_struct.priority | init_struct.direction);
  72. DMA_CHCTL(dma_periph,channelx) = ctl;
  73. /* configure peripheral increasing mode */
  74. if(DMA_PERIPH_INCREASE_ENABLE == init_struct.periph_inc){
  75. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_PNAGA;
  76. }else if(DMA_PERIPH_INCREASE_DISABLE == init_struct.periph_inc){
  77. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_PNAGA;
  78. }else{
  79. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_PAIF;
  80. }
  81. /* configure memory increasing mode */
  82. if(DMA_MEMORY_INCREASE_ENABLE == init_struct.memory_inc){
  83. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_MNAGA;
  84. }else{
  85. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_MNAGA;
  86. }
  87. /* configure DMA circular mode */
  88. if(DMA_CIRCULAR_MODE_ENABLE == init_struct.circular_mode){
  89. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_CMEN;
  90. }else{
  91. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_CMEN;
  92. }
  93. }
  94. /*!
  95. \brief initialize DMA multi data mode
  96. \param[in] dma_periph: DMAx(x=0,1)
  97. \arg DMAx(x=0,1)
  98. \param[in] channelx: specify which DMA channel is initialized
  99. \arg DMA_CHx(x=0..7)
  100. \param[in] dma_multi_data_parameter_struct: the data needed to initialize DMA multi data mode
  101. periph_addr: peripheral base address
  102. periph_width: DMA_PERIPH_WIDTH_8BIT,DMA_PERIPH_WIDTH_16BIT,DMA_PERIPH_WIDTH_32BIT
  103. periph_inc: DMA_PERIPH_INCREASE_ENABLE,DMA_PERIPH_INCREASE_DISABLE,DMA_PERIPH_INCREASE_FIX
  104. memory0_addr: memory0 base address
  105. memory_width: DMA_MEMORY_WIDTH_8BIT,DMA_MEMORY_WIDTH_16BIT,DMA_MEMORY_WIDTH_32BIT
  106. memory_inc: DMA_MEMORY_INCREASE_ENABLE,DMA_MEMORY_INCREASE_DISABLE
  107. direction: DMA_PERIPH_TO_MEMORY,DMA_MEMORY_TO_PERIPH,DMA_MEMORY_TO_MEMORY
  108. number: the number of remaining data to be transferred by the DMA
  109. priority: DMA_PRIORITY_LOW,DMA_PRIORITY_MEDIUM,DMA_PRIORITY_HIGH,DMA_PRIORITY_ULTRA_HIGH
  110. circular_mode: DMA_CIRCULAR_MODE_ENABLE,DMA_CIRCULAR_MODE_DISABLE
  111. memory_burst_width: DMA_MEMORY_BURST_SINGLE,DMA_MEMORY_BURST_4_BEAT,DMA_MEMORY_BURST_8_BEAT,DMA_MEMORY_BURST_16_BEAT
  112. periph_burst_width: DMA_PERIPH_BURST_SINGLE,DMA_PERIPH_BURST_4_BEAT,DMA_PERIPH_BURST_8_BEAT,DMA_PERIPH_BURST_16_BEAT
  113. critical_value: DMA_FIFO_1_WORD,DMA_FIFO_2_WORD,DMA_FIFO_3_WORD,DMA_FIFO_4_WORD
  114. \param[out] none
  115. \retval none
  116. */
  117. void dma_multi_data_mode_init(uint32_t dma_periph,dma_channel_enum channelx,dma_multi_data_parameter_struct init_struct)
  118. {
  119. uint32_t ctl;
  120. /* select multi data mode and configure FIFO critical value */
  121. DMA_CHFCTL(dma_periph,channelx) |= (DMA_CHXFCTL_MDMEN | init_struct.critical_value);
  122. /* configure peripheral base address */
  123. DMA_CHPADDR(dma_periph,channelx) = init_struct.periph_addr;
  124. /* configure memory base address */
  125. DMA_CHM0ADDR(dma_periph,channelx) = init_struct.memory0_addr;
  126. /* configure the number of remaining data to be transferred */
  127. DMA_CHCNT(dma_periph,channelx) = init_struct.number;
  128. /* configure peripheral and memory transfer width,channel priotity,transfer mode,peripheral and memory burst transfer width */
  129. ctl = DMA_CHCTL(dma_periph,channelx);
  130. ctl &= ~(DMA_CHXCTL_PWIDTH | DMA_CHXCTL_MWIDTH | DMA_CHXCTL_PRIO | DMA_CHXCTL_TM | DMA_CHXCTL_PBURST | DMA_CHXCTL_MBURST);
  131. ctl |= (init_struct.periph_width | (init_struct.memory_width ) | init_struct.priority | init_struct.direction | init_struct.memory_burst_width | init_struct.periph_burst_width);
  132. DMA_CHCTL(dma_periph,channelx) = ctl;
  133. /* configure peripheral increasing mode */
  134. if(DMA_PERIPH_INCREASE_ENABLE == init_struct.periph_inc){
  135. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_PNAGA;
  136. }else if(DMA_PERIPH_INCREASE_DISABLE == init_struct.periph_inc){
  137. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_PNAGA;
  138. }else{
  139. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_PAIF;
  140. }
  141. /* configure memory increasing mode */
  142. if(DMA_MEMORY_INCREASE_ENABLE == init_struct.memory_inc){
  143. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_MNAGA;
  144. }else{
  145. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_MNAGA;
  146. }
  147. /* configure DMA circular mode */
  148. if(DMA_CIRCULAR_MODE_ENABLE == init_struct.circular_mode){
  149. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_CMEN;
  150. }else{
  151. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_CMEN;
  152. }
  153. }
  154. /*!
  155. \brief get DMA flag is set or not
  156. \param[in] dma_periph: DMAx(x=0,1)
  157. \arg DMAx(x=0,1)
  158. \param[in] channelx: specify which DMA channel to get flag
  159. \arg DMA_CHx(x=0..7)
  160. \param[in] flag: specify get which flag
  161. \arg DMA_INTF_FEEIF: FIFO error and exception flag
  162. \arg DMA_INTF_SDEIF: single data mode exception flag
  163. \arg DMA_INTF_TAEIF: transfer access error flag
  164. \arg DMA_INTF_HTFIF: half transfer finish flag
  165. \arg DMA_INTF_FTFIF: full transger finish flag
  166. \param[out] none
  167. \retval FlagStatus: SET or RESET
  168. */
  169. FlagStatus dma_flag_get(uint32_t dma_periph,dma_channel_enum channelx,uint32_t flag)
  170. {
  171. if(channelx < DMA_CH4){
  172. if(DMA_INTF0(dma_periph) & DMA_FLAG_ADD(flag,channelx)){
  173. return SET;
  174. }else{
  175. return RESET;
  176. }
  177. }else{
  178. channelx -= (dma_channel_enum)4;
  179. if(DMA_INTF1(dma_periph) & DMA_FLAG_ADD(flag,channelx)){
  180. return SET;
  181. }else{
  182. return RESET;
  183. }
  184. }
  185. }
  186. /*!
  187. \brief clear DMA a channel flag
  188. \param[in] dma_periph: DMAx(x=0,1)
  189. \arg DMAx(x=0,1)
  190. \param[in] channelx: specify which DMA channel to get flag
  191. \arg DMA_CHx(x=0..7)
  192. \param[in] flag: specify get which flag
  193. \arg DMA_INTF_FEEIF: FIFO error and exception flag
  194. \arg DMA_INTF_SDEIF: single data mode exception flag
  195. \arg DMA_INTF_TAEIF: transfer access error flag
  196. \arg DMA_INTF_HTFIF: half transfer finish flag
  197. \arg DMA_INTF_FTFIF: full transger finish flag
  198. \param[out] none
  199. \retval none
  200. */
  201. void dma_flag_clear(uint32_t dma_periph,dma_channel_enum channelx,uint32_t flag)
  202. {
  203. if(channelx < DMA_CH4){
  204. DMA_INTC0(dma_periph) |= DMA_FLAG_ADD(flag,channelx);
  205. }else{
  206. channelx -= (dma_channel_enum)4;
  207. DMA_INTC1(dma_periph) |= DMA_FLAG_ADD(flag,channelx);
  208. }
  209. }
  210. /*!
  211. \brief get DMA interrupt flag is set or not
  212. \param[in] dma_periph: DMAx(x=0,1)
  213. \arg DMAx(x=0,1)
  214. \param[in] channelx: specify which DMA channel to get interrupt flag
  215. \arg DMA_CHx(x=0..7)
  216. \param[in] interrupt: specify get which flag
  217. \arg DMA_INTF_FEEIF: FIFO error and exception flag
  218. \arg DMA_INTF_SDEIF: single data mode exception flag
  219. \arg DMA_INTF_TAEIF: transfer access error flag
  220. \arg DMA_INTF_HTFIF: half transfer finish flag
  221. \arg DMA_INTF_FTFIF: full transger finish flag
  222. \param[out] none
  223. \retval FlagStatus: SET or RESET
  224. */
  225. FlagStatus dma_interrupt_flag_get(uint32_t dma_periph,dma_channel_enum channelx,uint32_t interrupt)
  226. {
  227. uint32_t interrupt_enable = 0U,interrupt_flag = 0U;
  228. dma_channel_enum channel_flag_offset = channelx;
  229. if(channelx < DMA_CH4){
  230. switch(interrupt){
  231. case DMA_INTF_FEEIF:
  232. interrupt_flag = DMA_INTF0(dma_periph) & DMA_FLAG_ADD(interrupt,channelx);
  233. interrupt_enable = DMA_CHFCTL(dma_periph,channelx) & DMA_CHXFCTL_FEEIE;
  234. break;
  235. case DMA_INTF_SDEIF:
  236. interrupt_flag = DMA_INTF0(dma_periph) & DMA_FLAG_ADD(interrupt,channelx);
  237. interrupt_enable = DMA_CHCTL(dma_periph,channelx) & DMA_CHXCTL_SDEIE;
  238. break;
  239. case DMA_INTF_TAEIF:
  240. interrupt_flag = DMA_INTF0(dma_periph) & DMA_FLAG_ADD(interrupt,channelx);
  241. interrupt_enable = DMA_CHCTL(dma_periph,channelx) & DMA_CHXCTL_TAEIE;
  242. break;
  243. case DMA_INTF_HTFIF:
  244. interrupt_flag = DMA_INTF0(dma_periph) & DMA_FLAG_ADD(interrupt,channelx);
  245. interrupt_enable = DMA_CHCTL(dma_periph,channelx) & DMA_CHXCTL_HTFIE;
  246. break;
  247. case DMA_INTF_FTFIF:
  248. interrupt_flag = (DMA_INTF0(dma_periph) & DMA_FLAG_ADD(interrupt,channelx));
  249. interrupt_enable = (DMA_CHCTL(dma_periph,channelx) & DMA_CHXCTL_FTFIE);
  250. break;
  251. default:
  252. break;
  253. }
  254. }else{
  255. channel_flag_offset -= (dma_channel_enum)4;
  256. switch(interrupt){
  257. case DMA_INTF_FEEIF:
  258. interrupt_flag = DMA_INTF1(dma_periph) & DMA_FLAG_ADD(interrupt,channel_flag_offset);
  259. interrupt_enable = DMA_CHFCTL(dma_periph,channelx) & DMA_CHXFCTL_FEEIE;
  260. break;
  261. case DMA_INTF_SDEIF:
  262. interrupt_flag = DMA_INTF1(dma_periph) & DMA_FLAG_ADD(interrupt,channel_flag_offset);
  263. interrupt_enable = DMA_CHCTL(dma_periph,channelx) & DMA_CHXCTL_SDEIE;
  264. break;
  265. case DMA_INTF_TAEIF:
  266. interrupt_flag = DMA_INTF1(dma_periph) & DMA_FLAG_ADD(interrupt,channel_flag_offset);
  267. interrupt_enable = DMA_CHCTL(dma_periph,channelx) & DMA_CHXCTL_TAEIE;
  268. break;
  269. case DMA_INTF_HTFIF:
  270. interrupt_flag = DMA_INTF1(dma_periph) & DMA_FLAG_ADD(interrupt,channel_flag_offset);
  271. interrupt_enable = DMA_CHCTL(dma_periph,channelx) & DMA_CHXCTL_HTFIE;
  272. break;
  273. case DMA_INTF_FTFIF:
  274. interrupt_flag = DMA_INTF1(dma_periph) & DMA_FLAG_ADD(interrupt,channel_flag_offset);
  275. interrupt_enable = DMA_CHCTL(dma_periph,channelx) & DMA_CHXCTL_FTFIE;
  276. break;
  277. default:
  278. break;
  279. }
  280. }
  281. if(interrupt_flag && interrupt_enable){
  282. return SET;
  283. }else{
  284. return RESET;
  285. }
  286. }
  287. /*!
  288. \brief clear DMA a channel interrupt flag
  289. \param[in] dma_periph: DMAx(x=0,1)
  290. \arg DMAx(x=0,1)
  291. \param[in] channelx: specify which DMA channel to clear interrupt flag
  292. \arg DMA_CHx(x=0..7)
  293. \param[in] interrupt: specify get which flag
  294. \arg DMA_INTC_FEEIFC: clear FIFO error and exception flag
  295. \arg DMA_INTC_SDEIFC: clear single data mode exception flag
  296. \arg DMA_INTC_TAEIFC: clear transfer access error flag
  297. \arg DMA_INTC_HTFIFC: clear half transfer finish flag
  298. \arg DMA_INTC_FTFIFC: clear full transger finish flag
  299. \param[out] none
  300. \retval none
  301. */
  302. void dma_interrupt_flag_clear(uint32_t dma_periph,dma_channel_enum channelx,uint32_t interrupt)
  303. {
  304. if(channelx < DMA_CH4){
  305. DMA_INTC0(dma_periph) |= DMA_FLAG_ADD(interrupt,channelx);
  306. }else{
  307. channelx -= (dma_channel_enum)4;
  308. DMA_INTC1(dma_periph) |= DMA_FLAG_ADD(interrupt,channelx);
  309. }
  310. }
  311. /*!
  312. \brief enable DMA interrupt
  313. \param[in] dma_periph: DMAx(x=0,1)
  314. \arg DMAx(x=0,1)
  315. \param[in] channelx: specify which DMA channel
  316. \arg DMA_CHx(x=0..7)
  317. \param[in] source: specify which interrupt to enbale
  318. \arg DMA_CHXCTL_SDEIE: single data mode exception interrupt enable
  319. \arg DMA_CHXCTL_TAEIE: tranfer access error interrupt enable
  320. \arg DMA_CHXCTL_HTFIE: half transfer finish interrupt enable
  321. \arg DMA_CHXCTL_FTFIE: full transfer finish interrupt enable
  322. \arg DMA_CHXFCTL_FEEIE: FIFO exception interrupt enable
  323. \param[out] none
  324. \retval none
  325. */
  326. void dma_interrupt_enable(uint32_t dma_periph,dma_channel_enum channelx,uint32_t source)
  327. {
  328. if(DMA_CHXFCTL_FEEIE != source){
  329. DMA_CHCTL(dma_periph,channelx) |= source;
  330. }else{
  331. DMA_CHFCTL(dma_periph,channelx) |= source;
  332. }
  333. }
  334. /*!
  335. \brief disable DMA interrupt
  336. \param[in] dma_periph: DMAx(x=0,1)
  337. \arg DMAx(x=0,1)
  338. \param[in] channelx: specify which DMA channel
  339. \arg DMA_CHx(x=0..7)
  340. \param[in] source: specify which interrupt to disbale
  341. \arg DMA_CHXCTL_SDEIE: single data mode exception interrupt enable
  342. \arg DMA_CHXCTL_TAEIE: tranfer access error interrupt enable
  343. \arg DMA_CHXCTL_HTFIE: half transfer finish interrupt enable
  344. \arg DMA_CHXCTL_FTFIE: full transfer finish interrupt enable
  345. \arg DMA_CHXFCTL_FEEIE: FIFO exception interrupt enable
  346. \param[out] none
  347. \retval none
  348. */
  349. void dma_interrupt_disable(uint32_t dma_periph,dma_channel_enum channelx,uint32_t source)
  350. {
  351. if(DMA_CHXFCTL_FEEIE != source){
  352. DMA_CHCTL(dma_periph,channelx) &= ~source;
  353. }else{
  354. DMA_CHFCTL(dma_periph,channelx) &= ~source;
  355. }
  356. }
  357. /*!
  358. \brief set DMA peripheral base address
  359. \param[in] dma_periph: DMAx(x=0,1)
  360. \arg DMAx(x=0,1)
  361. \param[in] channelx: specify which DMA channel to set peripheral base address
  362. \arg DMA_CHx(x=0..7)
  363. \param[in] address: peripheral base address
  364. \param[out] none
  365. \retval none
  366. */
  367. void dma_periph_address_config(uint32_t dma_periph,dma_channel_enum channelx,uint32_t address)
  368. {
  369. DMA_CHPADDR(dma_periph,channelx) = address;
  370. }
  371. /*!
  372. \brief set DMA Memory0 base address
  373. \param[in] dma_periph: DMAx(x=0,1)
  374. \arg DMAx(x=0,1)
  375. \param[in] channelx: specify which DMA channel to set Memory base address
  376. \arg DMA_CHx(x=0..7)
  377. \param[in] memory_flag: DMA_MEMORY_x(x=0,1)
  378. \param[in] address: Memory base address
  379. \param[out] none
  380. \retval none
  381. */
  382. void dma_memory_address_config(uint32_t dma_periph,dma_channel_enum channelx,uint8_t memory_flag,uint32_t address)
  383. {
  384. if(memory_flag){
  385. DMA_CHM1ADDR(dma_periph,channelx) = address;
  386. }else{
  387. DMA_CHM0ADDR(dma_periph,channelx) = address;
  388. }
  389. }
  390. /*!
  391. \brief set the number of remaining data to be transferred by the DMA
  392. \param[in] dma_periph: DMAx(x=0,1)
  393. \arg DMAx(x=0,1)
  394. \param[in] channelx: specify which DMA channel to set number
  395. \arg DMA_CHx(x=0..7)
  396. \param[in] number: the number of remaining data to be transferred by the DMA
  397. \param[out] none
  398. \retval none
  399. */
  400. void dma_transfer_number_config(uint32_t dma_periph,dma_channel_enum channelx,uint32_t number)
  401. {
  402. DMA_CHCNT(dma_periph,channelx) = number;
  403. }
  404. /*!
  405. \brief get the number of remaining data to be transferred by the DMA
  406. \param[in] dma_periph: DMAx(x=0,1)
  407. \arg DMAx(x=0,1)
  408. \param[in] channelx: specify which DMA channel to set number
  409. \arg DMA_CHx(x=0..7)
  410. \param[out] none
  411. \retval uint32_t: the number of remaining data to be transferred by the DMA
  412. */
  413. uint32_t dma_transfer_number_get(uint32_t dma_periph,dma_channel_enum channelx)
  414. {
  415. return (uint32_t)DMA_CHCNT(dma_periph,channelx);
  416. }
  417. /*!
  418. \brief configure priority level of DMA channel
  419. \param[in] dma_periph: DMAx(x=0,1)
  420. \arg DMAx(x=0,1)
  421. \param[in] channelx: specify which DMA channel
  422. \arg DMA_CHx(x=0..7)
  423. \param[in] priority: priority Level of this channel
  424. \arg DMA_PRIORITY_LOW: low priority
  425. \arg DMA_PRIORITY_MEDIUM: medium priority
  426. \arg DMA_PRIORITY_HIGH: high priority
  427. \arg DMA_PRIORITY_ULTRA_HIGH: ultra high priority
  428. \param[out] none
  429. \retval none
  430. */
  431. void dma_priority_config(uint32_t dma_periph,dma_channel_enum channelx,uint32_t priority)
  432. {
  433. uint32_t ctl;
  434. /* acquire DMA_CHxCTL register */
  435. ctl = DMA_CHCTL(dma_periph,channelx);
  436. /* assign regiser */
  437. ctl &= ~DMA_CHXCTL_PRIO;
  438. ctl |= priority;
  439. DMA_CHCTL(dma_periph,channelx) = ctl;
  440. }
  441. /*!
  442. \brief configure transfer burst beats of memory
  443. \param[in] dma_periph: DMAx(x=0,1)
  444. \arg DMAx(x=0,1)
  445. \param[in] channelx: specify which DMA channel
  446. \arg DMA_CHx(x=0..7)
  447. \param[in] mbeat: transfer burst beats
  448. \arg DMA_MEMORY_BURST_SINGLE: memory transfer single burst
  449. \arg DMA_MEMORY_BURST_4_BEAT: memory transfer 4-beat burst
  450. \arg DMA_MEMORY_BURST_8_BEAT: memory transfer 8-beat burst
  451. \arg DMA_MEMORY_BURST_16_BEAT: memory transfer 16-beat burst
  452. \param[out] none
  453. \retval none
  454. */
  455. void dma_memory_burst_beats_config (uint32_t dma_periph,dma_channel_enum channelx,uint32_t mbeat)
  456. {
  457. uint32_t ctl;
  458. /* acquire DMA_CHxCTL register */
  459. ctl = DMA_CHCTL(dma_periph,channelx);
  460. /* assign regiser */
  461. ctl &= ~DMA_CHXCTL_MBURST;
  462. ctl |= mbeat;
  463. DMA_CHCTL(dma_periph,channelx) = ctl;
  464. }
  465. /*!
  466. \brief configure transfer burst beats of peripheral
  467. \param[in] dma_periph: DMAx(x=0,1)
  468. \arg DMAx(x=0,1)
  469. \param[in] channelx: specify which DMA channel
  470. \arg DMA_CHx(x=0..7)
  471. \param[in] pbeat: transfer burst beats
  472. \arg DMA_PERIPH_BURST_SINGLE: peripheral transfer single burst
  473. \arg DMA_PERIPH_BURST_4_BEAT: peripheral transfer 4-beat burst
  474. \arg DMA_PERIPH_BURST_8_BEAT: peripheral transfer 8-beat burst
  475. \arg DMA_PERIPH_BURST_16_BEAT: peripheral transfer 16-beat burst
  476. \param[out] none
  477. \retval none
  478. */
  479. void dma_periph_burst_beats_config (uint32_t dma_periph,dma_channel_enum channelx,uint32_t pbeat)
  480. {
  481. uint32_t ctl;
  482. /* acquire DMA_CHxCTL register */
  483. ctl = DMA_CHCTL(dma_periph,channelx);
  484. /* assign regiser */
  485. ctl &= ~DMA_CHXCTL_PBURST;
  486. ctl |= pbeat;
  487. DMA_CHCTL(dma_periph,channelx) = ctl;
  488. }
  489. /*!
  490. \brief configure transfer data size of memory
  491. \param[in] dma_periph: DMAx(x=0,1)
  492. \arg DMAx(x=0,1)
  493. \param[in] channelx: specify which DMA channel
  494. \arg DMA_CHx(x=0..7)
  495. \param[in] msize: transfer data size of memory
  496. \arg DMA_MEMORY_WIDTH_8BIT: transfer data size of memory is 8-bit
  497. \arg DMA_MEMORY_WIDTH_16BIT: transfer data size of memory is 16-bit
  498. \arg DMA_MEMORY_WIDTH_32BIT: transfer data size of memory is 32-bit
  499. \param[out] none
  500. \retval none
  501. */
  502. void dma_memory_width_config(uint32_t dma_periph,dma_channel_enum channelx,uint32_t msize)
  503. {
  504. uint32_t ctl;
  505. /* acquire DMA_CHxCTL register */
  506. ctl = DMA_CHCTL(dma_periph,channelx);
  507. /* assign regiser */
  508. ctl &= ~DMA_CHXCTL_MWIDTH;
  509. ctl |= msize;
  510. DMA_CHCTL(dma_periph,channelx) = ctl;
  511. }
  512. /*!
  513. \brief configure transfer data size of peripheral
  514. \param[in] dma_periph: DMAx(x=0,1)
  515. \arg DMAx(x=0,1)
  516. \param[in] channelx: specify which DMA channel
  517. \arg DMA_CHx(x=0..7)
  518. \param[in] msize: transfer data size of peripheral
  519. \arg DMA_PERIPHERAL_WIDTH_8BIT: transfer data size of peripheral is 8-bit
  520. \arg DMA_PERIPHERAL_WIDTH_16BIT: transfer data size of peripheral is 16-bit
  521. \arg DMA_PERIPHERAL_WIDTH_32BIT: transfer data size of peripheral is 32-bit
  522. \param[out] none
  523. \retval none
  524. */
  525. void dma_periph_width_config (uint32_t dma_periph,dma_channel_enum channelx,uint32_t psize)
  526. {
  527. uint32_t ctl;
  528. /* acquire DMA_CHxCTL register */
  529. ctl = DMA_CHCTL(dma_periph,channelx);
  530. /* assign regiser */
  531. ctl &= ~DMA_CHXCTL_PWIDTH;
  532. ctl |= psize;
  533. DMA_CHCTL(dma_periph,channelx) = ctl;
  534. }
  535. /*!
  536. \brief configure memory address generation generation_algorithm
  537. \param[in] dma_periph: DMAx(x=0,1)
  538. \arg DMAx(x=0,1)
  539. \param[in] channelx: specify which DMA channel
  540. \arg DMA_CHx(x=0..7)
  541. \param[in] generation_algorithm: the address generation algorithm
  542. \arg DMA_MEMORY_INCREASE_ENABLE: next address of memory is increasing address mode
  543. \arg DMA_MEMORY_INCREASE_DISABLE: next address of memory is fixed address mode
  544. \param[out] none
  545. \retval none
  546. */
  547. void dma_memory_address_generation_config(uint32_t dma_periph,dma_channel_enum channelx,uint8_t generation_algorithm)
  548. {
  549. if(DMA_MEMORY_INCREASE_ENABLE == generation_algorithm){
  550. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_MNAGA;
  551. }else{
  552. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_MNAGA;
  553. }
  554. }
  555. /*!
  556. \brief configure peripheral address generation generation_algorithm
  557. \param[in] dma_periph: DMAx(x=0,1)
  558. \arg DMAx(x=0,1)
  559. \param[in] channelx: specify which DMA channel
  560. \arg DMA_CHx(x=0..7)
  561. \param[in] generation_algorithm: the address generation algorithm
  562. \arg DMA_PERIPH_INCREASE_ENABLE: next address of peripheral is increasing address mode
  563. \arg DMA_PERIPH_INCREASE_DISABLE: next address of peripheral is fixed address mode
  564. \arg DMA_PERIPH_INCREASE_FIX: increasing steps of peripheral address is fixed
  565. \param[out] none
  566. \retval none
  567. */
  568. void dma_peripheral_address_generation_config(uint32_t dma_periph,dma_channel_enum channelx,uint8_t generation_algorithm)
  569. {
  570. if(DMA_PERIPH_INCREASE_ENABLE == generation_algorithm){
  571. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_PNAGA;
  572. }else if(DMA_PERIPH_INCREASE_DISABLE == generation_algorithm){
  573. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_PNAGA;
  574. }else{
  575. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_PNAGA;
  576. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_PAIF;
  577. }
  578. }
  579. /*!
  580. \brief enable DMA circulation mode
  581. \param[in] dma_periph: DMAx(x=0,1)
  582. \arg DMAx(x=0,1)
  583. \param[in] channelx: specify which DMA channel
  584. \arg DMA_CHx(x=0..7)
  585. \param[out] none
  586. \retval none
  587. */
  588. void dma_circulation_enable(uint32_t dma_periph,dma_channel_enum channelx)
  589. {
  590. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_CMEN;
  591. }
  592. /*!
  593. \brief disable DMA circulation mode
  594. \param[in] dma_periph: DMAx(x=0,1)
  595. \arg DMAx(x=0,1)
  596. \param[in] channelx: specify which DMA channel
  597. \arg DMA_CHx(x=0..7)
  598. \param[out] none
  599. \retval none
  600. */
  601. void dma_circulation_disable(uint32_t dma_periph,dma_channel_enum channelx)
  602. {
  603. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_CMEN;
  604. }
  605. /*!
  606. \brief configure the direction of data transfer on the channel
  607. \param[in] dma_periph: DMAx(x=0,1)
  608. \arg DMAx(x=0,1)
  609. \param[in] channelx: specify which DMA channel
  610. \arg DMA_CHx(x=0..7)
  611. \param[in] direction: specify the direction of data transfer
  612. \arg DMA_PERIPH_TO_MEMORY: read from peripheral and write to memory
  613. \arg DMA_MEMORY_TO_PERIPH: read from memory and write to peripheral
  614. \arg DMA_MEMORY_TO_MEMORY: read from memory and write to memory
  615. \param[out] none
  616. \retval none
  617. */
  618. void dma_transfer_direction_config(uint32_t dma_periph,dma_channel_enum channelx,uint8_t direction)
  619. {
  620. uint32_t ctl;
  621. /* acquire DMA_CHxCTL register */
  622. ctl = DMA_CHCTL(dma_periph,channelx);
  623. /* assign regiser */
  624. ctl &= ~DMA_CHXCTL_TM;
  625. ctl |= direction;
  626. DMA_CHCTL(dma_periph,channelx) = ctl;
  627. }
  628. /*!
  629. \brief enable DMA channel
  630. \param[in] dma_periph: DMAx(x=0,1)
  631. \arg DMAx(x=0,1)
  632. \param[in] channelx: specify which DMA channel
  633. \arg DMA_CHx(x=0..7)
  634. \param[out] none
  635. \retval none
  636. */
  637. void dma_channel_enable(uint32_t dma_periph,dma_channel_enum channelx)
  638. {
  639. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_CHEN;
  640. }
  641. /*!
  642. \brief disable DMA channel
  643. \param[in] dma_periph: DMAx(x=0,1)
  644. \arg DMAx(x=0,1)
  645. \param[in] channelx: specify which DMA channel
  646. \arg DMA_CHx(x=0..7)
  647. \param[out] none
  648. \retval none
  649. */
  650. void dma_channel_disable(uint32_t dma_periph,dma_channel_enum channelx)
  651. {
  652. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_CHEN;
  653. }
  654. /*!
  655. \brief DMA channel peripheral select
  656. \param[in] dma_periph: DMAx(x=0,1)
  657. \arg DMAx(x=0,1)
  658. \param[in] channelx: specify which DMA channel
  659. \arg DMA_CHx(x=0..7)
  660. \param[in] sub_periph: specify DMA channel peripheral
  661. \arg DMA_SUBPERIx(x=0..7)
  662. \param[out] none
  663. \retval none
  664. */
  665. void dma_channel_subperipheral_select(uint32_t dma_periph,dma_channel_enum channelx,dma_subperipheral_enum sub_periph)
  666. {
  667. uint32_t ctl;
  668. /* acquire DMA_CHxCTL register */
  669. ctl = DMA_CHCTL(dma_periph,channelx);
  670. /* assign regiser */
  671. ctl &= ~DMA_CHXCTL_PERIEN;
  672. ctl |= ((uint32_t)sub_periph << CHXCTL_PERIEN_OFFSET);
  673. DMA_CHCTL(dma_periph,channelx) = ctl;
  674. }
  675. /*!
  676. \brief DMA switch buffer mode config
  677. \param[in] dma_periph: DMAx(x=0,1)
  678. \arg DMAx(x=0,1)
  679. \param[in] channelx: specify which DMA channel
  680. \arg DMA_CHx(x=0..7)
  681. \param[in] memory1_addr: memory1 base address
  682. \param[in] memory_select: DMA_MEMORY_0 or DMA_MEMORY_1
  683. \param[out] none
  684. \retval none
  685. */
  686. void dma_switch_buffer_mode_config(uint32_t dma_periph,dma_channel_enum channelx,uint32_t memory1_addr,uint32_t memory_select)
  687. {
  688. /* configure memory1 base address */
  689. DMA_CHM1ADDR(dma_periph,channelx) = memory1_addr;
  690. if(DMA_MEMORY_0 == memory_select){
  691. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_MBS;
  692. }else{
  693. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_MBS;
  694. }
  695. }
  696. /*!
  697. \brief DMA switch buffer mode enable
  698. \param[in] dma_periph: DMAx(x=0,1)
  699. \arg DMAx(x=0,1)
  700. \param[in] channelx: specify which DMA channel
  701. \arg DMA_CHx(x=0..7)
  702. \param[in] newvalue: ENABLE or DISABLE
  703. \param[out] none
  704. \retval none
  705. */
  706. void dma_switch_buffer_mode_enable(uint32_t dma_periph,dma_channel_enum channelx,ControlStatus newvalue)
  707. {
  708. if(ENABLE == newvalue){
  709. /* switch buffer mode enable */
  710. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_SBMEN;
  711. }else{
  712. /* switch buffer mode disable */
  713. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_SBMEN;
  714. }
  715. }
  716. /*!
  717. \brief DMA using memory get
  718. \param[in] dma_periph: DMAx(x=0,1)
  719. \arg DMAx(x=0,1)
  720. \param[in] channelx: specify which DMA channel
  721. \arg DMA_CHx(x=0..7)
  722. \param[out] none
  723. \retval the using memory
  724. */
  725. uint32_t dma_using_memory_get(uint32_t dma_periph,dma_channel_enum channelx)
  726. {
  727. if((DMA_CHCTL(dma_periph,channelx)) & DMA_CHXCTL_MBS){
  728. return DMA_MEMORY_1;
  729. }else{
  730. return DMA_MEMORY_0;
  731. }
  732. }
  733. /*!
  734. \brief DMA flow controller configure
  735. \param[in] dma_periph: DMAx(x=0,1)
  736. \arg DMAx(x=0,1)
  737. \param[in] channelx: specify which DMA channel
  738. \arg DMA_CHx(x=0..7)
  739. \param[in] controller: specify DMA flow controler
  740. \arg DMA_FLOW_CONTROLLER_DMA: DMA is the flow controller
  741. \arg DMA_FLOW_CONTROLLER_PERI: peripheral is the flow controller
  742. \param[out] none
  743. \retval none
  744. */
  745. void dma_flow_controller_config(uint32_t dma_periph,dma_channel_enum channelx,uint32_t controller)
  746. {
  747. if(DMA_FLOW_CONTROLLER_DMA == controller){
  748. DMA_CHCTL(dma_periph,channelx) &= ~DMA_CHXCTL_TFCS;
  749. }else{
  750. DMA_CHCTL(dma_periph,channelx) |= DMA_CHXCTL_TFCS;
  751. }
  752. }
  753. /*!
  754. \brief DMA FIFO status get
  755. \param[in] dma_periph: DMAx(x=0,1)
  756. \arg DMAx(x=0,1)
  757. \param[in] channelx: specify which DMA channel
  758. \arg DMA_CHx(x=0..7)
  759. \param[out] none
  760. \retval the using memory
  761. */
  762. uint32_t dma_fifo_status_get(uint32_t dma_periph,dma_channel_enum channelx)
  763. {
  764. return (DMA_CHFCTL(dma_periph,channelx) & DMA_CHXFCTL_FCNT);
  765. }