12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808 |
- /*
- * The Clear BSD License
- * Copyright (c) 2015, Freescale Semiconductor, Inc.
- * Copyright 2016-2017 NXP
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted (subject to the limitations in the disclaimer below) provided
- * that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
- *
- * o Neither the name of the copyright holder nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "fsl_dspi.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- /*! @brief Typedef for master interrupt handler. */
- typedef void (*dspi_master_isr_t)(SPI_Type *base, dspi_master_handle_t *handle);
- /*! @brief Typedef for slave interrupt handler. */
- typedef void (*dspi_slave_isr_t)(SPI_Type *base, dspi_slave_handle_t *handle);
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /*!
- * @brief Get instance number for DSPI module.
- *
- * @param base DSPI peripheral base address.
- */
- uint32_t DSPI_GetInstance(SPI_Type *base);
- /*!
- * @brief Configures the DSPI peripheral chip select polarity.
- *
- * This function takes in the desired peripheral chip select (Pcs) and it's corresponding desired polarity and
- * configures the Pcs signal to operate with the desired characteristic.
- *
- * @param base DSPI peripheral address.
- * @param pcs The particular peripheral chip select (parameter value is of type dspi_which_pcs_t) for which we wish to
- * apply the active high or active low characteristic.
- * @param activeLowOrHigh The setting for either "active high, inactive low (0)" or "active low, inactive high(1)" of
- * type dspi_pcs_polarity_config_t.
- */
- static void DSPI_SetOnePcsPolarity(SPI_Type *base, dspi_which_pcs_t pcs, dspi_pcs_polarity_config_t activeLowOrHigh);
- /*!
- * @brief Master fill up the TX FIFO with data.
- * This is not a public API.
- */
- static void DSPI_MasterTransferFillUpTxFifo(SPI_Type *base, dspi_master_handle_t *handle);
- /*!
- * @brief Master finish up a transfer.
- * It would call back if there is callback function and set the state to idle.
- * This is not a public API.
- */
- static void DSPI_MasterTransferComplete(SPI_Type *base, dspi_master_handle_t *handle);
- /*!
- * @brief Slave fill up the TX FIFO with data.
- * This is not a public API.
- */
- static void DSPI_SlaveTransferFillUpTxFifo(SPI_Type *base, dspi_slave_handle_t *handle);
- /*!
- * @brief Slave finish up a transfer.
- * It would call back if there is callback function and set the state to idle.
- * This is not a public API.
- */
- static void DSPI_SlaveTransferComplete(SPI_Type *base, dspi_slave_handle_t *handle);
- /*!
- * @brief DSPI common interrupt handler.
- *
- * @param base DSPI peripheral address.
- * @param handle pointer to g_dspiHandle which stores the transfer state.
- */
- static void DSPI_CommonIRQHandler(SPI_Type *base, void *param);
- /*!
- * @brief Master prepare the transfer.
- * Basically it set up dspi_master_handle .
- * This is not a public API.
- */
- static void DSPI_MasterTransferPrepare(SPI_Type *base, dspi_master_handle_t *handle, dspi_transfer_t *transfer);
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- /* Defines constant value arrays for the baud rate pre-scalar and scalar divider values.*/
- static const uint32_t s_baudratePrescaler[] = {2, 3, 5, 7};
- static const uint32_t s_baudrateScaler[] = {2, 4, 6, 8, 16, 32, 64, 128,
- 256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
- static const uint32_t s_delayPrescaler[] = {1, 3, 5, 7};
- static const uint32_t s_delayScaler[] = {2, 4, 8, 16, 32, 64, 128, 256,
- 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536};
- /*! @brief Pointers to dspi bases for each instance. */
- static SPI_Type *const s_dspiBases[] = SPI_BASE_PTRS;
- /*! @brief Pointers to dspi IRQ number for each instance. */
- static IRQn_Type const s_dspiIRQ[] = SPI_IRQS;
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /*! @brief Pointers to dspi clocks for each instance. */
- static clock_ip_name_t const s_dspiClock[] = DSPI_CLOCKS;
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- /*! @brief Pointers to dspi handles for each instance. */
- static void *g_dspiHandle[ARRAY_SIZE(s_dspiBases)];
- /*! @brief Pointer to master IRQ handler for each instance. */
- static dspi_master_isr_t s_dspiMasterIsr;
- /*! @brief Pointer to slave IRQ handler for each instance. */
- static dspi_slave_isr_t s_dspiSlaveIsr;
- /* @brief Dummy data for each instance. This data is used when user's tx buffer is NULL*/
- volatile uint8_t s_dummyData[ARRAY_SIZE(s_dspiBases)] = {0};
- /**********************************************************************************************************************
- * Code
- *********************************************************************************************************************/
- uint32_t DSPI_GetInstance(SPI_Type *base)
- {
- uint32_t instance;
- /* Find the instance index from base address mappings. */
- for (instance = 0; instance < ARRAY_SIZE(s_dspiBases); instance++)
- {
- if (s_dspiBases[instance] == base)
- {
- break;
- }
- }
- assert(instance < ARRAY_SIZE(s_dspiBases));
- return instance;
- }
- void DSPI_SetDummyData(SPI_Type *base, uint8_t dummyData)
- {
- uint32_t instance = DSPI_GetInstance(base);
- s_dummyData[instance] = dummyData;
- }
- void DSPI_MasterInit(SPI_Type *base, const dspi_master_config_t *masterConfig, uint32_t srcClock_Hz)
- {
- assert(masterConfig);
- uint32_t temp;
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* enable DSPI clock */
- CLOCK_EnableClock(s_dspiClock[DSPI_GetInstance(base)]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- DSPI_Enable(base, true);
- DSPI_StopTransfer(base);
- DSPI_SetMasterSlaveMode(base, kDSPI_Master);
- temp = base->MCR & (~(SPI_MCR_CONT_SCKE_MASK | SPI_MCR_MTFE_MASK | SPI_MCR_ROOE_MASK | SPI_MCR_SMPL_PT_MASK |
- SPI_MCR_DIS_TXF_MASK | SPI_MCR_DIS_RXF_MASK));
- base->MCR = temp | SPI_MCR_CONT_SCKE(masterConfig->enableContinuousSCK) |
- SPI_MCR_MTFE(masterConfig->enableModifiedTimingFormat) |
- SPI_MCR_ROOE(masterConfig->enableRxFifoOverWrite) | SPI_MCR_SMPL_PT(masterConfig->samplePoint) |
- SPI_MCR_DIS_TXF(false) | SPI_MCR_DIS_RXF(false);
- DSPI_SetOnePcsPolarity(base, masterConfig->whichPcs, masterConfig->pcsActiveHighOrLow);
- if (0 == DSPI_MasterSetBaudRate(base, masterConfig->whichCtar, masterConfig->ctarConfig.baudRate, srcClock_Hz))
- {
- assert(false);
- }
- temp = base->CTAR[masterConfig->whichCtar] &
- ~(SPI_CTAR_FMSZ_MASK | SPI_CTAR_CPOL_MASK | SPI_CTAR_CPHA_MASK | SPI_CTAR_LSBFE_MASK);
- base->CTAR[masterConfig->whichCtar] =
- temp | SPI_CTAR_FMSZ(masterConfig->ctarConfig.bitsPerFrame - 1) | SPI_CTAR_CPOL(masterConfig->ctarConfig.cpol) |
- SPI_CTAR_CPHA(masterConfig->ctarConfig.cpha) | SPI_CTAR_LSBFE(masterConfig->ctarConfig.direction);
- DSPI_MasterSetDelayTimes(base, masterConfig->whichCtar, kDSPI_PcsToSck, srcClock_Hz,
- masterConfig->ctarConfig.pcsToSckDelayInNanoSec);
- DSPI_MasterSetDelayTimes(base, masterConfig->whichCtar, kDSPI_LastSckToPcs, srcClock_Hz,
- masterConfig->ctarConfig.lastSckToPcsDelayInNanoSec);
- DSPI_MasterSetDelayTimes(base, masterConfig->whichCtar, kDSPI_BetweenTransfer, srcClock_Hz,
- masterConfig->ctarConfig.betweenTransferDelayInNanoSec);
- DSPI_SetDummyData(base, DSPI_DUMMY_DATA);
- DSPI_StartTransfer(base);
- }
- void DSPI_MasterGetDefaultConfig(dspi_master_config_t *masterConfig)
- {
- assert(masterConfig);
- masterConfig->whichCtar = kDSPI_Ctar0;
- masterConfig->ctarConfig.baudRate = 500000;
- masterConfig->ctarConfig.bitsPerFrame = 8;
- masterConfig->ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
- masterConfig->ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
- masterConfig->ctarConfig.direction = kDSPI_MsbFirst;
- masterConfig->ctarConfig.pcsToSckDelayInNanoSec = 1000;
- masterConfig->ctarConfig.lastSckToPcsDelayInNanoSec = 1000;
- masterConfig->ctarConfig.betweenTransferDelayInNanoSec = 1000;
- masterConfig->whichPcs = kDSPI_Pcs0;
- masterConfig->pcsActiveHighOrLow = kDSPI_PcsActiveLow;
- masterConfig->enableContinuousSCK = false;
- masterConfig->enableRxFifoOverWrite = false;
- masterConfig->enableModifiedTimingFormat = false;
- masterConfig->samplePoint = kDSPI_SckToSin0Clock;
- }
- void DSPI_SlaveInit(SPI_Type *base, const dspi_slave_config_t *slaveConfig)
- {
- assert(slaveConfig);
- uint32_t temp = 0;
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* enable DSPI clock */
- CLOCK_EnableClock(s_dspiClock[DSPI_GetInstance(base)]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- DSPI_Enable(base, true);
- DSPI_StopTransfer(base);
- DSPI_SetMasterSlaveMode(base, kDSPI_Slave);
- temp = base->MCR & (~(SPI_MCR_CONT_SCKE_MASK | SPI_MCR_MTFE_MASK | SPI_MCR_ROOE_MASK | SPI_MCR_SMPL_PT_MASK |
- SPI_MCR_DIS_TXF_MASK | SPI_MCR_DIS_RXF_MASK));
- base->MCR = temp | SPI_MCR_CONT_SCKE(slaveConfig->enableContinuousSCK) |
- SPI_MCR_MTFE(slaveConfig->enableModifiedTimingFormat) |
- SPI_MCR_ROOE(slaveConfig->enableRxFifoOverWrite) | SPI_MCR_SMPL_PT(slaveConfig->samplePoint) |
- SPI_MCR_DIS_TXF(false) | SPI_MCR_DIS_RXF(false);
- DSPI_SetOnePcsPolarity(base, kDSPI_Pcs0, kDSPI_PcsActiveLow);
- temp = base->CTAR[slaveConfig->whichCtar] &
- ~(SPI_CTAR_FMSZ_MASK | SPI_CTAR_CPOL_MASK | SPI_CTAR_CPHA_MASK | SPI_CTAR_LSBFE_MASK);
- base->CTAR[slaveConfig->whichCtar] = temp | SPI_CTAR_SLAVE_FMSZ(slaveConfig->ctarConfig.bitsPerFrame - 1) |
- SPI_CTAR_SLAVE_CPOL(slaveConfig->ctarConfig.cpol) |
- SPI_CTAR_SLAVE_CPHA(slaveConfig->ctarConfig.cpha);
- DSPI_SetDummyData(base, DSPI_DUMMY_DATA);
- DSPI_StartTransfer(base);
- }
- void DSPI_SlaveGetDefaultConfig(dspi_slave_config_t *slaveConfig)
- {
- assert(slaveConfig);
- slaveConfig->whichCtar = kDSPI_Ctar0;
- slaveConfig->ctarConfig.bitsPerFrame = 8;
- slaveConfig->ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh;
- slaveConfig->ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
- slaveConfig->enableContinuousSCK = false;
- slaveConfig->enableRxFifoOverWrite = false;
- slaveConfig->enableModifiedTimingFormat = false;
- slaveConfig->samplePoint = kDSPI_SckToSin0Clock;
- }
- void DSPI_Deinit(SPI_Type *base)
- {
- DSPI_StopTransfer(base);
- DSPI_Enable(base, false);
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* disable DSPI clock */
- CLOCK_DisableClock(s_dspiClock[DSPI_GetInstance(base)]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- }
- static void DSPI_SetOnePcsPolarity(SPI_Type *base, dspi_which_pcs_t pcs, dspi_pcs_polarity_config_t activeLowOrHigh)
- {
- uint32_t temp;
- temp = base->MCR;
- if (activeLowOrHigh == kDSPI_PcsActiveLow)
- {
- temp |= SPI_MCR_PCSIS(pcs);
- }
- else
- {
- temp &= ~SPI_MCR_PCSIS(pcs);
- }
- base->MCR = temp;
- }
- uint32_t DSPI_MasterSetBaudRate(SPI_Type *base,
- dspi_ctar_selection_t whichCtar,
- uint32_t baudRate_Bps,
- uint32_t srcClock_Hz)
- {
- /* for master mode configuration, if slave mode detected, return 0*/
- if (!DSPI_IsMaster(base))
- {
- return 0;
- }
- uint32_t temp;
- uint32_t prescaler, bestPrescaler;
- uint32_t scaler, bestScaler;
- uint32_t dbr, bestDbr;
- uint32_t realBaudrate, bestBaudrate;
- uint32_t diff, min_diff;
- uint32_t baudrate = baudRate_Bps;
- /* find combination of prescaler and scaler resulting in baudrate closest to the requested value */
- min_diff = 0xFFFFFFFFU;
- bestPrescaler = 0;
- bestScaler = 0;
- bestDbr = 1;
- bestBaudrate = 0; /* required to avoid compilation warning */
- /* In all for loops, if min_diff = 0, the exit for loop*/
- for (prescaler = 0; (prescaler < 4) && min_diff; prescaler++)
- {
- for (scaler = 0; (scaler < 16) && min_diff; scaler++)
- {
- for (dbr = 1; (dbr < 3) && min_diff; dbr++)
- {
- realBaudrate = ((srcClock_Hz * dbr) / (s_baudratePrescaler[prescaler] * (s_baudrateScaler[scaler])));
- /* calculate the baud rate difference based on the conditional statement that states that the calculated
- * baud rate must not exceed the desired baud rate.
- */
- if (baudrate >= realBaudrate)
- {
- diff = baudrate - realBaudrate;
- if (min_diff > diff)
- {
- /* a better match found */
- min_diff = diff;
- bestPrescaler = prescaler;
- bestScaler = scaler;
- bestBaudrate = realBaudrate;
- bestDbr = dbr;
- }
- }
- }
- }
- }
- /* write the best dbr, prescalar, and baud rate scalar to the CTAR */
- temp = base->CTAR[whichCtar] & ~(SPI_CTAR_DBR_MASK | SPI_CTAR_PBR_MASK | SPI_CTAR_BR_MASK);
- base->CTAR[whichCtar] = temp | ((bestDbr - 1) << SPI_CTAR_DBR_SHIFT) | (bestPrescaler << SPI_CTAR_PBR_SHIFT) |
- (bestScaler << SPI_CTAR_BR_SHIFT);
- /* return the actual calculated baud rate */
- return bestBaudrate;
- }
- void DSPI_MasterSetDelayScaler(
- SPI_Type *base, dspi_ctar_selection_t whichCtar, uint32_t prescaler, uint32_t scaler, dspi_delay_type_t whichDelay)
- {
- /* these settings are only relevant in master mode */
- if (DSPI_IsMaster(base))
- {
- switch (whichDelay)
- {
- case kDSPI_PcsToSck:
- base->CTAR[whichCtar] = (base->CTAR[whichCtar] & (~SPI_CTAR_PCSSCK_MASK) & (~SPI_CTAR_CSSCK_MASK)) |
- SPI_CTAR_PCSSCK(prescaler) | SPI_CTAR_CSSCK(scaler);
- break;
- case kDSPI_LastSckToPcs:
- base->CTAR[whichCtar] = (base->CTAR[whichCtar] & (~SPI_CTAR_PASC_MASK) & (~SPI_CTAR_ASC_MASK)) |
- SPI_CTAR_PASC(prescaler) | SPI_CTAR_ASC(scaler);
- break;
- case kDSPI_BetweenTransfer:
- base->CTAR[whichCtar] = (base->CTAR[whichCtar] & (~SPI_CTAR_PDT_MASK) & (~SPI_CTAR_DT_MASK)) |
- SPI_CTAR_PDT(prescaler) | SPI_CTAR_DT(scaler);
- break;
- default:
- break;
- }
- }
- }
- uint32_t DSPI_MasterSetDelayTimes(SPI_Type *base,
- dspi_ctar_selection_t whichCtar,
- dspi_delay_type_t whichDelay,
- uint32_t srcClock_Hz,
- uint32_t delayTimeInNanoSec)
- {
- /* for master mode configuration, if slave mode detected, return 0 */
- if (!DSPI_IsMaster(base))
- {
- return 0;
- }
- uint32_t prescaler, bestPrescaler;
- uint32_t scaler, bestScaler;
- uint32_t realDelay, bestDelay;
- uint32_t diff, min_diff;
- uint32_t initialDelayNanoSec;
- /* find combination of prescaler and scaler resulting in the delay closest to the
- * requested value
- */
- min_diff = 0xFFFFFFFFU;
- /* Initialize prescaler and scaler to their max values to generate the max delay */
- bestPrescaler = 0x3;
- bestScaler = 0xF;
- bestDelay = (((1000000000U * 4) / srcClock_Hz) * s_delayPrescaler[bestPrescaler] * s_delayScaler[bestScaler]) / 4;
- /* First calculate the initial, default delay */
- initialDelayNanoSec = 1000000000U / srcClock_Hz * 2;
- /* If the initial, default delay is already greater than the desired delay, then
- * set the delays to their initial value (0) and return the delay. In other words,
- * there is no way to decrease the delay value further.
- */
- if (initialDelayNanoSec >= delayTimeInNanoSec)
- {
- DSPI_MasterSetDelayScaler(base, whichCtar, 0, 0, whichDelay);
- return initialDelayNanoSec;
- }
- /* In all for loops, if min_diff = 0, the exit for loop */
- for (prescaler = 0; (prescaler < 4) && min_diff; prescaler++)
- {
- for (scaler = 0; (scaler < 16) && min_diff; scaler++)
- {
- realDelay = ((4000000000U / srcClock_Hz) * s_delayPrescaler[prescaler] * s_delayScaler[scaler]) / 4;
- /* calculate the delay difference based on the conditional statement
- * that states that the calculated delay must not be less then the desired delay
- */
- if (realDelay >= delayTimeInNanoSec)
- {
- diff = realDelay - delayTimeInNanoSec;
- if (min_diff > diff)
- {
- /* a better match found */
- min_diff = diff;
- bestPrescaler = prescaler;
- bestScaler = scaler;
- bestDelay = realDelay;
- }
- }
- }
- }
- /* write the best dbr, prescalar, and baud rate scalar to the CTAR */
- DSPI_MasterSetDelayScaler(base, whichCtar, bestPrescaler, bestScaler, whichDelay);
- /* return the actual calculated baud rate */
- return bestDelay;
- }
- void DSPI_GetDefaultDataCommandConfig(dspi_command_data_config_t *command)
- {
- assert(command);
- command->isPcsContinuous = false;
- command->whichCtar = kDSPI_Ctar0;
- command->whichPcs = kDSPI_Pcs0;
- command->isEndOfQueue = false;
- command->clearTransferCount = false;
- }
- void DSPI_MasterWriteDataBlocking(SPI_Type *base, dspi_command_data_config_t *command, uint16_t data)
- {
- assert(command);
- /* First, clear Transmit Complete Flag (TCF) */
- DSPI_ClearStatusFlags(base, kDSPI_TxCompleteFlag);
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
- {
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- }
- base->PUSHR = SPI_PUSHR_CONT(command->isPcsContinuous) | SPI_PUSHR_CTAS(command->whichCtar) |
- SPI_PUSHR_PCS(command->whichPcs) | SPI_PUSHR_EOQ(command->isEndOfQueue) |
- SPI_PUSHR_CTCNT(command->clearTransferCount) | SPI_PUSHR_TXDATA(data);
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- /* Wait till TCF sets */
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxCompleteFlag))
- {
- }
- }
- void DSPI_MasterWriteCommandDataBlocking(SPI_Type *base, uint32_t data)
- {
- /* First, clear Transmit Complete Flag (TCF) */
- DSPI_ClearStatusFlags(base, kDSPI_TxCompleteFlag);
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
- {
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- }
- base->PUSHR = data;
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- /* Wait till TCF sets */
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxCompleteFlag))
- {
- }
- }
- void DSPI_SlaveWriteDataBlocking(SPI_Type *base, uint32_t data)
- {
- /* First, clear Transmit Complete Flag (TCF) */
- DSPI_ClearStatusFlags(base, kDSPI_TxCompleteFlag);
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
- {
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- }
- base->PUSHR_SLAVE = data;
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- /* Wait till TCF sets */
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxCompleteFlag))
- {
- }
- }
- void DSPI_EnableInterrupts(SPI_Type *base, uint32_t mask)
- {
- if (mask & SPI_RSER_TFFF_RE_MASK)
- {
- base->RSER &= ~SPI_RSER_TFFF_DIRS_MASK;
- }
- if (mask & SPI_RSER_RFDF_RE_MASK)
- {
- base->RSER &= ~SPI_RSER_RFDF_DIRS_MASK;
- }
- base->RSER |= mask;
- }
- /*Transactional APIs -- Master*/
- void DSPI_MasterTransferCreateHandle(SPI_Type *base,
- dspi_master_handle_t *handle,
- dspi_master_transfer_callback_t callback,
- void *userData)
- {
- assert(handle);
- /* Zero the handle. */
- memset(handle, 0, sizeof(*handle));
- g_dspiHandle[DSPI_GetInstance(base)] = handle;
- handle->callback = callback;
- handle->userData = userData;
- }
- status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer)
- {
- assert(transfer);
- uint16_t wordToSend = 0;
- uint16_t wordReceived = 0;
- uint8_t dummyData = s_dummyData[DSPI_GetInstance(base)];
- uint8_t bitsPerFrame;
- uint32_t command;
- uint32_t lastCommand;
- uint8_t *txData;
- uint8_t *rxData;
- uint32_t remainingSendByteCount;
- uint32_t remainingReceiveByteCount;
- uint32_t fifoSize;
- dspi_command_data_config_t commandStruct;
- /* If the transfer count is zero, then return immediately.*/
- if (transfer->dataSize == 0)
- {
- return kStatus_InvalidArgument;
- }
- DSPI_StopTransfer(base);
- DSPI_DisableInterrupts(base, kDSPI_AllInterruptEnable);
- DSPI_FlushFifo(base, true, true);
- DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
- /*Calculate the command and lastCommand*/
- commandStruct.whichPcs =
- (dspi_which_pcs_t)(1U << ((transfer->configFlags & DSPI_MASTER_PCS_MASK) >> DSPI_MASTER_PCS_SHIFT));
- commandStruct.isEndOfQueue = false;
- commandStruct.clearTransferCount = false;
- commandStruct.whichCtar =
- (dspi_ctar_selection_t)((transfer->configFlags & DSPI_MASTER_CTAR_MASK) >> DSPI_MASTER_CTAR_SHIFT);
- commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterPcsContinuous);
- command = DSPI_MasterGetFormattedCommand(&(commandStruct));
- commandStruct.isEndOfQueue = true;
- commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterActiveAfterTransfer);
- lastCommand = DSPI_MasterGetFormattedCommand(&(commandStruct));
- /*Calculate the bitsPerFrame*/
- bitsPerFrame = ((base->CTAR[commandStruct.whichCtar] & SPI_CTAR_FMSZ_MASK) >> SPI_CTAR_FMSZ_SHIFT) + 1;
- txData = transfer->txData;
- rxData = transfer->rxData;
- remainingSendByteCount = transfer->dataSize;
- remainingReceiveByteCount = transfer->dataSize;
- if ((base->MCR & SPI_MCR_DIS_RXF_MASK) || (base->MCR & SPI_MCR_DIS_TXF_MASK))
- {
- fifoSize = 1;
- }
- else
- {
- fifoSize = FSL_FEATURE_DSPI_FIFO_SIZEn(base);
- }
- DSPI_StartTransfer(base);
- if (bitsPerFrame <= 8)
- {
- while (remainingSendByteCount > 0)
- {
- if (remainingSendByteCount == 1)
- {
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
- {
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- }
- if (txData != NULL)
- {
- base->PUSHR = (*txData) | (lastCommand);
- txData++;
- }
- else
- {
- base->PUSHR = (lastCommand) | (dummyData);
- }
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- remainingSendByteCount--;
- while (remainingReceiveByteCount > 0)
- {
- if (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
- {
- if (rxData != NULL)
- {
- /* Read data from POPR*/
- *(rxData) = DSPI_ReadData(base);
- rxData++;
- }
- else
- {
- DSPI_ReadData(base);
- }
- remainingReceiveByteCount--;
- DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
- }
- }
- }
- else
- {
- /*Wait until Tx Fifo is not full*/
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
- {
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- }
- if (txData != NULL)
- {
- base->PUSHR = command | (uint16_t)(*txData);
- txData++;
- }
- else
- {
- base->PUSHR = command | dummyData;
- }
- remainingSendByteCount--;
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- while ((remainingReceiveByteCount - remainingSendByteCount) >= fifoSize)
- {
- if (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
- {
- if (rxData != NULL)
- {
- *(rxData) = DSPI_ReadData(base);
- rxData++;
- }
- else
- {
- DSPI_ReadData(base);
- }
- remainingReceiveByteCount--;
- DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
- }
- }
- }
- }
- }
- else
- {
- while (remainingSendByteCount > 0)
- {
- if (remainingSendByteCount <= 2)
- {
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
- {
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- }
- if (txData != NULL)
- {
- wordToSend = *(txData);
- ++txData;
- if (remainingSendByteCount > 1)
- {
- wordToSend |= (unsigned)(*(txData)) << 8U;
- ++txData;
- }
- }
- else
- {
- wordToSend = dummyData;
- }
- base->PUSHR = lastCommand | wordToSend;
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- remainingSendByteCount = 0;
- while (remainingReceiveByteCount > 0)
- {
- if (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
- {
- wordReceived = DSPI_ReadData(base);
- if (remainingReceiveByteCount != 1)
- {
- if (rxData != NULL)
- {
- *(rxData) = wordReceived;
- ++rxData;
- *(rxData) = wordReceived >> 8;
- ++rxData;
- }
- remainingReceiveByteCount -= 2;
- }
- else
- {
- if (rxData != NULL)
- {
- *(rxData) = wordReceived;
- ++rxData;
- }
- remainingReceiveByteCount--;
- }
- DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
- }
- }
- }
- else
- {
- /*Wait until Tx Fifo is not full*/
- while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))
- {
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- }
- if (txData != NULL)
- {
- wordToSend = *(txData);
- ++txData;
- wordToSend |= (unsigned)(*(txData)) << 8U;
- ++txData;
- }
- else
- {
- wordToSend = dummyData;
- }
- base->PUSHR = command | wordToSend;
- remainingSendByteCount -= 2;
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- while (((remainingReceiveByteCount - remainingSendByteCount) / 2) >= fifoSize)
- {
- if (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
- {
- wordReceived = DSPI_ReadData(base);
- if (rxData != NULL)
- {
- *rxData = wordReceived;
- ++rxData;
- *rxData = wordReceived >> 8;
- ++rxData;
- }
- remainingReceiveByteCount -= 2;
- DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
- }
- }
- }
- }
- }
- return kStatus_Success;
- }
- static void DSPI_MasterTransferPrepare(SPI_Type *base, dspi_master_handle_t *handle, dspi_transfer_t *transfer)
- {
- assert(handle);
- assert(transfer);
- dspi_command_data_config_t commandStruct;
- DSPI_StopTransfer(base);
- DSPI_FlushFifo(base, true, true);
- DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
- commandStruct.whichPcs =
- (dspi_which_pcs_t)(1U << ((transfer->configFlags & DSPI_MASTER_PCS_MASK) >> DSPI_MASTER_PCS_SHIFT));
- commandStruct.isEndOfQueue = false;
- commandStruct.clearTransferCount = false;
- commandStruct.whichCtar =
- (dspi_ctar_selection_t)((transfer->configFlags & DSPI_MASTER_CTAR_MASK) >> DSPI_MASTER_CTAR_SHIFT);
- commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterPcsContinuous);
- handle->command = DSPI_MasterGetFormattedCommand(&(commandStruct));
- commandStruct.isEndOfQueue = true;
- commandStruct.isPcsContinuous = (bool)(transfer->configFlags & kDSPI_MasterActiveAfterTransfer);
- handle->lastCommand = DSPI_MasterGetFormattedCommand(&(commandStruct));
- handle->bitsPerFrame = ((base->CTAR[commandStruct.whichCtar] & SPI_CTAR_FMSZ_MASK) >> SPI_CTAR_FMSZ_SHIFT) + 1;
- if ((base->MCR & SPI_MCR_DIS_RXF_MASK) || (base->MCR & SPI_MCR_DIS_TXF_MASK))
- {
- handle->fifoSize = 1;
- }
- else
- {
- handle->fifoSize = FSL_FEATURE_DSPI_FIFO_SIZEn(base);
- }
- handle->txData = transfer->txData;
- handle->rxData = transfer->rxData;
- handle->remainingSendByteCount = transfer->dataSize;
- handle->remainingReceiveByteCount = transfer->dataSize;
- handle->totalByteCount = transfer->dataSize;
- }
- status_t DSPI_MasterTransferNonBlocking(SPI_Type *base, dspi_master_handle_t *handle, dspi_transfer_t *transfer)
- {
- assert(handle);
- assert(transfer);
- /* If the transfer count is zero, then return immediately.*/
- if (transfer->dataSize == 0)
- {
- return kStatus_InvalidArgument;
- }
- /* Check that we're not busy.*/
- if (handle->state == kDSPI_Busy)
- {
- return kStatus_DSPI_Busy;
- }
- handle->state = kDSPI_Busy;
- /* Disable the NVIC for DSPI peripheral. */
- DisableIRQ(s_dspiIRQ[DSPI_GetInstance(base)]);
- DSPI_MasterTransferPrepare(base, handle, transfer);
- /* RX FIFO Drain request: RFDF_RE to enable RFDF interrupt
- * Since SPI is a synchronous interface, we only need to enable the RX interrupt.
- * The IRQ handler will get the status of RX and TX interrupt flags.
- */
- s_dspiMasterIsr = DSPI_MasterTransferHandleIRQ;
- DSPI_EnableInterrupts(base, kDSPI_RxFifoDrainRequestInterruptEnable);
- DSPI_StartTransfer(base);
- /* Fill up the Tx FIFO to trigger the transfer. */
- DSPI_MasterTransferFillUpTxFifo(base, handle);
- /* Enable the NVIC for DSPI peripheral. */
- EnableIRQ(s_dspiIRQ[DSPI_GetInstance(base)]);
- return kStatus_Success;
- }
- status_t DSPI_MasterHalfDuplexTransferBlocking(SPI_Type *base, dspi_half_duplex_transfer_t *xfer)
- {
- assert(xfer);
- dspi_transfer_t tempXfer = {0};
- status_t status;
- if (xfer->isTransmitFirst)
- {
- tempXfer.txData = xfer->txData;
- tempXfer.rxData = NULL;
- tempXfer.dataSize = xfer->txDataSize;
- }
- else
- {
- tempXfer.txData = NULL;
- tempXfer.rxData = xfer->rxData;
- tempXfer.dataSize = xfer->rxDataSize;
- }
- /* If the pcs pin keep assert between transmit and receive. */
- if (xfer->isPcsAssertInTransfer)
- {
- tempXfer.configFlags = (xfer->configFlags) | kDSPI_MasterActiveAfterTransfer;
- }
- else
- {
- tempXfer.configFlags = (xfer->configFlags) & (uint32_t)(~kDSPI_MasterActiveAfterTransfer);
- }
- status = DSPI_MasterTransferBlocking(base, &tempXfer);
- if (status != kStatus_Success)
- {
- return status;
- }
- if (xfer->isTransmitFirst)
- {
- tempXfer.txData = NULL;
- tempXfer.rxData = xfer->rxData;
- tempXfer.dataSize = xfer->rxDataSize;
- }
- else
- {
- tempXfer.txData = xfer->txData;
- tempXfer.rxData = NULL;
- tempXfer.dataSize = xfer->txDataSize;
- }
- tempXfer.configFlags = xfer->configFlags;
- /* DSPI transfer blocking. */
- status = DSPI_MasterTransferBlocking(base, &tempXfer);
- return status;
- }
- status_t DSPI_MasterHalfDuplexTransferNonBlocking(SPI_Type *base,
- dspi_master_handle_t *handle,
- dspi_half_duplex_transfer_t *xfer)
- {
- assert(xfer);
- assert(handle);
- dspi_transfer_t tempXfer = {0};
- status_t status;
- if (xfer->isTransmitFirst)
- {
- tempXfer.txData = xfer->txData;
- tempXfer.rxData = NULL;
- tempXfer.dataSize = xfer->txDataSize;
- }
- else
- {
- tempXfer.txData = NULL;
- tempXfer.rxData = xfer->rxData;
- tempXfer.dataSize = xfer->rxDataSize;
- }
- /* If the pcs pin keep assert between transmit and receive. */
- if (xfer->isPcsAssertInTransfer)
- {
- tempXfer.configFlags = (xfer->configFlags) | kDSPI_MasterActiveAfterTransfer;
- }
- else
- {
- tempXfer.configFlags = (xfer->configFlags) & (uint32_t)(~kDSPI_MasterActiveAfterTransfer);
- }
- status = DSPI_MasterTransferBlocking(base, &tempXfer);
- if (status != kStatus_Success)
- {
- return status;
- }
- if (xfer->isTransmitFirst)
- {
- tempXfer.txData = NULL;
- tempXfer.rxData = xfer->rxData;
- tempXfer.dataSize = xfer->rxDataSize;
- }
- else
- {
- tempXfer.txData = xfer->txData;
- tempXfer.rxData = NULL;
- tempXfer.dataSize = xfer->txDataSize;
- }
- tempXfer.configFlags = xfer->configFlags;
- status = DSPI_MasterTransferNonBlocking(base, handle, &tempXfer);
- return status;
- }
- status_t DSPI_MasterTransferGetCount(SPI_Type *base, dspi_master_handle_t *handle, size_t *count)
- {
- assert(handle);
- if (!count)
- {
- return kStatus_InvalidArgument;
- }
- /* Catch when there is not an active transfer. */
- if (handle->state != kDSPI_Busy)
- {
- *count = 0;
- return kStatus_NoTransferInProgress;
- }
- *count = handle->totalByteCount - handle->remainingReceiveByteCount;
- return kStatus_Success;
- }
- static void DSPI_MasterTransferComplete(SPI_Type *base, dspi_master_handle_t *handle)
- {
- assert(handle);
- /* Disable interrupt requests*/
- DSPI_DisableInterrupts(base, kDSPI_RxFifoDrainRequestInterruptEnable | kDSPI_TxFifoFillRequestInterruptEnable);
- status_t status = 0;
- if (handle->state == kDSPI_Error)
- {
- status = kStatus_DSPI_Error;
- }
- else
- {
- status = kStatus_Success;
- }
- handle->state = kDSPI_Idle;
- if (handle->callback)
- {
- handle->callback(base, handle, status, handle->userData);
- }
- }
- static void DSPI_MasterTransferFillUpTxFifo(SPI_Type *base, dspi_master_handle_t *handle)
- {
- assert(handle);
- uint16_t wordToSend = 0;
- uint8_t dummyData = s_dummyData[DSPI_GetInstance(base)];
- /* If bits/frame is greater than one byte */
- if (handle->bitsPerFrame > 8)
- {
- /* Fill the fifo until it is full or until the send word count is 0 or until the difference
- * between the remainingReceiveByteCount and remainingSendByteCount equals the FIFO depth.
- * The reason for checking the difference is to ensure we only send as much as the
- * RX FIFO can receive.
- * For this case where bitsPerFrame > 8, each entry in the FIFO contains 2 bytes of the
- * send data, hence the difference between the remainingReceiveByteCount and
- * remainingSendByteCount must be divided by 2 to convert this difference into a
- * 16-bit (2 byte) value.
- */
- while ((DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag) &&
- ((handle->remainingReceiveByteCount - handle->remainingSendByteCount) / 2 < handle->fifoSize))
- {
- if (handle->remainingSendByteCount <= 2)
- {
- if (handle->txData)
- {
- if (handle->remainingSendByteCount == 1)
- {
- wordToSend = *(handle->txData);
- }
- else
- {
- wordToSend = *(handle->txData);
- ++handle->txData; /* increment to next data byte */
- wordToSend |= (unsigned)(*(handle->txData)) << 8U;
- }
- }
- else
- {
- wordToSend = dummyData;
- }
- handle->remainingSendByteCount = 0;
- base->PUSHR = handle->lastCommand | wordToSend;
- }
- /* For all words except the last word */
- else
- {
- if (handle->txData)
- {
- wordToSend = *(handle->txData);
- ++handle->txData; /* increment to next data byte */
- wordToSend |= (unsigned)(*(handle->txData)) << 8U;
- ++handle->txData; /* increment to next data byte */
- }
- else
- {
- wordToSend = dummyData;
- }
- handle->remainingSendByteCount -= 2; /* decrement remainingSendByteCount by 2 */
- base->PUSHR = handle->command | wordToSend;
- }
- /* Try to clear the TFFF; if the TX FIFO is full this will clear */
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- /* exit loop if send count is zero, else update local variables for next loop.
- * If this is the first time write to the PUSHR, write only once.
- */
- if ((handle->remainingSendByteCount == 0) || (handle->remainingSendByteCount == handle->totalByteCount - 2))
- {
- break;
- }
- } /* End of TX FIFO fill while loop */
- }
- /* Optimized for bits/frame less than or equal to one byte. */
- else
- {
- /* Fill the fifo until it is full or until the send word count is 0 or until the difference
- * between the remainingReceiveByteCount and remainingSendByteCount equals the FIFO depth.
- * The reason for checking the difference is to ensure we only send as much as the
- * RX FIFO can receive.
- */
- while ((DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag) &&
- ((handle->remainingReceiveByteCount - handle->remainingSendByteCount) < handle->fifoSize))
- {
- if (handle->txData)
- {
- wordToSend = *(handle->txData);
- ++handle->txData;
- }
- else
- {
- wordToSend = dummyData;
- }
- if (handle->remainingSendByteCount == 1)
- {
- base->PUSHR = handle->lastCommand | wordToSend;
- }
- else
- {
- base->PUSHR = handle->command | wordToSend;
- }
- /* Try to clear the TFFF; if the TX FIFO is full this will clear */
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- --handle->remainingSendByteCount;
- /* exit loop if send count is zero, else update local variables for next loop
- * If this is the first time write to the PUSHR, write only once.
- */
- if ((handle->remainingSendByteCount == 0) || (handle->remainingSendByteCount == handle->totalByteCount - 1))
- {
- break;
- }
- }
- }
- }
- void DSPI_MasterTransferAbort(SPI_Type *base, dspi_master_handle_t *handle)
- {
- assert(handle);
- DSPI_StopTransfer(base);
- /* Disable interrupt requests*/
- DSPI_DisableInterrupts(base, kDSPI_RxFifoDrainRequestInterruptEnable | kDSPI_TxFifoFillRequestInterruptEnable);
- handle->state = kDSPI_Idle;
- }
- void DSPI_MasterTransferHandleIRQ(SPI_Type *base, dspi_master_handle_t *handle)
- {
- assert(handle);
- /* RECEIVE IRQ handler: Check read buffer only if there are remaining bytes to read. */
- if (handle->remainingReceiveByteCount)
- {
- /* Check read buffer.*/
- uint16_t wordReceived; /* Maximum supported data bit length in master mode is 16-bits */
- /* If bits/frame is greater than one byte */
- if (handle->bitsPerFrame > 8)
- {
- while (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
- {
- wordReceived = DSPI_ReadData(base);
- /* clear the rx fifo drain request, needed for non-DMA applications as this flag
- * will remain set even if the rx fifo is empty. By manually clearing this flag, it
- * either remain clear if no more data is in the fifo, or it will set if there is
- * more data in the fifo.
- */
- DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
- /* Store read bytes into rx buffer only if a buffer pointer was provided */
- if (handle->rxData)
- {
- /* For the last word received, if there is an extra byte due to the odd transfer
- * byte count, only save the last byte and discard the upper byte
- */
- if (handle->remainingReceiveByteCount == 1)
- {
- *handle->rxData = wordReceived; /* Write first data byte */
- --handle->remainingReceiveByteCount;
- }
- else
- {
- *handle->rxData = wordReceived; /* Write first data byte */
- ++handle->rxData; /* increment to next data byte */
- *handle->rxData = wordReceived >> 8; /* Write second data byte */
- ++handle->rxData; /* increment to next data byte */
- handle->remainingReceiveByteCount -= 2;
- }
- }
- else
- {
- if (handle->remainingReceiveByteCount == 1)
- {
- --handle->remainingReceiveByteCount;
- }
- else
- {
- handle->remainingReceiveByteCount -= 2;
- }
- }
- if (handle->remainingReceiveByteCount == 0)
- {
- break;
- }
- } /* End of RX FIFO drain while loop */
- }
- /* Optimized for bits/frame less than or equal to one byte. */
- else
- {
- while (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
- {
- wordReceived = DSPI_ReadData(base);
- /* clear the rx fifo drain request, needed for non-DMA applications as this flag
- * will remain set even if the rx fifo is empty. By manually clearing this flag, it
- * either remain clear if no more data is in the fifo, or it will set if there is
- * more data in the fifo.
- */
- DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
- /* Store read bytes into rx buffer only if a buffer pointer was provided */
- if (handle->rxData)
- {
- *handle->rxData = wordReceived;
- ++handle->rxData;
- }
- --handle->remainingReceiveByteCount;
- if (handle->remainingReceiveByteCount == 0)
- {
- break;
- }
- } /* End of RX FIFO drain while loop */
- }
- }
- /* Check write buffer. We always have to send a word in order to keep the transfer
- * moving. So if the caller didn't provide a send buffer, we just send a zero.
- */
- if (handle->remainingSendByteCount)
- {
- DSPI_MasterTransferFillUpTxFifo(base, handle);
- }
- /* Check if we're done with this transfer.*/
- if ((handle->remainingSendByteCount == 0) && (handle->remainingReceiveByteCount == 0))
- {
- /* Complete the transfer and disable the interrupts */
- DSPI_MasterTransferComplete(base, handle);
- }
- }
- /*Transactional APIs -- Slave*/
- void DSPI_SlaveTransferCreateHandle(SPI_Type *base,
- dspi_slave_handle_t *handle,
- dspi_slave_transfer_callback_t callback,
- void *userData)
- {
- assert(handle);
- /* Zero the handle. */
- memset(handle, 0, sizeof(*handle));
- g_dspiHandle[DSPI_GetInstance(base)] = handle;
- handle->callback = callback;
- handle->userData = userData;
- }
- status_t DSPI_SlaveTransferNonBlocking(SPI_Type *base, dspi_slave_handle_t *handle, dspi_transfer_t *transfer)
- {
- assert(handle);
- assert(transfer);
- /* If receive length is zero */
- if (transfer->dataSize == 0)
- {
- return kStatus_InvalidArgument;
- }
- /* If both send buffer and receive buffer is null */
- if ((!(transfer->txData)) && (!(transfer->rxData)))
- {
- return kStatus_InvalidArgument;
- }
- /* Check that we're not busy.*/
- if (handle->state == kDSPI_Busy)
- {
- return kStatus_DSPI_Busy;
- }
- handle->state = kDSPI_Busy;
- /* Enable the NVIC for DSPI peripheral. */
- EnableIRQ(s_dspiIRQ[DSPI_GetInstance(base)]);
- /* Store transfer information */
- handle->txData = transfer->txData;
- handle->rxData = transfer->rxData;
- handle->remainingSendByteCount = transfer->dataSize;
- handle->remainingReceiveByteCount = transfer->dataSize;
- handle->totalByteCount = transfer->dataSize;
- handle->errorCount = 0;
- uint8_t whichCtar = (transfer->configFlags & DSPI_SLAVE_CTAR_MASK) >> DSPI_SLAVE_CTAR_SHIFT;
- handle->bitsPerFrame =
- (((base->CTAR_SLAVE[whichCtar]) & SPI_CTAR_SLAVE_FMSZ_MASK) >> SPI_CTAR_SLAVE_FMSZ_SHIFT) + 1;
- DSPI_StopTransfer(base);
- DSPI_FlushFifo(base, true, true);
- DSPI_ClearStatusFlags(base, kDSPI_AllStatusFlag);
- s_dspiSlaveIsr = DSPI_SlaveTransferHandleIRQ;
- /* Enable RX FIFO drain request, the slave only use this interrupt */
- DSPI_EnableInterrupts(base, kDSPI_RxFifoDrainRequestInterruptEnable);
- if (handle->rxData)
- {
- /* RX FIFO overflow request enable */
- DSPI_EnableInterrupts(base, kDSPI_RxFifoOverflowInterruptEnable);
- }
- if (handle->txData)
- {
- /* TX FIFO underflow request enable */
- DSPI_EnableInterrupts(base, kDSPI_TxFifoUnderflowInterruptEnable);
- }
- DSPI_StartTransfer(base);
- /* Prepare data to transmit */
- DSPI_SlaveTransferFillUpTxFifo(base, handle);
- return kStatus_Success;
- }
- status_t DSPI_SlaveTransferGetCount(SPI_Type *base, dspi_slave_handle_t *handle, size_t *count)
- {
- assert(handle);
- if (!count)
- {
- return kStatus_InvalidArgument;
- }
- /* Catch when there is not an active transfer. */
- if (handle->state != kDSPI_Busy)
- {
- *count = 0;
- return kStatus_NoTransferInProgress;
- }
- *count = handle->totalByteCount - handle->remainingReceiveByteCount;
- return kStatus_Success;
- }
- static void DSPI_SlaveTransferFillUpTxFifo(SPI_Type *base, dspi_slave_handle_t *handle)
- {
- assert(handle);
- uint16_t transmitData = 0;
- uint8_t dummyPattern = s_dummyData[DSPI_GetInstance(base)];
- /* Service the transmitter, if transmit buffer provided, transmit the data,
- * else transmit dummy pattern
- */
- while (DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag)
- {
- /* Transmit data */
- if (handle->remainingSendByteCount > 0)
- {
- /* Have data to transmit, update the transmit data and push to FIFO */
- if (handle->bitsPerFrame <= 8)
- {
- /* bits/frame is 1 byte */
- if (handle->txData)
- {
- /* Update transmit data and transmit pointer */
- transmitData = *handle->txData;
- handle->txData++;
- }
- else
- {
- transmitData = dummyPattern;
- }
- /* Decrease remaining dataSize */
- --handle->remainingSendByteCount;
- }
- /* bits/frame is 2 bytes */
- else
- {
- /* With multibytes per frame transmission, the transmit frame contains data from
- * transmit buffer until sent dataSize matches user request. Other bytes will set to
- * dummy pattern value.
- */
- if (handle->txData)
- {
- /* Update first byte of transmit data and transmit pointer */
- transmitData = *handle->txData;
- handle->txData++;
- if (handle->remainingSendByteCount == 1)
- {
- /* Decrease remaining dataSize */
- --handle->remainingSendByteCount;
- /* Update second byte of transmit data to second byte of dummy pattern */
- transmitData = transmitData | (uint16_t)(((uint16_t)dummyPattern) << 8);
- }
- else
- {
- /* Update second byte of transmit data and transmit pointer */
- transmitData = transmitData | (uint16_t)((uint16_t)(*handle->txData) << 8);
- handle->txData++;
- handle->remainingSendByteCount -= 2;
- }
- }
- else
- {
- if (handle->remainingSendByteCount == 1)
- {
- --handle->remainingSendByteCount;
- }
- else
- {
- handle->remainingSendByteCount -= 2;
- }
- transmitData = (uint16_t)((uint16_t)(dummyPattern) << 8) | dummyPattern;
- }
- }
- }
- else
- {
- break;
- }
- /* Write the data to the DSPI data register */
- base->PUSHR_SLAVE = transmitData;
- /* Try to clear TFFF by writing a one to it; it will not clear if TX FIFO not full */
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- }
- }
- static void DSPI_SlaveTransferComplete(SPI_Type *base, dspi_slave_handle_t *handle)
- {
- assert(handle);
- /* Disable interrupt requests */
- DSPI_DisableInterrupts(base, kDSPI_TxFifoUnderflowInterruptEnable | kDSPI_TxFifoFillRequestInterruptEnable |
- kDSPI_RxFifoOverflowInterruptEnable | kDSPI_RxFifoDrainRequestInterruptEnable);
- /* The transfer is complete. */
- handle->txData = NULL;
- handle->rxData = NULL;
- handle->remainingReceiveByteCount = 0;
- handle->remainingSendByteCount = 0;
- status_t status = 0;
- if (handle->state == kDSPI_Error)
- {
- status = kStatus_DSPI_Error;
- }
- else
- {
- status = kStatus_Success;
- }
- handle->state = kDSPI_Idle;
- if (handle->callback)
- {
- handle->callback(base, handle, status, handle->userData);
- }
- }
- void DSPI_SlaveTransferAbort(SPI_Type *base, dspi_slave_handle_t *handle)
- {
- assert(handle);
- DSPI_StopTransfer(base);
- /* Disable interrupt requests */
- DSPI_DisableInterrupts(base, kDSPI_TxFifoUnderflowInterruptEnable | kDSPI_TxFifoFillRequestInterruptEnable |
- kDSPI_RxFifoOverflowInterruptEnable | kDSPI_RxFifoDrainRequestInterruptEnable);
- handle->state = kDSPI_Idle;
- handle->remainingSendByteCount = 0;
- handle->remainingReceiveByteCount = 0;
- }
- void DSPI_SlaveTransferHandleIRQ(SPI_Type *base, dspi_slave_handle_t *handle)
- {
- assert(handle);
- uint8_t dummyPattern = s_dummyData[DSPI_GetInstance(base)];
- uint32_t dataReceived;
- uint32_t dataSend = 0;
- /* Because SPI protocol is synchronous, the number of bytes that that slave received from the
- * master is the actual number of bytes that the slave transmitted to the master. So we only
- * monitor the received dataSize to know when the transfer is complete.
- */
- if (handle->remainingReceiveByteCount > 0)
- {
- while (DSPI_GetStatusFlags(base) & kDSPI_RxFifoDrainRequestFlag)
- {
- /* Have received data in the buffer. */
- dataReceived = base->POPR;
- /*Clear the rx fifo drain request, needed for non-DMA applications as this flag
- * will remain set even if the rx fifo is empty. By manually clearing this flag, it
- * either remain clear if no more data is in the fifo, or it will set if there is
- * more data in the fifo.
- */
- DSPI_ClearStatusFlags(base, kDSPI_RxFifoDrainRequestFlag);
- /* If bits/frame is one byte */
- if (handle->bitsPerFrame <= 8)
- {
- if (handle->rxData)
- {
- /* Receive buffer is not null, store data into it */
- *handle->rxData = dataReceived;
- ++handle->rxData;
- }
- /* Descrease remaining receive byte count */
- --handle->remainingReceiveByteCount;
- if (handle->remainingSendByteCount > 0)
- {
- if (handle->txData)
- {
- dataSend = *handle->txData;
- ++handle->txData;
- }
- else
- {
- dataSend = dummyPattern;
- }
- --handle->remainingSendByteCount;
- /* Write the data to the DSPI data register */
- base->PUSHR_SLAVE = dataSend;
- }
- }
- else /* If bits/frame is 2 bytes */
- {
- /* With multibytes frame receiving, we only receive till the received dataSize
- * matches user request. Other bytes will be ignored.
- */
- if (handle->rxData)
- {
- /* Receive buffer is not null, store first byte into it */
- *handle->rxData = dataReceived;
- ++handle->rxData;
- if (handle->remainingReceiveByteCount == 1)
- {
- /* Decrease remaining receive byte count */
- --handle->remainingReceiveByteCount;
- }
- else
- {
- /* Receive buffer is not null, store second byte into it */
- *handle->rxData = dataReceived >> 8;
- ++handle->rxData;
- handle->remainingReceiveByteCount -= 2;
- }
- }
- /* If no handle->rxData*/
- else
- {
- if (handle->remainingReceiveByteCount == 1)
- {
- /* Decrease remaining receive byte count */
- --handle->remainingReceiveByteCount;
- }
- else
- {
- handle->remainingReceiveByteCount -= 2;
- }
- }
- if (handle->remainingSendByteCount > 0)
- {
- if (handle->txData)
- {
- dataSend = *handle->txData;
- ++handle->txData;
- if (handle->remainingSendByteCount == 1)
- {
- --handle->remainingSendByteCount;
- dataSend |= (uint16_t)((uint16_t)(dummyPattern) << 8);
- }
- else
- {
- dataSend |= (uint32_t)(*handle->txData) << 8;
- ++handle->txData;
- handle->remainingSendByteCount -= 2;
- }
- }
- /* If no handle->txData*/
- else
- {
- if (handle->remainingSendByteCount == 1)
- {
- --handle->remainingSendByteCount;
- }
- else
- {
- handle->remainingSendByteCount -= 2;
- }
- dataSend = (uint16_t)((uint16_t)(dummyPattern) << 8) | dummyPattern;
- }
- /* Write the data to the DSPI data register */
- base->PUSHR_SLAVE = dataSend;
- }
- }
- /* Try to clear TFFF by writing a one to it; it will not clear if TX FIFO not full */
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);
- if (handle->remainingReceiveByteCount == 0)
- {
- break;
- }
- }
- }
- /* Check if remaining receive byte count matches user request */
- if ((handle->remainingReceiveByteCount == 0) || (handle->state == kDSPI_Error))
- {
- /* Other cases, stop the transfer. */
- DSPI_SlaveTransferComplete(base, handle);
- return;
- }
- /* Catch tx fifo underflow conditions, service only if tx under flow interrupt enabled */
- if ((DSPI_GetStatusFlags(base) & kDSPI_TxFifoUnderflowFlag) && (base->RSER & SPI_RSER_TFUF_RE_MASK))
- {
- DSPI_ClearStatusFlags(base, kDSPI_TxFifoUnderflowFlag);
- /* Change state to error and clear flag */
- if (handle->txData)
- {
- handle->state = kDSPI_Error;
- }
- handle->errorCount++;
- }
- /* Catch rx fifo overflow conditions, service only if rx over flow interrupt enabled */
- if ((DSPI_GetStatusFlags(base) & kDSPI_RxFifoOverflowFlag) && (base->RSER & SPI_RSER_RFOF_RE_MASK))
- {
- DSPI_ClearStatusFlags(base, kDSPI_RxFifoOverflowFlag);
- /* Change state to error and clear flag */
- if (handle->txData)
- {
- handle->state = kDSPI_Error;
- }
- handle->errorCount++;
- }
- }
- static void DSPI_CommonIRQHandler(SPI_Type *base, void *param)
- {
- if (DSPI_IsMaster(base))
- {
- s_dspiMasterIsr(base, (dspi_master_handle_t *)param);
- }
- else
- {
- s_dspiSlaveIsr(base, (dspi_slave_handle_t *)param);
- }
- /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
- exception return operation might vector to incorrect interrupt */
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- #if defined(SPI0)
- void SPI0_DriverIRQHandler(void)
- {
- assert(g_dspiHandle[0]);
- DSPI_CommonIRQHandler(SPI0, g_dspiHandle[0]);
- }
- #endif
- #if defined(SPI1)
- void SPI1_DriverIRQHandler(void)
- {
- assert(g_dspiHandle[1]);
- DSPI_CommonIRQHandler(SPI1, g_dspiHandle[1]);
- }
- #endif
- #if defined(SPI2)
- void SPI2_DriverIRQHandler(void)
- {
- assert(g_dspiHandle[2]);
- DSPI_CommonIRQHandler(SPI2, g_dspiHandle[2]);
- }
- #endif
- #if defined(SPI3)
- void SPI3_DriverIRQHandler(void)
- {
- assert(g_dspiHandle[3]);
- DSPI_CommonIRQHandler(SPI3, g_dspiHandle[3]);
- }
- #endif
- #if defined(SPI4)
- void SPI4_DriverIRQHandler(void)
- {
- assert(g_dspiHandle[4]);
- DSPI_CommonIRQHandler(SPI4, g_dspiHandle[4]);
- }
- #endif
- #if defined(SPI5)
- void SPI5_DriverIRQHandler(void)
- {
- assert(g_dspiHandle[5]);
- DSPI_CommonIRQHandler(SPI5, g_dspiHandle[5]);
- }
- #endif
- #if (FSL_FEATURE_SOC_DSPI_COUNT > 6)
- #error "Should write the SPIx_DriverIRQHandler function that instance greater than 5 !"
- #endif
|