1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688 |
- /*!
- \file gd32f30x_enet.c
- \brief ENET driver
- \version 2023-12-30, V2.2.0, firmware for GD32F30x
- */
- /*
- Copyright (c) 2020, GigaDevice Semiconductor Inc.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software without
- specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- OF SUCH DAMAGE.
- */
- #include "gd32f30x_enet.h"
- #include <stdlib.h>
- #ifdef GD32F30X_CL
- #if defined (__CC_ARM) /*!< ARM compiler */
- __align(4)
- enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM]; /*!< ENET RxDMA descriptor */
- __align(4)
- enet_descriptors_struct txdesc_tab[ENET_TXBUF_NUM]; /*!< ENET TxDMA descriptor */
- __align(4)
- uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE]; /*!< ENET receive buffer */
- __align(4)
- uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE]; /*!< ENET transmit buffer */
- #elif defined ( __ICCARM__ ) /*!< IAR compiler */
- #pragma data_alignment=4
- enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM]; /*!< ENET RxDMA descriptor */
- #pragma data_alignment=4
- enet_descriptors_struct txdesc_tab[ENET_TXBUF_NUM]; /*!< ENET TxDMA descriptor */
- #pragma data_alignment=4
- uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE]; /*!< ENET receive buffer */
- #pragma data_alignment=4
- uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE]; /*!< ENET transmit buffer */
- #elif defined (__GNUC__) /* GNU Compiler */
- enet_descriptors_struct rxdesc_tab[ENET_RXBUF_NUM] __attribute__ ((aligned (4))); /*!< ENET RxDMA descriptor */
- enet_descriptors_struct txdesc_tab[ENET_TXBUF_NUM] __attribute__ ((aligned (4))); /*!< ENET TxDMA descriptor */
- uint8_t rx_buff[ENET_RXBUF_NUM][ENET_RXBUF_SIZE] __attribute__ ((aligned (4))); /*!< ENET receive buffer */
- uint8_t tx_buff[ENET_TXBUF_NUM][ENET_TXBUF_SIZE] __attribute__ ((aligned (4))); /*!< ENET transmit buffer */
- #endif /* __CC_ARM */
- /* global transmit and receive descriptors pointers */
- enet_descriptors_struct *dma_current_txdesc;
- enet_descriptors_struct *dma_current_rxdesc;
- /* structure pointer of ptp descriptor for normal mode */
- enet_descriptors_struct *dma_current_ptp_txdesc = NULL;
- enet_descriptors_struct *dma_current_ptp_rxdesc = NULL;
- /* init structure parameters for ENET initialization */
- static enet_initpara_struct enet_initpara ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- static uint32_t enet_unknow_err = 0U;
- /* array of register offset for debug information get */
- static const uint16_t enet_reg_tab[] = {
- 0x0000, 0x0004, 0x0008, 0x000C, 0x0010, 0x0014, 0x0018, 0x001C, 0x0028, 0x002C, 0x0034,
- 0x0038, 0x003C, 0x0040, 0x0044, 0x0048, 0x004C, 0x0050, 0x0054, 0x0058, 0x005C, 0x1080,
-
- 0x0100, 0x0104, 0x0108, 0x010C, 0x0110, 0x014C, 0x0150, 0x0168, 0x0194, 0x0198, 0x01C4,
-
- 0x0700, 0x0704,0x0708, 0x070C, 0x0710, 0x0714, 0x0718, 0x071C, 0x0720, 0x0728, 0x072C,
-
- 0x1000, 0x1004, 0x1008, 0x100C, 0x1010, 0x1014, 0x1018, 0x101C, 0x1020, 0x1024, 0x1048,
- 0x104C, 0x1050, 0x1054};
- /* initialize ENET peripheral with generally concerned parameters, call it by enet_init() */
- static void enet_default_init(void);
- #ifndef USE_DELAY
- /* insert a delay time */
- static void enet_delay(uint32_t ncount);
- #endif /* USE_DELAY */
- /*!
- \brief deinitialize the ENET, and reset structure parameters for ENET initialization
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_deinit(void)
- {
- rcu_periph_reset_enable(RCU_ENETRST);
- rcu_periph_reset_disable(RCU_ENETRST);
- enet_initpara_reset();
- }
- /*!
- \brief configure the parameters which are usually less cared for initialization
- note -- this function must be called before enet_init(), otherwise
- configuration will be no effect
- \param[in] option: different function option, which is related to several parameters,
- only one parameter can be selected which is shown as below, refer to enet_option_enum
- \arg FORWARD_OPTION: choose to configure the frame forward related parameters
- \arg DMABUS_OPTION: choose to configure the DMA bus mode related parameters
- \arg DMA_MAXBURST_OPTION: choose to configure the DMA max burst related parameters
- \arg DMA_ARBITRATION_OPTION: choose to configure the DMA arbitration related parameters
- \arg STORE_OPTION: choose to configure the store forward mode related parameters
- \arg DMA_OPTION: choose to configure the DMA descriptor related parameters
- \arg VLAN_OPTION: choose to configure vlan related parameters
- \arg FLOWCTL_OPTION: choose to configure flow control related parameters
- \arg HASHH_OPTION: choose to configure hash high
- \arg HASHL_OPTION: choose to configure hash low
- \arg FILTER_OPTION: choose to configure frame filter related parameters
- \arg HALFDUPLEX_OPTION: choose to configure halfduplex mode related parameters
- \arg TIMER_OPTION: choose to configure time counter related parameters
- \arg INTERFRAMEGAP_OPTION: choose to configure the inter frame gap related parameters
- \param[in] para: the related parameters according to the option
- all the related parameters should be configured which are shown as below
- FORWARD_OPTION related parameters:
- - ENET_AUTO_PADCRC_DROP_ENABLE/ ENET_AUTO_PADCRC_DROP_DISABLE ;
- - ENET_TYPEFRAME_CRC_DROP_ENABLE/ ENET_TYPEFRAME_CRC_DROP_DISABLE ;
- - ENET_FORWARD_ERRFRAMES_ENABLE/ ENET_FORWARD_ERRFRAMES_DISABLE ;
- - ENET_FORWARD_UNDERSZ_GOODFRAMES_ENABLE/ ENET_FORWARD_UNDERSZ_GOODFRAMES_DISABLE .
- DMABUS_OPTION related parameters:
- - ENET_ADDRESS_ALIGN_ENABLE/ ENET_ADDRESS_ALIGN_DISABLE ;
- - ENET_FIXED_BURST_ENABLE/ ENET_FIXED_BURST_DISABLE ;
- - ENET_MIXED_BURST_ENABLE/ ENET_MIXED_BURST_DISABLE ;
- DMA_MAXBURST_OPTION related parameters:
- - ENET_RXDP_1BEAT/ ENET_RXDP_2BEAT/ ENET_RXDP_4BEAT/
- ENET_RXDP_8BEAT/ ENET_RXDP_16BEAT/ ENET_RXDP_32BEAT/
- ENET_RXDP_4xPGBL_4BEAT/ ENET_RXDP_4xPGBL_8BEAT/
- ENET_RXDP_4xPGBL_16BEAT/ ENET_RXDP_4xPGBL_32BEAT/
- ENET_RXDP_4xPGBL_64BEAT/ ENET_RXDP_4xPGBL_128BEAT ;
- - ENET_PGBL_1BEAT/ ENET_PGBL_2BEAT/ ENET_PGBL_4BEAT/
- ENET_PGBL_8BEAT/ ENET_PGBL_16BEAT/ ENET_PGBL_32BEAT/
- ENET_PGBL_4xPGBL_4BEAT/ ENET_PGBL_4xPGBL_8BEAT/
- ENET_PGBL_4xPGBL_16BEAT/ ENET_PGBL_4xPGBL_32BEAT/
- ENET_PGBL_4xPGBL_64BEAT/ ENET_PGBL_4xPGBL_128BEAT ;
- - ENET_RXTX_DIFFERENT_PGBL/ ENET_RXTX_SAME_PGBL ;
- DMA_ARBITRATION_OPTION related parameters:
- - ENET_ARBITRATION_RXPRIORTX
- - ENET_ARBITRATION_RXTX_1_1/ ENET_ARBITRATION_RXTX_2_1/
- ENET_ARBITRATION_RXTX_3_1/ ENET_ARBITRATION_RXTX_4_1/.
- STORE_OPTION related parameters:
- - ENET_RX_MODE_STOREFORWARD/ ENET_RX_MODE_CUTTHROUGH ;
- - ENET_TX_MODE_STOREFORWARD/ ENET_TX_MODE_CUTTHROUGH ;
- - ENET_RX_THRESHOLD_64BYTES/ ENET_RX_THRESHOLD_32BYTES/
- ENET_RX_THRESHOLD_96BYTES/ ENET_RX_THRESHOLD_128BYTES ;
- - ENET_TX_THRESHOLD_64BYTES/ ENET_TX_THRESHOLD_128BYTES/
- ENET_TX_THRESHOLD_192BYTES/ ENET_TX_THRESHOLD_256BYTES/
- ENET_TX_THRESHOLD_40BYTES/ ENET_TX_THRESHOLD_32BYTES/
- ENET_TX_THRESHOLD_24BYTES/ ENET_TX_THRESHOLD_16BYTES .
- DMA_OPTION related parameters:
- - ENET_FLUSH_RXFRAME_ENABLE/ ENET_FLUSH_RXFRAME_DISABLE ;
- - ENET_SECONDFRAME_OPT_ENABLE/ ENET_SECONDFRAME_OPT_DISABLE ;
- - ENET_ENHANCED_DESCRIPTOR/ ENET_NORMAL_DESCRIPTOR .
- VLAN_OPTION related parameters:
- - ENET_VLANTAGCOMPARISON_12BIT/ ENET_VLANTAGCOMPARISON_16BIT ;
- - MAC_VLT_VLTI(regval) .
- FLOWCTL_OPTION related parameters:
- - MAC_FCTL_PTM(regval) ;
- - ENET_ZERO_QUANTA_PAUSE_ENABLE/ ENET_ZERO_QUANTA_PAUSE_DISABLE ;
- - ENET_PAUSETIME_MINUS4/ ENET_PAUSETIME_MINUS28/
- ENET_PAUSETIME_MINUS144/ENET_PAUSETIME_MINUS256 ;
- - ENET_MAC0_AND_UNIQUE_ADDRESS_PAUSEDETECT/ ENET_UNIQUE_PAUSEDETECT ;
- - ENET_RX_FLOWCONTROL_ENABLE/ ENET_RX_FLOWCONTROL_DISABLE ;
- - ENET_TX_FLOWCONTROL_ENABLE/ ENET_TX_FLOWCONTROL_DISABLE ;
- - ENET_ACTIVE_THRESHOLD_256BYTES/ ENET_ACTIVE_THRESHOLD_512BYTES ;
- - ENET_ACTIVE_THRESHOLD_768BYTES/ ENET_ACTIVE_THRESHOLD_1024BYTES ;
- - ENET_ACTIVE_THRESHOLD_1280BYTES/ ENET_ACTIVE_THRESHOLD_1536BYTES ;
- - ENET_ACTIVE_THRESHOLD_1792BYTES ;
- - ENET_DEACTIVE_THRESHOLD_256BYTES/ ENET_DEACTIVE_THRESHOLD_512BYTES ;
- - ENET_DEACTIVE_THRESHOLD_768BYTES/ ENET_DEACTIVE_THRESHOLD_1024BYTES ;
- - ENET_DEACTIVE_THRESHOLD_1280BYTES/ ENET_DEACTIVE_THRESHOLD_1536BYTES ;
- - ENET_DEACTIVE_THRESHOLD_1792BYTES .
- HASHH_OPTION related parameters:
- - 0x0~0xFFFF FFFFU
- HASHL_OPTION related parameters:
- - 0x0~0xFFFF FFFFU
- FILTER_OPTION related parameters:
- - ENET_SRC_FILTER_NORMAL_ENABLE/ ENET_SRC_FILTER_INVERSE_ENABLE/
- ENET_SRC_FILTER_DISABLE ;
- - ENET_DEST_FILTER_INVERSE_ENABLE/ ENET_DEST_FILTER_INVERSE_DISABLE ;
- - ENET_MULTICAST_FILTER_HASH_OR_PERFECT/ ENET_MULTICAST_FILTER_HASH/
- ENET_MULTICAST_FILTER_PERFECT/ ENET_MULTICAST_FILTER_NONE ;
- - ENET_UNICAST_FILTER_EITHER/ ENET_UNICAST_FILTER_HASH/
- ENET_UNICAST_FILTER_PERFECT ;
- - ENET_PCFRM_PREVENT_ALL/ ENET_PCFRM_PREVENT_PAUSEFRAME/
- ENET_PCFRM_FORWARD_ALL/ ENET_PCFRM_FORWARD_FILTERED .
- HALFDUPLEX_OPTION related parameters:
- - ENET_CARRIERSENSE_ENABLE/ ENET_CARRIERSENSE_DISABLE ;
- - ENET_RECEIVEOWN_ENABLE/ ENET_RECEIVEOWN_DISABLE ;
- - ENET_RETRYTRANSMISSION_ENABLE/ ENET_RETRYTRANSMISSION_DISABLE ;
- - ENET_BACKOFFLIMIT_10/ ENET_BACKOFFLIMIT_8/
- ENET_BACKOFFLIMIT_4/ ENET_BACKOFFLIMIT_1 ;
- - ENET_DEFERRALCHECK_ENABLE/ ENET_DEFERRALCHECK_DISABLE .
- TIMER_OPTION related parameters:
- - ENET_WATCHDOG_ENABLE/ ENET_WATCHDOG_DISABLE ;
- - ENET_JABBER_ENABLE/ ENET_JABBER_DISABLE ;
- INTERFRAMEGAP_OPTION related parameters:
- - ENET_INTERFRAMEGAP_96BIT/ ENET_INTERFRAMEGAP_88BIT/
- ENET_INTERFRAMEGAP_80BIT/ ENET_INTERFRAMEGAP_72BIT/
- ENET_INTERFRAMEGAP_64BIT/ ENET_INTERFRAMEGAP_56BIT/
- ENET_INTERFRAMEGAP_48BIT/ ENET_INTERFRAMEGAP_40BIT .
- \param[out] none
- \retval none
- */
- void enet_initpara_config(enet_option_enum option, uint32_t para)
- {
- switch(option){
- case FORWARD_OPTION:
- /* choose to configure forward_frame, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)FORWARD_OPTION;
- enet_initpara.forward_frame = para;
- break;
- case DMABUS_OPTION:
- /* choose to configure dmabus_mode, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)DMABUS_OPTION;
- enet_initpara.dmabus_mode = para;
- break;
- case DMA_MAXBURST_OPTION:
- /* choose to configure dma_maxburst, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)DMA_MAXBURST_OPTION;
- enet_initpara.dma_maxburst = para;
- break;
- case DMA_ARBITRATION_OPTION:
- /* choose to configure dma_arbitration, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)DMA_ARBITRATION_OPTION;
- enet_initpara.dma_arbitration = para;
- break;
- case STORE_OPTION:
- /* choose to configure store_forward_mode, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)STORE_OPTION;
- enet_initpara.store_forward_mode = para;
- break;
- case DMA_OPTION:
- /* choose to configure dma_function, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)DMA_OPTION;
- #ifndef SELECT_DESCRIPTORS_ENHANCED_MODE
- para &= ~ENET_ENHANCED_DESCRIPTOR;
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- enet_initpara.dma_function = para;
- break;
- case VLAN_OPTION:
- /* choose to configure vlan_config, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)VLAN_OPTION;
- enet_initpara.vlan_config = para;
- break;
- case FLOWCTL_OPTION:
- /* choose to configure flow_control, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)FLOWCTL_OPTION;
- enet_initpara.flow_control = para;
- break;
- case HASHH_OPTION:
- /* choose to configure hashtable_high, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)HASHH_OPTION;
- enet_initpara.hashtable_high = para;
- break;
- case HASHL_OPTION:
- /* choose to configure hashtable_low, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)HASHL_OPTION;
- enet_initpara.hashtable_low = para;
- break;
- case FILTER_OPTION:
- /* choose to configure framesfilter_mode, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)FILTER_OPTION;
- enet_initpara.framesfilter_mode = para;
- break;
- case HALFDUPLEX_OPTION:
- /* choose to configure halfduplex_param, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)HALFDUPLEX_OPTION;
- enet_initpara.halfduplex_param = para;
- break;
- case TIMER_OPTION:
- /* choose to configure timer_config, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)TIMER_OPTION;
- enet_initpara.timer_config = para;
- break;
- case INTERFRAMEGAP_OPTION:
- /* choose to configure interframegap, and save the configuration parameters */
- enet_initpara.option_enable |= (uint32_t)INTERFRAMEGAP_OPTION;
- enet_initpara.interframegap = para;
- break;
- default:
- break;
- }
- }
- /*!
- \brief initialize ENET peripheral with generally concerned parameters and the less cared
- parameters
- \param[in] mediamode: PHY mode and mac loopback configurations, only one parameter can be selected
- which is shown as below, refer to enet_mediamode_enum
- \arg ENET_AUTO_NEGOTIATION: PHY auto negotiation
- \arg ENET_100M_FULLDUPLEX: 100Mbit/s, full-duplex
- \arg ENET_100M_HALFDUPLEX: 100Mbit/s, half-duplex
- \arg ENET_10M_FULLDUPLEX: 10Mbit/s, full-duplex
- \arg ENET_10M_HALFDUPLEX: 10Mbit/s, half-duplex
- \arg ENET_LOOPBACKMODE: MAC in loopback mode at the MII
- \param[in] checksum: IP frame checksum offload function, only one parameter can be selected
- which is shown as below, refer to enet_mediamode_enum
- \arg ENET_NO_AUTOCHECKSUM: disable IP frame checksum function
- \arg ENET_AUTOCHECKSUM_DROP_FAILFRAMES: enable IP frame checksum function
- \arg ENET_AUTOCHECKSUM_ACCEPT_FAILFRAMES: enable IP frame checksum function, and the received frame
- with only payload error but no other errors will not be dropped
- \param[in] recept: frame filter function, only one parameter can be selected
- which is shown as below, refer to enet_frmrecept_enum
- \arg ENET_PROMISCUOUS_MODE: promiscuous mode enabled
- \arg ENET_RECEIVEALL: all received frame are forwarded to application
- \arg ENET_BROADCAST_FRAMES_PASS: the address filters pass all received broadcast frames
- \arg ENET_BROADCAST_FRAMES_DROP: the address filters filter all incoming broadcast frames
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_init(enet_mediamode_enum mediamode, enet_chksumconf_enum checksum, enet_frmrecept_enum recept)
- {
- uint32_t reg_value=0U, reg_temp = 0U, temp = 0U;
- uint32_t media_temp = 0U;
- uint32_t timeout = 0U;
- uint16_t phy_value = 0U;
- ErrStatus phy_state= ERROR, enet_state = ERROR;
- /* PHY interface configuration, configure SMI clock and reset PHY chip */
- if(ERROR == enet_phy_config()){
- _ENET_DELAY_(PHY_RESETDELAY);
- if(ERROR == enet_phy_config()){
- return enet_state;
- }
- }
- /* initialize ENET peripheral with generally concerned parameters */
- enet_default_init();
- /* 1st, configure mediamode */
- media_temp = (uint32_t)mediamode;
- /* if is PHY auto negotiation */
- if((uint32_t)ENET_AUTO_NEGOTIATION == media_temp){
- /* wait for PHY_LINKED_STATUS bit be set */
- do{
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
- phy_value &= PHY_LINKED_STATUS;
- timeout++;
- }while((RESET == phy_value) && (timeout < PHY_READ_TO));
- /* return ERROR due to timeout */
- if(PHY_READ_TO == timeout){
- return enet_state;
- }
- /* reset timeout counter */
- timeout = 0U;
- /* enable auto-negotiation */
- phy_value = PHY_AUTONEGOTIATION;
- phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value);
- if(!phy_state){
- /* return ERROR due to write timeout */
- return enet_state;
- }
- /* wait for the PHY_AUTONEGO_COMPLETE bit be set */
- do{
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
- phy_value &= PHY_AUTONEGO_COMPLETE;
- timeout++;
- }while((RESET == phy_value) && (timeout < (uint32_t)PHY_READ_TO));
- /* return ERROR due to timeout */
- if(PHY_READ_TO == timeout){
- return enet_state;
- }
- /* reset timeout counter */
- timeout = 0U;
- /* read the result of the auto-negotiation */
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_SR, &phy_value);
- /* configure the duplex mode of MAC following the auto-negotiation result */
- if((uint16_t)RESET != (phy_value & PHY_DUPLEX_STATUS)){
- media_temp = ENET_MODE_FULLDUPLEX;
- }else{
- media_temp = ENET_MODE_HALFDUPLEX;
- }
- /* configure the communication speed of MAC following the auto-negotiation result */
- if((uint16_t)RESET !=(phy_value & PHY_SPEED_STATUS)){
- media_temp |= ENET_SPEEDMODE_10M;
- }else{
- media_temp |= ENET_SPEEDMODE_100M;
- }
- }else{
- phy_value = (uint16_t)((media_temp & ENET_MAC_CFG_DPM) >> 3);
- phy_value |= (uint16_t)((media_temp & ENET_MAC_CFG_SPD) >> 1);
- phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value);
- if(!phy_state){
- /* return ERROR due to write timeout */
- return enet_state;
- }
- /* PHY configuration need some time */
- _ENET_DELAY_(PHY_CONFIGDELAY);
- }
- /* after configuring the PHY, use mediamode to configure registers */
- reg_value = ENET_MAC_CFG;
- /* configure ENET_MAC_CFG register */
- reg_value &= (~(ENET_MAC_CFG_SPD |ENET_MAC_CFG_DPM |ENET_MAC_CFG_LBM));
- reg_value |= media_temp;
- ENET_MAC_CFG = reg_value;
- /* 2st, configure checksum */
- if(RESET != ((uint32_t)checksum & ENET_CHECKSUMOFFLOAD_ENABLE)){
- ENET_MAC_CFG |= ENET_CHECKSUMOFFLOAD_ENABLE;
- reg_value = ENET_DMA_CTL;
- /* configure ENET_DMA_CTL register */
- reg_value &= ~ENET_DMA_CTL_DTCERFD;
- reg_value |= ((uint32_t)checksum & ENET_DMA_CTL_DTCERFD);
- ENET_DMA_CTL = reg_value;
- }
- /* 3rd, configure recept */
- ENET_MAC_FRMF |= (uint32_t)recept;
- /* 4th, configure different function options */
- /* configure forward_frame related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)FORWARD_OPTION)){
- reg_temp = enet_initpara.forward_frame;
- reg_value = ENET_MAC_CFG;
- temp = reg_temp;
- /* configure ENET_MAC_CFG register */
- reg_value &= (~(ENET_MAC_CFG_TFCD |ENET_MAC_CFG_APCD));
- temp &= (ENET_MAC_CFG_TFCD | ENET_MAC_CFG_APCD);
- reg_value |= temp;
- ENET_MAC_CFG = reg_value;
- reg_value = ENET_DMA_CTL;
- temp = reg_temp;
- /* configure ENET_DMA_CTL register */
- reg_value &= (~(ENET_DMA_CTL_FERF |ENET_DMA_CTL_FUF));
- temp &= ((ENET_DMA_CTL_FERF | ENET_DMA_CTL_FUF)<<2);
- reg_value |= (temp >> 2);
- ENET_DMA_CTL = reg_value;
- }
- /* configure dmabus_mode related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)DMABUS_OPTION)){
- temp = enet_initpara.dmabus_mode;
- reg_value = ENET_DMA_BCTL;
- /* configure ENET_DMA_BCTL register */
- reg_value &= ~(ENET_DMA_BCTL_AA | ENET_DMA_BCTL_FB \
- |ENET_DMA_BCTL_FPBL | ENET_DMA_BCTL_MB);
- reg_value |= temp;
- ENET_DMA_BCTL = reg_value;
- }
- /* configure dma_maxburst related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_MAXBURST_OPTION)){
- temp = enet_initpara.dma_maxburst;
- reg_value = ENET_DMA_BCTL;
- /* configure ENET_DMA_BCTL register */
- reg_value &= ~(ENET_DMA_BCTL_RXDP| ENET_DMA_BCTL_PGBL | ENET_DMA_BCTL_UIP);
- reg_value |= temp;
- ENET_DMA_BCTL = reg_value;
- }
- /* configure dma_arbitration related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_ARBITRATION_OPTION)){
- temp = enet_initpara.dma_arbitration;
- reg_value = ENET_DMA_BCTL;
- /* configure ENET_DMA_BCTL register */
- reg_value &= ~(ENET_DMA_BCTL_RTPR | ENET_DMA_BCTL_DAB);
- reg_value |= temp;
- ENET_DMA_BCTL = reg_value;
- }
- /* configure store_forward_mode related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)STORE_OPTION)){
- temp = enet_initpara.store_forward_mode;
-
- reg_value = ENET_DMA_CTL;
- /* configure ENET_DMA_CTL register */
- reg_value &= ~(ENET_DMA_CTL_RSFD | ENET_DMA_CTL_TSFD| ENET_DMA_CTL_RTHC| ENET_DMA_CTL_TTHC);
- reg_value |= temp;
- ENET_DMA_CTL = reg_value;
- }
- /* configure dma_function related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)DMA_OPTION)){
- reg_temp = enet_initpara.dma_function;
- reg_value = ENET_DMA_CTL;
- temp = reg_temp;
- /* configure ENET_DMA_CTL register */
- reg_value &= (~(ENET_DMA_CTL_DAFRF |ENET_DMA_CTL_OSF));
- temp &= (ENET_DMA_CTL_DAFRF | ENET_DMA_CTL_OSF);
- reg_value |= temp;
- ENET_DMA_CTL = reg_value;
- reg_value = ENET_DMA_BCTL;
- temp = reg_temp;
- /* configure ENET_DMA_BCTL register */
- reg_value &= (~ENET_DMA_BCTL_DFM);
- temp &= ENET_DMA_BCTL_DFM;
- reg_value |= temp;
- ENET_DMA_BCTL = reg_value;
- }
- /* configure vlan_config related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)VLAN_OPTION)){
- reg_temp = enet_initpara.vlan_config;
-
- reg_value = ENET_MAC_VLT;
- /* configure ENET_MAC_VLT register */
- reg_value &= ~(ENET_MAC_VLT_VLTI | ENET_MAC_VLT_VLTC);
- reg_value |= reg_temp;
- ENET_MAC_VLT = reg_value;
- }
- /* configure flow_control related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)FLOWCTL_OPTION)){
- reg_temp = enet_initpara.flow_control;
-
- reg_value = ENET_MAC_FCTL;
- temp = reg_temp;
- /* configure ENET_MAC_FCTL register */
- reg_value &= ~(ENET_MAC_FCTL_PTM |ENET_MAC_FCTL_DZQP |ENET_MAC_FCTL_PLTS \
- | ENET_MAC_FCTL_UPFDT |ENET_MAC_FCTL_RFCEN |ENET_MAC_FCTL_TFCEN);
- temp &= (ENET_MAC_FCTL_PTM |ENET_MAC_FCTL_DZQP |ENET_MAC_FCTL_PLTS \
- | ENET_MAC_FCTL_UPFDT |ENET_MAC_FCTL_RFCEN |ENET_MAC_FCTL_TFCEN);
- reg_value |= temp;
- ENET_MAC_FCTL = reg_value;
-
- reg_value = ENET_MAC_FCTH;
- temp = reg_temp;
- /* configure ENET_MAC_FCTH register */
- reg_value &= ~(ENET_MAC_FCTH_RFA |ENET_MAC_FCTH_RFD);
- temp &= ((ENET_MAC_FCTH_RFA | ENET_MAC_FCTH_RFD )<<8);
- reg_value |= (temp >> 8);
- ENET_MAC_FCTH = reg_value;
- }
-
- /* configure hashtable_high related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)HASHH_OPTION)){
- ENET_MAC_HLH = enet_initpara.hashtable_high;
- }
- /* configure hashtable_low related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)HASHL_OPTION)){
- ENET_MAC_HLL = enet_initpara.hashtable_low;
- }
- /* configure framesfilter_mode related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)FILTER_OPTION)){
- reg_temp = enet_initpara.framesfilter_mode;
-
- reg_value = ENET_MAC_FRMF;
- /* configure ENET_MAC_FRMF register */
- reg_value &= ~(ENET_MAC_FRMF_SAFLT | ENET_MAC_FRMF_SAIFLT | ENET_MAC_FRMF_DAIFLT \
- | ENET_MAC_FRMF_HMF | ENET_MAC_FRMF_HPFLT | ENET_MAC_FRMF_MFD \
- | ENET_MAC_FRMF_HUF | ENET_MAC_FRMF_PCFRM);
- reg_value |= reg_temp;
- ENET_MAC_FRMF = reg_value;
- }
- /* configure halfduplex_param related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)HALFDUPLEX_OPTION)){
- reg_temp = enet_initpara.halfduplex_param;
- reg_value = ENET_MAC_CFG;
- /* configure ENET_MAC_CFG register */
- reg_value &= ~(ENET_MAC_CFG_CSD | ENET_MAC_CFG_ROD | ENET_MAC_CFG_RTD \
- | ENET_MAC_CFG_BOL | ENET_MAC_CFG_DFC);
- reg_value |= reg_temp;
- ENET_MAC_CFG = reg_value;
- }
- /* configure timer_config related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)TIMER_OPTION)){
- reg_temp = enet_initpara.timer_config;
-
- reg_value = ENET_MAC_CFG;
- /* configure ENET_MAC_CFG register */
- reg_value &= ~(ENET_MAC_CFG_WDD | ENET_MAC_CFG_JBD);
- reg_value |= reg_temp;
- ENET_MAC_CFG = reg_value;
- }
- /* configure interframegap related registers */
- if(RESET != (enet_initpara.option_enable & (uint32_t)INTERFRAMEGAP_OPTION)){
- reg_temp = enet_initpara.interframegap;
-
- reg_value = ENET_MAC_CFG;
- /* configure ENET_MAC_CFG register */
- reg_value &= ~ENET_MAC_CFG_IGBS;
- reg_value |= reg_temp;
- ENET_MAC_CFG = reg_value;
- }
- enet_state = SUCCESS;
- return enet_state;
- }
- /*!
- \brief reset all core internal registers located in CLK_TX and CLK_RX
- \param[in] none
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_software_reset(void)
- {
- uint32_t timeout = 0U;
- ErrStatus enet_state = ERROR;
- uint32_t dma_flag;
- /* reset all core internal registers located in CLK_TX and CLK_RX */
- ENET_DMA_BCTL |= ENET_DMA_BCTL_SWR;
- /* wait for reset operation complete */
- do{
- dma_flag = (ENET_DMA_BCTL & ENET_DMA_BCTL_SWR);
- timeout++;
- }while((RESET != dma_flag) && (ENET_DELAY_TO != timeout));
- /* reset operation complete */
- if(RESET == (ENET_DMA_BCTL & ENET_DMA_BCTL_SWR)){
- enet_state = SUCCESS;
- }
- return enet_state;
- }
- /*!
- \brief check receive frame valid and return frame size
- \param[in] none
- \param[out] none
- \retval size of received frame: 0x0 - 0x3FFF
- */
- uint32_t enet_rxframe_size_get(void)
- {
- uint32_t size = 0U;
- uint32_t status;
-
- /* get rdes0 information of current RxDMA descriptor */
- status = dma_current_rxdesc->status;
-
- /* if the desciptor is owned by DMA */
- if((uint32_t)RESET != (status & ENET_RDES0_DAV)){
- return 0U;
- }
-
- /* if has any error, or the frame uses two or more descriptors */
- if((((uint32_t)RESET) != (status & ENET_RDES0_ERRS)) ||
- (((uint32_t)RESET) == (status & ENET_RDES0_LDES)) ||
- (((uint32_t)RESET) == (status & ENET_RDES0_FDES))){
- /* drop current receive frame */
- enet_rxframe_drop();
- return 1U;
- }
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- /* if is an ethernet-type frame, and IP frame payload error occurred */
- if(((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_FRMT) &&
- ((uint32_t)RESET) != (dma_current_rxdesc->extended_status & ENET_RDES4_IPPLDERR)){
- /* drop current receive frame */
- enet_rxframe_drop();
- return 1U;
- }
- #else
- /* if is an ethernet-type frame, and IP frame payload error occurred */
- if((((uint32_t)RESET) != (status & ENET_RDES0_FRMT)) &&
- (((uint32_t)RESET) != (status & ENET_RDES0_PCERR))){
- /* drop current receive frame */
- enet_rxframe_drop();
- return 1U;
- }
- #endif
- /* if CPU owns current descriptor, no error occured, the frame uses only one descriptor */
- if((((uint32_t)RESET) == (status & ENET_RDES0_DAV)) &&
- (((uint32_t)RESET) == (status & ENET_RDES0_ERRS)) &&
- (((uint32_t)RESET) != (status & ENET_RDES0_LDES)) &&
- (((uint32_t)RESET) != (status & ENET_RDES0_FDES))){
- /* get the size of the received data including CRC */
- size = GET_RDES0_FRML(status);
- /* substract the CRC size */
- size = size - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (status & ENET_RDES0_FRMT))){
- size = size + 4U;
- }
- }else{
- enet_unknow_err++;
- enet_rxframe_drop();
- return 1U;
- }
- /* return packet size */
- return size;
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in chain mode
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[out] none
- \retval none
- */
- void enet_descriptors_chain_init(enet_dmadirection_enum direction)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc, *desc_tab;
- uint8_t *buf;
- /* if want to initialize DMA Tx descriptors */
- if (ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* select chain mode */
- desc_status = ENET_TDES0_TCHM;
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* select receive chained mode and set buffer1 size */
- desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
-
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- }
- dma_current_ptp_rxdesc = NULL;
- dma_current_ptp_txdesc = NULL;
- /* configure each descriptor */
- for(num=0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
- /* if is not the last descriptor */
- if(num < (count - 1U)){
- /* configure the next descriptor address */
- desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
- }else{
- /* when it is the last descriptor, the next descriptor address
- equals to first descriptor address in descriptor table */
- desc->buffer2_next_desc_addr = (uint32_t) desc_tab;
- }
- }
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in ring mode
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[out] none
- \retval none
- */
- void enet_descriptors_ring_init(enet_dmadirection_enum direction)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc;
- enet_descriptors_struct *desc_tab;
- uint8_t *buf;
- /* configure descriptor skip length */
- ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
- ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
- /* if want to initialize DMA Tx descriptors */
- if (ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* set buffer1 size */
- desc_bufsize = ENET_RXBUF_SIZE;
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- }
- dma_current_ptp_rxdesc = NULL;
- dma_current_ptp_txdesc = NULL;
- /* configure each descriptor */
- for(num=0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
-
- /* when it is the last descriptor */
- if(num == (count - 1U)){
- if (ENET_DMA_TX == direction){
- /* configure transmit end of ring mode */
- desc->status |= ENET_TDES0_TERM;
- }else{
- /* configure receive end of ring mode */
- desc->control_buffer_size |= ENET_RDES1_RERM;
- }
- }
- }
- }
- /*!
- \brief handle current received frame data to application buffer
- \param[in] bufsize: the size of buffer which is the parameter in function
- \param[out] buffer: pointer to the received frame data
- note -- if the input is NULL, user should copy data in application by himself
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_frame_receive(uint8_t *buffer, uint32_t bufsize)
- {
- uint32_t offset = 0U, size = 0U;
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has copied data in application */
- if(NULL != buffer){
- /* if no error occurs, and the frame uses only one descriptor */
- if((((uint32_t)RESET) == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
- (((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
- (((uint32_t)RESET) != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
- /* get the frame length except CRC */
- size = GET_RDES0_FRML(dma_current_rxdesc->status);
- size = size - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
- size = size + 4U;
- }
- /* to avoid situation that the frame size exceeds the buffer length */
- if(size > bufsize){
- return ERROR;
- }
- /* copy data from Rx buffer to application buffer */
- for(offset = 0U; offset<size; offset++){
- (*(buffer + offset)) = (*(__IO uint8_t *) (uint32_t)((dma_current_rxdesc->buffer1_addr) + offset));
- }
-
- }else{
- /* return ERROR */
- return ERROR;
- }
- }
- /* enable reception, descriptor is owned by DMA */
- dma_current_rxdesc->status = ENET_RDES0_DAV;
-
- /* check Rx buffer unavailable flag status */
- if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
- /* clear RBU flag */
- ENET_DMA_STAT = ENET_DMA_STAT_RBU;
- /* resume DMA reception by writing to the RPEN register*/
- ENET_DMA_RPEN = 0U;
- }
-
- /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
- /* chained mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + (GET_DMA_BCTL_DPSL(ENET_DMA_BCTL)));
- }
- }
- return SUCCESS;
- }
- /*!
- \brief handle application buffer data to transmit it
- \param[in] buffer: pointer to the frame data to be transmitted,
- note -- if the input is NULL, user should handle the data in application by himself
- \param[in] length: the length of frame data to be transmitted
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_frame_transmit(uint8_t *buffer, uint32_t length)
- {
- uint32_t offset = 0U;
- uint32_t dma_tbu_flag, dma_tu_flag;
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
- return ERROR;
- }
- /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
- if(length > ENET_MAX_FRAME_SIZE){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has handled data in application */
- if(NULL != buffer){
- /* copy frame data from application buffer to Tx buffer */
- for(offset = 0U; offset < length; offset++){
- (*(__IO uint8_t *) (uint32_t)((dma_current_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
- }
- }
- /* set the frame length */
- dma_current_txdesc->control_buffer_size = length;
- /* set the segment of frame, frame is transmitted in one descriptor */
- dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
- /* enable the DMA transmission */
- dma_current_txdesc->status |= ENET_TDES0_DAV;
- /* check Tx buffer unavailable flag status */
- dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
- dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
- if ((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
- /* clear TBU and TU flag */
- ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
- /* resume DMA transmission by writing to the TPEN register*/
- ENET_DMA_TPEN = 0U;
- }
- /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table*/
- /* chained mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
- dma_current_txdesc = (enet_descriptors_struct*) (dma_current_txdesc->buffer2_next_desc_addr);
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_txdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + (GET_DMA_BCTL_DPSL(ENET_DMA_BCTL)));
- }
- }
- return SUCCESS;
- }
- /*!
- \brief configure the transmit IP frame checksum offload calculation and insertion
- \param[in] desc: the descriptor pointer which users want to configure
- \param[in] checksum: IP frame checksum configuration
- only one parameter can be selected which is shown as below
- \arg ENET_CHECKSUM_DISABLE: checksum insertion disabled
- \arg ENET_CHECKSUM_IPV4HEADER: only IP header checksum calculation and insertion are enabled
- \arg ENET_CHECKSUM_TCPUDPICMP_SEGMENT: TCP/UDP/ICMP checksum insertion calculated but pseudo-header
- \arg ENET_CHECKSUM_TCPUDPICMP_FULL: TCP/UDP/ICMP checksum insertion fully calculated
- \param[out] none
- \retval ErrStatus: ERROR, SUCCESS
- */
- ErrStatus enet_transmit_checksum_config(enet_descriptors_struct *desc, uint32_t checksum)
- {
- if(NULL != desc){
- desc->status &= ~ENET_TDES0_CM;
- desc->status |= checksum;
- return SUCCESS;
- }else{
- return ERROR;
- }
- }
- /*!
- \brief ENET Tx and Rx function enable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_enable(void)
- {
- enet_tx_enable();
- enet_rx_enable();
- }
- /*!
- \brief ENET Tx and Rx function disable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_disable(void)
- {
- enet_tx_disable();
- enet_rx_disable();
- }
- /*!
- \brief configure MAC address
- \param[in] mac_addr: select which MAC address will be set,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_ADDRESS0: set MAC address 0 filter
- \arg ENET_MAC_ADDRESS1: set MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: set MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: set MAC address 3 filter
- \param[in] paddr: the buffer pointer which stores the MAC address
- (little-ending store, such as MAC address is aa:bb:cc:dd:ee:22, the buffer is {22, ee, dd, cc, bb, aa})
- \param[out] none
- \retval none
- */
- void enet_mac_address_set(enet_macaddress_enum mac_addr, uint8_t paddr[])
- {
- REG32(ENET_ADDRH_BASE + (uint32_t)mac_addr) = ENET_SET_MACADDRH(paddr);
- REG32(ENET_ADDRL_BASE + (uint32_t)mac_addr) = ENET_SET_MACADDRL(paddr);
- }
- /*!
- \brief get MAC address
- \param[in] mac_addr: select which MAC address will be get,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_ADDRESS0: get MAC address 0 filter
- \arg ENET_MAC_ADDRESS1: get MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: get MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: get MAC address 3 filter
- \param[out] paddr: the buffer pointer which is stored the MAC address
- (little-ending store, such as mac address is aa:bb:cc:dd:ee:22, the buffer is {22, ee, dd, cc, bb, aa})
- \param[in] bufsize: refer to the size of the buffer which stores the MAC address
- \arg 6 - 255
- \retval ErrStatus: ERROR, SUCCESS
- */
- ErrStatus enet_mac_address_get(enet_macaddress_enum mac_addr, uint8_t paddr[], uint8_t bufsize)
- {
- if(bufsize < 6U){
- return ERROR;
- }
- paddr[0] = ENET_GET_MACADDR(mac_addr, 0U);
- paddr[1] = ENET_GET_MACADDR(mac_addr, 1U);
- paddr[2] = ENET_GET_MACADDR(mac_addr, 2U);
- paddr[3] = ENET_GET_MACADDR(mac_addr, 3U);
- paddr[4] = ENET_GET_MACADDR(mac_addr, 4U);
- paddr[5] = ENET_GET_MACADDR(mac_addr, 5U);
- return SUCCESS;
- }
- /*!
- \brief get the ENET MAC/MSC/PTP/DMA status flag
- \param[in] enet_flag: ENET status flag, refer to enet_flag_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_FLAG_MPKR: magic packet received flag
- \arg ENET_MAC_FLAG_WUFR: wakeup frame received flag
- \arg ENET_MAC_FLAG_FLOWCONTROL: flow control status flag
- \arg ENET_MAC_FLAG_WUM: WUM status flag
- \arg ENET_MAC_FLAG_MSC: MSC status flag
- \arg ENET_MAC_FLAG_MSCR: MSC receive status flag
- \arg ENET_MAC_FLAG_MSCT: MSC transmit status flag
- \arg ENET_MAC_FLAG_TMST: time stamp trigger status flag
- \arg ENET_PTP_FLAG_TSSCO: timestamp second counter overflow flag
- \arg ENET_PTP_FLAG_TTM: target time match flag
- \arg ENET_MSC_FLAG_RFCE: received frames CRC error flag
- \arg ENET_MSC_FLAG_RFAE: received frames alignment error flag
- \arg ENET_MSC_FLAG_RGUF: received good unicast frames flag
- \arg ENET_MSC_FLAG_TGFSC: transmitted good frames single collision flag
- \arg ENET_MSC_FLAG_TGFMSC: transmitted good frames more single collision flag
- \arg ENET_MSC_FLAG_TGF: transmitted good frames flag
- \arg ENET_DMA_FLAG_TS: transmit status flag
- \arg ENET_DMA_FLAG_TPS: transmit process stopped status flag
- \arg ENET_DMA_FLAG_TBU: transmit buffer unavailable status flag
- \arg ENET_DMA_FLAG_TJT: transmit jabber timeout status flag
- \arg ENET_DMA_FLAG_RO: receive overflow status flag
- \arg ENET_DMA_FLAG_TU: transmit underflow status flag
- \arg ENET_DMA_FLAG_RS: receive status flag
- \arg ENET_DMA_FLAG_RBU: receive buffer unavailable status flag
- \arg ENET_DMA_FLAG_RPS: receive process stopped status flag
- \arg ENET_DMA_FLAG_RWT: receive watchdog timeout status flag
- \arg ENET_DMA_FLAG_ET: early transmit status flag
- \arg ENET_DMA_FLAG_FBE: fatal bus error status flag
- \arg ENET_DMA_FLAG_ER: early receive status flag
- \arg ENET_DMA_FLAG_AI: abnormal interrupt summary flag
- \arg ENET_DMA_FLAG_NI: normal interrupt summary flag
- \arg ENET_DMA_FLAG_EB_DMA_ERROR: DMA error flag
- \arg ENET_DMA_FLAG_EB_TRANSFER_ERROR: transfer error flag
- \arg ENET_DMA_FLAG_EB_ACCESS_ERROR: access error flag
- \arg ENET_DMA_FLAG_MSC: MSC status flag
- \arg ENET_DMA_FLAG_WUM: WUM status flag
- \arg ENET_DMA_FLAG_TST: timestamp trigger status flag
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus enet_flag_get(enet_flag_enum enet_flag)
- {
- if(RESET != (ENET_REG_VAL(enet_flag) & BIT(ENET_BIT_POS(enet_flag)))){
- return SET;
- }else{
- return RESET;
- }
- }
- /*!
- \brief clear the ENET DMA status flag
- \param[in] enet_flag: ENET DMA flag clear, refer to enet_flag_clear_enum
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_FLAG_TS_CLR: transmit status flag clear
- \arg ENET_DMA_FLAG_TPS_CLR: transmit process stopped status flag clear
- \arg ENET_DMA_FLAG_TBU_CLR: transmit buffer unavailable status flag clear
- \arg ENET_DMA_FLAG_TJT_CLR: transmit jabber timeout status flag clear
- \arg ENET_DMA_FLAG_RO_CLR: receive overflow status flag clear
- \arg ENET_DMA_FLAG_TU_CLR: transmit underflow status flag clear
- \arg ENET_DMA_FLAG_RS_CLR: receive status flag clear
- \arg ENET_DMA_FLAG_RBU_CLR: receive buffer unavailable status flag clear
- \arg ENET_DMA_FLAG_RPS_CLR: receive process stopped status flag clear
- \arg ENET_DMA_FLAG_RWT_CLR: receive watchdog timeout status flag clear
- \arg ENET_DMA_FLAG_ET_CLR: early transmit status flag clear
- \arg ENET_DMA_FLAG_FBE_CLR: fatal bus error status flag clear
- \arg ENET_DMA_FLAG_ER_CLR: early receive status flag clear
- \arg ENET_DMA_FLAG_AI_CLR: abnormal interrupt summary flag clear
- \arg ENET_DMA_FLAG_NI_CLR: normal interrupt summary flag clear
- \param[out] none
- \retval none
- */
- void enet_flag_clear(enet_flag_clear_enum enet_flag)
- {
- /* write 1 to the corresponding bit in ENET_DMA_STAT, to clear it */
- ENET_REG_VAL(enet_flag) = BIT(ENET_BIT_POS(enet_flag));
- }
- /*!
- \brief enable ENET MAC/MSC/DMA interrupt
- \param[in] enet_int: ENET interrupt,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_INT_WUMIM: WUM interrupt mask
- \arg ENET_MAC_INT_TMSTIM: timestamp trigger interrupt mask
- \arg ENET_MSC_INT_RFCEIM: received frame CRC error interrupt mask
- \arg ENET_MSC_INT_RFAEIM: received frames alignment error interrupt mask
- \arg ENET_MSC_INT_RGUFIM: received good unicast frames interrupt mask
- \arg ENET_MSC_INT_TGFSCIM: transmitted good frames single collision interrupt mask
- \arg ENET_MSC_INT_TGFMSCIM: transmitted good frames more single collision interrupt mask
- \arg ENET_MSC_INT_TGFIM: transmitted good frames interrupt mask
- \arg ENET_DMA_INT_TIE: transmit interrupt enable
- \arg ENET_DMA_INT_TPSIE: transmit process stopped interrupt enable
- \arg ENET_DMA_INT_TBUIE: transmit buffer unavailable interrupt enable
- \arg ENET_DMA_INT_TJTIE: transmit jabber timeout interrupt enable
- \arg ENET_DMA_INT_ROIE: receive overflow interrupt enable
- \arg ENET_DMA_INT_TUIE: transmit underflow interrupt enable
- \arg ENET_DMA_INT_RIE: receive interrupt enable
- \arg ENET_DMA_INT_RBUIE: receive buffer unavailable interrupt enable
- \arg ENET_DMA_INT_RPSIE: receive process stopped interrupt enable
- \arg ENET_DMA_INT_RWTIE: receive watchdog timeout interrupt enable
- \arg ENET_DMA_INT_ETIE: early transmit interrupt enable
- \arg ENET_DMA_INT_FBEIE: fatal bus error interrupt enable
- \arg ENET_DMA_INT_ERIE: early receive interrupt enable
- \arg ENET_DMA_INT_AIE: abnormal interrupt summary enable
- \arg ENET_DMA_INT_NIE: normal interrupt summary enable
- \param[out] none
- \retval none
- */
- void enet_interrupt_enable(enet_int_enum enet_int)
- {
- if(DMA_INTEN_REG_OFFSET == ((uint32_t)enet_int >> 6)){
- /* ENET_DMA_INTEN register interrupt */
- ENET_REG_VAL(enet_int) |= BIT(ENET_BIT_POS(enet_int));
- }else{
- /* other INTMSK register interrupt */
- ENET_REG_VAL(enet_int) &= ~BIT(ENET_BIT_POS(enet_int));
- }
- }
- /*!
- \brief disable ENET MAC/MSC/DMA interrupt
- \param[in] enet_int: ENET interrupt,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_INT_WUMIM: WUM interrupt mask
- \arg ENET_MAC_INT_TMSTIM: timestamp trigger interrupt mask
- \arg ENET_MSC_INT_RFCEIM: received frame CRC error interrupt mask
- \arg ENET_MSC_INT_RFAEIM: received frames alignment error interrupt mask
- \arg ENET_MSC_INT_RGUFIM: received good unicast frames interrupt mask
- \arg ENET_MSC_INT_TGFSCIM: transmitted good frames single collision interrupt mask
- \arg ENET_MSC_INT_TGFMSCIM: transmitted good frames more single collision interrupt mask
- \arg ENET_MSC_INT_TGFIM: transmitted good frames interrupt mask
- \arg ENET_DMA_INT_TIE: transmit interrupt enable
- \arg ENET_DMA_INT_TPSIE: transmit process stopped interrupt enable
- \arg ENET_DMA_INT_TBUIE: transmit buffer unavailable interrupt enable
- \arg ENET_DMA_INT_TJTIE: transmit jabber timeout interrupt enable
- \arg ENET_DMA_INT_ROIE: receive overflow interrupt enable
- \arg ENET_DMA_INT_TUIE: transmit underflow interrupt enable
- \arg ENET_DMA_INT_RIE: receive interrupt enable
- \arg ENET_DMA_INT_RBUIE: receive buffer unavailable interrupt enable
- \arg ENET_DMA_INT_RPSIE: receive process stopped interrupt enable
- \arg ENET_DMA_INT_RWTIE: receive watchdog timeout interrupt enable
- \arg ENET_DMA_INT_ETIE: early transmit interrupt enable
- \arg ENET_DMA_INT_FBEIE: fatal bus error interrupt enable
- \arg ENET_DMA_INT_ERIE: early receive interrupt enable
- \arg ENET_DMA_INT_AIE: abnormal interrupt summary enable
- \arg ENET_DMA_INT_NIE: normal interrupt summary enable
- \param[out] none
- \retval none
- */
- void enet_interrupt_disable(enet_int_enum enet_int)
- {
- if(DMA_INTEN_REG_OFFSET == ((uint32_t)enet_int >> 6)){
- /* ENET_DMA_INTEN register interrupt */
- ENET_REG_VAL(enet_int) &= ~BIT(ENET_BIT_POS(enet_int));
- }else{
- /* other INTMSK register interrupt */
- ENET_REG_VAL(enet_int) |= BIT(ENET_BIT_POS(enet_int));
- }
- }
- /*!
- \brief get ENET MAC/MSC/DMA interrupt flag
- \param[in] int_flag: ENET interrupt flag,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_INT_FLAG_WUM: WUM status flag
- \arg ENET_MAC_INT_FLAG_MSC: MSC status flag
- \arg ENET_MAC_INT_FLAG_MSCR: MSC receive status flag
- \arg ENET_MAC_INT_FLAG_MSCT: MSC transmit status flag
- \arg ENET_MAC_INT_FLAG_TMST: time stamp trigger status flag
- \arg ENET_MSC_INT_FLAG_RFCE: received frames CRC error flag
- \arg ENET_MSC_INT_FLAG_RFAE: received frames alignment error flag
- \arg ENET_MSC_INT_FLAG_RGUF: received good unicast frames flag
- \arg ENET_MSC_INT_FLAG_TGFSC: transmitted good frames single collision flag
- \arg ENET_MSC_INT_FLAG_TGFMSC: transmitted good frames more single collision flag
- \arg ENET_MSC_INT_FLAG_TGF: transmitted good frames flag
- \arg ENET_DMA_INT_FLAG_TS: transmit status flag
- \arg ENET_DMA_INT_FLAG_TPS: transmit process stopped status flag
- \arg ENET_DMA_INT_FLAG_TBU: transmit buffer unavailable status flag
- \arg ENET_DMA_INT_FLAG_TJT: transmit jabber timeout status flag
- \arg ENET_DMA_INT_FLAG_RO: receive overflow status flag
- \arg ENET_DMA_INT_FLAG_TU: transmit underflow status flag
- \arg ENET_DMA_INT_FLAG_RS: receive status flag
- \arg ENET_DMA_INT_FLAG_RBU: receive buffer unavailable status flag
- \arg ENET_DMA_INT_FLAG_RPS: receive process stopped status flag
- \arg ENET_DMA_INT_FLAG_RWT: receive watchdog timeout status flag
- \arg ENET_DMA_INT_FLAG_ET: early transmit status flag
- \arg ENET_DMA_INT_FLAG_FBE: fatal bus error status flag
- \arg ENET_DMA_INT_FLAG_ER: early receive status flag
- \arg ENET_DMA_INT_FLAG_AI: abnormal interrupt summary flag
- \arg ENET_DMA_INT_FLAG_NI: normal interrupt summary flag
- \arg ENET_DMA_INT_FLAG_MSC: MSC status flag
- \arg ENET_DMA_INT_FLAG_WUM: WUM status flag
- \arg ENET_DMA_INT_FLAG_TST: timestamp trigger status flag
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus enet_interrupt_flag_get(enet_int_flag_enum int_flag)
- {
- if(RESET != (ENET_REG_VAL(int_flag) & BIT(ENET_BIT_POS(int_flag)))){
- return SET;
- }else{
- return RESET;
- }
- }
- /*!
- \brief clear ENET DMA interrupt flag
- \param[in] int_flag_clear: clear ENET interrupt flag,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_INT_FLAG_TS_CLR: transmit status flag
- \arg ENET_DMA_INT_FLAG_TPS_CLR: transmit process stopped status flag
- \arg ENET_DMA_INT_FLAG_TBU_CLR: transmit buffer unavailable status flag
- \arg ENET_DMA_INT_FLAG_TJT_CLR: transmit jabber timeout status flag
- \arg ENET_DMA_INT_FLAG_RO_CLR: receive overflow status flag
- \arg ENET_DMA_INT_FLAG_TU_CLR: transmit underflow status flag
- \arg ENET_DMA_INT_FLAG_RS_CLR: receive status flag
- \arg ENET_DMA_INT_FLAG_RBU_CLR: receive buffer unavailable status flag
- \arg ENET_DMA_INT_FLAG_RPS_CLR: receive process stopped status flag
- \arg ENET_DMA_INT_FLAG_RWT_CLR: receive watchdog timeout status flag
- \arg ENET_DMA_INT_FLAG_ET_CLR: early transmit status flag
- \arg ENET_DMA_INT_FLAG_FBE_CLR: fatal bus error status flag
- \arg ENET_DMA_INT_FLAG_ER_CLR: early receive status flag
- \arg ENET_DMA_INT_FLAG_AI_CLR: abnormal interrupt summary flag
- \arg ENET_DMA_INT_FLAG_NI_CLR: normal interrupt summary flag
- \param[out] none
- \retval none
- */
- void enet_interrupt_flag_clear(enet_int_flag_clear_enum int_flag_clear)
- {
- /* write 1 to the corresponding bit in ENET_DMA_STAT, to clear it */
- ENET_REG_VAL(int_flag_clear) = BIT(ENET_BIT_POS(int_flag_clear));
- }
- /*!
- \brief ENET Tx function enable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_tx_enable(void)
- {
- ENET_MAC_CFG |= ENET_MAC_CFG_TEN;
- enet_txfifo_flush();
- ENET_DMA_CTL |= ENET_DMA_CTL_STE;
- }
- /*!
- \brief ENET Tx function disable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_tx_disable(void)
- {
- ENET_DMA_CTL &= ~ENET_DMA_CTL_STE;
- enet_txfifo_flush();
- ENET_MAC_CFG &= ~ENET_MAC_CFG_TEN;
- }
- /*!
- \brief ENET Rx function enable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_rx_enable(void)
- {
- ENET_MAC_CFG |= ENET_MAC_CFG_REN;
- ENET_DMA_CTL |= ENET_DMA_CTL_SRE;
- }
- /*!
- \brief ENET Rx function disable (include MAC and DMA module)
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_rx_disable(void)
- {
- ENET_DMA_CTL &= ~ENET_DMA_CTL_SRE;
- ENET_MAC_CFG &= ~ENET_MAC_CFG_REN;
- }
- /*!
- \brief put registers value into the application buffer
- \param[in] type: register type which will be get, refer to enet_registers_type_enum,
- only one parameter can be selected which is shown as below
- \arg ALL_MAC_REG: get the registers within the offset scope between ENET_MAC_CFG and ENET_MAC_FCTH
- \arg ALL_MSC_REG: get the registers within the offset scope between ENET_MSC_CTL and ENET_MSC_RGUFCNT
- \arg ALL_PTP_REG: get the registers within the offset scope between ENET_PTP_TSCTL and ENET_PTP_PPSCTL
- \arg ALL_DMA_REG: get the registers within the offset scope between ENET_DMA_BCTL and ENET_DMA_CRBADDR
- \param[in] num: the number of registers that the user want to get
- \param[out] preg: the application buffer pointer for storing the register value
- \retval none
- */
- void enet_registers_get(enet_registers_type_enum type, uint32_t *preg, uint32_t num)
- {
- uint32_t offset = 0U, max = 0U, limit = 0U;
- offset = (uint32_t)type;
- max = (uint32_t)type + num;
- limit = sizeof(enet_reg_tab)/sizeof(uint16_t);
- /* prevent element in this array is out of range */
- if(max > limit){
- max = limit;
- }
- for(; offset < max; offset++){
- /* get value of the corresponding register */
- *preg = REG32((ENET) + enet_reg_tab[offset]);
- preg++;
- }
- }
- /*!
- \brief get the enet debug status from the debug register
- \param[in] mac_debug: enet debug status,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_RECEIVER_NOT_IDLE: MAC receiver is not in idle state
- \arg ENET_RX_ASYNCHRONOUS_FIFO_STATE: Rx asynchronous FIFO status
- \arg ENET_RXFIFO_WRITING: RxFIFO is doing write operation
- \arg ENET_RXFIFO_READ_STATUS: RxFIFO read operation status
- \arg ENET_RXFIFO_STATE: RxFIFO state
- \arg ENET_MAC_TRANSMITTER_NOT_IDLE: MAC transmitter is not in idle state
- \arg ENET_MAC_TRANSMITTER_STATUS: status of MAC transmitter
- \arg ENET_PAUSE_CONDITION_STATUS: pause condition status
- \arg ENET_TXFIFO_READ_STATUS: TxFIFO read operation status
- \arg ENET_TXFIFO_WRITING: TxFIFO is doing write operation
- \arg ENET_TXFIFO_NOT_EMPTY: TxFIFO is not empty
- \arg ENET_TXFIFO_FULL: TxFIFO is full
- \param[out] none
- \retval value of the status users want to get
- */
- uint32_t enet_debug_status_get(uint32_t mac_debug)
- {
- uint32_t temp_state = 0U;
- switch(mac_debug){
- case ENET_RX_ASYNCHRONOUS_FIFO_STATE:
- temp_state = GET_MAC_DBG_RXAFS(ENET_MAC_DBG);
- break;
- case ENET_RXFIFO_READ_STATUS:
- temp_state = GET_MAC_DBG_RXFRS(ENET_MAC_DBG);
- break;
- case ENET_RXFIFO_STATE:
- temp_state = GET_MAC_DBG_RXFS(ENET_MAC_DBG);
- break;
- case ENET_MAC_TRANSMITTER_STATUS:
- temp_state = GET_MAC_DBG_SOMT(ENET_MAC_DBG);
- break;
- case ENET_TXFIFO_READ_STATUS:
- temp_state = GET_MAC_DBG_TXFRS(ENET_MAC_DBG);
- break;
- default:
- if(RESET != (ENET_MAC_DBG & mac_debug)){
- temp_state = 0x1U;
- }
- break;
- }
- return temp_state;
- }
- /*!
- \brief enable the MAC address filter
- \param[in] mac_addr: select which MAC address will be enable
- \arg ENET_MAC_ADDRESS1: enable MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: enable MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: enable MAC address 3 filter
- \param[out] none
- \retval none
- */
- void enet_address_filter_enable(enet_macaddress_enum mac_addr)
- {
- REG32(ENET_ADDRH_BASE + mac_addr) |= ENET_MAC_ADDR1H_AFE;
- }
- /*!
- \brief disable the MAC address filter
- \param[in] mac_addr: select which MAC address will be disable,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_ADDRESS1: disable MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: disable MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: disable MAC address 3 filter
- \param[out] none
- \retval none
- */
- void enet_address_filter_disable(enet_macaddress_enum mac_addr)
- {
- REG32(ENET_ADDRH_BASE + mac_addr) &= ~ENET_MAC_ADDR1H_AFE;
- }
- /*!
- \brief configure the MAC address filter
- \param[in] mac_addr: select which MAC address will be configured,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC_ADDRESS1: configure MAC address 1 filter
- \arg ENET_MAC_ADDRESS2: configure MAC address 2 filter
- \arg ENET_MAC_ADDRESS3: configure MAC address 3 filter
- \param[in] addr_mask: select which MAC address bytes will be mask,
- one or more parameters can be selected which are shown as below
- \arg ENET_ADDRESS_MASK_BYTE0: mask ENET_MAC_ADDR1L[7:0] bits
- \arg ENET_ADDRESS_MASK_BYTE1: mask ENET_MAC_ADDR1L[15:8] bits
- \arg ENET_ADDRESS_MASK_BYTE2: mask ENET_MAC_ADDR1L[23:16] bits
- \arg ENET_ADDRESS_MASK_BYTE3: mask ENET_MAC_ADDR1L [31:24] bits
- \arg ENET_ADDRESS_MASK_BYTE4: mask ENET_MAC_ADDR1H [7:0] bits
- \arg ENET_ADDRESS_MASK_BYTE5: mask ENET_MAC_ADDR1H [15:8] bits
- \param[in] filter_type: select which MAC address filter type will be selected,
- only one parameter can be selected which is shown as below
- \arg ENET_ADDRESS_FILTER_SA: The MAC address is used to compared with the SA field of the received frame
- \arg ENET_ADDRESS_FILTER_DA: The MAC address is used to compared with the DA field of the received frame
- \param[out] none
- \retval none
- */
- void enet_address_filter_config(enet_macaddress_enum mac_addr, uint32_t addr_mask, uint32_t filter_type)
- {
- uint32_t reg;
- /* get the address filter register value which is to be configured */
- reg = REG32(ENET_ADDRH_BASE + mac_addr);
- /* clear and configure the address filter register */
- reg &= ~(ENET_MAC_ADDR1H_MB | ENET_MAC_ADDR1H_SAF);
- reg |= (addr_mask | filter_type);
- REG32(ENET_ADDRH_BASE + mac_addr) = reg;
- }
- /*!
- \brief PHY interface configuration (configure SMI clock and reset PHY chip)
- \param[in] none
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_phy_config(void)
- {
- uint32_t ahbclk;
- uint32_t reg;
- uint16_t phy_value;
- ErrStatus enet_state = ERROR;
- /* clear the previous MDC clock */
- reg = ENET_MAC_PHY_CTL;
- reg &= ~ENET_MAC_PHY_CTL_CLR;
- /* get the HCLK frequency */
- ahbclk = rcu_clock_freq_get(CK_AHB);
- /* configure MDC clock according to HCLK frequency range */
- if(ENET_RANGE(ahbclk, 20000000U, 35000000U)){
- reg |= ENET_MDC_HCLK_DIV16;
- }else if(ENET_RANGE(ahbclk, 35000000U, 60000000U)){
- reg |= ENET_MDC_HCLK_DIV26;
- }else if(ENET_RANGE(ahbclk, 60000000U, 100000000U)){
- reg |= ENET_MDC_HCLK_DIV42;
- }else if((ENET_RANGE(ahbclk, 100000000U, 168000000U))||(168000000U == ahbclk)){
- reg |= ENET_MDC_HCLK_DIV62;
- }else{
- return enet_state;
- }
- ENET_MAC_PHY_CTL = reg;
- /* reset PHY */
- phy_value = PHY_RESET;
- if(ERROR == (enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &phy_value))){
- return enet_state;
- }
- /* PHY reset need some time */
- _ENET_DELAY_(ENET_DELAY_TO);
-
- /* check whether PHY reset is complete */
- if(ERROR == (enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &phy_value))){
- return enet_state;
- }
- /* PHY reset complete */
- if(RESET == (phy_value & PHY_RESET)){
- enet_state = SUCCESS;
- }
- return enet_state;
- }
- /*!
- \brief write to / read from a PHY register
- \param[in] direction: only one parameter can be selected which is shown as below
- \arg ENET_PHY_WRITE: write data to phy register
- \arg ENET_PHY_READ: read data from phy register
- \param[in] phy_address: 0x0 - 0x1F
- \param[in] phy_reg: 0x0 - 0x1F
- \param[in] pvalue: the value will be written to the PHY register in ENET_PHY_WRITE direction
- \param[out] pvalue: the value will be read from the PHY register in ENET_PHY_READ direction
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_phy_write_read(enet_phydirection_enum direction, uint16_t phy_address, uint16_t phy_reg, uint16_t *pvalue)
- {
- uint32_t reg, phy_flag;
- uint32_t timeout = 0U;
- ErrStatus enet_state = ERROR;
- /* configure ENET_MAC_PHY_CTL with write/read operation */
- reg = ENET_MAC_PHY_CTL;
- reg &= ~(ENET_MAC_PHY_CTL_PB | ENET_MAC_PHY_CTL_PW | ENET_MAC_PHY_CTL_PR | ENET_MAC_PHY_CTL_PA);
- reg |= (direction | MAC_PHY_CTL_PR(phy_reg) | MAC_PHY_CTL_PA(phy_address) | ENET_MAC_PHY_CTL_PB);
- /* if do the write operation, write value to the register */
- if(ENET_PHY_WRITE == direction){
- ENET_MAC_PHY_DATA = *pvalue;
- }
- /* do PHY write/read operation, and wait the operation complete */
- ENET_MAC_PHY_CTL = reg;
- do{
- phy_flag = (ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB);
- timeout++;
- }
- while((RESET != phy_flag) && (ENET_DELAY_TO != timeout));
- /* write/read operation complete */
- if(RESET == (ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB)){
- enet_state = SUCCESS;
- }
- /* if do the read operation, get value from the register */
- if(ENET_PHY_READ == direction){
- *pvalue = (uint16_t)ENET_MAC_PHY_DATA;
- }
- return enet_state;
- }
- /*!
- \brief enable the loopback function of PHY chip
- \param[in] none
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_phyloopback_enable(void)
- {
- uint16_t temp_phy = 0U;
- ErrStatus phy_state = ERROR;
- /* get the PHY configuration to update it */
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
- /* enable the PHY loopback mode */
- temp_phy |= PHY_LOOPBACK;
- /* update the PHY control register with the new configuration */
- phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
- return phy_state;
- }
- /*!
- \brief disable the loopback function of PHY chip
- \param[in] none
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_phyloopback_disable(void)
- {
- uint16_t temp_phy = 0U;
- ErrStatus phy_state = ERROR;
- /* get the PHY configuration to update it */
- enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
- /* disable the PHY loopback mode */
- temp_phy &= (uint16_t)~PHY_LOOPBACK;
- /* update the PHY control register with the new configuration */
- phy_state = enet_phy_write_read(ENET_PHY_WRITE, PHY_ADDRESS, PHY_REG_BCR, &temp_phy);
- return phy_state;
- }
- /*!
- \brief enable ENET forward feature
- \param[in] feature: the feature of ENET forward mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_AUTO_PADCRC_DROP: the function of the MAC strips the Pad/FCS field on received frames
- \arg ENET_TYPEFRAME_CRC_DROP: the function that FCS field(last 4 bytes) of frame will be dropped before forwarding
- \arg ENET_FORWARD_ERRFRAMES: the function that all frame received with error except runt error are forwarded to memory
- \arg ENET_FORWARD_UNDERSZ_GOODFRAMES: the function that forwarding undersized good frames
- \param[out] none
- \retval none
- */
- void enet_forward_feature_enable(uint32_t feature)
- {
- uint32_t mask;
- mask = (feature & (~(ENET_FORWARD_ERRFRAMES | ENET_FORWARD_UNDERSZ_GOODFRAMES)));
- ENET_MAC_CFG |= mask;
- mask = (feature & (~(ENET_AUTO_PADCRC_DROP | ENET_TYPEFRAME_CRC_DROP)));
- ENET_DMA_CTL |= (mask >> 2);
- }
- /*!
- \brief disable ENET forward feature
- \param[in] feature: the feature of ENET forward mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_AUTO_PADCRC_DROP: the function of the MAC strips the Pad/FCS field on received frames
- \arg ENET_TYPEFRAME_CRC_DROP: the function that FCS field(last 4 bytes) of frame will be dropped before forwarding
- \arg ENET_FORWARD_ERRFRAMES: the function that all frame received with error except runt error are forwarded to memory
- \arg ENET_FORWARD_UNDERSZ_GOODFRAMES: the function that forwarding undersized good frames
- \param[out] none
- \retval none
- */
- void enet_forward_feature_disable(uint32_t feature)
- {
- uint32_t mask;
- mask = (feature & (~(ENET_FORWARD_ERRFRAMES | ENET_FORWARD_UNDERSZ_GOODFRAMES)));
- ENET_MAC_CFG &= ~mask;
- mask = (feature & (~(ENET_AUTO_PADCRC_DROP | ENET_TYPEFRAME_CRC_DROP)));
- ENET_DMA_CTL &= ~(mask >> 2);
- }
-
- /*!
- \brief enable ENET fliter feature
- \param[in] feature: the feature of ENET fliter mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_SRC_FILTER: filter source address function
- \arg ENET_SRC_FILTER_INVERSE: inverse source address filtering result function
- \arg ENET_DEST_FILTER_INVERSE: inverse DA filtering result function
- \arg ENET_MULTICAST_FILTER_PASS: pass all multicast frames function
- \arg ENET_MULTICAST_FILTER_HASH_MODE: HASH multicast filter function
- \arg ENET_UNICAST_FILTER_HASH_MODE: HASH unicast filter function
- \arg ENET_FILTER_MODE_EITHER: HASH or perfect filter function
- \param[out] none
- \retval none
- */
- void enet_fliter_feature_enable(uint32_t feature)
- {
- ENET_MAC_FRMF |= feature;
- }
- /*!
- \brief disable ENET fliter feature
- \param[in] feature: the feature of ENET fliter mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_SRC_FILTER: filter source address function
- \arg ENET_SRC_FILTER_INVERSE: inverse source address filtering result function
- \arg ENET_DEST_FILTER_INVERSE: inverse DA filtering result function
- \arg ENET_MULTICAST_FILTER_PASS: pass all multicast frames function
- \arg ENET_MULTICAST_FILTER_HASH_MODE: HASH multicast filter function
- \arg ENET_UNICAST_FILTER_HASH_MODE: HASH unicast filter function
- \arg ENET_FILTER_MODE_EITHER: HASH or perfect filter function
- \param[out] none
- \retval none
- */
- void enet_fliter_feature_disable(uint32_t feature)
- {
- ENET_MAC_FRMF &= ~feature;
- }
- /*!
- \brief generate the pause frame, ENET will send pause frame after enable transmit flow control
- this function only use in full-dulex mode
- \param[in] none
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_pauseframe_generate(void)
- {
- ErrStatus enet_state =ERROR;
- uint32_t temp = 0U;
- /* in full-duplex mode, must make sure this bit is 0 before writing register */
- temp = ENET_MAC_FCTL & ENET_MAC_FCTL_FLCBBKPA;
- if(RESET == temp){
- ENET_MAC_FCTL |= ENET_MAC_FCTL_FLCBBKPA;
- enet_state = SUCCESS;
- }
- return enet_state;
- }
- /*!
- \brief configure the pause frame detect type
- \param[in] detect: pause frame detect type,
- only one parameter can be selected which is shown as below
- \arg ENET_MAC0_AND_UNIQUE_ADDRESS_PAUSEDETECT: besides the unique multicast address, MAC can also
- use the MAC0 address to detecting pause frame
- \arg ENET_UNIQUE_PAUSEDETECT: only the unique multicast address for pause frame which is specified
- in IEEE802.3 can be detected
- \param[out] none
- \retval none
- */
- void enet_pauseframe_detect_config(uint32_t detect)
- {
- ENET_MAC_FCTL &= ~ENET_MAC_FCTL_UPFDT;
- ENET_MAC_FCTL |= detect;
- }
- /*!
- \brief configure the pause frame parameters
- \param[in] pausetime: pause time in transmit pause control frame
- \param[in] pause_threshold: the threshold of the pause timer for retransmitting frames automatically,
- this value must make sure to be less than configured pause time, only one parameter can be
- selected which is shown as below
- \arg ENET_PAUSETIME_MINUS4: pause time minus 4 slot times
- \arg ENET_PAUSETIME_MINUS28: pause time minus 28 slot times
- \arg ENET_PAUSETIME_MINUS144: pause time minus 144 slot times
- \arg ENET_PAUSETIME_MINUS256: pause time minus 256 slot times
- \param[out] none
- \retval none
- */
- void enet_pauseframe_config(uint32_t pausetime, uint32_t pause_threshold)
- {
- ENET_MAC_FCTL &= ~(ENET_MAC_FCTL_PTM | ENET_MAC_FCTL_PLTS);
- ENET_MAC_FCTL |= (MAC_FCTL_PTM(pausetime) | pause_threshold);
- }
- /*!
- \brief configure the threshold of the flow control(deactive and active threshold)
- \param[in] deactive: the threshold of the deactive flow control, this value
- should always be less than active flow control value, only one
- parameter can be selected which is shown as below
- \arg ENET_DEACTIVE_THRESHOLD_256BYTES: threshold level is 256 bytes
- \arg ENET_DEACTIVE_THRESHOLD_512BYTES: threshold level is 512 bytes
- \arg ENET_DEACTIVE_THRESHOLD_768BYTES: threshold level is 768 bytes
- \arg ENET_DEACTIVE_THRESHOLD_1024BYTES: threshold level is 1024 bytes
- \arg ENET_DEACTIVE_THRESHOLD_1280BYTES: threshold level is 1280 bytes
- \arg ENET_DEACTIVE_THRESHOLD_1536BYTES: threshold level is 1536 bytes
- \arg ENET_DEACTIVE_THRESHOLD_1792BYTES: threshold level is 1792 bytes
- \param[in] active: the threshold of the active flow control, only one parameter
- can be selected which is shown as below
- \arg ENET_ACTIVE_THRESHOLD_256BYTES: threshold level is 256 bytes
- \arg ENET_ACTIVE_THRESHOLD_512BYTES: threshold level is 512 bytes
- \arg ENET_ACTIVE_THRESHOLD_768BYTES: threshold level is 768 bytes
- \arg ENET_ACTIVE_THRESHOLD_1024BYTES: threshold level is 1024 bytes
- \arg ENET_ACTIVE_THRESHOLD_1280BYTES: threshold level is 1280 bytes
- \arg ENET_ACTIVE_THRESHOLD_1536BYTES: threshold level is 1536 bytes
- \arg ENET_ACTIVE_THRESHOLD_1792BYTES: threshold level is 1792 bytes
- \param[out] none
- \retval none
- */
- void enet_flowcontrol_threshold_config(uint32_t deactive, uint32_t active)
- {
- ENET_MAC_FCTH = ((deactive | active) >> 8);
- }
- /*!
- \brief enable ENET flow control feature
- \param[in] feature: the feature of ENET flow control mode
- one or more parameters can be selected which are shown as below
- \arg ENET_ZERO_QUANTA_PAUSE: the automatic zero-quanta generation function
- \arg ENET_TX_FLOWCONTROL: the flow control operation in the MAC
- \arg ENET_RX_FLOWCONTROL: decoding function for the received pause frame and process it
- \arg ENET_BACK_PRESSURE: back pressure operation in the MAC(only use in half-dulex mode)
- \param[out] none
- \retval none
- */
- void enet_flowcontrol_feature_enable(uint32_t feature)
- {
- if(RESET != (feature & ENET_ZERO_QUANTA_PAUSE)){
- ENET_MAC_FCTL &= ~ENET_ZERO_QUANTA_PAUSE;
- }
- feature &= ~ENET_ZERO_QUANTA_PAUSE;
- ENET_MAC_FCTL |= feature;
- }
- /*!
- \brief disable ENET flow control feature
- \param[in] feature: the feature of ENET flow control mode
- one or more parameters can be selected which are shown as below
- \arg ENET_ZERO_QUANTA_PAUSE: the automatic zero-quanta generation function
- \arg ENET_TX_FLOWCONTROL: the flow control operation in the MAC
- \arg ENET_RX_FLOWCONTROL: decoding function for the received pause frame and process it
- \arg ENET_BACK_PRESSURE: back pressure operation in the MAC(only use in half-dulex mode)
- \param[out] none
- \retval none
- */
- void enet_flowcontrol_feature_disable(uint32_t feature)
- {
- if(RESET != (feature & ENET_ZERO_QUANTA_PAUSE)){
- ENET_MAC_FCTL |= ENET_ZERO_QUANTA_PAUSE;
- }
- feature &= ~ENET_ZERO_QUANTA_PAUSE;
- ENET_MAC_FCTL &= ~feature;
- }
- /*!
- \brief get the dma transmit/receive process state
- \param[in] direction: choose the direction of dma process which users want to check,
- refer to enet_dmadirection_enum, only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: dma transmit process
- \arg ENET_DMA_RX: dma receive process
- \param[out] none
- \retval state of dma process, the value range shows below:
- ENET_RX_STATE_STOPPED, ENET_RX_STATE_FETCHING, ENET_RX_STATE_WAITING,
- ENET_RX_STATE_SUSPENDED, ENET_RX_STATE_CLOSING, ENET_RX_STATE_QUEUING,
- ENET_TX_STATE_STOPPED, ENET_TX_STATE_FETCHING, ENET_TX_STATE_WAITING,
- ENET_TX_STATE_READING, ENET_TX_STATE_SUSPENDED, ENET_TX_STATE_CLOSING
- */
- uint32_t enet_dmaprocess_state_get(enet_dmadirection_enum direction)
- {
- uint32_t reval;
- reval = (uint32_t)(ENET_DMA_STAT & (uint32_t)direction);
- return reval;
- }
- /*!
- \brief poll the DMA transmission/reception enable by writing any value to the
- ENET_DMA_TPEN/ENET_DMA_RPEN register, this will make the DMA to resume transmission/reception
- \param[in] direction: choose the direction of DMA process which users want to resume,
- refer to enet_dmadirection_enum, only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA transmit process
- \arg ENET_DMA_RX: DMA receive process
- \param[out] none
- \retval none
- */
- void enet_dmaprocess_resume(enet_dmadirection_enum direction)
- {
- if(ENET_DMA_TX == direction){
- ENET_DMA_TPEN = 0U;
- }else{
- ENET_DMA_RPEN = 0U;
- }
- }
- /*!
- \brief check and recover the Rx process
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_rxprocess_check_recovery(void)
- {
- uint32_t status;
- /* get DAV information of current RxDMA descriptor */
- status = dma_current_rxdesc->status;
- status &= ENET_RDES0_DAV;
- /* if current descriptor is owned by DMA, but the descriptor address mismatches with
- receive descriptor address pointer updated by RxDMA controller */
- if((ENET_DMA_CRDADDR != ((uint32_t)dma_current_rxdesc)) &&
- (ENET_RDES0_DAV == status)){
- dma_current_rxdesc = (enet_descriptors_struct*)ENET_DMA_CRDADDR;
- }
- }
- /*!
- \brief flush the ENET transmit FIFO, and wait until the flush operation completes
- \param[in] none
- \param[out] none
- \retval ErrStatus: ERROR or SUCCESS
- */
- ErrStatus enet_txfifo_flush(void)
- {
- uint32_t flush_state;
- uint32_t timeout = 0U;
- ErrStatus enet_state = ERROR;
- /* set the FTF bit for flushing transmit FIFO */
- ENET_DMA_CTL |= ENET_DMA_CTL_FTF;
- /* wait until the flush operation completes */
- do{
- flush_state = ENET_DMA_CTL & ENET_DMA_CTL_FTF;
- timeout++;
- }while((RESET != flush_state) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(RESET == flush_state){
- enet_state = SUCCESS;
- }
- return enet_state;
- }
- /*!
- \brief get the transmit/receive address of current descriptor, or current buffer, or descriptor table
- \param[in] addr_get: choose the address which users want to get, refer to enet_desc_reg_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_RX_DESC_TABLE: the start address of the receive descriptor table
- \arg ENET_RX_CURRENT_DESC: the start descriptor address of the current receive descriptor read by
- the RxDMA controller
- \arg ENET_RX_CURRENT_BUFFER: the current receive buffer address being read by the RxDMA controller
- \arg ENET_TX_DESC_TABLE: the start address of the transmit descriptor table
- \arg ENET_TX_CURRENT_DESC: the start descriptor address of the current transmit descriptor read by
- the TxDMA controller
- \arg ENET_TX_CURRENT_BUFFER: the current transmit buffer address being read by the TxDMA controller
- \param[out] none
- \retval address value
- */
- uint32_t enet_current_desc_address_get(enet_desc_reg_enum addr_get)
- {
- uint32_t reval = 0U;
- reval = REG32((ENET) +(uint32_t)addr_get);
- return reval;
- }
- /*!
- \brief get the Tx or Rx descriptor information
- \param[in] desc: the descriptor pointer which users want to get information
- \param[in] info_get: the descriptor information type which is selected,
- only one parameter can be selected which is shown as below
- \arg RXDESC_BUFFER_1_SIZE: receive buffer 1 size
- \arg RXDESC_BUFFER_2_SIZE: receive buffer 2 size
- \arg RXDESC_FRAME_LENGTH: the byte length of the received frame that was transferred to the buffer
- \arg TXDESC_COLLISION_COUNT: the number of collisions occurred before the frame was transmitted
- \arg RXDESC_BUFFER_1_ADDR: the buffer1 address of the Rx frame
- \arg TXDESC_BUFFER_1_ADDR: the buffer1 address of the Tx frame
- \param[out] none
- \retval descriptor information, if value is 0xFFFFFFFFU, means the false input parameter
- */
- uint32_t enet_desc_information_get(enet_descriptors_struct *desc, enet_descstate_enum info_get)
- {
- uint32_t reval = 0xFFFFFFFFU;
- switch(info_get){
- case RXDESC_BUFFER_1_SIZE:
- reval = GET_RDES1_RB1S(desc->control_buffer_size);
- break;
- case RXDESC_BUFFER_2_SIZE:
- reval = GET_RDES1_RB2S(desc->control_buffer_size);
- break;
- case RXDESC_FRAME_LENGTH:
- reval = GET_RDES0_FRML(desc->status);
- if(reval > 4U){
- reval = reval - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (desc->status & ENET_RDES0_FRMT))){
- reval = reval + 4U;
- }
- }else{
- reval = 0U;
- }
- break;
- case RXDESC_BUFFER_1_ADDR:
- reval = desc->buffer1_addr;
- break;
- case TXDESC_BUFFER_1_ADDR:
- reval = desc->buffer1_addr;
- break;
- case TXDESC_COLLISION_COUNT:
- reval = GET_TDES0_COCNT(desc->status);
- break;
- default:
- break;
- }
- return reval;
- }
- /*!
- \brief get the number of missed frames during receiving
- \param[in] none
- \param[out] rxfifo_drop: pointer to the number of frames dropped by RxFIFO
- \param[out] rxdma_drop: pointer to the number of frames missed by the RxDMA controller
- \retval none
- */
- void enet_missed_frame_counter_get(uint32_t *rxfifo_drop, uint32_t *rxdma_drop)
- {
- uint32_t temp_counter = 0U;
- temp_counter = ENET_DMA_MFBOCNT;
- *rxfifo_drop = GET_DMA_MFBOCNT_MSFA(temp_counter);
- *rxdma_drop = GET_DMA_MFBOCNT_MSFC(temp_counter);
- }
- /*!
- \brief get the bit flag of ENET DMA descriptor
- \param[in] desc: the descriptor pointer which users want to get flag
- \param[in] desc_flag: the bit flag of ENET DMA descriptor,
- only one parameter can be selected which is shown as below
- \arg ENET_TDES0_DB: deferred
- \arg ENET_TDES0_UFE: underflow error
- \arg ENET_TDES0_EXD: excessive deferral
- \arg ENET_TDES0_VFRM: VLAN frame
- \arg ENET_TDES0_ECO: excessive collision
- \arg ENET_TDES0_LCO: late collision
- \arg ENET_TDES0_NCA: no carrier
- \arg ENET_TDES0_LCA: loss of carrier
- \arg ENET_TDES0_IPPE: IP payload error
- \arg ENET_TDES0_FRMF: frame flushed
- \arg ENET_TDES0_JT: jabber timeout
- \arg ENET_TDES0_ES: error summary
- \arg ENET_TDES0_IPHE: IP header error
- \arg ENET_TDES0_TTMSS: transmit timestamp status
- \arg ENET_TDES0_TCHM: the second address chained mode
- \arg ENET_TDES0_TERM: transmit end of ring mode
- \arg ENET_TDES0_TTSEN: transmit timestamp function enable
- \arg ENET_TDES0_DPAD: disable adding pad
- \arg ENET_TDES0_DCRC: disable CRC
- \arg ENET_TDES0_FSG: first segment
- \arg ENET_TDES0_LSG: last segment
- \arg ENET_TDES0_INTC: interrupt on completion
- \arg ENET_TDES0_DAV: DAV bit
-
- \arg ENET_RDES0_PCERR: payload checksum error
- \arg ENET_RDES0_EXSV: extended status valid
- \arg ENET_RDES0_CERR: CRC error
- \arg ENET_RDES0_DBERR: dribble bit error
- \arg ENET_RDES0_RERR: receive error
- \arg ENET_RDES0_RWDT: receive watchdog timeout
- \arg ENET_RDES0_FRMT: frame type
- \arg ENET_RDES0_LCO: late collision
- \arg ENET_RDES0_IPHERR: IP frame header error
- \arg ENET_RDES0_TSV: timestamp valid
- \arg ENET_RDES0_LDES: last descriptor
- \arg ENET_RDES0_FDES: first descriptor
- \arg ENET_RDES0_VTAG: VLAN tag
- \arg ENET_RDES0_OERR: overflow error
- \arg ENET_RDES0_LERR: length error
- \arg ENET_RDES0_SAFF: SA filter fail
- \arg ENET_RDES0_DERR: descriptor error
- \arg ENET_RDES0_ERRS: error summary
- \arg ENET_RDES0_DAFF: destination address filter fail
- \arg ENET_RDES0_DAV: descriptor available
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus enet_desc_flag_get(enet_descriptors_struct *desc, uint32_t desc_flag)
- {
- FlagStatus enet_flag = RESET;
-
- if((uint32_t)RESET != (desc->status & desc_flag)){
- enet_flag = SET;
- }
- return enet_flag;
- }
- /*!
- \brief set the bit flag of ENET DMA descriptor
- \param[in] desc: the descriptor pointer which users want to set flag
- \param[in] desc_flag: the bit flag of ENET DMA descriptor,
- only one parameter can be selected which is shown as below
- \arg ENET_TDES0_VFRM: VLAN frame
- \arg ENET_TDES0_FRMF: frame flushed
- \arg ENET_TDES0_TCHM: the second address chained mode
- \arg ENET_TDES0_TERM: transmit end of ring mode
- \arg ENET_TDES0_TTSEN: transmit timestamp function enable
- \arg ENET_TDES0_DPAD: disable adding pad
- \arg ENET_TDES0_DCRC: disable CRC
- \arg ENET_TDES0_FSG: first segment
- \arg ENET_TDES0_LSG: last segment
- \arg ENET_TDES0_INTC: interrupt on completion
- \arg ENET_TDES0_DAV: DAV bit
- \arg ENET_RDES0_DAV: descriptor available
- \param[out] none
- \retval none
- */
- void enet_desc_flag_set(enet_descriptors_struct *desc, uint32_t desc_flag)
- {
- desc->status |= desc_flag;
- }
- /*!
- \brief clear the bit flag of ENET DMA descriptor
- \param[in] desc: the descriptor pointer which users want to clear flag
- \param[in] desc_flag: the bit flag of ENET DMA descriptor,
- only one parameter can be selected which is shown as below
- \arg ENET_TDES0_VFRM: VLAN frame
- \arg ENET_TDES0_FRMF: frame flushed
- \arg ENET_TDES0_TCHM: the second address chained mode
- \arg ENET_TDES0_TERM: transmit end of ring mode
- \arg ENET_TDES0_TTSEN: transmit timestamp function enable
- \arg ENET_TDES0_DPAD: disable adding pad
- \arg ENET_TDES0_DCRC: disable CRC
- \arg ENET_TDES0_FSG: first segment
- \arg ENET_TDES0_LSG: last segment
- \arg ENET_TDES0_INTC: interrupt on completion
- \arg ENET_TDES0_DAV: DAV bit
- \arg ENET_RDES0_DAV: descriptor available
- \param[out] none
- \retval none
- */
- void enet_desc_flag_clear(enet_descriptors_struct *desc, uint32_t desc_flag)
- {
- desc->status &= ~desc_flag;
- }
- /*!
- \brief when receiving completed, set RS bit in ENET_DMA_STAT register will immediately set
- \param[in] desc: the descriptor pointer which users want to configure
- \param[out] none
- \retval none
- */
- void enet_rx_desc_immediate_receive_complete_interrupt(enet_descriptors_struct *desc)
- {
- desc->control_buffer_size &= ~ENET_RDES1_DINTC;
- }
- /*!
- \brief when receiving completed, set RS bit in ENET_DMA_STAT register will is set after a configurable delay time
- \param[in] desc: the descriptor pointer which users want to configure
- \param[in] delay_time: delay a time of 256*delay_time HCLK, this value must be between 0 and 0xFF
- \param[out] none
- \retval none
- */
- void enet_rx_desc_delay_receive_complete_interrupt(enet_descriptors_struct *desc, uint32_t delay_time)
- {
- desc->control_buffer_size |= ENET_RDES1_DINTC;
- ENET_DMA_RSWDC = DMA_RSWDC_WDCFRS(delay_time);
- }
- /*!
- \brief drop current receive frame
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_rxframe_drop(void)
- {
- /* enable reception, descriptor is owned by DMA */
- dma_current_rxdesc->status = ENET_RDES0_DAV;
- /* chained mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
- if(NULL != dma_current_ptp_rxdesc){
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->buffer2_next_desc_addr);
- /* if it is the last ptp descriptor */
- if(0U != dma_current_ptp_rxdesc->status){
- /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
- dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
- }else{
- /* ponter to the next ptp descriptor */
- dma_current_ptp_rxdesc++;
- }
- }else{
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
- }
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
- if(NULL != dma_current_ptp_rxdesc){
- dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
- }
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- if(NULL != dma_current_ptp_rxdesc){
- dma_current_ptp_rxdesc++;
- }
- }
- }
- }
- /*!
- \brief enable DMA feature
- \param[in] feature: the feature of DMA mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_NO_FLUSH_RXFRAME: RxDMA does not flushes frames function
- \arg ENET_SECONDFRAME_OPT: TxDMA controller operate on second frame function
- \param[out] none
- \retval none
- */
- void enet_dma_feature_enable(uint32_t feature)
- {
- ENET_DMA_CTL |= feature;
- }
- /*!
- \brief disable DMA feature
- \param[in] feature: the feature of DMA mode,
- one or more parameters can be selected which are shown as below
- \arg ENET_NO_FLUSH_RXFRAME: RxDMA does not flushes frames function
- \arg ENET_SECONDFRAME_OPT: TxDMA controller operate on second frame function
- \param[out] none
- \retval none
- */
- void enet_dma_feature_disable(uint32_t feature)
- {
- ENET_DMA_CTL &= ~feature;
- }
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- /*!
- \brief get the bit of extended status flag in ENET DMA descriptor
- \param[in] desc: the descriptor pointer which users want to get the extended status flag
- \param[in] desc_status: the extended status want to get,
- only one parameter can be selected which is shown as below
- \arg ENET_RDES4_IPPLDT: IP frame payload type
- \arg ENET_RDES4_IPHERR: IP frame header error
- \arg ENET_RDES4_IPPLDERR: IP frame payload error
- \arg ENET_RDES4_IPCKSB: IP frame checksum bypassed
- \arg ENET_RDES4_IPF4: IP frame in version 4
- \arg ENET_RDES4_IPF6: IP frame in version 6
- \arg ENET_RDES4_PTPMT: PTP message type
- \arg ENET_RDES4_PTPOEF: PTP on ethernet frame
- \arg ENET_RDES4_PTPVF: PTP version format
- \param[out] none
- \retval value of extended status
- */
- uint32_t enet_rx_desc_enhanced_status_get(enet_descriptors_struct *desc, uint32_t desc_status)
- {
- uint32_t reval = 0xFFFFFFFFU;
-
- switch (desc_status){
- case ENET_RDES4_IPPLDT:
- reval = GET_RDES4_IPPLDT(desc->extended_status);
- break;
- case ENET_RDES4_PTPMT:
- reval = GET_RDES4_PTPMT(desc->extended_status);
- break;
- default:
- if ((uint32_t)RESET != (desc->extended_status & desc_status)){
- reval = 1U;
- }else{
- reval = 0U;
- }
- }
-
- return reval;
- }
- /*!
- \brief configure descriptor to work in enhanced mode
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_desc_select_enhanced_mode(void)
- {
- ENET_DMA_BCTL |= ENET_DMA_BCTL_DFM;
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in enhanced chain mode with ptp function
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[out] none
- \retval none
- */
- void enet_ptp_enhanced_descriptors_chain_init(enet_dmadirection_enum direction)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc, *desc_tab;
- uint8_t *buf;
-
- /* if want to initialize DMA Tx descriptors */
- if (ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
-
- /* select chain mode, and enable transmit timestamp function */
- desc_status = ENET_TDES0_TCHM | ENET_TDES0_TTSEN;
-
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
-
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* select receive chained mode and set buffer1 size */
- desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
-
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- }
-
- /* configuration each descriptor */
- for(num = 0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
-
- /* if is not the last descriptor */
- if(num < (count - 1U)){
- /* configure the next descriptor address */
- desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
- }else{
- /* when it is the last descriptor, the next descriptor address
- equals to first descriptor address in descriptor table */
- desc->buffer2_next_desc_addr = (uint32_t)desc_tab;
- }
- }
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in enhanced ring mode with ptp function
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[out] none
- \retval none
- */
- void enet_ptp_enhanced_descriptors_ring_init(enet_dmadirection_enum direction)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc;
- enet_descriptors_struct *desc_tab;
- uint8_t *buf;
-
- /* configure descriptor skip length */
- ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
- ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
-
- /* if want to initialize DMA Tx descriptors */
- if (ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* select ring mode, and enable transmit timestamp function */
- desc_status = ENET_TDES0_TTSEN;
-
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
-
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* set buffer1 size */
- desc_bufsize = ENET_RXBUF_SIZE;
-
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- }
-
- /* configure each descriptor */
- for(num=0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
-
- /* when it is the last descriptor */
- if(num == (count - 1U)){
- if (ENET_DMA_TX == direction){
- /* configure transmit end of ring mode */
- desc->status |= ENET_TDES0_TERM;
- }else{
- /* configure receive end of ring mode */
- desc->control_buffer_size |= ENET_RDES1_RERM;
- }
- }
- }
- }
- /*!
- \brief receive a packet data with timestamp values to application buffer, when the DMA is in enhanced mode
- \param[in] bufsize: the size of buffer which is the parameter in function
- \param[out] buffer: pointer to the application buffer
- note -- if the input is NULL, user should copy data in application by himself
- \param[out] timestamp: pointer to the table which stores the timestamp high and low
- note -- if the input is NULL, timestamp is ignored
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptpframe_receive_enhanced_mode(uint8_t *buffer, uint32_t bufsize, uint32_t timestamp[])
- {
- uint32_t offset = 0U, size = 0U;
- uint32_t timeout = 0U;
- uint32_t rdes0_tsv_flag;
-
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
- return ERROR;
- }
-
- /* if buffer pointer is null, indicates that users has copied data in application */
- if(NULL != buffer){
- /* if no error occurs, and the frame uses only one descriptor */
- if(((uint32_t)RESET == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
- ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
- ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
- /* get the frame length except CRC */
- size = GET_RDES0_FRML(dma_current_rxdesc->status) - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
- size = size + 4U;
- }
-
- /* to avoid situation that the frame size exceeds the buffer length */
- if(size > bufsize){
- return ERROR;
- }
- /* copy data from Rx buffer to application buffer */
- for(offset = 0; offset < size; offset++){
- (*(buffer + offset)) = (*(__IO uint8_t *)((dma_current_rxdesc->buffer1_addr) + offset));
- }
- }else{
- return ERROR;
- }
- }
-
- /* if timestamp pointer is null, indicates that users don't care timestamp in application */
- if(NULL != timestamp){
- /* wait for ENET_RDES0_TSV flag to be set, the timestamp value is taken and
- write to the RDES6 and RDES7 */
- do{
- rdes0_tsv_flag = (dma_current_rxdesc->status & ENET_RDES0_TSV);
- timeout++;
- }while ((RESET == rdes0_tsv_flag) && (timeout < ENET_DELAY_TO));
-
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- return ERROR;
- }
-
- /* clear the ENET_RDES0_TSV flag */
- dma_current_rxdesc->status &= ~ENET_RDES0_TSV;
- /* get the timestamp value of the received frame */
- timestamp[0] = dma_current_rxdesc->timestamp_low;
- timestamp[1] = dma_current_rxdesc->timestamp_high;
- }
- /* enable reception, descriptor is owned by DMA */
- dma_current_rxdesc->status = ENET_RDES0_DAV;
-
- /* check Rx buffer unavailable flag status */
- if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
- /* Clear RBU flag */
- ENET_DMA_STAT = ENET_DMA_STAT_RBU;
- /* resume DMA reception by writing to the RPEN register*/
- ENET_DMA_RPEN = 0;
- }
-
- /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
- /* chained mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_rxdesc->buffer2_next_desc_addr);
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_rxdesc = (enet_descriptors_struct*) ((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- }
- }
-
- return SUCCESS;
- }
- /*!
- \brief send data with timestamp values in application buffer as a transmit packet, when the DMA is in enhanced mode
- \param[in] buffer: pointer on the application buffer
- note -- if the input is NULL, user should copy data in application by himself
- \param[in] length: the length of frame data to be transmitted
- \param[out] timestamp: pointer to the table which stores the timestamp high and low
- note -- if the input is NULL, timestamp is ignored
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptpframe_transmit_enhanced_mode(uint8_t *buffer, uint32_t length, uint32_t timestamp[])
- {
- uint32_t offset = 0;
- uint32_t dma_tbu_flag, dma_tu_flag;
- uint32_t tdes0_ttmss_flag;
- uint32_t timeout = 0;
-
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
- return ERROR;
- }
-
- /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
- if(length > ENET_MAX_FRAME_SIZE){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has handled data in application */
- if(NULL != buffer){
- /* copy frame data from application buffer to Tx buffer */
- for(offset = 0; offset < length; offset++){
- (*(__IO uint8_t *)((dma_current_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
- }
- }
- /* set the frame length */
- dma_current_txdesc->control_buffer_size = length;
- /* set the segment of frame, frame is transmitted in one descriptor */
- dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
- /* enable the DMA transmission */
- dma_current_txdesc->status |= ENET_TDES0_DAV;
- /* check Tx buffer unavailable flag status */
- dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
- dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
-
- if ((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
- /* Clear TBU and TU flag */
- ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
- /* resume DMA transmission by writing to the TPEN register*/
- ENET_DMA_TPEN = 0;
- }
-
- /* if timestamp pointer is null, indicates that users don't care timestamp in application */
- if(NULL != timestamp){
- /* wait for ENET_TDES0_TTMSS flag to be set, a timestamp was captured */
- do{
- tdes0_ttmss_flag = (dma_current_txdesc->status & ENET_TDES0_TTMSS);
- timeout++;
- }while((RESET == tdes0_ttmss_flag) && (timeout < ENET_DELAY_TO));
-
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- return ERROR;
- }
-
- /* clear the ENET_TDES0_TTMSS flag */
- dma_current_txdesc->status &= ~ENET_TDES0_TTMSS;
- /* get the timestamp value of the transmit frame */
- timestamp[0] = dma_current_txdesc->timestamp_low;
- timestamp[1] = dma_current_txdesc->timestamp_high;
- }
- /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table*/
- /* chained mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
- dma_current_txdesc = (enet_descriptors_struct*) (dma_current_txdesc->buffer2_next_desc_addr);
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_txdesc = (enet_descriptors_struct*) ((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- }
- }
- return SUCCESS;
- }
- #else
- /*!
- \brief configure descriptor to work in normal mode
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_desc_select_normal_mode(void)
- {
- ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DFM;
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in normal chain mode with PTP function
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[in] desc_ptptab: pointer to the first descriptor address of PTP Rx descriptor table
- \param[out] none
- \retval none
- */
- void enet_ptp_normal_descriptors_chain_init(enet_dmadirection_enum direction, enet_descriptors_struct *desc_ptptab)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc, *desc_tab;
- uint8_t *buf;
- /* if want to initialize DMA Tx descriptors */
- if(ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* select chain mode, and enable transmit timestamp function */
- desc_status = ENET_TDES0_TCHM | ENET_TDES0_TTSEN;
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- dma_current_ptp_txdesc = desc_ptptab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* select receive chained mode and set buffer1 size */
- desc_bufsize = ENET_RDES1_RCHM | (uint32_t)ENET_RXBUF_SIZE;
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- dma_current_ptp_rxdesc = desc_ptptab;
- }
- /* configure each descriptor */
- for(num = 0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
-
- /* if is not the last descriptor */
- if(num < (count - 1U)){
- /* configure the next descriptor address */
- desc->buffer2_next_desc_addr = (uint32_t)(desc_tab + num + 1U);
- }else{
- /* when it is the last descriptor, the next descriptor address
- equals to first descriptor address in descriptor table */
- desc->buffer2_next_desc_addr = (uint32_t)desc_tab;
- }
- /* set desc_ptptab equal to desc_tab */
- (&desc_ptptab[num])->buffer1_addr = desc->buffer1_addr;
- (&desc_ptptab[num])->buffer2_next_desc_addr = desc->buffer2_next_desc_addr;
- }
- /* when it is the last ptp descriptor, preserve the first descriptor
- address of desc_ptptab in ptp descriptor status */
- (&desc_ptptab[num-1U])->status = (uint32_t)desc_ptptab;
- }
- /*!
- \brief initialize the DMA Tx/Rx descriptors's parameters in normal ring mode with PTP function
- \param[in] direction: the descriptors which users want to init, refer to enet_dmadirection_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_DMA_TX: DMA Tx descriptors
- \arg ENET_DMA_RX: DMA Rx descriptors
- \param[in] desc_ptptab: pointer to the first descriptor address of PTP Rx descriptor table
- \param[out] none
- \retval none
- */
- void enet_ptp_normal_descriptors_ring_init(enet_dmadirection_enum direction, enet_descriptors_struct *desc_ptptab)
- {
- uint32_t num = 0U, count = 0U, maxsize = 0U;
- uint32_t desc_status = 0U, desc_bufsize = 0U;
- enet_descriptors_struct *desc, *desc_tab;
- uint8_t *buf;
- /* configure descriptor skip length */
- ENET_DMA_BCTL &= ~ENET_DMA_BCTL_DPSL;
- ENET_DMA_BCTL |= DMA_BCTL_DPSL(0);
- /* if want to initialize DMA Tx descriptors */
- if(ENET_DMA_TX == direction){
- /* save a copy of the DMA Tx descriptors */
- desc_tab = txdesc_tab;
- buf = &tx_buff[0][0];
- count = ENET_TXBUF_NUM;
- maxsize = ENET_TXBUF_SIZE;
- /* select ring mode, and enable transmit timestamp function */
- desc_status = ENET_TDES0_TTSEN;
- /* configure DMA Tx descriptor table address register */
- ENET_DMA_TDTADDR = (uint32_t)desc_tab;
- dma_current_txdesc = desc_tab;
- dma_current_ptp_txdesc = desc_ptptab;
- }else{
- /* if want to initialize DMA Rx descriptors */
- /* save a copy of the DMA Rx descriptors */
- desc_tab = rxdesc_tab;
- buf = &rx_buff[0][0];
- count = ENET_RXBUF_NUM;
- maxsize = ENET_RXBUF_SIZE;
- /* enable receiving */
- desc_status = ENET_RDES0_DAV;
- /* select receive ring mode and set buffer1 size */
- desc_bufsize = (uint32_t)ENET_RXBUF_SIZE;
- /* configure DMA Rx descriptor table address register */
- ENET_DMA_RDTADDR = (uint32_t)desc_tab;
- dma_current_rxdesc = desc_tab;
- dma_current_ptp_rxdesc = desc_ptptab;
- }
- /* configure each descriptor */
- for(num = 0U; num < count; num++){
- /* get the pointer to the next descriptor of the descriptor table */
- desc = desc_tab + num;
- /* configure descriptors */
- desc->status = desc_status;
- desc->control_buffer_size = desc_bufsize;
- desc->buffer1_addr = (uint32_t)(&buf[num * maxsize]);
- /* when it is the last descriptor */
- if(num == (count - 1U)){
- if (ENET_DMA_TX == direction){
- /* configure transmit end of ring mode */
- desc->status |= ENET_TDES0_TERM;
- }else{
- /* configure receive end of ring mode */
- desc->control_buffer_size |= ENET_RDES1_RERM;
- }
- }
- /* set desc_ptptab equal to desc_tab */
- (&desc_ptptab[num])->buffer1_addr = desc->buffer1_addr;
- (&desc_ptptab[num])->buffer2_next_desc_addr = desc->buffer2_next_desc_addr;
- }
- /* when it is the last ptp descriptor, preserve the first descriptor
- address of desc_ptptab in ptp descriptor status */
- (&desc_ptptab[num-1U])->status = (uint32_t)desc_ptptab;
- }
- /*!
- \brief receive a packet data with timestamp values to application buffer, when the DMA is in normal mode
- \param[in] bufsize: the size of buffer which is the parameter in function
- \param[out] timestamp: pointer to the table which stores the timestamp high and low
- \param[out] buffer: pointer to the application buffer
- note -- if the input is NULL, user should copy data in application by himself
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptpframe_receive_normal_mode(uint8_t *buffer, uint32_t bufsize, uint32_t timestamp[])
- {
- uint32_t offset = 0U, size = 0U;
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_DAV)){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has copied data in application */
- if(NULL != buffer){
- /* if no error occurs, and the frame uses only one descriptor */
- if(((uint32_t)RESET == (dma_current_rxdesc->status & ENET_RDES0_ERRS)) &&
- ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_LDES)) &&
- ((uint32_t)RESET != (dma_current_rxdesc->status & ENET_RDES0_FDES))){
- /* get the frame length except CRC */
- size = GET_RDES0_FRML(dma_current_rxdesc->status) - 4U;
- /* if is a type frame, and CRC is not included in forwarding frame */
- if((RESET != (ENET_MAC_CFG & ENET_MAC_CFG_TFCD)) && (RESET != (dma_current_rxdesc->status & ENET_RDES0_FRMT))){
- size = size + 4U;
- }
- /* to avoid situation that the frame size exceeds the buffer length */
- if(size > bufsize){
- return ERROR;
- }
- /* copy data from Rx buffer to application buffer */
- for(offset = 0U; offset < size; offset++){
- (*(buffer + offset)) = (*(__IO uint8_t *)(uint32_t)((dma_current_ptp_rxdesc->buffer1_addr) + offset));
- }
- }else{
- return ERROR;
- }
- }
- /* copy timestamp value from Rx descriptor to application array */
- timestamp[0] = dma_current_rxdesc->buffer1_addr;
- timestamp[1] = dma_current_rxdesc->buffer2_next_desc_addr;
- dma_current_rxdesc->buffer1_addr = dma_current_ptp_rxdesc ->buffer1_addr ;
- dma_current_rxdesc->buffer2_next_desc_addr = dma_current_ptp_rxdesc ->buffer2_next_desc_addr;
- /* enable reception, descriptor is owned by DMA */
- dma_current_rxdesc->status = ENET_RDES0_DAV;
-
- /* check Rx buffer unavailable flag status */
- if ((uint32_t)RESET != (ENET_DMA_STAT & ENET_DMA_STAT_RBU)){
- /* clear RBU flag */
- ENET_DMA_STAT = ENET_DMA_STAT_RBU;
- /* resume DMA reception by writing to the RPEN register*/
- ENET_DMA_RPEN = 0U;
- }
- /* update the current RxDMA descriptor pointer to the next decriptor in RxDMA decriptor table */
- /* chained mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RCHM)){
- dma_current_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->buffer2_next_desc_addr);
- /* if it is the last ptp descriptor */
- if(0U != dma_current_ptp_rxdesc->status){
- /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
- dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
- }else{
- /* ponter to the next ptp descriptor */
- dma_current_ptp_rxdesc++;
- }
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_rxdesc->control_buffer_size & ENET_RDES1_RERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_rxdesc = (enet_descriptors_struct*) (ENET_DMA_RDTADDR);
- /* RDES2 and RDES3 will not be covered by buffer address, so do not need to preserve a new table,
- use the same table with RxDMA descriptor */
- dma_current_ptp_rxdesc = (enet_descriptors_struct*) (dma_current_ptp_rxdesc->status);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_rxdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_rxdesc + ETH_DMARXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- dma_current_ptp_rxdesc ++;
- }
- }
- return SUCCESS;
- }
- /*!
- \brief send data with timestamp values in application buffer as a transmit packet, when the DMA is in normal mode
- \param[in] buffer: pointer on the application buffer
- note -- if the input is NULL, user should copy data in application by himself
- \param[in] length: the length of frame data to be transmitted
- \param[out] timestamp: pointer to the table which stores the timestamp high and low
- note -- if the input is NULL, timestamp is ignored
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptpframe_transmit_normal_mode(uint8_t *buffer, uint32_t length, uint32_t timestamp[])
- {
- uint32_t offset = 0U, timeout = 0U;
- uint32_t dma_tbu_flag, dma_tu_flag, tdes0_ttmss_flag;
- /* the descriptor is busy due to own by the DMA */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_DAV)){
- return ERROR;
- }
- /* only frame length no more than ENET_MAX_FRAME_SIZE is allowed */
- if(length > ENET_MAX_FRAME_SIZE){
- return ERROR;
- }
- /* if buffer pointer is null, indicates that users has handled data in application */
- if(NULL != buffer){
- /* copy frame data from application buffer to Tx buffer */
- for(offset = 0U; offset < length; offset++){
- (*(__IO uint8_t *) (uint32_t)((dma_current_ptp_txdesc->buffer1_addr) + offset)) = (*(buffer + offset));
- }
- }
- /* set the frame length */
- dma_current_txdesc->control_buffer_size = (length & (uint32_t)0x1FFF);
- /* set the segment of frame, frame is transmitted in one descriptor */
- dma_current_txdesc->status |= ENET_TDES0_LSG | ENET_TDES0_FSG;
- /* enable the DMA transmission */
- dma_current_txdesc->status |= ENET_TDES0_DAV;
- /* check Tx buffer unavailable flag status */
- dma_tbu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TBU);
- dma_tu_flag = (ENET_DMA_STAT & ENET_DMA_STAT_TU);
- if((RESET != dma_tbu_flag) || (RESET != dma_tu_flag)){
- /* clear TBU and TU flag */
- ENET_DMA_STAT = (dma_tbu_flag | dma_tu_flag);
- /* resume DMA transmission by writing to the TPEN register*/
- ENET_DMA_TPEN = 0U;
- }
- /* if timestamp pointer is null, indicates that users don't care timestamp in application */
- if(NULL != timestamp){
- /* wait for ENET_TDES0_TTMSS flag to be set, a timestamp was captured */
- do{
- tdes0_ttmss_flag = (dma_current_txdesc->status & ENET_TDES0_TTMSS);
- timeout++;
- }while((RESET == tdes0_ttmss_flag) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- return ERROR;
- }
- /* clear the ENET_TDES0_TTMSS flag */
- dma_current_txdesc->status &= ~ENET_TDES0_TTMSS;
- /* get the timestamp value of the transmit frame */
- timestamp[0] = dma_current_txdesc->buffer1_addr;
- timestamp[1] = dma_current_txdesc->buffer2_next_desc_addr;
- }
- dma_current_txdesc->buffer1_addr = dma_current_ptp_txdesc ->buffer1_addr ;
- dma_current_txdesc->buffer2_next_desc_addr = dma_current_ptp_txdesc ->buffer2_next_desc_addr;
- /* update the current TxDMA descriptor pointer to the next decriptor in TxDMA decriptor table */
- /* chained mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TCHM)){
- dma_current_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->buffer2_next_desc_addr);
- /* if it is the last ptp descriptor */
- if(0U != dma_current_ptp_txdesc->status){
- /* pointer back to the first ptp descriptor address in the desc_ptptab list address */
- dma_current_ptp_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->status);
- }else{
- /* ponter to the next ptp descriptor */
- dma_current_ptp_txdesc++;
- }
- }else{
- /* ring mode */
- if((uint32_t)RESET != (dma_current_txdesc->status & ENET_TDES0_TERM)){
- /* if is the last descriptor in table, the next descriptor is the table header */
- dma_current_txdesc = (enet_descriptors_struct*) (ENET_DMA_TDTADDR);
- /* TDES2 and TDES3 will not be covered by buffer address, so do not need to preserve a new table,
- use the same table with TxDMA descriptor */
- dma_current_ptp_txdesc = (enet_descriptors_struct*) (dma_current_ptp_txdesc->status);
- }else{
- /* the next descriptor is the current address, add the descriptor size, and descriptor skip length */
- dma_current_txdesc = (enet_descriptors_struct*) (uint32_t)((uint32_t)dma_current_txdesc + ETH_DMATXDESC_SIZE + GET_DMA_BCTL_DPSL(ENET_DMA_BCTL));
- dma_current_ptp_txdesc ++;
- }
- }
- return SUCCESS;
- }
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- /*!
- \brief wakeup frame filter register pointer reset
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_wum_filter_register_pointer_reset(void)
- {
- ENET_MAC_WUM |= ENET_MAC_WUM_WUFFRPR;
- }
- /*!
- \brief set the remote wakeup frame registers
- \param[in] pdata: pointer to buffer data which is written to remote wakeup frame registers (8 words total)
- \param[out] none
- \retval none
- */
- void enet_wum_filter_config(uint32_t pdata[])
- {
- uint32_t num = 0U;
- /* configure ENET_MAC_RWFF register */
- for(num = 0U; num < ETH_WAKEUP_REGISTER_LENGTH; num++){
- ENET_MAC_RWFF = pdata[num];
- }
- }
- /*!
- \brief enable wakeup management features
- \param[in] feature: one or more parameters can be selected which are shown as below
- \arg ENET_WUM_POWER_DOWN: power down mode
- \arg ENET_WUM_MAGIC_PACKET_FRAME: enable a wakeup event due to magic packet reception
- \arg ENET_WUM_WAKE_UP_FRAME: enable a wakeup event due to wakeup frame reception
- \arg ENET_WUM_GLOBAL_UNICAST: any received unicast frame passed filter is considered to be a wakeup frame
- \param[out] none
- \retval none
- */
- void enet_wum_feature_enable(uint32_t feature)
- {
- ENET_MAC_WUM |= feature;
- }
- /*!
- \brief disable wakeup management features
- \param[in] feature: one or more parameters can be selected which are shown as below
- \arg ENET_WUM_MAGIC_PACKET_FRAME: enable a wakeup event due to magic packet reception
- \arg ENET_WUM_WAKE_UP_FRAME: enable a wakeup event due to wakeup frame reception
- \arg ENET_WUM_GLOBAL_UNICAST: any received unicast frame passed filter is considered to be a wakeup frame
- \param[out] none
- \retval none
- */
- void enet_wum_feature_disable(uint32_t feature)
- {
- ENET_MAC_WUM &= (~feature);
- }
- /*!
- \brief reset the MAC statistics counters
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_msc_counters_reset(void)
- {
- /* reset all counters */
- ENET_MSC_CTL |= ENET_MSC_CTL_CTR;
- }
- /*!
- \brief enable the MAC statistics counter features
- \param[in] feature: one or more parameters can be selected which are shown as below
- \arg ENET_MSC_COUNTER_STOP_ROLLOVER: counter stop rollover
- \arg ENET_MSC_RESET_ON_READ: reset on read
- \arg ENET_MSC_COUNTERS_FREEZE: MSC counter freeze
- \param[out] none
- \retval none
- */
- void enet_msc_feature_enable(uint32_t feature)
- {
- ENET_MSC_CTL |= feature;
- }
- /*!
- \brief disable the MAC statistics counter features
- \param[in] feature: one or more parameters can be selected which are shown as below
- \arg ENET_MSC_COUNTER_STOP_ROLLOVER: counter stop rollover
- \arg ENET_MSC_RESET_ON_READ: reset on read
- \arg ENET_MSC_COUNTERS_FREEZE: MSC counter freeze
- \param[out] none
- \retval none
- */
- void enet_msc_feature_disable(uint32_t feature)
- {
- ENET_MSC_CTL &= (~feature);
- }
- /*!
- \brief configure MAC statistics counters preset mode
- \param[in] mode: MSC counters preset mode, refer to enet_msc_preset_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_MSC_PRESET_NONE: do not preset MSC counter
- \arg ENET_MSC_PRESET_HALF: preset all MSC counters to almost-half(0x7FFF FFF0) value
- \arg ENET_MSC_PRESET_FULL: preset all MSC counters to almost-full(0xFFFF FFF0) value
- \param[out] none
- \retval none
- */
- void enet_msc_counters_preset_config(enet_msc_preset_enum mode)
- {
- ENET_MSC_CTL &= ENET_MSC_PRESET_MASK;
- ENET_MSC_CTL |= (uint32_t)mode;
- }
- /*!
- \brief get MAC statistics counter
- \param[in] counter: MSC counters which is selected, refer to enet_msc_counter_enum,
- only one parameter can be selected which is shown as below
- \arg ENET_MSC_TX_SCCNT: MSC transmitted good frames after a single collision counter
- \arg ENET_MSC_TX_MSCCNT: MSC transmitted good frames after more than a single collision counter
- \arg ENET_MSC_TX_TGFCNT: MSC transmitted good frames counter
- \arg ENET_MSC_RX_RFCECNT: MSC received frames with CRC error counter
- \arg ENET_MSC_RX_RFAECNT: MSC received frames with alignment error counter
- \arg ENET_MSC_RX_RGUFCNT: MSC received good unicast frames counter
- \param[out] none
- \retval the MSC counter value
- */
- uint32_t enet_msc_counters_get(enet_msc_counter_enum counter)
- {
- uint32_t reval;
- reval = REG32((ENET + (uint32_t)counter));
- return reval;
- }
- /*!
- \brief change subsecond to nanosecond
- \param[in] subsecond: subsecond value
- \param[out] none
- \retval the nanosecond value
- */
- uint32_t enet_ptp_subsecond_2_nanosecond(uint32_t subsecond)
- {
- uint64_t val = subsecond * 1000000000Ull;
- val >>= 31;
- return (uint32_t)val;
- }
- /*!
- \brief change nanosecond to subsecond
- \param[in] nanosecond: nanosecond value
- \param[out] none
- \retval the subsecond value
- */
- uint32_t enet_ptp_nanosecond_2_subsecond(uint32_t nanosecond)
- {
- uint64_t val = nanosecond * 0x80000000Ull;
- val /= 1000000000U;
- return (uint32_t)val;
- }
- /*!
- \brief enable the PTP features
- \param[in] feature: the feature of ENET PTP mode
- one or more parameters can be selected which are shown as below
- \arg ENET_RXTX_TIMESTAMP: timestamp function for transmit and receive frames
- \arg ENET_PTP_TIMESTAMP_INT: timestamp interrupt trigger
- \arg ENET_ALL_RX_TIMESTAMP: all received frames are taken snapshot
- \arg ENET_NONTYPE_FRAME_SNAPSHOT: take snapshot when received non type frame
- \arg ENET_IPV6_FRAME_SNAPSHOT: take snapshot for IPv6 frame
- \arg ENET_IPV4_FRAME_SNAPSHOT: take snapshot for IPv4 frame
- \arg ENET_PTP_FRAME_USE_MACADDRESS_FILTER: use MAC address1-3 to filter the PTP frame
- \param[out] none
- \retval none
- */
- void enet_ptp_feature_enable(uint32_t feature)
- {
- ENET_PTP_TSCTL |= feature;
- }
- /*!
- \brief disable the PTP features
- \param[in] feature: the feature of ENET PTP mode
- one or more parameters can be selected which are shown as below
- \arg ENET_RXTX_TIMESTAMP: timestamp function for transmit and receive frames
- \arg ENET_PTP_TIMESTAMP_INT: timestamp interrupt trigger
- \arg ENET_ALL_RX_TIMESTAMP: all received frames are taken snapshot
- \arg ENET_NONTYPE_FRAME_SNAPSHOT: take snapshot when received non type frame
- \arg ENET_IPV6_FRAME_SNAPSHOT: take snapshot for IPv6 frame
- \arg ENET_IPV4_FRAME_SNAPSHOT: take snapshot for IPv4 frame
- \arg ENET_PTP_FRAME_USE_MACADDRESS_FILTER: use MAC address1-3 to filter the PTP frame
- \param[out] none
- \retval none
- */
- void enet_ptp_feature_disable(uint32_t feature)
- {
- ENET_PTP_TSCTL &= ~feature;
- }
- /*!
- \brief configure the PTP timestamp function
- \param[in] func: only one parameter can be selected which is shown as below
- \arg ENET_CKNT_ORDINARY: type of ordinary clock node type for timestamp
- \arg ENET_CKNT_BOUNDARY: type of boundary clock node type for timestamp
- \arg ENET_CKNT_END_TO_END: type of end-to-end transparent clock node type for timestamp
- \arg ENET_CKNT_PEER_TO_PEER: type of peer-to-peer transparent clock node type for timestamp
- \arg ENET_PTP_ADDEND_UPDATE: addend register update
- \arg ENET_PTP_SYSTIME_UPDATE: timestamp update
- \arg ENET_PTP_SYSTIME_INIT: timestamp initialize
- \arg ENET_PTP_FINEMODE: the system timestamp uses the fine method for updating
- \arg ENET_PTP_COARSEMODE: the system timestamp uses the coarse method for updating
- \arg ENET_SUBSECOND_DIGITAL_ROLLOVER: digital rollover mode
- \arg ENET_SUBSECOND_BINARY_ROLLOVER: binary rollover mode
- \arg ENET_SNOOPING_PTP_VERSION_2: version 2
- \arg ENET_SNOOPING_PTP_VERSION_1: version 1
- \arg ENET_EVENT_TYPE_MESSAGES_SNAPSHOT: only event type messages are taken snapshot
- \arg ENET_ALL_TYPE_MESSAGES_SNAPSHOT: all type messages are taken snapshot except announce,
- management and signaling message
- \arg ENET_MASTER_NODE_MESSAGE_SNAPSHOT: snapshot is only take for master node message
- \arg ENET_SLAVE_NODE_MESSAGE_SNAPSHOT: snapshot is only taken for slave node message
- \param[out] none
- \retval ErrStatus: SUCCESS or ERROR
- */
- ErrStatus enet_ptp_timestamp_function_config(enet_ptp_function_enum func)
- {
- uint32_t temp_config = 0U, temp_state = 0U;
- uint32_t timeout = 0U;
- ErrStatus enet_state = SUCCESS;
- switch(func){
- case ENET_CKNT_ORDINARY:
- case ENET_CKNT_BOUNDARY:
- case ENET_CKNT_END_TO_END:
- case ENET_CKNT_PEER_TO_PEER:
- ENET_PTP_TSCTL &= ~ENET_PTP_TSCTL_CKNT;
- ENET_PTP_TSCTL |= (uint32_t)func;
- break;
- case ENET_PTP_ADDEND_UPDATE:
- /* this bit must be read as zero before application set it */
- do{
- temp_state = ENET_PTP_TSCTL & ENET_PTP_TSCTL_TMSARU;
- timeout++;
- }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- enet_state = ERROR;
- }else{
- ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSARU;
- }
- break;
- case ENET_PTP_SYSTIME_UPDATE:
- /* both the TMSSTU and TMSSTI bits must be read as zero before application set this bit */
- do{
- temp_state = ENET_PTP_TSCTL & (ENET_PTP_TSCTL_TMSSTU | ENET_PTP_TSCTL_TMSSTI);
- timeout++;
- }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- enet_state = ERROR;
- }else{
- ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSSTU;
- }
- break;
- case ENET_PTP_SYSTIME_INIT:
- /* this bit must be read as zero before application set it */
- do{
- temp_state = ENET_PTP_TSCTL & ENET_PTP_TSCTL_TMSSTI;
- timeout++;
- }while((RESET != temp_state) && (timeout < ENET_DELAY_TO));
- /* return ERROR due to timeout */
- if(ENET_DELAY_TO == timeout){
- enet_state = ERROR;
- }else{
- ENET_PTP_TSCTL |= ENET_PTP_TSCTL_TMSSTI;
- }
- break;
- default:
- temp_config = (uint32_t)func & (~BIT(31));
- if(RESET != ((uint32_t)func & BIT(31))){
- ENET_PTP_TSCTL |= temp_config;
- }else{
- ENET_PTP_TSCTL &= ~temp_config;
- }
- break;
- }
- return enet_state;
- }
- /*!
- \brief configure system time subsecond increment value
- \param[in] subsecond: the value will be added to the subsecond value of system time,
- this value must be between 0 and 0xFF
- \param[out] none
- \retval none
- */
- void enet_ptp_subsecond_increment_config(uint32_t subsecond)
- {
- ENET_PTP_SSINC = PTP_SSINC_STMSSI(subsecond);
- }
- /*!
- \brief adjusting the clock frequency only in fine update mode
- \param[in] add: the value will be added to the accumulator register to achieve time synchronization
- \param[out] none
- \retval none
- */
- void enet_ptp_timestamp_addend_config(uint32_t add)
- {
- ENET_PTP_TSADDEND = add;
- }
- /*!
- \brief initialize or add/subtract to second of the system time
- \param[in] sign: timestamp update positive or negative sign,
- only one parameter can be selected which is shown as below
- \arg ENET_PTP_ADD_TO_TIME: timestamp update value is added to system time
- \arg ENET_PTP_SUBSTRACT_FROM_TIME: timestamp update value is subtracted from system time
- \param[in] second: initializing or adding/subtracting to second of the system time
- \param[in] subsecond: the current subsecond of the system time
- with 0.46 ns accuracy if required accuracy is 20 ns
- \param[out] none
- \retval none
- */
- void enet_ptp_timestamp_update_config(uint32_t sign, uint32_t second, uint32_t subsecond)
- {
- ENET_PTP_TSUH = second;
- ENET_PTP_TSUL = sign | PTP_TSUL_TMSUSS(subsecond);
- }
- /*!
- \brief configure the expected target time
- \param[in] second: the expected target second time
- \param[in] nanosecond: the expected target nanosecond time (signed)
- \param[out] none
- \retval none
- */
- void enet_ptp_expected_time_config(uint32_t second, uint32_t nanosecond)
- {
- ENET_PTP_ETH = second;
- ENET_PTP_ETL = nanosecond;
- }
- /*!
- \brief get the current system time
- \param[in] none
- \param[out] systime_struct: pointer to a enet_ptp_systime_struct structure which contains
- parameters of PTP system time
- members of the structure and the member values are shown as below:
- second: 0x0 - 0xFFFF FFFF
- nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
- sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
- \retval none
- */
- void enet_ptp_system_time_get(enet_ptp_systime_struct *systime_struct)
- {
- uint32_t temp_sec = 0U, temp_subs = 0U;
- /* get the value of sysytem time registers */
- temp_sec = (uint32_t)ENET_PTP_TSH;
- temp_subs = (uint32_t)ENET_PTP_TSL;
-
- /* get sysytem time and construct the enet_ptp_systime_struct structure */
- systime_struct->second = temp_sec;
- systime_struct->nanosecond = GET_PTP_TSL_STMSS(temp_subs);
- systime_struct->nanosecond = enet_ptp_subsecond_2_nanosecond(systime_struct->nanosecond);
- systime_struct->sign = GET_PTP_TSL_STS(temp_subs);
- }
- /*!
- \brief configure the PPS output frequency
- \param[in] freq: PPS output frequency,
- only one parameter can be selected which is shown as below
- \arg ENET_PPSOFC_1HZ: PPS output 1Hz frequency
- \arg ENET_PPSOFC_2HZ: PPS output 2Hz frequency
- \arg ENET_PPSOFC_4HZ: PPS output 4Hz frequency
- \arg ENET_PPSOFC_8HZ: PPS output 8Hz frequency
- \arg ENET_PPSOFC_16HZ: PPS output 16Hz frequency
- \arg ENET_PPSOFC_32HZ: PPS output 32Hz frequency
- \arg ENET_PPSOFC_64HZ: PPS output 64Hz frequency
- \arg ENET_PPSOFC_128HZ: PPS output 128Hz frequency
- \arg ENET_PPSOFC_256HZ: PPS output 256Hz frequency
- \arg ENET_PPSOFC_512HZ: PPS output 512Hz frequency
- \arg ENET_PPSOFC_1024HZ: PPS output 1024Hz frequency
- \arg ENET_PPSOFC_2048HZ: PPS output 2048Hz frequency
- \arg ENET_PPSOFC_4096HZ: PPS output 4096Hz frequency
- \arg ENET_PPSOFC_8192HZ: PPS output 8192Hz frequency
- \arg ENET_PPSOFC_16384HZ: PPS output 16384Hz frequency
- \arg ENET_PPSOFC_32768HZ: PPS output 32768Hz frequency
- \param[out] none
- \retval none
- */
- void enet_ptp_pps_output_frequency_config(uint32_t freq)
- {
- ENET_PTP_PPSCTL = freq;
- }
- /*!
- \brief configure and start PTP timestamp counter
- \param[in] updatemethod: method for updating
- \arg ENET_PTP_FINEMODE: fine correction method
- \arg ENET_PTP_COARSEMODE: coarse correction method
- \param[in] init_sec: second value for initializing system time
- \param[in] init_subsec: subsecond value for initializing system time
- \param[in] carry_cfg: the value to be added to the accumulator register (in fine method is used)
- \param[in] accuracy_cfg: the value to be added to the subsecond value of system time
- \param[out] none
- \retval none
- */
- void enet_ptp_start(int32_t updatemethod, uint32_t init_sec, uint32_t init_subsec, uint32_t carry_cfg, uint32_t accuracy_cfg)
- {
- /* mask the timestamp trigger interrupt */
- enet_interrupt_disable(ENET_MAC_INT_TMSTIM);
- /* enable timestamp */
- enet_ptp_feature_enable(ENET_ALL_RX_TIMESTAMP | ENET_RXTX_TIMESTAMP);
- /* configure system time subsecond increment based on the PTP clock frequency */
- enet_ptp_subsecond_increment_config(accuracy_cfg);
- if(ENET_PTP_FINEMODE == updatemethod){
- /* fine correction method: configure the timestamp addend, then update */
- enet_ptp_timestamp_addend_config(carry_cfg);
- enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
- /* wait until update is completed */
- while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_ADDEND_UPDATE)){
- }
- }
- /* choose the fine correction method */
- enet_ptp_timestamp_function_config((enet_ptp_function_enum)updatemethod);
- /* initialize the system time */
- enet_ptp_timestamp_update_config(ENET_PTP_ADD_TO_TIME, init_sec, init_subsec);
- enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
- #ifdef SELECT_DESCRIPTORS_ENHANCED_MODE
- enet_desc_select_enhanced_mode();
- #endif /* SELECT_DESCRIPTORS_ENHANCED_MODE */
- }
- /*!
- \brief adjust frequency in fine method by configure addend register
- \param[in] carry_cfg: the value to be added to the accumulator register
- \param[out] none
- \retval none
- */
- void enet_ptp_finecorrection_adjfreq(int32_t carry_cfg)
- {
- /* re-configure the timestamp addend, then update */
- enet_ptp_timestamp_addend_config((uint32_t)carry_cfg);
- enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
- }
- /*!
- \brief update system time in coarse method
- \param[in] systime_struct: : pointer to a enet_ptp_systime_struct structure which contains
- parameters of PTP system time
- members of the structure and the member values are shown as below:
- second: 0x0 - 0xFFFF FFFF
- nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
- sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
- \param[out] none
- \retval none
- */
- void enet_ptp_coarsecorrection_systime_update(enet_ptp_systime_struct *systime_struct)
- {
- uint32_t subsecond_val;
- uint32_t carry_cfg;
- subsecond_val = enet_ptp_nanosecond_2_subsecond(systime_struct->nanosecond);
- /* save the carry_cfg value */
- carry_cfg = ENET_PTP_TSADDEND_TMSA;
- /* update the system time */
- enet_ptp_timestamp_update_config(systime_struct->sign, systime_struct->second, subsecond_val);
- enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_UPDATE);
- /* wait until the update is completed */
- while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_SYSTIME_UPDATE)){
- }
- /* write back the carry_cfg value, then update */
- enet_ptp_timestamp_addend_config(carry_cfg);
- enet_ptp_timestamp_function_config(ENET_PTP_ADDEND_UPDATE);
- }
- /*!
- \brief set system time in fine method
- \param[in] systime_struct: : pointer to a enet_ptp_systime_struct structure which contains
- parameters of PTP system time
- members of the structure and the member values are shown as below:
- second: 0x0 - 0xFFFF FFFF
- nanosecond: 0x0 - 0x7FFF FFFF * 10^9 / 2^31
- sign: ENET_PTP_TIME_POSITIVE, ENET_PTP_TIME_NEGATIVE
- \param[out] none
- \retval none
- */
- void enet_ptp_finecorrection_settime(enet_ptp_systime_struct * systime_struct)
- {
- uint32_t subsecond_val;
- subsecond_val = enet_ptp_nanosecond_2_subsecond(systime_struct->nanosecond);
- /* initialize the system time */
- enet_ptp_timestamp_update_config(systime_struct->sign, systime_struct->second, subsecond_val);
- enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT);
-
- /* wait until the system time initialzation finished */
- while(SET == enet_ptp_flag_get((uint32_t)ENET_PTP_SYSTIME_INIT)){
- }
- }
- /*!
- \brief get the ptp flag status
- \param[in] flag: ptp flag status to be checked
- \arg ENET_PTP_ADDEND_UPDATE: addend register update
- \arg ENET_PTP_SYSTIME_UPDATE: timestamp update
- \arg ENET_PTP_SYSTIME_INIT: timestamp initialize
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus enet_ptp_flag_get(uint32_t flag)
- {
- FlagStatus bitstatus = RESET;
- if ((uint32_t)RESET != (ENET_PTP_TSCTL & flag)){
- bitstatus = SET;
- }
- return bitstatus;
- }
- /*!
- \brief reset the ENET initpara struct, call it before using enet_initpara_config()
- \param[in] none
- \param[out] none
- \retval none
- */
- void enet_initpara_reset(void)
- {
- enet_initpara.option_enable = 0U;
- enet_initpara.forward_frame = 0U;
- enet_initpara.dmabus_mode = 0U;
- enet_initpara.dma_maxburst = 0U;
- enet_initpara.dma_arbitration = 0U;
- enet_initpara.store_forward_mode = 0U;
- enet_initpara.dma_function = 0U;
- enet_initpara.vlan_config = 0U;
- enet_initpara.flow_control = 0U;
- enet_initpara.hashtable_high = 0U;
- enet_initpara.hashtable_low = 0U;
- enet_initpara.framesfilter_mode = 0U;
- enet_initpara.halfduplex_param = 0U;
- enet_initpara.timer_config = 0U;
- enet_initpara.interframegap = 0U;
- }
- /*!
- \brief initialize ENET peripheral with generally concerned parameters, call it by enet_init()
- \param[in] none
- \param[out] none
- \retval none
- */
- static void enet_default_init(void)
- {
- uint32_t reg_value = 0U;
- /* MAC */
- /* configure ENET_MAC_CFG register */
- reg_value = ENET_MAC_CFG;
- reg_value &= MAC_CFG_MASK;
- reg_value |= ENET_WATCHDOG_ENABLE | ENET_JABBER_ENABLE | ENET_INTERFRAMEGAP_96BIT \
- | ENET_SPEEDMODE_10M |ENET_MODE_HALFDUPLEX | ENET_LOOPBACKMODE_DISABLE \
- | ENET_CARRIERSENSE_ENABLE | ENET_RECEIVEOWN_ENABLE \
- | ENET_RETRYTRANSMISSION_ENABLE | ENET_BACKOFFLIMIT_10 \
- | ENET_DEFERRALCHECK_DISABLE \
- | ENET_TYPEFRAME_CRC_DROP_DISABLE \
- | ENET_AUTO_PADCRC_DROP_DISABLE \
- | ENET_CHECKSUMOFFLOAD_DISABLE;
- ENET_MAC_CFG = reg_value;
- /* configure ENET_MAC_FRMF register */
- ENET_MAC_FRMF = ENET_SRC_FILTER_DISABLE |ENET_DEST_FILTER_INVERSE_DISABLE \
- |ENET_MULTICAST_FILTER_PERFECT |ENET_UNICAST_FILTER_PERFECT \
- |ENET_PCFRM_PREVENT_ALL |ENET_BROADCASTFRAMES_ENABLE \
- |ENET_PROMISCUOUS_DISABLE |ENET_RX_FILTER_ENABLE;
- /* configure ENET_MAC_HLH, ENET_MAC_HLL register */
- ENET_MAC_HLH = 0x0U;
- ENET_MAC_HLL = 0x0U;
- /* configure ENET_MAC_FCTL, ENET_MAC_FCTH register */
- reg_value = ENET_MAC_FCTL;
- reg_value &= MAC_FCTL_MASK;
- reg_value |= MAC_FCTL_PTM(0) |ENET_ZERO_QUANTA_PAUSE_DISABLE \
- |ENET_PAUSETIME_MINUS4 |ENET_UNIQUE_PAUSEDETECT \
- |ENET_RX_FLOWCONTROL_DISABLE |ENET_TX_FLOWCONTROL_DISABLE;
- ENET_MAC_FCTL = reg_value;
- /* configure ENET_MAC_VLT register */
- ENET_MAC_VLT = ENET_VLANTAGCOMPARISON_16BIT |MAC_VLT_VLTI(0);
- /* disable MAC interrupt */
- ENET_MAC_INTMSK |= ENET_MAC_INTMSK_TMSTIM | ENET_MAC_INTMSK_WUMIM;
- /* MSC */
- /* disable MSC Rx interrupt */
- ENET_MSC_RINTMSK |= ENET_MSC_RINTMSK_RFAEIM | ENET_MSC_RINTMSK_RFCEIM \
- | ENET_MSC_RINTMSK_RGUFIM;
- /* disable MSC Tx interrupt */
- ENET_MSC_TINTMSK |= ENET_MSC_TINTMSK_TGFIM | ENET_MSC_TINTMSK_TGFMSCIM \
- | ENET_MSC_TINTMSK_TGFSCIM;
- /* DMA */
- /* configure ENET_DMA_CTL register */
- reg_value = ENET_DMA_CTL;
- reg_value &= DMA_CTL_MASK;
- reg_value |= ENET_TCPIP_CKSUMERROR_DROP |ENET_RX_MODE_STOREFORWARD \
- |ENET_FLUSH_RXFRAME_ENABLE |ENET_TX_MODE_STOREFORWARD \
- |ENET_TX_THRESHOLD_64BYTES |ENET_RX_THRESHOLD_64BYTES \
- |ENET_SECONDFRAME_OPT_DISABLE;
- ENET_DMA_CTL = reg_value;
- /* configure ENET_DMA_BCTL register */
- reg_value = ENET_DMA_BCTL;
- reg_value &= DMA_BCTL_MASK;
- reg_value = ENET_ADDRESS_ALIGN_ENABLE |ENET_ARBITRATION_RXTX_2_1 \
- |ENET_RXDP_32BEAT |ENET_PGBL_32BEAT |ENET_RXTX_DIFFERENT_PGBL \
- |ENET_FIXED_BURST_ENABLE |ENET_MIXED_BURST_DISABLE \
- |ENET_NORMAL_DESCRIPTOR;
- ENET_DMA_BCTL = reg_value;
- }
- #ifndef USE_DELAY
- /*!
- \brief insert a delay time
- \param[in] ncount: specifies the delay time length
- \param[out] none
- \param[out] none
- */
- static void enet_delay(uint32_t ncount)
- {
- __IO uint32_t delay_time = 0U;
- for(delay_time = ncount; delay_time != 0U; delay_time--){
- }
- }
- #endif /* USE_DELAY */
- #endif /* GD32F30X_CL */
|