gd32f30x_enet.c 151 KB

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