fsl_spi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright (c) 2016, 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_spi.h"
  31. #include "fsl_flexcomm.h"
  32. /*******************************************************************************
  33. * Definitons
  34. ******************************************************************************/
  35. /* Note: FIFOCFG[SIZE] has always value 1 = 8 items depth */
  36. #define SPI_FIFO_DEPTH(base) ((((base)->FIFOCFG & SPI_FIFOCFG_SIZE_MASK) >> SPI_FIFOCFG_SIZE_SHIFT) << 3)
  37. /* Convert transfer count to transfer bytes. dataWidth is a
  38. * range <0,15>. Range <8,15> represents 2B transfer */
  39. #define SPI_COUNT_TO_BYTES(dataWidth, count) ((count) << ((dataWidth) >> 3U))
  40. #define SPI_BYTES_TO_COUNT(dataWidth, bytes) ((bytes) >> ((dataWidth) >> 3U))
  41. /*******************************************************************************
  42. * Variables
  43. ******************************************************************************/
  44. /*! @brief internal SPI config array */
  45. static spi_config_t g_configs[FSL_FEATURE_SOC_SPI_COUNT] = {(spi_data_width_t)0};
  46. /*! @brief Array to map SPI instance number to base address. */
  47. static const uint32_t s_spiBaseAddrs[FSL_FEATURE_SOC_SPI_COUNT] = SPI_BASE_ADDRS;
  48. /*! @brief IRQ name array */
  49. static const IRQn_Type s_spiIRQ[] = SPI_IRQS;
  50. /*******************************************************************************
  51. * Code
  52. ******************************************************************************/
  53. /* Get the index corresponding to the FLEXCOMM */
  54. uint32_t SPI_GetInstance(SPI_Type *base)
  55. {
  56. int i;
  57. for (i = 0; i < FSL_FEATURE_SOC_SPI_COUNT; i++)
  58. {
  59. if ((uint32_t)base == s_spiBaseAddrs[i])
  60. {
  61. return i;
  62. }
  63. }
  64. assert(false);
  65. return 0;
  66. }
  67. void *SPI_GetConfig(SPI_Type *base)
  68. {
  69. int32_t instance;
  70. instance = SPI_GetInstance(base);
  71. if (instance < 0)
  72. {
  73. return NULL;
  74. }
  75. return &g_configs[instance];
  76. }
  77. void SPI_MasterGetDefaultConfig(spi_master_config_t *config)
  78. {
  79. assert(NULL != config);
  80. config->enableLoopback = false;
  81. config->enableMaster = true;
  82. config->polarity = kSPI_ClockPolarityActiveHigh;
  83. config->phase = kSPI_ClockPhaseFirstEdge;
  84. config->direction = kSPI_MsbFirst;
  85. config->baudRate_Bps = 500000U;
  86. config->dataWidth = kSPI_Data8Bits;
  87. config->sselNum = kSPI_Ssel0;
  88. config->txWatermark = kSPI_TxFifo0;
  89. config->rxWatermark = kSPI_RxFifo1;
  90. }
  91. status_t SPI_MasterInit(SPI_Type *base, const spi_master_config_t *config, uint32_t srcClock_Hz)
  92. {
  93. int32_t result = 0, instance = 0;
  94. uint32_t tmp;
  95. /* assert params */
  96. assert(!((NULL == base) || (NULL == config) || (0 == srcClock_Hz)));
  97. if ((NULL == base) || (NULL == config) || (0 == srcClock_Hz))
  98. {
  99. return kStatus_InvalidArgument;
  100. }
  101. /* initialize flexcomm to SPI mode */
  102. result = FLEXCOMM_Init(base, FLEXCOMM_PERIPH_SPI);
  103. assert(kStatus_Success == result);
  104. if (kStatus_Success != result)
  105. {
  106. return result;
  107. }
  108. /* set divider */
  109. result = SPI_MasterSetBaud(base, config->baudRate_Bps, srcClock_Hz);
  110. if (kStatus_Success != result)
  111. {
  112. return result;
  113. }
  114. /* get instance number */
  115. instance = SPI_GetInstance(base);
  116. assert(instance >= 0);
  117. /* configure SPI mode */
  118. tmp = base->CFG;
  119. tmp &= ~(SPI_CFG_MASTER_MASK | SPI_CFG_LSBF_MASK | SPI_CFG_CPHA_MASK | SPI_CFG_CPOL_MASK | SPI_CFG_LOOP_MASK | SPI_CFG_ENABLE_MASK);
  120. /* phase */
  121. tmp |= SPI_CFG_CPHA(config->phase);
  122. /* polarity */
  123. tmp |= SPI_CFG_CPOL(config->polarity);
  124. /* direction */
  125. tmp |= SPI_CFG_LSBF(config->direction);
  126. /* master mode */
  127. tmp |= SPI_CFG_MASTER(1);
  128. /* loopback */
  129. tmp |= SPI_CFG_LOOP(config->enableLoopback);
  130. base->CFG = tmp;
  131. /* store configuration */
  132. g_configs[instance].dataWidth = config->dataWidth;
  133. g_configs[instance].sselNum = config->sselNum;
  134. /* enable FIFOs */
  135. base->FIFOCFG |= SPI_FIFOCFG_EMPTYTX_MASK | SPI_FIFOCFG_EMPTYRX_MASK;
  136. base->FIFOCFG |= SPI_FIFOCFG_ENABLETX_MASK | SPI_FIFOCFG_ENABLERX_MASK;
  137. /* trigger level - empty txFIFO, one item in rxFIFO */
  138. tmp = base->FIFOTRIG & (~(SPI_FIFOTRIG_RXLVL_MASK | SPI_FIFOTRIG_TXLVL_MASK));
  139. tmp |= SPI_FIFOTRIG_TXLVL(config->txWatermark) | SPI_FIFOTRIG_RXLVL(config->rxWatermark);
  140. /* enable generating interrupts for FIFOTRIG levels */
  141. tmp |= SPI_FIFOTRIG_TXLVLENA_MASK | SPI_FIFOTRIG_RXLVLENA_MASK;
  142. /* set FIFOTRIG */
  143. base->FIFOTRIG = tmp;
  144. SPI_Enable(base, config->enableMaster);
  145. return kStatus_Success;
  146. }
  147. void SPI_SlaveGetDefaultConfig(spi_slave_config_t *config)
  148. {
  149. assert(NULL != config);
  150. config->enableSlave = true;
  151. config->polarity = kSPI_ClockPolarityActiveHigh;
  152. config->phase = kSPI_ClockPhaseFirstEdge;
  153. config->direction = kSPI_MsbFirst;
  154. config->dataWidth = kSPI_Data8Bits;
  155. config->txWatermark = kSPI_TxFifo0;
  156. config->rxWatermark = kSPI_RxFifo1;
  157. }
  158. status_t SPI_SlaveInit(SPI_Type *base, const spi_slave_config_t *config)
  159. {
  160. int32_t result = 0, instance;
  161. uint32_t tmp;
  162. /* assert params */
  163. assert(!((NULL == base) || (NULL == config)));
  164. if ((NULL == base) || (NULL == config))
  165. {
  166. return kStatus_InvalidArgument;
  167. }
  168. /* configure flexcomm to SPI, enable clock gate */
  169. result = FLEXCOMM_Init(base, FLEXCOMM_PERIPH_SPI);
  170. assert(kStatus_Success == result);
  171. if (kStatus_Success != result)
  172. {
  173. return result;
  174. }
  175. instance = SPI_GetInstance(base);
  176. /* configure SPI mode */
  177. tmp = base->CFG;
  178. tmp &= ~(SPI_CFG_MASTER_MASK | SPI_CFG_LSBF_MASK | SPI_CFG_CPHA_MASK | SPI_CFG_CPOL_MASK | SPI_CFG_ENABLE_MASK);
  179. /* phase */
  180. tmp |= SPI_CFG_CPHA(config->phase);
  181. /* polarity */
  182. tmp |= SPI_CFG_CPOL(config->polarity);
  183. /* direction */
  184. tmp |= SPI_CFG_LSBF(config->direction);
  185. base->CFG = tmp;
  186. /* store configuration */
  187. g_configs[instance].dataWidth = config->dataWidth;
  188. /* empty and enable FIFOs */
  189. base->FIFOCFG |= SPI_FIFOCFG_EMPTYTX_MASK | SPI_FIFOCFG_EMPTYRX_MASK;
  190. base->FIFOCFG |= SPI_FIFOCFG_ENABLETX_MASK | SPI_FIFOCFG_ENABLERX_MASK;
  191. /* trigger level - empty txFIFO, one item in rxFIFO */
  192. tmp = base->FIFOTRIG & (~(SPI_FIFOTRIG_RXLVL_MASK | SPI_FIFOTRIG_TXLVL_MASK));
  193. tmp |= SPI_FIFOTRIG_TXLVL(config->txWatermark) | SPI_FIFOTRIG_RXLVL(config->rxWatermark);
  194. /* enable generating interrupts for FIFOTRIG levels */
  195. tmp |= SPI_FIFOTRIG_TXLVLENA_MASK | SPI_FIFOTRIG_RXLVLENA_MASK;
  196. /* set FIFOTRIG */
  197. base->FIFOTRIG = tmp;
  198. SPI_Enable(base, config->enableSlave);
  199. return kStatus_Success;
  200. }
  201. void SPI_Deinit(SPI_Type *base)
  202. {
  203. /* Assert arguments */
  204. assert(NULL != base);
  205. /* Disable interrupts, disable dma requests, disable peripheral */
  206. base->FIFOINTENCLR = SPI_FIFOINTENCLR_TXERR_MASK | SPI_FIFOINTENCLR_RXERR_MASK | SPI_FIFOINTENCLR_TXLVL_MASK |
  207. SPI_FIFOINTENCLR_RXLVL_MASK;
  208. base->FIFOCFG &= ~(SPI_FIFOCFG_DMATX_MASK | SPI_FIFOCFG_DMARX_MASK);
  209. base->CFG &= ~(SPI_CFG_ENABLE_MASK);
  210. }
  211. void SPI_EnableTxDMA(SPI_Type *base, bool enable)
  212. {
  213. if (enable)
  214. {
  215. base->FIFOCFG |= SPI_FIFOCFG_DMATX_MASK;
  216. }
  217. else
  218. {
  219. base->FIFOCFG &= ~SPI_FIFOCFG_DMATX_MASK;
  220. }
  221. }
  222. void SPI_EnableRxDMA(SPI_Type *base, bool enable)
  223. {
  224. if (enable)
  225. {
  226. base->FIFOCFG |= SPI_FIFOCFG_DMARX_MASK;
  227. }
  228. else
  229. {
  230. base->FIFOCFG &= ~SPI_FIFOCFG_DMARX_MASK;
  231. }
  232. }
  233. status_t SPI_MasterSetBaud(SPI_Type *base, uint32_t baudrate_Bps, uint32_t srcClock_Hz)
  234. {
  235. uint32_t tmp;
  236. /* assert params */
  237. assert(!((NULL == base) || (0 == baudrate_Bps) || (0 == srcClock_Hz)));
  238. if ((NULL == base) || (0 == baudrate_Bps) || (0 == srcClock_Hz))
  239. {
  240. return kStatus_InvalidArgument;
  241. }
  242. /* calculate baudrate */
  243. tmp = (srcClock_Hz / baudrate_Bps) - 1;
  244. if (tmp > 0xFFFF)
  245. {
  246. return kStatus_SPI_BaudrateNotSupport;
  247. }
  248. base->DIV &= ~SPI_DIV_DIVVAL_MASK;
  249. base->DIV |= SPI_DIV_DIVVAL(tmp);
  250. return kStatus_Success;
  251. }
  252. void SPI_WriteData(SPI_Type *base, uint16_t data, uint32_t configFlags)
  253. {
  254. uint32_t control = 0;
  255. int32_t instance;
  256. /* check params */
  257. assert(NULL != base);
  258. /* get and check instance */
  259. instance = SPI_GetInstance(base);
  260. assert(!(instance < 0));
  261. if (instance < 0)
  262. {
  263. return;
  264. }
  265. /* set data width */
  266. control |= SPI_FIFOWR_LEN(g_configs[instance].dataWidth);
  267. /* set sssel */
  268. control |= (SPI_DEASSERT_ALL & (~SPI_DEASSERTNUM_SSEL(g_configs[instance].sselNum)));
  269. /* mask configFlags */
  270. control |= (configFlags & SPI_FIFOWR_FLAGS_MASK);
  271. /* control should not affect lower 16 bits */
  272. assert(!(control & 0xFFFF));
  273. base->FIFOWR = data | control;
  274. }
  275. status_t SPI_MasterTransferCreateHandle(SPI_Type *base,
  276. spi_master_handle_t *handle,
  277. spi_master_callback_t callback,
  278. void *userData)
  279. {
  280. int32_t instance = 0;
  281. /* check 'base' */
  282. assert(!(NULL == base));
  283. if (NULL == base)
  284. {
  285. return kStatus_InvalidArgument;
  286. }
  287. /* check 'handle' */
  288. assert(!(NULL == handle));
  289. if (NULL == handle)
  290. {
  291. return kStatus_InvalidArgument;
  292. }
  293. /* get flexcomm instance by 'base' param */
  294. instance = SPI_GetInstance(base);
  295. assert(!(instance < 0));
  296. if (instance < 0)
  297. {
  298. return kStatus_InvalidArgument;
  299. }
  300. memset(handle, 0, sizeof(*handle));
  301. /* Initialize the handle */
  302. if (base->CFG & SPI_CFG_MASTER_MASK)
  303. {
  304. FLEXCOMM_SetIRQHandler(base, (flexcomm_irq_handler_t)(uintptr_t)SPI_MasterTransferHandleIRQ, handle);
  305. }
  306. else
  307. {
  308. FLEXCOMM_SetIRQHandler(base, (flexcomm_irq_handler_t)(uintptr_t)SPI_SlaveTransferHandleIRQ, handle);
  309. }
  310. handle->dataWidth = g_configs[instance].dataWidth;
  311. /* in slave mode, the sselNum is not important */
  312. handle->sselNum = g_configs[instance].sselNum;
  313. handle->txWatermark = (spi_txfifo_watermark_t)SPI_FIFOTRIG_TXLVL_GET(base);
  314. handle->rxWatermark = (spi_rxfifo_watermark_t)SPI_FIFOTRIG_RXLVL_GET(base);
  315. handle->callback = callback;
  316. handle->userData = userData;
  317. /* Enable SPI NVIC */
  318. EnableIRQ(s_spiIRQ[instance]);
  319. return kStatus_Success;
  320. }
  321. status_t SPI_MasterTransferBlocking(SPI_Type *base, spi_transfer_t *xfer)
  322. {
  323. int32_t instance;
  324. uint32_t tx_ctrl = 0, last_ctrl = 0;
  325. uint32_t tmp32, rxRemainingBytes, txRemainingBytes, dataWidth;
  326. uint32_t toReceiveCount = 0;
  327. uint8_t *txData, *rxData;
  328. uint32_t fifoDepth;
  329. /* check params */
  330. assert(!((NULL == base) || (NULL == xfer) || ((NULL == xfer->txData) && (NULL == xfer->rxData))));
  331. if ((NULL == base) || (NULL == xfer) || ((NULL == xfer->txData) && (NULL == xfer->rxData)))
  332. {
  333. return kStatus_InvalidArgument;
  334. }
  335. fifoDepth = SPI_FIFO_DEPTH(base);
  336. txData = xfer->txData;
  337. rxData = xfer->rxData;
  338. txRemainingBytes = txData ? xfer->dataSize : 0;
  339. rxRemainingBytes = rxData ? xfer->dataSize : 0;
  340. instance = SPI_GetInstance(base);
  341. assert(instance >= 0);
  342. dataWidth = g_configs[instance].dataWidth;
  343. /* dataSize (in bytes) is not aligned to 16bit (2B) transfer */
  344. assert(!((dataWidth > kSPI_Data8Bits) && (xfer->dataSize & 0x1)));
  345. if ((dataWidth > kSPI_Data8Bits) && (xfer->dataSize & 0x1))
  346. {
  347. return kStatus_InvalidArgument;
  348. }
  349. /* clear tx/rx errors and empty FIFOs */
  350. base->FIFOCFG |= SPI_FIFOCFG_EMPTYTX_MASK | SPI_FIFOCFG_EMPTYRX_MASK;
  351. base->FIFOSTAT |= SPI_FIFOSTAT_TXERR_MASK | SPI_FIFOSTAT_RXERR_MASK;
  352. /* select slave to talk with */
  353. tx_ctrl |= (SPI_DEASSERT_ALL & (~SPI_DEASSERTNUM_SSEL(g_configs[instance].sselNum)));
  354. /* set width of data - range asserted at entry */
  355. tx_ctrl |= SPI_FIFOWR_LEN(dataWidth);
  356. /* end of transfer */
  357. last_ctrl |= (xfer->configFlags & (uint32_t)kSPI_FrameAssert) ? (uint32_t)kSPI_FrameAssert : 0;
  358. /* delay end of transfer */
  359. last_ctrl |= (xfer->configFlags & (uint32_t)kSPI_FrameDelay) ? (uint32_t)kSPI_FrameDelay : 0;
  360. /* last index of loop */
  361. while (txRemainingBytes || rxRemainingBytes || toReceiveCount)
  362. {
  363. /* if rxFIFO is not empty */
  364. if (base->FIFOSTAT & SPI_FIFOSTAT_RXNOTEMPTY_MASK)
  365. {
  366. tmp32 = base->FIFORD;
  367. /* rxBuffer is not empty */
  368. if (rxRemainingBytes)
  369. {
  370. *(rxData++) = tmp32;
  371. rxRemainingBytes--;
  372. /* read 16 bits at once */
  373. if (dataWidth > 8)
  374. {
  375. *(rxData++) = tmp32 >> 8;
  376. rxRemainingBytes--;
  377. }
  378. }
  379. /* decrease number of data expected to receive */
  380. toReceiveCount -= 1;
  381. }
  382. /* transmit if txFIFO is not full and data to receive does not exceed FIFO depth */
  383. if ((base->FIFOSTAT & SPI_FIFOSTAT_TXNOTFULL_MASK) && (toReceiveCount < fifoDepth) &&
  384. ((txRemainingBytes) || (rxRemainingBytes >= SPI_COUNT_TO_BYTES(dataWidth, toReceiveCount + 1))))
  385. {
  386. /* txBuffer is not empty */
  387. if (txRemainingBytes)
  388. {
  389. tmp32 = *(txData++);
  390. txRemainingBytes--;
  391. /* write 16 bit at once */
  392. if (dataWidth > 8)
  393. {
  394. tmp32 |= ((uint32_t)(*(txData++))) << 8U;
  395. txRemainingBytes--;
  396. }
  397. if (!txRemainingBytes)
  398. {
  399. tx_ctrl |= last_ctrl;
  400. }
  401. }
  402. else
  403. {
  404. tmp32 = SPI_DUMMYDATA;
  405. /* last transfer */
  406. if (rxRemainingBytes == SPI_COUNT_TO_BYTES(dataWidth, toReceiveCount + 1))
  407. {
  408. tx_ctrl |= last_ctrl;
  409. }
  410. }
  411. /* send data */
  412. tmp32 = tx_ctrl | tmp32;
  413. base->FIFOWR = tmp32;
  414. toReceiveCount += 1;
  415. }
  416. }
  417. /* wait if TX FIFO of previous transfer is not empty */
  418. while (!(base->FIFOSTAT & SPI_FIFOSTAT_TXEMPTY_MASK))
  419. {
  420. }
  421. return kStatus_Success;
  422. }
  423. status_t SPI_MasterTransferNonBlocking(SPI_Type *base, spi_master_handle_t *handle, spi_transfer_t *xfer)
  424. {
  425. /* check params */
  426. assert(
  427. !((NULL == base) || (NULL == handle) || (NULL == xfer) || ((NULL == xfer->txData) && (NULL == xfer->rxData))));
  428. if ((NULL == base) || (NULL == handle) || (NULL == xfer) || ((NULL == xfer->txData) && (NULL == xfer->rxData)))
  429. {
  430. return kStatus_InvalidArgument;
  431. }
  432. /* dataSize (in bytes) is not aligned to 16bit (2B) transfer */
  433. assert(!((handle->dataWidth > kSPI_Data8Bits) && (xfer->dataSize & 0x1)));
  434. if ((handle->dataWidth > kSPI_Data8Bits) && (xfer->dataSize & 0x1))
  435. {
  436. return kStatus_InvalidArgument;
  437. }
  438. /* Check if SPI is busy */
  439. if (handle->state == kStatus_SPI_Busy)
  440. {
  441. return kStatus_SPI_Busy;
  442. }
  443. /* Set the handle information */
  444. handle->txData = xfer->txData;
  445. handle->rxData = xfer->rxData;
  446. /* set count */
  447. handle->txRemainingBytes = xfer->txData ? xfer->dataSize : 0;
  448. handle->rxRemainingBytes = xfer->rxData ? xfer->dataSize : 0;
  449. handle->totalByteCount = xfer->dataSize;
  450. /* other options */
  451. handle->toReceiveCount = 0;
  452. handle->configFlags = xfer->configFlags;
  453. /* Set the SPI state to busy */
  454. handle->state = kStatus_SPI_Busy;
  455. /* clear FIFOs when transfer starts */
  456. base->FIFOCFG |= SPI_FIFOCFG_EMPTYTX_MASK | SPI_FIFOCFG_EMPTYRX_MASK;
  457. base->FIFOSTAT |= SPI_FIFOSTAT_TXERR_MASK | SPI_FIFOSTAT_RXERR_MASK;
  458. /* enable generating txIRQ and rxIRQ, first transfer is fired by empty txFIFO */
  459. base->FIFOINTENSET |= SPI_FIFOINTENSET_TXLVL_MASK | SPI_FIFOINTENSET_RXLVL_MASK;
  460. return kStatus_Success;
  461. }
  462. status_t SPI_MasterTransferGetCount(SPI_Type *base, spi_master_handle_t *handle, size_t *count)
  463. {
  464. assert(NULL != handle);
  465. if (!count)
  466. {
  467. return kStatus_InvalidArgument;
  468. }
  469. /* Catch when there is not an active transfer. */
  470. if (handle->state != kStatus_SPI_Busy)
  471. {
  472. *count = 0;
  473. return kStatus_NoTransferInProgress;
  474. }
  475. *count = handle->totalByteCount - handle->rxRemainingBytes;
  476. return kStatus_Success;
  477. }
  478. void SPI_MasterTransferAbort(SPI_Type *base, spi_master_handle_t *handle)
  479. {
  480. assert(NULL != handle);
  481. /* Disable interrupt requests*/
  482. base->FIFOINTENSET &= ~(SPI_FIFOINTENSET_TXLVL_MASK | SPI_FIFOINTENSET_RXLVL_MASK);
  483. /* Empty FIFOs */
  484. base->FIFOCFG |= SPI_FIFOCFG_EMPTYTX_MASK | SPI_FIFOCFG_EMPTYRX_MASK;
  485. handle->state = kStatus_SPI_Idle;
  486. handle->txRemainingBytes = 0;
  487. handle->rxRemainingBytes = 0;
  488. }
  489. static void SPI_TransferHandleIRQInternal(SPI_Type *base, spi_master_handle_t *handle)
  490. {
  491. uint32_t tx_ctrl = 0, last_ctrl = 0, tmp32;
  492. bool loopContinue;
  493. uint32_t fifoDepth;
  494. /* check params */
  495. assert((NULL != base) && (NULL != handle) && ((NULL != handle->txData) || (NULL != handle->rxData)));
  496. fifoDepth = SPI_FIFO_DEPTH(base);
  497. /* select slave to talk with */
  498. tx_ctrl |= (SPI_DEASSERT_ALL & SPI_ASSERTNUM_SSEL(handle->sselNum));
  499. /* set width of data */
  500. tx_ctrl |= SPI_FIFOWR_LEN(handle->dataWidth);
  501. /* end of transfer */
  502. last_ctrl |= (handle->configFlags & (uint32_t)kSPI_FrameAssert) ? (uint32_t)kSPI_FrameAssert : 0;
  503. /* delay end of transfer */
  504. last_ctrl |= (handle->configFlags & (uint32_t)kSPI_FrameDelay) ? (uint32_t)kSPI_FrameDelay : 0;
  505. do
  506. {
  507. loopContinue = false;
  508. /* rxFIFO is not empty */
  509. if (base->FIFOSTAT & SPI_FIFOSTAT_RXNOTEMPTY_MASK)
  510. {
  511. tmp32 = base->FIFORD;
  512. /* rxBuffer is not empty */
  513. if (handle->rxRemainingBytes)
  514. {
  515. /* low byte must go first */
  516. *(handle->rxData++) = tmp32;
  517. handle->rxRemainingBytes--;
  518. /* read 16 bits at once */
  519. if (handle->dataWidth > kSPI_Data8Bits)
  520. {
  521. *(handle->rxData++) = tmp32 >> 8;
  522. handle->rxRemainingBytes--;
  523. }
  524. }
  525. /* decrease number of data expected to receive */
  526. handle->toReceiveCount -= 1;
  527. loopContinue = true;
  528. }
  529. /* - txFIFO is not full
  530. * - we cannot cause rxFIFO overflow by sending more data than is the depth of FIFO
  531. * - txBuffer is not empty or the next 'toReceiveCount' data can fit into rxBuffer
  532. */
  533. if ((base->FIFOSTAT & SPI_FIFOSTAT_TXNOTFULL_MASK) && (handle->toReceiveCount < fifoDepth) &&
  534. ((handle->txRemainingBytes) ||
  535. (handle->rxRemainingBytes >= SPI_COUNT_TO_BYTES(handle->dataWidth, handle->toReceiveCount + 1))))
  536. {
  537. /* txBuffer is not empty */
  538. if (handle->txRemainingBytes)
  539. {
  540. /* low byte must go first */
  541. tmp32 = *(handle->txData++);
  542. handle->txRemainingBytes--;
  543. /* write 16 bit at once */
  544. if (handle->dataWidth > kSPI_Data8Bits)
  545. {
  546. tmp32 |= ((uint32_t)(*(handle->txData++))) << 8U;
  547. handle->txRemainingBytes--;
  548. }
  549. /* last transfer */
  550. if (!handle->txRemainingBytes)
  551. {
  552. tx_ctrl |= last_ctrl;
  553. }
  554. }
  555. else
  556. {
  557. tmp32 = SPI_DUMMYDATA;
  558. /* last transfer */
  559. if (handle->rxRemainingBytes == SPI_COUNT_TO_BYTES(handle->dataWidth, handle->toReceiveCount + 1))
  560. {
  561. tx_ctrl |= last_ctrl;
  562. }
  563. }
  564. /* send data */
  565. tmp32 = tx_ctrl | tmp32;
  566. base->FIFOWR = tmp32;
  567. /* increase number of expected data to receive */
  568. handle->toReceiveCount += 1;
  569. loopContinue = true;
  570. }
  571. } while (loopContinue);
  572. }
  573. void SPI_MasterTransferHandleIRQ(SPI_Type *base, spi_master_handle_t *handle)
  574. {
  575. assert((NULL != base) && (NULL != handle));
  576. /* IRQ behaviour:
  577. * - first interrupt is triggered by empty txFIFO. The transfer function
  578. * then tries empty rxFIFO and fill txFIFO interleaved that results to
  579. * strategy to process as many items as possible.
  580. * - the next IRQs can be:
  581. * rxIRQ from nonempty rxFIFO which requires to empty rxFIFO.
  582. * txIRQ from empty txFIFO which requires to refill txFIFO.
  583. * - last interrupt is triggered by empty txFIFO. The last state is
  584. * known by empty rxBuffer and txBuffer. If there is nothing to receive
  585. * or send - both operations have been finished and interrupts can be
  586. * disabled.
  587. */
  588. /* Data to send or read or expected to receive */
  589. if ((handle->txRemainingBytes) || (handle->rxRemainingBytes) || (handle->toReceiveCount))
  590. {
  591. /* Transmit or receive data */
  592. SPI_TransferHandleIRQInternal(base, handle);
  593. /* No data to send or read or receive. Transfer ends. Set txTrigger to 0 level and
  594. * enable txIRQ to confirm when txFIFO becomes empty */
  595. if ((!handle->txRemainingBytes) && (!handle->rxRemainingBytes) && (!handle->toReceiveCount))
  596. {
  597. base->FIFOTRIG = base->FIFOTRIG & (~SPI_FIFOTRIG_TXLVL_MASK);
  598. base->FIFOINTENSET |= SPI_FIFOINTENSET_TXLVL_MASK;
  599. }
  600. else
  601. {
  602. uint32_t rxRemainingCount = SPI_BYTES_TO_COUNT(handle->dataWidth, handle->rxRemainingBytes);
  603. /* If, there are no data to send or rxFIFO is already filled with necessary number of dummy data,
  604. * disable txIRQ. From this point only rxIRQ is used to receive data without any transmission */
  605. if ((!handle->txRemainingBytes) && (rxRemainingCount <= handle->toReceiveCount))
  606. {
  607. base->FIFOINTENCLR = SPI_FIFOINTENCLR_TXLVL_MASK;
  608. }
  609. /* Nothing to receive or transmit, but we still have pending data which are bellow rxLevel.
  610. * Cannot clear rxFIFO, txFIFO might be still active */
  611. if (rxRemainingCount == 0)
  612. {
  613. if ((handle->txRemainingBytes == 0) && (handle->toReceiveCount != 0) &&
  614. (handle->toReceiveCount < SPI_FIFOTRIG_RXLVL_GET(base) + 1))
  615. {
  616. base->FIFOTRIG =
  617. (base->FIFOTRIG & (~SPI_FIFOTRIG_RXLVL_MASK)) | SPI_FIFOTRIG_RXLVL(handle->toReceiveCount - 1);
  618. }
  619. }
  620. /* Expected to receive less data than rxLevel value, we have to update rxLevel */
  621. else
  622. {
  623. if (rxRemainingCount < (SPI_FIFOTRIG_RXLVL_GET(base) + 1))
  624. {
  625. base->FIFOTRIG =
  626. (base->FIFOTRIG & (~SPI_FIFOTRIG_RXLVL_MASK)) | SPI_FIFOTRIG_RXLVL(rxRemainingCount - 1);
  627. }
  628. }
  629. }
  630. }
  631. else
  632. {
  633. /* Empty txFIFO is confirmed. Disable IRQs and restore triggers values */
  634. base->FIFOINTENCLR = SPI_FIFOINTENCLR_RXLVL_MASK | SPI_FIFOINTENCLR_TXLVL_MASK;
  635. base->FIFOTRIG = (base->FIFOTRIG & (~(SPI_FIFOTRIG_RXLVL_MASK | SPI_FIFOTRIG_RXLVL_MASK))) |
  636. SPI_FIFOTRIG_RXLVL(handle->rxWatermark) | SPI_FIFOTRIG_TXLVL(handle->txWatermark);
  637. /* set idle state and call user callback */
  638. handle->state = kStatus_SPI_Idle;
  639. if (handle->callback)
  640. {
  641. (handle->callback)(base, handle, handle->state, handle->userData);
  642. }
  643. }
  644. }