stm32f7xx_hal_eth.c 70 KB

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