fsl_spi_dma.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, 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_spi_dma.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.flexcomm_spi_dma"
  41. #endif
  42. /*<! Structure definition for spi_dma_private_handle_t. The structure is private. */
  43. typedef struct _spi_dma_private_handle
  44. {
  45. SPI_Type *base;
  46. spi_dma_handle_t *handle;
  47. } spi_dma_private_handle_t;
  48. /*! @brief SPI transfer state, which is used for SPI transactiaonl APIs' internal state. */
  49. enum _spi_dma_states_t
  50. {
  51. kSPI_Idle = 0x0, /*!< SPI is idle state */
  52. kSPI_Busy /*!< SPI is busy tranferring data. */
  53. };
  54. typedef struct _spi_dma_txdummy
  55. {
  56. uint32_t lastWord;
  57. uint32_t word;
  58. } spi_dma_txdummy_t;
  59. /*<! Private handle only used for internally. */
  60. static spi_dma_private_handle_t s_dmaPrivateHandle[FSL_FEATURE_SOC_SPI_COUNT];
  61. /*******************************************************************************
  62. * Prototypes
  63. ******************************************************************************/
  64. /*!
  65. * @brief DMA callback function for SPI send transfer.
  66. *
  67. * @param handle DMA handle pointer.
  68. * @param userData User data for DMA callback function.
  69. */
  70. static void SPI_TxDMACallback(dma_handle_t *handle, void *userData, bool transferDone, uint32_t intmode);
  71. /*!
  72. * @brief DMA callback function for SPI receive transfer.
  73. *
  74. * @param handle DMA handle pointer.
  75. * @param userData User data for DMA callback function.
  76. */
  77. static void SPI_RxDMACallback(dma_handle_t *handle, void *userData, bool transferDone, uint32_t intmode);
  78. /*******************************************************************************
  79. * Variables
  80. ******************************************************************************/
  81. #if defined(__ICCARM__)
  82. #pragma data_alignment = 4
  83. static spi_dma_txdummy_t s_txDummy[FSL_FEATURE_SOC_SPI_COUNT] = {0};
  84. #elif defined(__CC_ARM)
  85. __attribute__((aligned(4))) static spi_dma_txdummy_t s_txDummy[FSL_FEATURE_SOC_SPI_COUNT] = {0};
  86. #elif defined(__GNUC__)
  87. __attribute__((aligned(4))) static spi_dma_txdummy_t s_txDummy[FSL_FEATURE_SOC_SPI_COUNT] = {0};
  88. #endif
  89. #if defined(__ICCARM__)
  90. #pragma data_alignment = 4
  91. static uint16_t s_rxDummy;
  92. static uint32_t s_txLastWord[FSL_FEATURE_SOC_SPI_COUNT];
  93. #elif defined(__CC_ARM)
  94. __attribute__((aligned(4))) static uint16_t s_rxDummy;
  95. __attribute__((aligned(4))) static uint32_t s_txLastWord[FSL_FEATURE_SOC_SPI_COUNT];
  96. #elif defined(__GNUC__)
  97. __attribute__((aligned(4))) static uint16_t s_rxDummy;
  98. __attribute__((aligned(4))) static uint32_t s_txLastWord[FSL_FEATURE_SOC_SPI_COUNT];
  99. #endif
  100. #if defined(__ICCARM__)
  101. #pragma data_alignment = 16
  102. static dma_descriptor_t s_spi_descriptor_table[FSL_FEATURE_SOC_SPI_COUNT] = {0};
  103. #elif defined(__CC_ARM)
  104. __attribute__((aligned(16))) static dma_descriptor_t s_spi_descriptor_table[FSL_FEATURE_SOC_SPI_COUNT] = {0};
  105. #elif defined(__GNUC__)
  106. __attribute__((aligned(16))) static dma_descriptor_t s_spi_descriptor_table[FSL_FEATURE_SOC_SPI_COUNT] = {0};
  107. #endif
  108. /*******************************************************************************
  109. * Code
  110. ******************************************************************************/
  111. static void XferToFifoWR(spi_transfer_t *xfer, uint32_t *fifowr)
  112. {
  113. *fifowr |= xfer->configFlags & (uint32_t)kSPI_FrameDelay ? (uint32_t)kSPI_FrameDelay : 0;
  114. *fifowr |= xfer->configFlags & (uint32_t)kSPI_FrameAssert ? (uint32_t)kSPI_FrameAssert : 0;
  115. }
  116. static void SpiConfigToFifoWR(spi_config_t *config, uint32_t *fifowr)
  117. {
  118. *fifowr |= (SPI_DEASSERT_ALL & (~SPI_DEASSERTNUM_SSEL(config->sselNum)));
  119. /* set width of data - range asserted at entry */
  120. *fifowr |= SPI_FIFOWR_LEN(config->dataWidth);
  121. }
  122. static void PrepareTxLastWord(spi_transfer_t *xfer, uint32_t *txLastWord, spi_config_t *config)
  123. {
  124. if (config->dataWidth > kSPI_Data8Bits)
  125. {
  126. *txLastWord = (((uint32_t)xfer->txData[xfer->dataSize - 1] << 8U) | (xfer->txData[xfer->dataSize - 2]));
  127. }
  128. else
  129. {
  130. *txLastWord = xfer->txData[xfer->dataSize - 1];
  131. }
  132. XferToFifoWR(xfer, txLastWord);
  133. SpiConfigToFifoWR(config, txLastWord);
  134. }
  135. static void SPI_SetupDummy(SPI_Type *base, spi_dma_txdummy_t *dummy, spi_transfer_t *xfer, spi_config_t *spi_config_p)
  136. {
  137. uint32_t instance = SPI_GetInstance(base);
  138. dummy->word = ((uint32_t)s_dummyData[instance] << 8U | s_dummyData[instance]);
  139. dummy->lastWord = ((uint32_t)s_dummyData[instance] << 8U | s_dummyData[instance]);
  140. XferToFifoWR(xfer, &dummy->word);
  141. XferToFifoWR(xfer, &dummy->lastWord);
  142. SpiConfigToFifoWR(spi_config_p, &dummy->word);
  143. SpiConfigToFifoWR(spi_config_p, &dummy->lastWord);
  144. /* Clear the end of transfer bit for continue word transfer. */
  145. dummy->word &= (uint32_t)(~kSPI_FrameAssert);
  146. }
  147. status_t SPI_MasterTransferCreateHandleDMA(SPI_Type *base,
  148. spi_dma_handle_t *handle,
  149. spi_dma_callback_t callback,
  150. void *userData,
  151. dma_handle_t *txHandle,
  152. dma_handle_t *rxHandle)
  153. {
  154. int32_t instance = 0;
  155. /* check 'base' */
  156. assert(!(NULL == base));
  157. if (NULL == base)
  158. {
  159. return kStatus_InvalidArgument;
  160. }
  161. /* check 'handle' */
  162. assert(!(NULL == handle));
  163. if (NULL == handle)
  164. {
  165. return kStatus_InvalidArgument;
  166. }
  167. instance = SPI_GetInstance(base);
  168. memset(handle, 0, sizeof(*handle));
  169. /* Set spi base to handle */
  170. handle->txHandle = txHandle;
  171. handle->rxHandle = rxHandle;
  172. handle->callback = callback;
  173. handle->userData = userData;
  174. /* Set SPI state to idle */
  175. handle->state = kSPI_Idle;
  176. /* Set handle to global state */
  177. s_dmaPrivateHandle[instance].base = base;
  178. s_dmaPrivateHandle[instance].handle = handle;
  179. /* Install callback for Tx dma channel */
  180. DMA_SetCallback(handle->txHandle, SPI_TxDMACallback, &s_dmaPrivateHandle[instance]);
  181. DMA_SetCallback(handle->rxHandle, SPI_RxDMACallback, &s_dmaPrivateHandle[instance]);
  182. return kStatus_Success;
  183. }
  184. status_t SPI_MasterTransferDMA(SPI_Type *base, spi_dma_handle_t *handle, spi_transfer_t *xfer)
  185. {
  186. int32_t instance;
  187. status_t result = kStatus_Success;
  188. spi_config_t *spi_config_p;
  189. assert(!((NULL == handle) || (NULL == xfer)));
  190. if ((NULL == handle) || (NULL == xfer))
  191. {
  192. return kStatus_InvalidArgument;
  193. }
  194. /* Byte size is zero. */
  195. assert(!(xfer->dataSize == 0));
  196. if (xfer->dataSize == 0)
  197. {
  198. return kStatus_InvalidArgument;
  199. }
  200. /* cannot get instance from base address */
  201. instance = SPI_GetInstance(base);
  202. assert(!(instance < 0));
  203. if (instance < 0)
  204. {
  205. return kStatus_InvalidArgument;
  206. }
  207. /* Check if the device is busy */
  208. if (handle->state == kSPI_Busy)
  209. {
  210. return kStatus_SPI_Busy;
  211. }
  212. else
  213. {
  214. uint32_t tmp;
  215. dma_transfer_config_t xferConfig = {0};
  216. spi_config_p = (spi_config_t *)SPI_GetConfig(base);
  217. handle->state = kStatus_SPI_Busy;
  218. handle->transferSize = xfer->dataSize;
  219. /* receive */
  220. SPI_EnableRxDMA(base, true);
  221. if (xfer->rxData)
  222. {
  223. DMA_PrepareTransfer(&xferConfig, ((void *)((uint32_t)&base->FIFORD)), xfer->rxData,
  224. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (sizeof(uint16_t)) : (sizeof(uint8_t))),
  225. xfer->dataSize, kDMA_PeripheralToMemory, NULL);
  226. }
  227. else
  228. {
  229. DMA_PrepareTransfer(&xferConfig, ((void *)((uint32_t)&base->FIFORD)), &s_rxDummy,
  230. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (sizeof(uint16_t)) : (sizeof(uint8_t))),
  231. xfer->dataSize, kDMA_StaticToStatic, NULL);
  232. }
  233. DMA_SubmitTransfer(handle->rxHandle, &xferConfig);
  234. handle->rxInProgress = true;
  235. DMA_StartTransfer(handle->rxHandle);
  236. /* transmit */
  237. SPI_EnableTxDMA(base, true);
  238. if (xfer->configFlags & kSPI_FrameAssert)
  239. {
  240. PrepareTxLastWord(xfer, &s_txLastWord[instance], spi_config_p);
  241. }
  242. if (xfer->txData)
  243. {
  244. /* If end of tranfer function is enabled and data transfer frame is bigger then 1, use dma
  245. * descriptor to send the last data.
  246. */
  247. if ((xfer->configFlags & kSPI_FrameAssert) &&
  248. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (xfer->dataSize > 2) : (xfer->dataSize > 1)))
  249. {
  250. dma_xfercfg_t tmp_xfercfg = {0};
  251. tmp_xfercfg.valid = true;
  252. tmp_xfercfg.swtrig = true;
  253. tmp_xfercfg.intA = true;
  254. tmp_xfercfg.byteWidth = sizeof(uint32_t);
  255. tmp_xfercfg.srcInc = 0;
  256. tmp_xfercfg.dstInc = 0;
  257. tmp_xfercfg.transferCount = 1;
  258. /* Create chained descriptor to transmit last word */
  259. DMA_CreateDescriptor(&s_spi_descriptor_table[instance], &tmp_xfercfg, &s_txLastWord[instance],
  260. ((void *)((uint32_t)&base->FIFOWR)), NULL);
  261. DMA_PrepareTransfer(
  262. &xferConfig, xfer->txData, ((void *)((uint32_t)&base->FIFOWR)),
  263. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (sizeof(uint16_t)) : (sizeof(uint8_t))),
  264. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (xfer->dataSize - 2) : (xfer->dataSize - 1)),
  265. kDMA_MemoryToPeripheral, &s_spi_descriptor_table[instance]);
  266. /* Disable interrupts for first descriptor to avoid calling callback twice. */
  267. xferConfig.xfercfg.intA = false;
  268. xferConfig.xfercfg.intB = false;
  269. result = DMA_SubmitTransfer(handle->txHandle, &xferConfig);
  270. if (result != kStatus_Success)
  271. {
  272. return result;
  273. }
  274. }
  275. else
  276. {
  277. DMA_PrepareTransfer(
  278. &xferConfig, xfer->txData, ((void *)((uint32_t)&base->FIFOWR)),
  279. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (sizeof(uint16_t)) : (sizeof(uint8_t))),
  280. xfer->dataSize, kDMA_MemoryToPeripheral, NULL);
  281. DMA_SubmitTransfer(handle->txHandle, &xferConfig);
  282. }
  283. }
  284. else
  285. {
  286. /* Setup tx dummy data. */
  287. SPI_SetupDummy(base, &s_txDummy[instance], xfer, spi_config_p);
  288. if ((xfer->configFlags & kSPI_FrameAssert) &&
  289. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (xfer->dataSize > 2) : (xfer->dataSize > 1)))
  290. {
  291. dma_xfercfg_t tmp_xfercfg = {0};
  292. tmp_xfercfg.valid = true;
  293. tmp_xfercfg.swtrig = true;
  294. tmp_xfercfg.intA = true;
  295. tmp_xfercfg.byteWidth = sizeof(uint32_t);
  296. tmp_xfercfg.srcInc = 0;
  297. tmp_xfercfg.dstInc = 0;
  298. tmp_xfercfg.transferCount = 1;
  299. /* Create chained descriptor to transmit last word */
  300. DMA_CreateDescriptor(&s_spi_descriptor_table[instance], &tmp_xfercfg, &s_txDummy[instance].lastWord,
  301. (void *)((uint32_t)&base->FIFOWR), NULL);
  302. /* Use common API to setup first descriptor */
  303. DMA_PrepareTransfer(
  304. &xferConfig, &s_txDummy[instance].word, ((void *)((uint32_t)&base->FIFOWR)),
  305. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (sizeof(uint16_t)) : (sizeof(uint8_t))),
  306. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (xfer->dataSize - 2) : (xfer->dataSize - 1)),
  307. kDMA_StaticToStatic, &s_spi_descriptor_table[instance]);
  308. /* Disable interrupts for first descriptor to avoid calling callback twice */
  309. xferConfig.xfercfg.intA = false;
  310. xferConfig.xfercfg.intB = false;
  311. result = DMA_SubmitTransfer(handle->txHandle, &xferConfig);
  312. if (result != kStatus_Success)
  313. {
  314. return result;
  315. }
  316. }
  317. else
  318. {
  319. DMA_PrepareTransfer(
  320. &xferConfig, &s_txDummy[instance].word, ((void *)((uint32_t)&base->FIFOWR)),
  321. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (sizeof(uint16_t)) : (sizeof(uint8_t))),
  322. xfer->dataSize, kDMA_StaticToStatic, NULL);
  323. result = DMA_SubmitTransfer(handle->txHandle, &xferConfig);
  324. if (result != kStatus_Success)
  325. {
  326. return result;
  327. }
  328. }
  329. }
  330. handle->txInProgress = true;
  331. tmp = 0;
  332. XferToFifoWR(xfer, &tmp);
  333. SpiConfigToFifoWR(spi_config_p, &tmp);
  334. /* Setup the control info.
  335. * Halfword writes to just the control bits (offset 0xE22) doesn't push anything into the FIFO.
  336. * And the data access type of control bits must be uint16_t, byte writes or halfword writes to FIFOWR
  337. * will push the data and the current control bits into the FIFO.
  338. */
  339. if ((xfer->configFlags & kSPI_FrameAssert) &&
  340. ((spi_config_p->dataWidth > kSPI_Data8Bits) ? (xfer->dataSize == 2U) : (xfer->dataSize == 1U)))
  341. {
  342. *(((uint16_t *)((uint32_t) & (base->FIFOWR))) + 1) = (uint16_t)(tmp >> 16U);
  343. }
  344. else
  345. {
  346. /* Clear the SPI_FIFOWR_EOT_MASK bit when data is not the last. */
  347. tmp &= (uint32_t)(~kSPI_FrameAssert);
  348. *(((uint16_t *)((uint32_t) & (base->FIFOWR))) + 1) = (uint16_t)(tmp >> 16U);
  349. }
  350. DMA_StartTransfer(handle->txHandle);
  351. }
  352. return result;
  353. }
  354. status_t SPI_MasterHalfDuplexTransferDMA(SPI_Type *base, spi_dma_handle_t *handle, spi_half_duplex_transfer_t *xfer)
  355. {
  356. assert(xfer);
  357. assert(handle);
  358. spi_transfer_t tempXfer = {0};
  359. status_t status;
  360. if (xfer->isTransmitFirst)
  361. {
  362. tempXfer.txData = xfer->txData;
  363. tempXfer.rxData = NULL;
  364. tempXfer.dataSize = xfer->txDataSize;
  365. }
  366. else
  367. {
  368. tempXfer.txData = NULL;
  369. tempXfer.rxData = xfer->rxData;
  370. tempXfer.dataSize = xfer->rxDataSize;
  371. }
  372. /* If the pcs pin keep assert between transmit and receive. */
  373. if (xfer->isPcsAssertInTransfer)
  374. {
  375. tempXfer.configFlags = (xfer->configFlags) & (uint32_t)(~kSPI_FrameAssert);
  376. }
  377. else
  378. {
  379. tempXfer.configFlags = (xfer->configFlags) | kSPI_FrameAssert;
  380. }
  381. status = SPI_MasterTransferBlocking(base, &tempXfer);
  382. if (status != kStatus_Success)
  383. {
  384. return status;
  385. }
  386. if (xfer->isTransmitFirst)
  387. {
  388. tempXfer.txData = NULL;
  389. tempXfer.rxData = xfer->rxData;
  390. tempXfer.dataSize = xfer->rxDataSize;
  391. }
  392. else
  393. {
  394. tempXfer.txData = xfer->txData;
  395. tempXfer.rxData = NULL;
  396. tempXfer.dataSize = xfer->txDataSize;
  397. }
  398. tempXfer.configFlags = xfer->configFlags;
  399. status = SPI_MasterTransferDMA(base, handle, &tempXfer);
  400. return status;
  401. }
  402. static void SPI_RxDMACallback(dma_handle_t *handle, void *userData, bool transferDone, uint32_t intmode)
  403. {
  404. spi_dma_private_handle_t *privHandle = (spi_dma_private_handle_t *)userData;
  405. spi_dma_handle_t *spiHandle = privHandle->handle;
  406. SPI_Type *base = privHandle->base;
  407. /* change the state */
  408. spiHandle->rxInProgress = false;
  409. /* All finished, call the callback */
  410. if ((spiHandle->txInProgress == false) && (spiHandle->rxInProgress == false))
  411. {
  412. spiHandle->state = kSPI_Idle;
  413. if (spiHandle->callback)
  414. {
  415. (spiHandle->callback)(base, spiHandle, kStatus_Success, spiHandle->userData);
  416. }
  417. }
  418. }
  419. static void SPI_TxDMACallback(dma_handle_t *handle, void *userData, bool transferDone, uint32_t intmode)
  420. {
  421. spi_dma_private_handle_t *privHandle = (spi_dma_private_handle_t *)userData;
  422. spi_dma_handle_t *spiHandle = privHandle->handle;
  423. SPI_Type *base = privHandle->base;
  424. /* change the state */
  425. spiHandle->txInProgress = false;
  426. /* All finished, call the callback */
  427. if ((spiHandle->txInProgress == false) && (spiHandle->rxInProgress == false))
  428. {
  429. spiHandle->state = kSPI_Idle;
  430. if (spiHandle->callback)
  431. {
  432. (spiHandle->callback)(base, spiHandle, kStatus_Success, spiHandle->userData);
  433. }
  434. }
  435. }
  436. void SPI_MasterTransferAbortDMA(SPI_Type *base, spi_dma_handle_t *handle)
  437. {
  438. assert(NULL != handle);
  439. /* Stop tx transfer first */
  440. DMA_AbortTransfer(handle->txHandle);
  441. /* Then rx transfer */
  442. DMA_AbortTransfer(handle->rxHandle);
  443. /* Set the handle state */
  444. handle->txInProgress = false;
  445. handle->rxInProgress = false;
  446. handle->state = kSPI_Idle;
  447. }
  448. status_t SPI_MasterTransferGetCountDMA(SPI_Type *base, spi_dma_handle_t *handle, size_t *count)
  449. {
  450. assert(handle);
  451. if (!count)
  452. {
  453. return kStatus_InvalidArgument;
  454. }
  455. /* Catch when there is not an active transfer. */
  456. if (handle->state != kSPI_Busy)
  457. {
  458. *count = 0;
  459. return kStatus_NoTransferInProgress;
  460. }
  461. size_t bytes;
  462. bytes = DMA_GetRemainingBytes(handle->rxHandle->base, handle->rxHandle->channel);
  463. *count = handle->transferSize - bytes;
  464. return kStatus_Success;
  465. }