stm32f2x7_eth.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2x7_eth.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 25-April-2011
  7. * @brief This file is the low level driver for STM32F2x7 Ethernet Controller.
  8. * This driver does not include low level functions for PTP time-stamp.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f2x7_eth.h"
  24. #include "stm32f2xx_rcc.h"
  25. #include <string.h>
  26. /** @addtogroup STM32F2x7_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. /**
  40. * @}
  41. */
  42. /** @defgroup ETH_Private_Macros
  43. * @{
  44. */
  45. /**
  46. * @}
  47. */
  48. /** @defgroup ETH_Private_Variables
  49. * @{
  50. */
  51. #if defined (__CC_ARM) /*!< ARM Compiler */
  52. __align(4)
  53. ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB];/* Ethernet Rx MA Descriptor */
  54. __align(4)
  55. ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB];/* Ethernet Tx DMA Descriptor */
  56. __align(4)
  57. uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; /* Ethernet Receive Buffer */
  58. __align(4)
  59. uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; /* Ethernet Transmit Buffer */
  60. #elif defined ( __ICCARM__ ) /*!< IAR Compiler */
  61. #pragma data_alignment=4
  62. ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB];/* Ethernet Rx MA Descriptor */
  63. #pragma data_alignment=4
  64. ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB];/* Ethernet Tx DMA Descriptor */
  65. #pragma data_alignment=4
  66. uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; /* Ethernet Receive Buffer */
  67. #pragma data_alignment=4
  68. uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; /* Ethernet Transmit Buffer */
  69. #elif defined (__GNUC__) /*!< GNU Compiler */
  70. ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB] __attribute__ ((aligned (4))); /* Ethernet Rx DMA Descriptor */
  71. ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB] __attribute__ ((aligned (4))); /* Ethernet Tx DMA Descriptor */
  72. uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE] __attribute__ ((aligned (4))); /* Ethernet Receive Buffer */
  73. uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __attribute__ ((aligned (4))); /* Ethernet Transmit Buffer */
  74. #elif defined (__TASKING__) /*!< TASKING Compiler */
  75. __align(4)
  76. ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB];/* Ethernet Rx MA Descriptor */
  77. __align(4)
  78. ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB];/* Ethernet Tx DMA Descriptor */
  79. __align(4)
  80. uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE]; /* Ethernet Receive Buffer */
  81. __align(4)
  82. uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE]; /* Ethernet Transmit Buffer */
  83. #endif /* __CC_ARM */
  84. /* Global pointers on Tx and Rx descriptor used to track transmit and receive descriptors */
  85. __IO ETH_DMADESCTypeDef *DMATxDescToSet;
  86. __IO ETH_DMADESCTypeDef *DMARxDescToGet;
  87. /* Structure used to hold the last received packet descriptors info */
  88. ETH_DMA_Rx_Frame_infos RX_Frame_Descriptor;
  89. __IO ETH_DMA_Rx_Frame_infos *DMA_RX_FRAME_infos;
  90. __IO uint32_t Frame_Rx_index;
  91. /**
  92. * @}
  93. */
  94. /** @defgroup ETH_Private_FunctionPrototypes
  95. * @{
  96. */
  97. /**
  98. * @}
  99. */
  100. /** @defgroup ETH_Private_Functions
  101. * @{
  102. */
  103. #ifndef USE_Delay
  104. /**
  105. * @brief Inserts a delay time.
  106. * @param nCount: specifies the delay time length.
  107. * @retval None
  108. */
  109. static void ETH_Delay(__IO uint32_t nCount)
  110. {
  111. __IO uint32_t index = 0;
  112. for(index = nCount; index != 0; index--)
  113. {
  114. }
  115. }
  116. #endif /* USE_Delay*/
  117. /******************************************************************************/
  118. /* Global ETH MAC/DMA functions */
  119. /******************************************************************************/
  120. /**
  121. * @brief Deinitializes the ETHERNET peripheral registers to their default reset values.
  122. * @param None
  123. * @retval None
  124. */
  125. void ETH_DeInit(void)
  126. {
  127. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_ETH_MAC, ENABLE);
  128. RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_ETH_MAC, DISABLE);
  129. }
  130. /**
  131. * @brief Fills each ETH_InitStruct member with its default value.
  132. * @param ETH_InitStruct: pointer to a ETH_InitTypeDef structure which will be initialized.
  133. * @retval None
  134. */
  135. void ETH_StructInit(ETH_InitTypeDef* ETH_InitStruct)
  136. {
  137. /* ETH_InitStruct members default value */
  138. /*------------------------ MAC Configuration ---------------------------*/
  139. /* PHY Auto-negotiation enabled */
  140. ETH_InitStruct->ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  141. /* MAC watchdog enabled: cuts-off long frame */
  142. ETH_InitStruct->ETH_Watchdog = ETH_Watchdog_Enable;
  143. /* MAC Jabber enabled in Half-duplex mode */
  144. ETH_InitStruct->ETH_Jabber = ETH_Jabber_Enable;
  145. /* Ethernet interframe gap set to 96 bits */
  146. ETH_InitStruct->ETH_InterFrameGap = ETH_InterFrameGap_96Bit;
  147. /* Carrier Sense Enabled in Half-Duplex mode */
  148. ETH_InitStruct->ETH_CarrierSense = ETH_CarrierSense_Enable;
  149. /* PHY speed configured to 100Mbit/s */
  150. ETH_InitStruct->ETH_Speed = ETH_Speed_100M;
  151. /* Receive own Frames in Half-Duplex mode enabled */
  152. ETH_InitStruct->ETH_ReceiveOwn = ETH_ReceiveOwn_Enable;
  153. /* MAC MII loopback disabled */
  154. ETH_InitStruct->ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  155. /* Full-Duplex mode selected */
  156. ETH_InitStruct->ETH_Mode = ETH_Mode_FullDuplex;
  157. /* IPv4 and TCP/UDP/ICMP frame Checksum Offload disabled */
  158. ETH_InitStruct->ETH_ChecksumOffload = ETH_ChecksumOffload_Disable;
  159. /* Retry Transmission enabled for half-duplex mode */
  160. ETH_InitStruct->ETH_RetryTransmission = ETH_RetryTransmission_Enable;
  161. /* Automatic PAD/CRC strip disabled*/
  162. ETH_InitStruct->ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  163. /* half-duplex mode retransmission Backoff time_limit = 10 slot times*/
  164. ETH_InitStruct->ETH_BackOffLimit = ETH_BackOffLimit_10;
  165. /* half-duplex mode Deferral check disabled */
  166. ETH_InitStruct->ETH_DeferralCheck = ETH_DeferralCheck_Disable;
  167. /* Receive all frames disabled */
  168. ETH_InitStruct->ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  169. /* Source address filtering (on the optional MAC addresses) disabled */
  170. ETH_InitStruct->ETH_SourceAddrFilter = ETH_SourceAddrFilter_Disable;
  171. /* Do not forward control frames that do not pass the address filtering */
  172. ETH_InitStruct->ETH_PassControlFrames = ETH_PassControlFrames_BlockAll;
  173. /* Disable reception of Broadcast frames */
  174. ETH_InitStruct->ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;
  175. /* Normal Destination address filtering (not reverse addressing) */
  176. ETH_InitStruct->ETH_DestinationAddrFilter = ETH_DestinationAddrFilter_Normal;
  177. /* Promiscuous address filtering mode disabled */
  178. ETH_InitStruct->ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  179. /* Perfect address filtering for multicast addresses */
  180. ETH_InitStruct->ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  181. /* Perfect address filtering for unicast addresses */
  182. ETH_InitStruct->ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  183. /* Initialize hash table high and low regs */
  184. ETH_InitStruct->ETH_HashTableHigh = 0x0;
  185. ETH_InitStruct->ETH_HashTableLow = 0x0;
  186. /* Flow control config (flow control disabled)*/
  187. ETH_InitStruct->ETH_PauseTime = 0x0;
  188. ETH_InitStruct->ETH_ZeroQuantaPause = ETH_ZeroQuantaPause_Disable;
  189. ETH_InitStruct->ETH_PauseLowThreshold = ETH_PauseLowThreshold_Minus4;
  190. ETH_InitStruct->ETH_UnicastPauseFrameDetect = ETH_UnicastPauseFrameDetect_Disable;
  191. ETH_InitStruct->ETH_ReceiveFlowControl = ETH_ReceiveFlowControl_Disable;
  192. ETH_InitStruct->ETH_TransmitFlowControl = ETH_TransmitFlowControl_Disable;
  193. /* VLANtag config (VLAN field not checked) */
  194. ETH_InitStruct->ETH_VLANTagComparison = ETH_VLANTagComparison_16Bit;
  195. ETH_InitStruct->ETH_VLANTagIdentifier = 0x0;
  196. /*---------------------- DMA Configuration -------------------------------*/
  197. /* Drops frames with with TCP/IP checksum errors */
  198. ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Disable;
  199. /* Store and forward mode enabled for receive */
  200. ETH_InitStruct->ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  201. /* Flush received frame that created FIFO overflow */
  202. ETH_InitStruct->ETH_FlushReceivedFrame = ETH_FlushReceivedFrame_Enable;
  203. /* Store and forward mode enabled for transmit */
  204. ETH_InitStruct->ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  205. /* Threshold TXFIFO level set to 64 bytes (used when threshold mode is enabled) */
  206. ETH_InitStruct->ETH_TransmitThresholdControl = ETH_TransmitThresholdControl_64Bytes;
  207. /* Disable forwarding frames with errors (short frames, CRC,...)*/
  208. ETH_InitStruct->ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  209. /* Disable undersized good frames */
  210. ETH_InitStruct->ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  211. /* Threshold RXFIFO level set to 64 bytes (used when Cut-through mode is enabled) */
  212. ETH_InitStruct->ETH_ReceiveThresholdControl = ETH_ReceiveThresholdControl_64Bytes;
  213. /* Disable Operate on second frame (transmit a second frame to FIFO without
  214. waiting status of previous frame*/
  215. ETH_InitStruct->ETH_SecondFrameOperate = ETH_SecondFrameOperate_Disable;
  216. /* DMA works on 32-bit aligned start source and destinations addresses */
  217. ETH_InitStruct->ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  218. /* Enabled Fixed Burst Mode (mix of INC4, INC8, INC16 and SINGLE DMA transactions */
  219. ETH_InitStruct->ETH_FixedBurst = ETH_FixedBurst_Enable;
  220. /* DMA transfer max burst length = 32 beats = 32 x 32bits */
  221. ETH_InitStruct->ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  222. ETH_InitStruct->ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  223. /* DMA Ring mode skip length = 0 */
  224. ETH_InitStruct->ETH_DescriptorSkipLength = 0x0;
  225. /* Equal priority (round-robin) between transmit and receive DMA engines */
  226. ETH_InitStruct->ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_1_1;
  227. }
  228. /**
  229. * @brief Initializes the ETHERNET peripheral according to the specified
  230. * parameters in the ETH_InitStruct .
  231. * @param ETH_InitStruct: pointer to a ETH_InitTypeDef structure that contains
  232. * the configuration information for the specified ETHERNET peripheral.
  233. * @param PHYAddress: external PHY address
  234. * @retval ETH_ERROR: Ethernet initialization failed
  235. * ETH_SUCCESS: Ethernet successfully initialized
  236. */
  237. uint32_t ETH_Init(ETH_InitTypeDef* ETH_InitStruct, uint16_t PHYAddress)
  238. {
  239. uint32_t RegValue = 0, tmpreg = 0;
  240. __IO uint32_t i = 0;
  241. RCC_ClocksTypeDef rcc_clocks;
  242. uint32_t hclk = 60000000;
  243. __IO uint32_t timeout = 0;
  244. /* Check the parameters */
  245. /* MAC --------------------------*/
  246. assert_param(IS_ETH_AUTONEGOTIATION(ETH_InitStruct->ETH_AutoNegotiation));
  247. assert_param(IS_ETH_WATCHDOG(ETH_InitStruct->ETH_Watchdog));
  248. assert_param(IS_ETH_JABBER(ETH_InitStruct->ETH_Jabber));
  249. assert_param(IS_ETH_INTER_FRAME_GAP(ETH_InitStruct->ETH_InterFrameGap));
  250. assert_param(IS_ETH_CARRIER_SENSE(ETH_InitStruct->ETH_CarrierSense));
  251. assert_param(IS_ETH_SPEED(ETH_InitStruct->ETH_Speed));
  252. assert_param(IS_ETH_RECEIVE_OWN(ETH_InitStruct->ETH_ReceiveOwn));
  253. assert_param(IS_ETH_LOOPBACK_MODE(ETH_InitStruct->ETH_LoopbackMode));
  254. assert_param(IS_ETH_DUPLEX_MODE(ETH_InitStruct->ETH_Mode));
  255. assert_param(IS_ETH_CHECKSUM_OFFLOAD(ETH_InitStruct->ETH_ChecksumOffload));
  256. assert_param(IS_ETH_RETRY_TRANSMISSION(ETH_InitStruct->ETH_RetryTransmission));
  257. assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(ETH_InitStruct->ETH_AutomaticPadCRCStrip));
  258. assert_param(IS_ETH_BACKOFF_LIMIT(ETH_InitStruct->ETH_BackOffLimit));
  259. assert_param(IS_ETH_DEFERRAL_CHECK(ETH_InitStruct->ETH_DeferralCheck));
  260. assert_param(IS_ETH_RECEIVE_ALL(ETH_InitStruct->ETH_ReceiveAll));
  261. assert_param(IS_ETH_SOURCE_ADDR_FILTER(ETH_InitStruct->ETH_SourceAddrFilter));
  262. assert_param(IS_ETH_CONTROL_FRAMES(ETH_InitStruct->ETH_PassControlFrames));
  263. assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(ETH_InitStruct->ETH_BroadcastFramesReception));
  264. assert_param(IS_ETH_DESTINATION_ADDR_FILTER(ETH_InitStruct->ETH_DestinationAddrFilter));
  265. assert_param(IS_ETH_PROMISCIOUS_MODE(ETH_InitStruct->ETH_PromiscuousMode));
  266. assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(ETH_InitStruct->ETH_MulticastFramesFilter));
  267. assert_param(IS_ETH_UNICAST_FRAMES_FILTER(ETH_InitStruct->ETH_UnicastFramesFilter));
  268. assert_param(IS_ETH_PAUSE_TIME(ETH_InitStruct->ETH_PauseTime));
  269. assert_param(IS_ETH_ZEROQUANTA_PAUSE(ETH_InitStruct->ETH_ZeroQuantaPause));
  270. assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(ETH_InitStruct->ETH_PauseLowThreshold));
  271. assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(ETH_InitStruct->ETH_UnicastPauseFrameDetect));
  272. assert_param(IS_ETH_RECEIVE_FLOWCONTROL(ETH_InitStruct->ETH_ReceiveFlowControl));
  273. assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(ETH_InitStruct->ETH_TransmitFlowControl));
  274. assert_param(IS_ETH_VLAN_TAG_COMPARISON(ETH_InitStruct->ETH_VLANTagComparison));
  275. assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(ETH_InitStruct->ETH_VLANTagIdentifier));
  276. /* DMA --------------------------*/
  277. assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame));
  278. assert_param(IS_ETH_RECEIVE_STORE_FORWARD(ETH_InitStruct->ETH_ReceiveStoreForward));
  279. assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(ETH_InitStruct->ETH_FlushReceivedFrame));
  280. assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(ETH_InitStruct->ETH_TransmitStoreForward));
  281. assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(ETH_InitStruct->ETH_TransmitThresholdControl));
  282. assert_param(IS_ETH_FORWARD_ERROR_FRAMES(ETH_InitStruct->ETH_ForwardErrorFrames));
  283. assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(ETH_InitStruct->ETH_ForwardUndersizedGoodFrames));
  284. assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(ETH_InitStruct->ETH_ReceiveThresholdControl));
  285. assert_param(IS_ETH_SECOND_FRAME_OPERATE(ETH_InitStruct->ETH_SecondFrameOperate));
  286. assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(ETH_InitStruct->ETH_AddressAlignedBeats));
  287. assert_param(IS_ETH_FIXED_BURST(ETH_InitStruct->ETH_FixedBurst));
  288. assert_param(IS_ETH_RXDMA_BURST_LENGTH(ETH_InitStruct->ETH_RxDMABurstLength));
  289. assert_param(IS_ETH_TXDMA_BURST_LENGTH(ETH_InitStruct->ETH_TxDMABurstLength));
  290. assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(ETH_InitStruct->ETH_DescriptorSkipLength));
  291. assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(ETH_InitStruct->ETH_DMAArbitration));
  292. /*-------------------------------- MAC Config ------------------------------*/
  293. /*---------------------- ETHERNET MACMIIAR Configuration -------------------*/
  294. /* Get the ETHERNET MACMIIAR value */
  295. tmpreg = ETH->MACMIIAR;
  296. /* Clear CSR Clock Range CR[2:0] bits */
  297. tmpreg &= MACMIIAR_CR_MASK;
  298. /* Get hclk frequency value */
  299. RCC_GetClocksFreq(&rcc_clocks);
  300. hclk = rcc_clocks.HCLK_Frequency;
  301. /* Set CR bits depending on hclk value */
  302. if((hclk >= 20000000)&&(hclk < 35000000))
  303. {
  304. /* CSR Clock Range between 20-35 MHz */
  305. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div16;
  306. }
  307. else if((hclk >= 35000000)&&(hclk < 60000000))
  308. {
  309. /* CSR Clock Range between 35-60 MHz */
  310. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div26;
  311. }
  312. else if((hclk >= 60000000)&&(hclk < 100000000))
  313. {
  314. /* CSR Clock Range between 60-100 MHz */
  315. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div42;
  316. }
  317. else /* ((hclk >= 100000000)&&(hclk <= 120000000)) */
  318. {
  319. /* CSR Clock Range between 100-120 MHz */
  320. tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div62;
  321. }
  322. /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
  323. ETH->MACMIIAR = (uint32_t)tmpreg;
  324. /*-------------------- PHY initialization and configuration ----------------*/
  325. /* Put the PHY in reset mode */
  326. if(!(ETH_WritePHYRegister(PHYAddress, PHY_BCR, PHY_Reset)))
  327. {
  328. /* Return ERROR in case of write timeout */
  329. return ETH_ERROR;
  330. }
  331. /* Delay to assure PHY reset */
  332. _eth_delay_(PHY_RESET_DELAY);
  333. if(ETH_InitStruct->ETH_AutoNegotiation != ETH_AutoNegotiation_Disable)
  334. {
  335. /* We wait for linked status... */
  336. do
  337. {
  338. timeout++;
  339. } while (!(ETH_ReadPHYRegister(PHYAddress, PHY_BSR) & PHY_Linked_Status) && (timeout < PHY_READ_TO));
  340. /* Return ERROR in case of timeout */
  341. if(timeout == PHY_READ_TO)
  342. {
  343. return ETH_ERROR;
  344. }
  345. /* Reset Timeout counter */
  346. timeout = 0;
  347. /* Enable Auto-Negotiation */
  348. if(!(ETH_WritePHYRegister(PHYAddress, PHY_BCR, PHY_AutoNegotiation)))
  349. {
  350. /* Return ERROR in case of write timeout */
  351. return ETH_ERROR;
  352. }
  353. /* Wait until the auto-negotiation will be completed */
  354. do
  355. {
  356. timeout++;
  357. } while (!(ETH_ReadPHYRegister(PHYAddress, PHY_BSR) & PHY_AutoNego_Complete) && (timeout < (uint32_t)PHY_READ_TO));
  358. /* Return ERROR in case of timeout */
  359. if(timeout == PHY_READ_TO)
  360. {
  361. return ETH_ERROR;
  362. }
  363. /* Reset Timeout counter */
  364. timeout = 0;
  365. /* Read the result of the auto-negotiation */
  366. RegValue = ETH_ReadPHYRegister(PHYAddress, PHY_SR);
  367. /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
  368. if((RegValue & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
  369. {
  370. /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
  371. ETH_InitStruct->ETH_Mode = ETH_Mode_FullDuplex;
  372. rt_kprintf("ETH FullDuplex\n");
  373. }
  374. else
  375. {
  376. /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
  377. ETH_InitStruct->ETH_Mode = ETH_Mode_HalfDuplex;
  378. rt_kprintf("ETH HalfDuplex\n");
  379. }
  380. /* Configure the MAC with the speed fixed by the auto-negotiation process */
  381. if(RegValue & PHY_SPEED_STATUS)
  382. {
  383. /* Set Ethernet speed to 10M following the auto-negotiation */
  384. ETH_InitStruct->ETH_Speed = ETH_Speed_10M;
  385. rt_kprintf("ETH speed 10M\n");
  386. }
  387. else
  388. {
  389. /* Set Ethernet speed to 100M following the auto-negotiation */
  390. ETH_InitStruct->ETH_Speed = ETH_Speed_100M;
  391. rt_kprintf("ETH speed 100M\n");
  392. }
  393. }
  394. else
  395. {
  396. if(!ETH_WritePHYRegister(PHYAddress, PHY_BCR, ((uint16_t)(ETH_InitStruct->ETH_Mode >> 3) |
  397. (uint16_t)(ETH_InitStruct->ETH_Speed >> 1))))
  398. {
  399. /* Return ERROR in case of write timeout */
  400. return ETH_ERROR;
  401. }
  402. /* Delay to assure PHY configuration */
  403. _eth_delay_(PHY_CONFIG_DELAY);
  404. }
  405. /*------------------------ ETHERNET MACCR Configuration --------------------*/
  406. /* Get the ETHERNET MACCR value */
  407. tmpreg = ETH->MACCR;
  408. /* Clear WD, PCE, PS, TE and RE bits */
  409. tmpreg &= MACCR_CLEAR_MASK;
  410. /* Set the WD bit according to ETH_Watchdog value */
  411. /* Set the JD: bit according to ETH_Jabber value */
  412. /* Set the IFG bit according to ETH_InterFrameGap value */
  413. /* Set the DCRS bit according to ETH_CarrierSense value */
  414. /* Set the FES bit according to ETH_Speed value */
  415. /* Set the DO bit according to ETH_ReceiveOwn value */
  416. /* Set the LM bit according to ETH_LoopbackMode value */
  417. /* Set the DM bit according to ETH_Mode value */
  418. /* Set the IPCO bit according to ETH_ChecksumOffload value */
  419. /* Set the DR bit according to ETH_RetryTransmission value */
  420. /* Set the ACS bit according to ETH_AutomaticPadCRCStrip value */
  421. /* Set the BL bit according to ETH_BackOffLimit value */
  422. /* Set the DC bit according to ETH_DeferralCheck value */
  423. tmpreg |= (uint32_t)(ETH_InitStruct->ETH_Watchdog |
  424. ETH_InitStruct->ETH_Jabber |
  425. ETH_InitStruct->ETH_InterFrameGap |
  426. ETH_InitStruct->ETH_CarrierSense |
  427. ETH_InitStruct->ETH_Speed |
  428. ETH_InitStruct->ETH_ReceiveOwn |
  429. ETH_InitStruct->ETH_LoopbackMode |
  430. ETH_InitStruct->ETH_Mode |
  431. ETH_InitStruct->ETH_ChecksumOffload |
  432. ETH_InitStruct->ETH_RetryTransmission |
  433. ETH_InitStruct->ETH_AutomaticPadCRCStrip |
  434. ETH_InitStruct->ETH_BackOffLimit |
  435. ETH_InitStruct->ETH_DeferralCheck);
  436. /* Write to ETHERNET MACCR */
  437. ETH->MACCR = (uint32_t)tmpreg;
  438. /*----------------------- ETHERNET MACFFR Configuration --------------------*/
  439. /* Set the RA bit according to ETH_ReceiveAll value */
  440. /* Set the SAF and SAIF bits according to ETH_SourceAddrFilter value */
  441. /* Set the PCF bit according to ETH_PassControlFrames value */
  442. /* Set the DBF bit according to ETH_BroadcastFramesReception value */
  443. /* Set the DAIF bit according to ETH_DestinationAddrFilter value */
  444. /* Set the PR bit according to ETH_PromiscuousMode value */
  445. /* Set the PM, HMC and HPF bits according to ETH_MulticastFramesFilter value */
  446. /* Set the HUC and HPF bits according to ETH_UnicastFramesFilter value */
  447. /* Write to ETHERNET MACFFR */
  448. ETH->MACFFR = (uint32_t)(ETH_InitStruct->ETH_ReceiveAll |
  449. ETH_InitStruct->ETH_SourceAddrFilter |
  450. ETH_InitStruct->ETH_PassControlFrames |
  451. ETH_InitStruct->ETH_BroadcastFramesReception |
  452. ETH_InitStruct->ETH_DestinationAddrFilter |
  453. ETH_InitStruct->ETH_PromiscuousMode |
  454. ETH_InitStruct->ETH_MulticastFramesFilter |
  455. ETH_InitStruct->ETH_UnicastFramesFilter);
  456. /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
  457. /* Write to ETHERNET MACHTHR */
  458. ETH->MACHTHR = (uint32_t)ETH_InitStruct->ETH_HashTableHigh;
  459. /* Write to ETHERNET MACHTLR */
  460. ETH->MACHTLR = (uint32_t)ETH_InitStruct->ETH_HashTableLow;
  461. /*----------------------- ETHERNET MACFCR Configuration --------------------*/
  462. /* Get the ETHERNET MACFCR value */
  463. tmpreg = ETH->MACFCR;
  464. /* Clear xx bits */
  465. tmpreg &= MACFCR_CLEAR_MASK;
  466. /* Set the PT bit according to ETH_PauseTime value */
  467. /* Set the DZPQ bit according to ETH_ZeroQuantaPause value */
  468. /* Set the PLT bit according to ETH_PauseLowThreshold value */
  469. /* Set the UP bit according to ETH_UnicastPauseFrameDetect value */
  470. /* Set the RFE bit according to ETH_ReceiveFlowControl value */
  471. /* Set the TFE bit according to ETH_TransmitFlowControl value */
  472. tmpreg |= (uint32_t)((ETH_InitStruct->ETH_PauseTime << 16) |
  473. ETH_InitStruct->ETH_ZeroQuantaPause |
  474. ETH_InitStruct->ETH_PauseLowThreshold |
  475. ETH_InitStruct->ETH_UnicastPauseFrameDetect |
  476. ETH_InitStruct->ETH_ReceiveFlowControl |
  477. ETH_InitStruct->ETH_TransmitFlowControl);
  478. /* Write to ETHERNET MACFCR */
  479. ETH->MACFCR = (uint32_t)tmpreg;
  480. /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/
  481. /* Set the ETV bit according to ETH_VLANTagComparison value */
  482. /* Set the VL bit according to ETH_VLANTagIdentifier value */
  483. ETH->MACVLANTR = (uint32_t)(ETH_InitStruct->ETH_VLANTagComparison |
  484. ETH_InitStruct->ETH_VLANTagIdentifier);
  485. /*-------------------------------- DMA Config ------------------------------*/
  486. /*----------------------- ETHERNET DMAOMR Configuration --------------------*/
  487. /* Get the ETHERNET DMAOMR value */
  488. tmpreg = ETH->DMAOMR;
  489. /* Clear xx bits */
  490. tmpreg &= DMAOMR_CLEAR_MASK;
  491. /* Set the DT bit according to ETH_DropTCPIPChecksumErrorFrame value */
  492. /* Set the RSF bit according to ETH_ReceiveStoreForward value */
  493. /* Set the DFF bit according to ETH_FlushReceivedFrame value */
  494. /* Set the TSF bit according to ETH_TransmitStoreForward value */
  495. /* Set the TTC bit according to ETH_TransmitThresholdControl value */
  496. /* Set the FEF bit according to ETH_ForwardErrorFrames value */
  497. /* Set the FUF bit according to ETH_ForwardUndersizedGoodFrames value */
  498. /* Set the RTC bit according to ETH_ReceiveThresholdControl value */
  499. /* Set the OSF bit according to ETH_SecondFrameOperate value */
  500. tmpreg |= (uint32_t)(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame |
  501. ETH_InitStruct->ETH_ReceiveStoreForward |
  502. ETH_InitStruct->ETH_FlushReceivedFrame |
  503. ETH_InitStruct->ETH_TransmitStoreForward |
  504. ETH_InitStruct->ETH_TransmitThresholdControl |
  505. ETH_InitStruct->ETH_ForwardErrorFrames |
  506. ETH_InitStruct->ETH_ForwardUndersizedGoodFrames |
  507. ETH_InitStruct->ETH_ReceiveThresholdControl |
  508. ETH_InitStruct->ETH_SecondFrameOperate);
  509. /* Write to ETHERNET DMAOMR */
  510. ETH->DMAOMR = (uint32_t)tmpreg;
  511. /*----------------------- ETHERNET DMABMR Configuration --------------------*/
  512. /* Set the AAL bit according to ETH_AddressAlignedBeats value */
  513. /* Set the FB bit according to ETH_FixedBurst value */
  514. /* Set the RPBL and 4*PBL bits according to ETH_RxDMABurstLength value */
  515. /* Set the PBL and 4*PBL bits according to ETH_TxDMABurstLength value */
  516. /* Set the DSL bit according to ETH_DesciptorSkipLength value */
  517. /* Set the PR and DA bits according to ETH_DMAArbitration value */
  518. ETH->DMABMR = (uint32_t)(ETH_InitStruct->ETH_AddressAlignedBeats |
  519. ETH_InitStruct->ETH_FixedBurst |
  520. ETH_InitStruct->ETH_RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
  521. ETH_InitStruct->ETH_TxDMABurstLength |
  522. (ETH_InitStruct->ETH_DescriptorSkipLength << 2) |
  523. ETH_InitStruct->ETH_DMAArbitration |
  524. ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
  525. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  526. /* Enable the Enhanced DMA descriptors */
  527. ETH->DMABMR |= ETH_DMABMR_EDE;
  528. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  529. /* Return Ethernet configuration success */
  530. return ETH_SUCCESS;
  531. }
  532. /**
  533. * @brief Enables ENET MAC and DMA reception/transmission
  534. * @param None
  535. * @retval None
  536. */
  537. void ETH_Start(void)
  538. {
  539. /* Enable transmit state machine of the MAC for transmission on the MII */
  540. ETH_MACTransmissionCmd(ENABLE);
  541. /* Flush Transmit FIFO */
  542. ETH_FlushTransmitFIFO();
  543. /* Enable receive state machine of the MAC for reception from the MII */
  544. ETH_MACReceptionCmd(ENABLE);
  545. /* Start DMA transmission */
  546. ETH_DMATransmissionCmd(ENABLE);
  547. /* Start DMA reception */
  548. ETH_DMAReceptionCmd(ENABLE);
  549. }
  550. /**
  551. * @brief Enables or disables the MAC transmission.
  552. * @param NewState: new state of the MAC transmission.
  553. * This parameter can be: ENABLE or DISABLE.
  554. * @retval None
  555. */
  556. void ETH_MACTransmissionCmd(FunctionalState NewState)
  557. {
  558. /* Check the parameters */
  559. assert_param(IS_FUNCTIONAL_STATE(NewState));
  560. if (NewState != DISABLE)
  561. {
  562. /* Enable the MAC transmission */
  563. ETH->MACCR |= ETH_MACCR_TE;
  564. }
  565. else
  566. {
  567. /* Disable the MAC transmission */
  568. ETH->MACCR &= ~ETH_MACCR_TE;
  569. }
  570. }
  571. /**
  572. * @brief Enables or disables the MAC reception.
  573. * @param NewState: new state of the MAC reception.
  574. * This parameter can be: ENABLE or DISABLE.
  575. * @retval None
  576. */
  577. void ETH_MACReceptionCmd(FunctionalState NewState)
  578. {
  579. /* Check the parameters */
  580. assert_param(IS_FUNCTIONAL_STATE(NewState));
  581. if (NewState != DISABLE)
  582. {
  583. /* Enable the MAC reception */
  584. ETH->MACCR |= ETH_MACCR_RE;
  585. }
  586. else
  587. {
  588. /* Disable the MAC reception */
  589. ETH->MACCR &= ~ETH_MACCR_RE;
  590. }
  591. }
  592. /**
  593. * @brief Checks whether the ETHERNET flow control busy bit is set or not.
  594. * @param None
  595. * @retval The new state of flow control busy status bit (SET or RESET).
  596. */
  597. FlagStatus ETH_GetFlowControlBusyStatus(void)
  598. {
  599. FlagStatus bitstatus = RESET;
  600. /* The Flow Control register should not be written to until this bit is cleared */
  601. if ((ETH->MACFCR & ETH_MACFCR_FCBBPA) != (uint32_t)RESET)
  602. {
  603. bitstatus = SET;
  604. }
  605. else
  606. {
  607. bitstatus = RESET;
  608. }
  609. return bitstatus;
  610. }
  611. /**
  612. * @brief Initiate a Pause Control Frame (Full-duplex only).
  613. * @param None
  614. * @retval None
  615. */
  616. void ETH_InitiatePauseControlFrame(void)
  617. {
  618. /* When Set In full duplex MAC initiates pause control frame */
  619. ETH->MACFCR |= ETH_MACFCR_FCBBPA;
  620. }
  621. /**
  622. * @brief Enables or disables the MAC BackPressure operation activation (Half-duplex only).
  623. * @param NewState: new state of the MAC BackPressure operation activation.
  624. * This parameter can be: ENABLE or DISABLE.
  625. * @retval None
  626. */
  627. void ETH_BackPressureActivationCmd(FunctionalState NewState)
  628. {
  629. /* Check the parameters */
  630. assert_param(IS_FUNCTIONAL_STATE(NewState));
  631. if (NewState != DISABLE)
  632. {
  633. /* Activate the MAC BackPressure operation */
  634. /* In Half duplex: during backpressure, when the MAC receives a new frame,
  635. the transmitter starts sending a JAM pattern resulting in a collision */
  636. ETH->MACFCR |= ETH_MACFCR_FCBBPA;
  637. }
  638. else
  639. {
  640. /* Desactivate the MAC BackPressure operation */
  641. ETH->MACFCR &= ~ETH_MACFCR_FCBBPA;
  642. }
  643. }
  644. /**
  645. * @brief Checks whether the specified ETHERNET MAC flag is set or not.
  646. * @param ETH_MAC_FLAG: specifies the flag to check.
  647. * This parameter can be one of the following values:
  648. * @arg ETH_MAC_FLAG_TST : Time stamp trigger flag
  649. * @arg ETH_MAC_FLAG_MMCT : MMC transmit flag
  650. * @arg ETH_MAC_FLAG_MMCR : MMC receive flag
  651. * @arg ETH_MAC_FLAG_MMC : MMC flag
  652. * @arg ETH_MAC_FLAG_PMT : PMT flag
  653. * @retval The new state of ETHERNET MAC flag (SET or RESET).
  654. */
  655. FlagStatus ETH_GetMACFlagStatus(uint32_t ETH_MAC_FLAG)
  656. {
  657. FlagStatus bitstatus = RESET;
  658. /* Check the parameters */
  659. assert_param(IS_ETH_MAC_GET_FLAG(ETH_MAC_FLAG));
  660. if ((ETH->MACSR & ETH_MAC_FLAG) != (uint32_t)RESET)
  661. {
  662. bitstatus = SET;
  663. }
  664. else
  665. {
  666. bitstatus = RESET;
  667. }
  668. return bitstatus;
  669. }
  670. /**
  671. * @brief Checks whether the specified ETHERNET MAC interrupt has occurred or not.
  672. * @param ETH_MAC_IT: specifies the interrupt source to check.
  673. * This parameter can be one of the following values:
  674. * @arg ETH_MAC_IT_TST : Time stamp trigger interrupt
  675. * @arg ETH_MAC_IT_MMCT : MMC transmit interrupt
  676. * @arg ETH_MAC_IT_MMCR : MMC receive interrupt
  677. * @arg ETH_MAC_IT_MMC : MMC interrupt
  678. * @arg ETH_MAC_IT_PMT : PMT interrupt
  679. * @retval The new state of ETHERNET MAC interrupt (SET or RESET).
  680. */
  681. ITStatus ETH_GetMACITStatus(uint32_t ETH_MAC_IT)
  682. {
  683. ITStatus bitstatus = RESET;
  684. /* Check the parameters */
  685. assert_param(IS_ETH_MAC_GET_IT(ETH_MAC_IT));
  686. if ((ETH->MACSR & ETH_MAC_IT) != (uint32_t)RESET)
  687. {
  688. bitstatus = SET;
  689. }
  690. else
  691. {
  692. bitstatus = RESET;
  693. }
  694. return bitstatus;
  695. }
  696. /**
  697. * @brief Enables or disables the specified ETHERNET MAC interrupts.
  698. * @param ETH_MAC_IT: specifies the ETHERNET MAC interrupt sources to be
  699. * enabled or disabled.
  700. * This parameter can be any combination of the following values:
  701. * @arg ETH_MAC_IT_TST : Time stamp trigger interrupt
  702. * @arg ETH_MAC_IT_PMT : PMT interrupt
  703. * @param NewState: new state of the specified ETHERNET MAC interrupts.
  704. * This parameter can be: ENABLE or DISABLE.
  705. * @retval None
  706. */
  707. void ETH_MACITConfig(uint32_t ETH_MAC_IT, FunctionalState NewState)
  708. {
  709. /* Check the parameters */
  710. assert_param(IS_ETH_MAC_IT(ETH_MAC_IT));
  711. assert_param(IS_FUNCTIONAL_STATE(NewState));
  712. if (NewState != DISABLE)
  713. {
  714. /* Enable the selected ETHERNET MAC interrupts */
  715. ETH->MACIMR &= (~(uint32_t)ETH_MAC_IT);
  716. }
  717. else
  718. {
  719. /* Disable the selected ETHERNET MAC interrupts */
  720. ETH->MACIMR |= ETH_MAC_IT;
  721. }
  722. }
  723. /**
  724. * @brief Configures the selected MAC address.
  725. * @param MacAddr: The MAC address to configure.
  726. * This parameter can be one of the following values:
  727. * @arg ETH_MAC_Address0 : MAC Address0
  728. * @arg ETH_MAC_Address1 : MAC Address1
  729. * @arg ETH_MAC_Address2 : MAC Address2
  730. * @arg ETH_MAC_Address3 : MAC Address3
  731. * @param Addr: Pointer on MAC address buffer data (6 bytes).
  732. * @retval None
  733. */
  734. void ETH_MACAddressConfig(uint32_t MacAddr, uint8_t *Addr)
  735. {
  736. uint32_t tmpreg;
  737. /* Check the parameters */
  738. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  739. /* Calculate the selected MAC address high register */
  740. tmpreg = ((uint32_t)Addr[5] << 8) | (uint32_t)Addr[4];
  741. /* Load the selected MAC address high register */
  742. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) = tmpreg;
  743. /* Calculate the selected MAC address low register */
  744. tmpreg = ((uint32_t)Addr[3] << 24) | ((uint32_t)Addr[2] << 16) | ((uint32_t)Addr[1] << 8) | Addr[0];
  745. /* Load the selected MAC address low register */
  746. (*(__IO uint32_t *) (ETH_MAC_ADDR_LBASE + MacAddr)) = tmpreg;
  747. }
  748. /**
  749. * @brief Get the selected MAC address.
  750. * @param MacAddr: The MAC address to return.
  751. * This parameter can be one of the following values:
  752. * @arg ETH_MAC_Address0 : MAC Address0
  753. * @arg ETH_MAC_Address1 : MAC Address1
  754. * @arg ETH_MAC_Address2 : MAC Address2
  755. * @arg ETH_MAC_Address3 : MAC Address3
  756. * @param Addr: Pointer on MAC address buffer data (6 bytes).
  757. * @retval None
  758. */
  759. void ETH_GetMACAddress(uint32_t MacAddr, uint8_t *Addr)
  760. {
  761. uint32_t tmpreg;
  762. /* Check the parameters */
  763. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  764. /* Get the selected MAC address high register */
  765. tmpreg =(*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr));
  766. /* Calculate the selected MAC address buffer */
  767. Addr[5] = ((tmpreg >> 8) & (uint8_t)0xFF);
  768. Addr[4] = (tmpreg & (uint8_t)0xFF);
  769. /* Load the selected MAC address low register */
  770. tmpreg =(*(__IO uint32_t *) (ETH_MAC_ADDR_LBASE + MacAddr));
  771. /* Calculate the selected MAC address buffer */
  772. Addr[3] = ((tmpreg >> 24) & (uint8_t)0xFF);
  773. Addr[2] = ((tmpreg >> 16) & (uint8_t)0xFF);
  774. Addr[1] = ((tmpreg >> 8 ) & (uint8_t)0xFF);
  775. Addr[0] = (tmpreg & (uint8_t)0xFF);
  776. }
  777. /**
  778. * @brief Enables or disables the Address filter module uses the specified
  779. * ETHERNET MAC address for perfect filtering
  780. * @param MacAddr: specifies the ETHERNET MAC address to be used for perfect filtering.
  781. * This parameter can be one of the following values:
  782. * @arg ETH_MAC_Address1 : MAC Address1
  783. * @arg ETH_MAC_Address2 : MAC Address2
  784. * @arg ETH_MAC_Address3 : MAC Address3
  785. * @param NewState: new state of the specified ETHERNET MAC address use.
  786. * This parameter can be: ENABLE or DISABLE.
  787. * @retval None
  788. */
  789. void ETH_MACAddressPerfectFilterCmd(uint32_t MacAddr, FunctionalState NewState)
  790. {
  791. /* Check the parameters */
  792. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  793. assert_param(IS_FUNCTIONAL_STATE(NewState));
  794. if (NewState != DISABLE)
  795. {
  796. /* Enable the selected ETHERNET MAC address for perfect filtering */
  797. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACA1HR_AE;
  798. }
  799. else
  800. {
  801. /* Disable the selected ETHERNET MAC address for perfect filtering */
  802. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_AE);
  803. }
  804. }
  805. /**
  806. * @brief Set the filter type for the specified ETHERNET MAC address
  807. * @param MacAddr: specifies the ETHERNET MAC address
  808. * This parameter can be one of the following values:
  809. * @arg ETH_MAC_Address1 : MAC Address1
  810. * @arg ETH_MAC_Address2 : MAC Address2
  811. * @arg ETH_MAC_Address3 : MAC Address3
  812. * @param Filter: specifies the used frame received field for comparison
  813. * This parameter can be one of the following values:
  814. * @arg ETH_MAC_AddressFilter_SA : MAC Address is used to compare with the
  815. * SA fields of the received frame.
  816. * @arg ETH_MAC_AddressFilter_DA : MAC Address is used to compare with the
  817. * DA fields of the received frame.
  818. * @retval None
  819. */
  820. void ETH_MACAddressFilterConfig(uint32_t MacAddr, uint32_t Filter)
  821. {
  822. /* Check the parameters */
  823. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  824. assert_param(IS_ETH_MAC_ADDRESS_FILTER(Filter));
  825. if (Filter != ETH_MAC_AddressFilter_DA)
  826. {
  827. /* The selected ETHERNET MAC address is used to compare with the SA fields of the
  828. received frame. */
  829. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= ETH_MACA1HR_SA;
  830. }
  831. else
  832. {
  833. /* The selected ETHERNET MAC address is used to compare with the DA fields of the
  834. received frame. */
  835. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_SA);
  836. }
  837. }
  838. /**
  839. * @brief Set the filter type for the specified ETHERNET MAC address
  840. * @param MacAddr: specifies the ETHERNET MAC address
  841. * This parameter can be one of the following values:
  842. * @arg ETH_MAC_Address1 : MAC Address1
  843. * @arg ETH_MAC_Address2 : MAC Address2
  844. * @arg ETH_MAC_Address3 : MAC Address3
  845. * @param MaskByte: specifies the used address bytes for comparison
  846. * This parameter can be any combination of the following values:
  847. * @arg ETH_MAC_AddressMask_Byte6 : Mask MAC Address high reg bits [15:8].
  848. * @arg ETH_MAC_AddressMask_Byte5 : Mask MAC Address high reg bits [7:0].
  849. * @arg ETH_MAC_AddressMask_Byte4 : Mask MAC Address low reg bits [31:24].
  850. * @arg ETH_MAC_AddressMask_Byte3 : Mask MAC Address low reg bits [23:16].
  851. * @arg ETH_MAC_AddressMask_Byte2 : Mask MAC Address low reg bits [15:8].
  852. * @arg ETH_MAC_AddressMask_Byte1 : Mask MAC Address low reg bits [7:0].
  853. * @retval None
  854. */
  855. void ETH_MACAddressMaskBytesFilterConfig(uint32_t MacAddr, uint32_t MaskByte)
  856. {
  857. /* Check the parameters */
  858. assert_param(IS_ETH_MAC_ADDRESS123(MacAddr));
  859. assert_param(IS_ETH_MAC_ADDRESS_MASK(MaskByte));
  860. /* Clear MBC bits in the selected MAC address high register */
  861. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) &=(~(uint32_t)ETH_MACA1HR_MBC);
  862. /* Set the selected Filter mask bytes */
  863. (*(__IO uint32_t *) (ETH_MAC_ADDR_HBASE + MacAddr)) |= MaskByte;
  864. }
  865. /******************************************************************************/
  866. /* DMA Descriptors functions */
  867. /******************************************************************************/
  868. /**
  869. * @brief This function should be called to get the received frame (to be used
  870. * with polling method only).
  871. * @param none
  872. * @retval Structure of type FrameTypeDef
  873. */
  874. FrameTypeDef ETH_Get_Received_Frame(void)
  875. {
  876. uint32_t framelength = 0;
  877. FrameTypeDef frame = {0,0,0};
  878. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  879. framelength = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARxDesc_FrameLengthShift) - 4;
  880. frame.length = framelength;
  881. /* Get the address of the buffer start address */
  882. /* Check if more than one segment in the frame */
  883. if (DMA_RX_FRAME_infos->Seg_Count >1)
  884. {
  885. frame.buffer =(DMA_RX_FRAME_infos->FS_Rx_Desc)->Buffer1Addr;
  886. }
  887. else
  888. {
  889. frame.buffer = DMARxDescToGet->Buffer1Addr;
  890. }
  891. frame.descriptor = DMARxDescToGet;
  892. /* Update the ETHERNET DMA global Rx descriptor with next Rx descriptor */
  893. /* Chained Mode */
  894. /* Selects the next DMA Rx descriptor list for next buffer to read */
  895. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  896. /* Return Frame */
  897. return (frame);
  898. }
  899. /**
  900. * @brief This function should be called when a frame is received using DMA
  901. * Receive interrupt, it allows scanning of Rx descriptors to get the
  902. * the receive frame (should be used with interrupt mode only)
  903. * @param None
  904. * @retval Structure of type FrameTypeDef
  905. */
  906. FrameTypeDef ETH_Get_Received_Frame_interrupt(void)
  907. {
  908. FrameTypeDef frame={0,0,0};
  909. __IO uint32_t descriptor_scan_counter = 0;
  910. /* scan descriptors owned by CPU */
  911. while (((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET)&&
  912. (descriptor_scan_counter<ETH_RXBUFNB))
  913. {
  914. /* Just by security */
  915. descriptor_scan_counter++;
  916. /* check if first segment in frame */
  917. if(((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET)&&
  918. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) == (uint32_t)RESET))
  919. {
  920. DMA_RX_FRAME_infos->FS_Rx_Desc = DMARxDescToGet;
  921. DMA_RX_FRAME_infos->Seg_Count = 1;
  922. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  923. }
  924. /* check if intermediate segment */
  925. else if (((DMARxDescToGet->Status & ETH_DMARxDesc_LS) == (uint32_t)RESET)&&
  926. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) == (uint32_t)RESET))
  927. {
  928. (DMA_RX_FRAME_infos->Seg_Count) ++;
  929. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  930. }
  931. /* should be last segment */
  932. else
  933. {
  934. /* last segment */
  935. DMA_RX_FRAME_infos->LS_Rx_Desc = DMARxDescToGet;
  936. (DMA_RX_FRAME_infos->Seg_Count)++;
  937. /* first segment is last segment */
  938. if ((DMA_RX_FRAME_infos->Seg_Count)==1)
  939. DMA_RX_FRAME_infos->FS_Rx_Desc = DMARxDescToGet;
  940. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  941. frame.length = ((DMARxDescToGet->Status & ETH_DMARxDesc_FL) >> ETH_DMARxDesc_FrameLengthShift) - 4;
  942. /* Get the address of the buffer start address */
  943. /* Check if more than one segment in the frame */
  944. if (DMA_RX_FRAME_infos->Seg_Count >1)
  945. {
  946. frame.buffer =(DMA_RX_FRAME_infos->FS_Rx_Desc)->Buffer1Addr;
  947. }
  948. else
  949. {
  950. frame.buffer = DMARxDescToGet->Buffer1Addr;
  951. }
  952. frame.descriptor = DMARxDescToGet;
  953. /* Update the ETHERNET DMA global Rx descriptor with next Rx descriptor */
  954. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  955. /* Return Frame */
  956. return (frame);
  957. }
  958. }
  959. return (frame);
  960. }
  961. /**
  962. * @brief Prepares DMA Tx descriptors to transmit an ethernet frame
  963. * @param FrameLength : length of the frame to send
  964. * @retval error status
  965. */
  966. uint32_t ETH_Prepare_Transmit_Descriptors(u16 FrameLength)
  967. {
  968. uint32_t buf_count =0, size=0,i=0;
  969. __IO ETH_DMADESCTypeDef *DMATxNextDesc;
  970. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  971. if((DMATxDescToSet->Status & ETH_DMATxDesc_OWN) != (u32)RESET)
  972. {
  973. /* Return ERROR: OWN bit set */
  974. return ETH_ERROR;
  975. }
  976. DMATxNextDesc = DMATxDescToSet;
  977. if (FrameLength > ETH_TX_BUF_SIZE)
  978. {
  979. buf_count = FrameLength/ETH_TX_BUF_SIZE;
  980. if (FrameLength%ETH_TX_BUF_SIZE) buf_count++;
  981. }
  982. else buf_count =1;
  983. if (buf_count ==1)
  984. {
  985. /*set LAST and FIRST segment */
  986. DMATxDescToSet->Status |=ETH_DMATxDesc_FS|ETH_DMATxDesc_LS;
  987. /* Set frame size */
  988. DMATxDescToSet->ControlBufferSize = (FrameLength& ETH_DMATxDesc_TBS1);
  989. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  990. DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
  991. DMATxDescToSet= (ETH_DMADESCTypeDef *)(DMATxDescToSet->Buffer2NextDescAddr);
  992. }
  993. else
  994. {
  995. for (i=0; i< buf_count; i++)
  996. {
  997. if (i==0)
  998. {
  999. /* Setting the first segment bit */
  1000. DMATxDescToSet->Status |= ETH_DMATxDesc_FS;
  1001. }
  1002. /* Program size */
  1003. DMATxNextDesc->ControlBufferSize = (ETH_TX_BUF_SIZE & ETH_DMATxDesc_TBS1);
  1004. if (i== (buf_count-1))
  1005. {
  1006. /* Setting the last segment bit */
  1007. DMATxNextDesc->Status |= ETH_DMATxDesc_LS;
  1008. size = FrameLength - (buf_count-1)*ETH_TX_BUF_SIZE;
  1009. DMATxNextDesc->ControlBufferSize = (size & ETH_DMATxDesc_TBS1);
  1010. }
  1011. /*give back descriptor to DMA */
  1012. DMATxNextDesc->Status |= ETH_DMATxDesc_OWN;
  1013. DMATxNextDesc = (ETH_DMADESCTypeDef *)(DMATxNextDesc->Buffer2NextDescAddr);
  1014. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  1015. }
  1016. DMATxDescToSet = DMATxNextDesc ;
  1017. }
  1018. if( (ETH->DMASR &ETH_DMASR_FBES) != (u32)RESET )
  1019. rt_kprintf("Bus Error\n");
  1020. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  1021. if ((ETH->DMASR & ETH_DMASR_TBUS) != (u32)RESET)
  1022. {
  1023. /* Clear TBUS ETHERNET DMA flag */
  1024. ETH->DMASR = ETH_DMASR_TBUS;
  1025. /* Resume DMA transmission*/
  1026. ETH->DMATPDR = 0;
  1027. }
  1028. /* Return SUCCESS */
  1029. return ETH_SUCCESS;
  1030. }
  1031. /**
  1032. * @brief Initializes the DMA Rx descriptors in chain mode.
  1033. * @param DMARxDescTab: Pointer on the first Rx desc list
  1034. * @param RxBuff: Pointer on the first RxBuffer list
  1035. * @param RxBuffCount: Number of the used Rx desc in the list
  1036. * @retval None
  1037. */
  1038. void ETH_DMARxDescChainInit(ETH_DMADESCTypeDef *DMARxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
  1039. {
  1040. uint32_t i = 0;
  1041. ETH_DMADESCTypeDef *DMARxDesc;
  1042. /* Set the DMARxDescToGet pointer with the first one of the DMARxDescTab list */
  1043. DMARxDescToGet = DMARxDescTab;
  1044. /* Fill each DMARxDesc descriptor with the right values */
  1045. for(i=0; i < RxBuffCount; i++)
  1046. {
  1047. /* Get the pointer on the ith member of the Rx Desc list */
  1048. DMARxDesc = DMARxDescTab+i;
  1049. /* Set Own bit of the Rx descriptor Status */
  1050. DMARxDesc->Status = ETH_DMARxDesc_OWN;
  1051. /* Set Buffer1 size and Second Address Chained bit */
  1052. DMARxDesc->ControlBufferSize = ETH_DMARxDesc_RCH | (uint32_t)ETH_RX_BUF_SIZE;
  1053. /* Set Buffer1 address pointer */
  1054. DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*ETH_RX_BUF_SIZE]);
  1055. /* Initialize the next descriptor with the Next Descriptor Polling Enable */
  1056. if(i < (RxBuffCount-1))
  1057. {
  1058. /* Set next descriptor address register with next descriptor base address */
  1059. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab+i+1);
  1060. }
  1061. else
  1062. {
  1063. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1064. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
  1065. }
  1066. }
  1067. /* Set Receive Descriptor List Address Register */
  1068. ETH->DMARDLAR = (uint32_t) DMARxDescTab;
  1069. DMA_RX_FRAME_infos = &RX_Frame_Descriptor;
  1070. }
  1071. /**
  1072. * @brief This function polls for a frame reception
  1073. * @param None
  1074. * @retval Returns 1 when a frame is received, 0 if none.
  1075. */
  1076. uint32_t ETH_CheckFrameReceived(void)
  1077. {
  1078. /* check if last segment */
  1079. if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  1080. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET))
  1081. {
  1082. DMA_RX_FRAME_infos->LS_Rx_Desc = DMARxDescToGet;
  1083. DMA_RX_FRAME_infos->Seg_Count++;
  1084. return 1;
  1085. }
  1086. /* check if first segment */
  1087. else if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  1088. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) != (uint32_t)RESET)&&
  1089. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) == (uint32_t)RESET))
  1090. {
  1091. DMA_RX_FRAME_infos->FS_Rx_Desc = DMARxDescToGet;
  1092. DMA_RX_FRAME_infos->LS_Rx_Desc = NULL;
  1093. DMA_RX_FRAME_infos->Seg_Count = 1;
  1094. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  1095. }
  1096. /* check if intermediate segment */
  1097. else if(((DMARxDescToGet->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  1098. ((DMARxDescToGet->Status & ETH_DMARxDesc_FS) == (uint32_t)RESET)&&
  1099. ((DMARxDescToGet->Status & ETH_DMARxDesc_LS) == (uint32_t)RESET))
  1100. {
  1101. (DMA_RX_FRAME_infos->Seg_Count) ++;
  1102. DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
  1103. }
  1104. return 0;
  1105. }
  1106. /**
  1107. * @brief Initializes the DMA Tx descriptors in chain mode.
  1108. * @param DMATxDescTab: Pointer on the first Tx desc list
  1109. * @param TxBuff: Pointer on the first TxBuffer list
  1110. * @param TxBuffCount: Number of the used Tx desc in the list
  1111. * @retval None
  1112. */
  1113. void ETH_DMATxDescChainInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t* TxBuff, uint32_t TxBuffCount)
  1114. {
  1115. uint32_t i = 0;
  1116. ETH_DMADESCTypeDef *DMATxDesc;
  1117. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  1118. DMATxDescToSet = DMATxDescTab;
  1119. /* Fill each DMATxDesc descriptor with the right values */
  1120. for(i=0; i < TxBuffCount; i++)
  1121. {
  1122. /* Get the pointer on the ith member of the Tx Desc list */
  1123. DMATxDesc = DMATxDescTab + i;
  1124. /* Set Second Address Chained bit */
  1125. DMATxDesc->Status = ETH_DMATxDesc_TCH;
  1126. /* Set Buffer1 address pointer */
  1127. DMATxDesc->Buffer1Addr = (uint32_t)(&TxBuff[i*ETH_TX_BUF_SIZE]);
  1128. /* Initialize the next descriptor with the Next Descriptor Polling Enable */
  1129. if(i < (TxBuffCount-1))
  1130. {
  1131. /* Set next descriptor address register with next descriptor base address */
  1132. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1);
  1133. }
  1134. else
  1135. {
  1136. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  1137. DMATxDesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  1138. }
  1139. }
  1140. /* Set Transmit Desciptor List Address Register */
  1141. ETH->DMATDLAR = (uint32_t) DMATxDescTab;
  1142. }
  1143. /**
  1144. * @brief Checks whether the specified ETHERNET DMA Tx Desc flag is set or not.
  1145. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1146. * @param ETH_DMATxDescFlag: specifies the flag to check.
  1147. * This parameter can be one of the following values:
  1148. * @arg ETH_DMATxDesc_OWN : OWN bit: descriptor is owned by DMA engine
  1149. * @arg ETH_DMATxDesc_IC : Interrupt on completion
  1150. * @arg ETH_DMATxDesc_LS : Last Segment
  1151. * @arg ETH_DMATxDesc_FS : First Segment
  1152. * @arg ETH_DMATxDesc_DC : Disable CRC
  1153. * @arg ETH_DMATxDesc_DP : Disable Pad
  1154. * @arg ETH_DMATxDesc_TTSE: Transmit Time Stamp Enable
  1155. * @arg ETH_DMATxDesc_CIC : Checksum insertion control
  1156. * @arg ETH_DMATxDesc_TER : Transmit End of Ring
  1157. * @arg ETH_DMATxDesc_TCH : Second Address Chained
  1158. * @arg ETH_DMATxDesc_TTSS: Tx Time Stamp Status
  1159. * @arg ETH_DMATxDesc_IHE : IP Header Error
  1160. * @arg ETH_DMATxDesc_ES : Error summary
  1161. * @arg ETH_DMATxDesc_JT : Jabber Timeout
  1162. * @arg ETH_DMATxDesc_FF : Frame Flushed: DMA/MTL flushed the frame due to SW flush
  1163. * @arg ETH_DMATxDesc_PCE : Payload Checksum Error
  1164. * @arg ETH_DMATxDesc_LCA : Loss of Carrier: carrier lost during transmission
  1165. * @arg ETH_DMATxDesc_NC : No Carrier: no carrier signal from the transceiver
  1166. * @arg ETH_DMATxDesc_LCO : Late Collision: transmission aborted due to collision
  1167. * @arg ETH_DMATxDesc_EC : Excessive Collision: transmission aborted after 16 collisions
  1168. * @arg ETH_DMATxDesc_VF : VLAN Frame
  1169. * @arg ETH_DMATxDesc_CC : Collision Count
  1170. * @arg ETH_DMATxDesc_ED : Excessive Deferral
  1171. * @arg ETH_DMATxDesc_UF : Underflow Error: late data arrival from the memory
  1172. * @arg ETH_DMATxDesc_DB : Deferred Bit
  1173. * @retval The new state of ETH_DMATxDescFlag (SET or RESET).
  1174. */
  1175. FlagStatus ETH_GetDMATxDescFlagStatus(ETH_DMADESCTypeDef *DMATxDesc, uint32_t ETH_DMATxDescFlag)
  1176. {
  1177. FlagStatus bitstatus = RESET;
  1178. /* Check the parameters */
  1179. assert_param(IS_ETH_DMATxDESC_GET_FLAG(ETH_DMATxDescFlag));
  1180. if ((DMATxDesc->Status & ETH_DMATxDescFlag) != (uint32_t)RESET)
  1181. {
  1182. bitstatus = SET;
  1183. }
  1184. else
  1185. {
  1186. bitstatus = RESET;
  1187. }
  1188. return bitstatus;
  1189. }
  1190. /**
  1191. * @brief Returns the specified ETHERNET DMA Tx Desc collision count.
  1192. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1193. * @retval The Transmit descriptor collision counter value.
  1194. */
  1195. uint32_t ETH_GetDMATxDescCollisionCount(ETH_DMADESCTypeDef *DMATxDesc)
  1196. {
  1197. /* Return the Receive descriptor frame length */
  1198. return ((DMATxDesc->Status & ETH_DMATxDesc_CC) >> ETH_DMATXDESC_COLLISION_COUNTSHIFT);
  1199. }
  1200. /**
  1201. * @brief Set the specified DMA Tx Desc Own bit.
  1202. * @param DMATxDesc: Pointer on a Tx desc
  1203. * @retval None
  1204. */
  1205. void ETH_SetDMATxDescOwnBit(ETH_DMADESCTypeDef *DMATxDesc)
  1206. {
  1207. /* Set the DMA Tx Desc Own bit */
  1208. DMATxDesc->Status |= ETH_DMATxDesc_OWN;
  1209. }
  1210. /**
  1211. * @brief Enables or disables the specified DMA Tx Desc Transmit interrupt.
  1212. * @param DMATxDesc: Pointer on a Tx desc
  1213. * @param NewState: new state of the DMA Tx Desc transmit interrupt.
  1214. * This parameter can be: ENABLE or DISABLE.
  1215. * @retval None
  1216. */
  1217. void ETH_DMATxDescTransmitITConfig(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1218. {
  1219. /* Check the parameters */
  1220. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1221. if (NewState != DISABLE)
  1222. {
  1223. /* Enable the DMA Tx Desc Transmit interrupt */
  1224. DMATxDesc->Status |= ETH_DMATxDesc_IC;
  1225. }
  1226. else
  1227. {
  1228. /* Disable the DMA Tx Desc Transmit interrupt */
  1229. DMATxDesc->Status &=(~(uint32_t)ETH_DMATxDesc_IC);
  1230. }
  1231. }
  1232. /**
  1233. * @brief configure Tx descriptor as last or first segment
  1234. * @param DMATxDesc: Pointer on a Tx desc
  1235. * @param DMATxDesc_FrameSegment: specifies is the actual Tx desc contain last or first segment.
  1236. * This parameter can be one of the following values:
  1237. * @arg ETH_DMATxDesc_LastSegment : actual Tx desc contain last segment
  1238. * @arg ETH_DMATxDesc_FirstSegment : actual Tx desc contain first segment
  1239. * @retval None
  1240. */
  1241. void ETH_DMATxDescFrameSegmentConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t DMATxDesc_FrameSegment)
  1242. {
  1243. /* Check the parameters */
  1244. assert_param(IS_ETH_DMA_TXDESC_SEGMENT(DMATxDesc_FrameSegment));
  1245. /* Selects the DMA Tx Desc Frame segment */
  1246. DMATxDesc->Status |= DMATxDesc_FrameSegment;
  1247. }
  1248. /**
  1249. * @brief Selects the specified ETHERNET DMA Tx Desc Checksum Insertion.
  1250. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1251. * @param DMATxDesc_Checksum: specifies is the DMA Tx desc checksum insertion.
  1252. * This parameter can be one of the following values:
  1253. * @arg ETH_DMATxDesc_ChecksumByPass : Checksum bypass
  1254. * @arg ETH_DMATxDesc_ChecksumIPV4Header : IPv4 header checksum
  1255. * @arg ETH_DMATxDesc_ChecksumTCPUDPICMPSegment : TCP/UDP/ICMP checksum. Pseudo header checksum is assumed to be present
  1256. * @arg ETH_DMATxDesc_ChecksumTCPUDPICMPFull : TCP/UDP/ICMP checksum fully in hardware including pseudo header
  1257. * @retval None
  1258. */
  1259. void ETH_DMATxDescChecksumInsertionConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t DMATxDesc_Checksum)
  1260. {
  1261. /* Check the parameters */
  1262. assert_param(IS_ETH_DMA_TXDESC_CHECKSUM(DMATxDesc_Checksum));
  1263. /* Set the selected DMA Tx desc checksum insertion control */
  1264. DMATxDesc->Status |= DMATxDesc_Checksum;
  1265. }
  1266. /**
  1267. * @brief Enables or disables the DMA Tx Desc CRC.
  1268. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1269. * @param NewState: new state of the specified DMA Tx Desc CRC.
  1270. * This parameter can be: ENABLE or DISABLE.
  1271. * @retval None
  1272. */
  1273. void ETH_DMATxDescCRCCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1274. {
  1275. /* Check the parameters */
  1276. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1277. if (NewState != DISABLE)
  1278. {
  1279. /* Enable the selected DMA Tx Desc CRC */
  1280. DMATxDesc->Status &= (~(uint32_t)ETH_DMATxDesc_DC);
  1281. }
  1282. else
  1283. {
  1284. /* Disable the selected DMA Tx Desc CRC */
  1285. DMATxDesc->Status |= ETH_DMATxDesc_DC;
  1286. }
  1287. }
  1288. /**
  1289. * @brief Enables or disables the DMA Tx Desc second address chained.
  1290. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1291. * @param NewState: new state of the specified DMA Tx Desc second address chained.
  1292. * This parameter can be: ENABLE or DISABLE.
  1293. * @retval None
  1294. */
  1295. void ETH_DMATxDescSecondAddressChainedCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1296. {
  1297. /* Check the parameters */
  1298. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1299. if (NewState != DISABLE)
  1300. {
  1301. /* Enable the selected DMA Tx Desc second address chained */
  1302. DMATxDesc->Status |= ETH_DMATxDesc_TCH;
  1303. }
  1304. else
  1305. {
  1306. /* Disable the selected DMA Tx Desc second address chained */
  1307. DMATxDesc->Status &=(~(uint32_t)ETH_DMATxDesc_TCH);
  1308. }
  1309. }
  1310. /**
  1311. * @brief Enables or disables the DMA Tx Desc padding for frame shorter than 64 bytes.
  1312. * @param DMATxDesc: pointer on a DMA Tx descriptor
  1313. * @param NewState: new state of the specified DMA Tx Desc padding for frame shorter than 64 bytes.
  1314. * This parameter can be: ENABLE or DISABLE.
  1315. * @retval None
  1316. */
  1317. void ETH_DMATxDescShortFramePaddingCmd(ETH_DMADESCTypeDef *DMATxDesc, FunctionalState NewState)
  1318. {
  1319. /* Check the parameters */
  1320. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1321. if (NewState != DISABLE)
  1322. {
  1323. /* Enable the selected DMA Tx Desc padding for frame shorter than 64 bytes */
  1324. DMATxDesc->Status &= (~(uint32_t)ETH_DMATxDesc_DP);
  1325. }
  1326. else
  1327. {
  1328. /* Disable the selected DMA Tx Desc padding for frame shorter than 64 bytes*/
  1329. DMATxDesc->Status |= ETH_DMATxDesc_DP;
  1330. }
  1331. }
  1332. /**
  1333. * @brief Configures the specified DMA Tx Desc buffer1 and buffer2 sizes.
  1334. * @param DMATxDesc: Pointer on a Tx desc
  1335. * @param BufferSize1: specifies the Tx desc buffer1 size.
  1336. * @param BufferSize2: specifies the Tx desc buffer2 size (put "0" if not used).
  1337. * @retval None
  1338. */
  1339. void ETH_DMATxDescBufferSizeConfig(ETH_DMADESCTypeDef *DMATxDesc, uint32_t BufferSize1, uint32_t BufferSize2)
  1340. {
  1341. /* Check the parameters */
  1342. assert_param(IS_ETH_DMATxDESC_BUFFER_SIZE(BufferSize1));
  1343. assert_param(IS_ETH_DMATxDESC_BUFFER_SIZE(BufferSize2));
  1344. /* Set the DMA Tx Desc buffer1 and buffer2 sizes values */
  1345. DMATxDesc->ControlBufferSize |= (BufferSize1 | (BufferSize2 << ETH_DMATXDESC_BUFFER2_SIZESHIFT));
  1346. }
  1347. /**
  1348. * @brief Checks whether the specified ETHERNET Rx Desc flag is set or not.
  1349. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1350. * @param ETH_DMARxDescFlag: specifies the flag to check.
  1351. * This parameter can be one of the following values:
  1352. * @arg ETH_DMARxDesc_OWN: OWN bit: descriptor is owned by DMA engine
  1353. * @arg ETH_DMARxDesc_AFM: DA Filter Fail for the rx frame
  1354. * @arg ETH_DMARxDesc_ES: Error summary
  1355. * @arg ETH_DMARxDesc_DE: Descriptor error: no more descriptors for receive frame
  1356. * @arg ETH_DMARxDesc_SAF: SA Filter Fail for the received frame
  1357. * @arg ETH_DMARxDesc_LE: Frame size not matching with length field
  1358. * @arg ETH_DMARxDesc_OE: Overflow Error: Frame was damaged due to buffer overflow
  1359. * @arg ETH_DMARxDesc_VLAN: VLAN Tag: received frame is a VLAN frame
  1360. * @arg ETH_DMARxDesc_FS: First descriptor of the frame
  1361. * @arg ETH_DMARxDesc_LS: Last descriptor of the frame
  1362. * @arg ETH_DMARxDesc_IPV4HCE: IPC Checksum Error/Giant Frame: Rx Ipv4 header checksum error
  1363. * @arg ETH_DMARxDesc_LC: Late collision occurred during reception
  1364. * @arg ETH_DMARxDesc_FT: Frame type - Ethernet, otherwise 802.3
  1365. * @arg ETH_DMARxDesc_RWT: Receive Watchdog Timeout: watchdog timer expired during reception
  1366. * @arg ETH_DMARxDesc_RE: Receive error: error reported by MII interface
  1367. * @arg ETH_DMARxDesc_DE: Dribble bit error: frame contains non int multiple of 8 bits
  1368. * @arg ETH_DMARxDesc_CE: CRC error
  1369. * @arg ETH_DMARxDesc_MAMPCE: Rx MAC Address/Payload Checksum Error: Rx MAC address matched/ Rx Payload Checksum Error
  1370. * @retval The new state of ETH_DMARxDescFlag (SET or RESET).
  1371. */
  1372. FlagStatus ETH_GetDMARxDescFlagStatus(ETH_DMADESCTypeDef *DMARxDesc, uint32_t ETH_DMARxDescFlag)
  1373. {
  1374. FlagStatus bitstatus = RESET;
  1375. /* Check the parameters */
  1376. assert_param(IS_ETH_DMARxDESC_GET_FLAG(ETH_DMARxDescFlag));
  1377. if ((DMARxDesc->Status & ETH_DMARxDescFlag) != (uint32_t)RESET)
  1378. {
  1379. bitstatus = SET;
  1380. }
  1381. else
  1382. {
  1383. bitstatus = RESET;
  1384. }
  1385. return bitstatus;
  1386. }
  1387. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  1388. /**
  1389. * @brief Checks whether the specified ETHERNET PTP Rx Desc extended flag is set or not.
  1390. * @param DMAPTPRxDesc: pointer on a DMA PTP Rx descriptor
  1391. * @param ETH_DMAPTPRxDescFlag: specifies the extended flag to check.
  1392. * This parameter can be one of the following values:
  1393. * @arg ETH_DMAPTPRxDesc_PTPV: PTP version
  1394. * @arg ETH_DMAPTPRxDesc_PTPFT: PTP frame type
  1395. * @arg ETH_DMAPTPRxDesc_PTPMT: PTP message type
  1396. * @arg ETH_DMAPTPRxDesc_IPV6PR: IPv6 packet received
  1397. * @arg ETH_DMAPTPRxDesc_IPV4PR: IPv4 packet received
  1398. * @arg ETH_DMAPTPRxDesc_IPCB: IP checksum bypassed
  1399. * @arg ETH_DMAPTPRxDesc_IPPE: IP payload error
  1400. * @arg ETH_DMAPTPRxDesc_IPHE: IP header error
  1401. * @arg ETH_DMAPTPRxDesc_IPPT: IP payload type
  1402. * @retval The new state of ETH_DMAPTPRxDescExtendedFlag (SET or RESET).
  1403. */
  1404. FlagStatus ETH_GetDMAPTPRxDescExtendedFlagStatus(ETH_DMADESCTypeDef *DMAPTPRxDesc, uint32_t ETH_DMAPTPRxDescExtendedFlag)
  1405. {
  1406. FlagStatus bitstatus = RESET;
  1407. /* Check the parameters */
  1408. assert_param(IS_ETH_DMAPTPRxDESC_GET_EXTENDED_FLAG(ETH_DMAPTPRxDescExtendedFlag));
  1409. if ((DMAPTPRxDesc->ExtendedStatus & ETH_DMAPTPRxDescExtendedFlag) != (uint32_t)RESET)
  1410. {
  1411. bitstatus = SET;
  1412. }
  1413. else
  1414. {
  1415. bitstatus = RESET;
  1416. }
  1417. return bitstatus;
  1418. }
  1419. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  1420. /**
  1421. * @brief Set the specified DMA Rx Desc Own bit.
  1422. * @param DMARxDesc: Pointer on a Rx desc
  1423. * @retval None
  1424. */
  1425. void ETH_SetDMARxDescOwnBit(ETH_DMADESCTypeDef *DMARxDesc)
  1426. {
  1427. /* Set the DMA Rx Desc Own bit */
  1428. DMARxDesc->Status |= ETH_DMARxDesc_OWN;
  1429. }
  1430. /**
  1431. * @brief Returns the specified DMA Rx Desc frame length.
  1432. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1433. * @retval The Rx descriptor received frame length.
  1434. */
  1435. uint32_t ETH_GetDMARxDescFrameLength(ETH_DMADESCTypeDef *DMARxDesc)
  1436. {
  1437. /* Return the Receive descriptor frame length */
  1438. return ((DMARxDesc->Status & ETH_DMARxDesc_FL) >> ETH_DMARXDESC_FRAME_LENGTHSHIFT);
  1439. }
  1440. /**
  1441. * @brief Enables or disables the specified DMA Rx Desc receive interrupt.
  1442. * @param DMARxDesc: Pointer on a Rx desc
  1443. * @param NewState: new state of the specified DMA Rx Desc interrupt.
  1444. * This parameter can be: ENABLE or DISABLE.
  1445. * @retval None
  1446. */
  1447. void ETH_DMARxDescReceiveITConfig(ETH_DMADESCTypeDef *DMARxDesc, FunctionalState NewState)
  1448. {
  1449. /* Check the parameters */
  1450. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1451. if (NewState != DISABLE)
  1452. {
  1453. /* Enable the DMA Rx Desc receive interrupt */
  1454. DMARxDesc->ControlBufferSize &=(~(uint32_t)ETH_DMARxDesc_DIC);
  1455. }
  1456. else
  1457. {
  1458. /* Disable the DMA Rx Desc receive interrupt */
  1459. DMARxDesc->ControlBufferSize |= ETH_DMARxDesc_DIC;
  1460. }
  1461. }
  1462. /**
  1463. * @brief Returns the specified ETHERNET DMA Rx Desc buffer size.
  1464. * @param DMARxDesc: pointer on a DMA Rx descriptor
  1465. * @param DMARxDesc_Buffer: specifies the DMA Rx Desc buffer.
  1466. * This parameter can be any one of the following values:
  1467. * @arg ETH_DMARxDesc_Buffer1 : DMA Rx Desc Buffer1
  1468. * @arg ETH_DMARxDesc_Buffer2 : DMA Rx Desc Buffer2
  1469. * @retval The Receive descriptor frame length.
  1470. */
  1471. uint32_t ETH_GetDMARxDescBufferSize(ETH_DMADESCTypeDef *DMARxDesc, uint32_t DMARxDesc_Buffer)
  1472. {
  1473. /* Check the parameters */
  1474. assert_param(IS_ETH_DMA_RXDESC_BUFFER(DMARxDesc_Buffer));
  1475. if(DMARxDesc_Buffer != ETH_DMARxDesc_Buffer1)
  1476. {
  1477. /* Return the DMA Rx Desc buffer2 size */
  1478. return ((DMARxDesc->ControlBufferSize & ETH_DMARxDesc_RBS2) >> ETH_DMARXDESC_BUFFER2_SIZESHIFT);
  1479. }
  1480. else
  1481. {
  1482. /* Return the DMA Rx Desc buffer1 size */
  1483. return (DMARxDesc->ControlBufferSize & ETH_DMARxDesc_RBS1);
  1484. }
  1485. }
  1486. /**
  1487. * @brief Get the size of the received packet.
  1488. * @param None
  1489. * @retval framelength: received packet size
  1490. */
  1491. uint32_t ETH_GetRxPktSize(ETH_DMADESCTypeDef *DMARxDesc)
  1492. {
  1493. uint32_t frameLength = 0;
  1494. if(((DMARxDesc->Status & ETH_DMARxDesc_OWN) == (uint32_t)RESET) &&
  1495. ((DMARxDesc->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET) &&
  1496. ((DMARxDesc->Status & ETH_DMARxDesc_LS) != (uint32_t)RESET))
  1497. {
  1498. /* Get the size of the packet: including 4 bytes of the CRC */
  1499. frameLength = ETH_GetDMARxDescFrameLength(DMARxDesc);
  1500. }
  1501. /* Return Frame Length */
  1502. return frameLength;
  1503. }
  1504. #ifdef USE_ENHANCED_DMA_DESCRIPTORS
  1505. /**
  1506. * @brief Enables or disables the Enhanced descriptor structure.
  1507. * @param NewState: new state of the Enhanced descriptor structure.
  1508. * This parameter can be: ENABLE or DISABLE.
  1509. * @retval None
  1510. */
  1511. void ETH_EnhancedDescriptorCmd(FunctionalState NewState)
  1512. {
  1513. /* Check the parameters */
  1514. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1515. if (NewState != DISABLE)
  1516. {
  1517. /* Enable enhanced descriptor structure */
  1518. ETH->DMABMR |= ETH_DMABMR_EDE;
  1519. }
  1520. else
  1521. {
  1522. /* Disable enhanced descriptor structure */
  1523. ETH->DMABMR &= ~ETH_DMABMR_EDE;
  1524. }
  1525. }
  1526. #endif /* USE_ENHANCED_DMA_DESCRIPTORS */
  1527. /******************************************************************************/
  1528. /* DMA functions */
  1529. /******************************************************************************/
  1530. /**
  1531. * @brief Resets all MAC subsystem internal registers and logic.
  1532. * @param None
  1533. * @retval None
  1534. */
  1535. void ETH_SoftwareReset(void)
  1536. {
  1537. /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
  1538. /* After reset all the registers holds their respective reset values */
  1539. ETH->DMABMR |= ETH_DMABMR_SR;
  1540. }
  1541. /**
  1542. * @brief Checks whether the ETHERNET software reset bit is set or not.
  1543. * @param None
  1544. * @retval The new state of DMA Bus Mode register SR bit (SET or RESET).
  1545. */
  1546. FlagStatus ETH_GetSoftwareResetStatus(void)
  1547. {
  1548. FlagStatus bitstatus = RESET;
  1549. if((ETH->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)
  1550. {
  1551. bitstatus = SET;
  1552. }
  1553. else
  1554. {
  1555. bitstatus = RESET;
  1556. }
  1557. return bitstatus;
  1558. }
  1559. /**
  1560. * @brief Checks whether the specified ETHERNET DMA flag is set or not.
  1561. * @param ETH_DMA_FLAG: specifies the flag to check.
  1562. * This parameter can be one of the following values:
  1563. * @arg ETH_DMA_FLAG_TST : Time-stamp trigger flag
  1564. * @arg ETH_DMA_FLAG_PMT : PMT flag
  1565. * @arg ETH_DMA_FLAG_MMC : MMC flag
  1566. * @arg ETH_DMA_FLAG_DataTransferError : Error bits 0-data buffer, 1-desc. access
  1567. * @arg ETH_DMA_FLAG_ReadWriteError : Error bits 0-write trnsf, 1-read transfr
  1568. * @arg ETH_DMA_FLAG_AccessError : Error bits 0-Rx DMA, 1-Tx DMA
  1569. * @arg ETH_DMA_FLAG_NIS : Normal interrupt summary flag
  1570. * @arg ETH_DMA_FLAG_AIS : Abnormal interrupt summary flag
  1571. * @arg ETH_DMA_FLAG_ER : Early receive flag
  1572. * @arg ETH_DMA_FLAG_FBE : Fatal bus error flag
  1573. * @arg ETH_DMA_FLAG_ET : Early transmit flag
  1574. * @arg ETH_DMA_FLAG_RWT : Receive watchdog timeout flag
  1575. * @arg ETH_DMA_FLAG_RPS : Receive process stopped flag
  1576. * @arg ETH_DMA_FLAG_RBU : Receive buffer unavailable flag
  1577. * @arg ETH_DMA_FLAG_R : Receive flag
  1578. * @arg ETH_DMA_FLAG_TU : Underflow flag
  1579. * @arg ETH_DMA_FLAG_RO : Overflow flag
  1580. * @arg ETH_DMA_FLAG_TJT : Transmit jabber timeout flag
  1581. * @arg ETH_DMA_FLAG_TBU : Transmit buffer unavailable flag
  1582. * @arg ETH_DMA_FLAG_TPS : Transmit process stopped flag
  1583. * @arg ETH_DMA_FLAG_T : Transmit flag
  1584. * @retval The new state of ETH_DMA_FLAG (SET or RESET).
  1585. */
  1586. FlagStatus ETH_GetDMAFlagStatus(uint32_t ETH_DMA_FLAG)
  1587. {
  1588. FlagStatus bitstatus = RESET;
  1589. /* Check the parameters */
  1590. assert_param(IS_ETH_DMA_GET_IT(ETH_DMA_FLAG));
  1591. if ((ETH->DMASR & ETH_DMA_FLAG) != (uint32_t)RESET)
  1592. {
  1593. bitstatus = SET;
  1594. }
  1595. else
  1596. {
  1597. bitstatus = RESET;
  1598. }
  1599. return bitstatus;
  1600. }
  1601. /**
  1602. * @brief Clears the ETHERNET’s DMA pending flag.
  1603. * @param ETH_DMA_FLAG: specifies the flag to clear.
  1604. * This parameter can be any combination of the following values:
  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_ETI : 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 : Transmit Underflow flag
  1615. * @arg ETH_DMA_FLAG_RO : Receive 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 None
  1621. */
  1622. void ETH_DMAClearFlag(uint32_t ETH_DMA_FLAG)
  1623. {
  1624. /* Check the parameters */
  1625. assert_param(IS_ETH_DMA_FLAG(ETH_DMA_FLAG));
  1626. /* Clear the selected ETHERNET DMA FLAG */
  1627. ETH->DMASR = (uint32_t) ETH_DMA_FLAG;
  1628. }
  1629. /**
  1630. * @brief Enables or disables the specified ETHERNET DMA interrupts.
  1631. * @param ETH_DMA_IT: specifies the ETHERNET DMA interrupt sources to be
  1632. * enabled or disabled.
  1633. * This parameter can be any combination of the following values:
  1634. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1635. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1636. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1637. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1638. * @arg ETH_DMA_IT_ET : Early transmit interrupt
  1639. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1640. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1641. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1642. * @arg ETH_DMA_IT_R : Receive interrupt
  1643. * @arg ETH_DMA_IT_TU : Underflow interrupt
  1644. * @arg ETH_DMA_IT_RO : Overflow interrupt
  1645. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1646. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1647. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1648. * @arg ETH_DMA_IT_T : Transmit interrupt
  1649. * @param NewState: new state of the specified ETHERNET DMA interrupts.
  1650. * This parameter can be: ENABLE or DISABLE.
  1651. * @retval None
  1652. */
  1653. void ETH_DMAITConfig(uint32_t ETH_DMA_IT, FunctionalState NewState)
  1654. {
  1655. /* Check the parameters */
  1656. assert_param(IS_ETH_DMA_IT(ETH_DMA_IT));
  1657. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1658. if (NewState != DISABLE)
  1659. {
  1660. /* Enable the selected ETHERNET DMA interrupts */
  1661. ETH->DMAIER |= ETH_DMA_IT;
  1662. }
  1663. else
  1664. {
  1665. /* Disable the selected ETHERNET DMA interrupts */
  1666. ETH->DMAIER &=(~(uint32_t)ETH_DMA_IT);
  1667. }
  1668. }
  1669. /**
  1670. * @brief Checks whether the specified ETHERNET DMA interrupt has occurred or not.
  1671. * @param ETH_DMA_IT: specifies the interrupt source to check.
  1672. * This parameter can be one of the following values:
  1673. * @arg ETH_DMA_IT_TST : Time-stamp trigger interrupt
  1674. * @arg ETH_DMA_IT_PMT : PMT interrupt
  1675. * @arg ETH_DMA_IT_MMC : MMC interrupt
  1676. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1677. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1678. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1679. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1680. * @arg ETH_DMA_IT_ET : Early transmit interrupt
  1681. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1682. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1683. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1684. * @arg ETH_DMA_IT_R : Receive interrupt
  1685. * @arg ETH_DMA_IT_TU : Underflow interrupt
  1686. * @arg ETH_DMA_IT_RO : Overflow interrupt
  1687. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1688. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1689. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1690. * @arg ETH_DMA_IT_T : Transmit interrupt
  1691. * @retval The new state of ETH_DMA_IT (SET or RESET).
  1692. */
  1693. ITStatus ETH_GetDMAITStatus(uint32_t ETH_DMA_IT)
  1694. {
  1695. ITStatus bitstatus = RESET;
  1696. /* Check the parameters */
  1697. assert_param(IS_ETH_DMA_GET_IT(ETH_DMA_IT));
  1698. if ((ETH->DMASR & ETH_DMA_IT) != (uint32_t)RESET)
  1699. {
  1700. bitstatus = SET;
  1701. }
  1702. else
  1703. {
  1704. bitstatus = RESET;
  1705. }
  1706. return bitstatus;
  1707. }
  1708. /**
  1709. * @brief Clears the ETHERNET’s DMA IT pending bit.
  1710. * @param ETH_DMA_IT: specifies the interrupt pending bit to clear.
  1711. * This parameter can be any combination of the following values:
  1712. * @arg ETH_DMA_IT_NIS : Normal interrupt summary
  1713. * @arg ETH_DMA_IT_AIS : Abnormal interrupt summary
  1714. * @arg ETH_DMA_IT_ER : Early receive interrupt
  1715. * @arg ETH_DMA_IT_FBE : Fatal bus error interrupt
  1716. * @arg ETH_DMA_IT_ETI : Early transmit interrupt
  1717. * @arg ETH_DMA_IT_RWT : Receive watchdog timeout interrupt
  1718. * @arg ETH_DMA_IT_RPS : Receive process stopped interrupt
  1719. * @arg ETH_DMA_IT_RBU : Receive buffer unavailable interrupt
  1720. * @arg ETH_DMA_IT_R : Receive interrupt
  1721. * @arg ETH_DMA_IT_TU : Transmit Underflow interrupt
  1722. * @arg ETH_DMA_IT_RO : Receive Overflow interrupt
  1723. * @arg ETH_DMA_IT_TJT : Transmit jabber timeout interrupt
  1724. * @arg ETH_DMA_IT_TBU : Transmit buffer unavailable interrupt
  1725. * @arg ETH_DMA_IT_TPS : Transmit process stopped interrupt
  1726. * @arg ETH_DMA_IT_T : Transmit interrupt
  1727. * @retval None
  1728. */
  1729. void ETH_DMAClearITPendingBit(uint32_t ETH_DMA_IT)
  1730. {
  1731. /* Check the parameters */
  1732. assert_param(IS_ETH_DMA_IT(ETH_DMA_IT));
  1733. /* Clear the selected ETHERNET DMA IT */
  1734. ETH->DMASR = (uint32_t) ETH_DMA_IT;
  1735. }
  1736. /**
  1737. * @brief Returns the ETHERNET DMA Transmit Process State.
  1738. * @param None
  1739. * @retval The new ETHERNET DMA Transmit Process State:
  1740. * This can be one of the following values:
  1741. * - ETH_DMA_TransmitProcess_Stopped : Stopped - Reset or Stop Tx Command issued
  1742. * - ETH_DMA_TransmitProcess_Fetching : Running - fetching the Tx descriptor
  1743. * - ETH_DMA_TransmitProcess_Waiting : Running - waiting for status
  1744. * - ETH_DMA_TransmitProcess_Reading : Running - reading the data from host memory
  1745. * - ETH_DMA_TransmitProcess_Suspended : Suspended - Tx Descriptor unavailable
  1746. * - ETH_DMA_TransmitProcess_Closing : Running - closing Rx descriptor
  1747. */
  1748. uint32_t ETH_GetTransmitProcessState(void)
  1749. {
  1750. return ((uint32_t)(ETH->DMASR & ETH_DMASR_TS));
  1751. }
  1752. /**
  1753. * @brief Returns the ETHERNET DMA Receive Process State.
  1754. * @param None
  1755. * @retval The new ETHERNET DMA Receive Process State:
  1756. * This can be one of the following values:
  1757. * - ETH_DMA_ReceiveProcess_Stopped : Stopped - Reset or Stop Rx Command issued
  1758. * - ETH_DMA_ReceiveProcess_Fetching : Running - fetching the Rx descriptor
  1759. * - ETH_DMA_ReceiveProcess_Waiting : Running - waiting for packet
  1760. * - ETH_DMA_ReceiveProcess_Suspended : Suspended - Rx Descriptor unavailable
  1761. * - ETH_DMA_ReceiveProcess_Closing : Running - closing descriptor
  1762. * - ETH_DMA_ReceiveProcess_Queuing : Running - queuing the receive frame into host memory
  1763. */
  1764. uint32_t ETH_GetReceiveProcessState(void)
  1765. {
  1766. return ((uint32_t)(ETH->DMASR & ETH_DMASR_RS));
  1767. }
  1768. /**
  1769. * @brief Clears the ETHERNET transmit FIFO.
  1770. * @param None
  1771. * @retval None
  1772. */
  1773. void ETH_FlushTransmitFIFO(void)
  1774. {
  1775. /* Set the Flush Transmit FIFO bit */
  1776. ETH->DMAOMR |= ETH_DMAOMR_FTF;
  1777. }
  1778. /**
  1779. * @brief Checks whether the ETHERNET flush transmit FIFO bit is cleared or not.
  1780. * @param None
  1781. * @retval The new state of ETHERNET flush transmit FIFO bit (SET or RESET).
  1782. */
  1783. FlagStatus ETH_GetFlushTransmitFIFOStatus(void)
  1784. {
  1785. FlagStatus bitstatus = RESET;
  1786. if ((ETH->DMAOMR & ETH_DMAOMR_FTF) != (uint32_t)RESET)
  1787. {
  1788. bitstatus = SET;
  1789. }
  1790. else
  1791. {
  1792. bitstatus = RESET;
  1793. }
  1794. return bitstatus;
  1795. }
  1796. /**
  1797. * @brief Enables or disables the DMA transmission.
  1798. * @param NewState: new state of the DMA transmission.
  1799. * This parameter can be: ENABLE or DISABLE.
  1800. * @retval None
  1801. */
  1802. void ETH_DMATransmissionCmd(FunctionalState NewState)
  1803. {
  1804. /* Check the parameters */
  1805. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1806. if (NewState != DISABLE)
  1807. {
  1808. /* Enable the DMA transmission */
  1809. ETH->DMAOMR |= ETH_DMAOMR_ST;
  1810. }
  1811. else
  1812. {
  1813. /* Disable the DMA transmission */
  1814. ETH->DMAOMR &= ~ETH_DMAOMR_ST;
  1815. }
  1816. }
  1817. /**
  1818. * @brief Enables or disables the DMA reception.
  1819. * @param NewState: new state of the DMA reception.
  1820. * This parameter can be: ENABLE or DISABLE.
  1821. * @retval None
  1822. */
  1823. void ETH_DMAReceptionCmd(FunctionalState NewState)
  1824. {
  1825. /* Check the parameters */
  1826. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1827. if (NewState != DISABLE)
  1828. {
  1829. /* Enable the DMA reception */
  1830. ETH->DMAOMR |= ETH_DMAOMR_SR;
  1831. }
  1832. else
  1833. {
  1834. /* Disable the DMA reception */
  1835. ETH->DMAOMR &= ~ETH_DMAOMR_SR;
  1836. }
  1837. }
  1838. /**
  1839. * @brief Checks whether the specified ETHERNET DMA overflow flag is set or not.
  1840. * @param ETH_DMA_Overflow: specifies the DMA overflow flag to check.
  1841. * This parameter can be one of the following values:
  1842. * @arg ETH_DMA_Overflow_RxFIFOCounter : Overflow for FIFO Overflows Counter
  1843. * @arg ETH_DMA_Overflow_MissedFrameCounter : Overflow for Buffer Unavailable Missed Frame Counter
  1844. * @retval The new state of ETHERNET DMA overflow Flag (SET or RESET).
  1845. */
  1846. FlagStatus ETH_GetDMAOverflowStatus(uint32_t ETH_DMA_Overflow)
  1847. {
  1848. FlagStatus bitstatus = RESET;
  1849. /* Check the parameters */
  1850. assert_param(IS_ETH_DMA_GET_OVERFLOW(ETH_DMA_Overflow));
  1851. if ((ETH->DMAMFBOCR & ETH_DMA_Overflow) != (uint32_t)RESET)
  1852. {
  1853. bitstatus = SET;
  1854. }
  1855. else
  1856. {
  1857. bitstatus = RESET;
  1858. }
  1859. return bitstatus;
  1860. }
  1861. /**
  1862. * @brief Get the ETHERNET DMA Rx Overflow Missed Frame Counter value.
  1863. * @param None
  1864. * @retval The value of Rx overflow Missed Frame Counter.
  1865. */
  1866. uint32_t ETH_GetRxOverflowMissedFrameCounter(void)
  1867. {
  1868. return ((uint32_t)((ETH->DMAMFBOCR & ETH_DMAMFBOCR_MFA)>>ETH_DMA_RX_OVERFLOW_MISSEDFRAMES_COUNTERSHIFT));
  1869. }
  1870. /**
  1871. * @brief Get the ETHERNET DMA Buffer Unavailable Missed Frame Counter value.
  1872. * @param None
  1873. * @retval The value of Buffer unavailable Missed Frame Counter.
  1874. */
  1875. uint32_t ETH_GetBufferUnavailableMissedFrameCounter(void)
  1876. {
  1877. return ((uint32_t)(ETH->DMAMFBOCR) & ETH_DMAMFBOCR_MFC);
  1878. }
  1879. /**
  1880. * @brief Get the ETHERNET DMA DMACHTDR register value.
  1881. * @param None
  1882. * @retval The value of the current Tx desc start address.
  1883. */
  1884. uint32_t ETH_GetCurrentTxDescStartAddress(void)
  1885. {
  1886. return ((uint32_t)(ETH->DMACHTDR));
  1887. }
  1888. /**
  1889. * @brief Get the ETHERNET DMA DMACHRDR register value.
  1890. * @param None
  1891. * @retval The value of the current Rx desc start address.
  1892. */
  1893. uint32_t ETH_GetCurrentRxDescStartAddress(void)
  1894. {
  1895. return ((uint32_t)(ETH->DMACHRDR));
  1896. }
  1897. /**
  1898. * @brief Get the ETHERNET DMA DMACHTBAR register value.
  1899. * @param None
  1900. * @retval The value of the current transmit descriptor data buffer address.
  1901. */
  1902. uint32_t ETH_GetCurrentTxBufferAddress(void)
  1903. {
  1904. return ((uint32_t)(ETH->DMACHTBAR));
  1905. }
  1906. /**
  1907. * @brief Get the ETHERNET DMA DMACHRBAR register value.
  1908. * @param None
  1909. * @retval The value of the current receive descriptor data buffer address.
  1910. */
  1911. uint32_t ETH_GetCurrentRxBufferAddress(void)
  1912. {
  1913. return ((uint32_t)(ETH->DMACHRBAR));
  1914. }
  1915. /**
  1916. * @brief Resumes the DMA Transmission by writing to the DmaTxPollDemand register
  1917. * (the data written could be anything). This forces the DMA to resume transmission.
  1918. * @param None
  1919. * @retval None.
  1920. */
  1921. void ETH_ResumeDMATransmission(void)
  1922. {
  1923. ETH->DMATPDR = 0;
  1924. }
  1925. /**
  1926. * @brief Resumes the DMA Transmission by writing to the DmaRxPollDemand register
  1927. * (the data written could be anything). This forces the DMA to resume reception.
  1928. * @param None
  1929. * @retval None.
  1930. */
  1931. void ETH_ResumeDMAReception(void)
  1932. {
  1933. ETH->DMARPDR = 0;
  1934. }
  1935. /**
  1936. * @brief Set the DMA Receive status watchdog timer register value
  1937. * @param Value: DMA Receive status watchdog timer register value
  1938. * @retval None
  1939. */
  1940. void ETH_SetReceiveWatchdogTimer(uint8_t Value)
  1941. {
  1942. /* Set the DMA Receive status watchdog timer register */
  1943. ETH->DMARSWTR = Value;
  1944. }
  1945. /******************************************************************************/
  1946. /* PHY functions */
  1947. /******************************************************************************/
  1948. /**
  1949. * @brief Read a PHY register
  1950. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  1951. * This parameter can be one of the following values: 0,..,31
  1952. * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
  1953. * This parameter can be one of the following values:
  1954. * @arg PHY_BCR: Transceiver Basic Control Register
  1955. * @arg PHY_BSR: Transceiver Basic Status Register
  1956. * @arg PHY_SR : Transceiver Status Register
  1957. * @arg More PHY register could be read depending on the used PHY
  1958. * @retval ETH_ERROR: in case of timeout
  1959. * MAC MIIDR register value: Data read from the selected PHY register (correct read )
  1960. */
  1961. uint16_t ETH_ReadPHYRegister(uint16_t PHYAddress, uint16_t PHYReg)
  1962. {
  1963. uint32_t tmpreg = 0;
  1964. __IO uint32_t timeout = 0;
  1965. /* Check the parameters */
  1966. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  1967. assert_param(IS_ETH_PHY_REG(PHYReg));
  1968. /* Get the ETHERNET MACMIIAR value */
  1969. tmpreg = ETH->MACMIIAR;
  1970. /* Keep only the CSR Clock Range CR[2:0] bits value */
  1971. tmpreg &= ~MACMIIAR_CR_MASK;
  1972. /* Prepare the MII address register value */
  1973. tmpreg |=(((uint32_t)PHYAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  1974. tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  1975. tmpreg &= ~ETH_MACMIIAR_MW; /* Set the read mode */
  1976. tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  1977. /* Write the result value into the MII Address register */
  1978. ETH->MACMIIAR = tmpreg;
  1979. /* Check for the Busy flag */
  1980. do
  1981. {
  1982. timeout++;
  1983. tmpreg = ETH->MACMIIAR;
  1984. } while ((tmpreg & ETH_MACMIIAR_MB) && (timeout < (uint32_t)PHY_READ_TO));
  1985. /* Return ERROR in case of timeout */
  1986. if(timeout == PHY_READ_TO)
  1987. {
  1988. return (uint16_t)ETH_ERROR;
  1989. }
  1990. /* Return data register value */
  1991. return (uint16_t)(ETH->MACMIIDR);
  1992. }
  1993. /**
  1994. * @brief Write to a PHY register
  1995. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  1996. * This parameter can be one of the following values: 0,..,31
  1997. * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
  1998. * This parameter can be one of the following values:
  1999. * @arg PHY_BCR : Transceiver Control Register
  2000. * @arg More PHY register could be written depending on the used PHY
  2001. * @param PHYValue: the value to write
  2002. * @retval ETH_ERROR: in case of timeout
  2003. * ETH_SUCCESS: for correct write
  2004. */
  2005. uint32_t ETH_WritePHYRegister(uint16_t PHYAddress, uint16_t PHYReg, uint16_t PHYValue)
  2006. {
  2007. uint32_t tmpreg = 0;
  2008. __IO uint32_t timeout = 0;
  2009. /* Check the parameters */
  2010. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  2011. assert_param(IS_ETH_PHY_REG(PHYReg));
  2012. /* Get the ETHERNET MACMIIAR value */
  2013. tmpreg = ETH->MACMIIAR;
  2014. /* Keep only the CSR Clock Range CR[2:0] bits value */
  2015. tmpreg &= ~MACMIIAR_CR_MASK;
  2016. /* Prepare the MII register address value */
  2017. tmpreg |=(((uint32_t)PHYAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  2018. tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  2019. tmpreg |= ETH_MACMIIAR_MW; /* Set the write mode */
  2020. tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  2021. /* Give the value to the MII data register */
  2022. ETH->MACMIIDR = PHYValue;
  2023. /* Write the result value into the MII Address register */
  2024. ETH->MACMIIAR = tmpreg;
  2025. /* Check for the Busy flag */
  2026. do
  2027. {
  2028. timeout++;
  2029. tmpreg = ETH->MACMIIAR;
  2030. } while ((tmpreg & ETH_MACMIIAR_MB) && (timeout < (uint32_t)PHY_WRITE_TO));
  2031. /* Return ERROR in case of timeout */
  2032. if(timeout == PHY_WRITE_TO)
  2033. {
  2034. return ETH_ERROR;
  2035. }
  2036. /* Return SUCCESS */
  2037. return ETH_SUCCESS;
  2038. }
  2039. /**
  2040. * @brief Enables or disables the PHY loopBack mode.
  2041. * @Note: Don't be confused with ETH_MACLoopBackCmd function which enables internal
  2042. * loopback at MII level
  2043. * @param PHYAddress: PHY device address, is the index of one of supported 32 PHY devices.
  2044. * @param NewState: new state of the PHY loopBack mode.
  2045. * This parameter can be: ENABLE or DISABLE.
  2046. * @retval ETH_ERROR: in case of bad PHY configuration
  2047. * ETH_SUCCESS: for correct PHY configuration
  2048. */
  2049. uint32_t ETH_PHYLoopBackCmd(uint16_t PHYAddress, FunctionalState NewState)
  2050. {
  2051. uint16_t tmpreg = 0;
  2052. /* Check the parameters */
  2053. assert_param(IS_ETH_PHY_ADDRESS(PHYAddress));
  2054. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2055. /* Get the PHY configuration to update it */
  2056. tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_BCR);
  2057. if (NewState != DISABLE)
  2058. {
  2059. /* Enable the PHY loopback mode */
  2060. tmpreg |= PHY_Loopback;
  2061. }
  2062. else
  2063. {
  2064. /* Disable the PHY loopback mode: normal mode */
  2065. tmpreg &= (uint16_t)(~(uint16_t)PHY_Loopback);
  2066. }
  2067. /* Update the PHY control register with the new configuration */
  2068. if(ETH_WritePHYRegister(PHYAddress, PHY_BCR, tmpreg) != (uint32_t)RESET)
  2069. {
  2070. return ETH_SUCCESS;
  2071. }
  2072. else
  2073. {
  2074. /* Return SUCCESS */
  2075. return ETH_ERROR;
  2076. }
  2077. }
  2078. /******************************************************************************/
  2079. /* Power Management(PMT) functions */
  2080. /******************************************************************************/
  2081. /**
  2082. * @brief Reset Wakeup frame filter register pointer.
  2083. * @param None
  2084. * @retval None
  2085. */
  2086. void ETH_ResetWakeUpFrameFilterRegisterPointer(void)
  2087. {
  2088. /* Resets the Remote Wake-up Frame Filter register pointer to 0x0000 */
  2089. ETH->MACPMTCSR |= ETH_MACPMTCSR_WFFRPR;
  2090. }
  2091. /**
  2092. * @brief Populates the remote wakeup frame registers.
  2093. * @param Buffer: Pointer on remote WakeUp Frame Filter Register buffer data (8 words).
  2094. * @retval None
  2095. */
  2096. void ETH_SetWakeUpFrameFilterRegister(uint32_t *Buffer)
  2097. {
  2098. uint32_t i = 0;
  2099. /* Fill Remote Wake-up Frame Filter register with Buffer data */
  2100. for(i =0; i<ETH_WAKEUP_REGISTER_LENGTH; i++)
  2101. {
  2102. /* Write each time to the same register */
  2103. ETH->MACRWUFFR = Buffer[i];
  2104. }
  2105. }
  2106. /**
  2107. * @brief Enables or disables any unicast packet filtered by the MAC address
  2108. * recognition to be a wake-up frame.
  2109. * @param NewState: new state of the MAC Global Unicast Wake-Up.
  2110. * This parameter can be: ENABLE or DISABLE.
  2111. * @retval None
  2112. */
  2113. void ETH_GlobalUnicastWakeUpCmd(FunctionalState NewState)
  2114. {
  2115. /* Check the parameters */
  2116. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2117. if (NewState != DISABLE)
  2118. {
  2119. /* Enable the MAC Global Unicast Wake-Up */
  2120. ETH->MACPMTCSR |= ETH_MACPMTCSR_GU;
  2121. }
  2122. else
  2123. {
  2124. /* Disable the MAC Global Unicast Wake-Up */
  2125. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_GU;
  2126. }
  2127. }
  2128. /**
  2129. * @brief Checks whether the specified ETHERNET PMT flag is set or not.
  2130. * @param ETH_PMT_FLAG: specifies the flag to check.
  2131. * This parameter can be one of the following values:
  2132. * @arg ETH_PMT_FLAG_WUFFRPR : Wake-Up Frame Filter Register Pointer Reset
  2133. * @arg ETH_PMT_FLAG_WUFR : Wake-Up Frame Received
  2134. * @arg ETH_PMT_FLAG_MPR : Magic Packet Received
  2135. * @retval The new state of ETHERNET PMT Flag (SET or RESET).
  2136. */
  2137. FlagStatus ETH_GetPMTFlagStatus(uint32_t ETH_PMT_FLAG)
  2138. {
  2139. FlagStatus bitstatus = RESET;
  2140. /* Check the parameters */
  2141. assert_param(IS_ETH_PMT_GET_FLAG(ETH_PMT_FLAG));
  2142. if ((ETH->MACPMTCSR & ETH_PMT_FLAG) != (uint32_t)RESET)
  2143. {
  2144. bitstatus = SET;
  2145. }
  2146. else
  2147. {
  2148. bitstatus = RESET;
  2149. }
  2150. return bitstatus;
  2151. }
  2152. /**
  2153. * @brief Enables or disables the MAC Wake-Up Frame Detection.
  2154. * @param NewState: new state of the MAC Wake-Up Frame Detection.
  2155. * This parameter can be: ENABLE or DISABLE.
  2156. * @retval None
  2157. */
  2158. void ETH_WakeUpFrameDetectionCmd(FunctionalState NewState)
  2159. {
  2160. /* Check the parameters */
  2161. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2162. if (NewState != DISABLE)
  2163. {
  2164. /* Enable the MAC Wake-Up Frame Detection */
  2165. ETH->MACPMTCSR |= ETH_MACPMTCSR_WFE;
  2166. }
  2167. else
  2168. {
  2169. /* Disable the MAC Wake-Up Frame Detection */
  2170. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_WFE;
  2171. }
  2172. }
  2173. /**
  2174. * @brief Enables or disables the MAC Magic Packet Detection.
  2175. * @param NewState: new state of the MAC Magic Packet Detection.
  2176. * This parameter can be: ENABLE or DISABLE.
  2177. * @retval None
  2178. */
  2179. void ETH_MagicPacketDetectionCmd(FunctionalState NewState)
  2180. {
  2181. /* Check the parameters */
  2182. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2183. if (NewState != DISABLE)
  2184. {
  2185. /* Enable the MAC Magic Packet Detection */
  2186. ETH->MACPMTCSR |= ETH_MACPMTCSR_MPE;
  2187. }
  2188. else
  2189. {
  2190. /* Disable the MAC Magic Packet Detection */
  2191. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_MPE;
  2192. }
  2193. }
  2194. /**
  2195. * @brief Enables or disables the MAC Power Down.
  2196. * @param NewState: new state of the MAC Power Down.
  2197. * This parameter can be: ENABLE or DISABLE.
  2198. * @retval None
  2199. */
  2200. void ETH_PowerDownCmd(FunctionalState NewState)
  2201. {
  2202. /* Check the parameters */
  2203. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2204. if (NewState != DISABLE)
  2205. {
  2206. /* Enable the MAC Power Down */
  2207. /* This puts the MAC in power down mode */
  2208. ETH->MACPMTCSR |= ETH_MACPMTCSR_PD;
  2209. }
  2210. else
  2211. {
  2212. /* Disable the MAC Power Down */
  2213. ETH->MACPMTCSR &= ~ETH_MACPMTCSR_PD;
  2214. }
  2215. }
  2216. /******************************************************************************/
  2217. /* MMC functions */
  2218. /******************************************************************************/
  2219. /**
  2220. * @brief Preset and Initialize the MMC counters to almost-full value: 0xFFFF_FFF0 (full - 16)
  2221. * @param None
  2222. * @retval None
  2223. */
  2224. void ETH_MMCCounterFullPreset(void)
  2225. {
  2226. /* Preset and Initialize the MMC counters to almost-full value */
  2227. ETH->MMCCR |= ETH_MMCCR_MCFHP | ETH_MMCCR_MCP;
  2228. }
  2229. /**
  2230. * @brief Preset and Initialize the MMC counters to almost-hal value: 0x7FFF_FFF0 (half - 16).
  2231. * @param None
  2232. * @retval None
  2233. */
  2234. void ETH_MMCCounterHalfPreset(void)
  2235. {
  2236. /* Preset the MMC counters to almost-full value */
  2237. ETH->MMCCR &= ~ETH_MMCCR_MCFHP;
  2238. /* Initialize the MMC counters to almost-half value */
  2239. ETH->MMCCR |= ETH_MMCCR_MCP;
  2240. }
  2241. /**
  2242. * @brief Enables or disables the MMC Counter Freeze.
  2243. * @param NewState: new state of the MMC Counter Freeze.
  2244. * This parameter can be: ENABLE or DISABLE.
  2245. * @retval None
  2246. */
  2247. void ETH_MMCCounterFreezeCmd(FunctionalState NewState)
  2248. {
  2249. /* Check the parameters */
  2250. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2251. if (NewState != DISABLE)
  2252. {
  2253. /* Enable the MMC Counter Freeze */
  2254. ETH->MMCCR |= ETH_MMCCR_MCF;
  2255. }
  2256. else
  2257. {
  2258. /* Disable the MMC Counter Freeze */
  2259. ETH->MMCCR &= ~ETH_MMCCR_MCF;
  2260. }
  2261. }
  2262. /**
  2263. * @brief Enables or disables the MMC Reset On Read.
  2264. * @param NewState: new state of the MMC Reset On Read.
  2265. * This parameter can be: ENABLE or DISABLE.
  2266. * @retval None
  2267. */
  2268. void ETH_MMCResetOnReadCmd(FunctionalState NewState)
  2269. {
  2270. /* Check the parameters */
  2271. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2272. if (NewState != DISABLE)
  2273. {
  2274. /* Enable the MMC Counter reset on read */
  2275. ETH->MMCCR |= ETH_MMCCR_ROR;
  2276. }
  2277. else
  2278. {
  2279. /* Disable the MMC Counter reset on read */
  2280. ETH->MMCCR &= ~ETH_MMCCR_ROR;
  2281. }
  2282. }
  2283. /**
  2284. * @brief Enables or disables the MMC Counter Stop Rollover.
  2285. * @param NewState: new state of the MMC Counter Stop Rollover.
  2286. * This parameter can be: ENABLE or DISABLE.
  2287. * @retval None
  2288. */
  2289. void ETH_MMCCounterRolloverCmd(FunctionalState NewState)
  2290. {
  2291. /* Check the parameters */
  2292. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2293. if (NewState != DISABLE)
  2294. {
  2295. /* Disable the MMC Counter Stop Rollover */
  2296. ETH->MMCCR &= ~ETH_MMCCR_CSR;
  2297. }
  2298. else
  2299. {
  2300. /* Enable the MMC Counter Stop Rollover */
  2301. ETH->MMCCR |= ETH_MMCCR_CSR;
  2302. }
  2303. }
  2304. /**
  2305. * @brief Resets the MMC Counters.
  2306. * @param None
  2307. * @retval None
  2308. */
  2309. void ETH_MMCCountersReset(void)
  2310. {
  2311. /* Resets the MMC Counters */
  2312. ETH->MMCCR |= ETH_MMCCR_CR;
  2313. }
  2314. /**
  2315. * @brief Enables or disables the specified ETHERNET MMC interrupts.
  2316. * @param ETH_MMC_IT: specifies the ETHERNET MMC interrupt sources to be enabled or disabled.
  2317. * This parameter can be any combination of Tx interrupt or
  2318. * any combination of Rx interrupt (but not both)of the following values:
  2319. * @arg ETH_MMC_IT_TGF : When Tx good frame counter reaches half the maximum value
  2320. * @arg ETH_MMC_IT_TGFMSC: When Tx good multi col counter reaches half the maximum value
  2321. * @arg ETH_MMC_IT_TGFSC : When Tx good single col counter reaches half the maximum value
  2322. * @arg ETH_MMC_IT_RGUF : When Rx good unicast frames counter reaches half the maximum value
  2323. * @arg ETH_MMC_IT_RFAE : When Rx alignment error counter reaches half the maximum value
  2324. * @arg ETH_MMC_IT_RFCE : When Rx crc error counter reaches half the maximum value
  2325. * @param NewState: new state of the specified ETHERNET MMC interrupts.
  2326. * This parameter can be: ENABLE or DISABLE.
  2327. * @retval None
  2328. */
  2329. void ETH_MMCITConfig(uint32_t ETH_MMC_IT, FunctionalState NewState)
  2330. {
  2331. /* Check the parameters */
  2332. assert_param(IS_ETH_MMC_IT(ETH_MMC_IT));
  2333. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2334. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2335. {
  2336. /* Remove Register mak from IT */
  2337. ETH_MMC_IT &= 0xEFFFFFFF;
  2338. /* ETHERNET MMC Rx interrupts selected */
  2339. if (NewState != DISABLE)
  2340. {
  2341. /* Enable the selected ETHERNET MMC interrupts */
  2342. ETH->MMCRIMR &=(~(uint32_t)ETH_MMC_IT);
  2343. }
  2344. else
  2345. {
  2346. /* Disable the selected ETHERNET MMC interrupts */
  2347. ETH->MMCRIMR |= ETH_MMC_IT;
  2348. }
  2349. }
  2350. else
  2351. {
  2352. /* ETHERNET MMC Tx interrupts selected */
  2353. if (NewState != DISABLE)
  2354. {
  2355. /* Enable the selected ETHERNET MMC interrupts */
  2356. ETH->MMCTIMR &=(~(uint32_t)ETH_MMC_IT);
  2357. }
  2358. else
  2359. {
  2360. /* Disable the selected ETHERNET MMC interrupts */
  2361. ETH->MMCTIMR |= ETH_MMC_IT;
  2362. }
  2363. }
  2364. }
  2365. /**
  2366. * @brief Checks whether the specified ETHERNET MMC IT is set or not.
  2367. * @param ETH_MMC_IT: specifies the ETHERNET MMC interrupt.
  2368. * This parameter can be one of the following values:
  2369. * @arg ETH_MMC_IT_TxFCGC: When Tx good frame counter reaches half the maximum value
  2370. * @arg ETH_MMC_IT_TxMCGC: When Tx good multi col counter reaches half the maximum value
  2371. * @arg ETH_MMC_IT_TxSCGC: When Tx good single col counter reaches half the maximum value
  2372. * @arg ETH_MMC_IT_RxUGFC: When Rx good unicast frames counter reaches half the maximum value
  2373. * @arg ETH_MMC_IT_RxAEC : When Rx alignment error counter reaches half the maximum value
  2374. * @arg ETH_MMC_IT_RxCEC : When Rx crc error counter reaches half the maximum value
  2375. * @retval The value of ETHERNET MMC IT (SET or RESET).
  2376. */
  2377. ITStatus ETH_GetMMCITStatus(uint32_t ETH_MMC_IT)
  2378. {
  2379. ITStatus bitstatus = RESET;
  2380. /* Check the parameters */
  2381. assert_param(IS_ETH_MMC_GET_IT(ETH_MMC_IT));
  2382. if ((ETH_MMC_IT & (uint32_t)0x10000000) != (uint32_t)RESET)
  2383. {
  2384. /* ETHERNET MMC Rx interrupts selected */
  2385. /* Check if the ETHERNET MMC Rx selected interrupt is enabled and occurred */
  2386. if ((((ETH->MMCRIR & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRIMR & ETH_MMC_IT) == (uint32_t)RESET))
  2387. {
  2388. bitstatus = SET;
  2389. }
  2390. else
  2391. {
  2392. bitstatus = RESET;
  2393. }
  2394. }
  2395. else
  2396. {
  2397. /* ETHERNET MMC Tx interrupts selected */
  2398. /* Check if the ETHERNET MMC Tx selected interrupt is enabled and occurred */
  2399. if ((((ETH->MMCTIR & ETH_MMC_IT) != (uint32_t)RESET)) && ((ETH->MMCRIMR & ETH_MMC_IT) == (uint32_t)RESET))
  2400. {
  2401. bitstatus = SET;
  2402. }
  2403. else
  2404. {
  2405. bitstatus = RESET;
  2406. }
  2407. }
  2408. return bitstatus;
  2409. }
  2410. /**
  2411. * @brief Get the specified ETHERNET MMC register value.
  2412. * @param ETH_MMCReg: specifies the ETHERNET MMC register.
  2413. * This parameter can be one of the following values:
  2414. * @arg ETH_MMCCR : MMC CR register
  2415. * @arg ETH_MMCRIR : MMC RIR register
  2416. * @arg ETH_MMCTIR : MMC TIR register
  2417. * @arg ETH_MMCRIMR : MMC RIMR register
  2418. * @arg ETH_MMCTIMR : MMC TIMR register
  2419. * @arg ETH_MMCTGFSCCR : MMC TGFSCCR register
  2420. * @arg ETH_MMCTGFMSCCR: MMC TGFMSCCR register
  2421. * @arg ETH_MMCTGFCR : MMC TGFCR register
  2422. * @arg ETH_MMCRFCECR : MMC RFCECR register
  2423. * @arg ETH_MMCRFAECR : MMC RFAECR register
  2424. * @arg ETH_MMCRGUFCR : MMC RGUFCRregister
  2425. * @retval The value of ETHERNET MMC Register value.
  2426. */
  2427. uint32_t ETH_GetMMCRegister(uint32_t ETH_MMCReg)
  2428. {
  2429. /* Check the parameters */
  2430. assert_param(IS_ETH_MMC_REGISTER(ETH_MMCReg));
  2431. /* Return the selected register value */
  2432. return (*(__IO uint32_t *)(ETH_MAC_BASE + ETH_MMCReg));
  2433. }
  2434. /**
  2435. * @}
  2436. */
  2437. /**
  2438. * @}
  2439. */