fsl_lpspi.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  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_lpspi.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /*!
  35. * @brief Default watermark values.
  36. *
  37. * The default watermarks are set to zero.
  38. */
  39. enum _lpspi_default_watermarks
  40. {
  41. kLpspiDefaultTxWatermark = 0,
  42. kLpspiDefaultRxWatermark = 0,
  43. };
  44. /*! @brief Typedef for master interrupt handler. */
  45. typedef void (*lpspi_master_isr_t)(LPSPI_Type *base, lpspi_master_handle_t *handle);
  46. /*! @brief Typedef for slave interrupt handler. */
  47. typedef void (*lpspi_slave_isr_t)(LPSPI_Type *base, lpspi_slave_handle_t *handle);
  48. /*******************************************************************************
  49. * Prototypes
  50. ******************************************************************************/
  51. /*!
  52. * @brief Get instance number for LPSPI module.
  53. *
  54. * @param base LPSPI peripheral base address.
  55. */
  56. uint32_t LPSPI_GetInstance(LPSPI_Type *base);
  57. /*!
  58. * @brief Configures the LPSPI peripheral chip select polarity.
  59. *
  60. * This function takes in the desired peripheral chip select (Pcs) and it's corresponding desired polarity and
  61. * configures the Pcs signal to operate with the desired characteristic.
  62. *
  63. * @param base LPSPI peripheral address.
  64. * @param pcs The particular peripheral chip select (parameter value is of type lpspi_which_pcs_t) for which we wish to
  65. * apply the active high or active low characteristic.
  66. * @param activeLowOrHigh The setting for either "active high, inactive low (0)" or "active low, inactive high(1)" of
  67. * type lpspi_pcs_polarity_config_t.
  68. */
  69. static void LPSPI_SetOnePcsPolarity(LPSPI_Type *base,
  70. lpspi_which_pcs_t pcs,
  71. lpspi_pcs_polarity_config_t activeLowOrHigh);
  72. /*!
  73. * @brief Combine the write data for 1 byte to 4 bytes.
  74. * This is not a public API.
  75. */
  76. static uint32_t LPSPI_CombineWriteData(uint8_t *txData, uint32_t bytesEachWrite, bool isByteSwap);
  77. /*!
  78. * @brief Separate the read data for 1 byte to 4 bytes.
  79. * This is not a public API.
  80. */
  81. static void LPSPI_SeparateReadData(uint8_t *rxData, uint32_t readData, uint32_t bytesEachRead, bool isByteSwap);
  82. /*!
  83. * @brief Master fill up the TX FIFO with data.
  84. * This is not a public API.
  85. */
  86. static void LPSPI_MasterTransferFillUpTxFifo(LPSPI_Type *base, lpspi_master_handle_t *handle);
  87. /*!
  88. * @brief Master finish up a transfer.
  89. * It would call back if there is callback function and set the state to idle.
  90. * This is not a public API.
  91. */
  92. static void LPSPI_MasterTransferComplete(LPSPI_Type *base, lpspi_master_handle_t *handle);
  93. /*!
  94. * @brief Slave fill up the TX FIFO with data.
  95. * This is not a public API.
  96. */
  97. static void LPSPI_SlaveTransferFillUpTxFifo(LPSPI_Type *base, lpspi_slave_handle_t *handle);
  98. /*!
  99. * @brief Slave finish up a transfer.
  100. * It would call back if there is callback function and set the state to idle.
  101. * This is not a public API.
  102. */
  103. static void LPSPI_SlaveTransferComplete(LPSPI_Type *base, lpspi_slave_handle_t *handle);
  104. /*!
  105. * @brief Check the argument for transfer .
  106. * This is not a public API. Not static because lpspi_edma.c will use this API.
  107. */
  108. bool LPSPI_CheckTransferArgument(lpspi_transfer_t *transfer, uint32_t bitsPerFrame, uint32_t bytesPerFrame);
  109. /*!
  110. * @brief LPSPI common interrupt handler.
  111. *
  112. * @param handle pointer to s_lpspiHandle which stores the transfer state.
  113. */
  114. static void LPSPI_CommonIRQHandler(LPSPI_Type *base, void *param);
  115. /*******************************************************************************
  116. * Variables
  117. ******************************************************************************/
  118. /* Defines constant value arrays for the baud rate pre-scalar and scalar divider values.*/
  119. static const uint8_t s_baudratePrescaler[] = {1, 2, 4, 8, 16, 32, 64, 128};
  120. /*! @brief Pointers to lpspi bases for each instance. */
  121. static LPSPI_Type *const s_lpspiBases[] = LPSPI_BASE_PTRS;
  122. /*! @brief Pointers to lpspi IRQ number for each instance. */
  123. static const IRQn_Type s_lpspiIRQ[] = LPSPI_IRQS;
  124. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  125. /*! @brief Pointers to lpspi clocks for each instance. */
  126. static const clock_ip_name_t s_lpspiClocks[] = LPSPI_CLOCKS;
  127. #if defined(LPSPI_PERIPH_CLOCKS)
  128. static const clock_ip_name_t s_LpspiPeriphClocks[] = LPSPI_PERIPH_CLOCKS;
  129. #endif
  130. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  131. /*! @brief Pointers to lpspi handles for each instance. */
  132. static void *s_lpspiHandle[ARRAY_SIZE(s_lpspiBases)] = {NULL};
  133. /*! @brief Pointer to master IRQ handler for each instance. */
  134. static lpspi_master_isr_t s_lpspiMasterIsr;
  135. /*! @brief Pointer to slave IRQ handler for each instance. */
  136. static lpspi_slave_isr_t s_lpspiSlaveIsr;
  137. /* @brief Dummy data for each instance. This data is used when user's tx buffer is NULL*/
  138. volatile uint8_t s_dummyData[ARRAY_SIZE(s_lpspiBases)] = {0};
  139. /**********************************************************************************************************************
  140. * Code
  141. *********************************************************************************************************************/
  142. uint32_t LPSPI_GetInstance(LPSPI_Type *base)
  143. {
  144. uint8_t instance = 0;
  145. /* Find the instance index from base address mappings. */
  146. for (instance = 0; instance < ARRAY_SIZE(s_lpspiBases); instance++)
  147. {
  148. if (s_lpspiBases[instance] == base)
  149. {
  150. break;
  151. }
  152. }
  153. assert(instance < ARRAY_SIZE(s_lpspiBases));
  154. return instance;
  155. }
  156. void LPSPI_SetDummyData(LPSPI_Type *base, uint8_t dummyData)
  157. {
  158. uint32_t instance = LPSPI_GetInstance(base);
  159. s_dummyData[instance] = dummyData;
  160. }
  161. void LPSPI_MasterInit(LPSPI_Type *base, const lpspi_master_config_t *masterConfig, uint32_t srcClock_Hz)
  162. {
  163. assert(masterConfig);
  164. uint32_t tcrPrescaleValue = 0;
  165. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  166. uint32_t instance = LPSPI_GetInstance(base);
  167. /* Enable LPSPI clock */
  168. CLOCK_EnableClock(s_lpspiClocks[instance]);
  169. #if defined(LPSPI_PERIPH_CLOCKS)
  170. CLOCK_EnableClock(s_LpspiPeriphClocks[instance]);
  171. #endif
  172. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  173. /* Reset to known status */
  174. LPSPI_Reset(base);
  175. /* Set LPSPI to master */
  176. LPSPI_SetMasterSlaveMode(base, kLPSPI_Master);
  177. /* Set specific PCS to active high or low */
  178. LPSPI_SetOnePcsPolarity(base, masterConfig->whichPcs, masterConfig->pcsActiveHighOrLow);
  179. /* Set Configuration Register 1 related setting.*/
  180. base->CFGR1 = (base->CFGR1 & ~(LPSPI_CFGR1_OUTCFG_MASK | LPSPI_CFGR1_PINCFG_MASK | LPSPI_CFGR1_NOSTALL_MASK)) |
  181. LPSPI_CFGR1_OUTCFG(masterConfig->dataOutConfig) | LPSPI_CFGR1_PINCFG(masterConfig->pinCfg) |
  182. LPSPI_CFGR1_NOSTALL(0);
  183. /* Set baudrate and delay times*/
  184. LPSPI_MasterSetBaudRate(base, masterConfig->baudRate, srcClock_Hz, &tcrPrescaleValue);
  185. /* Set default watermarks */
  186. LPSPI_SetFifoWatermarks(base, kLpspiDefaultTxWatermark, kLpspiDefaultRxWatermark);
  187. /* Set Transmit Command Register*/
  188. base->TCR = LPSPI_TCR_CPOL(masterConfig->cpol) | LPSPI_TCR_CPHA(masterConfig->cpha) |
  189. LPSPI_TCR_LSBF(masterConfig->direction) | LPSPI_TCR_FRAMESZ(masterConfig->bitsPerFrame - 1) |
  190. LPSPI_TCR_PRESCALE(tcrPrescaleValue) | LPSPI_TCR_PCS(masterConfig->whichPcs);
  191. LPSPI_Enable(base, true);
  192. LPSPI_MasterSetDelayTimes(base, masterConfig->pcsToSckDelayInNanoSec, kLPSPI_PcsToSck, srcClock_Hz);
  193. LPSPI_MasterSetDelayTimes(base, masterConfig->lastSckToPcsDelayInNanoSec, kLPSPI_LastSckToPcs, srcClock_Hz);
  194. LPSPI_MasterSetDelayTimes(base, masterConfig->betweenTransferDelayInNanoSec, kLPSPI_BetweenTransfer, srcClock_Hz);
  195. LPSPI_SetDummyData(base, LPSPI_DUMMY_DATA);
  196. }
  197. void LPSPI_MasterGetDefaultConfig(lpspi_master_config_t *masterConfig)
  198. {
  199. assert(masterConfig);
  200. masterConfig->baudRate = 500000;
  201. masterConfig->bitsPerFrame = 8;
  202. masterConfig->cpol = kLPSPI_ClockPolarityActiveHigh;
  203. masterConfig->cpha = kLPSPI_ClockPhaseFirstEdge;
  204. masterConfig->direction = kLPSPI_MsbFirst;
  205. masterConfig->pcsToSckDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
  206. masterConfig->lastSckToPcsDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
  207. masterConfig->betweenTransferDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
  208. masterConfig->whichPcs = kLPSPI_Pcs0;
  209. masterConfig->pcsActiveHighOrLow = kLPSPI_PcsActiveLow;
  210. masterConfig->pinCfg = kLPSPI_SdiInSdoOut;
  211. masterConfig->dataOutConfig = kLpspiDataOutRetained;
  212. }
  213. void LPSPI_SlaveInit(LPSPI_Type *base, const lpspi_slave_config_t *slaveConfig)
  214. {
  215. assert(slaveConfig);
  216. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  217. uint32_t instance = LPSPI_GetInstance(base);
  218. /* Enable LPSPI clock */
  219. CLOCK_EnableClock(s_lpspiClocks[instance]);
  220. #if defined(LPSPI_PERIPH_CLOCKS)
  221. CLOCK_EnableClock(s_LpspiPeriphClocks[instance]);
  222. #endif
  223. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  224. /* Reset to known status */
  225. LPSPI_Reset(base);
  226. LPSPI_SetMasterSlaveMode(base, kLPSPI_Slave);
  227. LPSPI_SetOnePcsPolarity(base, slaveConfig->whichPcs, slaveConfig->pcsActiveHighOrLow);
  228. base->CFGR1 = (base->CFGR1 & ~(LPSPI_CFGR1_OUTCFG_MASK | LPSPI_CFGR1_PINCFG_MASK)) |
  229. LPSPI_CFGR1_OUTCFG(slaveConfig->dataOutConfig) | LPSPI_CFGR1_PINCFG(slaveConfig->pinCfg);
  230. LPSPI_SetFifoWatermarks(base, kLpspiDefaultTxWatermark, kLpspiDefaultRxWatermark);
  231. base->TCR = LPSPI_TCR_CPOL(slaveConfig->cpol) | LPSPI_TCR_CPHA(slaveConfig->cpha) |
  232. LPSPI_TCR_LSBF(slaveConfig->direction) | LPSPI_TCR_FRAMESZ(slaveConfig->bitsPerFrame - 1);
  233. /* This operation will set the dummy data for edma transfer, no effect in interrupt way. */
  234. LPSPI_SetDummyData(base, LPSPI_DUMMY_DATA);
  235. LPSPI_Enable(base, true);
  236. }
  237. void LPSPI_SlaveGetDefaultConfig(lpspi_slave_config_t *slaveConfig)
  238. {
  239. assert(slaveConfig);
  240. slaveConfig->bitsPerFrame = 8; /*!< Bits per frame, minimum 8, maximum 4096.*/
  241. slaveConfig->cpol = kLPSPI_ClockPolarityActiveHigh; /*!< Clock polarity. */
  242. slaveConfig->cpha = kLPSPI_ClockPhaseFirstEdge; /*!< Clock phase. */
  243. slaveConfig->direction = kLPSPI_MsbFirst; /*!< MSB or LSB data shift direction. */
  244. slaveConfig->whichPcs = kLPSPI_Pcs0; /*!< Desired Peripheral Chip Select (pcs) */
  245. slaveConfig->pcsActiveHighOrLow = kLPSPI_PcsActiveLow; /*!< Desired PCS active high or low */
  246. slaveConfig->pinCfg = kLPSPI_SdiInSdoOut;
  247. slaveConfig->dataOutConfig = kLpspiDataOutRetained;
  248. }
  249. void LPSPI_Reset(LPSPI_Type *base)
  250. {
  251. /* Reset all internal logic and registers, except the Control Register. Remains set until cleared by software.*/
  252. base->CR |= LPSPI_CR_RST_MASK;
  253. /* Software reset doesn't reset the CR, so manual reset the FIFOs */
  254. base->CR |= LPSPI_CR_RRF_MASK | LPSPI_CR_RTF_MASK;
  255. /* Master logic is not reset and module is disabled.*/
  256. base->CR = 0x00U;
  257. }
  258. void LPSPI_Deinit(LPSPI_Type *base)
  259. {
  260. /* Reset to default value */
  261. LPSPI_Reset(base);
  262. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  263. uint32_t instance = LPSPI_GetInstance(base);
  264. /* Enable LPSPI clock */
  265. CLOCK_DisableClock(s_lpspiClocks[instance]);
  266. #if defined(LPSPI_PERIPH_CLOCKS)
  267. CLOCK_DisableClock(s_LpspiPeriphClocks[instance]);
  268. #endif
  269. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  270. }
  271. static void LPSPI_SetOnePcsPolarity(LPSPI_Type *base,
  272. lpspi_which_pcs_t pcs,
  273. lpspi_pcs_polarity_config_t activeLowOrHigh)
  274. {
  275. uint32_t cfgr1Value = 0;
  276. /* Clear the PCS polarity bit */
  277. cfgr1Value = base->CFGR1 & ~(1U << (LPSPI_CFGR1_PCSPOL_SHIFT + pcs));
  278. /* Configure the PCS polarity bit according to the activeLowOrHigh setting */
  279. base->CFGR1 = cfgr1Value | ((uint32_t)activeLowOrHigh << (LPSPI_CFGR1_PCSPOL_SHIFT + pcs));
  280. }
  281. uint32_t LPSPI_MasterSetBaudRate(LPSPI_Type *base,
  282. uint32_t baudRate_Bps,
  283. uint32_t srcClock_Hz,
  284. uint32_t *tcrPrescaleValue)
  285. {
  286. assert(tcrPrescaleValue);
  287. /* For master mode configuration only, if slave mode detected, return 0.
  288. * Also, the LPSPI module needs to be disabled first, if enabled, return 0
  289. */
  290. if ((!LPSPI_IsMaster(base)) || (base->CR & LPSPI_CR_MEN_MASK))
  291. {
  292. return 0;
  293. }
  294. uint32_t prescaler, bestPrescaler;
  295. uint32_t scaler, bestScaler;
  296. uint32_t realBaudrate, bestBaudrate;
  297. uint32_t diff, min_diff;
  298. uint32_t desiredBaudrate = baudRate_Bps;
  299. /* find combination of prescaler and scaler resulting in baudrate closest to the
  300. * requested value
  301. */
  302. min_diff = 0xFFFFFFFFU;
  303. /* Set to maximum divisor value bit settings so that if baud rate passed in is less
  304. * than the minimum possible baud rate, then the SPI will be configured to the lowest
  305. * possible baud rate
  306. */
  307. bestPrescaler = 7;
  308. bestScaler = 255;
  309. bestBaudrate = 0; /* required to avoid compilation warning */
  310. /* In all for loops, if min_diff = 0, the exit for loop*/
  311. for (prescaler = 0; (prescaler < 8) && min_diff; prescaler++)
  312. {
  313. for (scaler = 0; (scaler < 256) && min_diff; scaler++)
  314. {
  315. realBaudrate = (srcClock_Hz / (s_baudratePrescaler[prescaler] * (scaler + 2U)));
  316. /* calculate the baud rate difference based on the conditional statement
  317. * that states that the calculated baud rate must not exceed the desired baud rate
  318. */
  319. if (desiredBaudrate >= realBaudrate)
  320. {
  321. diff = desiredBaudrate - realBaudrate;
  322. if (min_diff > diff)
  323. {
  324. /* a better match found */
  325. min_diff = diff;
  326. bestPrescaler = prescaler;
  327. bestScaler = scaler;
  328. bestBaudrate = realBaudrate;
  329. }
  330. }
  331. }
  332. }
  333. /* Write the best baud rate scalar to the CCR.
  334. * Note, no need to check for error since we've already checked to make sure the module is
  335. * disabled and in master mode. Also, there is a limit on the maximum divider so we will not
  336. * exceed this.
  337. */
  338. base->CCR = (base->CCR & ~LPSPI_CCR_SCKDIV_MASK) | LPSPI_CCR_SCKDIV(bestScaler);
  339. /* return the best prescaler value for user to use later */
  340. *tcrPrescaleValue = bestPrescaler;
  341. /* return the actual calculated baud rate */
  342. return bestBaudrate;
  343. }
  344. void LPSPI_MasterSetDelayScaler(LPSPI_Type *base, uint32_t scaler, lpspi_delay_type_t whichDelay)
  345. {
  346. /*These settings are only relevant in master mode */
  347. switch (whichDelay)
  348. {
  349. case kLPSPI_PcsToSck:
  350. base->CCR = (base->CCR & (~LPSPI_CCR_PCSSCK_MASK)) | LPSPI_CCR_PCSSCK(scaler);
  351. break;
  352. case kLPSPI_LastSckToPcs:
  353. base->CCR = (base->CCR & (~LPSPI_CCR_SCKPCS_MASK)) | LPSPI_CCR_SCKPCS(scaler);
  354. break;
  355. case kLPSPI_BetweenTransfer:
  356. base->CCR = (base->CCR & (~LPSPI_CCR_DBT_MASK)) | LPSPI_CCR_DBT(scaler);
  357. break;
  358. default:
  359. assert(false);
  360. break;
  361. }
  362. }
  363. uint32_t LPSPI_MasterSetDelayTimes(LPSPI_Type *base,
  364. uint32_t delayTimeInNanoSec,
  365. lpspi_delay_type_t whichDelay,
  366. uint32_t srcClock_Hz)
  367. {
  368. uint64_t realDelay, bestDelay;
  369. uint32_t scaler, bestScaler;
  370. uint32_t diff, min_diff;
  371. uint64_t initialDelayNanoSec;
  372. uint32_t clockDividedPrescaler;
  373. /* For delay between transfer, an additional scaler value is needed */
  374. uint32_t additionalScaler = 0;
  375. /*As the RM note, the LPSPI baud rate clock is itself divided by the PRESCALE setting, which can vary between
  376. * transfers.*/
  377. clockDividedPrescaler =
  378. srcClock_Hz / s_baudratePrescaler[(base->TCR & LPSPI_TCR_PRESCALE_MASK) >> LPSPI_TCR_PRESCALE_SHIFT];
  379. /* Find combination of prescaler and scaler resulting in the delay closest to the requested value.*/
  380. min_diff = 0xFFFFFFFFU;
  381. /* Initialize scaler to max value to generate the max delay */
  382. bestScaler = 0xFFU;
  383. /* Calculate the initial (min) delay and maximum possible delay based on the specific delay as
  384. * the delay divisors are slightly different based on which delay we are configuring.
  385. */
  386. if (whichDelay == kLPSPI_BetweenTransfer)
  387. {
  388. /* First calculate the initial, default delay, note min delay is 2 clock cycles. Due to large size of
  389. calculated values (uint64_t), we need to break up the calculation into several steps to ensure
  390. accurate calculated results
  391. */
  392. initialDelayNanoSec = 1000000000U;
  393. initialDelayNanoSec *= 2U;
  394. initialDelayNanoSec /= clockDividedPrescaler;
  395. /* Calculate the maximum delay */
  396. bestDelay = 1000000000U;
  397. bestDelay *= 257U; /* based on DBT+2, or 255 + 2 */
  398. bestDelay /= clockDividedPrescaler;
  399. additionalScaler = 1U;
  400. }
  401. else
  402. {
  403. /* First calculate the initial, default delay, min delay is 1 clock cycle. Due to large size of calculated
  404. values (uint64_t), we need to break up the calculation into several steps to ensure accurate calculated
  405. results.
  406. */
  407. initialDelayNanoSec = 1000000000U;
  408. initialDelayNanoSec /= clockDividedPrescaler;
  409. /* Calculate the maximum delay */
  410. bestDelay = 1000000000U;
  411. bestDelay *= 256U; /* based on SCKPCS+1 or PCSSCK+1, or 255 + 1 */
  412. bestDelay /= clockDividedPrescaler;
  413. additionalScaler = 0;
  414. }
  415. /* If the initial, default delay is already greater than the desired delay, then
  416. * set the delay to their initial value (0) and return the delay. In other words,
  417. * there is no way to decrease the delay value further.
  418. */
  419. if (initialDelayNanoSec >= delayTimeInNanoSec)
  420. {
  421. LPSPI_MasterSetDelayScaler(base, 0, whichDelay);
  422. return initialDelayNanoSec;
  423. }
  424. /* If min_diff = 0, the exit for loop */
  425. for (scaler = 0; (scaler < 256U) && min_diff; scaler++)
  426. {
  427. /* Calculate the real delay value as we cycle through the scaler values.
  428. Due to large size of calculated values (uint64_t), we need to break up the
  429. calculation into several steps to ensure accurate calculated results
  430. */
  431. realDelay = 1000000000U;
  432. realDelay *= (scaler + 1 + additionalScaler);
  433. realDelay /= clockDividedPrescaler;
  434. /* calculate the delay difference based on the conditional statement
  435. * that states that the calculated delay must not be less then the desired delay
  436. */
  437. if (realDelay >= delayTimeInNanoSec)
  438. {
  439. diff = realDelay - delayTimeInNanoSec;
  440. if (min_diff > diff)
  441. {
  442. /* a better match found */
  443. min_diff = diff;
  444. bestScaler = scaler;
  445. bestDelay = realDelay;
  446. }
  447. }
  448. }
  449. /* write the best scaler value for the delay */
  450. LPSPI_MasterSetDelayScaler(base, bestScaler, whichDelay);
  451. /* return the actual calculated delay value (in ns) */
  452. return bestDelay;
  453. }
  454. /*Transactional APIs -- Master*/
  455. void LPSPI_MasterTransferCreateHandle(LPSPI_Type *base,
  456. lpspi_master_handle_t *handle,
  457. lpspi_master_transfer_callback_t callback,
  458. void *userData)
  459. {
  460. assert(handle);
  461. /* Zero the handle. */
  462. memset(handle, 0, sizeof(*handle));
  463. s_lpspiHandle[LPSPI_GetInstance(base)] = handle;
  464. /* Set irq handler. */
  465. s_lpspiMasterIsr = LPSPI_MasterTransferHandleIRQ;
  466. handle->callback = callback;
  467. handle->userData = userData;
  468. }
  469. bool LPSPI_CheckTransferArgument(lpspi_transfer_t *transfer, uint32_t bitsPerFrame, uint32_t bytesPerFrame)
  470. {
  471. assert(transfer);
  472. /* If the transfer count is zero, then return immediately.*/
  473. if (transfer->dataSize == 0)
  474. {
  475. return false;
  476. }
  477. /* If both send buffer and receive buffer is null */
  478. if ((!(transfer->txData)) && (!(transfer->rxData)))
  479. {
  480. return false;
  481. }
  482. /*The transfer data size should be integer multiples of bytesPerFrame if bytesPerFrame is less than or equal to 4 .
  483. *For bytesPerFrame greater than 4 situation:
  484. *the transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not integer multiples of 4 ,
  485. *otherwise , the transfer data size can be integer multiples of bytesPerFrame.
  486. */
  487. if (bytesPerFrame <= 4)
  488. {
  489. if ((transfer->dataSize % bytesPerFrame) != 0)
  490. {
  491. return false;
  492. }
  493. }
  494. else
  495. {
  496. if ((bytesPerFrame % 4U) != 0)
  497. {
  498. if (transfer->dataSize != bytesPerFrame)
  499. {
  500. return false;
  501. }
  502. }
  503. else
  504. {
  505. if ((transfer->dataSize % bytesPerFrame) != 0)
  506. {
  507. return false;
  508. }
  509. }
  510. }
  511. return true;
  512. }
  513. status_t LPSPI_MasterTransferBlocking(LPSPI_Type *base, lpspi_transfer_t *transfer)
  514. {
  515. assert(transfer);
  516. uint32_t bitsPerFrame = ((base->TCR & LPSPI_TCR_FRAMESZ_MASK) >> LPSPI_TCR_FRAMESZ_SHIFT) + 1;
  517. uint32_t bytesPerFrame = (bitsPerFrame + 7) / 8;
  518. uint32_t temp = 0U;
  519. uint8_t dummyData = s_dummyData[LPSPI_GetInstance(base)];
  520. if (!LPSPI_CheckTransferArgument(transfer, bitsPerFrame, bytesPerFrame))
  521. {
  522. return kStatus_InvalidArgument;
  523. }
  524. /* Check that LPSPI is not busy.*/
  525. if (LPSPI_GetStatusFlags(base) & kLPSPI_ModuleBusyFlag)
  526. {
  527. return kStatus_LPSPI_Busy;
  528. }
  529. uint8_t *txData = transfer->txData;
  530. uint8_t *rxData = transfer->rxData;
  531. uint32_t txRemainingByteCount = transfer->dataSize;
  532. uint32_t rxRemainingByteCount = transfer->dataSize;
  533. uint8_t bytesEachWrite;
  534. uint8_t bytesEachRead;
  535. uint32_t readData = 0;
  536. uint32_t wordToSend =
  537. ((uint32_t)dummyData) | ((uint32_t)dummyData << 8) | ((uint32_t)dummyData << 16) | ((uint32_t)dummyData << 24);
  538. /*The TX and RX FIFO sizes are always the same*/
  539. uint32_t fifoSize = LPSPI_GetRxFifoSize(base);
  540. uint32_t whichPcs = (transfer->configFlags & LPSPI_MASTER_PCS_MASK) >> LPSPI_MASTER_PCS_SHIFT;
  541. bool isPcsContinuous = (bool)(transfer->configFlags & kLPSPI_MasterPcsContinuous);
  542. bool isRxMask = false;
  543. bool isByteSwap = (bool)(transfer->configFlags & kLPSPI_MasterByteSwap);
  544. LPSPI_FlushFifo(base, true, true);
  545. LPSPI_ClearStatusFlags(base, kLPSPI_AllStatusFlag);
  546. if (!rxData)
  547. {
  548. isRxMask = true;
  549. }
  550. LPSPI_Enable(base, false);
  551. base->CFGR1 &= (~LPSPI_CFGR1_NOSTALL_MASK);
  552. /* Check if using 3-wire mode and the txData is NULL, set the output pin to tristated. */
  553. temp = base->CFGR1;
  554. temp &= LPSPI_CFGR1_PINCFG_MASK;
  555. if ((temp == LPSPI_CFGR1_PINCFG(kLPSPI_SdiInSdiOut)) || (temp == LPSPI_CFGR1_PINCFG(kLPSPI_SdoInSdoOut)))
  556. {
  557. if (!txData)
  558. {
  559. base->CFGR1 |= LPSPI_CFGR1_OUTCFG_MASK;
  560. }
  561. /* The 3-wire mode can't send and receive data at the same time. */
  562. if ((txData) && (rxData))
  563. {
  564. return kStatus_InvalidArgument;
  565. }
  566. }
  567. LPSPI_Enable(base, true);
  568. base->TCR =
  569. (base->TCR & ~(LPSPI_TCR_CONT_MASK | LPSPI_TCR_CONTC_MASK | LPSPI_TCR_RXMSK_MASK | LPSPI_TCR_PCS_MASK)) |
  570. LPSPI_TCR_CONT(isPcsContinuous) | LPSPI_TCR_CONTC(0) | LPSPI_TCR_RXMSK(isRxMask) | LPSPI_TCR_PCS(whichPcs);
  571. if (bytesPerFrame <= 4)
  572. {
  573. bytesEachWrite = bytesPerFrame;
  574. bytesEachRead = bytesPerFrame;
  575. }
  576. else
  577. {
  578. bytesEachWrite = 4;
  579. bytesEachRead = 4;
  580. }
  581. /*Write the TX data until txRemainingByteCount is equal to 0 */
  582. while (txRemainingByteCount > 0)
  583. {
  584. if (txRemainingByteCount < bytesEachWrite)
  585. {
  586. bytesEachWrite = txRemainingByteCount;
  587. }
  588. /*Wait until TX FIFO is not full*/
  589. while (LPSPI_GetTxFifoCount(base) == fifoSize)
  590. {
  591. }
  592. if (txData)
  593. {
  594. wordToSend = LPSPI_CombineWriteData(txData, bytesEachWrite, isByteSwap);
  595. txData += bytesEachWrite;
  596. }
  597. LPSPI_WriteData(base, wordToSend);
  598. txRemainingByteCount -= bytesEachWrite;
  599. /*Check whether there is RX data in RX FIFO . Read out the RX data so that the RX FIFO would not overrun.*/
  600. if (rxData)
  601. {
  602. while (LPSPI_GetRxFifoCount(base))
  603. {
  604. readData = LPSPI_ReadData(base);
  605. if (rxRemainingByteCount < bytesEachRead)
  606. {
  607. bytesEachRead = rxRemainingByteCount;
  608. }
  609. LPSPI_SeparateReadData(rxData, readData, bytesEachRead, isByteSwap);
  610. rxData += bytesEachRead;
  611. rxRemainingByteCount -= bytesEachRead;
  612. }
  613. }
  614. }
  615. /* After write all the data in TX FIFO , should write the TCR_CONTC to 0 to de-assert the PCS. Note that TCR
  616. * register also use the TX FIFO.
  617. */
  618. while ((LPSPI_GetTxFifoCount(base) == fifoSize))
  619. {
  620. }
  621. base->TCR = (base->TCR & ~(LPSPI_TCR_CONTC_MASK));
  622. /*Read out the RX data in FIFO*/
  623. if (rxData)
  624. {
  625. while (rxRemainingByteCount > 0)
  626. {
  627. while (LPSPI_GetRxFifoCount(base))
  628. {
  629. readData = LPSPI_ReadData(base);
  630. if (rxRemainingByteCount < bytesEachRead)
  631. {
  632. bytesEachRead = rxRemainingByteCount;
  633. }
  634. LPSPI_SeparateReadData(rxData, readData, bytesEachRead, isByteSwap);
  635. rxData += bytesEachRead;
  636. rxRemainingByteCount -= bytesEachRead;
  637. }
  638. }
  639. }
  640. else
  641. {
  642. /* If no RX buffer, then transfer is not complete until transfer complete flag sets */
  643. while (!(LPSPI_GetStatusFlags(base) & kLPSPI_TransferCompleteFlag))
  644. {
  645. }
  646. }
  647. return kStatus_Success;
  648. }
  649. status_t LPSPI_MasterTransferNonBlocking(LPSPI_Type *base, lpspi_master_handle_t *handle, lpspi_transfer_t *transfer)
  650. {
  651. assert(handle);
  652. assert(transfer);
  653. uint32_t bitsPerFrame = ((base->TCR & LPSPI_TCR_FRAMESZ_MASK) >> LPSPI_TCR_FRAMESZ_SHIFT) + 1;
  654. uint32_t bytesPerFrame = (bitsPerFrame + 7) / 8;
  655. uint32_t temp = 0U;
  656. uint8_t dummyData = s_dummyData[LPSPI_GetInstance(base)];
  657. if (!LPSPI_CheckTransferArgument(transfer, bitsPerFrame, bytesPerFrame))
  658. {
  659. return kStatus_InvalidArgument;
  660. }
  661. /* Check that we're not busy.*/
  662. if (handle->state == kLPSPI_Busy)
  663. {
  664. return kStatus_LPSPI_Busy;
  665. }
  666. handle->state = kLPSPI_Busy;
  667. bool isRxMask = false;
  668. uint8_t txWatermark;
  669. uint32_t whichPcs = (transfer->configFlags & LPSPI_MASTER_PCS_MASK) >> LPSPI_MASTER_PCS_SHIFT;
  670. handle->txData = transfer->txData;
  671. handle->rxData = transfer->rxData;
  672. handle->txRemainingByteCount = transfer->dataSize;
  673. handle->rxRemainingByteCount = transfer->dataSize;
  674. handle->totalByteCount = transfer->dataSize;
  675. handle->writeTcrInIsr = false;
  676. handle->writeRegRemainingTimes = (transfer->dataSize / bytesPerFrame) * ((bytesPerFrame + 3) / 4);
  677. handle->readRegRemainingTimes = handle->writeRegRemainingTimes;
  678. handle->txBuffIfNull =
  679. ((uint32_t)dummyData) | ((uint32_t)dummyData << 8) | ((uint32_t)dummyData << 16) | ((uint32_t)dummyData << 24);
  680. /*The TX and RX FIFO sizes are always the same*/
  681. handle->fifoSize = LPSPI_GetRxFifoSize(base);
  682. handle->isPcsContinuous = (bool)(transfer->configFlags & kLPSPI_MasterPcsContinuous);
  683. handle->isByteSwap = (bool)(transfer->configFlags & kLPSPI_MasterByteSwap);
  684. /*Set the RX and TX watermarks to reduce the ISR times.*/
  685. if (handle->fifoSize > 1)
  686. {
  687. txWatermark = 1;
  688. handle->rxWatermark = handle->fifoSize - 2;
  689. }
  690. else
  691. {
  692. txWatermark = 0;
  693. handle->rxWatermark = 0;
  694. }
  695. LPSPI_SetFifoWatermarks(base, txWatermark, handle->rxWatermark);
  696. LPSPI_Enable(base, false);
  697. /*Transfers will stall when transmit FIFO is empty or receive FIFO is full. */
  698. base->CFGR1 &= (~LPSPI_CFGR1_NOSTALL_MASK);
  699. /* Check if using 3-wire mode and the txData is NULL, set the output pin to tristated. */
  700. temp = base->CFGR1;
  701. temp &= LPSPI_CFGR1_PINCFG_MASK;
  702. if ((temp == LPSPI_CFGR1_PINCFG(kLPSPI_SdiInSdiOut)) || (temp == LPSPI_CFGR1_PINCFG(kLPSPI_SdoInSdoOut)))
  703. {
  704. if (!handle->txData)
  705. {
  706. base->CFGR1 |= LPSPI_CFGR1_OUTCFG_MASK;
  707. }
  708. /* The 3-wire mode can't send and receive data at the same time. */
  709. if ((handle->txData) && (handle->rxData))
  710. {
  711. return kStatus_InvalidArgument;
  712. }
  713. }
  714. LPSPI_Enable(base, true);
  715. /*Flush FIFO , clear status , disable all the inerrupts.*/
  716. LPSPI_FlushFifo(base, true, true);
  717. LPSPI_ClearStatusFlags(base, kLPSPI_AllStatusFlag);
  718. LPSPI_DisableInterrupts(base, kLPSPI_AllInterruptEnable);
  719. /* If there is not rxData , can mask the receive data (receive data is not stored in receive FIFO).
  720. * For master transfer , we'd better not masked the transmit data in TCR since the transfer flow is hard to
  721. * controlled by software.*/
  722. if (handle->rxData == NULL)
  723. {
  724. isRxMask = true;
  725. handle->rxRemainingByteCount = 0;
  726. }
  727. base->TCR =
  728. (base->TCR & ~(LPSPI_TCR_CONT_MASK | LPSPI_TCR_CONTC_MASK | LPSPI_TCR_RXMSK_MASK | LPSPI_TCR_PCS_MASK)) |
  729. LPSPI_TCR_CONT(handle->isPcsContinuous) | LPSPI_TCR_CONTC(0) | LPSPI_TCR_RXMSK(isRxMask) |
  730. LPSPI_TCR_PCS(whichPcs);
  731. /*Calculate the bytes for write/read the TX/RX register each time*/
  732. if (bytesPerFrame <= 4)
  733. {
  734. handle->bytesEachWrite = bytesPerFrame;
  735. handle->bytesEachRead = bytesPerFrame;
  736. }
  737. else
  738. {
  739. handle->bytesEachWrite = 4;
  740. handle->bytesEachRead = 4;
  741. }
  742. /* Enable the NVIC for LPSPI peripheral. Note that below code is useless if the LPSPI interrupt is in INTMUX ,
  743. * and you should also enable the INTMUX interupt in your application.
  744. */
  745. EnableIRQ(s_lpspiIRQ[LPSPI_GetInstance(base)]);
  746. /*TCR is also shared the FIFO , so wait for TCR written.*/
  747. while (LPSPI_GetTxFifoCount(base) != 0)
  748. {
  749. }
  750. /*Fill up the TX data in FIFO */
  751. LPSPI_MasterTransferFillUpTxFifo(base, handle);
  752. /* Since SPI is a synchronous interface, we only need to enable the RX interrupt if there is RX data.
  753. * The IRQ handler will get the status of RX and TX interrupt flags.
  754. */
  755. if (handle->rxData)
  756. {
  757. /*Set rxWatermark to (readRegRemainingTimes-1) if readRegRemainingTimes less than rxWatermark. Otherwise there
  758. *is not RX interrupt for the last datas because the RX count is not greater than rxWatermark.
  759. */
  760. if ((handle->readRegRemainingTimes) <= handle->rxWatermark)
  761. {
  762. base->FCR = (base->FCR & (~LPSPI_FCR_RXWATER_MASK)) | LPSPI_FCR_RXWATER(handle->readRegRemainingTimes - 1);
  763. }
  764. LPSPI_EnableInterrupts(base, kLPSPI_RxInterruptEnable);
  765. }
  766. else
  767. {
  768. LPSPI_EnableInterrupts(base, kLPSPI_TxInterruptEnable);
  769. }
  770. return kStatus_Success;
  771. }
  772. static void LPSPI_MasterTransferFillUpTxFifo(LPSPI_Type *base, lpspi_master_handle_t *handle)
  773. {
  774. assert(handle);
  775. uint32_t wordToSend = 0;
  776. /* Make sure the difference in remaining TX and RX byte counts does not exceed FIFO depth
  777. * and that the number of TX FIFO entries does not exceed the FIFO depth.
  778. * But no need to make the protection if there is no rxData.
  779. */
  780. while ((LPSPI_GetTxFifoCount(base) < (handle->fifoSize)) &&
  781. (((handle->readRegRemainingTimes - handle->writeRegRemainingTimes) < handle->fifoSize) ||
  782. (handle->rxData == NULL)))
  783. {
  784. if (handle->txRemainingByteCount < handle->bytesEachWrite)
  785. {
  786. handle->bytesEachWrite = handle->txRemainingByteCount;
  787. }
  788. if (handle->txData)
  789. {
  790. wordToSend = LPSPI_CombineWriteData(handle->txData, handle->bytesEachWrite, handle->isByteSwap);
  791. handle->txData += handle->bytesEachWrite;
  792. }
  793. else
  794. {
  795. wordToSend = handle->txBuffIfNull;
  796. }
  797. /*Write the word to TX register*/
  798. LPSPI_WriteData(base, wordToSend);
  799. /*Decrease the write TX register times.*/
  800. --handle->writeRegRemainingTimes;
  801. /*Decrease the remaining TX byte count.*/
  802. handle->txRemainingByteCount -= handle->bytesEachWrite;
  803. if (handle->txRemainingByteCount == 0)
  804. {
  805. /* If PCS is continuous, update TCR to de-assert PCS */
  806. if (handle->isPcsContinuous)
  807. {
  808. /* Only write to the TCR if the FIFO has room */
  809. if ((LPSPI_GetTxFifoCount(base) < (handle->fifoSize)))
  810. {
  811. base->TCR = (base->TCR & ~(LPSPI_TCR_CONTC_MASK));
  812. handle->writeTcrInIsr = false;
  813. }
  814. /* Else, set a global flag to tell the ISR to do write to the TCR */
  815. else
  816. {
  817. handle->writeTcrInIsr = true;
  818. }
  819. }
  820. break;
  821. }
  822. }
  823. }
  824. static void LPSPI_MasterTransferComplete(LPSPI_Type *base, lpspi_master_handle_t *handle)
  825. {
  826. assert(handle);
  827. /* Disable interrupt requests*/
  828. LPSPI_DisableInterrupts(base, kLPSPI_AllInterruptEnable);
  829. handle->state = kLPSPI_Idle;
  830. if (handle->callback)
  831. {
  832. handle->callback(base, handle, kStatus_Success, handle->userData);
  833. }
  834. }
  835. status_t LPSPI_MasterTransferGetCount(LPSPI_Type *base, lpspi_master_handle_t *handle, size_t *count)
  836. {
  837. assert(handle);
  838. if (!count)
  839. {
  840. return kStatus_InvalidArgument;
  841. }
  842. /* Catch when there is not an active transfer. */
  843. if (handle->state != kLPSPI_Busy)
  844. {
  845. *count = 0;
  846. return kStatus_NoTransferInProgress;
  847. }
  848. size_t remainingByte;
  849. if (handle->rxData)
  850. {
  851. remainingByte = handle->rxRemainingByteCount;
  852. }
  853. else
  854. {
  855. remainingByte = handle->txRemainingByteCount;
  856. }
  857. *count = handle->totalByteCount - remainingByte;
  858. return kStatus_Success;
  859. }
  860. void LPSPI_MasterTransferAbort(LPSPI_Type *base, lpspi_master_handle_t *handle)
  861. {
  862. assert(handle);
  863. /* Disable interrupt requests*/
  864. LPSPI_DisableInterrupts(base, kLPSPI_AllInterruptEnable);
  865. LPSPI_Reset(base);
  866. handle->state = kLPSPI_Idle;
  867. handle->txRemainingByteCount = 0;
  868. handle->rxRemainingByteCount = 0;
  869. }
  870. void LPSPI_MasterTransferHandleIRQ(LPSPI_Type *base, lpspi_master_handle_t *handle)
  871. {
  872. assert(handle);
  873. uint32_t readData;
  874. if (handle->rxData != NULL)
  875. {
  876. if (handle->rxRemainingByteCount)
  877. {
  878. /* First, disable the interrupts to avoid potentially triggering another interrupt
  879. * while reading out the RX FIFO as more data may be coming into the RX FIFO. We'll
  880. * re-enable the interrupts based on the LPSPI state after reading out the FIFO.
  881. */
  882. LPSPI_DisableInterrupts(base, kLPSPI_RxInterruptEnable);
  883. while ((LPSPI_GetRxFifoCount(base)) && (handle->rxRemainingByteCount))
  884. {
  885. /*Read out the data*/
  886. readData = LPSPI_ReadData(base);
  887. /*Decrease the read RX register times.*/
  888. --handle->readRegRemainingTimes;
  889. if (handle->rxRemainingByteCount < handle->bytesEachRead)
  890. {
  891. handle->bytesEachRead = handle->rxRemainingByteCount;
  892. }
  893. LPSPI_SeparateReadData(handle->rxData, readData, handle->bytesEachRead, handle->isByteSwap);
  894. handle->rxData += handle->bytesEachRead;
  895. /*Decrease the remaining RX byte count.*/
  896. handle->rxRemainingByteCount -= handle->bytesEachRead;
  897. }
  898. /* Re-enable the interrupts only if rxCount indicates there is more data to receive,
  899. * else we may get a spurious interrupt.
  900. * */
  901. if (handle->rxRemainingByteCount)
  902. {
  903. /* Set the TDF and RDF interrupt enables simultaneously to avoid race conditions */
  904. LPSPI_EnableInterrupts(base, kLPSPI_RxInterruptEnable);
  905. }
  906. }
  907. /*Set rxWatermark to (readRegRemainingTimes-1) if readRegRemainingTimes less than rxWatermark. Otherwise there
  908. *is not RX interrupt for the last datas because the RX count is not greater than rxWatermark.
  909. */
  910. if ((handle->readRegRemainingTimes) <= (handle->rxWatermark))
  911. {
  912. base->FCR =
  913. (base->FCR & (~LPSPI_FCR_RXWATER_MASK)) |
  914. LPSPI_FCR_RXWATER((handle->readRegRemainingTimes > 1) ? (handle->readRegRemainingTimes - 1U) : (0U));
  915. }
  916. }
  917. if (handle->txRemainingByteCount)
  918. {
  919. LPSPI_MasterTransferFillUpTxFifo(base, handle);
  920. }
  921. else
  922. {
  923. if ((LPSPI_GetTxFifoCount(base) < (handle->fifoSize)))
  924. {
  925. if ((handle->isPcsContinuous) && (handle->writeTcrInIsr))
  926. {
  927. base->TCR = (base->TCR & ~(LPSPI_TCR_CONTC_MASK));
  928. handle->writeTcrInIsr = false;
  929. }
  930. }
  931. }
  932. if ((handle->txRemainingByteCount == 0) && (handle->rxRemainingByteCount == 0) && (!handle->writeTcrInIsr))
  933. {
  934. /* If no RX buffer, then transfer is not complete until transfer complete flag sets */
  935. if (handle->rxData == NULL)
  936. {
  937. if (LPSPI_GetStatusFlags(base) & kLPSPI_TransferCompleteFlag)
  938. {
  939. /* Complete the transfer and disable the interrupts */
  940. LPSPI_MasterTransferComplete(base, handle);
  941. }
  942. else
  943. {
  944. LPSPI_EnableInterrupts(base, kLPSPI_TransferCompleteInterruptEnable);
  945. LPSPI_DisableInterrupts(base, kLPSPI_TxInterruptEnable | kLPSPI_RxInterruptEnable);
  946. }
  947. }
  948. else
  949. {
  950. /* Complete the transfer and disable the interrupts */
  951. LPSPI_MasterTransferComplete(base, handle);
  952. }
  953. }
  954. }
  955. /*Transactional APIs -- Slave*/
  956. void LPSPI_SlaveTransferCreateHandle(LPSPI_Type *base,
  957. lpspi_slave_handle_t *handle,
  958. lpspi_slave_transfer_callback_t callback,
  959. void *userData)
  960. {
  961. assert(handle);
  962. /* Zero the handle. */
  963. memset(handle, 0, sizeof(*handle));
  964. s_lpspiHandle[LPSPI_GetInstance(base)] = handle;
  965. /* Set irq handler. */
  966. s_lpspiSlaveIsr = LPSPI_SlaveTransferHandleIRQ;
  967. handle->callback = callback;
  968. handle->userData = userData;
  969. }
  970. status_t LPSPI_SlaveTransferNonBlocking(LPSPI_Type *base, lpspi_slave_handle_t *handle, lpspi_transfer_t *transfer)
  971. {
  972. assert(handle);
  973. assert(transfer);
  974. uint32_t bitsPerFrame = ((base->TCR & LPSPI_TCR_FRAMESZ_MASK) >> LPSPI_TCR_FRAMESZ_SHIFT) + 1;
  975. uint32_t bytesPerFrame = (bitsPerFrame + 7) / 8;
  976. uint32_t temp = 0U;
  977. if (!LPSPI_CheckTransferArgument(transfer, bitsPerFrame, bytesPerFrame))
  978. {
  979. return kStatus_InvalidArgument;
  980. }
  981. /* Check that we're not busy.*/
  982. if (handle->state == kLPSPI_Busy)
  983. {
  984. return kStatus_LPSPI_Busy;
  985. }
  986. handle->state = kLPSPI_Busy;
  987. bool isRxMask = false;
  988. bool isTxMask = false;
  989. uint32_t whichPcs = (transfer->configFlags & LPSPI_SLAVE_PCS_MASK) >> LPSPI_SLAVE_PCS_SHIFT;
  990. handle->txData = transfer->txData;
  991. handle->rxData = transfer->rxData;
  992. handle->txRemainingByteCount = transfer->dataSize;
  993. handle->rxRemainingByteCount = transfer->dataSize;
  994. handle->totalByteCount = transfer->dataSize;
  995. handle->writeRegRemainingTimes = (transfer->dataSize / bytesPerFrame) * ((bytesPerFrame + 3) / 4);
  996. handle->readRegRemainingTimes = handle->writeRegRemainingTimes;
  997. /*The TX and RX FIFO sizes are always the same*/
  998. handle->fifoSize = LPSPI_GetRxFifoSize(base);
  999. handle->isByteSwap = (bool)(transfer->configFlags & kLPSPI_SlaveByteSwap);
  1000. /*Set the RX and TX watermarks to reduce the ISR times.*/
  1001. uint8_t txWatermark;
  1002. if (handle->fifoSize > 1)
  1003. {
  1004. txWatermark = 1;
  1005. handle->rxWatermark = handle->fifoSize - 2;
  1006. }
  1007. else
  1008. {
  1009. txWatermark = 0;
  1010. handle->rxWatermark = 0;
  1011. }
  1012. LPSPI_SetFifoWatermarks(base, txWatermark, handle->rxWatermark);
  1013. /* Check if using 3-wire mode and the txData is NULL, set the output pin to tristated. */
  1014. temp = base->CFGR1;
  1015. temp &= LPSPI_CFGR1_PINCFG_MASK;
  1016. if ((temp == LPSPI_CFGR1_PINCFG(kLPSPI_SdiInSdiOut)) || (temp == LPSPI_CFGR1_PINCFG(kLPSPI_SdoInSdoOut)))
  1017. {
  1018. if (!handle->txData)
  1019. {
  1020. LPSPI_Enable(base, false);
  1021. base->CFGR1 |= LPSPI_CFGR1_OUTCFG_MASK;
  1022. LPSPI_Enable(base, true);
  1023. }
  1024. /* The 3-wire mode can't send and receive data at the same time. */
  1025. if ((handle->txData) && (handle->rxData))
  1026. {
  1027. return kStatus_InvalidArgument;
  1028. }
  1029. }
  1030. /*Flush FIFO , clear status , disable all the inerrupts.*/
  1031. LPSPI_FlushFifo(base, true, true);
  1032. LPSPI_ClearStatusFlags(base, kLPSPI_AllStatusFlag);
  1033. LPSPI_DisableInterrupts(base, kLPSPI_AllInterruptEnable);
  1034. /*If there is not rxData , can mask the receive data (receive data is not stored in receive FIFO).*/
  1035. if (handle->rxData == NULL)
  1036. {
  1037. isRxMask = true;
  1038. handle->rxRemainingByteCount = 0;
  1039. }
  1040. /*If there is not txData , can mask the transmit data (no data is loaded from transmit FIFO and output pin
  1041. * is tristated).
  1042. */
  1043. if (handle->txData == NULL)
  1044. {
  1045. isTxMask = true;
  1046. handle->txRemainingByteCount = 0;
  1047. }
  1048. base->TCR = (base->TCR &
  1049. ~(LPSPI_TCR_CONT_MASK | LPSPI_TCR_CONTC_MASK | LPSPI_TCR_RXMSK_MASK | LPSPI_TCR_TXMSK_MASK |
  1050. LPSPI_TCR_PCS_MASK)) |
  1051. LPSPI_TCR_CONT(0) | LPSPI_TCR_CONTC(0) | LPSPI_TCR_RXMSK(isRxMask) | LPSPI_TCR_TXMSK(isTxMask) |
  1052. LPSPI_TCR_PCS(whichPcs);
  1053. /*Calculate the bytes for write/read the TX/RX register each time*/
  1054. if (bytesPerFrame <= 4)
  1055. {
  1056. handle->bytesEachWrite = bytesPerFrame;
  1057. handle->bytesEachRead = bytesPerFrame;
  1058. }
  1059. else
  1060. {
  1061. handle->bytesEachWrite = 4;
  1062. handle->bytesEachRead = 4;
  1063. }
  1064. /* Enable the NVIC for LPSPI peripheral. Note that below code is useless if the LPSPI interrupt is in INTMUX ,
  1065. * and you should also enable the INTMUX interupt in your application.
  1066. */
  1067. EnableIRQ(s_lpspiIRQ[LPSPI_GetInstance(base)]);
  1068. /*TCR is also shared the FIFO , so wait for TCR written.*/
  1069. while (LPSPI_GetTxFifoCount(base) != 0)
  1070. {
  1071. }
  1072. /*Fill up the TX data in FIFO */
  1073. if (handle->txData)
  1074. {
  1075. LPSPI_SlaveTransferFillUpTxFifo(base, handle);
  1076. }
  1077. /* Since SPI is a synchronous interface, we only need to enable the RX interrupt if there is RX data.
  1078. * The IRQ handler will get the status of RX and TX interrupt flags.
  1079. */
  1080. if (handle->rxData)
  1081. {
  1082. /*Set rxWatermark to (readRegRemainingTimes-1) if readRegRemainingTimes less than rxWatermark. Otherwise there
  1083. *is not RX interrupt for the last datas because the RX count is not greater than rxWatermark.
  1084. */
  1085. if ((handle->readRegRemainingTimes) <= handle->rxWatermark)
  1086. {
  1087. base->FCR = (base->FCR & (~LPSPI_FCR_RXWATER_MASK)) | LPSPI_FCR_RXWATER(handle->readRegRemainingTimes - 1);
  1088. }
  1089. LPSPI_EnableInterrupts(base, kLPSPI_RxInterruptEnable);
  1090. }
  1091. else
  1092. {
  1093. LPSPI_EnableInterrupts(base, kLPSPI_TxInterruptEnable);
  1094. }
  1095. if (handle->rxData)
  1096. {
  1097. /* RX FIFO overflow request enable */
  1098. LPSPI_EnableInterrupts(base, kLPSPI_ReceiveErrorInterruptEnable);
  1099. }
  1100. if (handle->txData)
  1101. {
  1102. /* TX FIFO underflow request enable */
  1103. LPSPI_EnableInterrupts(base, kLPSPI_TransmitErrorInterruptEnable);
  1104. }
  1105. return kStatus_Success;
  1106. }
  1107. static void LPSPI_SlaveTransferFillUpTxFifo(LPSPI_Type *base, lpspi_slave_handle_t *handle)
  1108. {
  1109. assert(handle);
  1110. uint32_t wordToSend = 0;
  1111. while (LPSPI_GetTxFifoCount(base) < (handle->fifoSize))
  1112. {
  1113. if (handle->txRemainingByteCount < handle->bytesEachWrite)
  1114. {
  1115. handle->bytesEachWrite = handle->txRemainingByteCount;
  1116. }
  1117. wordToSend = LPSPI_CombineWriteData(handle->txData, handle->bytesEachWrite, handle->isByteSwap);
  1118. handle->txData += handle->bytesEachWrite;
  1119. /*Decrease the remaining TX byte count.*/
  1120. handle->txRemainingByteCount -= handle->bytesEachWrite;
  1121. /*Write the word to TX register*/
  1122. LPSPI_WriteData(base, wordToSend);
  1123. if (handle->txRemainingByteCount == 0)
  1124. {
  1125. break;
  1126. }
  1127. }
  1128. }
  1129. static void LPSPI_SlaveTransferComplete(LPSPI_Type *base, lpspi_slave_handle_t *handle)
  1130. {
  1131. assert(handle);
  1132. status_t status = 0;
  1133. /* Disable interrupt requests*/
  1134. LPSPI_DisableInterrupts(base, kLPSPI_AllInterruptEnable);
  1135. if (handle->state == kLPSPI_Error)
  1136. {
  1137. status = kStatus_LPSPI_Error;
  1138. }
  1139. else
  1140. {
  1141. status = kStatus_Success;
  1142. }
  1143. handle->state = kLPSPI_Idle;
  1144. if (handle->callback)
  1145. {
  1146. handle->callback(base, handle, status, handle->userData);
  1147. }
  1148. }
  1149. status_t LPSPI_SlaveTransferGetCount(LPSPI_Type *base, lpspi_slave_handle_t *handle, size_t *count)
  1150. {
  1151. assert(handle);
  1152. if (!count)
  1153. {
  1154. return kStatus_InvalidArgument;
  1155. }
  1156. /* Catch when there is not an active transfer. */
  1157. if (handle->state != kLPSPI_Busy)
  1158. {
  1159. *count = 0;
  1160. return kStatus_NoTransferInProgress;
  1161. }
  1162. size_t remainingByte;
  1163. if (handle->rxData)
  1164. {
  1165. remainingByte = handle->rxRemainingByteCount;
  1166. }
  1167. else
  1168. {
  1169. remainingByte = handle->txRemainingByteCount;
  1170. }
  1171. *count = handle->totalByteCount - remainingByte;
  1172. return kStatus_Success;
  1173. }
  1174. void LPSPI_SlaveTransferAbort(LPSPI_Type *base, lpspi_slave_handle_t *handle)
  1175. {
  1176. assert(handle);
  1177. /* Disable interrupt requests*/
  1178. LPSPI_DisableInterrupts(base, kLPSPI_TxInterruptEnable | kLPSPI_RxInterruptEnable);
  1179. LPSPI_Reset(base);
  1180. handle->state = kLPSPI_Idle;
  1181. handle->txRemainingByteCount = 0;
  1182. handle->rxRemainingByteCount = 0;
  1183. }
  1184. void LPSPI_SlaveTransferHandleIRQ(LPSPI_Type *base, lpspi_slave_handle_t *handle)
  1185. {
  1186. assert(handle);
  1187. uint32_t readData; /* variable to store word read from RX FIFO */
  1188. uint32_t wordToSend; /* variable to store word to write to TX FIFO */
  1189. if (handle->rxData != NULL)
  1190. {
  1191. if (handle->rxRemainingByteCount > 0)
  1192. {
  1193. while (LPSPI_GetRxFifoCount(base))
  1194. {
  1195. /*Read out the data*/
  1196. readData = LPSPI_ReadData(base);
  1197. /*Decrease the read RX register times.*/
  1198. --handle->readRegRemainingTimes;
  1199. if (handle->rxRemainingByteCount < handle->bytesEachRead)
  1200. {
  1201. handle->bytesEachRead = handle->rxRemainingByteCount;
  1202. }
  1203. LPSPI_SeparateReadData(handle->rxData, readData, handle->bytesEachRead, handle->isByteSwap);
  1204. handle->rxData += handle->bytesEachRead;
  1205. /*Decrease the remaining RX byte count.*/
  1206. handle->rxRemainingByteCount -= handle->bytesEachRead;
  1207. if ((handle->txRemainingByteCount > 0) && (handle->txData != NULL))
  1208. {
  1209. if (handle->txRemainingByteCount < handle->bytesEachWrite)
  1210. {
  1211. handle->bytesEachWrite = handle->txRemainingByteCount;
  1212. }
  1213. wordToSend = LPSPI_CombineWriteData(handle->txData, handle->bytesEachWrite, handle->isByteSwap);
  1214. handle->txData += handle->bytesEachWrite;
  1215. /*Decrease the remaining TX byte count.*/
  1216. handle->txRemainingByteCount -= handle->bytesEachWrite;
  1217. /*Write the word to TX register*/
  1218. LPSPI_WriteData(base, wordToSend);
  1219. }
  1220. if (handle->rxRemainingByteCount == 0)
  1221. {
  1222. break;
  1223. }
  1224. }
  1225. }
  1226. /*Set rxWatermark to (readRegRemainingTimes-1) if readRegRemainingTimes less than rxWatermark. Otherwise there
  1227. *is not RX interrupt for the last datas because the RX count is not greater than rxWatermark.
  1228. */
  1229. if ((handle->readRegRemainingTimes) <= (handle->rxWatermark))
  1230. {
  1231. base->FCR =
  1232. (base->FCR & (~LPSPI_FCR_RXWATER_MASK)) |
  1233. LPSPI_FCR_RXWATER((handle->readRegRemainingTimes > 1) ? (handle->readRegRemainingTimes - 1U) : (0U));
  1234. }
  1235. }
  1236. else if ((handle->txRemainingByteCount) && (handle->txData != NULL))
  1237. {
  1238. LPSPI_SlaveTransferFillUpTxFifo(base, handle);
  1239. }
  1240. else
  1241. {
  1242. __NOP();
  1243. }
  1244. if ((handle->txRemainingByteCount == 0) && (handle->rxRemainingByteCount == 0))
  1245. {
  1246. /* If no RX buffer, then transfer is not complete until transfer complete flag sets and the TX FIFO empty*/
  1247. if (handle->rxData == NULL)
  1248. {
  1249. if ((LPSPI_GetStatusFlags(base) & kLPSPI_FrameCompleteFlag) && (LPSPI_GetTxFifoCount(base) == 0))
  1250. {
  1251. /* Complete the transfer and disable the interrupts */
  1252. LPSPI_SlaveTransferComplete(base, handle);
  1253. }
  1254. else
  1255. {
  1256. LPSPI_ClearStatusFlags(base, kLPSPI_FrameCompleteFlag);
  1257. LPSPI_EnableInterrupts(base, kLPSPI_FrameCompleteInterruptEnable);
  1258. LPSPI_DisableInterrupts(base, kLPSPI_TxInterruptEnable | kLPSPI_RxInterruptEnable);
  1259. }
  1260. }
  1261. else
  1262. {
  1263. /* Complete the transfer and disable the interrupts */
  1264. LPSPI_SlaveTransferComplete(base, handle);
  1265. }
  1266. }
  1267. /* Catch tx fifo underflow conditions, service only if tx under flow interrupt enabled */
  1268. if ((LPSPI_GetStatusFlags(base) & kLPSPI_TransmitErrorFlag) && (base->IER & LPSPI_IER_TEIE_MASK))
  1269. {
  1270. LPSPI_ClearStatusFlags(base, kLPSPI_TransmitErrorFlag);
  1271. /* Change state to error and clear flag */
  1272. if (handle->txData)
  1273. {
  1274. handle->state = kLPSPI_Error;
  1275. }
  1276. handle->errorCount++;
  1277. }
  1278. /* Catch rx fifo overflow conditions, service only if rx over flow interrupt enabled */
  1279. if ((LPSPI_GetStatusFlags(base) & kLPSPI_ReceiveErrorFlag) && (base->IER & LPSPI_IER_REIE_MASK))
  1280. {
  1281. LPSPI_ClearStatusFlags(base, kLPSPI_ReceiveErrorFlag);
  1282. /* Change state to error and clear flag */
  1283. if (handle->txData)
  1284. {
  1285. handle->state = kLPSPI_Error;
  1286. }
  1287. handle->errorCount++;
  1288. }
  1289. }
  1290. static uint32_t LPSPI_CombineWriteData(uint8_t *txData, uint32_t bytesEachWrite, bool isByteSwap)
  1291. {
  1292. assert(txData);
  1293. uint32_t wordToSend = 0;
  1294. switch (bytesEachWrite)
  1295. {
  1296. case 1:
  1297. wordToSend = *txData;
  1298. ++txData;
  1299. break;
  1300. case 2:
  1301. if (!isByteSwap)
  1302. {
  1303. wordToSend = *txData;
  1304. ++txData;
  1305. wordToSend |= (unsigned)(*txData) << 8U;
  1306. ++txData;
  1307. }
  1308. else
  1309. {
  1310. wordToSend = (unsigned)(*txData) << 8U;
  1311. ++txData;
  1312. wordToSend |= *txData;
  1313. ++txData;
  1314. }
  1315. break;
  1316. case 3:
  1317. if (!isByteSwap)
  1318. {
  1319. wordToSend = *txData;
  1320. ++txData;
  1321. wordToSend |= (unsigned)(*txData) << 8U;
  1322. ++txData;
  1323. wordToSend |= (unsigned)(*txData) << 16U;
  1324. ++txData;
  1325. }
  1326. else
  1327. {
  1328. wordToSend = (unsigned)(*txData) << 16U;
  1329. ++txData;
  1330. wordToSend |= (unsigned)(*txData) << 8U;
  1331. ++txData;
  1332. wordToSend |= *txData;
  1333. ++txData;
  1334. }
  1335. break;
  1336. case 4:
  1337. if (!isByteSwap)
  1338. {
  1339. wordToSend = *txData;
  1340. ++txData;
  1341. wordToSend |= (unsigned)(*txData) << 8U;
  1342. ++txData;
  1343. wordToSend |= (unsigned)(*txData) << 16U;
  1344. ++txData;
  1345. wordToSend |= (unsigned)(*txData) << 24U;
  1346. ++txData;
  1347. }
  1348. else
  1349. {
  1350. wordToSend = (unsigned)(*txData) << 24U;
  1351. ++txData;
  1352. wordToSend |= (unsigned)(*txData) << 16U;
  1353. ++txData;
  1354. wordToSend |= (unsigned)(*txData) << 8U;
  1355. ++txData;
  1356. wordToSend |= *txData;
  1357. ++txData;
  1358. }
  1359. break;
  1360. default:
  1361. assert(false);
  1362. break;
  1363. }
  1364. return wordToSend;
  1365. }
  1366. static void LPSPI_SeparateReadData(uint8_t *rxData, uint32_t readData, uint32_t bytesEachRead, bool isByteSwap)
  1367. {
  1368. assert(rxData);
  1369. switch (bytesEachRead)
  1370. {
  1371. case 1:
  1372. *rxData = readData;
  1373. ++rxData;
  1374. break;
  1375. case 2:
  1376. if (!isByteSwap)
  1377. {
  1378. *rxData = readData;
  1379. ++rxData;
  1380. *rxData = readData >> 8;
  1381. ++rxData;
  1382. }
  1383. else
  1384. {
  1385. *rxData = readData >> 8;
  1386. ++rxData;
  1387. *rxData = readData;
  1388. ++rxData;
  1389. }
  1390. break;
  1391. case 3:
  1392. if (!isByteSwap)
  1393. {
  1394. *rxData = readData;
  1395. ++rxData;
  1396. *rxData = readData >> 8;
  1397. ++rxData;
  1398. *rxData = readData >> 16;
  1399. ++rxData;
  1400. }
  1401. else
  1402. {
  1403. *rxData = readData >> 16;
  1404. ++rxData;
  1405. *rxData = readData >> 8;
  1406. ++rxData;
  1407. *rxData = readData;
  1408. ++rxData;
  1409. }
  1410. break;
  1411. case 4:
  1412. if (!isByteSwap)
  1413. {
  1414. *rxData = readData;
  1415. ++rxData;
  1416. *rxData = readData >> 8;
  1417. ++rxData;
  1418. *rxData = readData >> 16;
  1419. ++rxData;
  1420. *rxData = readData >> 24;
  1421. ++rxData;
  1422. }
  1423. else
  1424. {
  1425. *rxData = readData >> 24;
  1426. ++rxData;
  1427. *rxData = readData >> 16;
  1428. ++rxData;
  1429. *rxData = readData >> 8;
  1430. ++rxData;
  1431. *rxData = readData;
  1432. ++rxData;
  1433. }
  1434. break;
  1435. default:
  1436. assert(false);
  1437. break;
  1438. }
  1439. }
  1440. static void LPSPI_CommonIRQHandler(LPSPI_Type *base, void *param)
  1441. {
  1442. if (LPSPI_IsMaster(base))
  1443. {
  1444. s_lpspiMasterIsr(base, (lpspi_master_handle_t *)param);
  1445. }
  1446. else
  1447. {
  1448. s_lpspiSlaveIsr(base, (lpspi_slave_handle_t *)param);
  1449. }
  1450. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1451. exception return operation might vector to incorrect interrupt */
  1452. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1453. __DSB();
  1454. #endif
  1455. }
  1456. #if defined(LPSPI0)
  1457. void LPSPI0_DriverIRQHandler(void)
  1458. {
  1459. assert(s_lpspiHandle[0]);
  1460. LPSPI_CommonIRQHandler(LPSPI0, s_lpspiHandle[0]);
  1461. }
  1462. #endif
  1463. #if defined(LPSPI1)
  1464. void LPSPI1_DriverIRQHandler(void)
  1465. {
  1466. assert(s_lpspiHandle[1]);
  1467. LPSPI_CommonIRQHandler(LPSPI1, s_lpspiHandle[1]);
  1468. }
  1469. #endif
  1470. #if defined(LPSPI2)
  1471. void LPSPI2_DriverIRQHandler(void)
  1472. {
  1473. assert(s_lpspiHandle[2]);
  1474. LPSPI_CommonIRQHandler(LPSPI2, s_lpspiHandle[2]);
  1475. }
  1476. #endif
  1477. #if defined(LPSPI3)
  1478. void LPSPI3_DriverIRQHandler(void)
  1479. {
  1480. assert(s_lpspiHandle[3]);
  1481. LPSPI_CommonIRQHandler(LPSPI3, s_lpspiHandle[3]);
  1482. }
  1483. #endif
  1484. #if defined(LPSPI4)
  1485. void LPSPI4_DriverIRQHandler(void)
  1486. {
  1487. assert(s_lpspiHandle[4]);
  1488. LPSPI_CommonIRQHandler(LPSPI4, s_lpspiHandle[4]);
  1489. }
  1490. #endif
  1491. #if defined(LPSPI5)
  1492. void LPSPI5_DriverIRQHandler(void)
  1493. {
  1494. assert(s_lpspiHandle[5]);
  1495. LPSPI_CommonIRQHandler(LPSPI5, s_lpspiHandle[5]);
  1496. }
  1497. #endif
  1498. #if defined(DMA__LPSPI0)
  1499. void DMA_SPI0_INT_DriverIRQHandler(void)
  1500. {
  1501. assert(s_lpspiHandle[LPSPI_GetInstance(DMA__LPSPI0)]);
  1502. LPSPI_CommonIRQHandler(DMA__LPSPI0, s_lpspiHandle[LPSPI_GetInstance(DMA__LPSPI0)]);
  1503. }
  1504. #endif
  1505. #if defined(DMA__LPSPI1)
  1506. void DMA_SPI1_INT_DriverIRQHandler(void)
  1507. {
  1508. assert(s_lpspiHandle[LPSPI_GetInstance(DMA__LPSPI1)]);
  1509. LPSPI_CommonIRQHandler(DMA__LPSPI1, s_lpspiHandle[LPSPI_GetInstance(DMA__LPSPI1)]);
  1510. }
  1511. #endif
  1512. #if defined(DMA__LPSPI2)
  1513. void DMA_SPI2_INT_DriverIRQHandler(void)
  1514. {
  1515. assert(s_lpspiHandle[LPSPI_GetInstance(DMA__LPSPI2)]);
  1516. LPSPI_CommonIRQHandler(DMA__LPSPI2, s_lpspiHandle[LPSPI_GetInstance(DMA__LPSPI2)]);
  1517. }
  1518. #endif
  1519. #if defined(DMA__LPSPI3)
  1520. void DMA_SPI3_INT_DriverIRQHandler(void)
  1521. {
  1522. assert(s_lpspiHandle[LPSPI_GetInstance(DMA__LPSPI3)]);
  1523. LPSPI_CommonIRQHandler(DMA__LPSPI3, s_lpspiHandle[LPSPI_GetInstance(DMA__LPSPI3)]);
  1524. }
  1525. #endif