fsl_dspi_edma.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_dspi_edma.h"
  35. /***********************************************************************************************************************
  36. * Definitions
  37. ***********************************************************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.dspi_edma"
  41. #endif
  42. /*!
  43. * @brief Structure definition for dspi_master_edma_private_handle_t. The structure is private.
  44. */
  45. typedef struct _dspi_master_edma_private_handle
  46. {
  47. SPI_Type *base; /*!< DSPI peripheral base address. */
  48. dspi_master_edma_handle_t *handle; /*!< dspi_master_edma_handle_t handle */
  49. } dspi_master_edma_private_handle_t;
  50. /*!
  51. * @brief Structure definition for dspi_slave_edma_private_handle_t. The structure is private.
  52. */
  53. typedef struct _dspi_slave_edma_private_handle
  54. {
  55. SPI_Type *base; /*!< DSPI peripheral base address. */
  56. dspi_slave_edma_handle_t *handle; /*!< dspi_master_edma_handle_t handle */
  57. } dspi_slave_edma_private_handle_t;
  58. /***********************************************************************************************************************
  59. * Prototypes
  60. ***********************************************************************************************************************/
  61. /*!
  62. * @brief EDMA_DspiMasterCallback after the DSPI master transfer completed by using EDMA.
  63. * This is not a public API.
  64. */
  65. static void EDMA_DspiMasterCallback(edma_handle_t *edmaHandle,
  66. void *g_dspiEdmaPrivateHandle,
  67. bool transferDone,
  68. uint32_t tcds);
  69. /*!
  70. * @brief EDMA_DspiSlaveCallback after the DSPI slave transfer completed by using EDMA.
  71. * This is not a public API.
  72. */
  73. static void EDMA_DspiSlaveCallback(edma_handle_t *edmaHandle,
  74. void *g_dspiEdmaPrivateHandle,
  75. bool transferDone,
  76. uint32_t tcds);
  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. #if (!(defined(FSL_FEATURE_DSPI_HAS_GASKET) && FSL_FEATURE_DSPI_HAS_GASKET))
  97. assert(edmaTxDataToIntermediaryHandle);
  98. #endif
  99. assert(edmaIntermediaryToTxRegHandle);
  100. /* Zero the handle. */
  101. memset(handle, 0, sizeof(*handle));
  102. uint32_t instance = DSPI_GetInstance(base);
  103. s_dspiMasterEdmaPrivateHandle[instance].base = base;
  104. s_dspiMasterEdmaPrivateHandle[instance].handle = handle;
  105. handle->callback = callback;
  106. handle->userData = userData;
  107. handle->edmaRxRegToRxDataHandle = edmaRxRegToRxDataHandle;
  108. handle->edmaTxDataToIntermediaryHandle = edmaTxDataToIntermediaryHandle;
  109. handle->edmaIntermediaryToTxRegHandle = edmaIntermediaryToTxRegHandle;
  110. }
  111. status_t DSPI_MasterTransferEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle, dspi_transfer_t *transfer)
  112. {
  113. assert(handle);
  114. assert(transfer);
  115. /* If the transfer count is zero, then return immediately.*/
  116. if (transfer->dataSize == 0)
  117. {
  118. return kStatus_InvalidArgument;
  119. }
  120. /* If both send buffer and receive buffer is null */
  121. if ((!(transfer->txData)) && (!(transfer->rxData)))
  122. {
  123. return kStatus_InvalidArgument;
  124. }
  125. /* Check that we're not busy.*/
  126. if (handle->state == kDSPI_Busy)
  127. {
  128. return kStatus_DSPI_Busy;
  129. }
  130. handle->state = kDSPI_Busy;
  131. uint32_t instance = DSPI_GetInstance(base);
  132. uint16_t wordToSend = 0;
  133. uint8_t dummyData = g_dspiDummyData[DSPI_GetInstance(base)];
  134. uint8_t dataAlreadyFed = 0;
  135. uint8_t dataFedMax = 2;
  136. uint32_t rxAddr = DSPI_GetRxRegisterAddress(base);
  137. uint32_t txAddr = DSPI_MasterGetTxRegisterAddress(base);
  138. edma_tcd_t *softwareTCD = (edma_tcd_t *)((uint32_t)(&handle->dspiSoftwareTCD[1]) & (~0x1FU));
  139. edma_transfer_config_t transferConfigA;
  140. edma_transfer_config_t transferConfigB;
  141. handle->txBuffIfNull = ((uint32_t)dummyData << 8) | dummyData;
  142. dspi_command_data_config_t commandStruct;
  143. DSPI_StopTransfer(base);
  144. DSPI_FlushFifo(base, true, true);
  145. DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
  146. commandStruct.whichPcs =
  147. (dspi_which_pcs_t)(1U << ((transfer->configFlags & DSPI_MASTER_PCS_MASK) >> DSPI_MASTER_PCS_SHIFT));
  148. commandStruct.isEndOfQueue = false;
  149. commandStruct.clearTransferCount = false;
  150. commandStruct.whichCtar =
  151. (dspi_ctar_selection_t)((transfer->configFlags & DSPI_MASTER_CTAR_MASK) >> DSPI_MASTER_CTAR_SHIFT);
  152. commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterPcsContinuous);
  153. handle->command = DSPI_MasterGetFormattedCommand(&(commandStruct));
  154. commandStruct.isEndOfQueue = true;
  155. commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterActiveAfterTransfer);
  156. handle->lastCommand = DSPI_MasterGetFormattedCommand(&(commandStruct));
  157. handle->bitsPerFrame = ((base->CTAR[commandStruct.whichCtar] & SPI_CTAR_FMSZ_MASK) >> SPI_CTAR_FMSZ_SHIFT) + 1;
  158. if ((base->MCR & SPI_MCR_DIS_RXF_MASK) || (base->MCR & SPI_MCR_DIS_TXF_MASK))
  159. {
  160. handle->fifoSize = 1;
  161. }
  162. else
  163. {
  164. handle->fifoSize = FSL_FEATURE_DSPI_FIFO_SIZEn(base);
  165. }
  166. handle->txData = transfer->txData;
  167. handle->rxData = transfer->rxData;
  168. handle->remainingSendByteCount = transfer->dataSize;
  169. handle->remainingReceiveByteCount = transfer->dataSize;
  170. handle->totalByteCount = transfer->dataSize;
  171. /* If using a shared RX/TX DMA request, then this limits the amount of data we can transfer
  172. * due to the linked channel. The max bytes is 511 if 8-bit/frame or 1022 if 16-bit/frame
  173. */
  174. uint32_t limited_size = 0;
  175. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  176. {
  177. limited_size = 32767u;
  178. }
  179. else
  180. {
  181. limited_size = 511u;
  182. }
  183. if (handle->bitsPerFrame > 8)
  184. {
  185. if (transfer->dataSize > (limited_size << 1u))
  186. {
  187. handle->state = kDSPI_Idle;
  188. return kStatus_DSPI_OutOfRange;
  189. }
  190. }
  191. else
  192. {
  193. if (transfer->dataSize > limited_size)
  194. {
  195. handle->state = kDSPI_Idle;
  196. return kStatus_DSPI_OutOfRange;
  197. }
  198. }
  199. /*The data size should be even if the bitsPerFrame is greater than 8 (that is 2 bytes per frame in dspi) */
  200. if ((handle->bitsPerFrame > 8) && (transfer->dataSize & 0x1))
  201. {
  202. handle->state = kDSPI_Idle;
  203. return kStatus_InvalidArgument;
  204. }
  205. DSPI_DisableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  206. EDMA_SetCallback(handle->edmaRxRegToRxDataHandle, EDMA_DspiMasterCallback,
  207. &s_dspiMasterEdmaPrivateHandle[instance]);
  208. /*
  209. (1)For DSPI instances with shared RX/TX DMA requests: Rx DMA request -> channel_A -> channel_B-> channel_C.
  210. channel_A minor link to channel_B , channel_B minor link to channel_C.
  211. Already pushed 1 or 2 data in SPI_PUSHR , then start the DMA tansfer.
  212. channel_A:SPI_POPR to rxData,
  213. channel_B:next txData to handle->command (low 16 bits),
  214. channel_C:handle->command (32 bits) to SPI_PUSHR, and use the scatter/gather to transfer the last data
  215. (handle->lastCommand to SPI_PUSHR).
  216. (2)For DSPI instances with separate RX and TX DMA requests:
  217. Rx DMA request -> channel_A
  218. Tx DMA request -> channel_C -> channel_B .
  219. channel_C major link to channel_B.
  220. So need prepare the first data in "intermediary" before the DMA
  221. transfer and then channel_B is used to prepare the next data to "intermediary"
  222. channel_A:SPI_POPR to rxData,
  223. channel_C: handle->command (32 bits) to SPI_PUSHR,
  224. channel_B: next txData to handle->command (low 16 bits), and use the scatter/gather to prepare the last data
  225. (handle->lastCommand to handle->Command).
  226. */
  227. /*If dspi has separate dma request , prepare the first data in "intermediary" .
  228. else (dspi has shared dma request) , send first 2 data if there is fifo or send first 1 data if there is no fifo*/
  229. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  230. {
  231. /* For DSPI instances with separate RX/TX DMA requests, we'll use the TX DMA request to
  232. * trigger the TX DMA channel and RX DMA request to trigger the RX DMA channel
  233. */
  234. /*Prepare the firt data*/
  235. if (handle->bitsPerFrame > 8)
  236. {
  237. /* If it's the last word */
  238. if (handle->remainingSendByteCount <= 2)
  239. {
  240. if (handle->txData)
  241. {
  242. wordToSend = *(handle->txData);
  243. ++handle->txData; /* increment to next data byte */
  244. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  245. }
  246. else
  247. {
  248. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  249. }
  250. handle->lastCommand = (handle->lastCommand & 0xffff0000U) | wordToSend;
  251. handle->command = handle->lastCommand;
  252. }
  253. else /* For all words except the last word , frame > 8bits */
  254. {
  255. if (handle->txData)
  256. {
  257. wordToSend = *(handle->txData);
  258. ++handle->txData; /* increment to next data byte */
  259. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  260. ++handle->txData; /* increment to next data byte */
  261. }
  262. else
  263. {
  264. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  265. }
  266. handle->command = (handle->command & 0xffff0000U) | wordToSend;
  267. }
  268. }
  269. else /* Optimized for bits/frame less than or equal to one byte. */
  270. {
  271. if (handle->txData)
  272. {
  273. wordToSend = *(handle->txData);
  274. ++handle->txData; /* increment to next data word*/
  275. }
  276. else
  277. {
  278. wordToSend = dummyData;
  279. }
  280. if (handle->remainingSendByteCount == 1)
  281. {
  282. handle->lastCommand = (handle->lastCommand & 0xffff0000U) | wordToSend;
  283. handle->command = handle->lastCommand;
  284. }
  285. else
  286. {
  287. handle->command = (handle->command & 0xffff0000U) | wordToSend;
  288. }
  289. }
  290. }
  291. else /*dspi has shared dma request*/
  292. {
  293. /* For DSPI instances with shared RX/TX DMA requests, we'll use the RX DMA request to
  294. * trigger ongoing transfers and will link to the TX DMA channel from the RX DMA channel.
  295. */
  296. /* If bits/frame is greater than one byte */
  297. if (handle->bitsPerFrame > 8)
  298. {
  299. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  300. {
  301. if (handle->remainingSendByteCount <= 2)
  302. {
  303. if (handle->txData)
  304. {
  305. wordToSend = *(handle->txData);
  306. ++handle->txData;
  307. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  308. }
  309. else
  310. {
  311. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  312. }
  313. handle->remainingSendByteCount = 0;
  314. base->PUSHR = (handle->lastCommand & 0xffff0000U) | wordToSend;
  315. }
  316. /* For all words except the last word */
  317. else
  318. {
  319. if (handle->txData)
  320. {
  321. wordToSend = *(handle->txData);
  322. ++handle->txData;
  323. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  324. ++handle->txData;
  325. }
  326. else
  327. {
  328. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  329. }
  330. handle->remainingSendByteCount -= 2;
  331. base->PUSHR = (handle->command & 0xffff0000U) | wordToSend;
  332. }
  333. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  334. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  335. dataAlreadyFed += 2;
  336. /* exit loop if send count is zero, else update local variables for next loop */
  337. if ((handle->remainingSendByteCount == 0) || (dataAlreadyFed == (dataFedMax * 2)))
  338. {
  339. break;
  340. }
  341. } /* End of TX FIFO fill while loop */
  342. }
  343. else /* Optimized for bits/frame less than or equal to one byte. */
  344. {
  345. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  346. {
  347. if (handle->txData)
  348. {
  349. wordToSend = *(handle->txData);
  350. ++handle->txData;
  351. }
  352. else
  353. {
  354. wordToSend = dummyData;
  355. }
  356. if (handle->remainingSendByteCount == 1)
  357. {
  358. base->PUSHR = (handle->lastCommand & 0xffff0000U) | wordToSend;
  359. }
  360. else
  361. {
  362. base->PUSHR = (handle->command & 0xffff0000U) | wordToSend;
  363. }
  364. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  365. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  366. --handle->remainingSendByteCount;
  367. dataAlreadyFed++;
  368. /* exit loop if send count is zero, else update local variables for next loop */
  369. if ((handle->remainingSendByteCount == 0) || (dataAlreadyFed == dataFedMax))
  370. {
  371. break;
  372. }
  373. } /* End of TX FIFO fill while loop */
  374. }
  375. }
  376. /***channel_A *** used for carry the data from Rx_Data_Register(POPR) to User_Receive_Buffer(rxData)*/
  377. EDMA_ResetChannel(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel);
  378. transferConfigA.srcAddr = (uint32_t)rxAddr;
  379. transferConfigA.srcOffset = 0;
  380. if (handle->rxData)
  381. {
  382. transferConfigA.destAddr = (uint32_t) & (handle->rxData[0]);
  383. transferConfigA.destOffset = 1;
  384. }
  385. else
  386. {
  387. transferConfigA.destAddr = (uint32_t) & (handle->rxBuffIfNull);
  388. transferConfigA.destOffset = 0;
  389. }
  390. transferConfigA.destTransferSize = kEDMA_TransferSize1Bytes;
  391. if (handle->bitsPerFrame <= 8)
  392. {
  393. transferConfigA.srcTransferSize = kEDMA_TransferSize1Bytes;
  394. transferConfigA.minorLoopBytes = 1;
  395. transferConfigA.majorLoopCounts = handle->remainingReceiveByteCount;
  396. }
  397. else
  398. {
  399. transferConfigA.srcTransferSize = kEDMA_TransferSize2Bytes;
  400. transferConfigA.minorLoopBytes = 2;
  401. transferConfigA.majorLoopCounts = handle->remainingReceiveByteCount / 2;
  402. }
  403. /* Store the initially configured eDMA minor byte transfer count into the DSPI handle */
  404. handle->nbytes = transferConfigA.minorLoopBytes;
  405. EDMA_SetTransferConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  406. &transferConfigA, NULL);
  407. EDMA_EnableChannelInterrupts(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  408. kEDMA_MajorInterruptEnable);
  409. /*Calculate the last data : handle->lastCommand*/
  410. if (((handle->remainingSendByteCount > 0) && (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))) ||
  411. ((((handle->remainingSendByteCount > 1) && (handle->bitsPerFrame <= 8)) ||
  412. ((handle->remainingSendByteCount > 2) && (handle->bitsPerFrame > 8))) &&
  413. (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))))
  414. {
  415. if (handle->txData)
  416. {
  417. uint32_t bufferIndex = 0;
  418. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  419. {
  420. if (handle->bitsPerFrame <= 8)
  421. {
  422. bufferIndex = handle->remainingSendByteCount - 1;
  423. }
  424. else
  425. {
  426. bufferIndex = handle->remainingSendByteCount - 2;
  427. }
  428. }
  429. else
  430. {
  431. bufferIndex = handle->remainingSendByteCount;
  432. }
  433. if (handle->bitsPerFrame <= 8)
  434. {
  435. handle->lastCommand = (handle->lastCommand & 0xffff0000U) | handle->txData[bufferIndex - 1];
  436. }
  437. else
  438. {
  439. handle->lastCommand = (handle->lastCommand & 0xffff0000U) |
  440. ((uint32_t)handle->txData[bufferIndex - 1] << 8) |
  441. handle->txData[bufferIndex - 2];
  442. }
  443. }
  444. else
  445. {
  446. if (handle->bitsPerFrame <= 8)
  447. {
  448. wordToSend = dummyData;
  449. }
  450. else
  451. {
  452. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  453. }
  454. handle->lastCommand = (handle->lastCommand & 0xffff0000U) | wordToSend;
  455. }
  456. }
  457. /* The feature of GASKET is that the SPI supports 8-bit or 16-bit writes to the PUSH TX FIFO,
  458. * allowing a single write to the command word followed by multiple writes to the transmit word.
  459. * The TX FIFO will save the last command word written, and convert a 8-bit/16-bit write to the
  460. * transmit word into a 32-bit write that pushes both the command word and transmit word into
  461. * the TX FIFO (PUSH TX FIFO Register In Master Mode)
  462. * So, if this feature is supported, we can use use one channel to carry the receive data from
  463. * receive regsiter to user data buffer, use the other channel to carry the data from user data buffer
  464. * to transmit register,and use the scatter/gather function to prepare the last data.
  465. * That is to say, if GASKET feature is supported, we can use only two channels for tansferring data.
  466. */
  467. #if defined(FSL_FEATURE_DSPI_HAS_GASKET) && FSL_FEATURE_DSPI_HAS_GASKET
  468. /* For DSPI instances with separate RX and TX DMA requests: use the scatter/gather to prepare the last data
  469. * (handle->lastCommand) to PUSHR register.
  470. */
  471. EDMA_ResetChannel(handle->edmaIntermediaryToTxRegHandle->base, handle->edmaIntermediaryToTxRegHandle->channel);
  472. if ((1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base)) ||
  473. ((handle->remainingSendByteCount > 0) && (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))))
  474. {
  475. transferConfigB.srcAddr = (uint32_t) & (handle->lastCommand);
  476. transferConfigB.destAddr = (uint32_t)txAddr;
  477. transferConfigB.srcTransferSize = kEDMA_TransferSize4Bytes;
  478. transferConfigB.destTransferSize = kEDMA_TransferSize4Bytes;
  479. transferConfigB.srcOffset = 0;
  480. transferConfigB.destOffset = 0;
  481. transferConfigB.minorLoopBytes = 4;
  482. transferConfigB.majorLoopCounts = 1;
  483. EDMA_TcdReset(softwareTCD);
  484. EDMA_TcdSetTransferConfig(softwareTCD, &transferConfigB, NULL);
  485. }
  486. /*User_Send_Buffer(txData) to PUSHR register. */
  487. if (((handle->remainingSendByteCount > 2) && (handle->bitsPerFrame <= 8)) ||
  488. ((handle->remainingSendByteCount > 4) && (handle->bitsPerFrame > 8)))
  489. {
  490. if (handle->txData)
  491. {
  492. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  493. {
  494. /* For DSPI with separate RX and TX DMA requests, one frame data has been carry
  495. * to handle->command, so need to reduce the pointer of txData.
  496. */
  497. transferConfigB.srcAddr =
  498. (uint32_t)((uint8_t *)(handle->txData) - ((handle->bitsPerFrame <= 8) ? (1U) : (2U)));
  499. transferConfigB.srcOffset = 1;
  500. }
  501. else
  502. {
  503. /* For DSPI with shared RX and TX DMA requests, one or two frame data have been carry
  504. * to PUSHR register, so no need to change the pointer of txData.
  505. */
  506. transferConfigB.srcAddr = (uint32_t)((uint8_t *)(handle->txData));
  507. transferConfigB.srcOffset = 1;
  508. }
  509. }
  510. else
  511. {
  512. transferConfigB.srcAddr = (uint32_t)(&handle->txBuffIfNull);
  513. transferConfigB.srcOffset = 0;
  514. }
  515. transferConfigB.destAddr = (uint32_t)txAddr;
  516. transferConfigB.destOffset = 0;
  517. transferConfigB.srcTransferSize = kEDMA_TransferSize1Bytes;
  518. if (handle->bitsPerFrame <= 8)
  519. {
  520. transferConfigB.destTransferSize = kEDMA_TransferSize1Bytes;
  521. transferConfigB.minorLoopBytes = 1;
  522. transferConfigB.majorLoopCounts = handle->remainingSendByteCount - 1;
  523. }
  524. else
  525. {
  526. transferConfigB.destTransferSize = kEDMA_TransferSize2Bytes;
  527. transferConfigB.minorLoopBytes = 2;
  528. transferConfigB.majorLoopCounts = (handle->remainingSendByteCount / 2) - 1;
  529. }
  530. EDMA_SetTransferConfig(handle->edmaIntermediaryToTxRegHandle->base,
  531. handle->edmaIntermediaryToTxRegHandle->channel, &transferConfigB, softwareTCD);
  532. }
  533. /* If only one word to transmit, only carry the lastcommand. */
  534. else
  535. {
  536. EDMA_SetTransferConfig(handle->edmaIntermediaryToTxRegHandle->base,
  537. handle->edmaIntermediaryToTxRegHandle->channel, &transferConfigB, NULL);
  538. }
  539. /*Start the EDMA channel_A , channel_C. */
  540. EDMA_StartTransfer(handle->edmaRxRegToRxDataHandle);
  541. EDMA_StartTransfer(handle->edmaIntermediaryToTxRegHandle);
  542. /* Set the channel link.
  543. * For DSPI instances with shared TX and RX DMA requests, setup channel minor link, first receive data from the
  544. * receive register, and then carry transmit data to PUSHER register.
  545. * For DSPI instance with separate TX and RX DMA requests, there is no need to set up channel link.
  546. */
  547. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  548. {
  549. /*Set channel priority*/
  550. uint8_t channelPriorityLow = handle->edmaRxRegToRxDataHandle->channel;
  551. uint8_t channelPriorityHigh = handle->edmaIntermediaryToTxRegHandle->channel;
  552. uint8_t t = 0;
  553. if (channelPriorityLow > channelPriorityHigh)
  554. {
  555. t = channelPriorityLow;
  556. channelPriorityLow = channelPriorityHigh;
  557. channelPriorityHigh = t;
  558. }
  559. edma_channel_Preemption_config_t preemption_config_t;
  560. preemption_config_t.enableChannelPreemption = true;
  561. preemption_config_t.enablePreemptAbility = true;
  562. preemption_config_t.channelPriority = channelPriorityLow;
  563. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  564. &preemption_config_t);
  565. preemption_config_t.channelPriority = channelPriorityHigh;
  566. EDMA_SetChannelPreemptionConfig(handle->edmaIntermediaryToTxRegHandle->base,
  567. handle->edmaIntermediaryToTxRegHandle->channel, &preemption_config_t);
  568. /*if there is Rx DMA request , carry the 32bits data (handle->command) to user data first , then link to
  569. channelC to carry the next data to PUSHER register.(txData to PUSHER) */
  570. if (handle->remainingSendByteCount > 0)
  571. {
  572. EDMA_SetChannelLink(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  573. kEDMA_MinorLink, handle->edmaIntermediaryToTxRegHandle->channel);
  574. }
  575. }
  576. DSPI_EnableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  577. /* Setup control info to PUSHER register. */
  578. *((uint16_t *)&(base->PUSHR) + 1) = (handle->command >> 16U);
  579. #else
  580. /***channel_B *** used for carry the data from User_Send_Buffer to "intermediary" because the SPIx_PUSHR should
  581. write the 32bits at once time . Then use channel_C to carry the "intermediary" to SPIx_PUSHR. Note that the
  582. SPIx_PUSHR upper 16 bits are the "command" and the low 16bits are data */
  583. EDMA_ResetChannel(handle->edmaTxDataToIntermediaryHandle->base, handle->edmaTxDataToIntermediaryHandle->channel);
  584. /*For DSPI instances with separate RX and TX DMA requests: use the scatter/gather to prepare the last data
  585. * (handle->lastCommand) to handle->Command*/
  586. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  587. {
  588. transferConfigB.srcAddr = (uint32_t) & (handle->lastCommand);
  589. transferConfigB.destAddr = (uint32_t) & (handle->command);
  590. transferConfigB.srcTransferSize = kEDMA_TransferSize4Bytes;
  591. transferConfigB.destTransferSize = kEDMA_TransferSize4Bytes;
  592. transferConfigB.srcOffset = 0;
  593. transferConfigB.destOffset = 0;
  594. transferConfigB.minorLoopBytes = 4;
  595. transferConfigB.majorLoopCounts = 1;
  596. EDMA_TcdReset(softwareTCD);
  597. EDMA_TcdSetTransferConfig(softwareTCD, &transferConfigB, NULL);
  598. }
  599. /*User_Send_Buffer(txData) to intermediary(handle->command)*/
  600. if (((((handle->remainingSendByteCount > 2) && (handle->bitsPerFrame <= 8)) ||
  601. ((handle->remainingSendByteCount > 4) && (handle->bitsPerFrame > 8))) &&
  602. (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))) ||
  603. (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base)))
  604. {
  605. if (handle->txData)
  606. {
  607. transferConfigB.srcAddr = (uint32_t)(handle->txData);
  608. transferConfigB.srcOffset = 1;
  609. }
  610. else
  611. {
  612. transferConfigB.srcAddr = (uint32_t)(&handle->txBuffIfNull);
  613. transferConfigB.srcOffset = 0;
  614. }
  615. transferConfigB.destAddr = (uint32_t)(&handle->command);
  616. transferConfigB.destOffset = 0;
  617. transferConfigB.srcTransferSize = kEDMA_TransferSize1Bytes;
  618. if (handle->bitsPerFrame <= 8)
  619. {
  620. transferConfigB.destTransferSize = kEDMA_TransferSize1Bytes;
  621. transferConfigB.minorLoopBytes = 1;
  622. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  623. {
  624. transferConfigB.majorLoopCounts = handle->remainingSendByteCount - 2;
  625. }
  626. else
  627. {
  628. /*Only enable channel_B minorlink to channel_C , so need to add one count due to the last time is
  629. majorlink , the majorlink would not trigger the channel_C*/
  630. transferConfigB.majorLoopCounts = handle->remainingSendByteCount + 1;
  631. }
  632. }
  633. else
  634. {
  635. transferConfigB.destTransferSize = kEDMA_TransferSize2Bytes;
  636. transferConfigB.minorLoopBytes = 2;
  637. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  638. {
  639. transferConfigB.majorLoopCounts = handle->remainingSendByteCount / 2 - 2;
  640. }
  641. else
  642. {
  643. /*Only enable channel_B minorlink to channel_C , so need to add one count due to the last time is
  644. * majorlink*/
  645. transferConfigB.majorLoopCounts = handle->remainingSendByteCount / 2 + 1;
  646. }
  647. }
  648. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  649. {
  650. EDMA_SetTransferConfig(handle->edmaTxDataToIntermediaryHandle->base,
  651. handle->edmaTxDataToIntermediaryHandle->channel, &transferConfigB, softwareTCD);
  652. EDMA_EnableAutoStopRequest(handle->edmaIntermediaryToTxRegHandle->base,
  653. handle->edmaIntermediaryToTxRegHandle->channel, false);
  654. }
  655. else
  656. {
  657. EDMA_SetTransferConfig(handle->edmaTxDataToIntermediaryHandle->base,
  658. handle->edmaTxDataToIntermediaryHandle->channel, &transferConfigB, NULL);
  659. }
  660. }
  661. else
  662. {
  663. EDMA_SetTransferConfig(handle->edmaTxDataToIntermediaryHandle->base,
  664. handle->edmaTxDataToIntermediaryHandle->channel, &transferConfigB, NULL);
  665. }
  666. /***channel_C ***carry the "intermediary" to SPIx_PUSHR. used the edma Scatter Gather function on channel_C to
  667. handle the last data */
  668. edma_transfer_config_t transferConfigC;
  669. EDMA_ResetChannel(handle->edmaIntermediaryToTxRegHandle->base, handle->edmaIntermediaryToTxRegHandle->channel);
  670. /*For DSPI instances with shared RX/TX DMA requests: use the scatter/gather to prepare the last data
  671. * (handle->lastCommand) to SPI_PUSHR*/
  672. if (((1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base)) && (handle->remainingSendByteCount > 0)))
  673. {
  674. transferConfigC.srcAddr = (uint32_t) & (handle->lastCommand);
  675. transferConfigC.destAddr = (uint32_t)txAddr;
  676. transferConfigC.srcTransferSize = kEDMA_TransferSize4Bytes;
  677. transferConfigC.destTransferSize = kEDMA_TransferSize4Bytes;
  678. transferConfigC.srcOffset = 0;
  679. transferConfigC.destOffset = 0;
  680. transferConfigC.minorLoopBytes = 4;
  681. transferConfigC.majorLoopCounts = 1;
  682. EDMA_TcdReset(softwareTCD);
  683. EDMA_TcdSetTransferConfig(softwareTCD, &transferConfigC, NULL);
  684. }
  685. if (((handle->remainingSendByteCount > 1) && (handle->bitsPerFrame <= 8)) ||
  686. ((handle->remainingSendByteCount > 2) && (handle->bitsPerFrame > 8)) ||
  687. (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base)))
  688. {
  689. transferConfigC.srcAddr = (uint32_t)(&(handle->command));
  690. transferConfigC.destAddr = (uint32_t)txAddr;
  691. transferConfigC.srcTransferSize = kEDMA_TransferSize4Bytes;
  692. transferConfigC.destTransferSize = kEDMA_TransferSize4Bytes;
  693. transferConfigC.srcOffset = 0;
  694. transferConfigC.destOffset = 0;
  695. transferConfigC.minorLoopBytes = 4;
  696. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  697. {
  698. if (handle->bitsPerFrame <= 8)
  699. {
  700. transferConfigC.majorLoopCounts = handle->remainingSendByteCount - 1;
  701. }
  702. else
  703. {
  704. transferConfigC.majorLoopCounts = handle->remainingSendByteCount / 2 - 1;
  705. }
  706. EDMA_SetTransferConfig(handle->edmaIntermediaryToTxRegHandle->base,
  707. handle->edmaIntermediaryToTxRegHandle->channel, &transferConfigC, softwareTCD);
  708. }
  709. else
  710. {
  711. transferConfigC.majorLoopCounts = 1;
  712. EDMA_SetTransferConfig(handle->edmaIntermediaryToTxRegHandle->base,
  713. handle->edmaIntermediaryToTxRegHandle->channel, &transferConfigC, NULL);
  714. }
  715. EDMA_EnableAutoStopRequest(handle->edmaIntermediaryToTxRegHandle->base,
  716. handle->edmaIntermediaryToTxRegHandle->channel, false);
  717. }
  718. else
  719. {
  720. EDMA_SetTransferConfig(handle->edmaIntermediaryToTxRegHandle->base,
  721. handle->edmaIntermediaryToTxRegHandle->channel, &transferConfigC, NULL);
  722. }
  723. /*Start the EDMA channel_A , channel_B , channel_C transfer*/
  724. EDMA_StartTransfer(handle->edmaRxRegToRxDataHandle);
  725. EDMA_StartTransfer(handle->edmaTxDataToIntermediaryHandle);
  726. EDMA_StartTransfer(handle->edmaIntermediaryToTxRegHandle);
  727. /*Set channel priority*/
  728. uint8_t channelPriorityLow = handle->edmaRxRegToRxDataHandle->channel;
  729. uint8_t channelPriorityMid = handle->edmaTxDataToIntermediaryHandle->channel;
  730. uint8_t channelPriorityHigh = handle->edmaIntermediaryToTxRegHandle->channel;
  731. uint8_t t = 0;
  732. if (channelPriorityLow > channelPriorityMid)
  733. {
  734. t = channelPriorityLow;
  735. channelPriorityLow = channelPriorityMid;
  736. channelPriorityMid = t;
  737. }
  738. if (channelPriorityLow > channelPriorityHigh)
  739. {
  740. t = channelPriorityLow;
  741. channelPriorityLow = channelPriorityHigh;
  742. channelPriorityHigh = t;
  743. }
  744. if (channelPriorityMid > channelPriorityHigh)
  745. {
  746. t = channelPriorityMid;
  747. channelPriorityMid = channelPriorityHigh;
  748. channelPriorityHigh = t;
  749. }
  750. edma_channel_Preemption_config_t preemption_config_t;
  751. preemption_config_t.enableChannelPreemption = true;
  752. preemption_config_t.enablePreemptAbility = true;
  753. preemption_config_t.channelPriority = channelPriorityLow;
  754. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  755. {
  756. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  757. &preemption_config_t);
  758. preemption_config_t.channelPriority = channelPriorityMid;
  759. EDMA_SetChannelPreemptionConfig(handle->edmaTxDataToIntermediaryHandle->base,
  760. handle->edmaTxDataToIntermediaryHandle->channel, &preemption_config_t);
  761. preemption_config_t.channelPriority = channelPriorityHigh;
  762. EDMA_SetChannelPreemptionConfig(handle->edmaIntermediaryToTxRegHandle->base,
  763. handle->edmaIntermediaryToTxRegHandle->channel, &preemption_config_t);
  764. }
  765. else
  766. {
  767. EDMA_SetChannelPreemptionConfig(handle->edmaIntermediaryToTxRegHandle->base,
  768. handle->edmaIntermediaryToTxRegHandle->channel, &preemption_config_t);
  769. preemption_config_t.channelPriority = channelPriorityMid;
  770. EDMA_SetChannelPreemptionConfig(handle->edmaTxDataToIntermediaryHandle->base,
  771. handle->edmaTxDataToIntermediaryHandle->channel, &preemption_config_t);
  772. preemption_config_t.channelPriority = channelPriorityHigh;
  773. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  774. &preemption_config_t);
  775. }
  776. /*Set the channel link.*/
  777. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  778. {
  779. /*if there is Tx DMA request , carry the 32bits data (handle->command) to PUSHR first , then link to channelB
  780. to prepare the next 32bits data (txData to handle->command) */
  781. if (handle->remainingSendByteCount > 1)
  782. {
  783. EDMA_SetChannelLink(handle->edmaIntermediaryToTxRegHandle->base,
  784. handle->edmaIntermediaryToTxRegHandle->channel, kEDMA_MajorLink,
  785. handle->edmaTxDataToIntermediaryHandle->channel);
  786. }
  787. DSPI_EnableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  788. }
  789. else
  790. {
  791. if (handle->remainingSendByteCount > 0)
  792. {
  793. EDMA_SetChannelLink(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  794. kEDMA_MinorLink, handle->edmaTxDataToIntermediaryHandle->channel);
  795. EDMA_SetChannelLink(handle->edmaTxDataToIntermediaryHandle->base,
  796. handle->edmaTxDataToIntermediaryHandle->channel, kEDMA_MinorLink,
  797. handle->edmaIntermediaryToTxRegHandle->channel);
  798. }
  799. DSPI_EnableDMA(base, kDSPI_RxDmaEnable);
  800. }
  801. #endif
  802. DSPI_StartTransfer(base);
  803. return kStatus_Success;
  804. }
  805. status_t DSPI_MasterHalfDuplexTransferEDMA(SPI_Type *base,
  806. dspi_master_edma_handle_t *handle,
  807. dspi_half_duplex_transfer_t *xfer)
  808. {
  809. assert(xfer);
  810. assert(handle);
  811. dspi_transfer_t tempXfer = {0};
  812. status_t status;
  813. if (xfer->isTransmitFirst)
  814. {
  815. tempXfer.txData = xfer->txData;
  816. tempXfer.rxData = NULL;
  817. tempXfer.dataSize = xfer->txDataSize;
  818. }
  819. else
  820. {
  821. tempXfer.txData = NULL;
  822. tempXfer.rxData = xfer->rxData;
  823. tempXfer.dataSize = xfer->rxDataSize;
  824. }
  825. /* If the pcs pin keep assert between transmit and receive. */
  826. if (xfer->isPcsAssertInTransfer)
  827. {
  828. tempXfer.configFlags = (xfer->configFlags) | kDSPI_MasterActiveAfterTransfer;
  829. }
  830. else
  831. {
  832. tempXfer.configFlags = (xfer->configFlags) & (uint32_t)(~kDSPI_MasterActiveAfterTransfer);
  833. }
  834. status = DSPI_MasterTransferBlocking(base, &tempXfer);
  835. if (status != kStatus_Success)
  836. {
  837. return status;
  838. }
  839. if (xfer->isTransmitFirst)
  840. {
  841. tempXfer.txData = NULL;
  842. tempXfer.rxData = xfer->rxData;
  843. tempXfer.dataSize = xfer->rxDataSize;
  844. }
  845. else
  846. {
  847. tempXfer.txData = xfer->txData;
  848. tempXfer.rxData = NULL;
  849. tempXfer.dataSize = xfer->txDataSize;
  850. }
  851. tempXfer.configFlags = xfer->configFlags;
  852. status = DSPI_MasterTransferEDMA(base, handle, &tempXfer);
  853. return status;
  854. }
  855. static void EDMA_DspiMasterCallback(edma_handle_t *edmaHandle,
  856. void *g_dspiEdmaPrivateHandle,
  857. bool transferDone,
  858. uint32_t tcds)
  859. {
  860. assert(edmaHandle);
  861. assert(g_dspiEdmaPrivateHandle);
  862. dspi_master_edma_private_handle_t *dspiEdmaPrivateHandle;
  863. dspiEdmaPrivateHandle = (dspi_master_edma_private_handle_t *)g_dspiEdmaPrivateHandle;
  864. DSPI_DisableDMA((dspiEdmaPrivateHandle->base), kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  865. dspiEdmaPrivateHandle->handle->state = kDSPI_Idle;
  866. if (dspiEdmaPrivateHandle->handle->callback)
  867. {
  868. dspiEdmaPrivateHandle->handle->callback(dspiEdmaPrivateHandle->base, dspiEdmaPrivateHandle->handle,
  869. kStatus_Success, dspiEdmaPrivateHandle->handle->userData);
  870. }
  871. }
  872. void DSPI_MasterTransferAbortEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle)
  873. {
  874. assert(handle);
  875. DSPI_StopTransfer(base);
  876. DSPI_DisableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  877. EDMA_AbortTransfer(handle->edmaRxRegToRxDataHandle);
  878. EDMA_AbortTransfer(handle->edmaTxDataToIntermediaryHandle);
  879. EDMA_AbortTransfer(handle->edmaIntermediaryToTxRegHandle);
  880. handle->state = kDSPI_Idle;
  881. }
  882. status_t DSPI_MasterTransferGetCountEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle, size_t *count)
  883. {
  884. assert(handle);
  885. if (!count)
  886. {
  887. return kStatus_InvalidArgument;
  888. }
  889. /* Catch when there is not an active transfer. */
  890. if (handle->state != kDSPI_Busy)
  891. {
  892. *count = 0;
  893. return kStatus_NoTransferInProgress;
  894. }
  895. size_t bytes;
  896. bytes = (uint32_t)handle->nbytes * EDMA_GetRemainingMajorLoopCount(handle->edmaRxRegToRxDataHandle->base,
  897. handle->edmaRxRegToRxDataHandle->channel);
  898. *count = handle->totalByteCount - bytes;
  899. return kStatus_Success;
  900. }
  901. void DSPI_SlaveTransferCreateHandleEDMA(SPI_Type *base,
  902. dspi_slave_edma_handle_t *handle,
  903. dspi_slave_edma_transfer_callback_t callback,
  904. void *userData,
  905. edma_handle_t *edmaRxRegToRxDataHandle,
  906. edma_handle_t *edmaTxDataToTxRegHandle)
  907. {
  908. assert(handle);
  909. assert(edmaRxRegToRxDataHandle);
  910. assert(edmaTxDataToTxRegHandle);
  911. /* Zero the handle. */
  912. memset(handle, 0, sizeof(*handle));
  913. uint32_t instance = DSPI_GetInstance(base);
  914. s_dspiSlaveEdmaPrivateHandle[instance].base = base;
  915. s_dspiSlaveEdmaPrivateHandle[instance].handle = handle;
  916. handle->callback = callback;
  917. handle->userData = userData;
  918. handle->edmaRxRegToRxDataHandle = edmaRxRegToRxDataHandle;
  919. handle->edmaTxDataToTxRegHandle = edmaTxDataToTxRegHandle;
  920. }
  921. status_t DSPI_SlaveTransferEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle, dspi_transfer_t *transfer)
  922. {
  923. assert(handle);
  924. assert(transfer);
  925. /* If send/receive length is zero */
  926. if (transfer->dataSize == 0)
  927. {
  928. return kStatus_InvalidArgument;
  929. }
  930. /* If both send buffer and receive buffer is null */
  931. if ((!(transfer->txData)) && (!(transfer->rxData)))
  932. {
  933. return kStatus_InvalidArgument;
  934. }
  935. /* Check that we're not busy.*/
  936. if (handle->state == kDSPI_Busy)
  937. {
  938. return kStatus_DSPI_Busy;
  939. }
  940. handle->state = kDSPI_Busy;
  941. uint32_t instance = DSPI_GetInstance(base);
  942. uint8_t whichCtar = (transfer->configFlags & DSPI_SLAVE_CTAR_MASK) >> DSPI_SLAVE_CTAR_SHIFT;
  943. handle->bitsPerFrame =
  944. (((base->CTAR_SLAVE[whichCtar]) & SPI_CTAR_SLAVE_FMSZ_MASK) >> SPI_CTAR_SLAVE_FMSZ_SHIFT) + 1;
  945. /* If using a shared RX/TX DMA request, then this limits the amount of data we can transfer
  946. * due to the linked channel. The max bytes is 511 if 8-bit/frame or 1022 if 16-bit/frame
  947. */
  948. uint32_t limited_size = 0;
  949. if (1 == FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  950. {
  951. limited_size = 32767u;
  952. }
  953. else
  954. {
  955. limited_size = 511u;
  956. }
  957. if (handle->bitsPerFrame > 8)
  958. {
  959. if (transfer->dataSize > (limited_size << 1u))
  960. {
  961. handle->state = kDSPI_Idle;
  962. return kStatus_DSPI_OutOfRange;
  963. }
  964. }
  965. else
  966. {
  967. if (transfer->dataSize > limited_size)
  968. {
  969. handle->state = kDSPI_Idle;
  970. return kStatus_DSPI_OutOfRange;
  971. }
  972. }
  973. /*The data size should be even if the bitsPerFrame is greater than 8 (that is 2 bytes per frame in dspi) */
  974. if ((handle->bitsPerFrame > 8) && (transfer->dataSize & 0x1))
  975. {
  976. handle->state = kDSPI_Idle;
  977. return kStatus_InvalidArgument;
  978. }
  979. EDMA_SetCallback(handle->edmaRxRegToRxDataHandle, EDMA_DspiSlaveCallback, &s_dspiSlaveEdmaPrivateHandle[instance]);
  980. /* Store transfer information */
  981. handle->txData = transfer->txData;
  982. handle->rxData = transfer->rxData;
  983. handle->remainingSendByteCount = transfer->dataSize;
  984. handle->remainingReceiveByteCount = transfer->dataSize;
  985. handle->totalByteCount = transfer->dataSize;
  986. uint16_t wordToSend = 0;
  987. uint8_t dummyData = g_dspiDummyData[DSPI_GetInstance(base)];
  988. uint8_t dataAlreadyFed = 0;
  989. uint8_t dataFedMax = 2;
  990. uint32_t rxAddr = DSPI_GetRxRegisterAddress(base);
  991. uint32_t txAddr = DSPI_SlaveGetTxRegisterAddress(base);
  992. edma_transfer_config_t transferConfigA;
  993. edma_transfer_config_t transferConfigC;
  994. DSPI_StopTransfer(base);
  995. DSPI_FlushFifo(base, true, true);
  996. DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
  997. DSPI_DisableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  998. DSPI_StartTransfer(base);
  999. /*if dspi has separate dma request , need not prepare data first .
  1000. else (dspi has shared dma request) , send first 2 data into fifo if there is fifo or send first 1 data to
  1001. slaveGetTxRegister if there is no fifo*/
  1002. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  1003. {
  1004. /* For DSPI instances with shared RX/TX DMA requests, we'll use the RX DMA request to
  1005. * trigger ongoing transfers and will link to the TX DMA channel from the RX DMA channel.
  1006. */
  1007. /* If bits/frame is greater than one byte */
  1008. if (handle->bitsPerFrame > 8)
  1009. {
  1010. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  1011. {
  1012. if (handle->txData)
  1013. {
  1014. wordToSend = *(handle->txData);
  1015. ++handle->txData; /* Increment to next data byte */
  1016. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  1017. ++handle->txData; /* Increment to next data byte */
  1018. }
  1019. else
  1020. {
  1021. wordToSend = ((uint32_t)dummyData << 8) | dummyData;
  1022. }
  1023. handle->remainingSendByteCount -= 2; /* decrement remainingSendByteCount by 2 */
  1024. base->PUSHR_SLAVE = wordToSend;
  1025. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  1026. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  1027. dataAlreadyFed += 2;
  1028. /* Exit loop if send count is zero, else update local variables for next loop */
  1029. if ((handle->remainingSendByteCount == 0) || (dataAlreadyFed == (dataFedMax * 2)))
  1030. {
  1031. break;
  1032. }
  1033. } /* End of TX FIFO fill while loop */
  1034. }
  1035. else /* Optimized for bits/frame less than or equal to one byte. */
  1036. {
  1037. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  1038. {
  1039. if (handle->txData)
  1040. {
  1041. wordToSend = *(handle->txData);
  1042. /* Increment to next data word*/
  1043. ++handle->txData;
  1044. }
  1045. else
  1046. {
  1047. wordToSend = dummyData;
  1048. }
  1049. base->PUSHR_SLAVE = wordToSend;
  1050. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  1051. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  1052. /* Decrement remainingSendByteCount*/
  1053. --handle->remainingSendByteCount;
  1054. dataAlreadyFed++;
  1055. /* Exit loop if send count is zero, else update local variables for next loop */
  1056. if ((handle->remainingSendByteCount == 0) || (dataAlreadyFed == dataFedMax))
  1057. {
  1058. break;
  1059. }
  1060. } /* End of TX FIFO fill while loop */
  1061. }
  1062. }
  1063. /***channel_A *** used for carry the data from Rx_Data_Register(POPR) to User_Receive_Buffer*/
  1064. if (handle->remainingReceiveByteCount > 0)
  1065. {
  1066. EDMA_ResetChannel(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel);
  1067. transferConfigA.srcAddr = (uint32_t)rxAddr;
  1068. transferConfigA.srcOffset = 0;
  1069. if (handle->rxData)
  1070. {
  1071. transferConfigA.destAddr = (uint32_t) & (handle->rxData[0]);
  1072. transferConfigA.destOffset = 1;
  1073. }
  1074. else
  1075. {
  1076. transferConfigA.destAddr = (uint32_t) & (handle->rxBuffIfNull);
  1077. transferConfigA.destOffset = 0;
  1078. }
  1079. transferConfigA.destTransferSize = kEDMA_TransferSize1Bytes;
  1080. if (handle->bitsPerFrame <= 8)
  1081. {
  1082. transferConfigA.srcTransferSize = kEDMA_TransferSize1Bytes;
  1083. transferConfigA.minorLoopBytes = 1;
  1084. transferConfigA.majorLoopCounts = handle->remainingReceiveByteCount;
  1085. }
  1086. else
  1087. {
  1088. transferConfigA.srcTransferSize = kEDMA_TransferSize2Bytes;
  1089. transferConfigA.minorLoopBytes = 2;
  1090. transferConfigA.majorLoopCounts = handle->remainingReceiveByteCount / 2;
  1091. }
  1092. /* Store the initially configured eDMA minor byte transfer count into the DSPI handle */
  1093. handle->nbytes = transferConfigA.minorLoopBytes;
  1094. EDMA_SetTransferConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  1095. &transferConfigA, NULL);
  1096. EDMA_EnableChannelInterrupts(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  1097. kEDMA_MajorInterruptEnable);
  1098. }
  1099. if (handle->remainingSendByteCount > 0)
  1100. {
  1101. /***channel_C *** used for carry the data from User_Send_Buffer to Tx_Data_Register(PUSHR_SLAVE)*/
  1102. EDMA_ResetChannel(handle->edmaTxDataToTxRegHandle->base, handle->edmaTxDataToTxRegHandle->channel);
  1103. transferConfigC.destAddr = (uint32_t)txAddr;
  1104. transferConfigC.destOffset = 0;
  1105. if (handle->txData)
  1106. {
  1107. transferConfigC.srcAddr = (uint32_t)(&(handle->txData[0]));
  1108. transferConfigC.srcOffset = 1;
  1109. }
  1110. else
  1111. {
  1112. transferConfigC.srcAddr = (uint32_t)(&handle->txBuffIfNull);
  1113. transferConfigC.srcOffset = 0;
  1114. if (handle->bitsPerFrame <= 8)
  1115. {
  1116. handle->txBuffIfNull = dummyData;
  1117. }
  1118. else
  1119. {
  1120. handle->txBuffIfNull = ((uint32_t)dummyData << 8) | dummyData;
  1121. }
  1122. }
  1123. transferConfigC.srcTransferSize = kEDMA_TransferSize1Bytes;
  1124. if (handle->bitsPerFrame <= 8)
  1125. {
  1126. transferConfigC.destTransferSize = kEDMA_TransferSize1Bytes;
  1127. transferConfigC.minorLoopBytes = 1;
  1128. transferConfigC.majorLoopCounts = handle->remainingSendByteCount;
  1129. }
  1130. else
  1131. {
  1132. transferConfigC.destTransferSize = kEDMA_TransferSize2Bytes;
  1133. transferConfigC.minorLoopBytes = 2;
  1134. transferConfigC.majorLoopCounts = handle->remainingSendByteCount / 2;
  1135. }
  1136. EDMA_SetTransferConfig(handle->edmaTxDataToTxRegHandle->base, handle->edmaTxDataToTxRegHandle->channel,
  1137. &transferConfigC, NULL);
  1138. EDMA_StartTransfer(handle->edmaTxDataToTxRegHandle);
  1139. }
  1140. EDMA_StartTransfer(handle->edmaRxRegToRxDataHandle);
  1141. /*Set channel priority*/
  1142. uint8_t channelPriorityLow = handle->edmaRxRegToRxDataHandle->channel;
  1143. uint8_t channelPriorityHigh = handle->edmaTxDataToTxRegHandle->channel;
  1144. uint8_t t = 0;
  1145. if (channelPriorityLow > channelPriorityHigh)
  1146. {
  1147. t = channelPriorityLow;
  1148. channelPriorityLow = channelPriorityHigh;
  1149. channelPriorityHigh = t;
  1150. }
  1151. edma_channel_Preemption_config_t preemption_config_t;
  1152. preemption_config_t.enableChannelPreemption = true;
  1153. preemption_config_t.enablePreemptAbility = true;
  1154. preemption_config_t.channelPriority = channelPriorityLow;
  1155. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  1156. {
  1157. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  1158. &preemption_config_t);
  1159. preemption_config_t.channelPriority = channelPriorityHigh;
  1160. EDMA_SetChannelPreemptionConfig(handle->edmaTxDataToTxRegHandle->base, handle->edmaTxDataToTxRegHandle->channel,
  1161. &preemption_config_t);
  1162. }
  1163. else
  1164. {
  1165. EDMA_SetChannelPreemptionConfig(handle->edmaTxDataToTxRegHandle->base, handle->edmaTxDataToTxRegHandle->channel,
  1166. &preemption_config_t);
  1167. preemption_config_t.channelPriority = channelPriorityHigh;
  1168. EDMA_SetChannelPreemptionConfig(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  1169. &preemption_config_t);
  1170. }
  1171. /*Set the channel link.
  1172. For DSPI instances with shared RX/TX DMA requests: Rx DMA request -> channel_A -> channel_C.
  1173. For DSPI instances with separate RX and TX DMA requests:
  1174. Rx DMA request -> channel_A
  1175. Tx DMA request -> channel_C */
  1176. if (1 != FSL_FEATURE_DSPI_HAS_SEPARATE_DMA_RX_TX_REQn(base))
  1177. {
  1178. if (handle->remainingSendByteCount > 0)
  1179. {
  1180. EDMA_SetChannelLink(handle->edmaRxRegToRxDataHandle->base, handle->edmaRxRegToRxDataHandle->channel,
  1181. kEDMA_MinorLink, handle->edmaTxDataToTxRegHandle->channel);
  1182. }
  1183. DSPI_EnableDMA(base, kDSPI_RxDmaEnable);
  1184. }
  1185. else
  1186. {
  1187. DSPI_EnableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  1188. }
  1189. return kStatus_Success;
  1190. }
  1191. static void EDMA_DspiSlaveCallback(edma_handle_t *edmaHandle,
  1192. void *g_dspiEdmaPrivateHandle,
  1193. bool transferDone,
  1194. uint32_t tcds)
  1195. {
  1196. assert(edmaHandle);
  1197. assert(g_dspiEdmaPrivateHandle);
  1198. dspi_slave_edma_private_handle_t *dspiEdmaPrivateHandle;
  1199. dspiEdmaPrivateHandle = (dspi_slave_edma_private_handle_t *)g_dspiEdmaPrivateHandle;
  1200. DSPI_DisableDMA((dspiEdmaPrivateHandle->base), kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  1201. dspiEdmaPrivateHandle->handle->state = kDSPI_Idle;
  1202. if (dspiEdmaPrivateHandle->handle->callback)
  1203. {
  1204. dspiEdmaPrivateHandle->handle->callback(dspiEdmaPrivateHandle->base, dspiEdmaPrivateHandle->handle,
  1205. kStatus_Success, dspiEdmaPrivateHandle->handle->userData);
  1206. }
  1207. }
  1208. void DSPI_SlaveTransferAbortEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle)
  1209. {
  1210. assert(handle);
  1211. DSPI_StopTransfer(base);
  1212. DSPI_DisableDMA(base, kDSPI_RxDmaEnable | kDSPI_TxDmaEnable);
  1213. EDMA_AbortTransfer(handle->edmaRxRegToRxDataHandle);
  1214. EDMA_AbortTransfer(handle->edmaTxDataToTxRegHandle);
  1215. handle->state = kDSPI_Idle;
  1216. }
  1217. status_t DSPI_SlaveTransferGetCountEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle, size_t *count)
  1218. {
  1219. assert(handle);
  1220. if (!count)
  1221. {
  1222. return kStatus_InvalidArgument;
  1223. }
  1224. /* Catch when there is not an active transfer. */
  1225. if (handle->state != kDSPI_Busy)
  1226. {
  1227. *count = 0;
  1228. return kStatus_NoTransferInProgress;
  1229. }
  1230. size_t bytes;
  1231. bytes = (uint32_t)handle->nbytes * EDMA_GetRemainingMajorLoopCount(handle->edmaRxRegToRxDataHandle->base,
  1232. handle->edmaRxRegToRxDataHandle->channel);
  1233. *count = handle->totalByteCount - bytes;
  1234. return kStatus_Success;
  1235. }