1
0

stm32_eth.c 127 KB

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