stm32f4xx_hal_eth.c 74 KB

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