stm32f2xx_eth.c 135 KB

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