fsl_dspi.c 62 KB

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