gd32f30x_dma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*!
  2. \file gd32f30x_dma.c
  3. \brief DMA driver
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32f30x_dma.h"
  30. #include <stdlib.h>
  31. #define DMA_WRONG_HANDLE while(1){}
  32. /* check whether peripheral matches channels or not */
  33. static ErrStatus dma_periph_and_channel_check(uint32_t dma_periph, dma_channel_enum channelx);
  34. /*!
  35. \brief deinitialize DMA a channel registers
  36. \param[in] dma_periph: DMAx(x=0,1)
  37. \arg DMAx(x=0,1)
  38. \param[in] channelx: specify which DMA channel is deinitialized
  39. only one parameter can be selected which is shown as below:
  40. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  41. \param[out] none
  42. \retval none
  43. */
  44. void dma_deinit(uint32_t dma_periph, dma_channel_enum channelx)
  45. {
  46. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  47. DMA_WRONG_HANDLE
  48. }
  49. /* disable DMA a channel */
  50. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_CHEN;
  51. /* reset DMA channel registers */
  52. DMA_CHCTL(dma_periph, channelx) = DMA_CHCTL_RESET_VALUE;
  53. DMA_CHCNT(dma_periph, channelx) = DMA_CHCNT_RESET_VALUE;
  54. DMA_CHPADDR(dma_periph, channelx) = DMA_CHPADDR_RESET_VALUE;
  55. DMA_CHMADDR(dma_periph, channelx) = DMA_CHMADDR_RESET_VALUE;
  56. DMA_INTC(dma_periph) |= DMA_FLAG_ADD(DMA_CHINTF_RESET_VALUE, channelx);
  57. }
  58. /*!
  59. \brief initialize the parameters of DMA struct with the default values
  60. \param[in] init_struct: the initialization data needed to initialize DMA channel
  61. \param[out] none
  62. \retval none
  63. */
  64. void dma_struct_para_init(dma_parameter_struct* init_struct)
  65. {
  66. if(NULL == init_struct){
  67. DMA_WRONG_HANDLE
  68. }
  69. /* set the DMA struct with the default values */
  70. init_struct->periph_addr = 0U;
  71. init_struct->periph_width = 0U;
  72. init_struct->periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  73. init_struct->memory_addr = 0U;
  74. init_struct->memory_width = 0U;
  75. init_struct->memory_inc = DMA_MEMORY_INCREASE_DISABLE;
  76. init_struct->number = 0U;
  77. init_struct->direction = DMA_PERIPHERAL_TO_MEMORY;
  78. init_struct->priority = DMA_PRIORITY_LOW;
  79. }
  80. /*!
  81. \brief initialize DMA channel
  82. \param[in] dma_periph: DMAx(x=0,1)
  83. \arg DMAx(x=0,1)
  84. \param[in] channelx: specify which DMA channel is initialized
  85. only one parameter can be selected which is shown as below:
  86. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  87. \param[in] init_struct: the data needed to initialize DMA channel
  88. periph_addr: peripheral base address
  89. periph_width: DMA_PERIPHERAL_WIDTH_8BIT, DMA_PERIPHERAL_WIDTH_16BIT, DMA_PERIPHERAL_WIDTH_32BIT
  90. periph_inc: DMA_PERIPH_INCREASE_ENABLE, DMA_PERIPH_INCREASE_DISABLE
  91. memory_addr: memory base address
  92. memory_width: DMA_MEMORY_WIDTH_8BIT, DMA_MEMORY_WIDTH_16BIT, DMA_MEMORY_WIDTH_32BIT
  93. memory_inc: DMA_MEMORY_INCREASE_ENABLE, DMA_MEMORY_INCREASE_DISABLE
  94. direction: DMA_PERIPHERAL_TO_MEMORY, DMA_MEMORY_TO_PERIPHERAL
  95. number: the number of remaining data to be transferred by the DMA
  96. priority: DMA_PRIORITY_LOW, DMA_PRIORITY_MEDIUM, DMA_PRIORITY_HIGH, DMA_PRIORITY_ULTRA_HIGH
  97. \param[out] none
  98. \retval none
  99. */
  100. void dma_init(uint32_t dma_periph, dma_channel_enum channelx, dma_parameter_struct* init_struct)
  101. {
  102. uint32_t ctl;
  103. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  104. DMA_WRONG_HANDLE
  105. }
  106. /* configure peripheral base address */
  107. DMA_CHPADDR(dma_periph, channelx) = init_struct->periph_addr;
  108. /* configure memory base address */
  109. DMA_CHMADDR(dma_periph, channelx) = init_struct->memory_addr;
  110. /* configure the number of remaining data to be transferred */
  111. DMA_CHCNT(dma_periph, channelx) = (init_struct->number & DMA_CHANNEL_CNT_MASK);
  112. /* configure peripheral transfer width,memory transfer width and priority */
  113. ctl = DMA_CHCTL(dma_periph, channelx);
  114. ctl &= ~(DMA_CHXCTL_PWIDTH | DMA_CHXCTL_MWIDTH | DMA_CHXCTL_PRIO);
  115. ctl |= (init_struct->periph_width | init_struct->memory_width | init_struct->priority);
  116. DMA_CHCTL(dma_periph, channelx) = ctl;
  117. /* configure peripheral increasing mode */
  118. if(DMA_PERIPH_INCREASE_ENABLE == init_struct->periph_inc){
  119. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_PNAGA;
  120. }else{
  121. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_PNAGA;
  122. }
  123. /* configure memory increasing mode */
  124. if(DMA_MEMORY_INCREASE_ENABLE == init_struct->memory_inc){
  125. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_MNAGA;
  126. }else{
  127. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_MNAGA;
  128. }
  129. /* configure the direction of data transfer */
  130. if(DMA_PERIPHERAL_TO_MEMORY == init_struct->direction){
  131. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_DIR;
  132. }else{
  133. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_DIR;
  134. }
  135. }
  136. /*!
  137. \brief enable DMA circulation mode
  138. \param[in] dma_periph: DMAx(x=0,1)
  139. \arg DMAx(x=0,1)
  140. \param[in] channelx: specify which DMA channel
  141. only one parameter can be selected which is shown as below:
  142. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  143. \param[out] none
  144. \retval none
  145. */
  146. void dma_circulation_enable(uint32_t dma_periph, dma_channel_enum channelx)
  147. {
  148. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  149. DMA_WRONG_HANDLE
  150. }
  151. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_CMEN;
  152. }
  153. /*!
  154. \brief disable DMA circulation mode
  155. \param[in] dma_periph: DMAx(x=0,1)
  156. \arg DMAx(x=0,1)
  157. \param[in] channelx: specify which DMA channel
  158. only one parameter can be selected which is shown as below:
  159. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  160. \param[out] none
  161. \retval none
  162. */
  163. void dma_circulation_disable(uint32_t dma_periph, dma_channel_enum channelx)
  164. {
  165. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  166. DMA_WRONG_HANDLE
  167. }
  168. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_CMEN;
  169. }
  170. /*!
  171. \brief enable memory to memory mode
  172. \param[in] dma_periph: DMAx(x=0,1)
  173. \arg DMAx(x=0,1)
  174. \param[in] channelx: specify which DMA channel
  175. only one parameter can be selected which is shown as below:
  176. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  177. \param[out] none
  178. \retval none
  179. */
  180. void dma_memory_to_memory_enable(uint32_t dma_periph, dma_channel_enum channelx)
  181. {
  182. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  183. DMA_WRONG_HANDLE
  184. }
  185. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_M2M;
  186. }
  187. /*!
  188. \brief disable memory to memory mode
  189. \param[in] dma_periph: DMAx(x=0,1)
  190. \arg DMAx(x=0,1)
  191. \param[in] channelx: specify which DMA channel
  192. only one parameter can be selected which is shown as below:
  193. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  194. \param[out] none
  195. \retval none
  196. */
  197. void dma_memory_to_memory_disable(uint32_t dma_periph, dma_channel_enum channelx)
  198. {
  199. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  200. DMA_WRONG_HANDLE
  201. }
  202. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_M2M;
  203. }
  204. /*!
  205. \brief enable DMA channel
  206. \param[in] dma_periph: DMAx(x=0,1)
  207. \arg DMAx(x=0,1)
  208. \param[in] channelx: specify which DMA channel
  209. only one parameter can be selected which is shown as below:
  210. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  211. \param[out] none
  212. \retval none
  213. */
  214. void dma_channel_enable(uint32_t dma_periph, dma_channel_enum channelx)
  215. {
  216. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  217. DMA_WRONG_HANDLE
  218. }
  219. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_CHEN;
  220. }
  221. /*!
  222. \brief disable DMA channel
  223. \param[in] dma_periph: DMAx(x=0,1)
  224. \arg DMAx(x=0,1)
  225. \param[in] channelx: specify which DMA channel
  226. only one parameter can be selected which is shown as below:
  227. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  228. \param[out] none
  229. \retval none
  230. */
  231. void dma_channel_disable(uint32_t dma_periph, dma_channel_enum channelx)
  232. {
  233. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  234. DMA_WRONG_HANDLE
  235. }
  236. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_CHEN;
  237. }
  238. /*!
  239. \brief set DMA peripheral base address
  240. \param[in] dma_periph: DMAx(x=0,1)
  241. \arg DMAx(x=0,1)
  242. \param[in] channelx: specify which DMA channel to set peripheral base address
  243. only one parameter can be selected which is shown as below:
  244. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  245. \param[in] address: peripheral base address
  246. \param[out] none
  247. \retval none
  248. */
  249. void dma_periph_address_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t address)
  250. {
  251. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  252. DMA_WRONG_HANDLE
  253. }
  254. DMA_CHPADDR(dma_periph, channelx) = address;
  255. }
  256. /*!
  257. \brief set DMA memory base address
  258. \param[in] dma_periph: DMAx(x=0,1)
  259. \arg DMAx(x=0,1)
  260. \param[in] channelx: specify which DMA channel to set memory base address
  261. only one parameter can be selected which is shown as below:
  262. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  263. \param[in] address: memory base address
  264. \param[out] none
  265. \retval none
  266. */
  267. void dma_memory_address_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t address)
  268. {
  269. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  270. DMA_WRONG_HANDLE
  271. }
  272. DMA_CHMADDR(dma_periph, channelx) = address;
  273. }
  274. /*!
  275. \brief set the number of remaining data to be transferred by the DMA
  276. \param[in] dma_periph: DMAx(x=0,1)
  277. \arg DMAx(x=0,1)
  278. \param[in] channelx: specify which DMA channel to set number
  279. only one parameter can be selected which is shown as below:
  280. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  281. \param[in] number: the number of remaining data to be transferred by the DMA
  282. \param[out] none
  283. \retval none
  284. */
  285. void dma_transfer_number_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t number)
  286. {
  287. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  288. DMA_WRONG_HANDLE
  289. }
  290. DMA_CHCNT(dma_periph, channelx) = (number & DMA_CHANNEL_CNT_MASK);
  291. }
  292. /*!
  293. \brief get the number of remaining data to be transferred by the DMA
  294. \param[in] dma_periph: DMAx(x=0,1)
  295. \arg DMAx(x=0,1)
  296. \param[in] channelx: specify which DMA channel to set number
  297. only one parameter can be selected which is shown as below:
  298. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  299. \param[out] none
  300. \retval uint32_t: the number of remaining data to be transferred by the DMA
  301. */
  302. uint32_t dma_transfer_number_get(uint32_t dma_periph, dma_channel_enum channelx)
  303. {
  304. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  305. DMA_WRONG_HANDLE
  306. }
  307. return (uint32_t)DMA_CHCNT(dma_periph, channelx);
  308. }
  309. /*!
  310. \brief configure priority level of DMA channel
  311. \param[in] dma_periph: DMAx(x=0,1)
  312. \arg DMAx(x=0,1)
  313. \param[in] channelx: specify which DMA channel
  314. only one parameter can be selected which is shown as below:
  315. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  316. \param[in] priority: priority Level of this channel
  317. only one parameter can be selected which is shown as below:
  318. \arg DMA_PRIORITY_LOW: low priority
  319. \arg DMA_PRIORITY_MEDIUM: medium priority
  320. \arg DMA_PRIORITY_HIGH: high priority
  321. \arg DMA_PRIORITY_ULTRA_HIGH: ultra high priority
  322. \param[out] none
  323. \retval none
  324. */
  325. void dma_priority_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t priority)
  326. {
  327. uint32_t ctl;
  328. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  329. DMA_WRONG_HANDLE
  330. }
  331. /* acquire DMA_CHxCTL register */
  332. ctl = DMA_CHCTL(dma_periph, channelx);
  333. /* assign regiser */
  334. ctl &= ~DMA_CHXCTL_PRIO;
  335. ctl |= priority;
  336. DMA_CHCTL(dma_periph, channelx) = ctl;
  337. }
  338. /*!
  339. \brief configure transfer data size of memory
  340. \param[in] dma_periph: DMAx(x=0,1)
  341. \arg DMAx(x=0,1)
  342. \param[in] channelx: specify which DMA channel
  343. only one parameter can be selected which is shown as below:
  344. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  345. \param[in] mwidth: transfer data width of memory
  346. only one parameter can be selected which is shown as below:
  347. \arg DMA_MEMORY_WIDTH_8BIT: transfer data width of memory is 8-bit
  348. \arg DMA_MEMORY_WIDTH_16BIT: transfer data width of memory is 16-bit
  349. \arg DMA_MEMORY_WIDTH_32BIT: transfer data width of memory is 32-bit
  350. \param[out] none
  351. \retval none
  352. */
  353. void dma_memory_width_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t mwidth)
  354. {
  355. uint32_t ctl;
  356. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  357. DMA_WRONG_HANDLE
  358. }
  359. /* acquire DMA_CHxCTL register */
  360. ctl = DMA_CHCTL(dma_periph, channelx);
  361. /* assign regiser */
  362. ctl &= ~DMA_CHXCTL_MWIDTH;
  363. ctl |= mwidth;
  364. DMA_CHCTL(dma_periph, channelx) = ctl;
  365. }
  366. /*!
  367. \brief configure transfer data size of peripheral
  368. \param[in] dma_periph: DMAx(x=0,1)
  369. \arg DMAx(x=0,1)
  370. \param[in] channelx: specify which DMA channel
  371. only one parameter can be selected which is shown as below:
  372. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  373. \param[in] pwidth: transfer data width of peripheral
  374. only one parameter can be selected which is shown as below:
  375. \arg DMA_PERIPHERAL_WIDTH_8BIT: transfer data width of peripheral is 8-bit
  376. \arg DMA_PERIPHERAL_WIDTH_16BIT: transfer data width of peripheral is 16-bit
  377. \arg DMA_PERIPHERAL_WIDTH_32BIT: transfer data width of peripheral is 32-bit
  378. \param[out] none
  379. \retval none
  380. */
  381. void dma_periph_width_config (uint32_t dma_periph, dma_channel_enum channelx, uint32_t pwidth)
  382. {
  383. uint32_t ctl;
  384. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  385. DMA_WRONG_HANDLE
  386. }
  387. /* acquire DMA_CHxCTL register */
  388. ctl = DMA_CHCTL(dma_periph, channelx);
  389. /* assign regiser */
  390. ctl &= ~DMA_CHXCTL_PWIDTH;
  391. ctl |= pwidth;
  392. DMA_CHCTL(dma_periph, channelx) = ctl;
  393. }
  394. /*!
  395. \brief enable next address increasement algorithm of memory
  396. \param[in] dma_periph: DMAx(x=0,1)
  397. \arg DMAx(x=0,1)
  398. \param[in] channelx: specify which DMA channel
  399. only one parameter can be selected which is shown as below:
  400. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  401. \param[out] none
  402. \retval none
  403. */
  404. void dma_memory_increase_enable(uint32_t dma_periph, dma_channel_enum channelx)
  405. {
  406. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  407. DMA_WRONG_HANDLE
  408. }
  409. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_MNAGA;
  410. }
  411. /*!
  412. \brief disable next address increasement algorithm of memory
  413. \param[in] dma_periph: DMAx(x=0,1)
  414. \arg DMAx(x=0,1)
  415. \param[in] channelx: specify which DMA channel
  416. only one parameter can be selected which is shown as below:
  417. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  418. \param[out] none
  419. \retval none
  420. */
  421. void dma_memory_increase_disable(uint32_t dma_periph, dma_channel_enum channelx)
  422. {
  423. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  424. DMA_WRONG_HANDLE
  425. }
  426. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_MNAGA;
  427. }
  428. /*!
  429. \brief enable next address increasement algorithm of peripheral
  430. \param[in] dma_periph: DMAx(x=0,1)
  431. \arg DMAx(x=0,1)
  432. \param[in] channelx: specify which DMA channel
  433. only one parameter can be selected which is shown as below:
  434. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  435. \param[out] none
  436. \retval none
  437. */
  438. void dma_periph_increase_enable(uint32_t dma_periph, dma_channel_enum channelx)
  439. {
  440. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  441. DMA_WRONG_HANDLE
  442. }
  443. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_PNAGA;
  444. }
  445. /*!
  446. \brief disable next address increasement algorithm of peripheral
  447. \param[in] dma_periph: DMAx(x=0,1)
  448. \arg DMAx(x=0,1)
  449. \param[in] channelx: specify which DMA channel
  450. only one parameter can be selected which is shown as below:
  451. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  452. \param[out] none
  453. \retval none
  454. */
  455. void dma_periph_increase_disable(uint32_t dma_periph, dma_channel_enum channelx)
  456. {
  457. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  458. DMA_WRONG_HANDLE
  459. }
  460. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_PNAGA;
  461. }
  462. /*!
  463. \brief configure the direction of data transfer on the channel
  464. \param[in] dma_periph: DMAx(x=0,1)
  465. \arg DMAx(x=0,1)
  466. \param[in] channelx: specify which DMA channel
  467. only one parameter can be selected which is shown as below:
  468. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  469. \param[in] direction: specify the direction of data transfer
  470. only one parameter can be selected which is shown as below:
  471. \arg DMA_PERIPHERAL_TO_MEMORY: read from peripheral and write to memory
  472. \arg DMA_MEMORY_TO_PERIPHERAL: read from memory and write to peripheral
  473. \param[out] none
  474. \retval none
  475. */
  476. void dma_transfer_direction_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t direction)
  477. {
  478. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  479. DMA_WRONG_HANDLE
  480. }
  481. if(DMA_PERIPHERAL_TO_MEMORY == direction){
  482. DMA_CHCTL(dma_periph, channelx) &= ~DMA_CHXCTL_DIR;
  483. } else {
  484. DMA_CHCTL(dma_periph, channelx) |= DMA_CHXCTL_DIR;
  485. }
  486. }
  487. /*!
  488. \brief check DMA flag is set or not
  489. \param[in] dma_periph: DMAx(x=0,1)
  490. \arg DMAx(x=0,1)
  491. \param[in] channelx: specify which DMA channel to get flag
  492. only one parameter can be selected which is shown as below:
  493. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  494. \param[in] flag: specify get which flag
  495. only one parameter can be selected which is shown as below:
  496. \arg DMA_FLAG_G: global interrupt flag of channel
  497. \arg DMA_FLAG_FTF: full transfer finish flag of channel
  498. \arg DMA_FLAG_HTF: half transfer finish flag of channel
  499. \arg DMA_FLAG_ERR: error flag of channel
  500. \param[out] none
  501. \retval FlagStatus: SET or RESET
  502. */
  503. FlagStatus dma_flag_get(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag)
  504. {
  505. FlagStatus reval;
  506. if(RESET != (DMA_INTF(dma_periph) & DMA_FLAG_ADD(flag, channelx))){
  507. reval = SET;
  508. }else{
  509. reval = RESET;
  510. }
  511. return reval;
  512. }
  513. /*!
  514. \brief clear DMA a channel flag
  515. \param[in] dma_periph: DMAx(x=0,1)
  516. \arg DMAx(x=0,1)
  517. \param[in] channelx: specify which DMA channel to clear flag
  518. only one parameter can be selected which is shown as below:
  519. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  520. \param[in] flag: specify get which flag
  521. only one parameter can be selected which is shown as below:
  522. \arg DMA_FLAG_G: global interrupt flag of channel
  523. \arg DMA_FLAG_FTF: full transfer finish flag of channel
  524. \arg DMA_FLAG_HTF: half transfer finish flag of channel
  525. \arg DMA_FLAG_ERR: error flag of channel
  526. \param[out] none
  527. \retval none
  528. */
  529. void dma_flag_clear(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag)
  530. {
  531. DMA_INTC(dma_periph) |= DMA_FLAG_ADD(flag, channelx);
  532. }
  533. /*!
  534. \brief check DMA flag and interrupt enable bit is set or not
  535. \param[in] dma_periph: DMAx(x=0,1)
  536. \arg DMAx(x=0,1)
  537. \param[in] channelx: specify which DMA channel to get flag
  538. only one parameter can be selected which is shown as below:
  539. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  540. \param[in] flag: specify get which flag
  541. only one parameter can be selected which is shown as below:
  542. \arg DMA_INT_FLAG_FTF: full transfer finish interrupt flag of channel
  543. \arg DMA_INT_FLAG_HTF: half transfer finish interrupt flag of channel
  544. \arg DMA_INT_FLAG_ERR: error interrupt flag of channel
  545. \param[out] none
  546. \retval FlagStatus: SET or RESET
  547. */
  548. FlagStatus dma_interrupt_flag_get(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag)
  549. {
  550. uint32_t interrupt_enable = 0U, interrupt_flag = 0U;
  551. switch(flag){
  552. case DMA_INT_FLAG_FTF:
  553. interrupt_flag = DMA_INTF(dma_periph) & DMA_FLAG_ADD(flag, channelx);
  554. interrupt_enable = DMA_CHCTL(dma_periph, channelx) & DMA_CHXCTL_FTFIE;
  555. break;
  556. case DMA_INT_FLAG_HTF:
  557. interrupt_flag = DMA_INTF(dma_periph) & DMA_FLAG_ADD(flag, channelx);
  558. interrupt_enable = DMA_CHCTL(dma_periph, channelx) & DMA_CHXCTL_HTFIE;
  559. break;
  560. case DMA_INT_FLAG_ERR:
  561. interrupt_flag = DMA_INTF(dma_periph) & DMA_FLAG_ADD(flag, channelx);
  562. interrupt_enable = DMA_CHCTL(dma_periph, channelx) & DMA_CHXCTL_ERRIE;
  563. break;
  564. default:
  565. DMA_WRONG_HANDLE
  566. }
  567. if(interrupt_flag && interrupt_enable){
  568. return SET;
  569. }else{
  570. return RESET;
  571. }
  572. }
  573. /*!
  574. \brief clear DMA a channel flag
  575. \param[in] dma_periph: DMAx(x=0,1)
  576. \arg DMAx(x=0,1)
  577. \param[in] channelx: specify which DMA channel to clear flag
  578. only one parameter can be selected which is shown as below:
  579. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  580. \param[in] flag: specify get which flag
  581. only one parameter can be selected which is shown as below:
  582. \arg DMA_INT_FLAG_G: global interrupt flag of channel
  583. \arg DMA_INT_FLAG_FTF: full transfer finish interrupt flag of channel
  584. \arg DMA_INT_FLAG_HTF: half transfer finish interrupt flag of channel
  585. \arg DMA_INT_FLAG_ERR: error interrupt flag of channel
  586. \param[out] none
  587. \retval none
  588. */
  589. void dma_interrupt_flag_clear(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag)
  590. {
  591. DMA_INTC(dma_periph) |= DMA_FLAG_ADD(flag, channelx);
  592. }
  593. /*!
  594. \brief enable DMA interrupt
  595. \param[in] dma_periph: DMAx(x=0,1)
  596. \arg DMAx(x=0,1)
  597. \param[in] channelx: specify which DMA channel
  598. only one parameter can be selected which is shown as below:
  599. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  600. \param[in] source: specify which interrupt to enbale
  601. one or more parameters can be selected which are shown as below
  602. \arg DMA_INT_FTF: channel full transfer finish interrupt
  603. \arg DMA_INT_HTF: channel half transfer finish interrupt
  604. \arg DMA_INT_ERR: channel error interrupt
  605. \param[out] none
  606. \retval none
  607. */
  608. void dma_interrupt_enable(uint32_t dma_periph, dma_channel_enum channelx, uint32_t source)
  609. {
  610. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  611. DMA_WRONG_HANDLE
  612. }
  613. DMA_CHCTL(dma_periph, channelx) |= source;
  614. }
  615. /*!
  616. \brief disable DMA interrupt
  617. \param[in] dma_periph: DMAx(x=0,1)
  618. \arg DMAx(x=0,1)
  619. \param[in] channelx: specify which DMA channel
  620. only one parameter can be selected which is shown as below:
  621. \arg DMA0: DMA_CHx(x=0..6), DMA1: DMA_CHx(x=0..4)
  622. \param[in] source: specify which interrupt to disbale
  623. one or more parameters can be selected which are shown as below
  624. \arg DMA_INT_FTF: channel full transfer finish interrupt
  625. \arg DMA_INT_HTF: channel half transfer finish interrupt
  626. \arg DMA_INT_ERR: channel error interrupt
  627. \param[out] none
  628. \retval none
  629. */
  630. void dma_interrupt_disable(uint32_t dma_periph, dma_channel_enum channelx, uint32_t source)
  631. {
  632. if(ERROR == dma_periph_and_channel_check(dma_periph, channelx)){
  633. DMA_WRONG_HANDLE
  634. }
  635. DMA_CHCTL(dma_periph, channelx) &= ~source;
  636. }
  637. /*!
  638. \brief check whether peripheral and channels match
  639. \param[in] dma_periph: DMAx(x=0,1)
  640. \arg DMAx(x=0,1)
  641. \param[in] channelx: specify which DMA channel
  642. only one parameter can be selected which is shown as below:
  643. \arg DMA_CHx(x=0..6)
  644. \param[out] none
  645. \retval none
  646. */
  647. static ErrStatus dma_periph_and_channel_check(uint32_t dma_periph, dma_channel_enum channelx)
  648. {
  649. ErrStatus val = SUCCESS;
  650. if(DMA1 == dma_periph){
  651. /* for DMA1, the channel is from DMA_CH0 to DMA_CH4 */
  652. if(channelx > DMA_CH4){
  653. val = ERROR;
  654. }
  655. }
  656. return val;
  657. }