gd32f30x_enet.c 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615
  1. /*!
  2. \file gd32f30x_enet.c
  3. \brief ENET driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-02-10, V1.0.1, firmware for GD32F30x
  8. */
  9. #include "gd32f30x_enet.h"
  10. #ifdef GD32F30X_CL
  11. #if defined (__CC_ARM) /*!< ARM compiler */
  12. __align(4)
  13. enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM]; /*!< ENET RxDMA descriptor */
  14. __align(4)
  15. enet_descriptors_struct txdesc_tab[ENET_TXBUF_NUM]; /*!< ENET TxDMA descriptor */
  16. __align(4)
  17. uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE]; /*!< ENET receive buffer */
  18. __align(4)
  19. uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE]; /*!< ENET transmit buffer */
  20. #elif defined ( __ICCARM__ ) /*!< IAR compiler */
  21. #pragma data_alignment=4
  22. enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM]; /*!< ENET RxDMA descriptor */
  23. #pragma data_alignment=4
  24. enet_descriptors_struct txdesc_tab[ENET_TXBUF_NUM]; /*!< ENET TxDMA descriptor */
  25. #pragma data_alignment=4
  26. uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE]; /*!< ENET receive buffer */
  27. #pragma data_alignment=4
  28. uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE]; /*!< ENET transmit buffer */
  29. #endif /* __CC_ARM */
  30. /* global transmit and receive descriptors pointers */
  31. enet_descriptors_struct *dma_current_txdesc;
  32. enet_descriptors_struct *dma_current_rxdesc;
  33. /* structure pointer of ptp descriptor for normal mode */
  34. enet_descriptors_struct *dma_current_ptp_txdesc = NULL;
  35. enet_descriptors_struct *dma_current_ptp_rxdesc = NULL;
  36. /* init structure parameters for ENET initialization */
  37. static enet_initpara_struct enet_initpara ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  38. /* array of register offset for debug information get */
  39. static const uint16_t enet_reg_tab[] = {
  40. 0x0000, 0x0004, 0x0008, 0x000C, 0x0010, 0x0014, 0x0018, 0x001C, 0x0028, 0x002C, 0x0034,
  41. 0x0038, 0x003C, 0x0040, 0x0044, 0x0048, 0x004C, 0x0050, 0x0054, 0x0058, 0x005C, 0x1080,
  42. 0x0100, 0x0104, 0x0108, 0x010C, 0x0110, 0x014C, 0x0150, 0x0168, 0x0194, 0x0198, 0x01C4,
  43. 0x0700, 0x0704,0x0708, 0x070C, 0x0710, 0x0714, 0x0718, 0x071C, 0x0720, 0x0728, 0x072C,
  44. 0x1000, 0x1004, 0x1008, 0x100C, 0x1010, 0x1014, 0x1018, 0x101C, 0x1020, 0x1024, 0x1048,
  45. 0x104C, 0x1050, 0x1054};
  46. /*!
  47. \brief deinitialize the ENET, and reset structure parameters for ENET initialization
  48. \param[in] none
  49. \param[out] none
  50. \retval none
  51. */
  52. void enet_deinit(void)
  53. {
  54. rcu_periph_reset_enable(RCU_ENETRST);
  55. rcu_periph_reset_disable(RCU_ENETRST);
  56. enet_initpara_reset();
  57. }
  58. /*!
  59. \brief configure the parameters which are usually less cared for initialization
  60. note -- this function must be called before enet_init(), otherwise
  61. configuration will be no effect
  62. \param[in] option: different function option, which is related to several parameters,
  63. only one parameter can be selected which is shown as below, refer to enet_option_enum
  64. \arg FORWARD_OPTION: choose to configure the frame forward related parameters
  65. \arg DMABUS_OPTION: choose to configure the DMA bus mode related parameters
  66. \arg DMA_MAXBURST_OPTION: choose to configure the DMA max burst related parameters
  67. \arg DMA_ARBITRATION_OPTION: choose to configure the DMA arbitration related parameters
  68. \arg STORE_OPTION: choose to configure the store forward mode related parameters
  69. \arg DMA_OPTION: choose to configure the DMA descriptor related parameters
  70. \arg VLAN_OPTION: choose to configure vlan related parameters
  71. \arg FLOWCTL_OPTION: choose to configure flow control related parameters
  72. \arg HASHH_OPTION: choose to configure hash high
  73. \arg HASHL_OPTION: choose to configure hash low
  74. \arg FILTER_OPTION: choose to configure frame filter related parameters
  75. \arg HALFDUPLEX_OPTION: choose to configure halfduplex mode related parameters
  76. \arg TIMER_OPTION: choose to configure time counter related parameters
  77. \arg INTERFRAMEGAP_OPTION: choose to configure the inter frame gap related parameters
  78. \param[in] para: the related parameters according to the option
  79. all the related parameters should be configured which are shown as below
  80. FORWARD_OPTION related parameters:
  81. - ENET_AUTO_PADCRC_DROP_ENABLE/ ENET_AUTO_PADCRC_DROP_DISABLE ;
  82. - ENET_TYPEFRAME_CRC_DROP_ENABLE/ ENET_TYPEFRAME_CRC_DROP_DISABLE ;
  83. - ENET_FORWARD_ERRFRAMES_ENABLE/ ENET_FORWARD_ERRFRAMES_DISABLE ;
  84. - ENET_FORWARD_UNDERSZ_GOODFRAMES_ENABLE/ ENET_FORWARD_UNDERSZ_GOODFRAMES_DISABLE .
  85. DMABUS_OPTION related parameters:
  86. - ENET_ADDRESS_ALIGN_ENABLE/ ENET_ADDRESS_ALIGN_DISABLE ;
  87. - ENET_FIXED_BURST_ENABLE/ ENET_FIXED_BURST_DISABLE ;
  88. - ENET_MIXED_BURST_ENABLE/ ENET_MIXED_BURST_DISABLE ;
  89. DMA_MAXBURST_OPTION related parameters:
  90. - ENET_RXDP_1BEAT/ ENET_RXDP_2BEAT/ ENET_RXDP_4BEAT/
  91. ENET_RXDP_8BEAT/ ENET_RXDP_16BEAT/ ENET_RXDP_32BEAT/
  92. ENET_RXDP_4xPGBL_4BEAT/ ENET_RXDP_4xPGBL_8BEAT/
  93. ENET_RXDP_4xPGBL_16BEAT/ ENET_RXDP_4xPGBL_32BEAT/
  94. ENET_RXDP_4xPGBL_64BEAT/ ENET_RXDP_4xPGBL_128BEAT ;
  95. - ENET_PGBL_1BEAT/ ENET_PGBL_2BEAT/ ENET_PGBL_4BEAT/
  96. ENET_PGBL_8BEAT/ ENET_PGBL_16BEAT/ ENET_PGBL_32BEAT/
  97. ENET_PGBL_4xPGBL_4BEAT/ ENET_PGBL_4xPGBL_8BEAT/
  98. ENET_PGBL_4xPGBL_16BEAT/ ENET_PGBL_4xPGBL_32BEAT/
  99. ENET_PGBL_4xPGBL_64BEAT/ ENET_PGBL_4xPGBL_128BEAT ;
  100. - ENET_RXTX_DIFFERENT_PGBL/ ENET_RXTX_SAME_PGBL ;
  101. DMA_ARBITRATION_OPTION related parameters:
  102. - ENET_ARBITRATION_RXPRIORTX
  103. - ENET_ARBITRATION_RXTX_1_1/ ENET_ARBITRATION_RXTX_2_1/
  104. ENET_ARBITRATION_RXTX_3_1/ ENET_ARBITRATION_RXTX_4_1/.
  105. STORE_OPTION related parameters:
  106. - ENET_RX_MODE_STOREFORWARD/ ENET_RX_MODE_CUTTHROUGH ;
  107. - ENET_TX_MODE_STOREFORWARD/ ENET_TX_MODE_CUTTHROUGH ;
  108. - ENET_RX_THRESHOLD_64BYTES/ ENET_RX_THRESHOLD_32BYTES/
  109. ENET_RX_THRESHOLD_96BYTES/ ENET_RX_THRESHOLD_128BYTES ;
  110. - ENET_TX_THRESHOLD_64BYTES/ ENET_TX_THRESHOLD_128BYTES/
  111. ENET_TX_THRESHOLD_192BYTES/ ENET_TX_THRESHOLD_256BYTES/
  112. ENET_TX_THRESHOLD_40BYTES/ ENET_TX_THRESHOLD_32BYTES/
  113. ENET_TX_THRESHOLD_24BYTES/ ENET_TX_THRESHOLD_16BYTES .
  114. DMA_OPTION related parameters:
  115. - ENET_FLUSH_RXFRAME_ENABLE/ ENET_FLUSH_RXFRAME_DISABLE ;
  116. - ENET_SECONDFRAME_OPT_ENABLE/ ENET_SECONDFRAME_OPT_DISABLE ;
  117. - ENET_ENHANCED_DESCRIPTOR/ ENET_NORMAL_DESCRIPTOR .
  118. VLAN_OPTION related parameters:
  119. - ENET_VLANTAGCOMPARISON_12BIT/ ENET_VLANTAGCOMPARISON_16BIT ;
  120. - MAC_VLT_VLTI(regval) .
  121. FLOWCTL_OPTION related parameters:
  122. - MAC_FCTL_PTM(regval) ;
  123. - ENET_ZERO_QUANTA_PAUSE_ENABLE/ ENET_ZERO_QUANTA_PAUSE_DISABLE ;
  124. - ENET_PAUSETIME_MINUS4/ ENET_PAUSETIME_MINUS28/
  125. ENET_PAUSETIME_MINUS144/ENET_PAUSETIME_MINUS256 ;
  126. - ENET_MAC0_AND_UNIQUE_ADDRESS_PAUSEDETECT/ ENET_UNIQUE_PAUSEDETECT ;
  127. - ENET_RX_FLOWCONTROL_ENABLE/ ENET_RX_FLOWCONTROL_DISABLE ;
  128. - ENET_TX_FLOWCONTROL_ENABLE/ ENET_TX_FLOWCONTROL_DISABLE .
  129. HASHH_OPTION related parameters:
  130. - 0x0~0xFFFF FFFFU
  131. HASHL_OPTION related parameters:
  132. - 0x0~0xFFFF FFFFU
  133. FILTER_OPTION related parameters:
  134. - ENET_SRC_FILTER_NORMAL_ENABLE/ ENET_SRC_FILTER_INVERSE_ENABLE/
  135. ENET_SRC_FILTER_DISABLE ;
  136. - ENET_DEST_FILTER_INVERSE_ENABLE/ ENET_DEST_FILTER_INVERSE_DISABLE ;
  137. - ENET_MULTICAST_FILTER_HASH_OR_PERFECT/ ENET_MULTICAST_FILTER_HASH/
  138. ENET_MULTICAST_FILTER_PERFECT/ ENET_MULTICAST_FILTER_NONE ;
  139. - ENET_UNICAST_FILTER_EITHER/ ENET_UNICAST_FILTER_HASH/
  140. ENET_UNICAST_FILTER_PERFECT ;
  141. - ENET_PCFRM_PREVENT_ALL/ ENET_PCFRM_PREVENT_PAUSEFRAME/
  142. ENET_PCFRM_FORWARD_ALL/ ENET_PCFRM_FORWARD_FILTERED .
  143. HALFDUPLEX_OPTION related parameters:
  144. - ENET_CARRIERSENSE_ENABLE/ ENET_CARRIERSENSE_DISABLE ;
  145. - ENET_RECEIVEOWN_ENABLE/ ENET_RECEIVEOWN_DISABLE ;
  146. - ENET_RETRYTRANSMISSION_ENABLE/ ENET_RETRYTRANSMISSION_DISABLE ;
  147. - ENET_BACKOFFLIMIT_10/ ENET_BACKOFFLIMIT_8/
  148. ENET_BACKOFFLIMIT_4/ ENET_BACKOFFLIMIT_1 ;
  149. - ENET_DEFERRALCHECK_ENABLE/ ENET_DEFERRALCHECK_DISABLE .
  150. TIMER_OPTION related parameters:
  151. - ENET_WATCHDOG_ENABLE/ ENET_WATCHDOG_DISABLE ;
  152. - ENET_JABBER_ENABLE/ ENET_JABBER_DISABLE ;
  153. INTERFRAMEGAP_OPTION related parameters:
  154. - ENET_INTERFRAMEGAP_96BIT/ ENET_INTERFRAMEGAP_88BIT/
  155. ENET_INTERFRAMEGAP_80BIT/ ENET_INTERFRAMEGAP_72BIT/
  156. ENET_INTERFRAMEGAP_64BIT/ ENET_INTERFRAMEGAP_56BIT/
  157. ENET_INTERFRAMEGAP_48BIT/ ENET_INTERFRAMEGAP_40BIT .
  158. \param[out] none
  159. \retval none
  160. */
  161. void enet_initpara_config(enet_option_enum option, uint32_t para)
  162. {
  163. switch(option){
  164. case FORWARD_OPTION:
  165. /* choose to configure forward_frame, and save the configuration parameters */
  166. enet_initpara.option_enable |= (uint32_t)FORWARD_OPTION;
  167. enet_initpara.forward_frame = para;
  168. break;
  169. case DMABUS_OPTION:
  170. /* choose to configure dmabus_mode, and save the configuration parameters */
  171. enet_initpara.option_enable |= (uint32_t)DMABUS_OPTION;
  172. enet_initpara.dmabus_mode = para;
  173. break;
  174. case DMA_MAXBURST_OPTION:
  175. /* choose to configure dma_maxburst, and save the configuration parameters */
  176. enet_initpara.option_enable |= (uint32_t)DMA_MAXBURST_OPTION;
  177. enet_initpara.dma_maxburst = para;
  178. break;
  179. case DMA_ARBITRATION_OPTION:
  180. /* choose to configure dma_arbitration, and save the configuration parameters */
  181. enet_initpara.option_enable |= (uint32_t)DMA_ARBITRATION_OPTION;
  182. enet_initpara.dma_arbitration = para;
  183. break;
  184. case STORE_OPTION:
  185. /* choose to configure store_forward_mode, and save the configuration parameters */
  186. enet_initpara.option_enable |= (uint32_t)STORE_OPTION;
  187. enet_initpara.store_forward_mode = para;
  188. break;
  189. case DMA_OPTION:
  190. /* choose to configure dma_function, and save the configuration parameters */
  191. enet_initpara.option_enable |= (uint32_t)DMA_OPTION;
  192. #ifndef SELECT_DESCRIPTORS_ENHANCED_MODE
  193. para &= ~ENET_ENHANCED_DESCRIPTOR;
  194. #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
  195. enet_initpara.dma_function = para;
  196. break;
  197. case VLAN_OPTION:
  198. /* choose to configure vlan_config, and save the configuration parameters */
  199. enet_initpara.option_enable |= (uint32_t)VLAN_OPTION;
  200. enet_initpara.vlan_config = para;
  201. break;
  202. case FLOWCTL_OPTION:
  203. /* choose to configure flow_control, and save the configuration parameters */
  204. enet_initpara.option_enable |= (uint32_t)FLOWCTL_OPTION;
  205. enet_initpara.flow_control = para;
  206. break;
  207. case HASHH_OPTION:
  208. /* choose to configure hashtable_high, and save the configuration parameters */
  209. enet_initpara.option_enable |= (uint32_t)HASHH_OPTION;
  210. enet_initpara.hashtable_high = para;
  211. break;
  212. case HASHL_OPTION:
  213. /* choose to configure hashtable_low, and save the configuration parameters */
  214. enet_initpara.option_enable |= (uint32_t)HASHL_OPTION;
  215. enet_initpara.hashtable_low = para;
  216. break;
  217. case FILTER_OPTION:
  218. /* choose to configure framesfilter_mode, and save the configuration parameters */
  219. enet_initpara.option_enable |= (uint32_t)FILTER_OPTION;
  220. enet_initpara.framesfilter_mode = para;
  221. break;
  222. case HALFDUPLEX_OPTION:
  223. /* choose to configure halfduplex_param, and save the configuration parameters */
  224. enet_initpara.option_enable |= (uint32_t)HALFDUPLEX_OPTION;
  225. enet_initpara.halfduplex_param = para;
  226. break;
  227. case TIMER_OPTION:
  228. /* choose to configure timer_config, and save the configuration parameters */
  229. enet_initpara.option_enable |= (uint32_t)TIMER_OPTION;
  230. enet_initpara.timer_config = para;
  231. break;
  232. case INTERFRAMEGAP_OPTION:
  233. /* choose to configure interframegap, and save the configuration parameters */
  234. enet_initpara.option_enable |= (uint32_t)INTERFRAMEGAP_OPTION;
  235. enet_initpara.interframegap = para;
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. /*!
  242. \brief initialize ENET peripheral with generally concerned parameters and the less cared
  243. parameters
  244. \param[in] mediamode: PHY mode and mac loopback configurations, only one parameter can be selected
  245. which is shown as below, refer to enet_mediamode_enum
  246. \arg ENET_AUTO_NEGOTIATION: PHY auto negotiation
  247. \arg ENET_100M_FULLDUPLEX: 100Mbit/s, full-duplex
  248. \arg ENET_100M_HALFDUPLEX: 100Mbit/s, half-duplex
  249. \arg ENET_10M_FULLDUPLEX: 10Mbit/s, full-duplex
  250. \arg ENET_10M_HALFDUPLEX: 10Mbit/s, half-duplex
  251. \arg ENET_LOOPBACKMODE: MAC in loopback mode at the MII
  252. \param[in] checksum: IP frame checksum offload function, only one parameter can be selected
  253. which is shown as below, refer to enet_mediamode_enum
  254. \arg ENET_NO_AUTOCHECKSUM: disable IP frame checksum function
  255. \arg ENET_AUTOCHECKSUM_DROP_FAILFRAMES: enable IP frame checksum function
  256. \arg ENET_AUTOCHECKSUM_ACCEPT_FAILFRAMES: enable IP frame checksum function, and the received frame
  257. with only payload error but no other errors will not be dropped
  258. \param[in] recept: frame filter function, only one parameter can be selected
  259. which is shown as below, refer to enet_frmrecept_enum
  260. \arg ENET_PROMISCUOUS_MODE: promiscuous mode enabled
  261. \arg ENET_RECEIVEALL: all received frame are forwarded to application
  262. \arg ENET_BROADCAST_FRAMES_PASS: the address filters pass all received broadcast frames
  263. \arg ENET_BROADCAST_FRAMES_DROP: the address filters filter all incoming broadcast frames
  264. \param[out] none
  265. \retval ErrStatus: ERROR or SUCCESS
  266. */
  267. ErrStatus enet_init(enet_mediamode_enum mediamode, enet_chksumconf_enum checksum, enet_frmrecept_enum recept)
  268. {
  269. uint32_t reg_value=0U, reg_temp = 0U, temp = 0U;
  270. uint32_t media_temp = 0U;
  271. uint32_t timeout = 0U;
  272. uint16_t phy_value = 0U;
  273. ErrStatus phy_state= ERROR, enet_state = ERROR;
  274. /* PHY interface configuration, configure SMI clock and reset PHY chip */
  275. if(ERROR == enet_phy_config()){
  276. _ENET_DELAY_(PHY_RESETDELAY);
  277. if(ERROR == enet_phy_config()){
  278. return enet_state;
  279. }
  280. }
  281. /* initialize ENET peripheral with generally concerned parameters */
  282. enet_default_init();
  283. /* 1st, configure mediamode */
  284. media_temp = (uint32_t)mediamode;
  285. /* if is PHY auto negotiation */
  286. if((uint32_t)ENET_AUTO_NEGOTIATION == media_temp){
  287. /* wait for PHY_LINKED_STATUS bit be set */
  288. do{
  289. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
  290. phy_value &= PHY_LINKED_STATUS;
  291. timeout++;
  292. }while((RESET == phy_value) && (timeout < PHY_READ_TO));
  293. /* return ERROR due to timeout */
  294. if(PHY_READ_TO == timeout){
  295. return enet_state;
  296. }
  297. /* reset timeout counter */
  298. timeout = 0U;
  299. /* enable auto-negotiation */
  300. phy_value = PHY_AUTONEGOTIATION;
  301. phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value);
  302. if(!phy_state){
  303. /* return ERROR due to write timeout */
  304. return enet_state;
  305. }
  306. /* wait for the PHY_AUTONEGO_COMPLETE bit be set */
  307. do{
  308. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
  309. phy_value &= PHY_AUTONEGO_COMPLETE;
  310. timeout++;
  311. }while((RESET == phy_value) && (timeout < (uint32_t)PHY_READ_TO));
  312. /* return ERROR due to timeout */
  313. if(PHY_READ_TO == timeout){
  314. return enet_state;
  315. }
  316. /* reset timeout counter */
  317. timeout = 0U;
  318. /* read the result of the auto-negotiation */
  319. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_SR, &phy_value);
  320. /* configure the duplex mode of MAC following the auto-negotiation result */
  321. if((uint16_t)RESET != (phy_value & PHY_DUPLEX_STATUS)){
  322. media_temp = ENET_MODE_FULLDUPLEX;
  323. }else{
  324. media_temp = ENET_MODE_HALFDUPLEX;
  325. }
  326. /* configure the communication speed of MAC following the auto-negotiation result */
  327. if((uint16_t)RESET !=(phy_value & PHY_SPEED_STATUS)){
  328. media_temp |= ENET_SPEEDMODE_10M;
  329. }else{
  330. media_temp |= ENET_SPEEDMODE_100M;
  331. }
  332. }else{
  333. phy_value = (uint16_t)((media_temp & ENET_MAC_CFG_DPM) >> 3);
  334. phy_value |= (uint16_t)((media_temp & ENET_MAC_CFG_SPD) >> 1);
  335. phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value);
  336. if(!phy_state){
  337. /* return ERROR due to write timeout */
  338. return enet_state;
  339. }
  340. /* PHY configuration need some time */
  341. _ENET_DELAY_(PHY_CONFIGDELAY);
  342. }
  343. /* after configuring the PHY, use mediamode to configure registers */
  344. reg_value = ENET_MAC_CFG;
  345. /* configure ENET_MAC_CFG register */
  346. reg_value &= (~(ENET_MAC_CFG_SPD |ENET_MAC_CFG_DPM |ENET_MAC_CFG_LBM));
  347. reg_value |= media_temp;
  348. ENET_MAC_CFG = reg_value;
  349. /* 2st, configure checksum */
  350. if(RESET != ((uint32_t)checksum & ENET_CHECKSUMOFFLOAD_ENABLE)){
  351. ENET_MAC_CFG |= ENET_CHECKSUMOFFLOAD_ENABLE;
  352. reg_value = ENET_DMA_CTL;
  353. /* configure ENET_DMA_CTL register */
  354. reg_value &= ~ENET_DMA_CTL_DTCERFD;
  355. reg_value |= ((uint32_t)checksum & ENET_DMA_CTL_DTCERFD);
  356. ENET_DMA_CTL = reg_value;
  357. }
  358. /* 3rd, configure recept */
  359. ENET_MAC_FRMF |= (uint32_t)recept;
  360. /* 4th, configure different function options */
  361. /* configure forward_frame related registers */
  362. if(RESET != (enet_initpara.option_enable & (uint32_t)FORWARD_OPTION)){
  363. reg_temp = enet_initpara.forward_frame;
  364. reg_value = ENET_MAC_CFG;
  365. temp = reg_temp;
  366. /* configure ENET_MAC_CFG register */
  367. reg_value &= (~(ENET_MAC_CFG_TFCD |ENET_MAC_CFG_APCD));
  368. temp &= (ENET_MAC_CFG_TFCD | ENET_MAC_CFG_APCD);
  369. reg_value |= temp;
  370. ENET_MAC_CFG = reg_value;
  371. reg_value = ENET_DMA_CTL;
  372. temp = reg_temp;
  373. /* configure ENET_DMA_CTL register */
  374. reg_value &= (~(ENET_DMA_CTL_FERF |ENET_DMA_CTL_FUF));
  375. temp &= ((ENET_DMA_CTL_FERF | ENET_DMA_CTL_FUF)<<2);
  376. reg_value |= (temp >> 2);
  377. ENET_DMA_CTL = reg_value;
  378. }
  379. /* configure dmabus_mode related registers */
  380. if(RESET != (enet_initpara.option_enable & (uint32_t)DMABUS_OPTION)){
  381. temp = enet_initpara.dmabus_mode;
  382. reg_value = ENET_DMA_BCTL;
  383. /* configure ENET_DMA_BCTL register */
  384. reg_value &= ~(ENET_DMA_BCTL_AA | ENET_DMA_BCTL_FB \
  385. |ENET_DMA_BCTL_FPBL | ENET_DMA_BCTL_MB);
  386. reg_value |= temp;
  387. ENET_DMA_BCTL = reg_value;
  388. }
  389. /* configure dma_maxburst related registers */
  390. if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_MAXBURST_OPTION)){
  391. temp = enet_initpara.dma_maxburst;
  392. reg_value = ENET_DMA_BCTL;
  393. /* configure ENET_DMA_BCTL register */
  394. reg_value &= ~(ENET_DMA_BCTL_RXDP| ENET_DMA_BCTL_PGBL | ENET_DMA_BCTL_UIP);
  395. reg_value |= temp;
  396. ENET_DMA_BCTL = reg_value;
  397. }
  398. /* configure dma_arbitration related registers */
  399. if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_ARBITRATION_OPTION)){
  400. temp = enet_initpara.dma_arbitration;
  401. reg_value = ENET_DMA_BCTL;
  402. /* configure ENET_DMA_BCTL register */
  403. reg_value &= ~(ENET_DMA_BCTL_RTPR | ENET_DMA_BCTL_DAB);
  404. reg_value |= temp;
  405. ENET_DMA_BCTL = reg_value;
  406. }
  407. /* configure store_forward_mode related registers */
  408. if(RESET != (enet_initpara.option_enable & (uint32_t)STORE_OPTION)){
  409. temp = enet_initpara.store_forward_mode;
  410. reg_value = ENET_DMA_CTL;
  411. /* configure ENET_DMA_CTL register */
  412. reg_value &= ~(ENET_DMA_CTL_RSFD | ENET_DMA_CTL_TSFD| ENET_DMA_CTL_RTHC| ENET_DMA_CTL_TTHC);
  413. reg_value |= temp;
  414. ENET_DMA_CTL = reg_value;
  415. }
  416. /* configure dma_function related registers */
  417. if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_OPTION)){
  418. reg_temp = enet_initpara.dma_function;
  419. reg_value = ENET_DMA_CTL;
  420. temp = reg_temp;
  421. /* configure ENET_DMA_CTL register */
  422. reg_value &= (~(ENET_DMA_CTL_DAFRF |ENET_DMA_CTL_OSF));
  423. temp &= (ENET_DMA_CTL_DAFRF | ENET_DMA_CTL_OSF);
  424. reg_value |= temp;
  425. ENET_DMA_CTL = reg_value;
  426. reg_value = ENET_DMA_BCTL;
  427. temp = reg_temp;
  428. /* configure ENET_DMA_BCTL register */
  429. reg_value &= (~ENET_DMA_BCTL_DFM);
  430. temp &= ENET_DMA_BCTL_DFM;
  431. reg_value |= temp;
  432. ENET_DMA_BCTL = reg_value;
  433. }
  434. /* configure vlan_config related registers */
  435. if(RESET != (enet_initpara.option_enable & (uint32_t)VLAN_OPTION)){
  436. reg_temp = enet_initpara.vlan_config;
  437. reg_value = ENET_MAC_VLT;
  438. /* configure ENET_MAC_VLT register */
  439. reg_value &= ~(ENET_MAC_VLT_VLTI | ENET_MAC_VLT_VLTC);
  440. reg_value |= reg_temp;
  441. ENET_MAC_VLT = reg_value;
  442. }
  443. /* configure flow_control related registers */
  444. if(RESET != (enet_initpara.option_enable & (uint32_t)FLOWCTL_OPTION)){
  445. reg_temp = enet_initpara.flow_control;
  446. reg_value = ENET_MAC_FCTL;
  447. temp = reg_temp;
  448. /* configure ENET_MAC_FCTL register */
  449. reg_value &= ~(ENET_MAC_FCTL_PTM |ENET_MAC_FCTL_DZQP |ENET_MAC_FCTL_PLTS \
  450. | ENET_MAC_FCTL_UPFDT |ENET_MAC_FCTL_RFCEN |ENET_MAC_FCTL_TFCEN);
  451. temp &= (ENET_MAC_FCTL_PTM |ENET_MAC_FCTL_DZQP |ENET_MAC_FCTL_PLTS \
  452. | ENET_MAC_FCTL_UPFDT |ENET_MAC_FCTL_RFCEN |ENET_MAC_FCTL_TFCEN);
  453. reg_value |= temp;
  454. ENET_MAC_FCTL = reg_value;
  455. reg_value = ENET_MAC_FCTH;
  456. temp = reg_temp;
  457. /* configure ENET_MAC_FCTH register */
  458. reg_value &= ~(ENET_MAC_FCTH_RFA |ENET_MAC_FCTH_RFD);
  459. temp &= ((ENET_MAC_FCTH_RFA | ENET_MAC_FCTH_RFD )<<8);
  460. reg_value |= (temp >> 8);
  461. ENET_MAC_FCTH = reg_value;
  462. }
  463. /* configure hashtable_high related registers */
  464. if(RESET != (enet_initpara.option_enable & (uint32_t)HASHH_OPTION)){
  465. ENET_MAC_HLH = enet_initpara.hashtable_high;
  466. }
  467. /* configure hashtable_low related registers */
  468. if(RESET != (enet_initpara.option_enable & (uint32_t)HASHL_OPTION)){
  469. ENET_MAC_HLL = enet_initpara.hashtable_low;
  470. }
  471. /* configure framesfilter_mode related registers */
  472. if(RESET != (enet_initpara.option_enable & (uint32_t)FILTER_OPTION)){
  473. reg_temp = enet_initpara.framesfilter_mode;
  474. reg_value = ENET_MAC_FRMF;
  475. /* configure ENET_MAC_FRMF register */
  476. reg_value &= ~(ENET_MAC_FRMF_SAFLT | ENET_MAC_FRMF_SAIFLT | ENET_MAC_FRMF_DAIFLT \
  477. | ENET_MAC_FRMF_HMF | ENET_MAC_FRMF_HPFLT | ENET_MAC_FRMF_MFD \
  478. | ENET_MAC_FRMF_HUF | ENET_MAC_FRMF_PCFRM);
  479. reg_value |= reg_temp;
  480. ENET_MAC_FRMF = reg_value;
  481. }
  482. /* configure halfduplex_param related registers */
  483. if(RESET != (enet_initpara.option_enable & (uint32_t)HALFDUPLEX_OPTION)){
  484. reg_temp = enet_initpara.halfduplex_param;
  485. reg_value = ENET_MAC_CFG;
  486. /* configure ENET_MAC_CFG register */
  487. reg_value &= ~(ENET_MAC_CFG_CSD | ENET_MAC_CFG_ROD | ENET_MAC_CFG_RTD \
  488. | ENET_MAC_CFG_BOL | ENET_MAC_CFG_DFC);
  489. reg_value |= reg_temp;
  490. ENET_MAC_CFG = reg_value;
  491. }
  492. /* configure timer_config related registers */
  493. if(RESET != (enet_initpara.option_enable & (uint32_t)TIMER_OPTION)){
  494. reg_temp = enet_initpara.timer_config;
  495. reg_value = ENET_MAC_CFG;
  496. /* configure ENET_MAC_CFG register */
  497. reg_value &= ~(ENET_MAC_CFG_WDD | ENET_MAC_CFG_JBD);
  498. reg_value |= reg_temp;
  499. ENET_MAC_CFG = reg_value;
  500. }
  501. /* configure interframegap related registers */
  502. if(RESET != (enet_initpara.option_enable & (uint32_t)INTERFRAMEGAP_OPTION)){
  503. reg_temp = enet_initpara.interframegap;
  504. reg_value = ENET_MAC_CFG;
  505. /* configure ENET_MAC_CFG register */
  506. reg_value &= ~ENET_MAC_CFG_IGBS;
  507. reg_value |= reg_temp;
  508. ENET_MAC_CFG = reg_value;
  509. }
  510. enet_state = SUCCESS;
  511. return enet_state;
  512. }
  513. /*!
  514. \brief reset all core internal registers located in CLK_TX and CLK_RX
  515. \param[in] none
  516. \param[out] none
  517. \retval ErrStatus: SUCCESS or ERROR
  518. */
  519. ErrStatus enet_software_reset(void)
  520. {
  521. uint32_t timeout = 0U;
  522. ErrStatus enet_state = ERROR;
  523. uint32_t dma_flag;
  524. /* reset all core internal registers located in CLK_TX and CLK_RX */
  525. ENET_DMA_BCTL |= ENET_DMA_BCTL_SWR;
  526. /* wait for reset operation complete */
  527. do{
  528. dma_flag = (ENET_DMA_BCTL & ENET_DMA_BCTL_SWR);
  529. timeout++;
  530. }while((RESET != dma_flag) && (ENET_DELAY_TO != timeout));
  531. /* reset operation complete */
  532. if(RESET == (ENET_DMA_BCTL & ENET_DMA_BCTL_SWR)){
  533. enet_state = SUCCESS;
  534. }
  535. return enet_state;
  536. }
  537. /*!
  538. \brief check receive frame valid and return frame size
  539. \param[in] none
  540. \param[out] none
  541. \retval size of received frame: 0x0 - 0x3FFF
  542. */
  543. uint32_t enet_rxframe_size_get(void)
  544. {
  545. uint32_t size = 0U;
  546. uint32_t status;
  547. /* get rdes0 information of current RxDMA descriptor */
  548. status = dma_current_rxdesc->status;
  549. /* if the desciptor is owned by DMA */
  550. if((uint32_t)RESET != (status & ENET_RDES0_DAV)){
  551. return 0U;
  552. }
  553. /* if has any error, or the frame uses two or more descriptors */
  554. if((((uint32_t)RESET) != (status & ENET_RDES0_ERRS)) ||
  555. (((uint32_t)RESET) == (status & ENET_RDES0_LDES)) ||
  556. (((uint32_t)RESET) == (status & ENET_RDES0_FDES))){
  557. /* drop current receive frame */
  558. enet_rxframe_drop();
  559. return 0U;
  560. }
  561. #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
  562. /* if is an ethernet-type frame, and IP frame payload error occurred */
  563. if(((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_FRMT) &&
  564. ((uint32_t)RESET) != (dma_current_rxdesc->extended_status & ENET_RDES4_IPPLDERR)){
  565. /* drop current receive frame */
  566. enet_rxframe_drop();
  567. return 0U;
  568. }
  569. #else
  570. /* if is an ethernet-type frame, and IP frame payload error occurred */
  571. if((((uint32_t)RESET) != (status & ENET_RDES0_FRMT)) &&
  572. (((uint32_t)RESET) != (status & ENET_RDES0_PCERR))){
  573. /* drop current receive frame */
  574. enet_rxframe_drop();
  575. return 0U;
  576. }
  577. #endif
  578. /* if CPU owns current descriptor, no error occured, the frame uses only one descriptor */
  579. if((((uint32_t)RESET) == (status & ENET_RDES0_DAV)) &&
  580. (((uint32_t)RESET) == (status & ENET_RDES0_ERRS)) &&
  581. (((uint32_t)RESET) != (status & ENET_RDES0_LDES)) &&
  582. (((uint32_t)RESET) != (status & ENET_RDES0_FDES))){
  583. /* get the size of the received data including CRC */
  584. size = GET_RDES0_FRML(status);
  585. /* substract the CRC size */
  586. size = size - 4U;
  587. /* if is a type frame, and CRC is not included in forwarding frame */
  588. if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (status & ENET_RDES0_FRMT))){
  589. size = size + 4U;
  590. }
  591. }
  592. /* return packet size */
  593. return size;
  594. }
  595. /*!
  596. \brief initialize the DMA Tx/Rx descriptors's parameters in chain mode
  597. \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
  598. only one parameter can be selected which is shown as below
  599. \arg ENET_DMA_TX: DMA Tx descriptors
  600. \arg ENET_DMA_RX: DMA Rx descriptors
  601. \param[out] none
  602. \retval none
  603. */
  604. void enet_descriptors_chain_init(enet_dmadirection_enum direction)
  605. {
  606. uint32_t num = 0U, count = 0U, maxsize = 0U;
  607. uint32_t desc_status = 0U, desc_bufsize = 0U;
  608. enet_descriptors_struct *desc, *desc_tab;
  609. uint8_t *buf;
  610. /* if want to initialize DMA Tx descriptors */
  611. if (ENET_DMA_TX == direction){
  612. /* save a copy of the DMA Tx descriptors */
  613. desc_tab = txdesc_tab;
  614. buf = &tx_buff[0][0];
  615. count = ENET_TXBUF_NUM;
  616. maxsize = ENET_TXBUF_SIZE;
  617. /* select chain mode */
  618. desc_status = ENET_TDES0_TCHM;
  619. /* configure DMA Tx descriptor table address register */
  620. ENET_DMA_TDTADDR = (uint32_t)desc_tab;
  621. dma_current_txdesc = desc_tab;
  622. }else{
  623. /* if want to initialize DMA Rx descriptors */
  624. /* save a copy of the DMA Rx descriptors */
  625. desc_tab = rxdesc_tab;
  626. buf = &rx_buff[0][0];
  627. count = ENET_RXBUF_NUM;
  628. maxsize = ENET_RXBUF_SIZE;
  629. /* enable receiving */
  630. desc_status = ENET_RDES0_DAV;
  631. /* select receive chained mode and set buffer1 size */
  632. desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
  633. /* configure DMA Rx descriptor table address register */
  634. ENET_DMA_RDTADDR = (uint32_t)desc_tab;
  635. dma_current_rxdesc = desc_tab;
  636. }
  637. dma_current_ptp_rxdesc = NULL;
  638. dma_current_ptp_txdesc = NULL;
  639. /* configure each descriptor */
  640. for(num=0U; num < count; num++){
  641. /* get the pointer to the next descriptor of the descriptor table */
  642. desc = desc_tab + num;
  643. /* configure descriptors */
  644. desc->status = desc_status;
  645. desc->control_buffer_size = desc_bufsize;
  646. desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
  647. /* if is not the last descriptor */
  648. if(num < (count - 1U)){
  649. /* configure the next descriptor address */
  650. desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
  651. }else{
  652. /* when it is the last descriptor, the next descriptor address
  653. equals to first descriptor address in descriptor table */
  654. desc->buffer2_next_desc_addr = (uint32_t) desc_tab;
  655. }
  656. }
  657. }
  658. /*!
  659. \brief initialize the DMA Tx/Rx descriptors's parameters in ring mode
  660. \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
  661. only one parameter can be selected which is shown as below
  662. \arg ENET_DMA_TX: DMA Tx descriptors
  663. \arg ENET_DMA_RX: DMA Rx descriptors
  664. \param[out] none
  665. \retval none
  666. */
  667. void enet_descriptors_ring_init(enet_dmadirection_enum direction)
  668. {
  669. uint32_t num = 0U, count = 0U, maxsize = 0U;
  670. uint32_t desc_status = 0U, desc_bufsize = 0U;
  671. enet_descriptors_struct *desc;
  672. enet_descriptors_struct *desc_tab;
  673. uint8_t *buf;
  674. /* configure descriptor skip length */
  675. ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
  676. ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
  677. /* if want to initialize DMA Tx descriptors */
  678. if (ENET_DMA_TX == direction){
  679. /* save a copy of the DMA Tx descriptors */
  680. desc_tab = txdesc_tab;
  681. buf = &tx_buff[0][0];
  682. count = ENET_TXBUF_NUM;
  683. maxsize = ENET_TXBUF_SIZE;
  684. /* configure DMA Tx descriptor table address register */
  685. ENET_DMA_TDTADDR = (uint32_t)desc_tab;
  686. dma_current_txdesc = desc_tab;
  687. }else{
  688. /* if want to initialize DMA Rx descriptors */
  689. /* save a copy of the DMA Rx descriptors */
  690. desc_tab = rxdesc_tab;
  691. buf = &rx_buff[0][0];
  692. count = ENET_RXBUF_NUM;
  693. maxsize = ENET_RXBUF_SIZE;
  694. /* enable receiving */
  695. desc_status = ENET_RDES0_DAV;
  696. /* set buffer1 size */
  697. desc_bufsize = ENET_RXBUF_SIZE;
  698. /* configure DMA Rx descriptor table address register */
  699. ENET_DMA_RDTADDR = (uint32_t)desc_tab;
  700. dma_current_rxdesc = desc_tab;
  701. }
  702. dma_current_ptp_rxdesc = NULL;
  703. dma_current_ptp_txdesc = NULL;
  704. /* configure each descriptor */
  705. for(num=0U; num < count; num++){
  706. /* get the pointer to the next descriptor of the descriptor table */
  707. desc = desc_tab + num;
  708. /* configure descriptors */
  709. desc->status = desc_status;
  710. desc->control_buffer_size = desc_bufsize;
  711. desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
  712. /* when it is the last descriptor */
  713. if(num == (count - 1U)){
  714. if (ENET_DMA_TX == direction){
  715. /* configure transmit end of ring mode */
  716. desc->status |= ENET_TDES0_TERM;
  717. }else{
  718. /* configure receive end of ring mode */
  719. desc->control_buffer_size |= ENET_RDES1_RERM;
  720. }
  721. }
  722. }
  723. }
  724. /*!
  725. \brief handle current received frame data to application buffer
  726. \param[in] bufsize: the size of buffer which is the parameter in function
  727. \param[out] buffer: pointer to the received frame data
  728. note -- if the input is NULL, user should copy data in application by himself
  729. \retval ErrStatus: SUCCESS or ERROR
  730. */
  731. ErrStatus enet_frame_receive(uint8_t *buffer, uint32_t bufsize)
  732. {
  733. uint32_t offset = 0U, size = 0U;
  734. /* the descriptor is busy due to own by the DMA */
  735. if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
  736. return ERROR;
  737. }
  738. /* if buffer pointer is null, indicates that users has copied data in application */
  739. if(NULL != buffer){
  740. /* if no error occurs, and the frame uses only one descriptor */
  741. if((((uint32_t)RESET) == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
  742. (((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
  743. (((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
  744. /* get the frame length except CRC */
  745. size = GET_RDES0_FRML(dma_current_rxdesc->status);
  746. size = size - 4U;
  747. /* if is a type frame, and CRC is not included in forwarding frame */
  748. if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
  749. size = size + 4U;
  750. }
  751. /* to avoid situation that the frame size exceeds the buffer length */
  752. if(size > bufsize){
  753. return ERROR;
  754. }
  755. /* copy data from Rx buffer to application buffer */
  756. for(offset = 0U; offset<size; offset++){
  757. (*(buffer + offset)) = (*(__IO uint8_t *) (uint32_t)((dma_current_rxdesc->buffer1_addr) + offset));
  758. }
  759. }else{
  760. /* return ERROR */
  761. return ERROR;
  762. }
  763. }
  764. /* enable reception, descriptor is owned by DMA */
  765. dma_current_rxdesc->status = ENET_RDES0_DAV;
  766. /* check Rx buffer unavailable flag status */
  767. if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
  768. /* clear RBU flag */
  769. ENET_DMA_STAT = ENET_DMA_STAT_RBU;
  770. /* resume DMA reception by writing to the RPEN register*/
  771. ENET_DMA_RPEN = 0U;
  772. }
  773. /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
  774. /* chained mode */
  775. if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
  776. dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
  777. }else{
  778. /* ring mode */
  779. if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
  780. /* if is the last descriptor in table, the next descriptor is the table header */
  781. dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
  782. }else{
  783. /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
  784. dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + (GET_DMA_BCTL_DPSL(ENET_DMA_BCTL)));
  785. }
  786. }
  787. return SUCCESS;
  788. }
  789. /*!
  790. \brief handle application buffer data to transmit it
  791. \param[in] buffer: pointer to the frame data to be transmitted,
  792. note -- if the input is NULL, user should handle the data in application by himself
  793. \param[in] length: the length of frame data to be transmitted
  794. \param[out] none
  795. \retval ErrStatus: SUCCESS or ERROR
  796. */
  797. ErrStatus enet_frame_transmit(uint8_t *buffer, uint32_t length)
  798. {
  799. uint32_t offset = 0U;
  800. uint32_t dma_tbu_flag, dma_tu_flag;
  801. /* the descriptor is busy due to own by the DMA */
  802. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
  803. return ERROR;
  804. }
  805. /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
  806. if(length > ENET_MAX_FRAME_SIZE){
  807. return ERROR;
  808. }
  809. /* if buffer pointer is null, indicates that users has handled data in application */
  810. if(NULL != buffer){
  811. /* copy frame data from application buffer to Tx buffer */
  812. for(offset = 0U; offset < length; offset++){
  813. (*(__IO uint8_t *) (uint32_t)((dma_current_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
  814. }
  815. }
  816. /* set the frame length */
  817. dma_current_txdesc->control_buffer_size = length;
  818. /* set the segment of frame, frame is transmitted in one descriptor */
  819. dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
  820. /* enable the DMA transmission */
  821. dma_current_txdesc->status |= ENET_TDES0_DAV;
  822. /* check Tx buffer unavailable flag status */
  823. dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
  824. dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
  825. if ((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
  826. /* clear TBU and TU flag */
  827. ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
  828. /* resume DMA transmission by writing to the TPEN register*/
  829. ENET_DMA_TPEN = 0U;
  830. }
  831. /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table*/
  832. /* chained mode */
  833. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
  834. dma_current_txdesc = (enet_descriptors_struct*) (dma_current_txdesc->buffer2_next_desc_addr);
  835. }else{
  836. /* ring mode */
  837. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
  838. /* if is the last descriptor in table, the next descriptor is the table header */
  839. dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
  840. }else{
  841. /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
  842. dma_current_txdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + (GET_DMA_BCTL_DPSL(ENET_DMA_BCTL)));
  843. }
  844. }
  845. return SUCCESS;
  846. }
  847. /*!
  848. \brief configure the transmit IP frame checksum offload calculation and insertion
  849. \param[in] desc: the descriptor pointer which users want to configure
  850. \param[in] checksum: IP frame checksum configuration
  851. only one parameter can be selected which is shown as below
  852. \arg ENET_CHECKSUM_DISABLE: checksum insertion disabled
  853. \arg ENET_CHECKSUM_IPV4HEADER: only IP header checksum calculation and insertion are enabled
  854. \arg ENET_CHECKSUM_TCPUDPICMP_SEGMENT: TCP/UDP/ICMP checksum insertion calculated but pseudo-header
  855. \arg ENET_CHECKSUM_TCPUDPICMP_FULL: TCP/UDP/ICMP checksum insertion fully calculated
  856. \param[out] none
  857. \retval none
  858. */
  859. void enet_transmit_checksum_config(enet_descriptors_struct *desc, uint32_t checksum)
  860. {
  861. desc->status &= ~ENET_TDES0_CM;
  862. desc->status |= checksum;
  863. }
  864. /*!
  865. \brief ENET Tx and Rx function enable (include MAC and DMA module)
  866. \param[in] none
  867. \param[out] none
  868. \retval none
  869. */
  870. void enet_enable(void)
  871. {
  872. enet_tx_enable();
  873. enet_rx_enable();
  874. }
  875. /*!
  876. \brief ENET Tx and Rx function disable (include MAC and DMA module)
  877. \param[in] none
  878. \param[out] none
  879. \retval none
  880. */
  881. void enet_disable(void)
  882. {
  883. enet_tx_disable();
  884. enet_rx_disable();
  885. }
  886. /*!
  887. \brief configure MAC address
  888. \param[in] mac_addr: select which MAC address will be set,
  889. only one parameter can be selected which is shown as below
  890. \arg ENET_MAC_ADDRESS0: set MAC address 0 filter
  891. \arg ENET_MAC_ADDRESS1: set MAC address 1 filter
  892. \arg ENET_MAC_ADDRESS2: set MAC address 2 filter
  893. \arg ENET_MAC_ADDRESS3: set MAC address 3 filter
  894. \param[in] paddr: the buffer pointer which stores the MAC address
  895. (little-ending store, such as MAC address is aa:bb:cc:dd:ee:22, the buffer is {22, ee, dd, cc, bb, aa})
  896. \param[out] none
  897. \retval none
  898. */
  899. void enet_mac_address_set(enet_macaddress_enum mac_addr, uint8_t paddr[])
  900. {
  901. REG32(ENET_ADDRH_BASE + (uint32_t)mac_addr) = ENET_SET_MACADDRH(paddr);
  902. REG32(ENET_ADDRL_BASE + (uint32_t)mac_addr) = ENET_SET_MACADDRL(paddr);
  903. }
  904. /*!
  905. \brief get MAC address
  906. \param[in] mac_addr: select which MAC address will be get,
  907. only one parameter can be selected which is shown as below
  908. \arg ENET_MAC_ADDRESS0: get MAC address 0 filter
  909. \arg ENET_MAC_ADDRESS1: get MAC address 1 filter
  910. \arg ENET_MAC_ADDRESS2: get MAC address 2 filter
  911. \arg ENET_MAC_ADDRESS3: get MAC address 3 filter
  912. \param[out] paddr: the buffer pointer which is stored the MAC address
  913. (little-ending store, such as mac address is aa:bb:cc:dd:ee:22, the buffer is {22, ee, dd, cc, bb, aa})
  914. \retval none
  915. */
  916. void enet_mac_address_get(enet_macaddress_enum mac_addr, uint8_t paddr[])
  917. {
  918. paddr[0] = ENET_GET_MACADDR(mac_addr, 0U);
  919. paddr[1] = ENET_GET_MACADDR(mac_addr, 1U);
  920. paddr[2] = ENET_GET_MACADDR(mac_addr, 2U);
  921. paddr[3] = ENET_GET_MACADDR(mac_addr, 3U);
  922. paddr[4] = ENET_GET_MACADDR(mac_addr, 4U);
  923. paddr[5] = ENET_GET_MACADDR(mac_addr, 5U);
  924. }
  925. /*!
  926. \brief get the ENET MAC/MSC/PTP/DMA status flag
  927. \param[in] enet_flag: ENET status flag, refer to enet_flag_enum,
  928. only one parameter can be selected which is shown as below
  929. \arg ENET_MAC_FLAG_MPKR: magic packet received flag
  930. \arg ENET_MAC_FLAG_WUFR: wakeup frame received flag
  931. \arg ENET_MAC_FLAG_FLOWCONTROL: flow control status flag
  932. \arg ENET_MAC_FLAG_WUM: WUM status flag
  933. \arg ENET_MAC_FLAG_MSC: MSC status flag
  934. \arg ENET_MAC_FLAG_MSCR: MSC receive status flag
  935. \arg ENET_MAC_FLAG_MSCT: MSC transmit status flag
  936. \arg ENET_MAC_FLAG_TMST: time stamp trigger status flag
  937. \arg ENET_PTP_FLAG_TSSCO: timestamp second counter overflow flag
  938. \arg ENET_PTP_FLAG_TTM: target time match flag
  939. \arg ENET_MSC_FLAG_RFCE: received frames CRC error flag
  940. \arg ENET_MSC_FLAG_RFAE: received frames alignment error flag
  941. \arg ENET_MSC_FLAG_RGUF: received good unicast frames flag
  942. \arg ENET_MSC_FLAG_TGFSC: transmitted good frames single collision flag
  943. \arg ENET_MSC_FLAG_TGFMSC: transmitted good frames more single collision flag
  944. \arg ENET_MSC_FLAG_TGF: transmitted good frames flag
  945. \arg ENET_DMA_FLAG_TS: transmit status flag
  946. \arg ENET_DMA_FLAG_TPS: transmit process stopped status flag
  947. \arg ENET_DMA_FLAG_TBU: transmit buffer unavailable status flag
  948. \arg ENET_DMA_FLAG_TJT: transmit jabber timeout status flag
  949. \arg ENET_DMA_FLAG_RO: receive overflow status flag
  950. \arg ENET_DMA_FLAG_TU: transmit underflow status flag
  951. \arg ENET_DMA_FLAG_RS: receive status flag
  952. \arg ENET_DMA_FLAG_RBU: receive buffer unavailable status flag
  953. \arg ENET_DMA_FLAG_RPS: receive process stopped status flag
  954. \arg ENET_DMA_FLAG_RWT: receive watchdog timeout status flag
  955. \arg ENET_DMA_FLAG_ET: early transmit status flag
  956. \arg ENET_DMA_FLAG_FBE: fatal bus error status flag
  957. \arg ENET_DMA_FLAG_ER: early receive status flag
  958. \arg ENET_DMA_FLAG_AI: abnormal interrupt summary flag
  959. \arg ENET_DMA_FLAG_NI: normal interrupt summary flag
  960. \arg ENET_DMA_FLAG_EB_DMA_ERROR: DMA error flag
  961. \arg ENET_DMA_FLAG_EB_TRANSFER_ERROR: transfer error flag
  962. \arg ENET_DMA_FLAG_EB_ACCESS_ERROR: access error flag
  963. \arg ENET_DMA_FLAG_MSC: MSC status flag
  964. \arg ENET_DMA_FLAG_WUM: WUM status flag
  965. \arg ENET_DMA_FLAG_TST: timestamp trigger status flag
  966. \param[out] none
  967. \retval FlagStatus: SET or RESET
  968. */
  969. FlagStatus enet_flag_get(enet_flag_enum enet_flag)
  970. {
  971. if(RESET != (ENET_REG_VAL(enet_flag) & BIT(ENET_BIT_POS(enet_flag)))){
  972. return SET;
  973. }else{
  974. return RESET;
  975. }
  976. }
  977. /*!
  978. \brief clear the ENET DMA status flag
  979. \param[in] enet_flag: ENET DMA flag clear, refer to enet_flag_clear_enum
  980. only one parameter can be selected which is shown as below
  981. \arg ENET_DMA_FLAG_TS_CLR: transmit status flag clear
  982. \arg ENET_DMA_FLAG_TPS_CLR: transmit process stopped status flag clear
  983. \arg ENET_DMA_FLAG_TBU_CLR: transmit buffer unavailable status flag clear
  984. \arg ENET_DMA_FLAG_TJT_CLR: transmit jabber timeout status flag clear
  985. \arg ENET_DMA_FLAG_RO_CLR: receive overflow status flag clear
  986. \arg ENET_DMA_FLAG_TU_CLR: transmit underflow status flag clear
  987. \arg ENET_DMA_FLAG_RS_CLR: receive status flag clear
  988. \arg ENET_DMA_FLAG_RBU_CLR: receive buffer unavailable status flag clear
  989. \arg ENET_DMA_FLAG_RPS_CLR: receive process stopped status flag clear
  990. \arg ENET_DMA_FLAG_RWT_CLR: receive watchdog timeout status flag clear
  991. \arg ENET_DMA_FLAG_ET_CLR: early transmit status flag clear
  992. \arg ENET_DMA_FLAG_FBE_CLR: fatal bus error status flag clear
  993. \arg ENET_DMA_FLAG_ER_CLR: early receive status flag clear
  994. \arg ENET_DMA_FLAG_AI_CLR: abnormal interrupt summary flag clear
  995. \arg ENET_DMA_FLAG_NI_CLR: normal interrupt summary flag clear
  996. \param[out] none
  997. \retval none
  998. */
  999. void enet_flag_clear(enet_flag_clear_enum enet_flag)
  1000. {
  1001. /* write 1 to the corresponding bit in ENET_DMA_STAT, to clear it */
  1002. ENET_REG_VAL(enet_flag) = BIT(ENET_BIT_POS(enet_flag));
  1003. }
  1004. /*!
  1005. \brief enable ENET MAC/MSC/DMA interrupt
  1006. \param[in] enet_int: ENET interrupt,
  1007. only one parameter can be selected which is shown as below
  1008. \arg ENET_MAC_INT_WUMIM: WUM interrupt mask
  1009. \arg ENET_MAC_INT_TMSTIM: timestamp trigger interrupt mask
  1010. \arg ENET_MSC_INT_RFCEIM: received frame CRC error interrupt mask
  1011. \arg ENET_MSC_INT_RFAEIM: received frames alignment error interrupt mask
  1012. \arg ENET_MSC_INT_RGUFIM: received good unicast frames interrupt mask
  1013. \arg ENET_MSC_INT_TGFSCIM: transmitted good frames single collision interrupt mask
  1014. \arg ENET_MSC_INT_TGFMSCIM: transmitted good frames more single collision interrupt mask
  1015. \arg ENET_MSC_INT_TGFIM: transmitted good frames interrupt mask
  1016. \arg ENET_DMA_INT_TIE: transmit interrupt enable
  1017. \arg ENET_DMA_INT_TPSIE: transmit process stopped interrupt enable
  1018. \arg ENET_DMA_INT_TBUIE: transmit buffer unavailable interrupt enable
  1019. \arg ENET_DMA_INT_TJTIE: transmit jabber timeout interrupt enable
  1020. \arg ENET_DMA_INT_ROIE: receive overflow interrupt enable
  1021. \arg ENET_DMA_INT_TUIE: transmit underflow interrupt enable
  1022. \arg ENET_DMA_INT_RIE: receive interrupt enable
  1023. \arg ENET_DMA_INT_RBUIE: receive buffer unavailable interrupt enable
  1024. \arg ENET_DMA_INT_RPSIE: receive process stopped interrupt enable
  1025. \arg ENET_DMA_INT_RWTIE: receive watchdog timeout interrupt enable
  1026. \arg ENET_DMA_INT_ETIE: early transmit interrupt enable
  1027. \arg ENET_DMA_INT_FBEIE: fatal bus error interrupt enable
  1028. \arg ENET_DMA_INT_ERIE: early receive interrupt enable
  1029. \arg ENET_DMA_INT_AIE: abnormal interrupt summary enable
  1030. \arg ENET_DMA_INT_NIE: normal interrupt summary enable
  1031. \param[out] none
  1032. \retval none
  1033. */
  1034. void enet_interrupt_enable(enet_int_enum enet_int)
  1035. {
  1036. if(DMA_INTEN_REG_OFFSET == ((uint32_t)enet_int >> 6)){
  1037. /* ENET_DMA_INTEN register interrupt */
  1038. ENET_REG_VAL(enet_int) |= BIT(ENET_BIT_POS(enet_int));
  1039. }else{
  1040. /* other INTMSK register interrupt */
  1041. ENET_REG_VAL(enet_int) &= ~BIT(ENET_BIT_POS(enet_int));
  1042. }
  1043. }
  1044. /*!
  1045. \brief disable ENET MAC/MSC/DMA interrupt
  1046. \param[in] enet_int: ENET interrupt,
  1047. only one parameter can be selected which is shown as below
  1048. \arg ENET_MAC_INT_WUMIM: WUM interrupt mask
  1049. \arg ENET_MAC_INT_TMSTIM: timestamp trigger interrupt mask
  1050. \arg ENET_MSC_INT_RFCEIM: received frame CRC error interrupt mask
  1051. \arg ENET_MSC_INT_RFAEIM: received frames alignment error interrupt mask
  1052. \arg ENET_MSC_INT_RGUFIM: received good unicast frames interrupt mask
  1053. \arg ENET_MSC_INT_TGFSCIM: transmitted good frames single collision interrupt mask
  1054. \arg ENET_MSC_INT_TGFMSCIM: transmitted good frames more single collision interrupt mask
  1055. \arg ENET_MSC_INT_TGFIM: transmitted good frames interrupt mask
  1056. \arg ENET_DMA_INT_TIE: transmit interrupt enable
  1057. \arg ENET_DMA_INT_TPSIE: transmit process stopped interrupt enable
  1058. \arg ENET_DMA_INT_TBUIE: transmit buffer unavailable interrupt enable
  1059. \arg ENET_DMA_INT_TJTIE: transmit jabber timeout interrupt enable
  1060. \arg ENET_DMA_INT_ROIE: receive overflow interrupt enable
  1061. \arg ENET_DMA_INT_TUIE: transmit underflow interrupt enable
  1062. \arg ENET_DMA_INT_RIE: receive interrupt enable
  1063. \arg ENET_DMA_INT_RBUIE: receive buffer unavailable interrupt enable
  1064. \arg ENET_DMA_INT_RPSIE: receive process stopped interrupt enable
  1065. \arg ENET_DMA_INT_RWTIE: receive watchdog timeout interrupt enable
  1066. \arg ENET_DMA_INT_ETIE: early transmit interrupt enable
  1067. \arg ENET_DMA_INT_FBEIE: fatal bus error interrupt enable
  1068. \arg ENET_DMA_INT_ERIE: early receive interrupt enable
  1069. \arg ENET_DMA_INT_AIE: abnormal interrupt summary enable
  1070. \arg ENET_DMA_INT_NIE: normal interrupt summary enable
  1071. \param[out] none
  1072. \retval none
  1073. */
  1074. void enet_interrupt_disable(enet_int_enum enet_int)
  1075. {
  1076. if(DMA_INTEN_REG_OFFSET == ((uint32_t)enet_int >> 6)){
  1077. /* ENET_DMA_INTEN register interrupt */
  1078. ENET_REG_VAL(enet_int) &= ~BIT(ENET_BIT_POS(enet_int));
  1079. }else{
  1080. /* other INTMSK register interrupt */
  1081. ENET_REG_VAL(enet_int) |= BIT(ENET_BIT_POS(enet_int));
  1082. }
  1083. }
  1084. /*!
  1085. \brief get ENET MAC/MSC/DMA interrupt flag
  1086. \param[in] int_flag: ENET interrupt flag,
  1087. only one parameter can be selected which is shown as below
  1088. \arg ENET_MAC_INT_FLAG_WUM: WUM status flag
  1089. \arg ENET_MAC_INT_FLAG_MSC: MSC status flag
  1090. \arg ENET_MAC_INT_FLAG_MSCR: MSC receive status flag
  1091. \arg ENET_MAC_INT_FLAG_MSCT: MSC transmit status flag
  1092. \arg ENET_MAC_INT_FLAG_TMST: time stamp trigger status flag
  1093. \arg ENET_MSC_INT_FLAG_RFCE: received frames CRC error flag
  1094. \arg ENET_MSC_INT_FLAG_RFAE: received frames alignment error flag
  1095. \arg ENET_MSC_INT_FLAG_RGUF: received good unicast frames flag
  1096. \arg ENET_MSC_INT_FLAG_TGFSC: transmitted good frames single collision flag
  1097. \arg ENET_MSC_INT_FLAG_TGFMSC: transmitted good frames more single collision flag
  1098. \arg ENET_MSC_INT_FLAG_TGF: transmitted good frames flag
  1099. \arg ENET_DMA_INT_FLAG_TS: transmit status flag
  1100. \arg ENET_DMA_INT_FLAG_TPS: transmit process stopped status flag
  1101. \arg ENET_DMA_INT_FLAG_TBU: transmit buffer unavailable status flag
  1102. \arg ENET_DMA_INT_FLAG_TJT: transmit jabber timeout status flag
  1103. \arg ENET_DMA_INT_FLAG_RO: receive overflow status flag
  1104. \arg ENET_DMA_INT_FLAG_TU: transmit underflow status flag
  1105. \arg ENET_DMA_INT_FLAG_RS: receive status flag
  1106. \arg ENET_DMA_INT_FLAG_RBU: receive buffer unavailable status flag
  1107. \arg ENET_DMA_INT_FLAG_RPS: receive process stopped status flag
  1108. \arg ENET_DMA_INT_FLAG_RWT: receive watchdog timeout status flag
  1109. \arg ENET_DMA_INT_FLAG_ET: early transmit status flag
  1110. \arg ENET_DMA_INT_FLAG_FBE: fatal bus error status flag
  1111. \arg ENET_DMA_INT_FLAG_ER: early receive status flag
  1112. \arg ENET_DMA_INT_FLAG_AI: abnormal interrupt summary flag
  1113. \arg ENET_DMA_INT_FLAG_NI: normal interrupt summary flag
  1114. \arg ENET_DMA_INT_FLAG_MSC: MSC status flag
  1115. \arg ENET_DMA_INT_FLAG_WUM: WUM status flag
  1116. \arg ENET_DMA_INT_FLAG_TST: timestamp trigger status flag
  1117. \param[out] none
  1118. \retval FlagStatus: SET or RESET
  1119. */
  1120. FlagStatus enet_interrupt_flag_get(enet_int_flag_enum int_flag)
  1121. {
  1122. if(RESET != (ENET_REG_VAL(int_flag) & BIT(ENET_BIT_POS(int_flag)))){
  1123. return SET;
  1124. }else{
  1125. return RESET;
  1126. }
  1127. }
  1128. /*!
  1129. \brief clear ENET DMA interrupt flag
  1130. \param[in] int_flag_clear: clear ENET interrupt flag,
  1131. only one parameter can be selected which is shown as below
  1132. \arg ENET_DMA_INT_FLAG_TS_CLR: transmit status flag
  1133. \arg ENET_DMA_INT_FLAG_TPS_CLR: transmit process stopped status flag
  1134. \arg ENET_DMA_INT_FLAG_TBU_CLR: transmit buffer unavailable status flag
  1135. \arg ENET_DMA_INT_FLAG_TJT_CLR: transmit jabber timeout status flag
  1136. \arg ENET_DMA_INT_FLAG_RO_CLR: receive overflow status flag
  1137. \arg ENET_DMA_INT_FLAG_TU_CLR: transmit underflow status flag
  1138. \arg ENET_DMA_INT_FLAG_RS_CLR: receive status flag
  1139. \arg ENET_DMA_INT_FLAG_RBU_CLR: receive buffer unavailable status flag
  1140. \arg ENET_DMA_INT_FLAG_RPS_CLR: receive process stopped status flag
  1141. \arg ENET_DMA_INT_FLAG_RWT_CLR: receive watchdog timeout status flag
  1142. \arg ENET_DMA_INT_FLAG_ET_CLR: early transmit status flag
  1143. \arg ENET_DMA_INT_FLAG_FBE_CLR: fatal bus error status flag
  1144. \arg ENET_DMA_INT_FLAG_ER_CLR: early receive status flag
  1145. \arg ENET_DMA_INT_FLAG_AI_CLR: abnormal interrupt summary flag
  1146. \arg ENET_DMA_INT_FLAG_NI_CLR: normal interrupt summary flag
  1147. \param[out] none
  1148. \retval none
  1149. */
  1150. void enet_interrupt_flag_clear(enet_int_flag_clear_enum int_flag_clear)
  1151. {
  1152. /* write 1 to the corresponding bit in ENET_DMA_STAT, to clear it */
  1153. ENET_REG_VAL(int_flag_clear) = BIT(ENET_BIT_POS(int_flag_clear));
  1154. }
  1155. /*!
  1156. \brief ENET Tx function enable (include MAC and DMA module)
  1157. \param[in] none
  1158. \param[out] none
  1159. \retval none
  1160. */
  1161. void enet_tx_enable(void)
  1162. {
  1163. ENET_MAC_CFG |= ENET_MAC_CFG_TEN;
  1164. enet_txfifo_flush();
  1165. ENET_DMA_CTL |= ENET_DMA_CTL_STE;
  1166. }
  1167. /*!
  1168. \brief ENET Tx function disable (include MAC and DMA module)
  1169. \param[in] none
  1170. \param[out] none
  1171. \retval none
  1172. */
  1173. void enet_tx_disable(void)
  1174. {
  1175. ENET_DMA_CTL &= ~ENET_DMA_CTL_STE;
  1176. enet_txfifo_flush();
  1177. ENET_MAC_CFG &= ~ENET_MAC_CFG_TEN;
  1178. }
  1179. /*!
  1180. \brief ENET Rx function enable (include MAC and DMA module)
  1181. \param[in] none
  1182. \param[out] none
  1183. \retval none
  1184. */
  1185. void enet_rx_enable(void)
  1186. {
  1187. ENET_MAC_CFG |= ENET_MAC_CFG_REN;
  1188. ENET_DMA_CTL |= ENET_DMA_CTL_SRE;
  1189. }
  1190. /*!
  1191. \brief ENET Rx function disable (include MAC and DMA module)
  1192. \param[in] none
  1193. \param[out] none
  1194. \retval none
  1195. */
  1196. void enet_rx_disable(void)
  1197. {
  1198. ENET_DMA_CTL &= ~ENET_DMA_CTL_SRE;
  1199. ENET_MAC_CFG &= ~ENET_MAC_CFG_REN;
  1200. }
  1201. /*!
  1202. \brief put registers value into the application buffer
  1203. \param[in] type: register type which will be get, refer to enet_registers_type_enum,
  1204. only one parameter can be selected which is shown as below
  1205. \arg ALL_MAC_REG: get the registers within the offset scope between ENET_MAC_CFG and ENET_MAC_FCTH
  1206. \arg ALL_MSC_REG: get the registers within the offset scope between ENET_MSC_CTL and ENET_MSC_RGUFCNT
  1207. \arg ALL_PTP_REG: get the registers within the offset scope between ENET_PTP_TSCTL and ENET_PTP_PPSCTL
  1208. \arg ALL_DMA_REG: get the registers within the offset scope between ENET_DMA_BCTL and ENET_DMA_CRBADDR
  1209. \param[in] num: the number of registers that the user want to get
  1210. \param[out] preg: the application buffer pointer for storing the register value
  1211. \retval none
  1212. */
  1213. void enet_registers_get(enet_registers_type_enum type, uint32_t *preg, uint32_t num)
  1214. {
  1215. uint32_t offset = 0U, max = 0U, limit = 0U;
  1216. offset = (uint32_t)type;
  1217. max = (uint32_t)type + num;
  1218. limit = sizeof(enet_reg_tab)/sizeof(uint16_t);
  1219. /* prevent element in this array is out of range */
  1220. if(max > limit){
  1221. max = limit;
  1222. }
  1223. for(; offset < max; offset++){
  1224. /* get value of the corresponding register */
  1225. *preg = REG32((ENET) + enet_reg_tab[offset]);
  1226. preg++;
  1227. }
  1228. }
  1229. /*!
  1230. \brief get the enet debug status from the debug register
  1231. \param[in] mac_debug: enet debug status,
  1232. only one parameter can be selected which is shown as below
  1233. \arg ENET_MAC_RECEIVER_NOT_IDLE: MAC receiver is not in idle state
  1234. \arg ENET_RX_ASYNCHRONOUS_FIFO_STATE: Rx asynchronous FIFO status
  1235. \arg ENET_RXFIFO_WRITING: RxFIFO is doing write operation
  1236. \arg ENET_RXFIFO_READ_STATUS: RxFIFO read operation status
  1237. \arg ENET_RXFIFO_STATE: RxFIFO state
  1238. \arg ENET_MAC_TRANSMITTER_NOT_IDLE: MAC transmitter is not in idle state
  1239. \arg ENET_MAC_TRANSMITTER_STATUS: status of MAC transmitter
  1240. \arg ENET_PAUSE_CONDITION_STATUS: pause condition status
  1241. \arg ENET_TXFIFO_READ_STATUS: TxFIFO read operation status
  1242. \arg ENET_TXFIFO_WRITING: TxFIFO is doing write operation
  1243. \arg ENET_TXFIFO_NOT_EMPTY: TxFIFO is not empty
  1244. \arg ENET_TXFIFO_FULL: TxFIFO is full
  1245. \param[out] none
  1246. \retval value of the status users want to get
  1247. */
  1248. uint32_t enet_debug_status_get(uint32_t mac_debug)
  1249. {
  1250. uint32_t temp_state = 0U;
  1251. switch(mac_debug){
  1252. case ENET_RX_ASYNCHRONOUS_FIFO_STATE:
  1253. temp_state = GET_MAC_DBG_RXAFS(ENET_MAC_DBG);
  1254. break;
  1255. case ENET_RXFIFO_READ_STATUS:
  1256. temp_state = GET_MAC_DBG_RXFRS(ENET_MAC_DBG);
  1257. break;
  1258. case ENET_RXFIFO_STATE:
  1259. temp_state = GET_MAC_DBG_RXFS(ENET_MAC_DBG);
  1260. break;
  1261. case ENET_MAC_TRANSMITTER_STATUS:
  1262. temp_state = GET_MAC_DBG_SOMT(ENET_MAC_DBG);
  1263. break;
  1264. case ENET_TXFIFO_READ_STATUS:
  1265. temp_state = GET_MAC_DBG_TXFRS(ENET_MAC_DBG);
  1266. break;
  1267. default:
  1268. if(RESET != (ENET_MAC_DBG & mac_debug)){
  1269. temp_state = 0x1U;
  1270. }
  1271. break;
  1272. }
  1273. return temp_state;
  1274. }
  1275. /*!
  1276. \brief enable the MAC address filter
  1277. \param[in] mac_addr: select which MAC address will be enable
  1278. \arg ENET_MAC_ADDRESS1: enable MAC address 1 filter
  1279. \arg ENET_MAC_ADDRESS2: enable MAC address 2 filter
  1280. \arg ENET_MAC_ADDRESS3: enable MAC address 3 filter
  1281. \param[out] none
  1282. \retval none
  1283. */
  1284. void enet_address_filter_enable(enet_macaddress_enum mac_addr)
  1285. {
  1286. REG32(ENET_ADDRH_BASE + mac_addr) |= ENET_MAC_ADDR1H_AFE;
  1287. }
  1288. /*!
  1289. \brief disable the MAC address filter
  1290. \param[in] mac_addr: select which MAC address will be disable,
  1291. only one parameter can be selected which is shown as below
  1292. \arg ENET_MAC_ADDRESS1: disable MAC address 1 filter
  1293. \arg ENET_MAC_ADDRESS2: disable MAC address 2 filter
  1294. \arg ENET_MAC_ADDRESS3: disable MAC address 3 filter
  1295. \param[out] none
  1296. \retval none
  1297. */
  1298. void enet_address_filter_disable(enet_macaddress_enum mac_addr)
  1299. {
  1300. REG32(ENET_ADDRH_BASE + mac_addr) &= ~ENET_MAC_ADDR1H_AFE;
  1301. }
  1302. /*!
  1303. \brief configure the MAC address filter
  1304. \param[in] mac_addr: select which MAC address will be configured,
  1305. only one parameter can be selected which is shown as below
  1306. \arg ENET_MAC_ADDRESS1: configure MAC address 1 filter
  1307. \arg ENET_MAC_ADDRESS2: configure MAC address 2 filter
  1308. \arg ENET_MAC_ADDRESS3: configure MAC address 3 filter
  1309. \param[in] addr_mask: select which MAC address bytes will be mask,
  1310. one or more parameters can be selected which are shown as below
  1311. \arg ENET_ADDRESS_MASK_BYTE0: mask ENET_MAC_ADDR1L[7:0] bits
  1312. \arg ENET_ADDRESS_MASK_BYTE1: mask ENET_MAC_ADDR1L[15:8] bits
  1313. \arg ENET_ADDRESS_MASK_BYTE2: mask ENET_MAC_ADDR1L[23:16] bits
  1314. \arg ENET_ADDRESS_MASK_BYTE3: mask ENET_MAC_ADDR1L [31:24] bits
  1315. \arg ENET_ADDRESS_MASK_BYTE4: mask ENET_MAC_ADDR1H [7:0] bits
  1316. \arg ENET_ADDRESS_MASK_BYTE5: mask ENET_MAC_ADDR1H [15:8] bits
  1317. \param[in] filter_type: select which MAC address filter type will be selected,
  1318. only one parameter can be selected which is shown as below
  1319. \arg ENET_ADDRESS_FILTER_SA: The MAC address is used to compared with the SA field of the received frame
  1320. \arg ENET_ADDRESS_FILTER_DA: The MAC address is used to compared with the DA field of the received frame
  1321. \param[out] none
  1322. \retval none
  1323. */
  1324. void enet_address_filter_config(enet_macaddress_enum mac_addr, uint32_t addr_mask, uint32_t filter_type)
  1325. {
  1326. uint32_t reg;
  1327. /* get the address filter register value which is to be configured */
  1328. reg = REG32(ENET_ADDRH_BASE + mac_addr);
  1329. /* clear and configure the address filter register */
  1330. reg &= ~(ENET_MAC_ADDR1H_MB | ENET_MAC_ADDR1H_SAF);
  1331. reg |= (addr_mask | filter_type);
  1332. REG32(ENET_ADDRH_BASE + mac_addr) = reg;
  1333. }
  1334. /*!
  1335. \brief PHY interface configuration (configure SMI clock and reset PHY chip)
  1336. \param[in] none
  1337. \param[out] none
  1338. \retval ErrStatus: SUCCESS or ERROR
  1339. */
  1340. ErrStatus enet_phy_config(void)
  1341. {
  1342. uint32_t ahbclk;
  1343. uint32_t reg;
  1344. uint16_t phy_value;
  1345. ErrStatus enet_state = ERROR;
  1346. /* clear the previous MDC clock */
  1347. reg = ENET_MAC_PHY_CTL;
  1348. reg &= ~ENET_MAC_PHY_CTL_CLR;
  1349. /* get the HCLK frequency */
  1350. ahbclk = rcu_clock_freq_get(CK_AHB);
  1351. /* configure MDC clock according to HCLK frequency range */
  1352. if(ENET_RANGE(ahbclk, 20000000U, 35000000U)){
  1353. reg |= ENET_MDC_HCLK_DIV16;
  1354. }else if(ENET_RANGE(ahbclk, 35000000U, 60000000U)){
  1355. reg |= ENET_MDC_HCLK_DIV26;
  1356. }else if(ENET_RANGE(ahbclk, 60000000U, 100000000U)){
  1357. reg |= ENET_MDC_HCLK_DIV42;
  1358. }else if((ENET_RANGE(ahbclk, 100000000U, 120000000U))||(120000000U == ahbclk)){
  1359. reg |= ENET_MDC_HCLK_DIV62;
  1360. }else{
  1361. return enet_state;
  1362. }
  1363. ENET_MAC_PHY_CTL = reg;
  1364. /* reset PHY */
  1365. phy_value = PHY_RESET;
  1366. if(ERROR == (enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value))){
  1367. return enet_state;
  1368. }
  1369. /* PHY reset need some time */
  1370. _ENET_DELAY_(ENET_DELAY_TO);
  1371. /* check whether PHY reset is complete */
  1372. if(ERROR == (enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &phy_value))){
  1373. return enet_state;
  1374. }
  1375. /* PHY reset complete */
  1376. if(RESET == (phy_value & PHY_RESET)){
  1377. enet_state = SUCCESS;
  1378. }
  1379. return enet_state;
  1380. }
  1381. /*!
  1382. \brief write to / read from a PHY register
  1383. \param[in] direction: only one parameter can be selected which is shown as below
  1384. \arg ENET_PHY_WRITE: write data to phy register
  1385. \arg ENET_PHY_READ: read data from phy register
  1386. \param[in] phy_address: 0x0 - 0x1F
  1387. \param[in] phy_reg: 0x0 - 0x1F
  1388. \param[in] pvalue: the value will be written to the PHY register in ENET_PHY_WRITE direction
  1389. \param[out] pvalue: the value will be read from the PHY register in ENET_PHY_READ direction
  1390. \retval ErrStatus: SUCCESS or ERROR
  1391. */
  1392. ErrStatus enet_phy_write_read(enet_phydirection_enum direction, uint16_t phy_address, uint16_t phy_reg, uint16_t *pvalue)
  1393. {
  1394. uint32_t reg, phy_flag;
  1395. uint32_t timeout = 0U;
  1396. ErrStatus enet_state = ERROR;
  1397. /* configure ENET_MAC_PHY_CTL with write/read operation */
  1398. reg = ENET_MAC_PHY_CTL;
  1399. reg &= ~(ENET_MAC_PHY_CTL_PB | ENET_MAC_PHY_CTL_PW | ENET_MAC_PHY_CTL_PR | ENET_MAC_PHY_CTL_PA);
  1400. reg |= (direction | MAC_PHY_CTL_PR(phy_reg) | MAC_PHY_CTL_PA(phy_address) | ENET_MAC_PHY_CTL_PB);
  1401. /* if do the write operation, write value to the register */
  1402. if(ENET_PHY_WRITE == direction){
  1403. ENET_MAC_PHY_DATA = *pvalue;
  1404. }
  1405. /* do PHY write/read operation, and wait the operation complete */
  1406. ENET_MAC_PHY_CTL = reg;
  1407. do{
  1408. phy_flag = (ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB);
  1409. timeout++;
  1410. }
  1411. while((RESET != phy_flag) && (ENET_DELAY_TO != timeout));
  1412. /* write/read operation complete */
  1413. if(RESET == (ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB)){
  1414. enet_state = SUCCESS;
  1415. }
  1416. /* if do the read operation, get value from the register */
  1417. if(ENET_PHY_READ == direction){
  1418. *pvalue = (uint16_t)ENET_MAC_PHY_DATA;
  1419. }
  1420. return enet_state;
  1421. }
  1422. /*!
  1423. \brief enable the loopback function of PHY chip
  1424. \param[in] none
  1425. \param[out] none
  1426. \retval ErrStatus: ERROR or SUCCESS
  1427. */
  1428. ErrStatus enet_phyloopback_enable(void)
  1429. {
  1430. uint16_t temp_phy = 0U;
  1431. ErrStatus phy_state = ERROR;
  1432. /* get the PHY configuration to update it */
  1433. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
  1434. /* enable the PHY loopback mode */
  1435. temp_phy |= PHY_LOOPBACK;
  1436. /* update the PHY control register with the new configuration */
  1437. phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
  1438. return phy_state;
  1439. }
  1440. /*!
  1441. \brief disable the loopback function of PHY chip
  1442. \param[in] none
  1443. \param[out] none
  1444. \retval ErrStatus: ERROR or SUCCESS
  1445. */
  1446. ErrStatus enet_phyloopback_disable(void)
  1447. {
  1448. uint16_t temp_phy = 0U;
  1449. ErrStatus phy_state = ERROR;
  1450. /* get the PHY configuration to update it */
  1451. enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
  1452. /* disable the PHY loopback mode */
  1453. temp_phy &= (uint16_t)~PHY_LOOPBACK;
  1454. /* update the PHY control register with the new configuration */
  1455. phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
  1456. return phy_state;
  1457. }
  1458. /*!
  1459. \brief enable ENET forward feature
  1460. \param[in] feature: the feature of ENET forward mode,
  1461. one or more parameters can be selected which are shown as below
  1462. \arg ENET_AUTO_PADCRC_DROP: the function of the MAC strips the Pad/FCS field on received frames
  1463. \arg ENET_TYPEFRAME_CRC_DROP: the function that FCS field(last 4 bytes) of frame will be dropped before forwarding
  1464. \arg ENET_FORWARD_ERRFRAMES: the function that all frame received with error except runt error are forwarded to memory
  1465. \arg ENET_FORWARD_UNDERSZ_GOODFRAMES: the function that forwarding undersized good frames
  1466. \param[out] none
  1467. \retval none
  1468. */
  1469. void enet_forward_feature_enable(uint32_t feature)
  1470. {
  1471. uint32_t mask;
  1472. mask = (feature & (~(ENET_FORWARD_ERRFRAMES | ENET_FORWARD_UNDERSZ_GOODFRAMES)));
  1473. ENET_MAC_CFG |= mask;
  1474. mask = (feature & (~(ENET_AUTO_PADCRC_DROP | ENET_TYPEFRAME_CRC_DROP)));
  1475. ENET_DMA_CTL |= (mask >> 2);
  1476. }
  1477. /*!
  1478. \brief disable ENET forward feature
  1479. \param[in] feature: the feature of ENET forward mode,
  1480. one or more parameters can be selected which are shown as below
  1481. \arg ENET_AUTO_PADCRC_DROP: the automatic zero-quanta generation function
  1482. \arg ENET_TYPEFRAME_CRC_DROP: the flow control operation in the MAC
  1483. \arg ENET_FORWARD_ERRFRAMES: decoding function for the received pause frame and process it
  1484. \arg ENET_FORWARD_UNDERSZ_GOODFRAMES: back pressure operation in the MAC(only use in half-dulex mode)
  1485. \param[out] none
  1486. \retval none
  1487. */
  1488. void enet_forward_feature_disable(uint32_t feature)
  1489. {
  1490. uint32_t mask;
  1491. mask = (feature & (~(ENET_FORWARD_ERRFRAMES | ENET_FORWARD_UNDERSZ_GOODFRAMES)));
  1492. ENET_MAC_CFG &= ~mask;
  1493. mask = (feature & (~(ENET_AUTO_PADCRC_DROP | ENET_TYPEFRAME_CRC_DROP)));
  1494. ENET_DMA_CTL &= ~(mask >> 2);
  1495. }
  1496. /*!
  1497. \brief enable ENET fliter feature
  1498. \param[in] feature: the feature of ENET fliter mode,
  1499. one or more parameters can be selected which are shown as below
  1500. \arg ENET_SRC_FILTER: filter source address function
  1501. \arg ENET_SRC_FILTER_INVERSE: inverse source address filtering result function
  1502. \arg ENET_DEST_FILTER_INVERSE: inverse DA filtering result function
  1503. \arg ENET_MULTICAST_FILTER_PASS: pass all multicast frames function
  1504. \arg ENET_MULTICAST_FILTER_HASH_MODE: HASH multicast filter function
  1505. \arg ENET_UNICAST_FILTER_HASH_MODE: HASH unicast filter function
  1506. \arg ENET_FILTER_MODE_EITHER: HASH or perfect filter function
  1507. \param[out] none
  1508. \retval none
  1509. */
  1510. void enet_fliter_feature_enable(uint32_t feature)
  1511. {
  1512. ENET_MAC_FRMF |= feature;
  1513. }
  1514. /*!
  1515. \brief disable ENET fliter feature
  1516. \param[in] feature: the feature of ENET fliter mode,
  1517. one or more parameters can be selected which are shown as below
  1518. \arg ENET_SRC_FILTER: filter source address function
  1519. \arg ENET_SRC_FILTER_INVERSE: inverse source address filtering result function
  1520. \arg ENET_DEST_FILTER_INVERSE: inverse DA filtering result function
  1521. \arg ENET_MULTICAST_FILTER_PASS: pass all multicast frames function
  1522. \arg ENET_MULTICAST_FILTER_HASH_MODE: HASH multicast filter function
  1523. \arg ENET_UNICAST_FILTER_HASH_MODE: HASH unicast filter function
  1524. \arg ENET_FILTER_MODE_EITHER: HASH or perfect filter function
  1525. \param[out] none
  1526. \retval none
  1527. */
  1528. void enet_fliter_feature_disable(uint32_t feature)
  1529. {
  1530. ENET_MAC_FRMF &= ~feature;
  1531. }
  1532. /*!
  1533. \brief generate the pause frame, ENET will send pause frame after enable transmit flow control
  1534. this function only use in full-dulex mode
  1535. \param[in] none
  1536. \param[out] none
  1537. \retval ErrStatus: ERROR or SUCCESS
  1538. */
  1539. ErrStatus enet_pauseframe_generate(void)
  1540. {
  1541. ErrStatus enet_state =ERROR;
  1542. uint32_t temp = 0U;
  1543. /* in full-duplex mode, must make sure this bit is 0 before writing register */
  1544. temp = ENET_MAC_FCTL & ENET_MAC_FCTL_FLCBBKPA;
  1545. if(RESET == temp){
  1546. ENET_MAC_FCTL |= ENET_MAC_FCTL_FLCBBKPA;
  1547. enet_state = SUCCESS;
  1548. }
  1549. return enet_state;
  1550. }
  1551. /*!
  1552. \brief configure the pause frame detect type
  1553. \param[in] detect: pause frame detect type,
  1554. only one parameter can be selected which is shown as below
  1555. \arg ENET_MAC0_AND_UNIQUE_ADDRESS_PAUSEDETECT: besides the unique multicast address, MAC can also
  1556. use the MAC0 address to detecting pause frame
  1557. \arg ENET_UNIQUE_PAUSEDETECT: only the unique multicast address for pause frame which is specified
  1558. in IEEE802.3 can be detected
  1559. \param[out] none
  1560. \retval none
  1561. */
  1562. void enet_pauseframe_detect_config(uint32_t detect)
  1563. {
  1564. ENET_MAC_FCTL &= ~ENET_MAC_FCTL_UPFDT;
  1565. ENET_MAC_FCTL |= detect;
  1566. }
  1567. /*!
  1568. \brief configure the pause frame parameters
  1569. \param[in] pausetime: pause time in transmit pause control frame
  1570. \param[in] pause_threshold: the threshold of the pause timer for retransmitting frames automatically,
  1571. this value must make sure to be less than configured pause time, only one parameter can be
  1572. selected which is shown as below
  1573. \arg ENET_PAUSETIME_MINUS4: pause time minus 4 slot times
  1574. \arg ENET_PAUSETIME_MINUS28: pause time minus 28 slot times
  1575. \arg ENET_PAUSETIME_MINUS144: pause time minus 144 slot times
  1576. \arg ENET_PAUSETIME_MINUS256: pause time minus 256 slot times
  1577. \param[out] none
  1578. \retval none
  1579. */
  1580. void enet_pauseframe_config(uint32_t pausetime, uint32_t pause_threshold)
  1581. {
  1582. ENET_MAC_FCTL &= ~(ENET_MAC_FCTL_PTM | ENET_MAC_FCTL_PLTS);
  1583. ENET_MAC_FCTL |= (MAC_FCTL_PTM(pausetime) | pause_threshold);
  1584. }
  1585. /*!
  1586. \brief configure the threshold of the flow control(deactive and active threshold)
  1587. \param[in] deactive: the threshold of the deactive flow control, this value
  1588. should always be less than active flow control value, only one
  1589. parameter can be selected which is shown as below
  1590. \arg ENET_DEACTIVE_THRESHOLD_256BYTES: threshold level is 256 bytes
  1591. \arg ENET_DEACTIVE_THRESHOLD_512BYTES: threshold level is 512 bytes
  1592. \arg ENET_DEACTIVE_THRESHOLD_768BYTES: threshold level is 768 bytes
  1593. \arg ENET_DEACTIVE_THRESHOLD_1024BYTES: threshold level is 1024 bytes
  1594. \arg ENET_DEACTIVE_THRESHOLD_1280BYTES: threshold level is 1280 bytes
  1595. \arg ENET_DEACTIVE_THRESHOLD_1536BYTES: threshold level is 1536 bytes
  1596. \arg ENET_DEACTIVE_THRESHOLD_1792BYTES: threshold level is 1792 bytes
  1597. \param[in] active: the threshold of the active flow control, only one parameter
  1598. can be selected which is shown as below
  1599. \arg ENET_ACTIVE_THRESHOLD_256BYTES: threshold level is 256 bytes
  1600. \arg ENET_ACTIVE_THRESHOLD_512BYTES: threshold level is 512 bytes
  1601. \arg ENET_ACTIVE_THRESHOLD_768BYTES: threshold level is 768 bytes
  1602. \arg ENET_ACTIVE_THRESHOLD_1024BYTES: threshold level is 1024 bytes
  1603. \arg ENET_ACTIVE_THRESHOLD_1280BYTES: threshold level is 1280 bytes
  1604. \arg ENET_ACTIVE_THRESHOLD_1536BYTES: threshold level is 1536 bytes
  1605. \arg ENET_ACTIVE_THRESHOLD_1792BYTES: threshold level is 1792 bytes
  1606. \param[out] none
  1607. \retval none
  1608. */
  1609. void enet_flowcontrol_threshold_config(uint32_t deactive, uint32_t active)
  1610. {
  1611. ENET_MAC_FCTH = ((deactive | active) >> 8);
  1612. }
  1613. /*!
  1614. \brief enable ENET flow control feature
  1615. \param[in] feature: the feature of ENET flow control mode
  1616. one or more parameters can be selected which are shown as below
  1617. \arg ENET_ZERO_QUANTA_PAUSE: the automatic zero-quanta generation function
  1618. \arg ENET_TX_FLOWCONTROL: the flow control operation in the MAC
  1619. \arg ENET_RX_FLOWCONTROL: decoding function for the received pause frame and process it
  1620. \arg ENET_BACK_PRESSURE: back pressure operation in the MAC(only use in half-dulex mode)
  1621. \param[out] none
  1622. \retval none
  1623. */
  1624. void enet_flowcontrol_feature_enable(uint32_t feature)
  1625. {
  1626. if(RESET != (feature & ENET_ZERO_QUANTA_PAUSE)){
  1627. ENET_MAC_FCTL &= ~ENET_ZERO_QUANTA_PAUSE;
  1628. }
  1629. feature &= ~ENET_ZERO_QUANTA_PAUSE;
  1630. ENET_MAC_FCTL |= feature;
  1631. }
  1632. /*!
  1633. \brief disable ENET flow control feature
  1634. \param[in] feature: the feature of ENET flow control mode
  1635. one or more parameters can be selected which are shown as below
  1636. \arg ENET_ZERO_QUANTA_PAUSE: the automatic zero-quanta generation function
  1637. \arg ENET_TX_FLOWCONTROL: the flow control operation in the MAC
  1638. \arg ENET_RX_FLOWCONTROL: decoding function for the received pause frame and process it
  1639. \arg ENET_BACK_PRESSURE: back pressure operation in the MAC(only use in half-dulex mode)
  1640. \param[out] none
  1641. \retval none
  1642. */
  1643. void enet_flowcontrol_feature_disable(uint32_t feature)
  1644. {
  1645. if(RESET != (feature & ENET_ZERO_QUANTA_PAUSE)){
  1646. ENET_MAC_FCTL |= ENET_ZERO_QUANTA_PAUSE;
  1647. }
  1648. feature &= ~ENET_ZERO_QUANTA_PAUSE;
  1649. ENET_MAC_FCTL &= ~feature;
  1650. }
  1651. /*!
  1652. \brief get the dma transmit/receive process state
  1653. \param[in] direction: choose the direction of dma process which users want to check,
  1654. refer to enet_dmadirection_enum, only one parameter can be selected which is shown as below
  1655. \arg ENET_DMA_TX: dma transmit process
  1656. \arg ENET_DMA_RX: dma receive process
  1657. \param[out] none
  1658. \retval state of dma process, the value range shows below:
  1659. ENET_RX_STATE_STOPPED, ENET_RX_STATE_FETCHING, ENET_RX_STATE_WAITING,
  1660. ENET_RX_STATE_SUSPENDED, ENET_RX_STATE_CLOSING, ENET_RX_STATE_QUEUING,
  1661. ENET_TX_STATE_STOPPED, ENET_TX_STATE_FETCHING, ENET_TX_STATE_WAITING,
  1662. ENET_TX_STATE_READING, ENET_TX_STATE_SUSPENDED, ENET_TX_STATE_CLOSING
  1663. */
  1664. uint32_t enet_dmaprocess_state_get(enet_dmadirection_enum direction)
  1665. {
  1666. uint32_t reval;
  1667. reval = (uint32_t)(ENET_DMA_STAT & (uint32_t)direction);
  1668. return reval;
  1669. }
  1670. /*!
  1671. \brief poll the DMA transmission/reception enable by writing any value to the
  1672. ENET_DMA_TPEN/ENET_DMA_RPEN register, this will make the DMA to resume transmission/reception
  1673. \param[in] direction: choose the direction of DMA process which users want to resume,
  1674. refer to enet_dmadirection_enum, only one parameter can be selected which is shown as below
  1675. \arg ENET_DMA_TX: DMA transmit process
  1676. \arg ENET_DMA_RX: DMA receive process
  1677. \param[out] none
  1678. \retval none
  1679. */
  1680. void enet_dmaprocess_resume(enet_dmadirection_enum direction)
  1681. {
  1682. if(ENET_DMA_TX == direction){
  1683. ENET_DMA_TPEN = 0U;
  1684. }else{
  1685. ENET_DMA_RPEN = 0U;
  1686. }
  1687. }
  1688. /*!
  1689. \brief check and recover the Rx process
  1690. \param[in] none
  1691. \param[out] none
  1692. \retval none
  1693. */
  1694. void enet_rxprocess_check_recovery(void)
  1695. {
  1696. uint32_t status;
  1697. /* get DAV information of current RxDMA descriptor */
  1698. status = dma_current_rxdesc->status;
  1699. status &= ENET_RDES0_DAV;
  1700. /* if current descriptor is owned by DMA, but the descriptor address mismatches with
  1701. receive descriptor address pointer updated by RxDMA controller */
  1702. if((ENET_DMA_CRDADDR != ((uint32_t)dma_current_rxdesc)) &&
  1703. (ENET_RDES0_DAV == status)){
  1704. dma_current_rxdesc = (enet_descriptors_struct*)ENET_DMA_CRDADDR;
  1705. }
  1706. }
  1707. /*!
  1708. \brief flush the ENET transmit FIFO, and wait until the flush operation completes
  1709. \param[in] none
  1710. \param[out] none
  1711. \retval ErrStatus: ERROR or SUCCESS
  1712. */
  1713. ErrStatus enet_txfifo_flush(void)
  1714. {
  1715. uint32_t flush_state;
  1716. uint32_t timeout = 0U;
  1717. ErrStatus enet_state = ERROR;
  1718. /* set the FTF bit for flushing transmit FIFO */
  1719. ENET_DMA_CTL |= ENET_DMA_CTL_FTF;
  1720. /* wait until the flush operation completes */
  1721. do{
  1722. flush_state = ENET_DMA_CTL & ENET_DMA_CTL_FTF;
  1723. timeout++;
  1724. }while((RESET != flush_state) && (timeout < ENET_DELAY_TO));
  1725. /* return ERROR due to timeout */
  1726. if(RESET == flush_state){
  1727. enet_state = SUCCESS;
  1728. }
  1729. return enet_state;
  1730. }
  1731. /*!
  1732. \brief get the transmit/receive address of current descriptor, or current buffer, or descriptor table
  1733. \param[in] addr_get: choose the address which users want to get, refer to enet_desc_reg_enum,
  1734. only one parameter can be selected which is shown as below
  1735. \arg ENET_RX_DESC_TABLE: the start address of the receive descriptor table
  1736. \arg ENET_RX_CURRENT_DESC: the start descriptor address of the current receive descriptor read by
  1737. the RxDMA controller
  1738. \arg ENET_RX_CURRENT_BUFFER: the current receive buffer address being read by the RxDMA controller
  1739. \arg ENET_TX_DESC_TABLE: the start address of the transmit descriptor table
  1740. \arg ENET_TX_CURRENT_DESC: the start descriptor address of the current transmit descriptor read by
  1741. the TxDMA controller
  1742. \arg ENET_TX_CURRENT_BUFFER: the current transmit buffer address being read by the TxDMA controller
  1743. \param[out] none
  1744. \retval address value
  1745. */
  1746. uint32_t enet_current_desc_address_get(enet_desc_reg_enum addr_get)
  1747. {
  1748. uint32_t reval = 0U;
  1749. reval = REG32((ENET) +(uint32_t)addr_get);
  1750. return reval;
  1751. }
  1752. /*!
  1753. \brief get the Tx or Rx descriptor information
  1754. \param[in] desc: the descriptor pointer which users want to get information
  1755. \param[in] info_get: the descriptor information type which is selected,
  1756. only one parameter can be selected which is shown as below
  1757. \arg RXDESC_BUFFER_1_SIZE: receive buffer 1 size
  1758. \arg RXDESC_BUFFER_2_SIZE: receive buffer 2 size
  1759. \arg RXDESC_FRAME_LENGTH: the byte length of the received frame that was transferred to the buffer
  1760. \arg TXDESC_COLLISION_COUNT: the number of collisions occurred before the frame was transmitted
  1761. \arg RXDESC_BUFFER_1_ADDR: the buffer1 address of the Rx frame
  1762. \arg TXDESC_BUFFER_1_ADDR: the buffer1 address of the Tx frame
  1763. \param[out] none
  1764. \retval descriptor information, if value is 0xFFFFFFFFU, means the false input parameter
  1765. */
  1766. uint32_t enet_desc_information_get(enet_descriptors_struct *desc, enet_descstate_enum info_get)
  1767. {
  1768. uint32_t reval = 0xFFFFFFFFU;
  1769. switch(info_get){
  1770. case RXDESC_BUFFER_1_SIZE:
  1771. reval = GET_RDES1_RB1S(desc->control_buffer_size);
  1772. break;
  1773. case RXDESC_BUFFER_2_SIZE:
  1774. reval = GET_RDES1_RB2S(desc->control_buffer_size);
  1775. break;
  1776. case RXDESC_FRAME_LENGTH:
  1777. reval = GET_RDES0_FRML(desc->status);
  1778. reval = reval - 4U;
  1779. /* if is a type frame, and CRC is not included in forwarding frame */
  1780. if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (desc->status & ENET_RDES0_FRMT))){
  1781. reval = reval + 4U;
  1782. }
  1783. break;
  1784. case RXDESC_BUFFER_1_ADDR:
  1785. reval = desc->buffer1_addr;
  1786. break;
  1787. case TXDESC_BUFFER_1_ADDR:
  1788. reval = desc->buffer1_addr;
  1789. break;
  1790. case TXDESC_COLLISION_COUNT:
  1791. reval = GET_TDES0_COCNT(desc->status);
  1792. break;
  1793. default:
  1794. break;
  1795. }
  1796. return reval;
  1797. }
  1798. /*!
  1799. \brief get the number of missed frames during receiving
  1800. \param[in] none
  1801. \param[out] rxfifo_drop: pointer to the number of frames dropped by RxFIFO
  1802. \param[out] rxdma_drop: pointer to the number of frames missed by the RxDMA controller
  1803. \retval none
  1804. */
  1805. void enet_missed_frame_counter_get(uint32_t *rxfifo_drop, uint32_t *rxdma_drop)
  1806. {
  1807. uint32_t temp_counter = 0U;
  1808. temp_counter = ENET_DMA_MFBOCNT;
  1809. *rxfifo_drop = GET_DMA_MFBOCNT_MSFA(temp_counter);
  1810. *rxdma_drop = GET_DMA_MFBOCNT_MSFC(temp_counter);
  1811. }
  1812. /*!
  1813. \brief get the bit flag of ENET DMA descriptor
  1814. \param[in] desc: the descriptor pointer which users want to get flag
  1815. \param[in] desc_flag: the bit flag of ENET DMA descriptor,
  1816. only one parameter can be selected which is shown as below
  1817. \arg ENET_TDES0_DB: deferred
  1818. \arg ENET_TDES0_UFE: underflow error
  1819. \arg ENET_TDES0_EXD: excessive deferral
  1820. \arg ENET_TDES0_VFRM: VLAN frame
  1821. \arg ENET_TDES0_ECO: excessive collision
  1822. \arg ENET_TDES0_LCO: late collision
  1823. \arg ENET_TDES0_NCA: no carrier
  1824. \arg ENET_TDES0_LCA: loss of carrier
  1825. \arg ENET_TDES0_IPPE: IP payload error
  1826. \arg ENET_TDES0_FRMF: frame flushed
  1827. \arg ENET_TDES0_JT: jabber timeout
  1828. \arg ENET_TDES0_ES: error summary
  1829. \arg ENET_TDES0_IPHE: IP header error
  1830. \arg ENET_TDES0_TTMSS: transmit timestamp status
  1831. \arg ENET_TDES0_TCHM: the second address chained mode
  1832. \arg ENET_TDES0_TERM: transmit end of ring mode
  1833. \arg ENET_TDES0_TTSEN: transmit timestamp function enable
  1834. \arg ENET_TDES0_DPAD: disable adding pad
  1835. \arg ENET_TDES0_DCRC: disable CRC
  1836. \arg ENET_TDES0_FSG: first segment
  1837. \arg ENET_TDES0_LSG: last segment
  1838. \arg ENET_TDES0_INTC: interrupt on completion
  1839. \arg ENET_TDES0_DAV: DAV bit
  1840. \arg ENET_RDES0_PCERR: payload checksum error
  1841. \arg ENET_RDES0_EXSV: extended status valid
  1842. \arg ENET_RDES0_CERR: CRC error
  1843. \arg ENET_RDES0_DBERR: dribble bit error
  1844. \arg ENET_RDES0_RERR: receive error
  1845. \arg ENET_RDES0_RWDT: receive watchdog timeout
  1846. \arg ENET_RDES0_FRMT: frame type
  1847. \arg ENET_RDES0_LCO: late collision
  1848. \arg ENET_RDES0_IPHERR: IP frame header error
  1849. \arg ENET_RDES0_TSV: timestamp valid
  1850. \arg ENET_RDES0_LDES: last descriptor
  1851. \arg ENET_RDES0_FDES: first descriptor
  1852. \arg ENET_RDES0_VTAG: VLAN tag
  1853. \arg ENET_RDES0_OERR: overflow error
  1854. \arg ENET_RDES0_LERR: length error
  1855. \arg ENET_RDES0_SAFF: SA filter fail
  1856. \arg ENET_RDES0_DERR: descriptor error
  1857. \arg ENET_RDES0_ERRS: error summary
  1858. \arg ENET_RDES0_DAFF: destination address filter fail
  1859. \arg ENET_RDES0_DAV: descriptor available
  1860. \param[out] none
  1861. \retval FlagStatus: SET or RESET
  1862. */
  1863. FlagStatus enet_desc_flag_get(enet_descriptors_struct *desc, uint32_t desc_flag)
  1864. {
  1865. FlagStatus enet_flag = RESET;
  1866. if ((uint32_t)RESET != (desc->status & desc_flag)){
  1867. enet_flag = SET;
  1868. }
  1869. return enet_flag;
  1870. }
  1871. /*!
  1872. \brief set the bit flag of ENET DMA descriptor
  1873. \param[in] desc: the descriptor pointer which users want to set flag
  1874. \param[in] desc_flag: the bit flag of ENET DMA descriptor,
  1875. only one parameter can be selected which is shown as below
  1876. \arg ENET_TDES0_VFRM: VLAN frame
  1877. \arg ENET_TDES0_FRMF: frame flushed
  1878. \arg ENET_TDES0_TCHM: the second address chained mode
  1879. \arg ENET_TDES0_TERM: transmit end of ring mode
  1880. \arg ENET_TDES0_TTSEN: transmit timestamp function enable
  1881. \arg ENET_TDES0_DPAD: disable adding pad
  1882. \arg ENET_TDES0_DCRC: disable CRC
  1883. \arg ENET_TDES0_FSG: first segment
  1884. \arg ENET_TDES0_LSG: last segment
  1885. \arg ENET_TDES0_INTC: interrupt on completion
  1886. \arg ENET_TDES0_DAV: DAV bit
  1887. \arg ENET_RDES0_DAV: descriptor available
  1888. \param[out] none
  1889. \retval none
  1890. */
  1891. void enet_desc_flag_set(enet_descriptors_struct *desc, uint32_t desc_flag)
  1892. {
  1893. desc->status |= desc_flag;
  1894. }
  1895. /*!
  1896. \brief clear the bit flag of ENET DMA descriptor
  1897. \param[in] desc: the descriptor pointer which users want to clear flag
  1898. \param[in] desc_flag: the bit flag of ENET DMA descriptor,
  1899. only one parameter can be selected which is shown as below
  1900. \arg ENET_TDES0_VFRM: VLAN frame
  1901. \arg ENET_TDES0_FRMF: frame flushed
  1902. \arg ENET_TDES0_TCHM: the second address chained mode
  1903. \arg ENET_TDES0_TERM: transmit end of ring mode
  1904. \arg ENET_TDES0_TTSEN: transmit timestamp function enable
  1905. \arg ENET_TDES0_DPAD: disable adding pad
  1906. \arg ENET_TDES0_DCRC: disable CRC
  1907. \arg ENET_TDES0_FSG: first segment
  1908. \arg ENET_TDES0_LSG: last segment
  1909. \arg ENET_TDES0_INTC: interrupt on completion
  1910. \arg ENET_TDES0_DAV: DAV bit
  1911. \arg ENET_RDES0_DAV: descriptor available
  1912. \param[out] none
  1913. \retval none
  1914. */
  1915. void enet_desc_flag_clear(enet_descriptors_struct *desc, uint32_t desc_flag)
  1916. {
  1917. desc->status &= ~desc_flag;
  1918. }
  1919. /*!
  1920. \brief when receiving completed, set RS bit in ENET_DMA_STAT register will immediately set
  1921. \param[in] desc: the descriptor pointer which users want to configure
  1922. \param[out] none
  1923. \retval none
  1924. */
  1925. void enet_rx_desc_immediate_receive_complete_interrupt(enet_descriptors_struct *desc)
  1926. {
  1927. desc->control_buffer_size &= ~ENET_RDES1_DINTC;
  1928. }
  1929. /*!
  1930. \brief when receiving completed, set RS bit in ENET_DMA_STAT register will is set after a configurable delay time
  1931. \param[in] desc: the descriptor pointer which users want to configure
  1932. \param[in] delay_time: delay a time of 256*delay_time HCLK, this value must be between 0 and 0xFF
  1933. \param[out] none
  1934. \retval none
  1935. */
  1936. void enet_rx_desc_delay_receive_complete_interrupt(enet_descriptors_struct *desc, uint32_t delay_time)
  1937. {
  1938. desc->control_buffer_size |= ENET_RDES1_DINTC;
  1939. ENET_DMA_RSWDC = DMA_RSWDC_WDCFRS(delay_time);
  1940. }
  1941. /*!
  1942. \brief drop current receive frame
  1943. \param[in] none
  1944. \param[out] none
  1945. \retval none
  1946. */
  1947. void enet_rxframe_drop(void)
  1948. {
  1949. /* enable reception, descriptor is owned by DMA */
  1950. dma_current_rxdesc->status = ENET_RDES0_DAV;
  1951. /* chained mode */
  1952. if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
  1953. if(NULL != dma_current_ptp_rxdesc){
  1954. dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->buffer2_next_desc_addr);
  1955. /* if it is the last ptp descriptor */
  1956. if(0U != dma_current_ptp_rxdesc->status){
  1957. /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
  1958. dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
  1959. }else{
  1960. /* ponter to the next ptp descriptor */
  1961. dma_current_ptp_rxdesc++;
  1962. }
  1963. }else{
  1964. dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
  1965. }
  1966. }else{
  1967. /* ring mode */
  1968. if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
  1969. /* if is the last descriptor in table, the next descriptor is the table header */
  1970. dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
  1971. if(NULL != dma_current_ptp_rxdesc){
  1972. dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
  1973. }
  1974. }else{
  1975. /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
  1976. dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
  1977. if(NULL != dma_current_ptp_rxdesc){
  1978. dma_current_ptp_rxdesc++;
  1979. }
  1980. }
  1981. }
  1982. }
  1983. /*!
  1984. \brief enable DMA feature
  1985. \param[in] feature: the feature of DMA mode,
  1986. one or more parameters can be selected which are shown as below
  1987. \arg ENET_NO_FLUSH_RXFRAME: RxDMA does not flushes frames function
  1988. \arg ENET_SECONDFRAME_OPT: TxDMA controller operate on second frame function
  1989. \param[out] none
  1990. \retval none
  1991. */
  1992. void enet_dma_feature_enable(uint32_t feature)
  1993. {
  1994. ENET_DMA_CTL |= feature;
  1995. }
  1996. /*!
  1997. \brief disable DMA feature
  1998. \param[in] feature: the feature of DMA mode,
  1999. one or more parameters can be selected which are shown as below
  2000. \arg ENET_NO_FLUSH_RXFRAME: RxDMA does not flushes frames function
  2001. \arg ENET_SECONDFRAME_OPT: TxDMA controller operate on second frame function
  2002. \param[out] none
  2003. \retval none
  2004. */
  2005. void enet_dma_feature_disable(uint32_t feature)
  2006. {
  2007. ENET_DMA_CTL &= ~feature;
  2008. }
  2009. #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
  2010. /*!
  2011. \brief get the bit of extended status flag in ENET DMA descriptor
  2012. \param[in] desc: the descriptor pointer which users want to get the extended status flag
  2013. \param[in] desc_status: the extended status want to get,
  2014. only one parameter can be selected which is shown as below
  2015. \arg ENET_RDES4_IPPLDT: IP frame payload type
  2016. \arg ENET_RDES4_IPHERR: IP frame header error
  2017. \arg ENET_RDES4_IPPLDERR: IP frame payload error
  2018. \arg ENET_RDES4_IPCKSB: IP frame checksum bypassed
  2019. \arg ENET_RDES4_IPF4: IP frame in version 4
  2020. \arg ENET_RDES4_IPF6: IP frame in version 6
  2021. \arg ENET_RDES4_PTPMT: PTP message type
  2022. \arg ENET_RDES4_PTPOEF: PTP on ethernet frame
  2023. \arg ENET_RDES4_PTPVF: PTP version format
  2024. \param[out] none
  2025. \retval value of extended status
  2026. */
  2027. uint32_t enet_rx_desc_enhanced_status_get(enet_descriptors_struct *desc, uint32_t desc_status)
  2028. {
  2029. uint32_t reval = 0xFFFFFFFFU;
  2030. switch (desc_status){
  2031. case ENET_RDES4_IPPLDT:
  2032. reval = GET_RDES4_IPPLDT(desc->extended_status);
  2033. break;
  2034. case ENET_RDES4_PTPMT:
  2035. reval = GET_RDES4_PTPMT(desc->extended_status);
  2036. break;
  2037. default:
  2038. if ((uint32_t)RESET != (desc->extended_status & desc_status)){
  2039. reval = 1U;
  2040. }else{
  2041. reval = 0U;
  2042. }
  2043. }
  2044. return reval;
  2045. }
  2046. /*!
  2047. \brief configure descriptor to work in enhanced mode
  2048. \param[in] none
  2049. \param[out] none
  2050. \retval none
  2051. */
  2052. void enet_desc_select_enhanced_mode(void)
  2053. {
  2054. ENET_DMA_BCTL |= ENET_DMA_BCTL_DFM;
  2055. }
  2056. /*!
  2057. \brief initialize the DMA Tx/Rx descriptors's parameters in enhanced chain mode with ptp function
  2058. \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
  2059. only one parameter can be selected which is shown as below
  2060. \arg ENET_DMA_TX: DMA Tx descriptors
  2061. \arg ENET_DMA_RX: DMA Rx descriptors
  2062. \param[out] none
  2063. \retval none
  2064. */
  2065. void enet_ptp_enhanced_descriptors_chain_init(enet_dmadirection_enum direction)
  2066. {
  2067. uint32_t num = 0U, count = 0U, maxsize = 0U;
  2068. uint32_t desc_status = 0U, desc_bufsize = 0U;
  2069. enet_descriptors_struct *desc, *desc_tab;
  2070. uint8_t *buf;
  2071. /* if want to initialize DMA Tx descriptors */
  2072. if (ENET_DMA_TX == direction){
  2073. /* save a copy of the DMA Tx descriptors */
  2074. desc_tab = txdesc_tab;
  2075. buf = &tx_buff[0][0];
  2076. count = ENET_TXBUF_NUM;
  2077. maxsize = ENET_TXBUF_SIZE;
  2078. /* select chain mode, and enable transmit timestamp function */
  2079. desc_status = ENET_TDES0_TCHM | ENET_TDES0_TTSEN;
  2080. /* configure DMA Tx descriptor table address register */
  2081. ENET_DMA_TDTADDR = (uint32_t)desc_tab;
  2082. dma_current_txdesc = desc_tab;
  2083. }else{
  2084. /* if want to initialize DMA Rx descriptors */
  2085. /* save a copy of the DMA Rx descriptors */
  2086. desc_tab = rxdesc_tab;
  2087. buf = &rx_buff[0][0];
  2088. count = ENET_RXBUF_NUM;
  2089. maxsize = ENET_RXBUF_SIZE;
  2090. /* enable receiving */
  2091. desc_status = ENET_RDES0_DAV;
  2092. /* select receive chained mode and set buffer1 size */
  2093. desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
  2094. /* configure DMA Rx descriptor table address register */
  2095. ENET_DMA_RDTADDR = (uint32_t)desc_tab;
  2096. dma_current_rxdesc = desc_tab;
  2097. }
  2098. /* configuration each descriptor */
  2099. for(num = 0U; num < count; num++){
  2100. /* get the pointer to the next descriptor of the descriptor table */
  2101. desc = desc_tab + num;
  2102. /* configure descriptors */
  2103. desc->status = desc_status;
  2104. desc->control_buffer_size = desc_bufsize;
  2105. desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
  2106. /* if is not the last descriptor */
  2107. if(num < (count - 1U)){
  2108. /* configure the next descriptor address */
  2109. desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
  2110. }else{
  2111. /* when it is the last descriptor, the next descriptor address
  2112. equals to first descriptor address in descriptor table */
  2113. desc->buffer2_next_desc_addr = (uint32_t)desc_tab;
  2114. }
  2115. }
  2116. }
  2117. /*!
  2118. \brief initialize the DMA Tx/Rx descriptors's parameters in enhanced ring mode with ptp function
  2119. \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
  2120. only one parameter can be selected which is shown as below
  2121. \arg ENET_DMA_TX: DMA Tx descriptors
  2122. \arg ENET_DMA_RX: DMA Rx descriptors
  2123. \param[out] none
  2124. \retval none
  2125. */
  2126. void enet_ptp_enhanced_descriptors_ring_init(enet_dmadirection_enum direction)
  2127. {
  2128. uint32_t num = 0U, count = 0U, maxsize = 0U;
  2129. uint32_t desc_status = 0U, desc_bufsize = 0U;
  2130. enet_descriptors_struct *desc;
  2131. enet_descriptors_struct *desc_tab;
  2132. uint8_t *buf;
  2133. /* configure descriptor skip length */
  2134. ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
  2135. ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
  2136. /* if want to initialize DMA Tx descriptors */
  2137. if (ENET_DMA_TX == direction){
  2138. /* save a copy of the DMA Tx descriptors */
  2139. desc_tab = txdesc_tab;
  2140. buf = &tx_buff[0][0];
  2141. count = ENET_TXBUF_NUM;
  2142. maxsize = ENET_TXBUF_SIZE;
  2143. /* select ring mode, and enable transmit timestamp function */
  2144. desc_status = ENET_TDES0_TTSEN;
  2145. /* configure DMA Tx descriptor table address register */
  2146. ENET_DMA_TDTADDR = (uint32_t)desc_tab;
  2147. dma_current_txdesc = desc_tab;
  2148. }else{
  2149. /* if want to initialize DMA Rx descriptors */
  2150. /* save a copy of the DMA Rx descriptors */
  2151. desc_tab = rxdesc_tab;
  2152. buf = &rx_buff[0][0];
  2153. count = ENET_RXBUF_NUM;
  2154. maxsize = ENET_RXBUF_SIZE;
  2155. /* enable receiving */
  2156. desc_status = ENET_RDES0_DAV;
  2157. /* set buffer1 size */
  2158. desc_bufsize = ENET_RXBUF_SIZE;
  2159. /* configure DMA Rx descriptor table address register */
  2160. ENET_DMA_RDTADDR = (uint32_t)desc_tab;
  2161. dma_current_rxdesc = desc_tab;
  2162. }
  2163. /* configure each descriptor */
  2164. for(num=0U; num < count; num++){
  2165. /* get the pointer to the next descriptor of the descriptor table */
  2166. desc = desc_tab + num;
  2167. /* configure descriptors */
  2168. desc->status = desc_status;
  2169. desc->control_buffer_size = desc_bufsize;
  2170. desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
  2171. /* when it is the last descriptor */
  2172. if(num == (count - 1U)){
  2173. if (ENET_DMA_TX == direction){
  2174. /* configure transmit end of ring mode */
  2175. desc->status |= ENET_TDES0_TERM;
  2176. }else{
  2177. /* configure receive end of ring mode */
  2178. desc->control_buffer_size |= ENET_RDES1_RERM;
  2179. }
  2180. }
  2181. }
  2182. }
  2183. /*!
  2184. \brief receive a packet data with timestamp values to application buffer, when the DMA is in enhanced mode
  2185. \param[in] bufsize: the size of buffer which is the parameter in function
  2186. \param[out] buffer: pointer to the application buffer
  2187. note -- if the input is NULL, user should copy data in application by himself
  2188. \param[out] timestamp: pointer to the table which stores the timestamp high and low
  2189. note -- if the input is NULL, timestamp is ignored
  2190. \retval ErrStatus: SUCCESS or ERROR
  2191. */
  2192. ErrStatus enet_ptpframe_receive_enhanced_mode(uint8_t *buffer, uint32_t bufsize, uint32_t timestamp[])
  2193. {
  2194. uint32_t offset = 0U, size = 0U;
  2195. uint32_t timeout = 0U;
  2196. uint32_t rdes0_tsv_flag;
  2197. /* the descriptor is busy due to own by the DMA */
  2198. if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
  2199. return ERROR;
  2200. }
  2201. /* if buffer pointer is null, indicates that users has copied data in application */
  2202. if(NULL != buffer){
  2203. /* if no error occurs, and the frame uses only one descriptor */
  2204. if(((uint32_t)RESET == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
  2205. ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
  2206. ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
  2207. /* get the frame length except CRC */
  2208. size = GET_RDES0_FRML(dma_current_rxdesc->status) - 4U;
  2209. /* if is a type frame, and CRC is not included in forwarding frame */
  2210. if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
  2211. size = size + 4U;
  2212. }
  2213. /* to avoid situation that the frame size exceeds the buffer length */
  2214. if(size > bufsize){
  2215. return ERROR;
  2216. }
  2217. /* copy data from Rx buffer to application buffer */
  2218. for(offset = 0; offset < size; offset++){
  2219. (*(buffer + offset)) = (*(__IO uint8_t *)((dma_current_rxdesc->buffer1_addr) + offset));
  2220. }
  2221. }else{
  2222. return ERROR;
  2223. }
  2224. }
  2225. /* if timestamp pointer is null, indicates that users don't care timestamp in application */
  2226. if(NULL != timestamp){
  2227. /* wait for ENET_RDES0_TSV flag to be set, the timestamp value is taken and
  2228. write to the RDES6 and RDES7 */
  2229. do{
  2230. rdes0_tsv_flag = (dma_current_rxdesc->status & ENET_RDES0_TSV);
  2231. timeout++;
  2232. }while ((RESET == rdes0_tsv_flag) && (timeout < ENET_DELAY_TO));
  2233. /* return ERROR due to timeout */
  2234. if(ENET_DELAY_TO == timeout){
  2235. return ERROR;
  2236. }
  2237. /* clear the ENET_RDES0_TSV flag */
  2238. dma_current_rxdesc->status &= ~ENET_RDES0_TSV;
  2239. /* get the timestamp value of the received frame */
  2240. timestamp[0] = dma_current_rxdesc->timestamp_low;
  2241. timestamp[1] = dma_current_rxdesc->timestamp_high;
  2242. }
  2243. /* enable reception, descriptor is owned by DMA */
  2244. dma_current_rxdesc->status = ENET_RDES0_DAV;
  2245. /* check Rx buffer unavailable flag status */
  2246. if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
  2247. /* Clear RBU flag */
  2248. ENET_DMA_STAT = ENET_DMA_STAT_RBU;
  2249. /* resume DMA reception by writing to the RPEN register*/
  2250. ENET_DMA_RPEN = 0;
  2251. }
  2252. /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
  2253. /* chained mode */
  2254. if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
  2255. dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
  2256. }else{
  2257. /* ring mode */
  2258. if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
  2259. /* if is the last descriptor in table, the next descriptor is the table header */
  2260. dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
  2261. }else{
  2262. /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
  2263. dma_current_rxdesc = (enet_descriptors_struct*) ((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
  2264. }
  2265. }
  2266. return SUCCESS;
  2267. }
  2268. /*!
  2269. \brief send data with timestamp values in application buffer as a transmit packet, when the DMA is in enhanced mode
  2270. \param[in] buffer: pointer on the application buffer
  2271. note -- if the input is NULL, user should copy data in application by himself
  2272. \param[in] length: the length of frame data to be transmitted
  2273. \param[out] timestamp: pointer to the table which stores the timestamp high and low
  2274. note -- if the input is NULL, timestamp is ignored
  2275. \param[out] none
  2276. \retval ErrStatus: SUCCESS or ERROR
  2277. */
  2278. ErrStatus enet_ptpframe_transmit_enhanced_mode(uint8_t *buffer, uint32_t length, uint32_t timestamp[])
  2279. {
  2280. uint32_t offset = 0;
  2281. uint32_t dma_tbu_flag, dma_tu_flag;
  2282. uint32_t tdes0_ttmss_flag;
  2283. uint32_t timeout = 0;
  2284. /* the descriptor is busy due to own by the DMA */
  2285. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
  2286. return ERROR;
  2287. }
  2288. /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
  2289. if(length > ENET_MAX_FRAME_SIZE){
  2290. return ERROR;
  2291. }
  2292. /* if buffer pointer is null, indicates that users has handled data in application */
  2293. if(NULL != buffer){
  2294. /* copy frame data from application buffer to Tx buffer */
  2295. for(offset = 0; offset < length; offset++){
  2296. (*(__IO uint8_t *)((dma_current_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
  2297. }
  2298. }
  2299. /* set the frame length */
  2300. dma_current_txdesc->control_buffer_size = length;
  2301. /* set the segment of frame, frame is transmitted in one descriptor */
  2302. dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
  2303. /* enable the DMA transmission */
  2304. dma_current_txdesc->status |= ENET_TDES0_DAV;
  2305. /* check Tx buffer unavailable flag status */
  2306. dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
  2307. dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
  2308. if ((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
  2309. /* Clear TBU and TU flag */
  2310. ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
  2311. /* resume DMA transmission by writing to the TPEN register*/
  2312. ENET_DMA_TPEN = 0;
  2313. }
  2314. /* if timestamp pointer is null, indicates that users don't care timestamp in application */
  2315. if(NULL != timestamp){
  2316. /* wait for ENET_TDES0_TTMSS flag to be set, a timestamp was captured */
  2317. do{
  2318. tdes0_ttmss_flag = (dma_current_txdesc->status & ENET_TDES0_TTMSS);
  2319. timeout++;
  2320. }while((RESET == tdes0_ttmss_flag) && (timeout < ENET_DELAY_TO));
  2321. /* return ERROR due to timeout */
  2322. if(ENET_DELAY_TO == timeout){
  2323. return ERROR;
  2324. }
  2325. /* clear the ENET_TDES0_TTMSS flag */
  2326. dma_current_txdesc->status &= ~ENET_TDES0_TTMSS;
  2327. /* get the timestamp value of the transmit frame */
  2328. timestamp[0] = dma_current_txdesc->timestamp_low;
  2329. timestamp[1] = dma_current_txdesc->timestamp_high;
  2330. }
  2331. /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table*/
  2332. /* chained mode */
  2333. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
  2334. dma_current_txdesc = (enet_descriptors_struct*) (dma_current_txdesc->buffer2_next_desc_addr);
  2335. }else{
  2336. /* ring mode */
  2337. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
  2338. /* if is the last descriptor in table, the next descriptor is the table header */
  2339. dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
  2340. }else{
  2341. /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
  2342. dma_current_txdesc = (enet_descriptors_struct*) ((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
  2343. }
  2344. }
  2345. return SUCCESS;
  2346. }
  2347. #else
  2348. /*!
  2349. \brief configure descriptor to work in normal mode
  2350. \param[in] none
  2351. \param[out] none
  2352. \retval none
  2353. */
  2354. void enet_desc_select_normal_mode(void)
  2355. {
  2356. ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DFM;
  2357. }
  2358. /*!
  2359. \brief initialize the DMA Tx/Rx descriptors's parameters in normal chain mode with PTP function
  2360. \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
  2361. only one parameter can be selected which is shown as below
  2362. \arg ENET_DMA_TX: DMA Tx descriptors
  2363. \arg ENET_DMA_RX: DMA Rx descriptors
  2364. \param[in] desc_ptptab: pointer to the first descriptor address of PTP Rx descriptor table
  2365. \param[out] none
  2366. \retval none
  2367. */
  2368. void enet_ptp_normal_descriptors_chain_init(enet_dmadirection_enum direction, enet_descriptors_struct *desc_ptptab)
  2369. {
  2370. uint32_t num = 0U, count = 0U, maxsize = 0U;
  2371. uint32_t desc_status = 0U, desc_bufsize = 0U;
  2372. enet_descriptors_struct *desc, *desc_tab;
  2373. uint8_t *buf;
  2374. /* if want to initialize DMA Tx descriptors */
  2375. if (ENET_DMA_TX == direction){
  2376. /* save a copy of the DMA Tx descriptors */
  2377. desc_tab = txdesc_tab;
  2378. buf = &tx_buff[0][0];
  2379. count = ENET_TXBUF_NUM;
  2380. maxsize = ENET_TXBUF_SIZE;
  2381. /* select chain mode, and enable transmit timestamp function */
  2382. desc_status = ENET_TDES0_TCHM | ENET_TDES0_TTSEN;
  2383. /* configure DMA Tx descriptor table address register */
  2384. ENET_DMA_TDTADDR = (uint32_t)desc_tab;
  2385. dma_current_txdesc = desc_tab;
  2386. dma_current_ptp_txdesc = desc_ptptab;
  2387. }else{
  2388. /* if want to initialize DMA Rx descriptors */
  2389. /* save a copy of the DMA Rx descriptors */
  2390. desc_tab = rxdesc_tab;
  2391. buf = &rx_buff[0][0];
  2392. count = ENET_RXBUF_NUM;
  2393. maxsize = ENET_RXBUF_SIZE;
  2394. /* enable receiving */
  2395. desc_status = ENET_RDES0_DAV;
  2396. /* select receive chained mode and set buffer1 size */
  2397. desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
  2398. /* configure DMA Rx descriptor table address register */
  2399. ENET_DMA_RDTADDR = (uint32_t)desc_tab;
  2400. dma_current_rxdesc = desc_tab;
  2401. dma_current_ptp_rxdesc = desc_ptptab;
  2402. }
  2403. /* configure each descriptor */
  2404. for(num = 0U; num < count; num++){
  2405. /* get the pointer to the next descriptor of the descriptor table */
  2406. desc = desc_tab + num;
  2407. /* configure descriptors */
  2408. desc->status = desc_status;
  2409. desc->control_buffer_size = desc_bufsize;
  2410. desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
  2411. /* if is not the last descriptor */
  2412. if(num < (count - 1U)){
  2413. /* configure the next descriptor address */
  2414. desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
  2415. }else{
  2416. /* when it is the last descriptor, the next descriptor address
  2417. equals to first descriptor address in descriptor table */
  2418. desc->buffer2_next_desc_addr = (uint32_t)desc_tab;
  2419. }
  2420. /* set desc_ptptab equal to desc_tab */
  2421. (&desc_ptptab[num])->buffer1_addr = desc->buffer1_addr;
  2422. (&desc_ptptab[num])->buffer2_next_desc_addr = desc->buffer2_next_desc_addr;
  2423. }
  2424. /* when it is the last ptp descriptor, preserve the first descriptor
  2425. address of desc_ptptab in ptp descriptor status */
  2426. (&desc_ptptab[num-1U])->status = (uint32_t)desc_ptptab;
  2427. }
  2428. /*!
  2429. \brief initialize the DMA Tx/Rx descriptors's parameters in normal ring mode with PTP function
  2430. \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
  2431. only one parameter can be selected which is shown as below
  2432. \arg ENET_DMA_TX: DMA Tx descriptors
  2433. \arg ENET_DMA_RX: DMA Rx descriptors
  2434. \param[in] desc_ptptab: pointer to the first descriptor address of PTP Rx descriptor table
  2435. \param[out] none
  2436. \retval none
  2437. */
  2438. void enet_ptp_normal_descriptors_ring_init(enet_dmadirection_enum direction, enet_descriptors_struct *desc_ptptab)
  2439. {
  2440. uint32_t num = 0U, count = 0U, maxsize = 0U;
  2441. uint32_t desc_status = 0U, desc_bufsize = 0U;
  2442. enet_descriptors_struct *desc, *desc_tab;
  2443. uint8_t *buf;
  2444. /* configure descriptor skip length */
  2445. ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
  2446. ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
  2447. /* if want to initialize DMA Tx descriptors */
  2448. if (ENET_DMA_TX == direction){
  2449. /* save a copy of the DMA Tx descriptors */
  2450. desc_tab = txdesc_tab;
  2451. buf = &tx_buff[0][0];
  2452. count = ENET_TXBUF_NUM;
  2453. maxsize = ENET_TXBUF_SIZE;
  2454. /* select ring mode, and enable transmit timestamp function */
  2455. desc_status = ENET_TDES0_TTSEN;
  2456. /* configure DMA Tx descriptor table address register */
  2457. ENET_DMA_TDTADDR = (uint32_t)desc_tab;
  2458. dma_current_txdesc = desc_tab;
  2459. dma_current_ptp_txdesc = desc_ptptab;
  2460. }else{
  2461. /* if want to initialize DMA Rx descriptors */
  2462. /* save a copy of the DMA Rx descriptors */
  2463. desc_tab = rxdesc_tab;
  2464. buf = &rx_buff[0][0];
  2465. count = ENET_RXBUF_NUM;
  2466. maxsize = ENET_RXBUF_SIZE;
  2467. /* enable receiving */
  2468. desc_status = ENET_RDES0_DAV;
  2469. /* select receive ring mode and set buffer1 size */
  2470. desc_bufsize = (uint32_t)ENET_RXBUF_SIZE;
  2471. /* configure DMA Rx descriptor table address register */
  2472. ENET_DMA_RDTADDR = (uint32_t)desc_tab;
  2473. dma_current_rxdesc = desc_tab;
  2474. dma_current_ptp_rxdesc = desc_ptptab;
  2475. }
  2476. /* configure each descriptor */
  2477. for(num = 0U; num < count; num++){
  2478. /* get the pointer to the next descriptor of the descriptor table */
  2479. desc = desc_tab + num;
  2480. /* configure descriptors */
  2481. desc->status = desc_status;
  2482. desc->control_buffer_size = desc_bufsize;
  2483. desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
  2484. /* when it is the last descriptor */
  2485. if(num == (count - 1U)){
  2486. if (ENET_DMA_TX == direction){
  2487. /* configure transmit end of ring mode */
  2488. desc->status |= ENET_TDES0_TERM;
  2489. }else{
  2490. /* configure receive end of ring mode */
  2491. desc->control_buffer_size |= ENET_RDES1_RERM;
  2492. }
  2493. }
  2494. /* set desc_ptptab equal to desc_tab */
  2495. (&desc_ptptab[num])->buffer1_addr = desc->buffer1_addr;
  2496. (&desc_ptptab[num])->buffer2_next_desc_addr = desc->buffer2_next_desc_addr;
  2497. }
  2498. /* when it is the last ptp descriptor, preserve the first descriptor
  2499. address of desc_ptptab in ptp descriptor status */
  2500. (&desc_ptptab[num-1U])->status = (uint32_t)desc_ptptab;
  2501. }
  2502. /*!
  2503. \brief receive a packet data with timestamp values to application buffer, when the DMA is in normal mode
  2504. \param[in] bufsize: the size of buffer which is the parameter in function
  2505. \param[out] timestamp: pointer to the table which stores the timestamp high and low
  2506. \param[out] buffer: pointer to the application buffer
  2507. note -- if the input is NULL, user should copy data in application by himself
  2508. \retval ErrStatus: SUCCESS or ERROR
  2509. */
  2510. ErrStatus enet_ptpframe_receive_normal_mode(uint8_t *buffer, uint32_t bufsize, uint32_t timestamp[])
  2511. {
  2512. uint32_t offset = 0U, size = 0U;
  2513. /* the descriptor is busy due to own by the DMA */
  2514. if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
  2515. return ERROR;
  2516. }
  2517. /* if buffer pointer is null, indicates that users has copied data in application */
  2518. if(NULL != buffer){
  2519. /* if no error occurs, and the frame uses only one descriptor */
  2520. if(((uint32_t)RESET == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
  2521. ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
  2522. ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
  2523. /* get the frame length except CRC */
  2524. size = GET_RDES0_FRML(dma_current_rxdesc->status) - 4U;
  2525. /* if is a type frame, and CRC is not included in forwarding frame */
  2526. if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
  2527. size = size + 4U;
  2528. }
  2529. /* to avoid situation that the frame size exceeds the buffer length */
  2530. if(size > bufsize){
  2531. return ERROR;
  2532. }
  2533. /* copy data from Rx buffer to application buffer */
  2534. for(offset = 0U; offset < size; offset++){
  2535. (*(buffer + offset)) = (*(__IO uint8_t *)(uint32_t)((dma_current_ptp_rxdesc->buffer1_addr) + offset));
  2536. }
  2537. }else{
  2538. return ERROR;
  2539. }
  2540. }
  2541. /* copy timestamp value from Rx descriptor to application array */
  2542. timestamp[0] = dma_current_rxdesc->buffer1_addr;
  2543. timestamp[1] = dma_current_rxdesc->buffer2_next_desc_addr;
  2544. dma_current_rxdesc->buffer1_addr = dma_current_ptp_rxdesc ->buffer1_addr ;
  2545. dma_current_rxdesc->buffer2_next_desc_addr = dma_current_ptp_rxdesc ->buffer2_next_desc_addr;
  2546. /* enable reception, descriptor is owned by DMA */
  2547. dma_current_rxdesc->status = ENET_RDES0_DAV;
  2548. /* check Rx buffer unavailable flag status */
  2549. if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
  2550. /* clear RBU flag */
  2551. ENET_DMA_STAT = ENET_DMA_STAT_RBU;
  2552. /* resume DMA reception by writing to the RPEN register*/
  2553. ENET_DMA_RPEN = 0U;
  2554. }
  2555. /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
  2556. /* chained mode */
  2557. if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
  2558. dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->buffer2_next_desc_addr);
  2559. /* if it is the last ptp descriptor */
  2560. if(0U != dma_current_ptp_rxdesc->status){
  2561. /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
  2562. dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
  2563. }else{
  2564. /* ponter to the next ptp descriptor */
  2565. dma_current_ptp_rxdesc++;
  2566. }
  2567. }else{
  2568. /* ring mode */
  2569. if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
  2570. /* if is the last descriptor in table, the next descriptor is the table header */
  2571. dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
  2572. /* RDES2 and RDES3 will not be covered by buffer address, so do not need to preserve a new table,
  2573. use the same table with RxDMA descriptor */
  2574. dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
  2575. }else{
  2576. /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
  2577. dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
  2578. dma_current_ptp_rxdesc ++;
  2579. }
  2580. }
  2581. return SUCCESS;
  2582. }
  2583. /*!
  2584. \brief send data with timestamp values in application buffer as a transmit packet, when the DMA is in normal mode
  2585. \param[in] buffer: pointer on the application buffer
  2586. note -- if the input is NULL, user should copy data in application by himself
  2587. \param[in] length: the length of frame data to be transmitted
  2588. \param[out] timestamp: pointer to the table which stores the timestamp high and low
  2589. note -- if the input is NULL, timestamp is ignored
  2590. \retval ErrStatus: SUCCESS or ERROR
  2591. */
  2592. ErrStatus enet_ptpframe_transmit_normal_mode(uint8_t *buffer, uint32_t length, uint32_t timestamp[])
  2593. {
  2594. uint32_t offset = 0U, timeout = 0U;
  2595. uint32_t dma_tbu_flag, dma_tu_flag, tdes0_ttmss_flag;
  2596. /* the descriptor is busy due to own by the DMA */
  2597. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
  2598. return ERROR;
  2599. }
  2600. /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
  2601. if(length > ENET_MAX_FRAME_SIZE){
  2602. return ERROR;
  2603. }
  2604. /* if buffer pointer is null, indicates that users has handled data in application */
  2605. if(NULL != buffer){
  2606. /* copy frame data from application buffer to Tx buffer */
  2607. for(offset = 0U; offset < length; offset++){
  2608. (*(__IO uint8_t *) (uint32_t)((dma_current_ptp_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
  2609. }
  2610. }
  2611. /* set the frame length */
  2612. dma_current_txdesc->control_buffer_size = (length & (uint32_t)0x1FFF);
  2613. /* set the segment of frame, frame is transmitted in one descriptor */
  2614. dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
  2615. /* enable the DMA transmission */
  2616. dma_current_txdesc->status |= ENET_TDES0_DAV;
  2617. /* check Tx buffer unavailable flag status */
  2618. dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
  2619. dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
  2620. if((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
  2621. /* clear TBU and TU flag */
  2622. ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
  2623. /* resume DMA transmission by writing to the TPEN register*/
  2624. ENET_DMA_TPEN = 0U;
  2625. }
  2626. /* if timestamp pointer is null, indicates that users don't care timestamp in application */
  2627. if(NULL != timestamp){
  2628. /* wait for ENET_TDES0_TTMSS flag to be set, a timestamp was captured */
  2629. do{
  2630. tdes0_ttmss_flag = (dma_current_txdesc->status & ENET_TDES0_TTMSS);
  2631. timeout++;
  2632. }while((RESET == tdes0_ttmss_flag) && (timeout < ENET_DELAY_TO));
  2633. /* return ERROR due to timeout */
  2634. if(ENET_DELAY_TO == timeout){
  2635. return ERROR;
  2636. }
  2637. /* clear the ENET_TDES0_TTMSS flag */
  2638. dma_current_txdesc->status &= ~ENET_TDES0_TTMSS;
  2639. /* get the timestamp value of the transmit frame */
  2640. timestamp[0] = dma_current_txdesc->buffer1_addr;
  2641. timestamp[1] = dma_current_txdesc->buffer2_next_desc_addr;
  2642. }
  2643. dma_current_txdesc->buffer1_addr = dma_current_ptp_txdesc ->buffer1_addr ;
  2644. dma_current_txdesc->buffer2_next_desc_addr = dma_current_ptp_txdesc ->buffer2_next_desc_addr;
  2645. /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table */
  2646. /* chained mode */
  2647. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
  2648. dma_current_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->buffer2_next_desc_addr);
  2649. /* if it is the last ptp descriptor */
  2650. if(0U != dma_current_ptp_txdesc->status){
  2651. /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
  2652. dma_current_ptp_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->status);
  2653. }else{
  2654. /* ponter to the next ptp descriptor */
  2655. dma_current_ptp_txdesc++;
  2656. }
  2657. }else{
  2658. /* ring mode */
  2659. if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
  2660. /* if is the last descriptor in table, the next descriptor is the table header */
  2661. dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
  2662. /* TDES2 and TDES3 will not be covered by buffer address, so do not need to preserve a new table,
  2663. use the same table with TxDMA descriptor */
  2664. dma_current_ptp_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->status);
  2665. }else{
  2666. /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
  2667. dma_current_txdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
  2668. dma_current_ptp_txdesc ++;
  2669. }
  2670. }
  2671. return SUCCESS;
  2672. }
  2673. #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
  2674. /*!
  2675. \brief wakeup frame filter register pointer reset
  2676. \param[in] none
  2677. \param[out] none
  2678. \retval none
  2679. */
  2680. void enet_wum_filter_register_pointer_reset(void)
  2681. {
  2682. ENET_MAC_WUM |= ENET_MAC_WUM_WUFFRPR;
  2683. }
  2684. /*!
  2685. \brief set the remote wakeup frame registers
  2686. \param[in] pdata: pointer to buffer data which is written to remote wakeup frame registers (8 words total)
  2687. \param[out] none
  2688. \retval none
  2689. */
  2690. void enet_wum_filter_config(uint32_t pdata[])
  2691. {
  2692. uint32_t num = 0U;
  2693. /* configure ENET_MAC_RWFF register */
  2694. for(num = 0U; num < ETH_WAKEUP_REGISTER_LENGTH; num++){
  2695. ENET_MAC_RWFF = pdata[num];
  2696. }
  2697. }
  2698. /*!
  2699. \brief enable wakeup management features
  2700. \param[in] feature: one or more parameters can be selected which are shown as below
  2701. \arg ENET_WUM_POWER_DOWN: power down mode
  2702. \arg ENET_WUM_MAGIC_PACKET_FRAME: enable a wakeup event due to magic packet reception
  2703. \arg ENET_WUM_WAKE_UP_FRAME: enable a wakeup event due to wakeup frame reception
  2704. \arg ENET_WUM_GLOBAL_UNICAST: any received unicast frame passed filter is considered to be a wakeup frame
  2705. \param[out] none
  2706. \retval none
  2707. */
  2708. void enet_wum_feature_enable(uint32_t feature)
  2709. {
  2710. ENET_MAC_WUM |= feature;
  2711. }
  2712. /*!
  2713. \brief disable wakeup management features
  2714. \param[in] feature: one or more parameters can be selected which are shown as below
  2715. \arg ENET_WUM_MAGIC_PACKET_FRAME: enable a wakeup event due to magic packet reception
  2716. \arg ENET_WUM_WAKE_UP_FRAME: enable a wakeup event due to wakeup frame reception
  2717. \arg ENET_WUM_GLOBAL_UNICAST: any received unicast frame passed filter is considered to be a wakeup frame
  2718. \param[out] none
  2719. \retval none
  2720. */
  2721. void enet_wum_feature_disable(uint32_t feature)
  2722. {
  2723. ENET_MAC_WUM &= (~feature);
  2724. }
  2725. /*!
  2726. \brief reset the MAC statistics counters
  2727. \param[in] none
  2728. \param[out] none
  2729. \retval none
  2730. */
  2731. void enet_msc_counters_reset(void)
  2732. {
  2733. /* reset all counters */
  2734. ENET_MSC_CTL |= ENET_MSC_CTL_CTR;
  2735. }
  2736. /*!
  2737. \brief enable the MAC statistics counter features
  2738. \param[in] feature: one or more parameters can be selected which are shown as below
  2739. \arg ENET_MSC_COUNTER_STOP_ROLLOVER: counter stop rollover
  2740. \arg ENET_MSC_RESET_ON_READ: reset on read
  2741. \arg ENET_MSC_COUNTERS_FREEZE: MSC counter freeze
  2742. \param[out] none
  2743. \retval none
  2744. */
  2745. void enet_msc_feature_enable(uint32_t feature)
  2746. {
  2747. ENET_MSC_CTL |= feature;
  2748. }
  2749. /*!
  2750. \brief disable the MAC statistics counter features
  2751. \param[in] feature: one or more parameters can be selected which are shown as below
  2752. \arg ENET_MSC_COUNTER_STOP_ROLLOVER: counter stop rollover
  2753. \arg ENET_MSC_RESET_ON_READ: reset on read
  2754. \arg ENET_MSC_COUNTERS_FREEZE: MSC counter freeze
  2755. \param[out] none
  2756. \retval none
  2757. */
  2758. void enet_msc_feature_disable(uint32_t feature)
  2759. {
  2760. ENET_MSC_CTL &= (~feature);
  2761. }
  2762. /*!
  2763. \brief configure MAC statistics counters preset mode
  2764. \param[in] mode: MSC counters preset mode, refer to enet_msc_preset_enum,
  2765. only one parameter can be selected which is shown as below
  2766. \arg ENET_MSC_PRESET_NONE: do not preset MSC counter
  2767. \arg ENET_MSC_PRESET_HALF: preset all MSC counters to almost-half(0x7FFF FFF0) value
  2768. \arg ENET_MSC_PRESET_FULL: preset all MSC counters to almost-full(0xFFFF FFF0) value
  2769. \param[out] none
  2770. \retval none
  2771. */
  2772. void enet_msc_counters_preset_config(enet_msc_preset_enum mode)
  2773. {
  2774. ENET_MSC_CTL &= ENET_MSC_PRESET_MASK;
  2775. ENET_MSC_CTL |= (uint32_t)mode;
  2776. }
  2777. /*!
  2778. \brief get MAC statistics counter
  2779. \param[in] counter: MSC counters which is selected, refer to enet_msc_counter_enum,
  2780. only one parameter can be selected which is shown as below
  2781. \arg ENET_MSC_TX_SCCNT: MSC transmitted good frames after a single collision counter
  2782. \arg ENET_MSC_TX_MSCCNT: MSC transmitted good frames after more than a single collision counter
  2783. \arg ENET_MSC_TX_TGFCNT: MSC transmitted good frames counter
  2784. \arg ENET_MSC_RX_RFCECNT: MSC received frames with CRC error counter
  2785. \arg ENET_MSC_RX_RFAECNT: MSC received frames with alignment error counter
  2786. \arg ENET_MSC_RX_RGUFCNT: MSC received good unicast frames counter
  2787. \param[out] none
  2788. \retval the MSC counter value
  2789. */
  2790. uint32_t enet_msc_counters_get(enet_msc_counter_enum counter)
  2791. {
  2792. uint32_t reval;
  2793. reval = REG32((ENET + (uint32_t)counter));
  2794. return reval;
  2795. }
  2796. /*!
  2797. \brief change subsecond to nanosecond
  2798. \param[in] subsecond: subsecond value
  2799. \param[out] none
  2800. \retval the nanosecond value
  2801. */
  2802. uint32_t enet_ptp_subsecond_2_nanosecond(uint32_t subsecond)
  2803. {
  2804. uint64_t val = subsecond * 1000000000Ull;
  2805. val >>= 31;
  2806. return (uint32_t)val;
  2807. }
  2808. /*!
  2809. \brief change nanosecond to subsecond
  2810. \param[in] nanosecond: nanosecond value
  2811. \param[out] none
  2812. \retval the subsecond value
  2813. */
  2814. uint32_t enet_ptp_nanosecond_2_subsecond(uint32_t nanosecond)
  2815. {
  2816. uint64_t val = nanosecond * 0x80000000Ull;
  2817. val /= 1000000000U;
  2818. return (uint32_t)val;
  2819. }
  2820. /*!
  2821. \brief enable the PTP features
  2822. \param[in] feature: the feature of ENET PTP mode
  2823. one or more parameters can be selected which are shown as below
  2824. \arg ENET_RXTX_TIMESTAMP: timestamp function for transmit and receive frames
  2825. \arg ENET_PTP_TIMESTAMP_INT: timestamp interrupt trigger
  2826. \arg ENET_ALL_RX_TIMESTAMP: all received frames are taken snapshot
  2827. \arg ENET_NONTYPE_FRAME_SNAPSHOT: take snapshot when received non type frame
  2828. \arg ENET_IPV6_FRAME_SNAPSHOT: take snapshot for IPv6 frame
  2829. \arg ENET_IPV4_FRAME_SNAPSHOT: take snapshot for IPv4 frame
  2830. \arg ENET_PTP_FRAME_USE_MACADDRESS_FILTER: use MAC address1-3 to filter the PTP frame
  2831. \param[out] none
  2832. \retval none
  2833. */
  2834. void enet_ptp_feature_enable(uint32_t feature)
  2835. {
  2836. ENET_PTP_TSCTL |= feature;
  2837. }
  2838. /*!
  2839. \brief disable the PTP features
  2840. \param[in] feature: the feature of ENET PTP mode
  2841. one or more parameters can be selected which are shown as below
  2842. \arg ENET_RXTX_TIMESTAMP: timestamp function for transmit and receive frames
  2843. \arg ENET_PTP_TIMESTAMP_INT: timestamp interrupt trigger
  2844. \arg ENET_ALL_RX_TIMESTAMP: all received frames are taken snapshot
  2845. \arg ENET_NONTYPE_FRAME_SNAPSHOT: take snapshot when received non type frame
  2846. \arg ENET_IPV6_FRAME_SNAPSHOT: take snapshot for IPv6 frame
  2847. \arg ENET_IPV4_FRAME_SNAPSHOT: take snapshot for IPv4 frame
  2848. \arg ENET_PTP_FRAME_USE_MACADDRESS_FILTER: use MAC address1-3 to filter the PTP frame
  2849. \param[out] none
  2850. \retval none
  2851. */
  2852. void enet_ptp_feature_disable(uint32_t feature)
  2853. {
  2854. ENET_PTP_TSCTL &= ~feature;
  2855. }
  2856. /*!
  2857. \brief configure the PTP timestamp function
  2858. \param[in] func: only one parameter can be selected which is shown as below
  2859. \arg ENET_CKNT_ORDINARY: type of ordinary clock node type for timestamp
  2860. \arg ENET_CKNT_BOUNDARY: type of boundary clock node type for timestamp
  2861. \arg ENET_CKNT_END_TO_END: type of end-to-end transparent clock node type for timestamp
  2862. \arg ENET_CKNT_PEER_TO_PEER: type of peer-to-peer transparent clock node type for timestamp
  2863. \arg ENET_PTP_ADDEND_UPDATE: addend register update
  2864. \arg ENET_PTP_SYSTIME_UPDATE: timestamp update
  2865. \arg ENET_PTP_SYSTIME_INIT: timestamp initialize
  2866. \arg ENET_PTP_FINEMODE: the system timestamp uses the fine method for updating
  2867. \arg ENET_PTP_COARSEMODE: the system timestamp uses the coarse method for updating
  2868. \arg ENET_SUBSECOND_DIGITAL_ROLLOVER: digital rollover mode
  2869. \arg ENET_SUBSECOND_BINARY_ROLLOVER: binary rollover mode
  2870. \arg ENET_SNOOPING_PTP_VERSION_2: version 2
  2871. \arg ENET_SNOOPING_PTP_VERSION_1: version 1
  2872. \arg ENET_EVENT_TYPE_MESSAGES_SNAPSHOT: only event type messages are taken snapshot
  2873. \arg ENET_ALL_TYPE_MESSAGES_SNAPSHOT: all type messages are taken snapshot except announce,
  2874. management and signaling message
  2875. \arg ENET_MASTER_NODE_MESSAGE_SNAPSHOT: snapshot is only take for master node message
  2876. \arg ENET_SLAVE_NODE_MESSAGE_SNAPSHOT: snapshot is only taken for slave node message
  2877. \param[out] none
  2878. \retval ErrStatus: SUCCESS or ERROR
  2879. */
  2880. ErrStatus enet_ptp_timestamp_function_config(enet_ptp_function_enum func)
  2881. {
  2882. uint32_t temp_config = 0U, temp_state = 0U;
  2883. uint32_t timeout = 0U;
  2884. ErrStatus enet_state = SUCCESS;
  2885. switch(func){
  2886. case ENET_CKNT_ORDINARY:
  2887. case ENET_CKNT_BOUNDARY:
  2888. case ENET_CKNT_END_TO_END:
  2889. case ENET_CKNT_PEER_TO_PEER:
  2890. ENET_PTP_TSCTL &= ~ENET_PTP_TSCTL_CKNT;
  2891. ENET_PTP_TSCTL |= (uint32_t)func;
  2892. break;
  2893. case ENET_PTP_ADDEND_UPDATE:
  2894. /* this bit must be read as zero before application set it */
  2895. do{
  2896. temp_state = ENET_PTP_TSCTL & ENET_PTP_TSCTL_TMSARU;
  2897. timeout++;
  2898. }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
  2899. /* return ERROR due to timeout */
  2900. if(ENET_DELAY_TO == timeout){
  2901. enet_state = ERROR;
  2902. }else{
  2903. ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSARU;
  2904. }
  2905. break;
  2906. case ENET_PTP_SYSTIME_UPDATE:
  2907. /* both the TMSSTU and TMSSTI bits must be read as zero before application set this bit */
  2908. do{
  2909. temp_state = ENET_PTP_TSCTL & (ENET_PTP_TSCTL_TMSSTU | ENET_PTP_TSCTL_TMSSTI);
  2910. timeout++;
  2911. }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
  2912. /* return ERROR due to timeout */
  2913. if(ENET_DELAY_TO == timeout){
  2914. enet_state = ERROR;
  2915. }else{
  2916. ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSSTU;
  2917. }
  2918. break;
  2919. case ENET_PTP_SYSTIME_INIT:
  2920. /* this bit must be read as zero before application set it */
  2921. do{
  2922. temp_state = ENET_PTP_TSCTL & ENET_PTP_TSCTL_TMSSTI;
  2923. timeout++;
  2924. }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
  2925. /* return ERROR due to timeout */
  2926. if(ENET_DELAY_TO == timeout){
  2927. enet_state = ERROR;
  2928. }else{
  2929. ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSSTI;
  2930. }
  2931. break;
  2932. default:
  2933. temp_config = (uint32_t)func & (~BIT(31));
  2934. if(RESET != ((uint32_t)func & BIT(31))){
  2935. ENET_PTP_TSCTL |= temp_config;
  2936. }else{
  2937. ENET_PTP_TSCTL &= ~temp_config;
  2938. }
  2939. break;
  2940. }
  2941. return enet_state;
  2942. }
  2943. /*!
  2944. \brief configure system time subsecond increment value
  2945. \param[in] subsecond: the value will be added to the subsecond value of system time,
  2946. this value must be between 0 and 0xFF
  2947. \param[out] none
  2948. \retval none
  2949. */
  2950. void enet_ptp_subsecond_increment_config(uint32_t subsecond)
  2951. {
  2952. ENET_PTP_SSINC = PTP_SSINC_STMSSI(subsecond);
  2953. }
  2954. /*!
  2955. \brief adjusting the clock frequency only in fine update mode
  2956. \param[in] add: the value will be added to the accumulator register to achieve time synchronization
  2957. \param[out] none
  2958. \retval none
  2959. */
  2960. void enet_ptp_timestamp_addend_config(uint32_t add)
  2961. {
  2962. ENET_PTP_TSADDEND = add;
  2963. }
  2964. /*!
  2965. \brief initialize or add/subtract to second of the system time
  2966. \param[in] sign: timestamp update positive or negative sign,
  2967. only one parameter can be selected which is shown as below
  2968. \arg ENET_PTP_ADD_TO_TIME: timestamp update value is added to system time
  2969. \arg ENET_PTP_SUBSTRACT_FROM_TIME: timestamp update value is subtracted from system time
  2970. \param[in] second: initializing or adding/subtracting to second of the system time
  2971. \param[in] subsecond: the current subsecond of the system time
  2972. with 0.46 ns accuracy if required accuracy is 20 ns
  2973. \param[out] none
  2974. \retval none
  2975. */
  2976. void enet_ptp_timestamp_update_config(uint32_t sign, uint32_t second, uint32_t subsecond)
  2977. {
  2978. ENET_PTP_TSUH = second;
  2979. ENET_PTP_TSUL = sign | PTP_TSUL_TMSUSS(subsecond);
  2980. }
  2981. /*!
  2982. \brief configure the expected target time
  2983. \param[in] second: the expected target second time
  2984. \param[in] nanosecond: the expected target nanosecond time (signed)
  2985. \param[out] none
  2986. \retval none
  2987. */
  2988. void enet_ptp_expected_time_config(uint32_t second, uint32_t nanosecond)
  2989. {
  2990. ENET_PTP_ETH = second;
  2991. ENET_PTP_ETL = nanosecond;
  2992. }
  2993. /*!
  2994. \brief get the current system time
  2995. \param[in] none
  2996. \param[out] systime_struct: pointer to a enet_ptp_systime_struct structure which contains
  2997. parameters of PTP system time
  2998. members of the structure and the member values are shown as below:
  2999. second: 0x0 - 0xFFFF FFFF
  3000. nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
  3001. sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
  3002. \retval none
  3003. */
  3004. void enet_ptp_system_time_get(enet_ptp_systime_struct *systime_struct)
  3005. {
  3006. uint32_t temp_sec = 0U, temp_subs = 0U;
  3007. /* get the value of sysytem time registers */
  3008. temp_sec = (uint32_t)ENET_PTP_TSH;
  3009. temp_subs = (uint32_t)ENET_PTP_TSL;
  3010. /* get sysytem time and construct the enet_ptp_systime_struct structure */
  3011. systime_struct->second = temp_sec;
  3012. systime_struct->nanosecond = GET_PTP_TSL_STMSS(temp_subs);
  3013. systime_struct->nanosecond = enet_ptp_subsecond_2_nanosecond(systime_struct->nanosecond);
  3014. systime_struct->sign = GET_PTP_TSL_STS(temp_subs);
  3015. }
  3016. /*!
  3017. \brief configure the PPS output frequency
  3018. \param[in] freq: PPS output frequency,
  3019. only one parameter can be selected which is shown as below
  3020. \arg ENET_PPSOFC_1HZ: PPS output 1Hz frequency
  3021. \arg ENET_PPSOFC_2HZ: PPS output 2Hz frequency
  3022. \arg ENET_PPSOFC_4HZ: PPS output 4Hz frequency
  3023. \arg ENET_PPSOFC_8HZ: PPS output 8Hz frequency
  3024. \arg ENET_PPSOFC_16HZ: PPS output 16Hz frequency
  3025. \arg ENET_PPSOFC_32HZ: PPS output 32Hz frequency
  3026. \arg ENET_PPSOFC_64HZ: PPS output 64Hz frequency
  3027. \arg ENET_PPSOFC_128HZ: PPS output 128Hz frequency
  3028. \arg ENET_PPSOFC_256HZ: PPS output 256Hz frequency
  3029. \arg ENET_PPSOFC_512HZ: PPS output 512Hz frequency
  3030. \arg ENET_PPSOFC_1024HZ: PPS output 1024Hz frequency
  3031. \arg ENET_PPSOFC_2048HZ: PPS output 2048Hz frequency
  3032. \arg ENET_PPSOFC_4096HZ: PPS output 4096Hz frequency
  3033. \arg ENET_PPSOFC_8192HZ: PPS output 8192Hz frequency
  3034. \arg ENET_PPSOFC_16384HZ: PPS output 16384Hz frequency
  3035. \arg ENET_PPSOFC_32768HZ: PPS output 32768Hz frequency
  3036. \param[out] none
  3037. \retval none
  3038. */
  3039. void enet_ptp_pps_output_frequency_config(uint32_t freq)
  3040. {
  3041. ENET_PTP_PPSCTL = freq;
  3042. }
  3043. /*!
  3044. \brief configure and start PTP timestamp counter
  3045. \param[in] updatemethod: method for updating
  3046. \arg ENET_PTP_FINEMODE: fine correction method
  3047. \arg ENET_PTP_COARSEMODE: coarse correction method
  3048. \param[in] init_sec: second value for initializing system time
  3049. \param[in] init_subsec: subsecond value for initializing system time
  3050. \param[in] carry_cfg: the value to be added to the accumulator register (in fine method is used)
  3051. \param[in] accuracy_cfg: the value to be added to the subsecond value of system time
  3052. \param[out] none
  3053. \retval none
  3054. */
  3055. void enet_ptp_start(int32_t updatemethod, uint32_t init_sec, uint32_t init_subsec, uint32_t carry_cfg, uint32_t accuracy_cfg)
  3056. {
  3057. /* mask the timestamp trigger interrupt */
  3058. enet_interrupt_disable(ENET_MAC_INT_TMSTIM);
  3059. /* enable timestamp */
  3060. enet_ptp_feature_enable(ENET_ALL_RX_TIMESTAMP | ENET_RXTX_TIMESTAMP);
  3061. /* configure system time subsecond increment based on the PTP clock frequency */
  3062. enet_ptp_subsecond_increment_config(accuracy_cfg);
  3063. if(ENET_PTP_FINEMODE == updatemethod){
  3064. /* fine correction method: configure the timestamp addend, then update */
  3065. enet_ptp_timestamp_addend_config(carry_cfg);
  3066. enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
  3067. /* wait until update is completed */
  3068. while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_ADDEND_UPDATE)){
  3069. }
  3070. }
  3071. /* choose the fine correction method */
  3072. enet_ptp_timestamp_function_config((enet_ptp_function_enum)updatemethod);
  3073. /* initialize the system time */
  3074. enet_ptp_timestamp_update_config(ENET_PTP_ADD_TO_TIME, init_sec, init_subsec);
  3075. enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
  3076. #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
  3077. enet_desc_select_enhanced_mode();
  3078. #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
  3079. }
  3080. /*!
  3081. \brief adjust frequency in fine method by configure addend register
  3082. \param[in] carry_cfg: the value to be added to the accumulator register
  3083. \param[out] none
  3084. \retval none
  3085. */
  3086. void enet_ptp_finecorrection_adjfreq(int32_t carry_cfg)
  3087. {
  3088. /* re-configure the timestamp addend, then update */
  3089. enet_ptp_timestamp_addend_config((uint32_t)carry_cfg);
  3090. enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
  3091. }
  3092. /*!
  3093. \brief update system time in coarse method
  3094. \param[in] systime_struct: : pointer to a enet_ptp_systime_struct structure which contains
  3095. parameters of PTP system time
  3096. members of the structure and the member values are shown as below:
  3097. second: 0x0 - 0xFFFF FFFF
  3098. nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
  3099. sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
  3100. \param[out] none
  3101. \retval none
  3102. */
  3103. void enet_ptp_coarsecorrection_systime_update(enet_ptp_systime_struct *systime_struct)
  3104. {
  3105. uint32_t subsecond_val;
  3106. uint32_t carry_cfg;
  3107. subsecond_val = enet_ptp_nanosecond_2_subsecond(systime_struct->nanosecond);
  3108. /* save the carry_cfg value */
  3109. carry_cfg = ENET_PTP_TSADDEND_TMSA;
  3110. /* update the system time */
  3111. enet_ptp_timestamp_update_config(systime_struct->sign, systime_struct->second, subsecond_val);
  3112. enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_UPDATE);
  3113. /* wait until the update is completed */
  3114. while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_SYSTIME_UPDATE)){
  3115. }
  3116. /* write back the carry_cfg value, then update */
  3117. enet_ptp_timestamp_addend_config(carry_cfg);
  3118. enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
  3119. }
  3120. /*!
  3121. \brief set system time in fine method
  3122. \param[in] systime_struct: : pointer to a enet_ptp_systime_struct structure which contains
  3123. parameters of PTP system time
  3124. members of the structure and the member values are shown as below:
  3125. second: 0x0 - 0xFFFF FFFF
  3126. nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
  3127. sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
  3128. \param[out] none
  3129. \retval none
  3130. */
  3131. void enet_ptp_finecorrection_settime(enet_ptp_systime_struct * systime_struct)
  3132. {
  3133. uint32_t subsecond_val;
  3134. subsecond_val = enet_ptp_nanosecond_2_subsecond(systime_struct->nanosecond);
  3135. /* initialize the system time */
  3136. enet_ptp_timestamp_update_config(systime_struct->sign, systime_struct->second, subsecond_val);
  3137. enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
  3138. /* wait until the system time initialzation finished */
  3139. while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_SYSTIME_INIT)){
  3140. }
  3141. }
  3142. /*!
  3143. \brief get the ptp flag status
  3144. \param[in] flag: ptp flag status to be checked
  3145. \arg ENET_PTP_ADDEND_UPDATE: addend register update
  3146. \arg ENET_PTP_SYSTIME_UPDATE: timestamp update
  3147. \arg ENET_PTP_SYSTIME_INIT: timestamp initialize
  3148. \param[out] none
  3149. \retval FlagStatus: SET or RESET
  3150. */
  3151. FlagStatus enet_ptp_flag_get(uint32_t flag)
  3152. {
  3153. FlagStatus bitstatus = RESET;
  3154. if ((uint32_t)RESET != (ENET_PTP_TSCTL & flag)){
  3155. bitstatus = SET;
  3156. }
  3157. return bitstatus;
  3158. }
  3159. /*!
  3160. \brief reset the ENET initpara struct, call it before using enet_initpara_config()
  3161. \param[in] none
  3162. \param[out] none
  3163. \retval none
  3164. */
  3165. void enet_initpara_reset(void)
  3166. {
  3167. enet_initpara.option_enable = 0U;
  3168. enet_initpara.forward_frame = 0U;
  3169. enet_initpara.dmabus_mode = 0U;
  3170. enet_initpara.dma_maxburst = 0U;
  3171. enet_initpara.dma_arbitration = 0U;
  3172. enet_initpara.store_forward_mode = 0U;
  3173. enet_initpara.dma_function = 0U;
  3174. enet_initpara.vlan_config = 0U;
  3175. enet_initpara.flow_control = 0U;
  3176. enet_initpara.hashtable_high = 0U;
  3177. enet_initpara.hashtable_low = 0U;
  3178. enet_initpara.framesfilter_mode = 0U;
  3179. enet_initpara.halfduplex_param = 0U;
  3180. enet_initpara.timer_config = 0U;
  3181. enet_initpara.interframegap = 0U;
  3182. }
  3183. /*!
  3184. \brief initialize ENET peripheral with generally concerned parameters, call it by enet_init()
  3185. \param[in] none
  3186. \param[out] none
  3187. \retval none
  3188. */
  3189. static void enet_default_init(void)
  3190. {
  3191. uint32_t reg_value = 0U;
  3192. /* MAC */
  3193. /* configure ENET_MAC_CFG register */
  3194. reg_value = ENET_MAC_CFG;
  3195. reg_value &= MAC_CFG_MASK;
  3196. reg_value |= ENET_WATCHDOG_ENABLE | ENET_JABBER_ENABLE | ENET_INTERFRAMEGAP_96BIT \
  3197. | ENET_SPEEDMODE_10M |ENET_MODE_HALFDUPLEX | ENET_LOOPBACKMODE_DISABLE \
  3198. | ENET_CARRIERSENSE_ENABLE | ENET_RECEIVEOWN_ENABLE \
  3199. | ENET_RETRYTRANSMISSION_ENABLE | ENET_BACKOFFLIMIT_10 \
  3200. | ENET_DEFERRALCHECK_DISABLE \
  3201. | ENET_TYPEFRAME_CRC_DROP_DISABLE \
  3202. | ENET_AUTO_PADCRC_DROP_DISABLE \
  3203. | ENET_CHECKSUMOFFLOAD_DISABLE;
  3204. ENET_MAC_CFG = reg_value;
  3205. /* configure ENET_MAC_FRMF register */
  3206. ENET_MAC_FRMF = ENET_SRC_FILTER_DISABLE |ENET_DEST_FILTER_INVERSE_DISABLE \
  3207. |ENET_MULTICAST_FILTER_PERFECT |ENET_UNICAST_FILTER_PERFECT \
  3208. |ENET_PCFRM_PREVENT_ALL |ENET_BROADCASTFRAMES_ENABLE \
  3209. |ENET_PROMISCUOUS_DISABLE |ENET_RX_FILTER_ENABLE;
  3210. /* configure ENET_MAC_HLH, ENET_MAC_HLL register */
  3211. ENET_MAC_HLH = 0x0U;
  3212. ENET_MAC_HLL = 0x0U;
  3213. /* configure ENET_MAC_FCTL, ENET_MAC_FCTH register */
  3214. reg_value = ENET_MAC_FCTL;
  3215. reg_value &= MAC_FCTL_MASK;
  3216. reg_value |= MAC_FCTL_PTM(0) |ENET_ZERO_QUANTA_PAUSE_DISABLE \
  3217. |ENET_PAUSETIME_MINUS4 |ENET_UNIQUE_PAUSEDETECT \
  3218. |ENET_RX_FLOWCONTROL_DISABLE |ENET_TX_FLOWCONTROL_DISABLE;
  3219. ENET_MAC_FCTL = reg_value;
  3220. ENET_MAC_FCTH = ENET_DEACTIVE_THRESHOLD_512BYTES |ENET_ACTIVE_THRESHOLD_1536BYTES;
  3221. /* configure ENET_MAC_VLT register */
  3222. ENET_MAC_VLT = ENET_VLANTAGCOMPARISON_16BIT |MAC_VLT_VLTI(0);
  3223. /* DMA */
  3224. /* configure ENET_DMA_CTL register */
  3225. reg_value = ENET_DMA_CTL;
  3226. reg_value &= DMA_CTL_MASK;
  3227. reg_value |= ENET_TCPIP_CKSUMERROR_DROP |ENET_RX_MODE_STOREFORWARD \
  3228. |ENET_FLUSH_RXFRAME_ENABLE |ENET_TX_MODE_STOREFORWARD \
  3229. |ENET_TX_THRESHOLD_64BYTES |ENET_RX_THRESHOLD_64BYTES \
  3230. |ENET_FORWARD_ERRFRAMES_DISABLE |ENET_FORWARD_UNDERSZ_GOODFRAMES_DISABLE \
  3231. |ENET_SECONDFRAME_OPT_DISABLE;
  3232. ENET_DMA_CTL = reg_value;
  3233. /* configure ENET_DMA_BCTL register */
  3234. reg_value = ENET_DMA_BCTL;
  3235. reg_value &= DMA_BCTL_MASK;
  3236. reg_value = ENET_ADDRESS_ALIGN_ENABLE |ENET_ARBITRATION_RXTX_2_1 \
  3237. |ENET_RXDP_32BEAT |ENET_PGBL_32BEAT |ENET_RXTX_DIFFERENT_PGBL \
  3238. |ENET_FIXED_BURST_ENABLE |ENET_MIXED_BURST_DISABLE \
  3239. |ENET_NORMAL_DESCRIPTOR;
  3240. ENET_DMA_BCTL = reg_value;
  3241. }
  3242. #ifndef USE_DELAY
  3243. /*!
  3244. \brief insert a delay time
  3245. \param[in] ncount: specifies the delay time length
  3246. \param[out] none
  3247. \param[out] none
  3248. */
  3249. static void enet_delay(uint32_t ncount)
  3250. {
  3251. uint32_t delay_time = 0U;
  3252. for(delay_time = ncount; delay_time != 0U; delay_time--){
  3253. }
  3254. }
  3255. #endif /* USE_DELAY */
  3256. #endif /* GD32F30X_CL */