dma_8xx.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * @brief LPC8xx DMA chip driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2013
  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. #ifndef __DMA_8XX_H_
  32. #define __DMA_8XX_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup DMA_8XX CHIP: LPC8xx DMA Controller driver
  37. * @ingroup CHIP_8XX_Drivers
  38. * @{
  39. */
  40. /**
  41. * @brief DMA Controller shared registers structure
  42. */
  43. typedef struct { /*!< DMA shared registers structure */
  44. __IO uint32_t ENABLESET; /*!< DMA Channel Enable read and Set for all DMA channels */
  45. __I uint32_t RESERVED0;
  46. __O uint32_t ENABLECLR; /*!< DMA Channel Enable Clear for all DMA channels */
  47. __I uint32_t RESERVED1;
  48. __I uint32_t ACTIVE; /*!< DMA Channel Active status for all DMA channels */
  49. __I uint32_t RESERVED2;
  50. __I uint32_t BUSY; /*!< DMA Channel Busy status for all DMA channels */
  51. __I uint32_t RESERVED3;
  52. __IO uint32_t ERRINT; /*!< DMA Error Interrupt status for all DMA channels */
  53. __I uint32_t RESERVED4;
  54. __IO uint32_t INTENSET; /*!< DMA Interrupt Enable read and Set for all DMA channels */
  55. __I uint32_t RESERVED5;
  56. __O uint32_t INTENCLR; /*!< DMA Interrupt Enable Clear for all DMA channels */
  57. __I uint32_t RESERVED6;
  58. __IO uint32_t INTA; /*!< DMA Interrupt A status for all DMA channels */
  59. __I uint32_t RESERVED7;
  60. __IO uint32_t INTB; /*!< DMA Interrupt B status for all DMA channels */
  61. __I uint32_t RESERVED8;
  62. __O uint32_t SETVALID; /*!< DMA Set ValidPending control bits for all DMA channels */
  63. __I uint32_t RESERVED9;
  64. __O uint32_t SETTRIG; /*!< DMA Set Trigger control bits for all DMA channels */
  65. __I uint32_t RESERVED10;
  66. __O uint32_t ABORT; /*!< DMA Channel Abort control for all DMA channels */
  67. } LPC_DMA_COMMON_T;
  68. /**
  69. * @brief DMA Controller shared registers structure
  70. */
  71. typedef struct { /*!< DMA channel register structure */
  72. __IO uint32_t CFG; /*!< DMA Configuration register */
  73. __I uint32_t CTLSTAT; /*!< DMA Control and status register */
  74. __IO uint32_t XFERCFG; /*!< DMA Transfer configuration register */
  75. __I uint32_t RESERVED;
  76. } LPC_DMA_CHANNEL_T;
  77. /* Reserved bits masks... */
  78. #define DMA_CFG_RESERVED ((3<<2)|(1<<7)|(3<<12)|0xfffc0000)
  79. #define DMA_CTLSTAT_RESERVED (~(1|(1<<2)))
  80. #define DMA_XFERCFG_RESERVED ((3<<6)|(3<<10)|(0x3fu<<26))
  81. /* DMA channel mapping - each channel is mapped to an individual peripheral
  82. and direction or a DMA imput mux trigger */
  83. typedef enum {
  84. DMAREQ_USART0_RX, /*!< USART0 receive DMA channel */
  85. DMA_CH0 = DMAREQ_USART0_RX,
  86. DMAREQ_USART0_TX, /*!< USART0 transmit DMA channel */
  87. DMA_CH1 = DMAREQ_USART0_TX,
  88. DMAREQ_USART1_RX, /*!< USART1 receive DMA channel */
  89. DMA_CH2 = DMAREQ_USART1_RX,
  90. DMAREQ_USART1_TX, /*!< USART1 transmit DMA channel */
  91. DMA_CH3 = DMAREQ_USART1_TX,
  92. DMAREQ_USART2_RX, /*!< USART2 receive DMA channel */
  93. DMA_CH4 = DMAREQ_USART2_RX,
  94. DMAREQ_USART2_TX, /*!< USART2 transmit DMA channel */
  95. DMA_CH5 = DMAREQ_USART2_TX,
  96. DMAREQ_SPI0_RX,
  97. DMA_CH6 = DMAREQ_SPI0_RX, /*!< SPI0 receive DMA channel */
  98. DMAREQ_SPI0_TX,
  99. DMA_CH7 = DMAREQ_SPI0_TX, /*!< SPI0 transmit DMA channel */
  100. DMAREQ_SPI1_RX,
  101. DMA_CH8 = DMAREQ_SPI1_RX, /*!< SPI1 receive DMA channel */
  102. DMAREQ_SPI1_TX,
  103. DMA_CH9 = DMAREQ_SPI1_TX, /*!< SPI1 transmit DMA channel */
  104. DMAREQ_I2C0_MST,
  105. DMA_CH10 = DMAREQ_I2C0_MST, /*!< I2C0 Master DMA channel */
  106. DMAREQ_I2C0_SLV,
  107. DMA_CH11 = DMAREQ_I2C0_SLV, /*!< I2C0 Slave DMA channel */
  108. DMAREQ_I2C1_MST,
  109. DMA_CH12 = DMAREQ_I2C1_MST, /*!< I2C1 Master DMA channel */
  110. DMAREQ_I2C1_SLV,
  111. DMA_CH13 = DMAREQ_I2C1_SLV, /*!< I2C1 Slave DMA channel */
  112. DMAREQ_I2C2_MST,
  113. DMA_CH14 = DMAREQ_I2C2_MST, /*!< I2C2 Master DMA channel */
  114. DMAREQ_I2C2_SLV,
  115. DMA_CH15 = DMAREQ_I2C2_SLV, /*!< I2C2 Slave DMA channel */
  116. DMAREQ_I2C3_MST,
  117. DMA_CH16 = DMAREQ_I2C3_MST, /*!< I2C2 Master DMA channel */
  118. DMAREQ_I2C3_SLV,
  119. DMA_CH17 = DMAREQ_I2C3_SLV, /*!< I2C2 Slave DMA channel */
  120. } DMA_CHID_T;
  121. /* On LPC82x, Max DMA channel is 18 */
  122. #define MAX_DMA_CHANNEL (DMA_CH17 + 1)
  123. /* Reserved bits masks... */
  124. #define DMA_COMMON_RESERVED (~(0UL) << MAX_DMA_CHANNEL)
  125. #define DMA_ENABLESET_RESERVED DMA_COMMON_RESERVED
  126. #define DMA_ENABLECLR_RESERVED DMA_COMMON_RESERVED
  127. #define DMA_ACTIVE_RESERVED DMA_COMMON_RESERVED
  128. #define DMA_BUSY_RESERVED DMA_COMMON_RESERVED
  129. #define DMA_ERRINT_RESERVED DMA_COMMON_RESERVED
  130. #define DMA_INTENSET_RESERVED DMA_COMMON_RESERVED
  131. #define DMA_INTENCLR_RESERVED DMA_COMMON_RESERVED
  132. #define DMA_INTA_RESERVED DMA_COMMON_RESERVED
  133. #define DMA_INTB_RESERVED DMA_COMMON_RESERVED
  134. #define DMA_SETVALID_RESERVED DMA_COMMON_RESERVED
  135. #define DMA_SETTRIG_RESERVED DMA_COMMON_RESERVED
  136. #define DMA_ABORT_RESERVED DMA_COMMON_RESERVED
  137. /**
  138. * @brief DMA Controller register block structure
  139. */
  140. typedef struct { /*!< DMA Structure */
  141. __IO uint32_t CTRL; /*!< DMA control register */
  142. __I uint32_t INTSTAT; /*!< DMA Interrupt status register */
  143. __IO uint32_t SRAMBASE; /*!< DMA SRAM address of the channel configuration table */
  144. __I uint32_t RESERVED2[5];
  145. LPC_DMA_COMMON_T DMACOMMON[1]; /*!< DMA shared channel (common) registers */
  146. __I uint32_t RESERVED0[225];
  147. LPC_DMA_CHANNEL_T DMACH[MAX_DMA_CHANNEL]; /*!< DMA channel registers */
  148. } LPC_DMA_T;
  149. /* Reserved bits masks... */
  150. #define DMA_CTRL_RESERVED (~1)
  151. #define DMA_INTSTAT_RESERVED (~7)
  152. #define DMA_SRAMBASE_RESERVED (0xFF)
  153. typedef enum {
  154. WIDTH_8_BITS = 0,
  155. WIDTH_16_BITS = 1,
  156. WIDTH_32_BITS = 2
  157. } DataWidth;
  158. /**
  159. * @brief Easy API, setup all required things to make DMA ready to use
  160. * @param ch : DMA channel ID
  161. * @return Nothing
  162. */
  163. void ChipEz_DMA_Init(uint32_t isEnableIRQ);
  164. /**
  165. * @brief Easy API, setup the next transfer for a channel, this function updates DMA descriptor
  166. * @param ch : DMA channel ID
  167. * @param src_address: Source base address
  168. * @param src_increment: Increment of source address after each transfer. Could be DMA_XFERCFG_SRCINC_0/1/2/4
  169. * @param dst_address: Destination base address
  170. * @param dst_increment: Increment of destination address after each transfer. Could be DMA_XFERCFG_DSTINC_0/1/2/4
  171. * @param xfr_width: Width of transfer unit, could be WIDTH_8_BITS, WIDTH_16_BITS, or WIDTH_32_BITS
  172. * @param length_bytes: Transfer length, in bytes. Must NOT makes transfer unit count exceed 1024.
  173. * @param priority: Channel priority, used internally in DMA, could be from 0 (highest) to 7 (lowest)
  174. * @return Nothing
  175. * @note This function will set the DMA descriptor in the SRAM table to the
  176. * the passed descriptor. This function is only meant to be used when
  177. * the DMA channel is not active and can be used to setup the
  178. * initial transfer for a linked list or ping-pong buffer or just a
  179. * single transfer without a next descriptor.
  180. */
  181. void ChipEz_DMA_InitChannel( DMA_CHID_T ch, uint32_t src_address, uint32_t src_increment,
  182. uint32_t dst_address, uint32_t dst_increment, uint32_t xfr_width, uint32_t length_bytes, uint32_t priority);
  183. /**
  184. * @brief Easy API, Start transfer of a DMA channel
  185. * @param ch : DMA channel ID
  186. * @param src_increment: Increment of source address after each transfer. Could be DMA_XFERCFG_SRCINC_0/1/2/4.
  187. * Should be consistent with the same parameter used in ChipEz_DMA_InitChannel()
  188. * @param dst_increment: Increment of destination address after each transfer. Could be DMA_XFERCFG_DSTINC_0/1/2/4
  189. * hould be consistent with the same parameter used in ChipEz_DMA_InitChannel()
  190. * @param xfr_width: Width of transfer unit, could be WIDTH_8_BITS, WIDTH_16_BITS, or WIDTH_32_BITS
  191. * Should be consistent with the same parameter used in ChipEz_DMA_InitChannel()
  192. * @param length_bytes: Transfer length, in bytes. Must NOT makes transfer unit count exceed 1024.
  193. * Should be consistent with the same parameter used in ChipEz_DMA_InitChannel()
  194. * @param priority: Channel priority, used internally in DMA, could be from 0 (highest) to 7 (lowest)
  195. * @return Nothing
  196. */
  197. void ChipEz_DMA_StartTransfer(DMA_CHID_T ch, uint32_t src_increment, uint32_t dst_increment, uint32_t xfr_width, uint32_t length_bytes);
  198. /**
  199. * @brief Easier API, Combine ChipEz_DMA_InitChannel and ChipEz_DMA_StartTransfer with channel busy check
  200. parameter is the same as ChipEz_DMA_InitChannel
  201. @return TRUE when success, FALSE when current channel is busy.
  202. */
  203. bool ChipEzr_DMA_Transfer( DMA_CHID_T channel, uint32_t src_address, uint32_t src_increment,
  204. uint32_t dst_address, uint32_t dst_increment, uint32_t xfr_width, uint32_t length_bytes, uint32_t priority);
  205. /**
  206. * @brief Easy API, Abort a DMA channel safely
  207. * @param ch : DMA channel ID
  208. * @return Nothing
  209. */
  210. void ChipEz_DMA_AbortChannel(DMA_CHID_T ch);
  211. /** @defgroup DMA_COMMONDRV_8XX CHIP: LPC8xx DMA Controller driver common functions
  212. * @{
  213. */
  214. /**
  215. * @brief Initialize DMA controller
  216. * @param pDMA : The base of DMA controller on the chip
  217. * @return Nothing
  218. */
  219. STATIC INLINE void Chip_DMA_Init(LPC_DMA_T *pDMA)
  220. {
  221. (void) pDMA;
  222. Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_DMA);
  223. }
  224. /**
  225. * @brief De-Initialize DMA controller
  226. * @param pDMA : The base of DMA controller on the chip
  227. * @return Nothing
  228. */
  229. STATIC INLINE void Chip_DMA_DeInit(LPC_DMA_T *pDMA)
  230. {
  231. (void) pDMA;
  232. Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_DMA);
  233. }
  234. /**
  235. * @brief Enable DMA controller
  236. * @param pDMA : The base of DMA controller on the chip
  237. * @return Nothing
  238. */
  239. STATIC INLINE void Chip_DMA_Enable(LPC_DMA_T *pDMA)
  240. {
  241. pDMA->CTRL = 1;
  242. }
  243. /**
  244. * @brief Disable DMA controller
  245. * @param pDMA : The base of DMA controller on the chip
  246. * @return Nothing
  247. */
  248. STATIC INLINE void Chip_DMA_Disable(LPC_DMA_T *pDMA)
  249. {
  250. pDMA->CTRL = 0;
  251. }
  252. /* DMA interrupt status bits (common) */
  253. #define DMA_INTSTAT_ACTIVEINT 0x2 /*!< Summarizes whether any enabled interrupts are pending */
  254. #define DMA_INTSTAT_ACTIVEERRINT 0x4 /*!< Summarizes whether any error interrupts are pending */
  255. /**
  256. * @brief Get pending interrupt or error interrupts
  257. * @param pDMA : The base of DMA controller on the chip
  258. * @return An Or'ed value of DMA_INTSTAT_* types
  259. * @note If any DMA channels have an active interrupt or error interrupt
  260. * pending, this functional will a common status that applies to all
  261. * channels.
  262. */
  263. STATIC INLINE uint32_t Chip_DMA_GetIntStatus(LPC_DMA_T *pDMA)
  264. {
  265. return (pDMA->INTSTAT & ~DMA_INTSTAT_RESERVED);
  266. }
  267. /* DMA channel source/address/next descriptor */
  268. typedef struct {
  269. uint32_t xfercfg; /*!< Transfer configuration (only used in linked lists and ping-pong configs) */
  270. uint32_t source; /*!< DMA transfer source end address */
  271. uint32_t dest; /*!< DMA transfer desintation end address */
  272. uint32_t next; /*!< Link to next DMA descriptor, must be 16 byte aligned */
  273. } DMA_CHDESC_T;
  274. /* DMA SRAM table - this can be optionally used with the Chip_DMA_SetSRAMBase()
  275. function if a DMA SRAM table is needed. */
  276. extern DMA_CHDESC_T Chip_DMA_Table[MAX_DMA_CHANNEL];
  277. /**
  278. * @brief Set DMA controller SRAM base address
  279. * @param pDMA : The base of DMA controller on the chip
  280. * @param base : The base address where the DMA descriptors will be stored
  281. * @return Nothing
  282. * @note A 256 byte block of memory aligned on a 256 byte boundary must be
  283. * provided for this function. It sets the base address used for
  284. * DMA descriptor table (16 descriptors total that use 16 bytes each).<br>
  285. *
  286. * A pre-defined table with correct alignment can be used for this
  287. * function by calling Chip_DMA_SetSRAMBase(LPC_DMA, DMA_ADDR(Chip_DMA_Table));
  288. */
  289. STATIC INLINE void Chip_DMA_SetSRAMBase(LPC_DMA_T *pDMA, uint32_t base)
  290. {
  291. pDMA->SRAMBASE = base;
  292. }
  293. /**
  294. * @brief Returns DMA controller SRAM base address
  295. * @param pDMA : The base of DMA controller on the chip
  296. * @return The base address where the DMA descriptors are stored
  297. */
  298. STATIC INLINE uint32_t Chip_DMA_GetSRAMBase(LPC_DMA_T *pDMA)
  299. {
  300. return (pDMA->SRAMBASE & ~DMA_SRAMBASE_RESERVED);
  301. }
  302. /**
  303. * @}
  304. */
  305. /** @defgroup DMA_COMMON_8XX CHIP: LPC8xx DMA Controller driver common channel functions
  306. * @{
  307. */
  308. /**
  309. * @brief Enables a single DMA channel
  310. * @param pDMA : The base of DMA controller on the chip
  311. * @param ch : DMA channel ID
  312. * @return Nothing
  313. */
  314. STATIC INLINE void Chip_DMA_EnableChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  315. {
  316. pDMA->DMACOMMON[0].ENABLESET = (1 << ch);
  317. }
  318. /**
  319. * @brief Disables a single DMA channel
  320. * @param pDMA : The base of DMA controller on the chip
  321. * @param ch : DMA channel ID
  322. * @return Nothing
  323. */
  324. STATIC INLINE void Chip_DMA_DisableChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  325. {
  326. pDMA->DMACOMMON[0].ENABLECLR = (1 << ch);
  327. }
  328. /**
  329. * @brief Returns all enabled DMA channels
  330. * @param pDMA : The base of DMA controller on the chip
  331. * @return An Or'ed value of all enabled DMA channels (0 - 15)
  332. * @note A high values in bits 0 .. 15 in the return values indicates
  333. * that the channel for that bit (bit 0 = channel 0, bit 1 -
  334. * channel 1, etc.) is enabled. A low state is disabled.
  335. */
  336. STATIC INLINE uint32_t Chip_DMA_GetEnabledChannels(LPC_DMA_T *pDMA)
  337. {
  338. return (pDMA->DMACOMMON[0].ENABLESET & ~DMA_ENABLESET_RESERVED);
  339. }
  340. /**
  341. * @brief Returns all active DMA channels
  342. * @param pDMA : The base of DMA controller on the chip
  343. * @return An Or'ed value of all active DMA channels (0 - 15)
  344. * @note A high values in bits 0 .. 15 in the return values indicates
  345. * that the channel for that bit (bit 0 = channel 0, bit 1 -
  346. * channel 1, etc.) is active. A low state is inactive. A active
  347. * channel indicates that a DMA operation has been started but
  348. * not yet fully completed.
  349. */
  350. STATIC INLINE uint32_t Chip_DMA_GetActiveChannels(LPC_DMA_T *pDMA)
  351. {
  352. return (pDMA->DMACOMMON[0].ACTIVE & ~DMA_ACTIVE_RESERVED);
  353. }
  354. /**
  355. * @brief Returns all busy DMA channels
  356. * @param pDMA : The base of DMA controller on the chip
  357. * @return An Or'ed value of all busy DMA channels (0 - 15)
  358. * @note A high values in bits 0 .. 15 in the return values indicates
  359. * that the channel for that bit (bit 0 = channel 0, bit 1 -
  360. * channel 1, etc.) is busy. A low state is not busy. A DMA
  361. * channel is considered busy when there is any operation
  362. * related to that channel in the DMA controller�s internal
  363. * pipeline.
  364. */
  365. STATIC INLINE uint32_t Chip_DMA_GetBusyChannels(LPC_DMA_T *pDMA)
  366. {
  367. return (pDMA->DMACOMMON[0].BUSY & ~DMA_BUSY_RESERVED);
  368. }
  369. /**
  370. * @brief Returns pending error interrupt status for all DMA channels
  371. * @param pDMA : The base of DMA controller on the chip
  372. * @return An Or'ed value of all channels (0 - 15) error interrupt status
  373. * @note A high values in bits 0 .. 15 in the return values indicates
  374. * that the channel for that bit (bit 0 = channel 0, bit 1 -
  375. * channel 1, etc.) has a pending error interrupt. A low state
  376. * indicates no error interrupt.
  377. */
  378. STATIC INLINE uint32_t Chip_DMA_GetErrorIntChannels(LPC_DMA_T *pDMA)
  379. {
  380. return (pDMA->DMACOMMON[0].ERRINT & ~DMA_ERRINT_RESERVED);
  381. }
  382. /**
  383. * @brief Clears a pending error interrupt status for a single DMA channel
  384. * @param pDMA : The base of DMA controller on the chip
  385. * @param ch : DMA channel ID
  386. * @return Nothing
  387. */
  388. STATIC INLINE void Chip_DMA_ClearErrorIntChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  389. {
  390. pDMA->DMACOMMON[0].ERRINT = (1 << ch);
  391. }
  392. /**
  393. * @brief Enables a single DMA channel's interrupt used in common DMA interrupt
  394. * @param pDMA : The base of DMA controller on the chip
  395. * @param ch : DMA channel ID
  396. * @return Nothing
  397. */
  398. STATIC INLINE void Chip_DMA_EnableIntChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  399. {
  400. pDMA->DMACOMMON[0].INTENSET = (1 << ch);
  401. }
  402. /**
  403. * @brief Disables a single DMA channel's interrupt used in common DMA interrupt
  404. * @param pDMA : The base of DMA controller on the chip
  405. * @param ch : DMA channel ID
  406. * @return Nothing
  407. */
  408. STATIC INLINE void Chip_DMA_DisableIntChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  409. {
  410. pDMA->DMACOMMON[0].INTENCLR = (1 << ch);
  411. }
  412. /**
  413. * @brief Returns all enabled interrupt channels
  414. * @param pDMA : The base of DMA controller on the chip
  415. * @return Nothing
  416. * @note A high values in bits 0 .. 15 in the return values indicates
  417. * that the channel for that bit (bit 0 = channel 0, bit 1 -
  418. * channel 1, etc.) has an enabled interrupt for the channel.
  419. * A low state indicates that the DMA channel will not contribute
  420. * to the common DMA interrupt status.
  421. */
  422. STATIC INLINE uint32_t Chip_DMA_GetEnableIntChannels(LPC_DMA_T *pDMA)
  423. {
  424. return (pDMA->DMACOMMON[0].INTENSET & ~DMA_INTENSET_RESERVED);
  425. }
  426. /**
  427. * @brief Returns active A interrupt status for all channels
  428. * @param pDMA : The base of DMA controller on the chip
  429. * @return Nothing
  430. * @note A high values in bits 0 .. 15 in the return values indicates
  431. * that the channel for that bit (bit 0 = channel 0, bit 1 -
  432. * channel 1, etc.) has an active A interrupt for the channel.
  433. * A low state indicates that the A interrupt is not active.
  434. */
  435. STATIC INLINE uint32_t Chip_DMA_GetActiveIntAChannels(LPC_DMA_T *pDMA)
  436. {
  437. return (pDMA->DMACOMMON[0].INTA & ~DMA_INTA_RESERVED);
  438. }
  439. /**
  440. * @brief Clears active A interrupt status for a single channel
  441. * @param pDMA : The base of DMA controller on the chip
  442. * @param ch : DMA channel ID
  443. * @return Nothing
  444. */
  445. STATIC INLINE void Chip_DMA_ClearActiveIntAChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  446. {
  447. pDMA->DMACOMMON[0].INTA = (1 << ch);
  448. }
  449. /**
  450. * @brief Returns active B interrupt status for all channels
  451. * @param pDMA : The base of DMA controller on the chip
  452. * @return Nothing
  453. * @note A high values in bits 0 .. 15 in the return values indicates
  454. * that the channel for that bit (bit 0 = channel 0, bit 1 -
  455. * channel 1, etc.) has an active B interrupt for the channel.
  456. * A low state indicates that the B interrupt is not active.
  457. */
  458. STATIC INLINE uint32_t Chip_DMA_GetActiveIntBChannels(LPC_DMA_T *pDMA)
  459. {
  460. return (pDMA->DMACOMMON[0].INTB & ~DMA_INTB_RESERVED);
  461. }
  462. /**
  463. * @brief Clears active B interrupt status for a single channel
  464. * @param pDMA : The base of DMA controller on the chip
  465. * @param ch : DMA channel ID
  466. * @return Nothing
  467. */
  468. STATIC INLINE void Chip_DMA_ClearActiveIntBChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  469. {
  470. pDMA->DMACOMMON[0].INTB = (1 << ch);
  471. }
  472. /**
  473. * @brief Sets the VALIDPENDING control bit for a single channel
  474. * @param pDMA : The base of DMA controller on the chip
  475. * @param ch : DMA channel ID
  476. * @return Nothing
  477. * @note See the User Manual for more information for what this bit does.
  478. *
  479. */
  480. STATIC INLINE void Chip_DMA_SetValidChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  481. {
  482. pDMA->DMACOMMON[0].SETVALID = (1 << ch);
  483. }
  484. /**
  485. * @brief Sets the TRIG bit for a single channel
  486. * @param pDMA : The base of DMA controller on the chip
  487. * @param ch : DMA channel ID
  488. * @return Nothing
  489. * @note See the User Manual for more information for what this bit does.
  490. */
  491. STATIC INLINE void Chip_DMA_SetTrigChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  492. {
  493. pDMA->DMACOMMON[0].SETTRIG = (1 << ch);
  494. }
  495. /**
  496. * @brief Aborts a DMA operation for a single channel
  497. * @param pDMA : The base of DMA controller on the chip
  498. * @param ch : DMA channel ID
  499. * @return Nothing
  500. * @note To abort a channel, the channel should first be disabled. Then wait
  501. * until the channel is no longer busy by checking the corresponding
  502. * bit in BUSY. Finally, abort the channel operation. This prevents the
  503. * channel from restarting an incomplete operation when it is enabled
  504. * again.
  505. */
  506. STATIC INLINE void Chip_DMA_AbortChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  507. {
  508. pDMA->DMACOMMON[0].ABORT = (1 << ch);
  509. }
  510. /**
  511. * @}
  512. */
  513. /** @defgroup DMA_CHANNEL_8XX CHIP: LPC8xx DMA Controller driver channel specific functions
  514. * @{
  515. */
  516. /* Support macro for DMA_CHDESC_T */
  517. #define DMA_ADDR(addr) ((uint32_t) (addr))
  518. /* Support definitions for setting the configuration of a DMA channel. You
  519. will need to get more information on these options from the User manual. */
  520. #define DMA_CFG_PERIPHREQEN (1 << 0) /*!< Enables Peripheral DMA requests */
  521. #define DMA_CFG_HWTRIGEN (1 << 1) /*!< Use hardware triggering via imput mux */
  522. #define DMA_CFG_TRIGPOL_LOW (0 << 4) /*!< Hardware trigger is active low or falling edge */
  523. #define DMA_CFG_TRIGPOL_HIGH (1 << 4) /*!< Hardware trigger is active high or rising edge */
  524. #define DMA_CFG_TRIGTYPE_EDGE (0 << 5) /*!< Hardware trigger is edge triggered */
  525. #define DMA_CFG_TRIGTYPE_LEVEL (1 << 5) /*!< Hardware trigger is level triggered */
  526. #define DMA_CFG_TRIGBURST_SNGL (0 << 6) /*!< Single transfer. Hardware trigger causes a single transfer */
  527. #define DMA_CFG_TRIGBURST_BURST (1 << 6) /*!< Burst transfer (see UM) */
  528. #define DMA_CFG_BURSTPOWER_1 (0 << 8) /*!< Set DMA burst size to 1 transfer */
  529. #define DMA_CFG_BURSTPOWER_2 (1 << 8) /*!< Set DMA burst size to 2 transfers */
  530. #define DMA_CFG_BURSTPOWER_4 (2 << 8) /*!< Set DMA burst size to 4 transfers */
  531. #define DMA_CFG_BURSTPOWER_8 (3 << 8) /*!< Set DMA burst size to 8 transfers */
  532. #define DMA_CFG_BURSTPOWER_16 (4 << 8) /*!< Set DMA burst size to 16 transfers */
  533. #define DMA_CFG_BURSTPOWER_32 (5 << 8) /*!< Set DMA burst size to 32 transfers */
  534. #define DMA_CFG_BURSTPOWER_64 (6 << 8) /*!< Set DMA burst size to 64 transfers */
  535. #define DMA_CFG_BURSTPOWER_128 (7 << 8) /*!< Set DMA burst size to 128 transfers */
  536. #define DMA_CFG_BURSTPOWER_256 (8 << 8) /*!< Set DMA burst size to 256 transfers */
  537. #define DMA_CFG_BURSTPOWER_512 (9 << 8) /*!< Set DMA burst size to 512 transfers */
  538. #define DMA_CFG_BURSTPOWER_1024 (10 << 8) /*!< Set DMA burst size to 1024 transfers */
  539. #define DMA_CFG_BURSTPOWER(n) ((n) << 8) /*!< Set DMA burst size to 2^n transfers, max n=10 */
  540. #define DMA_CFG_SRCBURSTWRAP (1 << 14) /*!< Source burst wrapping is enabled for this DMA channel */
  541. #define DMA_CFG_DSTBURSTWRAP (1 << 15) /*!< Destination burst wrapping is enabled for this DMA channel */
  542. #define DMA_CFG_CHPRIORITY(p) ((p) << 16) /*!< Sets DMA channel priority, min 0 (highest), max 3 (lowest) */
  543. /**
  544. * @brief Setup a DMA channel configuration
  545. * @param pDMA : The base of DMA controller on the chip
  546. * @param ch : DMA channel ID
  547. * @param cfg : An Or'ed value of DMA_CFG_* values that define the channel's configuration
  548. * @return Nothing
  549. * @note This function sets up all configurable options for the DMA channel.
  550. * These options are usually set once for a channel and then unchanged.<br>
  551. *
  552. * The following example show how to configure the channel for peripheral
  553. * DMA requests, burst transfer size of 1 (in 'transfers', not bytes),
  554. * continuous reading of the same source address, incrementing destination
  555. * address, and highest channel priority.<br>
  556. * Example: Chip_DMA_SetupChannelConfig(pDMA, SSP0_RX_DMA,
  557. * (DMA_CFG_PERIPHREQEN | DMA_CFG_TRIGBURST_BURST | DMA_CFG_BURSTPOWER_1 |
  558. * DMA_CFG_SRCBURSTWRAP | DMA_CFG_CHPRIORITY(0)));<br>
  559. *
  560. * The following example show how to configure the channel for an external
  561. * trigger from the imput mux with low edge polarity, a burst transfer size of 8,
  562. * incrementing source and destination addresses, and lowest channel
  563. * priority.<br>
  564. * Example: Chip_DMA_SetupChannelConfig(pDMA, DMA_CH14,
  565. * (DMA_CFG_HWTRIGEN | DMA_CFG_TRIGPOL_LOW | DMA_CFG_TRIGTYPE_EDGE |
  566. * DMA_CFG_TRIGBURST_BURST | DMA_CFG_BURSTPOWER_8 |
  567. * DMA_CFG_CHPRIORITY(3)));<br>
  568. *
  569. * For non-peripheral DMA triggering (DMA_CFG_HWTRIGEN definition), use the
  570. * DMA input mux functions to configure the DMA trigger source for a DMA channel.
  571. */
  572. STATIC INLINE void Chip_DMA_SetupChannelConfig(LPC_DMA_T *pDMA, DMA_CHID_T ch, uint32_t cfg)
  573. {
  574. pDMA->DMACH[ch].CFG = cfg;
  575. }
  576. /* DMA channel control and status register definitions */
  577. #define DMA_CTLSTAT_VALIDPENDING (1 << 0) /*!< Valid pending flag for this channel */
  578. #define DMA_CTLSTAT_TRIG (1 << 2) /*!< Trigger flag. Indicates that the trigger for this channel is currently set */
  579. /**
  580. * @brief Returns channel specific status flags
  581. * @param pDMA : The base of DMA controller on the chip
  582. * @param ch : DMA channel ID
  583. * @return AN Or'ed value of DMA_CTLSTAT_VALIDPENDING and DMA_CTLSTAT_TRIG
  584. */
  585. STATIC INLINE uint32_t Chip_DMA_GetChannelStatus(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  586. {
  587. return (pDMA->DMACH[ch].XFERCFG & ~DMA_XFERCFG_RESERVED);
  588. }
  589. /* DMA channel transfer configuration registers definitions */
  590. #define DMA_XFERCFG_CFGVALID (1 << 0) /*!< Configuration Valid flag */
  591. #define DMA_XFERCFG_RELOAD (1 << 1) /*!< Indicates whether the channels control structure will be reloaded when the current descriptor is exhausted */
  592. #define DMA_XFERCFG_SWTRIG (1 << 2) /*!< Software Trigger */
  593. #define DMA_XFERCFG_CLRTRIG (1 << 3) /*!< Clear Trigger */
  594. #define DMA_XFERCFG_SETINTA (1 << 4) /*!< Set Interrupt flag A for this channel to fire when descriptor is complete */
  595. #define DMA_XFERCFG_SETINTB (1 << 5) /*!< Set Interrupt flag B for this channel to fire when descriptor is complete */
  596. #define DMA_XFERCFG_WIDTH_8 (0 << 8) /*!< 8-bit transfers are performed */
  597. #define DMA_XFERCFG_WIDTH_16 (1 << 8) /*!< 16-bit transfers are performed */
  598. #define DMA_XFERCFG_WIDTH_32 (2 << 8) /*!< 32-bit transfers are performed */
  599. #define DMA_XFERCFG_SRCINC_0 (0 << 12) /*!< DMA source address is not incremented after a transfer */
  600. #define DMA_XFERCFG_SRCINC_1 (1 << 12) /*!< DMA source address is incremented by 1 (width) after a transfer */
  601. #define DMA_XFERCFG_SRCINC_2 (2 << 12) /*!< DMA source address is incremented by 2 (width) after a transfer */
  602. #define DMA_XFERCFG_SRCINC_4 (3 << 12) /*!< DMA source address is incremented by 4 (width) after a transfer */
  603. #define DMA_XFERCFG_DSTINC_0 (0 << 14) /*!< DMA destination address is not incremented after a transfer */
  604. #define DMA_XFERCFG_DSTINC_1 (1 << 14) /*!< DMA destination address is incremented by 1 (width) after a transfer */
  605. #define DMA_XFERCFG_DSTINC_2 (2 << 14) /*!< DMA destination address is incremented by 2 (width) after a transfer */
  606. #define DMA_XFERCFG_DSTINC_4 (3 << 14) /*!< DMA destination address is incremented by 4 (width) after a transfer */
  607. #define DMA_XFERCFG_XFERCOUNT(n) ((n - 1) << 16) /*!< DMA transfer count in 'transfers', between (0)1 and (1023)1024 */
  608. /**
  609. * @brief Setup a DMA channel transfer configuration
  610. * @param pDMA : The base of DMA controller on the chip
  611. * @param ch : DMA channel ID
  612. * @param cfg : An Or'ed value of DMA_XFERCFG_* values that define the channel's transfer configuration
  613. * @return Nothing
  614. * @note This function sets up the transfer configuration for the DMA channel.<br>
  615. *
  616. * The following example show how to configure the channel's transfer for
  617. * multiple transfer descriptors (ie, ping-pong), interrupt 'A' trigger on
  618. * transfer descriptor completion, 128 byte size transfers, and source and
  619. * destination address increment.<br>
  620. * Example: Chip_DMA_SetupChannelTransfer(pDMA, SSP0_RX_DMA,
  621. * (DMA_XFERCFG_CFGVALID | DMA_XFERCFG_RELOAD | DMA_XFERCFG_SETINTA |
  622. * DMA_XFERCFG_WIDTH_8 | DMA_XFERCFG_SRCINC_1 | DMA_XFERCFG_DSTINC_1 |
  623. * DMA_XFERCFG_XFERCOUNT(128)));<br>
  624. */
  625. STATIC INLINE void Chip_DMA_SetupChannelTransfer(LPC_DMA_T *pDMA, DMA_CHID_T ch, uint32_t cfg)
  626. {
  627. pDMA->DMACH[ch].XFERCFG = cfg;
  628. }
  629. /**
  630. * @brief Set DMA transfer register interrupt bits (safe)
  631. * @param pDMA : The base of DMA controller on the chip
  632. * @param ch : DMA channel ID
  633. * @param mask : Bits to set
  634. * @return Nothing
  635. * @note This function safely sets bits in the DMA channel specific XFERCFG
  636. * register.
  637. */
  638. STATIC INLINE void Chip_DMA_SetTranBits(LPC_DMA_T *pDMA, DMA_CHID_T ch, uint32_t mask)
  639. {
  640. /* Read and write values may not be the same, write 0 to
  641. undefined bits */
  642. pDMA->DMACH[ch].XFERCFG = (pDMA->DMACH[ch].XFERCFG & ~DMA_XFERCFG_RESERVED) | mask;
  643. }
  644. /**
  645. * @brief Clear DMA transfer register interrupt bits (safe)
  646. * @param pDMA : The base of DMA controller on the chip
  647. * @param ch : DMA channel ID
  648. * @param mask : Bits to clear
  649. * @return Nothing
  650. * @note This function safely clears bits in the DMA channel specific XFERCFG
  651. * register.
  652. */
  653. STATIC INLINE void Chip_DMA_ClearTranBits(LPC_DMA_T *pDMA, DMA_CHID_T ch, uint32_t mask)
  654. {
  655. /* Read and write values may not be the same, write 0 to
  656. undefined bits */
  657. pDMA->DMACH[ch].XFERCFG &= ~(DMA_XFERCFG_RESERVED | mask);
  658. }
  659. /**
  660. * @brief Update the transfer size in an existing DMA channel transfer configuration
  661. * @param pDMA : The base of DMA controller on the chip
  662. * @param ch : DMA channel ID
  663. * @param trans : Number of transfers to update the transfer configuration to (1 - 1023)
  664. * @return Nothing
  665. */
  666. STATIC INLINE void Chip_DMA_SetupChannelTransferSize(LPC_DMA_T *pDMA, DMA_CHID_T ch, uint32_t trans)
  667. {
  668. pDMA->DMACH[ch].XFERCFG = (pDMA->DMACH[ch].XFERCFG & ~(DMA_XFERCFG_RESERVED | (0x3FF << 16))) | DMA_XFERCFG_XFERCOUNT(trans);
  669. }
  670. /**
  671. * @brief Sets a DMA channel configuration as valid
  672. * @param pDMA : The base of DMA controller on the chip
  673. * @param ch : DMA channel ID
  674. * @return Nothing
  675. */
  676. STATIC INLINE void Chip_DMA_SetChannelValid(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  677. {
  678. Chip_DMA_SetTranBits(pDMA, ch, DMA_XFERCFG_CFGVALID);
  679. }
  680. /**
  681. * @brief Sets a DMA channel configuration as invalid
  682. * @param pDMA : The base of DMA controller on the chip
  683. * @param ch : DMA channel ID
  684. * @return Nothing
  685. */
  686. STATIC INLINE void Chip_DMA_SetChannelInValid(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  687. {
  688. Chip_DMA_ClearTranBits(pDMA, ch, DMA_XFERCFG_CFGVALID);
  689. }
  690. /**
  691. * @brief Performs a software trigger of the DMA channel
  692. * @param pDMA : The base of DMA controller on the chip
  693. * @param ch : DMA channel ID
  694. * @return Nothing
  695. */
  696. STATIC INLINE void Chip_DMA_SWTriggerChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  697. {
  698. Chip_DMA_SetTranBits(pDMA, ch, DMA_XFERCFG_SWTRIG);
  699. }
  700. /**
  701. * @brief Checks if the given channel is active or not
  702. * @param pDMA : The base of DMA controller on the chip
  703. * @param ch : DMA channel ID
  704. * @return 1 if channel @a ch is active; 0 if channel @a ch is not active
  705. */
  706. STATIC INLINE bool Chip_DMA_IsChannelActive(LPC_DMA_T *pDMA, DMA_CHID_T ch)
  707. {
  708. return (pDMA->DMACOMMON[0].ACTIVE & (1 << ch)) != 0;
  709. }
  710. /**
  711. * @brief Sets up a DMA channel with the passed DMA transfer descriptor
  712. * @param pDMA : The base of DMA controller on the chip
  713. * @param ch : DMA channel ID
  714. * @param desc : Pointer to DMA transfer descriptor
  715. * @return false if the DMA channel was active, otherwise true
  716. * @note This function will set the DMA descriptor in the SRAM table to the
  717. * the passed descriptor. This function is only meant to be used when
  718. * the DMA channel is not active and can be used to setup the
  719. * initial transfer for a linked list or ping-pong buffer or just a
  720. * single transfer without a next descriptor.<br>
  721. *
  722. * If using this function to write the initial transfer descriptor in
  723. * a linked list or ping-pong buffer configuration, it should contain a
  724. * non-NULL 'next' field pointer.
  725. */
  726. STATIC INLINE bool Chip_DMA_SetupTranChannel(LPC_DMA_T *pDMA, DMA_CHID_T ch, const DMA_CHDESC_T *desc)
  727. {
  728. /* If channel is active return false */
  729. if (Chip_DMA_IsChannelActive(pDMA, ch))
  730. return false;
  731. /* Assign the descriptor to descriptor table */
  732. ((DMA_CHDESC_T *) (pDMA->SRAMBASE & ~DMA_SRAMBASE_RESERVED))[ch] = *desc;
  733. return true;
  734. }
  735. /**
  736. * @}
  737. */
  738. #endif /* __DMA_8XX_H_ */