stm32f4xx_eth.c 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_eth.c
  4. * @author MCD Application Team
  5. * @version V0.0.1
  6. * @date 10/21/2010
  7. * @brief This file provides all the ETH firmware functions for STM32F2xx devices.
  8. * This driver is based on V1.1.0 of "stm32_eth.c" driver, and updated
  9. * to support new feature added in STM32F2xx devices (Enhanced DMA descriptors)
  10. ******************************************************************************
  11. * @copy
  12. *
  13. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  14. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  15. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  16. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  17. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  18. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  19. *
  20. * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  21. */
  22. /*
  23. * Change Logs:
  24. * Date Author Notes
  25. * 2011-07-22 aozima first implementation.
  26. * 2012-09-24 aozima update for stm32f4.
  27. * 2012-09-26 aozima add phy monitor.
  28. */
  29. /* Includes ------------------------------------------------------------------*/
  30. #include "stm32f4xx_eth.h"
  31. #include "stm32f4xx_rcc.h"
  32. /* STM32F ETH dirver options */
  33. #define RMII_MODE /* MII_MODE or RMII_MODE */
  34. #define RMII_TX_GPIO_GROUP 2 /* 1:GPIOB or 2:GPIOG */
  35. //#define CHECKSUM_BY_HARDWARE /* don't ues hardware checksum. */
  36. /** @addtogroup STM32F4XX_ETH_Driver
  37. * @brief ETH driver modules
  38. * @{
  39. */
  40. /** @defgroup ETH_Private_TypesDefinitions
  41. * @{
  42. */
  43. /**
  44. * @}
  45. */
  46. /** @defgroup ETH_Private_Defines
  47. * @{
  48. */
  49. /* Global pointers on Tx and Rx descriptor used to track transmit and receive descriptors */
  50. ETH_DMADESCTypeDef *DMATxDescToSet;
  51. ETH_DMADESCTypeDef *DMARxDescToGet;
  52. ETH_DMADESCTypeDef *DMAPTPTxDescToSet;
  53. ETH_DMADESCTypeDef *DMAPTPRxDescToGet;
  54. /* ETHERNET MAC address offsets */
  55. #define ETH_MAC_ADDR_HBASE (ETH_MAC_BASE + 0x40) /* ETHERNET MAC address high offset */
  56. #define ETH_MAC_ADDR_LBASE (ETH_MAC_BASE + 0x44) /* ETHERNET MAC address low offset */
  57. /* ETHERNET MACMIIAR register Mask */
  58. #define MACMIIAR_CR_MASK ((uint32_t)0xFFFFFFE3)
  59. /* ETHERNET MACCR register Mask */
  60. #define MACCR_CLEAR_MASK ((uint32_t)0xFF20810F)
  61. /* ETHERNET MACFCR register Mask */
  62. #define MACFCR_CLEAR_MASK ((uint32_t)0x0000FF41)
  63. /* ETHERNET DMAOMR register Mask */
  64. #define DMAOMR_CLEAR_MASK ((uint32_t)0xF8DE3F23)
  65. /* ETHERNET Remote Wake-up frame register length */
  66. #define ETH_WAKEUP_REGISTER_LENGTH 8
  67. /* ETHERNET Missed frames counter Shift */
  68. #define ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTERSHIFT 17
  69. /* ETHERNET DMA Tx descriptors Collision Count Shift */
  70. #define ETH_DMATXDESC_COLLISION_COUNTSHIFT 3
  71. /* ETHERNET DMA Tx descriptors Buffer2 Size Shift */
  72. #define ETH_DMATXDESC_BUFFER2_SIZESHIFT 16
  73. /* ETHERNET DMA Rx descriptors Frame Length Shift */
  74. #define ETH_DMARXDESC_FRAME_LENGTHSHIFT 16
  75. /* ETHERNET DMA Rx descriptors Buffer2 Size Shift */
  76. #define ETH_DMARXDESC_BUFFER2_SIZESHIFT 16
  77. /* ETHERNET errors */
  78. #define ETH_ERROR ((uint32_t)0)
  79. #define ETH_SUCCESS ((uint32_t)1)
  80. /**
  81. * @}
  82. */
  83. /** @defgroup ETH_Private_Macros
  84. * @{
  85. */
  86. /**
  87. * @}
  88. */
  89. /** @defgroup ETH_Private_Variables
  90. * @{
  91. */
  92. /**
  93. * @}
  94. */
  95. /** @defgroup ETH_Private_FunctionPrototypes
  96. * @{
  97. */
  98. /**
  99. * @}
  100. */
  101. /** @defgroup ETH_Private_Functions
  102. * @{
  103. */
  104. /**
  105. * @brief Deinitializes the ETHERNET peripheral registers to their default reset values.
  106. * @param None
  107. * @retval None
  108. */
  109. void ETH_DeInit(void)
  110. {
  111. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_ETH_MAC, ENABLE);
  112. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_ETH_MAC, DISABLE);
  113. }
  114. /**
  115. * @brief Initializes the ETHERNET peripheral according to the specified
  116. * parameters in the ETH_InitStruct .
  117. * @param ETH_InitStruct: pointer to a ETH_InitTypeDef structure that contains
  118. * the configuration information for the specified ETHERNET peripheral.
  119. * @param PHYAddress: external PHY address
  120. * @retval ETH_ERROR: Ethernet initialization failed
  121. * ETH_SUCCESS: Ethernet successfully initialized
  122. */
  123. uint32_t ETH_Init(ETH_InitTypeDef* ETH_InitStruct)
  124. {
  125. uint32_t tmpreg = 0;
  126. __IO uint32_t i = 0;
  127. RCC_ClocksTypeDef rcc_clocks;
  128. uint32_t hclk = 60000000;
  129. __IO uint32_t timeout = 0;
  130. /* Check the parameters */
  131. /* MAC --------------------------*/
  132. assert_param(IS_ETH_AUTONEGOTIATION(ETH_InitStruct->ETH_AutoNegotiation));
  133. assert_param(IS_ETH_WATCHDOG(ETH_InitStruct->ETH_Watchdog));
  134. assert_param(IS_ETH_JABBER(ETH_InitStruct->ETH_Jabber));
  135. assert_param(IS_ETH_INTER_FRAME_GAP(ETH_InitStruct->ETH_InterFrameGap));
  136. assert_param(IS_ETH_CARRIER_SENSE(ETH_InitStruct->ETH_CarrierSense));
  137. assert_param(IS_ETH_SPEED(ETH_InitStruct->ETH_Speed));
  138. assert_param(IS_ETH_RECEIVE_OWN(ETH_InitStruct->ETH_ReceiveOwn));
  139. assert_param(IS_ETH_LOOPBACK_MODE(ETH_InitStruct->ETH_LoopbackMode));
  140. assert_param(IS_ETH_DUPLEX_MODE(ETH_InitStruct->ETH_Mode));
  141. assert_param(IS_ETH_CHECKSUM_OFFLOAD(ETH_InitStruct->ETH_ChecksumOffload));
  142. assert_param(IS_ETH_RETRY_TRANSMISSION(ETH_InitStruct->ETH_RetryTransmission));
  143. assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(ETH_InitStruct->ETH_AutomaticPadCRCStrip));
  144. assert_param(IS_ETH_BACKOFF_LIMIT(ETH_InitStruct->ETH_BackOffLimit));
  145. assert_param(IS_ETH_DEFERRAL_CHECK(ETH_InitStruct->ETH_DeferralCheck));
  146. assert_param(IS_ETH_RECEIVE_ALL(ETH_InitStruct->ETH_ReceiveAll));
  147. assert_param(IS_ETH_SOURCE_ADDR_FILTER(ETH_InitStruct->ETH_SourceAddrFilter));
  148. assert_param(IS_ETH_CONTROL_FRAMES(ETH_InitStruct->ETH_PassControlFrames));
  149. assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(ETH_InitStruct->ETH_BroadcastFramesReception));
  150. assert_param(IS_ETH_DESTINATION_ADDR_FILTER(ETH_InitStruct->ETH_DestinationAddrFilter));
  151. // assert_param(IS_ETH_PROMISCIOUS_MODE(ETH_InitStruct->ETH_PromiscuousMode));
  152. assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(ETH_InitStruct->ETH_MulticastFramesFilter));
  153. assert_param(IS_ETH_UNICAST_FRAMES_FILTER(ETH_InitStruct->ETH_UnicastFramesFilter));
  154. assert_param(IS_ETH_PAUSE_TIME(ETH_InitStruct->ETH_PauseTime));
  155. assert_param(IS_ETH_ZEROQUANTA_PAUSE(ETH_InitStruct->ETH_ZeroQuantaPause));
  156. assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(ETH_InitStruct->ETH_PauseLowThreshold));
  157. assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(ETH_InitStruct->ETH_UnicastPauseFrameDetect));
  158. assert_param(IS_ETH_RECEIVE_FLOWCONTROL(ETH_InitStruct->ETH_ReceiveFlowControl));
  159. assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(ETH_InitStruct->ETH_TransmitFlowControl));
  160. assert_param(IS_ETH_VLAN_TAG_COMPARISON(ETH_InitStruct->ETH_VLANTagComparison));
  161. assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(ETH_InitStruct->ETH_VLANTagIdentifier));
  162. /* DMA --------------------------*/
  163. assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame));
  164. assert_param(IS_ETH_RECEIVE_STORE_FORWARD(ETH_InitStruct->ETH_ReceiveStoreForward));
  165. assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(ETH_InitStruct->ETH_FlushReceivedFrame));
  166. assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(ETH_InitStruct->ETH_TransmitStoreForward));
  167. assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(ETH_InitStruct->ETH_TransmitThresholdControl));
  168. assert_param(IS_ETH_FORWARD_ERROR_FRAMES(ETH_InitStruct->ETH_ForwardErrorFrames));
  169. assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(ETH_InitStruct->ETH_ForwardUndersizedGoodFrames));
  170. assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(ETH_InitStruct->ETH_ReceiveThresholdControl));
  171. assert_param(IS_ETH_SECOND_FRAME_OPERATE(ETH_InitStruct->ETH_SecondFrameOperate));
  172. assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(ETH_InitStruct->ETH_AddressAlignedBeats));
  173. assert_param(IS_ETH_FIXED_BURST(ETH_InitStruct->ETH_FixedBurst));
  174. assert_param(IS_ETH_RXDMA_BURST_LENGTH(ETH_InitStruct->ETH_RxDMABurstLength));
  175. assert_param(IS_ETH_TXDMA_BURST_LENGTH(ETH_InitStruct->ETH_TxDMABurstLength));
  176. assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(ETH_InitStruct->ETH_DescriptorSkipLength));
  177. assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(ETH_InitStruct->ETH_DMAArbitration));
  178. /*-------------------------------- MAC Config ------------------------------*/
  179. /*---------------------- ETHERNET MACMIIAR Configuration -------------------*/
  180. /* Get the ETHERNET MACMIIAR value */
  181. tmpreg = ETH->MACMIIAR;
  182. /* Clear CSR Clock Range CR[2:0] bits */
  183. tmpreg &= MACMIIAR_CR_MASK;
  184. /* Get hclk frequency value */
  185. RCC_GetClocksFreq(&rcc_clocks);
  186. hclk = rcc_clocks.HCLK_Frequency;
  187. /* Set CR bits depending on hclk value */
  188. if((hclk >= 20000000)&&(hclk < 35000000))
  189. {
  190. /* CSR Clock Range between 20-35 MHz */
  191. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div16;
  192. }
  193. else if((hclk >= 35000000)&&(hclk < 60000000))
  194. {
  195. /* CSR Clock Range between 35-60 MHz */
  196. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div26;
  197. }
  198. else if((hclk >= 60000000)&&(hclk < 100000000))
  199. {
  200. /* CSR Clock Range between 60-100 MHz */
  201. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div42;
  202. }
  203. else if((hclk >= 100000000)&&(hclk < 150000000))
  204. {
  205. /* CSR Clock Range between 100-150 MHz */
  206. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div62;
  207. }
  208. else /* ((hclk >= 150000000)&&(hclk <= 168000000)) */
  209. {
  210. /* CSR Clock Range between 150-168 MHz */
  211. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div102;
  212. }
  213. /* Write to ETHERNET MAC MIIAR: configure the ETHERNET CSR Clock Range */
  214. ETH->MACMIIAR = (uint32_t)tmpreg;
  215. /*------------------------ ETHERNET MACCR Configuration --------------------*/
  216. /* Get the ETHERNET MACCR value */
  217. tmpreg = ETH->MACCR;
  218. /* Clear WD, PCE, PS, TE and RE bits */
  219. tmpreg &= MACCR_CLEAR_MASK;
  220. /* Set the WD bit according to ETH_Watchdog value */
  221. /* Set the JD: bit according to ETH_Jabber value */
  222. /* Set the IFG bit according to ETH_InterFrameGap value */
  223. /* Set the DCRS bit according to ETH_CarrierSense value */
  224. /* Set the FES bit according to ETH_Speed value */
  225. /* Set the DO bit according to ETH_ReceiveOwn value */
  226. /* Set the LM bit according to ETH_LoopbackMode value */
  227. /* Set the DM bit according to ETH_Mode value */
  228. /* Set the IPCO bit according to ETH_ChecksumOffload value */
  229. /* Set the DR bit according to ETH_RetryTransmission value */
  230. /* Set the ACS bit according to ETH_AutomaticPadCRCStrip value */
  231. /* Set the BL bit according to ETH_BackOffLimit value */
  232. /* Set the DC bit according to ETH_DeferralCheck value */
  233. tmpreg |= (uint32_t)(ETH_InitStruct->ETH_Watchdog |
  234. ETH_InitStruct->ETH_Jabber |
  235. ETH_InitStruct->ETH_InterFrameGap |
  236. ETH_InitStruct->ETH_CarrierSense |
  237. ETH_InitStruct->ETH_Speed |
  238. ETH_InitStruct->ETH_ReceiveOwn |
  239. ETH_InitStruct->ETH_LoopbackMode |
  240. ETH_InitStruct->ETH_Mode |
  241. ETH_InitStruct->ETH_ChecksumOffload |
  242. ETH_InitStruct->ETH_RetryTransmission |
  243. ETH_InitStruct->ETH_AutomaticPadCRCStrip |
  244. ETH_InitStruct->ETH_BackOffLimit |
  245. ETH_InitStruct->ETH_DeferralCheck);
  246. /* Write to ETHERNET MACCR */
  247. ETH->MACCR = (uint32_t)tmpreg;
  248. /*----------------------- ETHERNET MACFFR Configuration --------------------*/
  249. /* Set the RA bit according to ETH_ReceiveAll value */
  250. /* Set the SAF and SAIF bits according to ETH_SourceAddrFilter value */
  251. /* Set the PCF bit according to ETH_PassControlFrames value */
  252. /* Set the DBF bit according to ETH_BroadcastFramesReception value */
  253. /* Set the DAIF bit according to ETH_DestinationAddrFilter value */
  254. /* Set the PR bit according to ETH_PromiscuousMode value */
  255. /* Set the PM, HMC and HPF bits according to ETH_MulticastFramesFilter value */
  256. /* Set the HUC and HPF bits according to ETH_UnicastFramesFilter value */
  257. /* Write to ETHERNET MACFFR */
  258. ETH->MACFFR = (uint32_t)(ETH_InitStruct->ETH_ReceiveAll |
  259. ETH_InitStruct->ETH_SourceAddrFilter |
  260. ETH_InitStruct->ETH_PassControlFrames |
  261. ETH_InitStruct->ETH_BroadcastFramesReception |
  262. ETH_InitStruct->ETH_DestinationAddrFilter |
  263. ETH_InitStruct->ETH_PromiscuousMode |
  264. ETH_InitStruct->ETH_MulticastFramesFilter |
  265. ETH_InitStruct->ETH_UnicastFramesFilter);
  266. /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
  267. /* Write to ETHERNET MACHTHR */
  268. ETH->MACHTHR = (uint32_t)ETH_InitStruct->ETH_HashTableHigh;
  269. /* Write to ETHERNET MACHTLR */
  270. ETH->MACHTLR = (uint32_t)ETH_InitStruct->ETH_HashTableLow;
  271. /*----------------------- ETHERNET MACFCR Configuration --------------------*/
  272. /* Get the ETHERNET MACFCR value */
  273. tmpreg = ETH->MACFCR;
  274. /* Clear xx bits */
  275. tmpreg &= MACFCR_CLEAR_MASK;
  276. /* Set the PT bit according to ETH_PauseTime value */
  277. /* Set the DZPQ bit according to ETH_ZeroQuantaPause value */
  278. /* Set the PLT bit according to ETH_PauseLowThreshold value */
  279. /* Set the UP bit according to ETH_UnicastPauseFrameDetect value */
  280. /* Set the RFE bit according to ETH_ReceiveFlowControl value */
  281. /* Set the TFE bit according to ETH_TransmitFlowControl value */
  282. tmpreg |= (uint32_t)((ETH_InitStruct->ETH_PauseTime << 16) |
  283. ETH_InitStruct->ETH_ZeroQuantaPause |
  284. ETH_InitStruct->ETH_PauseLowThreshold |
  285. ETH_InitStruct->ETH_UnicastPauseFrameDetect |
  286. ETH_InitStruct->ETH_ReceiveFlowControl |
  287. ETH_InitStruct->ETH_TransmitFlowControl);
  288. /* Write to ETHERNET MACFCR */
  289. ETH->MACFCR = (uint32_t)tmpreg;
  290. /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/
  291. /* Set the ETV bit according to ETH_VLANTagComparison value */
  292. /* Set the VL bit according to ETH_VLANTagIdentifier value */
  293. ETH->MACVLANTR = (uint32_t)(ETH_InitStruct->ETH_VLANTagComparison |
  294. ETH_InitStruct->ETH_VLANTagIdentifier);
  295. /*-------------------------------- DMA Config ------------------------------*/
  296. /*----------------------- ETHERNET DMAOMR Configuration --------------------*/
  297. /* Get the ETHERNET DMAOMR value */
  298. tmpreg = ETH->DMAOMR;
  299. /* Clear xx bits */
  300. tmpreg &= DMAOMR_CLEAR_MASK;
  301. /* Set the DT bit according to ETH_DropTCPIPChecksumErrorFrame value */
  302. /* Set the RSF bit according to ETH_ReceiveStoreForward value */
  303. /* Set the DFF bit according to ETH_FlushReceivedFrame value */
  304. /* Set the TSF bit according to ETH_TransmitStoreForward value */
  305. /* Set the TTC bit according to ETH_TransmitThresholdControl value */
  306. /* Set the FEF bit according to ETH_ForwardErrorFrames value */
  307. /* Set the FUF bit according to ETH_ForwardUndersizedGoodFrames value */
  308. /* Set the RTC bit according to ETH_ReceiveThresholdControl value */
  309. /* Set the OSF bit according to ETH_SecondFrameOperate value */
  310. tmpreg |= (uint32_t)(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame |
  311. ETH_InitStruct->ETH_ReceiveStoreForward |
  312. ETH_InitStruct->ETH_FlushReceivedFrame |
  313. ETH_InitStruct->ETH_TransmitStoreForward |
  314. ETH_InitStruct->ETH_TransmitThresholdControl |
  315. ETH_InitStruct->ETH_ForwardErrorFrames |
  316. ETH_InitStruct->ETH_ForwardUndersizedGoodFrames |
  317. ETH_InitStruct->ETH_ReceiveThresholdControl |
  318. ETH_InitStruct->ETH_SecondFrameOperate);
  319. /* Write to ETHERNET DMAOMR */
  320. ETH->DMAOMR = (uint32_t)tmpreg;
  321. /*----------------------- ETHERNET DMABMR Configuration --------------------*/
  322. /* Set the AAL bit according to ETH_AddressAlignedBeats value */
  323. /* Set the FB bit according to ETH_FixedBurst value */
  324. /* Set the RPBL and 4*PBL bits according to ETH_RxDMABurstLength value */
  325. /* Set the PBL and 4*PBL bits according to ETH_TxDMABurstLength value */
  326. /* Set the DSL bit according to ETH_DesciptorSkipLength value */
  327. /* Set the PR and DA bits according to ETH_DMAArbitration value */
  328. ETH->DMABMR = (uint32_t)(ETH_InitStruct->ETH_AddressAlignedBeats |
  329. ETH_InitStruct->ETH_FixedBurst |
  330. ETH_InitStruct->ETH_RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
  331. ETH_InitStruct->ETH_TxDMABurstLength |
  332. (ETH_InitStruct->ETH_DescriptorSkipLength << 2) |
  333. ETH_InitStruct->ETH_DMAArbitration |
  334. ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
  335. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  336. /* Enable the Enhanced DMA descriptors */
  337. ETH->DMABMR |= ETH_DMABMR_EDE;
  338. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  339. /* Return Ethernet configuration success */
  340. return ETH_SUCCESS;
  341. }
  342. void ETH_StructInit(ETH_InitTypeDef* ETH_InitStruct)
  343. {
  344. /* ETH_InitStruct members default value */
  345. /*------------------------ MAC Configuration ---------------------------*/
  346. /* PHY Auto-negotiation enabled */
  347. ETH_InitStruct->ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  348. /* MAC watchdog enabled: cuts-off long frame */
  349. ETH_InitStruct->ETH_Watchdog = ETH_Watchdog_Enable;
  350. /* MAC Jabber enabled in Half-duplex mode */
  351. ETH_InitStruct->ETH_Jabber = ETH_Jabber_Enable;
  352. /* Ethernet interframe gap set to 96 bits */
  353. ETH_InitStruct->ETH_InterFrameGap = ETH_InterFrameGap_96Bit;
  354. /* Carrier Sense Enabled in Half-Duplex mode */
  355. ETH_InitStruct->ETH_CarrierSense = ETH_CarrierSense_Enable;
  356. /* PHY speed configured to 100Mbit/s */
  357. ETH_InitStruct->ETH_Speed = ETH_Speed_100M;
  358. /* Receive own Frames in Half-Duplex mode enabled */
  359. ETH_InitStruct->ETH_ReceiveOwn = ETH_ReceiveOwn_Enable;
  360. /* MAC MII loopback disabled */
  361. ETH_InitStruct->ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  362. /* Full-Duplex mode selected */
  363. ETH_InitStruct->ETH_Mode = ETH_Mode_FullDuplex;
  364. /* IPv4 and TCP/UDP/ICMP frame Checksum Offload disabled */
  365. ETH_InitStruct->ETH_ChecksumOffload = ETH_ChecksumOffload_Disable;
  366. /* Retry Transmission enabled for half-duplex mode */
  367. ETH_InitStruct->ETH_RetryTransmission = ETH_RetryTransmission_Enable;
  368. /* Automatic PAD/CRC strip disabled*/
  369. ETH_InitStruct->ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  370. /* half-duplex mode retransmission Backoff time_limit = 10 slot times*/
  371. ETH_InitStruct->ETH_BackOffLimit = ETH_BackOffLimit_10;
  372. /* half-duplex mode Deferral check disabled */
  373. ETH_InitStruct->ETH_DeferralCheck = ETH_DeferralCheck_Disable;
  374. /* Receive all frames disabled */
  375. ETH_InitStruct->ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  376. /* Source address filtering (on the optional MAC addresses) disabled */
  377. ETH_InitStruct->ETH_SourceAddrFilter = ETH_SourceAddrFilter_Disable;
  378. /* Do not forward control frames that do not pass the address filtering */
  379. ETH_InitStruct->ETH_PassControlFrames = ETH_PassControlFrames_BlockAll;
  380. /* Disable reception of Broadcast frames */
  381. ETH_InitStruct->ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;
  382. /* Normal Destination address filtering (not reverse addressing) */
  383. ETH_InitStruct->ETH_DestinationAddrFilter = ETH_DestinationAddrFilter_Normal;
  384. /* Promiscuous address filtering mode disabled */
  385. ETH_InitStruct->ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  386. /* Perfect address filtering for multicast addresses */
  387. ETH_InitStruct->ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  388. /* Perfect address filtering for unicast addresses */
  389. ETH_InitStruct->ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  390. /* Initialize hash table high and low regs */
  391. ETH_InitStruct->ETH_HashTableHigh = 0x0;
  392. ETH_InitStruct->ETH_HashTableLow = 0x0;
  393. /* Flow control config (flow control disabled)*/
  394. ETH_InitStruct->ETH_PauseTime = 0x0;
  395. ETH_InitStruct->ETH_ZeroQuantaPause = ETH_ZeroQuantaPause_Disable;
  396. ETH_InitStruct->ETH_PauseLowThreshold = ETH_PauseLowThreshold_Minus4;
  397. ETH_InitStruct->ETH_UnicastPauseFrameDetect = ETH_UnicastPauseFrameDetect_Disable;
  398. ETH_InitStruct->ETH_ReceiveFlowControl = ETH_ReceiveFlowControl_Disable;
  399. ETH_InitStruct->ETH_TransmitFlowControl = ETH_TransmitFlowControl_Disable;
  400. /* VLANtag config (VLAN field not checked) */
  401. ETH_InitStruct->ETH_VLANTagComparison = ETH_VLANTagComparison_16Bit;
  402. ETH_InitStruct->ETH_VLANTagIdentifier = 0x0;
  403. /*---------------------- DMA Configuration -------------------------------*/
  404. /* Drops frames with with TCP/IP checksum errors */
  405. ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Disable;
  406. /* Store and forward mode enabled for receive */
  407. ETH_InitStruct->ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  408. /* Flush received frame that created FIFO overflow */
  409. ETH_InitStruct->ETH_FlushReceivedFrame = ETH_FlushReceivedFrame_Enable;
  410. /* Store and forward mode enabled for transmit */
  411. ETH_InitStruct->ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  412. /* Threshold TXFIFO level set to 64 bytes (used when threshold mode is enabled) */
  413. ETH_InitStruct->ETH_TransmitThresholdControl = ETH_TransmitThresholdControl_64Bytes;
  414. /* Disable forwarding frames with errors (short frames, CRC,...)*/
  415. ETH_InitStruct->ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  416. /* Disable undersized good frames */
  417. ETH_InitStruct->ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  418. /* Threshold RXFIFO level set to 64 bytes (used when Cut-through mode is enabled) */
  419. ETH_InitStruct->ETH_ReceiveThresholdControl = ETH_ReceiveThresholdControl_64Bytes;
  420. /* Disable Operate on second frame (transmit a second frame to FIFO without
  421. waiting status of previous frame*/
  422. ETH_InitStruct->ETH_SecondFrameOperate = ETH_SecondFrameOperate_Disable;
  423. /* DMA works on 32-bit aligned start source and destinations addresses */
  424. ETH_InitStruct->ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  425. /* Enabled Fixed Burst Mode (mix of INC4, INC8, INC16 and SINGLE DMA transactions */
  426. ETH_InitStruct->ETH_FixedBurst = ETH_FixedBurst_Enable;
  427. /* DMA transfer max burst length = 32 beats = 32 x 32bits */
  428. ETH_InitStruct->ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  429. ETH_InitStruct->ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  430. /* DMA Ring mode skip length = 0 */
  431. ETH_InitStruct->ETH_DescriptorSkipLength = 0x0;
  432. /* Equal priority (round-robin) between transmit and receive DMA engines */
  433. ETH_InitStruct->ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_1_1;
  434. }
  435. /**
  436. * @brief Enables ENET MAC and DMA reception/transmission
  437. * @param None
  438. * @retval None
  439. */
  440. void ETH_Start(void)
  441. {
  442. /* Enable transmit state machine of the MAC for transmission on the MII */
  443. ETH_MACTransmissionCmd(ENABLE);
  444. /* Flush Transmit FIFO */
  445. ETH_FlushTransmitFIFO();
  446. /* Enable receive state machine of the MAC for reception from the MII */
  447. ETH_MACReceptionCmd(ENABLE);
  448. /* Start DMA transmission */
  449. ETH_DMATransmissionCmd(ENABLE);
  450. /* Start DMA reception */
  451. ETH_DMAReceptionCmd(ENABLE);
  452. }
  453. /**
  454. * @brief Transmits a packet, from application buffer, pointed by ppkt.
  455. * @param ppkt: pointer to the application's packet buffer to transmit.
  456. * @param FrameLength: Tx Packet size.
  457. * @retval ETH_ERROR: in case of Tx desc owned by DMA
  458. * ETH_SUCCESS: for correct transmission
  459. */
  460. uint32_t ETH_HandleTxPkt(uint8_t *ppkt, uint16_t FrameLength)
  461. {
  462. uint32_t offset = 0;
  463. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  464. if((DMATxDescToSet->Status & ETH_DMATxDesc_OWN) != (uint32_t)RESET)
  465. {
  466. /* Return ERROR: OWN bit set */
  467. return ETH_ERROR;
  468. }
  469. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  470. for(offset=0; offset<FrameLength; offset++)
  471. {
  472. (*(__IO uint8_t *)((DMATxDescToSet->Buffer1Addr) + offset)) = (*(ppkt + offset));
  473. }
  474. /* Setting the Frame Length: bits[12:0] */
  475. DMATxDescToSet->ControlBufferSize = (FrameLength & ETH_DMATxDesc_TBS1);
  476. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  477. DMATxDescToSet->Status |= ETH_DMATxDesc_LS | ETH_DMATxDesc_FS;
  478. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  479. DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
  480. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  481. if ((ETH->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
  482. {
  483. /* Clear TBUS ETHERNET DMA flag */
  484. ETH->DMASR = ETH_DMASR_TBUS;
  485. /* Resume DMA transmission*/
  486. ETH->DMATPDR = 0;
  487. }
  488. /* Update the ETHERNET DMA global Tx descriptor with next Tx decriptor */
  489. /* Chained Mode */
  490. if((DMATxDescToSet->Status & ETH_DMATxDesc_TCH) != (uint32_t)RESET)
  491. {
  492. /* Selects the next DMA Tx descriptor list for next buffer to send */
  493. DMATxDescToSet = (ETH_DMADESCTypeDef*) (DMATxDescToSet->Buffer2NextDescAddr);
  494. }
  495. else /* Ring Mode */
  496. {
  497. if((DMATxDescToSet->Status & ETH_DMATxDesc_TER) != (uint32_t)RESET)
  498. {
  499. /* Selects the first DMA Tx descriptor for next buffer to send: last Tx descriptor was used */
  500. DMATxDescToSet = (ETH_DMADESCTypeDef*) (ETH->DMATDLAR);
  501. }
  502. else
  503. {
  504. /* Selects the next DMA Tx descriptor list for next buffer to send */
  505. DMATxDescToSet = (ETH_DMADESCTypeDef*) ((uint32_t)DMATxDescToSet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  506. }
  507. }
  508. /* Return SUCCESS */
  509. return ETH_SUCCESS;
  510. }
  511. /**
  512. * @brief Receives a packet and copies it to memory pointed by ppkt.
  513. * @param ppkt: pointer to the application packet receive buffer.
  514. * @retval ETH_ERROR: if there is error in reception
  515. * framelength: received packet size if packet reception is correct
  516. */
  517. uint32_t ETH_HandleRxPkt(uint8_t *ppkt)
  518. {
  519. uint32_t offset = 0, framelength = 0;
  520. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  521. if((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) != (uint32_t)RESET)
  522. {
  523. /* Return error: OWN bit set */
  524. return ETH_ERROR;
  525. }
  526. if(((DMARxDescToGet->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  527. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET) &&
  528. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET))
  529. {
  530. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  531. framelength = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  532. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  533. for(offset=0; offset<framelength; offset++)
  534. {
  535. (*(ppkt + offset)) = (*(__IO uint8_t *)((DMARxDescToGet->Buffer1Addr) + offset));
  536. }
  537. }
  538. else
  539. {
  540. /* Return ERROR */
  541. framelength = ETH_ERROR;
  542. }
  543. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  544. DMARxDescToGet->Status = ETH_DMARxDesc_OWN;
  545. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  546. if ((ETH->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  547. {
  548. /* Clear RBUS ETHERNET DMA flag */
  549. ETH->DMASR = ETH_DMASR_RBUS;
  550. /* Resume DMA reception */
  551. ETH->DMARPDR = 0;
  552. }
  553. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  554. /* Chained Mode */
  555. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RCH) != (uint32_t)RESET)
  556. {
  557. /* Selects the next DMA Rx descriptor list for next buffer to read */
  558. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  559. }
  560. else /* Ring Mode */
  561. {
  562. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RER) != (uint32_t)RESET)
  563. {
  564. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  565. DMARxDescToGet = (ETH_DMADESCTypeDef*) (ETH->DMARDLAR);
  566. }
  567. else
  568. {
  569. /* Selects the next DMA Rx descriptor list for next buffer to read */
  570. DMARxDescToGet = (ETH_DMADESCTypeDef*) ((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  571. }
  572. }
  573. /* Return Frame Length/ERROR */
  574. return (framelength);
  575. }
  576. /**
  577. * @brief Get the size of received the received packet.
  578. * @param None
  579. * @retval framelength: received packet size
  580. */
  581. uint32_t ETH_GetRxPktSize(void)
  582. {
  583. uint32_t frameLength = 0;
  584. if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  585. ((DMARxDescToGet->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  586. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET) &&
  587. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET))
  588. {
  589. /* Get the size of the packet: including 4 bytes of the CRC */
  590. frameLength = ETH_GetDMARxDescFrameLength(DMARxDescToGet);
  591. }
  592. /* Return Frame Length */
  593. return frameLength;
  594. }
  595. /**
  596. * @brief Drop a Received packet (too small packet, etc...)
  597. * @param None
  598. * @retval None
  599. */
  600. void ETH_DropRxPkt(void)
  601. {
  602. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  603. DMARxDescToGet->Status = ETH_DMARxDesc_OWN;
  604. /* Chained Mode */
  605. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RCH) != (uint32_t)RESET)
  606. {
  607. /* Selects the next DMA Rx descriptor list for next buffer read */
  608. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  609. }
  610. else /* Ring Mode */
  611. {
  612. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RER) != (uint32_t)RESET)
  613. {
  614. /* Selects the next DMA Rx descriptor list for next buffer read: this will
  615. be the first Rx descriptor in this case */
  616. DMARxDescToGet = (ETH_DMADESCTypeDef*) (ETH->DMARDLAR);
  617. }
  618. else
  619. {
  620. /* Selects the next DMA Rx descriptor list for next buffer read */
  621. DMARxDescToGet = (ETH_DMADESCTypeDef*) ((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  622. }
  623. }
  624. }
  625. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  626. /**
  627. * @brief Enables or disables the Enhanced descriptor structure.
  628. * @param NewState: new state of the Enhanced descriptor structure.
  629. * This parameter can be: ENABLE or DISABLE.
  630. * @retval None
  631. */
  632. void ETH_EnhancedDescriptorCmd(FunctionalState NewState)
  633. {
  634. /* Check the parameters */
  635. assert_param(IS_FUNCTIONAL_STATE(NewState));
  636. if (NewState != DISABLE)
  637. {
  638. /* Enable enhanced descriptor structure */
  639. ETH->DMABMR |= ETH_DMABMR_EDE;
  640. }
  641. else
  642. {
  643. /* Disable enhanced descriptor structure */
  644. ETH->DMABMR &= ~ETH_DMABMR_EDE;
  645. }
  646. }
  647. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  648. /*--------------------------------- PHY ------------------------------------*/
  649. /**
  650. * @brief Read a PHY register
  651. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  652. * This parameter can be one of the following values: 0,..,31
  653. * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
  654. * This parameter can be one of the following values:
  655. * @arg PHY_BCR: Tranceiver Basic Control Register
  656. * @arg PHY_BSR: Tranceiver Basic Status Register
  657. * @arg PHY_SR : Tranceiver Status Register
  658. * @arg More PHY register could be read depending on the used PHY
  659. * @retval ETH_ERROR: in case of timeout
  660. * MAC MIIDR register value: Data read from the selected PHY register (correct read )
  661. */
  662. uint16_t ETH_ReadPHYRegister(uint16_t PHYAddress, uint16_t PHYReg)
  663. {
  664. uint32_t tmpreg = 0;
  665. __IO uint32_t timeout = 0;
  666. /* Check the parameters */
  667. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  668. assert_param(IS_ETH_PHY_REG(PHYReg));
  669. /* Get the ETHERNET MACMIIAR value */
  670. tmpreg = ETH->MACMIIAR;
  671. /* Keep only the CSR Clock Range CR[2:0] bits value */
  672. tmpreg &= ~MACMIIAR_CR_MASK;
  673. /* Prepare the MII address register value */
  674. tmpreg |=(((uint32_t)PHYAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  675. tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  676. tmpreg &= ~ETH_MACMIIAR_MW; /* Set the read mode */
  677. tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  678. /* Write the result value into the MII Address register */
  679. ETH->MACMIIAR = tmpreg;
  680. /* Check for the Busy flag */
  681. do
  682. {
  683. timeout++;
  684. tmpreg = ETH->MACMIIAR;
  685. } while ((tmpreg & ETH_MACMIIAR_MB) && (timeout < (uint32_t)PHY_READ_TO));
  686. /* Return ERROR in case of timeout */
  687. if(timeout == PHY_READ_TO)
  688. {
  689. return (uint16_t)ETH_ERROR;
  690. }
  691. /* Return data register value */
  692. return (uint16_t)(ETH->MACMIIDR);
  693. }
  694. /**
  695. * @brief Write to a PHY register
  696. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  697. * This parameter can be one of the following values: 0,..,31
  698. * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
  699. * This parameter can be one of the following values:
  700. * @arg PHY_BCR : Tranceiver Control Register
  701. * @arg More PHY register could be written depending on the used PHY
  702. * @param PHYValue: the value to write
  703. * @retval ETH_ERROR: in case of timeout
  704. * ETH_SUCCESS: for correct write
  705. */
  706. uint32_t ETH_WritePHYRegister(uint16_t PHYAddress, uint16_t PHYReg, uint16_t PHYValue)
  707. {
  708. uint32_t tmpreg = 0;
  709. __IO uint32_t timeout = 0;
  710. /* Check the parameters */
  711. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  712. assert_param(IS_ETH_PHY_REG(PHYReg));
  713. /* Get the ETHERNET MACMIIAR value */
  714. tmpreg = ETH->MACMIIAR;
  715. /* Keep only the CSR Clock Range CR[2:0] bits value */
  716. tmpreg &= ~MACMIIAR_CR_MASK;
  717. /* Prepare the MII register address value */
  718. tmpreg |=(((uint32_t)PHYAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  719. tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  720. tmpreg |= ETH_MACMIIAR_MW; /* Set the write mode */
  721. tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  722. /* Give the value to the MII data register */
  723. ETH->MACMIIDR = PHYValue;
  724. /* Write the result value into the MII Address register */
  725. ETH->MACMIIAR = tmpreg;
  726. /* Check for the Busy flag */
  727. do
  728. {
  729. timeout++;
  730. tmpreg = ETH->MACMIIAR;
  731. } while ((tmpreg & ETH_MACMIIAR_MB) && (timeout < (uint32_t)PHY_WRITE_TO));
  732. /* Return ERROR in case of timeout */
  733. if(timeout == PHY_WRITE_TO)
  734. {
  735. return ETH_ERROR;
  736. }
  737. /* Return SUCCESS */
  738. return ETH_SUCCESS;
  739. }
  740. /**
  741. * @brief Enables or disables the PHY loopBack mode.
  742. * @Note: Don't be confused with ETH_MACLoopBackCmd function which enables internal
  743. * loopback at MII level
  744. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  745. * This parameter can be one of the following values:
  746. * @param NewState: new state of the PHY loopBack mode.
  747. * This parameter can be: ENABLE or DISABLE.
  748. * @retval ETH_ERROR: in case of bad PHY configuration
  749. * ETH_SUCCESS: for correct PHY configuration
  750. */
  751. uint32_t ETH_PHYLoopBackCmd(uint16_t PHYAddress, FunctionalState NewState)
  752. {
  753. uint16_t tmpreg = 0;
  754. /* Check the parameters */
  755. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  756. assert_param(IS_FUNCTIONAL_STATE(NewState));
  757. /* Get the PHY configuration to update it */
  758. tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_BCR);
  759. if (NewState != DISABLE)
  760. {
  761. /* Enable the PHY loopback mode */
  762. tmpreg |= PHY_Loopback;
  763. }
  764. else
  765. {
  766. /* Disable the PHY loopback mode: normal mode */
  767. tmpreg &= (uint16_t)(~(uint16_t)PHY_Loopback);
  768. }
  769. /* Update the PHY control register with the new configuration */
  770. if(ETH_WritePHYRegister(PHYAddress, PHY_BCR, tmpreg) != (uint32_t)RESET)
  771. {
  772. return ETH_SUCCESS;
  773. }
  774. else
  775. {
  776. /* Return SUCCESS */
  777. return ETH_ERROR;
  778. }
  779. }
  780. /*--------------------------------- MAC ------------------------------------*/
  781. /**
  782. * @brief Enables or disables the MAC transmission.
  783. * @param NewState: new state of the MAC transmission.
  784. * This parameter can be: ENABLE or DISABLE.
  785. * @retval None
  786. */
  787. void ETH_MACTransmissionCmd(FunctionalState NewState)
  788. {
  789. /* Check the parameters */
  790. assert_param(IS_FUNCTIONAL_STATE(NewState));
  791. if (NewState != DISABLE)
  792. {
  793. /* Enable the MAC transmission */
  794. ETH->MACCR |= ETH_MACCR_TE;
  795. }
  796. else
  797. {
  798. /* Disable the MAC transmission */
  799. ETH->MACCR &= ~ETH_MACCR_TE;
  800. }
  801. }
  802. /**
  803. * @brief Enables or disables the MAC reception.
  804. * @param NewState: new state of the MAC reception.
  805. * This parameter can be: ENABLE or DISABLE.
  806. * @retval None
  807. */
  808. void ETH_MACReceptionCmd(FunctionalState NewState)
  809. {
  810. /* Check the parameters */
  811. assert_param(IS_FUNCTIONAL_STATE(NewState));
  812. if (NewState != DISABLE)
  813. {
  814. /* Enable the MAC reception */
  815. ETH->MACCR |= ETH_MACCR_RE;
  816. }
  817. else
  818. {
  819. /* Disable the MAC reception */
  820. ETH->MACCR &= ~ETH_MACCR_RE;
  821. }
  822. }
  823. /**
  824. * @brief Checks whether the ETHERNET flow control busy bit is set or not.
  825. * @param None
  826. * @retval The new state of flow control busy status bit (SET or RESET).
  827. */
  828. FlagStatus ETH_GetFlowControlBusyStatus(void)
  829. {
  830. FlagStatus bitstatus = RESET;
  831. /* The Flow Control register should not be written to until this bit is cleared */
  832. if ((ETH->MACFCR & ETH_MACFCR_FCBBPA) != (uint32_t)RESET)
  833. {
  834. bitstatus = SET;
  835. }
  836. else
  837. {
  838. bitstatus = RESET;
  839. }
  840. return bitstatus;
  841. }
  842. /**
  843. * @brief Initiate a Pause Control Frame (Full-duplex only).
  844. * @param None
  845. * @retval None
  846. */
  847. void ETH_InitiatePauseControlFrame(void)
  848. {
  849. /* When Set In full duplex MAC initiates pause control frame */
  850. ETH->MACFCR |= ETH_MACFCR_FCBBPA;
  851. }
  852. /**
  853. * @brief Enables or disables the MAC BackPressure operation activation (Half-duplex only).
  854. * @param NewState: new state of the MAC BackPressure operation activation.
  855. * This parameter can be: ENABLE or DISABLE.
  856. * @retval None
  857. */
  858. void ETH_BackPressureActivationCmd(FunctionalState NewState)
  859. {
  860. /* Check the parameters */
  861. assert_param(IS_FUNCTIONAL_STATE(NewState));
  862. if (NewState != DISABLE)
  863. {
  864. /* Activate the MAC BackPressure operation */
  865. /* In Half duplex: during backpressure, when the MAC receives a new frame,
  866. the transmitter starts sending a JAM pattern resulting in a collision */
  867. ETH->MACFCR |= ETH_MACFCR_FCBBPA;
  868. }
  869. else
  870. {
  871. /* Desactivate the MAC BackPressure operation */
  872. ETH->MACFCR &= ~ETH_MACFCR_FCBBPA;
  873. }
  874. }
  875. /**
  876. * @brief Checks whether the specified ETHERNET MAC flag is set or not.
  877. * @param ETH_MAC_FLAG: specifies the flag to check.
  878. * This parameter can be one of the following values:
  879. * @arg ETH_MAC_FLAG_TST : Time stamp trigger flag
  880. * @arg ETH_MAC_FLAG_MMCT : MMC transmit flag
  881. * @arg ETH_MAC_FLAG_MMCR : MMC receive flag
  882. * @arg ETH_MAC_FLAG_MMC : MMC flag
  883. * @arg ETH_MAC_FLAG_PMT : PMT flag
  884. * @retval The new state of ETHERNET MAC flag (SET or RESET).
  885. */
  886. FlagStatus ETH_GetMACFlagStatus(uint32_t ETH_MAC_FLAG)
  887. {
  888. FlagStatus bitstatus = RESET;
  889. /* Check the parameters */
  890. assert_param(IS_ETH_MAC_GET_FLAG(ETH_MAC_FLAG));
  891. if ((ETH->MACSR & ETH_MAC_FLAG) != (uint32_t)RESET)
  892. {
  893. bitstatus = SET;
  894. }
  895. else
  896. {
  897. bitstatus = RESET;
  898. }
  899. return bitstatus;
  900. }
  901. /**
  902. * @brief Checks whether the specified ETHERNET MAC interrupt has occurred or not.
  903. * @param ETH_MAC_IT: specifies the interrupt source to check.
  904. * This parameter can be one of the following values:
  905. * @arg ETH_MAC_IT_TST : Time stamp trigger interrupt
  906. * @arg ETH_MAC_IT_MMCT : MMC transmit interrupt
  907. * @arg ETH_MAC_IT_MMCR : MMC receive interrupt
  908. * @arg ETH_MAC_IT_MMC : MMC interrupt
  909. * @arg ETH_MAC_IT_PMT : PMT interrupt
  910. * @retval The new state of ETHERNET MAC interrupt (SET or RESET).
  911. */
  912. ITStatus ETH_GetMACITStatus(uint32_t ETH_MAC_IT)
  913. {
  914. ITStatus bitstatus = RESET;
  915. /* Check the parameters */
  916. assert_param(IS_ETH_MAC_GET_IT(ETH_MAC_IT));
  917. if ((ETH->MACSR & ETH_MAC_IT) != (uint32_t)RESET)
  918. {
  919. bitstatus = SET;
  920. }
  921. else
  922. {
  923. bitstatus = RESET;
  924. }
  925. return bitstatus;
  926. }
  927. /**
  928. * @brief Enables or disables the specified ETHERNET MAC interrupts.
  929. * @param ETH_MAC_IT: specifies the ETHERNET MAC interrupt sources to be
  930. * enabled or disabled.
  931. * This parameter can be any combination of the following values:
  932. * @arg ETH_MAC_IT_TST : Time stamp trigger interrupt
  933. * @arg ETH_MAC_IT_PMT : PMT interrupt
  934. * @param NewState: new state of the specified ETHERNET MAC interrupts.
  935. * This parameter can be: ENABLE or DISABLE.
  936. * @retval None
  937. */
  938. void ETH_MACITConfig(uint32_t ETH_MAC_IT, FunctionalState NewState)
  939. {
  940. /* Check the parameters */
  941. assert_param(IS_ETH_MAC_IT(ETH_MAC_IT));
  942. assert_param(IS_FUNCTIONAL_STATE(NewState));
  943. if (NewState != DISABLE)
  944. {
  945. /* Enable the selected ETHERNET MAC interrupts */
  946. ETH->MACIMR &= (~(uint32_t)ETH_MAC_IT);
  947. }
  948. else
  949. {
  950. /* Disable the selected ETHERNET MAC interrupts */
  951. ETH->MACIMR |= ETH_MAC_IT;
  952. }
  953. }
  954. /**
  955. * @brief configures the selected MAC address.
  956. * @param MacAddr: The MAC addres to configure.
  957. * This parameter can be one of the following values:
  958. * @arg ETH_MAC_Address0 : MAC Address0
  959. * @arg ETH_MAC_Address1 : MAC Address1
  960. * @arg ETH_MAC_Address2 : MAC Address2
  961. * @arg ETH_MAC_Address3 : MAC Address3
  962. * @param Addr: Pointer on MAC address buffer data (6 bytes).
  963. * @retval None
  964. */
  965. void ETH_MACAddressConfig(uint32_t MacAddr, uint8_t *Addr)
  966. {
  967. uint32_t tmpreg;
  968. /* Check the parameters */
  969. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  970. /* Calculate the selectecd MAC address high register */
  971. tmpreg = ((uint32_t)Addr[5] << 8) | (uint32_t)Addr[4];
  972. /* Load the selectecd MAC address high register */
  973. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) = tmpreg;
  974. /* Calculate the selectecd MAC address low register */
  975. tmpreg = ((uint32_t)Addr[3] << 24) | ((uint32_t)Addr[2] << 16) | ((uint32_t)Addr[1] << 8) | Addr[0];
  976. /* Load the selectecd MAC address low register */
  977. (*(__IO uint32_t *) (ETH_MAC_ADDR_LBASE + MacAddr)) = tmpreg;
  978. }
  979. /**
  980. * @brief Get the selected MAC address.
  981. * @param MacAddr: The MAC addres to return.
  982. * This parameter can be one of the following values:
  983. * @arg ETH_MAC_Address0 : MAC Address0
  984. * @arg ETH_MAC_Address1 : MAC Address1
  985. * @arg ETH_MAC_Address2 : MAC Address2
  986. * @arg ETH_MAC_Address3 : MAC Address3
  987. * @param Addr: Pointer on MAC address buffer data (6 bytes).
  988. * @retval None
  989. */
  990. void ETH_GetMACAddress(uint32_t MacAddr, uint8_t *Addr)
  991. {
  992. uint32_t tmpreg;
  993. /* Check the parameters */
  994. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  995. /* Get the selectecd MAC address high register */
  996. tmpreg =(*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr));
  997. /* Calculate the selectecd MAC address buffer */
  998. Addr[5] = ((tmpreg >> 8) & (uint8_t)0xFF);
  999. Addr[4] = (tmpreg & (uint8_t)0xFF);
  1000. /* Load the selectecd MAC address low register */
  1001. tmpreg =(*(__IO uint32_t *) (ETH_MAC_ADDR_LBASE + MacAddr));
  1002. /* Calculate the selectecd MAC address buffer */
  1003. Addr[3] = ((tmpreg >> 24) & (uint8_t)0xFF);
  1004. Addr[2] = ((tmpreg >> 16) & (uint8_t)0xFF);
  1005. Addr[1] = ((tmpreg >> 8 ) & (uint8_t)0xFF);
  1006. Addr[0] = (tmpreg & (uint8_t)0xFF);
  1007. }
  1008. /**
  1009. * @brief Enables or disables the Address filter module uses the specified
  1010. * ETHERNET MAC address for perfect filtering
  1011. * @param MacAddr: specifies the ETHERNET MAC address to be used for prfect filtering.
  1012. * This parameter can be one of the following values:
  1013. * @arg ETH_MAC_Address1 : MAC Address1
  1014. * @arg ETH_MAC_Address2 : MAC Address2
  1015. * @arg ETH_MAC_Address3 : MAC Address3
  1016. * @param NewState: new state of the specified ETHERNET MAC address use.
  1017. * This parameter can be: ENABLE or DISABLE.
  1018. * @retval None
  1019. */
  1020. void ETH_MACAddressPerfectFilterCmd(uint32_t MacAddr, FunctionalState NewState)
  1021. {
  1022. /* Check the parameters */
  1023. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  1024. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1025. if (NewState != DISABLE)
  1026. {
  1027. /* Enable the selected ETHERNET MAC address for perfect filtering */
  1028. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACA1HR_AE;
  1029. }
  1030. else
  1031. {
  1032. /* Disable the selected ETHERNET MAC address for perfect filtering */
  1033. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_AE);
  1034. }
  1035. }
  1036. /**
  1037. * @brief Set the filter type for the specified ETHERNET MAC address
  1038. * @param MacAddr: specifies the ETHERNET MAC address
  1039. * This parameter can be one of the following values:
  1040. * @arg ETH_MAC_Address1 : MAC Address1
  1041. * @arg ETH_MAC_Address2 : MAC Address2
  1042. * @arg ETH_MAC_Address3 : MAC Address3
  1043. * @param Filter: specifies the used frame received field for comparaison
  1044. * This parameter can be one of the following values:
  1045. * @arg ETH_MAC_AddressFilter_SA : MAC Address is used to compare with the
  1046. * SA fields of the received frame.
  1047. * @arg ETH_MAC_AddressFilter_DA : MAC Address is used to compare with the
  1048. * DA fields of the received frame.
  1049. * @retval None
  1050. */
  1051. void ETH_MACAddressFilterConfig(uint32_t MacAddr, uint32_t Filter)
  1052. {
  1053. /* Check the parameters */
  1054. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  1055. assert_param(IS_ETH_MAC_ADDRESS_FILTER(Filter));
  1056. if (Filter != ETH_MAC_AddressFilter_DA)
  1057. {
  1058. /* The selected ETHERNET MAC address is used to compare with the SA fields of the
  1059. received frame. */
  1060. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACA1HR_SA;
  1061. }
  1062. else
  1063. {
  1064. /* The selected ETHERNET MAC address is used to compare with the DA fields of the
  1065. received frame. */
  1066. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_SA);
  1067. }
  1068. }
  1069. /**
  1070. * @brief Set the filter type for the specified ETHERNET MAC address
  1071. * @param MacAddr: specifies the ETHERNET MAC address
  1072. * This parameter can be one of the following values:
  1073. * @arg ETH_MAC_Address1 : MAC Address1
  1074. * @arg ETH_MAC_Address2 : MAC Address2
  1075. * @arg ETH_MAC_Address3 : MAC Address3
  1076. * @param MaskByte: specifies the used address bytes for comparaison
  1077. * This parameter can be any combination of the following values:
  1078. * @arg ETH_MAC_AddressMask_Byte6 : Mask MAC Address high reg bits [15:8].
  1079. * @arg ETH_MAC_AddressMask_Byte5 : Mask MAC Address high reg bits [7:0].
  1080. * @arg ETH_MAC_AddressMask_Byte4 : Mask MAC Address low reg bits [31:24].
  1081. * @arg ETH_MAC_AddressMask_Byte3 : Mask MAC Address low reg bits [23:16].
  1082. * @arg ETH_MAC_AddressMask_Byte2 : Mask MAC Address low reg bits [15:8].
  1083. * @arg ETH_MAC_AddressMask_Byte1 : Mask MAC Address low reg bits [7:0].
  1084. * @retval None
  1085. */
  1086. void ETH_MACAddressMaskBytesFilterConfig(uint32_t MacAddr, uint32_t MaskByte)
  1087. {
  1088. /* Check the parameters */
  1089. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  1090. assert_param(IS_ETH_MAC_ADDRESS_MASK(MaskByte));
  1091. /* Clear MBC bits in the selected MAC address high register */
  1092. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_MBC);
  1093. /* Set the selected Filetr mask bytes */
  1094. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= MaskByte;
  1095. }
  1096. /*------------------------ DMA Tx/Rx Desciptors -----------------------------*/
  1097. /**
  1098. * @brief Initializes the DMA Tx descriptors in chain mode.
  1099. * @param DMATxDescTab: Pointer on the first Tx desc list
  1100. * @param TxBuff: Pointer on the first TxBuffer list
  1101. * @param TxBuffCount: Number of the used Tx desc in the list
  1102. * @retval None
  1103. */
  1104. void ETH_DMATxDescChainInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t* TxBuff, uint32_t TxBuffCount)
  1105. {
  1106. uint32_t i = 0;
  1107. ETH_DMADESCTypeDef *DMATxDesc;
  1108. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  1109. DMATxDescToSet = DMATxDescTab;
  1110. /* Fill each DMATxDesc descriptor with the right values */
  1111. for(i=0; i < TxBuffCount; i++)
  1112. {
  1113. /* Get the pointer on the ith member of the Tx Desc list */
  1114. DMATxDesc = DMATxDescTab + i;
  1115. /* Set Second Address Chained bit */
  1116. DMATxDesc->Status = ETH_DMATxDesc_TCH;
  1117. /* Set Buffer1 address pointer */
  1118. DMATxDesc->Buffer1Addr = (uint32_t)(&TxBuff[i*ETH_MAX_PACKET_SIZE]);
  1119. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  1120. if(i < (TxBuffCount-1))
  1121. {
  1122. /* Set next descriptor address register with next descriptor base address */
  1123. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1);
  1124. }
  1125. else
  1126. {
  1127. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1128. DMATxDesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  1129. }
  1130. }
  1131. /* Set Transmit Desciptor List Address Register */
  1132. ETH->DMATDLAR = (uint32_t) DMATxDescTab;
  1133. }
  1134. /**
  1135. * @brief Initializes the DMA Tx descriptors in ring mode.
  1136. * @param DMATxDescTab: Pointer on the first Tx desc list
  1137. * @param TxBuff1: Pointer on the first TxBuffer1 list
  1138. * @param TxBuff2: Pointer on the first TxBuffer2 list
  1139. * @param TxBuffCount: Number of the used Tx desc in the list
  1140. * Note: see decriptor skip length defined in ETH_DMA_InitStruct
  1141. * for the number of Words to skip between two unchained descriptors.
  1142. * @retval None
  1143. */
  1144. void ETH_DMATxDescRingInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t *TxBuff1, uint8_t *TxBuff2, uint32_t TxBuffCount)
  1145. {
  1146. uint32_t i = 0;
  1147. ETH_DMADESCTypeDef *DMATxDesc;
  1148. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  1149. DMATxDescToSet = DMATxDescTab;
  1150. /* Fill each DMATxDesc descriptor with the right values */
  1151. for(i=0; i < TxBuffCount; i++)
  1152. {
  1153. /* Get the pointer on the ith member of the Tx Desc list */
  1154. DMATxDesc = DMATxDescTab + i;
  1155. /* Set Buffer1 address pointer */
  1156. DMATxDesc->Buffer1Addr = (uint32_t)(&TxBuff1[i*ETH_MAX_PACKET_SIZE]);
  1157. /* Set Buffer2 address pointer */
  1158. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(&TxBuff2[i*ETH_MAX_PACKET_SIZE]);
  1159. /* Set Transmit End of Ring bit for last descriptor: The DMA returns to the base
  1160. address of the list, creating a Desciptor Ring */
  1161. if(i == (TxBuffCount-1))
  1162. {
  1163. /* Set Transmit End of Ring bit */
  1164. DMATxDesc->Status = ETH_DMATxDesc_TER;
  1165. }
  1166. }
  1167. /* Set Transmit Desciptor List Address Register */
  1168. ETH->DMATDLAR = (uint32_t) DMATxDescTab;
  1169. }
  1170. /**
  1171. * @brief Checks whether the specified ETHERNET DMA Tx Desc flag is set or not.
  1172. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1173. * @param ETH_DMATxDescFlag: specifies the flag to check.
  1174. * This parameter can be one of the following values:
  1175. * @arg ETH_DMATxDesc_OWN : OWN bit: descriptor is owned by DMA engine
  1176. * @arg ETH_DMATxDesc_IC : Interrupt on completetion
  1177. * @arg ETH_DMATxDesc_LS : Last Segment
  1178. * @arg ETH_DMATxDesc_FS : First Segment
  1179. * @arg ETH_DMATxDesc_DC : Disable CRC
  1180. * @arg ETH_DMATxDesc_DP : Disable Pad
  1181. * @arg ETH_DMATxDesc_TTSE: Transmit Time Stamp Enable
  1182. * @arg ETH_DMATxDesc_CIC : Checksum insertion control
  1183. * @arg ETH_DMATxDesc_TER : Transmit End of Ring
  1184. * @arg ETH_DMATxDesc_TCH : Second Address Chained
  1185. * @arg ETH_DMATxDesc_TTSS: Tx Time Stamp Status
  1186. * @arg ETH_DMATxDesc_IHE : IP Header Error
  1187. * @arg ETH_DMATxDesc_ES : Error summary
  1188. * @arg ETH_DMATxDesc_JT : Jabber Timeout
  1189. * @arg ETH_DMATxDesc_FF : Frame Flushed: DMA/MTL flushed the frame due to SW flush
  1190. * @arg ETH_DMATxDesc_PCE : Payload Checksum Error
  1191. * @arg ETH_DMATxDesc_LCA : Loss of Carrier: carrier lost during tramsmission
  1192. * @arg ETH_DMATxDesc_NC : No Carrier: no carrier signal from the tranceiver
  1193. * @arg ETH_DMATxDesc_LCO : Late Collision: transmission aborted due to collision
  1194. * @arg ETH_DMATxDesc_EC : Excessive Collision: transmission aborted after 16 collisions
  1195. * @arg ETH_DMATxDesc_VF : VLAN Frame
  1196. * @arg ETH_DMATxDesc_CC : Collision Count
  1197. * @arg ETH_DMATxDesc_ED : Excessive Deferral
  1198. * @arg ETH_DMATxDesc_UF : Underflow Error: late data arrival from the memory
  1199. * @arg ETH_DMATxDesc_DB : Deferred Bit
  1200. * @retval The new state of ETH_DMATxDescFlag (SET or RESET).
  1201. */
  1202. FlagStatus ETH_GetDMATxDescFlagStatus(ETH_DMADESCTypeDef *DMATxDesc, uint32_t ETH_DMATxDescFlag)
  1203. {
  1204. FlagStatus bitstatus = RESET;
  1205. /* Check the parameters */
  1206. assert_param(IS_ETH_DMATxDESC_GET_FLAG(ETH_DMATxDescFlag));
  1207. if ((DMATxDesc->Status & ETH_DMATxDescFlag) != (uint32_t)RESET)
  1208. {
  1209. bitstatus = SET;
  1210. }
  1211. else
  1212. {
  1213. bitstatus = RESET;
  1214. }
  1215. return bitstatus;
  1216. }
  1217. /**
  1218. * @brief Returns the specified ETHERNET DMA Tx Desc collision count.
  1219. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1220. * @retval The Transmit descriptor collision counter value.
  1221. */
  1222. uint32_t ETH_GetDMATxDescCollisionCount(ETH_DMADESCTypeDef *DMATxDesc)
  1223. {
  1224. /* Return the Receive descriptor frame length */
  1225. return ((DMATxDesc->Status & ETH_DMATxDesc_CC) >> ETH_DMATXDESC_COLLISION_COUNTSHIFT);
  1226. }
  1227. /**
  1228. * @brief Set the specified DMA Tx Desc Own bit.
  1229. * @param DMATxDesc: Pointer on a Tx desc
  1230. * @retval None
  1231. */
  1232. void ETH_SetDMATxDescOwnBit(ETH_DMADESCTypeDef *DMATxDesc)
  1233. {
  1234. /* Set the DMA Tx Desc Own bit */
  1235. DMATxDesc->Status |= ETH_DMATxDesc_OWN;
  1236. }
  1237. /**
  1238. * @brief Enables or disables the specified DMA Tx Desc Transmit interrupt.
  1239. * @param DMATxDesc: Pointer on a Tx desc
  1240. * @param NewState: new state of the DMA Tx Desc transmit interrupt.
  1241. * This parameter can be: ENABLE or DISABLE.
  1242. * @retval None
  1243. */
  1244. void ETH_DMATxDescTransmitITConfig(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1245. {
  1246. /* Check the parameters */
  1247. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1248. if (NewState != DISABLE)
  1249. {
  1250. /* Enable the DMA Tx Desc Transmit interrupt */
  1251. DMATxDesc->Status |= ETH_DMATxDesc_IC;
  1252. }
  1253. else
  1254. {
  1255. /* Disable the DMA Tx Desc Transmit interrupt */
  1256. DMATxDesc->Status &=(~(uint32_t)ETH_DMATxDesc_IC);
  1257. }
  1258. }
  1259. /**
  1260. * @brief Enables or disables the specified DMA Tx Desc Transmit interrupt.
  1261. * @param DMATxDesc: Pointer on a Tx desc
  1262. * @param DMATxDesc_FrameSegment: specifies is the actual Tx desc contain last or first segment.
  1263. * This parameter can be one of the following values:
  1264. * @arg ETH_DMATxDesc_LastSegment : actual Tx desc contain last segment
  1265. * @arg ETH_DMATxDesc_FirstSegment : actual Tx desc contain first segment
  1266. * @retval None
  1267. */
  1268. void ETH_DMATxDescFrameSegmentConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t DMATxDesc_FrameSegment)
  1269. {
  1270. /* Check the parameters */
  1271. assert_param(IS_ETH_DMA_TXDESC_SEGMENT(DMATxDesc_FrameSegment));
  1272. /* Selects the DMA Tx Desc Frame segment */
  1273. DMATxDesc->Status |= DMATxDesc_FrameSegment;
  1274. }
  1275. /**
  1276. * @brief Selects the specified ETHERNET DMA Tx Desc Checksum Insertion.
  1277. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1278. * @param DMATxDesc_Checksum: specifies is the DMA Tx desc checksum insertion.
  1279. * This parameter can be one of the following values:
  1280. * @arg ETH_DMATxDesc_ChecksumByPass : Checksum bypass
  1281. * @arg ETH_DMATxDesc_ChecksumIPV4Header : IPv4 header checksum
  1282. * @arg ETH_DMATxDesc_ChecksumTCPUDPICMPSegment : TCP/UDP/ICMP checksum. Pseudo header checksum is assumed to be present
  1283. * @arg ETH_DMATxDesc_ChecksumTCPUDPICMPFull : TCP/UDP/ICMP checksum fully in hardware including pseudo header
  1284. * @retval None
  1285. */
  1286. void ETH_DMATxDescChecksumInsertionConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t DMATxDesc_Checksum)
  1287. {
  1288. /* Check the parameters */
  1289. assert_param(IS_ETH_DMA_TXDESC_CHECKSUM(DMATxDesc_Checksum));
  1290. /* Set the selected DMA Tx desc checksum insertion control */
  1291. DMATxDesc->Status |= DMATxDesc_Checksum;
  1292. }
  1293. /**
  1294. * @brief Enables or disables the DMA Tx Desc CRC.
  1295. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1296. * @param NewState: new state of the specified DMA Tx Desc CRC.
  1297. * This parameter can be: ENABLE or DISABLE.
  1298. * @retval None
  1299. */
  1300. void ETH_DMATxDescCRCCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1301. {
  1302. /* Check the parameters */
  1303. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1304. if (NewState != DISABLE)
  1305. {
  1306. /* Enable the selected DMA Tx Desc CRC */
  1307. DMATxDesc->Status &= (~(uint32_t)ETH_DMATxDesc_DC);
  1308. }
  1309. else
  1310. {
  1311. /* Disable the selected DMA Tx Desc CRC */
  1312. DMATxDesc->Status |= ETH_DMATxDesc_DC;
  1313. }
  1314. }
  1315. /**
  1316. * @brief Enables or disables the DMA Tx Desc end of ring.
  1317. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1318. * @param NewState: new state of the specified DMA Tx Desc end of ring.
  1319. * This parameter can be: ENABLE or DISABLE.
  1320. * @retval None
  1321. */
  1322. void ETH_DMATxDescEndOfRingCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1323. {
  1324. /* Check the parameters */
  1325. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1326. if (NewState != DISABLE)
  1327. {
  1328. /* Enable the selected DMA Tx Desc end of ring */
  1329. DMATxDesc->Status |= ETH_DMATxDesc_TER;
  1330. }
  1331. else
  1332. {
  1333. /* Disable the selected DMA Tx Desc end of ring */
  1334. DMATxDesc->Status &= (~(uint32_t)ETH_DMATxDesc_TER);
  1335. }
  1336. }
  1337. /**
  1338. * @brief Enables or disables the DMA Tx Desc second address chained.
  1339. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1340. * @param NewState: new state of the specified DMA Tx Desc second address chained.
  1341. * This parameter can be: ENABLE or DISABLE.
  1342. * @retval None
  1343. */
  1344. void ETH_DMATxDescSecondAddressChainedCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1345. {
  1346. /* Check the parameters */
  1347. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1348. if (NewState != DISABLE)
  1349. {
  1350. /* Enable the selected DMA Tx Desc second address chained */
  1351. DMATxDesc->Status |= ETH_DMATxDesc_TCH;
  1352. }
  1353. else
  1354. {
  1355. /* Disable the selected DMA Tx Desc second address chained */
  1356. DMATxDesc->Status &=(~(uint32_t)ETH_DMATxDesc_TCH);
  1357. }
  1358. }
  1359. /**
  1360. * @brief Enables or disables the DMA Tx Desc padding for frame shorter than 64 bytes.
  1361. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1362. * @param NewState: new state of the specified DMA Tx Desc padding for frame shorter than 64 bytes.
  1363. * This parameter can be: ENABLE or DISABLE.
  1364. * @retval None
  1365. */
  1366. void ETH_DMATxDescShortFramePaddingCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1367. {
  1368. /* Check the parameters */
  1369. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1370. if (NewState != DISABLE)
  1371. {
  1372. /* Enable the selected DMA Tx Desc padding for frame shorter than 64 bytes */
  1373. DMATxDesc->Status &= (~(uint32_t)ETH_DMATxDesc_DP);
  1374. }
  1375. else
  1376. {
  1377. /* Disable the selected DMA Tx Desc padding for frame shorter than 64 bytes*/
  1378. DMATxDesc->Status |= ETH_DMATxDesc_DP;
  1379. }
  1380. }
  1381. /**
  1382. * @brief Enables or disables the DMA Tx Desc time stamp.
  1383. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1384. * @param NewState: new state of the specified DMA Tx Desc time stamp.
  1385. * This parameter can be: ENABLE or DISABLE.
  1386. * @retval None
  1387. */
  1388. void ETH_DMATxDescTimeStampCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1389. {
  1390. /* Check the parameters */
  1391. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1392. if (NewState != DISABLE)
  1393. {
  1394. /* Enable the selected DMA Tx Desc time stamp */
  1395. DMATxDesc->Status |= ETH_DMATxDesc_TTSE;
  1396. }
  1397. else
  1398. {
  1399. /* Disable the selected DMA Tx Desc time stamp */
  1400. DMATxDesc->Status &=(~(uint32_t)ETH_DMATxDesc_TTSE);
  1401. }
  1402. }
  1403. /**
  1404. * @brief configures the specified DMA Tx Desc buffer1 and buffer2 sizes.
  1405. * @param DMATxDesc: Pointer on a Tx desc
  1406. * @param BufferSize1: specifies the Tx desc buffer1 size.
  1407. * @param BufferSize2: specifies the Tx desc buffer2 size (put "0" if not used).
  1408. * @retval None
  1409. */
  1410. void ETH_DMATxDescBufferSizeConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t BufferSize1, uint32_t BufferSize2)
  1411. {
  1412. /* Check the parameters */
  1413. assert_param(IS_ETH_DMATxDESC_BUFFER_SIZE(BufferSize1));
  1414. assert_param(IS_ETH_DMATxDESC_BUFFER_SIZE(BufferSize2));
  1415. /* Set the DMA Tx Desc buffer1 and buffer2 sizes values */
  1416. DMATxDesc->ControlBufferSize |= (BufferSize1 | (BufferSize2 << ETH_DMATXDESC_BUFFER2_SIZESHIFT));
  1417. }
  1418. /**
  1419. * @brief Initializes the DMA Rx descriptors in chain mode.
  1420. * @param DMARxDescTab: Pointer on the first Rx desc list
  1421. * @param RxBuff: Pointer on the first RxBuffer list
  1422. * @param RxBuffCount: Number of the used Rx desc in the list
  1423. * @retval None
  1424. */
  1425. void ETH_DMARxDescChainInit(ETH_DMADESCTypeDef *DMARxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
  1426. {
  1427. uint32_t i = 0;
  1428. ETH_DMADESCTypeDef *DMARxDesc;
  1429. /* Set the DMARxDescToGet pointer with the first one of the DMARxDescTab list */
  1430. DMARxDescToGet = DMARxDescTab;
  1431. /* Fill each DMARxDesc descriptor with the right values */
  1432. for(i=0; i < RxBuffCount; i++)
  1433. {
  1434. /* Get the pointer on the ith member of the Rx Desc list */
  1435. DMARxDesc = DMARxDescTab+i;
  1436. /* Set Own bit of the Rx descriptor Status */
  1437. DMARxDesc->Status = ETH_DMARxDesc_OWN;
  1438. /* Set Buffer1 size and Second Address Chained bit */
  1439. DMARxDesc->ControlBufferSize = ETH_DMARxDesc_RCH | (uint32_t)ETH_MAX_PACKET_SIZE;
  1440. /* Set Buffer1 address pointer */
  1441. DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*ETH_MAX_PACKET_SIZE]);
  1442. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  1443. if(i < (RxBuffCount-1))
  1444. {
  1445. /* Set next descriptor address register with next descriptor base address */
  1446. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab+i+1);
  1447. }
  1448. else
  1449. {
  1450. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1451. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
  1452. }
  1453. }
  1454. /* Set Receive Desciptor List Address Register */
  1455. ETH->DMARDLAR = (uint32_t) DMARxDescTab;
  1456. }
  1457. /**
  1458. * @brief Initializes the DMA Rx descriptors in ring mode.
  1459. * @param DMARxDescTab: Pointer on the first Rx desc list
  1460. * @param RxBuff1: Pointer on the first RxBuffer1 list
  1461. * @param RxBuff2: Pointer on the first RxBuffer2 list
  1462. * @param RxBuffCount: Number of the used Rx desc in the list
  1463. * Note: see decriptor skip length defined in ETH_DMA_InitStruct
  1464. * for the number of Words to skip between two unchained descriptors.
  1465. * @retval None
  1466. */
  1467. void ETH_DMARxDescRingInit(ETH_DMADESCTypeDef *DMARxDescTab, uint8_t *RxBuff1, uint8_t *RxBuff2, uint32_t RxBuffCount)
  1468. {
  1469. uint32_t i = 0;
  1470. ETH_DMADESCTypeDef *DMARxDesc;
  1471. /* Set the DMARxDescToGet pointer with the first one of the DMARxDescTab list */
  1472. DMARxDescToGet = DMARxDescTab;
  1473. /* Fill each DMARxDesc descriptor with the right values */
  1474. for(i=0; i < RxBuffCount; i++)
  1475. {
  1476. /* Get the pointer on the ith member of the Rx Desc list */
  1477. DMARxDesc = DMARxDescTab+i;
  1478. /* Set Own bit of the Rx descriptor Status */
  1479. DMARxDesc->Status = ETH_DMARxDesc_OWN;
  1480. /* Set Buffer1 size */
  1481. DMARxDesc->ControlBufferSize = ETH_MAX_PACKET_SIZE;
  1482. /* Set Buffer1 address pointer */
  1483. DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff1[i*ETH_MAX_PACKET_SIZE]);
  1484. /* Set Buffer2 address pointer */
  1485. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(&RxBuff2[i*ETH_MAX_PACKET_SIZE]);
  1486. /* Set Receive End of Ring bit for last descriptor: The DMA returns to the base
  1487. address of the list, creating a Desciptor Ring */
  1488. if(i == (RxBuffCount-1))
  1489. {
  1490. /* Set Receive End of Ring bit */
  1491. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_RER;
  1492. }
  1493. }
  1494. /* Set Receive Desciptor List Address Register */
  1495. ETH->DMARDLAR = (uint32_t) DMARxDescTab;
  1496. }
  1497. /**
  1498. * @brief Checks whether the specified ETHERNET Rx Desc flag is set or not.
  1499. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1500. * @param ETH_DMARxDescFlag: specifies the flag to check.
  1501. * This parameter can be one of the following values:
  1502. * @arg ETH_DMARxDesc_OWN: OWN bit: descriptor is owned by DMA engine
  1503. * @arg ETH_DMARxDesc_AFM: DA Filter Fail for the rx frame
  1504. * @arg ETH_DMARxDesc_ES: Error summary
  1505. * @arg ETH_DMARxDesc_DE: Desciptor error: no more descriptors for receive frame
  1506. * @arg ETH_DMARxDesc_SAF: SA Filter Fail for the received frame
  1507. * @arg ETH_DMARxDesc_LE: Frame size not matching with length field
  1508. * @arg ETH_DMARxDesc_OE: Overflow Error: Frame was damaged due to buffer overflow
  1509. * @arg ETH_DMARxDesc_VLAN: VLAN Tag: received frame is a VLAN frame
  1510. * @arg ETH_DMARxDesc_FS: First descriptor of the frame
  1511. * @arg ETH_DMARxDesc_LS: Last descriptor of the frame
  1512. * @arg ETH_DMARxDesc_IPV4HCE: IPC Checksum Error/Giant Frame: Rx Ipv4 header checksum error
  1513. * @arg ETH_DMARxDesc_LC: Late collision occurred during reception
  1514. * @arg ETH_DMARxDesc_FT: Frame type - Ethernet, otherwise 802.3
  1515. * @arg ETH_DMARxDesc_RWT: Receive Watchdog Timeout: watchdog timer expired during reception
  1516. * @arg ETH_DMARxDesc_RE: Receive error: error reported by MII interface
  1517. * @arg ETH_DMARxDesc_DE: Dribble bit error: frame contains non int multiple of 8 bits
  1518. * @arg ETH_DMARxDesc_CE: CRC error
  1519. * @arg ETH_DMARxDesc_MAMPCE: Rx MAC Address/Payload Checksum Error: Rx MAC address matched/ Rx Payload Checksum Error
  1520. * @retval The new state of ETH_DMARxDescFlag (SET or RESET).
  1521. */
  1522. FlagStatus ETH_GetDMARxDescFlagStatus(ETH_DMADESCTypeDef *DMARxDesc, uint32_t ETH_DMARxDescFlag)
  1523. {
  1524. FlagStatus bitstatus = RESET;
  1525. /* Check the parameters */
  1526. assert_param(IS_ETH_DMARxDESC_GET_FLAG(ETH_DMARxDescFlag));
  1527. if ((DMARxDesc->Status & ETH_DMARxDescFlag) != (uint32_t)RESET)
  1528. {
  1529. bitstatus = SET;
  1530. }
  1531. else
  1532. {
  1533. bitstatus = RESET;
  1534. }
  1535. return bitstatus;
  1536. }
  1537. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  1538. /**
  1539. * @brief Checks whether the specified ETHERNET PTP Rx Desc extended flag is set or not.
  1540. * @param DMAPTPRxDesc: pointer on a DMA PTP Rx descriptor
  1541. * @param ETH_DMAPTPRxDescFlag: specifies the extended flag to check.
  1542. * This parameter can be one of the following values:
  1543. * @arg ETH_DMAPTPRxDesc_PTPV: PTP version
  1544. * @arg ETH_DMAPTPRxDesc_PTPFT: PTP frame type
  1545. * @arg ETH_DMAPTPRxDesc_PTPMT: PTP message type
  1546. * @arg ETH_DMAPTPRxDesc_IPV6PR: IPv6 packet received
  1547. * @arg ETH_DMAPTPRxDesc_IPV4PR: IPv4 packet received
  1548. * @arg ETH_DMAPTPRxDesc_IPCB: IP checksum bypassed
  1549. * @arg ETH_DMAPTPRxDesc_IPPE: IP payload error
  1550. * @arg ETH_DMAPTPRxDesc_IPHE: IP header error
  1551. * @arg ETH_DMAPTPRxDesc_IPPT: IP payload type
  1552. * @retval The new state of ETH_DMAPTPRxDescExtendedFlag (SET or RESET).
  1553. */
  1554. FlagStatus ETH_GetDMAPTPRxDescExtendedFlagStatus(ETH_DMADESCTypeDef *DMAPTPRxDesc, uint32_t ETH_DMAPTPRxDescExtendedFlag)
  1555. {
  1556. FlagStatus bitstatus = RESET;
  1557. /* Check the parameters */
  1558. assert_param(IS_ETH_DMAPTPRxDESC_GET_EXTENDED_FLAG(ETH_DMAPTPRxDescExtendedFlag));
  1559. if ((DMAPTPRxDesc->ExtendedStatus & ETH_DMAPTPRxDescExtendedFlag) != (uint32_t)RESET)
  1560. {
  1561. bitstatus = SET;
  1562. }
  1563. else
  1564. {
  1565. bitstatus = RESET;
  1566. }
  1567. return bitstatus;
  1568. }
  1569. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  1570. /**
  1571. * @brief Set the specified DMA Rx Desc Own bit.
  1572. * @param DMARxDesc: Pointer on a Rx desc
  1573. * @retval None
  1574. */
  1575. void ETH_SetDMARxDescOwnBit(ETH_DMADESCTypeDef *DMARxDesc)
  1576. {
  1577. /* Set the DMA Rx Desc Own bit */
  1578. DMARxDesc->Status |= ETH_DMARxDesc_OWN;
  1579. }
  1580. /**
  1581. * @brief Returns the specified DMA Rx Desc frame length.
  1582. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1583. * @retval The Rx descriptor received frame length.
  1584. */
  1585. uint32_t ETH_GetDMARxDescFrameLength(ETH_DMADESCTypeDef *DMARxDesc)
  1586. {
  1587. /* Return the Receive descriptor frame length */
  1588. return ((DMARxDesc->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT);
  1589. }
  1590. /**
  1591. * @brief Enables or disables the specified DMA Rx Desc receive interrupt.
  1592. * @param DMARxDesc: Pointer on a Rx desc
  1593. * @param NewState: new state of the specified DMA Rx Desc interrupt.
  1594. * This parameter can be: ENABLE or DISABLE.
  1595. * @retval None
  1596. */
  1597. void ETH_DMARxDescReceiveITConfig(ETH_DMADESCTypeDef *DMARxDesc, FunctionalState NewState)
  1598. {
  1599. /* Check the parameters */
  1600. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1601. if (NewState != DISABLE)
  1602. {
  1603. /* Enable the DMA Rx Desc receive interrupt */
  1604. DMARxDesc->ControlBufferSize &=(~(uint32_t)ETH_DMARxDesc_DIC);
  1605. }
  1606. else
  1607. {
  1608. /* Disable the DMA Rx Desc receive interrupt */
  1609. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_DIC;
  1610. }
  1611. }
  1612. /**
  1613. * @brief Enables or disables the DMA Rx Desc end of ring.
  1614. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1615. * @param NewState: new state of the specified DMA Rx Desc end of ring.
  1616. * This parameter can be: ENABLE or DISABLE.
  1617. * @retval None
  1618. */
  1619. void ETH_DMARxDescEndOfRingCmd(ETH_DMADESCTypeDef *DMARxDesc, FunctionalState NewState)
  1620. {
  1621. /* Check the parameters */
  1622. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1623. if (NewState != DISABLE)
  1624. {
  1625. /* Enable the selected DMA Rx Desc end of ring */
  1626. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_RER;
  1627. }
  1628. else
  1629. {
  1630. /* Disable the selected DMA Rx Desc end of ring */
  1631. DMARxDesc->ControlBufferSize &=(~(uint32_t)ETH_DMARxDesc_RER);
  1632. }
  1633. }
  1634. /**
  1635. * @brief Enables or disables the DMA Rx Desc second address chained.
  1636. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1637. * @param NewState: new state of the specified DMA Rx Desc second address chained.
  1638. * This parameter can be: ENABLE or DISABLE.
  1639. * @retval None
  1640. */
  1641. void ETH_DMARxDescSecondAddressChainedCmd(ETH_DMADESCTypeDef *DMARxDesc, FunctionalState NewState)
  1642. {
  1643. /* Check the parameters */
  1644. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1645. if (NewState != DISABLE)
  1646. {
  1647. /* Enable the selected DMA Rx Desc second address chained */
  1648. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_RCH;
  1649. }
  1650. else
  1651. {
  1652. /* Disable the selected DMA Rx Desc second address chained */
  1653. DMARxDesc->ControlBufferSize &=(~(uint32_t)ETH_DMARxDesc_RCH);
  1654. }
  1655. }
  1656. /**
  1657. * @brief Returns the specified ETHERNET DMA Rx Desc buffer size.
  1658. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1659. * @param DMARxDesc_Buffer: specifies the DMA Rx Desc buffer.
  1660. * This parameter can be any one of the following values:
  1661. * @arg ETH_DMARxDesc_Buffer1 : DMA Rx Desc Buffer1
  1662. * @arg ETH_DMARxDesc_Buffer2 : DMA Rx Desc Buffer2
  1663. * @retval The Receive descriptor frame length.
  1664. */
  1665. uint32_t ETH_GetDMARxDescBufferSize(ETH_DMADESCTypeDef *DMARxDesc, uint32_t DMARxDesc_Buffer)
  1666. {
  1667. /* Check the parameters */
  1668. assert_param(IS_ETH_DMA_RXDESC_BUFFER(DMARxDesc_Buffer));
  1669. if(DMARxDesc_Buffer != ETH_DMARxDesc_Buffer1)
  1670. {
  1671. /* Return the DMA Rx Desc buffer2 size */
  1672. return ((DMARxDesc->ControlBufferSize & ETH_DMARxDesc_RBS2) >> ETH_DMARXDESC_BUFFER2_SIZESHIFT);
  1673. }
  1674. else
  1675. {
  1676. /* Return the DMA Rx Desc buffer1 size */
  1677. return (DMARxDesc->ControlBufferSize & ETH_DMARxDesc_RBS1);
  1678. }
  1679. }
  1680. /*--------------------------------- DMA ------------------------------------*/
  1681. /**
  1682. * @brief Resets all MAC subsystem internal registers and logic.
  1683. * @param None
  1684. * @retval None
  1685. */
  1686. void ETH_SoftwareReset(void)
  1687. {
  1688. /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
  1689. /* After reset all the registers holds their respective reset values */
  1690. ETH->DMABMR |= ETH_DMABMR_SR;
  1691. }
  1692. /**
  1693. * @brief Checks whether the ETHERNET software reset bit is set or not.
  1694. * @param None
  1695. * @retval The new state of DMA Bus Mode register SR bit (SET or RESET).
  1696. */
  1697. FlagStatus ETH_GetSoftwareResetStatus(void)
  1698. {
  1699. FlagStatus bitstatus = RESET;
  1700. if((ETH->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)
  1701. {
  1702. bitstatus = SET;
  1703. }
  1704. else
  1705. {
  1706. bitstatus = RESET;
  1707. }
  1708. return bitstatus;
  1709. }
  1710. /**
  1711. * @brief Checks whether the specified ETHERNET DMA flag is set or not.
  1712. * @param ETH_DMA_FLAG: specifies the flag to check.
  1713. * This parameter can be one of the following values:
  1714. * @arg ETH_DMA_FLAG_TST : Time-stamp trigger flag
  1715. * @arg ETH_DMA_FLAG_PMT : PMT flag
  1716. * @arg ETH_DMA_FLAG_MMC : MMC flag
  1717. * @arg ETH_DMA_FLAG_DataTransferError : Error bits 0-data buffer, 1-desc. access
  1718. * @arg ETH_DMA_FLAG_ReadWriteError : Error bits 0-write trnsf, 1-read transfr
  1719. * @arg ETH_DMA_FLAG_AccessError : Error bits 0-Rx DMA, 1-Tx DMA
  1720. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1721. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1722. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1723. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1724. * @arg ETH_DMA_FLAG_ET : Early transmit flag
  1725. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1726. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1727. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1728. * @arg ETH_DMA_FLAG_R : Receive flag
  1729. * @arg ETH_DMA_FLAG_TU : Underflow flag
  1730. * @arg ETH_DMA_FLAG_RO : Overflow flag
  1731. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1732. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1733. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1734. * @arg ETH_DMA_FLAG_T : Transmit flag
  1735. * @retval The new state of ETH_DMA_FLAG (SET or RESET).
  1736. */
  1737. FlagStatus ETH_GetDMAFlagStatus(uint32_t ETH_DMA_FLAG)
  1738. {
  1739. FlagStatus bitstatus = RESET;
  1740. /* Check the parameters */
  1741. assert_param(IS_ETH_DMA_GET_IT(ETH_DMA_FLAG));
  1742. if ((ETH->DMASR & ETH_DMA_FLAG) != (uint32_t)RESET)
  1743. {
  1744. bitstatus = SET;
  1745. }
  1746. else
  1747. {
  1748. bitstatus = RESET;
  1749. }
  1750. return bitstatus;
  1751. }
  1752. /**
  1753. * @brief Clears the ETHERNET¡¯s DMA pending flag.
  1754. * @param ETH_DMA_FLAG: specifies the flag to clear.
  1755. * This parameter can be any combination of the following values:
  1756. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1757. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1758. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1759. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1760. * @arg ETH_DMA_FLAG_ETI : Early transmit flag
  1761. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1762. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1763. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1764. * @arg ETH_DMA_FLAG_R : Receive flag
  1765. * @arg ETH_DMA_FLAG_TU : Transmit Underflow flag
  1766. * @arg ETH_DMA_FLAG_RO : Receive Overflow flag
  1767. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1768. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1769. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1770. * @arg ETH_DMA_FLAG_T : Transmit flag
  1771. * @retval None
  1772. */
  1773. void ETH_DMAClearFlag(uint32_t ETH_DMA_FLAG)
  1774. {
  1775. /* Check the parameters */
  1776. assert_param(IS_ETH_DMA_FLAG(ETH_DMA_FLAG));
  1777. /* Clear the selected ETHERNET DMA FLAG */
  1778. ETH->DMASR = (uint32_t) ETH_DMA_FLAG;
  1779. }
  1780. /**
  1781. * @brief Checks whether the specified ETHERNET DMA interrupt has occured or not.
  1782. * @param ETH_DMA_IT: specifies the interrupt source to check.
  1783. * This parameter can be one of the following values:
  1784. * @arg ETH_DMA_IT_TST : Time-stamp trigger interrupt
  1785. * @arg ETH_DMA_IT_PMT : PMT interrupt
  1786. * @arg ETH_DMA_IT_MMC : MMC interrupt
  1787. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1788. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1789. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1790. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1791. * @arg ETH_DMA_IT_ET : Early transmit interrupt
  1792. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1793. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1794. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1795. * @arg ETH_DMA_IT_R : Receive interrupt
  1796. * @arg ETH_DMA_IT_TU : Underflow interrupt
  1797. * @arg ETH_DMA_IT_RO : Overflow interrupt
  1798. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1799. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1800. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1801. * @arg ETH_DMA_IT_T : Transmit interrupt
  1802. * @retval The new state of ETH_DMA_IT (SET or RESET).
  1803. */
  1804. ITStatus ETH_GetDMAITStatus(uint32_t ETH_DMA_IT)
  1805. {
  1806. ITStatus bitstatus = RESET;
  1807. /* Check the parameters */
  1808. assert_param(IS_ETH_DMA_GET_IT(ETH_DMA_IT));
  1809. if ((ETH->DMASR & ETH_DMA_IT) != (uint32_t)RESET)
  1810. {
  1811. bitstatus = SET;
  1812. }
  1813. else
  1814. {
  1815. bitstatus = RESET;
  1816. }
  1817. return bitstatus;
  1818. }
  1819. /**
  1820. * @brief Clears the ETHERNET¡¯s DMA IT pending bit.
  1821. * @param ETH_DMA_IT: specifies the interrupt pending bit to clear.
  1822. * This parameter can be any combination of the following values:
  1823. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1824. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1825. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1826. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1827. * @arg ETH_DMA_IT_ETI : Early transmit interrupt
  1828. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1829. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1830. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1831. * @arg ETH_DMA_IT_R : Receive interrupt
  1832. * @arg ETH_DMA_IT_TU : Transmit Underflow interrupt
  1833. * @arg ETH_DMA_IT_RO : Receive Overflow interrupt
  1834. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1835. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1836. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1837. * @arg ETH_DMA_IT_T : Transmit interrupt
  1838. * @retval None
  1839. */
  1840. void ETH_DMAClearITPendingBit(uint32_t ETH_DMA_IT)
  1841. {
  1842. /* Check the parameters */
  1843. assert_param(IS_ETH_DMA_IT(ETH_DMA_IT));
  1844. /* Clear the selected ETHERNET DMA IT */
  1845. ETH->DMASR = (uint32_t) ETH_DMA_IT;
  1846. }
  1847. /**
  1848. * @brief Returns the ETHERNET DMA Transmit Process State.
  1849. * @param None
  1850. * @retval The new ETHERNET DMA Transmit Process State:
  1851. * This can be one of the following values:
  1852. * - ETH_DMA_TransmitProcess_Stopped : Stopped - Reset or Stop Tx Command issued
  1853. * - ETH_DMA_TransmitProcess_Fetching : Running - fetching the Tx descriptor
  1854. * - ETH_DMA_TransmitProcess_Waiting : Running - waiting for status
  1855. * - ETH_DMA_TransmitProcess_Reading : unning - reading the data from host memory
  1856. * - ETH_DMA_TransmitProcess_Suspended : Suspended - Tx Desciptor unavailabe
  1857. * - ETH_DMA_TransmitProcess_Closing : Running - closing Rx descriptor
  1858. */
  1859. uint32_t ETH_GetTransmitProcessState(void)
  1860. {
  1861. return ((uint32_t)(ETH->DMASR & ETH_DMASR_TS));
  1862. }
  1863. /**
  1864. * @brief Returns the ETHERNET DMA Receive Process State.
  1865. * @param None
  1866. * @retval The new ETHERNET DMA Receive Process State:
  1867. * This can be one of the following values:
  1868. * - ETH_DMA_ReceiveProcess_Stopped : Stopped - Reset or Stop Rx Command issued
  1869. * - ETH_DMA_ReceiveProcess_Fetching : Running - fetching the Rx descriptor
  1870. * - ETH_DMA_ReceiveProcess_Waiting : Running - waiting for packet
  1871. * - ETH_DMA_ReceiveProcess_Suspended : Suspended - Rx Desciptor unavailable
  1872. * - ETH_DMA_ReceiveProcess_Closing : Running - closing descriptor
  1873. * - ETH_DMA_ReceiveProcess_Queuing : Running - queuing the recieve frame into host memory
  1874. */
  1875. uint32_t ETH_GetReceiveProcessState(void)
  1876. {
  1877. return ((uint32_t)(ETH->DMASR & ETH_DMASR_RS));
  1878. }
  1879. /**
  1880. * @brief Clears the ETHERNET transmit FIFO.
  1881. * @param None
  1882. * @retval None
  1883. */
  1884. void ETH_FlushTransmitFIFO(void)
  1885. {
  1886. /* Set the Flush Transmit FIFO bit */
  1887. ETH->DMAOMR |= ETH_DMAOMR_FTF;
  1888. }
  1889. /**
  1890. * @brief Checks whether the ETHERNET transmit FIFO bit is cleared or not.
  1891. * @param None
  1892. * @retval The new state of ETHERNET flush transmit FIFO bit (SET or RESET).
  1893. */
  1894. FlagStatus ETH_GetFlushTransmitFIFOStatus(void)
  1895. {
  1896. FlagStatus bitstatus = RESET;
  1897. if ((ETH->DMAOMR & ETH_DMAOMR_FTF) != (uint32_t)RESET)
  1898. {
  1899. bitstatus = SET;
  1900. }
  1901. else
  1902. {
  1903. bitstatus = RESET;
  1904. }
  1905. return bitstatus;
  1906. }
  1907. /**
  1908. * @brief Enables or disables the DMA transmission.
  1909. * @param NewState: new state of the DMA transmission.
  1910. * This parameter can be: ENABLE or DISABLE.
  1911. * @retval None
  1912. */
  1913. void ETH_DMATransmissionCmd(FunctionalState NewState)
  1914. {
  1915. /* Check the parameters */
  1916. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1917. if (NewState != DISABLE)
  1918. {
  1919. /* Enable the DMA transmission */
  1920. ETH->DMAOMR |= ETH_DMAOMR_ST;
  1921. }
  1922. else
  1923. {
  1924. /* Disable the DMA transmission */
  1925. ETH->DMAOMR &= ~ETH_DMAOMR_ST;
  1926. }
  1927. }
  1928. /**
  1929. * @brief Enables or disables the DMA reception.
  1930. * @param NewState: new state of the DMA reception.
  1931. * This parameter can be: ENABLE or DISABLE.
  1932. * @retval None
  1933. */
  1934. void ETH_DMAReceptionCmd(FunctionalState NewState)
  1935. {
  1936. /* Check the parameters */
  1937. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1938. if (NewState != DISABLE)
  1939. {
  1940. /* Enable the DMA reception */
  1941. ETH->DMAOMR |= ETH_DMAOMR_SR;
  1942. }
  1943. else
  1944. {
  1945. /* Disable the DMA reception */
  1946. ETH->DMAOMR &= ~ETH_DMAOMR_SR;
  1947. }
  1948. }
  1949. /**
  1950. * @brief Enables or disables the specified ETHERNET DMA interrupts.
  1951. * @param ETH_DMA_IT: specifies the ETHERNET DMA interrupt sources to be
  1952. * enabled or disabled.
  1953. * This parameter can be any combination of the following values:
  1954. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1955. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1956. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1957. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1958. * @arg ETH_DMA_IT_ET : Early transmit interrupt
  1959. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1960. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1961. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1962. * @arg ETH_DMA_IT_R : Receive interrupt
  1963. * @arg ETH_DMA_IT_TU : Underflow interrupt
  1964. * @arg ETH_DMA_IT_RO : Overflow interrupt
  1965. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1966. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1967. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1968. * @arg ETH_DMA_IT_T : Transmit interrupt
  1969. * @param NewState: new state of the specified ETHERNET DMA interrupts.
  1970. * This parameter can be: ENABLE or DISABLE.
  1971. * @retval None
  1972. */
  1973. void ETH_DMAITConfig(uint32_t ETH_DMA_IT, FunctionalState NewState)
  1974. {
  1975. /* Check the parameters */
  1976. assert_param(IS_ETH_DMA_IT(ETH_DMA_IT));
  1977. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1978. if (NewState != DISABLE)
  1979. {
  1980. /* Enable the selected ETHERNET DMA interrupts */
  1981. ETH->DMAIER |= ETH_DMA_IT;
  1982. }
  1983. else
  1984. {
  1985. /* Disable the selected ETHERNET DMA interrupts */
  1986. ETH->DMAIER &=(~(uint32_t)ETH_DMA_IT);
  1987. }
  1988. }
  1989. /**
  1990. * @brief Checks whether the specified ETHERNET DMA overflow flag is set or not.
  1991. * @param ETH_DMA_Overflow: specifies the DMA overflow flag to check.
  1992. * This parameter can be one of the following values:
  1993. * @arg ETH_DMA_Overflow_RxFIFOCounter : Overflow for FIFO Overflow Counter
  1994. * @arg ETH_DMA_Overflow_MissedFrameCounter : Overflow for Missed Frame Counter
  1995. * @retval The new state of ETHERNET DMA overflow Flag (SET or RESET).
  1996. */
  1997. FlagStatus ETH_GetDMAOverflowStatus(uint32_t ETH_DMA_Overflow)
  1998. {
  1999. FlagStatus bitstatus = RESET;
  2000. /* Check the parameters */
  2001. assert_param(IS_ETH_DMA_GET_OVERFLOW(ETH_DMA_Overflow));
  2002. if ((ETH->DMAMFBOCR & ETH_DMA_Overflow) != (uint32_t)RESET)
  2003. {
  2004. bitstatus = SET;
  2005. }
  2006. else
  2007. {
  2008. bitstatus = RESET;
  2009. }
  2010. return bitstatus;
  2011. }
  2012. /**
  2013. * @brief Get the ETHERNET DMA Rx Overflow Missed Frame Counter value.
  2014. * @param None
  2015. * @retval The value of Rx overflow Missed Frame Counter.
  2016. */
  2017. uint32_t ETH_GetRxOverflowMissedFrameCounter(void)
  2018. {
  2019. return ((uint32_t)((ETH->DMAMFBOCR & ETH_DMAMFBOCR_MFA)>>ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTERSHIFT));
  2020. }
  2021. /**
  2022. * @brief Get the ETHERNET DMA Buffer Unavailable Missed Frame Counter value.
  2023. * @param None
  2024. * @retval The value of Buffer unavailable Missed Frame Counter.
  2025. */
  2026. uint32_t ETH_GetBufferUnavailableMissedFrameCounter(void)
  2027. {
  2028. return ((uint32_t)(ETH->DMAMFBOCR) & ETH_DMAMFBOCR_MFC);
  2029. }
  2030. /**
  2031. * @brief Get the ETHERNET DMA DMACHTDR register value.
  2032. * @param None
  2033. * @retval The value of the current Tx desc start address.
  2034. */
  2035. uint32_t ETH_GetCurrentTxDescStartAddress(void)
  2036. {
  2037. return ((uint32_t)(ETH->DMACHTDR));
  2038. }
  2039. /**
  2040. * @brief Get the ETHERNET DMA DMACHRDR register value.
  2041. * @param None
  2042. * @retval The value of the current Rx desc start address.
  2043. */
  2044. uint32_t ETH_GetCurrentRxDescStartAddress(void)
  2045. {
  2046. return ((uint32_t)(ETH->DMACHRDR));
  2047. }
  2048. /**
  2049. * @brief Get the ETHERNET DMA DMACHTBAR register value.
  2050. * @param None
  2051. * @retval The value of the current Tx buffer address.
  2052. */
  2053. uint32_t ETH_GetCurrentTxBufferAddress(void)
  2054. {
  2055. return ((uint32_t)(ETH->DMACHTBAR));
  2056. }
  2057. /**
  2058. * @brief Get the ETHERNET DMA DMACHRBAR register value.
  2059. * @param None
  2060. * @retval The value of the current Rx buffer address.
  2061. */
  2062. uint32_t ETH_GetCurrentRxBufferAddress(void)
  2063. {
  2064. return ((uint32_t)(ETH->DMACHRBAR));
  2065. }
  2066. /**
  2067. * @brief Resumes the DMA Transmission by writing to the DmaTxPollDemand register
  2068. * (the data written could be anything). This forces the DMA to resume transmission.
  2069. * @param None
  2070. * @retval None.
  2071. */
  2072. void ETH_ResumeDMATransmission(void)
  2073. {
  2074. ETH->DMATPDR = 0;
  2075. }
  2076. /**
  2077. * @brief Resumes the DMA Transmission by writing to the DmaRxPollDemand register
  2078. * (the data written could be anything). This forces the DMA to resume reception.
  2079. * @param None
  2080. * @retval None.
  2081. */
  2082. void ETH_ResumeDMAReception(void)
  2083. {
  2084. ETH->DMARPDR = 0;
  2085. }
  2086. /**
  2087. * @brief Set the DMA Receive status watchdog timer register value
  2088. * @param Value: DMA Receive status watchdog timer register value
  2089. * @retval None
  2090. */
  2091. void ETH_SetReceiveWatchdogTimer(uint8_t Value)
  2092. {
  2093. /* Set the DMA Receive status watchdog timer register */
  2094. ETH->DMARSWTR = Value;
  2095. }
  2096. /*--------------------------------- PMT ------------------------------------*/
  2097. /**
  2098. * @brief Reset Wakeup frame filter register pointer.
  2099. * @param None
  2100. * @retval None
  2101. */
  2102. void ETH_ResetWakeUpFrameFilterRegisterPointer(void)
  2103. {
  2104. /* Resets the Remote Wake-up Frame Filter register pointer to 0x0000 */
  2105. ETH->MACPMTCSR |= ETH_MACPMTCSR_WFFRPR;
  2106. }
  2107. /**
  2108. * @brief Populates the remote wakeup frame registers.
  2109. * @param Buffer: Pointer on remote WakeUp Frame Filter Register buffer data (8 words).
  2110. * @retval None
  2111. */
  2112. void ETH_SetWakeUpFrameFilterRegister(uint32_t *Buffer)
  2113. {
  2114. uint32_t i = 0;
  2115. /* Fill Remote Wake-up Frame Filter register with Buffer data */
  2116. for(i =0; i<ETH_WAKEUP_REGISTER_LENGTH; i++)
  2117. {
  2118. /* Write each time to the same register */
  2119. ETH->MACRWUFFR = Buffer[i];
  2120. }
  2121. }
  2122. /**
  2123. * @brief Enables or disables any unicast packet filtered by the MAC address
  2124. * recognition to be a wake-up frame.
  2125. * @param NewState: new state of the MAC Global Unicast Wake-Up.
  2126. * This parameter can be: ENABLE or DISABLE.
  2127. * @retval None
  2128. */
  2129. void ETH_GlobalUnicastWakeUpCmd(FunctionalState NewState)
  2130. {
  2131. /* Check the parameters */
  2132. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2133. if (NewState != DISABLE)
  2134. {
  2135. /* Enable the MAC Global Unicast Wake-Up */
  2136. ETH->MACPMTCSR |= ETH_MACPMTCSR_GU;
  2137. }
  2138. else
  2139. {
  2140. /* Disable the MAC Global Unicast Wake-Up */
  2141. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_GU;
  2142. }
  2143. }
  2144. /**
  2145. * @brief Checks whether the specified ETHERNET PMT flag is set or not.
  2146. * @param ETH_PMT_FLAG: specifies the flag to check.
  2147. * This parameter can be one of the following values:
  2148. * @arg ETH_PMT_FLAG_WUFFRPR : Wake-Up Frame Filter Register Poniter Reset
  2149. * @arg ETH_PMT_FLAG_WUFR : Wake-Up Frame Received
  2150. * @arg ETH_PMT_FLAG_MPR : Magic Packet Received
  2151. * @retval The new state of ETHERNET PMT Flag (SET or RESET).
  2152. */
  2153. FlagStatus ETH_GetPMTFlagStatus(uint32_t ETH_PMT_FLAG)
  2154. {
  2155. FlagStatus bitstatus = RESET;
  2156. /* Check the parameters */
  2157. assert_param(IS_ETH_PMT_GET_FLAG(ETH_PMT_FLAG));
  2158. if ((ETH->MACPMTCSR & ETH_PMT_FLAG) != (uint32_t)RESET)
  2159. {
  2160. bitstatus = SET;
  2161. }
  2162. else
  2163. {
  2164. bitstatus = RESET;
  2165. }
  2166. return bitstatus;
  2167. }
  2168. /**
  2169. * @brief Enables or disables the MAC Wake-Up Frame Detection.
  2170. * @param NewState: new state of the MAC Wake-Up Frame Detection.
  2171. * This parameter can be: ENABLE or DISABLE.
  2172. * @retval None
  2173. */
  2174. void ETH_WakeUpFrameDetectionCmd(FunctionalState NewState)
  2175. {
  2176. /* Check the parameters */
  2177. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2178. if (NewState != DISABLE)
  2179. {
  2180. /* Enable the MAC Wake-Up Frame Detection */
  2181. ETH->MACPMTCSR |= ETH_MACPMTCSR_WFE;
  2182. }
  2183. else
  2184. {
  2185. /* Disable the MAC Wake-Up Frame Detection */
  2186. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_WFE;
  2187. }
  2188. }
  2189. /**
  2190. * @brief Enables or disables the MAC Magic Packet Detection.
  2191. * @param NewState: new state of the MAC Magic Packet Detection.
  2192. * This parameter can be: ENABLE or DISABLE.
  2193. * @retval None
  2194. */
  2195. void ETH_MagicPacketDetectionCmd(FunctionalState NewState)
  2196. {
  2197. /* Check the parameters */
  2198. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2199. if (NewState != DISABLE)
  2200. {
  2201. /* Enable the MAC Magic Packet Detection */
  2202. ETH->MACPMTCSR |= ETH_MACPMTCSR_MPE;
  2203. }
  2204. else
  2205. {
  2206. /* Disable the MAC Magic Packet Detection */
  2207. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_MPE;
  2208. }
  2209. }
  2210. /**
  2211. * @brief Enables or disables the MAC Power Down.
  2212. * @param NewState: new state of the MAC Power Down.
  2213. * This parameter can be: ENABLE or DISABLE.
  2214. * @retval None
  2215. */
  2216. void ETH_PowerDownCmd(FunctionalState NewState)
  2217. {
  2218. /* Check the parameters */
  2219. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2220. if (NewState != DISABLE)
  2221. {
  2222. /* Enable the MAC Power Down */
  2223. /* This puts the MAC in power down mode */
  2224. ETH->MACPMTCSR |= ETH_MACPMTCSR_PD;
  2225. }
  2226. else
  2227. {
  2228. /* Disable the MAC Power Down */
  2229. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_PD;
  2230. }
  2231. }
  2232. /*--------------------------------- MMC ------------------------------------*/
  2233. /**
  2234. * @brief Preset and Initialize the MMC counters to almost-full value: 0xFFFF_FFF0 (full - 16)
  2235. * @param None
  2236. * @retval None
  2237. */
  2238. void ETH_MMCCounterFullPreset(void)
  2239. {
  2240. /* Preset and Initialize the MMC counters to almost-full value */
  2241. ETH->MMCCR |= ETH_MMCCR_MCFHP | ETH_MMCCR_MCP;
  2242. }
  2243. /**
  2244. * @brief Preset and Initialize the MMC counters to almost-hal value: 0x7FFF_FFF0 (half - 16).
  2245. * @param None
  2246. * @retval None
  2247. */
  2248. void ETH_MMCCounterHalfPreset(void)
  2249. {
  2250. /* Preset the MMC counters to almost-full value */
  2251. ETH->MMCCR &= ~ETH_MMCCR_MCFHP;
  2252. /* Initialize the MMC counters to almost-half value */
  2253. ETH->MMCCR |= ETH_MMCCR_MCP;
  2254. }
  2255. /**
  2256. * @brief Enables or disables the MMC Counter Freeze.
  2257. * @param NewState: new state of the MMC Counter Freeze.
  2258. * This parameter can be: ENABLE or DISABLE.
  2259. * @retval None
  2260. */
  2261. void ETH_MMCCounterFreezeCmd(FunctionalState NewState)
  2262. {
  2263. /* Check the parameters */
  2264. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2265. if (NewState != DISABLE)
  2266. {
  2267. /* Enable the MMC Counter Freeze */
  2268. ETH->MMCCR |= ETH_MMCCR_MCF;
  2269. }
  2270. else
  2271. {
  2272. /* Disable the MMC Counter Freeze */
  2273. ETH->MMCCR &= ~ETH_MMCCR_MCF;
  2274. }
  2275. }
  2276. /**
  2277. * @brief Enables or disables the MMC Reset On Read.
  2278. * @param NewState: new state of the MMC Reset On Read.
  2279. * This parameter can be: ENABLE or DISABLE.
  2280. * @retval None
  2281. */
  2282. void ETH_MMCResetOnReadCmd(FunctionalState NewState)
  2283. {
  2284. /* Check the parameters */
  2285. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2286. if (NewState != DISABLE)
  2287. {
  2288. /* Enable the MMC Counter reset on read */
  2289. ETH->MMCCR |= ETH_MMCCR_ROR;
  2290. }
  2291. else
  2292. {
  2293. /* Disable the MMC Counter reset on read */
  2294. ETH->MMCCR &= ~ETH_MMCCR_ROR;
  2295. }
  2296. }
  2297. /**
  2298. * @brief Enables or disables the MMC Counter Stop Rollover.
  2299. * @param NewState: new state of the MMC Counter Stop Rollover.
  2300. * This parameter can be: ENABLE or DISABLE.
  2301. * @retval None
  2302. */
  2303. void ETH_MMCCounterRolloverCmd(FunctionalState NewState)
  2304. {
  2305. /* Check the parameters */
  2306. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2307. if (NewState != DISABLE)
  2308. {
  2309. /* Disable the MMC Counter Stop Rollover */
  2310. ETH->MMCCR &= ~ETH_MMCCR_CSR;
  2311. }
  2312. else
  2313. {
  2314. /* Enable the MMC Counter Stop Rollover */
  2315. ETH->MMCCR |= ETH_MMCCR_CSR;
  2316. }
  2317. }
  2318. /**
  2319. * @brief Resets the MMC Counters.
  2320. * @param None
  2321. * @retval None
  2322. */
  2323. void ETH_MMCCountersReset(void)
  2324. {
  2325. /* Resets the MMC Counters */
  2326. ETH->MMCCR |= ETH_MMCCR_CR;
  2327. }
  2328. /**
  2329. * @brief Enables or disables the specified ETHERNET MMC interrupts.
  2330. * @param ETH_MMC_IT: specifies the ETHERNET MMC interrupt sources to be enabled or disabled.
  2331. * This parameter can be any combination of Tx interrupt or
  2332. * any combination of Rx interrupt (but not both)of the following values:
  2333. * @arg ETH_MMC_IT_TGF : When Tx good frame counter reaches half the maximum value
  2334. * @arg ETH_MMC_IT_TGFMSC: When Tx good multi col counter reaches half the maximum value
  2335. * @arg ETH_MMC_IT_TGFSC : When Tx good single col counter reaches half the maximum value
  2336. * @arg ETH_MMC_IT_RGUF : When Rx good unicast frames counter reaches half the maximum value
  2337. * @arg ETH_MMC_IT_RFAE : When Rx alignment error counter reaches half the maximum value
  2338. * @arg ETH_MMC_IT_RFCE : When Rx crc error counter reaches half the maximum value
  2339. * @param NewState: new state of the specified ETHERNET MMC interrupts.
  2340. * This parameter can be: ENABLE or DISABLE.
  2341. * @retval None
  2342. */
  2343. void ETH_MMCITConfig(uint32_t ETH_MMC_IT, FunctionalState NewState)
  2344. {
  2345. /* Check the parameters */
  2346. assert_param(IS_ETH_MMC_IT(ETH_MMC_IT));
  2347. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2348. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2349. {
  2350. /* Remove register mak from IT */
  2351. ETH_MMC_IT &= 0xEFFFFFFF;
  2352. /* ETHERNET MMC Rx interrupts selected */
  2353. if (NewState != DISABLE)
  2354. {
  2355. /* Enable the selected ETHERNET MMC interrupts */
  2356. ETH->MMCRIMR &=(~(uint32_t)ETH_MMC_IT);
  2357. }
  2358. else
  2359. {
  2360. /* Disable the selected ETHERNET MMC interrupts */
  2361. ETH->MMCRIMR |= ETH_MMC_IT;
  2362. }
  2363. }
  2364. else
  2365. {
  2366. /* ETHERNET MMC Tx interrupts selected */
  2367. if (NewState != DISABLE)
  2368. {
  2369. /* Enable the selected ETHERNET MMC interrupts */
  2370. ETH->MMCTIMR &=(~(uint32_t)ETH_MMC_IT);
  2371. }
  2372. else
  2373. {
  2374. /* Disable the selected ETHERNET MMC interrupts */
  2375. ETH->MMCTIMR |= ETH_MMC_IT;
  2376. }
  2377. }
  2378. }
  2379. /**
  2380. * @brief Checks whether the specified ETHERNET MMC IT is set or not.
  2381. * @param ETH_MMC_IT: specifies the ETHERNET MMC interrupt.
  2382. * This parameter can be one of the following values:
  2383. * @arg ETH_MMC_IT_TxFCGC: When Tx good frame counter reaches half the maximum value
  2384. * @arg ETH_MMC_IT_TxMCGC: When Tx good multi col counter reaches half the maximum value
  2385. * @arg ETH_MMC_IT_TxSCGC: When Tx good single col counter reaches half the maximum value
  2386. * @arg ETH_MMC_IT_RxUGFC: When Rx good unicast frames counter reaches half the maximum value
  2387. * @arg ETH_MMC_IT_RxAEC : When Rx alignment error counter reaches half the maximum value
  2388. * @arg ETH_MMC_IT_RxCEC : When Rx crc error counter reaches half the maximum value
  2389. * @retval The value of ETHERNET MMC IT (SET or RESET).
  2390. */
  2391. ITStatus ETH_GetMMCITStatus(uint32_t ETH_MMC_IT)
  2392. {
  2393. ITStatus bitstatus = RESET;
  2394. /* Check the parameters */
  2395. assert_param(IS_ETH_MMC_GET_IT(ETH_MMC_IT));
  2396. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2397. {
  2398. /* ETHERNET MMC Rx interrupts selected */
  2399. /* Check if the ETHERNET MMC Rx selected interrupt is enabled and occured */
  2400. if ((((ETH->MMCRIR & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRIMR & ETH_MMC_IT) == (uint32_t)RESET))
  2401. {
  2402. bitstatus = SET;
  2403. }
  2404. else
  2405. {
  2406. bitstatus = RESET;
  2407. }
  2408. }
  2409. else
  2410. {
  2411. /* ETHERNET MMC Tx interrupts selected */
  2412. /* Check if the ETHERNET MMC Tx selected interrupt is enabled and occured */
  2413. if ((((ETH->MMCTIR & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCTIMR & ETH_MMC_IT) == (uint32_t)RESET))
  2414. {
  2415. bitstatus = SET;
  2416. }
  2417. else
  2418. {
  2419. bitstatus = RESET;
  2420. }
  2421. }
  2422. return bitstatus;
  2423. }
  2424. /**
  2425. * @brief Get the specified ETHERNET MMC register value.
  2426. * @param ETH_MMCReg: specifies the ETHERNET MMC register.
  2427. * This parameter can be one of the following values:
  2428. * @arg ETH_MMCCR : MMC CR register
  2429. * @arg ETH_MMCRIR : MMC RIR register
  2430. * @arg ETH_MMCTIR : MMC TIR register
  2431. * @arg ETH_MMCRIMR : MMC RIMR register
  2432. * @arg ETH_MMCTIMR : MMC TIMR register
  2433. * @arg ETH_MMCTGFSCCR : MMC TGFSCCR register
  2434. * @arg ETH_MMCTGFMSCCR: MMC TGFMSCCR register
  2435. * @arg ETH_MMCTGFCR : MMC TGFCR register
  2436. * @arg ETH_MMCRFCECR : MMC RFCECR register
  2437. * @arg ETH_MMCRFAECR : MMC RFAECR register
  2438. * @arg ETH_MMCRGUFCR : MMC RGUFCRregister
  2439. * @retval The value of ETHERNET MMC Register value.
  2440. */
  2441. uint32_t ETH_GetMMCRegister(uint32_t ETH_MMCReg)
  2442. {
  2443. /* Check the parameters */
  2444. assert_param(IS_ETH_MMC_REGISTER(ETH_MMCReg));
  2445. /* Return the selected register value */
  2446. return (*(__IO uint32_t *)(ETH_MAC_BASE + ETH_MMCReg));
  2447. }
  2448. /*--------------------------------- PTP ------------------------------------*/
  2449. /**
  2450. * @brief Sets the PTP node clock type.
  2451. * @param ClockType: specifies the PTP node clock type.
  2452. * This parameter can be one of the following values:
  2453. * @arg ETH_PTP_OrdinaryClock : Ordinary Clock.
  2454. * @arg ETH_PTP_BoundaryClock : Boundary Clock.
  2455. * @arg ETH_PTP_EndToEndTransparentClock : End To End Transparent Clock.
  2456. * @arg ETH_PTP_PeerToPeerTransparentClock : Peer To Peer Transparent Clock.
  2457. * @retval None
  2458. */
  2459. void ETH_PTPNodeClockTypeConfig(uint32_t ClockType)
  2460. {
  2461. /* Check the parameters */
  2462. assert_param(IS_ETH_PTP_TYPE_CLOCK(ClockType));
  2463. /* Clear the PTP node clock type */
  2464. ETH->PTPTSCR &= (~(uint32_t)ETH_PTPTSCR_TSCNT);
  2465. /* Set the new PTP node clock type */
  2466. ETH->PTPTSCR |= ClockType;
  2467. }
  2468. /**
  2469. * @brief Enables or disables the selected PTP snapshot method.
  2470. * @param SnapshotMethod: specifies the PTP snapshot method.
  2471. * This parameter can be one of the following values:
  2472. * @arg ETH_PTP_SnapshotMasterMessage : snapshot for message relevant to master.
  2473. * @arg ETH_PTP_SnapshotEventMessage : snapshot for event message.
  2474. * @arg ETH_PTP_SnapshotIPV4Frames : snapshot for IPv4 frames.
  2475. * @arg ETH_PTP_SnapshotIPV6Frames : snapshot for IPv6 frames.
  2476. * @arg ETH_PTP_SnapshotPTPOverEthernetFrames : snapshot for PTP over ethernet frames.
  2477. * @arg ETH_PTP_SnapshotAllReceivedFrames : snapshot for all received frames.
  2478. * @param NewState: new state of the PTP snapshot method
  2479. * This parameter can be: ENABLE or DISABLE.
  2480. * @retval None
  2481. */
  2482. void ETH_PTPSnapshotCmd(uint32_t SnapshotMethod, FunctionalState NewState)
  2483. {
  2484. /* Check the parameters */
  2485. assert_param(IS_ETH_PTP_SNAPSHOT(SnapshotMethod));
  2486. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2487. if (NewState != DISABLE)
  2488. {
  2489. /* Enable the selected PTP snapshot method */
  2490. ETH->PTPTSCR |= SnapshotMethod;
  2491. }
  2492. else
  2493. {
  2494. /* Disable the selected PTP snapshot method */
  2495. ETH->PTPTSCR &= (~(uint32_t)SnapshotMethod);
  2496. }
  2497. }
  2498. /**
  2499. * @brief Enables or disables the PTP packet snooping version 2 format.
  2500. * @param NewState: new state of the PTP packet snooping version 2 format
  2501. * This parameter can be: ENABLE or DISABLE.
  2502. * @retval None
  2503. */
  2504. void ETH_PTPPacketSnoopingV2FormatCmd(FunctionalState NewState)
  2505. {
  2506. /* Check the parameters */
  2507. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2508. if (NewState != DISABLE)
  2509. {
  2510. /* Enable the PTP packet snooping version 2 format */
  2511. ETH->PTPTSCR |= ETH_PTPTSSR_TSPTPPSV2E;
  2512. }
  2513. else
  2514. {
  2515. /* Disable the PTP packet snooping version 2 format */
  2516. ETH->PTPTSCR &= (~(uint32_t)ETH_PTPTSSR_TSPTPPSV2E);
  2517. }
  2518. }
  2519. /**
  2520. * @brief Enables or disables the PTP Subsecond rollover.
  2521. * @param NewState: new state of the PTP Subsecond rollover
  2522. * This parameter can be: ENABLE or DISABLE.
  2523. * @retval None
  2524. */
  2525. void ETH_PTPSubSecondRolloverCmd(FunctionalState NewState)
  2526. {
  2527. /* Check the parameters */
  2528. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2529. if (NewState != DISABLE)
  2530. {
  2531. /* Enable the PTP Subsecond rollover */
  2532. ETH->PTPTSCR |= ETH_PTPTSSR_TSSSR;
  2533. }
  2534. else
  2535. {
  2536. /* Disable the PTP Subsecond rollover */
  2537. ETH->PTPTSCR &= (~(uint32_t)ETH_PTPTSSR_TSSSR);
  2538. }
  2539. }
  2540. /**
  2541. * @brief Updated the PTP block for fine correction with the Time Stamp Addend register value.
  2542. * @param None
  2543. * @retval None
  2544. */
  2545. void ETH_EnablePTPTimeStampAddend(void)
  2546. {
  2547. /* Enable the PTP block update with the Time Stamp Addend register value */
  2548. ETH->PTPTSCR |= ETH_PTPTSCR_TSARU;
  2549. }
  2550. /**
  2551. * @brief Enable the PTP Time Stamp interrupt trigger
  2552. * @param None
  2553. * @retval None
  2554. */
  2555. void ETH_EnablePTPTimeStampInterruptTrigger(void)
  2556. {
  2557. /* Enable the PTP target time interrupt */
  2558. ETH->PTPTSCR |= ETH_PTPTSCR_TSITE;
  2559. }
  2560. /**
  2561. * @brief Updated the PTP system time with the Time Stamp Update register value.
  2562. * @param None
  2563. * @retval None
  2564. */
  2565. void ETH_EnablePTPTimeStampUpdate(void)
  2566. {
  2567. /* Enable the PTP system time update with the Time Stamp Update register value */
  2568. ETH->PTPTSCR |= ETH_PTPTSCR_TSSTU;
  2569. }
  2570. /**
  2571. * @brief Initialize the PTP Time Stamp
  2572. * @param None
  2573. * @retval None
  2574. */
  2575. void ETH_InitializePTPTimeStamp(void)
  2576. {
  2577. /* Initialize the PTP Time Stamp */
  2578. ETH->PTPTSCR |= ETH_PTPTSCR_TSSTI;
  2579. }
  2580. /**
  2581. * @brief Selects the PTP Update method
  2582. * @param UpdateMethod: the PTP Update method
  2583. * This parameter can be one of the following values:
  2584. * @arg ETH_PTP_FineUpdate : Fine Update method
  2585. * @arg ETH_PTP_CoarseUpdate : Coarse Update method
  2586. * @retval None
  2587. */
  2588. void ETH_PTPUpdateMethodConfig(uint32_t UpdateMethod)
  2589. {
  2590. /* Check the parameters */
  2591. assert_param(IS_ETH_PTP_UPDATE(UpdateMethod));
  2592. if (UpdateMethod != ETH_PTP_CoarseUpdate)
  2593. {
  2594. /* Enable the PTP Fine Update method */
  2595. ETH->PTPTSCR |= ETH_PTPTSCR_TSFCU;
  2596. }
  2597. else
  2598. {
  2599. /* Disable the PTP Coarse Update method */
  2600. ETH->PTPTSCR &= (~(uint32_t)ETH_PTPTSCR_TSFCU);
  2601. }
  2602. }
  2603. /**
  2604. * @brief Enables or disables the PTP time stamp for transmit and receive frames.
  2605. * @param NewState: new state of the PTP time stamp for transmit and receive frames
  2606. * This parameter can be: ENABLE or DISABLE.
  2607. * @retval None
  2608. */
  2609. void ETH_PTPTimeStampCmd(FunctionalState NewState)
  2610. {
  2611. /* Check the parameters */
  2612. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2613. if (NewState != DISABLE)
  2614. {
  2615. /* Enable the PTP time stamp for transmit and receive frames */
  2616. ETH->PTPTSCR |= ETH_PTPTSCR_TSE;
  2617. }
  2618. else
  2619. {
  2620. /* Disable the PTP time stamp for transmit and receive frames */
  2621. ETH->PTPTSCR &= (~(uint32_t)ETH_PTPTSCR_TSE);
  2622. }
  2623. }
  2624. /**
  2625. * @brief Checks whether the specified ETHERNET PTP flag is set or not.
  2626. * @param ETH_PTP_FLAG: specifies the flag to check.
  2627. * This parameter can be one of the following values:
  2628. * @arg ETH_PTP_FLAG_TSARU : Addend Register Update
  2629. * @arg ETH_PTP_FLAG_TSITE : Time Stamp Interrupt Trigger Enable
  2630. * @arg ETH_PTP_FLAG_TSSTU : Time Stamp Update
  2631. * @arg ETH_PTP_FLAG_TSSTI : Time Stamp Initialize
  2632. * @retval The new state of ETHERNET PTP Flag (SET or RESET).
  2633. */
  2634. FlagStatus ETH_GetPTPFlagStatus(uint32_t ETH_PTP_FLAG)
  2635. {
  2636. uint32_t flagpos = 0x0;
  2637. FlagStatus bitstatus = RESET;
  2638. uint32_t ethernetreg = 0x0;
  2639. /* Check the parameters */
  2640. assert_param(IS_ETH_PTP_GET_FLAG(ETH_PTP_FLAG));
  2641. /* Get the Flag position */
  2642. flagpos &= 0xEFFFFFFF;
  2643. /* Get the Ethernet register index */
  2644. ethernetreg = (((uint32_t)ETH_PTP_FLAG) & 0x10000000);
  2645. if (ethernetreg != (uint32_t)RESET) /* The flag is in PTPTSCR register */
  2646. {
  2647. flagpos &= ETH->PTPTSCR;
  2648. }
  2649. else /* The IT is in PTPTSSR register */
  2650. {
  2651. flagpos &= ETH->PTPTSSR;
  2652. }
  2653. if (flagpos != (uint32_t)RESET)
  2654. {
  2655. bitstatus = SET;
  2656. }
  2657. else
  2658. {
  2659. bitstatus = RESET;
  2660. }
  2661. return bitstatus;
  2662. }
  2663. /**
  2664. * @brief Sets the system time Sub-Second Increment value.
  2665. * @param SubSecondValue: specifies the PTP Sub-Second Increment Register value.
  2666. * @retval None
  2667. */
  2668. void ETH_SetPTPSubSecondIncrement(uint32_t SubSecondValue)
  2669. {
  2670. /* Check the parameters */
  2671. assert_param(IS_ETH_PTP_SUBSECOND_INCREMENT(SubSecondValue));
  2672. /* Set the PTP Sub-Second Increment Register */
  2673. ETH->PTPSSIR = SubSecondValue;
  2674. }
  2675. /**
  2676. * @brief Sets the Time Stamp update sign and values.
  2677. * @param Sign: specifies the PTP Time update value sign.
  2678. * This parameter can be one of the following values:
  2679. * @arg ETH_PTP_PositiveTime : positive time value.
  2680. * @arg ETH_PTP_NegativeTime : negative time value.
  2681. * @param SecondValue: specifies the PTP Time update second value.
  2682. * @param SubSecondValue: specifies the PTP Time update sub-second value.
  2683. * This parameter is a 31 bit value, bit32 correspond to the sign.
  2684. * @retval None
  2685. */
  2686. void ETH_SetPTPTimeStampUpdate(uint32_t Sign, uint32_t SecondValue, uint32_t SubSecondValue)
  2687. {
  2688. /* Check the parameters */
  2689. assert_param(IS_ETH_PTP_TIME_SIGN(Sign));
  2690. assert_param(IS_ETH_PTP_TIME_STAMP_UPDATE_SUBSECOND(SubSecondValue));
  2691. /* Set the PTP Time Update High Register */
  2692. ETH->PTPTSHUR = SecondValue;
  2693. /* Set the PTP Time Update Low Register with sign */
  2694. ETH->PTPTSLUR = Sign | SubSecondValue;
  2695. }
  2696. /**
  2697. * @brief Sets the Time Stamp Addend value.
  2698. * @param Value: specifies the PTP Time Stamp Addend Register value.
  2699. * @retval None
  2700. */
  2701. void ETH_SetPTPTimeStampAddend(uint32_t Value)
  2702. {
  2703. /* Set the PTP Time Stamp Addend Register */
  2704. ETH->PTPTSAR = Value;
  2705. }
  2706. /**
  2707. * @brief Sets the Target Time registers values.
  2708. * @param HighValue: specifies the PTP Target Time High Register value.
  2709. * @param LowValue: specifies the PTP Target Time Low Register value.
  2710. * @retval None
  2711. */
  2712. void ETH_SetPTPTargetTime(uint32_t HighValue, uint32_t LowValue)
  2713. {
  2714. /* Set the PTP Target Time High Register */
  2715. ETH->PTPTTHR = HighValue;
  2716. /* Set the PTP Target Time Low Register */
  2717. ETH->PTPTTLR = LowValue;
  2718. }
  2719. /**
  2720. * @brief Get the specified ETHERNET PTP register value.
  2721. * @param ETH_PTPReg: specifies the ETHERNET PTP register.
  2722. * This parameter can be one of the following values:
  2723. * @arg ETH_PTPTSCR : Sub-Second Increment Register
  2724. * @arg ETH_PTPSSIR : Sub-Second Increment Register
  2725. * @arg ETH_PTPTSHR : Time Stamp High Register
  2726. * @arg ETH_PTPTSLR : Time Stamp Low Register
  2727. * @arg ETH_PTPTSHUR : Time Stamp High Update Register
  2728. * @arg ETH_PTPTSLUR : Time Stamp Low Update Register
  2729. * @arg ETH_PTPTSAR : Time Stamp Addend Register
  2730. * @arg ETH_PTPTTHR : Target Time High Register
  2731. * @arg ETH_PTPTTLR : Target Time Low Register
  2732. * @retval The value of ETHERNET PTP Register value.
  2733. */
  2734. uint32_t ETH_GetPTPRegister(uint32_t ETH_PTPReg)
  2735. {
  2736. /* Check the parameters */
  2737. assert_param(IS_ETH_PTP_REGISTER(ETH_PTPReg));
  2738. /* Return the selected register value */
  2739. return (*(__IO uint32_t *)(ETH_MAC_BASE + ETH_PTPReg));
  2740. }
  2741. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  2742. /**
  2743. * @brief Initializes the DMA Tx descriptors in chain mode with PTP.
  2744. * @param DMAPTPTxDescTab: Pointer on the first Tx desc list
  2745. * @param TxBuff: Pointer on the first TxBuffer list
  2746. * @param TxBuffCount: Number of the used Tx desc in the list
  2747. * @retval None
  2748. */
  2749. void ETH_DMAPTPTxDescChainInit(ETH_DMADESCTypeDef *DMAPTPTxDescTab, uint8_t* TxBuff, uint32_t TxBuffCount)
  2750. {
  2751. uint32_t i = 0;
  2752. ETH_DMADESCTypeDef *DMAPTPTxDesc;
  2753. /* Set the DMAPTPTxDescToSet pointer with the first one of the DMAPTPTxDescTab list */
  2754. DMAPTPTxDescToSet = DMAPTPTxDescTab;
  2755. /* Fill each DMAPTPTxDesc descriptor with the right values */
  2756. for(i=0; i < TxBuffCount; i++)
  2757. {
  2758. /* Get the pointer on the ith member of the Tx Desc list */
  2759. DMAPTPTxDesc = DMAPTPTxDescTab + i;
  2760. /* Set Second Address Chained bit */
  2761. DMAPTPTxDesc->Status = ETH_DMATxDesc_TCH | ETH_DMATxDesc_TTSE;
  2762. /* Set Buffer1 address pointer */
  2763. DMAPTPTxDesc->Buffer1Addr = (uint32_t)(&TxBuff[i*ETH_MAX_PACKET_SIZE]);
  2764. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  2765. if(i < (TxBuffCount-1))
  2766. {
  2767. /* Set next descriptor address register with next descriptor base address */
  2768. DMAPTPTxDesc->Buffer2NextDescAddr = (uint32_t)(DMAPTPTxDescTab+i+1);
  2769. }
  2770. else
  2771. {
  2772. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  2773. DMAPTPTxDesc->Buffer2NextDescAddr = (uint32_t) DMAPTPTxDescTab;
  2774. }
  2775. }
  2776. /* Set Transmit Desciptor List Address Register */
  2777. ETH->DMATDLAR = (uint32_t) DMAPTPTxDescTab;
  2778. }
  2779. /**
  2780. * @brief Initializes the DMA Rx descriptors in chain mode.
  2781. * @param DMAPTPRxDescTab: Pointer on the first Rx desc list
  2782. * @param RxBuff: Pointer on the first RxBuffer list
  2783. * @param RxBuffCount: Number of the used Rx desc in the list
  2784. * @retval None
  2785. */
  2786. void ETH_DMAPTPRxDescChainInit(ETH_DMADESCTypeDef *DMAPTPRxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
  2787. {
  2788. uint32_t i = 0;
  2789. ETH_DMADESCTypeDef *DMAPTPRxDesc;
  2790. /* Set the DMAPTPRxDescToGet pointer with the first one of the DMAPTPRxDescTab list */
  2791. DMAPTPRxDescToGet = DMAPTPRxDescTab;
  2792. /* Fill each DMAPTPRxDesc descriptor with the right values */
  2793. for(i=0; i < RxBuffCount; i++)
  2794. {
  2795. /* Get the pointer on the ith member of the Rx Desc list */
  2796. DMAPTPRxDesc = DMAPTPRxDescTab+i;
  2797. /* Set Own bit of the Rx descriptor Status */
  2798. DMAPTPRxDesc->Status = ETH_DMARxDesc_OWN;
  2799. /* Set Buffer1 size and Second Address Chained bit */
  2800. DMAPTPRxDesc->ControlBufferSize = ETH_DMARxDesc_RCH | (uint32_t)ETH_MAX_PACKET_SIZE;
  2801. /* Set Buffer1 address pointer */
  2802. DMAPTPRxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*ETH_MAX_PACKET_SIZE]);
  2803. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  2804. if(i < (RxBuffCount-1))
  2805. {
  2806. /* Set next descriptor address register with next descriptor base address */
  2807. DMAPTPRxDesc->Buffer2NextDescAddr = (uint32_t)(DMAPTPRxDescTab+i+1);
  2808. }
  2809. else
  2810. {
  2811. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  2812. DMAPTPRxDesc->Buffer2NextDescAddr = (uint32_t)(DMAPTPRxDescTab);
  2813. }
  2814. }
  2815. /* Set Receive Desciptor List Address Register */
  2816. ETH->DMARDLAR = (uint32_t) DMAPTPRxDescTab;
  2817. }
  2818. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  2819. /**
  2820. * @brief Transmits a packet, from application buffer, pointed by ppkt with Time Stamp values.
  2821. * @param ppkt: pointer to application packet buffer to transmit.
  2822. * @param FrameLength: Tx Packet size.
  2823. * @param PTPTxTab: Pointer on the first PTP Tx table to store Time stamp values.
  2824. * @retval ETH_ERROR: in case of Tx desc owned by DMA
  2825. * ETH_SUCCESS: for correct transmission
  2826. */
  2827. uint32_t ETH_HandlePTPTxPkt(uint8_t *ppkt, uint16_t FrameLength, uint32_t *PTPTxTab)
  2828. {
  2829. uint32_t offset = 0, timeout = 0;
  2830. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  2831. if((DMAPTPTxDescToSet->Status & ETH_DMATxDesc_OWN) != (uint32_t)RESET)
  2832. {
  2833. /* Return ERROR: OWN bit set */
  2834. return ETH_ERROR;
  2835. }
  2836. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  2837. for(offset=0; offset<FrameLength; offset++)
  2838. {
  2839. (*(__IO uint8_t *)((DMAPTPTxDescToSet->Buffer1Addr) + offset)) = (*(ppkt + offset));
  2840. }
  2841. /* Setting the Frame Length: bits[12:0] */
  2842. DMAPTPTxDescToSet->ControlBufferSize = (FrameLength & ETH_DMATxDesc_TBS1);
  2843. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  2844. DMAPTPTxDescToSet->Status |= ETH_DMATxDesc_LS | ETH_DMATxDesc_FS;
  2845. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  2846. DMAPTPTxDescToSet->Status |= ETH_DMATxDesc_OWN;
  2847. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  2848. if ((ETH->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
  2849. {
  2850. /* Clear TBUS ETHERNET DMA flag */
  2851. ETH->DMASR = ETH_DMASR_TBUS;
  2852. /* Resume DMA transmission*/
  2853. ETH->DMATPDR = 0;
  2854. }
  2855. /* Wait for ETH_DMATxDesc_TTSS flag to be set */
  2856. do
  2857. {
  2858. timeout++;
  2859. } while (!(DMAPTPTxDescToSet->Status & ETH_DMATxDesc_TTSS) && (timeout < 0xFFFF));
  2860. /* Return ERROR in case of timeout */
  2861. if(timeout == PHY_READ_TO)
  2862. {
  2863. return ETH_ERROR;
  2864. }
  2865. /* Clear the DMATxDescToSet status register TTSS flag */
  2866. DMATxDescToSet->Status &= ~ETH_DMATxDesc_TTSS;
  2867. *PTPTxTab++ = DMAPTPTxDescToSet->TimeStampLow;
  2868. *PTPTxTab = DMAPTPTxDescToSet->TimeStampHigh;
  2869. /* Update the ETHERNET DMA global Tx descriptor with next Tx decriptor */
  2870. /* Chained Mode */
  2871. if((DMAPTPTxDescToSet->Status & ETH_DMATxDesc_TCH) != (uint32_t)RESET)
  2872. {
  2873. /* Selects the next DMA Tx descriptor list for next buffer to send */
  2874. DMAPTPTxDescToSet = (ETH_DMADESCTypeDef*) (DMAPTPTxDescToSet->Buffer2NextDescAddr);
  2875. }
  2876. else /* Ring Mode */
  2877. {
  2878. if((DMAPTPTxDescToSet->Status & ETH_DMATxDesc_TER) != (uint32_t)RESET)
  2879. {
  2880. /* Selects the first DMA Tx descriptor for next buffer to send: last Tx descriptor was used */
  2881. DMAPTPTxDescToSet = (ETH_DMADESCTypeDef*) (ETH->DMATDLAR);
  2882. }
  2883. else
  2884. {
  2885. /* Selects the next DMA Tx descriptor list for next buffer to send */
  2886. DMAPTPTxDescToSet = (ETH_DMADESCTypeDef*) ((uint32_t)DMAPTPTxDescToSet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  2887. }
  2888. }
  2889. /* Return SUCCESS */
  2890. return ETH_SUCCESS;
  2891. }
  2892. /**
  2893. * @brief Receives a packet and copies it to memory pointed by ppkt with Time Stamp values.
  2894. * @param ppkt: pointer to application packet receive buffer.
  2895. * @param PTPRxTab: Pointer on the first PTP Rx table to store Time stamp values.
  2896. * @retval ETH_ERROR: if there is error in reception
  2897. * framelength: received packet size if packet reception is correct
  2898. */
  2899. uint32_t ETH_HandlePTPRxPkt(uint8_t *ppkt, uint32_t *PTPRxTab)
  2900. {
  2901. uint32_t offset = 0, framelength = 0;
  2902. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  2903. if((DMAPTPRxDescToGet->Status & ETH_DMARxDesc_OWN) != (uint32_t)RESET)
  2904. {
  2905. /* Return error: OWN bit set */
  2906. return ETH_ERROR;
  2907. }
  2908. if(((DMAPTPRxDescToGet->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  2909. ((DMAPTPRxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET) &&
  2910. ((DMAPTPRxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET))
  2911. {
  2912. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  2913. framelength = ((DMAPTPRxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  2914. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  2915. for(offset=0; offset<framelength; offset++)
  2916. {
  2917. (*(ppkt + offset)) = (*(__IO uint8_t *)((DMAPTPRxDescToGet->Buffer1Addr) + offset));
  2918. }
  2919. }
  2920. else
  2921. {
  2922. /* Return ERROR */
  2923. framelength = ETH_ERROR;
  2924. }
  2925. *PTPRxTab++ = DMAPTPRxDescToGet->TimeStampLow;
  2926. *PTPRxTab = DMAPTPRxDescToGet->TimeStampHigh;
  2927. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  2928. DMAPTPRxDescToGet->Status = ETH_DMARxDesc_OWN;
  2929. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  2930. if ((ETH->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  2931. {
  2932. /* Clear RBUS ETHERNET DMA flag */
  2933. ETH->DMASR = ETH_DMASR_RBUS;
  2934. /* Resume DMA reception */
  2935. ETH->DMARPDR = 0;
  2936. }
  2937. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  2938. /* Chained Mode */
  2939. if((DMAPTPRxDescToGet->ControlBufferSize & ETH_DMARxDesc_RCH) != (uint32_t)RESET)
  2940. {
  2941. /* Selects the next DMA Rx descriptor list for next buffer to read */
  2942. DMAPTPRxDescToGet = (ETH_DMADESCTypeDef*) (DMAPTPRxDescToGet->Buffer2NextDescAddr);
  2943. }
  2944. else /* Ring Mode */
  2945. {
  2946. if((DMAPTPRxDescToGet->ControlBufferSize & ETH_DMARxDesc_RER) != (uint32_t)RESET)
  2947. {
  2948. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  2949. DMAPTPRxDescToGet = (ETH_DMADESCTypeDef*) (ETH->DMARDLAR);
  2950. }
  2951. else
  2952. {
  2953. /* Selects the next DMA Rx descriptor list for next buffer to read */
  2954. DMAPTPRxDescToGet = (ETH_DMADESCTypeDef*) ((uint32_t)DMAPTPRxDescToGet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  2955. }
  2956. }
  2957. /* Return Frame Length/ERROR */
  2958. return (framelength);
  2959. }
  2960. /**
  2961. * @}
  2962. */
  2963. /**
  2964. * @}
  2965. */
  2966. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
  2967. /*
  2968. * STM32 Eth Driver for RT-Thread
  2969. * Change Logs:
  2970. * Date Author Notes
  2971. * 2009-10-05 Bernard eth interface driver for STM32F107 CL
  2972. */
  2973. #include <rtthread.h>
  2974. #include <netif/ethernetif.h>
  2975. #include "lwipopts.h"
  2976. /* debug option */
  2977. //#define ETH_DEBUG
  2978. //#define ETH_RX_DUMP
  2979. //#define ETH_TX_DUMP
  2980. #ifdef ETH_DEBUG
  2981. #define STM32_ETH_PRINTF rt_kprintf
  2982. #else
  2983. #define STM32_ETH_PRINTF(...)
  2984. #endif
  2985. #define ETH_RXBUFNB 4
  2986. #define ETH_TXBUFNB 2
  2987. static ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
  2988. static rt_uint8_t Rx_Buff[ETH_RXBUFNB][ETH_MAX_PACKET_SIZE], Tx_Buff[ETH_TXBUFNB][ETH_MAX_PACKET_SIZE];
  2989. #define MAX_ADDR_LEN 6
  2990. struct rt_stm32_eth
  2991. {
  2992. /* inherit from ethernet device */
  2993. struct eth_device parent;
  2994. /* interface address info. */
  2995. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  2996. uint32_t ETH_Speed; /*!< @ref ETH_Speed */
  2997. uint32_t ETH_Mode; /*!< @ref ETH_Duplex_Mode */
  2998. uint32_t ETH_HashTableHigh;
  2999. uint32_t ETH_HashTableLow;
  3000. };
  3001. static struct rt_stm32_eth stm32_eth_device;
  3002. static struct rt_semaphore tx_wait;
  3003. static rt_bool_t tx_is_waiting = RT_FALSE;
  3004. /* interrupt service routine */
  3005. void ETH_IRQHandler(void)
  3006. {
  3007. rt_uint32_t status, ier;
  3008. /* enter interrupt */
  3009. rt_interrupt_enter();
  3010. status = ETH->DMASR;
  3011. ier = ETH->DMAIER;
  3012. if(status & ETH_DMA_IT_MMC)
  3013. {
  3014. STM32_ETH_PRINTF("ETH_DMA_IT_MMC\r\n");
  3015. ETH_DMAClearITPendingBit(ETH_DMA_IT_MMC);
  3016. }
  3017. if(status & ETH_DMA_IT_NIS)
  3018. {
  3019. rt_uint32_t nis_clear = ETH_DMA_IT_NIS;
  3020. /* [0]:Transmit Interrupt. */
  3021. if((status & ier) & ETH_DMA_IT_T) /* packet transmission */
  3022. {
  3023. STM32_ETH_PRINTF("ETH_DMA_IT_T\r\n");
  3024. if (tx_is_waiting == RT_TRUE)
  3025. {
  3026. tx_is_waiting = RT_FALSE;
  3027. rt_sem_release(&tx_wait);
  3028. }
  3029. nis_clear |= ETH_DMA_IT_T;
  3030. }
  3031. /* [2]:Transmit Buffer Unavailable. */
  3032. /* [6]:Receive Interrupt. */
  3033. if((status & ier) & ETH_DMA_IT_R) /* packet reception */
  3034. {
  3035. STM32_ETH_PRINTF("ETH_DMA_IT_R\r\n");
  3036. /* a frame has been received */
  3037. eth_device_ready(&(stm32_eth_device.parent));
  3038. nis_clear |= ETH_DMA_IT_R;
  3039. }
  3040. /* [14]:Early Receive Interrupt. */
  3041. ETH_DMAClearITPendingBit(nis_clear);
  3042. }
  3043. if(status & ETH_DMA_IT_AIS)
  3044. {
  3045. rt_uint32_t ais_clear = ETH_DMA_IT_AIS;
  3046. STM32_ETH_PRINTF("ETH_DMA_IT_AIS\r\n");
  3047. /* [1]:Transmit Process Stopped. */
  3048. if(status & ETH_DMA_IT_TPS)
  3049. {
  3050. STM32_ETH_PRINTF("AIS ETH_DMA_IT_TPS\r\n");
  3051. ais_clear |= ETH_DMA_IT_TPS;
  3052. }
  3053. /* [3]:Transmit Jabber Timeout. */
  3054. if(status & ETH_DMA_IT_TJT)
  3055. {
  3056. STM32_ETH_PRINTF("AIS ETH_DMA_IT_TJT\r\n");
  3057. ais_clear |= ETH_DMA_IT_TJT;
  3058. }
  3059. /* [4]: Receive FIFO Overflow. */
  3060. if(status & ETH_DMA_IT_RO)
  3061. {
  3062. STM32_ETH_PRINTF("AIS ETH_DMA_IT_RO\r\n");
  3063. ais_clear |= ETH_DMA_IT_RO;
  3064. }
  3065. /* [5]: Transmit Underflow. */
  3066. if(status & ETH_DMA_IT_TU)
  3067. {
  3068. STM32_ETH_PRINTF("AIS ETH_DMA_IT_TU\r\n");
  3069. ais_clear |= ETH_DMA_IT_TU;
  3070. }
  3071. /* [7]: Receive Buffer Unavailable. */
  3072. if(status & ETH_DMA_IT_RBU)
  3073. {
  3074. STM32_ETH_PRINTF("AIS ETH_DMA_IT_RBU\r\n");
  3075. ais_clear |= ETH_DMA_IT_RBU;
  3076. }
  3077. /* [8]: Receive Process Stopped. */
  3078. if(status & ETH_DMA_IT_RPS)
  3079. {
  3080. STM32_ETH_PRINTF("AIS ETH_DMA_IT_RPS\r\n");
  3081. ais_clear |= ETH_DMA_IT_RPS;
  3082. }
  3083. /* [9]: Receive Watchdog Timeout. */
  3084. if(status & ETH_DMA_IT_RWT)
  3085. {
  3086. STM32_ETH_PRINTF("AIS ETH_DMA_IT_RWT\r\n");
  3087. ais_clear |= ETH_DMA_IT_RWT;
  3088. }
  3089. /* [10]: Early Transmit Interrupt. */
  3090. /* [13]: Fatal Bus Error. */
  3091. if(status & ETH_DMA_IT_FBE)
  3092. {
  3093. STM32_ETH_PRINTF("AIS ETH_DMA_IT_FBE\r\n");
  3094. ais_clear |= ETH_DMA_IT_FBE;
  3095. }
  3096. ETH_DMAClearITPendingBit(ais_clear);
  3097. }
  3098. /* leave interrupt */
  3099. rt_interrupt_leave();
  3100. }
  3101. /* RT-Thread Device Interface */
  3102. #include <rtthread.h>
  3103. #include <netif/ethernetif.h>
  3104. #include <netif/etharp.h>
  3105. #include <lwip/icmp.h>
  3106. #include "lwipopts.h"
  3107. #if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
  3108. /* polynomial: 0x04C11DB7 */
  3109. static uint32_t ethcrc(const uint8_t *data, size_t length)
  3110. {
  3111. uint32_t crc = 0xffffffff;
  3112. size_t i;
  3113. int j;
  3114. for (i = 0; i < length; i++)
  3115. {
  3116. for (j = 0; j < 8; j++)
  3117. {
  3118. if (((crc >> 31) ^ (data[i] >> j)) & 0x01)
  3119. {
  3120. /* x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1 */
  3121. crc = (crc << 1) ^ 0x04C11DB7;
  3122. }
  3123. else
  3124. {
  3125. crc = crc << 1;
  3126. }
  3127. }
  3128. }
  3129. return ~crc;
  3130. }
  3131. #define HASH_BITS 6 /* #bits in hash */
  3132. static void register_multicast_address(struct rt_stm32_eth *stm32_eth, const uint8_t *mac)
  3133. {
  3134. uint32_t crc;
  3135. uint8_t hash;
  3136. /* calculate crc32 value of mac address */
  3137. crc = ethcrc(mac, 6);
  3138. /* only upper 6 bits (HASH_BITS) are used
  3139. * which point to specific bit in he hash registers
  3140. */
  3141. hash = (crc >> 26) & 0x3F;
  3142. //rt_kprintf("register_multicast_address crc: %08X hash: %02X\n", crc, hash);
  3143. if (hash > 31)
  3144. {
  3145. stm32_eth->ETH_HashTableHigh |= 1 << (hash - 32);
  3146. ETH->MACHTHR = stm32_eth->ETH_HashTableHigh;
  3147. }
  3148. else
  3149. {
  3150. stm32_eth->ETH_HashTableLow |= 1 << hash;
  3151. ETH->MACHTLR = stm32_eth->ETH_HashTableLow;
  3152. }
  3153. }
  3154. #endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
  3155. #if LWIP_IPV4 && LWIP_IGMP
  3156. static err_t igmp_mac_filter( struct netif *netif, const ip4_addr_t *ip4_addr, u8_t action )
  3157. {
  3158. uint8_t mac[6];
  3159. const uint8_t *p = (const uint8_t *)ip4_addr;
  3160. struct rt_stm32_eth *stm32_eth = (struct rt_stm32_eth *)netif->state;
  3161. mac[0] = 0x01;
  3162. mac[1] = 0x00;
  3163. mac[2] = 0x5E;
  3164. mac[3] = *(p+1) & 0x7F;
  3165. mac[4] = *(p+2);
  3166. mac[5] = *(p+3);
  3167. register_multicast_address(stm32_eth, mac);
  3168. if(1)
  3169. {
  3170. rt_kprintf("%s %s %s ", __FUNCTION__, (action==NETIF_ADD_MAC_FILTER)?"add":"del", ip4addr_ntoa(ip4_addr));
  3171. rt_kprintf("%02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  3172. }
  3173. return 0;
  3174. }
  3175. #endif /* LWIP_IPV4 && LWIP_IGMP */
  3176. #if LWIP_IPV6 && LWIP_IPV6_MLD
  3177. static err_t mld_mac_filter( struct netif *netif, const ip6_addr_t *ip6_addr, u8_t action )
  3178. {
  3179. uint8_t mac[6];
  3180. const uint8_t *p = (const uint8_t *)&ip6_addr->addr[3];
  3181. struct rt_stm32_eth *stm32_eth = (struct rt_stm32_eth *)netif->state;
  3182. mac[0] = 0x33;
  3183. mac[1] = 0x33;
  3184. mac[2] = *(p+0);
  3185. mac[3] = *(p+1);
  3186. mac[4] = *(p+2);
  3187. mac[5] = *(p+3);
  3188. register_multicast_address(stm32_eth, mac);
  3189. if(1)
  3190. {
  3191. rt_kprintf("%s %s %s ", __FUNCTION__, (action==NETIF_ADD_MAC_FILTER)?"add":"del", ip6addr_ntoa(ip6_addr));
  3192. rt_kprintf("%02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  3193. }
  3194. return 0;
  3195. }
  3196. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
  3197. /* initialize the interface */
  3198. static rt_err_t rt_stm32_eth_init(rt_device_t dev)
  3199. {
  3200. struct rt_stm32_eth * stm32_eth = (struct rt_stm32_eth *)dev;
  3201. ETH_InitTypeDef ETH_InitStructure;
  3202. /* Enable ETHERNET clock */
  3203. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
  3204. RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
  3205. SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
  3206. /* Reset ETHERNET on AHB Bus */
  3207. ETH_DeInit();
  3208. /* Software reset */
  3209. ETH_SoftwareReset();
  3210. /* Wait for software reset */
  3211. while (ETH_GetSoftwareResetStatus() == SET);
  3212. /* ETHERNET Configuration --------------------------------------------------*/
  3213. /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
  3214. ETH_StructInit(&ETH_InitStructure);
  3215. /* Fill ETH_InitStructure parametrs */
  3216. /*------------------------ MAC -----------------------------------*/
  3217. ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  3218. ETH_InitStructure.ETH_Speed = stm32_eth->ETH_Speed;
  3219. ETH_InitStructure.ETH_Mode = stm32_eth->ETH_Mode;
  3220. ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  3221. ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
  3222. ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  3223. ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  3224. ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
  3225. ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  3226. ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_HashTable;
  3227. ETH_InitStructure.ETH_HashTableHigh = stm32_eth->ETH_HashTableHigh;
  3228. ETH_InitStructure.ETH_HashTableLow = stm32_eth->ETH_HashTableLow;
  3229. ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  3230. #ifdef CHECKSUM_BY_HARDWARE
  3231. ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
  3232. #endif
  3233. /*------------------------ DMA -----------------------------------*/
  3234. /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
  3235. the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
  3236. if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
  3237. ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
  3238. ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  3239. ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  3240. ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  3241. ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  3242. ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
  3243. ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  3244. ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
  3245. ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  3246. ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  3247. ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
  3248. /* configure Ethernet */
  3249. ETH_Init(&ETH_InitStructure);
  3250. /* Enable DMA Receive interrupt (need to enable in this case Normal interrupt) */
  3251. ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R | ETH_DMA_IT_T, ENABLE);
  3252. /* Initialize Tx Descriptors list: Chain Mode */
  3253. ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  3254. /* Initialize Rx Descriptors list: Chain Mode */
  3255. ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  3256. /* MAC address configuration */
  3257. ETH_MACAddressConfig(ETH_MAC_Address0, (u8*)&stm32_eth_device.dev_addr[0]);
  3258. /* Enable MAC and DMA transmission and reception */
  3259. ETH_Start();
  3260. #if LWIP_IPV4 && LWIP_IGMP
  3261. netif_set_igmp_mac_filter(stm32_eth->parent.netif, igmp_mac_filter);
  3262. #endif /* LWIP_IPV4 && LWIP_IGMP */
  3263. #if LWIP_IPV6 && LWIP_IPV6_MLD
  3264. netif_set_mld_mac_filter(stm32_eth->parent.netif, mld_mac_filter);
  3265. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
  3266. return RT_EOK;
  3267. }
  3268. static rt_err_t rt_stm32_eth_open(rt_device_t dev, rt_uint16_t oflag)
  3269. {
  3270. return RT_EOK;
  3271. }
  3272. static rt_err_t rt_stm32_eth_close(rt_device_t dev)
  3273. {
  3274. return RT_EOK;
  3275. }
  3276. static rt_size_t rt_stm32_eth_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  3277. {
  3278. rt_set_errno(-RT_ENOSYS);
  3279. return 0;
  3280. }
  3281. static rt_size_t rt_stm32_eth_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  3282. {
  3283. rt_set_errno(-RT_ENOSYS);
  3284. return 0;
  3285. }
  3286. static rt_err_t rt_stm32_eth_control(rt_device_t dev, int cmd, void *args)
  3287. {
  3288. switch(cmd)
  3289. {
  3290. case NIOCTL_GADDR:
  3291. /* get mac address */
  3292. if(args) rt_memcpy(args, stm32_eth_device.dev_addr, 6);
  3293. else return -RT_ERROR;
  3294. break;
  3295. default :
  3296. break;
  3297. }
  3298. return RT_EOK;
  3299. }
  3300. /* ethernet device interface */
  3301. /* transmit packet. */
  3302. rt_err_t rt_stm32_eth_tx( rt_device_t dev, struct pbuf* p)
  3303. {
  3304. struct pbuf* q;
  3305. rt_uint32_t offset;
  3306. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  3307. while ((DMATxDescToSet->Status & ETH_DMATxDesc_OWN) != (uint32_t)RESET)
  3308. {
  3309. rt_err_t result;
  3310. rt_uint32_t level;
  3311. level = rt_hw_interrupt_disable();
  3312. tx_is_waiting = RT_TRUE;
  3313. rt_hw_interrupt_enable(level);
  3314. /* it's own bit set, wait it */
  3315. result = rt_sem_take(&tx_wait, RT_WAITING_FOREVER);
  3316. if (result == RT_EOK) break;
  3317. if (result == -RT_ERROR) return -RT_ERROR;
  3318. }
  3319. offset = 0;
  3320. for (q = p; q != NULL; q = q->next)
  3321. {
  3322. uint8_t *to;
  3323. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  3324. to = (uint8_t*)((DMATxDescToSet->Buffer1Addr) + offset);
  3325. memcpy(to, q->payload, q->len);
  3326. offset += q->len;
  3327. }
  3328. #ifdef ETH_TX_DUMP
  3329. {
  3330. rt_uint32_t i;
  3331. rt_uint8_t *ptr = (rt_uint8_t*)(DMATxDescToSet->Buffer1Addr);
  3332. STM32_ETH_PRINTF("tx_dump, len:%d\r\n", p->tot_len);
  3333. for(i=0; i<p->tot_len; i++)
  3334. {
  3335. STM32_ETH_PRINTF("%02x ",*ptr);
  3336. ptr++;
  3337. if(((i+1)%8) == 0)
  3338. {
  3339. STM32_ETH_PRINTF(" ");
  3340. }
  3341. if(((i+1)%16) == 0)
  3342. {
  3343. STM32_ETH_PRINTF("\r\n");
  3344. }
  3345. }
  3346. STM32_ETH_PRINTF("\r\ndump done!\r\n");
  3347. }
  3348. #endif
  3349. /* Setting the Frame Length: bits[12:0] */
  3350. DMATxDescToSet->ControlBufferSize = (p->tot_len & ETH_DMATxDesc_TBS1);
  3351. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  3352. DMATxDescToSet->Status |= ETH_DMATxDesc_LS | ETH_DMATxDesc_FS;
  3353. /* Enable TX Completion Interrupt */
  3354. DMATxDescToSet->Status |= ETH_DMATxDesc_IC;
  3355. #ifdef CHECKSUM_BY_HARDWARE
  3356. DMATxDescToSet->Status |= ETH_DMATxDesc_ChecksumTCPUDPICMPFull;
  3357. /* clean ICMP checksum STM32F need */
  3358. {
  3359. struct eth_hdr *ethhdr = (struct eth_hdr *)(DMATxDescToSet->Buffer1Addr);
  3360. /* is IP ? */
  3361. if( ethhdr->type == htons(ETHTYPE_IP) )
  3362. {
  3363. struct ip_hdr *iphdr = (struct ip_hdr *)(DMATxDescToSet->Buffer1Addr + SIZEOF_ETH_HDR);
  3364. /* is ICMP ? */
  3365. if( IPH_PROTO(iphdr) == IP_PROTO_ICMP )
  3366. {
  3367. struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)(DMATxDescToSet->Buffer1Addr + SIZEOF_ETH_HDR + sizeof(struct ip_hdr) );
  3368. iecho->chksum = 0;
  3369. }
  3370. }
  3371. }
  3372. #endif
  3373. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  3374. DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
  3375. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  3376. if ((ETH->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
  3377. {
  3378. /* Clear TBUS ETHERNET DMA flag */
  3379. ETH->DMASR = ETH_DMASR_TBUS;
  3380. /* Transmit Poll Demand to resume DMA transmission*/
  3381. ETH->DMATPDR = 0;
  3382. }
  3383. /* Update the ETHERNET DMA global Tx descriptor with next Tx decriptor */
  3384. /* Chained Mode */
  3385. /* Selects the next DMA Tx descriptor list for next buffer to send */
  3386. DMATxDescToSet = (ETH_DMADESCTypeDef*) (DMATxDescToSet->Buffer2NextDescAddr);
  3387. /* Return SUCCESS */
  3388. return RT_EOK;
  3389. }
  3390. /* reception packet. */
  3391. struct pbuf *rt_stm32_eth_rx(rt_device_t dev)
  3392. {
  3393. struct pbuf* p;
  3394. rt_uint32_t offset = 0, framelength = 0;
  3395. /* init p pointer */
  3396. p = RT_NULL;
  3397. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  3398. if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) != (uint32_t)RESET))
  3399. return p;
  3400. if (((DMARxDescToGet->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  3401. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET) &&
  3402. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET))
  3403. {
  3404. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  3405. framelength = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  3406. /* allocate buffer */
  3407. p = pbuf_alloc(PBUF_LINK, framelength, PBUF_RAM);
  3408. if (p != RT_NULL)
  3409. {
  3410. struct pbuf* q;
  3411. for (q = p; q != RT_NULL; q= q->next)
  3412. {
  3413. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  3414. memcpy(q->payload, (uint8_t *)((DMARxDescToGet->Buffer1Addr) + offset), q->len);
  3415. offset += q->len;
  3416. }
  3417. #ifdef ETH_RX_DUMP
  3418. {
  3419. rt_uint32_t i;
  3420. rt_uint8_t *ptr = (rt_uint8_t*)(DMARxDescToGet->Buffer1Addr);
  3421. STM32_ETH_PRINTF("rx_dump, len:%d\r\n", p->tot_len);
  3422. for(i=0; i<p->tot_len; i++)
  3423. {
  3424. STM32_ETH_PRINTF("%02x ", *ptr);
  3425. ptr++;
  3426. if(((i+1)%8) == 0)
  3427. {
  3428. STM32_ETH_PRINTF(" ");
  3429. }
  3430. if(((i+1)%16) == 0)
  3431. {
  3432. STM32_ETH_PRINTF("\r\n");
  3433. }
  3434. }
  3435. STM32_ETH_PRINTF("\r\ndump done!\r\n");
  3436. }
  3437. #endif
  3438. }
  3439. }
  3440. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  3441. DMARxDescToGet->Status = ETH_DMARxDesc_OWN;
  3442. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  3443. if ((ETH->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  3444. {
  3445. /* Clear RBUS ETHERNET DMA flag */
  3446. ETH->DMASR = ETH_DMASR_RBUS;
  3447. /* Resume DMA reception */
  3448. ETH->DMARPDR = 0;
  3449. }
  3450. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  3451. /* Chained Mode */
  3452. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RCH) != (uint32_t)RESET)
  3453. {
  3454. /* Selects the next DMA Rx descriptor list for next buffer to read */
  3455. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  3456. }
  3457. else /* Ring Mode */
  3458. {
  3459. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RER) != (uint32_t)RESET)
  3460. {
  3461. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  3462. DMARxDescToGet = (ETH_DMADESCTypeDef*) (ETH->DMARDLAR);
  3463. }
  3464. else
  3465. {
  3466. /* Selects the next DMA Rx descriptor list for next buffer to read */
  3467. DMARxDescToGet = (ETH_DMADESCTypeDef*) ((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  3468. }
  3469. }
  3470. return p;
  3471. }
  3472. static void NVIC_Configuration(void)
  3473. {
  3474. NVIC_InitTypeDef NVIC_InitStructure;
  3475. /* Enable the Ethernet global Interrupt */
  3476. NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
  3477. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  3478. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  3479. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  3480. NVIC_Init(&NVIC_InitStructure);
  3481. }
  3482. /*
  3483. * GPIO Configuration for ETH
  3484. */
  3485. static void GPIO_Configuration(void)
  3486. {
  3487. GPIO_InitTypeDef GPIO_InitStructure;
  3488. /* Enable SYSCFG clock */
  3489. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  3490. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC, ENABLE);
  3491. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  3492. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  3493. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  3494. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  3495. /* config MDIO and MDC. */
  3496. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH); /* config ETH_MDIO */
  3497. GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH); /* config ETH_MDC */
  3498. /* config PA2: MDIO */
  3499. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  3500. GPIO_Init(GPIOA, &GPIO_InitStructure);
  3501. /* config PC1: MDC */
  3502. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  3503. GPIO_Init(GPIOC, &GPIO_InitStructure);
  3504. /* Ethernet pins configuration ************************************************/
  3505. #if defined(MII_MODE)
  3506. /*
  3507. ETH_MDIO ------------> PA2
  3508. ETH_MDC -------------> PC1
  3509. ETH_MII_CRS ---------> PA0
  3510. ETH_MII_COL ---------> PA3
  3511. ETH_MII_RX_CLK ------> PA1
  3512. ETH_MII_RX_ER -------> PB10
  3513. ETH_MII_RX_ER -------> PI10
  3514. ETH_MII_RX_DV -------> PA7
  3515. ETH_MII_RXD0 --------> PC4
  3516. ETH_MII_RXD1 --------> PC5
  3517. ETH_MII_RXD2 --------> PB0
  3518. ETH_MII_RXD3 --------> PB1
  3519. ETH_MII_TX_EN -------> PB11
  3520. ETH_MII_TX_EN -------> PG11
  3521. ETH_MII_TX_CLK ------> PC3
  3522. ETH_MII_TXD0 --------> PB12
  3523. ETH_MII_TXD0 --------> PG13
  3524. ETH_MII_TXD1 --------> PB13
  3525. ETH_MII_TXD1 --------> PG14
  3526. ETH_MII_TXD2 --------> PC2
  3527. ETH_MII_TXD3 --------> PB8
  3528. ETH_MII_TXD3 -------> PE2
  3529. */
  3530. #error insert MII GPIO initial.
  3531. #elif defined(RMII_MODE)
  3532. /*
  3533. ETH_MDIO ------------> PA2
  3534. ETH_MDC -------------> PC1
  3535. ETH_RMII_REF_CLK ----> PA1
  3536. ETH_RMII_CRS_DV -----> PA7
  3537. ETH_RMII_RXD0 -------> PC4
  3538. ETH_RMII_RXD1 -------> PC5
  3539. ETH_RMII_TX_EN ------> PG11
  3540. ETH_RMII_TXD0 -------> PG13
  3541. ETH_RMII_TXD1 -------> PG14
  3542. ETH_RMII_TX_EN ------> PB11
  3543. ETH_RMII_TXD0 -------> PB12
  3544. ETH_RMII_TXD1 -------> PB13
  3545. */
  3546. GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH); /* RMII_REF_CLK */
  3547. GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH); /* RMII_CRS_DV */
  3548. /* configure PA1:RMII_REF_CLK, PA7:RMII_CRS_DV. */
  3549. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_7;
  3550. GPIO_Init(GPIOA, &GPIO_InitStructure);
  3551. GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH); /* RMII_RXD0 */
  3552. GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH); /* RMII_RXD1 */
  3553. /* configure PC4:RMII_RXD0, PC5:RMII_RXD1. */
  3554. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  3555. GPIO_Init(GPIOC, &GPIO_InitStructure);
  3556. # if RMII_TX_GPIO_GROUP == 1
  3557. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  3558. GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_ETH); /* RMII_TX_EN */
  3559. GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_ETH); /* RMII_TXD0 */
  3560. GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH); /* RMII_TXD1 */
  3561. /* configure PB11:RMII_TX_EN, PB12:RMII_TXD0, PB13:RMII_TXD1 */
  3562. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  3563. GPIO_Init(GPIOB, &GPIO_InitStructure);
  3564. # elif RMII_TX_GPIO_GROUP == 2
  3565. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
  3566. GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_ETH); /* RMII_TX_EN */
  3567. GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_ETH); /* RMII_TXD0 */
  3568. GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_ETH); /* RMII_TXD1 */
  3569. /* configure PG11:RMII_TX_EN, PG13:RMII_TXD0, PG14:RMII_TXD1 */
  3570. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14;
  3571. GPIO_Init(GPIOG, &GPIO_InitStructure);
  3572. # else
  3573. # error RMII_TX_GPIO_GROUP setting error!
  3574. # endif /*RMII_TX_GPIO_GROUP */
  3575. #endif /* RMII_MODE */
  3576. }
  3577. /* PHY: LAN8720 */
  3578. static uint8_t phy_speed = 0;
  3579. #define PHY_LINK_MASK (1<<0)
  3580. #define PHY_100M_MASK (1<<1)
  3581. #define PHY_DUPLEX_MASK (1<<2)
  3582. static void phy_monitor_thread_entry(void *parameter)
  3583. {
  3584. uint8_t phy_addr = 0xFF;
  3585. uint8_t phy_speed_new = 0;
  3586. /* phy search */
  3587. {
  3588. rt_uint32_t i;
  3589. rt_uint16_t temp;
  3590. for(i=0; i<=0x1F; i++)
  3591. {
  3592. temp = ETH_ReadPHYRegister(i, 0x02);
  3593. if( temp != 0xFFFF )
  3594. {
  3595. phy_addr = i;
  3596. break;
  3597. }
  3598. }
  3599. } /* phy search */
  3600. if(phy_addr == 0xFF)
  3601. {
  3602. STM32_ETH_PRINTF("phy not probe!\r\n");
  3603. return;
  3604. }
  3605. else
  3606. {
  3607. STM32_ETH_PRINTF("found a phy, address:0x%02X\r\n", phy_addr);
  3608. }
  3609. /* RESET PHY */
  3610. STM32_ETH_PRINTF("RESET PHY!\r\n");
  3611. ETH_WritePHYRegister(phy_addr, PHY_BCR, PHY_Reset);
  3612. rt_thread_delay(RT_TICK_PER_SECOND * 2);
  3613. ETH_WritePHYRegister(phy_addr, PHY_BCR, PHY_AutoNegotiation);
  3614. while(1)
  3615. {
  3616. uint16_t status = ETH_ReadPHYRegister(phy_addr, PHY_BSR);
  3617. STM32_ETH_PRINTF("LAN8720 status:0x%04X\r\n", status);
  3618. phy_speed_new = 0;
  3619. if(status & (PHY_AutoNego_Complete | PHY_Linked_Status))
  3620. {
  3621. uint16_t SR;
  3622. SR = ETH_ReadPHYRegister(phy_addr, 31);
  3623. STM32_ETH_PRINTF("LAN8720 REG 31:0x%04X\r\n", SR);
  3624. SR = (SR >> 2) & 0x07; /* LAN8720, REG31[4:2], Speed Indication. */
  3625. phy_speed_new = PHY_LINK_MASK;
  3626. if((SR & 0x03) == 2)
  3627. {
  3628. phy_speed_new |= PHY_100M_MASK;
  3629. }
  3630. if(SR & 0x04)
  3631. {
  3632. phy_speed_new |= PHY_DUPLEX_MASK;
  3633. }
  3634. }
  3635. /* linkchange */
  3636. if(phy_speed_new != phy_speed)
  3637. {
  3638. if(phy_speed_new & PHY_LINK_MASK)
  3639. {
  3640. STM32_ETH_PRINTF("link up ");
  3641. if(phy_speed_new & PHY_100M_MASK)
  3642. {
  3643. STM32_ETH_PRINTF("100Mbps");
  3644. stm32_eth_device.ETH_Speed = ETH_Speed_100M;
  3645. }
  3646. else
  3647. {
  3648. stm32_eth_device.ETH_Speed = ETH_Speed_10M;
  3649. STM32_ETH_PRINTF("10Mbps");
  3650. }
  3651. if(phy_speed_new & PHY_DUPLEX_MASK)
  3652. {
  3653. STM32_ETH_PRINTF(" full-duplex\r\n");
  3654. stm32_eth_device.ETH_Mode = ETH_Mode_FullDuplex;
  3655. }
  3656. else
  3657. {
  3658. STM32_ETH_PRINTF(" half-duplex\r\n");
  3659. stm32_eth_device.ETH_Mode = ETH_Mode_HalfDuplex;
  3660. }
  3661. rt_stm32_eth_init((rt_device_t)&stm32_eth_device);
  3662. /* send link up. */
  3663. eth_device_linkchange(&stm32_eth_device.parent, RT_TRUE);
  3664. } /* link up. */
  3665. else
  3666. {
  3667. STM32_ETH_PRINTF("link down\r\n");
  3668. /* send link down. */
  3669. eth_device_linkchange(&stm32_eth_device.parent, RT_FALSE);
  3670. } /* link down. */
  3671. phy_speed = phy_speed_new;
  3672. } /* linkchange */
  3673. rt_thread_delay(RT_TICK_PER_SECOND);
  3674. } /* while(1) */
  3675. }
  3676. void rt_hw_stm32_eth_init(void)
  3677. {
  3678. /* PHY RESET: PC0 */
  3679. {
  3680. GPIO_InitTypeDef GPIO_InitStructure;
  3681. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  3682. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  3683. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  3684. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  3685. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  3686. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  3687. GPIO_Init(GPIOC, &GPIO_InitStructure);
  3688. GPIO_ResetBits(GPIOC, GPIO_Pin_0);
  3689. rt_thread_delay(2);
  3690. GPIO_SetBits(GPIOC, GPIO_Pin_0);
  3691. rt_thread_delay(2);
  3692. }
  3693. GPIO_Configuration();
  3694. NVIC_Configuration();
  3695. stm32_eth_device.ETH_Speed = ETH_Speed_100M;
  3696. stm32_eth_device.ETH_Mode = ETH_Mode_FullDuplex;
  3697. /* OUI 00-80-E1 STMICROELECTRONICS. */
  3698. stm32_eth_device.dev_addr[0] = 0x00;
  3699. stm32_eth_device.dev_addr[1] = 0x80;
  3700. stm32_eth_device.dev_addr[2] = 0xE1;
  3701. /* generate MAC addr from 96bit unique ID (only for test). */
  3702. stm32_eth_device.dev_addr[3] = *(rt_uint8_t*)(0x1FFF7A10+4);
  3703. stm32_eth_device.dev_addr[4] = *(rt_uint8_t*)(0x1FFF7A10+2);
  3704. stm32_eth_device.dev_addr[5] = *(rt_uint8_t*)(0x1FFF7A10+0);
  3705. stm32_eth_device.parent.parent.init = rt_stm32_eth_init;
  3706. stm32_eth_device.parent.parent.open = rt_stm32_eth_open;
  3707. stm32_eth_device.parent.parent.close = rt_stm32_eth_close;
  3708. stm32_eth_device.parent.parent.read = rt_stm32_eth_read;
  3709. stm32_eth_device.parent.parent.write = rt_stm32_eth_write;
  3710. stm32_eth_device.parent.parent.control = rt_stm32_eth_control;
  3711. stm32_eth_device.parent.parent.user_data = RT_NULL;
  3712. stm32_eth_device.parent.eth_rx = rt_stm32_eth_rx;
  3713. stm32_eth_device.parent.eth_tx = rt_stm32_eth_tx;
  3714. /* init tx semaphore */
  3715. rt_sem_init(&tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO);
  3716. /* register eth device */
  3717. eth_device_init(&(stm32_eth_device.parent), "e0");
  3718. /* start phy monitor */
  3719. {
  3720. rt_thread_t tid;
  3721. tid = rt_thread_create("phy",
  3722. phy_monitor_thread_entry,
  3723. RT_NULL,
  3724. 512,
  3725. RT_THREAD_PRIORITY_MAX - 2,
  3726. 2);
  3727. if (tid != RT_NULL)
  3728. rt_thread_startup(tid);
  3729. }
  3730. }
  3731. INIT_PREV_EXPORT(rt_hw_stm32_eth_init);