fsl_edma.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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, 2)) /*!< Version 2.1.2. */
  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. */
  173. typedef struct _edma_transfer_config
  174. {
  175. uint32_t srcAddr; /*!< Source data address. */
  176. uint32_t destAddr; /*!< Destination data address. */
  177. edma_transfer_size_t srcTransferSize; /*!< Source data transfer size. */
  178. edma_transfer_size_t destTransferSize; /*!< Destination data transfer size. */
  179. int16_t srcOffset; /*!< Sign-extended offset applied to the current source address to
  180. form the next-state value as each source read is completed. */
  181. int16_t destOffset; /*!< Sign-extended offset applied to the current destination address to
  182. form the next-state value as each destination write is completed. */
  183. uint32_t minorLoopBytes; /*!< Bytes to transfer in a minor loop*/
  184. uint32_t majorLoopCounts; /*!< Major loop iteration count. */
  185. } edma_transfer_config_t;
  186. /*! @brief eDMA channel priority configuration */
  187. typedef struct _edma_channel_Preemption_config
  188. {
  189. bool enableChannelPreemption; /*!< If true: a channel can be suspended by other channel with higher priority */
  190. bool enablePreemptAbility; /*!< If true: a channel can suspend other channel with low priority */
  191. uint8_t channelPriority; /*!< Channel priority */
  192. } edma_channel_Preemption_config_t;
  193. /*! @brief eDMA minor offset configuration */
  194. typedef struct _edma_minor_offset_config
  195. {
  196. bool enableSrcMinorOffset; /*!< Enable(true) or Disable(false) source minor loop offset. */
  197. bool enableDestMinorOffset; /*!< Enable(true) or Disable(false) destination minor loop offset. */
  198. uint32_t minorOffset; /*!< Offset for a minor loop mapping. */
  199. } edma_minor_offset_config_t;
  200. /*!
  201. * @brief eDMA TCD.
  202. *
  203. * This structure is same as TCD register which is described in reference manual,
  204. * and is used to configure the scatter/gather feature as a next hardware TCD.
  205. */
  206. typedef struct _edma_tcd
  207. {
  208. __IO uint32_t SADDR; /*!< SADDR register, used to save source address */
  209. __IO uint16_t SOFF; /*!< SOFF register, save offset bytes every transfer */
  210. __IO uint16_t ATTR; /*!< ATTR register, source/destination transfer size and modulo */
  211. __IO uint32_t NBYTES; /*!< Nbytes register, minor loop length in bytes */
  212. __IO uint32_t SLAST; /*!< SLAST register */
  213. __IO uint32_t DADDR; /*!< DADDR register, used for destination address */
  214. __IO uint16_t DOFF; /*!< DOFF register, used for destination offset */
  215. __IO uint16_t CITER; /*!< CITER register, current minor loop numbers, for unfinished minor loop.*/
  216. __IO uint32_t DLAST_SGA; /*!< DLASTSGA register, next stcd address used in scatter-gather mode */
  217. __IO uint16_t CSR; /*!< CSR register, for TCD control status */
  218. __IO uint16_t BITER; /*!< BITER register, begin minor loop count. */
  219. } edma_tcd_t;
  220. /*! @brief Callback for eDMA */
  221. struct _edma_handle;
  222. /*! @brief Define callback function for eDMA. */
  223. typedef void (*edma_callback)(struct _edma_handle *handle, void *userData, bool transferDone, uint32_t tcds);
  224. /*! @brief eDMA transfer handle structure */
  225. typedef struct _edma_handle
  226. {
  227. edma_callback callback; /*!< Callback function for major count exhausted. */
  228. void *userData; /*!< Callback function parameter. */
  229. DMA_Type *base; /*!< eDMA peripheral base address. */
  230. edma_tcd_t *tcdPool; /*!< Pointer to memory stored TCDs. */
  231. uint8_t channel; /*!< eDMA channel number. */
  232. volatile int8_t header; /*!< The first TCD index. Should point to the next TCD to be loaded into the eDMA engine. */
  233. volatile int8_t tail; /*!< The last TCD index. Should point to the next TCD to be stored into the memory pool. */
  234. volatile int8_t tcdUsed; /*!< The number of used TCD slots. Should reflect the number of TCDs can be used/loaded in
  235. the memory. */
  236. volatile int8_t tcdSize; /*!< The total number of TCD slots in the queue. */
  237. uint8_t flags; /*!< The status of the current channel. */
  238. } edma_handle_t;
  239. /*******************************************************************************
  240. * APIs
  241. ******************************************************************************/
  242. #if defined(__cplusplus)
  243. extern "C" {
  244. #endif /* __cplusplus */
  245. /*!
  246. * @name eDMA initialization and de-initialization
  247. * @{
  248. */
  249. /*!
  250. * @brief Initializes the eDMA peripheral.
  251. *
  252. * This function ungates the eDMA clock and configures the eDMA peripheral according
  253. * to the configuration structure.
  254. *
  255. * @param base eDMA peripheral base address.
  256. * @param config A pointer to the configuration structure, see "edma_config_t".
  257. * @note This function enables the minor loop map feature.
  258. */
  259. void EDMA_Init(DMA_Type *base, const edma_config_t *config);
  260. /*!
  261. * @brief Deinitializes the eDMA peripheral.
  262. *
  263. * This function gates the eDMA clock.
  264. *
  265. * @param base eDMA peripheral base address.
  266. */
  267. void EDMA_Deinit(DMA_Type *base);
  268. /*!
  269. * @brief Push content of TCD structure into hardware TCD register.
  270. *
  271. * @param base EDMA peripheral base address.
  272. * @param channel EDMA channel number.
  273. * @param tcd Point to TCD structure.
  274. */
  275. void EDMA_InstallTCD(DMA_Type *base, uint32_t channel, edma_tcd_t *tcd);
  276. /*!
  277. * @brief Gets the eDMA default configuration structure.
  278. *
  279. * This function sets the configuration structure to default values.
  280. * The default configuration is set to the following values.
  281. * @code
  282. * config.enableContinuousLinkMode = false;
  283. * config.enableHaltOnError = true;
  284. * config.enableRoundRobinArbitration = false;
  285. * config.enableDebugMode = false;
  286. * @endcode
  287. *
  288. * @param config A pointer to the eDMA configuration structure.
  289. */
  290. void EDMA_GetDefaultConfig(edma_config_t *config);
  291. /* @} */
  292. /*!
  293. * @name eDMA Channel Operation
  294. * @{
  295. */
  296. /*!
  297. * @brief Sets all TCD registers to default values.
  298. *
  299. * This function sets TCD registers for this channel to default values.
  300. *
  301. * @param base eDMA peripheral base address.
  302. * @param channel eDMA channel number.
  303. * @note This function must not be called while the channel transfer is ongoing
  304. * or it causes unpredictable results.
  305. * @note This function enables the auto stop request feature.
  306. */
  307. void EDMA_ResetChannel(DMA_Type *base, uint32_t channel);
  308. /*!
  309. * @brief Configures the eDMA transfer attribute.
  310. *
  311. * This function configures the transfer attribute, including source address, destination address,
  312. * transfer size, address offset, and so on. It also configures the scatter gather feature if the
  313. * user supplies the TCD address.
  314. * Example:
  315. * @code
  316. * edma_transfer_t config;
  317. * edma_tcd_t tcd;
  318. * config.srcAddr = ..;
  319. * config.destAddr = ..;
  320. * ...
  321. * EDMA_SetTransferConfig(DMA0, channel, &config, &stcd);
  322. * @endcode
  323. *
  324. * @param base eDMA peripheral base address.
  325. * @param channel eDMA channel number.
  326. * @param config Pointer to eDMA transfer configuration structure.
  327. * @param nextTcd Point to TCD structure. It can be NULL if users
  328. * do not want to enable scatter/gather feature.
  329. * @note If nextTcd is not NULL, it means scatter gather feature is enabled
  330. * and DREQ bit is cleared in the previous transfer configuration, which
  331. * is set in the eDMA_ResetChannel.
  332. */
  333. void EDMA_SetTransferConfig(DMA_Type *base,
  334. uint32_t channel,
  335. const edma_transfer_config_t *config,
  336. edma_tcd_t *nextTcd);
  337. /*!
  338. * @brief Configures the eDMA minor offset feature.
  339. *
  340. * The minor offset means that the signed-extended value is added to the source address or destination
  341. * address after each minor loop.
  342. *
  343. * @param base eDMA peripheral base address.
  344. * @param channel eDMA channel number.
  345. * @param config A pointer to the minor offset configuration structure.
  346. */
  347. void EDMA_SetMinorOffsetConfig(DMA_Type *base, uint32_t channel, const edma_minor_offset_config_t *config);
  348. /*!
  349. * @brief Configures the eDMA channel preemption feature.
  350. *
  351. * This function configures the channel preemption attribute and the priority of the channel.
  352. *
  353. * @param base eDMA peripheral base address.
  354. * @param channel eDMA channel number
  355. * @param config A pointer to the channel preemption configuration structure.
  356. */
  357. static inline void EDMA_SetChannelPreemptionConfig(DMA_Type *base,
  358. uint32_t channel,
  359. const edma_channel_Preemption_config_t *config)
  360. {
  361. assert(channel < FSL_FEATURE_EDMA_MODULE_CHANNEL);
  362. assert(config != NULL);
  363. DMA_DCHPRIn(base, channel) =
  364. (DMA_DCHPRI0_DPA(!config->enablePreemptAbility) | DMA_DCHPRI0_ECP(config->enableChannelPreemption) |
  365. DMA_DCHPRI0_CHPRI(config->channelPriority));
  366. }
  367. /*!
  368. * @brief Sets the channel link for the eDMA transfer.
  369. *
  370. * This function configures either the minor link or the major link mode. The minor link means that the channel link is
  371. * triggered every time CITER decreases by 1. The major link means that the channel link is triggered when the CITER is
  372. * exhausted.
  373. *
  374. * @param base eDMA peripheral base address.
  375. * @param channel eDMA channel number.
  376. * @param type A channel link type, which can be one of the following:
  377. * @arg kEDMA_LinkNone
  378. * @arg kEDMA_MinorLink
  379. * @arg kEDMA_MajorLink
  380. * @param linkedChannel The linked channel number.
  381. * @note Users should ensure that DONE flag is cleared before calling this interface, or the configuration is invalid.
  382. */
  383. void EDMA_SetChannelLink(DMA_Type *base, uint32_t channel, edma_channel_link_type_t type, uint32_t linkedChannel);
  384. /*!
  385. * @brief Sets the bandwidth for the eDMA transfer.
  386. *
  387. * Because the eDMA processes the minor loop, it continuously generates read/write sequences
  388. * until the minor count is exhausted. The bandwidth forces the eDMA to stall after the completion of
  389. * each read/write access to control the bus request bandwidth seen by the crossbar switch.
  390. *
  391. * @param base eDMA peripheral base address.
  392. * @param channel eDMA channel number.
  393. * @param bandWidth A bandwidth setting, which can be one of the following:
  394. * @arg kEDMABandwidthStallNone
  395. * @arg kEDMABandwidthStall4Cycle
  396. * @arg kEDMABandwidthStall8Cycle
  397. */
  398. void EDMA_SetBandWidth(DMA_Type *base, uint32_t channel, edma_bandwidth_t bandWidth);
  399. /*!
  400. * @brief Sets the source modulo and the destination modulo for the eDMA transfer.
  401. *
  402. * This function defines a specific address range specified to be the value after (SADDR + SOFF)/(DADDR + DOFF)
  403. * calculation is performed or the original register value. It provides the ability to implement a circular data
  404. * queue easily.
  405. *
  406. * @param base eDMA peripheral base address.
  407. * @param channel eDMA channel number.
  408. * @param srcModulo A source modulo value.
  409. * @param destModulo A destination modulo value.
  410. */
  411. void EDMA_SetModulo(DMA_Type *base, uint32_t channel, edma_modulo_t srcModulo, edma_modulo_t destModulo);
  412. #if defined(FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT) && FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT
  413. /*!
  414. * @brief Enables an async request for the eDMA transfer.
  415. *
  416. * @param base eDMA peripheral base address.
  417. * @param channel eDMA channel number.
  418. * @param enable The command to enable (true) or disable (false).
  419. */
  420. static inline void EDMA_EnableAsyncRequest(DMA_Type *base, uint32_t channel, bool enable)
  421. {
  422. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  423. base->EARS = (base->EARS & (~(1U << channel))) | ((uint32_t)enable << channel);
  424. }
  425. #endif /* FSL_FEATURE_EDMA_ASYNCHRO_REQUEST_CHANNEL_COUNT */
  426. /*!
  427. * @brief Enables an auto stop request for the eDMA transfer.
  428. *
  429. * If enabling the auto stop request, the eDMA hardware automatically disables the hardware channel request.
  430. *
  431. * @param base eDMA peripheral base address.
  432. * @param channel eDMA channel number.
  433. * @param enable The command to enable (true) or disable (false).
  434. */
  435. static inline void EDMA_EnableAutoStopRequest(DMA_Type *base, uint32_t channel, bool enable)
  436. {
  437. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  438. base->TCD[channel].CSR = (base->TCD[channel].CSR & (~DMA_CSR_DREQ_MASK)) | DMA_CSR_DREQ(enable);
  439. }
  440. /*!
  441. * @brief Enables the interrupt source for the eDMA transfer.
  442. *
  443. * @param base eDMA peripheral base address.
  444. * @param channel eDMA channel number.
  445. * @param mask The mask of interrupt source to be set. Users need to use
  446. * the defined edma_interrupt_enable_t type.
  447. */
  448. void EDMA_EnableChannelInterrupts(DMA_Type *base, uint32_t channel, uint32_t mask);
  449. /*!
  450. * @brief Disables the interrupt source for the eDMA transfer.
  451. *
  452. * @param base eDMA peripheral base address.
  453. * @param channel eDMA channel number.
  454. * @param mask The mask of the interrupt source to be set. Use
  455. * the defined edma_interrupt_enable_t type.
  456. */
  457. void EDMA_DisableChannelInterrupts(DMA_Type *base, uint32_t channel, uint32_t mask);
  458. /* @} */
  459. /*!
  460. * @name eDMA TCD Operation
  461. * @{
  462. */
  463. /*!
  464. * @brief Sets all fields to default values for the TCD structure.
  465. *
  466. * This function sets all fields for this TCD structure to default value.
  467. *
  468. * @param tcd Pointer to the TCD structure.
  469. * @note This function enables the auto stop request feature.
  470. */
  471. void EDMA_TcdReset(edma_tcd_t *tcd);
  472. /*!
  473. * @brief Configures the eDMA TCD transfer attribute.
  474. *
  475. * The TCD is a transfer control descriptor. The content of the TCD is the same as the hardware TCD registers.
  476. * The STCD is used in the scatter-gather mode.
  477. * This function configures the TCD transfer attribute, including source address, destination address,
  478. * transfer size, address offset, and so on. It also configures the scatter gather feature if the
  479. * user supplies the next TCD address.
  480. * Example:
  481. * @code
  482. * edma_transfer_t config = {
  483. * ...
  484. * }
  485. * edma_tcd_t tcd __aligned(32);
  486. * edma_tcd_t nextTcd __aligned(32);
  487. * EDMA_TcdSetTransferConfig(&tcd, &config, &nextTcd);
  488. * @endcode
  489. *
  490. * @param tcd Pointer to the TCD structure.
  491. * @param config Pointer to eDMA transfer configuration structure.
  492. * @param nextTcd Pointer to the next TCD structure. It can be NULL if users
  493. * do not want to enable scatter/gather feature.
  494. * @note TCD address should be 32 bytes aligned or it causes an eDMA error.
  495. * @note If the nextTcd is not NULL, the scatter gather feature is enabled
  496. * and DREQ bit is cleared in the previous transfer configuration, which
  497. * is set in the EDMA_TcdReset.
  498. */
  499. void EDMA_TcdSetTransferConfig(edma_tcd_t *tcd, const edma_transfer_config_t *config, edma_tcd_t *nextTcd);
  500. /*!
  501. * @brief Configures the eDMA TCD minor offset feature.
  502. *
  503. * A minor offset is a signed-extended value added to the source address or a destination
  504. * address after each minor loop.
  505. *
  506. * @param tcd A point to the TCD structure.
  507. * @param config A pointer to the minor offset configuration structure.
  508. */
  509. void EDMA_TcdSetMinorOffsetConfig(edma_tcd_t *tcd, const edma_minor_offset_config_t *config);
  510. /*!
  511. * @brief Sets the channel link for the eDMA TCD.
  512. *
  513. * This function configures either a minor link or a major link. The minor link means the channel link is
  514. * triggered every time CITER decreases by 1. The major link means that the channel link is triggered when the CITER is
  515. * exhausted.
  516. *
  517. * @note Users should ensure that DONE flag is cleared before calling this interface, or the configuration is invalid.
  518. * @param tcd Point to the TCD structure.
  519. * @param type Channel link type, it can be one of:
  520. * @arg kEDMA_LinkNone
  521. * @arg kEDMA_MinorLink
  522. * @arg kEDMA_MajorLink
  523. * @param linkedChannel The linked channel number.
  524. */
  525. void EDMA_TcdSetChannelLink(edma_tcd_t *tcd, edma_channel_link_type_t type, uint32_t linkedChannel);
  526. /*!
  527. * @brief Sets the bandwidth for the eDMA TCD.
  528. *
  529. * Because the eDMA processes the minor loop, it continuously generates read/write sequences
  530. * until the minor count is exhausted. The bandwidth forces the eDMA to stall after the completion of
  531. * each read/write access to control the bus request bandwidth seen by the crossbar switch.
  532. * @param tcd A pointer to the TCD structure.
  533. * @param bandWidth A bandwidth setting, which can be one of the following:
  534. * @arg kEDMABandwidthStallNone
  535. * @arg kEDMABandwidthStall4Cycle
  536. * @arg kEDMABandwidthStall8Cycle
  537. */
  538. static inline void EDMA_TcdSetBandWidth(edma_tcd_t *tcd, edma_bandwidth_t bandWidth)
  539. {
  540. assert(tcd != NULL);
  541. assert(((uint32_t)tcd & 0x1FU) == 0);
  542. tcd->CSR = (tcd->CSR & (~DMA_CSR_BWC_MASK)) | DMA_CSR_BWC(bandWidth);
  543. }
  544. /*!
  545. * @brief Sets the source modulo and the destination modulo for the eDMA TCD.
  546. *
  547. * This function defines a specific address range specified to be the value after (SADDR + SOFF)/(DADDR + DOFF)
  548. * calculation is performed or the original register value. It provides the ability to implement a circular data
  549. * queue easily.
  550. *
  551. * @param tcd A pointer to the TCD structure.
  552. * @param srcModulo A source modulo value.
  553. * @param destModulo A destination modulo value.
  554. */
  555. void EDMA_TcdSetModulo(edma_tcd_t *tcd, edma_modulo_t srcModulo, edma_modulo_t destModulo);
  556. /*!
  557. * @brief Sets the auto stop request for the eDMA TCD.
  558. *
  559. * If enabling the auto stop request, the eDMA hardware automatically disables the hardware channel request.
  560. *
  561. * @param tcd A pointer to the TCD structure.
  562. * @param enable The command to enable (true) or disable (false).
  563. */
  564. static inline void EDMA_TcdEnableAutoStopRequest(edma_tcd_t *tcd, bool enable)
  565. {
  566. assert(tcd != NULL);
  567. assert(((uint32_t)tcd & 0x1FU) == 0);
  568. tcd->CSR = (tcd->CSR & (~DMA_CSR_DREQ_MASK)) | DMA_CSR_DREQ(enable);
  569. }
  570. /*!
  571. * @brief Enables the interrupt source for the eDMA TCD.
  572. *
  573. * @param tcd Point to the TCD structure.
  574. * @param mask The mask of interrupt source to be set. Users need to use
  575. * the defined edma_interrupt_enable_t type.
  576. */
  577. void EDMA_TcdEnableInterrupts(edma_tcd_t *tcd, uint32_t mask);
  578. /*!
  579. * @brief Disables the interrupt source for the eDMA TCD.
  580. *
  581. * @param tcd Point to the TCD structure.
  582. * @param mask The mask of interrupt source to be set. Users need to use
  583. * the defined edma_interrupt_enable_t type.
  584. */
  585. void EDMA_TcdDisableInterrupts(edma_tcd_t *tcd, uint32_t mask);
  586. /*! @} */
  587. /*!
  588. * @name eDMA Channel Transfer Operation
  589. * @{
  590. */
  591. /*!
  592. * @brief Enables the eDMA hardware channel request.
  593. *
  594. * This function enables the hardware channel request.
  595. *
  596. * @param base eDMA peripheral base address.
  597. * @param channel eDMA channel number.
  598. */
  599. static inline void EDMA_EnableChannelRequest(DMA_Type *base, uint32_t channel)
  600. {
  601. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  602. base->SERQ = DMA_SERQ_SERQ(channel);
  603. }
  604. /*!
  605. * @brief Disables the eDMA hardware channel request.
  606. *
  607. * This function disables the hardware channel request.
  608. *
  609. * @param base eDMA peripheral base address.
  610. * @param channel eDMA channel number.
  611. */
  612. static inline void EDMA_DisableChannelRequest(DMA_Type *base, uint32_t channel)
  613. {
  614. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  615. base->CERQ = DMA_CERQ_CERQ(channel);
  616. }
  617. /*!
  618. * @brief Starts the eDMA transfer by using the software trigger.
  619. *
  620. * This function starts a minor loop transfer.
  621. *
  622. * @param base eDMA peripheral base address.
  623. * @param channel eDMA channel number.
  624. */
  625. static inline void EDMA_TriggerChannelStart(DMA_Type *base, uint32_t channel)
  626. {
  627. assert(channel < FSL_FEATURE_DMAMUX_MODULE_CHANNEL);
  628. base->SSRT = DMA_SSRT_SSRT(channel);
  629. }
  630. /*! @} */
  631. /*!
  632. * @name eDMA Channel Status Operation
  633. * @{
  634. */
  635. /*!
  636. * @brief Gets the remaining major loop count from the eDMA current channel TCD.
  637. *
  638. * This function checks the TCD (Task Control Descriptor) status for a specified
  639. * eDMA channel and returns the the number of major loop count that has not finished.
  640. *
  641. * @param base eDMA peripheral base address.
  642. * @param channel eDMA channel number.
  643. * @return Major loop count which has not been transferred yet for the current TCD.
  644. * @note 1. This function can only be used to get unfinished major loop count of transfer without
  645. * the next TCD, or it might be inaccuracy.
  646. * 2. The unfinished/remaining transfer bytes cannot be obtained directly from registers while
  647. * the channel is running.
  648. * Because to calculate the remaining bytes, the initial NBYTES configured in DMA_TCDn_NBYTES_MLNO
  649. * register is needed while the eDMA IP does not support getting it while a channel is active.
  650. * In another word, the NBYTES value reading is always the actual (decrementing) NBYTES value the dma_engine
  651. * is working with while a channel is running.
  652. * Consequently, to get the remaining transfer bytes, a software-saved initial value of NBYTES (for example
  653. * copied before enabling the channel) is needed. The formula to calculate it is shown below:
  654. * RemainingBytes = RemainingMajorLoopCount * NBYTES(initially configured)
  655. */
  656. uint32_t EDMA_GetRemainingMajorLoopCount(DMA_Type *base, uint32_t channel);
  657. /*!
  658. * @brief Gets the eDMA channel error status flags.
  659. *
  660. * @param base eDMA peripheral base address.
  661. * @return The mask of error status flags. Users need to use the
  662. * _edma_error_status_flags type to decode the return variables.
  663. */
  664. static inline uint32_t EDMA_GetErrorStatusFlags(DMA_Type *base)
  665. {
  666. return base->ES;
  667. }
  668. /*!
  669. * @brief Gets the eDMA channel status flags.
  670. *
  671. * @param base eDMA peripheral base address.
  672. * @param channel eDMA channel number.
  673. * @return The mask of channel status flags. Users need to use the
  674. * _edma_channel_status_flags type to decode the return variables.
  675. */
  676. uint32_t EDMA_GetChannelStatusFlags(DMA_Type *base, uint32_t channel);
  677. /*!
  678. * @brief Clears the eDMA channel status flags.
  679. *
  680. * @param base eDMA peripheral base address.
  681. * @param channel eDMA channel number.
  682. * @param mask The mask of channel status to be cleared. Users need to use
  683. * the defined _edma_channel_status_flags type.
  684. */
  685. void EDMA_ClearChannelStatusFlags(DMA_Type *base, uint32_t channel, uint32_t mask);
  686. /*! @} */
  687. /*!
  688. * @name eDMA Transactional Operation
  689. */
  690. /*!
  691. * @brief Creates the eDMA handle.
  692. *
  693. * This function is called if using the transactional API for eDMA. This function
  694. * initializes the internal state of the eDMA handle.
  695. *
  696. * @param handle eDMA handle pointer. The eDMA handle stores callback function and
  697. * parameters.
  698. * @param base eDMA peripheral base address.
  699. * @param channel eDMA channel number.
  700. */
  701. void EDMA_CreateHandle(edma_handle_t *handle, DMA_Type *base, uint32_t channel);
  702. /*!
  703. * @brief Installs the TCDs memory pool into the eDMA handle.
  704. *
  705. * This function is called after the EDMA_CreateHandle to use scatter/gather feature.
  706. *
  707. * @param handle eDMA handle pointer.
  708. * @param tcdPool A memory pool to store TCDs. It must be 32 bytes aligned.
  709. * @param tcdSize The number of TCD slots.
  710. */
  711. void EDMA_InstallTCDMemory(edma_handle_t *handle, edma_tcd_t *tcdPool, uint32_t tcdSize);
  712. /*!
  713. * @brief Installs a callback function for the eDMA transfer.
  714. *
  715. * This callback is called in the eDMA IRQ handler. Use the callback to do something after
  716. * the current major loop transfer completes.
  717. *
  718. * @param handle eDMA handle pointer.
  719. * @param callback eDMA callback function pointer.
  720. * @param userData A parameter for the callback function.
  721. */
  722. void EDMA_SetCallback(edma_handle_t *handle, edma_callback callback, void *userData);
  723. /*!
  724. * @brief Prepares the eDMA transfer structure.
  725. *
  726. * This function prepares the transfer configuration structure according to the user input.
  727. *
  728. * @param config The user configuration structure of type edma_transfer_t.
  729. * @param srcAddr eDMA transfer source address.
  730. * @param srcWidth eDMA transfer source address width(bytes).
  731. * @param destAddr eDMA transfer destination address.
  732. * @param destWidth eDMA transfer destination address width(bytes).
  733. * @param bytesEachRequest eDMA transfer bytes per channel request.
  734. * @param transferBytes eDMA transfer bytes to be transferred.
  735. * @param type eDMA transfer type.
  736. * @note The data address and the data width must be consistent. For example, if the SRC
  737. * is 4 bytes, the source address must be 4 bytes aligned, or it results in
  738. * source address error (SAE).
  739. */
  740. void EDMA_PrepareTransfer(edma_transfer_config_t *config,
  741. void *srcAddr,
  742. uint32_t srcWidth,
  743. void *destAddr,
  744. uint32_t destWidth,
  745. uint32_t bytesEachRequest,
  746. uint32_t transferBytes,
  747. edma_transfer_type_t type);
  748. /*!
  749. * @brief Submits the eDMA transfer request.
  750. *
  751. * This function submits the eDMA transfer request according to the transfer configuration structure.
  752. * If submitting the transfer request repeatedly, this function packs an unprocessed request as
  753. * a TCD and enables scatter/gather feature to process it in the next time.
  754. *
  755. * @param handle eDMA handle pointer.
  756. * @param config Pointer to eDMA transfer configuration structure.
  757. * @retval kStatus_EDMA_Success It means submit transfer request succeed.
  758. * @retval kStatus_EDMA_QueueFull It means TCD queue is full. Submit transfer request is not allowed.
  759. * @retval kStatus_EDMA_Busy It means the given channel is busy, need to submit request later.
  760. */
  761. status_t EDMA_SubmitTransfer(edma_handle_t *handle, const edma_transfer_config_t *config);
  762. /*!
  763. * @brief eDMA starts transfer.
  764. *
  765. * This function enables the channel request. Users can call this function after submitting the transfer request
  766. * or before submitting the transfer request.
  767. *
  768. * @param handle eDMA handle pointer.
  769. */
  770. void EDMA_StartTransfer(edma_handle_t *handle);
  771. /*!
  772. * @brief eDMA stops transfer.
  773. *
  774. * This function disables the channel request to pause the transfer. Users can call EDMA_StartTransfer()
  775. * again to resume the transfer.
  776. *
  777. * @param handle eDMA handle pointer.
  778. */
  779. void EDMA_StopTransfer(edma_handle_t *handle);
  780. /*!
  781. * @brief eDMA aborts transfer.
  782. *
  783. * This function disables the channel request and clear transfer status bits.
  784. * Users can submit another transfer after calling this API.
  785. *
  786. * @param handle DMA handle pointer.
  787. */
  788. void EDMA_AbortTransfer(edma_handle_t *handle);
  789. /*!
  790. * @brief Get unused TCD slot number.
  791. *
  792. * This function gets current tcd index which is run. If the TCD pool pointer is NULL, it will return 0.
  793. *
  794. * @param handle DMA handle pointer.
  795. * @return The unused tcd slot number.
  796. */
  797. static inline uint32_t EDMA_GetUnusedTCDNumber(edma_handle_t *handle)
  798. {
  799. return (handle->tcdSize - handle->tcdUsed);
  800. }
  801. /*!
  802. * @brief Get the next tcd address.
  803. *
  804. * This function gets the next tcd address. If this is last TCD, return 0.
  805. *
  806. * @param handle DMA handle pointer.
  807. * @return The next TCD address.
  808. */
  809. static inline uint32_t EDMA_GetNextTCDAddress(edma_handle_t *handle)
  810. {
  811. return (handle->base->TCD[handle->channel].DLAST_SGA);
  812. }
  813. /*!
  814. * @brief eDMA IRQ handler for the current major loop transfer completion.
  815. *
  816. * This function clears the channel major interrupt flag and calls
  817. * the callback function if it is not NULL.
  818. *
  819. * Note:
  820. * For the case using TCD queue, when the major iteration count is exhausted, additional operations are performed.
  821. * These include the final address adjustments and reloading of the BITER field into the CITER.
  822. * Assertion of an optional interrupt request also occurs at this time, as does a possible fetch of a new TCD from
  823. * memory using the scatter/gather address pointer included in the descriptor (if scatter/gather is enabled).
  824. *
  825. * For instance, when the time interrupt of TCD[0] happens, the TCD[1] has already been loaded into the eDMA engine.
  826. * As sga and sga_index are calculated based on the DLAST_SGA bitfield lies in the TCD_CSR register, the sga_index
  827. * in this case should be 2 (DLAST_SGA of TCD[1] stores the address of TCD[2]). Thus, the "tcdUsed" updated should be
  828. * (tcdUsed - 2U) which indicates the number of TCDs can be loaded in the memory pool (because TCD[0] and TCD[1] have
  829. * been loaded into the eDMA engine at this point already.).
  830. *
  831. * For the last two continuous ISRs in a scatter/gather process, they both load the last TCD (The last ISR does not
  832. * load a new TCD) from the memory pool to the eDMA engine when major loop completes.
  833. * Therefore, ensure that the header and tcdUsed updated are identical for them.
  834. * tcdUsed are both 0 in this case as no TCD to be loaded.
  835. *
  836. * See the "eDMA basic data flow" in the eDMA Functional description section of the Reference Manual for
  837. * further details.
  838. *
  839. * @param handle eDMA handle pointer.
  840. */
  841. void EDMA_HandleIRQ(edma_handle_t *handle);
  842. /* @} */
  843. #if defined(__cplusplus)
  844. }
  845. #endif /* __cplusplus */
  846. /* @} */
  847. #endif /*_FSL_EDMA_H_*/