fsl_ecspi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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_ecspi.h"
  31. /*******************************************************************************
  32. * Definitons
  33. ******************************************************************************/
  34. /*! @brief ECSPI transfer state, which is used for ECSPI transactiaonl APIs' internal state. */
  35. enum _ecspi_transfer_states_t
  36. {
  37. kECSPI_Idle = 0x0, /*!< ECSPI is idle state */
  38. kECSPI_Busy /*!< ECSPI is busy tranferring data. */
  39. };
  40. /*! @brief Typedef for ecspi master interrupt handler. ecspi master and slave handle is the same. */
  41. typedef void (*ecspi_isr_t)(ECSPI_Type *base, ecspi_master_handle_t *ecspiHandle);
  42. /*******************************************************************************
  43. * Prototypes
  44. ******************************************************************************/
  45. /*!
  46. * @brief Get the instance for ECSPI module.
  47. *
  48. * @param base ECSPI base address
  49. */
  50. uint32_t ECSPI_GetInstance(ECSPI_Type *base);
  51. /*!
  52. * @brief Sends a buffer of data bytes in non-blocking way.
  53. *
  54. * @param base ECSPI base pointer
  55. * @param buffer The data bytes to send
  56. * @param size The number of data bytes to send
  57. */
  58. static void ECSPI_WriteNonBlocking(ECSPI_Type *base, uint32_t *buffer, size_t size);
  59. /*!
  60. * @brief Receive a buffer of data bytes in non-blocking way.
  61. *
  62. * @param base ECSPI base pointer
  63. * @param buffer The data bytes to send
  64. * @param size The number of data bytes to send
  65. */
  66. static void ECSPI_ReadNonBlocking(ECSPI_Type *base, uint32_t *buffer, size_t size);
  67. /*!
  68. * @brief Send a piece of data for ECSPI.
  69. *
  70. * This function computes the number of data to be written into D register or Tx FIFO,
  71. * and write the data into it. At the same time, this function updates the values in
  72. * master handle structure.
  73. *
  74. * @param base ECSPI base pointer
  75. * @param handle Pointer to ECSPI master handle structure.
  76. */
  77. static void ECSPI_SendTransfer(ECSPI_Type *base, ecspi_master_handle_t *handle);
  78. /*!
  79. * @brief Receive a piece of data for ECSPI master.
  80. *
  81. * This function computes the number of data to receive from D register or Rx FIFO,
  82. * and write the data to destination address. At the same time, this function updates
  83. * the values in master handle structure.
  84. *
  85. * @param base ECSPI base pointer
  86. * @param handle Pointer to ECSPI master handle structure.
  87. */
  88. static void ECSPI_ReceiveTransfer(ECSPI_Type *base, ecspi_master_handle_t *handle);
  89. /*!
  90. * @brief Sets the ECSPI channel configuration structure to default values.
  91. *
  92. * This function is to get the channel configuration structure initialized for use in ECSPI_SetChannelConfig().
  93. * User may use the initialized structure unchanged in ECSPI_SetChannelConfig(), or modify
  94. * some fields of the structure before calling ECSPI_SetChannelConfig().
  95. *
  96. * @param config pointer to config structure
  97. */
  98. static void ECSPI_GetDefaultChannelConfig(ecspi_channel_config_t *config);
  99. /*!
  100. * @brief Common IRQ handler for SPI.
  101. *
  102. * @param base SPI base pointer.
  103. * @param instance SPI instance number.
  104. */
  105. static void ECSPI_CommonIRQHandler(ECSPI_Type *base, ecspi_master_handle_t *handle);
  106. /*******************************************************************************
  107. * Variables
  108. ******************************************************************************/
  109. /*! @brief Base pointer array */
  110. static ECSPI_Type *const s_ecspiBases[] = ECSPI_BASE_PTRS;
  111. /*! @brief ECSPI internal handle pointer array */
  112. static ecspi_master_handle_t *s_ecspiHandle[ARRAY_SIZE(s_ecspiBases)];
  113. /*! @brief IRQ name array */
  114. static const IRQn_Type s_ecspiIRQ[] = ECSPI_IRQS;
  115. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  116. /*! @brief Clock array name */
  117. static const clock_ip_name_t s_ecspiClock[] = ECSPI_CLOCKS;
  118. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  119. /*! @brief Pointer to master IRQ handler for each instance. */
  120. static ecspi_isr_t s_ecspiMasterIsr;
  121. /*! @brief Pointer to slave IRQ handler for each instance. */
  122. static ecspi_isr_t s_ecspiSlaveIsr;
  123. /*******************************************************************************
  124. * Code
  125. ******************************************************************************/
  126. uint32_t ECSPI_GetInstance(ECSPI_Type *base)
  127. {
  128. uint32_t instance;
  129. /* Find the instance index from base address mappings. */
  130. for (instance = 0; instance < ARRAY_SIZE(s_ecspiBases); instance++)
  131. {
  132. if (s_ecspiBases[instance] == base)
  133. {
  134. break;
  135. }
  136. }
  137. assert(instance <= ARRAY_SIZE(s_ecspiBases));
  138. return instance;
  139. }
  140. static void ECSPI_WriteNonBlocking(ECSPI_Type *base, uint32_t *buffer, size_t size)
  141. {
  142. size_t i = 0U;
  143. for (i = 0U; i < size; i++)
  144. {
  145. if (buffer != NULL)
  146. {
  147. base->TXDATA = *buffer++;
  148. }
  149. else
  150. {
  151. ECSPI_WriteData(base, ECSPI_DUMMYDATA);
  152. }
  153. }
  154. }
  155. static void ECSPI_ReadNonBlocking(ECSPI_Type *base, uint32_t *buffer, size_t size)
  156. {
  157. if (NULL != buffer)
  158. {
  159. while (size--)
  160. {
  161. *buffer++ = ECSPI_ReadData(base);
  162. }
  163. }
  164. else
  165. {
  166. while (size--)
  167. {
  168. (void)ECSPI_ReadData(base);
  169. }
  170. }
  171. }
  172. static void ECSPI_SendTransfer(ECSPI_Type *base, ecspi_master_handle_t *handle)
  173. {
  174. assert(base);
  175. assert(handle);
  176. uint32_t dataCounts = 0U;
  177. /* Caculate the data size to send */
  178. dataCounts = (FSL_FEATURE_ECSPI_TX_FIFO_SIZEn(base) - ECSPI_GetTxFifoCount(base)) < (handle->txRemainingBytes) ?
  179. (FSL_FEATURE_ECSPI_TX_FIFO_SIZEn(base) - ECSPI_GetTxFifoCount(base)) :
  180. (handle->txRemainingBytes);
  181. while (dataCounts--)
  182. {
  183. ECSPI_WriteNonBlocking(base, handle->txData, 1);
  184. if (NULL != handle->txData)
  185. {
  186. handle->txData += 1U;
  187. }
  188. handle->txRemainingBytes -= 1U;
  189. }
  190. }
  191. static void ECSPI_ReceiveTransfer(ECSPI_Type *base, ecspi_master_handle_t *handle)
  192. {
  193. assert(base);
  194. uint32_t dataCounts = 0U;
  195. /* Caculate the data size need to receive */
  196. dataCounts =
  197. (ECSPI_GetRxFifoCount(base) < handle->rxRemainingBytes) ? ECSPI_GetRxFifoCount(base) : handle->rxRemainingBytes;
  198. ECSPI_ReadNonBlocking(base, handle->rxData, dataCounts);
  199. if (NULL != handle->rxData)
  200. {
  201. handle->rxData += dataCounts;
  202. }
  203. handle->rxRemainingBytes -= dataCounts;
  204. }
  205. static void ECSPI_GetDefaultChannelConfig(ecspi_channel_config_t *config)
  206. {
  207. config->channelMode = kECSPI_Slave; /*!< ECSPI peripheral operates in slave mode.*/
  208. config->clockInactiveState = kECSPI_ClockInactiveStateLow; /*!< Clock line (SCLK) inactive state */
  209. config->dataLineInactiveState = kECSPI_DataLineInactiveStateLow; /*!< Data line (MOSI&MISO) inactive state */
  210. config->chipSlectActiveState = kECSPI_ChipSelectActiveStateLow; /*!< Chip select(SS) line active state */
  211. config->waveForm = kECSPI_WaveFormSingle; /*!< ECSPI SS wave form */
  212. config->polarity = kECSPI_PolarityActiveHigh; /*!< Clock polarity */
  213. config->phase = kECSPI_ClockPhaseFirstEdge; /*!< clock phase */
  214. }
  215. void ECSPI_MasterGetDefaultConfig(ecspi_master_config_t *config)
  216. {
  217. config->channel = kECSPI_Channel0;
  218. config->burstLength = 8;
  219. config->samplePeriodClock = kECSPI_spiClock;
  220. config->baudRate_Bps = 500000;
  221. config->chipSelectDelay = 0;
  222. config->samplePeriod = 0;
  223. config->txFifoThreshold = 1;
  224. config->rxFifoThreshold = 0;
  225. /* Default configuration of channel */
  226. ECSPI_GetDefaultChannelConfig(&config->channelConfig);
  227. /*!< ECSPI peripheral operates in slave mode.*/
  228. config->channelConfig.channelMode = kECSPI_Master;
  229. }
  230. void ECSPI_MasterInit(ECSPI_Type *base, const ecspi_master_config_t *config, uint32_t srcClock_Hz)
  231. {
  232. assert(config && srcClock_Hz);
  233. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  234. /* Open clock gate for SPI and open interrupt */
  235. CLOCK_EnableClock(s_ecspiClock[ECSPI_GetInstance(base)]);
  236. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  237. /* Reset control register to default value */
  238. ECSPI_SoftwareReset(base);
  239. /* Config CONREG register */
  240. base->CONREG = ECSPI_CONREG_BURST_LENGTH(config->burstLength - 1) | ECSPI_CONREG_SMC(1) | ECSPI_CONREG_EN(1);
  241. /* Config CONFIGREG register */
  242. ECSPI_SetChannelConfig(base, config->channel, &config->channelConfig);
  243. /* Config DMAREG register */
  244. base->DMAREG |=
  245. ECSPI_DMAREG_TX_THRESHOLD(config->txFifoThreshold) | ECSPI_DMAREG_RX_THRESHOLD(config->rxFifoThreshold);
  246. /* Config PERIODREG register */
  247. base->PERIODREG |= ECSPI_PERIODREG_CSRC(config->samplePeriodClock) |
  248. ECSPI_PERIODREG_SAMPLE_PERIOD(config->samplePeriod) |
  249. ECSPI_PERIODREG_CSD_CTL(config->chipSelectDelay);
  250. /* Set baud rate */
  251. ECSPI_SetBaudRate(base, config->baudRate_Bps, srcClock_Hz);
  252. }
  253. void ECSPI_SlaveGetDefaultConfig(ecspi_slave_config_t *config)
  254. {
  255. /* Default configuration of channel nember */
  256. config->channel = kECSPI_Channel0;
  257. config->burstLength = 8;
  258. config->txFifoThreshold = 1;
  259. config->rxFifoThreshold = 0;
  260. /* Set default channel configuration */
  261. ECSPI_GetDefaultChannelConfig(&config->channelConfig);
  262. /* ECSPI peripheral operates in slave mode.*/
  263. config->channelConfig.channelMode = kECSPI_Slave;
  264. }
  265. void ECSPI_SlaveInit(ECSPI_Type *base, const ecspi_slave_config_t *config)
  266. {
  267. assert(base && config);
  268. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  269. /* Open clock gate for SPI and open interrupt */
  270. CLOCK_EnableClock(s_ecspiClock[ECSPI_GetInstance(base)]);
  271. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  272. /* Reset control register to default value */
  273. ECSPI_SoftwareReset(base);
  274. /* Config CONREG register */
  275. base->CONREG = ECSPI_CONREG_BURST_LENGTH(config->burstLength - 1) | ECSPI_CONREG_EN(1);
  276. /* Config DMAREG register */
  277. base->DMAREG |=
  278. ECSPI_DMAREG_TX_THRESHOLD(config->txFifoThreshold) | ECSPI_DMAREG_RX_THRESHOLD(config->rxFifoThreshold);
  279. /* Setup channel configuration */
  280. ECSPI_SetChannelConfig(base, config->channel, &config->channelConfig);
  281. }
  282. void ECSPI_Deinit(ECSPI_Type *base)
  283. {
  284. /* Disable ECSPI module before shutting down */
  285. base->CONREG &= ~ECSPI_CONREG_EN_MASK;
  286. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  287. /* Gate the clock */
  288. CLOCK_DisableClock(s_ecspiClock[ECSPI_GetInstance(base)]);
  289. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  290. }
  291. void ECSPI_SetBaudRate(ECSPI_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
  292. {
  293. assert(base);
  294. uint8_t bestPreDividerValue = 0U, preDividerValue = 0U;
  295. uint8_t bestPostDividerValue = 0U, postDividerValue = 0U;
  296. uint32_t realBaudrate = 0U;
  297. uint32_t diff = 0xFFFFFFFFU;
  298. uint32_t min_diff = 0xFFFFFFFFU;
  299. for (preDividerValue = 0; (preDividerValue < 16) && diff; preDividerValue++)
  300. {
  301. for (postDividerValue = 0; (postDividerValue < 16) && diff; postDividerValue++)
  302. {
  303. realBaudrate = (srcClock_Hz / (preDividerValue + 1)) >> postDividerValue;
  304. if (realBaudrate > baudRate_Bps)
  305. {
  306. diff = realBaudrate - baudRate_Bps;
  307. if (diff < min_diff)
  308. {
  309. min_diff = diff;
  310. bestPreDividerValue = preDividerValue;
  311. bestPostDividerValue = postDividerValue;
  312. }
  313. }
  314. else
  315. {
  316. diff = baudRate_Bps - realBaudrate;
  317. if (diff < min_diff)
  318. {
  319. min_diff = diff;
  320. bestPreDividerValue = preDividerValue;
  321. bestPostDividerValue = postDividerValue;
  322. }
  323. }
  324. }
  325. }
  326. base->CONREG |= ECSPI_CONREG_PRE_DIVIDER(bestPreDividerValue) | ECSPI_CONREG_POST_DIVIDER(bestPostDividerValue);
  327. }
  328. void ECSPI_SetChannelConfig(ECSPI_Type *base, ecspi_channel_source_t channel, const ecspi_channel_config_t *config)
  329. {
  330. switch (channel)
  331. {
  332. case kECSPI_Channel0:
  333. base->CONREG |= ECSPI_CONREG_CHANNEL_MODE(config->channelMode);
  334. base->CONFIGREG |=
  335. (ECSPI_CONFIGREG_SCLK_CTL(config->clockInactiveState) |
  336. ECSPI_CONFIGREG_DATA_CTL(config->dataLineInactiveState) |
  337. ECSPI_CONFIGREG_SS_POL(config->chipSlectActiveState) | ECSPI_CONFIGREG_SS_CTL(config->waveForm) |
  338. ECSPI_CONFIGREG_SCLK_POL(config->polarity) | ECSPI_CONFIGREG_SCLK_PHA(config->phase));
  339. break;
  340. case kECSPI_Channel1:
  341. base->CONREG |= ECSPI_CONREG_CHANNEL_MODE(config->channelMode) << 1;
  342. base->CONFIGREG |=
  343. ((ECSPI_CONFIGREG_SCLK_CTL(config->clockInactiveState) << 1) |
  344. (ECSPI_CONFIGREG_DATA_CTL(config->dataLineInactiveState) << 1) |
  345. (ECSPI_CONFIGREG_SS_POL(config->chipSlectActiveState) << 1) |
  346. (ECSPI_CONFIGREG_SS_CTL(config->waveForm) << 1) | (ECSPI_CONFIGREG_SCLK_POL(config->polarity) << 1) |
  347. (ECSPI_CONFIGREG_SCLK_PHA(config->phase) << 1));
  348. break;
  349. case kECSPI_Channel2:
  350. base->CONREG |= ECSPI_CONREG_CHANNEL_MODE(config->channelMode) << 2;
  351. base->CONFIGREG |=
  352. ((ECSPI_CONFIGREG_SCLK_CTL(config->clockInactiveState) << 2) |
  353. (ECSPI_CONFIGREG_DATA_CTL(config->dataLineInactiveState) << 2) |
  354. (ECSPI_CONFIGREG_SS_POL(config->chipSlectActiveState) << 2) |
  355. (ECSPI_CONFIGREG_SS_CTL(config->waveForm) << 2) | (ECSPI_CONFIGREG_SCLK_POL(config->polarity) << 2) |
  356. (ECSPI_CONFIGREG_SCLK_PHA(config->phase) << 2));
  357. break;
  358. case kECSPI_Channel3:
  359. base->CONREG |= ECSPI_CONREG_CHANNEL_MODE(config->channelMode) << 3;
  360. base->CONFIGREG |=
  361. ((ECSPI_CONFIGREG_SCLK_CTL(config->clockInactiveState) << 3) |
  362. (ECSPI_CONFIGREG_DATA_CTL(config->dataLineInactiveState) << 3) |
  363. (ECSPI_CONFIGREG_SS_POL(config->chipSlectActiveState) << 3) |
  364. (ECSPI_CONFIGREG_SS_CTL(config->waveForm) << 3) | (ECSPI_CONFIGREG_SCLK_POL(config->polarity) << 3) |
  365. (ECSPI_CONFIGREG_SCLK_PHA(config->phase) << 3));
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. void ECSPI_WriteBlocking(ECSPI_Type *base, uint32_t *buffer, size_t size)
  372. {
  373. size_t i = 0U;
  374. while (i < size)
  375. {
  376. /* Wait for TX fifo buffer empty */
  377. while (!(base->STATREG & ECSPI_STATREG_TE_MASK))
  378. {
  379. }
  380. /* Write data to tx register */
  381. if (NULL != buffer)
  382. {
  383. ECSPI_WriteData(base, *buffer++);
  384. }
  385. else
  386. {
  387. ECSPI_WriteData(base, ECSPI_DUMMYDATA);
  388. }
  389. i++;
  390. }
  391. }
  392. static status_t ECSPI_ReadBlocking(ECSPI_Type *base, uint32_t *buffer, size_t size)
  393. {
  394. assert(base);
  395. uint32_t state = 0U;
  396. size_t i = 0U;
  397. while (i < size)
  398. {
  399. /* Wait for RX FIFO buffer ready */
  400. while (!(base->STATREG & ECSPI_STATREG_RR_MASK))
  401. {
  402. /* Get status flags of ECSPI */
  403. state = ECSPI_GetStatusFlags(base);
  404. /* If hardware overflow happen */
  405. if (ECSPI_STATREG_RO_MASK & state)
  406. {
  407. /* Clear overflow flag for next transfer */
  408. ECSPI_ClearStatusFlags(base, kECSPI_RxFifoOverFlowFlag);
  409. return kStatus_ECSPI_HardwareOverFlow;
  410. }
  411. }
  412. /* Read data from rx register */
  413. if (NULL != buffer)
  414. {
  415. *buffer++ = ECSPI_ReadData(base);
  416. }
  417. else
  418. {
  419. (void)ECSPI_ReadData(base);
  420. }
  421. i++;
  422. }
  423. return kStatus_Success;
  424. }
  425. void ECSPI_MasterTransferCreateHandle(ECSPI_Type *base,
  426. ecspi_master_handle_t *handle,
  427. ecspi_master_callback_t callback,
  428. void *userData)
  429. {
  430. assert(base);
  431. assert(handle);
  432. uint8_t instance = ECSPI_GetInstance(base);
  433. /* Initialize the handle */
  434. s_ecspiHandle[instance] = handle;
  435. handle->callback = callback;
  436. handle->userData = userData;
  437. s_ecspiMasterIsr = ECSPI_MasterTransferHandleIRQ;
  438. /* Enable ECSPI NVIC */
  439. EnableIRQ(s_ecspiIRQ[instance]);
  440. }
  441. status_t ECSPI_MasterTransferBlocking(ECSPI_Type *base, ecspi_transfer_t *xfer)
  442. {
  443. assert(base && xfer);
  444. status_t state;
  445. uint32_t burstLength = 0U;
  446. uint32_t dataCounts = 0U;
  447. /* Check if the argument is legal */
  448. if ((xfer->txData == NULL) && (xfer->rxData == NULL))
  449. {
  450. return kStatus_InvalidArgument;
  451. }
  452. /* Select ECSPI channel to current channel
  453. * Note:
  454. * xfer.channel must be configured before transfer, because every channel has
  455. * it's own configuration,if don't configure this parameter, transfer channel
  456. * will use the default channel0.
  457. */
  458. ECSPI_SetChannelSelect(base, xfer->channel);
  459. /* Caculate the data size need to be send for one burst */
  460. burstLength = ((base->CONREG & ECSPI_CONREG_BURST_LENGTH_MASK) >> ECSPI_CONREG_BURST_LENGTH_SHIFT) + 1;
  461. dataCounts = (burstLength % 32) ? (burstLength / 32 + 1) : (burstLength / 32);
  462. while (xfer->dataSize > 0)
  463. {
  464. /* ECSPI will transmit and receive at the same time, if txData is NULL,
  465. * instance will transmit dummy data, the dummy data can be set by user.
  466. * if rxData is NULL, data will be read from RX FIFO buffer, but the
  467. * data will be ignored by driver.
  468. * Note that, txData and rxData can not be both NULL.
  469. */
  470. ECSPI_WriteBlocking(base, xfer->txData, dataCounts);
  471. if (NULL != xfer->txData)
  472. {
  473. xfer->txData += dataCounts;
  474. }
  475. state = ECSPI_ReadBlocking(base, xfer->rxData, dataCounts);
  476. if ((kStatus_Success == state) && (NULL != xfer->rxData))
  477. {
  478. xfer->rxData += dataCounts;
  479. }
  480. if (kStatus_ECSPI_HardwareOverFlow == state)
  481. {
  482. return kStatus_ECSPI_HardwareOverFlow;
  483. }
  484. xfer->dataSize -= dataCounts;
  485. }
  486. return kStatus_Success;
  487. }
  488. status_t ECSPI_MasterTransferNonBlocking(ECSPI_Type *base, ecspi_master_handle_t *handle, ecspi_transfer_t *xfer)
  489. {
  490. assert(base && handle && xfer);
  491. /* Check if ECSPI is busy */
  492. if (handle->state == kECSPI_Busy)
  493. {
  494. return kStatus_ECSPI_Busy;
  495. }
  496. /* Check if the input arguments valid */
  497. if (((xfer->txData == NULL) && (xfer->rxData == NULL)) || (xfer->dataSize == 0U))
  498. {
  499. return kStatus_InvalidArgument;
  500. }
  501. /* Set the handle information */
  502. handle->channel = xfer->channel;
  503. handle->txData = xfer->txData;
  504. handle->rxData = xfer->rxData;
  505. handle->transferSize = xfer->dataSize;
  506. handle->txRemainingBytes = xfer->dataSize;
  507. handle->rxRemainingBytes = xfer->dataSize;
  508. /* Set the ECSPI state to busy */
  509. handle->state = kECSPI_Busy;
  510. /* Select ECSPI channel to current channel
  511. * Note:
  512. * xfer.channel must be configured before transferfer, because every channel has
  513. * it's own configuration, if don't configure this parameter, transfer channel
  514. * will use the default channel0.
  515. */
  516. ECSPI_SetChannelSelect(base, xfer->channel);
  517. /* First send data to Tx FIFO to start a ECSPI transfer */
  518. ECSPI_SendTransfer(base, handle);
  519. if (NULL != xfer->rxData)
  520. {
  521. /* Enable Rx data request interrupt and receive overflow interrupt, when data in RX FIFO buffer is greater
  522. * than the RX_THRESHOLD, then a interrupt occurred. Only enable Rx interrupt,
  523. * use rx interrupt to driver ECSPI transfer.
  524. */
  525. ECSPI_EnableInterrupts(base, kECSPI_RxFifoReadyInterruptEnable | kECSPI_RxFifoOverFlowInterruptEnable);
  526. }
  527. else
  528. {
  529. /* Enable Tx data request interrupt, when data in TX FIFO buffer is greater
  530. * than the TX_THRESHOLD, then a interrupt occurred.
  531. */
  532. ECSPI_EnableInterrupts(base, kECSPI_TxFifoDataRequstInterruptEnable);
  533. }
  534. return kStatus_Success;
  535. }
  536. status_t ECSPI_MasterTransferGetCount(ECSPI_Type *base, ecspi_master_handle_t *handle, size_t *count)
  537. {
  538. assert(handle);
  539. status_t status = kStatus_Success;
  540. if (handle->state != kStatus_ECSPI_Busy)
  541. {
  542. status = kStatus_NoTransferInProgress;
  543. }
  544. else
  545. {
  546. /* Return remaing bytes in different cases */
  547. if (handle->rxData)
  548. {
  549. *count = handle->transferSize - handle->rxRemainingBytes;
  550. }
  551. else
  552. {
  553. *count = handle->transferSize - handle->txRemainingBytes;
  554. }
  555. }
  556. return status;
  557. }
  558. void ECSPI_MasterTransferAbort(ECSPI_Type *base, ecspi_master_handle_t *handle)
  559. {
  560. assert(handle);
  561. /* Stop interrupts */
  562. if (NULL != handle->rxData)
  563. {
  564. ECSPI_DisableInterrupts(base, kECSPI_RxFifoReadyInterruptEnable | kECSPI_RxFifoOverFlowInterruptEnable);
  565. }
  566. else
  567. {
  568. ECSPI_DisableInterrupts(base, kECSPI_TxFifoDataRequstInterruptEnable);
  569. }
  570. /* Transfer finished, set the state to Done*/
  571. handle->state = kECSPI_Idle;
  572. /* Clear the internal state */
  573. handle->rxRemainingBytes = 0;
  574. handle->txRemainingBytes = 0;
  575. }
  576. void ECSPI_MasterTransferHandleIRQ(ECSPI_Type *base, ecspi_master_handle_t *handle)
  577. {
  578. assert(handle);
  579. /* If hardware overflow happens */
  580. if (base->STATREG & ECSPI_STATREG_RO_MASK)
  581. {
  582. /* Clear overflow flag for next transfer */
  583. ECSPI_ClearStatusFlags(base, kECSPI_RxFifoOverFlowFlag);
  584. if (handle->callback)
  585. {
  586. (handle->callback)(base, handle, kStatus_ECSPI_HardwareOverFlow, handle->userData);
  587. }
  588. }
  589. /* If need to receive data, do a receive */
  590. if (handle->rxRemainingBytes)
  591. {
  592. ECSPI_ReceiveTransfer(base, handle);
  593. }
  594. /* We always need to send a data to make the ECSPI run */
  595. if (handle->txRemainingBytes)
  596. {
  597. ECSPI_SendTransfer(base, handle);
  598. }
  599. /* All the transfer finished */
  600. if ((handle->txRemainingBytes == 0) && (handle->rxRemainingBytes == 0))
  601. {
  602. /* Complete the transfer */
  603. ECSPI_MasterTransferAbort(base, handle);
  604. if (handle->callback)
  605. {
  606. (handle->callback)(base, handle, kStatus_Success, handle->userData);
  607. }
  608. }
  609. }
  610. void ECSPI_SlaveTransferCreateHandle(ECSPI_Type *base,
  611. ecspi_slave_handle_t *handle,
  612. ecspi_slave_callback_t callback,
  613. void *userData)
  614. {
  615. assert(handle);
  616. /* Slave create handle share same logic with master create handle, the only difference
  617. is the Isr pointer. */
  618. ECSPI_MasterTransferCreateHandle(base, handle, callback, userData);
  619. s_ecspiSlaveIsr = ECSPI_SlaveTransferHandleIRQ;
  620. }
  621. void ECSPI_SlaveTransferHandleIRQ(ECSPI_Type *base, ecspi_slave_handle_t *handle)
  622. {
  623. assert(handle);
  624. /* If hardware overflow happens */
  625. if (base->STATREG & ECSPI_STATREG_RO_MASK)
  626. {
  627. /* Clear overflow flag for next transfer */
  628. ECSPI_ClearStatusFlags(base, kECSPI_RxFifoOverFlowFlag);
  629. if (handle->callback)
  630. {
  631. (handle->callback)(base, handle, kStatus_ECSPI_HardwareOverFlow, handle->userData);
  632. }
  633. }
  634. /* If needs to receive data, do a receive */
  635. if (handle->rxRemainingBytes)
  636. {
  637. ECSPI_ReceiveTransfer(base, handle);
  638. }
  639. /* We always need to send a data to make the ECSPI run */
  640. if (handle->txRemainingBytes)
  641. {
  642. ECSPI_SendTransfer(base, handle);
  643. }
  644. /* All the transfer finished */
  645. if ((handle->txRemainingBytes == 0) && (handle->rxRemainingBytes == 0))
  646. {
  647. /* Complete the transfer */
  648. ECSPI_SlaveTransferAbort(base, handle);
  649. if (handle->callback)
  650. {
  651. (handle->callback)(base, handle, kStatus_Success, handle->userData);
  652. }
  653. }
  654. }
  655. static void ECSPI_CommonIRQHandler(ECSPI_Type *base, ecspi_master_handle_t *handle)
  656. {
  657. if (ECSPI_IsMaster(base, handle->channel))
  658. {
  659. s_ecspiMasterIsr(base, handle);
  660. }
  661. else
  662. {
  663. s_ecspiSlaveIsr(base, handle);
  664. }
  665. }
  666. #if defined(ECSPI1)
  667. void ECSPI1_DriverIRQHandler(void)
  668. {
  669. assert(s_ecspiHandle[1]);
  670. ECSPI_CommonIRQHandler(ECSPI1, s_ecspiHandle[1]);
  671. }
  672. #endif /* ECSPI1 */
  673. #if defined(ECSPI2)
  674. void ECSPI2_DriverIRQHandler(void)
  675. {
  676. assert(s_ecspiHandle[2]);
  677. ECSPI_CommonIRQHandler(ECSPI2, s_ecspiHandle[2]);
  678. }
  679. #endif /* ECSPI2 */
  680. #if defined(ECSPI3)
  681. void ECSPI3_DriverIRQHandler(void)
  682. {
  683. assert(s_ecspiHandle[3]);
  684. ECSPI_CommonIRQHandler(ECSPI3, s_ecspiHandle[3]);
  685. }
  686. #endif /* ECSPI3 */
  687. #if defined(ECSPI4)
  688. void ECSPI4_DriverIRQHandler(void)
  689. {
  690. assert(s_ecspiHandle[4]);
  691. ECSPI_CommonIRQHandler(ECSPI4, s_ecspiHandle[4]);
  692. }
  693. #endif /* ECSPI4 */