stm32f2xx_eth.c 136 KB

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