gd32f30x_enet.c 155 KB

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