1
0

gd32f10x_enet.c 124 KB

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