stm32f4x7_eth.c 98 KB

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