fsl_flexio_spi.c 34 KB

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