stm32f4xx_hal_eth.c 71 KB

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