You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2065 lines
71 KiB

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