gd32f4xx_enet.c 142 KB

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