stm32_eth.c 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694
  1. /**
  2. ******************************************************************************
  3. * @file stm32_eth.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 11/20/2009
  7. * @brief This file provides all the ETH firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32_eth.h"
  22. #include "stm32f10x_rcc.h"
  23. /* STM32F107 ETH dirver options */
  24. #define CHECKSUM_BY_HARDWARE
  25. #define MII_MODE /* MII mode for STM3210C-EVAL Board (MB784) (check jumpers setting) */
  26. //#define RMII_MODE /* RMII mode for STM3210C-EVAL Board (MB784) (check jumpers setting) */
  27. #define PHY_ADDRESS 0x01 /* Relative to STM3210C-EVAL Board */
  28. /** @addtogroup STM32_ETH_Driver
  29. * @brief ETH driver modules
  30. * @{
  31. */
  32. /** @defgroup ETH_Private_TypesDefinitions
  33. * @{
  34. */
  35. /**
  36. * @}
  37. */
  38. /** @defgroup ETH_Private_Defines
  39. * @{
  40. */
  41. /* Global pointers on Tx and Rx descriptor used to track transmit and receive descriptors */
  42. ETH_DMADESCTypeDef *DMATxDescToSet;
  43. ETH_DMADESCTypeDef *DMARxDescToGet;
  44. ETH_DMADESCTypeDef *DMAPTPTxDescToSet;
  45. ETH_DMADESCTypeDef *DMAPTPRxDescToGet;
  46. /* ETHERNET MAC address offsets */
  47. #define ETH_MAC_ADDR_HBASE (ETH_MAC_BASE + 0x40) /* ETHERNET MAC address high offset */
  48. #define ETH_MAC_ADDR_LBASE (ETH_MAC_BASE + 0x44) /* ETHERNET MAC address low offset */
  49. /* ETHERNET MACMIIAR register Mask */
  50. #define MACMIIAR_CR_MASK ((uint32_t)0xFFFFFFE3)
  51. /* ETHERNET MACCR register Mask */
  52. #define MACCR_CLEAR_MASK ((uint32_t)0xFF20810F)
  53. /* ETHERNET MACFCR register Mask */
  54. #define MACFCR_CLEAR_MASK ((uint32_t)0x0000FF41)
  55. /* ETHERNET DMAOMR register Mask */
  56. #define DMAOMR_CLEAR_MASK ((uint32_t)0xF8DE3F23)
  57. /* ETHERNET Remote Wake-up frame register length */
  58. #define ETH_WAKEUP_REGISTER_LENGTH 8
  59. /* ETHERNET Missed frames counter Shift */
  60. #define ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTERSHIFT 17
  61. /* ETHERNET DMA Tx descriptors Collision Count Shift */
  62. #define ETH_DMATXDESC_COLLISION_COUNTSHIFT 3
  63. /* ETHERNET DMA Tx descriptors Buffer2 Size Shift */
  64. #define ETH_DMATXDESC_BUFFER2_SIZESHIFT 16
  65. /* ETHERNET DMA Rx descriptors Frame Length Shift */
  66. #define ETH_DMARXDESC_FRAME_LENGTHSHIFT 16
  67. /* ETHERNET DMA Rx descriptors Buffer2 Size Shift */
  68. #define ETH_DMARXDESC_BUFFER2_SIZESHIFT 16
  69. /* ETHERNET errors */
  70. #define ETH_ERROR ((uint32_t)0)
  71. #define ETH_SUCCESS ((uint32_t)1)
  72. /**
  73. * @}
  74. */
  75. /** @defgroup ETH_Private_Macros
  76. * @{
  77. */
  78. /**
  79. * @}
  80. */
  81. /** @defgroup ETH_Private_Variables
  82. * @{
  83. */
  84. /**
  85. * @}
  86. */
  87. /** @defgroup ETH_Private_FunctionPrototypes
  88. * @{
  89. */
  90. /**
  91. * @}
  92. */
  93. /** @defgroup ETH_Private_Functions
  94. * @{
  95. */
  96. /**
  97. * @brief Deinitializes the ETHERNET peripheral registers to their default reset values.
  98. * @param None
  99. * @retval None
  100. */
  101. void ETH_DeInit(void)
  102. {
  103. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_ETH_MAC, ENABLE);
  104. RCC_AHBPeriphResetCmd(RCC_AHBPeriph_ETH_MAC, DISABLE);
  105. }
  106. /**
  107. * @brief Initializes the ETHERNET peripheral according to the specified
  108. * parameters in the ETH_InitStruct .
  109. * @param ETH_InitStruct: pointer to a ETH_InitTypeDef structure that contains
  110. * the configuration information for the specified ETHERNET peripheral.
  111. * @param PHYAddress: external PHY address
  112. * @retval ETH_ERROR: Ethernet initialization failed
  113. * ETH_SUCCESS: Ethernet successfully initialized
  114. */
  115. uint32_t ETH_Init(ETH_InitTypeDef* ETH_InitStruct, uint16_t PHYAddress)
  116. {
  117. uint32_t RegValue = 0, tmpreg = 0;
  118. __IO uint32_t i = 0;
  119. RCC_ClocksTypeDef rcc_clocks;
  120. uint32_t hclk = 60000000;
  121. __IO uint32_t timeout = 0;
  122. /* Check the parameters */
  123. /* MAC --------------------------*/
  124. assert_param(IS_ETH_AUTONEGOTIATION(ETH_InitStruct->ETH_AutoNegotiation));
  125. assert_param(IS_ETH_WATCHDOG(ETH_InitStruct->ETH_Watchdog));
  126. assert_param(IS_ETH_JABBER(ETH_InitStruct->ETH_Jabber));
  127. assert_param(IS_ETH_INTER_FRAME_GAP(ETH_InitStruct->ETH_InterFrameGap));
  128. assert_param(IS_ETH_CARRIER_SENSE(ETH_InitStruct->ETH_CarrierSense));
  129. assert_param(IS_ETH_SPEED(ETH_InitStruct->ETH_Speed));
  130. assert_param(IS_ETH_RECEIVE_OWN(ETH_InitStruct->ETH_ReceiveOwn));
  131. assert_param(IS_ETH_LOOPBACK_MODE(ETH_InitStruct->ETH_LoopbackMode));
  132. assert_param(IS_ETH_DUPLEX_MODE(ETH_InitStruct->ETH_Mode));
  133. assert_param(IS_ETH_CHECKSUM_OFFLOAD(ETH_InitStruct->ETH_ChecksumOffload));
  134. assert_param(IS_ETH_RETRY_TRANSMISSION(ETH_InitStruct->ETH_RetryTransmission));
  135. assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(ETH_InitStruct->ETH_AutomaticPadCRCStrip));
  136. assert_param(IS_ETH_BACKOFF_LIMIT(ETH_InitStruct->ETH_BackOffLimit));
  137. assert_param(IS_ETH_DEFERRAL_CHECK(ETH_InitStruct->ETH_DeferralCheck));
  138. assert_param(IS_ETH_RECEIVE_ALL(ETH_InitStruct->ETH_ReceiveAll));
  139. assert_param(IS_ETH_SOURCE_ADDR_FILTER(ETH_InitStruct->ETH_SourceAddrFilter));
  140. assert_param(IS_ETH_CONTROL_FRAMES(ETH_InitStruct->ETH_PassControlFrames));
  141. assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(ETH_InitStruct->ETH_BroadcastFramesReception));
  142. assert_param(IS_ETH_DESTINATION_ADDR_FILTER(ETH_InitStruct->ETH_DestinationAddrFilter));
  143. assert_param(IS_ETH_PROMISCUOUS_MODE(ETH_InitStruct->ETH_PromiscuousMode));
  144. assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(ETH_InitStruct->ETH_MulticastFramesFilter));
  145. assert_param(IS_ETH_UNICAST_FRAMES_FILTER(ETH_InitStruct->ETH_UnicastFramesFilter));
  146. assert_param(IS_ETH_PAUSE_TIME(ETH_InitStruct->ETH_PauseTime));
  147. assert_param(IS_ETH_ZEROQUANTA_PAUSE(ETH_InitStruct->ETH_ZeroQuantaPause));
  148. assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(ETH_InitStruct->ETH_PauseLowThreshold));
  149. assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(ETH_InitStruct->ETH_UnicastPauseFrameDetect));
  150. assert_param(IS_ETH_RECEIVE_FLOWCONTROL(ETH_InitStruct->ETH_ReceiveFlowControl));
  151. assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(ETH_InitStruct->ETH_TransmitFlowControl));
  152. assert_param(IS_ETH_VLAN_TAG_COMPARISON(ETH_InitStruct->ETH_VLANTagComparison));
  153. assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(ETH_InitStruct->ETH_VLANTagIdentifier));
  154. /* DMA --------------------------*/
  155. assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame));
  156. assert_param(IS_ETH_RECEIVE_STORE_FORWARD(ETH_InitStruct->ETH_ReceiveStoreForward));
  157. assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(ETH_InitStruct->ETH_FlushReceivedFrame));
  158. assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(ETH_InitStruct->ETH_TransmitStoreForward));
  159. assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(ETH_InitStruct->ETH_TransmitThresholdControl));
  160. assert_param(IS_ETH_FORWARD_ERROR_FRAMES(ETH_InitStruct->ETH_ForwardErrorFrames));
  161. assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(ETH_InitStruct->ETH_ForwardUndersizedGoodFrames));
  162. assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(ETH_InitStruct->ETH_ReceiveThresholdControl));
  163. assert_param(IS_ETH_SECOND_FRAME_OPERATE(ETH_InitStruct->ETH_SecondFrameOperate));
  164. assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(ETH_InitStruct->ETH_AddressAlignedBeats));
  165. assert_param(IS_ETH_FIXED_BURST(ETH_InitStruct->ETH_FixedBurst));
  166. assert_param(IS_ETH_RXDMA_BURST_LENGTH(ETH_InitStruct->ETH_RxDMABurstLength));
  167. assert_param(IS_ETH_TXDMA_BURST_LENGTH(ETH_InitStruct->ETH_TxDMABurstLength));
  168. assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(ETH_InitStruct->ETH_DescriptorSkipLength));
  169. assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(ETH_InitStruct->ETH_DMAArbitration));
  170. /*-------------------------------- MAC Config ------------------------------*/
  171. /*---------------------- ETHERNET MACMIIAR Configuration -------------------*/
  172. /* Get the ETHERNET MACMIIAR value */
  173. tmpreg = ETH->MACMIIAR;
  174. /* Clear CSR Clock Range CR[2:0] bits */
  175. tmpreg &= MACMIIAR_CR_MASK;
  176. /* Get hclk frequency value */
  177. RCC_GetClocksFreq(&rcc_clocks);
  178. hclk = rcc_clocks.HCLK_Frequency;
  179. /* Set CR bits depending on hclk value */
  180. if((hclk >= 20000000)&&(hclk < 35000000))
  181. {
  182. /* CSR Clock Range between 20-35 MHz */
  183. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div16;
  184. }
  185. else if((hclk >= 35000000)&&(hclk < 60000000))
  186. {
  187. /* CSR Clock Range between 35-60 MHz */
  188. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div26;
  189. }
  190. else /* ((hclk >= 60000000)&&(hclk <= 72000000)) */
  191. {
  192. /* CSR Clock Range between 60-72 MHz */
  193. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div42;
  194. }
  195. /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
  196. ETH->MACMIIAR = (uint32_t)tmpreg;
  197. /*-------------------- PHY initialization and configuration ----------------*/
  198. /* Put the PHY in reset mode */
  199. if(!(ETH_WritePHYRegister(PHYAddress, PHY_BCR, PHY_Reset)))
  200. {
  201. /* Return ERROR in case of write timeout */
  202. return ETH_ERROR;
  203. }
  204. /* Delay to assure PHY reset */
  205. for(i = PHY_ResetDelay; i != 0; i--)
  206. {
  207. }
  208. if(ETH_InitStruct->ETH_AutoNegotiation != ETH_AutoNegotiation_Disable)
  209. {
  210. /* We wait for linked satus... */
  211. do
  212. {
  213. timeout++;
  214. } while (!(ETH_ReadPHYRegister(PHYAddress, PHY_BSR) & PHY_Linked_Status) && (timeout < PHY_READ_TO));
  215. /* Return ERROR in case of timeout */
  216. if(timeout == PHY_READ_TO)
  217. {
  218. return ETH_ERROR;
  219. }
  220. /* Reset Timeout counter */
  221. timeout = 0;
  222. /* Enable Auto-Negotiation */
  223. if(!(ETH_WritePHYRegister(PHYAddress, PHY_BCR, PHY_AutoNegotiation)))
  224. {
  225. /* Return ERROR in case of write timeout */
  226. return ETH_ERROR;
  227. }
  228. /* Wait until the autonegotiation will be completed */
  229. do
  230. {
  231. timeout++;
  232. } while (!(ETH_ReadPHYRegister(PHYAddress, PHY_BSR) & PHY_AutoNego_Complete) && (timeout < (uint32_t)PHY_READ_TO));
  233. /* Return ERROR in case of timeout */
  234. if(timeout == PHY_READ_TO)
  235. {
  236. return ETH_ERROR;
  237. }
  238. /* Reset Timeout counter */
  239. timeout = 0;
  240. /* Read the result of the autonegotiation */
  241. RegValue = ETH_ReadPHYRegister(PHYAddress, PHY_SR);
  242. /* Configure the MAC with the Duplex Mode fixed by the autonegotiation process */
  243. if((RegValue & PHY_Duplex_Status) != (uint32_t)RESET)
  244. {
  245. /* Set Ethernet duplex mode to FullDuplex following the autonegotiation */
  246. ETH_InitStruct->ETH_Mode = ETH_Mode_FullDuplex;
  247. }
  248. else
  249. {
  250. /* Set Ethernet duplex mode to HalfDuplex following the autonegotiation */
  251. ETH_InitStruct->ETH_Mode = ETH_Mode_HalfDuplex;
  252. }
  253. /* Configure the MAC with the speed fixed by the autonegotiation process */
  254. if(RegValue & PHY_Speed_Status)
  255. {
  256. /* Set Ethernet speed to 10M following the autonegotiation */
  257. ETH_InitStruct->ETH_Speed = ETH_Speed_10M;
  258. }
  259. else
  260. {
  261. /* Set Ethernet speed to 100M following the autonegotiation */
  262. ETH_InitStruct->ETH_Speed = ETH_Speed_100M;
  263. }
  264. }
  265. else
  266. {
  267. if(!ETH_WritePHYRegister(PHYAddress, PHY_BCR, ((uint16_t)(ETH_InitStruct->ETH_Mode >> 3) |
  268. (uint16_t)(ETH_InitStruct->ETH_Speed >> 1))))
  269. {
  270. /* Return ERROR in case of write timeout */
  271. return ETH_ERROR;
  272. }
  273. /* Delay to assure PHY configuration */
  274. for(i = PHY_ConfigDelay; i != 0; i--)
  275. {
  276. }
  277. }
  278. /*------------------------ ETHERNET MACCR Configuration --------------------*/
  279. /* Get the ETHERNET MACCR value */
  280. tmpreg = ETH->MACCR;
  281. /* Clear WD, PCE, PS, TE and RE bits */
  282. tmpreg &= MACCR_CLEAR_MASK;
  283. /* Set the WD bit according to ETH_Watchdog value */
  284. /* Set the JD: bit according to ETH_Jabber value */
  285. /* Set the IFG bit according to ETH_InterFrameGap value */
  286. /* Set the DCRS bit according to ETH_CarrierSense value */
  287. /* Set the FES bit according to ETH_Speed value */
  288. /* Set the DO bit according to ETH_ReceiveOwn value */
  289. /* Set the LM bit according to ETH_LoopbackMode value */
  290. /* Set the DM bit according to ETH_Mode value */
  291. /* Set the IPC bit according to ETH_ChecksumOffload value */
  292. /* Set the DR bit according to ETH_RetryTransmission value */
  293. /* Set the ACS bit according to ETH_AutomaticPadCRCStrip value */
  294. /* Set the BL bit according to ETH_BackOffLimit value */
  295. /* Set the DC bit according to ETH_DeferralCheck value */
  296. tmpreg |= (uint32_t)(ETH_InitStruct->ETH_Watchdog |
  297. ETH_InitStruct->ETH_Jabber |
  298. ETH_InitStruct->ETH_InterFrameGap |
  299. ETH_InitStruct->ETH_CarrierSense |
  300. ETH_InitStruct->ETH_Speed |
  301. ETH_InitStruct->ETH_ReceiveOwn |
  302. ETH_InitStruct->ETH_LoopbackMode |
  303. ETH_InitStruct->ETH_Mode |
  304. ETH_InitStruct->ETH_ChecksumOffload |
  305. ETH_InitStruct->ETH_RetryTransmission |
  306. ETH_InitStruct->ETH_AutomaticPadCRCStrip |
  307. ETH_InitStruct->ETH_BackOffLimit |
  308. ETH_InitStruct->ETH_DeferralCheck);
  309. /* Write to ETHERNET MACCR */
  310. ETH->MACCR = (uint32_t)tmpreg;
  311. /*----------------------- ETHERNET MACFFR Configuration --------------------*/
  312. /* Set the RA bit according to ETH_ReceiveAll value */
  313. /* Set the SAF and SAIF bits according to ETH_SourceAddrFilter value */
  314. /* Set the PCF bit according to ETH_PassControlFrames value */
  315. /* Set the DBF bit according to ETH_BroadcastFramesReception value */
  316. /* Set the DAIF bit according to ETH_DestinationAddrFilter value */
  317. /* Set the PR bit according to ETH_PromiscuousMode value */
  318. /* Set the PM, HMC and HPF bits according to ETH_MulticastFramesFilter value */
  319. /* Set the HUC and HPF bits according to ETH_UnicastFramesFilter value */
  320. /* Write to ETHERNET MACFFR */
  321. ETH->MACFFR = (uint32_t)(ETH_InitStruct->ETH_ReceiveAll |
  322. ETH_InitStruct->ETH_SourceAddrFilter |
  323. ETH_InitStruct->ETH_PassControlFrames |
  324. ETH_InitStruct->ETH_BroadcastFramesReception |
  325. ETH_InitStruct->ETH_DestinationAddrFilter |
  326. ETH_InitStruct->ETH_PromiscuousMode |
  327. ETH_InitStruct->ETH_MulticastFramesFilter |
  328. ETH_InitStruct->ETH_UnicastFramesFilter);
  329. /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
  330. /* Write to ETHERNET MACHTHR */
  331. ETH->MACHTHR = (uint32_t)ETH_InitStruct->ETH_HashTableHigh;
  332. /* Write to ETHERNET MACHTLR */
  333. ETH->MACHTLR = (uint32_t)ETH_InitStruct->ETH_HashTableLow;
  334. /*----------------------- ETHERNET MACFCR Configuration --------------------*/
  335. /* Get the ETHERNET MACFCR value */
  336. tmpreg = ETH->MACFCR;
  337. /* Clear xx bits */
  338. tmpreg &= MACFCR_CLEAR_MASK;
  339. /* Set the PT bit according to ETH_PauseTime value */
  340. /* Set the DZPQ bit according to ETH_ZeroQuantaPause value */
  341. /* Set the PLT bit according to ETH_PauseLowThreshold value */
  342. /* Set the UP bit according to ETH_UnicastPauseFrameDetect value */
  343. /* Set the RFE bit according to ETH_ReceiveFlowControl value */
  344. /* Set the TFE bit according to ETH_TransmitFlowControl value */
  345. tmpreg |= (uint32_t)((ETH_InitStruct->ETH_PauseTime << 16) |
  346. ETH_InitStruct->ETH_ZeroQuantaPause |
  347. ETH_InitStruct->ETH_PauseLowThreshold |
  348. ETH_InitStruct->ETH_UnicastPauseFrameDetect |
  349. ETH_InitStruct->ETH_ReceiveFlowControl |
  350. ETH_InitStruct->ETH_TransmitFlowControl);
  351. /* Write to ETHERNET MACFCR */
  352. ETH->MACFCR = (uint32_t)tmpreg;
  353. /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/
  354. /* Set the ETV bit according to ETH_VLANTagComparison value */
  355. /* Set the VL bit according to ETH_VLANTagIdentifier value */
  356. ETH->MACVLANTR = (uint32_t)(ETH_InitStruct->ETH_VLANTagComparison |
  357. ETH_InitStruct->ETH_VLANTagIdentifier);
  358. /*-------------------------------- DMA Config ------------------------------*/
  359. /*----------------------- ETHERNET DMAOMR Configuration --------------------*/
  360. /* Get the ETHERNET DMAOMR value */
  361. tmpreg = ETH->DMAOMR;
  362. /* Clear xx bits */
  363. tmpreg &= DMAOMR_CLEAR_MASK;
  364. /* Set the DT bit according to ETH_DropTCPIPChecksumErrorFrame value */
  365. /* Set the RSF bit according to ETH_ReceiveStoreForward value */
  366. /* Set the DFF bit according to ETH_FlushReceivedFrame value */
  367. /* Set the TSF bit according to ETH_TransmitStoreForward value */
  368. /* Set the TTC bit according to ETH_TransmitThresholdControl value */
  369. /* Set the FEF bit according to ETH_ForwardErrorFrames value */
  370. /* Set the FUF bit according to ETH_ForwardUndersizedGoodFrames value */
  371. /* Set the RTC bit according to ETH_ReceiveThresholdControl value */
  372. /* Set the OSF bit according to ETH_SecondFrameOperate value */
  373. tmpreg |= (uint32_t)(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame |
  374. ETH_InitStruct->ETH_ReceiveStoreForward |
  375. ETH_InitStruct->ETH_FlushReceivedFrame |
  376. ETH_InitStruct->ETH_TransmitStoreForward |
  377. ETH_InitStruct->ETH_TransmitThresholdControl |
  378. ETH_InitStruct->ETH_ForwardErrorFrames |
  379. ETH_InitStruct->ETH_ForwardUndersizedGoodFrames |
  380. ETH_InitStruct->ETH_ReceiveThresholdControl |
  381. ETH_InitStruct->ETH_SecondFrameOperate);
  382. /* Write to ETHERNET DMAOMR */
  383. ETH->DMAOMR = (uint32_t)tmpreg;
  384. /*----------------------- ETHERNET DMABMR Configuration --------------------*/
  385. /* Set the AAL bit according to ETH_AddressAlignedBeats value */
  386. /* Set the FB bit according to ETH_FixedBurst value */
  387. /* Set the RPBL and 4*PBL bits according to ETH_RxDMABurstLength value */
  388. /* Set the PBL and 4*PBL bits according to ETH_TxDMABurstLength value */
  389. /* Set the DSL bit according to ETH_DesciptorSkipLength value */
  390. /* Set the PR and DA bits according to ETH_DMAArbitration value */
  391. ETH->DMABMR = (uint32_t)(ETH_InitStruct->ETH_AddressAlignedBeats |
  392. ETH_InitStruct->ETH_FixedBurst |
  393. ETH_InitStruct->ETH_RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
  394. ETH_InitStruct->ETH_TxDMABurstLength |
  395. (ETH_InitStruct->ETH_DescriptorSkipLength << 2) |
  396. ETH_InitStruct->ETH_DMAArbitration |
  397. ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
  398. /* Return Ethernet configuration success */
  399. return ETH_SUCCESS;
  400. }
  401. /**
  402. * @brief Fills each ETH_InitStruct member with its default value.
  403. * @param ETH_InitStruct: pointer to a ETH_InitTypeDef structure which will be initialized.
  404. * @retval None
  405. */
  406. void ETH_StructInit(ETH_InitTypeDef* ETH_InitStruct)
  407. {
  408. /* ETH_InitStruct members default value */
  409. /*------------------------ MAC -----------------------------------*/
  410. ETH_InitStruct->ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
  411. ETH_InitStruct->ETH_Watchdog = ETH_Watchdog_Enable;
  412. ETH_InitStruct->ETH_Jabber = ETH_Jabber_Enable;
  413. ETH_InitStruct->ETH_InterFrameGap = ETH_InterFrameGap_96Bit;
  414. ETH_InitStruct->ETH_CarrierSense = ETH_CarrierSense_Enable;
  415. ETH_InitStruct->ETH_Speed = ETH_Speed_10M;
  416. ETH_InitStruct->ETH_ReceiveOwn = ETH_ReceiveOwn_Enable;
  417. ETH_InitStruct->ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  418. ETH_InitStruct->ETH_Mode = ETH_Mode_HalfDuplex;
  419. ETH_InitStruct->ETH_ChecksumOffload = ETH_ChecksumOffload_Disable;
  420. ETH_InitStruct->ETH_RetryTransmission = ETH_RetryTransmission_Enable;
  421. ETH_InitStruct->ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  422. ETH_InitStruct->ETH_BackOffLimit = ETH_BackOffLimit_10;
  423. ETH_InitStruct->ETH_DeferralCheck = ETH_DeferralCheck_Disable;
  424. ETH_InitStruct->ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  425. ETH_InitStruct->ETH_SourceAddrFilter = ETH_SourceAddrFilter_Disable;
  426. ETH_InitStruct->ETH_PassControlFrames = ETH_PassControlFrames_BlockAll;
  427. ETH_InitStruct->ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;
  428. ETH_InitStruct->ETH_DestinationAddrFilter = ETH_DestinationAddrFilter_Normal;
  429. ETH_InitStruct->ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  430. ETH_InitStruct->ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  431. ETH_InitStruct->ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  432. ETH_InitStruct->ETH_HashTableHigh = 0x0;
  433. ETH_InitStruct->ETH_HashTableLow = 0x0;
  434. ETH_InitStruct->ETH_PauseTime = 0x0;
  435. ETH_InitStruct->ETH_ZeroQuantaPause = ETH_ZeroQuantaPause_Disable;
  436. ETH_InitStruct->ETH_PauseLowThreshold = ETH_PauseLowThreshold_Minus4;
  437. ETH_InitStruct->ETH_UnicastPauseFrameDetect = ETH_UnicastPauseFrameDetect_Disable;
  438. ETH_InitStruct->ETH_ReceiveFlowControl = ETH_ReceiveFlowControl_Disable;
  439. ETH_InitStruct->ETH_TransmitFlowControl = ETH_TransmitFlowControl_Disable;
  440. ETH_InitStruct->ETH_VLANTagComparison = ETH_VLANTagComparison_16Bit;
  441. ETH_InitStruct->ETH_VLANTagIdentifier = 0x0;
  442. /*------------------------ DMA -----------------------------------*/
  443. ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Disable;
  444. ETH_InitStruct->ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  445. ETH_InitStruct->ETH_FlushReceivedFrame = ETH_FlushReceivedFrame_Disable;
  446. ETH_InitStruct->ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  447. ETH_InitStruct->ETH_TransmitThresholdControl = ETH_TransmitThresholdControl_64Bytes;
  448. ETH_InitStruct->ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  449. ETH_InitStruct->ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  450. ETH_InitStruct->ETH_ReceiveThresholdControl = ETH_ReceiveThresholdControl_64Bytes;
  451. ETH_InitStruct->ETH_SecondFrameOperate = ETH_SecondFrameOperate_Disable;
  452. ETH_InitStruct->ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  453. ETH_InitStruct->ETH_FixedBurst = ETH_FixedBurst_Disable;
  454. ETH_InitStruct->ETH_RxDMABurstLength = ETH_RxDMABurstLength_1Beat;
  455. ETH_InitStruct->ETH_TxDMABurstLength = ETH_TxDMABurstLength_1Beat;
  456. ETH_InitStruct->ETH_DescriptorSkipLength = 0x0;
  457. ETH_InitStruct->ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_1_1;
  458. }
  459. /**
  460. * @brief Enables ENET MAC and DMA reception/transmission
  461. * @param None
  462. * @retval None
  463. */
  464. void ETH_Start(void)
  465. {
  466. /* Enable transmit state machine of the MAC for transmission on the MII */
  467. ETH_MACTransmissionCmd(ENABLE);
  468. /* Flush Transmit FIFO */
  469. ETH_FlushTransmitFIFO();
  470. /* Enable receive state machine of the MAC for reception from the MII */
  471. ETH_MACReceptionCmd(ENABLE);
  472. /* Start DMA transmission */
  473. ETH_DMATransmissionCmd(ENABLE);
  474. /* Start DMA reception */
  475. ETH_DMAReceptionCmd(ENABLE);
  476. }
  477. /**
  478. * @brief Transmits a packet, from application buffer, pointed by ppkt.
  479. * @param ppkt: pointer to the application's packet buffer to transmit.
  480. * @param FrameLength: Tx Packet size.
  481. * @retval ETH_ERROR: in case of Tx desc owned by DMA
  482. * ETH_SUCCESS: for correct transmission
  483. */
  484. uint32_t ETH_HandleTxPkt(uint8_t *ppkt, uint16_t FrameLength)
  485. {
  486. uint32_t offset = 0;
  487. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  488. if((DMATxDescToSet->Status & ETH_DMATxDesc_OWN) != (uint32_t)RESET)
  489. {
  490. /* Return ERROR: OWN bit set */
  491. return ETH_ERROR;
  492. }
  493. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  494. for(offset=0; offset<FrameLength; offset++)
  495. {
  496. (*(__IO uint8_t *)((DMATxDescToSet->Buffer1Addr) + offset)) = (*(ppkt + offset));
  497. }
  498. /* Setting the Frame Length: bits[12:0] */
  499. DMATxDescToSet->ControlBufferSize = (FrameLength & ETH_DMATxDesc_TBS1);
  500. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  501. DMATxDescToSet->Status |= ETH_DMATxDesc_LS | ETH_DMATxDesc_FS;
  502. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  503. DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
  504. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  505. if ((ETH->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
  506. {
  507. /* Clear TBUS ETHERNET DMA flag */
  508. ETH->DMASR = ETH_DMASR_TBUS;
  509. /* Resume DMA transmission*/
  510. ETH->DMATPDR = 0;
  511. }
  512. /* Update the ETHERNET DMA global Tx descriptor with next Tx decriptor */
  513. /* Chained Mode */
  514. if((DMATxDescToSet->Status & ETH_DMATxDesc_TCH) != (uint32_t)RESET)
  515. {
  516. /* Selects the next DMA Tx descriptor list for next buffer to send */
  517. DMATxDescToSet = (ETH_DMADESCTypeDef*) (DMATxDescToSet->Buffer2NextDescAddr);
  518. }
  519. else /* Ring Mode */
  520. {
  521. if((DMATxDescToSet->Status & ETH_DMATxDesc_TER) != (uint32_t)RESET)
  522. {
  523. /* Selects the first DMA Tx descriptor for next buffer to send: last Tx descriptor was used */
  524. DMATxDescToSet = (ETH_DMADESCTypeDef*) (ETH->DMATDLAR);
  525. }
  526. else
  527. {
  528. /* Selects the next DMA Tx descriptor list for next buffer to send */
  529. DMATxDescToSet = (ETH_DMADESCTypeDef*) ((uint32_t)DMATxDescToSet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  530. }
  531. }
  532. /* Return SUCCESS */
  533. return ETH_SUCCESS;
  534. }
  535. /**
  536. * @brief Receives a packet and copies it to memory pointed by ppkt.
  537. * @param ppkt: pointer to the application packet receive buffer.
  538. * @retval ETH_ERROR: if there is error in reception
  539. * framelength: received packet size if packet reception is correct
  540. */
  541. uint32_t ETH_HandleRxPkt(uint8_t *ppkt)
  542. {
  543. uint32_t offset = 0, framelength = 0;
  544. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  545. if((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) != (uint32_t)RESET)
  546. {
  547. /* Return error: OWN bit set */
  548. return ETH_ERROR;
  549. }
  550. if(((DMARxDescToGet->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  551. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET) &&
  552. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET))
  553. {
  554. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  555. framelength = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  556. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  557. for(offset=0; offset<framelength; offset++)
  558. {
  559. (*(ppkt + offset)) = (*(__IO uint8_t *)((DMARxDescToGet->Buffer1Addr) + offset));
  560. }
  561. }
  562. else
  563. {
  564. /* Return ERROR */
  565. framelength = ETH_ERROR;
  566. }
  567. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  568. DMARxDescToGet->Status = ETH_DMARxDesc_OWN;
  569. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  570. if ((ETH->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  571. {
  572. /* Clear RBUS ETHERNET DMA flag */
  573. ETH->DMASR = ETH_DMASR_RBUS;
  574. /* Resume DMA reception */
  575. ETH->DMARPDR = 0;
  576. }
  577. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  578. /* Chained Mode */
  579. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RCH) != (uint32_t)RESET)
  580. {
  581. /* Selects the next DMA Rx descriptor list for next buffer to read */
  582. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  583. }
  584. else /* Ring Mode */
  585. {
  586. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RER) != (uint32_t)RESET)
  587. {
  588. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  589. DMARxDescToGet = (ETH_DMADESCTypeDef*) (ETH->DMARDLAR);
  590. }
  591. else
  592. {
  593. /* Selects the next DMA Rx descriptor list for next buffer to read */
  594. DMARxDescToGet = (ETH_DMADESCTypeDef*) ((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  595. }
  596. }
  597. /* Return Frame Length/ERROR */
  598. return (framelength);
  599. }
  600. /**
  601. * @brief Get the size of received the received packet.
  602. * @param None
  603. * @retval framelength: received packet size
  604. */
  605. uint32_t ETH_GetRxPktSize(void)
  606. {
  607. uint32_t frameLength = 0;
  608. if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  609. ((DMARxDescToGet->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  610. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET) &&
  611. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET))
  612. {
  613. /* Get the size of the packet: including 4 bytes of the CRC */
  614. frameLength = ETH_GetDMARxDescFrameLength(DMARxDescToGet);
  615. }
  616. /* Return Frame Length */
  617. return frameLength;
  618. }
  619. /**
  620. * @brief Drop a Received packet (too small packet, etc...)
  621. * @param None
  622. * @retval None
  623. */
  624. void ETH_DropRxPkt(void)
  625. {
  626. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  627. DMARxDescToGet->Status = ETH_DMARxDesc_OWN;
  628. /* Chained Mode */
  629. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RCH) != (uint32_t)RESET)
  630. {
  631. /* Selects the next DMA Rx descriptor list for next buffer read */
  632. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  633. }
  634. else /* Ring Mode */
  635. {
  636. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RER) != (uint32_t)RESET)
  637. {
  638. /* Selects the next DMA Rx descriptor list for next buffer read: this will
  639. be the first Rx descriptor in this case */
  640. DMARxDescToGet = (ETH_DMADESCTypeDef*) (ETH->DMARDLAR);
  641. }
  642. else
  643. {
  644. /* Selects the next DMA Rx descriptor list for next buffer read */
  645. DMARxDescToGet = (ETH_DMADESCTypeDef*) ((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  646. }
  647. }
  648. }
  649. /*--------------------------------- PHY ------------------------------------*/
  650. /**
  651. * @brief Read a PHY register
  652. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  653. * This parameter can be one of the following values: 0,..,31
  654. * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
  655. * This parameter can be one of the following values:
  656. * @arg PHY_BCR: Tranceiver Basic Control Register
  657. * @arg PHY_BSR: Tranceiver Basic Status Register
  658. * @arg PHY_SR : Tranceiver Status Register
  659. * @arg More PHY register could be read depending on the used PHY
  660. * @retval ETH_ERROR: in case of timeout
  661. * MAC MIIDR register value: Data read from the selected PHY register (correct read )
  662. */
  663. uint16_t ETH_ReadPHYRegister(uint16_t PHYAddress, uint16_t PHYReg)
  664. {
  665. uint32_t tmpreg = 0;
  666. __IO uint32_t timeout = 0;
  667. /* Check the parameters */
  668. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  669. assert_param(IS_ETH_PHY_REG(PHYReg));
  670. /* Get the ETHERNET MACMIIAR value */
  671. tmpreg = ETH->MACMIIAR;
  672. /* Keep only the CSR Clock Range CR[2:0] bits value */
  673. tmpreg &= ~MACMIIAR_CR_MASK;
  674. /* Prepare the MII address register value */
  675. tmpreg |=(((uint32_t)PHYAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  676. tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  677. tmpreg &= ~ETH_MACMIIAR_MW; /* Set the read mode */
  678. tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  679. /* Write the result value into the MII Address register */
  680. ETH->MACMIIAR = tmpreg;
  681. /* Check for the Busy flag */
  682. do
  683. {
  684. timeout++;
  685. tmpreg = ETH->MACMIIAR;
  686. } while ((tmpreg & ETH_MACMIIAR_MB) && (timeout < (uint32_t)PHY_READ_TO));
  687. /* Return ERROR in case of timeout */
  688. if(timeout == PHY_READ_TO)
  689. {
  690. return (uint16_t)ETH_ERROR;
  691. }
  692. /* Return data register value */
  693. return (uint16_t)(ETH->MACMIIDR);
  694. }
  695. /**
  696. * @brief Write to a PHY register
  697. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  698. * This parameter can be one of the following values: 0,..,31
  699. * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
  700. * This parameter can be one of the following values:
  701. * @arg PHY_BCR : Tranceiver Control Register
  702. * @arg More PHY register could be written depending on the used PHY
  703. * @param PHYValue: the value to write
  704. * @retval ETH_ERROR: in case of timeout
  705. * ETH_SUCCESS: for correct write
  706. */
  707. uint32_t ETH_WritePHYRegister(uint16_t PHYAddress, uint16_t PHYReg, uint16_t PHYValue)
  708. {
  709. uint32_t tmpreg = 0;
  710. __IO uint32_t timeout = 0;
  711. /* Check the parameters */
  712. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  713. assert_param(IS_ETH_PHY_REG(PHYReg));
  714. /* Get the ETHERNET MACMIIAR value */
  715. tmpreg = ETH->MACMIIAR;
  716. /* Keep only the CSR Clock Range CR[2:0] bits value */
  717. tmpreg &= ~MACMIIAR_CR_MASK;
  718. /* Prepare the MII register address value */
  719. tmpreg |=(((uint32_t)PHYAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  720. tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  721. tmpreg |= ETH_MACMIIAR_MW; /* Set the write mode */
  722. tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  723. /* Give the value to the MII data register */
  724. ETH->MACMIIDR = PHYValue;
  725. /* Write the result value into the MII Address register */
  726. ETH->MACMIIAR = tmpreg;
  727. /* Check for the Busy flag */
  728. do
  729. {
  730. timeout++;
  731. tmpreg = ETH->MACMIIAR;
  732. } while ((tmpreg & ETH_MACMIIAR_MB) && (timeout < (uint32_t)PHY_WRITE_TO));
  733. /* Return ERROR in case of timeout */
  734. if(timeout == PHY_WRITE_TO)
  735. {
  736. return ETH_ERROR;
  737. }
  738. /* Return SUCCESS */
  739. return ETH_SUCCESS;
  740. }
  741. /**
  742. * @brief Enables or disables the PHY loopBack mode.
  743. * @Note: Don't be confused with ETH_MACLoopBackCmd function which enables internal
  744. * loopback at MII level
  745. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  746. * This parameter can be one of the following values:
  747. * @param NewState: new state of the PHY loopBack mode.
  748. * This parameter can be: ENABLE or DISABLE.
  749. * @retval ETH_ERROR: in case of bad PHY configuration
  750. * ETH_SUCCESS: for correct PHY configuration
  751. */
  752. uint32_t ETH_PHYLoopBackCmd(uint16_t PHYAddress, FunctionalState NewState)
  753. {
  754. uint16_t tmpreg = 0;
  755. /* Check the parameters */
  756. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  757. assert_param(IS_FUNCTIONAL_STATE(NewState));
  758. /* Get the PHY configuration to update it */
  759. tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_BCR);
  760. if (NewState != DISABLE)
  761. {
  762. /* Enable the PHY loopback mode */
  763. tmpreg |= PHY_Loopback;
  764. }
  765. else
  766. {
  767. /* Disable the PHY loopback mode: normal mode */
  768. tmpreg &= (uint16_t)(~(uint16_t)PHY_Loopback);
  769. }
  770. /* Update the PHY control register with the new configuration */
  771. if(ETH_WritePHYRegister(PHYAddress, PHY_BCR, tmpreg) != (uint32_t)RESET)
  772. {
  773. return ETH_SUCCESS;
  774. }
  775. else
  776. {
  777. /* Return SUCCESS */
  778. return ETH_ERROR;
  779. }
  780. }
  781. /*--------------------------------- MAC ------------------------------------*/
  782. /**
  783. * @brief Enables or disables the MAC transmission.
  784. * @param NewState: new state of the MAC transmission.
  785. * This parameter can be: ENABLE or DISABLE.
  786. * @retval None
  787. */
  788. void ETH_MACTransmissionCmd(FunctionalState NewState)
  789. {
  790. /* Check the parameters */
  791. assert_param(IS_FUNCTIONAL_STATE(NewState));
  792. if (NewState != DISABLE)
  793. {
  794. /* Enable the MAC transmission */
  795. ETH->MACCR |= ETH_MACCR_TE;
  796. }
  797. else
  798. {
  799. /* Disable the MAC transmission */
  800. ETH->MACCR &= ~ETH_MACCR_TE;
  801. }
  802. }
  803. /**
  804. * @brief Enables or disables the MAC reception.
  805. * @param NewState: new state of the MAC reception.
  806. * This parameter can be: ENABLE or DISABLE.
  807. * @retval None
  808. */
  809. void ETH_MACReceptionCmd(FunctionalState NewState)
  810. {
  811. /* Check the parameters */
  812. assert_param(IS_FUNCTIONAL_STATE(NewState));
  813. if (NewState != DISABLE)
  814. {
  815. /* Enable the MAC reception */
  816. ETH->MACCR |= ETH_MACCR_RE;
  817. }
  818. else
  819. {
  820. /* Disable the MAC reception */
  821. ETH->MACCR &= ~ETH_MACCR_RE;
  822. }
  823. }
  824. /**
  825. * @brief Checks whether the ETHERNET flow control busy bit is set or not.
  826. * @param None
  827. * @retval The new state of flow control busy status bit (SET or RESET).
  828. */
  829. FlagStatus ETH_GetFlowControlBusyStatus(void)
  830. {
  831. FlagStatus bitstatus = RESET;
  832. /* The Flow Control register should not be written to until this bit is cleared */
  833. if ((ETH->MACFCR & ETH_MACFCR_FCBBPA) != (uint32_t)RESET)
  834. {
  835. bitstatus = SET;
  836. }
  837. else
  838. {
  839. bitstatus = RESET;
  840. }
  841. return bitstatus;
  842. }
  843. /**
  844. * @brief Initiate a Pause Control Frame (Full-duplex only).
  845. * @param None
  846. * @retval None
  847. */
  848. void ETH_InitiatePauseControlFrame(void)
  849. {
  850. /* When Set In full duplex MAC initiates pause control frame */
  851. ETH->MACFCR |= ETH_MACFCR_FCBBPA;
  852. }
  853. /**
  854. * @brief Enables or disables the MAC BackPressure operation activation (Half-duplex only).
  855. * @param NewState: new state of the MAC BackPressure operation activation.
  856. * This parameter can be: ENABLE or DISABLE.
  857. * @retval None
  858. */
  859. void ETH_BackPressureActivationCmd(FunctionalState NewState)
  860. {
  861. /* Check the parameters */
  862. assert_param(IS_FUNCTIONAL_STATE(NewState));
  863. if (NewState != DISABLE)
  864. {
  865. /* Activate the MAC BackPressure operation */
  866. /* In Half duplex: during backpressure, when the MAC receives a new frame,
  867. the transmitter starts sending a JAM pattern resulting in a collision */
  868. ETH->MACFCR |= ETH_MACFCR_FCBBPA;
  869. }
  870. else
  871. {
  872. /* Desactivate the MAC BackPressure operation */
  873. ETH->MACFCR &= ~ETH_MACFCR_FCBBPA;
  874. }
  875. }
  876. /**
  877. * @brief Checks whether the specified ETHERNET MAC flag is set or not.
  878. * @param ETH_MAC_FLAG: specifies the flag to check.
  879. * This parameter can be one of the following values:
  880. * @arg ETH_MAC_FLAG_TST : Time stamp trigger flag
  881. * @arg ETH_MAC_FLAG_MMCT : MMC transmit flag
  882. * @arg ETH_MAC_FLAG_MMCR : MMC receive flag
  883. * @arg ETH_MAC_FLAG_MMC : MMC flag
  884. * @arg ETH_MAC_FLAG_PMT : PMT flag
  885. * @retval The new state of ETHERNET MAC flag (SET or RESET).
  886. */
  887. FlagStatus ETH_GetMACFlagStatus(uint32_t ETH_MAC_FLAG)
  888. {
  889. FlagStatus bitstatus = RESET;
  890. /* Check the parameters */
  891. assert_param(IS_ETH_MAC_GET_FLAG(ETH_MAC_FLAG));
  892. if ((ETH->MACSR & ETH_MAC_FLAG) != (uint32_t)RESET)
  893. {
  894. bitstatus = SET;
  895. }
  896. else
  897. {
  898. bitstatus = RESET;
  899. }
  900. return bitstatus;
  901. }
  902. /**
  903. * @brief Checks whether the specified ETHERNET MAC interrupt has occurred or not.
  904. * @param ETH_MAC_IT: specifies the interrupt source to check.
  905. * This parameter can be one of the following values:
  906. * @arg ETH_MAC_IT_TST : Time stamp trigger interrupt
  907. * @arg ETH_MAC_IT_MMCT : MMC transmit interrupt
  908. * @arg ETH_MAC_IT_MMCR : MMC receive interrupt
  909. * @arg ETH_MAC_IT_MMC : MMC interrupt
  910. * @arg ETH_MAC_IT_PMT : PMT interrupt
  911. * @retval The new state of ETHERNET MAC interrupt (SET or RESET).
  912. */
  913. ITStatus ETH_GetMACITStatus(uint32_t ETH_MAC_IT)
  914. {
  915. ITStatus bitstatus = RESET;
  916. /* Check the parameters */
  917. assert_param(IS_ETH_MAC_GET_IT(ETH_MAC_IT));
  918. if ((ETH->MACSR & ETH_MAC_IT) != (uint32_t)RESET)
  919. {
  920. bitstatus = SET;
  921. }
  922. else
  923. {
  924. bitstatus = RESET;
  925. }
  926. return bitstatus;
  927. }
  928. /**
  929. * @brief Enables or disables the specified ETHERNET MAC interrupts.
  930. * @param ETH_MAC_IT: specifies the ETHERNET MAC interrupt sources to be
  931. * enabled or disabled.
  932. * This parameter can be any combination of the following values:
  933. * @arg ETH_MAC_IT_TST : Time stamp trigger interrupt
  934. * @arg ETH_MAC_IT_PMT : PMT interrupt
  935. * @param NewState: new state of the specified ETHERNET MAC interrupts.
  936. * This parameter can be: ENABLE or DISABLE.
  937. * @retval None
  938. */
  939. void ETH_MACITConfig(uint32_t ETH_MAC_IT, FunctionalState NewState)
  940. {
  941. /* Check the parameters */
  942. assert_param(IS_ETH_MAC_IT(ETH_MAC_IT));
  943. assert_param(IS_FUNCTIONAL_STATE(NewState));
  944. if (NewState != DISABLE)
  945. {
  946. /* Enable the selected ETHERNET MAC interrupts */
  947. ETH->MACIMR &= (~(uint32_t)ETH_MAC_IT);
  948. }
  949. else
  950. {
  951. /* Disable the selected ETHERNET MAC interrupts */
  952. ETH->MACIMR |= ETH_MAC_IT;
  953. }
  954. }
  955. /**
  956. * @brief Configures the selected MAC address.
  957. * @param MacAddr: The MAC addres to configure.
  958. * This parameter can be one of the following values:
  959. * @arg ETH_MAC_Address0 : MAC Address0
  960. * @arg ETH_MAC_Address1 : MAC Address1
  961. * @arg ETH_MAC_Address2 : MAC Address2
  962. * @arg ETH_MAC_Address3 : MAC Address3
  963. * @param Addr: Pointer on MAC address buffer data (6 bytes).
  964. * @retval None
  965. */
  966. void ETH_MACAddressConfig(uint32_t MacAddr, uint8_t *Addr)
  967. {
  968. uint32_t tmpreg;
  969. /* Check the parameters */
  970. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  971. /* Calculate the selectecd MAC address high register */
  972. tmpreg = ((uint32_t)Addr[5] << 8) | (uint32_t)Addr[4];
  973. /* Load the selectecd MAC address high register */
  974. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) = tmpreg;
  975. /* Calculate the selectecd MAC address low register */
  976. tmpreg = ((uint32_t)Addr[3] << 24) | ((uint32_t)Addr[2] << 16) | ((uint32_t)Addr[1] << 8) | Addr[0];
  977. /* Load the selectecd MAC address low register */
  978. (*(__IO uint32_t *) (ETH_MAC_ADDR_LBASE + MacAddr)) = tmpreg;
  979. }
  980. /**
  981. * @brief Get the selected MAC address.
  982. * @param MacAddr: The MAC addres to return.
  983. * This parameter can be one of the following values:
  984. * @arg ETH_MAC_Address0 : MAC Address0
  985. * @arg ETH_MAC_Address1 : MAC Address1
  986. * @arg ETH_MAC_Address2 : MAC Address2
  987. * @arg ETH_MAC_Address3 : MAC Address3
  988. * @param Addr: Pointer on MAC address buffer data (6 bytes).
  989. * @retval None
  990. */
  991. void ETH_GetMACAddress(uint32_t MacAddr, uint8_t *Addr)
  992. {
  993. uint32_t tmpreg;
  994. /* Check the parameters */
  995. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  996. /* Get the selectecd MAC address high register */
  997. tmpreg =(*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr));
  998. /* Calculate the selectecd MAC address buffer */
  999. Addr[5] = ((tmpreg >> 8) & (uint8_t)0xFF);
  1000. Addr[4] = (tmpreg & (uint8_t)0xFF);
  1001. /* Load the selectecd MAC address low register */
  1002. tmpreg =(*(__IO uint32_t *) (ETH_MAC_ADDR_LBASE + MacAddr));
  1003. /* Calculate the selectecd MAC address buffer */
  1004. Addr[3] = ((tmpreg >> 24) & (uint8_t)0xFF);
  1005. Addr[2] = ((tmpreg >> 16) & (uint8_t)0xFF);
  1006. Addr[1] = ((tmpreg >> 8 ) & (uint8_t)0xFF);
  1007. Addr[0] = (tmpreg & (uint8_t)0xFF);
  1008. }
  1009. /**
  1010. * @brief Enables or disables the Address filter module uses the specified
  1011. * ETHERNET MAC address for perfect filtering
  1012. * @param MacAddr: specifies the ETHERNET MAC address to be used for prfect filtering.
  1013. * This parameter can be one of the following values:
  1014. * @arg ETH_MAC_Address1 : MAC Address1
  1015. * @arg ETH_MAC_Address2 : MAC Address2
  1016. * @arg ETH_MAC_Address3 : MAC Address3
  1017. * @param NewState: new state of the specified ETHERNET MAC address use.
  1018. * This parameter can be: ENABLE or DISABLE.
  1019. * @retval None
  1020. */
  1021. void ETH_MACAddressPerfectFilterCmd(uint32_t MacAddr, FunctionalState NewState)
  1022. {
  1023. /* Check the parameters */
  1024. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  1025. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1026. if (NewState != DISABLE)
  1027. {
  1028. /* Enable the selected ETHERNET MAC address for perfect filtering */
  1029. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACA1HR_AE;
  1030. }
  1031. else
  1032. {
  1033. /* Disable the selected ETHERNET MAC address for perfect filtering */
  1034. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_AE);
  1035. }
  1036. }
  1037. /**
  1038. * @brief Set the filter type for the specified ETHERNET MAC address
  1039. * @param MacAddr: specifies the ETHERNET MAC address
  1040. * This parameter can be one of the following values:
  1041. * @arg ETH_MAC_Address1 : MAC Address1
  1042. * @arg ETH_MAC_Address2 : MAC Address2
  1043. * @arg ETH_MAC_Address3 : MAC Address3
  1044. * @param Filter: specifies the used frame received field for comparaison
  1045. * This parameter can be one of the following values:
  1046. * @arg ETH_MAC_AddressFilter_SA : MAC Address is used to compare with the
  1047. * SA fields of the received frame.
  1048. * @arg ETH_MAC_AddressFilter_DA : MAC Address is used to compare with the
  1049. * DA fields of the received frame.
  1050. * @retval None
  1051. */
  1052. void ETH_MACAddressFilterConfig(uint32_t MacAddr, uint32_t Filter)
  1053. {
  1054. /* Check the parameters */
  1055. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  1056. assert_param(IS_ETH_MAC_ADDRESS_FILTER(Filter));
  1057. if (Filter != ETH_MAC_AddressFilter_DA)
  1058. {
  1059. /* The selected ETHERNET MAC address is used to compare with the SA fields of the
  1060. received frame. */
  1061. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACA1HR_SA;
  1062. }
  1063. else
  1064. {
  1065. /* The selected ETHERNET MAC address is used to compare with the DA fields of the
  1066. received frame. */
  1067. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_SA);
  1068. }
  1069. }
  1070. /**
  1071. * @brief Set the filter type for the specified ETHERNET MAC address
  1072. * @param MacAddr: specifies the ETHERNET MAC address
  1073. * This parameter can be one of the following values:
  1074. * @arg ETH_MAC_Address1 : MAC Address1
  1075. * @arg ETH_MAC_Address2 : MAC Address2
  1076. * @arg ETH_MAC_Address3 : MAC Address3
  1077. * @param MaskByte: specifies the used address bytes for comparaison
  1078. * This parameter can be any combination of the following values:
  1079. * @arg ETH_MAC_AddressMask_Byte6 : Mask MAC Address high reg bits [15:8].
  1080. * @arg ETH_MAC_AddressMask_Byte5 : Mask MAC Address high reg bits [7:0].
  1081. * @arg ETH_MAC_AddressMask_Byte4 : Mask MAC Address low reg bits [31:24].
  1082. * @arg ETH_MAC_AddressMask_Byte3 : Mask MAC Address low reg bits [23:16].
  1083. * @arg ETH_MAC_AddressMask_Byte2 : Mask MAC Address low reg bits [15:8].
  1084. * @arg ETH_MAC_AddressMask_Byte1 : Mask MAC Address low reg bits [7:0].
  1085. * @retval None
  1086. */
  1087. void ETH_MACAddressMaskBytesFilterConfig(uint32_t MacAddr, uint32_t MaskByte)
  1088. {
  1089. /* Check the parameters */
  1090. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  1091. assert_param(IS_ETH_MAC_ADDRESS_MASK(MaskByte));
  1092. /* Clear MBC bits in the selected MAC address high register */
  1093. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_MBC);
  1094. /* Set the selected Filetr mask bytes */
  1095. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= MaskByte;
  1096. }
  1097. /*------------------------ DMA Tx/Rx Desciptors -----------------------------*/
  1098. /**
  1099. * @brief Initializes the DMA Tx descriptors in chain mode.
  1100. * @param DMATxDescTab: Pointer on the first Tx desc list
  1101. * @param TxBuff: Pointer on the first TxBuffer list
  1102. * @param TxBuffCount: Number of the used Tx desc in the list
  1103. * @retval None
  1104. */
  1105. void ETH_DMATxDescChainInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t* TxBuff, uint32_t TxBuffCount)
  1106. {
  1107. uint32_t i = 0;
  1108. ETH_DMADESCTypeDef *DMATxDesc;
  1109. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  1110. DMATxDescToSet = DMATxDescTab;
  1111. /* Fill each DMATxDesc descriptor with the right values */
  1112. for(i=0; i < TxBuffCount; i++)
  1113. {
  1114. /* Get the pointer on the ith member of the Tx Desc list */
  1115. DMATxDesc = DMATxDescTab + i;
  1116. /* Set Second Address Chained bit */
  1117. DMATxDesc->Status = ETH_DMATxDesc_TCH;
  1118. /* Set Buffer1 address pointer */
  1119. DMATxDesc->Buffer1Addr = (uint32_t)(&TxBuff[i*ETH_MAX_PACKET_SIZE]);
  1120. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  1121. if(i < (TxBuffCount-1))
  1122. {
  1123. /* Set next descriptor address register with next descriptor base address */
  1124. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1);
  1125. }
  1126. else
  1127. {
  1128. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1129. DMATxDesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  1130. }
  1131. }
  1132. /* Set Transmit Desciptor List Address Register */
  1133. ETH->DMATDLAR = (uint32_t) DMATxDescTab;
  1134. }
  1135. /**
  1136. * @brief Initializes the DMA Tx descriptors in ring mode.
  1137. * @param DMATxDescTab: Pointer on the first Tx desc list
  1138. * @param TxBuff1: Pointer on the first TxBuffer1 list
  1139. * @param TxBuff2: Pointer on the first TxBuffer2 list
  1140. * @param TxBuffCount: Number of the used Tx desc in the list
  1141. * Note: see decriptor skip length defined in ETH_DMA_InitStruct
  1142. * for the number of Words to skip between two unchained descriptors.
  1143. * @retval None
  1144. */
  1145. void ETH_DMATxDescRingInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t *TxBuff1, uint8_t *TxBuff2, uint32_t TxBuffCount)
  1146. {
  1147. uint32_t i = 0;
  1148. ETH_DMADESCTypeDef *DMATxDesc;
  1149. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  1150. DMATxDescToSet = DMATxDescTab;
  1151. /* Fill each DMATxDesc descriptor with the right values */
  1152. for(i=0; i < TxBuffCount; i++)
  1153. {
  1154. /* Get the pointer on the ith member of the Tx Desc list */
  1155. DMATxDesc = DMATxDescTab + i;
  1156. /* Set Buffer1 address pointer */
  1157. DMATxDesc->Buffer1Addr = (uint32_t)(&TxBuff1[i*ETH_MAX_PACKET_SIZE]);
  1158. /* Set Buffer2 address pointer */
  1159. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(&TxBuff2[i*ETH_MAX_PACKET_SIZE]);
  1160. /* Set Transmit End of Ring bit for last descriptor: The DMA returns to the base
  1161. address of the list, creating a Desciptor Ring */
  1162. if(i == (TxBuffCount-1))
  1163. {
  1164. /* Set Transmit End of Ring bit */
  1165. DMATxDesc->Status = ETH_DMATxDesc_TER;
  1166. }
  1167. }
  1168. /* Set Transmit Desciptor List Address Register */
  1169. ETH->DMATDLAR = (uint32_t) DMATxDescTab;
  1170. }
  1171. /**
  1172. * @brief Checks whether the specified ETHERNET DMA Tx Desc flag is set or not.
  1173. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1174. * @param ETH_DMATxDescFlag: specifies the flag to check.
  1175. * This parameter can be one of the following values:
  1176. * @arg ETH_DMATxDesc_OWN : OWN bit: descriptor is owned by DMA engine
  1177. * @arg ETH_DMATxDesc_IC : Interrupt on completetion
  1178. * @arg ETH_DMATxDesc_LS : Last Segment
  1179. * @arg ETH_DMATxDesc_FS : First Segment
  1180. * @arg ETH_DMATxDesc_DC : Disable CRC
  1181. * @arg ETH_DMATxDesc_DP : Disable Pad
  1182. * @arg ETH_DMATxDesc_TTSE: Transmit Time Stamp Enable
  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. /**
  1538. * @brief Set the specified DMA Rx Desc Own bit.
  1539. * @param DMARxDesc: Pointer on a Rx desc
  1540. * @retval None
  1541. */
  1542. void ETH_SetDMARxDescOwnBit(ETH_DMADESCTypeDef *DMARxDesc)
  1543. {
  1544. /* Set the DMA Rx Desc Own bit */
  1545. DMARxDesc->Status |= ETH_DMARxDesc_OWN;
  1546. }
  1547. /**
  1548. * @brief Returns the specified DMA Rx Desc frame length.
  1549. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1550. * @retval The Rx descriptor received frame length.
  1551. */
  1552. uint32_t ETH_GetDMARxDescFrameLength(ETH_DMADESCTypeDef *DMARxDesc)
  1553. {
  1554. /* Return the Receive descriptor frame length */
  1555. return ((DMARxDesc->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT);
  1556. }
  1557. /**
  1558. * @brief Enables or disables the specified DMA Rx Desc receive interrupt.
  1559. * @param DMARxDesc: Pointer on a Rx desc
  1560. * @param NewState: new state of the specified DMA Rx Desc interrupt.
  1561. * This parameter can be: ENABLE or DISABLE.
  1562. * @retval None
  1563. */
  1564. void ETH_DMARxDescReceiveITConfig(ETH_DMADESCTypeDef *DMARxDesc, FunctionalState NewState)
  1565. {
  1566. /* Check the parameters */
  1567. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1568. if (NewState != DISABLE)
  1569. {
  1570. /* Enable the DMA Rx Desc receive interrupt */
  1571. DMARxDesc->ControlBufferSize &=(~(uint32_t)ETH_DMARxDesc_DIC);
  1572. }
  1573. else
  1574. {
  1575. /* Disable the DMA Rx Desc receive interrupt */
  1576. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_DIC;
  1577. }
  1578. }
  1579. /**
  1580. * @brief Enables or disables the DMA Rx Desc end of ring.
  1581. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1582. * @param NewState: new state of the specified DMA Rx Desc end of ring.
  1583. * This parameter can be: ENABLE or DISABLE.
  1584. * @retval None
  1585. */
  1586. void ETH_DMARxDescEndOfRingCmd(ETH_DMADESCTypeDef *DMARxDesc, FunctionalState NewState)
  1587. {
  1588. /* Check the parameters */
  1589. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1590. if (NewState != DISABLE)
  1591. {
  1592. /* Enable the selected DMA Rx Desc end of ring */
  1593. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_RER;
  1594. }
  1595. else
  1596. {
  1597. /* Disable the selected DMA Rx Desc end of ring */
  1598. DMARxDesc->ControlBufferSize &=(~(uint32_t)ETH_DMARxDesc_RER);
  1599. }
  1600. }
  1601. /**
  1602. * @brief Enables or disables the DMA Rx Desc second address chained.
  1603. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1604. * @param NewState: new state of the specified DMA Rx Desc second address chained.
  1605. * This parameter can be: ENABLE or DISABLE.
  1606. * @retval None
  1607. */
  1608. void ETH_DMARxDescSecondAddressChainedCmd(ETH_DMADESCTypeDef *DMARxDesc, FunctionalState NewState)
  1609. {
  1610. /* Check the parameters */
  1611. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1612. if (NewState != DISABLE)
  1613. {
  1614. /* Enable the selected DMA Rx Desc second address chained */
  1615. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_RCH;
  1616. }
  1617. else
  1618. {
  1619. /* Disable the selected DMA Rx Desc second address chained */
  1620. DMARxDesc->ControlBufferSize &=(~(uint32_t)ETH_DMARxDesc_RCH);
  1621. }
  1622. }
  1623. /**
  1624. * @brief Returns the specified ETHERNET DMA Rx Desc buffer size.
  1625. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1626. * @param DMARxDesc_Buffer: specifies the DMA Rx Desc buffer.
  1627. * This parameter can be any one of the following values:
  1628. * @arg ETH_DMARxDesc_Buffer1 : DMA Rx Desc Buffer1
  1629. * @arg ETH_DMARxDesc_Buffer2 : DMA Rx Desc Buffer2
  1630. * @retval The Receive descriptor frame length.
  1631. */
  1632. uint32_t ETH_GetDMARxDescBufferSize(ETH_DMADESCTypeDef *DMARxDesc, uint32_t DMARxDesc_Buffer)
  1633. {
  1634. /* Check the parameters */
  1635. assert_param(IS_ETH_DMA_RXDESC_BUFFER(DMARxDesc_Buffer));
  1636. if(DMARxDesc_Buffer != ETH_DMARxDesc_Buffer1)
  1637. {
  1638. /* Return the DMA Rx Desc buffer2 size */
  1639. return ((DMARxDesc->ControlBufferSize & ETH_DMARxDesc_RBS2) >> ETH_DMARXDESC_BUFFER2_SIZESHIFT);
  1640. }
  1641. else
  1642. {
  1643. /* Return the DMA Rx Desc buffer1 size */
  1644. return (DMARxDesc->ControlBufferSize & ETH_DMARxDesc_RBS1);
  1645. }
  1646. }
  1647. /*--------------------------------- DMA ------------------------------------*/
  1648. /**
  1649. * @brief Resets all MAC subsystem internal registers and logic.
  1650. * @param None
  1651. * @retval None
  1652. */
  1653. void ETH_SoftwareReset(void)
  1654. {
  1655. /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
  1656. /* After reset all the registers holds their respective reset values */
  1657. ETH->DMABMR |= ETH_DMABMR_SR;
  1658. }
  1659. /**
  1660. * @brief Checks whether the ETHERNET software reset bit is set or not.
  1661. * @param None
  1662. * @retval The new state of DMA Bus Mode register SR bit (SET or RESET).
  1663. */
  1664. FlagStatus ETH_GetSoftwareResetStatus(void)
  1665. {
  1666. FlagStatus bitstatus = RESET;
  1667. if((ETH->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)
  1668. {
  1669. bitstatus = SET;
  1670. }
  1671. else
  1672. {
  1673. bitstatus = RESET;
  1674. }
  1675. return bitstatus;
  1676. }
  1677. /**
  1678. * @brief Checks whether the specified ETHERNET DMA flag is set or not.
  1679. * @param ETH_DMA_FLAG: specifies the flag to check.
  1680. * This parameter can be one of the following values:
  1681. * @arg ETH_DMA_FLAG_TST : Time-stamp trigger flag
  1682. * @arg ETH_DMA_FLAG_PMT : PMT flag
  1683. * @arg ETH_DMA_FLAG_MMC : MMC flag
  1684. * @arg ETH_DMA_FLAG_DataTransferError : Error bits 0-data buffer, 1-desc. access
  1685. * @arg ETH_DMA_FLAG_ReadWriteError : Error bits 0-write trnsf, 1-read transfr
  1686. * @arg ETH_DMA_FLAG_AccessError : Error bits 0-Rx DMA, 1-Tx DMA
  1687. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1688. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1689. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1690. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1691. * @arg ETH_DMA_FLAG_ET : Early transmit flag
  1692. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1693. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1694. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1695. * @arg ETH_DMA_FLAG_R : Receive flag
  1696. * @arg ETH_DMA_FLAG_TU : Underflow flag
  1697. * @arg ETH_DMA_FLAG_RO : Overflow flag
  1698. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1699. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1700. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1701. * @arg ETH_DMA_FLAG_T : Transmit flag
  1702. * @retval The new state of ETH_DMA_FLAG (SET or RESET).
  1703. */
  1704. FlagStatus ETH_GetDMAFlagStatus(uint32_t ETH_DMA_FLAG)
  1705. {
  1706. FlagStatus bitstatus = RESET;
  1707. /* Check the parameters */
  1708. assert_param(IS_ETH_DMA_GET_IT(ETH_DMA_FLAG));
  1709. if ((ETH->DMASR & ETH_DMA_FLAG) != (uint32_t)RESET)
  1710. {
  1711. bitstatus = SET;
  1712. }
  1713. else
  1714. {
  1715. bitstatus = RESET;
  1716. }
  1717. return bitstatus;
  1718. }
  1719. /**
  1720. * @brief Clears the ETHERNET's DMA pending flag.
  1721. * @param ETH_DMA_FLAG: specifies the flag to clear.
  1722. * This parameter can be any combination of the following values:
  1723. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1724. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1725. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1726. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1727. * @arg ETH_DMA_FLAG_ETI : Early transmit flag
  1728. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1729. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1730. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1731. * @arg ETH_DMA_FLAG_R : Receive flag
  1732. * @arg ETH_DMA_FLAG_TU : Transmit Underflow flag
  1733. * @arg ETH_DMA_FLAG_RO : Receive Overflow flag
  1734. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1735. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1736. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1737. * @arg ETH_DMA_FLAG_T : Transmit flag
  1738. * @retval None
  1739. */
  1740. void ETH_DMAClearFlag(uint32_t ETH_DMA_FLAG)
  1741. {
  1742. /* Check the parameters */
  1743. assert_param(IS_ETH_DMA_FLAG(ETH_DMA_FLAG));
  1744. /* Clear the selected ETHERNET DMA FLAG */
  1745. ETH->DMASR = (uint32_t) ETH_DMA_FLAG;
  1746. }
  1747. /**
  1748. * @brief Checks whether the specified ETHERNET DMA interrupt has occured or not.
  1749. * @param ETH_DMA_IT: specifies the interrupt source to check.
  1750. * This parameter can be one of the following values:
  1751. * @arg ETH_DMA_IT_TST : Time-stamp trigger interrupt
  1752. * @arg ETH_DMA_IT_PMT : PMT interrupt
  1753. * @arg ETH_DMA_IT_MMC : MMC interrupt
  1754. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1755. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1756. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1757. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1758. * @arg ETH_DMA_IT_ET : Early transmit interrupt
  1759. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1760. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1761. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1762. * @arg ETH_DMA_IT_R : Receive interrupt
  1763. * @arg ETH_DMA_IT_TU : Underflow interrupt
  1764. * @arg ETH_DMA_IT_RO : Overflow interrupt
  1765. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1766. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1767. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1768. * @arg ETH_DMA_IT_T : Transmit interrupt
  1769. * @retval The new state of ETH_DMA_IT (SET or RESET).
  1770. */
  1771. ITStatus ETH_GetDMAITStatus(uint32_t ETH_DMA_IT)
  1772. {
  1773. ITStatus bitstatus = RESET;
  1774. /* Check the parameters */
  1775. assert_param(IS_ETH_DMA_GET_IT(ETH_DMA_IT));
  1776. if ((ETH->DMASR & ETH_DMA_IT) != (uint32_t)RESET)
  1777. {
  1778. bitstatus = SET;
  1779. }
  1780. else
  1781. {
  1782. bitstatus = RESET;
  1783. }
  1784. return bitstatus;
  1785. }
  1786. /**
  1787. * @brief Clears the ETHERNET's DMA IT pending bit.
  1788. * @param ETH_DMA_IT: specifies the interrupt pending bit to clear.
  1789. * This parameter can be any combination of the following values:
  1790. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1791. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1792. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1793. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1794. * @arg ETH_DMA_IT_ETI : Early transmit interrupt
  1795. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1796. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1797. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1798. * @arg ETH_DMA_IT_R : Receive interrupt
  1799. * @arg ETH_DMA_IT_TU : Transmit Underflow interrupt
  1800. * @arg ETH_DMA_IT_RO : Receive Overflow interrupt
  1801. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1802. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1803. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1804. * @arg ETH_DMA_IT_T : Transmit interrupt
  1805. * @retval None
  1806. */
  1807. void ETH_DMAClearITPendingBit(uint32_t ETH_DMA_IT)
  1808. {
  1809. /* Check the parameters */
  1810. assert_param(IS_ETH_DMA_IT(ETH_DMA_IT));
  1811. /* Clear the selected ETHERNET DMA IT */
  1812. ETH->DMASR = (uint32_t) ETH_DMA_IT;
  1813. }
  1814. /**
  1815. * @brief Returns the ETHERNET DMA Transmit Process State.
  1816. * @param None
  1817. * @retval The new ETHERNET DMA Transmit Process State:
  1818. * This can be one of the following values:
  1819. * - ETH_DMA_TransmitProcess_Stopped : Stopped - Reset or Stop Tx Command issued
  1820. * - ETH_DMA_TransmitProcess_Fetching : Running - fetching the Tx descriptor
  1821. * - ETH_DMA_TransmitProcess_Waiting : Running - waiting for status
  1822. * - ETH_DMA_TransmitProcess_Reading : unning - reading the data from host memory
  1823. * - ETH_DMA_TransmitProcess_Suspended : Suspended - Tx Desciptor unavailabe
  1824. * - ETH_DMA_TransmitProcess_Closing : Running - closing Rx descriptor
  1825. */
  1826. uint32_t ETH_GetTransmitProcessState(void)
  1827. {
  1828. return ((uint32_t)(ETH->DMASR & ETH_DMASR_TS));
  1829. }
  1830. /**
  1831. * @brief Returns the ETHERNET DMA Receive Process State.
  1832. * @param None
  1833. * @retval The new ETHERNET DMA Receive Process State:
  1834. * This can be one of the following values:
  1835. * - ETH_DMA_ReceiveProcess_Stopped : Stopped - Reset or Stop Rx Command issued
  1836. * - ETH_DMA_ReceiveProcess_Fetching : Running - fetching the Rx descriptor
  1837. * - ETH_DMA_ReceiveProcess_Waiting : Running - waiting for packet
  1838. * - ETH_DMA_ReceiveProcess_Suspended : Suspended - Rx Desciptor unavailable
  1839. * - ETH_DMA_ReceiveProcess_Closing : Running - closing descriptor
  1840. * - ETH_DMA_ReceiveProcess_Queuing : Running - queuing the recieve frame into host memory
  1841. */
  1842. uint32_t ETH_GetReceiveProcessState(void)
  1843. {
  1844. return ((uint32_t)(ETH->DMASR & ETH_DMASR_RS));
  1845. }
  1846. /**
  1847. * @brief Clears the ETHERNET transmit FIFO.
  1848. * @param None
  1849. * @retval None
  1850. */
  1851. void ETH_FlushTransmitFIFO(void)
  1852. {
  1853. /* Set the Flush Transmit FIFO bit */
  1854. ETH->DMAOMR |= ETH_DMAOMR_FTF;
  1855. }
  1856. /**
  1857. * @brief Checks whether the ETHERNET transmit FIFO bit is cleared or not.
  1858. * @param None
  1859. * @retval The new state of ETHERNET flush transmit FIFO bit (SET or RESET).
  1860. */
  1861. FlagStatus ETH_GetFlushTransmitFIFOStatus(void)
  1862. {
  1863. FlagStatus bitstatus = RESET;
  1864. if ((ETH->DMAOMR & ETH_DMAOMR_FTF) != (uint32_t)RESET)
  1865. {
  1866. bitstatus = SET;
  1867. }
  1868. else
  1869. {
  1870. bitstatus = RESET;
  1871. }
  1872. return bitstatus;
  1873. }
  1874. /**
  1875. * @brief Enables or disables the DMA transmission.
  1876. * @param NewState: new state of the DMA transmission.
  1877. * This parameter can be: ENABLE or DISABLE.
  1878. * @retval None
  1879. */
  1880. void ETH_DMATransmissionCmd(FunctionalState NewState)
  1881. {
  1882. /* Check the parameters */
  1883. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1884. if (NewState != DISABLE)
  1885. {
  1886. /* Enable the DMA transmission */
  1887. ETH->DMAOMR |= ETH_DMAOMR_ST;
  1888. }
  1889. else
  1890. {
  1891. /* Disable the DMA transmission */
  1892. ETH->DMAOMR &= ~ETH_DMAOMR_ST;
  1893. }
  1894. }
  1895. /**
  1896. * @brief Enables or disables the DMA reception.
  1897. * @param NewState: new state of the DMA reception.
  1898. * This parameter can be: ENABLE or DISABLE.
  1899. * @retval None
  1900. */
  1901. void ETH_DMAReceptionCmd(FunctionalState NewState)
  1902. {
  1903. /* Check the parameters */
  1904. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1905. if (NewState != DISABLE)
  1906. {
  1907. /* Enable the DMA reception */
  1908. ETH->DMAOMR |= ETH_DMAOMR_SR;
  1909. }
  1910. else
  1911. {
  1912. /* Disable the DMA reception */
  1913. ETH->DMAOMR &= ~ETH_DMAOMR_SR;
  1914. }
  1915. }
  1916. /**
  1917. * @brief Enables or disables the specified ETHERNET DMA interrupts.
  1918. * @param ETH_DMA_IT: specifies the ETHERNET DMA interrupt sources to be
  1919. * enabled or disabled.
  1920. * This parameter can be any combination of the following values:
  1921. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1922. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1923. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1924. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1925. * @arg ETH_DMA_IT_ET : Early transmit interrupt
  1926. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1927. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1928. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1929. * @arg ETH_DMA_IT_R : Receive interrupt
  1930. * @arg ETH_DMA_IT_TU : Underflow interrupt
  1931. * @arg ETH_DMA_IT_RO : Overflow interrupt
  1932. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1933. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1934. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1935. * @arg ETH_DMA_IT_T : Transmit interrupt
  1936. * @param NewState: new state of the specified ETHERNET DMA interrupts.
  1937. * This parameter can be: ENABLE or DISABLE.
  1938. * @retval None
  1939. */
  1940. void ETH_DMAITConfig(uint32_t ETH_DMA_IT, FunctionalState NewState)
  1941. {
  1942. /* Check the parameters */
  1943. assert_param(IS_ETH_DMA_IT(ETH_DMA_IT));
  1944. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1945. if (NewState != DISABLE)
  1946. {
  1947. /* Enable the selected ETHERNET DMA interrupts */
  1948. ETH->DMAIER |= ETH_DMA_IT;
  1949. }
  1950. else
  1951. {
  1952. /* Disable the selected ETHERNET DMA interrupts */
  1953. ETH->DMAIER &=(~(uint32_t)ETH_DMA_IT);
  1954. }
  1955. }
  1956. /**
  1957. * @brief Checks whether the specified ETHERNET DMA overflow flag is set or not.
  1958. * @param ETH_DMA_Overflow: specifies the DMA overflow flag to check.
  1959. * This parameter can be one of the following values:
  1960. * @arg ETH_DMA_Overflow_RxFIFOCounter : Overflow for FIFO Overflow Counter
  1961. * @arg ETH_DMA_Overflow_MissedFrameCounter : Overflow for Missed Frame Counter
  1962. * @retval The new state of ETHERNET DMA overflow Flag (SET or RESET).
  1963. */
  1964. FlagStatus ETH_GetDMAOverflowStatus(uint32_t ETH_DMA_Overflow)
  1965. {
  1966. FlagStatus bitstatus = RESET;
  1967. /* Check the parameters */
  1968. assert_param(IS_ETH_DMA_GET_OVERFLOW(ETH_DMA_Overflow));
  1969. if ((ETH->DMAMFBOCR & ETH_DMA_Overflow) != (uint32_t)RESET)
  1970. {
  1971. bitstatus = SET;
  1972. }
  1973. else
  1974. {
  1975. bitstatus = RESET;
  1976. }
  1977. return bitstatus;
  1978. }
  1979. /**
  1980. * @brief Get the ETHERNET DMA Rx Overflow Missed Frame Counter value.
  1981. * @param None
  1982. * @retval The value of Rx overflow Missed Frame Counter.
  1983. */
  1984. uint32_t ETH_GetRxOverflowMissedFrameCounter(void)
  1985. {
  1986. return ((uint32_t)((ETH->DMAMFBOCR & ETH_DMAMFBOCR_MFA)>>ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTERSHIFT));
  1987. }
  1988. /**
  1989. * @brief Get the ETHERNET DMA Buffer Unavailable Missed Frame Counter value.
  1990. * @param None
  1991. * @retval The value of Buffer unavailable Missed Frame Counter.
  1992. */
  1993. uint32_t ETH_GetBufferUnavailableMissedFrameCounter(void)
  1994. {
  1995. return ((uint32_t)(ETH->DMAMFBOCR) & ETH_DMAMFBOCR_MFC);
  1996. }
  1997. /**
  1998. * @brief Get the ETHERNET DMA DMACHTDR register value.
  1999. * @param None
  2000. * @retval The value of the current Tx desc start address.
  2001. */
  2002. uint32_t ETH_GetCurrentTxDescStartAddress(void)
  2003. {
  2004. return ((uint32_t)(ETH->DMACHTDR));
  2005. }
  2006. /**
  2007. * @brief Get the ETHERNET DMA DMACHRDR register value.
  2008. * @param None
  2009. * @retval The value of the current Rx desc start address.
  2010. */
  2011. uint32_t ETH_GetCurrentRxDescStartAddress(void)
  2012. {
  2013. return ((uint32_t)(ETH->DMACHRDR));
  2014. }
  2015. /**
  2016. * @brief Get the ETHERNET DMA DMACHTBAR register value.
  2017. * @param None
  2018. * @retval The value of the current Tx buffer address.
  2019. */
  2020. uint32_t ETH_GetCurrentTxBufferAddress(void)
  2021. {
  2022. return ((uint32_t)(ETH->DMACHTBAR));
  2023. }
  2024. /**
  2025. * @brief Get the ETHERNET DMA DMACHRBAR register value.
  2026. * @param None
  2027. * @retval The value of the current Rx buffer address.
  2028. */
  2029. uint32_t ETH_GetCurrentRxBufferAddress(void)
  2030. {
  2031. return ((uint32_t)(ETH->DMACHRBAR));
  2032. }
  2033. /**
  2034. * @brief Resumes the DMA Transmission by writing to the DmaTxPollDemand register
  2035. * (the data written could be anything). This forces the DMA to resume transmission.
  2036. * @param None
  2037. * @retval None.
  2038. */
  2039. void ETH_ResumeDMATransmission(void)
  2040. {
  2041. ETH->DMATPDR = 0;
  2042. }
  2043. /**
  2044. * @brief Resumes the DMA Transmission by writing to the DmaRxPollDemand register
  2045. * (the data written could be anything). This forces the DMA to resume reception.
  2046. * @param None
  2047. * @retval None.
  2048. */
  2049. void ETH_ResumeDMAReception(void)
  2050. {
  2051. ETH->DMARPDR = 0;
  2052. }
  2053. /*--------------------------------- PMT ------------------------------------*/
  2054. /**
  2055. * @brief Reset Wakeup frame filter register pointer.
  2056. * @param None
  2057. * @retval None
  2058. */
  2059. void ETH_ResetWakeUpFrameFilterRegisterPointer(void)
  2060. {
  2061. /* Resets the Remote Wake-up Frame Filter register pointer to 0x0000 */
  2062. ETH->MACPMTCSR |= ETH_MACPMTCSR_WFFRPR;
  2063. }
  2064. /**
  2065. * @brief Populates the remote wakeup frame registers.
  2066. * @param Buffer: Pointer on remote WakeUp Frame Filter Register buffer data (8 words).
  2067. * @retval None
  2068. */
  2069. void ETH_SetWakeUpFrameFilterRegister(uint32_t *Buffer)
  2070. {
  2071. uint32_t i = 0;
  2072. /* Fill Remote Wake-up Frame Filter register with Buffer data */
  2073. for(i =0; i<ETH_WAKEUP_REGISTER_LENGTH; i++)
  2074. {
  2075. /* Write each time to the same register */
  2076. ETH->MACRWUFFR = Buffer[i];
  2077. }
  2078. }
  2079. /**
  2080. * @brief Enables or disables any unicast packet filtered by the MAC address
  2081. * recognition to be a wake-up frame.
  2082. * @param NewState: new state of the MAC Global Unicast Wake-Up.
  2083. * This parameter can be: ENABLE or DISABLE.
  2084. * @retval None
  2085. */
  2086. void ETH_GlobalUnicastWakeUpCmd(FunctionalState NewState)
  2087. {
  2088. /* Check the parameters */
  2089. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2090. if (NewState != DISABLE)
  2091. {
  2092. /* Enable the MAC Global Unicast Wake-Up */
  2093. ETH->MACPMTCSR |= ETH_MACPMTCSR_GU;
  2094. }
  2095. else
  2096. {
  2097. /* Disable the MAC Global Unicast Wake-Up */
  2098. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_GU;
  2099. }
  2100. }
  2101. /**
  2102. * @brief Checks whether the specified ETHERNET PMT flag is set or not.
  2103. * @param ETH_PMT_FLAG: specifies the flag to check.
  2104. * This parameter can be one of the following values:
  2105. * @arg ETH_PMT_FLAG_WUFFRPR : Wake-Up Frame Filter Register Poniter Reset
  2106. * @arg ETH_PMT_FLAG_WUFR : Wake-Up Frame Received
  2107. * @arg ETH_PMT_FLAG_MPR : Magic Packet Received
  2108. * @retval The new state of ETHERNET PMT Flag (SET or RESET).
  2109. */
  2110. FlagStatus ETH_GetPMTFlagStatus(uint32_t ETH_PMT_FLAG)
  2111. {
  2112. FlagStatus bitstatus = RESET;
  2113. /* Check the parameters */
  2114. assert_param(IS_ETH_PMT_GET_FLAG(ETH_PMT_FLAG));
  2115. if ((ETH->MACPMTCSR & ETH_PMT_FLAG) != (uint32_t)RESET)
  2116. {
  2117. bitstatus = SET;
  2118. }
  2119. else
  2120. {
  2121. bitstatus = RESET;
  2122. }
  2123. return bitstatus;
  2124. }
  2125. /**
  2126. * @brief Enables or disables the MAC Wake-Up Frame Detection.
  2127. * @param NewState: new state of the MAC Wake-Up Frame Detection.
  2128. * This parameter can be: ENABLE or DISABLE.
  2129. * @retval None
  2130. */
  2131. void ETH_WakeUpFrameDetectionCmd(FunctionalState NewState)
  2132. {
  2133. /* Check the parameters */
  2134. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2135. if (NewState != DISABLE)
  2136. {
  2137. /* Enable the MAC Wake-Up Frame Detection */
  2138. ETH->MACPMTCSR |= ETH_MACPMTCSR_WFE;
  2139. }
  2140. else
  2141. {
  2142. /* Disable the MAC Wake-Up Frame Detection */
  2143. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_WFE;
  2144. }
  2145. }
  2146. /**
  2147. * @brief Enables or disables the MAC Magic Packet Detection.
  2148. * @param NewState: new state of the MAC Magic Packet Detection.
  2149. * This parameter can be: ENABLE or DISABLE.
  2150. * @retval None
  2151. */
  2152. void ETH_MagicPacketDetectionCmd(FunctionalState NewState)
  2153. {
  2154. /* Check the parameters */
  2155. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2156. if (NewState != DISABLE)
  2157. {
  2158. /* Enable the MAC Magic Packet Detection */
  2159. ETH->MACPMTCSR |= ETH_MACPMTCSR_MPE;
  2160. }
  2161. else
  2162. {
  2163. /* Disable the MAC Magic Packet Detection */
  2164. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_MPE;
  2165. }
  2166. }
  2167. /**
  2168. * @brief Enables or disables the MAC Power Down.
  2169. * @param NewState: new state of the MAC Power Down.
  2170. * This parameter can be: ENABLE or DISABLE.
  2171. * @retval None
  2172. */
  2173. void ETH_PowerDownCmd(FunctionalState NewState)
  2174. {
  2175. /* Check the parameters */
  2176. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2177. if (NewState != DISABLE)
  2178. {
  2179. /* Enable the MAC Power Down */
  2180. /* This puts the MAC in power down mode */
  2181. ETH->MACPMTCSR |= ETH_MACPMTCSR_PD;
  2182. }
  2183. else
  2184. {
  2185. /* Disable the MAC Power Down */
  2186. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_PD;
  2187. }
  2188. }
  2189. /*--------------------------------- MMC ------------------------------------*/
  2190. /**
  2191. * @brief Enables or disables the MMC Counter Freeze.
  2192. * @param NewState: new state of the MMC Counter Freeze.
  2193. * This parameter can be: ENABLE or DISABLE.
  2194. * @retval None
  2195. */
  2196. void ETH_MMCCounterFreezeCmd(FunctionalState NewState)
  2197. {
  2198. /* Check the parameters */
  2199. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2200. if (NewState != DISABLE)
  2201. {
  2202. /* Enable the MMC Counter Freeze */
  2203. ETH->MMCCR |= ETH_MMCCR_MCF;
  2204. }
  2205. else
  2206. {
  2207. /* Disable the MMC Counter Freeze */
  2208. ETH->MMCCR &= ~ETH_MMCCR_MCF;
  2209. }
  2210. }
  2211. /**
  2212. * @brief Enables or disables the MMC Reset On Read.
  2213. * @param NewState: new state of the MMC Reset On Read.
  2214. * This parameter can be: ENABLE or DISABLE.
  2215. * @retval None
  2216. */
  2217. void ETH_MMCResetOnReadCmd(FunctionalState NewState)
  2218. {
  2219. /* Check the parameters */
  2220. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2221. if (NewState != DISABLE)
  2222. {
  2223. /* Enable the MMC Counter reset on read */
  2224. ETH->MMCCR |= ETH_MMCCR_ROR;
  2225. }
  2226. else
  2227. {
  2228. /* Disable the MMC Counter reset on read */
  2229. ETH->MMCCR &= ~ETH_MMCCR_ROR;
  2230. }
  2231. }
  2232. /**
  2233. * @brief Enables or disables the MMC Counter Stop Rollover.
  2234. * @param NewState: new state of the MMC Counter Stop Rollover.
  2235. * This parameter can be: ENABLE or DISABLE.
  2236. * @retval None
  2237. */
  2238. void ETH_MMCCounterRolloverCmd(FunctionalState NewState)
  2239. {
  2240. /* Check the parameters */
  2241. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2242. if (NewState != DISABLE)
  2243. {
  2244. /* Disable the MMC Counter Stop Rollover */
  2245. ETH->MMCCR &= ~ETH_MMCCR_CSR;
  2246. }
  2247. else
  2248. {
  2249. /* Enable the MMC Counter Stop Rollover */
  2250. ETH->MMCCR |= ETH_MMCCR_CSR;
  2251. }
  2252. }
  2253. /**
  2254. * @brief Resets the MMC Counters.
  2255. * @param None
  2256. * @retval None
  2257. */
  2258. void ETH_MMCCountersReset(void)
  2259. {
  2260. /* Resets the MMC Counters */
  2261. ETH->MMCCR |= ETH_MMCCR_CR;
  2262. }
  2263. /**
  2264. * @brief Enables or disables the specified ETHERNET MMC interrupts.
  2265. * @param ETH_MMC_IT: specifies the ETHERNET MMC interrupt sources to be enabled or disabled.
  2266. * This parameter can be any combination of Tx interrupt or
  2267. * any combination of Rx interrupt (but not both)of the following values:
  2268. * @arg ETH_MMC_IT_TGF : When Tx good frame counter reaches half the maximum value
  2269. * @arg ETH_MMC_IT_TGFMSC: When Tx good multi col counter reaches half the maximum value
  2270. * @arg ETH_MMC_IT_TGFSC : When Tx good single col counter reaches half the maximum value
  2271. * @arg ETH_MMC_IT_RGUF : When Rx good unicast frames counter reaches half the maximum value
  2272. * @arg ETH_MMC_IT_RFAE : When Rx alignment error counter reaches half the maximum value
  2273. * @arg ETH_MMC_IT_RFCE : When Rx crc error counter reaches half the maximum value
  2274. * @param NewState: new state of the specified ETHERNET MMC interrupts.
  2275. * This parameter can be: ENABLE or DISABLE.
  2276. * @retval None
  2277. */
  2278. void ETH_MMCITConfig(uint32_t ETH_MMC_IT, FunctionalState NewState)
  2279. {
  2280. /* Check the parameters */
  2281. assert_param(IS_ETH_MMC_IT(ETH_MMC_IT));
  2282. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2283. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2284. {
  2285. /* Remove egister mak from IT */
  2286. ETH_MMC_IT &= 0xEFFFFFFF;
  2287. /* ETHERNET MMC Rx interrupts selected */
  2288. if (NewState != DISABLE)
  2289. {
  2290. /* Enable the selected ETHERNET MMC interrupts */
  2291. ETH->MMCRIMR &=(~(uint32_t)ETH_MMC_IT);
  2292. }
  2293. else
  2294. {
  2295. /* Disable the selected ETHERNET MMC interrupts */
  2296. ETH->MMCRIMR |= ETH_MMC_IT;
  2297. }
  2298. }
  2299. else
  2300. {
  2301. /* ETHERNET MMC Tx interrupts selected */
  2302. if (NewState != DISABLE)
  2303. {
  2304. /* Enable the selected ETHERNET MMC interrupts */
  2305. ETH->MMCTIMR &=(~(uint32_t)ETH_MMC_IT);
  2306. }
  2307. else
  2308. {
  2309. /* Disable the selected ETHERNET MMC interrupts */
  2310. ETH->MMCTIMR |= ETH_MMC_IT;
  2311. }
  2312. }
  2313. }
  2314. /**
  2315. * @brief Checks whether the specified ETHERNET MMC IT is set or not.
  2316. * @param ETH_MMC_IT: specifies the ETHERNET MMC interrupt.
  2317. * This parameter can be one of the following values:
  2318. * @arg ETH_MMC_IT_TxFCGC: When Tx good frame counter reaches half the maximum value
  2319. * @arg ETH_MMC_IT_TxMCGC: When Tx good multi col counter reaches half the maximum value
  2320. * @arg ETH_MMC_IT_TxSCGC: When Tx good single col counter reaches half the maximum value
  2321. * @arg ETH_MMC_IT_RxUGFC: When Rx good unicast frames counter reaches half the maximum value
  2322. * @arg ETH_MMC_IT_RxAEC : When Rx alignment error counter reaches half the maximum value
  2323. * @arg ETH_MMC_IT_RxCEC : When Rx crc error counter reaches half the maximum value
  2324. * @retval The value of ETHERNET MMC IT (SET or RESET).
  2325. */
  2326. ITStatus ETH_GetMMCITStatus(uint32_t ETH_MMC_IT)
  2327. {
  2328. ITStatus bitstatus = RESET;
  2329. /* Check the parameters */
  2330. assert_param(IS_ETH_MMC_GET_IT(ETH_MMC_IT));
  2331. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2332. {
  2333. /* ETHERNET MMC Rx interrupts selected */
  2334. /* Check if the ETHERNET MMC Rx selected interrupt is enabled and occured */
  2335. if ((((ETH->MMCRIR & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRIMR & ETH_MMC_IT) != (uint32_t)RESET))
  2336. {
  2337. bitstatus = SET;
  2338. }
  2339. else
  2340. {
  2341. bitstatus = RESET;
  2342. }
  2343. }
  2344. else
  2345. {
  2346. /* ETHERNET MMC Tx interrupts selected */
  2347. /* Check if the ETHERNET MMC Tx selected interrupt is enabled and occured */
  2348. if ((((ETH->MMCTIR & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRIMR & ETH_MMC_IT) != (uint32_t)RESET))
  2349. {
  2350. bitstatus = SET;
  2351. }
  2352. else
  2353. {
  2354. bitstatus = RESET;
  2355. }
  2356. }
  2357. return bitstatus;
  2358. }
  2359. /**
  2360. * @brief Get the specified ETHERNET MMC register value.
  2361. * @param ETH_MMCReg: specifies the ETHERNET MMC register.
  2362. * This parameter can be one of the following values:
  2363. * @arg ETH_MMCCR : MMC CR register
  2364. * @arg ETH_MMCRIR : MMC RIR register
  2365. * @arg ETH_MMCTIR : MMC TIR register
  2366. * @arg ETH_MMCRIMR : MMC RIMR register
  2367. * @arg ETH_MMCTIMR : MMC TIMR register
  2368. * @arg ETH_MMCTGFSCCR : MMC TGFSCCR register
  2369. * @arg ETH_MMCTGFMSCCR: MMC TGFMSCCR register
  2370. * @arg ETH_MMCTGFCR : MMC TGFCR register
  2371. * @arg ETH_MMCRFCECR : MMC RFCECR register
  2372. * @arg ETH_MMCRFAECR : MMC RFAECR register
  2373. * @arg ETH_MMCRGUFCR : MMC RGUFCRregister
  2374. * @retval The value of ETHERNET MMC Register value.
  2375. */
  2376. uint32_t ETH_GetMMCRegister(uint32_t ETH_MMCReg)
  2377. {
  2378. /* Check the parameters */
  2379. assert_param(IS_ETH_MMC_REGISTER(ETH_MMCReg));
  2380. /* Return the selected register value */
  2381. return (*(__IO uint32_t *)(ETH_MAC_BASE + ETH_MMCReg));
  2382. }
  2383. /*--------------------------------- PTP ------------------------------------*/
  2384. /**
  2385. * @brief Updated the PTP block for fine correction with the Time Stamp Addend register value.
  2386. * @param None
  2387. * @retval None
  2388. */
  2389. void ETH_EnablePTPTimeStampAddend(void)
  2390. {
  2391. /* Enable the PTP block update with the Time Stamp Addend register value */
  2392. ETH->PTPTSCR |= ETH_PTPTSCR_TSARU;
  2393. }
  2394. /**
  2395. * @brief Enable the PTP Time Stamp interrupt trigger
  2396. * @param None
  2397. * @retval None
  2398. */
  2399. void ETH_EnablePTPTimeStampInterruptTrigger(void)
  2400. {
  2401. /* Enable the PTP target time interrupt */
  2402. ETH->PTPTSCR |= ETH_PTPTSCR_TSITE;
  2403. }
  2404. /**
  2405. * @brief Updated the PTP system time with the Time Stamp Update register value.
  2406. * @param None
  2407. * @retval None
  2408. */
  2409. void ETH_EnablePTPTimeStampUpdate(void)
  2410. {
  2411. /* Enable the PTP system time update with the Time Stamp Update register value */
  2412. ETH->PTPTSCR |= ETH_PTPTSCR_TSSTU;
  2413. }
  2414. /**
  2415. * @brief Initialize the PTP Time Stamp
  2416. * @param None
  2417. * @retval None
  2418. */
  2419. void ETH_InitializePTPTimeStamp(void)
  2420. {
  2421. /* Initialize the PTP Time Stamp */
  2422. ETH->PTPTSCR |= ETH_PTPTSCR_TSSTI;
  2423. }
  2424. /**
  2425. * @brief Selects the PTP Update method
  2426. * @param UpdateMethod: the PTP Update method
  2427. * This parameter can be one of the following values:
  2428. * @arg ETH_PTP_FineUpdate : Fine Update method
  2429. * @arg ETH_PTP_CoarseUpdate : Coarse Update method
  2430. * @retval None
  2431. */
  2432. void ETH_PTPUpdateMethodConfig(uint32_t UpdateMethod)
  2433. {
  2434. /* Check the parameters */
  2435. assert_param(IS_ETH_PTP_UPDATE(UpdateMethod));
  2436. if (UpdateMethod != ETH_PTP_CoarseUpdate)
  2437. {
  2438. /* Enable the PTP Fine Update method */
  2439. ETH->PTPTSCR |= ETH_PTPTSCR_TSFCU;
  2440. }
  2441. else
  2442. {
  2443. /* Disable the PTP Coarse Update method */
  2444. ETH->PTPTSCR &= (~(uint32_t)ETH_PTPTSCR_TSFCU);
  2445. }
  2446. }
  2447. /**
  2448. * @brief Enables or disables the PTP time stamp for transmit and receive frames.
  2449. * @param NewState: new state of the PTP time stamp for transmit and receive frames
  2450. * This parameter can be: ENABLE or DISABLE.
  2451. * @retval None
  2452. */
  2453. void ETH_PTPTimeStampCmd(FunctionalState NewState)
  2454. {
  2455. /* Check the parameters */
  2456. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2457. if (NewState != DISABLE)
  2458. {
  2459. /* Enable the PTP time stamp for transmit and receive frames */
  2460. ETH->PTPTSCR |= ETH_PTPTSCR_TSE;
  2461. }
  2462. else
  2463. {
  2464. /* Disable the PTP time stamp for transmit and receive frames */
  2465. ETH->PTPTSCR &= (~(uint32_t)ETH_PTPTSCR_TSE);
  2466. }
  2467. }
  2468. /**
  2469. * @brief Checks whether the specified ETHERNET PTP flag is set or not.
  2470. * @param ETH_PTP_FLAG: specifies the flag to check.
  2471. * This parameter can be one of the following values:
  2472. * @arg ETH_PTP_FLAG_TSARU : Addend Register Update
  2473. * @arg ETH_PTP_FLAG_TSITE : Time Stamp Interrupt Trigger Enable
  2474. * @arg ETH_PTP_FLAG_TSSTU : Time Stamp Update
  2475. * @arg ETH_PTP_FLAG_TSSTI : Time Stamp Initialize
  2476. * @retval The new state of ETHERNET PTP Flag (SET or RESET).
  2477. */
  2478. FlagStatus ETH_GetPTPFlagStatus(uint32_t ETH_PTP_FLAG)
  2479. {
  2480. FlagStatus bitstatus = RESET;
  2481. /* Check the parameters */
  2482. assert_param(IS_ETH_PTP_GET_FLAG(ETH_PTP_FLAG));
  2483. if ((ETH->PTPTSCR & ETH_PTP_FLAG) != (uint32_t)RESET)
  2484. {
  2485. bitstatus = SET;
  2486. }
  2487. else
  2488. {
  2489. bitstatus = RESET;
  2490. }
  2491. return bitstatus;
  2492. }
  2493. /**
  2494. * @brief Sets the system time Sub-Second Increment value.
  2495. * @param SubSecondValue: specifies the PTP Sub-Second Increment Register value.
  2496. * @retval None
  2497. */
  2498. void ETH_SetPTPSubSecondIncrement(uint32_t SubSecondValue)
  2499. {
  2500. /* Check the parameters */
  2501. assert_param(IS_ETH_PTP_SUBSECOND_INCREMENT(SubSecondValue));
  2502. /* Set the PTP Sub-Second Increment Register */
  2503. ETH->PTPSSIR = SubSecondValue;
  2504. }
  2505. /**
  2506. * @brief Sets the Time Stamp update sign and values.
  2507. * @param Sign: specifies the PTP Time update value sign.
  2508. * This parameter can be one of the following values:
  2509. * @arg ETH_PTP_PositiveTime : positive time value.
  2510. * @arg ETH_PTP_NegativeTime : negative time value.
  2511. * @param SecondValue: specifies the PTP Time update second value.
  2512. * @param SubSecondValue: specifies the PTP Time update sub-second value.
  2513. * This parameter is a 31 bit value, bit32 correspond to the sign.
  2514. * @retval None
  2515. */
  2516. void ETH_SetPTPTimeStampUpdate(uint32_t Sign, uint32_t SecondValue, uint32_t SubSecondValue)
  2517. {
  2518. /* Check the parameters */
  2519. assert_param(IS_ETH_PTP_TIME_SIGN(Sign));
  2520. assert_param(IS_ETH_PTP_TIME_STAMP_UPDATE_SUBSECOND(SubSecondValue));
  2521. /* Set the PTP Time Update High Register */
  2522. ETH->PTPTSHUR = SecondValue;
  2523. /* Set the PTP Time Update Low Register with sign */
  2524. ETH->PTPTSLUR = Sign | SubSecondValue;
  2525. }
  2526. /**
  2527. * @brief Sets the Time Stamp Addend value.
  2528. * @param Value: specifies the PTP Time Stamp Addend Register value.
  2529. * @retval None
  2530. */
  2531. void ETH_SetPTPTimeStampAddend(uint32_t Value)
  2532. {
  2533. /* Set the PTP Time Stamp Addend Register */
  2534. ETH->PTPTSAR = Value;
  2535. }
  2536. /**
  2537. * @brief Sets the Target Time registers values.
  2538. * @param HighValue: specifies the PTP Target Time High Register value.
  2539. * @param LowValue: specifies the PTP Target Time Low Register value.
  2540. * @retval None
  2541. */
  2542. void ETH_SetPTPTargetTime(uint32_t HighValue, uint32_t LowValue)
  2543. {
  2544. /* Set the PTP Target Time High Register */
  2545. ETH->PTPTTHR = HighValue;
  2546. /* Set the PTP Target Time Low Register */
  2547. ETH->PTPTTLR = LowValue;
  2548. }
  2549. /**
  2550. * @brief Get the specified ETHERNET PTP register value.
  2551. * @param ETH_PTPReg: specifies the ETHERNET PTP register.
  2552. * This parameter can be one of the following values:
  2553. * @arg ETH_PTPTSCR : Sub-Second Increment Register
  2554. * @arg ETH_PTPSSIR : Sub-Second Increment Register
  2555. * @arg ETH_PTPTSHR : Time Stamp High Register
  2556. * @arg ETH_PTPTSLR : Time Stamp Low Register
  2557. * @arg ETH_PTPTSHUR : Time Stamp High Update Register
  2558. * @arg ETH_PTPTSLUR : Time Stamp Low Update Register
  2559. * @arg ETH_PTPTSAR : Time Stamp Addend Register
  2560. * @arg ETH_PTPTTHR : Target Time High Register
  2561. * @arg ETH_PTPTTLR : Target Time Low Register
  2562. * @retval The value of ETHERNET PTP Register value.
  2563. */
  2564. uint32_t ETH_GetPTPRegister(uint32_t ETH_PTPReg)
  2565. {
  2566. /* Check the parameters */
  2567. assert_param(IS_ETH_PTP_REGISTER(ETH_PTPReg));
  2568. /* Return the selected register value */
  2569. return (*(__IO uint32_t *)(ETH_MAC_BASE + ETH_PTPReg));
  2570. }
  2571. /**
  2572. * @brief Initializes the DMA Tx descriptors in chain mode with PTP.
  2573. * @param DMATxDescTab: Pointer on the first Tx desc list
  2574. * @param DMAPTPTxDescTab: Pointer on the first PTP Tx desc list
  2575. * @param TxBuff: Pointer on the first TxBuffer list
  2576. * @param TxBuffCount: Number of the used Tx desc in the list
  2577. * @retval None
  2578. */
  2579. void ETH_DMAPTPTxDescChainInit(ETH_DMADESCTypeDef *DMATxDescTab, ETH_DMADESCTypeDef *DMAPTPTxDescTab,
  2580. uint8_t* TxBuff, uint32_t TxBuffCount)
  2581. {
  2582. uint32_t i = 0;
  2583. ETH_DMADESCTypeDef *DMATxDesc;
  2584. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  2585. DMATxDescToSet = DMATxDescTab;
  2586. DMAPTPTxDescToSet = DMAPTPTxDescTab;
  2587. /* Fill each DMATxDesc descriptor with the right values */
  2588. for(i=0; i < TxBuffCount; i++)
  2589. {
  2590. /* Get the pointer on the ith member of the Tx Desc list */
  2591. DMATxDesc = DMATxDescTab+i;
  2592. /* Set Second Address Chained bit and enable PTP */
  2593. DMATxDesc->Status = ETH_DMATxDesc_TCH | ETH_DMATxDesc_TTSE;
  2594. /* Set Buffer1 address pointer */
  2595. DMATxDesc->Buffer1Addr =(uint32_t)(&TxBuff[i*ETH_MAX_PACKET_SIZE]);
  2596. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  2597. if(i < (TxBuffCount-1))
  2598. {
  2599. /* Set next descriptor address register with next descriptor base address */
  2600. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1);
  2601. }
  2602. else
  2603. {
  2604. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  2605. DMATxDesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  2606. }
  2607. /* make DMAPTPTxDescTab points to the same addresses as DMATxDescTab */
  2608. (&DMAPTPTxDescTab[i])->Buffer1Addr = DMATxDesc->Buffer1Addr;
  2609. (&DMAPTPTxDescTab[i])->Buffer2NextDescAddr = DMATxDesc->Buffer2NextDescAddr;
  2610. }
  2611. /* Store on the last DMAPTPTxDescTab desc status record the first list address */
  2612. (&DMAPTPTxDescTab[i-1])->Status = (uint32_t) DMAPTPTxDescTab;
  2613. /* Set Transmit Desciptor List Address Register */
  2614. ETH->DMATDLAR = (uint32_t) DMATxDescTab;
  2615. }
  2616. /**
  2617. * @brief Initializes the DMA Rx descriptors in chain mode.
  2618. * @param DMARxDescTab: Pointer on the first Rx desc list
  2619. * @param DMAPTPRxDescTab: Pointer on the first PTP Rx desc list
  2620. * @param RxBuff: Pointer on the first RxBuffer list
  2621. * @param RxBuffCount: Number of the used Rx desc in the list
  2622. * @retval None
  2623. */
  2624. void ETH_DMAPTPRxDescChainInit(ETH_DMADESCTypeDef *DMARxDescTab, ETH_DMADESCTypeDef *DMAPTPRxDescTab,
  2625. uint8_t *RxBuff, uint32_t RxBuffCount)
  2626. {
  2627. uint32_t i = 0;
  2628. ETH_DMADESCTypeDef *DMARxDesc;
  2629. /* Set the DMARxDescToGet pointer with the first one of the DMARxDescTab list */
  2630. DMARxDescToGet = DMARxDescTab;
  2631. DMAPTPRxDescToGet = DMAPTPRxDescTab;
  2632. /* Fill each DMARxDesc descriptor with the right values */
  2633. for(i=0; i < RxBuffCount; i++)
  2634. {
  2635. /* Get the pointer on the ith member of the Rx Desc list */
  2636. DMARxDesc = DMARxDescTab+i;
  2637. /* Set Own bit of the Rx descriptor Status */
  2638. DMARxDesc->Status = ETH_DMARxDesc_OWN;
  2639. /* Set Buffer1 size and Second Address Chained bit */
  2640. DMARxDesc->ControlBufferSize = ETH_DMARxDesc_RCH | (uint32_t)ETH_MAX_PACKET_SIZE;
  2641. /* Set Buffer1 address pointer */
  2642. DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*ETH_MAX_PACKET_SIZE]);
  2643. /* Initialize the next descriptor with the Next Desciptor Polling Enable */
  2644. if(i < (RxBuffCount-1))
  2645. {
  2646. /* Set next descriptor address register with next descriptor base address */
  2647. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab+i+1);
  2648. }
  2649. else
  2650. {
  2651. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  2652. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
  2653. }
  2654. /* Make DMAPTPRxDescTab points to the same addresses as DMARxDescTab */
  2655. (&DMAPTPRxDescTab[i])->Buffer1Addr = DMARxDesc->Buffer1Addr;
  2656. (&DMAPTPRxDescTab[i])->Buffer2NextDescAddr = DMARxDesc->Buffer2NextDescAddr;
  2657. }
  2658. /* Store on the last DMAPTPRxDescTab desc status record the first list address */
  2659. (&DMAPTPRxDescTab[i-1])->Status = (uint32_t) DMAPTPRxDescTab;
  2660. /* Set Receive Desciptor List Address Register */
  2661. ETH->DMARDLAR = (uint32_t) DMARxDescTab;
  2662. }
  2663. /**
  2664. * @brief Transmits a packet, from application buffer, pointed by ppkt with Time Stamp values.
  2665. * @param ppkt: pointer to application packet buffer to transmit.
  2666. * @param FrameLength: Tx Packet size.
  2667. * @param PTPTxTab: Pointer on the first PTP Tx table to store Time stamp values.
  2668. * @retval ETH_ERROR: in case of Tx desc owned by DMA
  2669. * ETH_SUCCESS: for correct transmission
  2670. */
  2671. uint32_t ETH_HandlePTPTxPkt(uint8_t *ppkt, uint16_t FrameLength, uint32_t *PTPTxTab)
  2672. {
  2673. uint32_t offset = 0, timeout = 0;
  2674. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  2675. if((DMATxDescToSet->Status & ETH_DMATxDesc_OWN) != (uint32_t)RESET)
  2676. {
  2677. /* Return ERROR: OWN bit set */
  2678. return ETH_ERROR;
  2679. }
  2680. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  2681. for(offset=0; offset<FrameLength; offset++)
  2682. {
  2683. (*(__IO uint8_t *)((DMAPTPTxDescToSet->Buffer1Addr) + offset)) = (*(ppkt + offset));
  2684. }
  2685. /* Setting the Frame Length: bits[12:0] */
  2686. DMATxDescToSet->ControlBufferSize = (FrameLength & (uint32_t)0x1FFF);
  2687. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  2688. DMATxDescToSet->Status |= ETH_DMATxDesc_LS | ETH_DMATxDesc_FS;
  2689. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  2690. DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
  2691. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  2692. if ((ETH->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
  2693. {
  2694. /* Clear TBUS ETHERNET DMA flag */
  2695. ETH->DMASR = ETH_DMASR_TBUS;
  2696. /* Resume DMA transmission*/
  2697. ETH->DMATPDR = 0;
  2698. }
  2699. /* Wait for ETH_DMATxDesc_TTSS flag to be set */
  2700. do
  2701. {
  2702. timeout++;
  2703. } while (!(DMATxDescToSet->Status & ETH_DMATxDesc_TTSS) && (timeout < 0xFFFF));
  2704. /* Return ERROR in case of timeout */
  2705. if(timeout == PHY_READ_TO)
  2706. {
  2707. return ETH_ERROR;
  2708. }
  2709. /* Clear the DMATxDescToSet status register TTSS flag */
  2710. DMATxDescToSet->Status &= ~ETH_DMATxDesc_TTSS;
  2711. *PTPTxTab++ = DMATxDescToSet->Buffer1Addr;
  2712. *PTPTxTab = DMATxDescToSet->Buffer2NextDescAddr;
  2713. /* Update the ENET DMA current descriptor */
  2714. /* Chained Mode */
  2715. if((DMATxDescToSet->Status & ETH_DMATxDesc_TCH) != (uint32_t)RESET)
  2716. {
  2717. /* Selects the next DMA Tx descriptor list for next buffer read */
  2718. DMATxDescToSet = (ETH_DMADESCTypeDef*) (DMAPTPTxDescToSet->Buffer2NextDescAddr);
  2719. if(DMAPTPTxDescToSet->Status != 0)
  2720. {
  2721. DMAPTPTxDescToSet = (ETH_DMADESCTypeDef*) (DMAPTPTxDescToSet->Status);
  2722. }
  2723. else
  2724. {
  2725. DMAPTPTxDescToSet++;
  2726. }
  2727. }
  2728. else /* Ring Mode */
  2729. {
  2730. if((DMATxDescToSet->Status & ETH_DMATxDesc_TER) != (uint32_t)RESET)
  2731. {
  2732. /* Selects the next DMA Tx descriptor list for next buffer read: this will
  2733. be the first Tx descriptor in this case */
  2734. DMATxDescToSet = (ETH_DMADESCTypeDef*) (ETH->DMATDLAR);
  2735. DMAPTPTxDescToSet = (ETH_DMADESCTypeDef*) (ETH->DMATDLAR);
  2736. }
  2737. else
  2738. {
  2739. /* Selects the next DMA Tx descriptor list for next buffer read */
  2740. DMATxDescToSet = (ETH_DMADESCTypeDef*) ((uint32_t)DMATxDescToSet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  2741. DMAPTPTxDescToSet = (ETH_DMADESCTypeDef*) ((uint32_t)DMAPTPTxDescToSet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  2742. }
  2743. }
  2744. /* Return SUCCESS */
  2745. return ETH_SUCCESS;
  2746. }
  2747. /**
  2748. * @brief Receives a packet and copies it to memory pointed by ppkt with Time Stamp values.
  2749. * @param ppkt: pointer to application packet receive buffer.
  2750. * @param PTPRxTab: Pointer on the first PTP Rx table to store Time stamp values.
  2751. * @retval ETH_ERROR: if there is error in reception
  2752. * framelength: received packet size if packet reception is correct
  2753. */
  2754. uint32_t ETH_HandlePTPRxPkt(uint8_t *ppkt, uint32_t *PTPRxTab)
  2755. {
  2756. uint32_t offset = 0, framelength = 0;
  2757. /* Check if the descriptor is owned by the ENET or CPU */
  2758. if((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) != (uint32_t)RESET)
  2759. {
  2760. /* Return error: OWN bit set */
  2761. return ETH_ERROR;
  2762. }
  2763. if(((DMARxDescToGet->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  2764. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET) &&
  2765. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET))
  2766. {
  2767. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  2768. framelength = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  2769. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  2770. for(offset=0; offset<framelength; offset++)
  2771. {
  2772. (*(ppkt + offset)) = (*(__IO uint8_t *)((DMAPTPRxDescToGet->Buffer1Addr) + offset));
  2773. }
  2774. }
  2775. else
  2776. {
  2777. /* Return ERROR */
  2778. framelength = ETH_ERROR;
  2779. }
  2780. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  2781. if ((ETH->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  2782. {
  2783. /* Clear RBUS ETHERNET DMA flag */
  2784. ETH->DMASR = ETH_DMASR_RBUS;
  2785. /* Resume DMA reception */
  2786. ETH->DMARPDR = 0;
  2787. }
  2788. *PTPRxTab++ = DMARxDescToGet->Buffer1Addr;
  2789. *PTPRxTab = DMARxDescToGet->Buffer2NextDescAddr;
  2790. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  2791. DMARxDescToGet->Status |= ETH_DMARxDesc_OWN;
  2792. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  2793. /* Chained Mode */
  2794. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RCH) != (uint32_t)RESET)
  2795. {
  2796. /* Selects the next DMA Rx descriptor list for next buffer read */
  2797. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMAPTPRxDescToGet->Buffer2NextDescAddr);
  2798. if(DMAPTPRxDescToGet->Status != 0)
  2799. {
  2800. DMAPTPRxDescToGet = (ETH_DMADESCTypeDef*) (DMAPTPRxDescToGet->Status);
  2801. }
  2802. else
  2803. {
  2804. DMAPTPRxDescToGet++;
  2805. }
  2806. }
  2807. else /* Ring Mode */
  2808. {
  2809. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RER) != (uint32_t)RESET)
  2810. {
  2811. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  2812. DMARxDescToGet = (ETH_DMADESCTypeDef*) (ETH->DMARDLAR);
  2813. }
  2814. else
  2815. {
  2816. /* Selects the next DMA Rx descriptor list for next buffer to read */
  2817. DMARxDescToGet = (ETH_DMADESCTypeDef*) ((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  2818. }
  2819. }
  2820. /* Return Frame Length/ERROR */
  2821. return (framelength);
  2822. }
  2823. /**
  2824. * @}
  2825. */
  2826. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  2827. /*
  2828. * STM32 Eth Driver for RT-Thread
  2829. * Change Logs:
  2830. * Date Author Notes
  2831. * 2009-10-05 Bernard eth interface driver for STM32F107 CL
  2832. */
  2833. #include <rtthread.h>
  2834. #include <netif/ethernetif.h>
  2835. #include <netif/etharp.h>
  2836. #include <lwip/icmp.h>
  2837. #include "lwipopts.h"
  2838. #define ETH_DEBUG
  2839. //#define ETH_RX_DUMP
  2840. //#define ETH_TX_DUMP
  2841. #ifdef ETH_DEBUG
  2842. #define STM32_ETH_TRACE rt_kprintf
  2843. #else
  2844. #define STM32_ETH_TRACE(...)
  2845. #endif
  2846. #define ETH_RXBUFNB 4
  2847. #define ETH_TXBUFNB 2
  2848. static ETH_InitTypeDef ETH_InitStructure;
  2849. static ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
  2850. static rt_uint8_t Rx_Buff[ETH_RXBUFNB][ETH_MAX_PACKET_SIZE], Tx_Buff[ETH_TXBUFNB][ETH_MAX_PACKET_SIZE];
  2851. #define MAX_ADDR_LEN 6
  2852. struct rt_stm32_eth
  2853. {
  2854. /* inherit from ethernet device */
  2855. struct eth_device parent;
  2856. /* interface address info. */
  2857. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  2858. };
  2859. static struct rt_stm32_eth stm32_eth_device;
  2860. static struct rt_semaphore tx_buf_free;
  2861. /* interrupt service routine for ETH */
  2862. void ETH_IRQHandler(void)
  2863. {
  2864. rt_uint32_t status;
  2865. /* enter interrupt */
  2866. rt_interrupt_enter();
  2867. /* get DMA IT status */
  2868. status = ETH->DMASR;
  2869. if ( (status & ETH_DMA_IT_R) != (u32)RESET ) /* packet receiption */
  2870. {
  2871. /* a frame has been received */
  2872. eth_device_ready(&(stm32_eth_device.parent));
  2873. ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
  2874. }
  2875. if ( (status & ETH_DMA_IT_T) != (u32)RESET ) /* packet transmission */
  2876. {
  2877. rt_sem_release(&tx_buf_free);
  2878. ETH_DMAClearITPendingBit(ETH_DMA_IT_T);
  2879. }
  2880. /* Clear received IT */
  2881. if ((status & ETH_DMA_IT_NIS) != (u32)RESET)
  2882. ETH->DMASR = (u32)ETH_DMA_IT_NIS;
  2883. if ((status & ETH_DMA_IT_AIS) != (u32)RESET)
  2884. ETH->DMASR = (u32)ETH_DMA_IT_AIS;
  2885. if ((status & ETH_DMA_IT_RO) != (u32)RESET)
  2886. ETH->DMASR = (u32)ETH_DMA_IT_RO;
  2887. if ((status & ETH_DMA_IT_RBU) != (u32)RESET)
  2888. {
  2889. ETH_ResumeDMAReception();
  2890. ETH->DMASR = (u32)ETH_DMA_IT_RBU;
  2891. }
  2892. if ((status & ETH_DMA_IT_TBU) != (u32)RESET)
  2893. {
  2894. ETH_ResumeDMATransmission();
  2895. ETH->DMASR = (u32)ETH_DMA_IT_TBU;
  2896. }
  2897. /* leave interrupt */
  2898. rt_interrupt_leave();
  2899. }
  2900. #define MICR 0x11
  2901. #define MISR 0x12
  2902. void EXTI9_5_IRQHandler(void)
  2903. {
  2904. volatile rt_uint16_t status;
  2905. /* enter interrupt */
  2906. rt_interrupt_enter();
  2907. status = ETH_ReadPHYRegister(PHY_ADDRESS, MISR);
  2908. if (status & (1 << 13))
  2909. {
  2910. /* change of link */
  2911. status = ETH_ReadPHYRegister(PHY_ADDRESS, PHY_SR);
  2912. if (status & 0x01) /* link established */
  2913. {
  2914. netif_set_link_up(stm32_eth_device.parent.netif);
  2915. }
  2916. else
  2917. {
  2918. netif_set_link_down(stm32_eth_device.parent.netif);
  2919. }
  2920. }
  2921. /* Clear the Key Button EXTI line pending bit */
  2922. EXTI_ClearITPendingBit(EXTI_Line5);
  2923. /* leave interrupt */
  2924. rt_interrupt_leave();
  2925. }
  2926. void rt_eth_phy_init(void)
  2927. {
  2928. GPIO_InitTypeDef GPIO_InitStructure;
  2929. EXTI_InitTypeDef EXTI_InitStructure;
  2930. NVIC_InitTypeDef NVIC_InitStructure;
  2931. /* Configure PC5 as input for PHY interrupt */
  2932. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  2933. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  2934. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  2935. GPIO_Init(GPIOC, &GPIO_InitStructure);
  2936. /* Connect PHY Interrupt Line to GPIOC Pin 5 */
  2937. GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5);
  2938. /* Configure PHY Interrupt Line to generate an interrupt on falling edge */
  2939. EXTI_InitStructure.EXTI_Line = EXTI_Line5;
  2940. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  2941. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  2942. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  2943. EXTI_Init(&EXTI_InitStructure);
  2944. /* set PHY interrupt */
  2945. ETH_WritePHYRegister(PHY_ADDRESS, MICR, 0x0003);
  2946. ETH_WritePHYRegister(PHY_ADDRESS, MISR, 0x0060);
  2947. /* Clear PHY Interrupt Line pending bit */
  2948. EXTI_ClearITPendingBit(EXTI_Line5);
  2949. /* Enable the EXTI0 Interrupt */
  2950. NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
  2951. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  2952. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  2953. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  2954. NVIC_Init(&NVIC_InitStructure);
  2955. }
  2956. /* RT-Thread Device Interface */
  2957. /* initialize the interface */
  2958. static rt_err_t rt_stm32_eth_init(rt_device_t dev)
  2959. {
  2960. vu32 Value = 0;
  2961. /* Reset ETHERNET on AHB Bus */
  2962. ETH_DeInit();
  2963. /* Software reset */
  2964. ETH_SoftwareReset();
  2965. /* Wait for software reset */
  2966. while(ETH_GetSoftwareResetStatus()==SET);
  2967. /* ETHERNET Configuration ------------------------------------------------------*/
  2968. /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
  2969. ETH_StructInit(&ETH_InitStructure);
  2970. /* Fill ETH_InitStructure parametrs */
  2971. /*------------------------ MAC -----------------------------------*/
  2972. ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable ;
  2973. ETH_InitStructure.ETH_Speed = ETH_Speed_100M;
  2974. ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
  2975. ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  2976. ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
  2977. ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  2978. ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Enable;
  2979. ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;
  2980. ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  2981. ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  2982. ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  2983. #ifdef CHECKSUM_BY_HARDWARE
  2984. ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
  2985. #endif
  2986. /*------------------------ DMA -----------------------------------*/
  2987. /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
  2988. the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
  2989. if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
  2990. ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
  2991. ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  2992. ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  2993. ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  2994. ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  2995. ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
  2996. ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  2997. ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
  2998. ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  2999. ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  3000. ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
  3001. /* Configure ETHERNET */
  3002. Value = ETH_Init(&ETH_InitStructure, PHY_ADDRESS);
  3003. /* Enable DMA Receive interrupt (need to enable in this case Normal interrupt) */
  3004. ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R | ETH_DMA_IT_T, ENABLE);
  3005. /* Initialize Tx Descriptors list: Chain Mode */
  3006. ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
  3007. /* Initialize Rx Descriptors list: Chain Mode */
  3008. ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
  3009. /* MAC address configuration */
  3010. ETH_MACAddressConfig(ETH_MAC_Address0, (u8*)&stm32_eth_device.dev_addr[0]);
  3011. /* Enable MAC and DMA transmission and reception */
  3012. ETH_Start();
  3013. return RT_EOK;
  3014. }
  3015. static rt_err_t rt_stm32_eth_open(rt_device_t dev, rt_uint16_t oflag)
  3016. {
  3017. return RT_EOK;
  3018. }
  3019. static rt_err_t rt_stm32_eth_close(rt_device_t dev)
  3020. {
  3021. return RT_EOK;
  3022. }
  3023. static rt_size_t rt_stm32_eth_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  3024. {
  3025. rt_set_errno(-RT_ENOSYS);
  3026. return 0;
  3027. }
  3028. static rt_size_t rt_stm32_eth_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  3029. {
  3030. rt_set_errno(-RT_ENOSYS);
  3031. return 0;
  3032. }
  3033. static rt_err_t rt_stm32_eth_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  3034. {
  3035. switch(cmd)
  3036. {
  3037. case NIOCTL_GADDR:
  3038. /* get mac address */
  3039. if(args) rt_memcpy(args, stm32_eth_device.dev_addr, 6);
  3040. else return -RT_ERROR;
  3041. break;
  3042. default :
  3043. break;
  3044. }
  3045. return RT_EOK;
  3046. }
  3047. /* ethernet device interface */
  3048. /* transmit packet. */
  3049. rt_err_t rt_stm32_eth_tx( rt_device_t dev, struct pbuf* p)
  3050. {
  3051. struct pbuf* q;
  3052. rt_uint32_t offset;
  3053. /* get free tx buffer */
  3054. {
  3055. rt_err_t result;
  3056. result = rt_sem_take(&tx_buf_free, 2);
  3057. if (result != RT_EOK) return -RT_ERROR;
  3058. }
  3059. offset = 0;
  3060. for (q = p; q != NULL; q = q->next)
  3061. {
  3062. rt_uint8_t* ptr;
  3063. rt_uint32_t len;
  3064. len = q->len;
  3065. ptr = q->payload;
  3066. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  3067. while (len)
  3068. {
  3069. (*(__IO uint8_t *)((DMATxDescToSet->Buffer1Addr) + offset)) = *ptr;
  3070. offset ++;
  3071. ptr ++;
  3072. len --;
  3073. }
  3074. }
  3075. #ifdef ETH_TX_DUMP
  3076. {
  3077. rt_uint32_t i;
  3078. rt_uint8_t *ptr = (rt_uint8_t*)(DMATxDescToSet->Buffer1Addr);
  3079. STM32_ETH_TRACE("tx_dump:");
  3080. for(i=0; i<p->tot_len; i++)
  3081. {
  3082. if( (i%8) == 0 )
  3083. {
  3084. STM32_ETH_TRACE(" ");
  3085. }
  3086. if( (i%16) == 0 )
  3087. {
  3088. STM32_ETH_TRACE("\r\n");
  3089. }
  3090. STM32_ETH_TRACE("%02x ",*ptr);
  3091. ptr++;
  3092. }
  3093. STM32_ETH_TRACE("\r\ndump done!\r\n");
  3094. }
  3095. #endif
  3096. /* Setting the Frame Length: bits[12:0] */
  3097. DMATxDescToSet->ControlBufferSize = (p->tot_len & ETH_DMATxDesc_TBS1);
  3098. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  3099. DMATxDescToSet->Status |= ETH_DMATxDesc_LS | ETH_DMATxDesc_FS;
  3100. /* Enable TX Completion Interrupt */
  3101. DMATxDescToSet->Status |= ETH_DMATxDesc_IC;
  3102. #ifdef CHECKSUM_BY_HARDWARE
  3103. DMATxDescToSet->Status |= ETH_DMATxDesc_ChecksumTCPUDPICMPFull;
  3104. /* clean ICMP checksum STM32F need */
  3105. {
  3106. struct eth_hdr *ethhdr = (struct eth_hdr *)(DMATxDescToSet->Buffer1Addr);
  3107. /* is IP ? */
  3108. if( ethhdr->type == htons(ETHTYPE_IP) )
  3109. {
  3110. struct ip_hdr *iphdr = (struct ip_hdr *)(DMATxDescToSet->Buffer1Addr + SIZEOF_ETH_HDR);
  3111. /* is ICMP ? */
  3112. if( IPH_PROTO(iphdr) == IP_PROTO_ICMP )
  3113. {
  3114. struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)(DMATxDescToSet->Buffer1Addr + SIZEOF_ETH_HDR + sizeof(struct ip_hdr) );
  3115. iecho->chksum = 0;
  3116. }
  3117. }
  3118. }
  3119. #endif
  3120. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  3121. DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
  3122. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  3123. if ((ETH->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
  3124. {
  3125. /* Clear TBUS ETHERNET DMA flag */
  3126. ETH->DMASR = ETH_DMASR_TBUS;
  3127. /* Transmit Poll Demand to resume DMA transmission*/
  3128. ETH->DMATPDR = 0;
  3129. }
  3130. /* Update the ETHERNET DMA global Tx descriptor with next Tx decriptor */
  3131. /* Chained Mode */
  3132. /* Selects the next DMA Tx descriptor list for next buffer to send */
  3133. DMATxDescToSet = (ETH_DMADESCTypeDef*) (DMATxDescToSet->Buffer2NextDescAddr);
  3134. /* Return SUCCESS */
  3135. return RT_EOK;
  3136. }
  3137. /* reception packet. */
  3138. struct pbuf *rt_stm32_eth_rx(rt_device_t dev)
  3139. {
  3140. struct pbuf* p;
  3141. rt_uint32_t offset = 0, framelength = 0;
  3142. /* init p pointer */
  3143. p = RT_NULL;
  3144. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  3145. if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) != (uint32_t)RESET))
  3146. return p;
  3147. if (((DMARxDescToGet->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  3148. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET) &&
  3149. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET))
  3150. {
  3151. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  3152. framelength = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  3153. /* allocate buffer */
  3154. p = pbuf_alloc(PBUF_LINK, framelength, PBUF_RAM);
  3155. if (p != RT_NULL)
  3156. {
  3157. rt_uint8_t* ptr;
  3158. struct pbuf* q;
  3159. rt_size_t len;
  3160. for (q = p; q != RT_NULL; q= q->next)
  3161. {
  3162. ptr = q->payload;
  3163. len = q->len;
  3164. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  3165. while (len)
  3166. {
  3167. *ptr = (*(__IO uint8_t *)((DMARxDescToGet->Buffer1Addr) + offset));
  3168. offset ++;
  3169. ptr ++;
  3170. len --;
  3171. }
  3172. }
  3173. }
  3174. }
  3175. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  3176. DMARxDescToGet->Status = ETH_DMARxDesc_OWN;
  3177. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  3178. if ((ETH->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
  3179. {
  3180. /* Clear RBUS ETHERNET DMA flag */
  3181. ETH->DMASR = ETH_DMASR_RBUS;
  3182. /* Resume DMA reception */
  3183. ETH->DMARPDR = 0;
  3184. }
  3185. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  3186. /* Chained Mode */
  3187. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RCH) != (uint32_t)RESET)
  3188. {
  3189. /* Selects the next DMA Rx descriptor list for next buffer to read */
  3190. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  3191. }
  3192. else /* Ring Mode */
  3193. {
  3194. if((DMARxDescToGet->ControlBufferSize & ETH_DMARxDesc_RER) != (uint32_t)RESET)
  3195. {
  3196. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  3197. DMARxDescToGet = (ETH_DMADESCTypeDef*) (ETH->DMARDLAR);
  3198. }
  3199. else
  3200. {
  3201. /* Selects the next DMA Rx descriptor list for next buffer to read */
  3202. DMARxDescToGet = (ETH_DMADESCTypeDef*) ((uint32_t)DMARxDescToGet + 0x10 + ((ETH->DMABMR & ETH_DMABMR_DSL) >> 2));
  3203. }
  3204. }
  3205. return p;
  3206. }
  3207. static void RCC_Configuration(void)
  3208. {
  3209. /* Enable ETHERNET clock */
  3210. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC | RCC_AHBPeriph_ETH_MAC_Tx |
  3211. RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);
  3212. /* Enable GPIOs clocks */
  3213. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
  3214. RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE| RCC_APB2Periph_AFIO, ENABLE);
  3215. }
  3216. static void NVIC_Configuration(void)
  3217. {
  3218. NVIC_InitTypeDef NVIC_InitStructure;
  3219. /* Enable the EXTI0 Interrupt */
  3220. NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
  3221. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  3222. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  3223. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  3224. NVIC_Init(&NVIC_InitStructure);
  3225. }
  3226. /*
  3227. * GPIO Configuration for ETH
  3228. */
  3229. static void GPIO_Configuration(void)
  3230. {
  3231. GPIO_InitTypeDef GPIO_InitStructure;
  3232. /* ETHERNET pins remapp in STM3210C-EVAL board: RX_DV and RxD[3:0] */
  3233. GPIO_PinRemapConfig(GPIO_Remap_ETH, ENABLE);
  3234. /* MII/RMII Media interface selection */
  3235. #ifdef MII_MODE /* Mode MII with STM3210C-EVAL */
  3236. GPIO_ETH_MediaInterfaceConfig(GPIO_ETH_MediaInterface_MII);
  3237. /* Get HSE clock = 25MHz on PA8 pin(MCO) */
  3238. RCC_MCOConfig(RCC_MCO_HSE);
  3239. #elif defined RMII_MODE /* Mode RMII with STM3210C-EVAL */
  3240. GPIO_ETH_MediaInterfaceConfig(GPIO_ETH_MediaInterface_RMII);
  3241. /* Get HSE clock = 25MHz on PA8 pin(MCO) */
  3242. /* set PLL3 clock output to 50MHz (25MHz /5 *10 =50MHz) */
  3243. RCC_PLL3Config(RCC_PLL3Mul_10);
  3244. /* Enable PLL3 */
  3245. RCC_PLL3Cmd(ENABLE);
  3246. /* Wait till PLL3 is ready */
  3247. while (RCC_GetFlagStatus(RCC_FLAG_PLL3RDY) == RESET)
  3248. {}
  3249. /* Get clock PLL3 clock on PA8 pin */
  3250. RCC_MCOConfig(RCC_MCO_PLL3CLK);
  3251. #endif
  3252. /* ETHERNET pins configuration */
  3253. /* AF Output Push Pull:
  3254. - ETH_MII_MDIO / ETH_RMII_MDIO: PA2
  3255. - ETH_MII_MDC / ETH_RMII_MDC: PC1
  3256. - ETH_MII_TXD2: PC2
  3257. - ETH_MII_TX_EN / ETH_RMII_TX_EN: PB11
  3258. - ETH_MII_TXD0 / ETH_RMII_TXD0: PB12
  3259. - ETH_MII_TXD1 / ETH_RMII_TXD1: PB13
  3260. - ETH_MII_PPS_OUT / ETH_RMII_PPS_OUT: PB5
  3261. - ETH_MII_TXD3: PB8 */
  3262. /* Configure PA2 as alternate function push-pull */
  3263. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  3264. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  3265. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  3266. GPIO_Init(GPIOA, &GPIO_InitStructure);
  3267. /* Configure PC1, PC2 and PC3 as alternate function push-pull */
  3268. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
  3269. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  3270. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  3271. GPIO_Init(GPIOC, &GPIO_InitStructure);
  3272. /* Configure PB5, PB8, PB11, PB12 and PB13 as alternate function push-pull */
  3273. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_11 |
  3274. GPIO_Pin_12 | GPIO_Pin_13;
  3275. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  3276. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  3277. GPIO_Init(GPIOB, &GPIO_InitStructure);
  3278. /**************************************************************/
  3279. /* For Remapped Ethernet pins */
  3280. /*************************************************************/
  3281. /* Input (Reset Value):
  3282. - ETH_MII_CRS CRS: PA0
  3283. - ETH_MII_RX_CLK / ETH_RMII_REF_CLK: PA1
  3284. - ETH_MII_COL: PA3
  3285. - ETH_MII_RX_DV / ETH_RMII_CRS_DV: PD8
  3286. - ETH_MII_TX_CLK: PC3
  3287. - ETH_MII_RXD0 / ETH_RMII_RXD0: PD9
  3288. - ETH_MII_RXD1 / ETH_RMII_RXD1: PD10
  3289. - ETH_MII_RXD2: PD11
  3290. - ETH_MII_RXD3: PD12
  3291. - ETH_MII_RX_ER: PB10 */
  3292. /* Configure PA0, PA1 and PA3 as input */
  3293. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3;
  3294. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  3295. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  3296. GPIO_Init(GPIOA, &GPIO_InitStructure);
  3297. /* Configure PB10 as input */
  3298. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  3299. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  3300. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  3301. GPIO_Init(GPIOB, &GPIO_InitStructure);
  3302. /* Configure PC3 as input */
  3303. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  3304. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  3305. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  3306. GPIO_Init(GPIOC, &GPIO_InitStructure);
  3307. /* Configure PD8, PD9, PD10, PD11 and PD12 as input */
  3308. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
  3309. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  3310. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  3311. GPIO_Init(GPIOD, &GPIO_InitStructure); /**/
  3312. /* MCO pin configuration------------------------------------------------- */
  3313. /* Configure MCO (PA8) as alternate function push-pull */
  3314. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  3315. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  3316. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  3317. GPIO_Init(GPIOA, &GPIO_InitStructure);
  3318. }
  3319. void rt_hw_stm32_eth_init()
  3320. {
  3321. RCC_Configuration();
  3322. GPIO_Configuration();
  3323. NVIC_Configuration();
  3324. // OUI 00-80-E1 STMICROELECTRONICS
  3325. stm32_eth_device.dev_addr[0] = 0x00;
  3326. stm32_eth_device.dev_addr[1] = 0x80;
  3327. stm32_eth_device.dev_addr[2] = 0xE1;
  3328. // generate MAC addr from 96bit unique ID (only for test)
  3329. stm32_eth_device.dev_addr[3] = *(rt_uint8_t*)(0x1FFFF7E8+7);
  3330. stm32_eth_device.dev_addr[4] = *(rt_uint8_t*)(0x1FFFF7E8+8);
  3331. stm32_eth_device.dev_addr[5] = *(rt_uint8_t*)(0x1FFFF7E8+9);
  3332. stm32_eth_device.parent.parent.init = rt_stm32_eth_init;
  3333. stm32_eth_device.parent.parent.open = rt_stm32_eth_open;
  3334. stm32_eth_device.parent.parent.close = rt_stm32_eth_close;
  3335. stm32_eth_device.parent.parent.read = rt_stm32_eth_read;
  3336. stm32_eth_device.parent.parent.write = rt_stm32_eth_write;
  3337. stm32_eth_device.parent.parent.control = rt_stm32_eth_control;
  3338. stm32_eth_device.parent.parent.user_data = RT_NULL;
  3339. stm32_eth_device.parent.eth_rx = rt_stm32_eth_rx;
  3340. stm32_eth_device.parent.eth_tx = rt_stm32_eth_tx;
  3341. /* init tx buffer free semaphore */
  3342. rt_sem_init(&tx_buf_free, "tx_buf", ETH_TXBUFNB, RT_IPC_FLAG_FIFO);
  3343. /* register eth device */
  3344. eth_device_init(&(stm32_eth_device.parent), "e0");
  3345. }
  3346. #include <finsh.h>
  3347. void phy(void)
  3348. {
  3349. rt_uint16_t v;
  3350. v = ETH_ReadPHYRegister(PHY_ADDRESS, PHY_BCR);
  3351. rt_kprintf("PHY BCR: 0x%04x\n", v);
  3352. v = ETH_ReadPHYRegister(PHY_ADDRESS, PHY_BSR);
  3353. rt_kprintf("PHY BSR: 0x%04x\n", v);
  3354. v = ETH_ReadPHYRegister(PHY_ADDRESS, PHY_SR);
  3355. rt_kprintf("PHY SR: 0x%04x\n", v);
  3356. v = ETH_ReadPHYRegister(PHY_ADDRESS, 0x11);
  3357. rt_kprintf("PHY MICR: 0x%04x\n", v);
  3358. v = ETH_ReadPHYRegister(PHY_ADDRESS, 0x12);
  3359. rt_kprintf("PHY MISR: 0x%04x\n", v);
  3360. }
  3361. FINSH_FUNCTION_EXPORT(phy, read phy);