fsl_dspi.c 57 KB

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