stm32_eth.c 127 KB

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