12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680 |
- /*!
- \file gd32f30x_enet.c
- \brief ENET driver
- \version 2017-02-10, V1.0.0, firmware for GD32F30x
- \version 2018-10-10, V1.1.0, firmware for GD32F30x
- \version 2018-12-25, V2.0.0, firmware for GD32F30x
- \version 2020-04-02, V2.0.1, firmware for GD32F30x
- \version 2020-09-30, V2.1.0, firmware for GD32F30x
- */
- /*
- Copyright (c) 2020, GigaDevice Semiconductor Inc.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software without
- specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- OF SUCH DAMAGE.
- */
- #include "gd32f30x_enet.h"
- #include <stdlib.h>
- #ifdef GD32F30X_CL
- #if defined (__CC_ARM) /*!< ARM compiler */
- __align(4)
- enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM]; /*!< ENET RxDMA descriptor */
- __align(4)
- enet_descriptors_struct txdesc_tab[ENET_TXBUF_NUM]; /*!< ENET TxDMA descriptor */
- __align(4)
- uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE]; /*!< ENET receive buffer */
- __align(4)
- uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE]; /*!< ENET transmit buffer */
- #elif defined ( __ICCARM__ ) /*!< IAR compiler */
- #pragma data_alignment=4
- enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM]; /*!< ENET RxDMA descriptor */
- #pragma data_alignment=4
- enet_descriptors_struct txdesc_tab[ENET_TXBUF_NUM]; /*!< ENET TxDMA descriptor */
- #pragma data_alignment=4
- uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE]; /*!< ENET receive buffer */
- #pragma data_alignment=4
- uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE]; /*!< ENET transmit buffer */
- #elif defined (__GNUC__) /* GNU Compiler */
- enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM] __attribute__ ((aligned (4))); /*!< ENET RxDMA descriptor */
- enet_descriptors_struct txdesc_tab[ENET_TXBUF_NUM] __attribute__ ((aligned (4))); /*!< ENET TxDMA descriptor */
- uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE] __attribute__ ((aligned (4))); /*!< ENET receive buffer */
- uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE] __attribute__ ((aligned (4))); /*!< ENET transmit buffer */
- #endif /* __CC_ARM */
- /* global transmit and receive descriptors pointers */
- enet_descriptors_struct *dma_current_txdesc;
- enet_descriptors_struct *dma_current_rxdesc;
- /* structure pointer of ptp descriptor for normal mode */
- enet_descriptors_struct *dma_current_ptp_txdesc = NULL;
- enet_descriptors_struct *dma_current_ptp_rxdesc = NULL;
- /* init structure parameters for ENET initialization */
- static enet_initpara_struct enet_initpara ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- static uint32_t enet_unknow_err = 0U;
- /* array of register offset for debug information get */
- static const uint16_t enet_reg_tab[] = {
- 0x0000, 0x0004, 0x0008, 0x000C, 0x0010, 0x0014, 0x0018, 0x001C, 0x0028, 0x002C, 0x0034,
- 0x0038, 0x003C, 0x0040, 0x0044, 0x0048, 0x004C, 0x0050, 0x0054, 0x0058, 0x005C, 0x1080,
-
- 0x0100, 0x0104, 0x0108, 0x010C, 0x0110, 0x014C, 0x0150, 0x0168, 0x0194, 0x0198, 0x01C4,
-
- 0x0700, 0x0704,0x0708, 0x070C, 0x0710, 0x0714, 0x0718, 0x071C, 0x0720, 0x0728, 0x072C,
-
- 0x1000, 0x1004, 0x1008, 0x100C, 0x1010, 0x1014, 0x1018, 0x101C, 0x1020, 0x1024, 0x1048,
- 0x104C, 0x1050, 0x1054};
- /* initialize ENET peripheral with generally concerned parameters, call it by enet_init() */
- static void enet_default_init(void);
- #ifndef USE_DELAY
- /* insert a delay time */
- static void enet_delay(uint32_t ncount);
- #endif /* USE_DELAY */
- /*!
- \brief deinitialize the ENET, and reset structure parameters for ENET initialization
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_deinit(void)
- {
- rcu_periph_reset_enable(RCU_ENETRST);
- rcu_periph_reset_disable(RCU_ENETRST);
- enet_initpara_reset();
- }
- /*!
- \brief configure the parameters which are usually less cared for initialization
- note -- this function must be called before enet_init(), otherwise
- configuration will be no effect
- \param[in] option: different function option, which is related to several parameters,
- only one parameter can be selected which is shown as below, refer to enet_option_enum
- \arg FORWARD_OPTION: choose to configure the frame forward related parameters
- \arg DMABUS_OPTION: choose to configure the DMA bus mode related parameters
- \arg DMA_MAXBURST_OPTION: choose to configure the DMA max burst related parameters
- \arg DMA_ARBITRATION_OPTION: choose to configure the DMA arbitration related parameters
- \arg STORE_OPTION: choose to configure the store forward mode related parameters
- \arg DMA_OPTION: choose to configure the DMA descriptor related parameters
- \arg VLAN_OPTION: choose to configure vlan related parameters
- \arg FLOWCTL_OPTION: choose to configure flow control related parameters
- \arg HASHH_OPTION: choose to configure hash high
- \arg HASHL_OPTION: choose to configure hash low
- \arg FILTER_OPTION: choose to configure frame filter related parameters
- \arg HALFDUPLEX_OPTION: choose to configure halfduplex mode related parameters
- \arg TIMER_OPTION: choose to configure time counter related parameters
- \arg INTERFRAMEGAP_OPTION: choose to configure the inter frame gap related parameters
- \param[in] para: the related parameters according to the option
- all the related parameters should be configured which are shown as below
- FORWARD_OPTION related parameters:
- - ENET_AUTO_PADCRC_DROP_ENABLE/ ENET_AUTO_PADCRC_DROP_DISABLE ;
- - ENET_TYPEFRAME_CRC_DROP_ENABLE/ ENET_TYPEFRAME_CRC_DROP_DISABLE ;
- - ENET_FORWARD_ERRFRAMES_ENABLE/ ENET_FORWARD_ERRFRAMES_DISABLE ;
- - ENET_FORWARD_UNDERSZ_GOODFRAMES_ENABLE/ ENET_FORWARD_UNDERSZ_GOODFRAMES_DISABLE .
- DMABUS_OPTION related parameters:
- - ENET_ADDRESS_ALIGN_ENABLE/ ENET_ADDRESS_ALIGN_DISABLE ;
- - ENET_FIXED_BURST_ENABLE/ ENET_FIXED_BURST_DISABLE ;
- - ENET_MIXED_BURST_ENABLE/ ENET_MIXED_BURST_DISABLE ;
- DMA_MAXBURST_OPTION related parameters:
- - ENET_RXDP_1BEAT/ ENET_RXDP_2BEAT/ ENET_RXDP_4BEAT/
- ENET_RXDP_8BEAT/ ENET_RXDP_16BEAT/ ENET_RXDP_32BEAT/
- ENET_RXDP_4xPGBL_4BEAT/ ENET_RXDP_4xPGBL_8BEAT/
- ENET_RXDP_4xPGBL_16BEAT/ ENET_RXDP_4xPGBL_32BEAT/
- ENET_RXDP_4xPGBL_64BEAT/ ENET_RXDP_4xPGBL_128BEAT ;
- - ENET_PGBL_1BEAT/ ENET_PGBL_2BEAT/ ENET_PGBL_4BEAT/
- ENET_PGBL_8BEAT/ ENET_PGBL_16BEAT/ ENET_PGBL_32BEAT/
- ENET_PGBL_4xPGBL_4BEAT/ ENET_PGBL_4xPGBL_8BEAT/
- ENET_PGBL_4xPGBL_16BEAT/ ENET_PGBL_4xPGBL_32BEAT/
- ENET_PGBL_4xPGBL_64BEAT/ ENET_PGBL_4xPGBL_128BEAT ;
- - ENET_RXTX_DIFFERENT_PGBL/ ENET_RXTX_SAME_PGBL ;
- DMA_ARBITRATION_OPTION related parameters:
- - ENET_ARBITRATION_RXPRIORTX
- - ENET_ARBITRATION_RXTX_1_1/ ENET_ARBITRATION_RXTX_2_1/
- ENET_ARBITRATION_RXTX_3_1/ ENET_ARBITRATION_RXTX_4_1/.
- STORE_OPTION related parameters:
- - ENET_RX_MODE_STOREFORWARD/ ENET_RX_MODE_CUTTHROUGH ;
- - ENET_TX_MODE_STOREFORWARD/ ENET_TX_MODE_CUTTHROUGH ;
- - ENET_RX_THRESHOLD_64BYTES/ ENET_RX_THRESHOLD_32BYTES/
- ENET_RX_THRESHOLD_96BYTES/ ENET_RX_THRESHOLD_128BYTES ;
- - ENET_TX_THRESHOLD_64BYTES/ ENET_TX_THRESHOLD_128BYTES/
- ENET_TX_THRESHOLD_192BYTES/ ENET_TX_THRESHOLD_256BYTES/
- ENET_TX_THRESHOLD_40BYTES/ ENET_TX_THRESHOLD_32BYTES/
- ENET_TX_THRESHOLD_24BYTES/ ENET_TX_THRESHOLD_16BYTES .
- DMA_OPTION related parameters:
- - ENET_FLUSH_RXFRAME_ENABLE/ ENET_FLUSH_RXFRAME_DISABLE ;
- - ENET_SECONDFRAME_OPT_ENABLE/ ENET_SECONDFRAME_OPT_DISABLE ;
- - ENET_ENHANCED_DESCRIPTOR/ ENET_NORMAL_DESCRIPTOR .
- VLAN_OPTION related parameters:
- - ENET_VLANTAGCOMPARISON_12BIT/ ENET_VLANTAGCOMPARISON_16BIT ;
- - MAC_VLT_VLTI(regval) .
- FLOWCTL_OPTION related parameters:
- - MAC_FCTL_PTM(regval) ;
- - ENET_ZERO_QUANTA_PAUSE_ENABLE/ ENET_ZERO_QUANTA_PAUSE_DISABLE ;
- - ENET_PAUSETIME_MINUS4/ ENET_PAUSETIME_MINUS28/
- ENET_PAUSETIME_MINUS144/ENET_PAUSETIME_MINUS256 ;
- - ENET_MAC0_AND_UNIQUE_ADDRESS_PAUSEDETECT/ ENET_UNIQUE_PAUSEDETECT ;
- - ENET_RX_FLOWCONTROL_ENABLE/ ENET_RX_FLOWCONTROL_DISABLE ;
- - ENET_TX_FLOWCONTROL_ENABLE/ ENET_TX_FLOWCONTROL_DISABLE ;
- - ENET_ACTIVE_THRESHOLD_256BYTES/ ENET_ACTIVE_THRESHOLD_512BYTES ;
- - ENET_ACTIVE_THRESHOLD_768BYTES/ ENET_ACTIVE_THRESHOLD_1024BYTES ;
- - ENET_ACTIVE_THRESHOLD_1280BYTES/ ENET_ACTIVE_THRESHOLD_1536BYTES ;
- - ENET_ACTIVE_THRESHOLD_1792BYTES ;
- - ENET_DEACTIVE_THRESHOLD_256BYTES/ ENET_DEACTIVE_THRESHOLD_512BYTES ;
- - ENET_DEACTIVE_THRESHOLD_768BYTES/ ENET_DEACTIVE_THRESHOLD_1024BYTES ;
- - ENET_DEACTIVE_THRESHOLD_1280BYTES/ ENET_DEACTIVE_THRESHOLD_1536BYTES ;
- - ENET_DEACTIVE_THRESHOLD_1792BYTES .
- HASHH_OPTION related parameters:
- - 0x0~0xFFFF FFFFU
- HASHL_OPTION related parameters:
- - 0x0~0xFFFF FFFFU
- FILTER_OPTION related parameters:
- - ENET_SRC_FILTER_NORMAL_ENABLE/ ENET_SRC_FILTER_INVERSE_ENABLE/
- ENET_SRC_FILTER_DISABLE ;
- - ENET_DEST_FILTER_INVERSE_ENABLE/ ENET_DEST_FILTER_INVERSE_DISABLE ;
- - ENET_MULTICAST_FILTER_HASH_OR_PERFECT/ ENET_MULTICAST_FILTER_HASH/
- ENET_MULTICAST_FILTER_PERFECT/ ENET_MULTICAST_FILTER_NONE ;
- - ENET_UNICAST_FILTER_EITHER/ ENET_UNICAST_FILTER_HASH/
- ENET_UNICAST_FILTER_PERFECT ;
- - ENET_PCFRM_PREVENT_ALL/ ENET_PCFRM_PREVENT_PAUSEFRAME/
- ENET_PCFRM_FORWARD_ALL/ ENET_PCFRM_FORWARD_FILTERED .
- HALFDUPLEX_OPTION related parameters:
- - ENET_CARRIERSENSE_ENABLE/ ENET_CARRIERSENSE_DISABLE ;
- - ENET_RECEIVEOWN_ENABLE/ ENET_RECEIVEOWN_DISABLE ;
- - ENET_RETRYTRANSMISSION_ENABLE/ ENET_RETRYTRANSMISSION_DISABLE ;
- - ENET_BACKOFFLIMIT_10/ ENET_BACKOFFLIMIT_8/
- ENET_BACKOFFLIMIT_4/ ENET_BACKOFFLIMIT_1 ;
- - ENET_DEFERRALCHECK_ENABLE/ ENET_DEFERRALCHECK_DISABLE .
- TIMER_OPTION related parameters:
- - ENET_WATCHDOG_ENABLE/ ENET_WATCHDOG_DISABLE ;
- - ENET_JABBER_ENABLE/ ENET_JABBER_DISABLE ;
- INTERFRAMEGAP_OPTION related parameters:
- - ENET_INTERFRAMEGAP_96BIT/ ENET_INTERFRAMEGAP_88BIT/
- ENET_INTERFRAMEGAP_80BIT/ ENET_INTERFRAMEGAP_72BIT/
- ENET_INTERFRAMEGAP_64BIT/ ENET_INTERFRAMEGAP_56BIT/
- ENET_INTERFRAMEGAP_48BIT/ ENET_INTERFRAMEGAP_40BIT .
- \param[out] none
- \retval none
- */
- void enet_initpara_config(enet_option_enum option, uint32_t para)
- {
- switch(option){
- case FORWARD_OPTION:
- /* choose to configure forward_frame, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)FORWARD_OPTION;
- enet_initpara.forward_frame = para;
- break;
- case DMABUS_OPTION:
- /* choose to configure dmabus_mode, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)DMABUS_OPTION;
- enet_initpara.dmabus_mode = para;
- break;
- case DMA_MAXBURST_OPTION:
- /* choose to configure dma_maxburst, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)DMA_MAXBURST_OPTION;
- enet_initpara.dma_maxburst = para;
- break;
- case DMA_ARBITRATION_OPTION:
- /* choose to configure dma_arbitration, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)DMA_ARBITRATION_OPTION;
- enet_initpara.dma_arbitration = para;
- break;
- case STORE_OPTION:
- /* choose to configure store_forward_mode, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)STORE_OPTION;
- enet_initpara.store_forward_mode = para;
- break;
- case DMA_OPTION:
- /* choose to configure dma_function, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)DMA_OPTION;
- #ifndef SELECT_DESCRIPTORS_ENHANCED_MODE
- para &= ~ENET_ENHANCED_DESCRIPTOR;
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- enet_initpara.dma_function = para;
- break;
- case VLAN_OPTION:
- /* choose to configure vlan_config, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)VLAN_OPTION;
- enet_initpara.vlan_config = para;
- break;
- case FLOWCTL_OPTION:
- /* choose to configure flow_control, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)FLOWCTL_OPTION;
- enet_initpara.flow_control = para;
- break;
- case HASHH_OPTION:
- /* choose to configure hashtable_high, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)HASHH_OPTION;
- enet_initpara.hashtable_high = para;
- break;
- case HASHL_OPTION:
- /* choose to configure hashtable_low, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)HASHL_OPTION;
- enet_initpara.hashtable_low = para;
- break;
- case FILTER_OPTION:
- /* choose to configure framesfilter_mode, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)FILTER_OPTION;
- enet_initpara.framesfilter_mode = para;
- break;
- case HALFDUPLEX_OPTION:
- /* choose to configure halfduplex_param, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)HALFDUPLEX_OPTION;
- enet_initpara.halfduplex_param = para;
- break;
- case TIMER_OPTION:
- /* choose to configure timer_config, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)TIMER_OPTION;
- enet_initpara.timer_config = para;
- break;
- case INTERFRAMEGAP_OPTION:
- /* choose to configure interframegap, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)INTERFRAMEGAP_OPTION;
- enet_initpara.interframegap = para;
- break;
- default:
- break;
- }
- }
- /*!
- \brief initialize ENET peripheral with generally concerned parameters and the less cared
- parameters
- \param[in] mediamode: PHY mode and mac loopback configurations, only one parameter can be selected
- which is shown as below, refer to enet_mediamode_enum
- \arg ENET_AUTO_NEGOTIATION: PHY auto negotiation
- \arg ENET_100M_FULLDUPLEX: 100Mbit/s, full-duplex
- \arg ENET_100M_HALFDUPLEX: 100Mbit/s, half-duplex
- \arg ENET_10M_FULLDUPLEX: 10Mbit/s, full-duplex
- \arg ENET_10M_HALFDUPLEX: 10Mbit/s, half-duplex
- \arg ENET_LOOPBACKMODE: MAC in loopback mode at the MII
- \param[in] checksum: IP frame checksum offload function, only one parameter can be selected
- which is shown as below, refer to enet_mediamode_enum
- \arg ENET_NO_AUTOCHECKSUM: disable IP frame checksum function
- \arg ENET_AUTOCHECKSUM_DROP_FAILFRAMES: enable IP frame checksum function
- \arg ENET_AUTOCHECKSUM_ACCEPT_FAILFRAMES: enable IP frame checksum function, and the received frame
- with only payload error but no other errors will not be dropped
- \param[in] recept: frame filter function, only one parameter can be selected
- which is shown as below, refer to enet_frmrecept_enum
- \arg ENET_PROMISCUOUS_MODE: promiscuous mode enabled
- \arg ENET_RECEIVEALL: all received frame are forwarded to application
- \arg ENET_BROADCAST_FRAMES_PASS: the address filters pass all received broadcast frames
- \arg ENET_BROADCAST_FRAMES_DROP: the address filters filter all incoming broadcast frames
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_init(enet_mediamode_enum mediamode, enet_chksumconf_enum checksum, enet_frmrecept_enum recept)
- {
- uint32_t reg_value=0U, reg_temp = 0U, temp = 0U;
- uint32_t media_temp = 0U;
- uint32_t timeout = 0U;
- uint16_t phy_value = 0U;
- ErrStatus phy_state= ERROR, enet_state = ERROR;
- /* PHY interface configuration, configure SMI clock and reset PHY chip */
- if(ERROR == enet_phy_config()){
- _ENET_DELAY_(PHY_RESETDELAY);
- if(ERROR == enet_phy_config()){
- return enet_state;
- }
- }
- /* initialize ENET peripheral with generally concerned parameters */
- enet_default_init();
- /* 1st, configure mediamode */
- media_temp = (uint32_t)mediamode;
- /* if is PHY auto negotiation */
- if((uint32_t)ENET_AUTO_NEGOTIATION == media_temp){
- /* wait for PHY_LINKED_STATUS bit be set */
- do{
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
- phy_value &= PHY_LINKED_STATUS;
- timeout++;
- }while((RESET == phy_value) && (timeout < PHY_READ_TO));
- /* return ERROR due to timeout */
- if(PHY_READ_TO == timeout){
- return enet_state;
- }
- /* reset timeout counter */
- timeout = 0U;
- /* enable auto-negotiation */
- phy_value = PHY_AUTONEGOTIATION;
- phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value);
- if(!phy_state){
- /* return ERROR due to write timeout */
- return enet_state;
- }
- /* wait for the PHY_AUTONEGO_COMPLETE bit be set */
- do{
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
- phy_value &= PHY_AUTONEGO_COMPLETE;
- timeout++;
- }while((RESET == phy_value) && (timeout < (uint32_t)PHY_READ_TO));
- /* return ERROR due to timeout */
- if(PHY_READ_TO == timeout){
- return enet_state;
- }
- /* reset timeout counter */
- timeout = 0U;
- /* read the result of the auto-negotiation */
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_SR, &phy_value);
- /* configure the duplex mode of MAC following the auto-negotiation result */
- if((uint16_t)RESET != (phy_value & PHY_DUPLEX_STATUS)){
- media_temp = ENET_MODE_FULLDUPLEX;
- }else{
- media_temp = ENET_MODE_HALFDUPLEX;
- }
- /* configure the communication speed of MAC following the auto-negotiation result */
- if((uint16_t)RESET !=(phy_value & PHY_SPEED_STATUS)){
- media_temp |= ENET_SPEEDMODE_10M;
- }else{
- media_temp |= ENET_SPEEDMODE_100M;
- }
- }else{
- phy_value = (uint16_t)((media_temp & ENET_MAC_CFG_DPM) >> 3);
- phy_value |= (uint16_t)((media_temp & ENET_MAC_CFG_SPD) >> 1);
- phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value);
- if(!phy_state){
- /* return ERROR due to write timeout */
- return enet_state;
- }
- /* PHY configuration need some time */
- _ENET_DELAY_(PHY_CONFIGDELAY);
- }
- /* after configuring the PHY, use mediamode to configure registers */
- reg_value = ENET_MAC_CFG;
- /* configure ENET_MAC_CFG register */
- reg_value &= (~(ENET_MAC_CFG_SPD |ENET_MAC_CFG_DPM |ENET_MAC_CFG_LBM));
- reg_value |= media_temp;
- ENET_MAC_CFG = reg_value;
- /* 2st, configure checksum */
- if(RESET != ((uint32_t)checksum & ENET_CHECKSUMOFFLOAD_ENABLE)){
- ENET_MAC_CFG |= ENET_CHECKSUMOFFLOAD_ENABLE;
- reg_value = ENET_DMA_CTL;
- /* configure ENET_DMA_CTL register */
- reg_value &= ~ENET_DMA_CTL_DTCERFD;
- reg_value |= ((uint32_t)checksum & ENET_DMA_CTL_DTCERFD);
- ENET_DMA_CTL = reg_value;
- }
- /* 3rd, configure recept */
- ENET_MAC_FRMF |= (uint32_t)recept;
- /* 4th, configure different function options */
- /* configure forward_frame related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)FORWARD_OPTION)){
- reg_temp = enet_initpara.forward_frame;
- reg_value = ENET_MAC_CFG;
- temp = reg_temp;
- /* configure ENET_MAC_CFG register */
- reg_value &= (~(ENET_MAC_CFG_TFCD |ENET_MAC_CFG_APCD));
- temp &= (ENET_MAC_CFG_TFCD | ENET_MAC_CFG_APCD);
- reg_value |= temp;
- ENET_MAC_CFG = reg_value;
- reg_value = ENET_DMA_CTL;
- temp = reg_temp;
- /* configure ENET_DMA_CTL register */
- reg_value &= (~(ENET_DMA_CTL_FERF |ENET_DMA_CTL_FUF));
- temp &= ((ENET_DMA_CTL_FERF | ENET_DMA_CTL_FUF)<<2);
- reg_value |= (temp >> 2);
- ENET_DMA_CTL = reg_value;
- }
- /* configure dmabus_mode related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)DMABUS_OPTION)){
- temp = enet_initpara.dmabus_mode;
- reg_value = ENET_DMA_BCTL;
- /* configure ENET_DMA_BCTL register */
- reg_value &= ~(ENET_DMA_BCTL_AA | ENET_DMA_BCTL_FB \
- |ENET_DMA_BCTL_FPBL | ENET_DMA_BCTL_MB);
- reg_value |= temp;
- ENET_DMA_BCTL = reg_value;
- }
- /* configure dma_maxburst related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_MAXBURST_OPTION)){
- temp = enet_initpara.dma_maxburst;
- reg_value = ENET_DMA_BCTL;
- /* configure ENET_DMA_BCTL register */
- reg_value &= ~(ENET_DMA_BCTL_RXDP| ENET_DMA_BCTL_PGBL | ENET_DMA_BCTL_UIP);
- reg_value |= temp;
- ENET_DMA_BCTL = reg_value;
- }
- /* configure dma_arbitration related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_ARBITRATION_OPTION)){
- temp = enet_initpara.dma_arbitration;
- reg_value = ENET_DMA_BCTL;
- /* configure ENET_DMA_BCTL register */
- reg_value &= ~(ENET_DMA_BCTL_RTPR | ENET_DMA_BCTL_DAB);
- reg_value |= temp;
- ENET_DMA_BCTL = reg_value;
- }
- /* configure store_forward_mode related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)STORE_OPTION)){
- temp = enet_initpara.store_forward_mode;
-
- reg_value = ENET_DMA_CTL;
- /* configure ENET_DMA_CTL register */
- reg_value &= ~(ENET_DMA_CTL_RSFD | ENET_DMA_CTL_TSFD| ENET_DMA_CTL_RTHC| ENET_DMA_CTL_TTHC);
- reg_value |= temp;
- ENET_DMA_CTL = reg_value;
- }
- /* configure dma_function related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_OPTION)){
- reg_temp = enet_initpara.dma_function;
- reg_value = ENET_DMA_CTL;
- temp = reg_temp;
- /* configure ENET_DMA_CTL register */
- reg_value &= (~(ENET_DMA_CTL_DAFRF |ENET_DMA_CTL_OSF));
- temp &= (ENET_DMA_CTL_DAFRF | ENET_DMA_CTL_OSF);
- reg_value |= temp;
- ENET_DMA_CTL = reg_value;
- reg_value = ENET_DMA_BCTL;
- temp = reg_temp;
- /* configure ENET_DMA_BCTL register */
- reg_value &= (~ENET_DMA_BCTL_DFM);
- temp &= ENET_DMA_BCTL_DFM;
- reg_value |= temp;
- ENET_DMA_BCTL = reg_value;
- }
- /* configure vlan_config related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)VLAN_OPTION)){
- reg_temp = enet_initpara.vlan_config;
-
- reg_value = ENET_MAC_VLT;
- /* configure ENET_MAC_VLT register */
- reg_value &= ~(ENET_MAC_VLT_VLTI | ENET_MAC_VLT_VLTC);
- reg_value |= reg_temp;
- ENET_MAC_VLT = reg_value;
- }
- /* configure flow_control related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)FLOWCTL_OPTION)){
- reg_temp = enet_initpara.flow_control;
-
- reg_value = ENET_MAC_FCTL;
- temp = reg_temp;
- /* configure ENET_MAC_FCTL register */
- reg_value &= ~(ENET_MAC_FCTL_PTM |ENET_MAC_FCTL_DZQP |ENET_MAC_FCTL_PLTS \
- | ENET_MAC_FCTL_UPFDT |ENET_MAC_FCTL_RFCEN |ENET_MAC_FCTL_TFCEN);
- temp &= (ENET_MAC_FCTL_PTM |ENET_MAC_FCTL_DZQP |ENET_MAC_FCTL_PLTS \
- | ENET_MAC_FCTL_UPFDT |ENET_MAC_FCTL_RFCEN |ENET_MAC_FCTL_TFCEN);
- reg_value |= temp;
- ENET_MAC_FCTL = reg_value;
-
- reg_value = ENET_MAC_FCTH;
- temp = reg_temp;
- /* configure ENET_MAC_FCTH register */
- reg_value &= ~(ENET_MAC_FCTH_RFA |ENET_MAC_FCTH_RFD);
- temp &= ((ENET_MAC_FCTH_RFA | ENET_MAC_FCTH_RFD )<<8);
- reg_value |= (temp >> 8);
- ENET_MAC_FCTH = reg_value;
- }
-
- /* configure hashtable_high related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)HASHH_OPTION)){
- ENET_MAC_HLH = enet_initpara.hashtable_high;
- }
- /* configure hashtable_low related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)HASHL_OPTION)){
- ENET_MAC_HLL = enet_initpara.hashtable_low;
- }
- /* configure framesfilter_mode related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)FILTER_OPTION)){
- reg_temp = enet_initpara.framesfilter_mode;
-
- reg_value = ENET_MAC_FRMF;
- /* configure ENET_MAC_FRMF register */
- reg_value &= ~(ENET_MAC_FRMF_SAFLT | ENET_MAC_FRMF_SAIFLT | ENET_MAC_FRMF_DAIFLT \
- | ENET_MAC_FRMF_HMF | ENET_MAC_FRMF_HPFLT | ENET_MAC_FRMF_MFD \
- | ENET_MAC_FRMF_HUF | ENET_MAC_FRMF_PCFRM);
- reg_value |= reg_temp;
- ENET_MAC_FRMF = reg_value;
- }
- /* configure halfduplex_param related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)HALFDUPLEX_OPTION)){
- reg_temp = enet_initpara.halfduplex_param;
- reg_value = ENET_MAC_CFG;
- /* configure ENET_MAC_CFG register */
- reg_value &= ~(ENET_MAC_CFG_CSD | ENET_MAC_CFG_ROD | ENET_MAC_CFG_RTD \
- | ENET_MAC_CFG_BOL | ENET_MAC_CFG_DFC);
- reg_value |= reg_temp;
- ENET_MAC_CFG = reg_value;
- }
- /* configure timer_config related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)TIMER_OPTION)){
- reg_temp = enet_initpara.timer_config;
-
- reg_value = ENET_MAC_CFG;
- /* configure ENET_MAC_CFG register */
- reg_value &= ~(ENET_MAC_CFG_WDD | ENET_MAC_CFG_JBD);
- reg_value |= reg_temp;
- ENET_MAC_CFG = reg_value;
- }
- /* configure interframegap related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)INTERFRAMEGAP_OPTION)){
- reg_temp = enet_initpara.interframegap;
-
- reg_value = ENET_MAC_CFG;
- /* configure ENET_MAC_CFG register */
- reg_value &= ~ENET_MAC_CFG_IGBS;
- reg_value |= reg_temp;
- ENET_MAC_CFG = reg_value;
- }
- enet_state = SUCCESS;
- return enet_state;
- }
- /*!
- \brief reset all core internal registers located in CLK_TX and CLK_RX
- \param[in] none
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_software_reset(void)
- {
- uint32_t timeout = 0U;
- ErrStatus enet_state = ERROR;
- uint32_t dma_flag;
- /* reset all core internal registers located in CLK_TX and CLK_RX */
- ENET_DMA_BCTL |= ENET_DMA_BCTL_SWR;
- /* wait for reset operation complete */
- do{
- dma_flag = (ENET_DMA_BCTL & ENET_DMA_BCTL_SWR);
- timeout++;
- }while((RESET != dma_flag) && (ENET_DELAY_TO != timeout));
- /* reset operation complete */
- if(RESET == (ENET_DMA_BCTL & ENET_DMA_BCTL_SWR)){
- enet_state = SUCCESS;
- }
- return enet_state;
- }
- /*!
- \brief check receive frame valid and return frame size
- \param[in] none
- \param[out] none
- \retval size of received frame: 0x0 - 0x3FFF
- */
- uint32_t enet_rxframe_size_get(void)
- {
- uint32_t size = 0U;
- uint32_t status;
-
- /* get rdes0 information of current RxDMA descriptor */
- status = dma_current_rxdesc->status;
-
- /* if the desciptor is owned by DMA */
- if((uint32_t)RESET != (status & ENET_RDES0_DAV)){
- return 0U;
- }
-
- /* if has any error, or the frame uses two or more descriptors */
- if((((uint32_t)RESET) != (status & ENET_RDES0_ERRS)) ||
- (((uint32_t)RESET) == (status & ENET_RDES0_LDES)) ||
- (((uint32_t)RESET) == (status & ENET_RDES0_FDES))){
- /* drop current receive frame */
- enet_rxframe_drop();
- return 1U;
- }
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- /* if is an ethernet-type frame, and IP frame payload error occurred */
- if(((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_FRMT) &&
- ((uint32_t)RESET) != (dma_current_rxdesc->extended_status & ENET_RDES4_IPPLDERR)){
- /* drop current receive frame */
- enet_rxframe_drop();
- return 1U;
- }
- #else
- /* if is an ethernet-type frame, and IP frame payload error occurred */
- if((((uint32_t)RESET) != (status & ENET_RDES0_FRMT)) &&
- (((uint32_t)RESET) != (status & ENET_RDES0_PCERR))){
- /* drop current receive frame */
- enet_rxframe_drop();
- return 1U;
- }
- #endif
- /* if CPU owns current descriptor, no error occured, the frame uses only one descriptor */
- if((((uint32_t)RESET) == (status & ENET_RDES0_DAV)) &&
- (((uint32_t)RESET) == (status & ENET_RDES0_ERRS)) &&
- (((uint32_t)RESET) != (status & ENET_RDES0_LDES)) &&
- (((uint32_t)RESET) != (status & ENET_RDES0_FDES))){
- /* get the size of the received data including CRC */
- size = GET_RDES0_FRML(status);
- /* substract the CRC size */
- size = size - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (status & ENET_RDES0_FRMT))){
- size = size + 4U;
- }
- }else{
- enet_unknow_err++;
- enet_rxframe_drop();
- return 1U;
- }
- /* return packet size */
- return size;
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in chain mode
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[out] none
- \retval none
- */
- void enet_descriptors_chain_init(enet_dmadirection_enum direction)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc, *desc_tab;
- uint8_t *buf;
- /* if want to initialize DMA Tx descriptors */
- if (ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* select chain mode */
- desc_status = ENET_TDES0_TCHM;
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* select receive chained mode and set buffer1 size */
- desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
-
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- }
- dma_current_ptp_rxdesc = NULL;
- dma_current_ptp_txdesc = NULL;
- /* configure each descriptor */
- for(num=0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
- /* if is not the last descriptor */
- if(num < (count - 1U)){
- /* configure the next descriptor address */
- desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
- }else{
- /* when it is the last descriptor, the next descriptor address
- equals to first descriptor address in descriptor table */
- desc->buffer2_next_desc_addr = (uint32_t) desc_tab;
- }
- }
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in ring mode
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[out] none
- \retval none
- */
- void enet_descriptors_ring_init(enet_dmadirection_enum direction)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc;
- enet_descriptors_struct *desc_tab;
- uint8_t *buf;
- /* configure descriptor skip length */
- ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
- ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
- /* if want to initialize DMA Tx descriptors */
- if (ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* set buffer1 size */
- desc_bufsize = ENET_RXBUF_SIZE;
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- }
- dma_current_ptp_rxdesc = NULL;
- dma_current_ptp_txdesc = NULL;
- /* configure each descriptor */
- for(num=0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
-
- /* when it is the last descriptor */
- if(num == (count - 1U)){
- if (ENET_DMA_TX == direction){
- /* configure transmit end of ring mode */
- desc->status |= ENET_TDES0_TERM;
- }else{
- /* configure receive end of ring mode */
- desc->control_buffer_size |= ENET_RDES1_RERM;
- }
- }
- }
- }
- /*!
- \brief handle current received frame data to application buffer
- \param[in] bufsize: the size of buffer which is the parameter in function
- \param[out] buffer: pointer to the received frame data
- note -- if the input is NULL, user should copy data in application by himself
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_frame_receive(uint8_t *buffer, uint32_t bufsize)
- {
- uint32_t offset = 0U, size = 0U;
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has copied data in application */
- if(NULL != buffer){
- /* if no error occurs, and the frame uses only one descriptor */
- if((((uint32_t)RESET) == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
- (((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
- (((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
- /* get the frame length except CRC */
- size = GET_RDES0_FRML(dma_current_rxdesc->status);
- size = size - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
- size = size + 4U;
- }
- /* to avoid situation that the frame size exceeds the buffer length */
- if(size > bufsize){
- return ERROR;
- }
- /* copy data from Rx buffer to application buffer */
- for(offset = 0U; offset<size; offset++){
- (*(buffer + offset)) = (*(__IO uint8_t *) (uint32_t)((dma_current_rxdesc->buffer1_addr) + offset));
- }
-
- }else{
- /* return ERROR */
- return ERROR;
- }
- }
- /* enable reception, descriptor is owned by DMA */
- dma_current_rxdesc->status = ENET_RDES0_DAV;
-
- /* check Rx buffer unavailable flag status */
- if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
- /* clear RBU flag */
- ENET_DMA_STAT = ENET_DMA_STAT_RBU;
- /* resume DMA reception by writing to the RPEN register*/
- ENET_DMA_RPEN = 0U;
- }
-
- /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
- /* chained mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + (GET_DMA_BCTL_DPSL(ENET_DMA_BCTL)));
- }
- }
- return SUCCESS;
- }
- /*!
- \brief handle application buffer data to transmit it
- \param[in] buffer: pointer to the frame data to be transmitted,
- note -- if the input is NULL, user should handle the data in application by himself
- \param[in] length: the length of frame data to be transmitted
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_frame_transmit(uint8_t *buffer, uint32_t length)
- {
- uint32_t offset = 0U;
- uint32_t dma_tbu_flag, dma_tu_flag;
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
- return ERROR;
- }
- /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
- if(length > ENET_MAX_FRAME_SIZE){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has handled data in application */
- if(NULL != buffer){
- /* copy frame data from application buffer to Tx buffer */
- for(offset = 0U; offset < length; offset++){
- (*(__IO uint8_t *) (uint32_t)((dma_current_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
- }
- }
- /* set the frame length */
- dma_current_txdesc->control_buffer_size = length;
- /* set the segment of frame, frame is transmitted in one descriptor */
- dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
- /* enable the DMA transmission */
- dma_current_txdesc->status |= ENET_TDES0_DAV;
- /* check Tx buffer unavailable flag status */
- dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
- dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
- if ((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
- /* clear TBU and TU flag */
- ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
- /* resume DMA transmission by writing to the TPEN register*/
- ENET_DMA_TPEN = 0U;
- }
- /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table*/
- /* chained mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
- dma_current_txdesc = (enet_descriptors_struct*) (dma_current_txdesc->buffer2_next_desc_addr);
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_txdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + (GET_DMA_BCTL_DPSL(ENET_DMA_BCTL)));
- }
- }
- return SUCCESS;
- }
- /*!
- \brief configure the transmit IP frame checksum offload calculation and insertion
- \param[in] desc: the descriptor pointer which users want to configure
- \param[in] checksum: IP frame checksum configuration
- only one parameter can be selected which is shown as below
- \arg ENET_CHECKSUM_DISABLE: checksum insertion disabled
- \arg ENET_CHECKSUM_IPV4HEADER: only IP header checksum calculation and insertion are enabled
- \arg ENET_CHECKSUM_TCPUDPICMP_SEGMENT: TCP/UDP/ICMP checksum insertion calculated but pseudo-header
- \arg ENET_CHECKSUM_TCPUDPICMP_FULL: TCP/UDP/ICMP checksum insertion fully calculated
- \param[out] none
- \retval ErrStatus: ERROR, SUCCESS
- */
- ErrStatus enet_transmit_checksum_config(enet_descriptors_struct *desc, uint32_t checksum)
- {
- if(NULL != desc){
- desc->status &= ~ENET_TDES0_CM;
- desc->status |= checksum;
- return SUCCESS;
- }else{
- return ERROR;
- }
- }
- /*!
- \brief ENET Tx and Rx function enable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_enable(void)
- {
- enet_tx_enable();
- enet_rx_enable();
- }
- /*!
- \brief ENET Tx and Rx function disable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_disable(void)
- {
- enet_tx_disable();
- enet_rx_disable();
- }
- /*!
- \brief configure MAC address
- \param[in] mac_addr: select which MAC address will be set,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_ADDRESS0: set MAC address 0 filter
- \arg ENET_MAC_ADDRESS1: set MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: set MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: set MAC address 3 filter
- \param[in] paddr: the buffer pointer which stores the MAC address
- (little-ending store, such as MAC address is aa:bb:cc:dd:ee:22, the buffer is {22, ee, dd, cc, bb, aa})
- \param[out] none
- \retval none
- */
- void enet_mac_address_set(enet_macaddress_enum mac_addr, uint8_t paddr[])
- {
- REG32(ENET_ADDRH_BASE + (uint32_t)mac_addr) = ENET_SET_MACADDRH(paddr);
- REG32(ENET_ADDRL_BASE + (uint32_t)mac_addr) = ENET_SET_MACADDRL(paddr);
- }
- /*!
- \brief get MAC address
- \param[in] mac_addr: select which MAC address will be get,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_ADDRESS0: get MAC address 0 filter
- \arg ENET_MAC_ADDRESS1: get MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: get MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: get MAC address 3 filter
- \param[out] paddr: the buffer pointer which is stored the MAC address
- (little-ending store, such as mac address is aa:bb:cc:dd:ee:22, the buffer is {22, ee, dd, cc, bb, aa})
- \param[in] bufsize: refer to the size of the buffer which stores the MAC address
- \arg 6 - 255
- \retval ErrStatus: ERROR, SUCCESS
- */
- ErrStatus enet_mac_address_get(enet_macaddress_enum mac_addr, uint8_t paddr[], uint8_t bufsize)
- {
- if(bufsize < 6U){
- return ERROR;
- }
- paddr[0] = ENET_GET_MACADDR(mac_addr, 0U);
- paddr[1] = ENET_GET_MACADDR(mac_addr, 1U);
- paddr[2] = ENET_GET_MACADDR(mac_addr, 2U);
- paddr[3] = ENET_GET_MACADDR(mac_addr, 3U);
- paddr[4] = ENET_GET_MACADDR(mac_addr, 4U);
- paddr[5] = ENET_GET_MACADDR(mac_addr, 5U);
- return SUCCESS;
- }
- /*!
- \brief get the ENET MAC/MSC/PTP/DMA status flag
- \param[in] enet_flag: ENET status flag, refer to enet_flag_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_FLAG_MPKR: magic packet received flag
- \arg ENET_MAC_FLAG_WUFR: wakeup frame received flag
- \arg ENET_MAC_FLAG_FLOWCONTROL: flow control status flag
- \arg ENET_MAC_FLAG_WUM: WUM status flag
- \arg ENET_MAC_FLAG_MSC: MSC status flag
- \arg ENET_MAC_FLAG_MSCR: MSC receive status flag
- \arg ENET_MAC_FLAG_MSCT: MSC transmit status flag
- \arg ENET_MAC_FLAG_TMST: time stamp trigger status flag
- \arg ENET_PTP_FLAG_TSSCO: timestamp second counter overflow flag
- \arg ENET_PTP_FLAG_TTM: target time match flag
- \arg ENET_MSC_FLAG_RFCE: received frames CRC error flag
- \arg ENET_MSC_FLAG_RFAE: received frames alignment error flag
- \arg ENET_MSC_FLAG_RGUF: received good unicast frames flag
- \arg ENET_MSC_FLAG_TGFSC: transmitted good frames single collision flag
- \arg ENET_MSC_FLAG_TGFMSC: transmitted good frames more single collision flag
- \arg ENET_MSC_FLAG_TGF: transmitted good frames flag
- \arg ENET_DMA_FLAG_TS: transmit status flag
- \arg ENET_DMA_FLAG_TPS: transmit process stopped status flag
- \arg ENET_DMA_FLAG_TBU: transmit buffer unavailable status flag
- \arg ENET_DMA_FLAG_TJT: transmit jabber timeout status flag
- \arg ENET_DMA_FLAG_RO: receive overflow status flag
- \arg ENET_DMA_FLAG_TU: transmit underflow status flag
- \arg ENET_DMA_FLAG_RS: receive status flag
- \arg ENET_DMA_FLAG_RBU: receive buffer unavailable status flag
- \arg ENET_DMA_FLAG_RPS: receive process stopped status flag
- \arg ENET_DMA_FLAG_RWT: receive watchdog timeout status flag
- \arg ENET_DMA_FLAG_ET: early transmit status flag
- \arg ENET_DMA_FLAG_FBE: fatal bus error status flag
- \arg ENET_DMA_FLAG_ER: early receive status flag
- \arg ENET_DMA_FLAG_AI: abnormal interrupt summary flag
- \arg ENET_DMA_FLAG_NI: normal interrupt summary flag
- \arg ENET_DMA_FLAG_EB_DMA_ERROR: DMA error flag
- \arg ENET_DMA_FLAG_EB_TRANSFER_ERROR: transfer error flag
- \arg ENET_DMA_FLAG_EB_ACCESS_ERROR: access error flag
- \arg ENET_DMA_FLAG_MSC: MSC status flag
- \arg ENET_DMA_FLAG_WUM: WUM status flag
- \arg ENET_DMA_FLAG_TST: timestamp trigger status flag
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus enet_flag_get(enet_flag_enum enet_flag)
- {
- if(RESET != (ENET_REG_VAL(enet_flag) & BIT(ENET_BIT_POS(enet_flag)))){
- return SET;
- }else{
- return RESET;
- }
- }
- /*!
- \brief clear the ENET DMA status flag
- \param[in] enet_flag: ENET DMA flag clear, refer to enet_flag_clear_enum
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_FLAG_TS_CLR: transmit status flag clear
- \arg ENET_DMA_FLAG_TPS_CLR: transmit process stopped status flag clear
- \arg ENET_DMA_FLAG_TBU_CLR: transmit buffer unavailable status flag clear
- \arg ENET_DMA_FLAG_TJT_CLR: transmit jabber timeout status flag clear
- \arg ENET_DMA_FLAG_RO_CLR: receive overflow status flag clear
- \arg ENET_DMA_FLAG_TU_CLR: transmit underflow status flag clear
- \arg ENET_DMA_FLAG_RS_CLR: receive status flag clear
- \arg ENET_DMA_FLAG_RBU_CLR: receive buffer unavailable status flag clear
- \arg ENET_DMA_FLAG_RPS_CLR: receive process stopped status flag clear
- \arg ENET_DMA_FLAG_RWT_CLR: receive watchdog timeout status flag clear
- \arg ENET_DMA_FLAG_ET_CLR: early transmit status flag clear
- \arg ENET_DMA_FLAG_FBE_CLR: fatal bus error status flag clear
- \arg ENET_DMA_FLAG_ER_CLR: early receive status flag clear
- \arg ENET_DMA_FLAG_AI_CLR: abnormal interrupt summary flag clear
- \arg ENET_DMA_FLAG_NI_CLR: normal interrupt summary flag clear
- \param[out] none
- \retval none
- */
- void enet_flag_clear(enet_flag_clear_enum enet_flag)
- {
- /* write 1 to the corresponding bit in ENET_DMA_STAT, to clear it */
- ENET_REG_VAL(enet_flag) = BIT(ENET_BIT_POS(enet_flag));
- }
- /*!
- \brief enable ENET MAC/MSC/DMA interrupt
- \param[in] enet_int: ENET interrupt,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_INT_WUMIM: WUM interrupt mask
- \arg ENET_MAC_INT_TMSTIM: timestamp trigger interrupt mask
- \arg ENET_MSC_INT_RFCEIM: received frame CRC error interrupt mask
- \arg ENET_MSC_INT_RFAEIM: received frames alignment error interrupt mask
- \arg ENET_MSC_INT_RGUFIM: received good unicast frames interrupt mask
- \arg ENET_MSC_INT_TGFSCIM: transmitted good frames single collision interrupt mask
- \arg ENET_MSC_INT_TGFMSCIM: transmitted good frames more single collision interrupt mask
- \arg ENET_MSC_INT_TGFIM: transmitted good frames interrupt mask
- \arg ENET_DMA_INT_TIE: transmit interrupt enable
- \arg ENET_DMA_INT_TPSIE: transmit process stopped interrupt enable
- \arg ENET_DMA_INT_TBUIE: transmit buffer unavailable interrupt enable
- \arg ENET_DMA_INT_TJTIE: transmit jabber timeout interrupt enable
- \arg ENET_DMA_INT_ROIE: receive overflow interrupt enable
- \arg ENET_DMA_INT_TUIE: transmit underflow interrupt enable
- \arg ENET_DMA_INT_RIE: receive interrupt enable
- \arg ENET_DMA_INT_RBUIE: receive buffer unavailable interrupt enable
- \arg ENET_DMA_INT_RPSIE: receive process stopped interrupt enable
- \arg ENET_DMA_INT_RWTIE: receive watchdog timeout interrupt enable
- \arg ENET_DMA_INT_ETIE: early transmit interrupt enable
- \arg ENET_DMA_INT_FBEIE: fatal bus error interrupt enable
- \arg ENET_DMA_INT_ERIE: early receive interrupt enable
- \arg ENET_DMA_INT_AIE: abnormal interrupt summary enable
- \arg ENET_DMA_INT_NIE: normal interrupt summary enable
- \param[out] none
- \retval none
- */
- void enet_interrupt_enable(enet_int_enum enet_int)
- {
- if(DMA_INTEN_REG_OFFSET == ((uint32_t)enet_int >> 6)){
- /* ENET_DMA_INTEN register interrupt */
- ENET_REG_VAL(enet_int) |= BIT(ENET_BIT_POS(enet_int));
- }else{
- /* other INTMSK register interrupt */
- ENET_REG_VAL(enet_int) &= ~BIT(ENET_BIT_POS(enet_int));
- }
- }
- /*!
- \brief disable ENET MAC/MSC/DMA interrupt
- \param[in] enet_int: ENET interrupt,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_INT_WUMIM: WUM interrupt mask
- \arg ENET_MAC_INT_TMSTIM: timestamp trigger interrupt mask
- \arg ENET_MSC_INT_RFCEIM: received frame CRC error interrupt mask
- \arg ENET_MSC_INT_RFAEIM: received frames alignment error interrupt mask
- \arg ENET_MSC_INT_RGUFIM: received good unicast frames interrupt mask
- \arg ENET_MSC_INT_TGFSCIM: transmitted good frames single collision interrupt mask
- \arg ENET_MSC_INT_TGFMSCIM: transmitted good frames more single collision interrupt mask
- \arg ENET_MSC_INT_TGFIM: transmitted good frames interrupt mask
- \arg ENET_DMA_INT_TIE: transmit interrupt enable
- \arg ENET_DMA_INT_TPSIE: transmit process stopped interrupt enable
- \arg ENET_DMA_INT_TBUIE: transmit buffer unavailable interrupt enable
- \arg ENET_DMA_INT_TJTIE: transmit jabber timeout interrupt enable
- \arg ENET_DMA_INT_ROIE: receive overflow interrupt enable
- \arg ENET_DMA_INT_TUIE: transmit underflow interrupt enable
- \arg ENET_DMA_INT_RIE: receive interrupt enable
- \arg ENET_DMA_INT_RBUIE: receive buffer unavailable interrupt enable
- \arg ENET_DMA_INT_RPSIE: receive process stopped interrupt enable
- \arg ENET_DMA_INT_RWTIE: receive watchdog timeout interrupt enable
- \arg ENET_DMA_INT_ETIE: early transmit interrupt enable
- \arg ENET_DMA_INT_FBEIE: fatal bus error interrupt enable
- \arg ENET_DMA_INT_ERIE: early receive interrupt enable
- \arg ENET_DMA_INT_AIE: abnormal interrupt summary enable
- \arg ENET_DMA_INT_NIE: normal interrupt summary enable
- \param[out] none
- \retval none
- */
- void enet_interrupt_disable(enet_int_enum enet_int)
- {
- if(DMA_INTEN_REG_OFFSET == ((uint32_t)enet_int >> 6)){
- /* ENET_DMA_INTEN register interrupt */
- ENET_REG_VAL(enet_int) &= ~BIT(ENET_BIT_POS(enet_int));
- }else{
- /* other INTMSK register interrupt */
- ENET_REG_VAL(enet_int) |= BIT(ENET_BIT_POS(enet_int));
- }
- }
- /*!
- \brief get ENET MAC/MSC/DMA interrupt flag
- \param[in] int_flag: ENET interrupt flag,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_INT_FLAG_WUM: WUM status flag
- \arg ENET_MAC_INT_FLAG_MSC: MSC status flag
- \arg ENET_MAC_INT_FLAG_MSCR: MSC receive status flag
- \arg ENET_MAC_INT_FLAG_MSCT: MSC transmit status flag
- \arg ENET_MAC_INT_FLAG_TMST: time stamp trigger status flag
- \arg ENET_MSC_INT_FLAG_RFCE: received frames CRC error flag
- \arg ENET_MSC_INT_FLAG_RFAE: received frames alignment error flag
- \arg ENET_MSC_INT_FLAG_RGUF: received good unicast frames flag
- \arg ENET_MSC_INT_FLAG_TGFSC: transmitted good frames single collision flag
- \arg ENET_MSC_INT_FLAG_TGFMSC: transmitted good frames more single collision flag
- \arg ENET_MSC_INT_FLAG_TGF: transmitted good frames flag
- \arg ENET_DMA_INT_FLAG_TS: transmit status flag
- \arg ENET_DMA_INT_FLAG_TPS: transmit process stopped status flag
- \arg ENET_DMA_INT_FLAG_TBU: transmit buffer unavailable status flag
- \arg ENET_DMA_INT_FLAG_TJT: transmit jabber timeout status flag
- \arg ENET_DMA_INT_FLAG_RO: receive overflow status flag
- \arg ENET_DMA_INT_FLAG_TU: transmit underflow status flag
- \arg ENET_DMA_INT_FLAG_RS: receive status flag
- \arg ENET_DMA_INT_FLAG_RBU: receive buffer unavailable status flag
- \arg ENET_DMA_INT_FLAG_RPS: receive process stopped status flag
- \arg ENET_DMA_INT_FLAG_RWT: receive watchdog timeout status flag
- \arg ENET_DMA_INT_FLAG_ET: early transmit status flag
- \arg ENET_DMA_INT_FLAG_FBE: fatal bus error status flag
- \arg ENET_DMA_INT_FLAG_ER: early receive status flag
- \arg ENET_DMA_INT_FLAG_AI: abnormal interrupt summary flag
- \arg ENET_DMA_INT_FLAG_NI: normal interrupt summary flag
- \arg ENET_DMA_INT_FLAG_MSC: MSC status flag
- \arg ENET_DMA_INT_FLAG_WUM: WUM status flag
- \arg ENET_DMA_INT_FLAG_TST: timestamp trigger status flag
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus enet_interrupt_flag_get(enet_int_flag_enum int_flag)
- {
- if(RESET != (ENET_REG_VAL(int_flag) & BIT(ENET_BIT_POS(int_flag)))){
- return SET;
- }else{
- return RESET;
- }
- }
- /*!
- \brief clear ENET DMA interrupt flag
- \param[in] int_flag_clear: clear ENET interrupt flag,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_INT_FLAG_TS_CLR: transmit status flag
- \arg ENET_DMA_INT_FLAG_TPS_CLR: transmit process stopped status flag
- \arg ENET_DMA_INT_FLAG_TBU_CLR: transmit buffer unavailable status flag
- \arg ENET_DMA_INT_FLAG_TJT_CLR: transmit jabber timeout status flag
- \arg ENET_DMA_INT_FLAG_RO_CLR: receive overflow status flag
- \arg ENET_DMA_INT_FLAG_TU_CLR: transmit underflow status flag
- \arg ENET_DMA_INT_FLAG_RS_CLR: receive status flag
- \arg ENET_DMA_INT_FLAG_RBU_CLR: receive buffer unavailable status flag
- \arg ENET_DMA_INT_FLAG_RPS_CLR: receive process stopped status flag
- \arg ENET_DMA_INT_FLAG_RWT_CLR: receive watchdog timeout status flag
- \arg ENET_DMA_INT_FLAG_ET_CLR: early transmit status flag
- \arg ENET_DMA_INT_FLAG_FBE_CLR: fatal bus error status flag
- \arg ENET_DMA_INT_FLAG_ER_CLR: early receive status flag
- \arg ENET_DMA_INT_FLAG_AI_CLR: abnormal interrupt summary flag
- \arg ENET_DMA_INT_FLAG_NI_CLR: normal interrupt summary flag
- \param[out] none
- \retval none
- */
- void enet_interrupt_flag_clear(enet_int_flag_clear_enum int_flag_clear)
- {
- /* write 1 to the corresponding bit in ENET_DMA_STAT, to clear it */
- ENET_REG_VAL(int_flag_clear) = BIT(ENET_BIT_POS(int_flag_clear));
- }
- /*!
- \brief ENET Tx function enable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_tx_enable(void)
- {
- ENET_MAC_CFG |= ENET_MAC_CFG_TEN;
- enet_txfifo_flush();
- ENET_DMA_CTL |= ENET_DMA_CTL_STE;
- }
- /*!
- \brief ENET Tx function disable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_tx_disable(void)
- {
- ENET_DMA_CTL &= ~ENET_DMA_CTL_STE;
- enet_txfifo_flush();
- ENET_MAC_CFG &= ~ENET_MAC_CFG_TEN;
- }
- /*!
- \brief ENET Rx function enable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_rx_enable(void)
- {
- ENET_MAC_CFG |= ENET_MAC_CFG_REN;
- ENET_DMA_CTL |= ENET_DMA_CTL_SRE;
- }
- /*!
- \brief ENET Rx function disable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_rx_disable(void)
- {
- ENET_DMA_CTL &= ~ENET_DMA_CTL_SRE;
- ENET_MAC_CFG &= ~ENET_MAC_CFG_REN;
- }
- /*!
- \brief put registers value into the application buffer
- \param[in] type: register type which will be get, refer to enet_registers_type_enum,
- only one parameter can be selected which is shown as below
- \arg ALL_MAC_REG: get the registers within the offset scope between ENET_MAC_CFG and ENET_MAC_FCTH
- \arg ALL_MSC_REG: get the registers within the offset scope between ENET_MSC_CTL and ENET_MSC_RGUFCNT
- \arg ALL_PTP_REG: get the registers within the offset scope between ENET_PTP_TSCTL and ENET_PTP_PPSCTL
- \arg ALL_DMA_REG: get the registers within the offset scope between ENET_DMA_BCTL and ENET_DMA_CRBADDR
- \param[in] num: the number of registers that the user want to get
- \param[out] preg: the application buffer pointer for storing the register value
- \retval none
- */
- void enet_registers_get(enet_registers_type_enum type, uint32_t *preg, uint32_t num)
- {
- uint32_t offset = 0U, max = 0U, limit = 0U;
- offset = (uint32_t)type;
- max = (uint32_t)type + num;
- limit = sizeof(enet_reg_tab)/sizeof(uint16_t);
- /* prevent element in this array is out of range */
- if(max > limit){
- max = limit;
- }
- for(; offset < max; offset++){
- /* get value of the corresponding register */
- *preg = REG32((ENET) + enet_reg_tab[offset]);
- preg++;
- }
- }
- /*!
- \brief get the enet debug status from the debug register
- \param[in] mac_debug: enet debug status,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_RECEIVER_NOT_IDLE: MAC receiver is not in idle state
- \arg ENET_RX_ASYNCHRONOUS_FIFO_STATE: Rx asynchronous FIFO status
- \arg ENET_RXFIFO_WRITING: RxFIFO is doing write operation
- \arg ENET_RXFIFO_READ_STATUS: RxFIFO read operation status
- \arg ENET_RXFIFO_STATE: RxFIFO state
- \arg ENET_MAC_TRANSMITTER_NOT_IDLE: MAC transmitter is not in idle state
- \arg ENET_MAC_TRANSMITTER_STATUS: status of MAC transmitter
- \arg ENET_PAUSE_CONDITION_STATUS: pause condition status
- \arg ENET_TXFIFO_READ_STATUS: TxFIFO read operation status
- \arg ENET_TXFIFO_WRITING: TxFIFO is doing write operation
- \arg ENET_TXFIFO_NOT_EMPTY: TxFIFO is not empty
- \arg ENET_TXFIFO_FULL: TxFIFO is full
- \param[out] none
- \retval value of the status users want to get
- */
- uint32_t enet_debug_status_get(uint32_t mac_debug)
- {
- uint32_t temp_state = 0U;
- switch(mac_debug){
- case ENET_RX_ASYNCHRONOUS_FIFO_STATE:
- temp_state = GET_MAC_DBG_RXAFS(ENET_MAC_DBG);
- break;
- case ENET_RXFIFO_READ_STATUS:
- temp_state = GET_MAC_DBG_RXFRS(ENET_MAC_DBG);
- break;
- case ENET_RXFIFO_STATE:
- temp_state = GET_MAC_DBG_RXFS(ENET_MAC_DBG);
- break;
- case ENET_MAC_TRANSMITTER_STATUS:
- temp_state = GET_MAC_DBG_SOMT(ENET_MAC_DBG);
- break;
- case ENET_TXFIFO_READ_STATUS:
- temp_state = GET_MAC_DBG_TXFRS(ENET_MAC_DBG);
- break;
- default:
- if(RESET != (ENET_MAC_DBG & mac_debug)){
- temp_state = 0x1U;
- }
- break;
- }
- return temp_state;
- }
- /*!
- \brief enable the MAC address filter
- \param[in] mac_addr: select which MAC address will be enable
- \arg ENET_MAC_ADDRESS1: enable MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: enable MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: enable MAC address 3 filter
- \param[out] none
- \retval none
- */
- void enet_address_filter_enable(enet_macaddress_enum mac_addr)
- {
- REG32(ENET_ADDRH_BASE + mac_addr) |= ENET_MAC_ADDR1H_AFE;
- }
- /*!
- \brief disable the MAC address filter
- \param[in] mac_addr: select which MAC address will be disable,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_ADDRESS1: disable MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: disable MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: disable MAC address 3 filter
- \param[out] none
- \retval none
- */
- void enet_address_filter_disable(enet_macaddress_enum mac_addr)
- {
- REG32(ENET_ADDRH_BASE + mac_addr) &= ~ENET_MAC_ADDR1H_AFE;
- }
- /*!
- \brief configure the MAC address filter
- \param[in] mac_addr: select which MAC address will be configured,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_ADDRESS1: configure MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: configure MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: configure MAC address 3 filter
- \param[in] addr_mask: select which MAC address bytes will be mask,
- one or more parameters can be selected which are shown as below
- \arg ENET_ADDRESS_MASK_BYTE0: mask ENET_MAC_ADDR1L[7:0] bits
- \arg ENET_ADDRESS_MASK_BYTE1: mask ENET_MAC_ADDR1L[15:8] bits
- \arg ENET_ADDRESS_MASK_BYTE2: mask ENET_MAC_ADDR1L[23:16] bits
- \arg ENET_ADDRESS_MASK_BYTE3: mask ENET_MAC_ADDR1L [31:24] bits
- \arg ENET_ADDRESS_MASK_BYTE4: mask ENET_MAC_ADDR1H [7:0] bits
- \arg ENET_ADDRESS_MASK_BYTE5: mask ENET_MAC_ADDR1H [15:8] bits
- \param[in] filter_type: select which MAC address filter type will be selected,
- only one parameter can be selected which is shown as below
- \arg ENET_ADDRESS_FILTER_SA: The MAC address is used to compared with the SA field of the received frame
- \arg ENET_ADDRESS_FILTER_DA: The MAC address is used to compared with the DA field of the received frame
- \param[out] none
- \retval none
- */
- void enet_address_filter_config(enet_macaddress_enum mac_addr, uint32_t addr_mask, uint32_t filter_type)
- {
- uint32_t reg;
- /* get the address filter register value which is to be configured */
- reg = REG32(ENET_ADDRH_BASE + mac_addr);
- /* clear and configure the address filter register */
- reg &= ~(ENET_MAC_ADDR1H_MB | ENET_MAC_ADDR1H_SAF);
- reg |= (addr_mask | filter_type);
- REG32(ENET_ADDRH_BASE + mac_addr) = reg;
- }
- /*!
- \brief PHY interface configuration (configure SMI clock and reset PHY chip)
- \param[in] none
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_phy_config(void)
- {
- uint32_t ahbclk;
- uint32_t reg;
- uint16_t phy_value;
- ErrStatus enet_state = ERROR;
- /* clear the previous MDC clock */
- reg = ENET_MAC_PHY_CTL;
- reg &= ~ENET_MAC_PHY_CTL_CLR;
- /* get the HCLK frequency */
- ahbclk = rcu_clock_freq_get(CK_AHB);
- /* configure MDC clock according to HCLK frequency range */
- if(ENET_RANGE(ahbclk, 20000000U, 35000000U)){
- reg |= ENET_MDC_HCLK_DIV16;
- }else if(ENET_RANGE(ahbclk, 35000000U, 60000000U)){
- reg |= ENET_MDC_HCLK_DIV26;
- }else if(ENET_RANGE(ahbclk, 60000000U, 100000000U)){
- reg |= ENET_MDC_HCLK_DIV42;
- }else if((ENET_RANGE(ahbclk, 100000000U, 168000000U))||(168000000U == ahbclk)){
- reg |= ENET_MDC_HCLK_DIV62;
- }else{
- return enet_state;
- }
- ENET_MAC_PHY_CTL = reg;
- /* reset PHY */
- phy_value = PHY_RESET;
- if(ERROR == (enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value))){
- return enet_state;
- }
- /* PHY reset need some time */
- _ENET_DELAY_(ENET_DELAY_TO);
-
- /* check whether PHY reset is complete */
- if(ERROR == (enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &phy_value))){
- return enet_state;
- }
- /* PHY reset complete */
- if(RESET == (phy_value & PHY_RESET)){
- enet_state = SUCCESS;
- }
- return enet_state;
- }
- /*!
- \brief write to / read from a PHY register
- \param[in] direction: only one parameter can be selected which is shown as below
- \arg ENET_PHY_WRITE: write data to phy register
- \arg ENET_PHY_READ: read data from phy register
- \param[in] phy_address: 0x0 - 0x1F
- \param[in] phy_reg: 0x0 - 0x1F
- \param[in] pvalue: the value will be written to the PHY register in ENET_PHY_WRITE direction
- \param[out] pvalue: the value will be read from the PHY register in ENET_PHY_READ direction
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_phy_write_read(enet_phydirection_enum direction, uint16_t phy_address, uint16_t phy_reg, uint16_t *pvalue)
- {
- uint32_t reg, phy_flag;
- uint32_t timeout = 0U;
- ErrStatus enet_state = ERROR;
- /* configure ENET_MAC_PHY_CTL with write/read operation */
- reg = ENET_MAC_PHY_CTL;
- reg &= ~(ENET_MAC_PHY_CTL_PB | ENET_MAC_PHY_CTL_PW | ENET_MAC_PHY_CTL_PR | ENET_MAC_PHY_CTL_PA);
- reg |= (direction | MAC_PHY_CTL_PR(phy_reg) | MAC_PHY_CTL_PA(phy_address) | ENET_MAC_PHY_CTL_PB);
- /* if do the write operation, write value to the register */
- if(ENET_PHY_WRITE == direction){
- ENET_MAC_PHY_DATA = *pvalue;
- }
- /* do PHY write/read operation, and wait the operation complete */
- ENET_MAC_PHY_CTL = reg;
- do{
- phy_flag = (ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB);
- timeout++;
- }
- while((RESET != phy_flag) && (ENET_DELAY_TO != timeout));
- /* write/read operation complete */
- if(RESET == (ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB)){
- enet_state = SUCCESS;
- }
- /* if do the read operation, get value from the register */
- if(ENET_PHY_READ == direction){
- *pvalue = (uint16_t)ENET_MAC_PHY_DATA;
- }
- return enet_state;
- }
- /*!
- \brief enable the loopback function of PHY chip
- \param[in] none
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_phyloopback_enable(void)
- {
- uint16_t temp_phy = 0U;
- ErrStatus phy_state = ERROR;
- /* get the PHY configuration to update it */
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
- /* enable the PHY loopback mode */
- temp_phy |= PHY_LOOPBACK;
- /* update the PHY control register with the new configuration */
- phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
- return phy_state;
- }
- /*!
- \brief disable the loopback function of PHY chip
- \param[in] none
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_phyloopback_disable(void)
- {
- uint16_t temp_phy = 0U;
- ErrStatus phy_state = ERROR;
- /* get the PHY configuration to update it */
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
- /* disable the PHY loopback mode */
- temp_phy &= (uint16_t)~PHY_LOOPBACK;
- /* update the PHY control register with the new configuration */
- phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
- return phy_state;
- }
- /*!
- \brief enable ENET forward feature
- \param[in] feature: the feature of ENET forward mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_AUTO_PADCRC_DROP: the function of the MAC strips the Pad/FCS field on received frames
- \arg ENET_TYPEFRAME_CRC_DROP: the function that FCS field(last 4 bytes) of frame will be dropped before forwarding
- \arg ENET_FORWARD_ERRFRAMES: the function that all frame received with error except runt error are forwarded to memory
- \arg ENET_FORWARD_UNDERSZ_GOODFRAMES: the function that forwarding undersized good frames
- \param[out] none
- \retval none
- */
- void enet_forward_feature_enable(uint32_t feature)
- {
- uint32_t mask;
- mask = (feature & (~(ENET_FORWARD_ERRFRAMES | ENET_FORWARD_UNDERSZ_GOODFRAMES)));
- ENET_MAC_CFG |= mask;
- mask = (feature & (~(ENET_AUTO_PADCRC_DROP | ENET_TYPEFRAME_CRC_DROP)));
- ENET_DMA_CTL |= (mask >> 2);
- }
- /*!
- \brief disable ENET forward feature
- \param[in] feature: the feature of ENET forward mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_AUTO_PADCRC_DROP: the function of the MAC strips the Pad/FCS field on received frames
- \arg ENET_TYPEFRAME_CRC_DROP: the function that FCS field(last 4 bytes) of frame will be dropped before forwarding
- \arg ENET_FORWARD_ERRFRAMES: the function that all frame received with error except runt error are forwarded to memory
- \arg ENET_FORWARD_UNDERSZ_GOODFRAMES: the function that forwarding undersized good frames
- \param[out] none
- \retval none
- */
- void enet_forward_feature_disable(uint32_t feature)
- {
- uint32_t mask;
- mask = (feature & (~(ENET_FORWARD_ERRFRAMES | ENET_FORWARD_UNDERSZ_GOODFRAMES)));
- ENET_MAC_CFG &= ~mask;
- mask = (feature & (~(ENET_AUTO_PADCRC_DROP | ENET_TYPEFRAME_CRC_DROP)));
- ENET_DMA_CTL &= ~(mask >> 2);
- }
-
- /*!
- \brief enable ENET fliter feature
- \param[in] feature: the feature of ENET fliter mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_SRC_FILTER: filter source address function
- \arg ENET_SRC_FILTER_INVERSE: inverse source address filtering result function
- \arg ENET_DEST_FILTER_INVERSE: inverse DA filtering result function
- \arg ENET_MULTICAST_FILTER_PASS: pass all multicast frames function
- \arg ENET_MULTICAST_FILTER_HASH_MODE: HASH multicast filter function
- \arg ENET_UNICAST_FILTER_HASH_MODE: HASH unicast filter function
- \arg ENET_FILTER_MODE_EITHER: HASH or perfect filter function
- \param[out] none
- \retval none
- */
- void enet_fliter_feature_enable(uint32_t feature)
- {
- ENET_MAC_FRMF |= feature;
- }
- /*!
- \brief disable ENET fliter feature
- \param[in] feature: the feature of ENET fliter mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_SRC_FILTER: filter source address function
- \arg ENET_SRC_FILTER_INVERSE: inverse source address filtering result function
- \arg ENET_DEST_FILTER_INVERSE: inverse DA filtering result function
- \arg ENET_MULTICAST_FILTER_PASS: pass all multicast frames function
- \arg ENET_MULTICAST_FILTER_HASH_MODE: HASH multicast filter function
- \arg ENET_UNICAST_FILTER_HASH_MODE: HASH unicast filter function
- \arg ENET_FILTER_MODE_EITHER: HASH or perfect filter function
- \param[out] none
- \retval none
- */
- void enet_fliter_feature_disable(uint32_t feature)
- {
- ENET_MAC_FRMF &= ~feature;
- }
- /*!
- \brief generate the pause frame, ENET will send pause frame after enable transmit flow control
- this function only use in full-dulex mode
- \param[in] none
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_pauseframe_generate(void)
- {
- ErrStatus enet_state =ERROR;
- uint32_t temp = 0U;
- /* in full-duplex mode, must make sure this bit is 0 before writing register */
- temp = ENET_MAC_FCTL & ENET_MAC_FCTL_FLCBBKPA;
- if(RESET == temp){
- ENET_MAC_FCTL |= ENET_MAC_FCTL_FLCBBKPA;
- enet_state = SUCCESS;
- }
- return enet_state;
- }
- /*!
- \brief configure the pause frame detect type
- \param[in] detect: pause frame detect type,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC0_AND_UNIQUE_ADDRESS_PAUSEDETECT: besides the unique multicast address, MAC can also
- use the MAC0 address to detecting pause frame
- \arg ENET_UNIQUE_PAUSEDETECT: only the unique multicast address for pause frame which is specified
- in IEEE802.3 can be detected
- \param[out] none
- \retval none
- */
- void enet_pauseframe_detect_config(uint32_t detect)
- {
- ENET_MAC_FCTL &= ~ENET_MAC_FCTL_UPFDT;
- ENET_MAC_FCTL |= detect;
- }
- /*!
- \brief configure the pause frame parameters
- \param[in] pausetime: pause time in transmit pause control frame
- \param[in] pause_threshold: the threshold of the pause timer for retransmitting frames automatically,
- this value must make sure to be less than configured pause time, only one parameter can be
- selected which is shown as below
- \arg ENET_PAUSETIME_MINUS4: pause time minus 4 slot times
- \arg ENET_PAUSETIME_MINUS28: pause time minus 28 slot times
- \arg ENET_PAUSETIME_MINUS144: pause time minus 144 slot times
- \arg ENET_PAUSETIME_MINUS256: pause time minus 256 slot times
- \param[out] none
- \retval none
- */
- void enet_pauseframe_config(uint32_t pausetime, uint32_t pause_threshold)
- {
- ENET_MAC_FCTL &= ~(ENET_MAC_FCTL_PTM | ENET_MAC_FCTL_PLTS);
- ENET_MAC_FCTL |= (MAC_FCTL_PTM(pausetime) | pause_threshold);
- }
- /*!
- \brief configure the threshold of the flow control(deactive and active threshold)
- \param[in] deactive: the threshold of the deactive flow control, this value
- should always be less than active flow control value, only one
- parameter can be selected which is shown as below
- \arg ENET_DEACTIVE_THRESHOLD_256BYTES: threshold level is 256 bytes
- \arg ENET_DEACTIVE_THRESHOLD_512BYTES: threshold level is 512 bytes
- \arg ENET_DEACTIVE_THRESHOLD_768BYTES: threshold level is 768 bytes
- \arg ENET_DEACTIVE_THRESHOLD_1024BYTES: threshold level is 1024 bytes
- \arg ENET_DEACTIVE_THRESHOLD_1280BYTES: threshold level is 1280 bytes
- \arg ENET_DEACTIVE_THRESHOLD_1536BYTES: threshold level is 1536 bytes
- \arg ENET_DEACTIVE_THRESHOLD_1792BYTES: threshold level is 1792 bytes
- \param[in] active: the threshold of the active flow control, only one parameter
- can be selected which is shown as below
- \arg ENET_ACTIVE_THRESHOLD_256BYTES: threshold level is 256 bytes
- \arg ENET_ACTIVE_THRESHOLD_512BYTES: threshold level is 512 bytes
- \arg ENET_ACTIVE_THRESHOLD_768BYTES: threshold level is 768 bytes
- \arg ENET_ACTIVE_THRESHOLD_1024BYTES: threshold level is 1024 bytes
- \arg ENET_ACTIVE_THRESHOLD_1280BYTES: threshold level is 1280 bytes
- \arg ENET_ACTIVE_THRESHOLD_1536BYTES: threshold level is 1536 bytes
- \arg ENET_ACTIVE_THRESHOLD_1792BYTES: threshold level is 1792 bytes
- \param[out] none
- \retval none
- */
- void enet_flowcontrol_threshold_config(uint32_t deactive, uint32_t active)
- {
- ENET_MAC_FCTH = ((deactive | active) >> 8);
- }
- /*!
- \brief enable ENET flow control feature
- \param[in] feature: the feature of ENET flow control mode
- one or more parameters can be selected which are shown as below
- \arg ENET_ZERO_QUANTA_PAUSE: the automatic zero-quanta generation function
- \arg ENET_TX_FLOWCONTROL: the flow control operation in the MAC
- \arg ENET_RX_FLOWCONTROL: decoding function for the received pause frame and process it
- \arg ENET_BACK_PRESSURE: back pressure operation in the MAC(only use in half-dulex mode)
- \param[out] none
- \retval none
- */
- void enet_flowcontrol_feature_enable(uint32_t feature)
- {
- if(RESET != (feature & ENET_ZERO_QUANTA_PAUSE)){
- ENET_MAC_FCTL &= ~ENET_ZERO_QUANTA_PAUSE;
- }
- feature &= ~ENET_ZERO_QUANTA_PAUSE;
- ENET_MAC_FCTL |= feature;
- }
- /*!
- \brief disable ENET flow control feature
- \param[in] feature: the feature of ENET flow control mode
- one or more parameters can be selected which are shown as below
- \arg ENET_ZERO_QUANTA_PAUSE: the automatic zero-quanta generation function
- \arg ENET_TX_FLOWCONTROL: the flow control operation in the MAC
- \arg ENET_RX_FLOWCONTROL: decoding function for the received pause frame and process it
- \arg ENET_BACK_PRESSURE: back pressure operation in the MAC(only use in half-dulex mode)
- \param[out] none
- \retval none
- */
- void enet_flowcontrol_feature_disable(uint32_t feature)
- {
- if(RESET != (feature & ENET_ZERO_QUANTA_PAUSE)){
- ENET_MAC_FCTL |= ENET_ZERO_QUANTA_PAUSE;
- }
- feature &= ~ENET_ZERO_QUANTA_PAUSE;
- ENET_MAC_FCTL &= ~feature;
- }
- /*!
- \brief get the dma transmit/receive process state
- \param[in] direction: choose the direction of dma process which users want to check,
- refer to enet_dmadirection_enum, only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: dma transmit process
- \arg ENET_DMA_RX: dma receive process
- \param[out] none
- \retval state of dma process, the value range shows below:
- ENET_RX_STATE_STOPPED, ENET_RX_STATE_FETCHING, ENET_RX_STATE_WAITING,
- ENET_RX_STATE_SUSPENDED, ENET_RX_STATE_CLOSING, ENET_RX_STATE_QUEUING,
- ENET_TX_STATE_STOPPED, ENET_TX_STATE_FETCHING, ENET_TX_STATE_WAITING,
- ENET_TX_STATE_READING, ENET_TX_STATE_SUSPENDED, ENET_TX_STATE_CLOSING
- */
- uint32_t enet_dmaprocess_state_get(enet_dmadirection_enum direction)
- {
- uint32_t reval;
- reval = (uint32_t)(ENET_DMA_STAT & (uint32_t)direction);
- return reval;
- }
- /*!
- \brief poll the DMA transmission/reception enable by writing any value to the
- ENET_DMA_TPEN/ENET_DMA_RPEN register, this will make the DMA to resume transmission/reception
- \param[in] direction: choose the direction of DMA process which users want to resume,
- refer to enet_dmadirection_enum, only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA transmit process
- \arg ENET_DMA_RX: DMA receive process
- \param[out] none
- \retval none
- */
- void enet_dmaprocess_resume(enet_dmadirection_enum direction)
- {
- if(ENET_DMA_TX == direction){
- ENET_DMA_TPEN = 0U;
- }else{
- ENET_DMA_RPEN = 0U;
- }
- }
- /*!
- \brief check and recover the Rx process
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_rxprocess_check_recovery(void)
- {
- uint32_t status;
- /* get DAV information of current RxDMA descriptor */
- status = dma_current_rxdesc->status;
- status &= ENET_RDES0_DAV;
- /* if current descriptor is owned by DMA, but the descriptor address mismatches with
- receive descriptor address pointer updated by RxDMA controller */
- if((ENET_DMA_CRDADDR != ((uint32_t)dma_current_rxdesc)) &&
- (ENET_RDES0_DAV == status)){
- dma_current_rxdesc = (enet_descriptors_struct*)ENET_DMA_CRDADDR;
- }
- }
- /*!
- \brief flush the ENET transmit FIFO, and wait until the flush operation completes
- \param[in] none
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_txfifo_flush(void)
- {
- uint32_t flush_state;
- uint32_t timeout = 0U;
- ErrStatus enet_state = ERROR;
- /* set the FTF bit for flushing transmit FIFO */
- ENET_DMA_CTL |= ENET_DMA_CTL_FTF;
- /* wait until the flush operation completes */
- do{
- flush_state = ENET_DMA_CTL & ENET_DMA_CTL_FTF;
- timeout++;
- }while((RESET != flush_state) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(RESET == flush_state){
- enet_state = SUCCESS;
- }
- return enet_state;
- }
- /*!
- \brief get the transmit/receive address of current descriptor, or current buffer, or descriptor table
- \param[in] addr_get: choose the address which users want to get, refer to enet_desc_reg_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_RX_DESC_TABLE: the start address of the receive descriptor table
- \arg ENET_RX_CURRENT_DESC: the start descriptor address of the current receive descriptor read by
- the RxDMA controller
- \arg ENET_RX_CURRENT_BUFFER: the current receive buffer address being read by the RxDMA controller
- \arg ENET_TX_DESC_TABLE: the start address of the transmit descriptor table
- \arg ENET_TX_CURRENT_DESC: the start descriptor address of the current transmit descriptor read by
- the TxDMA controller
- \arg ENET_TX_CURRENT_BUFFER: the current transmit buffer address being read by the TxDMA controller
- \param[out] none
- \retval address value
- */
- uint32_t enet_current_desc_address_get(enet_desc_reg_enum addr_get)
- {
- uint32_t reval = 0U;
- reval = REG32((ENET) +(uint32_t)addr_get);
- return reval;
- }
- /*!
- \brief get the Tx or Rx descriptor information
- \param[in] desc: the descriptor pointer which users want to get information
- \param[in] info_get: the descriptor information type which is selected,
- only one parameter can be selected which is shown as below
- \arg RXDESC_BUFFER_1_SIZE: receive buffer 1 size
- \arg RXDESC_BUFFER_2_SIZE: receive buffer 2 size
- \arg RXDESC_FRAME_LENGTH: the byte length of the received frame that was transferred to the buffer
- \arg TXDESC_COLLISION_COUNT: the number of collisions occurred before the frame was transmitted
- \arg RXDESC_BUFFER_1_ADDR: the buffer1 address of the Rx frame
- \arg TXDESC_BUFFER_1_ADDR: the buffer1 address of the Tx frame
- \param[out] none
- \retval descriptor information, if value is 0xFFFFFFFFU, means the false input parameter
- */
- uint32_t enet_desc_information_get(enet_descriptors_struct *desc, enet_descstate_enum info_get)
- {
- uint32_t reval = 0xFFFFFFFFU;
- switch(info_get){
- case RXDESC_BUFFER_1_SIZE:
- reval = GET_RDES1_RB1S(desc->control_buffer_size);
- break;
- case RXDESC_BUFFER_2_SIZE:
- reval = GET_RDES1_RB2S(desc->control_buffer_size);
- break;
- case RXDESC_FRAME_LENGTH:
- reval = GET_RDES0_FRML(desc->status);
- if(reval > 4U){
- reval = reval - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (desc->status & ENET_RDES0_FRMT))){
- reval = reval + 4U;
- }
- }else{
- reval = 0U;
- }
- break;
- case RXDESC_BUFFER_1_ADDR:
- reval = desc->buffer1_addr;
- break;
- case TXDESC_BUFFER_1_ADDR:
- reval = desc->buffer1_addr;
- break;
- case TXDESC_COLLISION_COUNT:
- reval = GET_TDES0_COCNT(desc->status);
- break;
- default:
- break;
- }
- return reval;
- }
- /*!
- \brief get the number of missed frames during receiving
- \param[in] none
- \param[out] rxfifo_drop: pointer to the number of frames dropped by RxFIFO
- \param[out] rxdma_drop: pointer to the number of frames missed by the RxDMA controller
- \retval none
- */
- void enet_missed_frame_counter_get(uint32_t *rxfifo_drop, uint32_t *rxdma_drop)
- {
- uint32_t temp_counter = 0U;
- temp_counter = ENET_DMA_MFBOCNT;
- *rxfifo_drop = GET_DMA_MFBOCNT_MSFA(temp_counter);
- *rxdma_drop = GET_DMA_MFBOCNT_MSFC(temp_counter);
- }
- /*!
- \brief get the bit flag of ENET DMA descriptor
- \param[in] desc: the descriptor pointer which users want to get flag
- \param[in] desc_flag: the bit flag of ENET DMA descriptor,
- only one parameter can be selected which is shown as below
- \arg ENET_TDES0_DB: deferred
- \arg ENET_TDES0_UFE: underflow error
- \arg ENET_TDES0_EXD: excessive deferral
- \arg ENET_TDES0_VFRM: VLAN frame
- \arg ENET_TDES0_ECO: excessive collision
- \arg ENET_TDES0_LCO: late collision
- \arg ENET_TDES0_NCA: no carrier
- \arg ENET_TDES0_LCA: loss of carrier
- \arg ENET_TDES0_IPPE: IP payload error
- \arg ENET_TDES0_FRMF: frame flushed
- \arg ENET_TDES0_JT: jabber timeout
- \arg ENET_TDES0_ES: error summary
- \arg ENET_TDES0_IPHE: IP header error
- \arg ENET_TDES0_TTMSS: transmit timestamp status
- \arg ENET_TDES0_TCHM: the second address chained mode
- \arg ENET_TDES0_TERM: transmit end of ring mode
- \arg ENET_TDES0_TTSEN: transmit timestamp function enable
- \arg ENET_TDES0_DPAD: disable adding pad
- \arg ENET_TDES0_DCRC: disable CRC
- \arg ENET_TDES0_FSG: first segment
- \arg ENET_TDES0_LSG: last segment
- \arg ENET_TDES0_INTC: interrupt on completion
- \arg ENET_TDES0_DAV: DAV bit
-
- \arg ENET_RDES0_PCERR: payload checksum error
- \arg ENET_RDES0_EXSV: extended status valid
- \arg ENET_RDES0_CERR: CRC error
- \arg ENET_RDES0_DBERR: dribble bit error
- \arg ENET_RDES0_RERR: receive error
- \arg ENET_RDES0_RWDT: receive watchdog timeout
- \arg ENET_RDES0_FRMT: frame type
- \arg ENET_RDES0_LCO: late collision
- \arg ENET_RDES0_IPHERR: IP frame header error
- \arg ENET_RDES0_TSV: timestamp valid
- \arg ENET_RDES0_LDES: last descriptor
- \arg ENET_RDES0_FDES: first descriptor
- \arg ENET_RDES0_VTAG: VLAN tag
- \arg ENET_RDES0_OERR: overflow error
- \arg ENET_RDES0_LERR: length error
- \arg ENET_RDES0_SAFF: SA filter fail
- \arg ENET_RDES0_DERR: descriptor error
- \arg ENET_RDES0_ERRS: error summary
- \arg ENET_RDES0_DAFF: destination address filter fail
- \arg ENET_RDES0_DAV: descriptor available
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus enet_desc_flag_get(enet_descriptors_struct *desc, uint32_t desc_flag)
- {
- FlagStatus enet_flag = RESET;
-
- if((uint32_t)RESET != (desc->status & desc_flag)){
- enet_flag = SET;
- }
- return enet_flag;
- }
- /*!
- \brief set the bit flag of ENET DMA descriptor
- \param[in] desc: the descriptor pointer which users want to set flag
- \param[in] desc_flag: the bit flag of ENET DMA descriptor,
- only one parameter can be selected which is shown as below
- \arg ENET_TDES0_VFRM: VLAN frame
- \arg ENET_TDES0_FRMF: frame flushed
- \arg ENET_TDES0_TCHM: the second address chained mode
- \arg ENET_TDES0_TERM: transmit end of ring mode
- \arg ENET_TDES0_TTSEN: transmit timestamp function enable
- \arg ENET_TDES0_DPAD: disable adding pad
- \arg ENET_TDES0_DCRC: disable CRC
- \arg ENET_TDES0_FSG: first segment
- \arg ENET_TDES0_LSG: last segment
- \arg ENET_TDES0_INTC: interrupt on completion
- \arg ENET_TDES0_DAV: DAV bit
- \arg ENET_RDES0_DAV: descriptor available
- \param[out] none
- \retval none
- */
- void enet_desc_flag_set(enet_descriptors_struct *desc, uint32_t desc_flag)
- {
- desc->status |= desc_flag;
- }
- /*!
- \brief clear the bit flag of ENET DMA descriptor
- \param[in] desc: the descriptor pointer which users want to clear flag
- \param[in] desc_flag: the bit flag of ENET DMA descriptor,
- only one parameter can be selected which is shown as below
- \arg ENET_TDES0_VFRM: VLAN frame
- \arg ENET_TDES0_FRMF: frame flushed
- \arg ENET_TDES0_TCHM: the second address chained mode
- \arg ENET_TDES0_TERM: transmit end of ring mode
- \arg ENET_TDES0_TTSEN: transmit timestamp function enable
- \arg ENET_TDES0_DPAD: disable adding pad
- \arg ENET_TDES0_DCRC: disable CRC
- \arg ENET_TDES0_FSG: first segment
- \arg ENET_TDES0_LSG: last segment
- \arg ENET_TDES0_INTC: interrupt on completion
- \arg ENET_TDES0_DAV: DAV bit
- \arg ENET_RDES0_DAV: descriptor available
- \param[out] none
- \retval none
- */
- void enet_desc_flag_clear(enet_descriptors_struct *desc, uint32_t desc_flag)
- {
- desc->status &= ~desc_flag;
- }
- /*!
- \brief when receiving completed, set RS bit in ENET_DMA_STAT register will immediately set
- \param[in] desc: the descriptor pointer which users want to configure
- \param[out] none
- \retval none
- */
- void enet_rx_desc_immediate_receive_complete_interrupt(enet_descriptors_struct *desc)
- {
- desc->control_buffer_size &= ~ENET_RDES1_DINTC;
- }
- /*!
- \brief when receiving completed, set RS bit in ENET_DMA_STAT register will is set after a configurable delay time
- \param[in] desc: the descriptor pointer which users want to configure
- \param[in] delay_time: delay a time of 256*delay_time HCLK, this value must be between 0 and 0xFF
- \param[out] none
- \retval none
- */
- void enet_rx_desc_delay_receive_complete_interrupt(enet_descriptors_struct *desc, uint32_t delay_time)
- {
- desc->control_buffer_size |= ENET_RDES1_DINTC;
- ENET_DMA_RSWDC = DMA_RSWDC_WDCFRS(delay_time);
- }
- /*!
- \brief drop current receive frame
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_rxframe_drop(void)
- {
- /* enable reception, descriptor is owned by DMA */
- dma_current_rxdesc->status = ENET_RDES0_DAV;
- /* chained mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
- if(NULL != dma_current_ptp_rxdesc){
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->buffer2_next_desc_addr);
- /* if it is the last ptp descriptor */
- if(0U != dma_current_ptp_rxdesc->status){
- /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
- dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
- }else{
- /* ponter to the next ptp descriptor */
- dma_current_ptp_rxdesc++;
- }
- }else{
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
- }
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
- if(NULL != dma_current_ptp_rxdesc){
- dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
- }
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- if(NULL != dma_current_ptp_rxdesc){
- dma_current_ptp_rxdesc++;
- }
- }
- }
- }
- /*!
- \brief enable DMA feature
- \param[in] feature: the feature of DMA mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_NO_FLUSH_RXFRAME: RxDMA does not flushes frames function
- \arg ENET_SECONDFRAME_OPT: TxDMA controller operate on second frame function
- \param[out] none
- \retval none
- */
- void enet_dma_feature_enable(uint32_t feature)
- {
- ENET_DMA_CTL |= feature;
- }
- /*!
- \brief disable DMA feature
- \param[in] feature: the feature of DMA mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_NO_FLUSH_RXFRAME: RxDMA does not flushes frames function
- \arg ENET_SECONDFRAME_OPT: TxDMA controller operate on second frame function
- \param[out] none
- \retval none
- */
- void enet_dma_feature_disable(uint32_t feature)
- {
- ENET_DMA_CTL &= ~feature;
- }
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- /*!
- \brief get the bit of extended status flag in ENET DMA descriptor
- \param[in] desc: the descriptor pointer which users want to get the extended status flag
- \param[in] desc_status: the extended status want to get,
- only one parameter can be selected which is shown as below
- \arg ENET_RDES4_IPPLDT: IP frame payload type
- \arg ENET_RDES4_IPHERR: IP frame header error
- \arg ENET_RDES4_IPPLDERR: IP frame payload error
- \arg ENET_RDES4_IPCKSB: IP frame checksum bypassed
- \arg ENET_RDES4_IPF4: IP frame in version 4
- \arg ENET_RDES4_IPF6: IP frame in version 6
- \arg ENET_RDES4_PTPMT: PTP message type
- \arg ENET_RDES4_PTPOEF: PTP on ethernet frame
- \arg ENET_RDES4_PTPVF: PTP version format
- \param[out] none
- \retval value of extended status
- */
- uint32_t enet_rx_desc_enhanced_status_get(enet_descriptors_struct *desc, uint32_t desc_status)
- {
- uint32_t reval = 0xFFFFFFFFU;
-
- switch (desc_status){
- case ENET_RDES4_IPPLDT:
- reval = GET_RDES4_IPPLDT(desc->extended_status);
- break;
- case ENET_RDES4_PTPMT:
- reval = GET_RDES4_PTPMT(desc->extended_status);
- break;
- default:
- if ((uint32_t)RESET != (desc->extended_status & desc_status)){
- reval = 1U;
- }else{
- reval = 0U;
- }
- }
-
- return reval;
- }
- /*!
- \brief configure descriptor to work in enhanced mode
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_desc_select_enhanced_mode(void)
- {
- ENET_DMA_BCTL |= ENET_DMA_BCTL_DFM;
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in enhanced chain mode with ptp function
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[out] none
- \retval none
- */
- void enet_ptp_enhanced_descriptors_chain_init(enet_dmadirection_enum direction)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc, *desc_tab;
- uint8_t *buf;
-
- /* if want to initialize DMA Tx descriptors */
- if (ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
-
- /* select chain mode, and enable transmit timestamp function */
- desc_status = ENET_TDES0_TCHM | ENET_TDES0_TTSEN;
-
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
-
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* select receive chained mode and set buffer1 size */
- desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
-
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- }
-
- /* configuration each descriptor */
- for(num = 0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
-
- /* if is not the last descriptor */
- if(num < (count - 1U)){
- /* configure the next descriptor address */
- desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
- }else{
- /* when it is the last descriptor, the next descriptor address
- equals to first descriptor address in descriptor table */
- desc->buffer2_next_desc_addr = (uint32_t)desc_tab;
- }
- }
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in enhanced ring mode with ptp function
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[out] none
- \retval none
- */
- void enet_ptp_enhanced_descriptors_ring_init(enet_dmadirection_enum direction)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc;
- enet_descriptors_struct *desc_tab;
- uint8_t *buf;
-
- /* configure descriptor skip length */
- ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
- ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
-
- /* if want to initialize DMA Tx descriptors */
- if (ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* select ring mode, and enable transmit timestamp function */
- desc_status = ENET_TDES0_TTSEN;
-
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
-
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* set buffer1 size */
- desc_bufsize = ENET_RXBUF_SIZE;
-
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- }
-
- /* configure each descriptor */
- for(num=0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
-
- /* when it is the last descriptor */
- if(num == (count - 1U)){
- if (ENET_DMA_TX == direction){
- /* configure transmit end of ring mode */
- desc->status |= ENET_TDES0_TERM;
- }else{
- /* configure receive end of ring mode */
- desc->control_buffer_size |= ENET_RDES1_RERM;
- }
- }
- }
- }
- /*!
- \brief receive a packet data with timestamp values to application buffer, when the DMA is in enhanced mode
- \param[in] bufsize: the size of buffer which is the parameter in function
- \param[out] buffer: pointer to the application buffer
- note -- if the input is NULL, user should copy data in application by himself
- \param[out] timestamp: pointer to the table which stores the timestamp high and low
- note -- if the input is NULL, timestamp is ignored
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptpframe_receive_enhanced_mode(uint8_t *buffer, uint32_t bufsize, uint32_t timestamp[])
- {
- uint32_t offset = 0U, size = 0U;
- uint32_t timeout = 0U;
- uint32_t rdes0_tsv_flag;
-
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
- return ERROR;
- }
-
- /* if buffer pointer is null, indicates that users has copied data in application */
- if(NULL != buffer){
- /* if no error occurs, and the frame uses only one descriptor */
- if(((uint32_t)RESET == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
- ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
- ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
- /* get the frame length except CRC */
- size = GET_RDES0_FRML(dma_current_rxdesc->status) - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
- size = size + 4U;
- }
-
- /* to avoid situation that the frame size exceeds the buffer length */
- if(size > bufsize){
- return ERROR;
- }
- /* copy data from Rx buffer to application buffer */
- for(offset = 0; offset < size; offset++){
- (*(buffer + offset)) = (*(__IO uint8_t *)((dma_current_rxdesc->buffer1_addr) + offset));
- }
- }else{
- return ERROR;
- }
- }
-
- /* if timestamp pointer is null, indicates that users don't care timestamp in application */
- if(NULL != timestamp){
- /* wait for ENET_RDES0_TSV flag to be set, the timestamp value is taken and
- write to the RDES6 and RDES7 */
- do{
- rdes0_tsv_flag = (dma_current_rxdesc->status & ENET_RDES0_TSV);
- timeout++;
- }while ((RESET == rdes0_tsv_flag) && (timeout < ENET_DELAY_TO));
-
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- return ERROR;
- }
-
- /* clear the ENET_RDES0_TSV flag */
- dma_current_rxdesc->status &= ~ENET_RDES0_TSV;
- /* get the timestamp value of the received frame */
- timestamp[0] = dma_current_rxdesc->timestamp_low;
- timestamp[1] = dma_current_rxdesc->timestamp_high;
- }
- /* enable reception, descriptor is owned by DMA */
- dma_current_rxdesc->status = ENET_RDES0_DAV;
-
- /* check Rx buffer unavailable flag status */
- if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
- /* Clear RBU flag */
- ENET_DMA_STAT = ENET_DMA_STAT_RBU;
- /* resume DMA reception by writing to the RPEN register*/
- ENET_DMA_RPEN = 0;
- }
-
- /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
- /* chained mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_rxdesc = (enet_descriptors_struct*) ((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- }
- }
-
- return SUCCESS;
- }
- /*!
- \brief send data with timestamp values in application buffer as a transmit packet, when the DMA is in enhanced mode
- \param[in] buffer: pointer on the application buffer
- note -- if the input is NULL, user should copy data in application by himself
- \param[in] length: the length of frame data to be transmitted
- \param[out] timestamp: pointer to the table which stores the timestamp high and low
- note -- if the input is NULL, timestamp is ignored
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptpframe_transmit_enhanced_mode(uint8_t *buffer, uint32_t length, uint32_t timestamp[])
- {
- uint32_t offset = 0;
- uint32_t dma_tbu_flag, dma_tu_flag;
- uint32_t tdes0_ttmss_flag;
- uint32_t timeout = 0;
-
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
- return ERROR;
- }
-
- /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
- if(length > ENET_MAX_FRAME_SIZE){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has handled data in application */
- if(NULL != buffer){
- /* copy frame data from application buffer to Tx buffer */
- for(offset = 0; offset < length; offset++){
- (*(__IO uint8_t *)((dma_current_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
- }
- }
- /* set the frame length */
- dma_current_txdesc->control_buffer_size = length;
- /* set the segment of frame, frame is transmitted in one descriptor */
- dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
- /* enable the DMA transmission */
- dma_current_txdesc->status |= ENET_TDES0_DAV;
- /* check Tx buffer unavailable flag status */
- dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
- dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
-
- if ((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
- /* Clear TBU and TU flag */
- ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
- /* resume DMA transmission by writing to the TPEN register*/
- ENET_DMA_TPEN = 0;
- }
-
- /* if timestamp pointer is null, indicates that users don't care timestamp in application */
- if(NULL != timestamp){
- /* wait for ENET_TDES0_TTMSS flag to be set, a timestamp was captured */
- do{
- tdes0_ttmss_flag = (dma_current_txdesc->status & ENET_TDES0_TTMSS);
- timeout++;
- }while((RESET == tdes0_ttmss_flag) && (timeout < ENET_DELAY_TO));
-
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- return ERROR;
- }
-
- /* clear the ENET_TDES0_TTMSS flag */
- dma_current_txdesc->status &= ~ENET_TDES0_TTMSS;
- /* get the timestamp value of the transmit frame */
- timestamp[0] = dma_current_txdesc->timestamp_low;
- timestamp[1] = dma_current_txdesc->timestamp_high;
- }
- /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table*/
- /* chained mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
- dma_current_txdesc = (enet_descriptors_struct*) (dma_current_txdesc->buffer2_next_desc_addr);
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_txdesc = (enet_descriptors_struct*) ((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- }
- }
- return SUCCESS;
- }
- #else
- /*!
- \brief configure descriptor to work in normal mode
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_desc_select_normal_mode(void)
- {
- ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DFM;
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in normal chain mode with PTP function
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[in] desc_ptptab: pointer to the first descriptor address of PTP Rx descriptor table
- \param[out] none
- \retval none
- */
- void enet_ptp_normal_descriptors_chain_init(enet_dmadirection_enum direction, enet_descriptors_struct *desc_ptptab)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc, *desc_tab;
- uint8_t *buf;
- /* if want to initialize DMA Tx descriptors */
- if(ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* select chain mode, and enable transmit timestamp function */
- desc_status = ENET_TDES0_TCHM | ENET_TDES0_TTSEN;
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- dma_current_ptp_txdesc = desc_ptptab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* select receive chained mode and set buffer1 size */
- desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- dma_current_ptp_rxdesc = desc_ptptab;
- }
- /* configure each descriptor */
- for(num = 0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
-
- /* if is not the last descriptor */
- if(num < (count - 1U)){
- /* configure the next descriptor address */
- desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
- }else{
- /* when it is the last descriptor, the next descriptor address
- equals to first descriptor address in descriptor table */
- desc->buffer2_next_desc_addr = (uint32_t)desc_tab;
- }
- /* set desc_ptptab equal to desc_tab */
- (&desc_ptptab[num])->buffer1_addr = desc->buffer1_addr;
- (&desc_ptptab[num])->buffer2_next_desc_addr = desc->buffer2_next_desc_addr;
- }
- /* when it is the last ptp descriptor, preserve the first descriptor
- address of desc_ptptab in ptp descriptor status */
- (&desc_ptptab[num-1U])->status = (uint32_t)desc_ptptab;
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in normal ring mode with PTP function
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[in] desc_ptptab: pointer to the first descriptor address of PTP Rx descriptor table
- \param[out] none
- \retval none
- */
- void enet_ptp_normal_descriptors_ring_init(enet_dmadirection_enum direction, enet_descriptors_struct *desc_ptptab)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc, *desc_tab;
- uint8_t *buf;
- /* configure descriptor skip length */
- ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
- ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
- /* if want to initialize DMA Tx descriptors */
- if(ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* select ring mode, and enable transmit timestamp function */
- desc_status = ENET_TDES0_TTSEN;
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- dma_current_ptp_txdesc = desc_ptptab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* select receive ring mode and set buffer1 size */
- desc_bufsize = (uint32_t)ENET_RXBUF_SIZE;
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- dma_current_ptp_rxdesc = desc_ptptab;
- }
- /* configure each descriptor */
- for(num = 0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
- /* when it is the last descriptor */
- if(num == (count - 1U)){
- if (ENET_DMA_TX == direction){
- /* configure transmit end of ring mode */
- desc->status |= ENET_TDES0_TERM;
- }else{
- /* configure receive end of ring mode */
- desc->control_buffer_size |= ENET_RDES1_RERM;
- }
- }
- /* set desc_ptptab equal to desc_tab */
- (&desc_ptptab[num])->buffer1_addr = desc->buffer1_addr;
- (&desc_ptptab[num])->buffer2_next_desc_addr = desc->buffer2_next_desc_addr;
- }
- /* when it is the last ptp descriptor, preserve the first descriptor
- address of desc_ptptab in ptp descriptor status */
- (&desc_ptptab[num-1U])->status = (uint32_t)desc_ptptab;
- }
- /*!
- \brief receive a packet data with timestamp values to application buffer, when the DMA is in normal mode
- \param[in] bufsize: the size of buffer which is the parameter in function
- \param[out] timestamp: pointer to the table which stores the timestamp high and low
- \param[out] buffer: pointer to the application buffer
- note -- if the input is NULL, user should copy data in application by himself
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptpframe_receive_normal_mode(uint8_t *buffer, uint32_t bufsize, uint32_t timestamp[])
- {
- uint32_t offset = 0U, size = 0U;
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has copied data in application */
- if(NULL != buffer){
- /* if no error occurs, and the frame uses only one descriptor */
- if(((uint32_t)RESET == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
- ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
- ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
- /* get the frame length except CRC */
- size = GET_RDES0_FRML(dma_current_rxdesc->status) - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
- size = size + 4U;
- }
- /* to avoid situation that the frame size exceeds the buffer length */
- if(size > bufsize){
- return ERROR;
- }
- /* copy data from Rx buffer to application buffer */
- for(offset = 0U; offset < size; offset++){
- (*(buffer + offset)) = (*(__IO uint8_t *)(uint32_t)((dma_current_ptp_rxdesc->buffer1_addr) + offset));
- }
- }else{
- return ERROR;
- }
- }
- /* copy timestamp value from Rx descriptor to application array */
- timestamp[0] = dma_current_rxdesc->buffer1_addr;
- timestamp[1] = dma_current_rxdesc->buffer2_next_desc_addr;
- dma_current_rxdesc->buffer1_addr = dma_current_ptp_rxdesc ->buffer1_addr ;
- dma_current_rxdesc->buffer2_next_desc_addr = dma_current_ptp_rxdesc ->buffer2_next_desc_addr;
- /* enable reception, descriptor is owned by DMA */
- dma_current_rxdesc->status = ENET_RDES0_DAV;
-
- /* check Rx buffer unavailable flag status */
- if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
- /* clear RBU flag */
- ENET_DMA_STAT = ENET_DMA_STAT_RBU;
- /* resume DMA reception by writing to the RPEN register*/
- ENET_DMA_RPEN = 0U;
- }
- /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
- /* chained mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->buffer2_next_desc_addr);
- /* if it is the last ptp descriptor */
- if(0U != dma_current_ptp_rxdesc->status){
- /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
- dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
- }else{
- /* ponter to the next ptp descriptor */
- dma_current_ptp_rxdesc++;
- }
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
- /* RDES2 and RDES3 will not be covered by buffer address, so do not need to preserve a new table,
- use the same table with RxDMA descriptor */
- dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- dma_current_ptp_rxdesc ++;
- }
- }
- return SUCCESS;
- }
- /*!
- \brief send data with timestamp values in application buffer as a transmit packet, when the DMA is in normal mode
- \param[in] buffer: pointer on the application buffer
- note -- if the input is NULL, user should copy data in application by himself
- \param[in] length: the length of frame data to be transmitted
- \param[out] timestamp: pointer to the table which stores the timestamp high and low
- note -- if the input is NULL, timestamp is ignored
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptpframe_transmit_normal_mode(uint8_t *buffer, uint32_t length, uint32_t timestamp[])
- {
- uint32_t offset = 0U, timeout = 0U;
- uint32_t dma_tbu_flag, dma_tu_flag, tdes0_ttmss_flag;
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
- return ERROR;
- }
- /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
- if(length > ENET_MAX_FRAME_SIZE){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has handled data in application */
- if(NULL != buffer){
- /* copy frame data from application buffer to Tx buffer */
- for(offset = 0U; offset < length; offset++){
- (*(__IO uint8_t *) (uint32_t)((dma_current_ptp_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
- }
- }
- /* set the frame length */
- dma_current_txdesc->control_buffer_size = (length & (uint32_t)0x1FFF);
- /* set the segment of frame, frame is transmitted in one descriptor */
- dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
- /* enable the DMA transmission */
- dma_current_txdesc->status |= ENET_TDES0_DAV;
- /* check Tx buffer unavailable flag status */
- dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
- dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
- if((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
- /* clear TBU and TU flag */
- ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
- /* resume DMA transmission by writing to the TPEN register*/
- ENET_DMA_TPEN = 0U;
- }
- /* if timestamp pointer is null, indicates that users don't care timestamp in application */
- if(NULL != timestamp){
- /* wait for ENET_TDES0_TTMSS flag to be set, a timestamp was captured */
- do{
- tdes0_ttmss_flag = (dma_current_txdesc->status & ENET_TDES0_TTMSS);
- timeout++;
- }while((RESET == tdes0_ttmss_flag) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- return ERROR;
- }
- /* clear the ENET_TDES0_TTMSS flag */
- dma_current_txdesc->status &= ~ENET_TDES0_TTMSS;
- /* get the timestamp value of the transmit frame */
- timestamp[0] = dma_current_txdesc->buffer1_addr;
- timestamp[1] = dma_current_txdesc->buffer2_next_desc_addr;
- }
- dma_current_txdesc->buffer1_addr = dma_current_ptp_txdesc ->buffer1_addr ;
- dma_current_txdesc->buffer2_next_desc_addr = dma_current_ptp_txdesc ->buffer2_next_desc_addr;
- /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table */
- /* chained mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
- dma_current_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->buffer2_next_desc_addr);
- /* if it is the last ptp descriptor */
- if(0U != dma_current_ptp_txdesc->status){
- /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
- dma_current_ptp_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->status);
- }else{
- /* ponter to the next ptp descriptor */
- dma_current_ptp_txdesc++;
- }
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
- /* TDES2 and TDES3 will not be covered by buffer address, so do not need to preserve a new table,
- use the same table with TxDMA descriptor */
- dma_current_ptp_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->status);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_txdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- dma_current_ptp_txdesc ++;
- }
- }
- return SUCCESS;
- }
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- /*!
- \brief wakeup frame filter register pointer reset
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_wum_filter_register_pointer_reset(void)
- {
- ENET_MAC_WUM |= ENET_MAC_WUM_WUFFRPR;
- }
- /*!
- \brief set the remote wakeup frame registers
- \param[in] pdata: pointer to buffer data which is written to remote wakeup frame registers (8 words total)
- \param[out] none
- \retval none
- */
- void enet_wum_filter_config(uint32_t pdata[])
- {
- uint32_t num = 0U;
- /* configure ENET_MAC_RWFF register */
- for(num = 0U; num < ETH_WAKEUP_REGISTER_LENGTH; num++){
- ENET_MAC_RWFF = pdata[num];
- }
- }
- /*!
- \brief enable wakeup management features
- \param[in] feature: one or more parameters can be selected which are shown as below
- \arg ENET_WUM_POWER_DOWN: power down mode
- \arg ENET_WUM_MAGIC_PACKET_FRAME: enable a wakeup event due to magic packet reception
- \arg ENET_WUM_WAKE_UP_FRAME: enable a wakeup event due to wakeup frame reception
- \arg ENET_WUM_GLOBAL_UNICAST: any received unicast frame passed filter is considered to be a wakeup frame
- \param[out] none
- \retval none
- */
- void enet_wum_feature_enable(uint32_t feature)
- {
- ENET_MAC_WUM |= feature;
- }
- /*!
- \brief disable wakeup management features
- \param[in] feature: one or more parameters can be selected which are shown as below
- \arg ENET_WUM_MAGIC_PACKET_FRAME: enable a wakeup event due to magic packet reception
- \arg ENET_WUM_WAKE_UP_FRAME: enable a wakeup event due to wakeup frame reception
- \arg ENET_WUM_GLOBAL_UNICAST: any received unicast frame passed filter is considered to be a wakeup frame
- \param[out] none
- \retval none
- */
- void enet_wum_feature_disable(uint32_t feature)
- {
- ENET_MAC_WUM &= (~feature);
- }
- /*!
- \brief reset the MAC statistics counters
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_msc_counters_reset(void)
- {
- /* reset all counters */
- ENET_MSC_CTL |= ENET_MSC_CTL_CTR;
- }
- /*!
- \brief enable the MAC statistics counter features
- \param[in] feature: one or more parameters can be selected which are shown as below
- \arg ENET_MSC_COUNTER_STOP_ROLLOVER: counter stop rollover
- \arg ENET_MSC_RESET_ON_READ: reset on read
- \arg ENET_MSC_COUNTERS_FREEZE: MSC counter freeze
- \param[out] none
- \retval none
- */
- void enet_msc_feature_enable(uint32_t feature)
- {
- ENET_MSC_CTL |= feature;
- }
- /*!
- \brief disable the MAC statistics counter features
- \param[in] feature: one or more parameters can be selected which are shown as below
- \arg ENET_MSC_COUNTER_STOP_ROLLOVER: counter stop rollover
- \arg ENET_MSC_RESET_ON_READ: reset on read
- \arg ENET_MSC_COUNTERS_FREEZE: MSC counter freeze
- \param[out] none
- \retval none
- */
- void enet_msc_feature_disable(uint32_t feature)
- {
- ENET_MSC_CTL &= (~feature);
- }
- /*!
- \brief configure MAC statistics counters preset mode
- \param[in] mode: MSC counters preset mode, refer to enet_msc_preset_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_MSC_PRESET_NONE: do not preset MSC counter
- \arg ENET_MSC_PRESET_HALF: preset all MSC counters to almost-half(0x7FFF FFF0) value
- \arg ENET_MSC_PRESET_FULL: preset all MSC counters to almost-full(0xFFFF FFF0) value
- \param[out] none
- \retval none
- */
- void enet_msc_counters_preset_config(enet_msc_preset_enum mode)
- {
- ENET_MSC_CTL &= ENET_MSC_PRESET_MASK;
- ENET_MSC_CTL |= (uint32_t)mode;
- }
- /*!
- \brief get MAC statistics counter
- \param[in] counter: MSC counters which is selected, refer to enet_msc_counter_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_MSC_TX_SCCNT: MSC transmitted good frames after a single collision counter
- \arg ENET_MSC_TX_MSCCNT: MSC transmitted good frames after more than a single collision counter
- \arg ENET_MSC_TX_TGFCNT: MSC transmitted good frames counter
- \arg ENET_MSC_RX_RFCECNT: MSC received frames with CRC error counter
- \arg ENET_MSC_RX_RFAECNT: MSC received frames with alignment error counter
- \arg ENET_MSC_RX_RGUFCNT: MSC received good unicast frames counter
- \param[out] none
- \retval the MSC counter value
- */
- uint32_t enet_msc_counters_get(enet_msc_counter_enum counter)
- {
- uint32_t reval;
- reval = REG32((ENET + (uint32_t)counter));
- return reval;
- }
- /*!
- \brief change subsecond to nanosecond
- \param[in] subsecond: subsecond value
- \param[out] none
- \retval the nanosecond value
- */
- uint32_t enet_ptp_subsecond_2_nanosecond(uint32_t subsecond)
- {
- uint64_t val = subsecond * 1000000000Ull;
- val >>= 31;
- return (uint32_t)val;
- }
- /*!
- \brief change nanosecond to subsecond
- \param[in] nanosecond: nanosecond value
- \param[out] none
- \retval the subsecond value
- */
- uint32_t enet_ptp_nanosecond_2_subsecond(uint32_t nanosecond)
- {
- uint64_t val = nanosecond * 0x80000000Ull;
- val /= 1000000000U;
- return (uint32_t)val;
- }
- /*!
- \brief enable the PTP features
- \param[in] feature: the feature of ENET PTP mode
- one or more parameters can be selected which are shown as below
- \arg ENET_RXTX_TIMESTAMP: timestamp function for transmit and receive frames
- \arg ENET_PTP_TIMESTAMP_INT: timestamp interrupt trigger
- \arg ENET_ALL_RX_TIMESTAMP: all received frames are taken snapshot
- \arg ENET_NONTYPE_FRAME_SNAPSHOT: take snapshot when received non type frame
- \arg ENET_IPV6_FRAME_SNAPSHOT: take snapshot for IPv6 frame
- \arg ENET_IPV4_FRAME_SNAPSHOT: take snapshot for IPv4 frame
- \arg ENET_PTP_FRAME_USE_MACADDRESS_FILTER: use MAC address1-3 to filter the PTP frame
- \param[out] none
- \retval none
- */
- void enet_ptp_feature_enable(uint32_t feature)
- {
- ENET_PTP_TSCTL |= feature;
- }
- /*!
- \brief disable the PTP features
- \param[in] feature: the feature of ENET PTP mode
- one or more parameters can be selected which are shown as below
- \arg ENET_RXTX_TIMESTAMP: timestamp function for transmit and receive frames
- \arg ENET_PTP_TIMESTAMP_INT: timestamp interrupt trigger
- \arg ENET_ALL_RX_TIMESTAMP: all received frames are taken snapshot
- \arg ENET_NONTYPE_FRAME_SNAPSHOT: take snapshot when received non type frame
- \arg ENET_IPV6_FRAME_SNAPSHOT: take snapshot for IPv6 frame
- \arg ENET_IPV4_FRAME_SNAPSHOT: take snapshot for IPv4 frame
- \arg ENET_PTP_FRAME_USE_MACADDRESS_FILTER: use MAC address1-3 to filter the PTP frame
- \param[out] none
- \retval none
- */
- void enet_ptp_feature_disable(uint32_t feature)
- {
- ENET_PTP_TSCTL &= ~feature;
- }
- /*!
- \brief configure the PTP timestamp function
- \param[in] func: only one parameter can be selected which is shown as below
- \arg ENET_CKNT_ORDINARY: type of ordinary clock node type for timestamp
- \arg ENET_CKNT_BOUNDARY: type of boundary clock node type for timestamp
- \arg ENET_CKNT_END_TO_END: type of end-to-end transparent clock node type for timestamp
- \arg ENET_CKNT_PEER_TO_PEER: type of peer-to-peer transparent clock node type for timestamp
- \arg ENET_PTP_ADDEND_UPDATE: addend register update
- \arg ENET_PTP_SYSTIME_UPDATE: timestamp update
- \arg ENET_PTP_SYSTIME_INIT: timestamp initialize
- \arg ENET_PTP_FINEMODE: the system timestamp uses the fine method for updating
- \arg ENET_PTP_COARSEMODE: the system timestamp uses the coarse method for updating
- \arg ENET_SUBSECOND_DIGITAL_ROLLOVER: digital rollover mode
- \arg ENET_SUBSECOND_BINARY_ROLLOVER: binary rollover mode
- \arg ENET_SNOOPING_PTP_VERSION_2: version 2
- \arg ENET_SNOOPING_PTP_VERSION_1: version 1
- \arg ENET_EVENT_TYPE_MESSAGES_SNAPSHOT: only event type messages are taken snapshot
- \arg ENET_ALL_TYPE_MESSAGES_SNAPSHOT: all type messages are taken snapshot except announce,
- management and signaling message
- \arg ENET_MASTER_NODE_MESSAGE_SNAPSHOT: snapshot is only take for master node message
- \arg ENET_SLAVE_NODE_MESSAGE_SNAPSHOT: snapshot is only taken for slave node message
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptp_timestamp_function_config(enet_ptp_function_enum func)
- {
- uint32_t temp_config = 0U, temp_state = 0U;
- uint32_t timeout = 0U;
- ErrStatus enet_state = SUCCESS;
- switch(func){
- case ENET_CKNT_ORDINARY:
- case ENET_CKNT_BOUNDARY:
- case ENET_CKNT_END_TO_END:
- case ENET_CKNT_PEER_TO_PEER:
- ENET_PTP_TSCTL &= ~ENET_PTP_TSCTL_CKNT;
- ENET_PTP_TSCTL |= (uint32_t)func;
- break;
- case ENET_PTP_ADDEND_UPDATE:
- /* this bit must be read as zero before application set it */
- do{
- temp_state = ENET_PTP_TSCTL & ENET_PTP_TSCTL_TMSARU;
- timeout++;
- }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- enet_state = ERROR;
- }else{
- ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSARU;
- }
- break;
- case ENET_PTP_SYSTIME_UPDATE:
- /* both the TMSSTU and TMSSTI bits must be read as zero before application set this bit */
- do{
- temp_state = ENET_PTP_TSCTL & (ENET_PTP_TSCTL_TMSSTU | ENET_PTP_TSCTL_TMSSTI);
- timeout++;
- }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- enet_state = ERROR;
- }else{
- ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSSTU;
- }
- break;
- case ENET_PTP_SYSTIME_INIT:
- /* this bit must be read as zero before application set it */
- do{
- temp_state = ENET_PTP_TSCTL & ENET_PTP_TSCTL_TMSSTI;
- timeout++;
- }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- enet_state = ERROR;
- }else{
- ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSSTI;
- }
- break;
- default:
- temp_config = (uint32_t)func & (~BIT(31));
- if(RESET != ((uint32_t)func & BIT(31))){
- ENET_PTP_TSCTL |= temp_config;
- }else{
- ENET_PTP_TSCTL &= ~temp_config;
- }
- break;
- }
- return enet_state;
- }
- /*!
- \brief configure system time subsecond increment value
- \param[in] subsecond: the value will be added to the subsecond value of system time,
- this value must be between 0 and 0xFF
- \param[out] none
- \retval none
- */
- void enet_ptp_subsecond_increment_config(uint32_t subsecond)
- {
- ENET_PTP_SSINC = PTP_SSINC_STMSSI(subsecond);
- }
- /*!
- \brief adjusting the clock frequency only in fine update mode
- \param[in] add: the value will be added to the accumulator register to achieve time synchronization
- \param[out] none
- \retval none
- */
- void enet_ptp_timestamp_addend_config(uint32_t add)
- {
- ENET_PTP_TSADDEND = add;
- }
- /*!
- \brief initialize or add/subtract to second of the system time
- \param[in] sign: timestamp update positive or negative sign,
- only one parameter can be selected which is shown as below
- \arg ENET_PTP_ADD_TO_TIME: timestamp update value is added to system time
- \arg ENET_PTP_SUBSTRACT_FROM_TIME: timestamp update value is subtracted from system time
- \param[in] second: initializing or adding/subtracting to second of the system time
- \param[in] subsecond: the current subsecond of the system time
- with 0.46 ns accuracy if required accuracy is 20 ns
- \param[out] none
- \retval none
- */
- void enet_ptp_timestamp_update_config(uint32_t sign, uint32_t second, uint32_t subsecond)
- {
- ENET_PTP_TSUH = second;
- ENET_PTP_TSUL = sign | PTP_TSUL_TMSUSS(subsecond);
- }
- /*!
- \brief configure the expected target time
- \param[in] second: the expected target second time
- \param[in] nanosecond: the expected target nanosecond time (signed)
- \param[out] none
- \retval none
- */
- void enet_ptp_expected_time_config(uint32_t second, uint32_t nanosecond)
- {
- ENET_PTP_ETH = second;
- ENET_PTP_ETL = nanosecond;
- }
- /*!
- \brief get the current system time
- \param[in] none
- \param[out] systime_struct: pointer to a enet_ptp_systime_struct structure which contains
- parameters of PTP system time
- members of the structure and the member values are shown as below:
- second: 0x0 - 0xFFFF FFFF
- nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
- sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
- \retval none
- */
- void enet_ptp_system_time_get(enet_ptp_systime_struct *systime_struct)
- {
- uint32_t temp_sec = 0U, temp_subs = 0U;
- /* get the value of sysytem time registers */
- temp_sec = (uint32_t)ENET_PTP_TSH;
- temp_subs = (uint32_t)ENET_PTP_TSL;
-
- /* get sysytem time and construct the enet_ptp_systime_struct structure */
- systime_struct->second = temp_sec;
- systime_struct->nanosecond = GET_PTP_TSL_STMSS(temp_subs);
- systime_struct->nanosecond = enet_ptp_subsecond_2_nanosecond(systime_struct->nanosecond);
- systime_struct->sign = GET_PTP_TSL_STS(temp_subs);
- }
- /*!
- \brief configure the PPS output frequency
- \param[in] freq: PPS output frequency,
- only one parameter can be selected which is shown as below
- \arg ENET_PPSOFC_1HZ: PPS output 1Hz frequency
- \arg ENET_PPSOFC_2HZ: PPS output 2Hz frequency
- \arg ENET_PPSOFC_4HZ: PPS output 4Hz frequency
- \arg ENET_PPSOFC_8HZ: PPS output 8Hz frequency
- \arg ENET_PPSOFC_16HZ: PPS output 16Hz frequency
- \arg ENET_PPSOFC_32HZ: PPS output 32Hz frequency
- \arg ENET_PPSOFC_64HZ: PPS output 64Hz frequency
- \arg ENET_PPSOFC_128HZ: PPS output 128Hz frequency
- \arg ENET_PPSOFC_256HZ: PPS output 256Hz frequency
- \arg ENET_PPSOFC_512HZ: PPS output 512Hz frequency
- \arg ENET_PPSOFC_1024HZ: PPS output 1024Hz frequency
- \arg ENET_PPSOFC_2048HZ: PPS output 2048Hz frequency
- \arg ENET_PPSOFC_4096HZ: PPS output 4096Hz frequency
- \arg ENET_PPSOFC_8192HZ: PPS output 8192Hz frequency
- \arg ENET_PPSOFC_16384HZ: PPS output 16384Hz frequency
- \arg ENET_PPSOFC_32768HZ: PPS output 32768Hz frequency
- \param[out] none
- \retval none
- */
- void enet_ptp_pps_output_frequency_config(uint32_t freq)
- {
- ENET_PTP_PPSCTL = freq;
- }
- /*!
- \brief configure and start PTP timestamp counter
- \param[in] updatemethod: method for updating
- \arg ENET_PTP_FINEMODE: fine correction method
- \arg ENET_PTP_COARSEMODE: coarse correction method
- \param[in] init_sec: second value for initializing system time
- \param[in] init_subsec: subsecond value for initializing system time
- \param[in] carry_cfg: the value to be added to the accumulator register (in fine method is used)
- \param[in] accuracy_cfg: the value to be added to the subsecond value of system time
- \param[out] none
- \retval none
- */
- void enet_ptp_start(int32_t updatemethod, uint32_t init_sec, uint32_t init_subsec, uint32_t carry_cfg, uint32_t accuracy_cfg)
- {
- /* mask the timestamp trigger interrupt */
- enet_interrupt_disable(ENET_MAC_INT_TMSTIM);
- /* enable timestamp */
- enet_ptp_feature_enable(ENET_ALL_RX_TIMESTAMP | ENET_RXTX_TIMESTAMP);
- /* configure system time subsecond increment based on the PTP clock frequency */
- enet_ptp_subsecond_increment_config(accuracy_cfg);
- if(ENET_PTP_FINEMODE == updatemethod){
- /* fine correction method: configure the timestamp addend, then update */
- enet_ptp_timestamp_addend_config(carry_cfg);
- enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
- /* wait until update is completed */
- while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_ADDEND_UPDATE)){
- }
- }
- /* choose the fine correction method */
- enet_ptp_timestamp_function_config((enet_ptp_function_enum)updatemethod);
- /* initialize the system time */
- enet_ptp_timestamp_update_config(ENET_PTP_ADD_TO_TIME, init_sec, init_subsec);
- enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- enet_desc_select_enhanced_mode();
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- }
- /*!
- \brief adjust frequency in fine method by configure addend register
- \param[in] carry_cfg: the value to be added to the accumulator register
- \param[out] none
- \retval none
- */
- void enet_ptp_finecorrection_adjfreq(int32_t carry_cfg)
- {
- /* re-configure the timestamp addend, then update */
- enet_ptp_timestamp_addend_config((uint32_t)carry_cfg);
- enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
- }
- /*!
- \brief update system time in coarse method
- \param[in] systime_struct: : pointer to a enet_ptp_systime_struct structure which contains
- parameters of PTP system time
- members of the structure and the member values are shown as below:
- second: 0x0 - 0xFFFF FFFF
- nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
- sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
- \param[out] none
- \retval none
- */
- void enet_ptp_coarsecorrection_systime_update(enet_ptp_systime_struct *systime_struct)
- {
- uint32_t subsecond_val;
- uint32_t carry_cfg;
- subsecond_val = enet_ptp_nanosecond_2_subsecond(systime_struct->nanosecond);
- /* save the carry_cfg value */
- carry_cfg = ENET_PTP_TSADDEND_TMSA;
- /* update the system time */
- enet_ptp_timestamp_update_config(systime_struct->sign, systime_struct->second, subsecond_val);
- enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_UPDATE);
- /* wait until the update is completed */
- while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_SYSTIME_UPDATE)){
- }
- /* write back the carry_cfg value, then update */
- enet_ptp_timestamp_addend_config(carry_cfg);
- enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
- }
- /*!
- \brief set system time in fine method
- \param[in] systime_struct: : pointer to a enet_ptp_systime_struct structure which contains
- parameters of PTP system time
- members of the structure and the member values are shown as below:
- second: 0x0 - 0xFFFF FFFF
- nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
- sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
- \param[out] none
- \retval none
- */
- void enet_ptp_finecorrection_settime(enet_ptp_systime_struct * systime_struct)
- {
- uint32_t subsecond_val;
- subsecond_val = enet_ptp_nanosecond_2_subsecond(systime_struct->nanosecond);
- /* initialize the system time */
- enet_ptp_timestamp_update_config(systime_struct->sign, systime_struct->second, subsecond_val);
- enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
-
- /* wait until the system time initialzation finished */
- while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_SYSTIME_INIT)){
- }
- }
- /*!
- \brief get the ptp flag status
- \param[in] flag: ptp flag status to be checked
- \arg ENET_PTP_ADDEND_UPDATE: addend register update
- \arg ENET_PTP_SYSTIME_UPDATE: timestamp update
- \arg ENET_PTP_SYSTIME_INIT: timestamp initialize
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus enet_ptp_flag_get(uint32_t flag)
- {
- FlagStatus bitstatus = RESET;
- if ((uint32_t)RESET != (ENET_PTP_TSCTL & flag)){
- bitstatus = SET;
- }
- return bitstatus;
- }
- /*!
- \brief reset the ENET initpara struct, call it before using enet_initpara_config()
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_initpara_reset(void)
- {
- enet_initpara.option_enable = 0U;
- enet_initpara.forward_frame = 0U;
- enet_initpara.dmabus_mode = 0U;
- enet_initpara.dma_maxburst = 0U;
- enet_initpara.dma_arbitration = 0U;
- enet_initpara.store_forward_mode = 0U;
- enet_initpara.dma_function = 0U;
- enet_initpara.vlan_config = 0U;
- enet_initpara.flow_control = 0U;
- enet_initpara.hashtable_high = 0U;
- enet_initpara.hashtable_low = 0U;
- enet_initpara.framesfilter_mode = 0U;
- enet_initpara.halfduplex_param = 0U;
- enet_initpara.timer_config = 0U;
- enet_initpara.interframegap = 0U;
- }
- /*!
- \brief initialize ENET peripheral with generally concerned parameters, call it by enet_init()
- \param[in] none
- \param[out] none
- \retval none
- */
- static void enet_default_init(void)
- {
- uint32_t reg_value = 0U;
- /* MAC */
- /* configure ENET_MAC_CFG register */
- reg_value = ENET_MAC_CFG;
- reg_value &= MAC_CFG_MASK;
- reg_value |= ENET_WATCHDOG_ENABLE | ENET_JABBER_ENABLE | ENET_INTERFRAMEGAP_96BIT \
- | ENET_SPEEDMODE_10M |ENET_MODE_HALFDUPLEX | ENET_LOOPBACKMODE_DISABLE \
- | ENET_CARRIERSENSE_ENABLE | ENET_RECEIVEOWN_ENABLE \
- | ENET_RETRYTRANSMISSION_ENABLE | ENET_BACKOFFLIMIT_10 \
- | ENET_DEFERRALCHECK_DISABLE \
- | ENET_TYPEFRAME_CRC_DROP_DISABLE \
- | ENET_AUTO_PADCRC_DROP_DISABLE \
- | ENET_CHECKSUMOFFLOAD_DISABLE;
- ENET_MAC_CFG = reg_value;
- /* configure ENET_MAC_FRMF register */
- ENET_MAC_FRMF = ENET_SRC_FILTER_DISABLE |ENET_DEST_FILTER_INVERSE_DISABLE \
- |ENET_MULTICAST_FILTER_PERFECT |ENET_UNICAST_FILTER_PERFECT \
- |ENET_PCFRM_PREVENT_ALL |ENET_BROADCASTFRAMES_ENABLE \
- |ENET_PROMISCUOUS_DISABLE |ENET_RX_FILTER_ENABLE;
- /* configure ENET_MAC_HLH, ENET_MAC_HLL register */
- ENET_MAC_HLH = 0x0U;
- ENET_MAC_HLL = 0x0U;
- /* configure ENET_MAC_FCTL, ENET_MAC_FCTH register */
- reg_value = ENET_MAC_FCTL;
- reg_value &= MAC_FCTL_MASK;
- reg_value |= MAC_FCTL_PTM(0) |ENET_ZERO_QUANTA_PAUSE_DISABLE \
- |ENET_PAUSETIME_MINUS4 |ENET_UNIQUE_PAUSEDETECT \
- |ENET_RX_FLOWCONTROL_DISABLE |ENET_TX_FLOWCONTROL_DISABLE;
- ENET_MAC_FCTL = reg_value;
- /* configure ENET_MAC_VLT register */
- ENET_MAC_VLT = ENET_VLANTAGCOMPARISON_16BIT |MAC_VLT_VLTI(0);
- /* DMA */
- /* configure ENET_DMA_CTL register */
- reg_value = ENET_DMA_CTL;
- reg_value &= DMA_CTL_MASK;
- reg_value |= ENET_TCPIP_CKSUMERROR_DROP |ENET_RX_MODE_STOREFORWARD \
- |ENET_FLUSH_RXFRAME_ENABLE |ENET_TX_MODE_STOREFORWARD \
- |ENET_TX_THRESHOLD_64BYTES |ENET_RX_THRESHOLD_64BYTES \
- |ENET_SECONDFRAME_OPT_DISABLE;
- ENET_DMA_CTL = reg_value;
- /* configure ENET_DMA_BCTL register */
- reg_value = ENET_DMA_BCTL;
- reg_value &= DMA_BCTL_MASK;
- reg_value = ENET_ADDRESS_ALIGN_ENABLE |ENET_ARBITRATION_RXTX_2_1 \
- |ENET_RXDP_32BEAT |ENET_PGBL_32BEAT |ENET_RXTX_DIFFERENT_PGBL \
- |ENET_FIXED_BURST_ENABLE |ENET_MIXED_BURST_DISABLE \
- |ENET_NORMAL_DESCRIPTOR;
- ENET_DMA_BCTL = reg_value;
- }
- #ifndef USE_DELAY
- /*!
- \brief insert a delay time
- \param[in] ncount: specifies the delay time length
- \param[out] none
- \param[out] none
- */
- static void enet_delay(uint32_t ncount)
- {
- __IO uint32_t delay_time = 0U;
- for(delay_time = ncount; delay_time != 0U; delay_time--){
- }
- }
- #endif /* USE_DELAY */
- #endif /* GD32F30X_CL */
|