stm32_eth.c 131 KB

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