gd32f30x_enet.c 151 KB

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