fsl_dspi.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_dspi.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /*! @brief Typedef for master interrupt handler. */
  39. typedef void (*dspi_master_isr_t)(SPI_Type *base, dspi_master_handle_t *handle);
  40. /*! @brief Typedef for slave interrupt handler. */
  41. typedef void (*dspi_slave_isr_t)(SPI_Type *base, dspi_slave_handle_t *handle);
  42. /*******************************************************************************
  43. * Prototypes
  44. ******************************************************************************/
  45. /*!
  46. * @brief Get instance number for DSPI module.
  47. *
  48. * @param base DSPI peripheral base address.
  49. */
  50. uint32_t DSPI_GetInstance(SPI_Type *base);
  51. /*!
  52. * @brief Configures the DSPI peripheral chip select polarity.
  53. *
  54. * This function takes in the desired peripheral chip select (Pcs) and it's corresponding desired polarity and
  55. * configures the Pcs signal to operate with the desired characteristic.
  56. *
  57. * @param base DSPI peripheral address.
  58. * @param pcs The particular peripheral chip select (parameter value is of type dspi_which_pcs_t) for which we wish to
  59. * apply the active high or active low characteristic.
  60. * @param activeLowOrHigh The setting for either "active high, inactive low (0)" or "active low, inactive high(1)" of
  61. * type dspi_pcs_polarity_config_t.
  62. */
  63. static void DSPI_SetOnePcsPolarity(SPI_Type *base, dspi_which_pcs_t pcs, dspi_pcs_polarity_config_t activeLowOrHigh);
  64. /*!
  65. * @brief Master fill up the TX FIFO with data.
  66. * This is not a public API.
  67. */
  68. static void DSPI_MasterTransferFillUpTxFifo(SPI_Type *base, dspi_master_handle_t *handle);
  69. /*!
  70. * @brief Master finish up a transfer.
  71. * It would call back if there is callback function and set the state to idle.
  72. * This is not a public API.
  73. */
  74. static void DSPI_MasterTransferComplete(SPI_Type *base, dspi_master_handle_t *handle);
  75. /*!
  76. * @brief Slave fill up the TX FIFO with data.
  77. * This is not a public API.
  78. */
  79. static void DSPI_SlaveTransferFillUpTxFifo(SPI_Type *base, dspi_slave_handle_t *handle);
  80. /*!
  81. * @brief Slave finish up a transfer.
  82. * It would call back if there is callback function and set the state to idle.
  83. * This is not a public API.
  84. */
  85. static void DSPI_SlaveTransferComplete(SPI_Type *base, dspi_slave_handle_t *handle);
  86. /*!
  87. * @brief DSPI common interrupt handler.
  88. *
  89. * @param base DSPI peripheral address.
  90. * @param handle pointer to g_dspiHandle which stores the transfer state.
  91. */
  92. static void DSPI_CommonIRQHandler(SPI_Type *base, void *param);
  93. /*!
  94. * @brief Master prepare the transfer.
  95. * Basically it set up dspi_master_handle .
  96. * This is not a public API.
  97. */
  98. static void DSPI_MasterTransferPrepare(SPI_Type *base, dspi_master_handle_t *handle, dspi_transfer_t *transfer);
  99. /*******************************************************************************
  100. * Variables
  101. ******************************************************************************/
  102. /* Defines constant value arrays for the baud rate pre-scalar and scalar divider values.*/
  103. static const uint32_t s_baudratePrescaler[] = {2, 3, 5, 7};
  104. static const uint32_t s_baudrateScaler[] = {2, 4, 6, 8, 16, 32, 64, 128,
  105. 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
  106. static const uint32_t s_delayPrescaler[] = {1, 3, 5, 7};
  107. static const uint32_t s_delayScaler[] = {2, 4, 8, 16, 32, 64, 128, 256,
  108. 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536};
  109. /*! @brief Pointers to dspi bases for each instance. */
  110. static SPI_Type *const s_dspiBases[] = SPI_BASE_PTRS;
  111. /*! @brief Pointers to dspi IRQ number for each instance. */
  112. static IRQn_Type const s_dspiIRQ[] = SPI_IRQS;
  113. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  114. /*! @brief Pointers to dspi clocks for each instance. */
  115. static clock_ip_name_t const s_dspiClock[] = DSPI_CLOCKS;
  116. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  117. /*! @brief Pointers to dspi handles for each instance. */
  118. static void *g_dspiHandle[ARRAY_SIZE(s_dspiBases)];
  119. /*! @brief Pointer to master IRQ handler for each instance. */
  120. static dspi_master_isr_t s_dspiMasterIsr;
  121. /*! @brief Pointer to slave IRQ handler for each instance. */
  122. static dspi_slave_isr_t s_dspiSlaveIsr;
  123. /* @brief Dummy data for each instance. This data is used when user's tx buffer is NULL*/
  124. volatile uint8_t s_dummyData[ARRAY_SIZE(s_dspiBases)] = {0};
  125. /**********************************************************************************************************************
  126. * Code
  127. *********************************************************************************************************************/
  128. uint32_t DSPI_GetInstance(SPI_Type *base)
  129. {
  130. uint32_t instance;
  131. /* Find the instance index from base address mappings. */
  132. for (instance = 0; instance < ARRAY_SIZE(s_dspiBases); instance++)
  133. {
  134. if (s_dspiBases[instance] == base)
  135. {
  136. break;
  137. }
  138. }
  139. assert(instance < ARRAY_SIZE(s_dspiBases));
  140. return instance;
  141. }
  142. void DSPI_SetDummyData(SPI_Type *base, uint8_t dummyData)
  143. {
  144. uint32_t instance = DSPI_GetInstance(base);
  145. s_dummyData[instance] = dummyData;
  146. }
  147. void DSPI_MasterInit(SPI_Type *base, const dspi_master_config_t *masterConfig, uint32_t srcClock_Hz)
  148. {
  149. assert(masterConfig);
  150. uint32_t temp;
  151. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  152. /* enable DSPI clock */
  153. CLOCK_EnableClock(s_dspiClock[DSPI_GetInstance(base)]);
  154. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  155. DSPI_Enable(base, true);
  156. DSPI_StopTransfer(base);
  157. DSPI_SetMasterSlaveMode(base, kDSPI_Master);
  158. temp = base->MCR & (~(SPI_MCR_CONT_SCKE_MASK | SPI_MCR_MTFE_MASK | SPI_MCR_ROOE_MASK | SPI_MCR_SMPL_PT_MASK |
  159. SPI_MCR_DIS_TXF_MASK | SPI_MCR_DIS_RXF_MASK));
  160. base->MCR = temp | SPI_MCR_CONT_SCKE(masterConfig->enableContinuousSCK) |
  161. SPI_MCR_MTFE(masterConfig->enableModifiedTimingFormat) |
  162. SPI_MCR_ROOE(masterConfig->enableRxFifoOverWrite) | SPI_MCR_SMPL_PT(masterConfig->samplePoint) |
  163. SPI_MCR_DIS_TXF(false) | SPI_MCR_DIS_RXF(false);
  164. DSPI_SetOnePcsPolarity(base, masterConfig->whichPcs, masterConfig->pcsActiveHighOrLow);
  165. if (0 == DSPI_MasterSetBaudRate(base, masterConfig->whichCtar, masterConfig->ctarConfig.baudRate, srcClock_Hz))
  166. {
  167. assert(false);
  168. }
  169. temp = base->CTAR[masterConfig->whichCtar] &
  170. ~(SPI_CTAR_FMSZ_MASK | SPI_CTAR_CPOL_MASK | SPI_CTAR_CPHA_MASK | SPI_CTAR_LSBFE_MASK);
  171. base->CTAR[masterConfig->whichCtar] =
  172. temp | SPI_CTAR_FMSZ(masterConfig->ctarConfig.bitsPerFrame - 1) | SPI_CTAR_CPOL(masterConfig->ctarConfig.cpol) |
  173. SPI_CTAR_CPHA(masterConfig->ctarConfig.cpha) | SPI_CTAR_LSBFE(masterConfig->ctarConfig.direction);
  174. DSPI_MasterSetDelayTimes(base, masterConfig->whichCtar, kDSPI_PcsToSck, srcClock_Hz,
  175. masterConfig->ctarConfig.pcsToSckDelayInNanoSec);
  176. DSPI_MasterSetDelayTimes(base, masterConfig->whichCtar, kDSPI_LastSckToPcs, srcClock_Hz,
  177. masterConfig->ctarConfig.lastSckToPcsDelayInNanoSec);
  178. DSPI_MasterSetDelayTimes(base, masterConfig->whichCtar, kDSPI_BetweenTransfer, srcClock_Hz,
  179. masterConfig->ctarConfig.betweenTransferDelayInNanoSec);
  180. DSPI_SetDummyData(base, DSPI_DUMMY_DATA);
  181. DSPI_StartTransfer(base);
  182. }
  183. void DSPI_MasterGetDefaultConfig(dspi_master_config_t *masterConfig)
  184. {
  185. assert(masterConfig);
  186. masterConfig->whichCtar = kDSPI_Ctar0;
  187. masterConfig->ctarConfig.baudRate = 500000;
  188. masterConfig->ctarConfig.bitsPerFrame = 8;
  189. masterConfig->ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
  190. masterConfig->ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
  191. masterConfig->ctarConfig.direction = kDSPI_MsbFirst;
  192. masterConfig->ctarConfig.pcsToSckDelayInNanoSec = 1000;
  193. masterConfig->ctarConfig.lastSckToPcsDelayInNanoSec = 1000;
  194. masterConfig->ctarConfig.betweenTransferDelayInNanoSec = 1000;
  195. masterConfig->whichPcs = kDSPI_Pcs0;
  196. masterConfig->pcsActiveHighOrLow = kDSPI_PcsActiveLow;
  197. masterConfig->enableContinuousSCK = false;
  198. masterConfig->enableRxFifoOverWrite = false;
  199. masterConfig->enableModifiedTimingFormat = false;
  200. masterConfig->samplePoint = kDSPI_SckToSin0Clock;
  201. }
  202. void DSPI_SlaveInit(SPI_Type *base, const dspi_slave_config_t *slaveConfig)
  203. {
  204. assert(slaveConfig);
  205. uint32_t temp = 0;
  206. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  207. /* enable DSPI clock */
  208. CLOCK_EnableClock(s_dspiClock[DSPI_GetInstance(base)]);
  209. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  210. DSPI_Enable(base, true);
  211. DSPI_StopTransfer(base);
  212. DSPI_SetMasterSlaveMode(base, kDSPI_Slave);
  213. temp = base->MCR & (~(SPI_MCR_CONT_SCKE_MASK | SPI_MCR_MTFE_MASK | SPI_MCR_ROOE_MASK | SPI_MCR_SMPL_PT_MASK |
  214. SPI_MCR_DIS_TXF_MASK | SPI_MCR_DIS_RXF_MASK));
  215. base->MCR = temp | SPI_MCR_CONT_SCKE(slaveConfig->enableContinuousSCK) |
  216. SPI_MCR_MTFE(slaveConfig->enableModifiedTimingFormat) |
  217. SPI_MCR_ROOE(slaveConfig->enableRxFifoOverWrite) | SPI_MCR_SMPL_PT(slaveConfig->samplePoint) |
  218. SPI_MCR_DIS_TXF(false) | SPI_MCR_DIS_RXF(false);
  219. DSPI_SetOnePcsPolarity(base, kDSPI_Pcs0, kDSPI_PcsActiveLow);
  220. temp = base->CTAR[slaveConfig->whichCtar] &
  221. ~(SPI_CTAR_FMSZ_MASK | SPI_CTAR_CPOL_MASK | SPI_CTAR_CPHA_MASK | SPI_CTAR_LSBFE_MASK);
  222. base->CTAR[slaveConfig->whichCtar] = temp | SPI_CTAR_SLAVE_FMSZ(slaveConfig->ctarConfig.bitsPerFrame - 1) |
  223. SPI_CTAR_SLAVE_CPOL(slaveConfig->ctarConfig.cpol) |
  224. SPI_CTAR_SLAVE_CPHA(slaveConfig->ctarConfig.cpha);
  225. DSPI_SetDummyData(base, DSPI_DUMMY_DATA);
  226. DSPI_StartTransfer(base);
  227. }
  228. void DSPI_SlaveGetDefaultConfig(dspi_slave_config_t *slaveConfig)
  229. {
  230. assert(slaveConfig);
  231. slaveConfig->whichCtar = kDSPI_Ctar0;
  232. slaveConfig->ctarConfig.bitsPerFrame = 8;
  233. slaveConfig->ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
  234. slaveConfig->ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
  235. slaveConfig->enableContinuousSCK = false;
  236. slaveConfig->enableRxFifoOverWrite = false;
  237. slaveConfig->enableModifiedTimingFormat = false;
  238. slaveConfig->samplePoint = kDSPI_SckToSin0Clock;
  239. }
  240. void DSPI_Deinit(SPI_Type *base)
  241. {
  242. DSPI_StopTransfer(base);
  243. DSPI_Enable(base, false);
  244. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  245. /* disable DSPI clock */
  246. CLOCK_DisableClock(s_dspiClock[DSPI_GetInstance(base)]);
  247. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  248. }
  249. static void DSPI_SetOnePcsPolarity(SPI_Type *base, dspi_which_pcs_t pcs, dspi_pcs_polarity_config_t activeLowOrHigh)
  250. {
  251. uint32_t temp;
  252. temp = base->MCR;
  253. if (activeLowOrHigh == kDSPI_PcsActiveLow)
  254. {
  255. temp |= SPI_MCR_PCSIS(pcs);
  256. }
  257. else
  258. {
  259. temp &= ~SPI_MCR_PCSIS(pcs);
  260. }
  261. base->MCR = temp;
  262. }
  263. uint32_t DSPI_MasterSetBaudRate(SPI_Type *base,
  264. dspi_ctar_selection_t whichCtar,
  265. uint32_t baudRate_Bps,
  266. uint32_t srcClock_Hz)
  267. {
  268. /* for master mode configuration, if slave mode detected, return 0*/
  269. if (!DSPI_IsMaster(base))
  270. {
  271. return 0;
  272. }
  273. uint32_t temp;
  274. uint32_t prescaler, bestPrescaler;
  275. uint32_t scaler, bestScaler;
  276. uint32_t dbr, bestDbr;
  277. uint32_t realBaudrate, bestBaudrate;
  278. uint32_t diff, min_diff;
  279. uint32_t baudrate = baudRate_Bps;
  280. /* find combination of prescaler and scaler resulting in baudrate closest to the requested value */
  281. min_diff = 0xFFFFFFFFU;
  282. bestPrescaler = 0;
  283. bestScaler = 0;
  284. bestDbr = 1;
  285. bestBaudrate = 0; /* required to avoid compilation warning */
  286. /* In all for loops, if min_diff = 0, the exit for loop*/
  287. for (prescaler = 0; (prescaler < 4) && min_diff; prescaler++)
  288. {
  289. for (scaler = 0; (scaler < 16) && min_diff; scaler++)
  290. {
  291. for (dbr = 1; (dbr < 3) && min_diff; dbr++)
  292. {
  293. realBaudrate = ((srcClock_Hz * dbr) / (s_baudratePrescaler[prescaler] * (s_baudrateScaler[scaler])));
  294. /* calculate the baud rate difference based on the conditional statement that states that the calculated
  295. * baud rate must not exceed the desired baud rate.
  296. */
  297. if (baudrate >= realBaudrate)
  298. {
  299. diff = baudrate - realBaudrate;
  300. if (min_diff > diff)
  301. {
  302. /* a better match found */
  303. min_diff = diff;
  304. bestPrescaler = prescaler;
  305. bestScaler = scaler;
  306. bestBaudrate = realBaudrate;
  307. bestDbr = dbr;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. /* write the best dbr, prescalar, and baud rate scalar to the CTAR */
  314. temp = base->CTAR[whichCtar] & ~(SPI_CTAR_DBR_MASK | SPI_CTAR_PBR_MASK | SPI_CTAR_BR_MASK);
  315. base->CTAR[whichCtar] = temp | ((bestDbr - 1) << SPI_CTAR_DBR_SHIFT) | (bestPrescaler << SPI_CTAR_PBR_SHIFT) |
  316. (bestScaler << SPI_CTAR_BR_SHIFT);
  317. /* return the actual calculated baud rate */
  318. return bestBaudrate;
  319. }
  320. void DSPI_MasterSetDelayScaler(
  321. SPI_Type *base, dspi_ctar_selection_t whichCtar, uint32_t prescaler, uint32_t scaler, dspi_delay_type_t whichDelay)
  322. {
  323. /* these settings are only relevant in master mode */
  324. if (DSPI_IsMaster(base))
  325. {
  326. switch (whichDelay)
  327. {
  328. case kDSPI_PcsToSck:
  329. base->CTAR[whichCtar] = (base->CTAR[whichCtar] & (~SPI_CTAR_PCSSCK_MASK) & (~SPI_CTAR_CSSCK_MASK)) |
  330. SPI_CTAR_PCSSCK(prescaler) | SPI_CTAR_CSSCK(scaler);
  331. break;
  332. case kDSPI_LastSckToPcs:
  333. base->CTAR[whichCtar] = (base->CTAR[whichCtar] & (~SPI_CTAR_PASC_MASK) & (~SPI_CTAR_ASC_MASK)) |
  334. SPI_CTAR_PASC(prescaler) | SPI_CTAR_ASC(scaler);
  335. break;
  336. case kDSPI_BetweenTransfer:
  337. base->CTAR[whichCtar] = (base->CTAR[whichCtar] & (~SPI_CTAR_PDT_MASK) & (~SPI_CTAR_DT_MASK)) |
  338. SPI_CTAR_PDT(prescaler) | SPI_CTAR_DT(scaler);
  339. break;
  340. default:
  341. break;
  342. }
  343. }
  344. }
  345. uint32_t DSPI_MasterSetDelayTimes(SPI_Type *base,
  346. dspi_ctar_selection_t whichCtar,
  347. dspi_delay_type_t whichDelay,
  348. uint32_t srcClock_Hz,
  349. uint32_t delayTimeInNanoSec)
  350. {
  351. /* for master mode configuration, if slave mode detected, return 0 */
  352. if (!DSPI_IsMaster(base))
  353. {
  354. return 0;
  355. }
  356. uint32_t prescaler, bestPrescaler;
  357. uint32_t scaler, bestScaler;
  358. uint32_t realDelay, bestDelay;
  359. uint32_t diff, min_diff;
  360. uint32_t initialDelayNanoSec;
  361. /* find combination of prescaler and scaler resulting in the delay closest to the
  362. * requested value
  363. */
  364. min_diff = 0xFFFFFFFFU;
  365. /* Initialize prescaler and scaler to their max values to generate the max delay */
  366. bestPrescaler = 0x3;
  367. bestScaler = 0xF;
  368. bestDelay = (((1000000000U * 4) / srcClock_Hz) * s_delayPrescaler[bestPrescaler] * s_delayScaler[bestScaler]) / 4;
  369. /* First calculate the initial, default delay */
  370. initialDelayNanoSec = 1000000000U / srcClock_Hz * 2;
  371. /* If the initial, default delay is already greater than the desired delay, then
  372. * set the delays to their initial value (0) and return the delay. In other words,
  373. * there is no way to decrease the delay value further.
  374. */
  375. if (initialDelayNanoSec >= delayTimeInNanoSec)
  376. {
  377. DSPI_MasterSetDelayScaler(base, whichCtar, 0, 0, whichDelay);
  378. return initialDelayNanoSec;
  379. }
  380. /* In all for loops, if min_diff = 0, the exit for loop */
  381. for (prescaler = 0; (prescaler < 4) && min_diff; prescaler++)
  382. {
  383. for (scaler = 0; (scaler < 16) && min_diff; scaler++)
  384. {
  385. realDelay = ((4000000000U / srcClock_Hz) * s_delayPrescaler[prescaler] * s_delayScaler[scaler]) / 4;
  386. /* calculate the delay difference based on the conditional statement
  387. * that states that the calculated delay must not be less then the desired delay
  388. */
  389. if (realDelay >= delayTimeInNanoSec)
  390. {
  391. diff = realDelay - delayTimeInNanoSec;
  392. if (min_diff > diff)
  393. {
  394. /* a better match found */
  395. min_diff = diff;
  396. bestPrescaler = prescaler;
  397. bestScaler = scaler;
  398. bestDelay = realDelay;
  399. }
  400. }
  401. }
  402. }
  403. /* write the best dbr, prescalar, and baud rate scalar to the CTAR */
  404. DSPI_MasterSetDelayScaler(base, whichCtar, bestPrescaler, bestScaler, whichDelay);
  405. /* return the actual calculated baud rate */
  406. return bestDelay;
  407. }
  408. void DSPI_GetDefaultDataCommandConfig(dspi_command_data_config_t *command)
  409. {
  410. assert(command);
  411. command->isPcsContinuous = false;
  412. command->whichCtar = kDSPI_Ctar0;
  413. command->whichPcs = kDSPI_Pcs0;
  414. command->isEndOfQueue = false;
  415. command->clearTransferCount = false;
  416. }
  417. void DSPI_MasterWriteDataBlocking(SPI_Type *base, dspi_command_data_config_t *command, uint16_t data)
  418. {
  419. assert(command);
  420. /* First, clear Transmit Complete Flag (TCF) */
  421. DSPI_ClearStatusFlags(base, kDSPI_TxCompleteFlag);
  422. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
  423. {
  424. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  425. }
  426. base->PUSHR = SPI_PUSHR_CONT(command->isPcsContinuous) | SPI_PUSHR_CTAS(command->whichCtar) |
  427. SPI_PUSHR_PCS(command->whichPcs) | SPI_PUSHR_EOQ(command->isEndOfQueue) |
  428. SPI_PUSHR_CTCNT(command->clearTransferCount) | SPI_PUSHR_TXDATA(data);
  429. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  430. /* Wait till TCF sets */
  431. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxCompleteFlag))
  432. {
  433. }
  434. }
  435. void DSPI_MasterWriteCommandDataBlocking(SPI_Type *base, uint32_t data)
  436. {
  437. /* First, clear Transmit Complete Flag (TCF) */
  438. DSPI_ClearStatusFlags(base, kDSPI_TxCompleteFlag);
  439. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
  440. {
  441. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  442. }
  443. base->PUSHR = data;
  444. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  445. /* Wait till TCF sets */
  446. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxCompleteFlag))
  447. {
  448. }
  449. }
  450. void DSPI_SlaveWriteDataBlocking(SPI_Type *base, uint32_t data)
  451. {
  452. /* First, clear Transmit Complete Flag (TCF) */
  453. DSPI_ClearStatusFlags(base, kDSPI_TxCompleteFlag);
  454. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
  455. {
  456. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  457. }
  458. base->PUSHR_SLAVE = data;
  459. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  460. /* Wait till TCF sets */
  461. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxCompleteFlag))
  462. {
  463. }
  464. }
  465. void DSPI_EnableInterrupts(SPI_Type *base, uint32_t mask)
  466. {
  467. if (mask & SPI_RSER_TFFF_RE_MASK)
  468. {
  469. base->RSER &= ~SPI_RSER_TFFF_DIRS_MASK;
  470. }
  471. if (mask & SPI_RSER_RFDF_RE_MASK)
  472. {
  473. base->RSER &= ~SPI_RSER_RFDF_DIRS_MASK;
  474. }
  475. base->RSER |= mask;
  476. }
  477. /*Transactional APIs -- Master*/
  478. void DSPI_MasterTransferCreateHandle(SPI_Type *base,
  479. dspi_master_handle_t *handle,
  480. dspi_master_transfer_callback_t callback,
  481. void *userData)
  482. {
  483. assert(handle);
  484. /* Zero the handle. */
  485. memset(handle, 0, sizeof(*handle));
  486. g_dspiHandle[DSPI_GetInstance(base)] = handle;
  487. handle->callback = callback;
  488. handle->userData = userData;
  489. }
  490. status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer)
  491. {
  492. assert(transfer);
  493. uint16_t wordToSend = 0;
  494. uint16_t wordReceived = 0;
  495. uint8_t dummyData = s_dummyData[DSPI_GetInstance(base)];
  496. uint8_t bitsPerFrame;
  497. uint32_t command;
  498. uint32_t lastCommand;
  499. uint8_t *txData;
  500. uint8_t *rxData;
  501. uint32_t remainingSendByteCount;
  502. uint32_t remainingReceiveByteCount;
  503. uint32_t fifoSize;
  504. dspi_command_data_config_t commandStruct;
  505. /* If the transfer count is zero, then return immediately.*/
  506. if (transfer->dataSize == 0)
  507. {
  508. return kStatus_InvalidArgument;
  509. }
  510. DSPI_StopTransfer(base);
  511. DSPI_DisableInterrupts(base, kDSPI_AllInterruptEnable);
  512. DSPI_FlushFifo(base, true, true);
  513. DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
  514. /*Calculate the command and lastCommand*/
  515. commandStruct.whichPcs =
  516. (dspi_which_pcs_t)(1U << ((transfer->configFlags & DSPI_MASTER_PCS_MASK) >> DSPI_MASTER_PCS_SHIFT));
  517. commandStruct.isEndOfQueue = false;
  518. commandStruct.clearTransferCount = false;
  519. commandStruct.whichCtar =
  520. (dspi_ctar_selection_t)((transfer->configFlags & DSPI_MASTER_CTAR_MASK) >> DSPI_MASTER_CTAR_SHIFT);
  521. commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterPcsContinuous);
  522. command = DSPI_MasterGetFormattedCommand(&(commandStruct));
  523. commandStruct.isEndOfQueue = true;
  524. commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterActiveAfterTransfer);
  525. lastCommand = DSPI_MasterGetFormattedCommand(&(commandStruct));
  526. /*Calculate the bitsPerFrame*/
  527. bitsPerFrame = ((base->CTAR[commandStruct.whichCtar] & SPI_CTAR_FMSZ_MASK) >> SPI_CTAR_FMSZ_SHIFT) + 1;
  528. txData = transfer->txData;
  529. rxData = transfer->rxData;
  530. remainingSendByteCount = transfer->dataSize;
  531. remainingReceiveByteCount = transfer->dataSize;
  532. if ((base->MCR & SPI_MCR_DIS_RXF_MASK) || (base->MCR & SPI_MCR_DIS_TXF_MASK))
  533. {
  534. fifoSize = 1;
  535. }
  536. else
  537. {
  538. fifoSize = FSL_FEATURE_DSPI_FIFO_SIZEn(base);
  539. }
  540. DSPI_StartTransfer(base);
  541. if (bitsPerFrame <= 8)
  542. {
  543. while (remainingSendByteCount > 0)
  544. {
  545. if (remainingSendByteCount == 1)
  546. {
  547. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
  548. {
  549. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  550. }
  551. if (txData != NULL)
  552. {
  553. base->PUSHR = (*txData) | (lastCommand);
  554. txData++;
  555. }
  556. else
  557. {
  558. base->PUSHR = (lastCommand) | (dummyData);
  559. }
  560. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  561. remainingSendByteCount--;
  562. while (remainingReceiveByteCount > 0)
  563. {
  564. if (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
  565. {
  566. if (rxData != NULL)
  567. {
  568. /* Read data from POPR*/
  569. *(rxData) = DSPI_ReadData(base);
  570. rxData++;
  571. }
  572. else
  573. {
  574. DSPI_ReadData(base);
  575. }
  576. remainingReceiveByteCount--;
  577. DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
  578. }
  579. }
  580. }
  581. else
  582. {
  583. /*Wait until Tx Fifo is not full*/
  584. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
  585. {
  586. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  587. }
  588. if (txData != NULL)
  589. {
  590. base->PUSHR = command | (uint16_t)(*txData);
  591. txData++;
  592. }
  593. else
  594. {
  595. base->PUSHR = command | dummyData;
  596. }
  597. remainingSendByteCount--;
  598. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  599. while ((remainingReceiveByteCount - remainingSendByteCount) >= fifoSize)
  600. {
  601. if (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
  602. {
  603. if (rxData != NULL)
  604. {
  605. *(rxData) = DSPI_ReadData(base);
  606. rxData++;
  607. }
  608. else
  609. {
  610. DSPI_ReadData(base);
  611. }
  612. remainingReceiveByteCount--;
  613. DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
  614. }
  615. }
  616. }
  617. }
  618. }
  619. else
  620. {
  621. while (remainingSendByteCount > 0)
  622. {
  623. if (remainingSendByteCount <= 2)
  624. {
  625. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
  626. {
  627. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  628. }
  629. if (txData != NULL)
  630. {
  631. wordToSend = *(txData);
  632. ++txData;
  633. if (remainingSendByteCount > 1)
  634. {
  635. wordToSend |= (unsigned)(*(txData)) << 8U;
  636. ++txData;
  637. }
  638. }
  639. else
  640. {
  641. wordToSend = dummyData;
  642. }
  643. base->PUSHR = lastCommand | wordToSend;
  644. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  645. remainingSendByteCount = 0;
  646. while (remainingReceiveByteCount > 0)
  647. {
  648. if (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
  649. {
  650. wordReceived = DSPI_ReadData(base);
  651. if (remainingReceiveByteCount != 1)
  652. {
  653. if (rxData != NULL)
  654. {
  655. *(rxData) = wordReceived;
  656. ++rxData;
  657. *(rxData) = wordReceived >> 8;
  658. ++rxData;
  659. }
  660. remainingReceiveByteCount -= 2;
  661. }
  662. else
  663. {
  664. if (rxData != NULL)
  665. {
  666. *(rxData) = wordReceived;
  667. ++rxData;
  668. }
  669. remainingReceiveByteCount--;
  670. }
  671. DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
  672. }
  673. }
  674. }
  675. else
  676. {
  677. /*Wait until Tx Fifo is not full*/
  678. while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
  679. {
  680. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  681. }
  682. if (txData != NULL)
  683. {
  684. wordToSend = *(txData);
  685. ++txData;
  686. wordToSend |= (unsigned)(*(txData)) << 8U;
  687. ++txData;
  688. }
  689. else
  690. {
  691. wordToSend = dummyData;
  692. }
  693. base->PUSHR = command | wordToSend;
  694. remainingSendByteCount -= 2;
  695. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  696. while (((remainingReceiveByteCount - remainingSendByteCount) / 2) >= fifoSize)
  697. {
  698. if (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
  699. {
  700. wordReceived = DSPI_ReadData(base);
  701. if (rxData != NULL)
  702. {
  703. *rxData = wordReceived;
  704. ++rxData;
  705. *rxData = wordReceived >> 8;
  706. ++rxData;
  707. }
  708. remainingReceiveByteCount -= 2;
  709. DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
  710. }
  711. }
  712. }
  713. }
  714. }
  715. return kStatus_Success;
  716. }
  717. static void DSPI_MasterTransferPrepare(SPI_Type *base, dspi_master_handle_t *handle, dspi_transfer_t *transfer)
  718. {
  719. assert(handle);
  720. assert(transfer);
  721. dspi_command_data_config_t commandStruct;
  722. DSPI_StopTransfer(base);
  723. DSPI_FlushFifo(base, true, true);
  724. DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
  725. commandStruct.whichPcs =
  726. (dspi_which_pcs_t)(1U << ((transfer->configFlags & DSPI_MASTER_PCS_MASK) >> DSPI_MASTER_PCS_SHIFT));
  727. commandStruct.isEndOfQueue = false;
  728. commandStruct.clearTransferCount = false;
  729. commandStruct.whichCtar =
  730. (dspi_ctar_selection_t)((transfer->configFlags & DSPI_MASTER_CTAR_MASK) >> DSPI_MASTER_CTAR_SHIFT);
  731. commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterPcsContinuous);
  732. handle->command = DSPI_MasterGetFormattedCommand(&(commandStruct));
  733. commandStruct.isEndOfQueue = true;
  734. commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterActiveAfterTransfer);
  735. handle->lastCommand = DSPI_MasterGetFormattedCommand(&(commandStruct));
  736. handle->bitsPerFrame = ((base->CTAR[commandStruct.whichCtar] & SPI_CTAR_FMSZ_MASK) >> SPI_CTAR_FMSZ_SHIFT) + 1;
  737. if ((base->MCR & SPI_MCR_DIS_RXF_MASK) || (base->MCR & SPI_MCR_DIS_TXF_MASK))
  738. {
  739. handle->fifoSize = 1;
  740. }
  741. else
  742. {
  743. handle->fifoSize = FSL_FEATURE_DSPI_FIFO_SIZEn(base);
  744. }
  745. handle->txData = transfer->txData;
  746. handle->rxData = transfer->rxData;
  747. handle->remainingSendByteCount = transfer->dataSize;
  748. handle->remainingReceiveByteCount = transfer->dataSize;
  749. handle->totalByteCount = transfer->dataSize;
  750. }
  751. status_t DSPI_MasterTransferNonBlocking(SPI_Type *base, dspi_master_handle_t *handle, dspi_transfer_t *transfer)
  752. {
  753. assert(handle);
  754. assert(transfer);
  755. /* If the transfer count is zero, then return immediately.*/
  756. if (transfer->dataSize == 0)
  757. {
  758. return kStatus_InvalidArgument;
  759. }
  760. /* Check that we're not busy.*/
  761. if (handle->state == kDSPI_Busy)
  762. {
  763. return kStatus_DSPI_Busy;
  764. }
  765. handle->state = kDSPI_Busy;
  766. /* Disable the NVIC for DSPI peripheral. */
  767. DisableIRQ(s_dspiIRQ[DSPI_GetInstance(base)]);
  768. DSPI_MasterTransferPrepare(base, handle, transfer);
  769. /* RX FIFO Drain request: RFDF_RE to enable RFDF interrupt
  770. * Since SPI is a synchronous interface, we only need to enable the RX interrupt.
  771. * The IRQ handler will get the status of RX and TX interrupt flags.
  772. */
  773. s_dspiMasterIsr = DSPI_MasterTransferHandleIRQ;
  774. DSPI_EnableInterrupts(base, kDSPI_RxFifoDrainRequestInterruptEnable);
  775. DSPI_StartTransfer(base);
  776. /* Fill up the Tx FIFO to trigger the transfer. */
  777. DSPI_MasterTransferFillUpTxFifo(base, handle);
  778. /* Enable the NVIC for DSPI peripheral. */
  779. EnableIRQ(s_dspiIRQ[DSPI_GetInstance(base)]);
  780. return kStatus_Success;
  781. }
  782. status_t DSPI_MasterHalfDuplexTransferBlocking(SPI_Type *base, dspi_half_duplex_transfer_t *xfer)
  783. {
  784. assert(xfer);
  785. dspi_transfer_t tempXfer = {0};
  786. status_t status;
  787. if (xfer->isTransmitFirst)
  788. {
  789. tempXfer.txData = xfer->txData;
  790. tempXfer.rxData = NULL;
  791. tempXfer.dataSize = xfer->txDataSize;
  792. }
  793. else
  794. {
  795. tempXfer.txData = NULL;
  796. tempXfer.rxData = xfer->rxData;
  797. tempXfer.dataSize = xfer->rxDataSize;
  798. }
  799. /* If the pcs pin keep assert between transmit and receive. */
  800. if (xfer->isPcsAssertInTransfer)
  801. {
  802. tempXfer.configFlags = (xfer->configFlags) | kDSPI_MasterActiveAfterTransfer;
  803. }
  804. else
  805. {
  806. tempXfer.configFlags = (xfer->configFlags) & (uint32_t)(~kDSPI_MasterActiveAfterTransfer);
  807. }
  808. status = DSPI_MasterTransferBlocking(base, &tempXfer);
  809. if (status != kStatus_Success)
  810. {
  811. return status;
  812. }
  813. if (xfer->isTransmitFirst)
  814. {
  815. tempXfer.txData = NULL;
  816. tempXfer.rxData = xfer->rxData;
  817. tempXfer.dataSize = xfer->rxDataSize;
  818. }
  819. else
  820. {
  821. tempXfer.txData = xfer->txData;
  822. tempXfer.rxData = NULL;
  823. tempXfer.dataSize = xfer->txDataSize;
  824. }
  825. tempXfer.configFlags = xfer->configFlags;
  826. /* DSPI transfer blocking. */
  827. status = DSPI_MasterTransferBlocking(base, &tempXfer);
  828. return status;
  829. }
  830. status_t DSPI_MasterHalfDuplexTransferNonBlocking(SPI_Type *base,
  831. dspi_master_handle_t *handle,
  832. dspi_half_duplex_transfer_t *xfer)
  833. {
  834. assert(xfer);
  835. assert(handle);
  836. dspi_transfer_t tempXfer = {0};
  837. status_t status;
  838. if (xfer->isTransmitFirst)
  839. {
  840. tempXfer.txData = xfer->txData;
  841. tempXfer.rxData = NULL;
  842. tempXfer.dataSize = xfer->txDataSize;
  843. }
  844. else
  845. {
  846. tempXfer.txData = NULL;
  847. tempXfer.rxData = xfer->rxData;
  848. tempXfer.dataSize = xfer->rxDataSize;
  849. }
  850. /* If the pcs pin keep assert between transmit and receive. */
  851. if (xfer->isPcsAssertInTransfer)
  852. {
  853. tempXfer.configFlags = (xfer->configFlags) | kDSPI_MasterActiveAfterTransfer;
  854. }
  855. else
  856. {
  857. tempXfer.configFlags = (xfer->configFlags) & (uint32_t)(~kDSPI_MasterActiveAfterTransfer);
  858. }
  859. status = DSPI_MasterTransferBlocking(base, &tempXfer);
  860. if (status != kStatus_Success)
  861. {
  862. return status;
  863. }
  864. if (xfer->isTransmitFirst)
  865. {
  866. tempXfer.txData = NULL;
  867. tempXfer.rxData = xfer->rxData;
  868. tempXfer.dataSize = xfer->rxDataSize;
  869. }
  870. else
  871. {
  872. tempXfer.txData = xfer->txData;
  873. tempXfer.rxData = NULL;
  874. tempXfer.dataSize = xfer->txDataSize;
  875. }
  876. tempXfer.configFlags = xfer->configFlags;
  877. status = DSPI_MasterTransferNonBlocking(base, handle, &tempXfer);
  878. return status;
  879. }
  880. status_t DSPI_MasterTransferGetCount(SPI_Type *base, dspi_master_handle_t *handle, size_t *count)
  881. {
  882. assert(handle);
  883. if (!count)
  884. {
  885. return kStatus_InvalidArgument;
  886. }
  887. /* Catch when there is not an active transfer. */
  888. if (handle->state != kDSPI_Busy)
  889. {
  890. *count = 0;
  891. return kStatus_NoTransferInProgress;
  892. }
  893. *count = handle->totalByteCount - handle->remainingReceiveByteCount;
  894. return kStatus_Success;
  895. }
  896. static void DSPI_MasterTransferComplete(SPI_Type *base, dspi_master_handle_t *handle)
  897. {
  898. assert(handle);
  899. /* Disable interrupt requests*/
  900. DSPI_DisableInterrupts(base, kDSPI_RxFifoDrainRequestInterruptEnable | kDSPI_TxFifoFillRequestInterruptEnable);
  901. status_t status = 0;
  902. if (handle->state == kDSPI_Error)
  903. {
  904. status = kStatus_DSPI_Error;
  905. }
  906. else
  907. {
  908. status = kStatus_Success;
  909. }
  910. handle->state = kDSPI_Idle;
  911. if (handle->callback)
  912. {
  913. handle->callback(base, handle, status, handle->userData);
  914. }
  915. }
  916. static void DSPI_MasterTransferFillUpTxFifo(SPI_Type *base, dspi_master_handle_t *handle)
  917. {
  918. assert(handle);
  919. uint16_t wordToSend = 0;
  920. uint8_t dummyData = s_dummyData[DSPI_GetInstance(base)];
  921. /* If bits/frame is greater than one byte */
  922. if (handle->bitsPerFrame > 8)
  923. {
  924. /* Fill the fifo until it is full or until the send word count is 0 or until the difference
  925. * between the remainingReceiveByteCount and remainingSendByteCount equals the FIFO depth.
  926. * The reason for checking the difference is to ensure we only send as much as the
  927. * RX FIFO can receive.
  928. * For this case where bitsPerFrame > 8, each entry in the FIFO contains 2 bytes of the
  929. * send data, hence the difference between the remainingReceiveByteCount and
  930. * remainingSendByteCount must be divided by 2 to convert this difference into a
  931. * 16-bit (2 byte) value.
  932. */
  933. while ((DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag) &&
  934. ((handle->remainingReceiveByteCount - handle->remainingSendByteCount) / 2 < handle->fifoSize))
  935. {
  936. if (handle->remainingSendByteCount <= 2)
  937. {
  938. if (handle->txData)
  939. {
  940. if (handle->remainingSendByteCount == 1)
  941. {
  942. wordToSend = *(handle->txData);
  943. }
  944. else
  945. {
  946. wordToSend = *(handle->txData);
  947. ++handle->txData; /* increment to next data byte */
  948. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  949. }
  950. }
  951. else
  952. {
  953. wordToSend = dummyData;
  954. }
  955. handle->remainingSendByteCount = 0;
  956. base->PUSHR = handle->lastCommand | wordToSend;
  957. }
  958. /* For all words except the last word */
  959. else
  960. {
  961. if (handle->txData)
  962. {
  963. wordToSend = *(handle->txData);
  964. ++handle->txData; /* increment to next data byte */
  965. wordToSend |= (unsigned)(*(handle->txData)) << 8U;
  966. ++handle->txData; /* increment to next data byte */
  967. }
  968. else
  969. {
  970. wordToSend = dummyData;
  971. }
  972. handle->remainingSendByteCount -= 2; /* decrement remainingSendByteCount by 2 */
  973. base->PUSHR = handle->command | wordToSend;
  974. }
  975. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  976. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  977. /* exit loop if send count is zero, else update local variables for next loop.
  978. * If this is the first time write to the PUSHR, write only once.
  979. */
  980. if ((handle->remainingSendByteCount == 0) || (handle->remainingSendByteCount == handle->totalByteCount - 2))
  981. {
  982. break;
  983. }
  984. } /* End of TX FIFO fill while loop */
  985. }
  986. /* Optimized for bits/frame less than or equal to one byte. */
  987. else
  988. {
  989. /* Fill the fifo until it is full or until the send word count is 0 or until the difference
  990. * between the remainingReceiveByteCount and remainingSendByteCount equals the FIFO depth.
  991. * The reason for checking the difference is to ensure we only send as much as the
  992. * RX FIFO can receive.
  993. */
  994. while ((DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag) &&
  995. ((handle->remainingReceiveByteCount - handle->remainingSendByteCount) < handle->fifoSize))
  996. {
  997. if (handle->txData)
  998. {
  999. wordToSend = *(handle->txData);
  1000. ++handle->txData;
  1001. }
  1002. else
  1003. {
  1004. wordToSend = dummyData;
  1005. }
  1006. if (handle->remainingSendByteCount == 1)
  1007. {
  1008. base->PUSHR = handle->lastCommand | wordToSend;
  1009. }
  1010. else
  1011. {
  1012. base->PUSHR = handle->command | wordToSend;
  1013. }
  1014. /* Try to clear the TFFF; if the TX FIFO is full this will clear */
  1015. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  1016. --handle->remainingSendByteCount;
  1017. /* exit loop if send count is zero, else update local variables for next loop
  1018. * If this is the first time write to the PUSHR, write only once.
  1019. */
  1020. if ((handle->remainingSendByteCount == 0) || (handle->remainingSendByteCount == handle->totalByteCount - 1))
  1021. {
  1022. break;
  1023. }
  1024. }
  1025. }
  1026. }
  1027. void DSPI_MasterTransferAbort(SPI_Type *base, dspi_master_handle_t *handle)
  1028. {
  1029. assert(handle);
  1030. DSPI_StopTransfer(base);
  1031. /* Disable interrupt requests*/
  1032. DSPI_DisableInterrupts(base, kDSPI_RxFifoDrainRequestInterruptEnable | kDSPI_TxFifoFillRequestInterruptEnable);
  1033. handle->state = kDSPI_Idle;
  1034. }
  1035. void DSPI_MasterTransferHandleIRQ(SPI_Type *base, dspi_master_handle_t *handle)
  1036. {
  1037. assert(handle);
  1038. /* RECEIVE IRQ handler: Check read buffer only if there are remaining bytes to read. */
  1039. if (handle->remainingReceiveByteCount)
  1040. {
  1041. /* Check read buffer.*/
  1042. uint16_t wordReceived; /* Maximum supported data bit length in master mode is 16-bits */
  1043. /* If bits/frame is greater than one byte */
  1044. if (handle->bitsPerFrame > 8)
  1045. {
  1046. while (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
  1047. {
  1048. wordReceived = DSPI_ReadData(base);
  1049. /* clear the rx fifo drain request, needed for non-DMA applications as this flag
  1050. * will remain set even if the rx fifo is empty. By manually clearing this flag, it
  1051. * either remain clear if no more data is in the fifo, or it will set if there is
  1052. * more data in the fifo.
  1053. */
  1054. DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
  1055. /* Store read bytes into rx buffer only if a buffer pointer was provided */
  1056. if (handle->rxData)
  1057. {
  1058. /* For the last word received, if there is an extra byte due to the odd transfer
  1059. * byte count, only save the last byte and discard the upper byte
  1060. */
  1061. if (handle->remainingReceiveByteCount == 1)
  1062. {
  1063. *handle->rxData = wordReceived; /* Write first data byte */
  1064. --handle->remainingReceiveByteCount;
  1065. }
  1066. else
  1067. {
  1068. *handle->rxData = wordReceived; /* Write first data byte */
  1069. ++handle->rxData; /* increment to next data byte */
  1070. *handle->rxData = wordReceived >> 8; /* Write second data byte */
  1071. ++handle->rxData; /* increment to next data byte */
  1072. handle->remainingReceiveByteCount -= 2;
  1073. }
  1074. }
  1075. else
  1076. {
  1077. if (handle->remainingReceiveByteCount == 1)
  1078. {
  1079. --handle->remainingReceiveByteCount;
  1080. }
  1081. else
  1082. {
  1083. handle->remainingReceiveByteCount -= 2;
  1084. }
  1085. }
  1086. if (handle->remainingReceiveByteCount == 0)
  1087. {
  1088. break;
  1089. }
  1090. } /* End of RX FIFO drain while loop */
  1091. }
  1092. /* Optimized for bits/frame less than or equal to one byte. */
  1093. else
  1094. {
  1095. while (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
  1096. {
  1097. wordReceived = DSPI_ReadData(base);
  1098. /* clear the rx fifo drain request, needed for non-DMA applications as this flag
  1099. * will remain set even if the rx fifo is empty. By manually clearing this flag, it
  1100. * either remain clear if no more data is in the fifo, or it will set if there is
  1101. * more data in the fifo.
  1102. */
  1103. DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
  1104. /* Store read bytes into rx buffer only if a buffer pointer was provided */
  1105. if (handle->rxData)
  1106. {
  1107. *handle->rxData = wordReceived;
  1108. ++handle->rxData;
  1109. }
  1110. --handle->remainingReceiveByteCount;
  1111. if (handle->remainingReceiveByteCount == 0)
  1112. {
  1113. break;
  1114. }
  1115. } /* End of RX FIFO drain while loop */
  1116. }
  1117. }
  1118. /* Check write buffer. We always have to send a word in order to keep the transfer
  1119. * moving. So if the caller didn't provide a send buffer, we just send a zero.
  1120. */
  1121. if (handle->remainingSendByteCount)
  1122. {
  1123. DSPI_MasterTransferFillUpTxFifo(base, handle);
  1124. }
  1125. /* Check if we're done with this transfer.*/
  1126. if ((handle->remainingSendByteCount == 0) && (handle->remainingReceiveByteCount == 0))
  1127. {
  1128. /* Complete the transfer and disable the interrupts */
  1129. DSPI_MasterTransferComplete(base, handle);
  1130. }
  1131. }
  1132. /*Transactional APIs -- Slave*/
  1133. void DSPI_SlaveTransferCreateHandle(SPI_Type *base,
  1134. dspi_slave_handle_t *handle,
  1135. dspi_slave_transfer_callback_t callback,
  1136. void *userData)
  1137. {
  1138. assert(handle);
  1139. /* Zero the handle. */
  1140. memset(handle, 0, sizeof(*handle));
  1141. g_dspiHandle[DSPI_GetInstance(base)] = handle;
  1142. handle->callback = callback;
  1143. handle->userData = userData;
  1144. }
  1145. status_t DSPI_SlaveTransferNonBlocking(SPI_Type *base, dspi_slave_handle_t *handle, dspi_transfer_t *transfer)
  1146. {
  1147. assert(handle);
  1148. assert(transfer);
  1149. /* If receive length is zero */
  1150. if (transfer->dataSize == 0)
  1151. {
  1152. return kStatus_InvalidArgument;
  1153. }
  1154. /* If both send buffer and receive buffer is null */
  1155. if ((!(transfer->txData)) && (!(transfer->rxData)))
  1156. {
  1157. return kStatus_InvalidArgument;
  1158. }
  1159. /* Check that we're not busy.*/
  1160. if (handle->state == kDSPI_Busy)
  1161. {
  1162. return kStatus_DSPI_Busy;
  1163. }
  1164. handle->state = kDSPI_Busy;
  1165. /* Enable the NVIC for DSPI peripheral. */
  1166. EnableIRQ(s_dspiIRQ[DSPI_GetInstance(base)]);
  1167. /* Store transfer information */
  1168. handle->txData = transfer->txData;
  1169. handle->rxData = transfer->rxData;
  1170. handle->remainingSendByteCount = transfer->dataSize;
  1171. handle->remainingReceiveByteCount = transfer->dataSize;
  1172. handle->totalByteCount = transfer->dataSize;
  1173. handle->errorCount = 0;
  1174. uint8_t whichCtar = (transfer->configFlags & DSPI_SLAVE_CTAR_MASK) >> DSPI_SLAVE_CTAR_SHIFT;
  1175. handle->bitsPerFrame =
  1176. (((base->CTAR_SLAVE[whichCtar]) & SPI_CTAR_SLAVE_FMSZ_MASK) >> SPI_CTAR_SLAVE_FMSZ_SHIFT) + 1;
  1177. DSPI_StopTransfer(base);
  1178. DSPI_FlushFifo(base, true, true);
  1179. DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
  1180. s_dspiSlaveIsr = DSPI_SlaveTransferHandleIRQ;
  1181. /* Enable RX FIFO drain request, the slave only use this interrupt */
  1182. DSPI_EnableInterrupts(base, kDSPI_RxFifoDrainRequestInterruptEnable);
  1183. if (handle->rxData)
  1184. {
  1185. /* RX FIFO overflow request enable */
  1186. DSPI_EnableInterrupts(base, kDSPI_RxFifoOverflowInterruptEnable);
  1187. }
  1188. if (handle->txData)
  1189. {
  1190. /* TX FIFO underflow request enable */
  1191. DSPI_EnableInterrupts(base, kDSPI_TxFifoUnderflowInterruptEnable);
  1192. }
  1193. DSPI_StartTransfer(base);
  1194. /* Prepare data to transmit */
  1195. DSPI_SlaveTransferFillUpTxFifo(base, handle);
  1196. return kStatus_Success;
  1197. }
  1198. status_t DSPI_SlaveTransferGetCount(SPI_Type *base, dspi_slave_handle_t *handle, size_t *count)
  1199. {
  1200. assert(handle);
  1201. if (!count)
  1202. {
  1203. return kStatus_InvalidArgument;
  1204. }
  1205. /* Catch when there is not an active transfer. */
  1206. if (handle->state != kDSPI_Busy)
  1207. {
  1208. *count = 0;
  1209. return kStatus_NoTransferInProgress;
  1210. }
  1211. *count = handle->totalByteCount - handle->remainingReceiveByteCount;
  1212. return kStatus_Success;
  1213. }
  1214. static void DSPI_SlaveTransferFillUpTxFifo(SPI_Type *base, dspi_slave_handle_t *handle)
  1215. {
  1216. assert(handle);
  1217. uint16_t transmitData = 0;
  1218. uint8_t dummyPattern = s_dummyData[DSPI_GetInstance(base)];
  1219. /* Service the transmitter, if transmit buffer provided, transmit the data,
  1220. * else transmit dummy pattern
  1221. */
  1222. while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
  1223. {
  1224. /* Transmit data */
  1225. if (handle->remainingSendByteCount > 0)
  1226. {
  1227. /* Have data to transmit, update the transmit data and push to FIFO */
  1228. if (handle->bitsPerFrame <= 8)
  1229. {
  1230. /* bits/frame is 1 byte */
  1231. if (handle->txData)
  1232. {
  1233. /* Update transmit data and transmit pointer */
  1234. transmitData = *handle->txData;
  1235. handle->txData++;
  1236. }
  1237. else
  1238. {
  1239. transmitData = dummyPattern;
  1240. }
  1241. /* Decrease remaining dataSize */
  1242. --handle->remainingSendByteCount;
  1243. }
  1244. /* bits/frame is 2 bytes */
  1245. else
  1246. {
  1247. /* With multibytes per frame transmission, the transmit frame contains data from
  1248. * transmit buffer until sent dataSize matches user request. Other bytes will set to
  1249. * dummy pattern value.
  1250. */
  1251. if (handle->txData)
  1252. {
  1253. /* Update first byte of transmit data and transmit pointer */
  1254. transmitData = *handle->txData;
  1255. handle->txData++;
  1256. if (handle->remainingSendByteCount == 1)
  1257. {
  1258. /* Decrease remaining dataSize */
  1259. --handle->remainingSendByteCount;
  1260. /* Update second byte of transmit data to second byte of dummy pattern */
  1261. transmitData = transmitData | (uint16_t)(((uint16_t)dummyPattern) << 8);
  1262. }
  1263. else
  1264. {
  1265. /* Update second byte of transmit data and transmit pointer */
  1266. transmitData = transmitData | (uint16_t)((uint16_t)(*handle->txData) << 8);
  1267. handle->txData++;
  1268. handle->remainingSendByteCount -= 2;
  1269. }
  1270. }
  1271. else
  1272. {
  1273. if (handle->remainingSendByteCount == 1)
  1274. {
  1275. --handle->remainingSendByteCount;
  1276. }
  1277. else
  1278. {
  1279. handle->remainingSendByteCount -= 2;
  1280. }
  1281. transmitData = (uint16_t)((uint16_t)(dummyPattern) << 8) | dummyPattern;
  1282. }
  1283. }
  1284. }
  1285. else
  1286. {
  1287. break;
  1288. }
  1289. /* Write the data to the DSPI data register */
  1290. base->PUSHR_SLAVE = transmitData;
  1291. /* Try to clear TFFF by writing a one to it; it will not clear if TX FIFO not full */
  1292. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  1293. }
  1294. }
  1295. static void DSPI_SlaveTransferComplete(SPI_Type *base, dspi_slave_handle_t *handle)
  1296. {
  1297. assert(handle);
  1298. /* Disable interrupt requests */
  1299. DSPI_DisableInterrupts(base, kDSPI_TxFifoUnderflowInterruptEnable | kDSPI_TxFifoFillRequestInterruptEnable |
  1300. kDSPI_RxFifoOverflowInterruptEnable | kDSPI_RxFifoDrainRequestInterruptEnable);
  1301. /* The transfer is complete. */
  1302. handle->txData = NULL;
  1303. handle->rxData = NULL;
  1304. handle->remainingReceiveByteCount = 0;
  1305. handle->remainingSendByteCount = 0;
  1306. status_t status = 0;
  1307. if (handle->state == kDSPI_Error)
  1308. {
  1309. status = kStatus_DSPI_Error;
  1310. }
  1311. else
  1312. {
  1313. status = kStatus_Success;
  1314. }
  1315. handle->state = kDSPI_Idle;
  1316. if (handle->callback)
  1317. {
  1318. handle->callback(base, handle, status, handle->userData);
  1319. }
  1320. }
  1321. void DSPI_SlaveTransferAbort(SPI_Type *base, dspi_slave_handle_t *handle)
  1322. {
  1323. assert(handle);
  1324. DSPI_StopTransfer(base);
  1325. /* Disable interrupt requests */
  1326. DSPI_DisableInterrupts(base, kDSPI_TxFifoUnderflowInterruptEnable | kDSPI_TxFifoFillRequestInterruptEnable |
  1327. kDSPI_RxFifoOverflowInterruptEnable | kDSPI_RxFifoDrainRequestInterruptEnable);
  1328. handle->state = kDSPI_Idle;
  1329. handle->remainingSendByteCount = 0;
  1330. handle->remainingReceiveByteCount = 0;
  1331. }
  1332. void DSPI_SlaveTransferHandleIRQ(SPI_Type *base, dspi_slave_handle_t *handle)
  1333. {
  1334. assert(handle);
  1335. uint8_t dummyPattern = s_dummyData[DSPI_GetInstance(base)];
  1336. uint32_t dataReceived;
  1337. uint32_t dataSend = 0;
  1338. /* Because SPI protocol is synchronous, the number of bytes that that slave received from the
  1339. * master is the actual number of bytes that the slave transmitted to the master. So we only
  1340. * monitor the received dataSize to know when the transfer is complete.
  1341. */
  1342. if (handle->remainingReceiveByteCount > 0)
  1343. {
  1344. while (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
  1345. {
  1346. /* Have received data in the buffer. */
  1347. dataReceived = base->POPR;
  1348. /*Clear the rx fifo drain request, needed for non-DMA applications as this flag
  1349. * will remain set even if the rx fifo is empty. By manually clearing this flag, it
  1350. * either remain clear if no more data is in the fifo, or it will set if there is
  1351. * more data in the fifo.
  1352. */
  1353. DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
  1354. /* If bits/frame is one byte */
  1355. if (handle->bitsPerFrame <= 8)
  1356. {
  1357. if (handle->rxData)
  1358. {
  1359. /* Receive buffer is not null, store data into it */
  1360. *handle->rxData = dataReceived;
  1361. ++handle->rxData;
  1362. }
  1363. /* Descrease remaining receive byte count */
  1364. --handle->remainingReceiveByteCount;
  1365. if (handle->remainingSendByteCount > 0)
  1366. {
  1367. if (handle->txData)
  1368. {
  1369. dataSend = *handle->txData;
  1370. ++handle->txData;
  1371. }
  1372. else
  1373. {
  1374. dataSend = dummyPattern;
  1375. }
  1376. --handle->remainingSendByteCount;
  1377. /* Write the data to the DSPI data register */
  1378. base->PUSHR_SLAVE = dataSend;
  1379. }
  1380. }
  1381. else /* If bits/frame is 2 bytes */
  1382. {
  1383. /* With multibytes frame receiving, we only receive till the received dataSize
  1384. * matches user request. Other bytes will be ignored.
  1385. */
  1386. if (handle->rxData)
  1387. {
  1388. /* Receive buffer is not null, store first byte into it */
  1389. *handle->rxData = dataReceived;
  1390. ++handle->rxData;
  1391. if (handle->remainingReceiveByteCount == 1)
  1392. {
  1393. /* Decrease remaining receive byte count */
  1394. --handle->remainingReceiveByteCount;
  1395. }
  1396. else
  1397. {
  1398. /* Receive buffer is not null, store second byte into it */
  1399. *handle->rxData = dataReceived >> 8;
  1400. ++handle->rxData;
  1401. handle->remainingReceiveByteCount -= 2;
  1402. }
  1403. }
  1404. /* If no handle->rxData*/
  1405. else
  1406. {
  1407. if (handle->remainingReceiveByteCount == 1)
  1408. {
  1409. /* Decrease remaining receive byte count */
  1410. --handle->remainingReceiveByteCount;
  1411. }
  1412. else
  1413. {
  1414. handle->remainingReceiveByteCount -= 2;
  1415. }
  1416. }
  1417. if (handle->remainingSendByteCount > 0)
  1418. {
  1419. if (handle->txData)
  1420. {
  1421. dataSend = *handle->txData;
  1422. ++handle->txData;
  1423. if (handle->remainingSendByteCount == 1)
  1424. {
  1425. --handle->remainingSendByteCount;
  1426. dataSend |= (uint16_t)((uint16_t)(dummyPattern) << 8);
  1427. }
  1428. else
  1429. {
  1430. dataSend |= (uint32_t)(*handle->txData) << 8;
  1431. ++handle->txData;
  1432. handle->remainingSendByteCount -= 2;
  1433. }
  1434. }
  1435. /* If no handle->txData*/
  1436. else
  1437. {
  1438. if (handle->remainingSendByteCount == 1)
  1439. {
  1440. --handle->remainingSendByteCount;
  1441. }
  1442. else
  1443. {
  1444. handle->remainingSendByteCount -= 2;
  1445. }
  1446. dataSend = (uint16_t)((uint16_t)(dummyPattern) << 8) | dummyPattern;
  1447. }
  1448. /* Write the data to the DSPI data register */
  1449. base->PUSHR_SLAVE = dataSend;
  1450. }
  1451. }
  1452. /* Try to clear TFFF by writing a one to it; it will not clear if TX FIFO not full */
  1453. DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
  1454. if (handle->remainingReceiveByteCount == 0)
  1455. {
  1456. break;
  1457. }
  1458. }
  1459. }
  1460. /* Check if remaining receive byte count matches user request */
  1461. if ((handle->remainingReceiveByteCount == 0) || (handle->state == kDSPI_Error))
  1462. {
  1463. /* Other cases, stop the transfer. */
  1464. DSPI_SlaveTransferComplete(base, handle);
  1465. return;
  1466. }
  1467. /* Catch tx fifo underflow conditions, service only if tx under flow interrupt enabled */
  1468. if ((DSPI_GetStatusFlags(base) & kDSPI_TxFifoUnderflowFlag) && (base->RSER & SPI_RSER_TFUF_RE_MASK))
  1469. {
  1470. DSPI_ClearStatusFlags(base, kDSPI_TxFifoUnderflowFlag);
  1471. /* Change state to error and clear flag */
  1472. if (handle->txData)
  1473. {
  1474. handle->state = kDSPI_Error;
  1475. }
  1476. handle->errorCount++;
  1477. }
  1478. /* Catch rx fifo overflow conditions, service only if rx over flow interrupt enabled */
  1479. if ((DSPI_GetStatusFlags(base) & kDSPI_RxFifoOverflowFlag) && (base->RSER & SPI_RSER_RFOF_RE_MASK))
  1480. {
  1481. DSPI_ClearStatusFlags(base, kDSPI_RxFifoOverflowFlag);
  1482. /* Change state to error and clear flag */
  1483. if (handle->txData)
  1484. {
  1485. handle->state = kDSPI_Error;
  1486. }
  1487. handle->errorCount++;
  1488. }
  1489. }
  1490. static void DSPI_CommonIRQHandler(SPI_Type *base, void *param)
  1491. {
  1492. if (DSPI_IsMaster(base))
  1493. {
  1494. s_dspiMasterIsr(base, (dspi_master_handle_t *)param);
  1495. }
  1496. else
  1497. {
  1498. s_dspiSlaveIsr(base, (dspi_slave_handle_t *)param);
  1499. }
  1500. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1501. exception return operation might vector to incorrect interrupt */
  1502. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1503. __DSB();
  1504. #endif
  1505. }
  1506. #if defined(SPI0)
  1507. void SPI0_DriverIRQHandler(void)
  1508. {
  1509. assert(g_dspiHandle[0]);
  1510. DSPI_CommonIRQHandler(SPI0, g_dspiHandle[0]);
  1511. }
  1512. #endif
  1513. #if defined(SPI1)
  1514. void SPI1_DriverIRQHandler(void)
  1515. {
  1516. assert(g_dspiHandle[1]);
  1517. DSPI_CommonIRQHandler(SPI1, g_dspiHandle[1]);
  1518. }
  1519. #endif
  1520. #if defined(SPI2)
  1521. void SPI2_DriverIRQHandler(void)
  1522. {
  1523. assert(g_dspiHandle[2]);
  1524. DSPI_CommonIRQHandler(SPI2, g_dspiHandle[2]);
  1525. }
  1526. #endif
  1527. #if defined(SPI3)
  1528. void SPI3_DriverIRQHandler(void)
  1529. {
  1530. assert(g_dspiHandle[3]);
  1531. DSPI_CommonIRQHandler(SPI3, g_dspiHandle[3]);
  1532. }
  1533. #endif
  1534. #if defined(SPI4)
  1535. void SPI4_DriverIRQHandler(void)
  1536. {
  1537. assert(g_dspiHandle[4]);
  1538. DSPI_CommonIRQHandler(SPI4, g_dspiHandle[4]);
  1539. }
  1540. #endif
  1541. #if defined(SPI5)
  1542. void SPI5_DriverIRQHandler(void)
  1543. {
  1544. assert(g_dspiHandle[5]);
  1545. DSPI_CommonIRQHandler(SPI5, g_dspiHandle[5]);
  1546. }
  1547. #endif
  1548. #if (FSL_FEATURE_SOC_DSPI_COUNT > 6)
  1549. #error "Should write the SPIx_DriverIRQHandler function that instance greater than 5 !"
  1550. #endif