stm32f1xx_hal_eth.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  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. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. (#)Declare a ETH_HandleTypeDef handle structure, for example:
  19. ETH_HandleTypeDef heth;
  20. (#)Fill parameters of Init structure in heth handle
  21. (#)Call HAL_ETH_Init() API to initialize the Ethernet peripheral (MAC, DMA, ...)
  22. (#)Initialize the ETH low level resources through the HAL_ETH_MspInit() API:
  23. (##) Enable the Ethernet interface clock using
  24. (+++) __HAL_RCC_ETHMAC_CLK_ENABLE();
  25. (+++) __HAL_RCC_ETHMACTX_CLK_ENABLE();
  26. (+++) __HAL_RCC_ETHMACRX_CLK_ENABLE();
  27. (##) Initialize the related GPIO clocks
  28. (##) Configure Ethernet pin-out
  29. (##) Configure Ethernet NVIC interrupt (IT mode)
  30. (#)Initialize Ethernet DMA Descriptors in chain mode and point to allocated buffers:
  31. (##) HAL_ETH_DMATxDescListInit(); for Transmission process
  32. (##) HAL_ETH_DMARxDescListInit(); for Reception process
  33. (#)Enable MAC and DMA transmission and reception:
  34. (##) HAL_ETH_Start();
  35. (#)Prepare ETH DMA TX Descriptors and give the hand to ETH DMA to transfer
  36. the frame to MAC TX FIFO:
  37. (##) HAL_ETH_TransmitFrame();
  38. (#)Poll for a received frame in ETH RX DMA Descriptors and get received
  39. frame parameters
  40. (##) HAL_ETH_GetReceivedFrame(); (should be called into an infinite loop)
  41. (#) Get a received frame when an ETH RX interrupt occurs:
  42. (##) HAL_ETH_GetReceivedFrame_IT(); (called in IT mode only)
  43. (#) Communicate with external PHY device:
  44. (##) Read a specific register from the PHY
  45. HAL_ETH_ReadPHYRegister();
  46. (##) Write data to a specific RHY register:
  47. HAL_ETH_WritePHYRegister();
  48. (#) Configure the Ethernet MAC after ETH peripheral initialization
  49. HAL_ETH_ConfigMAC(); all MAC parameters should be filled.
  50. (#) Configure the Ethernet DMA after ETH peripheral initialization
  51. HAL_ETH_ConfigDMA(); all DMA parameters should be filled.
  52. -@- The PTP protocol and the DMA descriptors ring mode are not supported
  53. in this driver
  54. *** Callback registration ***
  55. =============================================
  56. The compilation define USE_HAL_ETH_REGISTER_CALLBACKS when set to 1
  57. allows the user to configure dynamically the driver callbacks.
  58. Use Function @ref HAL_ETH_RegisterCallback() to register an interrupt callback.
  59. Function @ref HAL_ETH_RegisterCallback() allows to register following callbacks:
  60. (+) TxCpltCallback : Tx Complete Callback.
  61. (+) RxCpltCallback : Rx Complete Callback.
  62. (+) DMAErrorCallback : DMA Error Callback.
  63. (+) MspInitCallback : MspInit Callback.
  64. (+) MspDeInitCallback: MspDeInit Callback.
  65. This function takes as parameters the HAL peripheral handle, the Callback ID
  66. and a pointer to the user callback function.
  67. Use function @ref HAL_ETH_UnRegisterCallback() to reset a callback to the default
  68. weak function.
  69. @ref HAL_ETH_UnRegisterCallback takes as parameters the HAL peripheral handle,
  70. and the Callback ID.
  71. This function allows to reset following callbacks:
  72. (+) TxCpltCallback : Tx Complete Callback.
  73. (+) RxCpltCallback : Rx Complete Callback.
  74. (+) DMAErrorCallback : DMA Error Callback.
  75. (+) MspInitCallback : MspInit Callback.
  76. (+) MspDeInitCallback: MspDeInit Callback.
  77. By default, after the HAL_ETH_Init and when the state is HAL_ETH_STATE_RESET
  78. all callbacks are set to the corresponding weak functions:
  79. examples @ref HAL_ETH_TxCpltCallback(), @ref HAL_ETH_RxCpltCallback().
  80. Exception done for MspInit and MspDeInit functions that are
  81. reset to the legacy weak function in the HAL_ETH_Init/ @ref HAL_ETH_DeInit only when
  82. these callbacks are null (not registered beforehand).
  83. if not, MspInit or MspDeInit are not null, the HAL_ETH_Init/ @ref HAL_ETH_DeInit
  84. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  85. Callbacks can be registered/unregistered in HAL_ETH_STATE_READY state only.
  86. Exception done MspInit/MspDeInit that can be registered/unregistered
  87. in HAL_ETH_STATE_READY or HAL_ETH_STATE_RESET state,
  88. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  89. In that case first register the MspInit/MspDeInit user callbacks
  90. using @ref HAL_ETH_RegisterCallback() before calling @ref HAL_ETH_DeInit
  91. or HAL_ETH_Init function.
  92. When The compilation define USE_HAL_ETH_REGISTER_CALLBACKS is set to 0 or
  93. not defined, the callback registration feature is not available and all callbacks
  94. are set to the corresponding weak functions.
  95. @endverbatim
  96. ******************************************************************************
  97. * @attention
  98. *
  99. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  100. * All rights reserved.</center></h2>
  101. *
  102. * This software component is licensed by ST under BSD 3-Clause license,
  103. * the "License"; You may not use this file except in compliance with the
  104. * License. You may obtain a copy of the License at:
  105. * opensource.org/licenses/BSD-3-Clause
  106. *
  107. ******************************************************************************
  108. */
  109. /* Includes ------------------------------------------------------------------*/
  110. #include "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 callabck 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. */
  1923. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/