stm32f1xx_hal_eth.c 76 KB

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