fsl_flexio_spi.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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_flexio_spi.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.flexio_spi"
  41. #endif
  42. /*! @brief FLEXIO SPI transfer state, which is used for SPI transactiaonl APIs' internal state. */
  43. enum _flexio_spi_transfer_states
  44. {
  45. kFLEXIO_SPI_Idle = 0x0U, /*!< Nothing in the transmitter/receiver's queue. */
  46. kFLEXIO_SPI_Busy, /*!< Transmiter/Receive's queue is not finished. */
  47. };
  48. /*******************************************************************************
  49. * Prototypes
  50. ******************************************************************************/
  51. /*!
  52. * @brief Send a piece of data for SPI.
  53. *
  54. * This function computes the number of data to be written into D register or Tx FIFO,
  55. * and write the data into it. At the same time, this function updates the values in
  56. * master handle structure.
  57. *
  58. * @param base pointer to FLEXIO_SPI_Type structure
  59. * @param handle Pointer to SPI master handle structure.
  60. */
  61. static void FLEXIO_SPI_TransferSendTransaction(FLEXIO_SPI_Type *base, flexio_spi_master_handle_t *handle);
  62. /*!
  63. * @brief Receive a piece of data for SPI master.
  64. *
  65. * This function computes the number of data to receive from D register or Rx FIFO,
  66. * and write the data to destination address. At the same time, this function updates
  67. * the values in master handle structure.
  68. *
  69. * @param base pointer to FLEXIO_SPI_Type structure
  70. * @param handle Pointer to SPI master handle structure.
  71. */
  72. static void FLEXIO_SPI_TransferReceiveTransaction(FLEXIO_SPI_Type *base, flexio_spi_master_handle_t *handle);
  73. /*******************************************************************************
  74. * Variables
  75. ******************************************************************************/
  76. /*******************************************************************************
  77. * Codes
  78. ******************************************************************************/
  79. static uint32_t FLEXIO_SPI_GetInstance(FLEXIO_SPI_Type *base)
  80. {
  81. return FLEXIO_GetInstance(base->flexioBase);
  82. }
  83. static void FLEXIO_SPI_TransferSendTransaction(FLEXIO_SPI_Type *base, flexio_spi_master_handle_t *handle)
  84. {
  85. uint16_t tmpData = FLEXIO_SPI_DUMMYDATA;
  86. if (handle->txData != NULL)
  87. {
  88. /* Transmit data and update tx size/buff. */
  89. if (handle->bytePerFrame == 1U)
  90. {
  91. tmpData = *(handle->txData);
  92. handle->txData++;
  93. }
  94. else
  95. {
  96. if (handle->direction == kFLEXIO_SPI_MsbFirst)
  97. {
  98. tmpData = (uint32_t)(handle->txData[0]) << 8U;
  99. tmpData += handle->txData[1];
  100. }
  101. else
  102. {
  103. tmpData = (uint32_t)(handle->txData[1]) << 8U;
  104. tmpData += handle->txData[0];
  105. }
  106. handle->txData += 2U;
  107. }
  108. }
  109. else
  110. {
  111. tmpData = FLEXIO_SPI_DUMMYDATA;
  112. }
  113. handle->txRemainingBytes -= handle->bytePerFrame;
  114. FLEXIO_SPI_WriteData(base, handle->direction, tmpData);
  115. if (!handle->txRemainingBytes)
  116. {
  117. FLEXIO_SPI_DisableInterrupts(base, kFLEXIO_SPI_TxEmptyInterruptEnable);
  118. }
  119. }
  120. static void FLEXIO_SPI_TransferReceiveTransaction(FLEXIO_SPI_Type *base, flexio_spi_master_handle_t *handle)
  121. {
  122. uint16_t tmpData;
  123. tmpData = FLEXIO_SPI_ReadData(base, handle->direction);
  124. if (handle->rxData != NULL)
  125. {
  126. if (handle->bytePerFrame == 1U)
  127. {
  128. *handle->rxData = tmpData;
  129. handle->rxData++;
  130. }
  131. else
  132. {
  133. if (handle->direction == kFLEXIO_SPI_MsbFirst)
  134. {
  135. *((uint16_t *)(handle->rxData)) = tmpData;
  136. }
  137. else
  138. {
  139. *((uint16_t *)(handle->rxData)) = (((tmpData << 8) & 0xff00U) | ((tmpData >> 8) & 0x00ffU));
  140. }
  141. handle->rxData += 2U;
  142. }
  143. }
  144. handle->rxRemainingBytes -= handle->bytePerFrame;
  145. }
  146. void FLEXIO_SPI_MasterInit(FLEXIO_SPI_Type *base, flexio_spi_master_config_t *masterConfig, uint32_t srcClock_Hz)
  147. {
  148. assert(base);
  149. assert(masterConfig);
  150. flexio_shifter_config_t shifterConfig;
  151. flexio_timer_config_t timerConfig;
  152. uint32_t ctrlReg = 0;
  153. uint16_t timerDiv = 0;
  154. uint16_t timerCmp = 0;
  155. /* Clear the shifterConfig & timerConfig struct. */
  156. memset(&shifterConfig, 0, sizeof(shifterConfig));
  157. memset(&timerConfig, 0, sizeof(timerConfig));
  158. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  159. /* Ungate flexio clock. */
  160. CLOCK_EnableClock(s_flexioClocks[FLEXIO_SPI_GetInstance(base)]);
  161. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  162. /* Configure FLEXIO SPI Master */
  163. ctrlReg = base->flexioBase->CTRL;
  164. ctrlReg &= ~(FLEXIO_CTRL_DOZEN_MASK | FLEXIO_CTRL_DBGE_MASK | FLEXIO_CTRL_FASTACC_MASK | FLEXIO_CTRL_FLEXEN_MASK);
  165. ctrlReg |= (FLEXIO_CTRL_DBGE(masterConfig->enableInDebug) | FLEXIO_CTRL_FASTACC(masterConfig->enableFastAccess) |
  166. FLEXIO_CTRL_FLEXEN(masterConfig->enableMaster));
  167. if (!masterConfig->enableInDoze)
  168. {
  169. ctrlReg |= FLEXIO_CTRL_DOZEN_MASK;
  170. }
  171. base->flexioBase->CTRL = ctrlReg;
  172. /* Do hardware configuration. */
  173. /* 1. Configure the shifter 0 for tx. */
  174. shifterConfig.timerSelect = base->timerIndex[0];
  175. shifterConfig.pinConfig = kFLEXIO_PinConfigOutput;
  176. shifterConfig.pinSelect = base->SDOPinIndex;
  177. shifterConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  178. shifterConfig.shifterMode = kFLEXIO_ShifterModeTransmit;
  179. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  180. if (masterConfig->phase == kFLEXIO_SPI_ClockPhaseFirstEdge)
  181. {
  182. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
  183. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
  184. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  185. }
  186. else
  187. {
  188. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
  189. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitLow;
  190. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnShift;
  191. }
  192. FLEXIO_SetShifterConfig(base->flexioBase, base->shifterIndex[0], &shifterConfig);
  193. /* 2. Configure the shifter 1 for rx. */
  194. shifterConfig.timerSelect = base->timerIndex[0];
  195. shifterConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  196. shifterConfig.pinSelect = base->SDIPinIndex;
  197. shifterConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  198. shifterConfig.shifterMode = kFLEXIO_ShifterModeReceive;
  199. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  200. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
  201. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  202. if (masterConfig->phase == kFLEXIO_SPI_ClockPhaseFirstEdge)
  203. {
  204. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
  205. }
  206. else
  207. {
  208. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
  209. }
  210. FLEXIO_SetShifterConfig(base->flexioBase, base->shifterIndex[1], &shifterConfig);
  211. /*3. Configure the timer 0 for SCK. */
  212. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_SHIFTnSTAT(base->shifterIndex[0]);
  213. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveLow;
  214. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  215. timerConfig.pinConfig = kFLEXIO_PinConfigOutput;
  216. timerConfig.pinSelect = base->SCKPinIndex;
  217. timerConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  218. timerConfig.timerMode = kFLEXIO_TimerModeDual8BitBaudBit;
  219. timerConfig.timerOutput = kFLEXIO_TimerOutputZeroNotAffectedByReset;
  220. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  221. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  222. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompare;
  223. timerConfig.timerEnable = kFLEXIO_TimerEnableOnTriggerHigh;
  224. timerConfig.timerStop = kFLEXIO_TimerStopBitEnableOnTimerDisable;
  225. timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
  226. timerDiv = srcClock_Hz / masterConfig->baudRate_Bps;
  227. timerDiv = timerDiv / 2 - 1;
  228. timerCmp = ((uint32_t)(masterConfig->dataMode * 2 - 1U)) << 8U;
  229. timerCmp |= timerDiv;
  230. timerConfig.timerCompare = timerCmp;
  231. FLEXIO_SetTimerConfig(base->flexioBase, base->timerIndex[0], &timerConfig);
  232. /* 4. Configure the timer 1 for CSn. */
  233. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_TIMn(base->timerIndex[0]);
  234. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
  235. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  236. timerConfig.pinConfig = kFLEXIO_PinConfigOutput;
  237. timerConfig.pinSelect = base->CSnPinIndex;
  238. timerConfig.pinPolarity = kFLEXIO_PinActiveLow;
  239. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  240. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  241. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  242. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  243. timerConfig.timerDisable = kFLEXIO_TimerDisableOnPreTimerDisable;
  244. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPrevTimerEnable;
  245. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  246. timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
  247. timerConfig.timerCompare = 0xFFFFU;
  248. FLEXIO_SetTimerConfig(base->flexioBase, base->timerIndex[1], &timerConfig);
  249. }
  250. void FLEXIO_SPI_MasterDeinit(FLEXIO_SPI_Type *base)
  251. {
  252. base->flexioBase->SHIFTCFG[base->shifterIndex[0]] = 0;
  253. base->flexioBase->SHIFTCTL[base->shifterIndex[0]] = 0;
  254. base->flexioBase->SHIFTCFG[base->shifterIndex[1]] = 0;
  255. base->flexioBase->SHIFTCTL[base->shifterIndex[1]] = 0;
  256. base->flexioBase->TIMCFG[base->timerIndex[0]] = 0;
  257. base->flexioBase->TIMCMP[base->timerIndex[0]] = 0;
  258. base->flexioBase->TIMCTL[base->timerIndex[0]] = 0;
  259. base->flexioBase->TIMCFG[base->timerIndex[1]] = 0;
  260. base->flexioBase->TIMCMP[base->timerIndex[1]] = 0;
  261. base->flexioBase->TIMCTL[base->timerIndex[1]] = 0;
  262. }
  263. void FLEXIO_SPI_MasterGetDefaultConfig(flexio_spi_master_config_t *masterConfig)
  264. {
  265. assert(masterConfig);
  266. masterConfig->enableMaster = true;
  267. masterConfig->enableInDoze = false;
  268. masterConfig->enableInDebug = true;
  269. masterConfig->enableFastAccess = false;
  270. /* Default baud rate 500kbps. */
  271. masterConfig->baudRate_Bps = 500000U;
  272. /* Default CPHA = 0. */
  273. masterConfig->phase = kFLEXIO_SPI_ClockPhaseFirstEdge;
  274. /* Default bit count at 8. */
  275. masterConfig->dataMode = kFLEXIO_SPI_8BitMode;
  276. }
  277. void FLEXIO_SPI_SlaveInit(FLEXIO_SPI_Type *base, flexio_spi_slave_config_t *slaveConfig)
  278. {
  279. assert(base && slaveConfig);
  280. flexio_shifter_config_t shifterConfig;
  281. flexio_timer_config_t timerConfig;
  282. uint32_t ctrlReg = 0;
  283. /* Clear the shifterConfig & timerConfig struct. */
  284. memset(&shifterConfig, 0, sizeof(shifterConfig));
  285. memset(&timerConfig, 0, sizeof(timerConfig));
  286. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  287. /* Ungate flexio clock. */
  288. CLOCK_EnableClock(s_flexioClocks[FLEXIO_SPI_GetInstance(base)]);
  289. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  290. /* Configure FLEXIO SPI Slave */
  291. ctrlReg = base->flexioBase->CTRL;
  292. ctrlReg &= ~(FLEXIO_CTRL_DOZEN_MASK | FLEXIO_CTRL_DBGE_MASK | FLEXIO_CTRL_FASTACC_MASK | FLEXIO_CTRL_FLEXEN_MASK);
  293. ctrlReg |= (FLEXIO_CTRL_DBGE(slaveConfig->enableInDebug) | FLEXIO_CTRL_FASTACC(slaveConfig->enableFastAccess) |
  294. FLEXIO_CTRL_FLEXEN(slaveConfig->enableSlave));
  295. if (!slaveConfig->enableInDoze)
  296. {
  297. ctrlReg |= FLEXIO_CTRL_DOZEN_MASK;
  298. }
  299. base->flexioBase->CTRL = ctrlReg;
  300. /* Do hardware configuration. */
  301. /* 1. Configure the shifter 0 for tx. */
  302. shifterConfig.timerSelect = base->timerIndex[0];
  303. shifterConfig.pinConfig = kFLEXIO_PinConfigOutput;
  304. shifterConfig.pinSelect = base->SDOPinIndex;
  305. shifterConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  306. shifterConfig.shifterMode = kFLEXIO_ShifterModeTransmit;
  307. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  308. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
  309. if (slaveConfig->phase == kFLEXIO_SPI_ClockPhaseFirstEdge)
  310. {
  311. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
  312. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  313. }
  314. else
  315. {
  316. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
  317. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnShift;
  318. }
  319. FLEXIO_SetShifterConfig(base->flexioBase, base->shifterIndex[0], &shifterConfig);
  320. /* 2. Configure the shifter 1 for rx. */
  321. shifterConfig.timerSelect = base->timerIndex[0];
  322. shifterConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  323. shifterConfig.pinSelect = base->SDIPinIndex;
  324. shifterConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  325. shifterConfig.shifterMode = kFLEXIO_ShifterModeReceive;
  326. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  327. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
  328. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
  329. if (slaveConfig->phase == kFLEXIO_SPI_ClockPhaseFirstEdge)
  330. {
  331. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
  332. }
  333. else
  334. {
  335. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
  336. }
  337. FLEXIO_SetShifterConfig(base->flexioBase, base->shifterIndex[1], &shifterConfig);
  338. /*3. Configure the timer 0 for shift clock. */
  339. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->CSnPinIndex);
  340. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveLow;
  341. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  342. timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  343. timerConfig.pinSelect = base->SCKPinIndex;
  344. timerConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  345. timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
  346. timerConfig.timerOutput = kFLEXIO_TimerOutputZeroNotAffectedByReset;
  347. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnPinInputShiftPinInput;
  348. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  349. timerConfig.timerEnable = kFLEXIO_TimerEnableOnTriggerRisingEdge;
  350. timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
  351. if (slaveConfig->phase == kFLEXIO_SPI_ClockPhaseFirstEdge)
  352. {
  353. /* The configuration kFLEXIO_TimerDisableOnTimerCompare only support continuous
  354. PCS access, change to kFLEXIO_TimerDisableNever to enable discontinuous PCS access. */
  355. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompare;
  356. timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
  357. }
  358. else
  359. {
  360. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTriggerFallingEdge;
  361. timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
  362. }
  363. timerConfig.timerCompare = slaveConfig->dataMode * 2 - 1U;
  364. FLEXIO_SetTimerConfig(base->flexioBase, base->timerIndex[0], &timerConfig);
  365. }
  366. void FLEXIO_SPI_SlaveDeinit(FLEXIO_SPI_Type *base)
  367. {
  368. FLEXIO_SPI_MasterDeinit(base);
  369. }
  370. void FLEXIO_SPI_SlaveGetDefaultConfig(flexio_spi_slave_config_t *slaveConfig)
  371. {
  372. assert(slaveConfig);
  373. slaveConfig->enableSlave = true;
  374. slaveConfig->enableInDoze = false;
  375. slaveConfig->enableInDebug = true;
  376. slaveConfig->enableFastAccess = false;
  377. /* Default CPHA = 0. */
  378. slaveConfig->phase = kFLEXIO_SPI_ClockPhaseFirstEdge;
  379. /* Default bit count at 8. */
  380. slaveConfig->dataMode = kFLEXIO_SPI_8BitMode;
  381. }
  382. void FLEXIO_SPI_EnableInterrupts(FLEXIO_SPI_Type *base, uint32_t mask)
  383. {
  384. if (mask & kFLEXIO_SPI_TxEmptyInterruptEnable)
  385. {
  386. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1 << base->shifterIndex[0]);
  387. }
  388. if (mask & kFLEXIO_SPI_RxFullInterruptEnable)
  389. {
  390. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1 << base->shifterIndex[1]);
  391. }
  392. }
  393. void FLEXIO_SPI_DisableInterrupts(FLEXIO_SPI_Type *base, uint32_t mask)
  394. {
  395. if (mask & kFLEXIO_SPI_TxEmptyInterruptEnable)
  396. {
  397. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1 << base->shifterIndex[0]);
  398. }
  399. if (mask & kFLEXIO_SPI_RxFullInterruptEnable)
  400. {
  401. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1 << base->shifterIndex[1]);
  402. }
  403. }
  404. void FLEXIO_SPI_EnableDMA(FLEXIO_SPI_Type *base, uint32_t mask, bool enable)
  405. {
  406. if (mask & kFLEXIO_SPI_TxDmaEnable)
  407. {
  408. FLEXIO_EnableShifterStatusDMA(base->flexioBase, 1U << base->shifterIndex[0], enable);
  409. }
  410. if (mask & kFLEXIO_SPI_RxDmaEnable)
  411. {
  412. FLEXIO_EnableShifterStatusDMA(base->flexioBase, 1U << base->shifterIndex[1], enable);
  413. }
  414. }
  415. uint32_t FLEXIO_SPI_GetStatusFlags(FLEXIO_SPI_Type *base)
  416. {
  417. uint32_t shifterStatus = FLEXIO_GetShifterStatusFlags(base->flexioBase);
  418. uint32_t status = 0;
  419. status = ((shifterStatus & (1U << base->shifterIndex[0])) >> base->shifterIndex[0]);
  420. status |= (((shifterStatus & (1U << base->shifterIndex[1])) >> (base->shifterIndex[1])) << 1U);
  421. return status;
  422. }
  423. void FLEXIO_SPI_ClearStatusFlags(FLEXIO_SPI_Type *base, uint32_t mask)
  424. {
  425. if (mask & kFLEXIO_SPI_TxBufferEmptyFlag)
  426. {
  427. FLEXIO_ClearShifterStatusFlags(base->flexioBase, 1U << base->shifterIndex[0]);
  428. }
  429. if (mask & kFLEXIO_SPI_RxBufferFullFlag)
  430. {
  431. FLEXIO_ClearShifterStatusFlags(base->flexioBase, 1U << base->shifterIndex[1]);
  432. }
  433. }
  434. void FLEXIO_SPI_MasterSetBaudRate(FLEXIO_SPI_Type *base, uint32_t baudRate_Bps, uint32_t srcClockHz)
  435. {
  436. uint16_t timerDiv = 0;
  437. uint16_t timerCmp = 0;
  438. FLEXIO_Type *flexioBase = base->flexioBase;
  439. /* Set TIMCMP[7:0] = (baud rate divider / 2) - 1.*/
  440. timerDiv = srcClockHz / baudRate_Bps;
  441. timerDiv = timerDiv / 2 - 1U;
  442. timerCmp = flexioBase->TIMCMP[base->timerIndex[0]];
  443. timerCmp &= 0xFF00U;
  444. timerCmp |= timerDiv;
  445. flexioBase->TIMCMP[base->timerIndex[0]] = timerCmp;
  446. }
  447. void FLEXIO_SPI_WriteBlocking(FLEXIO_SPI_Type *base,
  448. flexio_spi_shift_direction_t direction,
  449. const uint8_t *buffer,
  450. size_t size)
  451. {
  452. assert(buffer);
  453. assert(size);
  454. while (size--)
  455. {
  456. /* Wait until data transfer complete. */
  457. while (!(FLEXIO_SPI_GetStatusFlags(base) & kFLEXIO_SPI_TxBufferEmptyFlag))
  458. {
  459. }
  460. FLEXIO_SPI_WriteData(base, direction, *buffer++);
  461. }
  462. }
  463. void FLEXIO_SPI_ReadBlocking(FLEXIO_SPI_Type *base,
  464. flexio_spi_shift_direction_t direction,
  465. uint8_t *buffer,
  466. size_t size)
  467. {
  468. assert(buffer);
  469. assert(size);
  470. while (size--)
  471. {
  472. /* Wait until data transfer complete. */
  473. while (!(FLEXIO_SPI_GetStatusFlags(base) & kFLEXIO_SPI_RxBufferFullFlag))
  474. {
  475. }
  476. *buffer++ = FLEXIO_SPI_ReadData(base, direction);
  477. }
  478. }
  479. void FLEXIO_SPI_MasterTransferBlocking(FLEXIO_SPI_Type *base, flexio_spi_transfer_t *xfer)
  480. {
  481. flexio_spi_shift_direction_t direction;
  482. uint8_t bytesPerFrame;
  483. uint32_t dataMode = 0;
  484. uint16_t timerCmp = base->flexioBase->TIMCMP[base->timerIndex[0]];
  485. uint16_t tmpData = FLEXIO_SPI_DUMMYDATA;
  486. timerCmp &= 0x00FFU;
  487. /* Configure the values in handle. */
  488. switch (xfer->flags)
  489. {
  490. case kFLEXIO_SPI_8bitMsb:
  491. dataMode = (8 * 2 - 1U) << 8U;
  492. bytesPerFrame = 1;
  493. direction = kFLEXIO_SPI_MsbFirst;
  494. break;
  495. case kFLEXIO_SPI_8bitLsb:
  496. dataMode = (8 * 2 - 1U) << 8U;
  497. bytesPerFrame = 1;
  498. direction = kFLEXIO_SPI_LsbFirst;
  499. break;
  500. case kFLEXIO_SPI_16bitMsb:
  501. dataMode = (16 * 2 - 1U) << 8U;
  502. bytesPerFrame = 2;
  503. direction = kFLEXIO_SPI_MsbFirst;
  504. break;
  505. case kFLEXIO_SPI_16bitLsb:
  506. dataMode = (16 * 2 - 1U) << 8U;
  507. bytesPerFrame = 2;
  508. direction = kFLEXIO_SPI_LsbFirst;
  509. break;
  510. default:
  511. dataMode = (8 * 2 - 1U) << 8U;
  512. bytesPerFrame = 1;
  513. direction = kFLEXIO_SPI_MsbFirst;
  514. assert(true);
  515. break;
  516. }
  517. dataMode |= timerCmp;
  518. /* Configure transfer size. */
  519. base->flexioBase->TIMCMP[base->timerIndex[0]] = dataMode;
  520. while (xfer->dataSize)
  521. {
  522. /* Wait until data transfer complete. */
  523. while (!(FLEXIO_SPI_GetStatusFlags(base) & kFLEXIO_SPI_TxBufferEmptyFlag))
  524. {
  525. }
  526. if (xfer->txData != NULL)
  527. {
  528. /* Transmit data and update tx size/buff. */
  529. if (bytesPerFrame == 1U)
  530. {
  531. tmpData = *(xfer->txData);
  532. xfer->txData++;
  533. }
  534. else
  535. {
  536. if (direction == kFLEXIO_SPI_MsbFirst)
  537. {
  538. tmpData = (uint32_t)(xfer->txData[0]) << 8U;
  539. tmpData += xfer->txData[1];
  540. }
  541. else
  542. {
  543. tmpData = (uint32_t)(xfer->txData[1]) << 8U;
  544. tmpData += xfer->txData[0];
  545. }
  546. xfer->txData += 2U;
  547. }
  548. }
  549. else
  550. {
  551. tmpData = FLEXIO_SPI_DUMMYDATA;
  552. }
  553. xfer->dataSize -= bytesPerFrame;
  554. FLEXIO_SPI_WriteData(base, direction, tmpData);
  555. while (!(FLEXIO_SPI_GetStatusFlags(base) & kFLEXIO_SPI_RxBufferFullFlag))
  556. {
  557. }
  558. tmpData = FLEXIO_SPI_ReadData(base, direction);
  559. if (xfer->rxData != NULL)
  560. {
  561. if (bytesPerFrame == 1U)
  562. {
  563. *xfer->rxData = tmpData;
  564. xfer->rxData++;
  565. }
  566. else
  567. {
  568. if (direction == kFLEXIO_SPI_MsbFirst)
  569. {
  570. *((uint16_t *)(xfer->rxData)) = tmpData;
  571. }
  572. else
  573. {
  574. *((uint16_t *)(xfer->rxData)) = (((tmpData << 8) & 0xff00U) | ((tmpData >> 8) & 0x00ffU));
  575. }
  576. xfer->rxData += 2U;
  577. }
  578. }
  579. }
  580. }
  581. status_t FLEXIO_SPI_MasterTransferCreateHandle(FLEXIO_SPI_Type *base,
  582. flexio_spi_master_handle_t *handle,
  583. flexio_spi_master_transfer_callback_t callback,
  584. void *userData)
  585. {
  586. assert(handle);
  587. IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
  588. /* Zero the handle. */
  589. memset(handle, 0, sizeof(*handle));
  590. /* Register callback and userData. */
  591. handle->callback = callback;
  592. handle->userData = userData;
  593. /* Enable interrupt in NVIC. */
  594. EnableIRQ(flexio_irqs[FLEXIO_SPI_GetInstance(base)]);
  595. /* Save the context in global variables to support the double weak mechanism. */
  596. return FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_SPI_MasterTransferHandleIRQ);
  597. }
  598. status_t FLEXIO_SPI_MasterTransferNonBlocking(FLEXIO_SPI_Type *base,
  599. flexio_spi_master_handle_t *handle,
  600. flexio_spi_transfer_t *xfer)
  601. {
  602. assert(handle);
  603. assert(xfer);
  604. uint32_t dataMode = 0;
  605. uint16_t timerCmp = base->flexioBase->TIMCMP[base->timerIndex[0]];
  606. uint16_t tmpData = FLEXIO_SPI_DUMMYDATA;
  607. timerCmp &= 0x00FFU;
  608. /* Check if SPI is busy. */
  609. if (handle->state == kFLEXIO_SPI_Busy)
  610. {
  611. return kStatus_FLEXIO_SPI_Busy;
  612. }
  613. /* Check if the argument is legal. */
  614. if ((xfer->txData == NULL) && (xfer->rxData == NULL))
  615. {
  616. return kStatus_InvalidArgument;
  617. }
  618. /* Configure the values in handle */
  619. switch (xfer->flags)
  620. {
  621. case kFLEXIO_SPI_8bitMsb:
  622. dataMode = (8 * 2 - 1U) << 8U;
  623. handle->bytePerFrame = 1U;
  624. handle->direction = kFLEXIO_SPI_MsbFirst;
  625. break;
  626. case kFLEXIO_SPI_8bitLsb:
  627. dataMode = (8 * 2 - 1U) << 8U;
  628. handle->bytePerFrame = 1U;
  629. handle->direction = kFLEXIO_SPI_LsbFirst;
  630. break;
  631. case kFLEXIO_SPI_16bitMsb:
  632. dataMode = (16 * 2 - 1U) << 8U;
  633. handle->bytePerFrame = 2U;
  634. handle->direction = kFLEXIO_SPI_MsbFirst;
  635. break;
  636. case kFLEXIO_SPI_16bitLsb:
  637. dataMode = (16 * 2 - 1U) << 8U;
  638. handle->bytePerFrame = 2U;
  639. handle->direction = kFLEXIO_SPI_LsbFirst;
  640. break;
  641. default:
  642. dataMode = (8 * 2 - 1U) << 8U;
  643. handle->bytePerFrame = 1U;
  644. handle->direction = kFLEXIO_SPI_MsbFirst;
  645. assert(true);
  646. break;
  647. }
  648. dataMode |= timerCmp;
  649. /* Configure transfer size. */
  650. base->flexioBase->TIMCMP[base->timerIndex[0]] = dataMode;
  651. handle->state = kFLEXIO_SPI_Busy;
  652. handle->txData = xfer->txData;
  653. handle->rxData = xfer->rxData;
  654. handle->rxRemainingBytes = xfer->dataSize;
  655. /* Save total transfer size. */
  656. handle->transferSize = xfer->dataSize;
  657. /* Send first byte of data to trigger the rx interrupt. */
  658. if (handle->txData != NULL)
  659. {
  660. /* Transmit data and update tx size/buff. */
  661. if (handle->bytePerFrame == 1U)
  662. {
  663. tmpData = *(handle->txData);
  664. handle->txData++;
  665. }
  666. else
  667. {
  668. if (handle->direction == kFLEXIO_SPI_MsbFirst)
  669. {
  670. tmpData = (uint32_t)(handle->txData[0]) << 8U;
  671. tmpData += handle->txData[1];
  672. }
  673. else
  674. {
  675. tmpData = (uint32_t)(handle->txData[1]) << 8U;
  676. tmpData += handle->txData[0];
  677. }
  678. handle->txData += 2U;
  679. }
  680. }
  681. else
  682. {
  683. tmpData = FLEXIO_SPI_DUMMYDATA;
  684. }
  685. handle->txRemainingBytes = xfer->dataSize - handle->bytePerFrame;
  686. FLEXIO_SPI_WriteData(base, handle->direction, tmpData);
  687. /* Enable transmit and receive interrupt to handle rx. */
  688. FLEXIO_SPI_EnableInterrupts(base, kFLEXIO_SPI_RxFullInterruptEnable);
  689. return kStatus_Success;
  690. }
  691. status_t FLEXIO_SPI_MasterTransferGetCount(FLEXIO_SPI_Type *base, flexio_spi_master_handle_t *handle, size_t *count)
  692. {
  693. assert(handle);
  694. if (!count)
  695. {
  696. return kStatus_InvalidArgument;
  697. }
  698. /* Return remaing bytes in different cases. */
  699. if (handle->rxData)
  700. {
  701. *count = handle->transferSize - handle->rxRemainingBytes;
  702. }
  703. else
  704. {
  705. *count = handle->transferSize - handle->txRemainingBytes;
  706. }
  707. return kStatus_Success;
  708. }
  709. void FLEXIO_SPI_MasterTransferAbort(FLEXIO_SPI_Type *base, flexio_spi_master_handle_t *handle)
  710. {
  711. assert(handle);
  712. FLEXIO_SPI_DisableInterrupts(base, kFLEXIO_SPI_RxFullInterruptEnable);
  713. FLEXIO_SPI_DisableInterrupts(base, kFLEXIO_SPI_TxEmptyInterruptEnable);
  714. /* Transfer finished, set the state to idle. */
  715. handle->state = kFLEXIO_SPI_Idle;
  716. /* Clear the internal state. */
  717. handle->rxRemainingBytes = 0;
  718. handle->txRemainingBytes = 0;
  719. }
  720. void FLEXIO_SPI_MasterTransferHandleIRQ(void *spiType, void *spiHandle)
  721. {
  722. assert(spiHandle);
  723. flexio_spi_master_handle_t *handle = (flexio_spi_master_handle_t *)spiHandle;
  724. FLEXIO_SPI_Type *base;
  725. uint32_t status;
  726. if (handle->state == kFLEXIO_SPI_Idle)
  727. {
  728. return;
  729. }
  730. base = (FLEXIO_SPI_Type *)spiType;
  731. status = FLEXIO_SPI_GetStatusFlags(base);
  732. /* Handle rx. */
  733. if ((status & kFLEXIO_SPI_RxBufferFullFlag) && (handle->rxRemainingBytes))
  734. {
  735. FLEXIO_SPI_TransferReceiveTransaction(base, handle);
  736. }
  737. /* Handle tx. */
  738. if ((status & kFLEXIO_SPI_TxBufferEmptyFlag) && (handle->txRemainingBytes))
  739. {
  740. FLEXIO_SPI_TransferSendTransaction(base, handle);
  741. }
  742. /* All the transfer finished. */
  743. if ((handle->txRemainingBytes == 0U) && (handle->rxRemainingBytes == 0U))
  744. {
  745. FLEXIO_SPI_MasterTransferAbort(base, handle);
  746. if (handle->callback)
  747. {
  748. (handle->callback)(base, handle, kStatus_FLEXIO_SPI_Idle, handle->userData);
  749. }
  750. }
  751. }
  752. status_t FLEXIO_SPI_SlaveTransferCreateHandle(FLEXIO_SPI_Type *base,
  753. flexio_spi_slave_handle_t *handle,
  754. flexio_spi_slave_transfer_callback_t callback,
  755. void *userData)
  756. {
  757. assert(handle);
  758. IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
  759. /* Zero the handle. */
  760. memset(handle, 0, sizeof(*handle));
  761. /* Register callback and userData. */
  762. handle->callback = callback;
  763. handle->userData = userData;
  764. /* Enable interrupt in NVIC. */
  765. EnableIRQ(flexio_irqs[FLEXIO_SPI_GetInstance(base)]);
  766. /* Save the context in global variables to support the double weak mechanism. */
  767. return FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_SPI_SlaveTransferHandleIRQ);
  768. }
  769. status_t FLEXIO_SPI_SlaveTransferNonBlocking(FLEXIO_SPI_Type *base,
  770. flexio_spi_slave_handle_t *handle,
  771. flexio_spi_transfer_t *xfer)
  772. {
  773. assert(handle);
  774. assert(xfer);
  775. uint32_t dataMode = 0;
  776. /* Check if SPI is busy. */
  777. if (handle->state == kFLEXIO_SPI_Busy)
  778. {
  779. return kStatus_FLEXIO_SPI_Busy;
  780. }
  781. /* Check if the argument is legal. */
  782. if ((xfer->txData == NULL) && (xfer->rxData == NULL))
  783. {
  784. return kStatus_InvalidArgument;
  785. }
  786. /* Configure the values in handle */
  787. switch (xfer->flags)
  788. {
  789. case kFLEXIO_SPI_8bitMsb:
  790. dataMode = 8 * 2 - 1U;
  791. handle->bytePerFrame = 1U;
  792. handle->direction = kFLEXIO_SPI_MsbFirst;
  793. break;
  794. case kFLEXIO_SPI_8bitLsb:
  795. dataMode = 8 * 2 - 1U;
  796. handle->bytePerFrame = 1U;
  797. handle->direction = kFLEXIO_SPI_LsbFirst;
  798. break;
  799. case kFLEXIO_SPI_16bitMsb:
  800. dataMode = 16 * 2 - 1U;
  801. handle->bytePerFrame = 2U;
  802. handle->direction = kFLEXIO_SPI_MsbFirst;
  803. break;
  804. case kFLEXIO_SPI_16bitLsb:
  805. dataMode = 16 * 2 - 1U;
  806. handle->bytePerFrame = 2U;
  807. handle->direction = kFLEXIO_SPI_LsbFirst;
  808. break;
  809. default:
  810. dataMode = 8 * 2 - 1U;
  811. handle->bytePerFrame = 1U;
  812. handle->direction = kFLEXIO_SPI_MsbFirst;
  813. assert(true);
  814. break;
  815. }
  816. /* Configure transfer size. */
  817. base->flexioBase->TIMCMP[base->timerIndex[0]] = dataMode;
  818. handle->state = kFLEXIO_SPI_Busy;
  819. handle->txData = xfer->txData;
  820. handle->rxData = xfer->rxData;
  821. handle->txRemainingBytes = xfer->dataSize;
  822. handle->rxRemainingBytes = xfer->dataSize;
  823. /* Save total transfer size. */
  824. handle->transferSize = xfer->dataSize;
  825. /* Enable transmit and receive interrupt to handle tx and rx. */
  826. FLEXIO_SPI_EnableInterrupts(base, kFLEXIO_SPI_TxEmptyInterruptEnable);
  827. FLEXIO_SPI_EnableInterrupts(base, kFLEXIO_SPI_RxFullInterruptEnable);
  828. return kStatus_Success;
  829. }
  830. void FLEXIO_SPI_SlaveTransferHandleIRQ(void *spiType, void *spiHandle)
  831. {
  832. assert(spiHandle);
  833. flexio_spi_master_handle_t *handle = (flexio_spi_master_handle_t *)spiHandle;
  834. FLEXIO_SPI_Type *base;
  835. uint32_t status;
  836. if (handle->state == kFLEXIO_SPI_Idle)
  837. {
  838. return;
  839. }
  840. base = (FLEXIO_SPI_Type *)spiType;
  841. status = FLEXIO_SPI_GetStatusFlags(base);
  842. /* Handle tx. */
  843. if ((status & kFLEXIO_SPI_TxBufferEmptyFlag) && (handle->txRemainingBytes))
  844. {
  845. FLEXIO_SPI_TransferSendTransaction(base, handle);
  846. }
  847. /* Handle rx. */
  848. if ((status & kFLEXIO_SPI_RxBufferFullFlag) && (handle->rxRemainingBytes))
  849. {
  850. FLEXIO_SPI_TransferReceiveTransaction(base, handle);
  851. }
  852. /* All the transfer finished. */
  853. if ((handle->txRemainingBytes == 0U) && (handle->rxRemainingBytes == 0U))
  854. {
  855. FLEXIO_SPI_SlaveTransferAbort(base, handle);
  856. if (handle->callback)
  857. {
  858. (handle->callback)(base, handle, kStatus_FLEXIO_SPI_Idle, handle->userData);
  859. }
  860. }
  861. }