lpc17xx_gpdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. *
  35. * Parts taken from lpc177x_8x_gpdma.c 2011-06-02
  36. *
  37. * file lpc177x_8x_gpdma.c
  38. * brief Contains all functions support for GPDMA firmware library
  39. * on LPC177x_8x
  40. * version 1.0
  41. * date 02. June. 2011
  42. * author NXP MCU SW Application Team
  43. *
  44. * Copyright(C) 2011, NXP Semiconductor
  45. * All rights reserved.
  46. *
  47. ***********************************************************************
  48. * Software that is described herein is for illustrative purposes only
  49. * which provides customers with programming information regarding the
  50. * products. This software is supplied "AS IS" without any warranties.
  51. * NXP Semiconductors assumes no responsibility or liability for the
  52. * use of the software, conveys no license or title under any patent,
  53. * copyright, or mask work right to the product. NXP Semiconductors
  54. * reserves the right to make changes in the software without
  55. * notification. NXP Semiconductors also make no representation or
  56. * warranty that such application will be suitable for the specified
  57. * use without further testing or modification.
  58. * Permission to use, copy, modify, and distribute this software and its
  59. * documentation is hereby granted, under NXP Semiconductors'
  60. * relevant copyright in the software, without fee, provided that it
  61. * is used in conjunction with NXP Semiconductors microcontrollers. This
  62. * copyright, permission, and disclaimer notice must appear in all copies of
  63. * this code.
  64. **********************************************************************/
  65. #include <inttypes.h>
  66. #include <cfg/arch.h>
  67. #include <arch/cm3.h>
  68. #include <dev/irqreg.h>
  69. #if defined(MCU_LPC176x)
  70. #include <arch/cm3/nxp/lpc176x.h>
  71. #include <arch/cm3/nxp/lpc176x_clk.h>
  72. #elif defined(MCU_LPC177x_8x)
  73. #include <arch/cm3/nxp/lpc177x_8x.h>
  74. #include <arch/cm3/nxp/lpc177x_8x_clk.h>
  75. #elif defined(MCU_LPC407x_8x)
  76. #include <arch/cm3/nxp/lpc407x_8x.h>
  77. #include <arch/cm3/nxp/lpc407x_8x_clk.h>
  78. #else
  79. #warning "Unknown LPC familiy"
  80. #endif
  81. #include <arch/cm3/nxp/lpc17xx_gpdma.h>
  82. static int initialised = 0;
  83. /*!
  84. * \brief Lookup Table of Connection Type matched with
  85. * Peripheral Data (FIFO) register base address
  86. */
  87. volatile const void *GPDMA_LUTPerAddr[] = {
  88. [GPDMA_CONN_SSP0_Tx] = (&LPC_SSP0->DR), // SSP0 Tx
  89. [GPDMA_CONN_SSP0_Rx] = (&LPC_SSP0->DR), // SSP0 Rx
  90. [GPDMA_CONN_SSP1_Tx] = (&LPC_SSP1->DR), // SSP1 Tx
  91. [GPDMA_CONN_SSP1_Rx] = (&LPC_SSP1->DR), // SSP1 Rx
  92. [GPDMA_CONN_UART0_Tx] = (&LPC_UART0->THR), // UART0 Tx
  93. [GPDMA_CONN_UART0_Rx] = (&LPC_UART0->RBR), // UART0 Rx
  94. [GPDMA_CONN_UART1_Tx] = (&LPC_UART1->THR), // UART1 Tx
  95. [GPDMA_CONN_UART1_Rx] = (&LPC_UART1->RBR), // UART1 Rx
  96. [GPDMA_CONN_UART2_Tx] = (&LPC_UART2->THR), // UART2 Tx
  97. [GPDMA_CONN_UART2_Rx] = (&LPC_UART2->RBR), // UART2 Rx
  98. [GPDMA_CONN_UART3_Tx] = (&LPC_UART3->THR), // UART3 Tx
  99. [GPDMA_CONN_UART3_Rx] = (&LPC_UART3->RBR), // UART3 Rx
  100. [GPDMA_CONN_ADC] = (&LPC_ADC->GDR), // ADC
  101. [GPDMA_CONN_DAC] = (&LPC_DAC->CR), // DAC
  102. [GPDMA_CONN_I2S_Channel_0] = (&LPC_I2S->TXFIFO), // I2S Channel 0
  103. [GPDMA_CONN_I2S_Channel_1] = (&LPC_I2S->RXFIFO), // I2S Channel 1
  104. [GPDMA_CONN_MAT0_0] = (&LPC_TIM0->MR0), // MAT0.0
  105. [GPDMA_CONN_MAT0_1] = (&LPC_TIM0->MR1), // MAT0.1
  106. [GPDMA_CONN_MAT1_0] = (&LPC_TIM1->MR0), // MAT1.0
  107. [GPDMA_CONN_MAT1_1] = (&LPC_TIM1->MR1), // MAT1.1
  108. [GPDMA_CONN_MAT2_0] = (&LPC_TIM2->MR0), // MAT2.0
  109. [GPDMA_CONN_MAT2_1] = (&LPC_TIM2->MR1), // MAT2.1
  110. [GPDMA_CONN_MAT3_0] = (&LPC_TIM3->MR0), // MAT3.0
  111. [GPDMA_CONN_MAT3_1] = (&LPC_TIM3->MR1), // MAT3.1
  112. #if defined(MCU_LPC177x_8x) || defined(MCU_LPC407x_8x)
  113. [GPDMA_CONN_MCI] = (&LPC_MCI->FIFO), // SD Card
  114. [GPDMA_CONN_SSP2_Tx] = (&LPC_SSP2->DR), // SSP2 Tx
  115. [GPDMA_CONN_SSP2_Rx] = (&LPC_SSP2->DR), // SSP2 Rx
  116. [GPDMA_CONN_UART4_Tx] = (&LPC_UART4->THR), // UART4 Tx
  117. [GPDMA_CONN_UART4_Rx] = (&LPC_UART4->RBR), // UART4 Rx
  118. #endif
  119. };
  120. /*!
  121. * \brief Lookup Table of GPDMA Channel Number matched with
  122. * GPDMA channel pointer
  123. */
  124. const LPC_GPDMACH_TypeDef *pGPDMACh[GPDMA_NUM_CHANNELS] = {
  125. LPC_GPDMACH0, // GPDMA Channel 0
  126. LPC_GPDMACH1, // GPDMA Channel 1
  127. LPC_GPDMACH2, // GPDMA Channel 2
  128. LPC_GPDMACH3, // GPDMA Channel 3
  129. LPC_GPDMACH4, // GPDMA Channel 4
  130. LPC_GPDMACH5, // GPDMA Channel 5
  131. LPC_GPDMACH6, // GPDMA Channel 6
  132. LPC_GPDMACH7, // GPDMA Channel 7
  133. };
  134. /*!
  135. * \brief Optimized Peripheral Source and Destination burst size
  136. */
  137. const uint8_t GPDMA_LUTPerBurst[] = {
  138. [GPDMA_CONN_SSP0_Tx] = GPDMA_BSIZE_4, // SSP0 Tx
  139. [GPDMA_CONN_SSP0_Rx] = GPDMA_BSIZE_4, // SSP0 Rx
  140. [GPDMA_CONN_SSP1_Tx] = GPDMA_BSIZE_4, // SSP1 Tx
  141. [GPDMA_CONN_SSP1_Rx] = GPDMA_BSIZE_4, // SSP1 Rx
  142. [GPDMA_CONN_UART0_Tx] = GPDMA_BSIZE_1, // UART0 Tx
  143. [GPDMA_CONN_UART0_Rx] = GPDMA_BSIZE_1, // UART0 Rx
  144. [GPDMA_CONN_UART1_Tx] = GPDMA_BSIZE_1, // UART1 Tx
  145. [GPDMA_CONN_UART1_Rx] = GPDMA_BSIZE_1, // UART1 Rx
  146. [GPDMA_CONN_UART2_Tx] = GPDMA_BSIZE_1, // UART2 Tx
  147. [GPDMA_CONN_UART2_Rx] = GPDMA_BSIZE_1, // UART2 Rx
  148. [GPDMA_CONN_UART3_Tx] = GPDMA_BSIZE_1, // UART3 Tx
  149. [GPDMA_CONN_UART3_Rx] = GPDMA_BSIZE_1, // UART3 Rx
  150. [GPDMA_CONN_ADC] = GPDMA_BSIZE_1, // ADC
  151. [GPDMA_CONN_DAC] = GPDMA_BSIZE_1, // DAC
  152. [GPDMA_CONN_I2S_Channel_0] = GPDMA_BSIZE_32, // I2S channel 0
  153. [GPDMA_CONN_I2S_Channel_1] = GPDMA_BSIZE_32, // I2S channel 1
  154. [GPDMA_CONN_MAT0_0] = GPDMA_BSIZE_1, // MAT0.0
  155. [GPDMA_CONN_MAT0_1] = GPDMA_BSIZE_1, // MAT0.1
  156. [GPDMA_CONN_MAT1_0] = GPDMA_BSIZE_1, // MAT1.0
  157. [GPDMA_CONN_MAT1_1] = GPDMA_BSIZE_1, // MAT1.1
  158. [GPDMA_CONN_MAT2_0] = GPDMA_BSIZE_1, // MAT2.0
  159. [GPDMA_CONN_MAT2_1] = GPDMA_BSIZE_1, // MAT2.1
  160. [GPDMA_CONN_MAT3_0] = GPDMA_BSIZE_1, // MAT3.0
  161. [GPDMA_CONN_MAT3_1] = GPDMA_BSIZE_1, // MAT3.1
  162. #if defined(MCU_LPC177x_8x) || defined(MCU_LPC407x_8x)
  163. [GPDMA_CONN_MCI] = GPDMA_BSIZE_8, // SD Card
  164. [GPDMA_CONN_SSP2_Tx] = GPDMA_BSIZE_4, // SSP2 Tx
  165. [GPDMA_CONN_SSP2_Rx] = GPDMA_BSIZE_4, // SSP2 Rx
  166. [GPDMA_CONN_UART4_Tx] = GPDMA_BSIZE_1, // UART4 Tx
  167. [GPDMA_CONN_UART4_Rx] = GPDMA_BSIZE_1, // UART4 Rx
  168. #endif
  169. };
  170. /*!
  171. * \brief Optimized Peripheral Source and Destination transfer width
  172. */
  173. const uint8_t GPDMA_LUTPerWid[] = {
  174. [GPDMA_CONN_SSP0_Tx] = GPDMA_WIDTH_BYTE, // SSP0 Tx
  175. [GPDMA_CONN_SSP0_Rx] = GPDMA_WIDTH_BYTE, // SSP0 Rx
  176. [GPDMA_CONN_SSP1_Tx] = GPDMA_WIDTH_BYTE, // SSP1 Tx
  177. [GPDMA_CONN_SSP1_Rx] = GPDMA_WIDTH_BYTE, // SSP1 Rx
  178. [GPDMA_CONN_UART0_Tx] = GPDMA_WIDTH_BYTE, // UART0 Tx
  179. [GPDMA_CONN_UART0_Rx] = GPDMA_WIDTH_BYTE, // UART0 Rx
  180. [GPDMA_CONN_UART1_Tx] = GPDMA_WIDTH_BYTE, // UART1 Tx
  181. [GPDMA_CONN_UART1_Rx] = GPDMA_WIDTH_BYTE, // UART1 Rx
  182. [GPDMA_CONN_UART2_Tx] = GPDMA_WIDTH_BYTE, // UART2 Tx
  183. [GPDMA_CONN_UART2_Rx] = GPDMA_WIDTH_BYTE, // UART2 Rx
  184. [GPDMA_CONN_UART3_Tx] = GPDMA_WIDTH_BYTE, // UART3 Tx
  185. [GPDMA_CONN_UART3_Rx] = GPDMA_WIDTH_BYTE, // UART3 Rx
  186. [GPDMA_CONN_ADC] = GPDMA_WIDTH_WORD, // ADC
  187. [GPDMA_CONN_DAC] = GPDMA_WIDTH_BYTE, // DAC
  188. [GPDMA_CONN_I2S_Channel_0] = GPDMA_WIDTH_WORD, // I2S channel 0
  189. [GPDMA_CONN_I2S_Channel_1] = GPDMA_WIDTH_WORD, // I2S channel 1
  190. [GPDMA_CONN_MAT0_0] = GPDMA_WIDTH_WORD, // MAT0.0
  191. [GPDMA_CONN_MAT0_1] = GPDMA_WIDTH_WORD, // MAT0.1
  192. [GPDMA_CONN_MAT1_0] = GPDMA_WIDTH_WORD, // MAT1.0
  193. [GPDMA_CONN_MAT1_1] = GPDMA_WIDTH_WORD, // MAT1.1
  194. [GPDMA_CONN_MAT2_0] = GPDMA_WIDTH_WORD, // MAT2.0
  195. [GPDMA_CONN_MAT2_1] = GPDMA_WIDTH_WORD, // MAT2.1
  196. [GPDMA_CONN_MAT3_0] = GPDMA_WIDTH_WORD, // MAT3.0
  197. [GPDMA_CONN_MAT3_1] = GPDMA_WIDTH_WORD, // MAT3.1
  198. #if defined(MCU_LPC177x_8x) || defined(MCU_LPC407x_8x)
  199. [GPDMA_CONN_MCI] = GPDMA_WIDTH_WORD, // SD Card
  200. [GPDMA_CONN_SSP2_Tx] = GPDMA_WIDTH_BYTE, // SSP2 Tx
  201. [GPDMA_CONN_SSP2_Rx] = GPDMA_WIDTH_BYTE, // SSP2 Rx
  202. [GPDMA_CONN_UART4_Tx] = GPDMA_WIDTH_BYTE, // UART4 Tx
  203. [GPDMA_CONN_UART4_Rx] = GPDMA_WIDTH_BYTE, // UART4 Rx
  204. #endif
  205. };
  206. /* Channel info struct */
  207. static gpdma_vector_t GPDMA_Vector[GPDMA_NUM_CHANNELS];
  208. /*!
  209. * \brief Generic interrupt handler
  210. *
  211. * Generic interrupt handler will call any application specific handler
  212. * if one is registered. Application handlers are registered using Lpc17xxGPDMA_Setup
  213. *
  214. */
  215. void Lpc17xxGPDMA_DMA_IRQHandler(void *arg)
  216. {
  217. int ch;
  218. uint32_t int_stat = LPC_GPDMA->IntStat;
  219. uint32_t int_tc_stat = LPC_GPDMA->IntTCStat;
  220. uint32_t int_err_stat = LPC_GPDMA->IntErrStat;
  221. uint32_t status;
  222. ch = 0;
  223. while (int_stat) {
  224. status = 0;
  225. if (int_stat & 0x01) {
  226. if (int_tc_stat & 0x01) {
  227. status |= GPDMA_STAT_INTTC;
  228. LPC_GPDMA->IntTCClear = 1 << ch;
  229. }
  230. if (int_err_stat & 0x01) {
  231. status |= GPDMA_STAT_INTERR;
  232. LPC_GPDMA->IntErrClr = 1 << ch;
  233. }
  234. if (GPDMA_Vector[ch].handler != NULL) {
  235. GPDMA_Vector[ch].handler(ch, status, GPDMA_Vector[ch].arg);
  236. }
  237. int_stat >>= 1;
  238. int_tc_stat >>= 1;
  239. int_err_stat >>= 1;
  240. ch++;
  241. }
  242. }
  243. }
  244. /*!
  245. * \brief Initialize GPDMA controller
  246. *
  247. * \param none
  248. *
  249. * \return 0 on success, -1 in case of an error (DMA IRQ could not be registered)
  250. */
  251. int Lpc17xxGPDMA_Init(void)
  252. {
  253. int ch;
  254. if (!initialised) {
  255. /* Enable GPDMA clock */
  256. SysCtlPeripheralClkEnable(CLKPWR_PCONP_PCGPDMA);
  257. /* Reset all channel configuration register */
  258. LPC_GPDMACH0->CConfig = 0;
  259. LPC_GPDMACH1->CConfig = 0;
  260. LPC_GPDMACH2->CConfig = 0;
  261. LPC_GPDMACH3->CConfig = 0;
  262. LPC_GPDMACH4->CConfig = 0;
  263. LPC_GPDMACH5->CConfig = 0;
  264. LPC_GPDMACH6->CConfig = 0;
  265. LPC_GPDMACH7->CConfig = 0;
  266. /* Clear all DMA interrupt and error flag */
  267. LPC_GPDMA->IntTCClear = 0xFF;
  268. LPC_GPDMA->IntErrClr = 0xFF;
  269. for (ch = 0; ch < GPDMA_NUM_CHANNELS; ch++) {
  270. GPDMA_Vector[ch].handler = NULL;
  271. GPDMA_Vector[ch].arg = NULL;
  272. }
  273. /* Register the generic DMA IRQ handler */
  274. if (NutRegisterIrqHandler(&sig_DMA, Lpc17xxGPDMA_DMA_IRQHandler, NULL) != 0) {
  275. return -1;
  276. }
  277. NutIrqEnable(&sig_DMA);
  278. initialised = 1;
  279. }
  280. return 0;
  281. }
  282. /*!
  283. * \brief Setup GPDMA channel
  284. *
  285. * Setup GPDMA channel peripheral according to the specified
  286. * parameters in the ch_config.
  287. *
  288. * \param ch_config Pointer to a gpdma_channel_cfg_t structure that
  289. * contains the configuration information for the specified
  290. * GPDMA channel peripheral.
  291. *
  292. * \return 0 on success, -1 in case of an error
  293. */
  294. int Lpc17xxGPDMA_Setup(gpdma_channel_cfg_t *ch_config, void (*handler) (int ch, uint32_t status, void *), void* arg)
  295. {
  296. LPC_GPDMACH_TypeDef *pDMAch;
  297. uint32_t tmp1, tmp2;
  298. if ((ch_config == NULL) || (ch_config->ch >= GPDMA_NUM_CHANNELS)) {
  299. return -1;
  300. }
  301. if (LPC_GPDMA->EnbldChns & (GPDMA_DMACEnbldChns_Ch(ch_config->ch))) {
  302. /* Channel was enabled before. Need to release the channel first. Return an error. */
  303. return -1;
  304. }
  305. /* Get channel pointer */
  306. pDMAch = (LPC_GPDMACH_TypeDef *) pGPDMACh[ch_config->ch];
  307. /* Reset the interrupt status */
  308. LPC_GPDMA->IntTCClear = GPDMA_DMACIntTCClear_Ch(ch_config->ch);
  309. LPC_GPDMA->IntErrClr = GPDMA_DMACIntErrClr_Ch(ch_config->ch);
  310. /* Clear DMA config */
  311. pDMAch->CControl = 0x00;
  312. pDMAch->CConfig = 0x00;
  313. /* Assign Linker List Item value */
  314. pDMAch->CLLI = ch_config->dma_lli;
  315. /* Set value to Channel Control Registers */
  316. switch (ch_config->transfer_type) {
  317. /* Memory to memory */
  318. case GPDMA_TRANSFERTYPE_M2M:
  319. /* Assign physical source and destination address */
  320. pDMAch->CSrcAddr = ch_config->src_addr;
  321. pDMAch->CDestAddr = ch_config->dst_addr;
  322. pDMAch->CControl = GPDMA_DMACCxControl_TransferSize(ch_config->transfer_size) |
  323. GPDMA_DMACCxControl_SBSize(GPDMA_BSIZE_32) |
  324. GPDMA_DMACCxControl_DBSize(GPDMA_BSIZE_32) |
  325. GPDMA_DMACCxControl_SWidth(ch_config->transfer_width) |
  326. GPDMA_DMACCxControl_DWidth(ch_config->transfer_width) |
  327. GPDMA_DMACCxControl_SI |
  328. GPDMA_DMACCxControl_DI |
  329. GPDMA_DMACCxControl_I;
  330. break;
  331. /* Memory to peripheral */
  332. case GPDMA_TRANSFERTYPE_M2P:
  333. case GPDMA_TRANSFERTYPE_M2P_DEST_CTRL:
  334. /* Assign physical source */
  335. pDMAch->CSrcAddr = ch_config->src_addr;
  336. /* Assign peripheral destination address */
  337. pDMAch->CDestAddr = (uint32_t)GPDMA_LUTPerAddr[ch_config->dst_conn];
  338. pDMAch->CControl = GPDMA_DMACCxControl_TransferSize((uint32_t)ch_config->transfer_size) |
  339. GPDMA_DMACCxControl_SBSize((uint32_t)GPDMA_LUTPerBurst[ch_config->dst_conn]) |
  340. GPDMA_DMACCxControl_DBSize((uint32_t)GPDMA_LUTPerBurst[ch_config->dst_conn]) |
  341. GPDMA_DMACCxControl_SWidth((uint32_t)GPDMA_LUTPerWid[ch_config->dst_conn]) |
  342. GPDMA_DMACCxControl_DWidth((uint32_t)GPDMA_LUTPerWid[ch_config->dst_conn]) |
  343. GPDMA_DMACCxControl_SI |
  344. GPDMA_DMACCxControl_I;
  345. break;
  346. /* Peripheral to memory */
  347. case GPDMA_TRANSFERTYPE_P2M:
  348. case GPDMA_TRANSFERTYPE_P2M_SRC_CTRL:
  349. /* Assign peripheral source address */
  350. pDMAch->CSrcAddr = (uint32_t)GPDMA_LUTPerAddr[ch_config->src_conn];
  351. /* Assign memory destination address */
  352. pDMAch->CDestAddr = ch_config->dst_addr;
  353. pDMAch->CControl = GPDMA_DMACCxControl_TransferSize((uint32_t)ch_config->transfer_size) |
  354. GPDMA_DMACCxControl_SBSize((uint32_t)GPDMA_LUTPerBurst[ch_config->src_conn]) |
  355. GPDMA_DMACCxControl_DBSize((uint32_t)GPDMA_LUTPerBurst[ch_config->src_conn]) |
  356. GPDMA_DMACCxControl_SWidth((uint32_t)GPDMA_LUTPerWid[ch_config->src_conn]) |
  357. GPDMA_DMACCxControl_DWidth((uint32_t)GPDMA_LUTPerWid[ch_config->src_conn]) |
  358. GPDMA_DMACCxControl_DI |
  359. GPDMA_DMACCxControl_I;
  360. break;
  361. /* Peripheral to peripheral */
  362. case GPDMA_TRANSFERTYPE_P2P:
  363. /* Assign peripheral source address */
  364. pDMAch->CSrcAddr = (uint32_t)GPDMA_LUTPerAddr[ch_config->src_conn];
  365. /* Assign peripheral destination address */
  366. pDMAch->CDestAddr = (uint32_t)GPDMA_LUTPerAddr[ch_config->dst_conn];
  367. pDMAch->CControl = GPDMA_DMACCxControl_TransferSize((uint32_t)ch_config->transfer_size) |
  368. GPDMA_DMACCxControl_SBSize((uint32_t)GPDMA_LUTPerBurst[ch_config->src_conn]) |
  369. GPDMA_DMACCxControl_DBSize((uint32_t)GPDMA_LUTPerBurst[ch_config->dst_conn]) |
  370. GPDMA_DMACCxControl_SWidth((uint32_t)GPDMA_LUTPerWid[ch_config->src_conn]) |
  371. GPDMA_DMACCxControl_DWidth((uint32_t)GPDMA_LUTPerWid[ch_config->dst_conn]) |
  372. GPDMA_DMACCxControl_I;
  373. break;
  374. /* Unsupported transfer type, return an error */
  375. default:
  376. return -1;
  377. }
  378. /* Re-Configure DMA Request Select for source peripheral */
  379. if((ch_config->src_conn != 8)&&(ch_config->src_conn != 9)) {
  380. if (ch_config->src_conn > 15) {
  381. LPC_SC->DMAREQSEL |= (1 << (ch_config->src_conn - 16));
  382. } else {
  383. LPC_SC->DMAREQSEL &= ~(1 << (ch_config->src_conn));
  384. }
  385. }
  386. /* Re-Configure DMA Request Select for Destination peripheral */
  387. if((ch_config->dst_conn != 8) && (ch_config->dst_conn != 9)) {
  388. if (ch_config->dst_conn > 15) {
  389. LPC_SC->DMAREQSEL |= (1 << (ch_config->dst_conn - 16));
  390. } else {
  391. LPC_SC->DMAREQSEL &= ~(1 << (ch_config->dst_conn));
  392. }
  393. }
  394. /* Enable DMA channels, little endian */
  395. LPC_GPDMA->Config = GPDMA_DMACConfig_E;
  396. while (!(LPC_GPDMA->Config & GPDMA_DMACConfig_E));
  397. /* Calculate absolute value for Connection number */
  398. tmp1 = ch_config->src_conn;
  399. tmp1 = ((tmp1 > 15) ? (tmp1 - 16) : tmp1);
  400. tmp2 = ch_config->dst_conn;
  401. tmp2 = ((tmp2 > 15) ? (tmp2 - 16) : tmp2);
  402. /* Configure DMA Channel, enable Error Counter and Terminate counter */
  403. pDMAch->CConfig = GPDMA_DMACCxConfig_IE | GPDMA_DMACCxConfig_ITC | /* GPDMA_DMACCxConfig_E |*/
  404. GPDMA_DMACCxConfig_TransferType((uint32_t)ch_config->transfer_type) |
  405. GPDMA_DMACCxConfig_SrcPeripheral(tmp1) |
  406. GPDMA_DMACCxConfig_DestPeripheral(tmp2);
  407. /* Configure the channel info struct */
  408. GPDMA_Vector[ch_config->ch].handler = handler;
  409. GPDMA_Vector[ch_config->ch].arg = arg;
  410. return 0;
  411. }
  412. /*!
  413. * \brief Enable/Disable DMA channel
  414. *
  415. * Setup GPDMA channel peripheral according to the specified
  416. * parameters in the ch_config.
  417. *
  418. * \param ch GPDMA channel, should be in range from 0 to 7
  419. * \param enabled New state of this channel: 1: enabled, 0: disabled
  420. *
  421. * \return none
  422. */
  423. void Lpc17xxGPDMA_ChannelCmd(uint8_t ch, int enabled)
  424. {
  425. LPC_GPDMACH_TypeDef *pDMAch;
  426. /* Get Channel pointer */
  427. pDMAch = (LPC_GPDMACH_TypeDef *) pGPDMACh[ch];
  428. if (enabled) {
  429. pDMAch->CConfig |= GPDMA_DMACCxConfig_E;
  430. } else {
  431. pDMAch->CConfig &= ~GPDMA_DMACCxConfig_E;
  432. }
  433. }
  434. /*!
  435. * \brief Check interrupt status
  436. *
  437. * Check if corresponding channel does have an active interrupt request or not
  438. *
  439. * \param type type of status, should be:
  440. * - GPDMA_STAT_INT: GPDMA Interrupt Status
  441. * - GPDMA_STAT_INTTC: GPDMA Interrupt Terminal Count Request Status
  442. * - GPDMA_STAT_INTERR: GPDMA Interrupt Error Status
  443. * - GPDMA_STAT_RAWINTTC: GPDMA Raw Interrupt Terminal Count Status
  444. * - GPDMA_STAT_RAWINTERR: GPDMA Raw Error Interrupt Status
  445. * - GPDMA_STAT_ENABLED_CH:GPDMA Enabled Channel Status
  446. *
  447. * \param ch GPDMA channel, should be in range from 0 to 7
  448. *
  449. * \return status of DMA channel interrupt after masking
  450. * 1: the corresponding channel has no active interrupt request
  451. * 0: the corresponding channel does have an active interrupt request
  452. */
  453. int Lpc17xxGPDMA_IntGetStatus(gpdma_status_t type, uint8_t ch)
  454. {
  455. switch (type) {
  456. /* check status of DMA channel interrupts */
  457. case GPDMA_STAT_INT:
  458. if (LPC_GPDMA->IntStat & (GPDMA_DMACIntStat_Ch(ch))) {
  459. return 1;
  460. }
  461. return 0;
  462. /* check terminal count interrupt request status for DMA */
  463. case GPDMA_STAT_INTTC:
  464. if (LPC_GPDMA->IntTCStat & GPDMA_DMACIntTCStat_Ch(ch)) {
  465. return 1;
  466. }
  467. return 0;
  468. /* check interrupt status for DMA channels */
  469. case GPDMA_STAT_INTERR:
  470. if (LPC_GPDMA->IntErrStat & GPDMA_DMACIntTCClear_Ch(ch)) {
  471. return 1;
  472. }
  473. return 0;
  474. /* check status of the terminal count interrupt for DMA channels */
  475. case GPDMA_STAT_RAWINTTC:
  476. if (LPC_GPDMA->RawIntErrStat & GPDMA_DMACRawIntTCStat_Ch(ch)) {
  477. return 1;
  478. }
  479. return 0;
  480. /* check status of the error interrupt for DMA channels */
  481. case GPDMA_STAT_RAWINTERR:
  482. if (LPC_GPDMA->RawIntTCStat & GPDMA_DMACRawIntErrStat_Ch(ch)) {
  483. return 1;
  484. }
  485. return 0;
  486. /* check enable status for DMA channels */
  487. default:
  488. if (LPC_GPDMA->EnbldChns & GPDMA_DMACEnbldChns_Ch(ch)) {
  489. return 1;
  490. }
  491. return 0;
  492. }
  493. }
  494. /*!
  495. * \brief Clear one or more interrupt requests on DMA channels
  496. *
  497. * \param type type of status, should be:
  498. * - GPDMA_STATCLR_INTTC: GPDMA Interrupt Terminal Count Request Clear
  499. * - GPDMA_STATCLR_INTERR: GPDMA Interrupt Error Clear
  500. *
  501. * \param ch GPDMA channel, should be in range from 0 to 7
  502. *
  503. * \return none
  504. */
  505. void Lpc17xxGPDMA_ClearIntPending(gpdma_state_clear_t type, uint8_t ch)
  506. {
  507. if (type == GPDMA_STATCLR_INTTC) {
  508. /* clears the terminal count interrupt request on DMA channel */
  509. LPC_GPDMA->IntTCClear = GPDMA_DMACIntTCClear_Ch(ch);
  510. } else {
  511. /* clear the error interrupt request */
  512. LPC_GPDMA->IntErrClr = GPDMA_DMACIntErrClr_Ch(ch);
  513. }
  514. }