fsl_edma.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_EDMA_H_
  31. #define _FSL_EDMA_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup edma
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief eDMA driver version */
  43. #define FSL_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 1, 1)) /*!< Version 2.1.1. */
  44. /*@}*/
  45. /*! @brief Compute the offset unit from DCHPRI3 */
  46. #define DMA_DCHPRI_INDEX(channel) (((channel) & ~0x03U) | (3 - ((channel)&0x03U)))
  47. /*! @brief Get the pointer of DCHPRIn */
  48. #define DMA_DCHPRIn(base, channel) ((volatile uint8_t *)&(base->DCHPRI3))[DMA_DCHPRI_INDEX(channel)]
  49. /*! @brief eDMA transfer configuration */
  50. typedef enum _edma_transfer_size
  51. {
  52. kEDMA_TransferSize1Bytes = 0x0U, /*!< Source/Destination data transfer size is 1 byte every time */
  53. kEDMA_TransferSize2Bytes = 0x1U, /*!< Source/Destination data transfer size is 2 bytes every time */
  54. kEDMA_TransferSize4Bytes = 0x2U, /*!< Source/Destination data transfer size is 4 bytes every time */
  55. kEDMA_TransferSize16Bytes = 0x4U, /*!< Source/Destination data transfer size is 16 bytes every time */
  56. kEDMA_TransferSize32Bytes = 0x5U, /*!< Source/Destination data transfer size is 32 bytes every time */
  57. } edma_transfer_size_t;
  58. /*! @brief eDMA modulo configuration */
  59. typedef enum _edma_modulo
  60. {
  61. kEDMA_ModuloDisable = 0x0U, /*!< Disable modulo */
  62. kEDMA_Modulo2bytes, /*!< Circular buffer size is 2 bytes. */
  63. kEDMA_Modulo4bytes, /*!< Circular buffer size is 4 bytes. */
  64. kEDMA_Modulo8bytes, /*!< Circular buffer size is 8 bytes. */
  65. kEDMA_Modulo16bytes, /*!< Circular buffer size is 16 bytes. */
  66. kEDMA_Modulo32bytes, /*!< Circular buffer size is 32 bytes. */
  67. kEDMA_Modulo64bytes, /*!< Circular buffer size is 64 bytes. */
  68. kEDMA_Modulo128bytes, /*!< Circular buffer size is 128 bytes. */
  69. kEDMA_Modulo256bytes, /*!< Circular buffer size is 256 bytes. */
  70. kEDMA_Modulo512bytes, /*!< Circular buffer size is 512 bytes. */
  71. kEDMA_Modulo1Kbytes, /*!< Circular buffer size is 1 K bytes. */
  72. kEDMA_Modulo2Kbytes, /*!< Circular buffer size is 2 K bytes. */
  73. kEDMA_Modulo4Kbytes, /*!< Circular buffer size is 4 K bytes. */
  74. kEDMA_Modulo8Kbytes, /*!< Circular buffer size is 8 K bytes. */
  75. kEDMA_Modulo16Kbytes, /*!< Circular buffer size is 16 K bytes. */
  76. kEDMA_Modulo32Kbytes, /*!< Circular buffer size is 32 K bytes. */
  77. kEDMA_Modulo64Kbytes, /*!< Circular buffer size is 64 K bytes. */
  78. kEDMA_Modulo128Kbytes, /*!< Circular buffer size is 128 K bytes. */
  79. kEDMA_Modulo256Kbytes, /*!< Circular buffer size is 256 K bytes. */
  80. kEDMA_Modulo512Kbytes, /*!< Circular buffer size is 512 K bytes. */
  81. kEDMA_Modulo1Mbytes, /*!< Circular buffer size is 1 M bytes. */
  82. kEDMA_Modulo2Mbytes, /*!< Circular buffer size is 2 M bytes. */
  83. kEDMA_Modulo4Mbytes, /*!< Circular buffer size is 4 M bytes. */
  84. kEDMA_Modulo8Mbytes, /*!< Circular buffer size is 8 M bytes. */
  85. kEDMA_Modulo16Mbytes, /*!< Circular buffer size is 16 M bytes. */
  86. kEDMA_Modulo32Mbytes, /*!< Circular buffer size is 32 M bytes. */
  87. kEDMA_Modulo64Mbytes, /*!< Circular buffer size is 64 M bytes. */
  88. kEDMA_Modulo128Mbytes, /*!< Circular buffer size is 128 M bytes. */
  89. kEDMA_Modulo256Mbytes, /*!< Circular buffer size is 256 M bytes. */
  90. kEDMA_Modulo512Mbytes, /*!< Circular buffer size is 512 M bytes. */
  91. kEDMA_Modulo1Gbytes, /*!< Circular buffer size is 1 G bytes. */
  92. kEDMA_Modulo2Gbytes, /*!< Circular buffer size is 2 G bytes. */
  93. } edma_modulo_t;
  94. /*! @brief Bandwidth control */
  95. typedef enum _edma_bandwidth
  96. {
  97. kEDMA_BandwidthStallNone = 0x0U, /*!< No eDMA engine stalls. */
  98. kEDMA_BandwidthStall4Cycle = 0x2U, /*!< eDMA engine stalls for 4 cycles after each read/write. */
  99. kEDMA_BandwidthStall8Cycle = 0x3U, /*!< eDMA engine stalls for 8 cycles after each read/write. */
  100. } edma_bandwidth_t;
  101. /*! @brief Channel link type */
  102. typedef enum _edma_channel_link_type
  103. {
  104. kEDMA_LinkNone = 0x0U, /*!< No channel link */
  105. kEDMA_MinorLink, /*!< Channel link after each minor loop */
  106. kEDMA_MajorLink, /*!< Channel link while major loop count exhausted */
  107. } edma_channel_link_type_t;
  108. /*!@brief eDMA channel status flags. */
  109. enum _edma_channel_status_flags
  110. {
  111. kEDMA_DoneFlag = 0x1U, /*!< DONE flag, set while transfer finished, CITER value exhausted*/
  112. kEDMA_ErrorFlag = 0x2U, /*!< eDMA error flag, an error occurred in a transfer */
  113. kEDMA_InterruptFlag = 0x4U, /*!< eDMA interrupt flag, set while an interrupt occurred of this channel */
  114. };
  115. /*! @brief eDMA channel error status flags. */
  116. enum _edma_error_status_flags
  117. {
  118. kEDMA_DestinationBusErrorFlag = DMA_ES_DBE_MASK, /*!< Bus error on destination address */
  119. kEDMA_SourceBusErrorFlag = DMA_ES_SBE_MASK, /*!< Bus error on the source address */
  120. kEDMA_ScatterGatherErrorFlag = DMA_ES_SGE_MASK, /*!< Error on the Scatter/Gather address, not 32byte aligned. */
  121. kEDMA_NbytesErrorFlag = DMA_ES_NCE_MASK, /*!< NBYTES/CITER configuration error */
  122. kEDMA_DestinationOffsetErrorFlag = DMA_ES_DOE_MASK, /*!< Destination offset not aligned with destination size */
  123. kEDMA_DestinationAddressErrorFlag = DMA_ES_DAE_MASK, /*!< Destination address not aligned with destination size */
  124. kEDMA_SourceOffsetErrorFlag = DMA_ES_SOE_MASK, /*!< Source offset not aligned with source size */
  125. kEDMA_SourceAddressErrorFlag = DMA_ES_SAE_MASK, /*!< Source address not aligned with source size*/
  126. kEDMA_ErrorChannelFlag = DMA_ES_ERRCHN_MASK, /*!< Error channel number of the cancelled channel number */
  127. kEDMA_ChannelPriorityErrorFlag = DMA_ES_CPE_MASK, /*!< Channel priority is not unique. */
  128. kEDMA_TransferCanceledFlag = DMA_ES_ECX_MASK, /*!< Transfer cancelled */
  129. #if defined(FSL_FEATURE_EDMA_CHANNEL_GROUP_COUNT) && FSL_FEATURE_EDMA_CHANNEL_GROUP_COUNT > 1
  130. kEDMA_GroupPriorityErrorFlag = DMA_ES_GPE_MASK, /*!< Group priority is not unique. */
  131. #endif
  132. kEDMA_ValidFlag = DMA_ES_VLD_MASK, /*!< No error occurred, this bit is 0. Otherwise, it is 1. */
  133. };
  134. /*! @brief eDMA interrupt source */
  135. typedef enum _edma_interrupt_enable
  136. {
  137. kEDMA_ErrorInterruptEnable = 0x1U, /*!< Enable interrupt while channel error occurs. */
  138. kEDMA_MajorInterruptEnable = DMA_CSR_INTMAJOR_MASK, /*!< Enable interrupt while major count exhausted. */
  139. kEDMA_HalfInterruptEnable = DMA_CSR_INTHALF_MASK, /*!< Enable interrupt while major count to half value. */
  140. } edma_interrupt_enable_t;
  141. /*! @brief eDMA transfer type */
  142. typedef enum _edma_transfer_type
  143. {
  144. kEDMA_MemoryToMemory = 0x0U, /*!< Transfer from memory to memory */
  145. kEDMA_PeripheralToMemory, /*!< Transfer from peripheral to memory */
  146. kEDMA_MemoryToPeripheral, /*!< Transfer from memory to peripheral */
  147. } edma_transfer_type_t;
  148. /*! @brief eDMA transfer status */
  149. enum _edma_transfer_status
  150. {
  151. kStatus_EDMA_QueueFull = MAKE_STATUS(kStatusGroup_EDMA, 0), /*!< TCD queue is full. */
  152. kStatus_EDMA_Busy = MAKE_STATUS(kStatusGroup_EDMA, 1), /*!< Channel is busy and can't handle the
  153. transfer request. */
  154. };
  155. /*! @brief eDMA global configuration structure.*/
  156. typedef struct _edma_config
  157. {
  158. bool enableContinuousLinkMode; /*!< Enable (true) continuous link mode. Upon minor loop completion, the channel
  159. activates again if that channel has a minor loop channel link enabled and
  160. the link channel is itself. */
  161. bool enableHaltOnError; /*!< Enable (true) transfer halt on error. Any error causes the HALT bit to set.
  162. Subsequently, all service requests are ignored until the HALT bit is cleared.*/
  163. bool enableRoundRobinArbitration; /*!< Enable (true) round robin channel arbitration method or fixed priority
  164. arbitration is used for channel selection */
  165. bool enableDebugMode; /*!< Enable(true) eDMA debug mode. When in debug mode, the eDMA stalls the start of
  166. a new channel. Executing channels are allowed to complete. */
  167. } edma_config_t;
  168. /*!
  169. * @brief eDMA transfer configuration
  170. *
  171. * This structure configures the source/destination transfer attribute.
  172. * This figure shows the eDMA's transfer model:
  173. * _________________________________________________
  174. * | Transfer Size | |
  175. * Minor Loop |_______________| Major loop Count 1 |
  176. * Bytes | Transfer Size | |
  177. * ____________|_______________|____________________|--> Minor loop complete
  178. * ____________________________________
  179. * | | |
  180. * |_______________| Major Loop Count 2 |
  181. * | | |
  182. * |_______________|____________________|--> Minor loop Complete
  183. *
  184. * ---------------------------------------------------------> Transfer complete
  185. */
  186. typedef struct _edma_transfer_config
  187. {
  188. uint32_t srcAddr; /*!< Source data address. */
  189. uint32_t destAddr; /*!< Destination data address. */
  190. edma_transfer_size_t srcTransferSize; /*!< Source data transfer size. */
  191. edma_transfer_size_t destTransferSize; /*!< Destination data transfer size. */
  192. int16_t srcOffset; /*!< Sign-extended offset applied to the current source address to
  193. form the next-state value as each source read is completed. */
  194. int16_t destOffset; /*!< Sign-extended offset applied to the current destination address to
  195. form the next-state value as each destination write is completed. */
  196. uint32_t minorLoopBytes; /*!< Bytes to transfer in a minor loop*/
  197. uint32_t majorLoopCounts; /*!< Major loop iteration count. */
  198. } edma_transfer_config_t;
  199. /*! @brief eDMA channel priority configuration */
  200. typedef struct _edma_channel_Preemption_config
  201. {
  202. bool enableChannelPreemption; /*!< If true: a channel can be suspended by other channel with higher priority */
  203. bool enablePreemptAbility; /*!< If true: a channel can suspend other channel with low priority */
  204. uint8_t channelPriority; /*!< Channel priority */
  205. } edma_channel_Preemption_config_t;
  206. /*! @brief eDMA minor offset configuration */
  207. typedef struct _edma_minor_offset_config
  208. {
  209. bool enableSrcMinorOffset; /*!< Enable(true) or Disable(false) source minor loop offset. */
  210. bool enableDestMinorOffset; /*!< Enable(true) or Disable(false) destination minor loop offset. */
  211. uint32_t minorOffset; /*!< Offset for a minor loop mapping. */
  212. } edma_minor_offset_config_t;
  213. /*!
  214. * @brief eDMA TCD.
  215. *
  216. * This structure is same as TCD register which is described in reference manual,
  217. * and is used to configure the scatter/gather feature as a next hardware TCD.
  218. */
  219. typedef struct _edma_tcd
  220. {
  221. __IO uint32_t SADDR; /*!< SADDR register, used to save source address */
  222. __IO uint16_t SOFF; /*!< SOFF register, save offset bytes every transfer */
  223. __IO uint16_t ATTR; /*!< ATTR register, source/destination transfer size and modulo */
  224. __IO uint32_t NBYTES; /*!< Nbytes register, minor loop length in bytes */
  225. __IO uint32_t SLAST; /*!< SLAST register */
  226. __IO uint32_t DADDR; /*!< DADDR register, used for destination address */
  227. __IO uint16_t DOFF; /*!< DOFF register, used for destination offset */
  228. __IO uint16_t CITER; /*!< CITER register, current minor loop numbers, for unfinished minor loop.*/
  229. __IO uint32_t DLAST_SGA; /*!< DLASTSGA register, next stcd address used in scatter-gather mode */
  230. __IO uint16_t CSR; /*!< CSR register, for TCD control status */
  231. __IO uint16_t BITER; /*!< BITER register, begin minor loop count. */
  232. } edma_tcd_t;
  233. /*! @brief Callback for eDMA */
  234. struct _edma_handle;
  235. /*! @brief Define callback function for eDMA. */
  236. typedef void (*edma_callback)(struct _edma_handle *handle, void *userData, bool transferDone, uint32_t tcds);
  237. /*! @brief eDMA transfer handle structure */
  238. typedef struct _edma_handle
  239. {
  240. edma_callback callback; /*!< Callback function for major count exhausted. */
  241. void *userData; /*!< Callback function parameter. */
  242. DMA_Type *base; /*!< eDMA peripheral base address. */
  243. edma_tcd_t *tcdPool; /*!< Pointer to memory stored TCDs. */
  244. uint8_t channel; /*!< eDMA channel number. */
  245. volatile int8_t header; /*!< The first TCD index. Should point to the next TCD to be loaded into the eDMA engine. */
  246. volatile int8_t tail; /*!< The last TCD index. Should point to the next TCD to be stored into the memory pool. */
  247. volatile int8_t tcdUsed; /*!< The number of used TCD slots. Should reflect the number of TCDs can be used/loaded in
  248. the memory. */
  249. volatile int8_t tcdSize; /*!< The total number of TCD slots in the queue. */
  250. uint8_t flags; /*!< The status of the current channel. */
  251. } edma_handle_t;
  252. /*******************************************************************************
  253. * APIs
  254. ******************************************************************************/
  255. #if defined(__cplusplus)
  256. extern "C" {
  257. #endif /* __cplusplus */
  258. /*!
  259. * @name eDMA initialization and de-initialization
  260. * @{
  261. */
  262. /*!
  263. * @brief Initializes the eDMA peripheral.
  264. *
  265. * This function ungates the eDMA clock and configures the eDMA peripheral according
  266. * to the configuration structure.
  267. *
  268. * @param base eDMA peripheral base address.
  269. * @param config A pointer to the configuration structure, see "edma_config_t".
  270. * @note This function enables the minor loop map feature.
  271. */
  272. void EDMA_Init(DMA_Type *base, const edma_config_t *config);
  273. /*!
  274. * @brief Deinitializes the eDMA peripheral.
  275. *
  276. * This function gates the eDMA clock.
  277. *
  278. * @param base eDMA peripheral base address.
  279. */
  280. void EDMA_Deinit(DMA_Type *base);
  281. /*!
  282. * @brief Gets the eDMA default configuration structure.
  283. *
  284. * This function sets the configuration structure to default values.
  285. * The default configuration is set to the following values.
  286. * @code
  287. * config.enableContinuousLinkMode = false;
  288. * config.enableHaltOnError = true;
  289. * config.enableRoundRobinArbitration = false;
  290. * config.enableDebugMode = false;
  291. * @endcode
  292. *
  293. * @param config A pointer to the eDMA configuration structure.
  294. */
  295. void EDMA_GetDefaultConfig(edma_config_t *config);
  296. /* @} */
  297. /*!
  298. * @name eDMA Channel Operation
  299. * @{
  300. */
  301. /*!
  302. * @brief Sets all TCD registers to default values.
  303. *
  304. * This function sets TCD registers for this channel to default values.
  305. *
  306. * @param base eDMA peripheral base address.
  307. * @param channel eDMA channel number.
  308. * @note This function must not be called while the channel transfer is ongoing
  309. * or it causes unpredictable results.
  310. * @note This function enables the auto stop request feature.
  311. */
  312. void EDMA_ResetChannel(DMA_Type *base, uint32_t channel);
  313. /*!
  314. * @brief Configures the eDMA transfer attribute.
  315. *
  316. * This function configures the transfer attribute, including source address, destination address,
  317. * transfer size, address offset, and so on. It also configures the scatter gather feature if the
  318. * user supplies the TCD address.
  319. * Example:
  320. * @code
  321. * edma_transfer_t config;
  322. * edma_tcd_t tcd;
  323. * config.srcAddr = ..;
  324. * config.destAddr = ..;
  325. * ...
  326. * EDMA_SetTransferConfig(DMA0, channel, &config, &stcd);
  327. * @endcode
  328. *
  329. * @param base eDMA peripheral base address.
  330. * @param channel eDMA channel number.
  331. * @param config Pointer to eDMA transfer configuration structure.
  332. * @param nextTcd Point to TCD structure. It can be NULL if users
  333. * do not want to enable scatter/gather feature.
  334. * @note If nextTcd is not NULL, it means scatter gather feature is enabled
  335. * and DREQ bit is cleared in the previous transfer configuration, which
  336. * is set in the eDMA_ResetChannel.
  337. */
  338. void EDMA_SetTransferConfig(DMA_Type *base,
  339. uint32_t channel,
  340. const edma_transfer_config_t *config,
  341. edma_tcd_t *nextTcd);
  342. /*!
  343. * @brief Configures the eDMA minor offset feature.
  344. *
  345. * The minor offset means that the signed-extended value is added to the source address or destination
  346. * address after each minor loop.
  347. *
  348. * @param base eDMA peripheral base address.
  349. * @param channel eDMA channel number.
  350. * @param config A pointer to the minor offset configuration structure.
  351. */
  352. void EDMA_SetMinorOffsetConfig(DMA_Type *base, uint32_t channel, const edma_minor_offset_config_t *config);
  353. /*!
  354. * @brief Configures the eDMA channel preemption feature.
  355. *
  356. * This function configures the channel preemption attribute and the priority of the channel.
  357. *
  358. * @param base eDMA peripheral base address.
  359. * @param channel eDMA channel number
  360. * @param config A pointer to the channel preemption configuration structure.
  361. */
  362. static inline void EDMA_SetChannelPreemptionConfig(DMA_Type *base,
  363. uint32_t channel,
  364. const edma_channel_Preemption_config_t *config)
  365. {
  366. assert(channel < FSL_FEATURE_EDMA_MODULE_CHANNEL);
  367. assert(config != NULL);
  368. DMA_DCHPRIn(base, channel) =
  369. (DMA_DCHPRI0_DPA(!config->enablePreemptAbility) | DMA_DCHPRI0_ECP(config->enableChannelPreemption) |
  370. DMA_DCHPRI0_CHPRI(config->channelPriority));
  371. }
  372. /*!
  373. * @brief Sets the channel link for the eDMA transfer.
  374. *
  375. * This function configures either the minor link or the major link mode. The minor link means that the channel link is
  376. * triggered every time CITER decreases by 1. The major link means that the channel link is triggered when the CITER is
  377. * exhausted.
  378. *
  379. * @param base eDMA peripheral base address.
  380. * @param channel eDMA channel number.
  381. * @param type A channel link type, which can be one of the following:
  382. * @arg kEDMA_LinkNone
  383. * @arg kEDMA_MinorLink
  384. * @arg kEDMA_MajorLink
  385. * @param linkedChannel The linked channel number.
  386. * @note Users should ensure that DONE flag is cleared before calling this interface, or the configuration is invalid.
  387. */
  388. void EDMA_SetChannelLink(DMA_Type *base, uint32_t channel, edma_channel_link_type_t type, uint32_t linkedChannel);
  389. /*!
  390. * @brief Sets the bandwidth for the eDMA transfer.
  391. *
  392. * Because the eDMA processes the minor loop, it continuously generates read/write sequences
  393. * until the minor count is exhausted. The bandwidth forces the eDMA to stall after the completion of
  394. * each read/write access to control the bus request bandwidth seen by the crossbar switch.
  395. *
  396. * @param base eDMA peripheral base address.
  397. * @param channel eDMA channel number.
  398. * @param bandWidth A bandwidth setting, which can be one of the following:
  399. * @arg kEDMABandwidthStallNone
  400. * @arg kEDMABandwidthStall4Cycle
  401. * @arg kEDMABandwidthStall8Cycle
  402. */
  403. void EDMA_SetBandWidth(DMA_Type *base, uint32_t channel, edma_bandwidth_t bandWidth);
  404. /*!
  405. * @brief Sets the source modulo and the destination modulo for the eDMA transfer.
  406. *
  407. * This function defines a specific address range specified to be the value after (SADDR + SOFF)/(DADDR + DOFF)
  408. * calculation is performed or the original register value. It provides the ability to implement a circular data
  409. * queue easily.
  410. *
  411. * @param base eDMA peripheral base address.
  412. * @param channel eDMA channel number.
  413. * @param srcModulo A source modulo value.
  414. * @param destModulo A destination modulo value.
  415. */
  416. void EDMA_SetModulo(DMA_Type *base, uint32_t channel, edma_modulo_t srcModulo, edma_modulo_t destModulo);
  417. #if defined(FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT) && FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT
  418. /*!
  419. * @brief Enables an async request for the eDMA transfer.
  420. *
  421. * @param base eDMA peripheral base address.
  422. * @param channel eDMA channel number.
  423. * @param enable The command to enable (true) or disable (false).
  424. */
  425. static inline void EDMA_EnableAsyncRequest(DMA_Type *base, uint32_t channel, bool enable)
  426. {
  427. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  428. base->EARS = (base->EARS & (~(1U << channel))) | ((uint32_t)enable << channel);
  429. }
  430. #endif /* FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT */
  431. /*!
  432. * @brief Enables an auto stop request for the eDMA transfer.
  433. *
  434. * If enabling the auto stop request, the eDMA hardware automatically disables the hardware channel request.
  435. *
  436. * @param base eDMA peripheral base address.
  437. * @param channel eDMA channel number.
  438. * @param enable The command to enable (true) or disable (false).
  439. */
  440. static inline void EDMA_EnableAutoStopRequest(DMA_Type *base, uint32_t channel, bool enable)
  441. {
  442. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  443. base->TCD[channel].CSR = (base->TCD[channel].CSR & (~DMA_CSR_DREQ_MASK)) | DMA_CSR_DREQ(enable);
  444. }
  445. /*!
  446. * @brief Enables the interrupt source for the eDMA transfer.
  447. *
  448. * @param base eDMA peripheral base address.
  449. * @param channel eDMA channel number.
  450. * @param mask The mask of interrupt source to be set. Users need to use
  451. * the defined edma_interrupt_enable_t type.
  452. */
  453. void EDMA_EnableChannelInterrupts(DMA_Type *base, uint32_t channel, uint32_t mask);
  454. /*!
  455. * @brief Disables the interrupt source for the eDMA transfer.
  456. *
  457. * @param base eDMA peripheral base address.
  458. * @param channel eDMA channel number.
  459. * @param mask The mask of the interrupt source to be set. Use
  460. * the defined edma_interrupt_enable_t type.
  461. */
  462. void EDMA_DisableChannelInterrupts(DMA_Type *base, uint32_t channel, uint32_t mask);
  463. /* @} */
  464. /*!
  465. * @name eDMA TCD Operation
  466. * @{
  467. */
  468. /*!
  469. * @brief Sets all fields to default values for the TCD structure.
  470. *
  471. * This function sets all fields for this TCD structure to default value.
  472. *
  473. * @param tcd Pointer to the TCD structure.
  474. * @note This function enables the auto stop request feature.
  475. */
  476. void EDMA_TcdReset(edma_tcd_t *tcd);
  477. /*!
  478. * @brief Configures the eDMA TCD transfer attribute.
  479. *
  480. * The TCD is a transfer control descriptor. The content of the TCD is the same as the hardware TCD registers.
  481. * The STCD is used in the scatter-gather mode.
  482. * This function configures the TCD transfer attribute, including source address, destination address,
  483. * transfer size, address offset, and so on. It also configures the scatter gather feature if the
  484. * user supplies the next TCD address.
  485. * Example:
  486. * @code
  487. * edma_transfer_t config = {
  488. * ...
  489. * }
  490. * edma_tcd_t tcd __aligned(32);
  491. * edma_tcd_t nextTcd __aligned(32);
  492. * EDMA_TcdSetTransferConfig(&tcd, &config, &nextTcd);
  493. * @endcode
  494. *
  495. * @param tcd Pointer to the TCD structure.
  496. * @param config Pointer to eDMA transfer configuration structure.
  497. * @param nextTcd Pointer to the next TCD structure. It can be NULL if users
  498. * do not want to enable scatter/gather feature.
  499. * @note TCD address should be 32 bytes aligned or it causes an eDMA error.
  500. * @note If the nextTcd is not NULL, the scatter gather feature is enabled
  501. * and DREQ bit is cleared in the previous transfer configuration, which
  502. * is set in the EDMA_TcdReset.
  503. */
  504. void EDMA_TcdSetTransferConfig(edma_tcd_t *tcd, const edma_transfer_config_t *config, edma_tcd_t *nextTcd);
  505. /*!
  506. * @brief Configures the eDMA TCD minor offset feature.
  507. *
  508. * A minor offset is a signed-extended value added to the source address or a destination
  509. * address after each minor loop.
  510. *
  511. * @param tcd A point to the TCD structure.
  512. * @param config A pointer to the minor offset configuration structure.
  513. */
  514. void EDMA_TcdSetMinorOffsetConfig(edma_tcd_t *tcd, const edma_minor_offset_config_t *config);
  515. /*!
  516. * @brief Sets the channel link for the eDMA TCD.
  517. *
  518. * This function configures either a minor link or a major link. The minor link means the channel link is
  519. * triggered every time CITER decreases by 1. The major link means that the channel link is triggered when the CITER is
  520. * exhausted.
  521. *
  522. * @note Users should ensure that DONE flag is cleared before calling this interface, or the configuration is invalid.
  523. * @param tcd Point to the TCD structure.
  524. * @param type Channel link type, it can be one of:
  525. * @arg kEDMA_LinkNone
  526. * @arg kEDMA_MinorLink
  527. * @arg kEDMA_MajorLink
  528. * @param linkedChannel The linked channel number.
  529. */
  530. void EDMA_TcdSetChannelLink(edma_tcd_t *tcd, edma_channel_link_type_t type, uint32_t linkedChannel);
  531. /*!
  532. * @brief Sets the bandwidth for the eDMA TCD.
  533. *
  534. * Because the eDMA processes the minor loop, it continuously generates read/write sequences
  535. * until the minor count is exhausted. The bandwidth forces the eDMA to stall after the completion of
  536. * each read/write access to control the bus request bandwidth seen by the crossbar switch.
  537. * @param tcd A pointer to the TCD structure.
  538. * @param bandWidth A bandwidth setting, which can be one of the following:
  539. * @arg kEDMABandwidthStallNone
  540. * @arg kEDMABandwidthStall4Cycle
  541. * @arg kEDMABandwidthStall8Cycle
  542. */
  543. static inline void EDMA_TcdSetBandWidth(edma_tcd_t *tcd, edma_bandwidth_t bandWidth)
  544. {
  545. assert(tcd != NULL);
  546. assert(((uint32_t)tcd & 0x1FU) == 0);
  547. tcd->CSR = (tcd->CSR & (~DMA_CSR_BWC_MASK)) | DMA_CSR_BWC(bandWidth);
  548. }
  549. /*!
  550. * @brief Sets the source modulo and the destination modulo for the eDMA TCD.
  551. *
  552. * This function defines a specific address range specified to be the value after (SADDR + SOFF)/(DADDR + DOFF)
  553. * calculation is performed or the original register value. It provides the ability to implement a circular data
  554. * queue easily.
  555. *
  556. * @param tcd A pointer to the TCD structure.
  557. * @param srcModulo A source modulo value.
  558. * @param destModulo A destination modulo value.
  559. */
  560. void EDMA_TcdSetModulo(edma_tcd_t *tcd, edma_modulo_t srcModulo, edma_modulo_t destModulo);
  561. /*!
  562. * @brief Sets the auto stop request for the eDMA TCD.
  563. *
  564. * If enabling the auto stop request, the eDMA hardware automatically disables the hardware channel request.
  565. *
  566. * @param tcd A pointer to the TCD structure.
  567. * @param enable The command to enable (true) or disable (false).
  568. */
  569. static inline void EDMA_TcdEnableAutoStopRequest(edma_tcd_t *tcd, bool enable)
  570. {
  571. assert(tcd != NULL);
  572. assert(((uint32_t)tcd & 0x1FU) == 0);
  573. tcd->CSR = (tcd->CSR & (~DMA_CSR_DREQ_MASK)) | DMA_CSR_DREQ(enable);
  574. }
  575. /*!
  576. * @brief Enables the interrupt source for the eDMA TCD.
  577. *
  578. * @param tcd Point to the TCD structure.
  579. * @param mask The mask of interrupt source to be set. Users need to use
  580. * the defined edma_interrupt_enable_t type.
  581. */
  582. void EDMA_TcdEnableInterrupts(edma_tcd_t *tcd, uint32_t mask);
  583. /*!
  584. * @brief Disables the interrupt source for the eDMA TCD.
  585. *
  586. * @param tcd Point to the TCD structure.
  587. * @param mask The mask of interrupt source to be set. Users need to use
  588. * the defined edma_interrupt_enable_t type.
  589. */
  590. void EDMA_TcdDisableInterrupts(edma_tcd_t *tcd, uint32_t mask);
  591. /*! @} */
  592. /*!
  593. * @name eDMA Channel Transfer Operation
  594. * @{
  595. */
  596. /*!
  597. * @brief Enables the eDMA hardware channel request.
  598. *
  599. * This function enables the hardware channel request.
  600. *
  601. * @param base eDMA peripheral base address.
  602. * @param channel eDMA channel number.
  603. */
  604. static inline void EDMA_EnableChannelRequest(DMA_Type *base, uint32_t channel)
  605. {
  606. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  607. base->SERQ = DMA_SERQ_SERQ(channel);
  608. }
  609. /*!
  610. * @brief Disables the eDMA hardware channel request.
  611. *
  612. * This function disables the hardware channel request.
  613. *
  614. * @param base eDMA peripheral base address.
  615. * @param channel eDMA channel number.
  616. */
  617. static inline void EDMA_DisableChannelRequest(DMA_Type *base, uint32_t channel)
  618. {
  619. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  620. base->CERQ = DMA_CERQ_CERQ(channel);
  621. }
  622. /*!
  623. * @brief Starts the eDMA transfer by using the software trigger.
  624. *
  625. * This function starts a minor loop transfer.
  626. *
  627. * @param base eDMA peripheral base address.
  628. * @param channel eDMA channel number.
  629. */
  630. static inline void EDMA_TriggerChannelStart(DMA_Type *base, uint32_t channel)
  631. {
  632. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  633. base->SSRT = DMA_SSRT_SSRT(channel);
  634. }
  635. /*! @} */
  636. /*!
  637. * @name eDMA Channel Status Operation
  638. * @{
  639. */
  640. /*!
  641. * @brief Gets the remaining major loop count from the eDMA current channel TCD.
  642. *
  643. * This function checks the TCD (Task Control Descriptor) status for a specified
  644. * eDMA channel and returns the the number of major loop count that has not finished.
  645. *
  646. * @param base eDMA peripheral base address.
  647. * @param channel eDMA channel number.
  648. * @return Major loop count which has not been transferred yet for the current TCD.
  649. * @note 1. This function can only be used to get unfinished major loop count of transfer without
  650. * the next TCD, or it might be inaccuracy.
  651. * 2. The unfinished/remaining transfer bytes cannot be obtained directly from registers while
  652. * the channel is running.
  653. * Because to calculate the remaining bytes, the initial NBYTES configured in DMA_TCDn_NBYTES_MLNO
  654. * register is needed while the eDMA IP does not support getting it while a channel is active.
  655. * In another word, the NBYTES value reading is always the actual (decrementing) NBYTES value the dma_engine
  656. * is working with while a channel is running.
  657. * Consequently, to get the remaining transfer bytes, a software-saved initial value of NBYTES (for example
  658. * copied before enabling the channel) is needed. The formula to calculate it is shown below:
  659. * RemainingBytes = RemainingMajorLoopCount * NBYTES(initially configured)
  660. */
  661. uint32_t EDMA_GetRemainingMajorLoopCount(DMA_Type *base, uint32_t channel);
  662. /*!
  663. * @brief Gets the eDMA channel error status flags.
  664. *
  665. * @param base eDMA peripheral base address.
  666. * @return The mask of error status flags. Users need to use the
  667. * _edma_error_status_flags type to decode the return variables.
  668. */
  669. static inline uint32_t EDMA_GetErrorStatusFlags(DMA_Type *base)
  670. {
  671. return base->ES;
  672. }
  673. /*!
  674. * @brief Gets the eDMA channel status flags.
  675. *
  676. * @param base eDMA peripheral base address.
  677. * @param channel eDMA channel number.
  678. * @return The mask of channel status flags. Users need to use the
  679. * _edma_channel_status_flags type to decode the return variables.
  680. */
  681. uint32_t EDMA_GetChannelStatusFlags(DMA_Type *base, uint32_t channel);
  682. /*!
  683. * @brief Clears the eDMA channel status flags.
  684. *
  685. * @param base eDMA peripheral base address.
  686. * @param channel eDMA channel number.
  687. * @param mask The mask of channel status to be cleared. Users need to use
  688. * the defined _edma_channel_status_flags type.
  689. */
  690. void EDMA_ClearChannelStatusFlags(DMA_Type *base, uint32_t channel, uint32_t mask);
  691. /*! @} */
  692. /*!
  693. * @name eDMA Transactional Operation
  694. */
  695. /*!
  696. * @brief Creates the eDMA handle.
  697. *
  698. * This function is called if using the transactional API for eDMA. This function
  699. * initializes the internal state of the eDMA handle.
  700. *
  701. * @param handle eDMA handle pointer. The eDMA handle stores callback function and
  702. * parameters.
  703. * @param base eDMA peripheral base address.
  704. * @param channel eDMA channel number.
  705. */
  706. void EDMA_CreateHandle(edma_handle_t *handle, DMA_Type *base, uint32_t channel);
  707. /*!
  708. * @brief Installs the TCDs memory pool into the eDMA handle.
  709. *
  710. * This function is called after the EDMA_CreateHandle to use scatter/gather feature.
  711. *
  712. * @param handle eDMA handle pointer.
  713. * @param tcdPool A memory pool to store TCDs. It must be 32 bytes aligned.
  714. * @param tcdSize The number of TCD slots.
  715. */
  716. void EDMA_InstallTCDMemory(edma_handle_t *handle, edma_tcd_t *tcdPool, uint32_t tcdSize);
  717. /*!
  718. * @brief Installs a callback function for the eDMA transfer.
  719. *
  720. * This callback is called in the eDMA IRQ handler. Use the callback to do something after
  721. * the current major loop transfer completes.
  722. *
  723. * @param handle eDMA handle pointer.
  724. * @param callback eDMA callback function pointer.
  725. * @param userData A parameter for the callback function.
  726. */
  727. void EDMA_SetCallback(edma_handle_t *handle, edma_callback callback, void *userData);
  728. /*!
  729. * @brief Prepares the eDMA transfer structure.
  730. *
  731. * This function prepares the transfer configuration structure according to the user input.
  732. *
  733. * @param config The user configuration structure of type edma_transfer_t.
  734. * @param srcAddr eDMA transfer source address.
  735. * @param srcWidth eDMA transfer source address width(bytes).
  736. * @param destAddr eDMA transfer destination address.
  737. * @param destWidth eDMA transfer destination address width(bytes).
  738. * @param bytesEachRequest eDMA transfer bytes per channel request.
  739. * @param transferBytes eDMA transfer bytes to be transferred.
  740. * @param type eDMA transfer type.
  741. * @note The data address and the data width must be consistent. For example, if the SRC
  742. * is 4 bytes, the source address must be 4 bytes aligned, or it results in
  743. * source address error (SAE).
  744. */
  745. void EDMA_PrepareTransfer(edma_transfer_config_t *config,
  746. void *srcAddr,
  747. uint32_t srcWidth,
  748. void *destAddr,
  749. uint32_t destWidth,
  750. uint32_t bytesEachRequest,
  751. uint32_t transferBytes,
  752. edma_transfer_type_t type);
  753. /*!
  754. * @brief Submits the eDMA transfer request.
  755. *
  756. * This function submits the eDMA transfer request according to the transfer configuration structure.
  757. * If submitting the transfer request repeatedly, this function packs an unprocessed request as
  758. * a TCD and enables scatter/gather feature to process it in the next time.
  759. *
  760. * @param handle eDMA handle pointer.
  761. * @param config Pointer to eDMA transfer configuration structure.
  762. * @retval kStatus_EDMA_Success It means submit transfer request succeed.
  763. * @retval kStatus_EDMA_QueueFull It means TCD queue is full. Submit transfer request is not allowed.
  764. * @retval kStatus_EDMA_Busy It means the given channel is busy, need to submit request later.
  765. */
  766. status_t EDMA_SubmitTransfer(edma_handle_t *handle, const edma_transfer_config_t *config);
  767. /*!
  768. * @brief eDMA starts transfer.
  769. *
  770. * This function enables the channel request. Users can call this function after submitting the transfer request
  771. * or before submitting the transfer request.
  772. *
  773. * @param handle eDMA handle pointer.
  774. */
  775. void EDMA_StartTransfer(edma_handle_t *handle);
  776. /*!
  777. * @brief eDMA stops transfer.
  778. *
  779. * This function disables the channel request to pause the transfer. Users can call EDMA_StartTransfer()
  780. * again to resume the transfer.
  781. *
  782. * @param handle eDMA handle pointer.
  783. */
  784. void EDMA_StopTransfer(edma_handle_t *handle);
  785. /*!
  786. * @brief eDMA aborts transfer.
  787. *
  788. * This function disables the channel request and clear transfer status bits.
  789. * Users can submit another transfer after calling this API.
  790. *
  791. * @param handle DMA handle pointer.
  792. */
  793. void EDMA_AbortTransfer(edma_handle_t *handle);
  794. /*!
  795. * @brief eDMA IRQ handler for the current major loop transfer completion.
  796. *
  797. * This function clears the channel major interrupt flag and calls
  798. * the callback function if it is not NULL.
  799. *
  800. * Note:
  801. * For the case using TCD queue, when the major iteration count is exhausted, additional operations are performed.
  802. * These include the final address adjustments and reloading of the BITER field into the CITER.
  803. * Assertion of an optional interrupt request also occurs at this time, as does a possible fetch of a new TCD from
  804. * memory using the scatter/gather address pointer included in the descriptor (if scatter/gather is enabled).
  805. *
  806. * For instance, when the time interrupt of TCD[0] happens, the TCD[1] has already been loaded into the eDMA engine.
  807. * As sga and sga_index are calculated based on the DLAST_SGA bitfield lies in the TCD_CSR register, the sga_index
  808. * in this case should be 2 (DLAST_SGA of TCD[1] stores the address of TCD[2]). Thus, the "tcdUsed" updated should be
  809. * (tcdUsed - 2U) which indicates the number of TCDs can be loaded in the memory pool (because TCD[0] and TCD[1] have
  810. * been loaded into the eDMA engine at this point already.).
  811. *
  812. * For the last two continuous ISRs in a scatter/gather process, they both load the last TCD (The last ISR does not
  813. * load a new TCD) from the memory pool to the eDMA engine when major loop completes.
  814. * Therefore, ensure that the header and tcdUsed updated are identical for them.
  815. * tcdUsed are both 0 in this case as no TCD to be loaded.
  816. *
  817. * See the "eDMA basic data flow" in the eDMA Functional description section of the Reference Manual for
  818. * further details.
  819. *
  820. * @param handle eDMA handle pointer.
  821. */
  822. void EDMA_HandleIRQ(edma_handle_t *handle);
  823. /* @} */
  824. #if defined(__cplusplus)
  825. }
  826. #endif /* __cplusplus */
  827. /* @} */
  828. #endif /*_FSL_EDMA_H_*/