fsl_dspi_edma.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. #include "fsl_dspi_edma.h"
  31. /***********************************************************************************************************************
  32. * Definitons
  33. ***********************************************************************************************************************/
  34. /*!
  35. * @brief Structure definition for dspi_master_edma_private_handle_t. The structure is private.
  36. */
  37. typedef struct _dspi_master_edma_private_handle
  38. {
  39. SPI_Type *base; /*!< DSPI peripheral base address. */
  40. dspi_master_edma_handle_t *handle; /*!< dspi_master_edma_handle_t handle */
  41. } dspi_master_edma_private_handle_t;
  42. /*!
  43. * @brief Structure definition for dspi_slave_edma_private_handle_t. The structure is private.
  44. */
  45. typedef struct _dspi_slave_edma_private_handle
  46. {
  47. SPI_Type *base; /*!< DSPI peripheral base address. */
  48. dspi_slave_edma_handle_t *handle; /*!< dspi_master_edma_handle_t handle */
  49. } dspi_slave_edma_private_handle_t;
  50. /***********************************************************************************************************************
  51. * Prototypes
  52. ***********************************************************************************************************************/
  53. /*!
  54. * @brief EDMA_DspiMasterCallback after the DSPI master transfer completed by using EDMA.
  55. * This is not a public API.
  56. */
  57. static void EDMA_DspiMasterCallback(edma_handle_t *edmaHandle,
  58. void *g_dspiEdmaPrivateHandle,
  59. bool transferDone,
  60. uint32_t tcds);
  61. /*!
  62. * @brief EDMA_DspiSlaveCallback after the DSPI slave transfer completed by using EDMA.
  63. * This is not a public API.
  64. */
  65. static void EDMA_DspiSlaveCallback(edma_handle_t *edmaHandle,
  66. void *g_dspiEdmaPrivateHandle,
  67. bool transferDone,
  68. uint32_t tcds);
  69. /*!
  70. * @brief Get instance number for DSPI module.
  71. *
  72. * This is not a public API and it's extern from fsl_dspi.c.
  73. *
  74. * @param base DSPI peripheral base address
  75. */
  76. extern uint32_t DSPI_GetInstance(SPI_Type *base);
  77. /***********************************************************************************************************************
  78. * Variables
  79. ***********************************************************************************************************************/
  80. /*! @brief Pointers to dspi edma handles for each instance. */
  81. static dspi_master_edma_private_handle_t s_dspiMasterEdmaPrivateHandle[FSL_FEATURE_SOC_DSPI_COUNT];
  82. static dspi_slave_edma_private_handle_t s_dspiSlaveEdmaPrivateHandle[FSL_FEATURE_SOC_DSPI_COUNT];
  83. /***********************************************************************************************************************
  84. * Code
  85. ***********************************************************************************************************************/
  86. void DSPI_MasterTransferCreateHandleEDMA(SPI_Type *base,
  87. dspi_master_edma_handle_t *handle,
  88. dspi_master_edma_transfer_callback_t callback,
  89. void *userData,
  90. edma_handle_t *edmaRxRegToRxDataHandle,
  91. edma_handle_t *edmaTxDataToIntermediaryHandle,
  92. edma_handle_t *edmaIntermediaryToTxRegHandle)
  93. {
  94. assert(handle);
  95. assert(edmaRxRegToRxDataHandle);
  96. assert(edmaTxDataToIntermediaryHandle);
  97. assert(edmaIntermediaryToTxRegHandle);
  98. /* Zero the handle. */
  99. memset(handle, 0, sizeof(*handle));
  100. uint32_t instance = DSPI_GetInstance(base);
  101. s_dspiMasterEdmaPrivateHandle[instance].base = base;
  102. s_dspiMasterEdmaPrivateHandle[instance].handle = handle;
  103. handle->callback = callback;
  104. handle->userData = userData;
  105. handle->edmaRxRegToRxDataHandle = edmaRxRegToRxDataHandle;
  106. handle->edmaTxDataToIntermediaryHandle = edmaTxDataToIntermediaryHandle;
  107. handle->edmaIntermediaryToTxRegHandle = edmaIntermediaryToTxRegHandle;
  108. }
  109. status_t DSPI_MasterTransferEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle, dspi_transfer_t *transfer)
  110. {
  111. assert(handle);
  112. assert(transfer);
  113. /* If the transfer count is zero, then return immediately.*/
  114. if (transfer->dataSize == 0)
  115. {
  116. return kStatus_InvalidArgument;
  117. }
  118. /* If both send buffer and receive buffer is null */
  119. if ((!(transfer->txData)) && (!(transfer->rxData)))
  120. {
  121. return kStatus_InvalidArgument;
  122. }
  123. /* Check that we're not busy.*/
  124. if (handle->state == kDSPI_Busy)
  125. {
  126. return kStatus_DSPI_Busy;
  127. }
  128. handle->state = kDSPI_Busy;
  129. uint32_t instance = DSPI_GetInstance(base);
  130. uint16_t wordToSend = 0;
  131. uint8_t dummyData = DSPI_DUMMY_DATA;
  132. uint8_t dataAlreadyFed = 0;
  133. uint8_t dataFedMax = 2;
  134. uint32_t rxAddr = DSPI_GetRxRegisterAddress(base);
  135. uint32_t txAddr = DSPI_MasterGetTxRegisterAddress(base);
  136. edma_tcd_t *softwareTCD = (edma_tcd_t *)((uint32_t)(&handle->dspiSoftwareTCD[1]) & (~0x1FU));
  137. edma_transfer_config_t transferConfigA;
  138. edma_transfer_config_t transferConfigB;
  139. edma_transfer_config_t transferConfigC;
  140. handle->txBuffIfNull = ((uint32_t)DSPI_DUMMY_DATA << 8) | DSPI_DUMMY_DATA;
  141. dspi_command_data_config_t commandStruct;
  142. DSPI_StopTransfer(base);
  143. DSPI_FlushFifo(base, true, true);
  144. DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
  145. commandStruct.whichPcs =
  146. (dspi_which_pcs_t)(1U << ((transfer->configFlags & DSPI_MASTER_PCS_MASK) >> DSPI_MASTER_PCS_SHIFT));
  147. commandStruct.isEndOfQueue = false;
  148. commandStruct.clearTransferCount = false;
  149. commandStruct.whichCtar =
  150. (dspi_ctar_selection_t)((transfer->configFlags & DSPI_MASTER_CTAR_MASK) >> DSPI_MASTER_CTAR_SHIFT);
  151. commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterPcsContinuous);
  152. handle->command = DSPI_MasterGetFormattedCommand(&(commandStruct));
  153. commandStruct.isEndOfQueue = true;
  154. commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterActiveAfterTransfer);
  155. handle->lastCommand = DSPI_MasterGetFormattedCommand(&(commandStruct));
  156. handle->bitsPerFrame = ((base->CTAR[commandStruct.whichCtar] & SPI_CTAR_FMSZ_MASK) >> SPI_CTAR_FMSZ_SHIFT) + 1;
  157. if ((base->MCR & SPI_MCR_DIS_RXF_MASK) || (base->MCR & SPI_MCR_DIS_TXF_MASK))
  158. {
  159. handle->fifoSize = 1;
  160. }
  161. else
  162. {
  163. handle->fifoSize = FSL_FEATURE_DSPI_FIFO_SIZEn(base);
  164. }
  165. handle->txData = transfer->txData;
  166. handle->rxData = transfer->rxData;
  167. handle->remainingSendByteCount = transfer->dataSize;
  168. handle->remainingReceiveByteCount = transfer->dataSize;
  169. handle->totalByteCount = transfer->dataSize;
  170. /* If using a shared RX/TX DMA request, then this limits the amount of data we can transfer
  171. * due to the linked channel. The max bytes is 511 if 8-bit/frame or 1022 if 16-bit/frame
  172. */
  173. uint32_t limited_size = 0;
  174. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  175. {
  176. limited_size = 32767u;
  177. }
  178. else
  179. {
  180. limited_size = 511u;
  181. }
  182. if (handle->bitsPerFrame > 8)
  183. {
  184. if (transfer->dataSize > (limited_size << 1u))
  185. {
  186. handle->state = kDSPI_Idle;
  187. return kStatus_DSPI_OutOfRange;
  188. }
  189. }
  190. else
  191. {
  192. if (transfer->dataSize > limited_size)
  193. {
  194. handle->state = kDSPI_Idle;
  195. return kStatus_DSPI_OutOfRange;
  196. }
  197. }
  198. /*The data size should be even if the bitsPerFrame is greater than 8 (that is 2 bytes per frame in dspi) */
  199. if ((handle->bitsPerFrame > 8) && (transfer->dataSize & 0x1))
  200. {
  201. handle->state = kDSPI_Idle;
  202. return kStatus_InvalidArgument;
  203. }
  204. DSPI_DisableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  205. EDMA_SetCallback(handle->edmaRxRegToRxDataHandle, EDMA_DspiMasterCallback,
  206. &s_dspiMasterEdmaPrivateHandle[instance]);
  207. /*
  208. (1)For DSPI instances with shared RX/TX DMA requests: Rx DMA request -> channel_A -> channel_B-> channel_C.
  209. channel_A minor link to channel_B , channel_B minor link to channel_C.
  210. Already pushed 1 or 2 data in SPI_PUSHR , then start the DMA tansfer.
  211. channel_A:SPI_POPR to rxData,
  212. channel_B:next txData to handle->command (low 16 bits),
  213. channel_C:handle->command (32 bits) to SPI_PUSHR, and use the scatter/gather to transfer the last data
  214. (handle->lastCommand to SPI_PUSHR).
  215. (2)For DSPI instances with separate RX and TX DMA requests:
  216. Rx DMA request -> channel_A
  217. Tx DMA request -> channel_C -> channel_B .
  218. channel_C major link to channel_B.
  219. So need prepare the first data in "intermediary" before the DMA
  220. transfer and then channel_B is used to prepare the next data to "intermediary"
  221. channel_A:SPI_POPR to rxData,
  222. channel_C: handle->command (32 bits) to SPI_PUSHR,
  223. channel_B: next txData to handle->command (low 16 bits), and use the scatter/gather to prepare the last data
  224. (handle->lastCommand to handle->Command).
  225. */
  226. /*If dspi has separate dma request , prepare the first data in "intermediary" .
  227. else (dspi has shared dma request) , send first 2 data if there is fifo or send first 1 data if there is no fifo*/
  228. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  229. {
  230. /* For DSPI instances with separate RX/TX DMA requests, we'll use the TX DMA request to
  231. * trigger the TX DMA channel and RX DMA request to trigger the RX DMA channel
  232. */
  233. /*Prepare the firt data*/
  234. if (handle->bitsPerFrame > 8)
  235. {
  236. /* If it's the last word */
  237. if (handle->remainingSendByteCount <= 2)
  238. {
  239. if (handle->txData)
  240. {
  241. wordToSend = *(handle->txData);
  242. ++handle->txData; /* increment to next data byte */
  243. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  244. }
  245. else
  246. {
  247. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  248. }
  249. handle->lastCommand = (handle->lastCommand & 0xffff0000U) | wordToSend;
  250. handle->command = handle->lastCommand;
  251. }
  252. else /* For all words except the last word , frame > 8bits */
  253. {
  254. if (handle->txData)
  255. {
  256. wordToSend = *(handle->txData);
  257. ++handle->txData; /* increment to next data byte */
  258. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  259. ++handle->txData; /* increment to next data byte */
  260. }
  261. else
  262. {
  263. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  264. }
  265. handle->command = (handle->command & 0xffff0000U) | wordToSend;
  266. }
  267. }
  268. else /* Optimized for bits/frame less than or equal to one byte. */
  269. {
  270. if (handle->txData)
  271. {
  272. wordToSend = *(handle->txData);
  273. ++handle->txData; /* increment to next data word*/
  274. }
  275. else
  276. {
  277. wordToSend = dummyData;
  278. }
  279. if (handle->remainingSendByteCount == 1)
  280. {
  281. handle->lastCommand = (handle->lastCommand & 0xffff0000U) | wordToSend;
  282. handle->command = handle->lastCommand;
  283. }
  284. else
  285. {
  286. handle->command = (handle->command & 0xffff0000U) | wordToSend;
  287. }
  288. }
  289. }
  290. else /*dspi has shared dma request*/
  291. {
  292. /* For DSPI instances with shared RX/TX DMA requests, we'll use the RX DMA request to
  293. * trigger ongoing transfers and will link to the TX DMA channel from the RX DMA channel.
  294. */
  295. /* If bits/frame is greater than one byte */
  296. if (handle->bitsPerFrame > 8)
  297. {
  298. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  299. {
  300. if (handle->remainingSendByteCount <= 2)
  301. {
  302. if (handle->txData)
  303. {
  304. wordToSend = *(handle->txData);
  305. ++handle->txData;
  306. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  307. }
  308. else
  309. {
  310. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  311. }
  312. handle->remainingSendByteCount = 0;
  313. base->PUSHR = (handle->lastCommand & 0xffff0000U) | wordToSend;
  314. }
  315. /* For all words except the last word */
  316. else
  317. {
  318. if (handle->txData)
  319. {
  320. wordToSend = *(handle->txData);
  321. ++handle->txData;
  322. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  323. ++handle->txData;
  324. }
  325. else
  326. {
  327. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  328. }
  329. handle->remainingSendByteCount -= 2;
  330. base->PUSHR = (handle->command & 0xffff0000U) | wordToSend;
  331. }
  332. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  333. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  334. dataAlreadyFed += 2;
  335. /* exit loop if send count is zero, else update local variables for next loop */
  336. if ((handle->remainingSendByteCount == 0) || (dataAlreadyFed == (dataFedMax * 2)))
  337. {
  338. break;
  339. }
  340. } /* End of TX FIFO fill while loop */
  341. }
  342. else /* Optimized for bits/frame less than or equal to one byte. */
  343. {
  344. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  345. {
  346. if (handle->txData)
  347. {
  348. wordToSend = *(handle->txData);
  349. ++handle->txData;
  350. }
  351. else
  352. {
  353. wordToSend = dummyData;
  354. }
  355. if (handle->remainingSendByteCount == 1)
  356. {
  357. base->PUSHR = (handle->lastCommand & 0xffff0000U) | wordToSend;
  358. }
  359. else
  360. {
  361. base->PUSHR = (handle->command & 0xffff0000U) | wordToSend;
  362. }
  363. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  364. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  365. --handle->remainingSendByteCount;
  366. dataAlreadyFed++;
  367. /* exit loop if send count is zero, else update local variables for next loop */
  368. if ((handle->remainingSendByteCount == 0) || (dataAlreadyFed == dataFedMax))
  369. {
  370. break;
  371. }
  372. } /* End of TX FIFO fill while loop */
  373. }
  374. }
  375. /***channel_A *** used for carry the data from Rx_Data_Register(POPR) to User_Receive_Buffer(rxData)*/
  376. EDMA_ResetChannel(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel);
  377. transferConfigA.srcAddr = (uint32_t)rxAddr;
  378. transferConfigA.srcOffset = 0;
  379. if (handle->rxData)
  380. {
  381. transferConfigA.destAddr = (uint32_t) & (handle->rxData[0]);
  382. transferConfigA.destOffset = 1;
  383. }
  384. else
  385. {
  386. transferConfigA.destAddr = (uint32_t) & (handle->rxBuffIfNull);
  387. transferConfigA.destOffset = 0;
  388. }
  389. transferConfigA.destTransferSize = kEDMA_TransferSize1Bytes;
  390. if (handle->bitsPerFrame <= 8)
  391. {
  392. transferConfigA.srcTransferSize = kEDMA_TransferSize1Bytes;
  393. transferConfigA.minorLoopBytes = 1;
  394. transferConfigA.majorLoopCounts = handle->remainingReceiveByteCount;
  395. }
  396. else
  397. {
  398. transferConfigA.srcTransferSize = kEDMA_TransferSize2Bytes;
  399. transferConfigA.minorLoopBytes = 2;
  400. transferConfigA.majorLoopCounts = handle->remainingReceiveByteCount / 2;
  401. }
  402. /* Store the initially configured eDMA minor byte transfer count into the DSPI handle */
  403. handle->nbytes = transferConfigA.minorLoopBytes;
  404. EDMA_SetTransferConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  405. &transferConfigA, NULL);
  406. EDMA_EnableChannelInterrupts(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  407. kEDMA_MajorInterruptEnable);
  408. /***channel_B *** used for carry the data from User_Send_Buffer to "intermediary" because the SPIx_PUSHR should
  409. write the 32bits at once time . Then use channel_C to carry the "intermediary" to SPIx_PUSHR. Note that the
  410. SPIx_PUSHR upper 16 bits are the "command" and the low 16bits are data */
  411. EDMA_ResetChannel(handle->edmaTxDataToIntermediaryHandle->base, handle->edmaTxDataToIntermediaryHandle->channel);
  412. /*Calculate the last data : handle->lastCommand*/
  413. if (((handle->remainingSendByteCount > 0) && (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))) ||
  414. ((((handle->remainingSendByteCount > 1) && (handle->bitsPerFrame <= 8)) ||
  415. ((handle->remainingSendByteCount > 2) && (handle->bitsPerFrame > 8))) &&
  416. (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))))
  417. {
  418. if (handle->txData)
  419. {
  420. uint32_t bufferIndex = 0;
  421. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  422. {
  423. if (handle->bitsPerFrame <= 8)
  424. {
  425. bufferIndex = handle->remainingSendByteCount - 1;
  426. }
  427. else
  428. {
  429. bufferIndex = handle->remainingSendByteCount - 2;
  430. }
  431. }
  432. else
  433. {
  434. bufferIndex = handle->remainingSendByteCount;
  435. }
  436. if (handle->bitsPerFrame <= 8)
  437. {
  438. handle->lastCommand = (handle->lastCommand & 0xffff0000U) | handle->txData[bufferIndex - 1];
  439. }
  440. else
  441. {
  442. handle->lastCommand = (handle->lastCommand & 0xffff0000U) |
  443. ((uint32_t)handle->txData[bufferIndex - 1] << 8) |
  444. handle->txData[bufferIndex - 2];
  445. }
  446. }
  447. else
  448. {
  449. if (handle->bitsPerFrame <= 8)
  450. {
  451. wordToSend = dummyData;
  452. }
  453. else
  454. {
  455. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  456. }
  457. handle->lastCommand = (handle->lastCommand & 0xffff0000U) | wordToSend;
  458. }
  459. }
  460. /*For DSPI instances with separate RX and TX DMA requests: use the scatter/gather to prepare the last data
  461. * (handle->lastCommand) to handle->Command*/
  462. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  463. {
  464. transferConfigB.srcAddr = (uint32_t) & (handle->lastCommand);
  465. transferConfigB.destAddr = (uint32_t) & (handle->command);
  466. transferConfigB.srcTransferSize = kEDMA_TransferSize4Bytes;
  467. transferConfigB.destTransferSize = kEDMA_TransferSize4Bytes;
  468. transferConfigB.srcOffset = 0;
  469. transferConfigB.destOffset = 0;
  470. transferConfigB.minorLoopBytes = 4;
  471. transferConfigB.majorLoopCounts = 1;
  472. EDMA_TcdReset(softwareTCD);
  473. EDMA_TcdSetTransferConfig(softwareTCD, &transferConfigB, NULL);
  474. }
  475. /*User_Send_Buffer(txData) to intermediary(handle->command)*/
  476. if (((((handle->remainingSendByteCount > 2) && (handle->bitsPerFrame <= 8)) ||
  477. ((handle->remainingSendByteCount > 4) && (handle->bitsPerFrame > 8))) &&
  478. (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))) ||
  479. (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base)))
  480. {
  481. if (handle->txData)
  482. {
  483. transferConfigB.srcAddr = (uint32_t)(handle->txData);
  484. transferConfigB.srcOffset = 1;
  485. }
  486. else
  487. {
  488. transferConfigB.srcAddr = (uint32_t)(&handle->txBuffIfNull);
  489. transferConfigB.srcOffset = 0;
  490. }
  491. transferConfigB.destAddr = (uint32_t)(&handle->command);
  492. transferConfigB.destOffset = 0;
  493. transferConfigB.srcTransferSize = kEDMA_TransferSize1Bytes;
  494. if (handle->bitsPerFrame <= 8)
  495. {
  496. transferConfigB.destTransferSize = kEDMA_TransferSize1Bytes;
  497. transferConfigB.minorLoopBytes = 1;
  498. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  499. {
  500. transferConfigB.majorLoopCounts = handle->remainingSendByteCount - 2;
  501. }
  502. else
  503. {
  504. /*Only enable channel_B minorlink to channel_C , so need to add one count due to the last time is
  505. majorlink , the majorlink would not trigger the channel_C*/
  506. transferConfigB.majorLoopCounts = handle->remainingSendByteCount + 1;
  507. }
  508. }
  509. else
  510. {
  511. transferConfigB.destTransferSize = kEDMA_TransferSize2Bytes;
  512. transferConfigB.minorLoopBytes = 2;
  513. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  514. {
  515. transferConfigB.majorLoopCounts = handle->remainingSendByteCount / 2 - 2;
  516. }
  517. else
  518. {
  519. /*Only enable channel_B minorlink to channel_C , so need to add one count due to the last time is
  520. * majorlink*/
  521. transferConfigB.majorLoopCounts = handle->remainingSendByteCount / 2 + 1;
  522. }
  523. }
  524. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  525. {
  526. EDMA_SetTransferConfig(handle->edmaTxDataToIntermediaryHandle->base,
  527. handle->edmaTxDataToIntermediaryHandle->channel, &transferConfigB, softwareTCD);
  528. EDMA_EnableAutoStopRequest(handle->edmaIntermediaryToTxRegHandle->base,
  529. handle->edmaIntermediaryToTxRegHandle->channel, false);
  530. }
  531. else
  532. {
  533. EDMA_SetTransferConfig(handle->edmaTxDataToIntermediaryHandle->base,
  534. handle->edmaTxDataToIntermediaryHandle->channel, &transferConfigB, NULL);
  535. }
  536. }
  537. else
  538. {
  539. EDMA_SetTransferConfig(handle->edmaTxDataToIntermediaryHandle->base,
  540. handle->edmaTxDataToIntermediaryHandle->channel, &transferConfigB, NULL);
  541. }
  542. /***channel_C ***carry the "intermediary" to SPIx_PUSHR. used the edma Scatter Gather function on channel_C to
  543. handle the last data */
  544. EDMA_ResetChannel(handle->edmaIntermediaryToTxRegHandle->base, handle->edmaIntermediaryToTxRegHandle->channel);
  545. /*For DSPI instances with shared RX/TX DMA requests: use the scatter/gather to prepare the last data
  546. * (handle->lastCommand) to SPI_PUSHR*/
  547. if (((1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base)) && (handle->remainingSendByteCount > 0)))
  548. {
  549. transferConfigC.srcAddr = (uint32_t) & (handle->lastCommand);
  550. transferConfigC.destAddr = (uint32_t)txAddr;
  551. transferConfigC.srcTransferSize = kEDMA_TransferSize4Bytes;
  552. transferConfigC.destTransferSize = kEDMA_TransferSize4Bytes;
  553. transferConfigC.srcOffset = 0;
  554. transferConfigC.destOffset = 0;
  555. transferConfigC.minorLoopBytes = 4;
  556. transferConfigC.majorLoopCounts = 1;
  557. EDMA_TcdReset(softwareTCD);
  558. EDMA_TcdSetTransferConfig(softwareTCD, &transferConfigC, NULL);
  559. }
  560. if (((handle->remainingSendByteCount > 1) && (handle->bitsPerFrame <= 8)) ||
  561. ((handle->remainingSendByteCount > 2) && (handle->bitsPerFrame > 8)) ||
  562. (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base)))
  563. {
  564. transferConfigC.srcAddr = (uint32_t)(&(handle->command));
  565. transferConfigC.destAddr = (uint32_t)txAddr;
  566. transferConfigC.srcTransferSize = kEDMA_TransferSize4Bytes;
  567. transferConfigC.destTransferSize = kEDMA_TransferSize4Bytes;
  568. transferConfigC.srcOffset = 0;
  569. transferConfigC.destOffset = 0;
  570. transferConfigC.minorLoopBytes = 4;
  571. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  572. {
  573. if (handle->bitsPerFrame <= 8)
  574. {
  575. transferConfigC.majorLoopCounts = handle->remainingSendByteCount - 1;
  576. }
  577. else
  578. {
  579. transferConfigC.majorLoopCounts = handle->remainingSendByteCount / 2 - 1;
  580. }
  581. EDMA_SetTransferConfig(handle->edmaIntermediaryToTxRegHandle->base,
  582. handle->edmaIntermediaryToTxRegHandle->channel, &transferConfigC, softwareTCD);
  583. }
  584. else
  585. {
  586. transferConfigC.majorLoopCounts = 1;
  587. EDMA_SetTransferConfig(handle->edmaIntermediaryToTxRegHandle->base,
  588. handle->edmaIntermediaryToTxRegHandle->channel, &transferConfigC, NULL);
  589. }
  590. EDMA_EnableAutoStopRequest(handle->edmaIntermediaryToTxRegHandle->base,
  591. handle->edmaIntermediaryToTxRegHandle->channel, false);
  592. }
  593. else
  594. {
  595. EDMA_SetTransferConfig(handle->edmaIntermediaryToTxRegHandle->base,
  596. handle->edmaIntermediaryToTxRegHandle->channel, &transferConfigC, NULL);
  597. }
  598. /*Start the EDMA channel_A , channel_B , channel_C transfer*/
  599. EDMA_StartTransfer(handle->edmaRxRegToRxDataHandle);
  600. EDMA_StartTransfer(handle->edmaTxDataToIntermediaryHandle);
  601. EDMA_StartTransfer(handle->edmaIntermediaryToTxRegHandle);
  602. /*Set channel priority*/
  603. uint8_t channelPriorityLow = handle->edmaRxRegToRxDataHandle->channel;
  604. uint8_t channelPriorityMid = handle->edmaTxDataToIntermediaryHandle->channel;
  605. uint8_t channelPriorityHigh = handle->edmaIntermediaryToTxRegHandle->channel;
  606. uint8_t t = 0;
  607. if (channelPriorityLow > channelPriorityMid)
  608. {
  609. t = channelPriorityLow;
  610. channelPriorityLow = channelPriorityMid;
  611. channelPriorityMid = t;
  612. }
  613. if (channelPriorityLow > channelPriorityHigh)
  614. {
  615. t = channelPriorityLow;
  616. channelPriorityLow = channelPriorityHigh;
  617. channelPriorityHigh = t;
  618. }
  619. if (channelPriorityMid > channelPriorityHigh)
  620. {
  621. t = channelPriorityMid;
  622. channelPriorityMid = channelPriorityHigh;
  623. channelPriorityHigh = t;
  624. }
  625. edma_channel_Preemption_config_t preemption_config_t;
  626. preemption_config_t.enableChannelPreemption = true;
  627. preemption_config_t.enablePreemptAbility = true;
  628. preemption_config_t.channelPriority = channelPriorityLow;
  629. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  630. {
  631. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  632. &preemption_config_t);
  633. preemption_config_t.channelPriority = channelPriorityMid;
  634. EDMA_SetChannelPreemptionConfig(handle->edmaTxDataToIntermediaryHandle->base,
  635. handle->edmaTxDataToIntermediaryHandle->channel, &preemption_config_t);
  636. preemption_config_t.channelPriority = channelPriorityHigh;
  637. EDMA_SetChannelPreemptionConfig(handle->edmaIntermediaryToTxRegHandle->base,
  638. handle->edmaIntermediaryToTxRegHandle->channel, &preemption_config_t);
  639. }
  640. else
  641. {
  642. EDMA_SetChannelPreemptionConfig(handle->edmaIntermediaryToTxRegHandle->base,
  643. handle->edmaIntermediaryToTxRegHandle->channel, &preemption_config_t);
  644. preemption_config_t.channelPriority = channelPriorityMid;
  645. EDMA_SetChannelPreemptionConfig(handle->edmaTxDataToIntermediaryHandle->base,
  646. handle->edmaTxDataToIntermediaryHandle->channel, &preemption_config_t);
  647. preemption_config_t.channelPriority = channelPriorityHigh;
  648. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  649. &preemption_config_t);
  650. }
  651. /*Set the channel link.*/
  652. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  653. {
  654. /*if there is Tx DMA request , carry the 32bits data (handle->command) to PUSHR first , then link to channelB
  655. to prepare the next 32bits data (txData to handle->command) */
  656. if (handle->remainingSendByteCount > 1)
  657. {
  658. EDMA_SetChannelLink(handle->edmaIntermediaryToTxRegHandle->base,
  659. handle->edmaIntermediaryToTxRegHandle->channel, kEDMA_MajorLink,
  660. handle->edmaTxDataToIntermediaryHandle->channel);
  661. }
  662. DSPI_EnableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  663. }
  664. else
  665. {
  666. if (handle->remainingSendByteCount > 0)
  667. {
  668. EDMA_SetChannelLink(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  669. kEDMA_MinorLink, handle->edmaTxDataToIntermediaryHandle->channel);
  670. EDMA_SetChannelLink(handle->edmaTxDataToIntermediaryHandle->base,
  671. handle->edmaTxDataToIntermediaryHandle->channel, kEDMA_MinorLink,
  672. handle->edmaIntermediaryToTxRegHandle->channel);
  673. }
  674. DSPI_EnableDMA(base, kDSPI_RxDmaEnable);
  675. }
  676. DSPI_StartTransfer(base);
  677. return kStatus_Success;
  678. }
  679. static void EDMA_DspiMasterCallback(edma_handle_t *edmaHandle,
  680. void *g_dspiEdmaPrivateHandle,
  681. bool transferDone,
  682. uint32_t tcds)
  683. {
  684. assert(edmaHandle);
  685. assert(g_dspiEdmaPrivateHandle);
  686. dspi_master_edma_private_handle_t *dspiEdmaPrivateHandle;
  687. dspiEdmaPrivateHandle = (dspi_master_edma_private_handle_t *)g_dspiEdmaPrivateHandle;
  688. DSPI_DisableDMA((dspiEdmaPrivateHandle->base), kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  689. dspiEdmaPrivateHandle->handle->state = kDSPI_Idle;
  690. if (dspiEdmaPrivateHandle->handle->callback)
  691. {
  692. dspiEdmaPrivateHandle->handle->callback(dspiEdmaPrivateHandle->base, dspiEdmaPrivateHandle->handle,
  693. kStatus_Success, dspiEdmaPrivateHandle->handle->userData);
  694. }
  695. }
  696. void DSPI_MasterTransferAbortEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle)
  697. {
  698. assert(handle);
  699. DSPI_StopTransfer(base);
  700. DSPI_DisableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  701. EDMA_AbortTransfer(handle->edmaRxRegToRxDataHandle);
  702. EDMA_AbortTransfer(handle->edmaTxDataToIntermediaryHandle);
  703. EDMA_AbortTransfer(handle->edmaIntermediaryToTxRegHandle);
  704. handle->state = kDSPI_Idle;
  705. }
  706. status_t DSPI_MasterTransferGetCountEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle, size_t *count)
  707. {
  708. assert(handle);
  709. if (!count)
  710. {
  711. return kStatus_InvalidArgument;
  712. }
  713. /* Catch when there is not an active transfer. */
  714. if (handle->state != kDSPI_Busy)
  715. {
  716. *count = 0;
  717. return kStatus_NoTransferInProgress;
  718. }
  719. size_t bytes;
  720. bytes = (uint32_t)handle->nbytes * EDMA_GetRemainingMajorLoopCount(handle->edmaRxRegToRxDataHandle->base,
  721. handle->edmaRxRegToRxDataHandle->channel);
  722. *count = handle->totalByteCount - bytes;
  723. return kStatus_Success;
  724. }
  725. void DSPI_SlaveTransferCreateHandleEDMA(SPI_Type *base,
  726. dspi_slave_edma_handle_t *handle,
  727. dspi_slave_edma_transfer_callback_t callback,
  728. void *userData,
  729. edma_handle_t *edmaRxRegToRxDataHandle,
  730. edma_handle_t *edmaTxDataToTxRegHandle)
  731. {
  732. assert(handle);
  733. assert(edmaRxRegToRxDataHandle);
  734. assert(edmaTxDataToTxRegHandle);
  735. /* Zero the handle. */
  736. memset(handle, 0, sizeof(*handle));
  737. uint32_t instance = DSPI_GetInstance(base);
  738. s_dspiSlaveEdmaPrivateHandle[instance].base = base;
  739. s_dspiSlaveEdmaPrivateHandle[instance].handle = handle;
  740. handle->callback = callback;
  741. handle->userData = userData;
  742. handle->edmaRxRegToRxDataHandle = edmaRxRegToRxDataHandle;
  743. handle->edmaTxDataToTxRegHandle = edmaTxDataToTxRegHandle;
  744. }
  745. status_t DSPI_SlaveTransferEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle, dspi_transfer_t *transfer)
  746. {
  747. assert(handle);
  748. assert(transfer);
  749. /* If send/receive length is zero */
  750. if (transfer->dataSize == 0)
  751. {
  752. return kStatus_InvalidArgument;
  753. }
  754. /* If both send buffer and receive buffer is null */
  755. if ((!(transfer->txData)) && (!(transfer->rxData)))
  756. {
  757. return kStatus_InvalidArgument;
  758. }
  759. /* Check that we're not busy.*/
  760. if (handle->state == kDSPI_Busy)
  761. {
  762. return kStatus_DSPI_Busy;
  763. }
  764. handle->state = kDSPI_Busy;
  765. uint32_t instance = DSPI_GetInstance(base);
  766. uint8_t whichCtar = (transfer->configFlags & DSPI_SLAVE_CTAR_MASK) >> DSPI_SLAVE_CTAR_SHIFT;
  767. handle->bitsPerFrame =
  768. (((base->CTAR_SLAVE[whichCtar]) & SPI_CTAR_SLAVE_FMSZ_MASK) >> SPI_CTAR_SLAVE_FMSZ_SHIFT) + 1;
  769. /* If using a shared RX/TX DMA request, then this limits the amount of data we can transfer
  770. * due to the linked channel. The max bytes is 511 if 8-bit/frame or 1022 if 16-bit/frame
  771. */
  772. uint32_t limited_size = 0;
  773. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  774. {
  775. limited_size = 32767u;
  776. }
  777. else
  778. {
  779. limited_size = 511u;
  780. }
  781. if (handle->bitsPerFrame > 8)
  782. {
  783. if (transfer->dataSize > (limited_size << 1u))
  784. {
  785. handle->state = kDSPI_Idle;
  786. return kStatus_DSPI_OutOfRange;
  787. }
  788. }
  789. else
  790. {
  791. if (transfer->dataSize > limited_size)
  792. {
  793. handle->state = kDSPI_Idle;
  794. return kStatus_DSPI_OutOfRange;
  795. }
  796. }
  797. /*The data size should be even if the bitsPerFrame is greater than 8 (that is 2 bytes per frame in dspi) */
  798. if ((handle->bitsPerFrame > 8) && (transfer->dataSize & 0x1))
  799. {
  800. handle->state = kDSPI_Idle;
  801. return kStatus_InvalidArgument;
  802. }
  803. EDMA_SetCallback(handle->edmaRxRegToRxDataHandle, EDMA_DspiSlaveCallback, &s_dspiSlaveEdmaPrivateHandle[instance]);
  804. /* Store transfer information */
  805. handle->txData = transfer->txData;
  806. handle->rxData = transfer->rxData;
  807. handle->remainingSendByteCount = transfer->dataSize;
  808. handle->remainingReceiveByteCount = transfer->dataSize;
  809. handle->totalByteCount = transfer->dataSize;
  810. uint16_t wordToSend = 0;
  811. uint8_t dummyData = DSPI_DUMMY_DATA;
  812. uint8_t dataAlreadyFed = 0;
  813. uint8_t dataFedMax = 2;
  814. uint32_t rxAddr = DSPI_GetRxRegisterAddress(base);
  815. uint32_t txAddr = DSPI_SlaveGetTxRegisterAddress(base);
  816. edma_transfer_config_t transferConfigA;
  817. edma_transfer_config_t transferConfigC;
  818. DSPI_StopTransfer(base);
  819. DSPI_FlushFifo(base, true, true);
  820. DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
  821. DSPI_DisableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  822. DSPI_StartTransfer(base);
  823. /*if dspi has separate dma request , need not prepare data first .
  824. else (dspi has shared dma request) , send first 2 data into fifo if there is fifo or send first 1 data to
  825. slaveGetTxRegister if there is no fifo*/
  826. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  827. {
  828. /* For DSPI instances with shared RX/TX DMA requests, we'll use the RX DMA request to
  829. * trigger ongoing transfers and will link to the TX DMA channel from the RX DMA channel.
  830. */
  831. /* If bits/frame is greater than one byte */
  832. if (handle->bitsPerFrame > 8)
  833. {
  834. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  835. {
  836. if (handle->txData)
  837. {
  838. wordToSend = *(handle->txData);
  839. ++handle->txData; /* Increment to next data byte */
  840. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  841. ++handle->txData; /* Increment to next data byte */
  842. }
  843. else
  844. {
  845. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  846. }
  847. handle->remainingSendByteCount -= 2; /* decrement remainingSendByteCount by 2 */
  848. base->PUSHR_SLAVE = wordToSend;
  849. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  850. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  851. dataAlreadyFed += 2;
  852. /* Exit loop if send count is zero, else update local variables for next loop */
  853. if ((handle->remainingSendByteCount == 0) || (dataAlreadyFed == (dataFedMax * 2)))
  854. {
  855. break;
  856. }
  857. } /* End of TX FIFO fill while loop */
  858. }
  859. else /* Optimized for bits/frame less than or equal to one byte. */
  860. {
  861. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  862. {
  863. if (handle->txData)
  864. {
  865. wordToSend = *(handle->txData);
  866. /* Increment to next data word*/
  867. ++handle->txData;
  868. }
  869. else
  870. {
  871. wordToSend = dummyData;
  872. }
  873. base->PUSHR_SLAVE = wordToSend;
  874. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  875. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  876. /* Decrement remainingSendByteCount*/
  877. --handle->remainingSendByteCount;
  878. dataAlreadyFed++;
  879. /* Exit loop if send count is zero, else update local variables for next loop */
  880. if ((handle->remainingSendByteCount == 0) || (dataAlreadyFed == dataFedMax))
  881. {
  882. break;
  883. }
  884. } /* End of TX FIFO fill while loop */
  885. }
  886. }
  887. /***channel_A *** used for carry the data from Rx_Data_Register(POPR) to User_Receive_Buffer*/
  888. if (handle->remainingReceiveByteCount > 0)
  889. {
  890. EDMA_ResetChannel(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel);
  891. transferConfigA.srcAddr = (uint32_t)rxAddr;
  892. transferConfigA.srcOffset = 0;
  893. if (handle->rxData)
  894. {
  895. transferConfigA.destAddr = (uint32_t) & (handle->rxData[0]);
  896. transferConfigA.destOffset = 1;
  897. }
  898. else
  899. {
  900. transferConfigA.destAddr = (uint32_t) & (handle->rxBuffIfNull);
  901. transferConfigA.destOffset = 0;
  902. }
  903. transferConfigA.destTransferSize = kEDMA_TransferSize1Bytes;
  904. if (handle->bitsPerFrame <= 8)
  905. {
  906. transferConfigA.srcTransferSize = kEDMA_TransferSize1Bytes;
  907. transferConfigA.minorLoopBytes = 1;
  908. transferConfigA.majorLoopCounts = handle->remainingReceiveByteCount;
  909. }
  910. else
  911. {
  912. transferConfigA.srcTransferSize = kEDMA_TransferSize2Bytes;
  913. transferConfigA.minorLoopBytes = 2;
  914. transferConfigA.majorLoopCounts = handle->remainingReceiveByteCount / 2;
  915. }
  916. /* Store the initially configured eDMA minor byte transfer count into the DSPI handle */
  917. handle->nbytes = transferConfigA.minorLoopBytes;
  918. EDMA_SetTransferConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  919. &transferConfigA, NULL);
  920. EDMA_EnableChannelInterrupts(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  921. kEDMA_MajorInterruptEnable);
  922. }
  923. if (handle->remainingSendByteCount > 0)
  924. {
  925. /***channel_C *** used for carry the data from User_Send_Buffer to Tx_Data_Register(PUSHR_SLAVE)*/
  926. EDMA_ResetChannel(handle->edmaTxDataToTxRegHandle->base, handle->edmaTxDataToTxRegHandle->channel);
  927. transferConfigC.destAddr = (uint32_t)txAddr;
  928. transferConfigC.destOffset = 0;
  929. if (handle->txData)
  930. {
  931. transferConfigC.srcAddr = (uint32_t)(&(handle->txData[0]));
  932. transferConfigC.srcOffset = 1;
  933. }
  934. else
  935. {
  936. transferConfigC.srcAddr = (uint32_t)(&handle->txBuffIfNull);
  937. transferConfigC.srcOffset = 0;
  938. if (handle->bitsPerFrame <= 8)
  939. {
  940. handle->txBuffIfNull = DSPI_DUMMY_DATA;
  941. }
  942. else
  943. {
  944. handle->txBuffIfNull = (DSPI_DUMMY_DATA << 8) | DSPI_DUMMY_DATA;
  945. }
  946. }
  947. transferConfigC.srcTransferSize = kEDMA_TransferSize1Bytes;
  948. if (handle->bitsPerFrame <= 8)
  949. {
  950. transferConfigC.destTransferSize = kEDMA_TransferSize1Bytes;
  951. transferConfigC.minorLoopBytes = 1;
  952. transferConfigC.majorLoopCounts = handle->remainingSendByteCount;
  953. }
  954. else
  955. {
  956. transferConfigC.destTransferSize = kEDMA_TransferSize2Bytes;
  957. transferConfigC.minorLoopBytes = 2;
  958. transferConfigC.majorLoopCounts = handle->remainingSendByteCount / 2;
  959. }
  960. EDMA_SetTransferConfig(handle->edmaTxDataToTxRegHandle->base, handle->edmaTxDataToTxRegHandle->channel,
  961. &transferConfigC, NULL);
  962. EDMA_StartTransfer(handle->edmaTxDataToTxRegHandle);
  963. }
  964. EDMA_StartTransfer(handle->edmaRxRegToRxDataHandle);
  965. /*Set channel priority*/
  966. uint8_t channelPriorityLow = handle->edmaRxRegToRxDataHandle->channel;
  967. uint8_t channelPriorityHigh = handle->edmaTxDataToTxRegHandle->channel;
  968. uint8_t t = 0;
  969. if (channelPriorityLow > channelPriorityHigh)
  970. {
  971. t = channelPriorityLow;
  972. channelPriorityLow = channelPriorityHigh;
  973. channelPriorityHigh = t;
  974. }
  975. edma_channel_Preemption_config_t preemption_config_t;
  976. preemption_config_t.enableChannelPreemption = true;
  977. preemption_config_t.enablePreemptAbility = true;
  978. preemption_config_t.channelPriority = channelPriorityLow;
  979. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  980. {
  981. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  982. &preemption_config_t);
  983. preemption_config_t.channelPriority = channelPriorityHigh;
  984. EDMA_SetChannelPreemptionConfig(handle->edmaTxDataToTxRegHandle->base, handle->edmaTxDataToTxRegHandle->channel,
  985. &preemption_config_t);
  986. }
  987. else
  988. {
  989. EDMA_SetChannelPreemptionConfig(handle->edmaTxDataToTxRegHandle->base, handle->edmaTxDataToTxRegHandle->channel,
  990. &preemption_config_t);
  991. preemption_config_t.channelPriority = channelPriorityHigh;
  992. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  993. &preemption_config_t);
  994. }
  995. /*Set the channel link.
  996. For DSPI instances with shared RX/TX DMA requests: Rx DMA request -> channel_A -> channel_C.
  997. For DSPI instances with separate RX and TX DMA requests:
  998. Rx DMA request -> channel_A
  999. Tx DMA request -> channel_C */
  1000. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  1001. {
  1002. if (handle->remainingSendByteCount > 0)
  1003. {
  1004. EDMA_SetChannelLink(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  1005. kEDMA_MinorLink, handle->edmaTxDataToTxRegHandle->channel);
  1006. }
  1007. DSPI_EnableDMA(base, kDSPI_RxDmaEnable);
  1008. }
  1009. else
  1010. {
  1011. DSPI_EnableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  1012. }
  1013. return kStatus_Success;
  1014. }
  1015. static void EDMA_DspiSlaveCallback(edma_handle_t *edmaHandle,
  1016. void *g_dspiEdmaPrivateHandle,
  1017. bool transferDone,
  1018. uint32_t tcds)
  1019. {
  1020. assert(edmaHandle);
  1021. assert(g_dspiEdmaPrivateHandle);
  1022. dspi_slave_edma_private_handle_t *dspiEdmaPrivateHandle;
  1023. dspiEdmaPrivateHandle = (dspi_slave_edma_private_handle_t *)g_dspiEdmaPrivateHandle;
  1024. DSPI_DisableDMA((dspiEdmaPrivateHandle->base), kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  1025. dspiEdmaPrivateHandle->handle->state = kDSPI_Idle;
  1026. if (dspiEdmaPrivateHandle->handle->callback)
  1027. {
  1028. dspiEdmaPrivateHandle->handle->callback(dspiEdmaPrivateHandle->base, dspiEdmaPrivateHandle->handle,
  1029. kStatus_Success, dspiEdmaPrivateHandle->handle->userData);
  1030. }
  1031. }
  1032. void DSPI_SlaveTransferAbortEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle)
  1033. {
  1034. assert(handle);
  1035. DSPI_StopTransfer(base);
  1036. DSPI_DisableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  1037. EDMA_AbortTransfer(handle->edmaRxRegToRxDataHandle);
  1038. EDMA_AbortTransfer(handle->edmaTxDataToTxRegHandle);
  1039. handle->state = kDSPI_Idle;
  1040. }
  1041. status_t DSPI_SlaveTransferGetCountEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle, size_t *count)
  1042. {
  1043. assert(handle);
  1044. if (!count)
  1045. {
  1046. return kStatus_InvalidArgument;
  1047. }
  1048. /* Catch when there is not an active transfer. */
  1049. if (handle->state != kDSPI_Busy)
  1050. {
  1051. *count = 0;
  1052. return kStatus_NoTransferInProgress;
  1053. }
  1054. size_t bytes;
  1055. bytes = (uint32_t)handle->nbytes * EDMA_GetRemainingMajorLoopCount(handle->edmaRxRegToRxDataHandle->base,
  1056. handle->edmaRxRegToRxDataHandle->channel);
  1057. *count = handle->totalByteCount - bytes;
  1058. return kStatus_Success;
  1059. }