25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

952 lines
40 KiB

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