fsl_edma.h 38 KB

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