stm32_eth.c 129 KB

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