stm32_eth.c 128 KB

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