gd32f4xx_enet.c 144 KB

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