hw_dmaaltd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * @brief DMA controller ROM API declarations and functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include "hw_dmaaltd.h"
  34. #define DRVVERSION 0x0100
  35. typedef PRE_PACK struct POST_PACK {
  36. uint32_t xfercfg; /*!< DMA Configuration register */
  37. uint32_t src; /*!< DMA source address */
  38. uint32_t dest; /*!< DMA destination address */
  39. ROM_DMA_DESC_T *pNextLink; /*!< Pointer to next descriptor link in a chain, NULL to end */
  40. } ROM_DMA_PRVXFERDESC_T;
  41. /* Private data structure used for the DMA controller driver, holds the driver and
  42. peripheral context */
  43. typedef struct {
  44. void *pUserData; /*!< Pointer to user data used by driver instance, use NULL if not used */
  45. LPC_DMA_T *base; /*!< Base address of DMA controller to use */
  46. ROM_DMA_PRVXFERDESC_T *sramBase; /*!< SRAM descriptor table (all channels) */
  47. ROM_DMA_QUEUE_T *pQueueHead; /*!< Pointer to linked list of queue descriptors */
  48. } DMA_DATACONTEXT_T;
  49. static const uint8_t mskAlign[3] = {0x0, 0x1, 0x3};
  50. static const uint8_t widthBytes[3] = {0x1, 0x2, 0x4};
  51. #define _dma_ch_int_enable(p, ch) ((p)->DMACOMMON[0].INTENSET = (1 << (ch))) /* Enable interrupts for a channel */
  52. #define _dma_ch_int_disable(p, ch) ((p)->DMACOMMON[0].INTENCLR = (1 << (ch))) /* Disable interrupts for a channel */
  53. #define _dma_ch_enable(p, ch) ((p)->DMACOMMON[0].ENABLESET = (1 << (ch))) /* Enable a channel */
  54. #define _dma_ch_disable(p, ch) ((p)->DMACOMMON[0].ENABLECLR = (1 << (ch))) /* Disable a channel */
  55. static void _dma_abort_ch(LPC_DMA_T *pDMA, uint8_t dmaCh)
  56. {
  57. _dma_ch_disable(pDMA, dmaCh);
  58. /* Wait for channel to go unbusy */
  59. while ((pDMA->DMACOMMON[0].BUSY & (1 << dmaCh)) != 0) {}
  60. /* Abort */
  61. pDMA->DMACOMMON[0].ABORT = (1 << dmaCh);
  62. }
  63. static void _dma_start_desc_chain(DMA_DATACONTEXT_T *pDrv, uint8_t dmaCh, ROM_DMA_DESC_T *pDesc)
  64. {
  65. /* Switch to busy state */
  66. pDesc->status = ROM_DMA_DESC_STS_BUSY;
  67. /* Move transfer descriptor to DMA table */
  68. pDrv->sramBase[dmaCh].xfercfg = pDesc->xfercfg;
  69. pDrv->sramBase[dmaCh].src = pDesc->src;
  70. pDrv->sramBase[dmaCh].dest = pDesc->dest;
  71. pDrv->sramBase[dmaCh].pNextLink = (ROM_DMA_DESC_T *) pDesc->pNextLink;
  72. /* Start transfer */
  73. pDrv->base->DMACH[dmaCh].XFERCFG = pDesc->xfercfg;
  74. }
  75. // **********************************************************
  76. uint32_t dmaalt_get_mem_size(void)
  77. {
  78. return sizeof(DMA_DATACONTEXT_T);
  79. }
  80. ROM_DMA_HANDLE_T dmaalt_init(void *mem, const ROM_DMA_INIT_T *pInit)
  81. {
  82. DMA_DATACONTEXT_T *pDrv;
  83. /* Verify alignment is at least 4 bytes */
  84. if (((uint32_t) mem & 0x3) != 0) {
  85. return NULL;
  86. }
  87. pDrv = (DMA_DATACONTEXT_T *) mem;
  88. memset(pDrv, 0, sizeof(DMA_DATACONTEXT_T));
  89. /* Save pointer to user data */
  90. pDrv->pUserData = pInit->pUserData;
  91. pDrv->base = (LPC_DMA_T *) pInit->base;
  92. pDrv->sramBase = (ROM_DMA_PRVXFERDESC_T *) pInit->sramBase;
  93. /* Enable DMA controller */
  94. pDrv->base->CTRL = 1;
  95. pDrv->base->SRAMBASE = (uint32_t) pInit->sramBase;
  96. return (ROM_DMA_HANDLE_T) pDrv;
  97. }
  98. ErrorCode_t dmaalt_setup_channel(ROM_DMA_HANDLE_T pHandle, ROM_DMA_CHAN_CFG_T *pCfg, uint8_t dmaCh)
  99. {
  100. uint32_t cfg;
  101. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  102. /* Parameter checks */
  103. if ((pCfg->burstSize > (uint32_t) ROM_DMA_BURSTPOWER_1024) || (pCfg->channelPrio > 7)) {
  104. return ERR_DMA_PARAM;
  105. }
  106. /* Enable DMA channel, clear any errors, enable interrupts */
  107. pDrv->base->DMACOMMON[0].ENABLECLR = (1 << dmaCh);
  108. pDrv->base->DMACOMMON[0].ERRINT = (1 << dmaCh);
  109. pDrv->base->DMACOMMON[0].INTA = (1 << dmaCh);
  110. pDrv->base->DMACOMMON[0].INTB = (1 << dmaCh);
  111. /* Basic DMA configuration */
  112. if (pCfg->periphReq) {
  113. cfg = DMA_CFG_PERIPHREQEN;
  114. }
  115. else {
  116. /* Hardware triggering */
  117. cfg = DMA_CFG_HWTRIGEN;
  118. cfg |= (pCfg->triggerPolHi << 4) | (pCfg->triggerLevel << 5) | (pCfg->triggerBurst << 6);
  119. }
  120. cfg |= (pCfg->burstSize << 8) | (pCfg->srcBurstWrap << 14) | (pCfg->dstBurstWrap << 15) | (pCfg->channelPrio << 16);
  121. pDrv->base->DMACH[dmaCh].CFG = cfg;
  122. return LPC_OK;
  123. }
  124. ErrorCode_t dmaalt_init_queue(ROM_DMA_HANDLE_T pHandle, uint8_t dmaCh, ROM_DMA_QUEUE_T *pQueue)
  125. {
  126. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  127. /* Check queue structure alignment */
  128. if (((uint32_t) pQueue & 0x3) != 0) {
  129. /* Not aligned at 4 bytes, error */
  130. return ERR_DMA_NOT_ALIGNMENT;
  131. }
  132. memset(pQueue, 0, sizeof(ROM_DMA_QUEUE_T));
  133. /* Save DMA channekl for this queue */
  134. pQueue->dmaCh = dmaCh;
  135. /* Append to existing queue */
  136. if (pDrv->pQueueHead) {
  137. pQueue->pQueueHead = (struct ROM_DMA_QUEUE *) pDrv->pQueueHead;
  138. }
  139. pDrv->pQueueHead = pQueue;
  140. pQueue->queueSt = (uint8_t) ROM_QUEUE_ST_IDLE;
  141. return LPC_OK;
  142. }
  143. void dmaalt_register_queue_callback(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue, uint32_t cbIndex, void *pCB)
  144. {
  145. switch (cbIndex) {
  146. case ROM_DMA_XFERCOMPLETE_CB:
  147. pQueue->dmaCompCB = (dmaTransferCompleteCB) pCB;
  148. break;
  149. case ROM_DMA_XFERDESCCOMPLETE_CB:
  150. pQueue->dmaDescCompCB = (dmaTransferDescCompleteCB) pCB;
  151. break;
  152. case ROM_DMA_XFERERROR_CB:
  153. pQueue->dmaErrorCB = (dmaTransferErrorCB) pCB;
  154. break;
  155. }
  156. }
  157. ErrorCode_t dmaalt_build_descriptor_chain(ROM_DMA_HANDLE_T pHandle,
  158. ROM_DMA_XFERDESC_CFG_T *pXferCfg,
  159. ROM_DMA_DESC_T *pDesc,
  160. ROM_DMA_DESC_T *pDescPrev)
  161. {
  162. uint32_t cfg, xfercnt, burstSize;
  163. uint8_t srcWrap, destWrap;
  164. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  165. /* Parameter checks */
  166. if (pDesc == NULL) {
  167. return ERR_DMA_PARAM;
  168. }
  169. /* Passed descriptor must be correctly aligned */
  170. if (((uint32_t) pDesc & 0xF) != 0) {
  171. return ERR_DMA_NOT_ALIGNMENT;
  172. }
  173. /* Parameter checks */
  174. if (pXferCfg->width > (uint8_t) ROM_DMA_WIDTH_4) {
  175. return ERR_DMA_PARAM;
  176. }
  177. if ((pXferCfg->srcInc > (uint8_t) ROM_DMA_ADDRINC_4X) ||
  178. (pXferCfg->dstInc > (uint8_t) ROM_DMA_ADDRINC_4X)) {
  179. return ERR_DMA_PARAM;
  180. }
  181. if ((pXferCfg->xferCount < 1) || (pXferCfg->xferCount > 1024)) {
  182. return ERR_DMA_PARAM;
  183. }
  184. xfercnt = pXferCfg->xferCount - 1; /* Adjust for use with DMA */
  185. /* Check source and destination address alignment */
  186. if (((uint32_t) pXferCfg->src & mskAlign[pXferCfg->width]) != 0) {
  187. return ERR_DMA_NOT_ALIGNMENT;
  188. }
  189. if (((uint32_t) pXferCfg->dest & mskAlign[pXferCfg->width]) != 0) {
  190. return ERR_DMA_NOT_ALIGNMENT;
  191. }
  192. /* Get source and destination wrap states for the channel */
  193. cfg = pDrv->base->DMACH[pXferCfg->dmaCh].CFG;
  194. /* Get burst size in datum count, used for wrap end address, offset by
  195. (-1) for end address computation */
  196. burstSize = (1 << ((cfg >> 8) & 0xF)) - 1;
  197. /* Setup source transfer address */
  198. if (pXferCfg->srcInc == ROM_DMA_ADDRINC_0X) {
  199. /* No address increment - even with burst - so source address doesn't need
  200. to be adjusted */
  201. pDesc->src = (uint32_t) pXferCfg->src;
  202. }
  203. else {
  204. srcWrap = (uint8_t) ((cfg & (1 << 14)) != 0);
  205. if (srcWrap) {
  206. /* Wrap enabled - compute end address based on burst size and datum width */
  207. pDesc->src = (uint32_t) pXferCfg->src + ((uint32_t) widthBytes[pXferCfg->width] *
  208. burstSize * (1 << ((uint32_t) pXferCfg->srcInc - 1)));
  209. }
  210. else {
  211. /* No wrap - compute end address based on transfer size and datum width */
  212. pDesc->src = (uint32_t) pXferCfg->src + ((uint32_t) widthBytes[pXferCfg->width] *
  213. xfercnt * (1 << ((uint32_t) pXferCfg->srcInc - 1)));
  214. }
  215. }
  216. /* Setup destination transfer address */
  217. if (pXferCfg->dstInc == ROM_DMA_ADDRINC_0X) {
  218. /* No address increment - even with burst - so destination address doesn't need
  219. to be adjusted */
  220. pDesc->dest = (uint32_t) pXferCfg->dest;
  221. }
  222. else {
  223. destWrap = (uint8_t) ((cfg & (1 << 15)) != 0);
  224. if (destWrap) {
  225. /* Wrap enabled - compute end address based on burst size and datum width */
  226. pDesc->dest = (uint32_t) pXferCfg->dest + ((uint32_t) widthBytes[pXferCfg->width] *
  227. burstSize * (1 << ((uint32_t) pXferCfg->dstInc - 1)));
  228. }
  229. else {
  230. /* No wrap - compute end address based on transfer size and datum width */
  231. pDesc->dest = (uint32_t) pXferCfg->dest + ((uint32_t) widthBytes[pXferCfg->width] *
  232. xfercnt * (1 << ((uint32_t) pXferCfg->dstInc - 1)));
  233. }
  234. }
  235. /* Save pointer to user data context */
  236. pDesc->pUserData = pXferCfg->pUserData;
  237. /* Is the descriptor linked from a previous descriptor? */
  238. if (pDescPrev) {
  239. pDescPrev->pNextLink = (struct ROM_DMA_DESC *) pDesc;
  240. if (pXferCfg->enabCirc == 0) {
  241. pDescPrev->xfercfg &= ~(1 << 5);/* Disables INTB on previous descriptor link */
  242. pDescPrev->xfercfg |= (1 << 1); /* Reload on chained links */
  243. }
  244. }
  245. else {
  246. pDesc->pNextLink = NULL;
  247. }
  248. /* NULL out next chain descriptor pointers. The next chain descriptor is
  249. managed by the queue function, while the next link descriptor indicates the end
  250. of a chain. */
  251. pDesc->pNextChain = NULL;
  252. /* Current descriptor status is queueing. Status only applies to the first descriptor
  253. in a chain. */
  254. pDesc->status = ROM_DMA_DESC_STS_QUEUEING;
  255. pDesc->savedXferSize = pXferCfg->xferCount;
  256. /* Normalize parameters that are multibit to single bit */
  257. pXferCfg->swTrig = (pXferCfg->swTrig != 0);
  258. pXferCfg->clrTrig = (pXferCfg->clrTrig != 0);
  259. pXferCfg->fireDescCB = (pXferCfg->fireDescCB != 0);
  260. if (pXferCfg->enabCirc) {
  261. cfg = (1 << 1); /* Reload on chained links */
  262. }
  263. else {
  264. cfg = (1 << 5); /* INTB support for completion and next descriptor */
  265. }
  266. if (pXferCfg->stallDesc == 0) {
  267. cfg |= 0x1; /* CFGVALID */
  268. }
  269. /* Setup transfer configuration */
  270. cfg |= (pXferCfg->swTrig << 2) | (pXferCfg->clrTrig << 3) |
  271. (pXferCfg->fireDescCB << 4) | (pXferCfg->width << 8) | (pXferCfg->srcInc << 12) |
  272. (pXferCfg->dstInc << 14);
  273. cfg |= (xfercnt << 16);
  274. pDesc->xfercfg = cfg;
  275. return LPC_OK;
  276. }
  277. uint32_t dmaalt_get_transfer_count(ROM_DMA_HANDLE_T pHandle, ROM_DMA_DESC_T *pDesc)
  278. {
  279. uint32_t dataCount = 0;
  280. /* Count is only valid if descriptor is used */
  281. while (pDesc != NULL) {
  282. if (pDesc->status == ROM_DMA_DESC_STS_SPENT) {
  283. dataCount += (uint32_t) pDesc->savedXferSize;
  284. }
  285. pDesc = (ROM_DMA_DESC_T *) pDesc->pNextLink;
  286. }
  287. return dataCount;
  288. }
  289. void dmaalt_unstall_descriptor_chain(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue)
  290. {
  291. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  292. pDrv->base->DMACOMMON[0].SETVALID = (1 << pQueue->dmaCh);
  293. }
  294. void dmaalt_queue_descriptor(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue, ROM_DMA_DESC_T *pDescChainHead)
  295. {
  296. /* Add the link to the passed descriptor to the end of the queue */
  297. if (pQueue->pDescEnd != NULL) {
  298. pQueue->pDescEnd->pNextChain = (struct ROM_DMA_DESC *) pDescChainHead;
  299. }
  300. pQueue->pDescEnd = pDescChainHead;
  301. /* Next descriptor in queue */
  302. if (pQueue->pDescNext == NULL) {
  303. pQueue->pDescNext = pDescChainHead;
  304. }
  305. /* Descriptor is ready */
  306. pDescChainHead->status = ROM_DMA_DESC_STS_READY;
  307. }
  308. ROM_DMA_DESC_STS_T dmaalt_get_queue_pop_descriptor_status(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue)
  309. {
  310. if (pQueue->pDescPop) {
  311. return (ROM_DMA_DESC_STS_T) pQueue->pDescPop->status;
  312. }
  313. return ROM_DMA_DESC_STS_INVALID;
  314. }
  315. ROM_DMA_DESC_T *dmaalt_unqueue_descriptor(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue)
  316. {
  317. ROM_DMA_DESC_T *pDesc = NULL;
  318. /* Get current queue pop descriptor */
  319. if (pQueue->pDescPop) {
  320. /* Only expired (spent, error, or aborted descriptors can be unqueued. Use StopQueue to halt all
  321. descriptors queued. */
  322. if (pQueue->pDescPop->status >= ROM_DMA_DESC_STS_SPENT) {
  323. pDesc = (ROM_DMA_DESC_T *) pQueue->pDescPop;
  324. pQueue->pDescPop = (ROM_DMA_DESC_T *) pQueue->pDescPop->pNextChain;
  325. }
  326. }
  327. return pDesc;
  328. }
  329. ErrorCode_t dmaalt_start_queue(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue)
  330. {
  331. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  332. /* Is DMA already running? No need to restart */
  333. if ((pDrv->base->DMACOMMON[0].ACTIVE & (1 << pQueue->dmaCh)) != 0) {
  334. return LPC_OK;
  335. }
  336. /* Is queue empty? */
  337. if (pQueue->pDescNext == NULL) {
  338. return ERR_DMA_QUEUE_EMPTY;
  339. }
  340. /* Does the queue currently have a descriptor in it? */
  341. if (pQueue->pDescNext) {
  342. /* Is current descriptor chain ready? */
  343. if (pQueue->pDescNext->status == ROM_DMA_DESC_STS_READY) {
  344. /* Queue is now running */
  345. pQueue->queueSt = (uint8_t) ROM_QUEUE_ST_RUNNING;
  346. /* Enable this channel */
  347. _dma_ch_enable(pDrv->base, pQueue->dmaCh);
  348. _dma_ch_int_enable(pDrv->base, pQueue->dmaCh);
  349. _dma_start_desc_chain(pDrv, pQueue->dmaCh, pQueue->pDescNext);
  350. }
  351. }
  352. return LPC_OK;
  353. }
  354. ErrorCode_t dmaalt_stop_queue(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue)
  355. {
  356. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  357. ErrorCode_t error = LPC_OK;
  358. /* Disable interrupts for this channel */
  359. _dma_ch_int_disable(pDrv->base, pQueue->dmaCh);
  360. /* If queue is empty, no need to stop */
  361. if (pQueue->pDescNext == NULL) {
  362. return LPC_OK;
  363. }
  364. /* If current transfer is queued or ready, then switch it to aborted status
  365. and call completion callback if needed. */
  366. if (pQueue->pDescNext->status == ROM_DMA_DESC_STS_BUSY) {
  367. /* Abort transfer */
  368. _dma_abort_ch(pDrv->base, pQueue->dmaCh);
  369. }
  370. else if (!((pQueue->pDescNext->status == ROM_DMA_DESC_STS_QUEUEING) ||
  371. (pQueue->pDescNext->status == ROM_DMA_DESC_STS_READY))) {
  372. /* Other statuses are not legal for a queued descriptor */
  373. error = ERR_DMA_GENERAL;
  374. }
  375. /* Unlatch interrupts */
  376. pDrv->base->DMACOMMON[0].ERRINT = (1 << pQueue->dmaCh);
  377. pDrv->base->DMACOMMON[0].INTA = (1 << pQueue->dmaCh);
  378. pDrv->base->DMACOMMON[0].INTB = (1 << pQueue->dmaCh);
  379. /* Call completion callback to indicate abort state */
  380. pQueue->pDescNext->status = ROM_DMA_DESC_STS_ABORT;
  381. if (pQueue->dmaCompCB) {
  382. pQueue->dmaCompCB(pHandle, (struct ROM_DMA_QUEUE *) pQueue, pQueue->pDescNext);
  383. }
  384. /* Increment to next available descriptor since this one was aborted */
  385. pQueue->pDescNext = (ROM_DMA_DESC_T *) pQueue->pDescNext->pNextChain;
  386. /* Queue is now idle */
  387. pQueue->queueSt = (uint8_t) ROM_QUEUE_ST_IDLE;
  388. return error;
  389. }
  390. void dmaalt_flush_queue(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue)
  391. {
  392. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  393. /* Disable interrupts for this channel */
  394. _dma_ch_int_disable(pDrv->base, pQueue->dmaCh);
  395. /* Abort transfer */
  396. _dma_abort_ch(pDrv->base, pQueue->dmaCh);
  397. /* Unlatch interrupts */
  398. pDrv->base->DMACOMMON[0].ERRINT = (1 << pQueue->dmaCh);
  399. pDrv->base->DMACOMMON[0].INTA = (1 << pQueue->dmaCh);
  400. pDrv->base->DMACOMMON[0].INTB = (1 << pQueue->dmaCh);
  401. /* No callbacks on abort, all descriptors flushed */
  402. pQueue->pDescEnd = pQueue->pDescNext = pQueue->pDescPop = NULL;
  403. /* Queue is now idle */
  404. pQueue->queueSt = (uint8_t) ROM_QUEUE_ST_IDLE;
  405. }
  406. uint8_t dmaalt_get_queue_state(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue)
  407. {
  408. return pQueue->queueSt;
  409. }
  410. void dmaalt_force_trigger(ROM_DMA_HANDLE_T pHandle, ROM_DMA_QUEUE_T *pQueue)
  411. {
  412. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  413. pDrv->base->DMACOMMON[0].SETTRIG = (1 << pQueue->dmaCh);
  414. }
  415. // Otime = "optimize for speed of code execution"
  416. // ...add this pragma 1 line above the interrupt service routine function.
  417. void dmaalt_handler(ROM_DMA_HANDLE_T pHandle)
  418. {
  419. uint32_t err, inta, intb, all, dmaChMask;
  420. ROM_DMA_QUEUE_T *pQueue;
  421. ROM_DMA_DESC_T *pDesc;
  422. DMA_DATACONTEXT_T *pDrv = (DMA_DATACONTEXT_T *) pHandle;
  423. uint8_t nextChain = 0;
  424. /* DMA interrupt fires on one of three possible events:
  425. 1) ERROR : A DMA error has occured
  426. Calls error callback and stops queue
  427. 2) INTA on descriptor completion
  428. Calls descriptor completed callback
  429. 3) INTB on descriptor chain completion
  430. Calls descriptor chain completion callback */
  431. /* Loop through all enabled DMA channels */
  432. pQueue = pDrv->pQueueHead;
  433. err = pDrv->base->DMACOMMON[0].ERRINT;
  434. inta = pDrv->base->DMACOMMON[0].INTA;
  435. intb = pDrv->base->DMACOMMON[0].INTB;
  436. all = err | inta | intb;
  437. while (pQueue) {
  438. dmaChMask = (1 << pQueue->dmaCh);
  439. if ((all & dmaChMask) != 0) {
  440. /* DMA interrupt fire for this channel */
  441. if ((err & dmaChMask) != 0) {
  442. /* Abort current descriptor */
  443. _dma_ch_int_disable(pDrv->base, pQueue->dmaCh);
  444. _dma_abort_ch(pDrv->base, pQueue->dmaCh);
  445. /* Error interrupt, clear */
  446. pDrv->base->DMACOMMON[0].ERRINT = dmaChMask;
  447. pDrv->base->DMACOMMON[0].INTA = dmaChMask;
  448. pDrv->base->DMACOMMON[0].INTB = dmaChMask;
  449. /* Update status to error */
  450. pQueue->pDescNext->status = ROM_DMA_DESC_STS_ERROR;
  451. pQueue->queueSt = (uint8_t) ROM_QUEUE_ST_ERROR;
  452. /* Call error callback for channel */
  453. if (pQueue->dmaErrorCB) {
  454. pQueue->dmaErrorCB(pHandle, (struct ROM_DMA_QUEUE *) pQueue, pQueue->pDescNext);
  455. }
  456. nextChain = 1;
  457. }
  458. /* Interrupt A is used for user defined interrupt tied to a descriptor */
  459. if ((inta & dmaChMask) != 0) {
  460. pDrv->base->DMACOMMON[0].INTA = dmaChMask;
  461. /* Call transfer descriptor completion for channel */
  462. if (pQueue->dmaDescCompCB) {
  463. pQueue->dmaDescCompCB(pHandle, (struct ROM_DMA_QUEUE *) pQueue, pQueue->pDescNext);
  464. }
  465. }
  466. /* Interrupt B is used for user transfer descriptor chain completion */
  467. if ((intb & dmaChMask) != 0) {
  468. pDrv->base->DMACOMMON[0].INTB = dmaChMask;
  469. /* Update status to spent/complete */
  470. pQueue->pDescNext->status = ROM_DMA_DESC_STS_SPENT;
  471. /* Start the next descriptor chain? */
  472. pDesc = (ROM_DMA_DESC_T *) pQueue->pDescNext->pNextChain;
  473. if ((pDesc) && (pDesc->status == ROM_DMA_DESC_STS_READY)) {
  474. /* A queued descriptor is available and ready, so start it */
  475. _dma_start_desc_chain(pDrv, pQueue->dmaCh, pDesc);
  476. }
  477. /* Call transfer descriptor completion for channel */
  478. if (pQueue->dmaCompCB) {
  479. pQueue->dmaCompCB(pHandle, (struct ROM_DMA_QUEUE *) pQueue, pQueue->pDescNext);
  480. }
  481. nextChain = 1;
  482. }
  483. if (nextChain) {
  484. /* Need to save in pop queue? */
  485. if (pQueue->pDescPop == NULL) {
  486. pQueue->pDescPop = pQueue->pDescNext;
  487. }
  488. /* Advance to next queued descriptor */
  489. pQueue->pDescNext = (ROM_DMA_DESC_T *) pQueue->pDescNext->pNextChain;
  490. if (pQueue->pDescNext == NULL) {
  491. /* No more descriptors */
  492. pQueue->pDescEnd = NULL;
  493. }
  494. }
  495. all &= ~dmaChMask;
  496. }
  497. /* Next queue */
  498. pQueue = (ROM_DMA_QUEUE_T *) pQueue->pQueueHead;
  499. }
  500. if (all) {
  501. /* Unexpected interrupts, clear and disable */
  502. pDrv->base->DMACOMMON[0].ENABLECLR = all;
  503. pDrv->base->DMACOMMON[0].INTENCLR = all;
  504. pDrv->base->DMACOMMON[0].ERRINT = all;
  505. pDrv->base->DMACOMMON[0].INTA = all;
  506. pDrv->base->DMACOMMON[0].INTB = all;
  507. }
  508. }
  509. uint32_t dmaalt_get_driver_version(void)
  510. {
  511. return DRVVERSION;
  512. }
  513. // *********************************************************