12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662 |
- /*
- * Copyright 2019-2021 NXP
- * All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
- #include "fsl_enet_qos.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- /* Component ID definition, used by tools. */
- #ifndef FSL_COMPONENT_ID
- #define FSL_COMPONENT_ID "platform.drivers.enet_qos"
- #endif
- /*! @brief Defines 10^9 nanosecond. */
- #define ENET_QOS_NANOSECS_ONESECOND (1000000000U)
- /*! @brief Defines 10^6 microsecond.*/
- #define ENET_QOS_MICRSECS_ONESECOND (1000000U)
- /*! @brief Rx buffer LSB ignore bits. */
- #define ENET_QOS_RXBUFF_IGNORELSB_BITS (3U)
- /*! @brief ENET FIFO size unit. */
- #define ENET_QOS_FIFOSIZE_UNIT (256U)
- /*! @brief ENET half-dulpex default IPG. */
- #define ENET_QOS_HALFDUPLEX_DEFAULTIPG (4U)
- /*! @breif ENET miminum ring length. */
- #define ENET_QOS_MIN_RINGLEN (4U)
- /*! @breif ENET wakeup filter numbers. */
- #define ENET_QOS_WAKEUPFILTER_NUM (8U)
- /*! @breif Requried systime timer frequency. */
- #define ENET_QOS_SYSTIME_REQUIRED_CLK_MHZ (50U)
- /*! @brief Ethernet VLAN tag length. */
- #define ENET_QOS_FRAME_VLAN_TAGLEN 4U
- /*! @brief AVB TYPE */
- #define ENET_QOS_AVBTYPE 0x22F0U
- #define ENET_QOS_HEAD_TYPE_OFFSET (12)
- #define ENET_QOS_HEAD_AVBTYPE_OFFSET (16)
- /*! @brief Defines the macro for converting constants from host byte order to network byte order. */
- #define ENET_QOS_HTONS(n) __REV16(n)
- #define ENET_QOS_HTONL(n) __REV(n)
- #define ENET_QOS_NTOHS(n) __REV16(n)
- #define ENET_QOS_NTOHL(n) __REV(n)
- #define ENET_QOS_DMA_CHX_RX_CTRL_RBSZ
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /*!
- * @brief Increase the index in the ring.
- *
- * @param index The current index.
- * @param max The size.
- * @return the increased index.
- */
- static uint16_t ENET_QOS_IncreaseIndex(uint16_t index, uint16_t max);
- /*!
- * @brief Poll status flag.
- *
- * @param regAddr The register address to read out status
- * @param mask The mask to operate the register value.
- * @param readyStatus Indicate readyStatus for the field
- * @retval kStatus_Success Poll readyStatus Success.
- * @retval kStatus_ENET_QOS_Timeout Poll readyStatus timeout.
- */
- static status_t ENET_QOS_PollStatusFlag(volatile uint32_t *regAddr, uint32_t mask, uint32_t readyStatus);
- /*!
- * @brief Set ENET DMA controller with the configuration.
- *
- * @param base ENET peripheral base address.
- * @param config ENET Mac configuration.
- */
- static void ENET_QOS_SetDMAControl(ENET_QOS_Type *base, const enet_qos_config_t *config);
- /*!
- * @brief Set ENET MAC controller with the configuration.
- *
- * @param base ENET peripheral base address.
- * @param config ENET Mac configuration.
- * @param macAddr ENET six-byte mac address.
- */
- static void ENET_QOS_SetMacControl(ENET_QOS_Type *base,
- const enet_qos_config_t *config,
- uint8_t *macAddr,
- uint8_t macCount);
- /*!
- * @brief Set ENET MTL with the configuration.
- *
- * @param base ENET peripheral base address.
- * @param config ENET Mac configuration.
- */
- static void ENET_QOS_SetMTL(ENET_QOS_Type *base, const enet_qos_config_t *config);
- /*!
- * @brief Set ENET DMA transmit buffer descriptors for one channel.
- *
- * @param base ENET peripheral base address.
- * @param bufferConfig ENET buffer configuration.
- * @param intTxEnable tx interrupt enable.
- * @param channel The channel number, 0 , 1.
- */
- static status_t ENET_QOS_TxDescriptorsInit(ENET_QOS_Type *base,
- const enet_qos_buffer_config_t *bufferConfig,
- bool intTxEnable,
- uint8_t channel);
- /*!
- * @brief Set ENET DMA receive buffer descriptors for one channel.
- *
- * @param base ENET peripheral base address.
- * @param bufferConfig ENET buffer configuration.
- * @param intRxEnable tx interrupt enable.
- * @param channel The channel number, 0, 1.
- */
- static status_t ENET_QOS_RxDescriptorsInit(ENET_QOS_Type *base,
- enet_qos_config_t *config,
- const enet_qos_buffer_config_t *bufferConfig,
- bool intRxEnable,
- uint8_t channel);
- /*!
- * @brief Sets the ENET 1588 feature.
- *
- * Enable the enhacement 1588 buffer descriptor mode and start
- * the 1588 timer.
- *
- * @param base ENET peripheral base address.
- * @param config The ENET configuration.
- * @param refClk_Hz The reference clock for ptp 1588.
- */
- static status_t ENET_QOS_SetPtp1588(ENET_QOS_Type *base, const enet_qos_config_t *config, uint32_t refClk_Hz);
- /*!
- * @brief Store the receive time-stamp for event PTP frame in the time-stamp buffer ring.
- *
- * @param base ENET peripheral base address.
- * @param handle ENET handler.
- * @param rxDesc The ENET receive descriptor pointer.
- * @param channel The rx channel.
- * @param ts The timestamp structure pointer.
- */
- static void ENET_QOS_StoreRxFrameTime(ENET_QOS_Type *base,
- enet_qos_handle_t *handle,
- enet_qos_rx_bd_struct_t *rxDesc,
- // uint8_t channel,
- enet_qos_ptp_time_t *ts);
- /*!
- * @brief Check if txDirtyRing available.
- *
- * @param txDirtyRing pointer to txDirtyRing
- * @retval txDirty available status.
- */
- static inline bool ENET_QOS_TxDirtyRingAvailable(enet_qos_tx_dirty_ring_t *txDirtyRing);
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- /*! @brief Pointers to enet bases for each instance. */
- static ENET_QOS_Type *const s_enetqosBases[] = ENET_QOS_BASE_PTRS;
- /*! @brief Pointers to enet IRQ number for each instance. */
- static const IRQn_Type s_enetqosIrqId[] = ENET_QOS_IRQS;
- /* ENET ISR for transactional APIs. */
- static enet_qos_isr_t s_enetqosIsr;
- /*! @brief Pointers to enet handles for each instance. */
- static enet_qos_handle_t *s_ENETHandle[ARRAY_SIZE(s_enetqosBases)] = {NULL};
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /*! @brief Pointers to enet clocks for each instance. */
- const clock_ip_name_t s_enetqosClock[ARRAY_SIZE(s_enetqosBases)] = ENETQOS_CLOCKS;
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- /*******************************************************************************
- * Code
- ******************************************************************************/
- static status_t ENET_QOS_PollStatusFlag(volatile uint32_t *regAddr, uint32_t mask, uint32_t readyStatus)
- {
- uint8_t retryTimes = 10U;
- status_t result = kStatus_Success;
- while ((readyStatus != (*regAddr & mask)) && (0U != retryTimes))
- {
- retryTimes--;
- SDK_DelayAtLeastUs(1U, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
- }
- if (retryTimes == 0U)
- {
- result = kStatus_ENET_QOS_Timeout;
- }
- return result;
- }
- /*!
- * brief Sets the ENET AVB feature.
- *
- * ENET_QOS AVB feature configuration, set transmit bandwidth.
- * This API is called when the AVB feature is required.
- *
- * param base ENET_QOS peripheral base address.
- * param config The ENET_QOS AVB feature configuration structure.
- * param queueIndex ENET_QOS queue index.
- */
- void ENET_QOS_AVBConfigure(ENET_QOS_Type *base, const enet_qos_cbs_config_t *config, uint8_t queueIndex)
- {
- assert(config != NULL);
- /* Enable AV algorithm */
- base->MTL_QUEUE[queueIndex].MTL_TXQX_ETS_CTRL |= ENET_QOS_MTL_TXQX_ETS_CTRL_AVALG_MASK;
- /* Configure send slope */
- base->MTL_QUEUE[queueIndex].MTL_TXQX_SNDSLP_CRDT = config->sendSlope;
- /* Configure idle slope (same register as tx weight) */
- base->MTL_QUEUE[queueIndex].MTL_TXQX_QNTM_WGHT = config->idleSlope;
- /* Configure high credit */
- base->MTL_QUEUE[queueIndex].MTL_TXQX_HI_CRDT = config->highCredit;
- /* Configure high credit */
- base->MTL_QUEUE[queueIndex].MTL_TXQX_LO_CRDT = config->lowCredit;
- }
- static uint16_t ENET_QOS_IncreaseIndex(uint16_t index, uint16_t max)
- {
- /* Increase the index. */
- index++;
- if (index >= max)
- {
- index = 0;
- }
- return index;
- }
- static uint32_t ENET_QOS_ReverseBits(uint32_t value)
- {
- value = ((value & 0x55555555UL) << 1U) | ((value >> 1U) & 0x55555555UL);
- value = ((value & 0x33333333UL) << 2U) | ((value >> 2U) & 0x33333333UL);
- value = ((value & 0x0F0F0F0FUL) << 4U) | ((value >> 4U) & 0x0F0F0F0FUL);
- return (value >> 24U) | ((value >> 8U) & 0xFF00UL) | ((value & 0xFF00UL) << 8U) | (value << 24U);
- }
- static void ENET_QOS_SetDMAControl(ENET_QOS_Type *base, const enet_qos_config_t *config)
- {
- assert(config != NULL);
- uint8_t index;
- uint32_t reg;
- uint32_t burstLen;
- /* Reset first and wait for the complete
- * The reset bit will automatically be cleared after complete. */
- base->DMA_MODE |= ENET_QOS_DMA_MODE_SWR_MASK;
- while ((base->DMA_MODE & ENET_QOS_DMA_MODE_SWR_MASK) != 0U)
- {
- }
- /* Set the burst length. */
- for (index = 0; index < ENET_QOS_RING_NUM_MAX; index++)
- {
- burstLen = (uint32_t)kENET_QOS_BurstLen1;
- if (config->multiqueueCfg != NULL)
- {
- burstLen = (uint32_t)config->multiqueueCfg->burstLen;
- }
- base->DMA_CH[index].DMA_CHX_CTRL = burstLen & ENET_QOS_DMA_CHX_CTRL_PBLx8_MASK;
- reg = base->DMA_CH[index].DMA_CHX_TX_CTRL & ~ENET_QOS_DMA_CHX_TX_CTRL_TxPBL_MASK;
- base->DMA_CH[index].DMA_CHX_TX_CTRL = reg | ENET_QOS_DMA_CHX_TX_CTRL_TxPBL(burstLen & 0x3FU);
- reg = base->DMA_CH[index].DMA_CHX_RX_CTRL & ~ENET_QOS_DMA_CHX_RX_CTRL_RxPBL_MASK;
- base->DMA_CH[index].DMA_CHX_RX_CTRL = reg | ENET_QOS_DMA_CHX_RX_CTRL_RxPBL(burstLen & 0x3FU);
- }
- }
- static void ENET_QOS_SetMTL(ENET_QOS_Type *base, const enet_qos_config_t *config)
- {
- assert(config != NULL);
- uint32_t txqOpreg = 0;
- uint32_t rxqOpReg = 0;
- enet_qos_multiqueue_config_t *multiqCfg = config->multiqueueCfg;
- uint8_t index;
- /* Set transmit operation mode. */
- if ((config->specialControl & (uint32_t)kENET_QOS_StoreAndForward) != 0U)
- {
- txqOpreg = ENET_QOS_MTL_TXQX_OP_MODE_TSF_MASK;
- rxqOpReg = ENET_QOS_MTL_RXQX_OP_MODE_RSF_MASK;
- }
- /* Set transmit operation mode. */
- txqOpreg |= ENET_QOS_MTL_TXQX_OP_MODE_FTQ_MASK;
- /* Set receive operation mode. */
- rxqOpReg |= ENET_QOS_MTL_RXQX_OP_MODE_FUP_MASK | ENET_QOS_MTL_RXQX_OP_MODE_RFD(3U) |
- ENET_QOS_MTL_RXQX_OP_MODE_RFA(1U) | ENET_QOS_MTL_RXQX_OP_MODE_EHFC_MASK;
- if (multiqCfg == NULL)
- {
- txqOpreg |=
- ENET_QOS_MTL_TXQX_OP_MODE_TQS(((uint32_t)ENET_QOS_MTL_TXFIFOSIZE / (uint32_t)ENET_QOS_FIFOSIZE_UNIT - 1U));
- rxqOpReg |=
- ENET_QOS_MTL_RXQX_OP_MODE_RQS(((uint32_t)ENET_QOS_MTL_RXFIFOSIZE / (uint32_t)ENET_QOS_FIFOSIZE_UNIT - 1U));
- base->MTL_QUEUE[0].MTL_TXQX_OP_MODE = txqOpreg | ENET_QOS_MTL_TXQX_OP_MODE_TXQEN((uint32_t)kENET_QOS_DCB_Mode);
- base->MTL_QUEUE[0].MTL_RXQX_OP_MODE = rxqOpReg;
- }
- else
- {
- /* Set the schedule/arbitration(set for multiple queues). */
- base->MTL_OPERATION_MODE = ENET_QOS_MTL_OPERATION_MODE_SCHALG(multiqCfg->mtltxSche) |
- ENET_QOS_MTL_OPERATION_MODE_RAA(multiqCfg->mtlrxSche);
- for (index = 0; index < multiqCfg->txQueueUse; index++)
- {
- txqOpreg |= ENET_QOS_MTL_TXQX_OP_MODE_TQS(
- ((uint32_t)ENET_QOS_MTL_TXFIFOSIZE / ((uint32_t)multiqCfg->txQueueUse * ENET_QOS_FIFOSIZE_UNIT)) - 1U);
- base->MTL_QUEUE[index].MTL_TXQX_OP_MODE =
- txqOpreg | ENET_QOS_MTL_TXQX_OP_MODE_TXQEN((uint32_t)multiqCfg->txQueueConfig[index].mode);
- if (multiqCfg->txQueueConfig[index].mode == kENET_QOS_AVB_Mode)
- {
- ENET_QOS_AVBConfigure(base, multiqCfg->txQueueConfig[index].cbsConfig, index);
- }
- else
- {
- base->MTL_QUEUE[index].MTL_TXQX_QNTM_WGHT = multiqCfg->txQueueConfig[index].weight;
- }
- }
- volatile uint32_t *mtlrxQuemapReg;
- uint8_t configIndex;
- for (index = 0; index < multiqCfg->rxQueueUse; index++)
- {
- rxqOpReg |= ENET_QOS_MTL_RXQX_OP_MODE_RQS(
- ((uint32_t)ENET_QOS_MTL_RXFIFOSIZE / ((uint32_t)multiqCfg->rxQueueUse * ENET_QOS_FIFOSIZE_UNIT)) - 1U);
- base->MTL_QUEUE[index].MTL_RXQX_OP_MODE = rxqOpReg;
- mtlrxQuemapReg = (index < 4U) ? &base->MTL_RXQ_DMA_MAP0 : &base->MTL_RXQ_DMA_MAP1;
- configIndex = (index & 0x3U);
- *mtlrxQuemapReg &= ~((uint32_t)ENET_QOS_MTL_RXQ_DMA_MAP0_Q0MDMACH_MASK << (8U * configIndex));
- *mtlrxQuemapReg |= (uint32_t)ENET_QOS_MTL_RXQ_DMA_MAP0_Q0MDMACH(multiqCfg->rxQueueConfig[index].mapChannel)
- << (8U * configIndex);
- }
- }
- }
- static void ENET_QOS_SetMacControl(ENET_QOS_Type *base,
- const enet_qos_config_t *config,
- uint8_t *macAddr,
- uint8_t macCount)
- {
- assert(config != NULL);
- uint32_t reg = 0;
- /* Set Macaddr */
- /* The dma channel 0 is set as to which the rx packet
- * whose DA matches the MAC address content is routed. */
- if (macAddr != NULL)
- {
- for (uint8_t i = 0; i < macCount; i++)
- {
- ENET_QOS_SetMacAddr(base, macAddr, i);
- }
- }
- /* Set the receive filter. */
- reg =
- ENET_QOS_MAC_PACKET_FILTER_PR(((config->specialControl & (uint32_t)kENET_QOS_PromiscuousEnable) != 0U) ? 1U :
- 0U) |
- ENET_QOS_MAC_PACKET_FILTER_DBF(((config->specialControl & (uint32_t)kENET_QOS_BroadCastRxDisable) != 0U) ? 1U :
- 0U) |
- ENET_QOS_MAC_PACKET_FILTER_PM(((config->specialControl & (uint32_t)kENET_QOS_MulticastAllEnable) != 0U) ? 1U :
- 0U) |
- ENET_QOS_MAC_PACKET_FILTER_HMC(((config->specialControl & (uint32_t)kENET_QOS_HashMulticastEnable) != 0U) ? 1U :
- 0U);
- base->MAC_PACKET_FILTER = reg;
- /* Flow control. */
- if ((config->specialControl & (uint32_t)kENET_QOS_FlowControlEnable) != 0U)
- {
- base->MAC_RX_FLOW_CTRL = ENET_QOS_MAC_RX_FLOW_CTRL_RFE_MASK | ENET_QOS_MAC_RX_FLOW_CTRL_UP_MASK;
- base->MAC_TX_FLOW_CTRL_Q[0] = ENET_QOS_MAC_TX_FLOW_CTRL_Q_PT(config->pauseDuration);
- }
- /* Set the 1us ticket. */
- reg = config->csrClock_Hz / ENET_QOS_MICRSECS_ONESECOND - 1U;
- base->MAC_ONEUS_TIC_COUNTER = ENET_QOS_MAC_ONEUS_TIC_COUNTER_TIC_1US_CNTR(reg);
- /* Set the speed and duplex. */
- reg = ENET_QOS_MAC_CONFIGURATION_DM(config->miiDuplex) | (uint32_t)config->miiSpeed |
- ENET_QOS_MAC_CONFIGURATION_S2KP(((config->specialControl & (uint32_t)kENET_QOS_8023AS2KPacket) != 0U) ? 1U :
- 0U);
- if (config->miiDuplex == kENET_QOS_MiiHalfDuplex)
- {
- reg |= ENET_QOS_MAC_CONFIGURATION_IPG(ENET_QOS_HALFDUPLEX_DEFAULTIPG);
- }
- base->MAC_CONFIGURATION = reg;
- if (config->multiqueueCfg != NULL)
- {
- reg = 0U;
- uint8_t configIndex;
- enet_qos_multiqueue_config_t *multiqCfg = config->multiqueueCfg;
- uint32_t txQueuePrioMap0 = base->MAC_TXQ_PRTY_MAP0;
- uint32_t txQueuePrioMap1 = base->MAC_TXQ_PRTY_MAP1;
- uint32_t rxQueuePrioMap0 = base->MAC_RXQ_CTRL[2];
- uint32_t rxQueuePrioMap1 = base->MAC_RXQ_CTRL[3];
- uint32_t rxCtrlReg1 = base->MAC_RXQ_CTRL[1];
- for (uint8_t index = 0U; index < multiqCfg->txQueueUse; index++)
- {
- configIndex = index & 0x3U;
- /* Configure tx queue priority. */
- if (index < 4U)
- {
- txQueuePrioMap0 &= ~((uint32_t)ENET_QOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK << (8U * configIndex));
- txQueuePrioMap0 |= (uint32_t)ENET_QOS_MAC_TXQ_PRTY_MAP0_PSTQ0(multiqCfg->txQueueConfig[index].priority)
- << (8U * configIndex);
- }
- else
- {
- txQueuePrioMap1 &= ~((uint32_t)ENET_QOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK << (8U * configIndex));
- txQueuePrioMap1 |= (uint32_t)ENET_QOS_MAC_TXQ_PRTY_MAP0_PSTQ0(multiqCfg->txQueueConfig[index].priority)
- << (8U * configIndex);
- }
- }
- for (uint8_t index = 0U; index < multiqCfg->rxQueueUse; index++)
- {
- configIndex = index & 0x3U;
- /* Configure rx queue priority. */
- if (index < 4U)
- {
- rxQueuePrioMap0 &= ~((uint32_t)ENET_QOS_MAC_RXQ_CTRL_PSRQ0_MASK << (8U * configIndex));
- rxQueuePrioMap0 |= (uint32_t)ENET_QOS_MAC_RXQ_CTRL_PSRQ0(multiqCfg->rxQueueConfig[index].priority)
- << (8U * configIndex);
- }
- else
- {
- rxQueuePrioMap1 &= ~((uint32_t)ENET_QOS_MAC_RXQ_CTRL_PSRQ0_MASK << (8U * configIndex));
- rxQueuePrioMap1 |= (uint32_t)ENET_QOS_MAC_RXQ_CTRL_PSRQ0(multiqCfg->rxQueueConfig[index].priority)
- << (8U * configIndex);
- }
- /* Configure queue enable mode. */
- reg |= ENET_QOS_MAC_RXQ_CTRL_RXQ0EN((uint32_t)multiqCfg->rxQueueConfig[index].mode) << (2U * index);
- /* Configure rx queue routing */
- if (((uint8_t)multiqCfg->rxQueueConfig[index].packetRoute & (uint8_t)kENET_QOS_PacketAVCPQ) != 0U)
- {
- rxCtrlReg1 &= ~ENET_QOS_MAC_RXQ_CTRL_AVCPQ_MASK;
- rxCtrlReg1 |= (ENET_QOS_MAC_RXQ_CTRL_AVCPQ(index) | ENET_QOS_MAC_RXQ_CTRL_TACPQE_MASK);
- }
- if (((uint8_t)multiqCfg->rxQueueConfig[index].packetRoute & (uint8_t)kENET_QOS_PacketPTPQ) != 0U)
- {
- rxCtrlReg1 &= ~ENET_QOS_MAC_RXQ_CTRL_PTPQ_MASK;
- rxCtrlReg1 |= ENET_QOS_MAC_RXQ_CTRL_PTPQ(index);
- }
- if (((uint8_t)multiqCfg->rxQueueConfig[index].packetRoute & (uint8_t)kENET_QOS_PacketDCBCPQ) != 0U)
- {
- rxCtrlReg1 &= ~ENET_QOS_MAC_RXQ_CTRL_DCBCPQ_MASK;
- rxCtrlReg1 |= ENET_QOS_MAC_RXQ_CTRL_DCBCPQ(index);
- }
- if (((uint8_t)multiqCfg->rxQueueConfig[index].packetRoute & (uint8_t)kENET_QOS_PacketUPQ) != 0U)
- {
- rxCtrlReg1 &= ~ENET_QOS_MAC_RXQ_CTRL_UPQ_MASK;
- rxCtrlReg1 |= ENET_QOS_MAC_RXQ_CTRL_UPQ(index);
- }
- if (((uint8_t)multiqCfg->rxQueueConfig[index].packetRoute & (uint8_t)kENET_QOS_PacketMCBCQ) != 0U)
- {
- rxCtrlReg1 &= ~ENET_QOS_MAC_RXQ_CTRL_MCBCQ_MASK;
- rxCtrlReg1 |= (ENET_QOS_MAC_RXQ_CTRL_MCBCQ(index) | ENET_QOS_MAC_RXQ_CTRL_MCBCQEN_MASK);
- }
- }
- base->MAC_TXQ_PRTY_MAP0 = txQueuePrioMap0;
- base->MAC_TXQ_PRTY_MAP1 = txQueuePrioMap1;
- base->MAC_RXQ_CTRL[2] = rxQueuePrioMap0;
- base->MAC_RXQ_CTRL[3] = rxQueuePrioMap1;
- base->MAC_RXQ_CTRL[1] = rxCtrlReg1;
- }
- else
- {
- /* Configure queue enable mode. */
- reg = ENET_QOS_MAC_RXQ_CTRL_RXQ0EN((uint32_t)kENET_QOS_DCB_Mode);
- }
- /* Enable queue. */
- base->MAC_RXQ_CTRL[0] = reg;
- /* Mask MMC counters interrupts as we don't handle
- * them in the interrupt handler.
- */
- base->MAC_MMC_RX_INTERRUPT_MASK = 0xFFFFFFFFU;
- base->MAC_MMC_TX_INTERRUPT_MASK = 0xFFFFFFFFU;
- base->MAC_MMC_IPC_RX_INTERRUPT_MASK = 0xFFFFFFFFU;
- base->MAC_MMC_FPE_RX_INTERRUPT_MASK = 0xFFFFFFFFU;
- base->MAC_MMC_FPE_TX_INTERRUPT_MASK = 0xFFFFFFFFU;
- }
- static status_t ENET_QOS_TxDescriptorsInit(ENET_QOS_Type *base,
- const enet_qos_buffer_config_t *bufferConfig,
- bool intTxEnable,
- uint8_t channel)
- {
- uint16_t j;
- enet_qos_tx_bd_struct_t *txbdPtr;
- uint32_t control = intTxEnable ? ENET_QOS_TXDESCRIP_RD_IOC_MASK : 0U;
- const enet_qos_buffer_config_t *buffCfg = bufferConfig;
- if (buffCfg == NULL)
- {
- return kStatus_InvalidArgument;
- }
- /* Check the ring length. */
- if (buffCfg->txRingLen < ENET_QOS_MIN_RINGLEN)
- {
- return kStatus_InvalidArgument;
- }
- /* Set the tx descriptor start/tail pointer, shall be word aligned. */
- base->DMA_CH[channel].DMA_CHX_TXDESC_LIST_ADDR =
- (uint32_t)buffCfg->txDescStartAddrAlign & ENET_QOS_DMA_CHX_TXDESC_LIST_ADDR_TDESLA_MASK;
- base->DMA_CH[channel].DMA_CHX_TXDESC_TAIL_PTR =
- (uint32_t)buffCfg->txDescTailAddrAlign & ENET_QOS_DMA_CHX_TXDESC_TAIL_PTR_TDTP_MASK;
- /* Set the tx ring length. */
- base->DMA_CH[channel].DMA_CHX_TXDESC_RING_LENGTH =
- ((uint32_t)buffCfg->txRingLen - 1U) & ENET_QOS_DMA_CHX_TXDESC_RING_LENGTH_TDRL_MASK;
- /* Init the txbdPtr to the transmit descriptor start address. */
- txbdPtr = (enet_qos_tx_bd_struct_t *)(buffCfg->txDescStartAddrAlign);
- for (j = 0; j < buffCfg->txRingLen; j++)
- {
- txbdPtr->buff1Addr = 0;
- txbdPtr->buff2Addr = 0;
- txbdPtr->buffLen = control;
- txbdPtr->controlStat = 0;
- txbdPtr++;
- }
- return kStatus_Success;
- }
- static status_t ENET_QOS_RxDescriptorsInit(ENET_QOS_Type *base,
- enet_qos_config_t *config,
- const enet_qos_buffer_config_t *bufferConfig,
- bool intRxEnable,
- uint8_t channel)
- {
- uint16_t j;
- uint32_t reg;
- enet_qos_rx_bd_struct_t *rxbdPtr;
- uint16_t index;
- bool doubleBuffEnable = ((config->specialControl & (uint32_t)kENET_QOS_DescDoubleBuffer) != 0U) ? true : false;
- const enet_qos_buffer_config_t *buffCfg = bufferConfig;
- uint32_t control = ENET_QOS_RXDESCRIP_RD_BUFF1VALID_MASK;
- if (buffCfg == NULL)
- {
- return kStatus_InvalidArgument;
- }
- if (intRxEnable)
- {
- control |= ENET_QOS_RXDESCRIP_RD_IOC_MASK;
- }
- if (doubleBuffEnable)
- {
- control |= ENET_QOS_RXDESCRIP_RD_BUFF2VALID_MASK;
- }
- /* Not give ownership to DMA before Rx buffer is ready */
- if ((config->rxBuffAlloc == NULL) || (config->rxBuffFree == NULL))
- {
- control |= ENET_QOS_RXDESCRIP_WR_OWN_MASK;
- }
- /* Check the ring length. */
- if (buffCfg->rxRingLen < ENET_QOS_MIN_RINGLEN)
- {
- return kStatus_InvalidArgument;
- }
- /* Set the rx descriptor start/tail pointer, shall be word aligned. */
- base->DMA_CH[channel].DMA_CHX_RXDESC_LIST_ADDR =
- (uint32_t)buffCfg->rxDescStartAddrAlign & ENET_QOS_DMA_CHX_RXDESC_LIST_ADDR_RDESLA_MASK;
- base->DMA_CH[channel].DMA_CHX_RXDESC_TAIL_PTR =
- (uint32_t)buffCfg->rxDescTailAddrAlign & ENET_QOS_DMA_CHX_RXDESC_TAIL_PTR_RDTP_MASK;
- base->DMA_CH[channel].DMA_CHX_RXDESC_RING_LENGTH =
- ((uint32_t)buffCfg->rxRingLen - 1U) & ENET_QOS_DMA_CHX_RXDESC_RING_LENGTH_RDRL_MASK;
- reg = base->DMA_CH[channel].DMA_CHX_RX_CTRL & ~ENET_QOS_DMA_CHX_RX_CTRL_RBSZ_13_y_MASK;
- reg |= ENET_QOS_DMA_CHX_RX_CTRL_RBSZ_13_y(buffCfg->rxBuffSizeAlign >> ENET_QOS_RXBUFF_IGNORELSB_BITS);
- base->DMA_CH[channel].DMA_CHX_RX_CTRL = reg;
- /* Init the rxbdPtr to the receive descriptor start address. */
- rxbdPtr = (enet_qos_rx_bd_struct_t *)(buffCfg->rxDescStartAddrAlign);
- for (j = 0U; j < buffCfg->rxRingLen; j++)
- {
- if ((config->rxBuffAlloc == NULL) || (config->rxBuffFree == NULL))
- {
- if (doubleBuffEnable)
- {
- index = 2U * j;
- }
- else
- {
- index = j;
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buffCfg->rxBufferStartAddr[index] =
- MEMORY_ConvertMemoryMapAddress((uint32_t)buffCfg->rxBufferStartAddr[index], kMEMORY_Local2DMA);
- #endif
- rxbdPtr->buff1Addr = buffCfg->rxBufferStartAddr[index];
- /* The second buffer is set with 0 because it is not required for normal case. */
- if (doubleBuffEnable)
- {
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buffCfg->rxBufferStartAddr[index + 1U] =
- MEMORY_ConvertMemoryMapAddress((uint32_t)buffCfg->rxBufferStartAddr[index + 1U], kMEMORY_Local2DMA);
- #endif
- rxbdPtr->buff2Addr = buffCfg->rxBufferStartAddr[index + 1U];
- }
- else
- {
- rxbdPtr->buff2Addr = 0;
- }
- }
- /* Set the valid and DMA own flag.*/
- rxbdPtr->control = control;
- rxbdPtr++;
- }
- return kStatus_Success;
- }
- static status_t ENET_QOS_SetPtp1588(ENET_QOS_Type *base, const enet_qos_config_t *config, uint32_t refClk_Hz)
- {
- assert(config != NULL);
- assert(config->ptpConfig != NULL);
- assert(refClk_Hz != 0U);
- uint32_t control = 0U;
- status_t result = kStatus_Success;
- enet_qos_ptp_config_t *ptpConfig = config->ptpConfig;
- uint32_t ptpClk_Hz = refClk_Hz;
- uint32_t ssInc, snsSinc;
- /* Clear the timestamp interrupt first. */
- base->MAC_INTERRUPT_ENABLE &= ~ENET_QOS_MAC_INTERRUPT_ENABLE_TSIE_MASK;
- if (ptpConfig->fineUpdateEnable)
- {
- control |= ENET_QOS_MAC_TIMESTAMP_CONTROL_TSCFUPDT_MASK;
- ptpClk_Hz = ptpConfig->systemTimeClock_Hz; /* PTP clock 50MHz. */
- }
- /* Enable the IEEE 1588 timestamping and snapshot for event message. */
- control |= ENET_QOS_MAC_TIMESTAMP_CONTROL_TSENA_MASK | ENET_QOS_MAC_TIMESTAMP_CONTROL_TSIPV4ENA_MASK |
- ENET_QOS_MAC_TIMESTAMP_CONTROL_TSIPV6ENA_MASK | ENET_QOS_MAC_TIMESTAMP_CONTROL_TSENALL_MASK |
- ENET_QOS_MAC_TIMESTAMP_CONTROL_TSEVNTENA_MASK | ENET_QOS_MAC_TIMESTAMP_CONTROL_SNAPTYPSEL_MASK |
- ENET_QOS_MAC_TIMESTAMP_CONTROL_TSCTRLSSR(ptpConfig->tsRollover);
- if (ptpConfig->ptp1588V2Enable)
- {
- control |= ENET_QOS_MAC_TIMESTAMP_CONTROL_TSVER2ENA_MASK | ENET_QOS_MAC_TIMESTAMP_CONTROL_TSIPENA_MASK;
- }
- /* Initialize the sub-second increment register. */
- if (ptpConfig->tsRollover == kENET_QOS_DigitalRollover)
- {
- ssInc = (uint32_t)(((uint64_t)ENET_QOS_NANOSECS_ONESECOND << 8U) / ptpClk_Hz);
- }
- else
- {
- ssInc = (uint32_t)((((uint64_t)ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_TSSS_MASK + 1U) << 8U) / ptpClk_Hz);
- }
- snsSinc = ssInc & 0xFFU;
- ssInc = (ssInc >> 8U) & 0xFFU;
- base->MAC_TIMESTAMP_CONTROL = control;
- /* Initialize the system timer. */
- base->MAC_SYSTEM_TIME_NANOSECONDS_UPDATE = 0;
- /* Set the second.*/
- base->MAC_SYSTEM_TIME_SECONDS_UPDATE = 0;
- base->MAC_SYSTEM_TIME_HIGHER_WORD_SECONDS = 0;
- /* Initialize the system timer. */
- base->MAC_TIMESTAMP_CONTROL |= ENET_QOS_MAC_TIMESTAMP_CONTROL_TSINIT_MASK;
- while ((base->MAC_TIMESTAMP_CONTROL & ENET_QOS_MAC_TIMESTAMP_CONTROL_TSINIT_MASK) != 0U)
- {
- }
- base->MAC_SUB_SECOND_INCREMENT =
- ENET_QOS_MAC_SUB_SECOND_INCREMENT_SSINC(ssInc) | ENET_QOS_MAC_SUB_SECOND_INCREMENT_SNSINC(snsSinc);
- /* Set the initial added value for the fine update. */
- if (ptpConfig->fineUpdateEnable)
- {
- result = ENET_QOS_Ptp1588CorrectTimerInFine(base, ptpConfig->defaultAddend);
- }
- return result;
- }
- static inline bool ENET_QOS_TxDirtyRingAvailable(enet_qos_tx_dirty_ring_t *txDirtyRing)
- {
- return !txDirtyRing->isFull;
- }
- static void ENET_QOS_StoreRxFrameTime(ENET_QOS_Type *base,
- enet_qos_handle_t *handle,
- enet_qos_rx_bd_struct_t *rxDesc,
- enet_qos_ptp_time_t *ts)
- {
- assert(ts != NULL);
- uint32_t nanosecond;
- /* Get transmit time stamp second. */
- nanosecond = rxDesc->buff1Addr;
- if ((base->MAC_TIMESTAMP_CONTROL & ENET_QOS_MAC_TIMESTAMP_CONTROL_TSCTRLSSR_MASK) == 0U)
- {
- /* Binary rollover, 0.465ns accuracy. */
- nanosecond = (uint32_t)(((uint64_t)nanosecond * 465U) / 1000U);
- }
- ts->second = rxDesc->reserved;
- ts->nanosecond = nanosecond;
- }
- uint32_t ENET_QOS_GetInstance(ENET_QOS_Type *base)
- {
- uint32_t instance;
- /* Find the instance index from base address mappings. */
- for (instance = 0; instance < ARRAY_SIZE(s_enetqosBases); instance++)
- {
- if (s_enetqosBases[instance] == base)
- {
- break;
- }
- }
- assert(instance < ARRAY_SIZE(s_enetqosBases));
- return instance;
- }
- /*!
- * brief Gets the ENET default configuration structure.
- *
- * The purpose of this API is to get the default ENET configure
- * structure for ENET_QOS_Init(). User may use the initialized
- * structure unchanged in ENET_QOS_Init(), or modify some fields of the
- * structure before calling ENET_QOS_Init().
- * Example:
- code
- enet_qos_config_t config;
- ENET_QOS_GetDefaultConfig(&config);
- endcode
- * param config The ENET mac controller configuration structure pointer.
- */
- void ENET_QOS_GetDefaultConfig(enet_qos_config_t *config)
- {
- /* Checks input parameter. */
- assert(config != NULL);
- /* Initializes the configure structure to zero. */
- (void)memset(config, 0, sizeof(*config));
- /* Sets RGMII mode, full duplex, 1000Mbps for MAC and PHY data interface. */
- config->miiMode = kENET_QOS_RgmiiMode;
- config->miiSpeed = kENET_QOS_MiiSpeed1000M;
- config->miiDuplex = kENET_QOS_MiiFullDuplex;
- /* Sets default configuration for other options. */
- config->specialControl = 0;
- config->multiqueueCfg = NULL;
- config->pauseDuration = 0;
- config->ptpConfig = NULL;
- }
- /*!
- * brief Initializes the ENET module.
- *
- * This function set up the with ENET basic configuration.
- *
- * param base ENET peripheral base address.
- * param config ENET mac configuration structure pointer.
- * The "enet_qos_config_t" type mac configuration return from ENET_QOS_GetDefaultConfig
- * can be used directly. It is also possible to verify the Mac configuration using other methods.
- * param macAddr ENET mac address of Ethernet device. This MAC address should be
- * provided.
- * param refclkSrc_Hz ENET input reference clock.
- */
- status_t ENET_QOS_Up(
- ENET_QOS_Type *base, const enet_qos_config_t *config, uint8_t *macAddr, uint8_t macCount, uint32_t refclkSrc_Hz)
- {
- assert(config != NULL);
- status_t result = kStatus_Success;
- /* Initializes the ENET MTL with basic function. */
- ENET_QOS_SetMTL(base, config);
- /* Initializes the ENET MAC with basic function. */
- ENET_QOS_SetMacControl(base, config, macAddr, macCount);
- return result;
- }
- /*!
- * brief Initializes the ENET module.
- *
- * This function ungates the module clock and initializes it with the ENET basic
- * configuration.
- *
- * param base ENET peripheral base address.
- * param config ENET mac configuration structure pointer.
- * The "enet_qos_config_t" type mac configuration return from ENET_QOS_GetDefaultConfig
- * can be used directly. It is also possible to verify the Mac configuration using other methods.
- * param macAddr ENET mac address of Ethernet device. This MAC address should be
- * provided.
- * param refclkSrc_Hz ENET input reference clock.
- */
- status_t ENET_QOS_Init(
- ENET_QOS_Type *base, const enet_qos_config_t *config, uint8_t *macAddr, uint8_t macCount, uint32_t refclkSrc_Hz)
- {
- assert(config != NULL);
- status_t result = kStatus_Success;
- uint32_t instance = ENET_QOS_GetInstance(base);
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* Ungate ENET clock. */
- (void)CLOCK_EnableClock(s_enetqosClock[instance]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- /* System configure fistly. */
- ENET_QOS_SetSYSControl(config->miiMode);
- /* Initializes the ENET DMA with basic function. */
- ENET_QOS_SetDMAControl(base, config);
- (void)ENET_QOS_Up(base, config, macAddr, macCount, refclkSrc_Hz);
- if (config->ptpConfig != NULL)
- {
- result = ENET_QOS_SetPtp1588(base, config, refclkSrc_Hz);
- }
- return result;
- }
- /*!
- * brief Stops the ENET module.
- * This function disables the ENET module.
- *
- * param base ENET peripheral base address.
- */
- void ENET_QOS_Down(ENET_QOS_Type *base)
- {
- enet_qos_handle_t *handle = s_ENETHandle[ENET_QOS_GetInstance(base)];
- enet_qos_tx_bd_struct_t *txbdPtr;
- uint8_t index;
- uint32_t primask, j;
- /* Disable all interrupts */
- ENET_QOS_DisableInterrupts(base, 0xFF);
- for (index = 0; index < handle->txQueueUse; index++)
- {
- enet_qos_tx_bd_ring_t *txBdRing = &handle->txBdRing[index];
- enet_qos_tx_dirty_ring_t *txDirtyRing = (enet_qos_tx_dirty_ring_t *)&handle->txDirtyRing[index];
- /* Clear pending descriptors */
- if (handle->callback != NULL)
- {
- while (txBdRing->txDescUsed > 0U)
- {
- enet_qos_frame_info_t *txDirty = &txDirtyRing->txDirtyBase[txDirtyRing->txConsumIdx];
- txDirty->isTsAvail = false;
- handle->callback(base, handle, kENET_QOS_TxIntEvent, index, handle->userData);
- primask = DisableGlobalIRQ();
- txBdRing->txDescUsed--;
- EnableGlobalIRQ(primask);
- }
- }
- /* Disable Tx DMA */
- base->DMA_CH[index].DMA_CHX_TX_CTRL &= ~ENET_QOS_DMA_CHX_TX_CTRL_ST_MASK;
- /* Flush Tx Queue */
- base->MTL_QUEUE[index].MTL_TXQX_OP_MODE |= ENET_QOS_MTL_TXQX_OP_MODE_FTQ_MASK;
- /* Wait until Tx Queue is empty */
- while ((base->MTL_QUEUE[index].MTL_TXQX_DBG &
- (ENET_QOS_MTL_TXQX_DBG_TXQSTS_MASK | ENET_QOS_MTL_TXQX_DBG_PTXQ_MASK)) != 0U)
- {
- }
- /* Reset hardware ring buffer */
- base->DMA_CH[index].DMA_CHX_TXDESC_LIST_ADDR =
- (uint32_t)handle->txBdRing[index].txBdBase & ENET_QOS_DMA_CHX_TXDESC_LIST_ADDR_TDESLA_MASK;
- /* Reset software ring buffer */
- handle->txBdRing[index].txGenIdx = 0;
- handle->txBdRing[index].txConsumIdx = 0;
- handle->txBdRing[index].txDescUsed = 0;
- handle->txDirtyRing[index].txGenIdx = 0;
- handle->txDirtyRing[index].txConsumIdx = 0;
- handle->txDirtyRing[index].isFull = false;
- txbdPtr = (enet_qos_tx_bd_struct_t *)(handle->txBdRing[index].txBdBase);
- for (j = 0; j < handle->txBdRing[index].txRingLen; j++)
- {
- txbdPtr->buff1Addr = 0;
- txbdPtr->buff2Addr = 0;
- txbdPtr->buffLen = 0;
- txbdPtr->controlStat = 0;
- txbdPtr++;
- }
- }
- /* Disable MAC Rx/Tx */
- base->MAC_CONFIGURATION &= ~(ENET_QOS_MAC_CONFIGURATION_TE_MASK | ENET_QOS_MAC_CONFIGURATION_RE_MASK);
- /* Disable Rx DMA */
- for (index = 0; index < handle->rxQueueUse; index++)
- {
- base->DMA_CH[index].DMA_CHX_RX_CTRL &= ~ENET_QOS_DMA_CHX_RX_CTRL_SR_MASK;
- }
- }
- /*!
- * brief Deinitializes the ENET module.
- * This function gates the module clock and disables the ENET module.
- *
- * param base ENET peripheral base address.
- */
- void ENET_QOS_Deinit(ENET_QOS_Type *base)
- {
- /* Reset first and wait for the complete
- * The reset bit will automatically be cleared after complete. */
- base->DMA_MODE |= ENET_QOS_DMA_MODE_SWR_MASK;
- while ((base->DMA_MODE & ENET_QOS_DMA_MODE_SWR_MASK) != 0U)
- {
- }
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* Disables the clock source. */
- (void)CLOCK_DisableClock(s_enetqosClock[ENET_QOS_GetInstance(base)]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- }
- /*!
- * brief Initialize for all ENET descriptors.
- *
- * note This function is do all tx/rx descriptors initialization. Because this API
- * read all interrupt registers first and then set the interrupt flag for all descriptos,
- * if the interrupt register is set. so the descriptor initialization should be called
- * after ENET_QOS_Init(), ENET_QOS_EnableInterrupts() and ENET_QOS_CreateHandle()(if transactional APIs
- * are used).
- *
- * param base ENET peripheral base address.
- * param config The configuration for ENET.
- * param bufferConfig All buffers configuration.
- */
- status_t ENET_QOS_DescriptorInit(ENET_QOS_Type *base, enet_qos_config_t *config, enet_qos_buffer_config_t *bufferConfig)
- {
- assert(config != NULL);
- assert(bufferConfig != NULL);
- bool intTxEnable = false;
- bool intRxEnable = false;
- uint8_t ringNum = 1;
- uint8_t txQueueUse = 1;
- uint8_t rxQueueUse = 1;
- uint8_t channel;
- if (config->multiqueueCfg != NULL)
- {
- ringNum = MAX(config->multiqueueCfg->txQueueUse, config->multiqueueCfg->rxQueueUse);
- txQueueUse = config->multiqueueCfg->txQueueUse;
- rxQueueUse = config->multiqueueCfg->rxQueueUse;
- }
- for (channel = 0; channel < ringNum; channel++)
- {
- intRxEnable = ((base->DMA_CH[channel].DMA_CHX_INT_EN & ENET_QOS_DMA_CHX_INT_EN_RIE_MASK) != 0U) ? true : false;
- intTxEnable = ((base->DMA_CH[channel].DMA_CHX_INT_EN & ENET_QOS_DMA_CHX_INT_EN_TIE_MASK) != 0U) ? true : false;
- if (channel < txQueueUse)
- {
- if ((ENET_QOS_TxDescriptorsInit(base, bufferConfig, intTxEnable, channel) != kStatus_Success))
- {
- return kStatus_Fail;
- }
- }
- if (channel < rxQueueUse)
- {
- if ((ENET_QOS_RxDescriptorsInit(base, config, bufferConfig, intRxEnable, channel) != kStatus_Success))
- {
- return kStatus_Fail;
- }
- }
- bufferConfig++;
- }
- return kStatus_Success;
- }
- /*!
- * brief Allocates Rx buffers for all BDs.
- * It's used for zero copy Rx. In zero copy Rx case, Rx buffers are dynamic. This function
- * will populate initial buffers in all BDs for receiving. Then ENET_QOS_GetRxFrame() is used
- * to get Rx frame with zero copy, it will allocate new buffer to replace the buffer in BD taken
- * by application application should free those buffers after they're used.
- *
- * note This function should be called after ENET_QOS_CreateHandler() and buffer allocating callback
- * function should be ready.
- *
- * param base ENET_QOS peripheral base address.
- * param handle The ENET_QOS handler structure. This is the same handler pointer used in the ENET_QOS_Init.
- */
- status_t ENET_QOS_RxBufferAllocAll(ENET_QOS_Type *base, enet_qos_handle_t *handle)
- {
- status_t result = kStatus_Success;
- enet_qos_rx_bd_struct_t *rxbdPtr;
- uint32_t buffAddr;
- uint8_t channel;
- uint16_t index;
- uint16_t j;
- if ((handle->rxBuffAlloc == NULL) || (handle->rxBuffFree == NULL))
- {
- return kStatus_ENET_QOS_InitMemoryFail;
- }
- for (channel = 0; channel < handle->rxQueueUse; channel++)
- {
- /* Init the rxbdPtr to the receive descriptor start address. */
- rxbdPtr = handle->rxBdRing[channel].rxBdBase;
- for (j = 0U; j < handle->rxBdRing[channel].rxRingLen; j++)
- {
- if (handle->doubleBuffEnable)
- {
- index = 2U * j;
- }
- else
- {
- index = j;
- }
- buffAddr = (uint32_t)(uint32_t *)handle->rxBuffAlloc(base, handle->userData, channel);
- if (buffAddr == 0U)
- {
- result = kStatus_ENET_QOS_InitMemoryFail;
- break;
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buffAddr = MEMORY_ConvertMemoryMapAddress(buffAddr, kMEMORY_Local2DMA);
- #endif
- rxbdPtr->buff1Addr = buffAddr;
- handle->rxBufferStartAddr[channel][index] = buffAddr;
- /* The second buffer is set with 0 because it is not required for normal case. */
- if (handle->doubleBuffEnable)
- {
- buffAddr = (uint32_t)(uint32_t *)handle->rxBuffAlloc(base, handle->userData, channel);
- if (buffAddr == 0U)
- {
- result = kStatus_ENET_QOS_InitMemoryFail;
- break;
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buffAddr = MEMORY_ConvertMemoryMapAddress(buffAddr, kMEMORY_Local2DMA);
- #endif
- rxbdPtr->buff2Addr = buffAddr;
- handle->rxBufferStartAddr[channel][index + 1U] = buffAddr;
- }
- else
- {
- rxbdPtr->buff2Addr = 0;
- }
- /* Set the valid and DMA own flag.*/
- rxbdPtr->control |= ENET_QOS_RXDESCRIP_WR_OWN_MASK;
- rxbdPtr++;
- }
- }
- if (result == kStatus_ENET_QOS_InitMemoryFail)
- {
- ENET_QOS_RxBufferFreeAll(base, handle);
- }
- return result;
- }
- /*!
- * brief Frees Rx buffers in all BDs.
- * It's used for zero copy Rx. In zero copy Rx case, Rx buffers are dynamic. This function
- * will free left buffers in all BDs.
- *
- * param base ENET_QOS peripheral base address.
- * param handle The ENET_QOS handler structure. This is the same handler pointer used in the ENET_QOS_Init.
- */
- void ENET_QOS_RxBufferFreeAll(ENET_QOS_Type *base, enet_qos_handle_t *handle)
- {
- uint32_t buffAddr;
- uint8_t channel;
- uint16_t index;
- uint16_t j;
- if (handle->rxBuffFree != NULL)
- {
- for (channel = 0; channel < handle->rxQueueUse; channel++)
- {
- for (j = 0U; j < handle->rxBdRing[channel].rxRingLen; j++)
- {
- if (handle->doubleBuffEnable)
- {
- index = 2U * j;
- }
- else
- {
- index = j;
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buffAddr = MEMORY_ConvertMemoryMapAddress((uint32_t)handle->rxBufferStartAddr[channel][index],
- kMEMORY_DMA2Local);
- #else
- buffAddr = (uint32_t)handle->rxBufferStartAddr[channel][index];
- #endif
- if (buffAddr != 0U)
- {
- handle->rxBuffFree(base, (void *)(uint32_t *)buffAddr, handle->userData, channel);
- }
- /* The second buffer is set with 0 because it is not required for normal case. */
- if (handle->doubleBuffEnable)
- {
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buffAddr = MEMORY_ConvertMemoryMapAddress((uint32_t)handle->rxBufferStartAddr[channel][index + 1],
- kMEMORY_DMA2Local);
- #else
- buffAddr = (uint32_t)handle->rxBufferStartAddr[channel][index + 1U];
- #endif
- if (buffAddr != 0U)
- {
- handle->rxBuffFree(base, (void *)(uint32_t *)buffAddr, handle->userData, channel);
- }
- }
- }
- }
- }
- }
- /*!
- * brief Starts the ENET rx/tx.
- * This function enable the tx/rx and starts the rx/tx DMA.
- * This shall be set after ENET initialization and before
- * starting to receive the data.
- *
- * param base ENET peripheral base address.
- * param rxRingNum The number of the used rx rings. It shall not be
- * larger than the ENET_QOS_RING_NUM_MAX(2). If the ringNum is set with
- * 1, the ring 0 will be used.
- * param txRingNum The number of the used tx rings. It shall not be
- * larger than the ENET_QOS_RING_NUM_MAX(2). If the ringNum is set with
- * 1, the ring 0 will be used.
- *
- * note This must be called after all the ENET initilization.
- * And should be called when the ENET receive/transmit is required.
- */
- void ENET_QOS_StartRxTx(ENET_QOS_Type *base, uint8_t txRingNum, uint8_t rxRingNum)
- {
- assert(txRingNum != 0U);
- assert(rxRingNum != 0U);
- uint8_t index;
- if (txRingNum > ENET_QOS_RING_NUM_MAX)
- {
- txRingNum = ENET_QOS_RING_NUM_MAX;
- }
- if (rxRingNum > ENET_QOS_RING_NUM_MAX)
- {
- rxRingNum = ENET_QOS_RING_NUM_MAX;
- }
- /* Start/Acive the DMA first. */
- for (index = 0; index < rxRingNum; index++)
- {
- base->DMA_CH[index].DMA_CHX_RX_CTRL |= ENET_QOS_DMA_CHX_RX_CTRL_SR_MASK;
- }
- for (index = 0; index < txRingNum; index++)
- {
- base->DMA_CH[index].DMA_CHX_TX_CTRL |= ENET_QOS_DMA_CHX_TX_CTRL_ST_MASK;
- }
- /* Enable the RX and TX at same time. */
- base->MAC_CONFIGURATION |= (ENET_QOS_MAC_CONFIGURATION_TE_MASK | ENET_QOS_MAC_CONFIGURATION_RE_MASK);
- }
- /*!
- * brief Enables the ENET DMA and MAC interrupts.
- *
- * This function enables the ENET interrupt according to the provided mask. The mask
- * is a logical OR of enet_qos_dma_interrupt_enable_t and enet_qos_mac_interrupt_enable_t.
- * For example, to enable the dma and mac interrupt, do the following.
- * code
- * ENET_QOS_EnableInterrupts(ENET, kENET_QOS_DmaRx | kENET_QOS_DmaTx | kENET_QOS_MacPmt);
- * endcode
- *
- * param base ENET peripheral base address.
- * param mask ENET interrupts to enable. This is a logical OR of both
- * enumeration :: enet_qos_dma_interrupt_enable_t and enet_qos_mac_interrupt_enable_t.
- */
- void ENET_QOS_EnableInterrupts(ENET_QOS_Type *base, uint32_t mask)
- {
- uint32_t interrupt = mask & 0xFFFFU;
- uint8_t index;
- /* For dma interrupt. */
- if (interrupt != 0U)
- {
- for (index = 0; index < ENET_QOS_RING_NUM_MAX; index++)
- {
- /* Set for all abnormal interrupts. */
- if ((ENET_QOS_ABNORM_INT_MASK & interrupt) != 0U)
- {
- interrupt |= ENET_QOS_DMA_CHX_INT_EN_AIE_MASK;
- }
- /* Set for all normal interrupts. */
- if ((ENET_QOS_NORM_INT_MASK & interrupt) != 0U)
- {
- interrupt |= ENET_QOS_DMA_CHX_INT_EN_NIE_MASK;
- }
- base->DMA_CH[index].DMA_CHX_INT_EN = interrupt;
- }
- }
- interrupt = mask >> ENET_QOS_MACINT_ENUM_OFFSET;
- if (interrupt != 0U)
- {
- /* MAC interrupt */
- base->MAC_INTERRUPT_ENABLE |= interrupt;
- }
- }
- /*!
- * brief Clears the ENET mac interrupt events status flag.
- *
- * This function clears enabled ENET interrupts according to the provided mask. The mask
- * is a logical OR of enumeration members. See the ref enet_qos_mac_interrupt_enable_t.
- * For example, to clear the TX frame interrupt and RX frame interrupt, do the following.
- * code
- * ENET_QOS_ClearMacInterruptStatus(ENET, kENET_QOS_MacPmt);
- * endcode
- *
- * param base ENET peripheral base address.
- * param mask ENET interrupt source to be cleared.
- * This is the logical OR of members of the enumeration :: enet_qos_mac_interrupt_enable_t.
- */
- void ENET_QOS_ClearMacInterruptStatus(ENET_QOS_Type *base, uint32_t mask)
- {
- volatile uint32_t dummy;
- if ((mask & (uint32_t)kENET_QOS_MacTimestamp) != 0U)
- {
- dummy = base->MAC_TIMESTAMP_STATUS;
- }
- else if ((mask & (uint32_t)kENET_QOS_MacPmt) != 0U)
- {
- dummy = base->MAC_PMT_CONTROL_STATUS;
- }
- else
- {
- /* Add for avoid the misra 2004 rule 14.10 */
- }
- (void)dummy;
- }
- /*!
- * brief Disables the ENET DMA and MAC interrupts.
- *
- * This function disables the ENET interrupt according to the provided mask. The mask
- * is a logical OR of enet_qos_dma_interrupt_enable_t and enet_qos_mac_interrupt_enable_t.
- * For example, to disable the dma and mac interrupt, do the following.
- * code
- * ENET_QOS_DisableInterrupts(ENET, kENET_QOS_DmaRx | kENET_QOS_DmaTx | kENET_QOS_MacPmt);
- * endcode
- *
- * param base ENET peripheral base address.
- * param mask ENET interrupts to disables. This is a logical OR of both
- * enumeration :: enet_qos_dma_interrupt_enable_t and enet_qos_mac_interrupt_enable_t.
- */
- void ENET_QOS_DisableInterrupts(ENET_QOS_Type *base, uint32_t mask)
- {
- uint32_t interrupt = mask & 0xFFFFU;
- uint8_t index;
- /* For dma interrupt. */
- if (interrupt != 0U)
- {
- for (index = 0; index < ENET_QOS_RING_NUM_MAX; index++)
- {
- /* Set for all abnormal interrupts. */
- if ((ENET_QOS_ABNORM_INT_MASK & interrupt) != 0U)
- {
- interrupt |= ENET_QOS_DMA_CHX_INT_EN_AIE_MASK;
- }
- /* Set for all normal interrupts. */
- if ((ENET_QOS_NORM_INT_MASK & interrupt) != 0U)
- {
- interrupt |= ENET_QOS_DMA_CHX_INT_EN_NIE_MASK;
- }
- base->DMA_CH[index].DMA_CHX_INT_EN &= ~interrupt;
- }
- }
- interrupt = mask >> ENET_QOS_MACINT_ENUM_OFFSET;
- if (interrupt != 0U)
- {
- /* MAC interrupt */
- base->MAC_INTERRUPT_ENABLE &= ~interrupt;
- }
- }
- /*!
- * @brief Set the second level IRQ handler, allow user to overwrite the default
- * second level weak IRQ handler.
- *
- * @param ISRHandler he handler to install.
- */
- void ENET_QOS_SetISRHandler(ENET_QOS_Type *base, enet_qos_isr_t ISRHandler)
- {
- /* Update IRQ entry. */
- s_enetqosIsr = ISRHandler;
- /* Enable NVIC. */
- (void)EnableIRQ(s_enetqosIrqId[ENET_QOS_GetInstance(base)]);
- }
- /*!
- * brief Create ENET Handler
- *
- * This is a transactional API and it's provided to store all datas which are needed
- * during the whole transactional process. This API should not be used when you use
- * functional APIs to do data tx/rx. This is funtion will store many data/flag for
- * transactional use, so all configure API such as ENET_QOS_Init(), ENET_QOS_DescriptorInit(),
- * ENET_QOS_EnableInterrupts() etc.
- *
- * note as our transactional transmit API use the zero-copy transmit buffer.
- * so there are two thing we emphasize here:
- * 1. tx buffer free/requeue for application should be done in the tx
- * interrupt handler. Please set callback: kENET_QOS_TxIntEvent with tx buffer free/requeue
- * process APIs.
- * 2. the tx interrupt is forced to open.
- *
- * param base ENET peripheral base address.
- * param handle ENET handler.
- * param config ENET configuration.
- * param bufferConfig ENET buffer configuration.
- * param callback The callback function.
- * param userData The application data.
- */
- void ENET_QOS_CreateHandler(ENET_QOS_Type *base,
- enet_qos_handle_t *handle,
- enet_qos_config_t *config,
- enet_qos_buffer_config_t *bufferConfig,
- enet_qos_callback_t callback,
- void *userData)
- {
- assert(config != NULL);
- assert(bufferConfig != NULL);
- assert(callback != NULL);
- uint8_t ringNum = 1;
- uint8_t count = 0;
- uint32_t rxIntEnable = 0;
- uint8_t txQueueUse = 1;
- uint8_t rxQueueUse = 1;
- enet_qos_buffer_config_t *buffConfig = bufferConfig;
- /* Store transfer parameters in handle pointer. */
- (void)memset(handle, 0, sizeof(enet_qos_handle_t));
- if (config->multiqueueCfg != NULL)
- {
- txQueueUse = config->multiqueueCfg->txQueueUse;
- rxQueueUse = config->multiqueueCfg->rxQueueUse;
- ringNum = MAX(txQueueUse, rxQueueUse);
- }
- handle->txQueueUse = txQueueUse;
- handle->rxQueueUse = rxQueueUse;
- if ((config->specialControl & (uint32_t)kENET_QOS_DescDoubleBuffer) != 0U)
- {
- handle->doubleBuffEnable = true;
- }
- for (count = 0; count < ringNum; count++)
- {
- if (count < txQueueUse)
- {
- handle->txBdRing[count].txBdBase = buffConfig->txDescStartAddrAlign;
- handle->txBdRing[count].txRingLen = buffConfig->txRingLen;
- handle->txBdRing[count].txGenIdx = 0;
- handle->txBdRing[count].txConsumIdx = 0;
- handle->txBdRing[count].txDescUsed = 0;
- handle->txDirtyRing[count].txDirtyBase = buffConfig->txDirtyStartAddr;
- handle->txDirtyRing[count].txRingLen = buffConfig->txRingLen;
- handle->txDirtyRing[count].txGenIdx = 0;
- handle->txDirtyRing[count].txConsumIdx = 0;
- /* Enable tx interrupt for use transactional API to do tx buffer free/requeue. */
- base->DMA_CH[count].DMA_CHX_INT_EN |= ENET_QOS_DMA_CHX_INT_EN_TIE_MASK | ENET_QOS_DMA_CHX_INT_EN_NIE_MASK;
- }
- if (count < rxQueueUse)
- {
- handle->rxBdRing[count].rxBdBase = buffConfig->rxDescStartAddrAlign;
- handle->rxBdRing[count].rxGenIdx = 0;
- handle->rxBdRing[count].rxRingLen = buffConfig->rxRingLen;
- handle->rxBdRing[count].rxBuffSizeAlign = buffConfig->rxBuffSizeAlign;
- /* Record rx buffer address for re-init Rx buffer descriptor */
- handle->rxBufferStartAddr[count] = buffConfig->rxBufferStartAddr;
- /* Record rx buffer need cache maintain */
- handle->rxMaintainEnable[count] = buffConfig->rxBuffNeedMaintain;
- /* Check if the rx interrrupt is enabled. */
- rxIntEnable |= (base->DMA_CH[count].DMA_CHX_INT_EN & ENET_QOS_DMA_CHX_INT_EN_RIE_MASK);
- }
- buffConfig++;
- }
- handle->rxintEnable = (rxIntEnable != 0U) ? true : false;
- /* Save the handle pointer in the global variables. */
- s_ENETHandle[ENET_QOS_GetInstance(base)] = handle;
- /* Set Rx alloc/free callback. */
- handle->rxBuffAlloc = config->rxBuffAlloc;
- handle->rxBuffFree = config->rxBuffFree;
- /* Set callback and userData. */
- handle->callback = callback;
- handle->userData = userData;
- /* Use default ENET_QOS_CommonIRQHandler as default weak IRQ handler. */
- ENET_QOS_SetISRHandler(base, ENET_QOS_CommonIRQHandler);
- }
- /*!
- * brief Gets the ENET module Mac address.
- *
- * param base ENET peripheral base address.
- * param macAddr The six-byte Mac address pointer.
- * The pointer is allocated by application and input into the API.
- */
- void ENET_QOS_GetMacAddr(ENET_QOS_Type *base, uint8_t *macAddr, uint8_t index)
- {
- assert(macAddr != NULL);
- uint32_t address = base->MAC_ADDRESS[index].LOW;
- /* Get from physical address lower register. */
- macAddr[2] = (uint8_t)(0xFFU & (address >> 24U));
- macAddr[3] = (uint8_t)(0xFFU & (address >> 16U));
- macAddr[4] = (uint8_t)(0xFFU & (address >> 8U));
- macAddr[5] = (uint8_t)(0xFFU & address);
- /* Get from physical address high register. */
- address = base->MAC_ADDRESS[index].HIGH;
- macAddr[0] = (uint8_t)(0xFFU & (address >> 8U));
- macAddr[1] = (uint8_t)(0xFFU & address);
- }
- /*!
- * brief Adds the ENET_QOS device to a multicast group.
- *
- * param base ENET_QOS peripheral base address.
- * param address The six-byte multicast group address which is provided by application.
- */
- void ENET_QOS_AddMulticastGroup(ENET_QOS_Type *base, uint8_t *address)
- {
- assert(address != NULL);
- enet_qos_handle_t *handle = s_ENETHandle[ENET_QOS_GetInstance(base)];
- uint32_t crc = 0xFFFFFFFFU;
- uint32_t count1 = 0;
- uint32_t count2 = 0;
- /* Calculates the CRC-32 polynomial on the multicast group address. */
- for (count1 = 0; count1 < 6U; count1++)
- {
- uint8_t c = address[count1];
- for (count2 = 0; count2 < 0x08U; count2++)
- {
- if (((c ^ crc) & 1U) != 0U)
- {
- crc >>= 1U;
- c >>= 1U;
- crc ^= 0xEDB88320U;
- }
- else
- {
- crc >>= 1U;
- c >>= 1U;
- }
- }
- }
- /* Calculate bitwise reverse value. */
- crc = ENET_QOS_ReverseBits(~crc);
- /* Get highest 6 bits*/
- crc = crc >> 26U;
- handle->multicastCount[crc]++;
- if (0U != (crc & 0x20U))
- {
- base->MAC_HASH_TABLE_REG1 |= (1UL << (crc & 0x1FU));
- }
- else
- {
- base->MAC_HASH_TABLE_REG0 |= (1UL << (crc & 0x1FU));
- }
- }
- /*!
- * brief Moves the ENET_QOS device from a multicast group.
- *
- * param base ENET_QOS peripheral base address.
- * param address The six-byte multicast group address which is provided by application.
- */
- void ENET_QOS_LeaveMulticastGroup(ENET_QOS_Type *base, uint8_t *address)
- {
- assert(address != NULL);
- enet_qos_handle_t *handle = s_ENETHandle[ENET_QOS_GetInstance(base)];
- uint32_t crc = 0xFFFFFFFFU;
- uint32_t count1 = 0;
- uint32_t count2 = 0;
- /* Calculates the CRC-32 polynomial on the multicast group address. */
- for (count1 = 0; count1 < 6U; count1++)
- {
- uint8_t c = address[count1];
- for (count2 = 0; count2 < 0x08U; count2++)
- {
- if (((c ^ crc) & 1U) != 0U)
- {
- crc >>= 1U;
- c >>= 1U;
- crc ^= 0xEDB88320U;
- }
- else
- {
- crc >>= 1U;
- c >>= 1U;
- }
- }
- }
- /* Calculate bitwise reverse value. */
- crc = ENET_QOS_ReverseBits(~crc);
- /* Get highest 6 bits*/
- crc = crc >> 26U;
- handle->multicastCount[crc]--;
- /* Set the hash table if no collisions */
- if (0U == handle->multicastCount[crc])
- {
- if (0U != (crc & 0x20U))
- {
- base->MAC_HASH_TABLE_REG1 &= ~((1UL << (crc & 0x1FU)));
- }
- else
- {
- base->MAC_HASH_TABLE_REG0 &= ~((1UL << (crc & 0x1FU)));
- }
- }
- }
- /*!
- * brief Sets the ENET SMI(serial management interface)- MII management interface.
- *
- * param base ENET peripheral base address.
- */
- void ENET_QOS_SetSMI(ENET_QOS_Type *base, uint32_t csrClock_Hz)
- {
- uint32_t crDiv = 0;
- uint32_t srcClock_Hz = csrClock_Hz / 1000000U;
- assert((srcClock_Hz >= 20U) && (srcClock_Hz < 800U));
- if (srcClock_Hz < 35U)
- {
- crDiv = 2;
- }
- else if (srcClock_Hz < 60U)
- {
- crDiv = 3;
- }
- else if (srcClock_Hz < 100U)
- {
- crDiv = 0;
- }
- else if (srcClock_Hz < 150U)
- {
- crDiv = 1;
- }
- else if (srcClock_Hz < 250U)
- {
- crDiv = 4;
- }
- else if (srcClock_Hz < 300U)
- {
- crDiv = 5;
- }
- else if (srcClock_Hz < 500U)
- {
- crDiv = 6;
- }
- else if (srcClock_Hz < 800U)
- {
- crDiv = 7;
- }
- else
- {
- /* Empty else */
- }
- base->MAC_MDIO_ADDRESS = ENET_QOS_MAC_MDIO_ADDRESS_CR(crDiv);
- }
- /*!
- * brief Starts a SMI write command.
- * It supports MDIO IEEE802.3 Clause 22.
- * After send command, user needs to check whether the transmission is over
- * with ENET_QOS_IsSMIBusy().
- *
- * param base ENET peripheral base address.
- * param phyAddr The PHY address.
- * param phyReg The PHY register.
- * param data The data written to PHY.
- */
- void ENET_QOS_StartSMIWrite(ENET_QOS_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t data)
- {
- uint32_t reg = base->MAC_MDIO_ADDRESS & ENET_QOS_MAC_MDIO_ADDRESS_CR_MASK;
- /* Build MII write command. */
- base->MAC_MDIO_ADDRESS = reg | (uint32_t)kENET_QOS_MiiWriteFrame | ENET_QOS_MAC_MDIO_ADDRESS_PA(phyAddr) |
- ENET_QOS_MAC_MDIO_ADDRESS_RDA(phyReg);
- base->MAC_MDIO_DATA = data;
- base->MAC_MDIO_ADDRESS |= ENET_QOS_MAC_MDIO_ADDRESS_GB_MASK;
- }
- /*!
- * brief Starts an SMI read command.
- * It supports MDIO IEEE802.3 Clause 22.
- * After send command, user needs to check whether the transmission is over
- * with ENET_QOS_IsSMIBusy().
- *
- * param base ENET peripheral base address.
- * param phyAddr The PHY address.
- * param phyReg The PHY register.
- */
- void ENET_QOS_StartSMIRead(ENET_QOS_Type *base, uint32_t phyAddr, uint32_t phyReg)
- {
- uint32_t reg = base->MAC_MDIO_ADDRESS & ENET_QOS_MAC_MDIO_ADDRESS_CR_MASK;
- /* Build MII read command. */
- base->MAC_MDIO_ADDRESS = reg | (uint32_t)kENET_QOS_MiiReadFrame | ENET_QOS_MAC_MDIO_ADDRESS_PA(phyAddr) |
- ENET_QOS_MAC_MDIO_ADDRESS_RDA(phyReg);
- base->MAC_MDIO_ADDRESS |= ENET_QOS_MAC_MDIO_ADDRESS_GB_MASK;
- }
- /*!
- * brief Starts a SMI write command.
- * It supports MDIO IEEE802.3 Clause 45.
- * After send command, user needs to check whether the transmission is over
- * with ENET_QOS_IsSMIBusy().
- *
- * param base ENET peripheral base address.
- * param phyAddr The PHY address.
- * param device The PHY device type.
- * param phyReg The PHY register address.
- * param data The data written to PHY.
- */
- void ENET_QOS_StartExtC45SMIWrite(
- ENET_QOS_Type *base, uint32_t phyAddr, uint32_t device, uint32_t phyReg, uint32_t data)
- {
- uint32_t reg = base->MAC_MDIO_ADDRESS & ENET_QOS_MAC_MDIO_ADDRESS_CR_MASK;
- /* Build MII write command. */
- base->MAC_MDIO_ADDRESS = reg | ENET_QOS_MAC_MDIO_ADDRESS_C45E_MASK | (uint32_t)kENET_QOS_MiiWriteFrame |
- ENET_QOS_MAC_MDIO_ADDRESS_PA(phyAddr) | ENET_QOS_MAC_MDIO_ADDRESS_RDA(device);
- base->MAC_MDIO_DATA = data | ENET_QOS_MAC_MDIO_DATA_RA(phyReg);
- base->MAC_MDIO_ADDRESS |= ENET_QOS_MAC_MDIO_ADDRESS_GB_MASK;
- }
- /*!
- * brief Starts a SMI write command.
- * It supports MDIO IEEE802.3 Clause 45.
- * After send command, user needs to check whether the transmission is over
- * with ENET_QOS_IsSMIBusy().
- *
- * param base ENET peripheral base address.
- * param phyAddr The PHY address.
- * param device The PHY device type.
- * param phyReg The PHY register address.
- */
- void ENET_QOS_StartExtC45SMIRead(ENET_QOS_Type *base, uint32_t phyAddr, uint32_t device, uint32_t phyReg)
- {
- uint32_t reg = base->MAC_MDIO_ADDRESS & ENET_QOS_MAC_MDIO_ADDRESS_CR_MASK;
- /* Build MII read command. */
- base->MAC_MDIO_ADDRESS = reg | ENET_QOS_MAC_MDIO_ADDRESS_C45E_MASK | (uint32_t)kENET_QOS_MiiReadFrame |
- ENET_QOS_MAC_MDIO_ADDRESS_PA(phyAddr) | ENET_QOS_MAC_MDIO_ADDRESS_RDA(device);
- base->MAC_MDIO_DATA = ENET_QOS_MAC_MDIO_DATA_RA(phyReg);
- base->MAC_MDIO_ADDRESS |= ENET_QOS_MAC_MDIO_ADDRESS_GB_MASK;
- }
- /*!
- * brief Set the MAC to enter into power down mode.
- * the remote power wake up frame and magic frame can wake up
- * the ENET from the power down mode.
- *
- * param base ENET peripheral base address.
- * param wakeFilter The wakeFilter provided to configure the wake up frame fitlter.
- * Set the wakeFilter to NULL is not required. But if you have the filter requirement,
- * please make sure the wakeFilter pointer shall be eight continous
- * 32-bits configuration.
- */
- void ENET_QOS_EnterPowerDown(ENET_QOS_Type *base, uint32_t *wakeFilter)
- {
- uint8_t index;
- uint32_t *reg = wakeFilter;
- /* Disable the tx dma. */
- base->DMA_CH[0].DMA_CHX_TX_CTRL &= ~ENET_QOS_DMA_CHX_TX_CTRL_ST_MASK;
- base->DMA_CH[1].DMA_CHX_TX_CTRL &= ~ENET_QOS_DMA_CHX_TX_CTRL_ST_MASK;
- /* Disable the mac tx/rx. */
- base->MAC_CONFIGURATION &= ~(ENET_QOS_MAC_CONFIGURATION_RE_MASK | ENET_QOS_MAC_CONFIGURATION_TE_MASK);
- /* Enable the remote wakeup packet and enable the power down mode. */
- if (wakeFilter != NULL)
- {
- for (index = 0; index < ENET_QOS_WAKEUPFILTER_NUM; index++)
- {
- base->MAC_RWK_PACKET_FILTER = *reg;
- reg++;
- }
- }
- base->MAC_PMT_CONTROL_STATUS = ENET_QOS_MAC_PMT_CONTROL_STATUS_MGKPKTEN_MASK |
- ENET_QOS_MAC_PMT_CONTROL_STATUS_RWKPKTEN_MASK |
- ENET_QOS_MAC_PMT_CONTROL_STATUS_PWRDWN_MASK;
- /* Enable the MAC rx. */
- base->MAC_CONFIGURATION |= ENET_QOS_MAC_CONFIGURATION_RE_MASK;
- }
- /*!
- * brief Enable/Disable Rx parser, please notice that for enable/disable Rx Parser,
- * should better disable Receive first.
- *
- * param base ENET_QOS peripheral base address.
- * param enable Enable/Disable Rx parser function
- */
- status_t ENET_QOS_EnableRxParser(ENET_QOS_Type *base, bool enable)
- {
- status_t result = kStatus_Success;
- if (enable)
- {
- base->MTL_OPERATION_MODE |= ENET_QOS_MTL_OPERATION_MODE_FRPE_MASK;
- }
- else
- {
- base->MTL_OPERATION_MODE &= ~ENET_QOS_MTL_OPERATION_MODE_FRPE_MASK;
- result = ENET_QOS_PollStatusFlag(&(base->MTL_RXP_CONTROL_STATUS), ENET_QOS_MTL_RXP_CONTROL_STATUS_RXPI_MASK,
- ENET_QOS_MTL_RXP_CONTROL_STATUS_RXPI_MASK);
- }
- return result;
- }
- /*!
- * brief Gets the size of the read frame.
- * This function gets a received frame size from the ENET buffer descriptors.
- * note The FCS of the frame is automatically removed by MAC and the size is the length without the FCS.
- * After calling ENET_QOS_GetRxFrameSize, ENET_QOS_ReadFrame() should be called to update the
- * receive buffers If the result is not "kStatus_ENET_QOS_RxFrameEmpty".
- *
- * param handle The ENET handler structure. This is the same handler pointer used in the ENET_QOS_Init.
- * param length The length of the valid frame received.
- * param channel The DMAC channel for the rx.
- * retval kStatus_ENET_QOS_RxFrameEmpty No frame received. Should not call ENET_QOS_ReadFrame to read frame.
- * retval kStatus_ENET_QOS_RxFrameError Data error happens. ENET_QOS_ReadFrame should be called with NULL data
- * and NULL length to update the receive buffers.
- * retval kStatus_Success Receive a frame Successfully then the ENET_QOS_ReadFrame
- * should be called with the right data buffer and the captured data length input.
- */
- status_t ENET_QOS_GetRxFrameSize(ENET_QOS_Type *base, enet_qos_handle_t *handle, uint32_t *length, uint8_t channel)
- {
- assert(handle != NULL);
- assert(length != NULL);
- enet_qos_rx_bd_ring_t *rxBdRing = (enet_qos_rx_bd_ring_t *)&handle->rxBdRing[channel];
- enet_qos_rx_bd_struct_t *rxDesc = &rxBdRing->rxBdBase[rxBdRing->rxGenIdx];
- uint16_t index = rxBdRing->rxGenIdx;
- uint32_t control = rxDesc->control;
- /* Reset the length to zero. */
- *length = 0;
- if ((control & ENET_QOS_RXDESCRIP_WR_OWN_MASK) != 0U)
- {
- return kStatus_ENET_QOS_RxFrameEmpty;
- }
- else
- {
- do
- {
- /* Application owns the buffer descriptor, get the length. */
- if ((control & ENET_QOS_RXDESCRIP_WR_LD_MASK) != 0U)
- {
- if ((control & ENET_QOS_RXDESCRIP_WR_ERRSUM_MASK) != 0U)
- {
- return kStatus_ENET_QOS_RxFrameError;
- }
- *length = (control & ENET_QOS_RXDESCRIP_WR_PACKETLEN_MASK) - ENET_QOS_FCS_LEN;
- return kStatus_Success;
- }
- index = ENET_QOS_IncreaseIndex(index, rxBdRing->rxRingLen);
- rxDesc = &rxBdRing->rxBdBase[index];
- control = rxDesc->control;
- } while (index != rxBdRing->rxGenIdx);
- return kStatus_ENET_QOS_RxFrameError;
- }
- }
- static void ENET_QOS_DropFrame(ENET_QOS_Type *base, enet_qos_handle_t *handle, uint8_t channel)
- {
- enet_qos_rx_bd_ring_t *rxBdRing = (enet_qos_rx_bd_ring_t *)&handle->rxBdRing[channel];
- enet_qos_rx_bd_struct_t *rxDesc;
- uint16_t index = rxBdRing->rxGenIdx;
- bool tsAvailable = false;
- uint32_t buff1Addr = 0;
- uint32_t buff2Addr = 0;
- /* Not check DMA ownership here, assume there's at least one valid frame left in BD ring */
- do
- {
- /* Get the control flag. */
- rxDesc = &rxBdRing->rxBdBase[rxBdRing->rxGenIdx];
- if (!handle->doubleBuffEnable)
- {
- buff1Addr = handle->rxBufferStartAddr[channel][rxBdRing->rxGenIdx];
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, NULL, handle->rxintEnable,
- handle->doubleBuffEnable);
- }
- else
- {
- buff1Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx];
- buff2Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx + 1U];
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, (void *)(uint8_t *)buff2Addr,
- handle->rxintEnable, handle->doubleBuffEnable);
- }
- rxBdRing->rxGenIdx = ENET_QOS_IncreaseIndex(rxBdRing->rxGenIdx, rxBdRing->rxRingLen);
- /* Find the last buffer descriptor for the frame. */
- if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_LD_MASK) != 0U)
- {
- if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_RS1V_MASK) != 0U)
- {
- if ((rxDesc->reserved & ENET_QOS_RXDESCRIP_WR_PTPTSA_MASK) != 0U)
- {
- tsAvailable = true;
- }
- }
- /* Reinit for the context descriptor which has been updated by DMA. */
- rxDesc = &rxBdRing->rxBdBase[rxBdRing->rxGenIdx];
- if (tsAvailable && ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_CTXT_MASK) != 0U))
- {
- if (!handle->doubleBuffEnable)
- {
- buff1Addr = handle->rxBufferStartAddr[channel][rxBdRing->rxGenIdx];
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, NULL, handle->rxintEnable,
- handle->doubleBuffEnable);
- }
- else
- {
- buff1Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx];
- buff2Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx + 1U];
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, (void *)(uint8_t *)buff2Addr,
- handle->rxintEnable, handle->doubleBuffEnable);
- }
- rxBdRing->rxGenIdx = ENET_QOS_IncreaseIndex(rxBdRing->rxGenIdx, rxBdRing->rxRingLen);
- }
- break;
- }
- } while (rxBdRing->rxGenIdx != index);
- /* Always try to start receive, in case it had stopped */
- base->DMA_CH[channel].DMA_CHX_RXDESC_TAIL_PTR = (uint32_t)(uint8_t *)&rxBdRing->rxBdBase[rxBdRing->rxRingLen];
- }
- /*!
- * brief Reads a frame from the ENET device.
- * This function reads a frame from the ENET DMA descriptors.
- * The ENET_QOS_GetRxFrameSize should be used to get the size of the prepared data buffer.
- * For example use rx dma channel 0:
- * code
- * uint32_t length;
- * enet_qos_handle_t g_handle;
- * enet_qos_ptp_time_t ts;
- * status = ENET_QOS_GetRxFrameSize(&g_handle, &length, 0);
- * if (length != 0)
- * {
- * uint8_t *data = memory allocate interface;
- * if (!data)
- * {
- * ENET_QOS_ReadFrame(ENET, &g_handle, NULL, 0, 0, &ts);
- * }
- * else
- * {
- * status = ENET_QOS_ReadFrame(ENET, &g_handle, data, length, 0, &ts);
- * }
- * }
- * else if (status == kStatus_ENET_QOS_RxFrameError)
- * {
- * ENET_QOS_ReadFrame(ENET, &g_handle, NULL, 0, 0, &ts);
- * }
- * endcode
- * param base ENET peripheral base address.
- * param handle The ENET handler structure. This is the same handler pointer used in the ENET_QOS_Init.
- * param data The data buffer provided by user to store the frame which memory size should be at least "length".
- * param length The size of the data buffer which is still the length of the received frame.
- * param channel The rx DMA channel. shall not be larger than 2.
- * return The execute status, successful or failure.
- */
- status_t ENET_QOS_ReadFrame(ENET_QOS_Type *base,
- enet_qos_handle_t *handle,
- uint8_t *data,
- uint32_t length,
- uint8_t channel,
- enet_qos_ptp_time_t *ts)
- {
- assert(handle != NULL);
- assert(channel < handle->rxQueueUse);
- uint32_t len = 0;
- uint32_t offset = 0;
- uint32_t control;
- bool isLastBuff = false;
- enet_qos_rx_bd_ring_t *rxBdRing = (enet_qos_rx_bd_ring_t *)&handle->rxBdRing[channel];
- enet_qos_rx_bd_struct_t *rxDesc;
- status_t result = kStatus_Fail;
- uint32_t buff1Addr = 0; /*!< Buffer 1 address */
- uint32_t buff2Addr = 0; /*!< Buffer 2 or next descriptor address */
- bool tsAvailable = false;
- /* For data-NULL input, only update the buffer descriptor. */
- if (data == NULL)
- {
- ENET_QOS_DropFrame(base, handle, channel);
- result = kStatus_Success;
- }
- else
- {
- while ((!isLastBuff))
- {
- /* The last buffer descriptor of a frame. */
- rxDesc = &rxBdRing->rxBdBase[rxBdRing->rxGenIdx];
- control = rxDesc->control;
- if (!handle->doubleBuffEnable)
- {
- buff1Addr = handle->rxBufferStartAddr[channel][rxBdRing->rxGenIdx];
- if (handle->rxMaintainEnable[channel])
- {
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- /* Add the cache invalidate maintain. */
- DCACHE_InvalidateByRange(MEMORY_ConvertMemoryMapAddress(buff1Addr, kMEMORY_DMA2Local),
- rxBdRing->rxBuffSizeAlign);
- #else
- /* Add the cache invalidate maintain. */
- DCACHE_InvalidateByRange(buff1Addr, rxBdRing->rxBuffSizeAlign);
- #endif
- }
- }
- else
- {
- buff1Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx];
- buff2Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx + 1U];
- if (handle->rxMaintainEnable[channel])
- {
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- /* Add the cache invalidate maintain. */
- DCACHE_InvalidateByRange(MEMORY_ConvertMemoryMapAddress(buff1Addr, kMEMORY_DMA2Local),
- rxBdRing->rxBuffSizeAlign);
- /* Add the cache invalidate maintain. */
- DCACHE_InvalidateByRange(MEMORY_ConvertMemoryMapAddress(buff2Addr, kMEMORY_DMA2Local),
- rxBdRing->rxBuffSizeAlign);
- #else
- /* Add the cache invalidate maintain. */
- DCACHE_InvalidateByRange(buff1Addr, rxBdRing->rxBuffSizeAlign);
- /* Add the cache invalidate maintain. */
- DCACHE_InvalidateByRange(buff2Addr, rxBdRing->rxBuffSizeAlign);
- #endif
- }
- }
- rxBdRing->rxGenIdx = ENET_QOS_IncreaseIndex(rxBdRing->rxGenIdx, rxBdRing->rxRingLen);
- if ((control & ENET_QOS_RXDESCRIP_WR_LD_MASK) != 0U)
- {
- /* This is a valid frame. */
- isLastBuff = true;
- /* Remove FCS */
- len = (control & ENET_QOS_RXDESCRIP_WR_PACKETLEN_MASK) - ENET_QOS_FCS_LEN;
- if (length == len)
- {
- /* Copy the frame to user's buffer. */
- len -= offset;
- if (len > rxBdRing->rxBuffSizeAlign)
- {
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- (void)memcpy((void *)&data[offset],
- (void *)(uint8_t *)MEMORY_ConvertMemoryMapAddress(buff1Addr, kMEMORY_DMA2Local),
- rxBdRing->rxBuffSizeAlign);
- #else
- (void)memcpy((void *)&data[offset], (void *)(uint8_t *)buff1Addr, rxBdRing->rxBuffSizeAlign);
- #endif
- offset += rxBdRing->rxBuffSizeAlign;
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- (void)memcpy((void *)&data[offset],
- (void *)(uint8_t *)MEMORY_ConvertMemoryMapAddress(buff2Addr, kMEMORY_DMA2Local),
- len - rxBdRing->rxBuffSizeAlign);
- #else
- (void)memcpy((void *)&data[offset], (void *)(uint8_t *)buff2Addr,
- len - rxBdRing->rxBuffSizeAlign);
- #endif
- }
- else
- {
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- (void)memcpy((void *)&data[offset],
- (void *)(uint8_t *)MEMORY_ConvertMemoryMapAddress(buff1Addr, kMEMORY_DMA2Local),
- len);
- #else
- (void)memcpy((void *)&data[offset], (void *)(uint8_t *)buff1Addr, len);
- #endif
- }
- result = kStatus_Success;
- }
- if ((rxDesc->reserved & ENET_QOS_RXDESCRIP_WR_PTPTSA_MASK) != 0U)
- {
- tsAvailable = true;
- }
- /* Updates the receive buffer descriptors. */
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, (void *)(uint8_t *)buff2Addr,
- handle->rxintEnable, handle->doubleBuffEnable);
- /* Store the rx timestamp which is in the next buffer descriptor of the last
- * descriptor of a frame. */
- rxDesc = &rxBdRing->rxBdBase[rxBdRing->rxGenIdx];
- control = rxDesc->control;
- /* If tsAvailable is true, a context descriptor is expected but might not be yet
- * available.
- */
- if (tsAvailable)
- {
- uint8_t retryTimes = 10;
- while (((control & ENET_QOS_RXDESCRIP_WR_OWN_MASK) != 0U) ||
- ((control & ENET_QOS_RXDESCRIP_WR_CTXT_MASK) == 0U))
- {
- SDK_DelayAtLeastUs(1U, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
- if (0U == retryTimes--)
- {
- assert(false);
- }
- control = rxDesc->control;
- }
- }
- /* Reinit for the context descritor which has been updated by DMA. */
- if ((control & ENET_QOS_RXDESCRIP_WR_CTXT_MASK) != 0U)
- {
- if (tsAvailable && (NULL != ts))
- {
- ENET_QOS_StoreRxFrameTime(base, handle, rxDesc, ts);
- }
- if (!handle->doubleBuffEnable)
- {
- buff1Addr = handle->rxBufferStartAddr[channel][rxBdRing->rxGenIdx];
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, NULL, handle->rxintEnable,
- handle->doubleBuffEnable);
- }
- else
- {
- buff1Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx];
- buff2Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx + 1U];
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, (void *)(uint8_t *)buff2Addr,
- handle->rxintEnable, handle->doubleBuffEnable);
- }
- rxBdRing->rxGenIdx = ENET_QOS_IncreaseIndex(rxBdRing->rxGenIdx, rxBdRing->rxRingLen);
- }
- }
- else
- {
- /* Store a frame on several buffer descriptors. */
- isLastBuff = false;
- /* Length check. */
- if (offset >= length)
- {
- /* Updates the receive buffer descriptors. */
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, (void *)(uint8_t *)buff2Addr,
- handle->rxintEnable, handle->doubleBuffEnable);
- break;
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- (void)memcpy((void *)&data[offset],
- (void *)(uint8_t *)MEMORY_ConvertMemoryMapAddress(buff1Addr, kMEMORY_DMA2Local),
- rxBdRing->rxBuffSizeAlign);
- #else
- (void)memcpy((void *)&data[offset], (void *)(uint8_t *)buff1Addr, rxBdRing->rxBuffSizeAlign);
- #endif
- offset += rxBdRing->rxBuffSizeAlign;
- if (buff2Addr != 0U)
- {
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- (void)memcpy((void *)&data[offset],
- (void *)(uint8_t *)MEMORY_ConvertMemoryMapAddress(buff2Addr, kMEMORY_DMA2Local),
- rxBdRing->rxBuffSizeAlign);
- #else
- (void)memcpy((void *)&data[offset], (void *)(uint8_t *)buff2Addr, rxBdRing->rxBuffSizeAlign);
- #endif
- offset += rxBdRing->rxBuffSizeAlign;
- }
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, (void *)(uint8_t *)buff2Addr,
- handle->rxintEnable, handle->doubleBuffEnable);
- }
- }
- /* Always try to start receive, in case it had stopped */
- base->DMA_CH[channel].DMA_CHX_RXDESC_TAIL_PTR = (uint32_t)(uint8_t *)&rxBdRing->rxBdBase[rxBdRing->rxRingLen];
- }
- return result;
- }
- /*!
- * brief Updates the buffers and the own status for a given rx descriptor.
- * This function is a low level functional API to Updates the
- * buffers and the own status for a given rx descriptor.
- *
- * param rxDesc The given rx descriptor.
- * param buffer1 The first buffer address in the descriptor.
- * param buffer2 The second buffer address in the descriptor.
- * param intEnable Interrupt enable flag.
- * param doubleBuffEnable The double buffer enable flag.
- *
- * note This must be called after all the ENET initilization.
- * And should be called when the ENET receive/transmit is required.
- */
- void ENET_QOS_UpdateRxDescriptor(
- enet_qos_rx_bd_struct_t *rxDesc, void *buffer1, void *buffer2, bool intEnable, bool doubleBuffEnable)
- {
- assert(rxDesc != NULL);
- uint32_t control = ENET_QOS_RXDESCRIP_RD_OWN_MASK | ENET_QOS_RXDESCRIP_RD_BUFF1VALID_MASK;
- if (intEnable)
- {
- control |= ENET_QOS_RXDESCRIP_RD_IOC_MASK;
- }
- if (doubleBuffEnable)
- {
- control |= ENET_QOS_RXDESCRIP_RD_BUFF2VALID_MASK;
- }
- /* Update the buffer if needed. */
- if (buffer1 != NULL)
- {
- rxDesc->buff1Addr = (uint32_t)(uint8_t *)buffer1;
- }
- if (buffer2 != NULL)
- {
- rxDesc->buff2Addr = (uint32_t)(uint8_t *)buffer2;
- }
- else
- {
- rxDesc->buff2Addr = 0;
- }
- rxDesc->reserved = 0;
- /* Add a data barrier to be sure that the address is written before the
- ownership bit status. */
- __DMB();
- rxDesc->control = control;
- }
- /*!
- * brief Setup a given tx descriptor.
- * This function is a low level functional API to setup or prepare
- * a given tx descriptor.
- *
- * param txDesc The given tx descriptor.
- * param buffer1 The first buffer address in the descriptor.
- * param bytes1 The bytes in the fist buffer.
- * param buffer2 The second buffer address in the descriptor.
- * param bytes1 The bytes in the second buffer.
- * param framelen The length of the frame to be transmitted.
- * param intEnable Interrupt enable flag.
- * param tsEnable The timestamp enable.
- * param flag The flag of this tx desciriptor, see "enet_qos_desc_flag" .
- * param slotNum The slot num used for AV only.
- *
- * note This must be called after all the ENET initilization.
- * And should be called when the ENET receive/transmit is required.
- * Transmit buffers are 'zero-copy' buffers, so the buffer must remain in
- * memory until the packet has been fully transmitted. The buffers
- * should be free or requeued in the transmit interrupt irq handler.
- */
- void ENET_QOS_SetupTxDescriptor(enet_qos_tx_bd_struct_t *txDesc,
- void *buffer1,
- uint32_t bytes1,
- void *buffer2,
- uint32_t bytes2,
- uint32_t framelen,
- bool intEnable,
- bool tsEnable,
- enet_qos_desc_flag flag,
- uint8_t slotNum)
- {
- uint32_t control = ENET_QOS_TXDESCRIP_RD_BL1(bytes1) | ENET_QOS_TXDESCRIP_RD_BL2(bytes2);
- if (tsEnable)
- {
- control |= ENET_QOS_TXDESCRIP_RD_TTSE_MASK;
- }
- else
- {
- control &= ~ENET_QOS_TXDESCRIP_RD_TTSE_MASK;
- }
- if (intEnable)
- {
- control |= ENET_QOS_TXDESCRIP_RD_IOC_MASK;
- }
- else
- {
- control &= ~ENET_QOS_TXDESCRIP_RD_IOC_MASK;
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buffer1 = (void *)(uint32_t *)MEMORY_ConvertMemoryMapAddress((uint32_t)(uint32_t *)buffer1, kMEMORY_Local2DMA);
- buffer2 = (void *)(uint32_t *)MEMORY_ConvertMemoryMapAddress((uint32_t)(uint32_t *)buffer2, kMEMORY_Local2DMA);
- #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
- /* Preare the descriptor for transmit. */
- txDesc->buff1Addr = (uint32_t)(uint8_t *)buffer1;
- txDesc->buff2Addr = (uint32_t)(uint8_t *)buffer2;
- txDesc->buffLen = control;
- /* Make sure all fields of descriptor are written before setting ownership */
- __DMB();
- control = ENET_QOS_TXDESCRIP_RD_FL(framelen) | ENET_QOS_TXDESCRIP_RD_LDFD(flag) | ENET_QOS_TXDESCRIP_RD_OWN_MASK;
- txDesc->controlStat = control;
- /* Make sure the descriptor is written in memory (before MAC starts checking it) */
- __DSB();
- }
- /*!
- * brief Reclaim tx descriptors.
- * This function is used to update the tx descriptor status and
- * store the tx timestamp when the 1588 feature is enabled.
- * This is called by the transmit interupt IRQ handler after the
- * complete of a frame transmission.
- *
- * param base ENET peripheral base address.
- * param handle The ENET handler pointer. This is the same handler pointer used in the ENET_QOS_Init.
- * param channel The tx DMA channnel.
- *
- */
- void ENET_QOS_ReclaimTxDescriptor(ENET_QOS_Type *base, enet_qos_handle_t *handle, uint8_t channel)
- {
- enet_qos_tx_bd_ring_t *txBdRing = &handle->txBdRing[channel];
- enet_qos_tx_bd_struct_t *txDesc = &txBdRing->txBdBase[txBdRing->txConsumIdx];
- enet_qos_tx_dirty_ring_t *txDirtyRing = (enet_qos_tx_dirty_ring_t *)&handle->txDirtyRing[channel];
- enet_qos_frame_info_t *txDirty = NULL;
- uint32_t control, primask;
- control = txDesc->controlStat;
- /* Need to update the first index for transmit buffer free. */
- while ((txBdRing->txDescUsed > 0U) && (0U == (control & ENET_QOS_TXDESCRIP_RD_OWN_MASK)))
- {
- if ((control & ENET_QOS_TXDESCRIP_RD_LD_MASK) != 0U)
- {
- if (ENET_QOS_TxDirtyRingAvailable(txDirtyRing))
- {
- txDirty = &txDirtyRing->txDirtyBase[txBdRing->txConsumIdx];
- txDirtyRing->txGenIdx = ENET_QOS_IncreaseIndex(txDirtyRing->txGenIdx, txDirtyRing->txRingLen);
- if (txDirtyRing->txGenIdx == txDirtyRing->txConsumIdx)
- {
- txDirtyRing->isFull = true;
- }
- if ((control & ENET_QOS_TXDESCRIP_WB_TTSS_MASK) != 0U)
- {
- enet_qos_ptp_time_t *ts = &txDirty->timeStamp;
- uint32_t nanosecond;
- /* Get transmit time stamp second. */
- nanosecond = txDesc->buff1Addr;
- txDirty->isTsAvail = true;
- if (0U == (base->MAC_TIMESTAMP_CONTROL & ENET_QOS_MAC_TIMESTAMP_CONTROL_TSCTRLSSR_MASK))
- {
- /* Binary rollover, 0.465ns accuracy. */
- nanosecond = (nanosecond * 465U) / 1000U;
- }
- ts->second = txDesc->buff2Addr;
- ts->nanosecond = nanosecond;
- }
- else
- {
- txDirty->isTsAvail = false;
- }
- }
- }
- /* For tx buffer free or requeue for each descriptor.
- * The tx interrupt callback should free/requeue the tx buffer. */
- if (handle->callback != NULL)
- {
- handle->callback(base, handle, kENET_QOS_TxIntEvent, channel, handle->userData);
- }
- primask = DisableGlobalIRQ();
- txBdRing->txDescUsed--;
- EnableGlobalIRQ(primask);
- /* Update the txConsumIdx/txDesc. */
- txBdRing->txConsumIdx = ENET_QOS_IncreaseIndex(txBdRing->txConsumIdx, txBdRing->txRingLen);
- txDesc = &txBdRing->txBdBase[txBdRing->txConsumIdx];
- control = txDesc->controlStat;
- }
- }
- /*!
- * brief Transmits an ENET frame.
- * note The CRC is automatically appended to the data. Input the data
- * to send without the CRC.
- *
- * param base ENET peripheral base address.
- * param handle The ENET handler pointer. This is the same handler pointer used in the ENET_QOS_Init.
- * param data The data buffer provided by user to be send.
- * param length The length of the data to be send.
- * param channel Channel to send the frame, same with queue index.
- * param isNeedTs True means save timestamp
- * param context pointer to user context to be kept in the tx dirty frame information.
- * retval kStatus_Success Send frame succeed.
- * retval kStatus_ENET_QOS_TxFrameBusy Transmit buffer descriptor is busy under transmission.
- * The transmit busy happens when the data send rate is over the MAC capacity.
- * The waiting mechanism is recommended to be added after each call return with
- * kStatus_ENET_QOS_TxFrameBusy.
- */
- status_t ENET_QOS_SendFrame(ENET_QOS_Type *base,
- enet_qos_handle_t *handle,
- uint8_t *data,
- uint32_t length,
- uint8_t channel,
- bool isNeedTs,
- void *context)
- {
- assert(handle != NULL);
- assert(data != NULL);
- assert(channel < handle->txQueueUse);
- enet_qos_tx_bd_ring_t *txBdRing;
- enet_qos_tx_bd_struct_t *txDesc;
- enet_qos_tx_dirty_ring_t *txDirtyRing;
- enet_qos_frame_info_t *txDirty;
- uint32_t primask;
- if (length > 2U * ENET_QOS_TXDESCRIP_RD_BL1_MASK)
- {
- return kStatus_ENET_QOS_TxFrameOverLen;
- }
- /* Check if the DMA owns the descriptor. */
- txBdRing = (enet_qos_tx_bd_ring_t *)&handle->txBdRing[channel];
- txDesc = &txBdRing->txBdBase[txBdRing->txGenIdx];
- if (txBdRing->txRingLen == txBdRing->txDescUsed)
- {
- return kStatus_ENET_QOS_TxFrameBusy;
- }
- txDirtyRing = (enet_qos_tx_dirty_ring_t *)&handle->txDirtyRing[channel];
- txDirty = &txDirtyRing->txDirtyBase[txBdRing->txGenIdx];
- txDirty->context = context;
- /* Fill the descriptor. */
- if (length <= ENET_QOS_TXDESCRIP_RD_BL1_MASK)
- {
- ENET_QOS_SetupTxDescriptor(txDesc, data, length, NULL, 0, length, true, isNeedTs, kENET_QOS_FirstLastFlag, 0);
- }
- else
- {
- ENET_QOS_SetupTxDescriptor(txDesc, data, ENET_QOS_TXDESCRIP_RD_BL1_MASK, &data[ENET_QOS_TXDESCRIP_RD_BL1_MASK],
- (length - ENET_QOS_TXDESCRIP_RD_BL1_MASK), length, true, isNeedTs,
- kENET_QOS_FirstLastFlag, 0);
- }
- /* Increase the index. */
- txBdRing->txGenIdx = ENET_QOS_IncreaseIndex(txBdRing->txGenIdx, txBdRing->txRingLen);
- /* Disable interrupt first and then enable interrupt to avoid the race condition. */
- primask = DisableGlobalIRQ();
- txBdRing->txDescUsed++;
- EnableGlobalIRQ(primask);
- /* Update the transmit tail address. */
- txDesc = &txBdRing->txBdBase[txBdRing->txGenIdx];
- if (txBdRing->txGenIdx == 0U)
- {
- txDesc = &txBdRing->txBdBase[txBdRing->txRingLen];
- }
- base->DMA_CH[channel].DMA_CHX_TXDESC_TAIL_PTR = (uint32_t)txDesc & ~ENET_QOS_ADDR_ALIGNMENT;
- return kStatus_Success;
- }
- /*!
- * brief Gets the sent frame.
- *
- * This function is used to get the sent frame for timestamp and buffer clean operation.
- *
- * param handle The ENET handler pointer.This is the same state pointer used in
- * ENET_QOS_Init.
- * param txFrame Input parameter, pointer to enet_qos_frame_info_t for saving read out frame information.
- * param channel Read out frame from specified channel.
- */
- void ENET_QOS_GetTxFrame(enet_qos_handle_t *handle, enet_qos_frame_info_t *txFrame, uint8_t channel)
- {
- assert(handle != NULL);
- assert(channel < handle->txQueueUse);
- enet_qos_tx_dirty_ring_t *txDirtyRing = (enet_qos_tx_dirty_ring_t *)&handle->txDirtyRing[channel];
- enet_qos_frame_info_t *txDirty = &txDirtyRing->txDirtyBase[txDirtyRing->txConsumIdx];
- (void)memcpy(txFrame, txDirty, sizeof(enet_qos_frame_info_t));
- txDirtyRing->isFull = false;
- txDirtyRing->txConsumIdx = ENET_QOS_IncreaseIndex(txDirtyRing->txConsumIdx, txDirtyRing->txRingLen);
- }
- static inline void ENET_QOS_GetRxFrameErr(enet_qos_rx_bd_struct_t *rxDesc, enet_qos_rx_frame_error_t *rxFrameError)
- {
- uint32_t rdes2 = rxDesc->buff2Addr;
- uint32_t rdes3 = rxDesc->control;
- (void)memset(rxFrameError, 0, sizeof(enet_qos_rx_frame_error_t));
- if ((rdes2 & ENET_QOS_RXDESCRIP_WR_SA_FAILURE_MASK) != 0U)
- {
- rxFrameError->rxSrcAddrFilterErr = true;
- }
- if ((rdes2 & ENET_QOS_RXDESCRIP_WR_DA_FAILURE_MASK) != 0U)
- {
- rxFrameError->rxDstAddrFilterErr = true;
- }
- if ((rdes3 & ENET_QOS_RXDESCRIP_WR_DE_MASK) != 0U)
- {
- rxFrameError->rxDstAddrFilterErr = true;
- }
- if ((rdes3 & ENET_QOS_RXDESCRIP_WR_RE_MASK) != 0U)
- {
- rxFrameError->rxReceiveErr = true;
- }
- if ((rdes3 & ENET_QOS_RXDESCRIP_WR_OE_MASK) != 0U)
- {
- rxFrameError->rxOverFlowErr = true;
- }
- if ((rdes3 & ENET_QOS_RXDESCRIP_WR_RWT_MASK) != 0U)
- {
- rxFrameError->rxWatchDogErr = true;
- }
- if ((rdes3 & ENET_QOS_RXDESCRIP_WR_GP_MASK) != 0U)
- {
- rxFrameError->rxGaintPacketErr = true;
- }
- if ((rdes3 & ENET_QOS_RXDESCRIP_WR_CRC_MASK) != 0U)
- {
- rxFrameError->rxCrcErr = true;
- }
- }
- /*!
- * brief Receives one frame in specified BD ring with zero copy.
- *
- * This function will use the user-defined allocate and free callback. Every time application gets one frame through
- * this function, driver will allocate new buffers for the BDs whose buffers have been taken by application.
- * note This function will drop current frame and update related BDs as available for DMA if new buffers allocating
- * fails. Application must provide a memory pool including at least BD number + 1 buffers(+2 if enable double buffer)
- * to make this function work normally. If user calls this function in Rx interrupt handler, be careful that this
- * function makes Rx BD ready with allocating new buffer(normal) or updating current BD(out of memory). If there's
- * always new Rx frame input, Rx interrupt will be triggered forever. Application need to disable Rx interrupt according
- * to specific design in this case.
- *
- * param base ENET peripheral base address.
- * param handle The ENET handler pointer. This is the same handler pointer used in the ENET_Init.
- * param rxFrame The received frame information structure provided by user.
- * param ringId The ring index or ring number.
- * retval kStatus_Success Succeed to get one frame and allocate new memory for Rx buffer.
- * retval kStatus_ENET_QOS_RxFrameEmpty There's no Rx frame in the BD.
- * retval kStatus_ENET_QOS_RxFrameError There's issue in this receiving.
- * retval kStatus_ENET_QOS_RxFrameDrop There's no new buffer memory for BD, drop this frame.
- */
- status_t ENET_QOS_GetRxFrame(ENET_QOS_Type *base,
- enet_qos_handle_t *handle,
- enet_qos_rx_frame_struct_t *rxFrame,
- uint8_t channel)
- {
- assert(handle != NULL);
- assert(channel < handle->rxQueueUse);
- enet_qos_rx_bd_ring_t *rxBdRing = (enet_qos_rx_bd_ring_t *)&handle->rxBdRing[channel];
- enet_qos_rx_bd_struct_t *rxDesc = &rxBdRing->rxBdBase[rxBdRing->rxGenIdx];
- uint16_t index = rxBdRing->rxGenIdx;
- status_t result = kStatus_Success;
- uint32_t buff1Addr = 0;
- uint32_t buff2Addr = 0;
- uint16_t buff1Len = 0;
- uint16_t buff2Len = 0;
- uint16_t offset = 0;
- void *newBuff1 = NULL;
- void *newBuff2 = NULL;
- bool isDrop = false;
- bool isLastBuff = false;
- bool tsAvailable = false;
- /* Check the frame status. */
- do
- {
- if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_OWN_MASK) != 0U)
- {
- result = kStatus_ENET_QOS_RxFrameEmpty;
- break;
- }
- /* Check timestamp and error. */
- if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_LD_MASK) != 0U)
- {
- if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_RS1V_MASK) != 0U)
- {
- if ((rxDesc->reserved & ENET_QOS_RXDESCRIP_WR_PTPTSA_MASK) != 0U)
- {
- /* Context descriptor is expected but might not be yet available. */
- uint8_t retryTimes = 10;
- while (((rxDesc->control & ENET_QOS_RXDESCRIP_WR_OWN_MASK) != 0U) ||
- ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_CTXT_MASK) == 0U))
- {
- /* Timsstamp value is not corrupted. */
- if ((rxDesc->buff1Addr != 0xFFFFFFFFU) && (rxDesc->buff2Addr != 0xFFFFFFFFU))
- {
- break;
- }
- if (retryTimes-- == 0U)
- {
- break;
- }
- }
- if (retryTimes != 0U)
- {
- tsAvailable = true;
- }
- else
- {
- result = kStatus_ENET_QOS_RxFrameEmpty;
- break;
- }
- }
- }
- /* Get the frame error if there is. */
- if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_ERRSUM_MASK) != 0U)
- {
- ENET_QOS_GetRxFrameErr(rxDesc, &rxFrame->rxFrameError);
- result = kStatus_ENET_QOS_RxFrameError;
- }
- else if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_PACKETLEN_MASK) == 0U)
- {
- result = kStatus_ENET_QOS_RxFrameEmpty;
- }
- else
- {
- /* Intentional empty */
- }
- break;
- }
- index = ENET_QOS_IncreaseIndex(index, rxBdRing->rxRingLen);
- if (index == rxBdRing->rxGenIdx)
- {
- result = kStatus_ENET_QOS_RxFrameEmpty;
- break;
- }
- rxDesc = &rxBdRing->rxBdBase[index];
- } while (index != rxBdRing->rxGenIdx);
- /* Drop the error frame and return error. */
- if (result != kStatus_Success)
- {
- if (result == kStatus_ENET_QOS_RxFrameError)
- {
- ENET_QOS_DropFrame(base, handle, channel);
- }
- return result;
- }
- /* Get the valid frame */
- index = 0;
- do
- {
- rxDesc = &rxBdRing->rxBdBase[rxBdRing->rxGenIdx];
- /* Caculate the buffer and frame length. */
- if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_LD_MASK) != 0U)
- {
- isLastBuff = true;
- rxFrame->totLen = (uint16_t)(rxDesc->control & ENET_QOS_RXDESCRIP_WR_PACKETLEN_MASK);
- if (rxFrame->totLen - offset > (uint16_t)rxBdRing->rxBuffSizeAlign)
- {
- buff1Len = (uint16_t)rxBdRing->rxBuffSizeAlign;
- if (handle->doubleBuffEnable)
- {
- buff2Len = rxFrame->totLen - offset - (uint16_t)rxBdRing->rxBuffSizeAlign - ENET_QOS_FCS_LEN;
- }
- }
- else
- {
- buff1Len = rxFrame->totLen - offset - ENET_QOS_FCS_LEN;
- }
- rxFrame->totLen -= ENET_QOS_FCS_LEN;
- }
- else
- {
- if (!handle->doubleBuffEnable)
- {
- buff1Len = (uint16_t)rxBdRing->rxBuffSizeAlign;
- offset += buff1Len;
- }
- else
- {
- buff1Len = (uint16_t)rxBdRing->rxBuffSizeAlign;
- buff2Len = (uint16_t)rxBdRing->rxBuffSizeAlign;
- offset += buff1Len + buff2Len;
- }
- }
- /* Allocate new buffer to replace the buffer taken by application */
- newBuff1 = handle->rxBuffAlloc(base, handle->userData, channel);
- if (newBuff1 == NULL)
- {
- isDrop = true;
- }
- else if (handle->doubleBuffEnable && (buff2Len != 0U))
- {
- newBuff2 = handle->rxBuffAlloc(base, handle->userData, channel);
- if (newBuff2 == NULL)
- {
- handle->rxBuffFree(base, newBuff1, handle->userData, channel);
- isDrop = true;
- }
- }
- else
- {
- /* Intentional empty */
- }
- if (!isDrop)
- {
- /* Get the frame data information into Rx frame structure. */
- if (!handle->doubleBuffEnable)
- {
- buff1Addr = handle->rxBufferStartAddr[channel][rxBdRing->rxGenIdx];
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buff1Addr = MEMORY_ConvertMemoryMapAddress(buff1Addr, kMEMORY_DMA2Local);
- #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
- if (handle->rxMaintainEnable[channel])
- {
- DCACHE_InvalidateByRange(buff1Addr, rxBdRing->rxBuffSizeAlign);
- }
- rxFrame->rxBuffArray[index].buffer = (void *)(uint32_t *)buff1Addr;
- rxFrame->rxBuffArray[index].length = buff1Len;
- index++;
- }
- else
- {
- buff1Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx];
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buff1Addr = MEMORY_ConvertMemoryMapAddress(buff1Addr, kMEMORY_DMA2Local);
- #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
- if (handle->rxMaintainEnable[channel])
- {
- DCACHE_InvalidateByRange(buff1Addr, rxBdRing->rxBuffSizeAlign);
- }
- rxFrame->rxBuffArray[index].buffer = (void *)(uint32_t *)buff1Addr;
- rxFrame->rxBuffArray[index].length = buff1Len;
- index++;
- /* If there's no data in buffer2, not add it into rxFrame */
- if (buff2Len != 0U)
- {
- buff2Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx + 1U];
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buff2Addr = MEMORY_ConvertMemoryMapAddress(buff2Addr, kMEMORY_DMA2Local);
- #endif /* FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET */
- if (handle->rxMaintainEnable[channel])
- {
- DCACHE_InvalidateByRange(buff2Addr, rxBdRing->rxBuffSizeAlign);
- }
- rxFrame->rxBuffArray[index].buffer = (void *)(uint32_t *)buff2Addr;
- rxFrame->rxBuffArray[index].length = buff2Len;
- index++;
- }
- }
- /* Give new buffer from application to BD */
- if (!handle->doubleBuffEnable)
- {
- if (handle->rxMaintainEnable[channel])
- {
- DCACHE_InvalidateByRange((uint32_t)(uint32_t *)newBuff1, rxBdRing->rxBuffSizeAlign);
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buff1Addr = MEMORY_ConvertMemoryMapAddress((uint32_t)(uint32_t *)newBuff1, kMEMORY_Local2DMA);
- #else
- buff1Addr = (uint32_t)(uint32_t *)newBuff1;
- #endif
- handle->rxBufferStartAddr[channel][rxBdRing->rxGenIdx] = buff1Addr;
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint32_t *)buff1Addr, NULL, handle->rxintEnable,
- handle->doubleBuffEnable);
- }
- else
- {
- if (handle->rxMaintainEnable[channel])
- {
- DCACHE_InvalidateByRange((uint32_t)(uint32_t *)newBuff1, rxBdRing->rxBuffSizeAlign);
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buff1Addr = MEMORY_ConvertMemoryMapAddress((uint32_t)(uint32_t *)newBuff1, kMEMORY_Local2DMA);
- #else
- buff1Addr = (uint32_t)(uint32_t *)newBuff1;
- #endif
- handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx] = buff1Addr;
- if (buff2Len != 0U)
- {
- if (handle->rxMaintainEnable[channel])
- {
- DCACHE_InvalidateByRange((uint32_t)(uint32_t *)newBuff2, rxBdRing->rxBuffSizeAlign);
- }
- #if defined(FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET) && FSL_FEATURE_MEMORY_HAS_ADDRESS_OFFSET
- buff2Addr = MEMORY_ConvertMemoryMapAddress((uint32_t)(uint32_t *)newBuff2, kMEMORY_Local2DMA);
- #else
- buff2Addr = (uint32_t)(uint32_t *)newBuff2;
- #endif
- handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx + 1U] = buff2Addr;
- }
- else
- {
- /* If there's no data in buffer2, keep it */
- buff2Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx + 1U];
- }
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint32_t *)buff1Addr, (void *)(uint32_t *)buff2Addr,
- handle->rxintEnable, handle->doubleBuffEnable);
- }
- rxBdRing->rxGenIdx = ENET_QOS_IncreaseIndex(rxBdRing->rxGenIdx, rxBdRing->rxRingLen);
- /* Update context BD if there is */
- if (isLastBuff && tsAvailable)
- {
- rxDesc = &rxBdRing->rxBdBase[rxBdRing->rxGenIdx];
- if ((rxDesc->control & ENET_QOS_RXDESCRIP_WR_CTXT_MASK) != 0U)
- {
- ENET_QOS_StoreRxFrameTime(base, handle, rxDesc, &rxFrame->rxAttribute.timestamp);
- rxFrame->rxAttribute.isTsAvail = true;
- if (!handle->doubleBuffEnable)
- {
- buff1Addr = handle->rxBufferStartAddr[channel][rxBdRing->rxGenIdx];
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, NULL, handle->rxintEnable,
- handle->doubleBuffEnable);
- }
- else
- {
- buff1Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx];
- buff2Addr = handle->rxBufferStartAddr[channel][2U * rxBdRing->rxGenIdx + 1U];
- ENET_QOS_UpdateRxDescriptor(rxDesc, (void *)(uint8_t *)buff1Addr, (void *)(uint8_t *)buff2Addr,
- handle->rxintEnable, handle->doubleBuffEnable);
- }
- rxBdRing->rxGenIdx = ENET_QOS_IncreaseIndex(rxBdRing->rxGenIdx, rxBdRing->rxRingLen);
- }
- }
- /* Always try to start receive, in case it had stopped */
- base->DMA_CH[channel].DMA_CHX_RXDESC_TAIL_PTR =
- (uint32_t)(uint8_t *)&rxBdRing->rxBdBase[rxBdRing->rxRingLen];
- }
- else
- {
- /* Drop frame if there's no new buffer memory */
- /* Free the incomplete frame buffers. */
- while (index-- != 0U)
- {
- handle->rxBuffFree(base, &rxFrame->rxBuffArray[index].buffer, handle->userData, channel);
- }
- /* Update all left BDs of this frame from current index. */
- ENET_QOS_DropFrame(base, handle, channel);
- result = kStatus_ENET_QOS_RxFrameDrop;
- break;
- }
- } while (!isLastBuff);
- return result;
- }
- /*!
- * brief Gets the current ENET time from the PTP 1588 timer without IRQ disable.
- *
- * param base ENET peripheral base address.
- * param second The PTP 1588 system timer second.
- * param nanosecond The PTP 1588 system timer nanosecond.
- * For the unit of the nanosecond is 1ns. so the nanosecond is the real nanosecond.
- */
- void ENET_QOS_Ptp1588GetTimerNoIRQDisable(ENET_QOS_Type *base, uint64_t *second, uint32_t *nanosecond)
- {
- assert(second != NULL);
- assert(nanosecond != NULL);
- uint32_t high_sec[2];
- uint32_t sec[2];
- /* Get the current PTP time. */
- /* Since register reads are not atomic, we need to check for wraps during the read */
- high_sec[1] = base->MAC_SYSTEM_TIME_HIGHER_WORD_SECONDS & ENET_QOS_MAC_SYSTEM_TIME_HIGHER_WORD_SECONDS_TSHWR_MASK;
- do
- {
- high_sec[0] = high_sec[1];
- sec[1] = base->MAC_SYSTEM_TIME_SECONDS;
- do
- {
- sec[0] = sec[1];
- *nanosecond = base->MAC_SYSTEM_TIME_NANOSECONDS & ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_TSSS_MASK;
- sec[1] = base->MAC_SYSTEM_TIME_SECONDS;
- } while (sec[1] != sec[0]);
- high_sec[1] =
- base->MAC_SYSTEM_TIME_HIGHER_WORD_SECONDS & ENET_QOS_MAC_SYSTEM_TIME_HIGHER_WORD_SECONDS_TSHWR_MASK;
- } while (high_sec[1] != high_sec[0]);
- *second = ((uint64_t)high_sec[1] << 32U) | sec[1];
- if ((base->MAC_TIMESTAMP_CONTROL & ENET_QOS_MAC_TIMESTAMP_CONTROL_TSCTRLSSR_MASK) == 0U)
- {
- /* Binary rollover, the unit of the increment is ~ 0.465 ns. */
- *nanosecond = (*nanosecond * 465U) / 1000U;
- }
- }
- /*!
- * brief Gets the current ENET time from the PTP 1588 timer, get a more accurate value
- * with IRQ disabled during get timer.
- *
- * param base ENET peripheral base address.
- * param second The PTP 1588 system timer second.
- * param nanosecond The PTP 1588 system timer nanosecond.
- * For the unit of the nanosecond is 1ns. so the nanosecond is the real nanosecond.
- */
- void ENET_QOS_Ptp1588GetTimer(ENET_QOS_Type *base, uint64_t *second, uint32_t *nanosecond)
- {
- uint32_t primask;
- /* Disables the interrupt. */
- primask = DisableGlobalIRQ();
- ENET_QOS_Ptp1588GetTimerNoIRQDisable(base, second, nanosecond);
- /* Enables the interrupt. */
- EnableGlobalIRQ(primask);
- }
- /*!
- * brief Coreect the ENET PTP 1588 timer in coarse method.
- *
- * param base ENET peripheral base address.
- * param operation The system time operation, refer to "enet_qos_systime_op"
- * param second The correction second.
- * param nanosecond The correction nanosecond.
- */
- status_t ENET_QOS_Ptp1588CorrectTimerInCoarse(ENET_QOS_Type *base,
- enet_qos_systime_op operation,
- uint32_t second,
- uint32_t nanosecond)
- {
- uint32_t corrSecond = second;
- uint32_t corrNanosecond;
- status_t result = kStatus_Success;
- /* Set the system timer. */
- if ((base->MAC_TIMESTAMP_CONTROL & ENET_QOS_MAC_TIMESTAMP_CONTROL_TSCTRLSSR_MASK) != 0U)
- {
- if (operation == kENET_QOS_SystimeSubtract)
- {
- /* Set with the complement of the sub-second. */
- corrSecond = ENET_QOS_MAC_SYSTEM_TIME_SECONDS_UPDATE_TSS_MASK - (second - 1U);
- corrNanosecond = ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_UPDATE_ADDSUB_MASK |
- ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_UPDATE_TSSS(ENET_QOS_NANOSECS_ONESECOND - nanosecond);
- }
- else
- {
- corrNanosecond = ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_UPDATE_TSSS(nanosecond);
- }
- }
- else
- {
- nanosecond = ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_UPDATE_TSSS_MASK / ENET_QOS_NANOSECS_ONESECOND * nanosecond;
- if (operation == kENET_QOS_SystimeSubtract)
- {
- /* Set with the complement of the sub-second. */
- corrSecond = ENET_QOS_MAC_SYSTEM_TIME_SECONDS_UPDATE_TSS_MASK - (second - 1U);
- corrNanosecond = ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_UPDATE_ADDSUB_MASK |
- ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_UPDATE_TSSS(
- ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_UPDATE_TSSS_MASK + 1U - nanosecond);
- }
- else
- {
- corrNanosecond = ENET_QOS_MAC_SYSTEM_TIME_NANOSECONDS_UPDATE_TSSS(nanosecond);
- }
- }
- base->MAC_SYSTEM_TIME_SECONDS_UPDATE = corrSecond;
- base->MAC_SYSTEM_TIME_NANOSECONDS_UPDATE = corrNanosecond;
- /* Update the timer. */
- base->MAC_TIMESTAMP_CONTROL |= ENET_QOS_MAC_TIMESTAMP_CONTROL_TSUPDT_MASK;
- /* Wait for update finish */
- result = ENET_QOS_PollStatusFlag(&(base->MAC_TIMESTAMP_CONTROL), ENET_QOS_MAC_TIMESTAMP_CONTROL_TSUPDT_MASK, 0U);
- return result;
- }
- /*!
- * brief Correct the ENET PTP 1588 timer in fine method.
- *
- *
- * param base ENET peripheral base address.
- * param addend The addend value to be set in the fine method
- * note Should take refer to the chapter "System time correction" and
- * see the description for the "fine correction method".
- */
- status_t ENET_QOS_Ptp1588CorrectTimerInFine(ENET_QOS_Type *base, uint32_t addend)
- {
- status_t result = kStatus_Success;
- base->MAC_TIMESTAMP_ADDEND = addend;
- base->MAC_TIMESTAMP_CONTROL |= ENET_QOS_MAC_TIMESTAMP_CONTROL_TSADDREG_MASK;
- result = ENET_QOS_PollStatusFlag(&(base->MAC_TIMESTAMP_CONTROL), ENET_QOS_MAC_TIMESTAMP_CONTROL_TSADDREG_MASK, 0U);
- return result;
- }
- /*!
- * @brief Sets the ENET OQS PTP 1588 PPS target time registers.
- *
- * param base ENET QOS peripheral base address.
- * param instance The ENET QOS PTP PPS instance.
- * param seconds The target seconds.
- * param nanoseconds The target nanoseconds.
- */
- status_t ENET_QOS_Ptp1588PpsSetTrgtTime(ENET_QOS_Type *base,
- enet_qos_ptp_pps_instance_t instance,
- uint32_t seconds,
- uint32_t nanoseconds)
- {
- uint32_t *mac_pps_trgt_ns;
- uint32_t *mac_pps_trgt_s;
- mac_pps_trgt_ns = (uint32_t *)((uint32_t)&base->MAC_PPS0_TARGET_TIME_NANOSECONDS + 0x10U * (uint32_t)instance);
- mac_pps_trgt_s = (uint32_t *)((uint32_t)&base->MAC_PPS0_TARGET_TIME_SECONDS + 0x10U * (uint32_t)instance);
- if ((*mac_pps_trgt_ns & ENET_QOS_MAC_PPS0_TARGET_TIME_NANOSECONDS_TRGTBUSY0_MASK) != 0U)
- {
- return kStatus_ENET_QOS_TrgtBusy;
- }
- *mac_pps_trgt_ns = ENET_QOS_MAC_PPS0_TARGET_TIME_NANOSECONDS_TTSL0(nanoseconds);
- *mac_pps_trgt_s = ENET_QOS_MAC_PPS0_TARGET_TIME_SECONDS_TSTRH0(seconds);
- return kStatus_Success;
- }
- static status_t ENET_QOS_EstReadWriteWord(
- ENET_QOS_Type *base, uint32_t addr, uint32_t *data, uint8_t gcrr, uint8_t read, uint8_t dbgm)
- {
- uint32_t ctrl;
- int retry = 10;
- ctrl = ENET_QOS_MTL_EST_GCL_CONTROL_ADDR(addr) | ENET_QOS_MTL_EST_GCL_CONTROL_SRWO(1) |
- ENET_QOS_MTL_EST_GCL_CONTROL_DBGM(dbgm) | ENET_QOS_MTL_EST_GCL_CONTROL_GCRR(gcrr);
- if (read != 0U)
- {
- ctrl |= ENET_QOS_MTL_EST_GCL_CONTROL_R1W0(1);
- }
- else
- {
- base->MTL_EST_GCL_DATA = *data;
- }
- base->MTL_EST_GCL_CONTROL = ctrl;
- while ((base->MTL_EST_GCL_CONTROL & ENET_QOS_MTL_EST_GCL_CONTROL_SRWO_MASK) != 0U)
- {
- if (retry-- < 0)
- {
- return kStatus_Timeout;
- }
- SDK_DelayAtLeastUs(1, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
- }
- if (read != 0U)
- {
- *data = base->MTL_EST_GCL_DATA;
- }
- if ((base->MTL_EST_GCL_CONTROL & ENET_QOS_MTL_EST_GCL_CONTROL_ERR0_MASK) != 0U)
- {
- return kStatus_ENET_QOS_Est_SwListWriteAbort;
- }
- return kStatus_Success;
- }
- static status_t ENET_QOS_EstProgramWord(ENET_QOS_Type *base, uint32_t addr, uint32_t *data, uint8_t gcrr)
- {
- return ENET_QOS_EstReadWriteWord(base, addr, data, gcrr, 0, 0);
- }
- static status_t ENET_QOS_EstReadWord(ENET_QOS_Type *base, uint32_t addr, uint32_t *data, uint8_t gcrr, uint8_t dbgm)
- {
- return ENET_QOS_EstReadWriteWord(base, addr, data, gcrr, 1, dbgm);
- }
- /*!
- * @brief Program Gate Control List.
- *
- * This function is used to program the Enhanced Scheduled Transmisson. (IEEE802.1Qbv)
- *
- * @param base ENET peripheral base address..
- * @param gcl Pointer to the Gate Control List structure.
- * @param ptpClk_Hz frequency of the PTP clock.
- */
- status_t ENET_QOS_EstProgramGcl(ENET_QOS_Type *base, enet_qos_est_gcl_t *gcl, uint32_t ptpClk_Hz)
- {
- assert(gcl != NULL);
- uint32_t i, control, data;
- enet_qos_est_gate_op_t *gateOp;
- status_t rc;
- #define EST_MAX_INTERVAL ((1UL << ENET_QOS_EST_WID) - 1U)
- #define EST_MAX_GATE ((1UL << (32U - ENET_QOS_EST_WID)) - 1U)
- if (!gcl->enable)
- {
- goto exit;
- }
- /* Sanity checks */
- if (gcl->numEntries > ENET_QOS_EST_DEP)
- {
- return kStatus_ENET_QOS_Est_InvalidParameter;
- }
- if (gcl->opList == NULL)
- {
- return kStatus_ENET_QOS_Est_InvalidParameter;
- }
- gateOp = gcl->opList;
- for (i = 0; i < gcl->numEntries; i++)
- {
- if (gateOp->interval > EST_MAX_INTERVAL)
- {
- return kStatus_ENET_QOS_Est_InvalidParameter;
- }
- if (gateOp->gate > EST_MAX_GATE)
- {
- return kStatus_ENET_QOS_Est_InvalidParameter;
- }
- gateOp++;
- }
- /* Check if sw list is busy */
- if ((base->MTL_EST_CONTROL & ENET_QOS_MTL_EST_CONTROL_SSWL_MASK) != 0U)
- {
- return kStatus_ENET_QOS_Est_SwListBusy;
- }
- gateOp = gcl->opList;
- for (i = 0; i < gcl->numEntries; i++)
- {
- data = gateOp->interval | (gateOp->gate << ENET_QOS_EST_WID);
- rc = ENET_QOS_EstProgramWord(base, i, &data, 0);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- gateOp++;
- }
- /* BTR High */
- data = (uint32_t)(gcl->baseTime >> 32U);
- rc = ENET_QOS_EstProgramWord(base, (uint32_t)kENET_QOS_Ets_btr_high, &data, 1U);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- /* BTR Low */
- data = (uint32_t)gcl->baseTime;
- rc = ENET_QOS_EstProgramWord(base, (uint32_t)kENET_QOS_Ets_btr_low, &data, 1);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- /* CTR High */
- data = (uint32_t)(gcl->cycleTime >> 32U);
- rc = ENET_QOS_EstProgramWord(base, (uint32_t)kENET_QOS_Ets_ctr_high, &data, 1);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- /* CTR Low */
- data = (uint32_t)gcl->cycleTime;
- rc = ENET_QOS_EstProgramWord(base, (uint32_t)kENET_QOS_Ets_ctr_low, &data, 1);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- /* TER */
- data = gcl->extTime;
- rc = ENET_QOS_EstProgramWord(base, (uint32_t)kENET_QOS_Ets_ter, &data, 1);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- /* LLR */
- data = gcl->numEntries;
- rc = ENET_QOS_EstProgramWord(base, (uint32_t)kENET_QOS_Ets_llr, &data, 1);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- exit:
- control = base->MTL_EST_CONTROL;
- if (gcl->enable)
- {
- control &= ~ENET_QOS_MTL_EST_CONTROL_PTOV_MASK;
- control |= ENET_QOS_MTL_EST_CONTROL_SSWL_MASK | ENET_QOS_MTL_EST_CONTROL_EEST_MASK |
- ENET_QOS_MTL_EST_CONTROL_PTOV((1000000000U / ptpClk_Hz) * 6U);
- }
- else
- {
- control &= ~ENET_QOS_MTL_EST_CONTROL_EEST_MASK;
- }
- base->MTL_EST_CONTROL = control;
- return kStatus_Success;
- }
- /*!
- * @brief Read Gate Control List.
- *
- * This function is used to read the Enhanced Scheduled Transmisson list. (IEEE802.1Qbv)
- *
- * @param base ENET peripheral base address..
- * @param gcl Pointer to the Gate Control List structure.
- * @param listLen length of the provided opList array in gcl structure.
- * @param hwList Boolean if True read HW list, false read SW list.
- */
- status_t ENET_QOS_EstReadGcl(ENET_QOS_Type *base, enet_qos_est_gcl_t *gcl, uint32_t listLen, bool hwList)
- {
- assert(gcl != NULL);
- assert(gcl->opList != NULL);
- uint8_t dbgm = 0;
- uint32_t data, i;
- enet_qos_est_gate_op_t *gateOp;
- status_t rc;
- if (hwList == true)
- {
- dbgm = 1;
- }
- /* LLR */
- rc = ENET_QOS_EstReadWord(base, (uint32_t)kENET_QOS_Ets_llr, &data, 1, dbgm);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- gcl->numEntries = data;
- if (gcl->numEntries > listLen)
- {
- return kStatus_ENET_QOS_Est_InvalidParameter;
- }
- /* BTR High */
- rc = ENET_QOS_EstReadWord(base, (uint32_t)kENET_QOS_Ets_btr_high, &data, 1, dbgm);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- gcl->baseTime = (uint64_t)data << 32U;
- /* BTR Low */
- rc = ENET_QOS_EstReadWord(base, (uint32_t)kENET_QOS_Ets_btr_low, &data, 1, dbgm);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- gcl->baseTime |= data;
- /* CTR High */
- rc = ENET_QOS_EstReadWord(base, (uint32_t)kENET_QOS_Ets_ctr_high, &data, 1, dbgm);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- gcl->cycleTime = (uint64_t)data << 32U;
- /* CTR Low */
- rc = ENET_QOS_EstReadWord(base, (uint32_t)kENET_QOS_Ets_ctr_low, &data, 1, dbgm);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- gcl->cycleTime |= data;
- /* TER */
- rc = ENET_QOS_EstReadWord(base, (uint32_t)kENET_QOS_Ets_ter, &data, 1, dbgm);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- gcl->extTime = data;
- gateOp = gcl->opList;
- for (i = 0; i < gcl->numEntries; i++)
- {
- rc = ENET_QOS_EstReadWord(base, i, &data, 0, dbgm);
- if (rc != kStatus_Success)
- {
- return rc;
- }
- gateOp->interval = data & (EST_MAX_INTERVAL);
- gateOp->gate = data >> ENET_QOS_EST_WID;
- gateOp++;
- }
- return kStatus_Success;
- }
- /*!
- * brief Read flexible rx parser configuration at specified index.
- *
- * This function is used to read flexible rx parser configuration at specified index.
- *
- * param base ENET peripheral base address..
- * param rxpConfig The rx parser configuration pointer.
- * param entryIndex The rx parser entry index to read, start from 0.
- * retval kStatus_Success Configure rx parser success.
- * retval kStatus_ENET_QOS_Timeout Poll status flag timeout.
- */
- status_t ENET_QOS_ReadRxParser(ENET_QOS_Type *base, enet_qos_rxp_config_t *rxpConfig, uint16_t entryIndex)
- {
- assert(rxpConfig != NULL);
- assert(entryIndex < ENET_QOS_RXP_ENTRY_COUNT);
- uint32_t *dataPtr;
- uint8_t entrySize = sizeof(enet_qos_rxp_config_t) / sizeof(uint32_t);
- uint32_t value = 0U;
- status_t result = kStatus_Success;
- /* Wait hardware not busy */
- result = ENET_QOS_PollStatusFlag(&(base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS),
- ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_STARTBUSY_MASK, 0U);
- if (kStatus_Success != result)
- {
- return result;
- }
- for (uint8_t i = 0; i < entrySize; i++)
- {
- /* Read address. */
- value = ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_ADDR((uint32_t)entrySize * entryIndex + i);
- /* Issue read command. */
- value &= ~ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_WRRDN_MASK;
- base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS = value;
- /* Start Read */
- value |= ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_STARTBUSY_MASK;
- base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS = value;
- /* Wait hardware not busy */
- result = ENET_QOS_PollStatusFlag(&base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS,
- ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_STARTBUSY_MASK, 0U);
- if (kStatus_Success != result)
- {
- return result;
- }
- dataPtr = (uint32_t *)(void *)&rxpConfig[entryIndex];
- dataPtr = &dataPtr[i];
- /* Read data */
- *dataPtr = base->MTL_RXP_INDIRECT_ACC_DATA;
- }
- return result;
- }
- /*!
- * brief Configure flexible rx parser.
- *
- * This function is used to configure the flexible rx parser table.
- *
- * param base ENET peripheral base address..
- * param rxpConfig The rx parser configuration pointer.
- * param entryCount The rx parser entry count.
- * retval kStatus_Success Configure rx parser success.
- * retval kStatus_ENET_QOS_Timeout Poll status flag timeout.
- */
- status_t ENET_QOS_ConfigureRxParser(ENET_QOS_Type *base, enet_qos_rxp_config_t *rxpConfig, uint16_t entryCount)
- {
- assert(rxpConfig != NULL);
- assert(entryCount <= ENET_QOS_RXP_ENTRY_COUNT);
- uint32_t *dataPtr;
- uint32_t entrySize = sizeof(enet_qos_rxp_config_t) / sizeof(uint32_t);
- uint32_t value = 0U;
- status_t result = kStatus_Success;
- bool enableRx = false;
- /* Disable the MAC rx. */
- if (0U != (base->MAC_CONFIGURATION & ENET_QOS_MAC_CONFIGURATION_RE_MASK))
- {
- base->MAC_CONFIGURATION &= ~ENET_QOS_MAC_CONFIGURATION_RE_MASK;
- enableRx = true;
- }
- /* Disable frame parser. */
- result = ENET_QOS_EnableRxParser(base, false);
- if (kStatus_Success != result)
- {
- return result;
- }
- for (uint8_t count = 0; count < entryCount; count++)
- {
- for (uint8_t i = 0; i < entrySize; i++)
- {
- /* Wait hardware not busy */
- result = ENET_QOS_PollStatusFlag(&base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS,
- ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_STARTBUSY_MASK, 0U);
- if (kStatus_Success != result)
- {
- return result;
- }
- dataPtr = (uint32_t *)(void *)&rxpConfig[count];
- dataPtr = &dataPtr[i];
- /* Write data before issue write command */
- base->MTL_RXP_INDIRECT_ACC_DATA = *dataPtr;
- /* Write address and issue write command */
- value = ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_ADDR(entrySize * count + i);
- // base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS = value;
- value |= ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_WRRDN_MASK;
- base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS = value;
- /* Start write */
- value |= ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_STARTBUSY_MASK;
- base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS = value;
- }
- }
- /* Wait hardware not busy */
- result = ENET_QOS_PollStatusFlag(&(base->MTL_RXP_INDIRECT_ACC_CONTROL_STATUS),
- ENET_QOS_MTL_RXP_INDIRECT_ACC_CONTROL_STATUS_STARTBUSY_MASK, 0U);
- if (kStatus_Success != result)
- {
- return result;
- }
- /* Program NVE and NPE. */
- value = base->MTL_RXP_CONTROL_STATUS;
- value &= ~(ENET_QOS_MTL_RXP_CONTROL_STATUS_NVE_MASK | ENET_QOS_MTL_RXP_CONTROL_STATUS_NPE_MASK);
- value |= ENET_QOS_MTL_RXP_CONTROL_STATUS_NPE((uint32_t)entryCount - 1U);
- if (entryCount < 3U)
- {
- value |= ENET_QOS_MTL_RXP_CONTROL_STATUS_NVE(2U);
- }
- else
- {
- value |= ENET_QOS_MTL_RXP_CONTROL_STATUS_NVE((uint32_t)entryCount - 1U);
- }
- base->MTL_RXP_CONTROL_STATUS = value;
- /* Enable frame parser. */
- result = ENET_QOS_EnableRxParser(base, true);
- /* Enable Receive */
- if (enableRx)
- {
- base->MAC_CONFIGURATION |= ENET_QOS_MAC_CONFIGURATION_RE_MASK;
- }
- return result;
- }
- /*!
- * brief Gets statistical data in transfer.
- *
- * param base ENET_QOS peripheral base address.
- * param statistics The statistics structure pointer.
- */
- void ENET_QOS_GetStatistics(ENET_QOS_Type *base, enet_qos_transfer_stats_t *statistics)
- {
- /* Rx statistics */
- statistics->statsRxFrameCount = base->MAC_RX_PACKETS_COUNT_GOOD_BAD;
- statistics->statsRxCrcErr = base->MAC_RX_CRC_ERROR_PACKETS;
- statistics->statsRxAlignErr = base->MAC_RX_ALIGNMENT_ERROR_PACKETS;
- statistics->statsRxLengthErr = base->MAC_RX_LENGTH_ERROR_PACKETS;
- statistics->statsRxFifoOverflowErr = base->MAC_RX_FIFO_OVERFLOW_PACKETS;
- /* Tx statistics */
- statistics->statsTxFrameCount = base->MAC_TX_PACKET_COUNT_GOOD_BAD;
- statistics->statsTxFifoUnderRunErr = base->MAC_TX_UNDERFLOW_ERROR_PACKETS;
- }
- /*!
- * brief The ENET IRQ handler.
- *
- * param base ENET peripheral base address.
- * param handle The ENET handler pointer.
- */
- void ENET_QOS_CommonIRQHandler(ENET_QOS_Type *base, enet_qos_handle_t *handle)
- {
- /* Check for the interrupt source type. */
- /* DMA CHANNEL 0. */
- if ((base->DMA_INTERRUPT_STATUS & ENET_QOS_DMA_INTERRUPT_STATUS_DC0IS_MASK) != 0U)
- {
- uint32_t flag = base->DMA_CH[0].DMA_CHX_STAT;
- if ((flag & ENET_QOS_DMA_CHX_STAT_RI_MASK) != 0U)
- {
- base->DMA_CH[0].DMA_CHX_STAT = ENET_QOS_DMA_CHX_STAT_RI_MASK | ENET_QOS_DMA_CHX_STAT_NIS_MASK;
- if (handle->callback != NULL)
- {
- handle->callback(base, handle, kENET_QOS_RxIntEvent, 0, handle->userData);
- }
- }
- if ((flag & ENET_QOS_DMA_CHX_STAT_TI_MASK) != 0U)
- {
- base->DMA_CH[0].DMA_CHX_STAT = ENET_QOS_DMA_CHX_STAT_TI_MASK | ENET_QOS_DMA_CHX_STAT_NIS_MASK;
- ENET_QOS_ReclaimTxDescriptor(base, handle, 0);
- }
- }
- /* DMA CHANNEL 1. */
- if ((base->DMA_INTERRUPT_STATUS & ENET_QOS_DMA_INTERRUPT_STATUS_DC1IS_MASK) != 0U)
- {
- uint32_t flag = base->DMA_CH[1].DMA_CHX_STAT;
- if ((flag & ENET_QOS_DMA_CHX_STAT_RI_MASK) != 0U)
- {
- base->DMA_CH[1].DMA_CHX_STAT = ENET_QOS_DMA_CHX_STAT_RI_MASK | ENET_QOS_DMA_CHX_STAT_NIS_MASK;
- if (handle->callback != NULL)
- {
- handle->callback(base, handle, kENET_QOS_RxIntEvent, 1, handle->userData);
- }
- }
- if ((flag & ENET_QOS_DMA_CHX_STAT_TI_MASK) != 0U)
- {
- base->DMA_CH[1].DMA_CHX_STAT = ENET_QOS_DMA_CHX_STAT_TI_MASK | ENET_QOS_DMA_CHX_STAT_NIS_MASK;
- ENET_QOS_ReclaimTxDescriptor(base, handle, 1);
- }
- }
- /* DMA CHANNEL 2. */
- if ((base->DMA_INTERRUPT_STATUS & ENET_QOS_DMA_INTERRUPT_STATUS_DC2IS_MASK) != 0U)
- {
- uint32_t flag = base->DMA_CH[2].DMA_CHX_STAT;
- if ((flag & ENET_QOS_DMA_CHX_STAT_RI_MASK) != 0U)
- {
- base->DMA_CH[2].DMA_CHX_STAT = ENET_QOS_DMA_CHX_STAT_RI_MASK | ENET_QOS_DMA_CHX_STAT_NIS_MASK;
- if (handle->callback != NULL)
- {
- handle->callback(base, handle, kENET_QOS_RxIntEvent, 2, handle->userData);
- }
- }
- if ((flag & ENET_QOS_DMA_CHX_STAT_TI_MASK) != 0U)
- {
- base->DMA_CH[2].DMA_CHX_STAT = ENET_QOS_DMA_CHX_STAT_TI_MASK | ENET_QOS_DMA_CHX_STAT_NIS_MASK;
- ENET_QOS_ReclaimTxDescriptor(base, handle, 2);
- }
- }
- /* DMA CHANNEL 3. */
- if ((base->DMA_INTERRUPT_STATUS & ENET_QOS_DMA_INTERRUPT_STATUS_DC3IS_MASK) != 0U)
- {
- uint32_t flag = base->DMA_CH[3].DMA_CHX_STAT;
- if ((flag & ENET_QOS_DMA_CHX_STAT_RI_MASK) != 0U)
- {
- base->DMA_CH[3].DMA_CHX_STAT = ENET_QOS_DMA_CHX_STAT_RI_MASK | ENET_QOS_DMA_CHX_STAT_NIS_MASK;
- if (handle->callback != NULL)
- {
- handle->callback(base, handle, kENET_QOS_RxIntEvent, 3, handle->userData);
- }
- }
- if ((flag & ENET_QOS_DMA_CHX_STAT_TI_MASK) != 0U)
- {
- base->DMA_CH[3].DMA_CHX_STAT = ENET_QOS_DMA_CHX_STAT_TI_MASK | ENET_QOS_DMA_CHX_STAT_NIS_MASK;
- ENET_QOS_ReclaimTxDescriptor(base, handle, 3);
- }
- }
- /* MAC TIMESTAMP. */
- if ((base->DMA_INTERRUPT_STATUS & ENET_QOS_DMA_INTERRUPT_STATUS_MACIS_MASK) != 0U)
- {
- if ((base->MAC_INTERRUPT_STATUS & ENET_QOS_MAC_INTERRUPT_STATUS_TSIS_MASK) != 0U)
- {
- if (handle->callback != NULL)
- {
- handle->callback(base, handle, kENET_QOS_TimeStampIntEvent, 0, handle->userData);
- }
- }
- }
- SDK_ISR_EXIT_BARRIER;
- }
- #if defined(ENET_QOS)
- void ENET_QOS_DriverIRQHandler(void);
- void ENET_QOS_DriverIRQHandler(void)
- {
- s_enetqosIsr(ENET_QOS, s_ENETHandle[0]);
- }
- #endif
- #if defined(CONNECTIVITY__ENET_QOS)
- void CONNECTIVITY_EQOS_INT_DriverIRQHandler(void);
- void CONNECTIVITY_EQOS_INT_DriverIRQHandler(void)
- {
- s_enetqosIsr(CONNECTIVITY__ENET_QOS, s_ENETHandle[0]);
- }
- #endif
|