stm32f4xx_hal_eth.c 72 KB

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