stm32f4xx_hal_eth.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_eth.c
  4. * @author MCD Application Team
  5. * @brief ETH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Ethernet (ETH) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Errors functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. (#)Declare a ETH_HandleTypeDef handle structure, for example:
  19. ETH_HandleTypeDef heth;
  20. (#)Fill parameters of Init structure in heth handle
  21. (#)Call HAL_ETH_Init() API to initialize the Ethernet peripheral (MAC, DMA, ...)
  22. (#)Initialize the ETH low level resources through the HAL_ETH_MspInit() API:
  23. (##) Enable the Ethernet interface clock using
  24. (+++) __HAL_RCC_ETHMAC_CLK_ENABLE();
  25. (+++) __HAL_RCC_ETHMACTX_CLK_ENABLE();
  26. (+++) __HAL_RCC_ETHMACRX_CLK_ENABLE();
  27. (##) Initialize the related GPIO clocks
  28. (##) Configure Ethernet pin-out
  29. (##) Configure Ethernet NVIC interrupt (IT mode)
  30. (#)Initialize Ethernet DMA Descriptors in chain mode and point to allocated buffers:
  31. (##) HAL_ETH_DMATxDescListInit(); for Transmission process
  32. (##) HAL_ETH_DMARxDescListInit(); for Reception process
  33. (#)Enable MAC and DMA transmission and reception:
  34. (##) HAL_ETH_Start();
  35. (#)Prepare ETH DMA TX Descriptors and give the hand to ETH DMA to transfer
  36. the frame to MAC TX FIFO:
  37. (##) HAL_ETH_TransmitFrame();
  38. (#)Poll for a received frame in ETH RX DMA Descriptors and get received
  39. frame parameters
  40. (##) HAL_ETH_GetReceivedFrame(); (should be called into an infinite loop)
  41. (#) Get a received frame when an ETH RX interrupt occurs:
  42. (##) HAL_ETH_GetReceivedFrame_IT(); (called in IT mode only)
  43. (#) Communicate with external PHY device:
  44. (##) Read a specific register from the PHY
  45. HAL_ETH_ReadPHYRegister();
  46. (##) Write data to a specific RHY register:
  47. HAL_ETH_WritePHYRegister();
  48. (#) Configure the Ethernet MAC after ETH peripheral initialization
  49. HAL_ETH_ConfigMAC(); all MAC parameters should be filled.
  50. (#) Configure the Ethernet DMA after ETH peripheral initialization
  51. HAL_ETH_ConfigDMA(); all DMA parameters should be filled.
  52. -@- The PTP protocol and the DMA descriptors ring mode are not supported
  53. in this driver
  54. *** Callback registration ***
  55. =============================================
  56. The compilation define USE_HAL_ETH_REGISTER_CALLBACKS when set to 1
  57. allows the user to configure dynamically the driver callbacks.
  58. Use Function @ref HAL_ETH_RegisterCallback() to register an interrupt callback.
  59. Function @ref HAL_ETH_RegisterCallback() allows to register following callbacks:
  60. (+) TxCpltCallback : Tx Complete Callback.
  61. (+) RxCpltCallback : Rx Complete Callback.
  62. (+) DMAErrorCallback : DMA Error Callback.
  63. (+) MspInitCallback : MspInit Callback.
  64. (+) MspDeInitCallback: MspDeInit Callback.
  65. This function takes as parameters the HAL peripheral handle, the Callback ID
  66. and a pointer to the user callback function.
  67. Use function @ref HAL_ETH_UnRegisterCallback() to reset a callback to the default
  68. weak function.
  69. @ref HAL_ETH_UnRegisterCallback takes as parameters the HAL peripheral handle,
  70. and the Callback ID.
  71. This function allows to reset following callbacks:
  72. (+) TxCpltCallback : Tx Complete Callback.
  73. (+) RxCpltCallback : Rx Complete Callback.
  74. (+) DMAErrorCallback : DMA Error Callback.
  75. (+) MspInitCallback : MspInit Callback.
  76. (+) MspDeInitCallback: MspDeInit Callback.
  77. By default, after the HAL_ETH_Init and when the state is HAL_ETH_STATE_RESET
  78. all callbacks are set to the corresponding weak functions:
  79. examples @ref HAL_ETH_TxCpltCallback(), @ref HAL_ETH_RxCpltCallback().
  80. Exception done for MspInit and MspDeInit functions that are
  81. reset to the legacy weak function in the HAL_ETH_Init/ @ref HAL_ETH_DeInit only when
  82. these callbacks are null (not registered beforehand).
  83. if not, MspInit or MspDeInit are not null, the HAL_ETH_Init/ @ref HAL_ETH_DeInit
  84. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  85. Callbacks can be registered/unregistered in HAL_ETH_STATE_READY state only.
  86. Exception done MspInit/MspDeInit that can be registered/unregistered
  87. in HAL_ETH_STATE_READY or HAL_ETH_STATE_RESET state,
  88. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  89. In that case first register the MspInit/MspDeInit user callbacks
  90. using @ref HAL_ETH_RegisterCallback() before calling @ref HAL_ETH_DeInit
  91. or HAL_ETH_Init function.
  92. When The compilation define USE_HAL_ETH_REGISTER_CALLBACKS is set to 0 or
  93. not defined, the callback registration feature is not available and all callbacks
  94. are set to the corresponding weak functions.
  95. @endverbatim
  96. ******************************************************************************
  97. * @attention
  98. *
  99. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  100. * All rights reserved.</center></h2>
  101. *
  102. * This software component is licensed by ST under BSD 3-Clause license,
  103. * the "License"; You may not use this file except in compliance with the
  104. * License. You may obtain a copy of the License at:
  105. * opensource.org/licenses/BSD-3-Clause
  106. *
  107. ******************************************************************************
  108. */
  109. /* Includes ------------------------------------------------------------------*/
  110. #include "stm32f4xx_hal.h"
  111. /** @addtogroup STM32F4xx_HAL_Driver
  112. * @{
  113. */
  114. /** @defgroup ETH ETH
  115. * @brief ETH HAL module driver
  116. * @{
  117. */
  118. #ifdef HAL_ETH_MODULE_ENABLED
  119. #if defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) ||\
  120. defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  121. /* Private typedef -----------------------------------------------------------*/
  122. /* Private define ------------------------------------------------------------*/
  123. /** @defgroup ETH_Private_Constants ETH Private Constants
  124. * @{
  125. */
  126. #define ETH_TIMEOUT_SWRESET 500U
  127. #define ETH_TIMEOUT_LINKED_STATE 5000U
  128. #define ETH_TIMEOUT_AUTONEGO_COMPLETED 5000U
  129. /**
  130. * @}
  131. */
  132. /* Private macro -------------------------------------------------------------*/
  133. /* Private variables ---------------------------------------------------------*/
  134. /* Private function prototypes -----------------------------------------------*/
  135. /** @defgroup ETH_Private_Functions ETH Private Functions
  136. * @{
  137. */
  138. static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err);
  139. static void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr);
  140. static void ETH_MACReceptionEnable(ETH_HandleTypeDef *heth);
  141. static void ETH_MACReceptionDisable(ETH_HandleTypeDef *heth);
  142. static void ETH_MACTransmissionEnable(ETH_HandleTypeDef *heth);
  143. static void ETH_MACTransmissionDisable(ETH_HandleTypeDef *heth);
  144. static void ETH_DMATransmissionEnable(ETH_HandleTypeDef *heth);
  145. static void ETH_DMATransmissionDisable(ETH_HandleTypeDef *heth);
  146. static void ETH_DMAReceptionEnable(ETH_HandleTypeDef *heth);
  147. static void ETH_DMAReceptionDisable(ETH_HandleTypeDef *heth);
  148. static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef *heth);
  149. static void ETH_Delay(uint32_t mdelay);
  150. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  151. static void ETH_InitCallbacksToDefault(ETH_HandleTypeDef *heth);
  152. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  153. /**
  154. * @}
  155. */
  156. /* Private functions ---------------------------------------------------------*/
  157. /** @defgroup ETH_Exported_Functions ETH Exported Functions
  158. * @{
  159. */
  160. /** @defgroup ETH_Exported_Functions_Group1 Initialization and de-initialization functions
  161. * @brief Initialization and Configuration functions
  162. *
  163. @verbatim
  164. ===============================================================================
  165. ##### Initialization and de-initialization functions #####
  166. ===============================================================================
  167. [..] This section provides functions allowing to:
  168. (+) Initialize and configure the Ethernet peripheral
  169. (+) De-initialize the Ethernet peripheral
  170. @endverbatim
  171. * @{
  172. */
  173. /**
  174. * @brief Initializes the Ethernet MAC and DMA according to default
  175. * parameters.
  176. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  177. * the configuration information for ETHERNET module
  178. * @retval HAL status
  179. */
  180. HAL_StatusTypeDef HAL_ETH_Init(ETH_HandleTypeDef *heth)
  181. {
  182. uint32_t tmpreg1 = 0U, phyreg = 0U;
  183. uint32_t hclk = 60000000U;
  184. uint32_t tickstart = 0U;
  185. uint32_t err = ETH_SUCCESS;
  186. /* Check the ETH peripheral state */
  187. if(heth == NULL)
  188. {
  189. return HAL_ERROR;
  190. }
  191. /* Check parameters */
  192. assert_param(IS_ETH_AUTONEGOTIATION(heth->Init.AutoNegotiation));
  193. assert_param(IS_ETH_RX_MODE(heth->Init.RxMode));
  194. assert_param(IS_ETH_CHECKSUM_MODE(heth->Init.ChecksumMode));
  195. assert_param(IS_ETH_MEDIA_INTERFACE(heth->Init.MediaInterface));
  196. if(heth->State == HAL_ETH_STATE_RESET)
  197. {
  198. /* Allocate lock resource and initialize it */
  199. heth->Lock = HAL_UNLOCKED;
  200. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  201. ETH_InitCallbacksToDefault(heth);
  202. if(heth->MspInitCallback == NULL)
  203. {
  204. /* Init the low level hardware : GPIO, CLOCK, NVIC. */
  205. heth->MspInitCallback = HAL_ETH_MspInit;
  206. }
  207. heth->MspInitCallback(heth);
  208. #else
  209. /* Init the low level hardware : GPIO, CLOCK, NVIC. */
  210. HAL_ETH_MspInit(heth);
  211. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  212. }
  213. /* Enable SYSCFG Clock */
  214. __HAL_RCC_SYSCFG_CLK_ENABLE();
  215. /* Select MII or RMII Mode*/
  216. SYSCFG->PMC &= ~(SYSCFG_PMC_MII_RMII_SEL);
  217. SYSCFG->PMC |= (uint32_t)heth->Init.MediaInterface;
  218. /* Ethernet Software reset */
  219. /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
  220. /* After reset all the registers holds their respective reset values */
  221. (heth->Instance)->DMABMR |= ETH_DMABMR_SR;
  222. /* Get tick */
  223. tickstart = HAL_GetTick();
  224. /* Wait for software reset */
  225. while (((heth->Instance)->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)
  226. {
  227. /* Check for the Timeout */
  228. if((HAL_GetTick() - tickstart ) > ETH_TIMEOUT_SWRESET)
  229. {
  230. heth->State= HAL_ETH_STATE_TIMEOUT;
  231. /* Process Unlocked */
  232. __HAL_UNLOCK(heth);
  233. /* Note: The SWR is not performed if the ETH_RX_CLK or the ETH_TX_CLK are
  234. not available, please check your external PHY or the IO configuration */
  235. return HAL_TIMEOUT;
  236. }
  237. }
  238. /*-------------------------------- MAC Initialization ----------------------*/
  239. /* Get the ETHERNET MACMIIAR value */
  240. tmpreg1 = (heth->Instance)->MACMIIAR;
  241. /* Clear CSR Clock Range CR[2:0] bits */
  242. tmpreg1 &= ETH_MACMIIAR_CR_MASK;
  243. /* Get hclk frequency value */
  244. hclk = HAL_RCC_GetHCLKFreq();
  245. /* Set CR bits depending on hclk value */
  246. if((hclk >= 20000000U)&&(hclk < 35000000U))
  247. {
  248. /* CSR Clock Range between 20-35 MHz */
  249. tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div16;
  250. }
  251. else if((hclk >= 35000000U)&&(hclk < 60000000U))
  252. {
  253. /* CSR Clock Range between 35-60 MHz */
  254. tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div26;
  255. }
  256. else if((hclk >= 60000000U)&&(hclk < 100000000U))
  257. {
  258. /* CSR Clock Range between 60-100 MHz */
  259. tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div42;
  260. }
  261. else if((hclk >= 100000000U)&&(hclk < 150000000U))
  262. {
  263. /* CSR Clock Range between 100-150 MHz */
  264. tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div62;
  265. }
  266. else /* ((hclk >= 150000000)&&(hclk <= 183000000)) */
  267. {
  268. /* CSR Clock Range between 150-183 MHz */
  269. tmpreg1 |= (uint32_t)ETH_MACMIIAR_CR_Div102;
  270. }
  271. /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
  272. (heth->Instance)->MACMIIAR = (uint32_t)tmpreg1;
  273. /*-------------------- PHY initialization and configuration ----------------*/
  274. /* Put the PHY in reset mode */
  275. if((HAL_ETH_WritePHYRegister(heth, PHY_BCR, PHY_RESET)) != HAL_OK)
  276. {
  277. /* In case of write timeout */
  278. err = ETH_ERROR;
  279. /* Config MAC and DMA */
  280. ETH_MACDMAConfig(heth, err);
  281. /* Set the ETH peripheral state to READY */
  282. heth->State = HAL_ETH_STATE_READY;
  283. /* Return HAL_ERROR */
  284. return HAL_ERROR;
  285. }
  286. /* Delay to assure PHY reset */
  287. HAL_Delay(PHY_RESET_DELAY);
  288. if((heth->Init).AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE)
  289. {
  290. /* Get tick */
  291. tickstart = HAL_GetTick();
  292. /* We wait for linked status */
  293. do
  294. {
  295. HAL_ETH_ReadPHYRegister(heth, PHY_BSR, &phyreg);
  296. /* Check for the Timeout */
  297. if((HAL_GetTick() - tickstart ) > ETH_TIMEOUT_LINKED_STATE)
  298. {
  299. /* In case of write timeout */
  300. err = ETH_ERROR;
  301. /* Config MAC and DMA */
  302. ETH_MACDMAConfig(heth, err);
  303. heth->State= HAL_ETH_STATE_READY;
  304. /* Process Unlocked */
  305. __HAL_UNLOCK(heth);
  306. return HAL_TIMEOUT;
  307. }
  308. } while (((phyreg & PHY_LINKED_STATUS) != PHY_LINKED_STATUS));
  309. /* Enable Auto-Negotiation */
  310. if((HAL_ETH_WritePHYRegister(heth, PHY_BCR, PHY_AUTONEGOTIATION)) != HAL_OK)
  311. {
  312. /* In case of write timeout */
  313. err = ETH_ERROR;
  314. /* Config MAC and DMA */
  315. ETH_MACDMAConfig(heth, err);
  316. /* Set the ETH peripheral state to READY */
  317. heth->State = HAL_ETH_STATE_READY;
  318. /* Return HAL_ERROR */
  319. return HAL_ERROR;
  320. }
  321. /* Get tick */
  322. tickstart = HAL_GetTick();
  323. /* Wait until the auto-negotiation will be completed */
  324. do
  325. {
  326. HAL_ETH_ReadPHYRegister(heth, PHY_BSR, &phyreg);
  327. /* Check for the Timeout */
  328. if((HAL_GetTick() - tickstart ) > ETH_TIMEOUT_AUTONEGO_COMPLETED)
  329. {
  330. /* In case of write timeout */
  331. err = ETH_ERROR;
  332. /* Config MAC and DMA */
  333. ETH_MACDMAConfig(heth, err);
  334. heth->State= HAL_ETH_STATE_READY;
  335. /* Process Unlocked */
  336. __HAL_UNLOCK(heth);
  337. return HAL_TIMEOUT;
  338. }
  339. } while (((phyreg & PHY_AUTONEGO_COMPLETE) != PHY_AUTONEGO_COMPLETE));
  340. /* Read the result of the auto-negotiation */
  341. if((HAL_ETH_ReadPHYRegister(heth, PHY_SR, &phyreg)) != HAL_OK)
  342. {
  343. /* In case of write timeout */
  344. err = ETH_ERROR;
  345. /* Config MAC and DMA */
  346. ETH_MACDMAConfig(heth, err);
  347. /* Set the ETH peripheral state to READY */
  348. heth->State = HAL_ETH_STATE_READY;
  349. /* Return HAL_ERROR */
  350. return HAL_ERROR;
  351. }
  352. /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
  353. if((phyreg & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
  354. {
  355. /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
  356. (heth->Init).DuplexMode = ETH_MODE_FULLDUPLEX;
  357. }
  358. else
  359. {
  360. /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
  361. (heth->Init).DuplexMode = ETH_MODE_HALFDUPLEX;
  362. }
  363. /* Configure the MAC with the speed fixed by the auto-negotiation process */
  364. if((phyreg & PHY_SPEED_STATUS) == PHY_SPEED_STATUS)
  365. {
  366. /* Set Ethernet speed to 10M following the auto-negotiation */
  367. (heth->Init).Speed = ETH_SPEED_10M;
  368. }
  369. else
  370. {
  371. /* Set Ethernet speed to 100M following the auto-negotiation */
  372. (heth->Init).Speed = ETH_SPEED_100M;
  373. }
  374. }
  375. else /* AutoNegotiation Disable */
  376. {
  377. /* Check parameters */
  378. assert_param(IS_ETH_SPEED(heth->Init.Speed));
  379. assert_param(IS_ETH_DUPLEX_MODE(heth->Init.DuplexMode));
  380. /* Set MAC Speed and Duplex Mode */
  381. if(HAL_ETH_WritePHYRegister(heth, PHY_BCR, ((uint16_t)((heth->Init).DuplexMode >> 3U) |
  382. (uint16_t)((heth->Init).Speed >> 1U))) != HAL_OK)
  383. {
  384. /* In case of write timeout */
  385. err = ETH_ERROR;
  386. /* Config MAC and DMA */
  387. ETH_MACDMAConfig(heth, err);
  388. /* Set the ETH peripheral state to READY */
  389. heth->State = HAL_ETH_STATE_READY;
  390. /* Return HAL_ERROR */
  391. return HAL_ERROR;
  392. }
  393. /* Delay to assure PHY configuration */
  394. HAL_Delay(PHY_CONFIG_DELAY);
  395. }
  396. /* Config MAC and DMA */
  397. ETH_MACDMAConfig(heth, err);
  398. /* Set ETH HAL State to Ready */
  399. heth->State= HAL_ETH_STATE_READY;
  400. /* Return function status */
  401. return HAL_OK;
  402. }
  403. /**
  404. * @brief De-Initializes the ETH peripheral.
  405. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  406. * the configuration information for ETHERNET module
  407. * @retval HAL status
  408. */
  409. HAL_StatusTypeDef HAL_ETH_DeInit(ETH_HandleTypeDef *heth)
  410. {
  411. /* Set the ETH peripheral state to BUSY */
  412. heth->State = HAL_ETH_STATE_BUSY;
  413. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  414. if(heth->MspDeInitCallback == NULL)
  415. {
  416. heth->MspDeInitCallback = HAL_ETH_MspDeInit;
  417. }
  418. /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */
  419. heth->MspDeInitCallback(heth);
  420. #else
  421. /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */
  422. HAL_ETH_MspDeInit(heth);
  423. #endif
  424. /* Set ETH HAL state to Disabled */
  425. heth->State= HAL_ETH_STATE_RESET;
  426. /* Release Lock */
  427. __HAL_UNLOCK(heth);
  428. /* Return function status */
  429. return HAL_OK;
  430. }
  431. /**
  432. * @brief Initializes the DMA Tx descriptors in chain mode.
  433. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  434. * the configuration information for ETHERNET module
  435. * @param DMATxDescTab Pointer to the first Tx desc list
  436. * @param TxBuff Pointer to the first TxBuffer list
  437. * @param TxBuffCount Number of the used Tx desc in the list
  438. * @retval HAL status
  439. */
  440. HAL_StatusTypeDef HAL_ETH_DMATxDescListInit(ETH_HandleTypeDef *heth, ETH_DMADescTypeDef *DMATxDescTab, uint8_t *TxBuff, uint32_t TxBuffCount)
  441. {
  442. uint32_t i = 0U;
  443. ETH_DMADescTypeDef *dmatxdesc;
  444. /* Process Locked */
  445. __HAL_LOCK(heth);
  446. /* Set the ETH peripheral state to BUSY */
  447. heth->State = HAL_ETH_STATE_BUSY;
  448. /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
  449. heth->TxDesc = DMATxDescTab;
  450. /* Fill each DMATxDesc descriptor with the right values */
  451. for(i=0U; i < TxBuffCount; i++)
  452. {
  453. /* Get the pointer on the ith member of the Tx Desc list */
  454. dmatxdesc = DMATxDescTab + i;
  455. /* Set Second Address Chained bit */
  456. dmatxdesc->Status = ETH_DMATXDESC_TCH;
  457. /* Set Buffer1 address pointer */
  458. dmatxdesc->Buffer1Addr = (uint32_t)(&TxBuff[i*ETH_TX_BUF_SIZE]);
  459. if ((heth->Init).ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)
  460. {
  461. /* Set the DMA Tx descriptors checksum insertion */
  462. dmatxdesc->Status |= ETH_DMATXDESC_CHECKSUMTCPUDPICMPFULL;
  463. }
  464. /* Initialize the next descriptor with the Next Descriptor Polling Enable */
  465. if(i < (TxBuffCount-1U))
  466. {
  467. /* Set next descriptor address register with next descriptor base address */
  468. dmatxdesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1U);
  469. }
  470. else
  471. {
  472. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  473. dmatxdesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  474. }
  475. }
  476. /* Set Transmit Descriptor List Address Register */
  477. (heth->Instance)->DMATDLAR = (uint32_t) DMATxDescTab;
  478. /* Set ETH HAL State to Ready */
  479. heth->State= HAL_ETH_STATE_READY;
  480. /* Process Unlocked */
  481. __HAL_UNLOCK(heth);
  482. /* Return function status */
  483. return HAL_OK;
  484. }
  485. /**
  486. * @brief Initializes the DMA Rx descriptors in chain mode.
  487. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  488. * the configuration information for ETHERNET module
  489. * @param DMARxDescTab Pointer to the first Rx desc list
  490. * @param RxBuff Pointer to the first RxBuffer list
  491. * @param RxBuffCount Number of the used Rx desc in the list
  492. * @retval HAL status
  493. */
  494. HAL_StatusTypeDef HAL_ETH_DMARxDescListInit(ETH_HandleTypeDef *heth, ETH_DMADescTypeDef *DMARxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
  495. {
  496. uint32_t i = 0U;
  497. ETH_DMADescTypeDef *DMARxDesc;
  498. /* Process Locked */
  499. __HAL_LOCK(heth);
  500. /* Set the ETH peripheral state to BUSY */
  501. heth->State = HAL_ETH_STATE_BUSY;
  502. /* Set the Ethernet RxDesc pointer with the first one of the DMARxDescTab list */
  503. heth->RxDesc = DMARxDescTab;
  504. /* Fill each DMARxDesc descriptor with the right values */
  505. for(i=0U; i < RxBuffCount; i++)
  506. {
  507. /* Get the pointer on the ith member of the Rx Desc list */
  508. DMARxDesc = DMARxDescTab+i;
  509. /* Set Own bit of the Rx descriptor Status */
  510. DMARxDesc->Status = ETH_DMARXDESC_OWN;
  511. /* Set Buffer1 size and Second Address Chained bit */
  512. DMARxDesc->ControlBufferSize = ETH_DMARXDESC_RCH | ETH_RX_BUF_SIZE;
  513. /* Set Buffer1 address pointer */
  514. DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*ETH_RX_BUF_SIZE]);
  515. if((heth->Init).RxMode == ETH_RXINTERRUPT_MODE)
  516. {
  517. /* Enable Ethernet DMA Rx Descriptor interrupt */
  518. DMARxDesc->ControlBufferSize &= ~ETH_DMARXDESC_DIC;
  519. }
  520. /* Initialize the next descriptor with the Next Descriptor Polling Enable */
  521. if(i < (RxBuffCount-1U))
  522. {
  523. /* Set next descriptor address register with next descriptor base address */
  524. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab+i+1U);
  525. }
  526. else
  527. {
  528. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  529. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
  530. }
  531. }
  532. /* Set Receive Descriptor List Address Register */
  533. (heth->Instance)->DMARDLAR = (uint32_t) DMARxDescTab;
  534. /* Set ETH HAL State to Ready */
  535. heth->State= HAL_ETH_STATE_READY;
  536. /* Process Unlocked */
  537. __HAL_UNLOCK(heth);
  538. /* Return function status */
  539. return HAL_OK;
  540. }
  541. /**
  542. * @brief Initializes the ETH MSP.
  543. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  544. * the configuration information for ETHERNET module
  545. * @retval None
  546. */
  547. __weak void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)
  548. {
  549. /* Prevent unused argument(s) compilation warning */
  550. UNUSED(heth);
  551. /* NOTE : This function Should not be modified, when the callback is needed,
  552. the HAL_ETH_MspInit could be implemented in the user file
  553. */
  554. }
  555. /**
  556. * @brief DeInitializes ETH MSP.
  557. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  558. * the configuration information for ETHERNET module
  559. * @retval None
  560. */
  561. __weak void HAL_ETH_MspDeInit(ETH_HandleTypeDef *heth)
  562. {
  563. /* Prevent unused argument(s) compilation warning */
  564. UNUSED(heth);
  565. /* NOTE : This function Should not be modified, when the callback is needed,
  566. the HAL_ETH_MspDeInit could be implemented in the user file
  567. */
  568. }
  569. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  570. /**
  571. * @brief Register a User ETH Callback
  572. * To be used instead of the weak predefined callback
  573. * @param heth eth handle
  574. * @param CallbackID ID of the callback to be registered
  575. * This parameter can be one of the following values:
  576. * @arg @ref HAL_ETH_TX_COMPLETE_CB_ID Tx Complete Callback ID
  577. * @arg @ref HAL_ETH_RX_COMPLETE_CB_ID Rx Complete Callback ID
  578. * @arg @ref HAL_ETH_DMA_ERROR_CB_ID DMA Error Callback ID
  579. * @arg @ref HAL_ETH_MSPINIT_CB_ID MspInit callback ID
  580. * @arg @ref HAL_ETH_MSPDEINIT_CB_ID MspDeInit callback ID
  581. * @param pCallback pointer to the Callback function
  582. * @retval status
  583. */
  584. HAL_StatusTypeDef HAL_ETH_RegisterCallback(ETH_HandleTypeDef *heth, HAL_ETH_CallbackIDTypeDef CallbackID, pETH_CallbackTypeDef pCallback)
  585. {
  586. HAL_StatusTypeDef status = HAL_OK;
  587. if(pCallback == NULL)
  588. {
  589. return HAL_ERROR;
  590. }
  591. /* Process locked */
  592. __HAL_LOCK(heth);
  593. if(heth->State == HAL_ETH_STATE_READY)
  594. {
  595. switch (CallbackID)
  596. {
  597. case HAL_ETH_TX_COMPLETE_CB_ID :
  598. heth->TxCpltCallback = pCallback;
  599. break;
  600. case HAL_ETH_RX_COMPLETE_CB_ID :
  601. heth->RxCpltCallback = pCallback;
  602. break;
  603. case HAL_ETH_DMA_ERROR_CB_ID :
  604. heth->DMAErrorCallback = pCallback;
  605. break;
  606. case HAL_ETH_MSPINIT_CB_ID :
  607. heth->MspInitCallback = pCallback;
  608. break;
  609. case HAL_ETH_MSPDEINIT_CB_ID :
  610. heth->MspDeInitCallback = pCallback;
  611. break;
  612. default :
  613. /* Return error status */
  614. status = HAL_ERROR;
  615. break;
  616. }
  617. }
  618. else if(heth->State == HAL_ETH_STATE_RESET)
  619. {
  620. switch (CallbackID)
  621. {
  622. case HAL_ETH_MSPINIT_CB_ID :
  623. heth->MspInitCallback = pCallback;
  624. break;
  625. case HAL_ETH_MSPDEINIT_CB_ID :
  626. heth->MspDeInitCallback = pCallback;
  627. break;
  628. default :
  629. /* Return error status */
  630. status = HAL_ERROR;
  631. break;
  632. }
  633. }
  634. else
  635. {
  636. /* Return error status */
  637. status = HAL_ERROR;
  638. }
  639. /* Release Lock */
  640. __HAL_UNLOCK(heth);
  641. return status;
  642. }
  643. /**
  644. * @brief Unregister an ETH Callback
  645. * ETH callabck is redirected to the weak predefined callback
  646. * @param heth eth handle
  647. * @param CallbackID ID of the callback to be unregistered
  648. * This parameter can be one of the following values:
  649. * @arg @ref HAL_ETH_TX_COMPLETE_CB_ID Tx Complete Callback ID
  650. * @arg @ref HAL_ETH_RX_COMPLETE_CB_ID Rx Complete Callback ID
  651. * @arg @ref HAL_ETH_DMA_ERROR_CB_ID DMA Error Callback ID
  652. * @arg @ref HAL_ETH_MSPINIT_CB_ID MspInit callback ID
  653. * @arg @ref HAL_ETH_MSPDEINIT_CB_ID MspDeInit callback ID
  654. * @retval status
  655. */
  656. HAL_StatusTypeDef HAL_ETH_UnRegisterCallback(ETH_HandleTypeDef *heth, HAL_ETH_CallbackIDTypeDef CallbackID)
  657. {
  658. HAL_StatusTypeDef status = HAL_OK;
  659. /* Process locked */
  660. __HAL_LOCK(heth);
  661. if(heth->State == HAL_ETH_STATE_READY)
  662. {
  663. switch (CallbackID)
  664. {
  665. case HAL_ETH_TX_COMPLETE_CB_ID :
  666. heth->TxCpltCallback = HAL_ETH_TxCpltCallback;
  667. break;
  668. case HAL_ETH_RX_COMPLETE_CB_ID :
  669. heth->RxCpltCallback = HAL_ETH_RxCpltCallback;
  670. break;
  671. case HAL_ETH_DMA_ERROR_CB_ID :
  672. heth->DMAErrorCallback = HAL_ETH_ErrorCallback;
  673. break;
  674. case HAL_ETH_MSPINIT_CB_ID :
  675. heth->MspInitCallback = HAL_ETH_MspInit;
  676. break;
  677. case HAL_ETH_MSPDEINIT_CB_ID :
  678. heth->MspDeInitCallback = HAL_ETH_MspDeInit;
  679. break;
  680. default :
  681. /* Return error status */
  682. status = HAL_ERROR;
  683. break;
  684. }
  685. }
  686. else if(heth->State == HAL_ETH_STATE_RESET)
  687. {
  688. switch (CallbackID)
  689. {
  690. case HAL_ETH_MSPINIT_CB_ID :
  691. heth->MspInitCallback = HAL_ETH_MspInit;
  692. break;
  693. case HAL_ETH_MSPDEINIT_CB_ID :
  694. heth->MspDeInitCallback = HAL_ETH_MspDeInit;
  695. break;
  696. default :
  697. /* Return error status */
  698. status = HAL_ERROR;
  699. break;
  700. }
  701. }
  702. else
  703. {
  704. /* Return error status */
  705. status = HAL_ERROR;
  706. }
  707. /* Release Lock */
  708. __HAL_UNLOCK(heth);
  709. return status;
  710. }
  711. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  712. /**
  713. * @}
  714. */
  715. /** @defgroup ETH_Exported_Functions_Group2 IO operation functions
  716. * @brief Data transfers functions
  717. *
  718. @verbatim
  719. ==============================================================================
  720. ##### IO operation functions #####
  721. ==============================================================================
  722. [..] This section provides functions allowing to:
  723. (+) Transmit a frame
  724. HAL_ETH_TransmitFrame();
  725. (+) Receive a frame
  726. HAL_ETH_GetReceivedFrame();
  727. HAL_ETH_GetReceivedFrame_IT();
  728. (+) Read from an External PHY register
  729. HAL_ETH_ReadPHYRegister();
  730. (+) Write to an External PHY register
  731. HAL_ETH_WritePHYRegister();
  732. @endverbatim
  733. * @{
  734. */
  735. /**
  736. * @brief Sends an Ethernet frame.
  737. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  738. * the configuration information for ETHERNET module
  739. * @param FrameLength Amount of data to be sent
  740. * @retval HAL status
  741. */
  742. HAL_StatusTypeDef HAL_ETH_TransmitFrame(ETH_HandleTypeDef *heth, uint32_t FrameLength)
  743. {
  744. uint32_t bufcount = 0U, size = 0U, i = 0U;
  745. /* Process Locked */
  746. __HAL_LOCK(heth);
  747. /* Set the ETH peripheral state to BUSY */
  748. heth->State = HAL_ETH_STATE_BUSY;
  749. if (FrameLength == 0U)
  750. {
  751. /* Set ETH HAL state to READY */
  752. heth->State = HAL_ETH_STATE_READY;
  753. /* Process Unlocked */
  754. __HAL_UNLOCK(heth);
  755. return HAL_ERROR;
  756. }
  757. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  758. if(((heth->TxDesc)->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
  759. {
  760. /* OWN bit set */
  761. heth->State = HAL_ETH_STATE_BUSY_TX;
  762. /* Process Unlocked */
  763. __HAL_UNLOCK(heth);
  764. return HAL_ERROR;
  765. }
  766. /* Get the number of needed Tx buffers for the current frame */
  767. if (FrameLength > ETH_TX_BUF_SIZE)
  768. {
  769. bufcount = FrameLength/ETH_TX_BUF_SIZE;
  770. if (FrameLength % ETH_TX_BUF_SIZE)
  771. {
  772. bufcount++;
  773. }
  774. }
  775. else
  776. {
  777. bufcount = 1U;
  778. }
  779. if (bufcount == 1U)
  780. {
  781. /* Set LAST and FIRST segment */
  782. heth->TxDesc->Status |=ETH_DMATXDESC_FS|ETH_DMATXDESC_LS;
  783. /* Set frame size */
  784. heth->TxDesc->ControlBufferSize = (FrameLength & ETH_DMATXDESC_TBS1);
  785. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  786. heth->TxDesc->Status |= ETH_DMATXDESC_OWN;
  787. /* Point to next descriptor */
  788. heth->TxDesc= (ETH_DMADescTypeDef *)(heth->TxDesc->Buffer2NextDescAddr);
  789. }
  790. else
  791. {
  792. for (i=0U; i< bufcount; i++)
  793. {
  794. /* Clear FIRST and LAST segment bits */
  795. heth->TxDesc->Status &= ~(ETH_DMATXDESC_FS | ETH_DMATXDESC_LS);
  796. if (i == 0U)
  797. {
  798. /* Setting the first segment bit */
  799. heth->TxDesc->Status |= ETH_DMATXDESC_FS;
  800. }
  801. /* Program size */
  802. heth->TxDesc->ControlBufferSize = (ETH_TX_BUF_SIZE & ETH_DMATXDESC_TBS1);
  803. if (i == (bufcount-1U))
  804. {
  805. /* Setting the last segment bit */
  806. heth->TxDesc->Status |= ETH_DMATXDESC_LS;
  807. size = FrameLength - (bufcount-1U)*ETH_TX_BUF_SIZE;
  808. heth->TxDesc->ControlBufferSize = (size & ETH_DMATXDESC_TBS1);
  809. }
  810. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  811. heth->TxDesc->Status |= ETH_DMATXDESC_OWN;
  812. /* point to next descriptor */
  813. heth->TxDesc = (ETH_DMADescTypeDef *)(heth->TxDesc->Buffer2NextDescAddr);
  814. }
  815. }
  816. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  817. if (((heth->Instance)->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
  818. {
  819. /* Clear TBUS ETHERNET DMA flag */
  820. (heth->Instance)->DMASR = ETH_DMASR_TBUS;
  821. /* Resume DMA transmission*/
  822. (heth->Instance)->DMATPDR = 0U;
  823. }
  824. /* Set ETH HAL State to Ready */
  825. heth->State = HAL_ETH_STATE_READY;
  826. /* Process Unlocked */
  827. __HAL_UNLOCK(heth);
  828. /* Return function status */
  829. return HAL_OK;
  830. }
  831. /**
  832. * @brief Checks for received frames.
  833. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  834. * the configuration information for ETHERNET module
  835. * @retval HAL status
  836. */
  837. HAL_StatusTypeDef HAL_ETH_GetReceivedFrame(ETH_HandleTypeDef *heth)
  838. {
  839. uint32_t framelength = 0U;
  840. /* Process Locked */
  841. __HAL_LOCK(heth);
  842. /* Check the ETH state to BUSY */
  843. heth->State = HAL_ETH_STATE_BUSY;
  844. /* Check if segment is not owned by DMA */
  845. /* (((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET) && ((heth->RxDesc->Status & ETH_DMARXDESC_LS) != (uint32_t)RESET)) */
  846. if(((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET))
  847. {
  848. /* Check if last segment */
  849. if(((heth->RxDesc->Status & ETH_DMARXDESC_LS) != (uint32_t)RESET))
  850. {
  851. /* increment segment count */
  852. (heth->RxFrameInfos).SegCount++;
  853. /* Check if last segment is first segment: one segment contains the frame */
  854. if ((heth->RxFrameInfos).SegCount == 1U)
  855. {
  856. (heth->RxFrameInfos).FSRxDesc =heth->RxDesc;
  857. }
  858. heth->RxFrameInfos.LSRxDesc = heth->RxDesc;
  859. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  860. framelength = (((heth->RxDesc)->Status & ETH_DMARXDESC_FL) >> ETH_DMARXDESC_FRAMELENGTHSHIFT) - 4U;
  861. heth->RxFrameInfos.length = framelength;
  862. /* Get the address of the buffer start address */
  863. heth->RxFrameInfos.buffer = ((heth->RxFrameInfos).FSRxDesc)->Buffer1Addr;
  864. /* point to next descriptor */
  865. heth->RxDesc = (ETH_DMADescTypeDef*) ((heth->RxDesc)->Buffer2NextDescAddr);
  866. /* Set HAL State to Ready */
  867. heth->State = HAL_ETH_STATE_READY;
  868. /* Process Unlocked */
  869. __HAL_UNLOCK(heth);
  870. /* Return function status */
  871. return HAL_OK;
  872. }
  873. /* Check if first segment */
  874. else if((heth->RxDesc->Status & ETH_DMARXDESC_FS) != (uint32_t)RESET)
  875. {
  876. (heth->RxFrameInfos).FSRxDesc = heth->RxDesc;
  877. (heth->RxFrameInfos).LSRxDesc = NULL;
  878. (heth->RxFrameInfos).SegCount = 1U;
  879. /* Point to next descriptor */
  880. heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
  881. }
  882. /* Check if intermediate segment */
  883. else
  884. {
  885. (heth->RxFrameInfos).SegCount++;
  886. /* Point to next descriptor */
  887. heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
  888. }
  889. }
  890. /* Set ETH HAL State to Ready */
  891. heth->State = HAL_ETH_STATE_READY;
  892. /* Process Unlocked */
  893. __HAL_UNLOCK(heth);
  894. /* Return function status */
  895. return HAL_ERROR;
  896. }
  897. /**
  898. * @brief Gets the Received frame in interrupt mode.
  899. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  900. * the configuration information for ETHERNET module
  901. * @retval HAL status
  902. */
  903. HAL_StatusTypeDef HAL_ETH_GetReceivedFrame_IT(ETH_HandleTypeDef *heth)
  904. {
  905. uint32_t descriptorscancounter = 0U;
  906. /* Process Locked */
  907. __HAL_LOCK(heth);
  908. /* Set ETH HAL State to BUSY */
  909. heth->State = HAL_ETH_STATE_BUSY;
  910. /* Scan descriptors owned by CPU */
  911. while (((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET) && (descriptorscancounter < ETH_RXBUFNB))
  912. {
  913. /* Just for security */
  914. descriptorscancounter++;
  915. /* Check if first segment in frame */
  916. /* ((heth->RxDesc->Status & ETH_DMARXDESC_FS) != (uint32_t)RESET) && ((heth->RxDesc->Status & ETH_DMARXDESC_LS) == (uint32_t)RESET)) */
  917. if((heth->RxDesc->Status & (ETH_DMARXDESC_FS | ETH_DMARXDESC_LS)) == (uint32_t)ETH_DMARXDESC_FS)
  918. {
  919. heth->RxFrameInfos.FSRxDesc = heth->RxDesc;
  920. heth->RxFrameInfos.SegCount = 1U;
  921. /* Point to next descriptor */
  922. heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
  923. }
  924. /* Check if intermediate segment */
  925. /* ((heth->RxDesc->Status & ETH_DMARXDESC_LS) == (uint32_t)RESET)&& ((heth->RxDesc->Status & ETH_DMARXDESC_FS) == (uint32_t)RESET)) */
  926. else if ((heth->RxDesc->Status & (ETH_DMARXDESC_LS | ETH_DMARXDESC_FS)) == (uint32_t)RESET)
  927. {
  928. /* Increment segment count */
  929. (heth->RxFrameInfos.SegCount)++;
  930. /* Point to next descriptor */
  931. heth->RxDesc = (ETH_DMADescTypeDef*)(heth->RxDesc->Buffer2NextDescAddr);
  932. }
  933. /* Should be last segment */
  934. else
  935. {
  936. /* Last segment */
  937. heth->RxFrameInfos.LSRxDesc = heth->RxDesc;
  938. /* Increment segment count */
  939. (heth->RxFrameInfos.SegCount)++;
  940. /* Check if last segment is first segment: one segment contains the frame */
  941. if ((heth->RxFrameInfos.SegCount) == 1U)
  942. {
  943. heth->RxFrameInfos.FSRxDesc = heth->RxDesc;
  944. }
  945. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  946. heth->RxFrameInfos.length = (((heth->RxDesc)->Status & ETH_DMARXDESC_FL) >> ETH_DMARXDESC_FRAMELENGTHSHIFT) - 4U;
  947. /* Get the address of the buffer start address */
  948. heth->RxFrameInfos.buffer =((heth->RxFrameInfos).FSRxDesc)->Buffer1Addr;
  949. /* Point to next descriptor */
  950. heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
  951. /* Set HAL State to Ready */
  952. heth->State = HAL_ETH_STATE_READY;
  953. /* Process Unlocked */
  954. __HAL_UNLOCK(heth);
  955. /* Return function status */
  956. return HAL_OK;
  957. }
  958. }
  959. /* Set HAL State to Ready */
  960. heth->State = HAL_ETH_STATE_READY;
  961. /* Process Unlocked */
  962. __HAL_UNLOCK(heth);
  963. /* Return function status */
  964. return HAL_ERROR;
  965. }
  966. /**
  967. * @brief This function handles ETH interrupt request.
  968. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  969. * the configuration information for ETHERNET module
  970. * @retval HAL status
  971. */
  972. void HAL_ETH_IRQHandler(ETH_HandleTypeDef *heth)
  973. {
  974. /* Frame received */
  975. if (__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_R))
  976. {
  977. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  978. /*Call registered Receive complete callback*/
  979. heth->RxCpltCallback(heth);
  980. #else
  981. /* Receive complete callback */
  982. HAL_ETH_RxCpltCallback(heth);
  983. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  984. /* Clear the Eth DMA Rx IT pending bits */
  985. __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_R);
  986. /* Set HAL State to Ready */
  987. heth->State = HAL_ETH_STATE_READY;
  988. /* Process Unlocked */
  989. __HAL_UNLOCK(heth);
  990. }
  991. /* Frame transmitted */
  992. else if (__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_T))
  993. {
  994. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  995. /* Call resgistered Transfer complete callback*/
  996. heth->TxCpltCallback(heth);
  997. #else
  998. /* Transfer complete callback */
  999. HAL_ETH_TxCpltCallback(heth);
  1000. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1001. /* Clear the Eth DMA Tx IT pending bits */
  1002. __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_T);
  1003. /* Set HAL State to Ready */
  1004. heth->State = HAL_ETH_STATE_READY;
  1005. /* Process Unlocked */
  1006. __HAL_UNLOCK(heth);
  1007. }
  1008. /* Clear the interrupt flags */
  1009. __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_NIS);
  1010. /* ETH DMA Error */
  1011. if(__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_AIS))
  1012. {
  1013. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1014. heth->DMAErrorCallback(heth);
  1015. #else
  1016. /* Ethernet Error callback */
  1017. HAL_ETH_ErrorCallback(heth);
  1018. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1019. /* Clear the interrupt flags */
  1020. __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_FLAG_AIS);
  1021. /* Set HAL State to Ready */
  1022. heth->State = HAL_ETH_STATE_READY;
  1023. /* Process Unlocked */
  1024. __HAL_UNLOCK(heth);
  1025. }
  1026. }
  1027. /**
  1028. * @brief Tx Transfer completed callbacks.
  1029. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1030. * the configuration information for ETHERNET module
  1031. * @retval None
  1032. */
  1033. __weak void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth)
  1034. {
  1035. /* Prevent unused argument(s) compilation warning */
  1036. UNUSED(heth);
  1037. /* NOTE : This function Should not be modified, when the callback is needed,
  1038. the HAL_ETH_TxCpltCallback could be implemented in the user file
  1039. */
  1040. }
  1041. /**
  1042. * @brief Rx Transfer completed callbacks.
  1043. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1044. * the configuration information for ETHERNET module
  1045. * @retval None
  1046. */
  1047. __weak void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
  1048. {
  1049. /* Prevent unused argument(s) compilation warning */
  1050. UNUSED(heth);
  1051. /* NOTE : This function Should not be modified, when the callback is needed,
  1052. the HAL_ETH_TxCpltCallback could be implemented in the user file
  1053. */
  1054. }
  1055. /**
  1056. * @brief Ethernet transfer error callbacks
  1057. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1058. * the configuration information for ETHERNET module
  1059. * @retval None
  1060. */
  1061. __weak void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
  1062. {
  1063. /* Prevent unused argument(s) compilation warning */
  1064. UNUSED(heth);
  1065. /* NOTE : This function Should not be modified, when the callback is needed,
  1066. the HAL_ETH_TxCpltCallback could be implemented in the user file
  1067. */
  1068. }
  1069. /**
  1070. * @brief Reads a PHY register
  1071. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1072. * the configuration information for ETHERNET module
  1073. * @param PHYReg PHY register address, is the index of one of the 32 PHY register.
  1074. * This parameter can be one of the following values:
  1075. * PHY_BCR: Transceiver Basic Control Register,
  1076. * PHY_BSR: Transceiver Basic Status Register.
  1077. * More PHY register could be read depending on the used PHY
  1078. * @param RegValue PHY register value
  1079. * @retval HAL status
  1080. */
  1081. HAL_StatusTypeDef HAL_ETH_ReadPHYRegister(ETH_HandleTypeDef *heth, uint16_t PHYReg, uint32_t *RegValue)
  1082. {
  1083. uint32_t tmpreg1 = 0U;
  1084. uint32_t tickstart = 0U;
  1085. /* Check parameters */
  1086. assert_param(IS_ETH_PHY_ADDRESS(heth->Init.PhyAddress));
  1087. /* Check the ETH peripheral state */
  1088. if(heth->State == HAL_ETH_STATE_BUSY_RD)
  1089. {
  1090. return HAL_BUSY;
  1091. }
  1092. /* Set ETH HAL State to BUSY_RD */
  1093. heth->State = HAL_ETH_STATE_BUSY_RD;
  1094. /* Get the ETHERNET MACMIIAR value */
  1095. tmpreg1 = heth->Instance->MACMIIAR;
  1096. /* Keep only the CSR Clock Range CR[2:0] bits value */
  1097. tmpreg1 &= ~ETH_MACMIIAR_CR_MASK;
  1098. /* Prepare the MII address register value */
  1099. tmpreg1 |=(((uint32_t)heth->Init.PhyAddress << 11U) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  1100. tmpreg1 |=(((uint32_t)PHYReg<<6U) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  1101. tmpreg1 &= ~ETH_MACMIIAR_MW; /* Set the read mode */
  1102. tmpreg1 |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  1103. /* Write the result value into the MII Address register */
  1104. heth->Instance->MACMIIAR = tmpreg1;
  1105. /* Get tick */
  1106. tickstart = HAL_GetTick();
  1107. /* Check for the Busy flag */
  1108. while((tmpreg1 & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB)
  1109. {
  1110. /* Check for the Timeout */
  1111. if((HAL_GetTick() - tickstart ) > PHY_READ_TO)
  1112. {
  1113. heth->State= HAL_ETH_STATE_READY;
  1114. /* Process Unlocked */
  1115. __HAL_UNLOCK(heth);
  1116. return HAL_TIMEOUT;
  1117. }
  1118. tmpreg1 = heth->Instance->MACMIIAR;
  1119. }
  1120. /* Get MACMIIDR value */
  1121. *RegValue = (uint16_t)(heth->Instance->MACMIIDR);
  1122. /* Set ETH HAL State to READY */
  1123. heth->State = HAL_ETH_STATE_READY;
  1124. /* Return function status */
  1125. return HAL_OK;
  1126. }
  1127. /**
  1128. * @brief Writes to a PHY register.
  1129. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1130. * the configuration information for ETHERNET module
  1131. * @param PHYReg PHY register address, is the index of one of the 32 PHY register.
  1132. * This parameter can be one of the following values:
  1133. * PHY_BCR: Transceiver Control Register.
  1134. * More PHY register could be written depending on the used PHY
  1135. * @param RegValue the value to write
  1136. * @retval HAL status
  1137. */
  1138. HAL_StatusTypeDef HAL_ETH_WritePHYRegister(ETH_HandleTypeDef *heth, uint16_t PHYReg, uint32_t RegValue)
  1139. {
  1140. uint32_t tmpreg1 = 0U;
  1141. uint32_t tickstart = 0U;
  1142. /* Check parameters */
  1143. assert_param(IS_ETH_PHY_ADDRESS(heth->Init.PhyAddress));
  1144. /* Check the ETH peripheral state */
  1145. if(heth->State == HAL_ETH_STATE_BUSY_WR)
  1146. {
  1147. return HAL_BUSY;
  1148. }
  1149. /* Set ETH HAL State to BUSY_WR */
  1150. heth->State = HAL_ETH_STATE_BUSY_WR;
  1151. /* Get the ETHERNET MACMIIAR value */
  1152. tmpreg1 = heth->Instance->MACMIIAR;
  1153. /* Keep only the CSR Clock Range CR[2:0] bits value */
  1154. tmpreg1 &= ~ETH_MACMIIAR_CR_MASK;
  1155. /* Prepare the MII register address value */
  1156. tmpreg1 |=(((uint32_t)heth->Init.PhyAddress<<11U) & ETH_MACMIIAR_PA); /* Set the PHY device address */
  1157. tmpreg1 |=(((uint32_t)PHYReg<<6U) & ETH_MACMIIAR_MR); /* Set the PHY register address */
  1158. tmpreg1 |= ETH_MACMIIAR_MW; /* Set the write mode */
  1159. tmpreg1 |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
  1160. /* Give the value to the MII data register */
  1161. heth->Instance->MACMIIDR = (uint16_t)RegValue;
  1162. /* Write the result value into the MII Address register */
  1163. heth->Instance->MACMIIAR = tmpreg1;
  1164. /* Get tick */
  1165. tickstart = HAL_GetTick();
  1166. /* Check for the Busy flag */
  1167. while((tmpreg1 & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB)
  1168. {
  1169. /* Check for the Timeout */
  1170. if((HAL_GetTick() - tickstart ) > PHY_WRITE_TO)
  1171. {
  1172. heth->State= HAL_ETH_STATE_READY;
  1173. /* Process Unlocked */
  1174. __HAL_UNLOCK(heth);
  1175. return HAL_TIMEOUT;
  1176. }
  1177. tmpreg1 = heth->Instance->MACMIIAR;
  1178. }
  1179. /* Set ETH HAL State to READY */
  1180. heth->State = HAL_ETH_STATE_READY;
  1181. /* Return function status */
  1182. return HAL_OK;
  1183. }
  1184. /**
  1185. * @}
  1186. */
  1187. /** @defgroup ETH_Exported_Functions_Group3 Peripheral Control functions
  1188. * @brief Peripheral Control functions
  1189. *
  1190. @verbatim
  1191. ===============================================================================
  1192. ##### Peripheral Control functions #####
  1193. ===============================================================================
  1194. [..] This section provides functions allowing to:
  1195. (+) Enable MAC and DMA transmission and reception.
  1196. HAL_ETH_Start();
  1197. (+) Disable MAC and DMA transmission and reception.
  1198. HAL_ETH_Stop();
  1199. (+) Set the MAC configuration in runtime mode
  1200. HAL_ETH_ConfigMAC();
  1201. (+) Set the DMA configuration in runtime mode
  1202. HAL_ETH_ConfigDMA();
  1203. @endverbatim
  1204. * @{
  1205. */
  1206. /**
  1207. * @brief Enables Ethernet MAC and DMA reception/transmission
  1208. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1209. * the configuration information for ETHERNET module
  1210. * @retval HAL status
  1211. */
  1212. HAL_StatusTypeDef HAL_ETH_Start(ETH_HandleTypeDef *heth)
  1213. {
  1214. /* Process Locked */
  1215. __HAL_LOCK(heth);
  1216. /* Set the ETH peripheral state to BUSY */
  1217. heth->State = HAL_ETH_STATE_BUSY;
  1218. /* Enable transmit state machine of the MAC for transmission on the MII */
  1219. ETH_MACTransmissionEnable(heth);
  1220. /* Enable receive state machine of the MAC for reception from the MII */
  1221. ETH_MACReceptionEnable(heth);
  1222. /* Flush Transmit FIFO */
  1223. ETH_FlushTransmitFIFO(heth);
  1224. /* Start DMA transmission */
  1225. ETH_DMATransmissionEnable(heth);
  1226. /* Start DMA reception */
  1227. ETH_DMAReceptionEnable(heth);
  1228. /* Set the ETH state to READY*/
  1229. heth->State= HAL_ETH_STATE_READY;
  1230. /* Process Unlocked */
  1231. __HAL_UNLOCK(heth);
  1232. /* Return function status */
  1233. return HAL_OK;
  1234. }
  1235. /**
  1236. * @brief Stop Ethernet MAC and DMA reception/transmission
  1237. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1238. * the configuration information for ETHERNET module
  1239. * @retval HAL status
  1240. */
  1241. HAL_StatusTypeDef HAL_ETH_Stop(ETH_HandleTypeDef *heth)
  1242. {
  1243. /* Process Locked */
  1244. __HAL_LOCK(heth);
  1245. /* Set the ETH peripheral state to BUSY */
  1246. heth->State = HAL_ETH_STATE_BUSY;
  1247. /* Stop DMA transmission */
  1248. ETH_DMATransmissionDisable(heth);
  1249. /* Stop DMA reception */
  1250. ETH_DMAReceptionDisable(heth);
  1251. /* Disable receive state machine of the MAC for reception from the MII */
  1252. ETH_MACReceptionDisable(heth);
  1253. /* Flush Transmit FIFO */
  1254. ETH_FlushTransmitFIFO(heth);
  1255. /* Disable transmit state machine of the MAC for transmission on the MII */
  1256. ETH_MACTransmissionDisable(heth);
  1257. /* Set the ETH state*/
  1258. heth->State = HAL_ETH_STATE_READY;
  1259. /* Process Unlocked */
  1260. __HAL_UNLOCK(heth);
  1261. /* Return function status */
  1262. return HAL_OK;
  1263. }
  1264. /**
  1265. * @brief Set ETH MAC Configuration.
  1266. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1267. * the configuration information for ETHERNET module
  1268. * @param macconf MAC Configuration structure
  1269. * @retval HAL status
  1270. */
  1271. HAL_StatusTypeDef HAL_ETH_ConfigMAC(ETH_HandleTypeDef *heth, ETH_MACInitTypeDef *macconf)
  1272. {
  1273. uint32_t tmpreg1 = 0U;
  1274. /* Process Locked */
  1275. __HAL_LOCK(heth);
  1276. /* Set the ETH peripheral state to BUSY */
  1277. heth->State= HAL_ETH_STATE_BUSY;
  1278. assert_param(IS_ETH_SPEED(heth->Init.Speed));
  1279. assert_param(IS_ETH_DUPLEX_MODE(heth->Init.DuplexMode));
  1280. if (macconf != NULL)
  1281. {
  1282. /* Check the parameters */
  1283. assert_param(IS_ETH_WATCHDOG(macconf->Watchdog));
  1284. assert_param(IS_ETH_JABBER(macconf->Jabber));
  1285. assert_param(IS_ETH_INTER_FRAME_GAP(macconf->InterFrameGap));
  1286. assert_param(IS_ETH_CARRIER_SENSE(macconf->CarrierSense));
  1287. assert_param(IS_ETH_RECEIVE_OWN(macconf->ReceiveOwn));
  1288. assert_param(IS_ETH_LOOPBACK_MODE(macconf->LoopbackMode));
  1289. assert_param(IS_ETH_CHECKSUM_OFFLOAD(macconf->ChecksumOffload));
  1290. assert_param(IS_ETH_RETRY_TRANSMISSION(macconf->RetryTransmission));
  1291. assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(macconf->AutomaticPadCRCStrip));
  1292. assert_param(IS_ETH_BACKOFF_LIMIT(macconf->BackOffLimit));
  1293. assert_param(IS_ETH_DEFERRAL_CHECK(macconf->DeferralCheck));
  1294. assert_param(IS_ETH_RECEIVE_ALL(macconf->ReceiveAll));
  1295. assert_param(IS_ETH_SOURCE_ADDR_FILTER(macconf->SourceAddrFilter));
  1296. assert_param(IS_ETH_CONTROL_FRAMES(macconf->PassControlFrames));
  1297. assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(macconf->BroadcastFramesReception));
  1298. assert_param(IS_ETH_DESTINATION_ADDR_FILTER(macconf->DestinationAddrFilter));
  1299. assert_param(IS_ETH_PROMISCUOUS_MODE(macconf->PromiscuousMode));
  1300. assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(macconf->MulticastFramesFilter));
  1301. assert_param(IS_ETH_UNICAST_FRAMES_FILTER(macconf->UnicastFramesFilter));
  1302. assert_param(IS_ETH_PAUSE_TIME(macconf->PauseTime));
  1303. assert_param(IS_ETH_ZEROQUANTA_PAUSE(macconf->ZeroQuantaPause));
  1304. assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(macconf->PauseLowThreshold));
  1305. assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(macconf->UnicastPauseFrameDetect));
  1306. assert_param(IS_ETH_RECEIVE_FLOWCONTROL(macconf->ReceiveFlowControl));
  1307. assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(macconf->TransmitFlowControl));
  1308. assert_param(IS_ETH_VLAN_TAG_COMPARISON(macconf->VLANTagComparison));
  1309. assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(macconf->VLANTagIdentifier));
  1310. /*------------------------ ETHERNET MACCR Configuration --------------------*/
  1311. /* Get the ETHERNET MACCR value */
  1312. tmpreg1 = (heth->Instance)->MACCR;
  1313. /* Clear WD, PCE, PS, TE and RE bits */
  1314. tmpreg1 &= ETH_MACCR_CLEAR_MASK;
  1315. tmpreg1 |= (uint32_t)(macconf->Watchdog |
  1316. macconf->Jabber |
  1317. macconf->InterFrameGap |
  1318. macconf->CarrierSense |
  1319. (heth->Init).Speed |
  1320. macconf->ReceiveOwn |
  1321. macconf->LoopbackMode |
  1322. (heth->Init).DuplexMode |
  1323. macconf->ChecksumOffload |
  1324. macconf->RetryTransmission |
  1325. macconf->AutomaticPadCRCStrip |
  1326. macconf->BackOffLimit |
  1327. macconf->DeferralCheck);
  1328. /* Write to ETHERNET MACCR */
  1329. (heth->Instance)->MACCR = (uint32_t)tmpreg1;
  1330. /* Wait until the write operation will be taken into account :
  1331. at least four TX_CLK/RX_CLK clock cycles */
  1332. tmpreg1 = (heth->Instance)->MACCR;
  1333. HAL_Delay(ETH_REG_WRITE_DELAY);
  1334. (heth->Instance)->MACCR = tmpreg1;
  1335. /*----------------------- ETHERNET MACFFR Configuration --------------------*/
  1336. /* Write to ETHERNET MACFFR */
  1337. (heth->Instance)->MACFFR = (uint32_t)(macconf->ReceiveAll |
  1338. macconf->SourceAddrFilter |
  1339. macconf->PassControlFrames |
  1340. macconf->BroadcastFramesReception |
  1341. macconf->DestinationAddrFilter |
  1342. macconf->PromiscuousMode |
  1343. macconf->MulticastFramesFilter |
  1344. macconf->UnicastFramesFilter);
  1345. /* Wait until the write operation will be taken into account :
  1346. at least four TX_CLK/RX_CLK clock cycles */
  1347. tmpreg1 = (heth->Instance)->MACFFR;
  1348. HAL_Delay(ETH_REG_WRITE_DELAY);
  1349. (heth->Instance)->MACFFR = tmpreg1;
  1350. /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
  1351. /* Write to ETHERNET MACHTHR */
  1352. (heth->Instance)->MACHTHR = (uint32_t)macconf->HashTableHigh;
  1353. /* Write to ETHERNET MACHTLR */
  1354. (heth->Instance)->MACHTLR = (uint32_t)macconf->HashTableLow;
  1355. /*----------------------- ETHERNET MACFCR Configuration --------------------*/
  1356. /* Get the ETHERNET MACFCR value */
  1357. tmpreg1 = (heth->Instance)->MACFCR;
  1358. /* Clear xx bits */
  1359. tmpreg1 &= ETH_MACFCR_CLEAR_MASK;
  1360. tmpreg1 |= (uint32_t)((macconf->PauseTime << 16U) |
  1361. macconf->ZeroQuantaPause |
  1362. macconf->PauseLowThreshold |
  1363. macconf->UnicastPauseFrameDetect |
  1364. macconf->ReceiveFlowControl |
  1365. macconf->TransmitFlowControl);
  1366. /* Write to ETHERNET MACFCR */
  1367. (heth->Instance)->MACFCR = (uint32_t)tmpreg1;
  1368. /* Wait until the write operation will be taken into account :
  1369. at least four TX_CLK/RX_CLK clock cycles */
  1370. tmpreg1 = (heth->Instance)->MACFCR;
  1371. HAL_Delay(ETH_REG_WRITE_DELAY);
  1372. (heth->Instance)->MACFCR = tmpreg1;
  1373. /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/
  1374. (heth->Instance)->MACVLANTR = (uint32_t)(macconf->VLANTagComparison |
  1375. macconf->VLANTagIdentifier);
  1376. /* Wait until the write operation will be taken into account :
  1377. at least four TX_CLK/RX_CLK clock cycles */
  1378. tmpreg1 = (heth->Instance)->MACVLANTR;
  1379. HAL_Delay(ETH_REG_WRITE_DELAY);
  1380. (heth->Instance)->MACVLANTR = tmpreg1;
  1381. }
  1382. else /* macconf == NULL : here we just configure Speed and Duplex mode */
  1383. {
  1384. /*------------------------ ETHERNET MACCR Configuration --------------------*/
  1385. /* Get the ETHERNET MACCR value */
  1386. tmpreg1 = (heth->Instance)->MACCR;
  1387. /* Clear FES and DM bits */
  1388. tmpreg1 &= ~(0x00004800U);
  1389. tmpreg1 |= (uint32_t)(heth->Init.Speed | heth->Init.DuplexMode);
  1390. /* Write to ETHERNET MACCR */
  1391. (heth->Instance)->MACCR = (uint32_t)tmpreg1;
  1392. /* Wait until the write operation will be taken into account:
  1393. at least four TX_CLK/RX_CLK clock cycles */
  1394. tmpreg1 = (heth->Instance)->MACCR;
  1395. HAL_Delay(ETH_REG_WRITE_DELAY);
  1396. (heth->Instance)->MACCR = tmpreg1;
  1397. }
  1398. /* Set the ETH state to Ready */
  1399. heth->State= HAL_ETH_STATE_READY;
  1400. /* Process Unlocked */
  1401. __HAL_UNLOCK(heth);
  1402. /* Return function status */
  1403. return HAL_OK;
  1404. }
  1405. /**
  1406. * @brief Sets ETH DMA Configuration.
  1407. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1408. * the configuration information for ETHERNET module
  1409. * @param dmaconf DMA Configuration structure
  1410. * @retval HAL status
  1411. */
  1412. HAL_StatusTypeDef HAL_ETH_ConfigDMA(ETH_HandleTypeDef *heth, ETH_DMAInitTypeDef *dmaconf)
  1413. {
  1414. uint32_t tmpreg1 = 0U;
  1415. /* Process Locked */
  1416. __HAL_LOCK(heth);
  1417. /* Set the ETH peripheral state to BUSY */
  1418. heth->State= HAL_ETH_STATE_BUSY;
  1419. /* Check parameters */
  1420. assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(dmaconf->DropTCPIPChecksumErrorFrame));
  1421. assert_param(IS_ETH_RECEIVE_STORE_FORWARD(dmaconf->ReceiveStoreForward));
  1422. assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(dmaconf->FlushReceivedFrame));
  1423. assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(dmaconf->TransmitStoreForward));
  1424. assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(dmaconf->TransmitThresholdControl));
  1425. assert_param(IS_ETH_FORWARD_ERROR_FRAMES(dmaconf->ForwardErrorFrames));
  1426. assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(dmaconf->ForwardUndersizedGoodFrames));
  1427. assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(dmaconf->ReceiveThresholdControl));
  1428. assert_param(IS_ETH_SECOND_FRAME_OPERATE(dmaconf->SecondFrameOperate));
  1429. assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(dmaconf->AddressAlignedBeats));
  1430. assert_param(IS_ETH_FIXED_BURST(dmaconf->FixedBurst));
  1431. assert_param(IS_ETH_RXDMA_BURST_LENGTH(dmaconf->RxDMABurstLength));
  1432. assert_param(IS_ETH_TXDMA_BURST_LENGTH(dmaconf->TxDMABurstLength));
  1433. assert_param(IS_ETH_ENHANCED_DESCRIPTOR_FORMAT(dmaconf->EnhancedDescriptorFormat));
  1434. assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(dmaconf->DescriptorSkipLength));
  1435. assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(dmaconf->DMAArbitration));
  1436. /*----------------------- ETHERNET DMAOMR Configuration --------------------*/
  1437. /* Get the ETHERNET DMAOMR value */
  1438. tmpreg1 = (heth->Instance)->DMAOMR;
  1439. /* Clear xx bits */
  1440. tmpreg1 &= ETH_DMAOMR_CLEAR_MASK;
  1441. tmpreg1 |= (uint32_t)(dmaconf->DropTCPIPChecksumErrorFrame |
  1442. dmaconf->ReceiveStoreForward |
  1443. dmaconf->FlushReceivedFrame |
  1444. dmaconf->TransmitStoreForward |
  1445. dmaconf->TransmitThresholdControl |
  1446. dmaconf->ForwardErrorFrames |
  1447. dmaconf->ForwardUndersizedGoodFrames |
  1448. dmaconf->ReceiveThresholdControl |
  1449. dmaconf->SecondFrameOperate);
  1450. /* Write to ETHERNET DMAOMR */
  1451. (heth->Instance)->DMAOMR = (uint32_t)tmpreg1;
  1452. /* Wait until the write operation will be taken into account:
  1453. at least four TX_CLK/RX_CLK clock cycles */
  1454. tmpreg1 = (heth->Instance)->DMAOMR;
  1455. HAL_Delay(ETH_REG_WRITE_DELAY);
  1456. (heth->Instance)->DMAOMR = tmpreg1;
  1457. /*----------------------- ETHERNET DMABMR Configuration --------------------*/
  1458. (heth->Instance)->DMABMR = (uint32_t)(dmaconf->AddressAlignedBeats |
  1459. dmaconf->FixedBurst |
  1460. dmaconf->RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
  1461. dmaconf->TxDMABurstLength |
  1462. dmaconf->EnhancedDescriptorFormat |
  1463. (dmaconf->DescriptorSkipLength << 2U) |
  1464. dmaconf->DMAArbitration |
  1465. ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
  1466. /* Wait until the write operation will be taken into account:
  1467. at least four TX_CLK/RX_CLK clock cycles */
  1468. tmpreg1 = (heth->Instance)->DMABMR;
  1469. HAL_Delay(ETH_REG_WRITE_DELAY);
  1470. (heth->Instance)->DMABMR = tmpreg1;
  1471. /* Set the ETH state to Ready */
  1472. heth->State= HAL_ETH_STATE_READY;
  1473. /* Process Unlocked */
  1474. __HAL_UNLOCK(heth);
  1475. /* Return function status */
  1476. return HAL_OK;
  1477. }
  1478. /**
  1479. * @}
  1480. */
  1481. /** @defgroup ETH_Exported_Functions_Group4 Peripheral State functions
  1482. * @brief Peripheral State functions
  1483. *
  1484. @verbatim
  1485. ===============================================================================
  1486. ##### Peripheral State functions #####
  1487. ===============================================================================
  1488. [..]
  1489. This subsection permits to get in run-time the status of the peripheral
  1490. and the data flow.
  1491. (+) Get the ETH handle state:
  1492. HAL_ETH_GetState();
  1493. @endverbatim
  1494. * @{
  1495. */
  1496. /**
  1497. * @brief Return the ETH HAL state
  1498. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1499. * the configuration information for ETHERNET module
  1500. * @retval HAL state
  1501. */
  1502. HAL_ETH_StateTypeDef HAL_ETH_GetState(ETH_HandleTypeDef *heth)
  1503. {
  1504. /* Return ETH state */
  1505. return heth->State;
  1506. }
  1507. /**
  1508. * @}
  1509. */
  1510. /**
  1511. * @}
  1512. */
  1513. /** @addtogroup ETH_Private_Functions
  1514. * @{
  1515. */
  1516. /**
  1517. * @brief Configures Ethernet MAC and DMA with default parameters.
  1518. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1519. * the configuration information for ETHERNET module
  1520. * @param err Ethernet Init error
  1521. * @retval HAL status
  1522. */
  1523. static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err)
  1524. {
  1525. ETH_MACInitTypeDef macinit;
  1526. ETH_DMAInitTypeDef dmainit;
  1527. uint32_t tmpreg1 = 0U;
  1528. if (err != ETH_SUCCESS) /* Auto-negotiation failed */
  1529. {
  1530. /* Set Ethernet duplex mode to Full-duplex */
  1531. (heth->Init).DuplexMode = ETH_MODE_FULLDUPLEX;
  1532. /* Set Ethernet speed to 100M */
  1533. (heth->Init).Speed = ETH_SPEED_100M;
  1534. }
  1535. /* Ethernet MAC default initialization **************************************/
  1536. macinit.Watchdog = ETH_WATCHDOG_ENABLE;
  1537. macinit.Jabber = ETH_JABBER_ENABLE;
  1538. macinit.InterFrameGap = ETH_INTERFRAMEGAP_96BIT;
  1539. macinit.CarrierSense = ETH_CARRIERSENCE_ENABLE;
  1540. macinit.ReceiveOwn = ETH_RECEIVEOWN_ENABLE;
  1541. macinit.LoopbackMode = ETH_LOOPBACKMODE_DISABLE;
  1542. if(heth->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)
  1543. {
  1544. macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_ENABLE;
  1545. }
  1546. else
  1547. {
  1548. macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_DISABLE;
  1549. }
  1550. macinit.RetryTransmission = ETH_RETRYTRANSMISSION_DISABLE;
  1551. macinit.AutomaticPadCRCStrip = ETH_AUTOMATICPADCRCSTRIP_DISABLE;
  1552. macinit.BackOffLimit = ETH_BACKOFFLIMIT_10;
  1553. macinit.DeferralCheck = ETH_DEFFERRALCHECK_DISABLE;
  1554. macinit.ReceiveAll = ETH_RECEIVEAll_DISABLE;
  1555. macinit.SourceAddrFilter = ETH_SOURCEADDRFILTER_DISABLE;
  1556. macinit.PassControlFrames = ETH_PASSCONTROLFRAMES_BLOCKALL;
  1557. macinit.BroadcastFramesReception = ETH_BROADCASTFRAMESRECEPTION_ENABLE;
  1558. macinit.DestinationAddrFilter = ETH_DESTINATIONADDRFILTER_NORMAL;
  1559. macinit.PromiscuousMode = ETH_PROMISCUOUS_MODE_DISABLE;
  1560. macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_PERFECT;
  1561. macinit.UnicastFramesFilter = ETH_UNICASTFRAMESFILTER_PERFECT;
  1562. macinit.HashTableHigh = 0x0U;
  1563. macinit.HashTableLow = 0x0U;
  1564. macinit.PauseTime = 0x0U;
  1565. macinit.ZeroQuantaPause = ETH_ZEROQUANTAPAUSE_DISABLE;
  1566. macinit.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS4;
  1567. macinit.UnicastPauseFrameDetect = ETH_UNICASTPAUSEFRAMEDETECT_DISABLE;
  1568. macinit.ReceiveFlowControl = ETH_RECEIVEFLOWCONTROL_DISABLE;
  1569. macinit.TransmitFlowControl = ETH_TRANSMITFLOWCONTROL_DISABLE;
  1570. macinit.VLANTagComparison = ETH_VLANTAGCOMPARISON_16BIT;
  1571. macinit.VLANTagIdentifier = 0x0U;
  1572. /*------------------------ ETHERNET MACCR Configuration --------------------*/
  1573. /* Get the ETHERNET MACCR value */
  1574. tmpreg1 = (heth->Instance)->MACCR;
  1575. /* Clear WD, PCE, PS, TE and RE bits */
  1576. tmpreg1 &= ETH_MACCR_CLEAR_MASK;
  1577. /* Set the WD bit according to ETH Watchdog value */
  1578. /* Set the JD: bit according to ETH Jabber value */
  1579. /* Set the IFG bit according to ETH InterFrameGap value */
  1580. /* Set the DCRS bit according to ETH CarrierSense value */
  1581. /* Set the FES bit according to ETH Speed value */
  1582. /* Set the DO bit according to ETH ReceiveOwn value */
  1583. /* Set the LM bit according to ETH LoopbackMode value */
  1584. /* Set the DM bit according to ETH Mode value */
  1585. /* Set the IPCO bit according to ETH ChecksumOffload value */
  1586. /* Set the DR bit according to ETH RetryTransmission value */
  1587. /* Set the ACS bit according to ETH AutomaticPadCRCStrip value */
  1588. /* Set the BL bit according to ETH BackOffLimit value */
  1589. /* Set the DC bit according to ETH DeferralCheck value */
  1590. tmpreg1 |= (uint32_t)(macinit.Watchdog |
  1591. macinit.Jabber |
  1592. macinit.InterFrameGap |
  1593. macinit.CarrierSense |
  1594. (heth->Init).Speed |
  1595. macinit.ReceiveOwn |
  1596. macinit.LoopbackMode |
  1597. (heth->Init).DuplexMode |
  1598. macinit.ChecksumOffload |
  1599. macinit.RetryTransmission |
  1600. macinit.AutomaticPadCRCStrip |
  1601. macinit.BackOffLimit |
  1602. macinit.DeferralCheck);
  1603. /* Write to ETHERNET MACCR */
  1604. (heth->Instance)->MACCR = (uint32_t)tmpreg1;
  1605. /* Wait until the write operation will be taken into account:
  1606. at least four TX_CLK/RX_CLK clock cycles */
  1607. tmpreg1 = (heth->Instance)->MACCR;
  1608. HAL_Delay(ETH_REG_WRITE_DELAY);
  1609. (heth->Instance)->MACCR = tmpreg1;
  1610. /*----------------------- ETHERNET MACFFR Configuration --------------------*/
  1611. /* Set the RA bit according to ETH ReceiveAll value */
  1612. /* Set the SAF and SAIF bits according to ETH SourceAddrFilter value */
  1613. /* Set the PCF bit according to ETH PassControlFrames value */
  1614. /* Set the DBF bit according to ETH BroadcastFramesReception value */
  1615. /* Set the DAIF bit according to ETH DestinationAddrFilter value */
  1616. /* Set the PR bit according to ETH PromiscuousMode value */
  1617. /* Set the PM, HMC and HPF bits according to ETH MulticastFramesFilter value */
  1618. /* Set the HUC and HPF bits according to ETH UnicastFramesFilter value */
  1619. /* Write to ETHERNET MACFFR */
  1620. (heth->Instance)->MACFFR = (uint32_t)(macinit.ReceiveAll |
  1621. macinit.SourceAddrFilter |
  1622. macinit.PassControlFrames |
  1623. macinit.BroadcastFramesReception |
  1624. macinit.DestinationAddrFilter |
  1625. macinit.PromiscuousMode |
  1626. macinit.MulticastFramesFilter |
  1627. macinit.UnicastFramesFilter);
  1628. /* Wait until the write operation will be taken into account:
  1629. at least four TX_CLK/RX_CLK clock cycles */
  1630. tmpreg1 = (heth->Instance)->MACFFR;
  1631. HAL_Delay(ETH_REG_WRITE_DELAY);
  1632. (heth->Instance)->MACFFR = tmpreg1;
  1633. /*--------------- ETHERNET MACHTHR and MACHTLR Configuration --------------*/
  1634. /* Write to ETHERNET MACHTHR */
  1635. (heth->Instance)->MACHTHR = (uint32_t)macinit.HashTableHigh;
  1636. /* Write to ETHERNET MACHTLR */
  1637. (heth->Instance)->MACHTLR = (uint32_t)macinit.HashTableLow;
  1638. /*----------------------- ETHERNET MACFCR Configuration -------------------*/
  1639. /* Get the ETHERNET MACFCR value */
  1640. tmpreg1 = (heth->Instance)->MACFCR;
  1641. /* Clear xx bits */
  1642. tmpreg1 &= ETH_MACFCR_CLEAR_MASK;
  1643. /* Set the PT bit according to ETH PauseTime value */
  1644. /* Set the DZPQ bit according to ETH ZeroQuantaPause value */
  1645. /* Set the PLT bit according to ETH PauseLowThreshold value */
  1646. /* Set the UP bit according to ETH UnicastPauseFrameDetect value */
  1647. /* Set the RFE bit according to ETH ReceiveFlowControl value */
  1648. /* Set the TFE bit according to ETH TransmitFlowControl value */
  1649. tmpreg1 |= (uint32_t)((macinit.PauseTime << 16U) |
  1650. macinit.ZeroQuantaPause |
  1651. macinit.PauseLowThreshold |
  1652. macinit.UnicastPauseFrameDetect |
  1653. macinit.ReceiveFlowControl |
  1654. macinit.TransmitFlowControl);
  1655. /* Write to ETHERNET MACFCR */
  1656. (heth->Instance)->MACFCR = (uint32_t)tmpreg1;
  1657. /* Wait until the write operation will be taken into account:
  1658. at least four TX_CLK/RX_CLK clock cycles */
  1659. tmpreg1 = (heth->Instance)->MACFCR;
  1660. HAL_Delay(ETH_REG_WRITE_DELAY);
  1661. (heth->Instance)->MACFCR = tmpreg1;
  1662. /*----------------------- ETHERNET MACVLANTR Configuration ----------------*/
  1663. /* Set the ETV bit according to ETH VLANTagComparison value */
  1664. /* Set the VL bit according to ETH VLANTagIdentifier value */
  1665. (heth->Instance)->MACVLANTR = (uint32_t)(macinit.VLANTagComparison |
  1666. macinit.VLANTagIdentifier);
  1667. /* Wait until the write operation will be taken into account:
  1668. at least four TX_CLK/RX_CLK clock cycles */
  1669. tmpreg1 = (heth->Instance)->MACVLANTR;
  1670. HAL_Delay(ETH_REG_WRITE_DELAY);
  1671. (heth->Instance)->MACVLANTR = tmpreg1;
  1672. /* Ethernet DMA default initialization ************************************/
  1673. dmainit.DropTCPIPChecksumErrorFrame = ETH_DROPTCPIPCHECKSUMERRORFRAME_ENABLE;
  1674. dmainit.ReceiveStoreForward = ETH_RECEIVESTOREFORWARD_ENABLE;
  1675. dmainit.FlushReceivedFrame = ETH_FLUSHRECEIVEDFRAME_ENABLE;
  1676. dmainit.TransmitStoreForward = ETH_TRANSMITSTOREFORWARD_ENABLE;
  1677. dmainit.TransmitThresholdControl = ETH_TRANSMITTHRESHOLDCONTROL_64BYTES;
  1678. dmainit.ForwardErrorFrames = ETH_FORWARDERRORFRAMES_DISABLE;
  1679. dmainit.ForwardUndersizedGoodFrames = ETH_FORWARDUNDERSIZEDGOODFRAMES_DISABLE;
  1680. dmainit.ReceiveThresholdControl = ETH_RECEIVEDTHRESHOLDCONTROL_64BYTES;
  1681. dmainit.SecondFrameOperate = ETH_SECONDFRAMEOPERARTE_ENABLE;
  1682. dmainit.AddressAlignedBeats = ETH_ADDRESSALIGNEDBEATS_ENABLE;
  1683. dmainit.FixedBurst = ETH_FIXEDBURST_ENABLE;
  1684. dmainit.RxDMABurstLength = ETH_RXDMABURSTLENGTH_32BEAT;
  1685. dmainit.TxDMABurstLength = ETH_TXDMABURSTLENGTH_32BEAT;
  1686. dmainit.EnhancedDescriptorFormat = ETH_DMAENHANCEDDESCRIPTOR_ENABLE;
  1687. dmainit.DescriptorSkipLength = 0x0U;
  1688. dmainit.DMAArbitration = ETH_DMAARBITRATION_ROUNDROBIN_RXTX_1_1;
  1689. /* Get the ETHERNET DMAOMR value */
  1690. tmpreg1 = (heth->Instance)->DMAOMR;
  1691. /* Clear xx bits */
  1692. tmpreg1 &= ETH_DMAOMR_CLEAR_MASK;
  1693. /* Set the DT bit according to ETH DropTCPIPChecksumErrorFrame value */
  1694. /* Set the RSF bit according to ETH ReceiveStoreForward value */
  1695. /* Set the DFF bit according to ETH FlushReceivedFrame value */
  1696. /* Set the TSF bit according to ETH TransmitStoreForward value */
  1697. /* Set the TTC bit according to ETH TransmitThresholdControl value */
  1698. /* Set the FEF bit according to ETH ForwardErrorFrames value */
  1699. /* Set the FUF bit according to ETH ForwardUndersizedGoodFrames value */
  1700. /* Set the RTC bit according to ETH ReceiveThresholdControl value */
  1701. /* Set the OSF bit according to ETH SecondFrameOperate value */
  1702. tmpreg1 |= (uint32_t)(dmainit.DropTCPIPChecksumErrorFrame |
  1703. dmainit.ReceiveStoreForward |
  1704. dmainit.FlushReceivedFrame |
  1705. dmainit.TransmitStoreForward |
  1706. dmainit.TransmitThresholdControl |
  1707. dmainit.ForwardErrorFrames |
  1708. dmainit.ForwardUndersizedGoodFrames |
  1709. dmainit.ReceiveThresholdControl |
  1710. dmainit.SecondFrameOperate);
  1711. /* Write to ETHERNET DMAOMR */
  1712. (heth->Instance)->DMAOMR = (uint32_t)tmpreg1;
  1713. /* Wait until the write operation will be taken into account:
  1714. at least four TX_CLK/RX_CLK clock cycles */
  1715. tmpreg1 = (heth->Instance)->DMAOMR;
  1716. HAL_Delay(ETH_REG_WRITE_DELAY);
  1717. (heth->Instance)->DMAOMR = tmpreg1;
  1718. /*----------------------- ETHERNET DMABMR Configuration ------------------*/
  1719. /* Set the AAL bit according to ETH AddressAlignedBeats value */
  1720. /* Set the FB bit according to ETH FixedBurst value */
  1721. /* Set the RPBL and 4*PBL bits according to ETH RxDMABurstLength value */
  1722. /* Set the PBL and 4*PBL bits according to ETH TxDMABurstLength value */
  1723. /* Set the Enhanced DMA descriptors bit according to ETH EnhancedDescriptorFormat value*/
  1724. /* Set the DSL bit according to ETH DesciptorSkipLength value */
  1725. /* Set the PR and DA bits according to ETH DMAArbitration value */
  1726. (heth->Instance)->DMABMR = (uint32_t)(dmainit.AddressAlignedBeats |
  1727. dmainit.FixedBurst |
  1728. dmainit.RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
  1729. dmainit.TxDMABurstLength |
  1730. dmainit.EnhancedDescriptorFormat |
  1731. (dmainit.DescriptorSkipLength << 2U) |
  1732. dmainit.DMAArbitration |
  1733. ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
  1734. /* Wait until the write operation will be taken into account:
  1735. at least four TX_CLK/RX_CLK clock cycles */
  1736. tmpreg1 = (heth->Instance)->DMABMR;
  1737. HAL_Delay(ETH_REG_WRITE_DELAY);
  1738. (heth->Instance)->DMABMR = tmpreg1;
  1739. if((heth->Init).RxMode == ETH_RXINTERRUPT_MODE)
  1740. {
  1741. /* Enable the Ethernet Rx Interrupt */
  1742. __HAL_ETH_DMA_ENABLE_IT((heth), ETH_DMA_IT_NIS | ETH_DMA_IT_R);
  1743. }
  1744. /* Initialize MAC address in ethernet MAC */
  1745. ETH_MACAddressConfig(heth, ETH_MAC_ADDRESS0, heth->Init.MACAddr);
  1746. }
  1747. /**
  1748. * @brief Configures the selected MAC address.
  1749. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1750. * the configuration information for ETHERNET module
  1751. * @param MacAddr The MAC address to configure
  1752. * This parameter can be one of the following values:
  1753. * @arg ETH_MAC_Address0: MAC Address0
  1754. * @arg ETH_MAC_Address1: MAC Address1
  1755. * @arg ETH_MAC_Address2: MAC Address2
  1756. * @arg ETH_MAC_Address3: MAC Address3
  1757. * @param Addr Pointer to MAC address buffer data (6 bytes)
  1758. * @retval HAL status
  1759. */
  1760. static void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr)
  1761. {
  1762. uint32_t tmpreg1;
  1763. /* Prevent unused argument(s) compilation warning */
  1764. UNUSED(heth);
  1765. /* Check the parameters */
  1766. assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
  1767. /* Calculate the selected MAC address high register */
  1768. tmpreg1 = ((uint32_t)Addr[5U] << 8U) | (uint32_t)Addr[4U];
  1769. /* Load the selected MAC address high register */
  1770. (*(__IO uint32_t *)((uint32_t)(ETH_MAC_ADDR_HBASE + MacAddr))) = tmpreg1;
  1771. /* Calculate the selected MAC address low register */
  1772. tmpreg1 = ((uint32_t)Addr[3U] << 24U) | ((uint32_t)Addr[2U] << 16U) | ((uint32_t)Addr[1U] << 8U) | Addr[0U];
  1773. /* Load the selected MAC address low register */
  1774. (*(__IO uint32_t *)((uint32_t)(ETH_MAC_ADDR_LBASE + MacAddr))) = tmpreg1;
  1775. }
  1776. /**
  1777. * @brief Enables the MAC transmission.
  1778. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1779. * the configuration information for ETHERNET module
  1780. * @retval None
  1781. */
  1782. static void ETH_MACTransmissionEnable(ETH_HandleTypeDef *heth)
  1783. {
  1784. __IO uint32_t tmpreg1 = 0U;
  1785. /* Enable the MAC transmission */
  1786. (heth->Instance)->MACCR |= ETH_MACCR_TE;
  1787. /* Wait until the write operation will be taken into account:
  1788. at least four TX_CLK/RX_CLK clock cycles */
  1789. tmpreg1 = (heth->Instance)->MACCR;
  1790. ETH_Delay(ETH_REG_WRITE_DELAY);
  1791. (heth->Instance)->MACCR = tmpreg1;
  1792. }
  1793. /**
  1794. * @brief Disables the MAC transmission.
  1795. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1796. * the configuration information for ETHERNET module
  1797. * @retval None
  1798. */
  1799. static void ETH_MACTransmissionDisable(ETH_HandleTypeDef *heth)
  1800. {
  1801. __IO uint32_t tmpreg1 = 0U;
  1802. /* Disable the MAC transmission */
  1803. (heth->Instance)->MACCR &= ~ETH_MACCR_TE;
  1804. /* Wait until the write operation will be taken into account:
  1805. at least four TX_CLK/RX_CLK clock cycles */
  1806. tmpreg1 = (heth->Instance)->MACCR;
  1807. ETH_Delay(ETH_REG_WRITE_DELAY);
  1808. (heth->Instance)->MACCR = tmpreg1;
  1809. }
  1810. /**
  1811. * @brief Enables the MAC reception.
  1812. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1813. * the configuration information for ETHERNET module
  1814. * @retval None
  1815. */
  1816. static void ETH_MACReceptionEnable(ETH_HandleTypeDef *heth)
  1817. {
  1818. __IO uint32_t tmpreg1 = 0U;
  1819. /* Enable the MAC reception */
  1820. (heth->Instance)->MACCR |= ETH_MACCR_RE;
  1821. /* Wait until the write operation will be taken into account:
  1822. at least four TX_CLK/RX_CLK clock cycles */
  1823. tmpreg1 = (heth->Instance)->MACCR;
  1824. ETH_Delay(ETH_REG_WRITE_DELAY);
  1825. (heth->Instance)->MACCR = tmpreg1;
  1826. }
  1827. /**
  1828. * @brief Disables the MAC reception.
  1829. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1830. * the configuration information for ETHERNET module
  1831. * @retval None
  1832. */
  1833. static void ETH_MACReceptionDisable(ETH_HandleTypeDef *heth)
  1834. {
  1835. __IO uint32_t tmpreg1 = 0U;
  1836. /* Disable the MAC reception */
  1837. (heth->Instance)->MACCR &= ~ETH_MACCR_RE;
  1838. /* Wait until the write operation will be taken into account:
  1839. at least four TX_CLK/RX_CLK clock cycles */
  1840. tmpreg1 = (heth->Instance)->MACCR;
  1841. ETH_Delay(ETH_REG_WRITE_DELAY);
  1842. (heth->Instance)->MACCR = tmpreg1;
  1843. }
  1844. /**
  1845. * @brief Enables the DMA transmission.
  1846. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1847. * the configuration information for ETHERNET module
  1848. * @retval None
  1849. */
  1850. static void ETH_DMATransmissionEnable(ETH_HandleTypeDef *heth)
  1851. {
  1852. /* Enable the DMA transmission */
  1853. (heth->Instance)->DMAOMR |= ETH_DMAOMR_ST;
  1854. }
  1855. /**
  1856. * @brief Disables the DMA transmission.
  1857. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1858. * the configuration information for ETHERNET module
  1859. * @retval None
  1860. */
  1861. static void ETH_DMATransmissionDisable(ETH_HandleTypeDef *heth)
  1862. {
  1863. /* Disable the DMA transmission */
  1864. (heth->Instance)->DMAOMR &= ~ETH_DMAOMR_ST;
  1865. }
  1866. /**
  1867. * @brief Enables the DMA reception.
  1868. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1869. * the configuration information for ETHERNET module
  1870. * @retval None
  1871. */
  1872. static void ETH_DMAReceptionEnable(ETH_HandleTypeDef *heth)
  1873. {
  1874. /* Enable the DMA reception */
  1875. (heth->Instance)->DMAOMR |= ETH_DMAOMR_SR;
  1876. }
  1877. /**
  1878. * @brief Disables the DMA reception.
  1879. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1880. * the configuration information for ETHERNET module
  1881. * @retval None
  1882. */
  1883. static void ETH_DMAReceptionDisable(ETH_HandleTypeDef *heth)
  1884. {
  1885. /* Disable the DMA reception */
  1886. (heth->Instance)->DMAOMR &= ~ETH_DMAOMR_SR;
  1887. }
  1888. /**
  1889. * @brief Clears the ETHERNET transmit FIFO.
  1890. * @param heth pointer to a ETH_HandleTypeDef structure that contains
  1891. * the configuration information for ETHERNET module
  1892. * @retval None
  1893. */
  1894. static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef *heth)
  1895. {
  1896. __IO uint32_t tmpreg1 = 0U;
  1897. /* Set the Flush Transmit FIFO bit */
  1898. (heth->Instance)->DMAOMR |= ETH_DMAOMR_FTF;
  1899. /* Wait until the write operation will be taken into account:
  1900. at least four TX_CLK/RX_CLK clock cycles */
  1901. tmpreg1 = (heth->Instance)->DMAOMR;
  1902. ETH_Delay(ETH_REG_WRITE_DELAY);
  1903. (heth->Instance)->DMAOMR = tmpreg1;
  1904. }
  1905. /**
  1906. * @brief This function provides delay (in milliseconds) based on CPU cycles method.
  1907. * @param mdelay specifies the delay time length, in milliseconds.
  1908. * @retval None
  1909. */
  1910. static void ETH_Delay(uint32_t mdelay)
  1911. {
  1912. __IO uint32_t Delay = mdelay * (SystemCoreClock / 8U / 1000U);
  1913. do
  1914. {
  1915. __NOP();
  1916. }
  1917. while (Delay --);
  1918. }
  1919. #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
  1920. static void ETH_InitCallbacksToDefault(ETH_HandleTypeDef *heth)
  1921. {
  1922. /* Init the ETH Callback settings */
  1923. heth->TxCpltCallback = HAL_ETH_TxCpltCallback; /* Legacy weak TxCpltCallback */
  1924. heth->RxCpltCallback = HAL_ETH_RxCpltCallback; /* Legacy weak RxCpltCallback */
  1925. heth->DMAErrorCallback = HAL_ETH_ErrorCallback; /* Legacy weak DMAErrorCallback */
  1926. }
  1927. #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
  1928. /**
  1929. * @}
  1930. */
  1931. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx ||\
  1932. STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */
  1933. #endif /* HAL_ETH_MODULE_ENABLED */
  1934. /**
  1935. * @}
  1936. */
  1937. /**
  1938. * @}
  1939. */
  1940. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/