gd32f10x_enet.c 127 KB

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