Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_eth.c
blob0dea1145c56e1989bbe1c6d49aaf484d09cf250b
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_eth.c
4 * @author MCD Application Team
5 * @brief ETH HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Ethernet (ETH) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State and Errors functions
13 @verbatim
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
17 [..]
18 (#)Declare a ETH_HandleTypeDef handle structure, for example:
19 ETH_HandleTypeDef heth;
21 (#)Fill parameters of Init structure in heth handle
23 (#)Call HAL_ETH_Init() API to initialize the Ethernet peripheral (MAC, DMA, ...)
25 (#)Initialize the ETH low level resources through the HAL_ETH_MspInit() API:
26 (##) Enable the Ethernet interface clock using
27 (+++) __HAL_RCC_ETHMAC_CLK_ENABLE();
28 (+++) __HAL_RCC_ETHMACTX_CLK_ENABLE();
29 (+++) __HAL_RCC_ETHMACRX_CLK_ENABLE();
31 (##) Initialize the related GPIO clocks
32 (##) Configure Ethernet pin-out
33 (##) Configure Ethernet NVIC interrupt (IT mode)
35 (#)Initialize Ethernet DMA Descriptors in chain mode and point to allocated buffers:
36 (##) HAL_ETH_DMATxDescListInit(); for Transmission process
37 (##) HAL_ETH_DMARxDescListInit(); for Reception process
39 (#)Enable MAC and DMA transmission and reception:
40 (##) HAL_ETH_Start();
42 (#)Prepare ETH DMA TX Descriptors and give the hand to ETH DMA to transfer
43 the frame to MAC TX FIFO:
44 (##) HAL_ETH_TransmitFrame();
46 (#)Poll for a received frame in ETH RX DMA Descriptors and get received
47 frame parameters
48 (##) HAL_ETH_GetReceivedFrame(); (should be called into an infinite loop)
50 (#) Get a received frame when an ETH RX interrupt occurs:
51 (##) HAL_ETH_GetReceivedFrame_IT(); (called in IT mode only)
53 (#) Communicate with external PHY device:
54 (##) Read a specific register from the PHY
55 HAL_ETH_ReadPHYRegister();
56 (##) Write data to a specific RHY register:
57 HAL_ETH_WritePHYRegister();
59 (#) Configure the Ethernet MAC after ETH peripheral initialization
60 HAL_ETH_ConfigMAC(); all MAC parameters should be filled.
62 (#) Configure the Ethernet DMA after ETH peripheral initialization
63 HAL_ETH_ConfigDMA(); all DMA parameters should be filled.
65 *** Callback registration ***
66 =============================================
68 The compilation define USE_HAL_ETH_REGISTER_CALLBACKS when set to 1
69 allows the user to configure dynamically the driver callbacks.
70 Use Function @ref HAL_ETH_RegisterCallback() to register an interrupt callback.
72 Function @ref HAL_ETH_RegisterCallback() allows to register following callbacks:
73 (+) TxCpltCallback : Tx Complete Callback.
74 (+) RxCpltCallback : Rx Complete Callback.
75 (+) DMAErrorCallback : DMA Error Callback.
76 (+) MspInitCallback : MspInit Callback.
77 (+) MspDeInitCallback: MspDeInit Callback.
79 This function takes as parameters the HAL peripheral handle, the Callback ID
80 and a pointer to the user callback function.
82 Use function @ref HAL_ETH_UnRegisterCallback() to reset a callback to the default
83 weak function.
84 @ref HAL_ETH_UnRegisterCallback takes as parameters the HAL peripheral handle,
85 and the Callback ID.
86 This function allows to reset following callbacks:
87 (+) TxCpltCallback : Tx Complete Callback.
88 (+) RxCpltCallback : Rx Complete Callback.
89 (+) DMAErrorCallback : DMA Error Callback.
90 (+) MspInitCallback : MspInit Callback.
91 (+) MspDeInitCallback: MspDeInit Callback.
93 By default, after the HAL_ETH_Init and when the state is HAL_ETH_STATE_RESET
94 all callbacks are set to the corresponding weak functions:
95 examples @ref HAL_ETH_TxCpltCallback(), @ref HAL_ETH_RxCpltCallback().
96 Exception done for MspInit and MspDeInit functions that are
97 reset to the legacy weak function in the HAL_ETH_Init/ @ref HAL_ETH_DeInit only when
98 these callbacks are null (not registered beforehand).
99 if not, MspInit or MspDeInit are not null, the HAL_ETH_Init/ @ref HAL_ETH_DeInit
100 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
102 Callbacks can be registered/unregistered in HAL_ETH_STATE_READY state only.
103 Exception done MspInit/MspDeInit that can be registered/unregistered
104 in HAL_ETH_STATE_READY or HAL_ETH_STATE_RESET state,
105 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
106 In that case first register the MspInit/MspDeInit user callbacks
107 using @ref HAL_ETH_RegisterCallback() before calling @ref HAL_ETH_DeInit
108 or HAL_ETH_Init function.
110 When The compilation define USE_HAL_ETH_REGISTER_CALLBACKS is set to 0 or
111 not defined, the callback registration feature is not available and all callbacks
112 are set to the corresponding weak functions.
114 @endverbatim
115 ******************************************************************************
116 * @attention
118 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
119 * All rights reserved.</center></h2>
121 * This software component is licensed by ST under BSD 3-Clause license,
122 * the "License"; You may not use this file except in compliance with the
123 * License. You may obtain a copy of the License at:
124 * opensource.org/licenses/BSD-3-Clause
126 ******************************************************************************
129 /* Includes ------------------------------------------------------------------*/
130 #include "stm32f7xx_hal.h"
132 /** @addtogroup STM32F7xx_HAL_Driver
133 * @{
136 /** @defgroup ETH ETH
137 * @brief ETH HAL module driver
138 * @{
141 #ifdef HAL_ETH_MODULE_ENABLED
142 #if defined (ETH)
144 /* Private typedef -----------------------------------------------------------*/
145 /* Private define ------------------------------------------------------------*/
146 /** @defgroup ETH_Private_Constants ETH Private Constants
147 * @{
149 #define ETH_TIMEOUT_SWRESET ((uint32_t)500)
150 #define ETH_TIMEOUT_LINKED_STATE ((uint32_t)5000)
151 #define ETH_TIMEOUT_AUTONEGO_COMPLETED ((uint32_t)5000)
154 * @}
156 /* Private macro -------------------------------------------------------------*/
157 /* Private variables ---------------------------------------------------------*/
158 /* Private function prototypes -----------------------------------------------*/
159 /** @defgroup ETH_Private_Functions ETH Private Functions
160 * @{
162 static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err);
163 static void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr);
164 static void ETH_MACReceptionEnable(ETH_HandleTypeDef *heth);
165 static void ETH_MACReceptionDisable(ETH_HandleTypeDef *heth);
166 static void ETH_MACTransmissionEnable(ETH_HandleTypeDef *heth);
167 static void ETH_MACTransmissionDisable(ETH_HandleTypeDef *heth);
168 static void ETH_DMATransmissionEnable(ETH_HandleTypeDef *heth);
169 static void ETH_DMATransmissionDisable(ETH_HandleTypeDef *heth);
170 static void ETH_DMAReceptionEnable(ETH_HandleTypeDef *heth);
171 static void ETH_DMAReceptionDisable(ETH_HandleTypeDef *heth);
172 static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef *heth);
173 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
174 static void ETH_InitCallbacksToDefault(ETH_HandleTypeDef *heth);
175 #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
178 * @}
180 /* Private functions ---------------------------------------------------------*/
182 /** @defgroup ETH_Exported_Functions ETH Exported Functions
183 * @{
186 /** @defgroup ETH_Exported_Functions_Group1 Initialization and de-initialization functions
187 * @brief Initialization and Configuration functions
189 @verbatim
190 ===============================================================================
191 ##### Initialization and de-initialization functions #####
192 ===============================================================================
193 [..] This section provides functions allowing to:
194 (+) Initialize and configure the Ethernet peripheral
195 (+) De-initialize the Ethernet peripheral
197 @endverbatim
198 * @{
202 * @brief Initializes the Ethernet MAC and DMA according to default
203 * parameters.
204 * @param heth pointer to a ETH_HandleTypeDef structure that contains
205 * the configuration information for ETHERNET module
206 * @retval HAL status
208 HAL_StatusTypeDef HAL_ETH_Init(ETH_HandleTypeDef *heth)
210 uint32_t tempreg = 0, phyreg = 0;
211 uint32_t hclk = 60000000;
212 uint32_t tickstart = 0;
213 uint32_t err = ETH_SUCCESS;
215 /* Check the ETH peripheral state */
216 if(heth == NULL)
218 return HAL_ERROR;
221 /* Check parameters */
222 assert_param(IS_ETH_AUTONEGOTIATION(heth->Init.AutoNegotiation));
223 assert_param(IS_ETH_RX_MODE(heth->Init.RxMode));
224 assert_param(IS_ETH_CHECKSUM_MODE(heth->Init.ChecksumMode));
225 assert_param(IS_ETH_MEDIA_INTERFACE(heth->Init.MediaInterface));
227 if(heth->State == HAL_ETH_STATE_RESET)
229 /* Allocate lock resource and initialize it */
230 heth->Lock = HAL_UNLOCKED;
231 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
232 ETH_InitCallbacksToDefault(heth);
234 if(heth->MspInitCallback == NULL)
236 /* Init the low level hardware : GPIO, CLOCK, NVIC. */
237 heth->MspInitCallback = HAL_ETH_MspInit;
239 heth->MspInitCallback(heth);
241 #else
242 /* Init the low level hardware : GPIO, CLOCK, NVIC. */
243 HAL_ETH_MspInit(heth);
244 #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
247 /* Enable SYSCFG Clock */
248 __HAL_RCC_SYSCFG_CLK_ENABLE();
250 /* Select MII or RMII Mode*/
251 SYSCFG->PMC &= ~(SYSCFG_PMC_MII_RMII_SEL);
252 SYSCFG->PMC |= (uint32_t)heth->Init.MediaInterface;
254 /* Ethernet Software reset */
255 /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
256 /* After reset all the registers holds their respective reset values */
257 (heth->Instance)->DMABMR |= ETH_DMABMR_SR;
259 /* Get tick */
260 tickstart = HAL_GetTick();
262 /* Wait for software reset */
263 while (((heth->Instance)->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)
265 /* Check for the Timeout */
266 if((HAL_GetTick() - tickstart ) > ETH_TIMEOUT_SWRESET)
268 heth->State= HAL_ETH_STATE_TIMEOUT;
270 /* Process Unlocked */
271 __HAL_UNLOCK(heth);
273 /* Note: The SWR is not performed if the ETH_RX_CLK or the ETH_TX_CLK are
274 not available, please check your external PHY or the IO configuration */
276 return HAL_TIMEOUT;
280 /*-------------------------------- MAC Initialization ----------------------*/
281 /* Get the ETHERNET MACMIIAR value */
282 tempreg = (heth->Instance)->MACMIIAR;
283 /* Clear CSR Clock Range CR[2:0] bits */
284 tempreg &= ETH_MACMIIAR_CR_MASK;
286 /* Get hclk frequency value */
287 hclk = HAL_RCC_GetHCLKFreq();
289 /* Set CR bits depending on hclk value */
290 if((hclk >= 20000000)&&(hclk < 35000000))
292 /* CSR Clock Range between 20-35 MHz */
293 tempreg |= (uint32_t)ETH_MACMIIAR_CR_Div16;
295 else if((hclk >= 35000000)&&(hclk < 60000000))
297 /* CSR Clock Range between 35-60 MHz */
298 tempreg |= (uint32_t)ETH_MACMIIAR_CR_Div26;
300 else if((hclk >= 60000000)&&(hclk < 100000000))
302 /* CSR Clock Range between 60-100 MHz */
303 tempreg |= (uint32_t)ETH_MACMIIAR_CR_Div42;
305 else if((hclk >= 100000000)&&(hclk < 150000000))
307 /* CSR Clock Range between 100-150 MHz */
308 tempreg |= (uint32_t)ETH_MACMIIAR_CR_Div62;
310 else /* ((hclk >= 150000000)&&(hclk <= 216000000)) */
312 /* CSR Clock Range between 150-216 MHz */
313 tempreg |= (uint32_t)ETH_MACMIIAR_CR_Div102;
316 /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
317 (heth->Instance)->MACMIIAR = (uint32_t)tempreg;
319 /*-------------------- PHY initialization and configuration ----------------*/
320 /* Put the PHY in reset mode */
321 if((HAL_ETH_WritePHYRegister(heth, PHY_BCR, PHY_RESET)) != HAL_OK)
323 /* In case of write timeout */
324 err = ETH_ERROR;
326 /* Config MAC and DMA */
327 ETH_MACDMAConfig(heth, err);
329 /* Set the ETH peripheral state to READY */
330 heth->State = HAL_ETH_STATE_READY;
332 /* Return HAL_ERROR */
333 return HAL_ERROR;
336 /* Delay to assure PHY reset */
337 HAL_Delay(PHY_RESET_DELAY);
339 if((heth->Init).AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE)
341 /* Get tick */
342 tickstart = HAL_GetTick();
344 /* We wait for linked status */
347 HAL_ETH_ReadPHYRegister(heth, PHY_BSR, &phyreg);
349 /* Check for the Timeout */
350 if((HAL_GetTick() - tickstart ) > ETH_TIMEOUT_LINKED_STATE)
352 /* In case of write timeout */
353 err = ETH_ERROR;
355 /* Config MAC and DMA */
356 ETH_MACDMAConfig(heth, err);
358 heth->State= HAL_ETH_STATE_READY;
360 /* Process Unlocked */
361 __HAL_UNLOCK(heth);
363 return HAL_TIMEOUT;
365 } while (((phyreg & PHY_LINKED_STATUS) != PHY_LINKED_STATUS));
368 /* Enable Auto-Negotiation */
369 if((HAL_ETH_WritePHYRegister(heth, PHY_BCR, PHY_AUTONEGOTIATION)) != HAL_OK)
371 /* In case of write timeout */
372 err = ETH_ERROR;
374 /* Config MAC and DMA */
375 ETH_MACDMAConfig(heth, err);
377 /* Set the ETH peripheral state to READY */
378 heth->State = HAL_ETH_STATE_READY;
380 /* Return HAL_ERROR */
381 return HAL_ERROR;
384 /* Get tick */
385 tickstart = HAL_GetTick();
387 /* Wait until the auto-negotiation will be completed */
390 HAL_ETH_ReadPHYRegister(heth, PHY_BSR, &phyreg);
392 /* Check for the Timeout */
393 if((HAL_GetTick() - tickstart ) > ETH_TIMEOUT_AUTONEGO_COMPLETED)
395 /* In case of write timeout */
396 err = ETH_ERROR;
398 /* Config MAC and DMA */
399 ETH_MACDMAConfig(heth, err);
401 heth->State= HAL_ETH_STATE_READY;
403 /* Process Unlocked */
404 __HAL_UNLOCK(heth);
406 return HAL_TIMEOUT;
409 } while (((phyreg & PHY_AUTONEGO_COMPLETE) != PHY_AUTONEGO_COMPLETE));
411 /* Read the result of the auto-negotiation */
412 if((HAL_ETH_ReadPHYRegister(heth, PHY_SR, &phyreg)) != HAL_OK)
414 /* In case of write timeout */
415 err = ETH_ERROR;
417 /* Config MAC and DMA */
418 ETH_MACDMAConfig(heth, err);
420 /* Set the ETH peripheral state to READY */
421 heth->State = HAL_ETH_STATE_READY;
423 /* Return HAL_ERROR */
424 return HAL_ERROR;
427 /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
428 if((phyreg & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
430 /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
431 (heth->Init).DuplexMode = ETH_MODE_FULLDUPLEX;
433 else
435 /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
436 (heth->Init).DuplexMode = ETH_MODE_HALFDUPLEX;
438 /* Configure the MAC with the speed fixed by the auto-negotiation process */
439 if((phyreg & PHY_SPEED_STATUS) == PHY_SPEED_STATUS)
441 /* Set Ethernet speed to 10M following the auto-negotiation */
442 (heth->Init).Speed = ETH_SPEED_10M;
444 else
446 /* Set Ethernet speed to 100M following the auto-negotiation */
447 (heth->Init).Speed = ETH_SPEED_100M;
450 else /* AutoNegotiation Disable */
452 /* Check parameters */
453 assert_param(IS_ETH_SPEED(heth->Init.Speed));
454 assert_param(IS_ETH_DUPLEX_MODE(heth->Init.DuplexMode));
456 /* Set MAC Speed and Duplex Mode */
457 if(HAL_ETH_WritePHYRegister(heth, PHY_BCR, ((uint16_t)((heth->Init).DuplexMode >> 3) |
458 (uint16_t)((heth->Init).Speed >> 1))) != HAL_OK)
460 /* In case of write timeout */
461 err = ETH_ERROR;
463 /* Config MAC and DMA */
464 ETH_MACDMAConfig(heth, err);
466 /* Set the ETH peripheral state to READY */
467 heth->State = HAL_ETH_STATE_READY;
469 /* Return HAL_ERROR */
470 return HAL_ERROR;
473 /* Delay to assure PHY configuration */
474 HAL_Delay(PHY_CONFIG_DELAY);
477 /* Config MAC and DMA */
478 ETH_MACDMAConfig(heth, err);
480 /* Set ETH HAL State to Ready */
481 heth->State= HAL_ETH_STATE_READY;
483 /* Return function status */
484 return HAL_OK;
488 * @brief De-Initializes the ETH peripheral.
489 * @param heth pointer to a ETH_HandleTypeDef structure that contains
490 * the configuration information for ETHERNET module
491 * @retval HAL status
493 HAL_StatusTypeDef HAL_ETH_DeInit(ETH_HandleTypeDef *heth)
495 /* Set the ETH peripheral state to BUSY */
496 heth->State = HAL_ETH_STATE_BUSY;
498 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
499 if(heth->MspDeInitCallback == NULL)
501 heth->MspDeInitCallback = HAL_ETH_MspDeInit;
503 /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */
504 heth->MspDeInitCallback(heth);
505 #else
506 /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */
507 HAL_ETH_MspDeInit(heth);
508 #endif
510 /* Set ETH HAL state to Disabled */
511 heth->State= HAL_ETH_STATE_RESET;
513 /* Release Lock */
514 __HAL_UNLOCK(heth);
516 /* Return function status */
517 return HAL_OK;
521 * @brief Initializes the DMA Tx descriptors in chain mode.
522 * @param heth pointer to a ETH_HandleTypeDef structure that contains
523 * the configuration information for ETHERNET module
524 * @param DMATxDescTab Pointer to the first Tx desc list
525 * @param TxBuff Pointer to the first TxBuffer list
526 * @param TxBuffCount Number of the used Tx desc in the list
527 * @retval HAL status
529 HAL_StatusTypeDef HAL_ETH_DMATxDescListInit(ETH_HandleTypeDef *heth, ETH_DMADescTypeDef *DMATxDescTab, uint8_t *TxBuff, uint32_t TxBuffCount)
531 uint32_t i = 0;
532 ETH_DMADescTypeDef *dmatxdesc;
534 /* Process Locked */
535 __HAL_LOCK(heth);
537 /* Set the ETH peripheral state to BUSY */
538 heth->State = HAL_ETH_STATE_BUSY;
540 /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
541 heth->TxDesc = DMATxDescTab;
543 /* Fill each DMATxDesc descriptor with the right values */
544 for(i=0; i < TxBuffCount; i++)
546 /* Get the pointer on the ith member of the Tx Desc list */
547 dmatxdesc = DMATxDescTab + i;
549 /* Set Second Address Chained bit */
550 dmatxdesc->Status = ETH_DMATXDESC_TCH;
552 /* Set Buffer1 address pointer */
553 dmatxdesc->Buffer1Addr = (uint32_t)(&TxBuff[i*ETH_TX_BUF_SIZE]);
555 if ((heth->Init).ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)
557 /* Set the DMA Tx descriptors checksum insertion */
558 dmatxdesc->Status |= ETH_DMATXDESC_CHECKSUMTCPUDPICMPFULL;
561 /* Initialize the next descriptor with the Next Descriptor Polling Enable */
562 if(i < (TxBuffCount-1))
564 /* Set next descriptor address register with next descriptor base address */
565 dmatxdesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1);
567 else
569 /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
570 dmatxdesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
574 /* Set Transmit Descriptor List Address Register */
575 (heth->Instance)->DMATDLAR = (uint32_t) DMATxDescTab;
577 /* Set ETH HAL State to Ready */
578 heth->State= HAL_ETH_STATE_READY;
580 /* Process Unlocked */
581 __HAL_UNLOCK(heth);
583 /* Return function status */
584 return HAL_OK;
588 * @brief Initializes the DMA Rx descriptors in chain mode.
589 * @param heth pointer to a ETH_HandleTypeDef structure that contains
590 * the configuration information for ETHERNET module
591 * @param DMARxDescTab Pointer to the first Rx desc list
592 * @param RxBuff Pointer to the first RxBuffer list
593 * @param RxBuffCount Number of the used Rx desc in the list
594 * @retval HAL status
596 HAL_StatusTypeDef HAL_ETH_DMARxDescListInit(ETH_HandleTypeDef *heth, ETH_DMADescTypeDef *DMARxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
598 uint32_t i = 0;
599 ETH_DMADescTypeDef *DMARxDesc;
601 /* Process Locked */
602 __HAL_LOCK(heth);
604 /* Set the ETH peripheral state to BUSY */
605 heth->State = HAL_ETH_STATE_BUSY;
607 /* Set the Ethernet RxDesc pointer with the first one of the DMARxDescTab list */
608 heth->RxDesc = DMARxDescTab;
610 /* Fill each DMARxDesc descriptor with the right values */
611 for(i=0; i < RxBuffCount; i++)
613 /* Get the pointer on the ith member of the Rx Desc list */
614 DMARxDesc = DMARxDescTab+i;
616 /* Set Own bit of the Rx descriptor Status */
617 DMARxDesc->Status = ETH_DMARXDESC_OWN;
619 /* Set Buffer1 size and Second Address Chained bit */
620 DMARxDesc->ControlBufferSize = ETH_DMARXDESC_RCH | ETH_RX_BUF_SIZE;
622 /* Set Buffer1 address pointer */
623 DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*ETH_RX_BUF_SIZE]);
625 if((heth->Init).RxMode == ETH_RXINTERRUPT_MODE)
627 /* Enable Ethernet DMA Rx Descriptor interrupt */
628 DMARxDesc->ControlBufferSize &= ~ETH_DMARXDESC_DIC;
631 /* Initialize the next descriptor with the Next Descriptor Polling Enable */
632 if(i < (RxBuffCount-1))
634 /* Set next descriptor address register with next descriptor base address */
635 DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab+i+1);
637 else
639 /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
640 DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
644 /* Set Receive Descriptor List Address Register */
645 (heth->Instance)->DMARDLAR = (uint32_t) DMARxDescTab;
647 /* Set ETH HAL State to Ready */
648 heth->State= HAL_ETH_STATE_READY;
650 /* Process Unlocked */
651 __HAL_UNLOCK(heth);
653 /* Return function status */
654 return HAL_OK;
658 * @brief Initializes the ETH MSP.
659 * @param heth pointer to a ETH_HandleTypeDef structure that contains
660 * the configuration information for ETHERNET module
661 * @retval None
663 __weak void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)
665 /* Prevent unused argument(s) compilation warning */
666 UNUSED(heth);
668 /* NOTE : This function Should not be modified, when the callback is needed,
669 the HAL_ETH_MspInit could be implemented in the user file
674 * @brief DeInitializes ETH MSP.
675 * @param heth pointer to a ETH_HandleTypeDef structure that contains
676 * the configuration information for ETHERNET module
677 * @retval None
679 __weak void HAL_ETH_MspDeInit(ETH_HandleTypeDef *heth)
681 /* Prevent unused argument(s) compilation warning */
682 UNUSED(heth);
684 /* NOTE : This function Should not be modified, when the callback is needed,
685 the HAL_ETH_MspDeInit could be implemented in the user file
689 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
691 * @brief Register a User ETH Callback
692 * To be used instead of the weak predefined callback
693 * @param heth eth handle
694 * @param CallbackID ID of the callback to be registered
695 * This parameter can be one of the following values:
696 * @arg @ref HAL_ETH_TX_COMPLETE_CB_ID Tx Complete Callback ID
697 * @arg @ref HAL_ETH_RX_COMPLETE_CB_ID Rx Complete Callback ID
698 * @arg @ref HAL_ETH_DMA_ERROR_CB_ID DMA Error Callback ID
699 * @arg @ref HAL_ETH_MSPINIT_CB_ID MspInit callback ID
700 * @arg @ref HAL_ETH_MSPDEINIT_CB_ID MspDeInit callback ID
701 * @param pCallback pointer to the Callback function
702 * @retval status
704 HAL_StatusTypeDef HAL_ETH_RegisterCallback(ETH_HandleTypeDef *heth, HAL_ETH_CallbackIDTypeDef CallbackID, pETH_CallbackTypeDef pCallback)
706 HAL_StatusTypeDef status = HAL_OK;
708 if(pCallback == NULL)
710 return HAL_ERROR;
712 /* Process locked */
713 __HAL_LOCK(heth);
715 if(heth->State == HAL_ETH_STATE_READY)
717 switch (CallbackID)
719 case HAL_ETH_TX_COMPLETE_CB_ID :
720 heth->TxCpltCallback = pCallback;
721 break;
723 case HAL_ETH_RX_COMPLETE_CB_ID :
724 heth->RxCpltCallback = pCallback;
725 break;
727 case HAL_ETH_DMA_ERROR_CB_ID :
728 heth->DMAErrorCallback = pCallback;
729 break;
731 case HAL_ETH_MSPINIT_CB_ID :
732 heth->MspInitCallback = pCallback;
733 break;
735 case HAL_ETH_MSPDEINIT_CB_ID :
736 heth->MspDeInitCallback = pCallback;
737 break;
739 default :
740 /* Return error status */
741 status = HAL_ERROR;
742 break;
745 else if(heth->State == HAL_ETH_STATE_RESET)
747 switch (CallbackID)
749 case HAL_ETH_MSPINIT_CB_ID :
750 heth->MspInitCallback = pCallback;
751 break;
753 case HAL_ETH_MSPDEINIT_CB_ID :
754 heth->MspDeInitCallback = pCallback;
755 break;
757 default :
758 /* Return error status */
759 status = HAL_ERROR;
760 break;
763 else
765 /* Return error status */
766 status = HAL_ERROR;
769 /* Release Lock */
770 __HAL_UNLOCK(heth);
772 return status;
776 * @brief Unregister an ETH Callback
777 * ETH callabck is redirected to the weak predefined callback
778 * @param heth eth handle
779 * @param CallbackID ID of the callback to be unregistered
780 * This parameter can be one of the following values:
781 * @arg @ref HAL_ETH_TX_COMPLETE_CB_ID Tx Complete Callback ID
782 * @arg @ref HAL_ETH_RX_COMPLETE_CB_ID Rx Complete Callback ID
783 * @arg @ref HAL_ETH_DMA_ERROR_CB_ID DMA Error Callback ID
784 * @arg @ref HAL_ETH_MSPINIT_CB_ID MspInit callback ID
785 * @arg @ref HAL_ETH_MSPDEINIT_CB_ID MspDeInit callback ID
786 * @retval status
788 HAL_StatusTypeDef HAL_ETH_UnRegisterCallback(ETH_HandleTypeDef *heth, HAL_ETH_CallbackIDTypeDef CallbackID)
790 HAL_StatusTypeDef status = HAL_OK;
792 /* Process locked */
793 __HAL_LOCK(heth);
795 if(heth->State == HAL_ETH_STATE_READY)
797 switch (CallbackID)
799 case HAL_ETH_TX_COMPLETE_CB_ID :
800 heth->TxCpltCallback = HAL_ETH_TxCpltCallback;
801 break;
803 case HAL_ETH_RX_COMPLETE_CB_ID :
804 heth->RxCpltCallback = HAL_ETH_RxCpltCallback;
805 break;
807 case HAL_ETH_DMA_ERROR_CB_ID :
808 heth->DMAErrorCallback = HAL_ETH_ErrorCallback;
809 break;
811 case HAL_ETH_MSPINIT_CB_ID :
812 heth->MspInitCallback = HAL_ETH_MspInit;
813 break;
815 case HAL_ETH_MSPDEINIT_CB_ID :
816 heth->MspDeInitCallback = HAL_ETH_MspDeInit;
817 break;
819 default :
820 /* Return error status */
821 status = HAL_ERROR;
822 break;
825 else if(heth->State == HAL_ETH_STATE_RESET)
827 switch (CallbackID)
829 case HAL_ETH_MSPINIT_CB_ID :
830 heth->MspInitCallback = HAL_ETH_MspInit;
831 break;
833 case HAL_ETH_MSPDEINIT_CB_ID :
834 heth->MspDeInitCallback = HAL_ETH_MspDeInit;
835 break;
837 default :
838 /* Return error status */
839 status = HAL_ERROR;
840 break;
843 else
845 /* Return error status */
846 status = HAL_ERROR;
849 /* Release Lock */
850 __HAL_UNLOCK(heth);
852 return status;
854 #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
857 * @}
860 /** @defgroup ETH_Exported_Functions_Group2 IO operation functions
861 * @brief Data transfers functions
863 @verbatim
864 ==============================================================================
865 ##### IO operation functions #####
866 ==============================================================================
867 [..] This section provides functions allowing to:
868 (+) Transmit a frame
869 HAL_ETH_TransmitFrame();
870 (+) Receive a frame
871 HAL_ETH_GetReceivedFrame();
872 HAL_ETH_GetReceivedFrame_IT();
873 (+) Read from an External PHY register
874 HAL_ETH_ReadPHYRegister();
875 (+) Write to an External PHY register
876 HAL_ETH_WritePHYRegister();
878 @endverbatim
880 * @{
884 * @brief Sends an Ethernet frame.
885 * @param heth pointer to a ETH_HandleTypeDef structure that contains
886 * the configuration information for ETHERNET module
887 * @param FrameLength Amount of data to be sent
888 * @retval HAL status
890 HAL_StatusTypeDef HAL_ETH_TransmitFrame(ETH_HandleTypeDef *heth, uint32_t FrameLength)
892 uint32_t bufcount = 0, size = 0, i = 0;
894 /* Process Locked */
895 __HAL_LOCK(heth);
897 /* Set the ETH peripheral state to BUSY */
898 heth->State = HAL_ETH_STATE_BUSY;
900 if (FrameLength == 0)
902 /* Set ETH HAL state to READY */
903 heth->State = HAL_ETH_STATE_READY;
905 /* Process Unlocked */
906 __HAL_UNLOCK(heth);
908 return HAL_ERROR;
911 /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
912 if(((heth->TxDesc)->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
914 /* OWN bit set */
915 heth->State = HAL_ETH_STATE_BUSY_TX;
917 /* Process Unlocked */
918 __HAL_UNLOCK(heth);
920 return HAL_ERROR;
923 /* Get the number of needed Tx buffers for the current frame */
924 if (FrameLength > ETH_TX_BUF_SIZE)
926 bufcount = FrameLength/ETH_TX_BUF_SIZE;
927 if (FrameLength % ETH_TX_BUF_SIZE)
929 bufcount++;
932 else
934 bufcount = 1;
936 if (bufcount == 1)
938 /* Set LAST and FIRST segment */
939 heth->TxDesc->Status |=ETH_DMATXDESC_FS|ETH_DMATXDESC_LS;
940 /* Set frame size */
941 heth->TxDesc->ControlBufferSize = (FrameLength & ETH_DMATXDESC_TBS1);
942 /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
943 heth->TxDesc->Status |= ETH_DMATXDESC_OWN;
944 /* Point to next descriptor */
945 heth->TxDesc= (ETH_DMADescTypeDef *)(heth->TxDesc->Buffer2NextDescAddr);
947 else
949 for (i=0; i< bufcount; i++)
951 /* Clear FIRST and LAST segment bits */
952 heth->TxDesc->Status &= ~(ETH_DMATXDESC_FS | ETH_DMATXDESC_LS);
954 if (i == 0)
956 /* Setting the first segment bit */
957 heth->TxDesc->Status |= ETH_DMATXDESC_FS;
960 /* Program size */
961 heth->TxDesc->ControlBufferSize = (ETH_TX_BUF_SIZE & ETH_DMATXDESC_TBS1);
963 if (i == (bufcount-1))
965 /* Setting the last segment bit */
966 heth->TxDesc->Status |= ETH_DMATXDESC_LS;
967 size = FrameLength - (bufcount-1)*ETH_TX_BUF_SIZE;
968 heth->TxDesc->ControlBufferSize = (size & ETH_DMATXDESC_TBS1);
971 /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
972 heth->TxDesc->Status |= ETH_DMATXDESC_OWN;
973 /* point to next descriptor */
974 heth->TxDesc = (ETH_DMADescTypeDef *)(heth->TxDesc->Buffer2NextDescAddr);
978 /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
979 if (((heth->Instance)->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
981 /* Clear TBUS ETHERNET DMA flag */
982 (heth->Instance)->DMASR = ETH_DMASR_TBUS;
983 /* Resume DMA transmission*/
984 (heth->Instance)->DMATPDR = 0;
987 /* Set ETH HAL State to Ready */
988 heth->State = HAL_ETH_STATE_READY;
990 /* Process Unlocked */
991 __HAL_UNLOCK(heth);
993 /* Return function status */
994 return HAL_OK;
998 * @brief Checks for received frames.
999 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1000 * the configuration information for ETHERNET module
1001 * @retval HAL status
1003 HAL_StatusTypeDef HAL_ETH_GetReceivedFrame(ETH_HandleTypeDef *heth)
1005 uint32_t framelength = 0;
1007 /* Process Locked */
1008 __HAL_LOCK(heth);
1010 /* Check the ETH state to BUSY */
1011 heth->State = HAL_ETH_STATE_BUSY;
1013 /* Check if segment is not owned by DMA */
1014 /* (((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET) && ((heth->RxDesc->Status & ETH_DMARXDESC_LS) != (uint32_t)RESET)) */
1015 if(((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET))
1017 /* Check if last segment */
1018 if(((heth->RxDesc->Status & ETH_DMARXDESC_LS) != (uint32_t)RESET))
1020 /* increment segment count */
1021 (heth->RxFrameInfos).SegCount++;
1023 /* Check if last segment is first segment: one segment contains the frame */
1024 if ((heth->RxFrameInfos).SegCount == 1)
1026 (heth->RxFrameInfos).FSRxDesc =heth->RxDesc;
1029 heth->RxFrameInfos.LSRxDesc = heth->RxDesc;
1031 /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
1032 framelength = (((heth->RxDesc)->Status & ETH_DMARXDESC_FL) >> ETH_DMARXDESC_FRAMELENGTHSHIFT) - 4;
1033 heth->RxFrameInfos.length = framelength;
1035 /* Get the address of the buffer start address */
1036 heth->RxFrameInfos.buffer = ((heth->RxFrameInfos).FSRxDesc)->Buffer1Addr;
1037 /* point to next descriptor */
1038 heth->RxDesc = (ETH_DMADescTypeDef*) ((heth->RxDesc)->Buffer2NextDescAddr);
1040 /* Set HAL State to Ready */
1041 heth->State = HAL_ETH_STATE_READY;
1043 /* Process Unlocked */
1044 __HAL_UNLOCK(heth);
1046 /* Return function status */
1047 return HAL_OK;
1049 /* Check if first segment */
1050 else if((heth->RxDesc->Status & ETH_DMARXDESC_FS) != (uint32_t)RESET)
1052 (heth->RxFrameInfos).FSRxDesc = heth->RxDesc;
1053 (heth->RxFrameInfos).LSRxDesc = NULL;
1054 (heth->RxFrameInfos).SegCount = 1;
1055 /* Point to next descriptor */
1056 heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
1058 /* Check if intermediate segment */
1059 else
1061 (heth->RxFrameInfos).SegCount++;
1062 /* Point to next descriptor */
1063 heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
1067 /* Set ETH HAL State to Ready */
1068 heth->State = HAL_ETH_STATE_READY;
1070 /* Process Unlocked */
1071 __HAL_UNLOCK(heth);
1073 /* Return function status */
1074 return HAL_ERROR;
1078 * @brief Gets the Received frame in interrupt mode.
1079 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1080 * the configuration information for ETHERNET module
1081 * @retval HAL status
1083 HAL_StatusTypeDef HAL_ETH_GetReceivedFrame_IT(ETH_HandleTypeDef *heth)
1085 uint32_t descriptorscancounter = 0;
1087 /* Process Locked */
1088 __HAL_LOCK(heth);
1090 /* Set ETH HAL State to BUSY */
1091 heth->State = HAL_ETH_STATE_BUSY;
1093 /* Scan descriptors owned by CPU */
1094 while (((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET) && (descriptorscancounter < ETH_RXBUFNB))
1096 /* Just for security */
1097 descriptorscancounter++;
1099 /* Check if first segment in frame */
1100 /* ((heth->RxDesc->Status & ETH_DMARXDESC_FS) != (uint32_t)RESET) && ((heth->RxDesc->Status & ETH_DMARXDESC_LS) == (uint32_t)RESET)) */
1101 if((heth->RxDesc->Status & (ETH_DMARXDESC_FS | ETH_DMARXDESC_LS)) == (uint32_t)ETH_DMARXDESC_FS)
1103 heth->RxFrameInfos.FSRxDesc = heth->RxDesc;
1104 heth->RxFrameInfos.SegCount = 1;
1105 /* Point to next descriptor */
1106 heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
1108 /* Check if intermediate segment */
1109 /* ((heth->RxDesc->Status & ETH_DMARXDESC_LS) == (uint32_t)RESET)&& ((heth->RxDesc->Status & ETH_DMARXDESC_FS) == (uint32_t)RESET)) */
1110 else if ((heth->RxDesc->Status & (ETH_DMARXDESC_LS | ETH_DMARXDESC_FS)) == (uint32_t)RESET)
1112 /* Increment segment count */
1113 (heth->RxFrameInfos.SegCount)++;
1114 /* Point to next descriptor */
1115 heth->RxDesc = (ETH_DMADescTypeDef*)(heth->RxDesc->Buffer2NextDescAddr);
1117 /* Should be last segment */
1118 else
1120 /* Last segment */
1121 heth->RxFrameInfos.LSRxDesc = heth->RxDesc;
1123 /* Increment segment count */
1124 (heth->RxFrameInfos.SegCount)++;
1126 /* Check if last segment is first segment: one segment contains the frame */
1127 if ((heth->RxFrameInfos.SegCount) == 1)
1129 heth->RxFrameInfos.FSRxDesc = heth->RxDesc;
1132 /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
1133 heth->RxFrameInfos.length = (((heth->RxDesc)->Status & ETH_DMARXDESC_FL) >> ETH_DMARXDESC_FRAMELENGTHSHIFT) - 4;
1135 /* Get the address of the buffer start address */
1136 heth->RxFrameInfos.buffer =((heth->RxFrameInfos).FSRxDesc)->Buffer1Addr;
1138 /* Point to next descriptor */
1139 heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
1141 /* Set HAL State to Ready */
1142 heth->State = HAL_ETH_STATE_READY;
1144 /* Process Unlocked */
1145 __HAL_UNLOCK(heth);
1147 /* Return function status */
1148 return HAL_OK;
1152 /* Set HAL State to Ready */
1153 heth->State = HAL_ETH_STATE_READY;
1155 /* Process Unlocked */
1156 __HAL_UNLOCK(heth);
1158 /* Return function status */
1159 return HAL_ERROR;
1163 * @brief This function handles ETH interrupt request.
1164 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1165 * the configuration information for ETHERNET module
1166 * @retval HAL status
1168 void HAL_ETH_IRQHandler(ETH_HandleTypeDef *heth)
1170 /* Frame received */
1171 if (__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_R))
1173 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
1174 /*Call registered Receive complete callback*/
1175 heth->RxCpltCallback(heth);
1176 #else
1177 /* Receive complete callback */
1178 HAL_ETH_RxCpltCallback(heth);
1179 #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
1181 /* Clear the Eth DMA Rx IT pending bits */
1182 __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_R);
1184 /* Set HAL State to Ready */
1185 heth->State = HAL_ETH_STATE_READY;
1187 /* Process Unlocked */
1188 __HAL_UNLOCK(heth);
1191 /* Frame transmitted */
1192 else if (__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_T))
1194 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
1195 /* Call resgistered Transfer complete callback*/
1196 heth->TxCpltCallback(heth);
1197 #else
1198 /* Transfer complete callback */
1199 HAL_ETH_TxCpltCallback(heth);
1200 #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
1202 /* Clear the Eth DMA Tx IT pending bits */
1203 __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_T);
1205 /* Set HAL State to Ready */
1206 heth->State = HAL_ETH_STATE_READY;
1208 /* Process Unlocked */
1209 __HAL_UNLOCK(heth);
1212 /* Clear the interrupt flags */
1213 __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_NIS);
1215 /* ETH DMA Error */
1216 if(__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_AIS))
1218 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
1219 heth->DMAErrorCallback(heth);
1220 #else
1221 /* Ethernet Error callback */
1222 HAL_ETH_ErrorCallback(heth);
1223 #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
1225 /* Clear the interrupt flags */
1226 __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_FLAG_AIS);
1228 /* Set HAL State to Ready */
1229 heth->State = HAL_ETH_STATE_READY;
1231 /* Process Unlocked */
1232 __HAL_UNLOCK(heth);
1237 * @brief Tx Transfer completed callbacks.
1238 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1239 * the configuration information for ETHERNET module
1240 * @retval None
1242 __weak void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth)
1244 /* Prevent unused argument(s) compilation warning */
1245 UNUSED(heth);
1247 /* NOTE : This function Should not be modified, when the callback is needed,
1248 the HAL_ETH_TxCpltCallback could be implemented in the user file
1253 * @brief Rx Transfer completed callbacks.
1254 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1255 * the configuration information for ETHERNET module
1256 * @retval None
1258 __weak void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
1260 /* Prevent unused argument(s) compilation warning */
1261 UNUSED(heth);
1263 /* NOTE : This function Should not be modified, when the callback is needed,
1264 the HAL_ETH_RxCpltCallback could be implemented in the user file
1269 * @brief Ethernet transfer error callbacks
1270 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1271 * the configuration information for ETHERNET module
1272 * @retval None
1274 __weak void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
1276 /* Prevent unused argument(s) compilation warning */
1277 UNUSED(heth);
1279 /* NOTE : This function Should not be modified, when the callback is needed,
1280 the HAL_ETH_ErrorCallback could be implemented in the user file
1285 * @brief Reads a PHY register
1286 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1287 * the configuration information for ETHERNET module
1288 * @param PHYReg PHY register address, is the index of one of the 32 PHY register.
1289 * This parameter can be one of the following values:
1290 * PHY_BCR: Transceiver Basic Control Register,
1291 * PHY_BSR: Transceiver Basic Status Register.
1292 * More PHY register could be read depending on the used PHY
1293 * @param RegValue PHY register value
1294 * @retval HAL status
1296 HAL_StatusTypeDef HAL_ETH_ReadPHYRegister(ETH_HandleTypeDef *heth, uint16_t PHYReg, uint32_t *RegValue)
1298 uint32_t tmpreg = 0;
1299 uint32_t tickstart = 0;
1301 /* Check parameters */
1302 assert_param(IS_ETH_PHY_ADDRESS(heth->Init.PhyAddress));
1304 /* Check the ETH peripheral state */
1305 if(heth->State == HAL_ETH_STATE_BUSY_RD)
1307 return HAL_BUSY;
1309 /* Set ETH HAL State to BUSY_RD */
1310 heth->State = HAL_ETH_STATE_BUSY_RD;
1312 /* Get the ETHERNET MACMIIAR value */
1313 tmpreg = heth->Instance->MACMIIAR;
1315 /* Keep only the CSR Clock Range CR[2:0] bits value */
1316 tmpreg &= ~ETH_MACMIIAR_CR_MASK;
1318 /* Prepare the MII address register value */
1319 tmpreg |=(((uint32_t)heth->Init.PhyAddress << 11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
1320 tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
1321 tmpreg &= ~ETH_MACMIIAR_MW; /* Set the read mode */
1322 tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
1324 /* Write the result value into the MII Address register */
1325 heth->Instance->MACMIIAR = tmpreg;
1327 /* Get tick */
1328 tickstart = HAL_GetTick();
1330 /* Check for the Busy flag */
1331 while((tmpreg & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB)
1333 /* Check for the Timeout */
1334 if((HAL_GetTick() - tickstart ) > PHY_READ_TO)
1336 heth->State= HAL_ETH_STATE_READY;
1338 /* Process Unlocked */
1339 __HAL_UNLOCK(heth);
1341 return HAL_TIMEOUT;
1344 tmpreg = heth->Instance->MACMIIAR;
1347 /* Get MACMIIDR value */
1348 *RegValue = (uint16_t)(heth->Instance->MACMIIDR);
1350 /* Set ETH HAL State to READY */
1351 heth->State = HAL_ETH_STATE_READY;
1353 /* Return function status */
1354 return HAL_OK;
1358 * @brief Writes to a PHY register.
1359 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1360 * the configuration information for ETHERNET module
1361 * @param PHYReg PHY register address, is the index of one of the 32 PHY register.
1362 * This parameter can be one of the following values:
1363 * PHY_BCR: Transceiver Control Register.
1364 * More PHY register could be written depending on the used PHY
1365 * @param RegValue the value to write
1366 * @retval HAL status
1368 HAL_StatusTypeDef HAL_ETH_WritePHYRegister(ETH_HandleTypeDef *heth, uint16_t PHYReg, uint32_t RegValue)
1370 uint32_t tmpreg = 0;
1371 uint32_t tickstart = 0;
1373 /* Check parameters */
1374 assert_param(IS_ETH_PHY_ADDRESS(heth->Init.PhyAddress));
1376 /* Check the ETH peripheral state */
1377 if(heth->State == HAL_ETH_STATE_BUSY_WR)
1379 return HAL_BUSY;
1381 /* Set ETH HAL State to BUSY_WR */
1382 heth->State = HAL_ETH_STATE_BUSY_WR;
1384 /* Get the ETHERNET MACMIIAR value */
1385 tmpreg = heth->Instance->MACMIIAR;
1387 /* Keep only the CSR Clock Range CR[2:0] bits value */
1388 tmpreg &= ~ETH_MACMIIAR_CR_MASK;
1390 /* Prepare the MII register address value */
1391 tmpreg |=(((uint32_t)heth->Init.PhyAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
1392 tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
1393 tmpreg |= ETH_MACMIIAR_MW; /* Set the write mode */
1394 tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
1396 /* Give the value to the MII data register */
1397 heth->Instance->MACMIIDR = (uint16_t)RegValue;
1399 /* Write the result value into the MII Address register */
1400 heth->Instance->MACMIIAR = tmpreg;
1402 /* Get tick */
1403 tickstart = HAL_GetTick();
1405 /* Check for the Busy flag */
1406 while((tmpreg & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB)
1408 /* Check for the Timeout */
1409 if((HAL_GetTick() - tickstart ) > PHY_WRITE_TO)
1411 heth->State= HAL_ETH_STATE_READY;
1413 /* Process Unlocked */
1414 __HAL_UNLOCK(heth);
1416 return HAL_TIMEOUT;
1419 tmpreg = heth->Instance->MACMIIAR;
1422 /* Set ETH HAL State to READY */
1423 heth->State = HAL_ETH_STATE_READY;
1425 /* Return function status */
1426 return HAL_OK;
1430 * @}
1433 /** @defgroup ETH_Exported_Functions_Group3 Peripheral Control functions
1434 * @brief Peripheral Control functions
1436 @verbatim
1437 ===============================================================================
1438 ##### Peripheral Control functions #####
1439 ===============================================================================
1440 [..] This section provides functions allowing to:
1441 (+) Enable MAC and DMA transmission and reception.
1442 HAL_ETH_Start();
1443 (+) Disable MAC and DMA transmission and reception.
1444 HAL_ETH_Stop();
1445 (+) Set the MAC configuration in runtime mode
1446 HAL_ETH_ConfigMAC();
1447 (+) Set the DMA configuration in runtime mode
1448 HAL_ETH_ConfigDMA();
1450 @endverbatim
1451 * @{
1455 * @brief Enables Ethernet MAC and DMA reception/transmission
1456 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1457 * the configuration information for ETHERNET module
1458 * @retval HAL status
1460 HAL_StatusTypeDef HAL_ETH_Start(ETH_HandleTypeDef *heth)
1462 /* Process Locked */
1463 __HAL_LOCK(heth);
1465 /* Set the ETH peripheral state to BUSY */
1466 heth->State = HAL_ETH_STATE_BUSY;
1468 /* Enable transmit state machine of the MAC for transmission on the MII */
1469 ETH_MACTransmissionEnable(heth);
1471 /* Enable receive state machine of the MAC for reception from the MII */
1472 ETH_MACReceptionEnable(heth);
1474 /* Flush Transmit FIFO */
1475 ETH_FlushTransmitFIFO(heth);
1477 /* Start DMA transmission */
1478 ETH_DMATransmissionEnable(heth);
1480 /* Start DMA reception */
1481 ETH_DMAReceptionEnable(heth);
1483 /* Set the ETH state to READY*/
1484 heth->State= HAL_ETH_STATE_READY;
1486 /* Process Unlocked */
1487 __HAL_UNLOCK(heth);
1489 /* Return function status */
1490 return HAL_OK;
1494 * @brief Stop Ethernet MAC and DMA reception/transmission
1495 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1496 * the configuration information for ETHERNET module
1497 * @retval HAL status
1499 HAL_StatusTypeDef HAL_ETH_Stop(ETH_HandleTypeDef *heth)
1501 /* Process Locked */
1502 __HAL_LOCK(heth);
1504 /* Set the ETH peripheral state to BUSY */
1505 heth->State = HAL_ETH_STATE_BUSY;
1507 /* Stop DMA transmission */
1508 ETH_DMATransmissionDisable(heth);
1510 /* Stop DMA reception */
1511 ETH_DMAReceptionDisable(heth);
1513 /* Disable receive state machine of the MAC for reception from the MII */
1514 ETH_MACReceptionDisable(heth);
1516 /* Flush Transmit FIFO */
1517 ETH_FlushTransmitFIFO(heth);
1519 /* Disable transmit state machine of the MAC for transmission on the MII */
1520 ETH_MACTransmissionDisable(heth);
1522 /* Set the ETH state*/
1523 heth->State = HAL_ETH_STATE_READY;
1525 /* Process Unlocked */
1526 __HAL_UNLOCK(heth);
1528 /* Return function status */
1529 return HAL_OK;
1533 * @brief Set ETH MAC Configuration.
1534 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1535 * the configuration information for ETHERNET module
1536 * @param macconf MAC Configuration structure
1537 * @retval HAL status
1539 HAL_StatusTypeDef HAL_ETH_ConfigMAC(ETH_HandleTypeDef *heth, ETH_MACInitTypeDef *macconf)
1541 uint32_t tmpreg = 0;
1543 /* Process Locked */
1544 __HAL_LOCK(heth);
1546 /* Set the ETH peripheral state to BUSY */
1547 heth->State= HAL_ETH_STATE_BUSY;
1549 assert_param(IS_ETH_SPEED(heth->Init.Speed));
1550 assert_param(IS_ETH_DUPLEX_MODE(heth->Init.DuplexMode));
1552 if (macconf != NULL)
1554 /* Check the parameters */
1555 assert_param(IS_ETH_WATCHDOG(macconf->Watchdog));
1556 assert_param(IS_ETH_JABBER(macconf->Jabber));
1557 assert_param(IS_ETH_INTER_FRAME_GAP(macconf->InterFrameGap));
1558 assert_param(IS_ETH_CARRIER_SENSE(macconf->CarrierSense));
1559 assert_param(IS_ETH_RECEIVE_OWN(macconf->ReceiveOwn));
1560 assert_param(IS_ETH_LOOPBACK_MODE(macconf->LoopbackMode));
1561 assert_param(IS_ETH_CHECKSUM_OFFLOAD(macconf->ChecksumOffload));
1562 assert_param(IS_ETH_RETRY_TRANSMISSION(macconf->RetryTransmission));
1563 assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(macconf->AutomaticPadCRCStrip));
1564 assert_param(IS_ETH_BACKOFF_LIMIT(macconf->BackOffLimit));
1565 assert_param(IS_ETH_DEFERRAL_CHECK(macconf->DeferralCheck));
1566 assert_param(IS_ETH_RECEIVE_ALL(macconf->ReceiveAll));
1567 assert_param(IS_ETH_SOURCE_ADDR_FILTER(macconf->SourceAddrFilter));
1568 assert_param(IS_ETH_CONTROL_FRAMES(macconf->PassControlFrames));
1569 assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(macconf->BroadcastFramesReception));
1570 assert_param(IS_ETH_DESTINATION_ADDR_FILTER(macconf->DestinationAddrFilter));
1571 assert_param(IS_ETH_PROMISCUOUS_MODE(macconf->PromiscuousMode));
1572 assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(macconf->MulticastFramesFilter));
1573 assert_param(IS_ETH_UNICAST_FRAMES_FILTER(macconf->UnicastFramesFilter));
1574 assert_param(IS_ETH_PAUSE_TIME(macconf->PauseTime));
1575 assert_param(IS_ETH_ZEROQUANTA_PAUSE(macconf->ZeroQuantaPause));
1576 assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(macconf->PauseLowThreshold));
1577 assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(macconf->UnicastPauseFrameDetect));
1578 assert_param(IS_ETH_RECEIVE_FLOWCONTROL(macconf->ReceiveFlowControl));
1579 assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(macconf->TransmitFlowControl));
1580 assert_param(IS_ETH_VLAN_TAG_COMPARISON(macconf->VLANTagComparison));
1581 assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(macconf->VLANTagIdentifier));
1583 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1584 /* Get the ETHERNET MACCR value */
1585 tmpreg = (heth->Instance)->MACCR;
1586 /* Clear WD, PCE, PS, TE and RE bits */
1587 tmpreg &= ETH_MACCR_CLEAR_MASK;
1589 tmpreg |= (uint32_t)(macconf->Watchdog |
1590 macconf->Jabber |
1591 macconf->InterFrameGap |
1592 macconf->CarrierSense |
1593 (heth->Init).Speed |
1594 macconf->ReceiveOwn |
1595 macconf->LoopbackMode |
1596 (heth->Init).DuplexMode |
1597 macconf->ChecksumOffload |
1598 macconf->RetryTransmission |
1599 macconf->AutomaticPadCRCStrip |
1600 macconf->BackOffLimit |
1601 macconf->DeferralCheck);
1603 /* Write to ETHERNET MACCR */
1604 (heth->Instance)->MACCR = (uint32_t)tmpreg;
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;
1612 /*----------------------- ETHERNET MACFFR Configuration --------------------*/
1613 /* Write to ETHERNET MACFFR */
1614 (heth->Instance)->MACFFR = (uint32_t)(macconf->ReceiveAll |
1615 macconf->SourceAddrFilter |
1616 macconf->PassControlFrames |
1617 macconf->BroadcastFramesReception |
1618 macconf->DestinationAddrFilter |
1619 macconf->PromiscuousMode |
1620 macconf->MulticastFramesFilter |
1621 macconf->UnicastFramesFilter);
1623 /* Wait until the write operation will be taken into account :
1624 at least four TX_CLK/RX_CLK clock cycles */
1625 tmpreg = (heth->Instance)->MACFFR;
1626 HAL_Delay(ETH_REG_WRITE_DELAY);
1627 (heth->Instance)->MACFFR = tmpreg;
1629 /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
1630 /* Write to ETHERNET MACHTHR */
1631 (heth->Instance)->MACHTHR = (uint32_t)macconf->HashTableHigh;
1633 /* Write to ETHERNET MACHTLR */
1634 (heth->Instance)->MACHTLR = (uint32_t)macconf->HashTableLow;
1635 /*----------------------- ETHERNET MACFCR Configuration --------------------*/
1637 /* Get the ETHERNET MACFCR value */
1638 tmpreg = (heth->Instance)->MACFCR;
1639 /* Clear xx bits */
1640 tmpreg &= ETH_MACFCR_CLEAR_MASK;
1642 tmpreg |= (uint32_t)((macconf->PauseTime << 16) |
1643 macconf->ZeroQuantaPause |
1644 macconf->PauseLowThreshold |
1645 macconf->UnicastPauseFrameDetect |
1646 macconf->ReceiveFlowControl |
1647 macconf->TransmitFlowControl);
1649 /* Write to ETHERNET MACFCR */
1650 (heth->Instance)->MACFCR = (uint32_t)tmpreg;
1652 /* Wait until the write operation will be taken into account :
1653 at least four TX_CLK/RX_CLK clock cycles */
1654 tmpreg = (heth->Instance)->MACFCR;
1655 HAL_Delay(ETH_REG_WRITE_DELAY);
1656 (heth->Instance)->MACFCR = tmpreg;
1658 /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/
1659 (heth->Instance)->MACVLANTR = (uint32_t)(macconf->VLANTagComparison |
1660 macconf->VLANTagIdentifier);
1662 /* Wait until the write operation will be taken into account :
1663 at least four TX_CLK/RX_CLK clock cycles */
1664 tmpreg = (heth->Instance)->MACVLANTR;
1665 HAL_Delay(ETH_REG_WRITE_DELAY);
1666 (heth->Instance)->MACVLANTR = tmpreg;
1668 else /* macconf == NULL : here we just configure Speed and Duplex mode */
1670 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1671 /* Get the ETHERNET MACCR value */
1672 tmpreg = (heth->Instance)->MACCR;
1674 /* Clear FES and DM bits */
1675 tmpreg &= ~((uint32_t)0x00004800);
1677 tmpreg |= (uint32_t)(heth->Init.Speed | heth->Init.DuplexMode);
1679 /* Write to ETHERNET MACCR */
1680 (heth->Instance)->MACCR = (uint32_t)tmpreg;
1682 /* Wait until the write operation will be taken into account:
1683 at least four TX_CLK/RX_CLK clock cycles */
1684 tmpreg = (heth->Instance)->MACCR;
1685 HAL_Delay(ETH_REG_WRITE_DELAY);
1686 (heth->Instance)->MACCR = tmpreg;
1689 /* Set the ETH state to Ready */
1690 heth->State= HAL_ETH_STATE_READY;
1692 /* Process Unlocked */
1693 __HAL_UNLOCK(heth);
1695 /* Return function status */
1696 return HAL_OK;
1700 * @brief Sets ETH DMA Configuration.
1701 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1702 * the configuration information for ETHERNET module
1703 * @param dmaconf DMA Configuration structure
1704 * @retval HAL status
1706 HAL_StatusTypeDef HAL_ETH_ConfigDMA(ETH_HandleTypeDef *heth, ETH_DMAInitTypeDef *dmaconf)
1708 uint32_t tmpreg = 0;
1710 /* Process Locked */
1711 __HAL_LOCK(heth);
1713 /* Set the ETH peripheral state to BUSY */
1714 heth->State= HAL_ETH_STATE_BUSY;
1716 /* Check parameters */
1717 assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(dmaconf->DropTCPIPChecksumErrorFrame));
1718 assert_param(IS_ETH_RECEIVE_STORE_FORWARD(dmaconf->ReceiveStoreForward));
1719 assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(dmaconf->FlushReceivedFrame));
1720 assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(dmaconf->TransmitStoreForward));
1721 assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(dmaconf->TransmitThresholdControl));
1722 assert_param(IS_ETH_FORWARD_ERROR_FRAMES(dmaconf->ForwardErrorFrames));
1723 assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(dmaconf->ForwardUndersizedGoodFrames));
1724 assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(dmaconf->ReceiveThresholdControl));
1725 assert_param(IS_ETH_SECOND_FRAME_OPERATE(dmaconf->SecondFrameOperate));
1726 assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(dmaconf->AddressAlignedBeats));
1727 assert_param(IS_ETH_FIXED_BURST(dmaconf->FixedBurst));
1728 assert_param(IS_ETH_RXDMA_BURST_LENGTH(dmaconf->RxDMABurstLength));
1729 assert_param(IS_ETH_TXDMA_BURST_LENGTH(dmaconf->TxDMABurstLength));
1730 assert_param(IS_ETH_ENHANCED_DESCRIPTOR_FORMAT(dmaconf->EnhancedDescriptorFormat));
1731 assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(dmaconf->DescriptorSkipLength));
1732 assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(dmaconf->DMAArbitration));
1734 /*----------------------- ETHERNET DMAOMR Configuration --------------------*/
1735 /* Get the ETHERNET DMAOMR value */
1736 tmpreg = (heth->Instance)->DMAOMR;
1737 /* Clear xx bits */
1738 tmpreg &= ETH_DMAOMR_CLEAR_MASK;
1740 tmpreg |= (uint32_t)(dmaconf->DropTCPIPChecksumErrorFrame |
1741 dmaconf->ReceiveStoreForward |
1742 dmaconf->FlushReceivedFrame |
1743 dmaconf->TransmitStoreForward |
1744 dmaconf->TransmitThresholdControl |
1745 dmaconf->ForwardErrorFrames |
1746 dmaconf->ForwardUndersizedGoodFrames |
1747 dmaconf->ReceiveThresholdControl |
1748 dmaconf->SecondFrameOperate);
1750 /* Write to ETHERNET DMAOMR */
1751 (heth->Instance)->DMAOMR = (uint32_t)tmpreg;
1753 /* Wait until the write operation will be taken into account:
1754 at least four TX_CLK/RX_CLK clock cycles */
1755 tmpreg = (heth->Instance)->DMAOMR;
1756 HAL_Delay(ETH_REG_WRITE_DELAY);
1757 (heth->Instance)->DMAOMR = tmpreg;
1759 /*----------------------- ETHERNET DMABMR Configuration --------------------*/
1760 (heth->Instance)->DMABMR = (uint32_t)(dmaconf->AddressAlignedBeats |
1761 dmaconf->FixedBurst |
1762 dmaconf->RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
1763 dmaconf->TxDMABurstLength |
1764 dmaconf->EnhancedDescriptorFormat |
1765 (dmaconf->DescriptorSkipLength << 2) |
1766 dmaconf->DMAArbitration |
1767 ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
1769 /* Wait until the write operation will be taken into account:
1770 at least four TX_CLK/RX_CLK clock cycles */
1771 tmpreg = (heth->Instance)->DMABMR;
1772 HAL_Delay(ETH_REG_WRITE_DELAY);
1773 (heth->Instance)->DMABMR = tmpreg;
1775 /* Set the ETH state to Ready */
1776 heth->State= HAL_ETH_STATE_READY;
1778 /* Process Unlocked */
1779 __HAL_UNLOCK(heth);
1781 /* Return function status */
1782 return HAL_OK;
1786 * @}
1789 /** @defgroup ETH_Exported_Functions_Group4 Peripheral State functions
1790 * @brief Peripheral State functions
1792 @verbatim
1793 ===============================================================================
1794 ##### Peripheral State functions #####
1795 ===============================================================================
1796 [..]
1797 This subsection permits to get in run-time the status of the peripheral
1798 and the data flow.
1799 (+) Get the ETH handle state:
1800 HAL_ETH_GetState();
1803 @endverbatim
1804 * @{
1808 * @brief Return the ETH HAL state
1809 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1810 * the configuration information for ETHERNET module
1811 * @retval HAL state
1813 HAL_ETH_StateTypeDef HAL_ETH_GetState(ETH_HandleTypeDef *heth)
1815 /* Return ETH state */
1816 return heth->State;
1820 * @}
1824 * @}
1827 /** @addtogroup ETH_Private_Functions
1828 * @{
1832 * @brief Configures Ethernet MAC and DMA with default parameters.
1833 * @param heth pointer to a ETH_HandleTypeDef structure that contains
1834 * the configuration information for ETHERNET module
1835 * @param err Ethernet Init error
1836 * @retval HAL status
1838 static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err)
1840 ETH_MACInitTypeDef macinit;
1841 ETH_DMAInitTypeDef dmainit;
1842 uint32_t tmpreg = 0;
1844 if (err != ETH_SUCCESS) /* Auto-negotiation failed */
1846 /* Set Ethernet duplex mode to Full-duplex */
1847 (heth->Init).DuplexMode = ETH_MODE_FULLDUPLEX;
1849 /* Set Ethernet speed to 100M */
1850 (heth->Init).Speed = ETH_SPEED_100M;
1853 /* Ethernet MAC default initialization **************************************/
1854 macinit.Watchdog = ETH_WATCHDOG_ENABLE;
1855 macinit.Jabber = ETH_JABBER_ENABLE;
1856 macinit.InterFrameGap = ETH_INTERFRAMEGAP_96BIT;
1857 macinit.CarrierSense = ETH_CARRIERSENCE_ENABLE;
1858 macinit.ReceiveOwn = ETH_RECEIVEOWN_ENABLE;
1859 macinit.LoopbackMode = ETH_LOOPBACKMODE_DISABLE;
1860 if(heth->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)
1862 macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_ENABLE;
1864 else
1866 macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_DISABLE;
1868 macinit.RetryTransmission = ETH_RETRYTRANSMISSION_DISABLE;
1869 macinit.AutomaticPadCRCStrip = ETH_AUTOMATICPADCRCSTRIP_DISABLE;
1870 macinit.BackOffLimit = ETH_BACKOFFLIMIT_10;
1871 macinit.DeferralCheck = ETH_DEFFERRALCHECK_DISABLE;
1872 macinit.ReceiveAll = ETH_RECEIVEAll_DISABLE;
1873 macinit.SourceAddrFilter = ETH_SOURCEADDRFILTER_DISABLE;
1874 macinit.PassControlFrames = ETH_PASSCONTROLFRAMES_BLOCKALL;
1875 macinit.BroadcastFramesReception = ETH_BROADCASTFRAMESRECEPTION_ENABLE;
1876 macinit.DestinationAddrFilter = ETH_DESTINATIONADDRFILTER_NORMAL;
1877 macinit.PromiscuousMode = ETH_PROMISCUOUS_MODE_DISABLE;
1878 macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_PERFECT;
1879 macinit.UnicastFramesFilter = ETH_UNICASTFRAMESFILTER_PERFECT;
1880 macinit.HashTableHigh = 0x0;
1881 macinit.HashTableLow = 0x0;
1882 macinit.PauseTime = 0x0;
1883 macinit.ZeroQuantaPause = ETH_ZEROQUANTAPAUSE_DISABLE;
1884 macinit.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS4;
1885 macinit.UnicastPauseFrameDetect = ETH_UNICASTPAUSEFRAMEDETECT_DISABLE;
1886 macinit.ReceiveFlowControl = ETH_RECEIVEFLOWCONTROL_DISABLE;
1887 macinit.TransmitFlowControl = ETH_TRANSMITFLOWCONTROL_DISABLE;
1888 macinit.VLANTagComparison = ETH_VLANTAGCOMPARISON_16BIT;
1889 macinit.VLANTagIdentifier = 0x0;
1891 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1892 /* Get the ETHERNET MACCR value */
1893 tmpreg = (heth->Instance)->MACCR;
1894 /* Clear WD, PCE, PS, TE and RE bits */
1895 tmpreg &= ETH_MACCR_CLEAR_MASK;
1896 /* Set the WD bit according to ETH Watchdog value */
1897 /* Set the JD: bit according to ETH Jabber value */
1898 /* Set the IFG bit according to ETH InterFrameGap value */
1899 /* Set the DCRS bit according to ETH CarrierSense value */
1900 /* Set the FES bit according to ETH Speed value */
1901 /* Set the DO bit according to ETH ReceiveOwn value */
1902 /* Set the LM bit according to ETH LoopbackMode value */
1903 /* Set the DM bit according to ETH Mode value */
1904 /* Set the IPCO bit according to ETH ChecksumOffload value */
1905 /* Set the DR bit according to ETH RetryTransmission value */
1906 /* Set the ACS bit according to ETH AutomaticPadCRCStrip value */
1907 /* Set the BL bit according to ETH BackOffLimit value */
1908 /* Set the DC bit according to ETH DeferralCheck value */
1909 tmpreg |= (uint32_t)(macinit.Watchdog |
1910 macinit.Jabber |
1911 macinit.InterFrameGap |
1912 macinit.CarrierSense |
1913 (heth->Init).Speed |
1914 macinit.ReceiveOwn |
1915 macinit.LoopbackMode |
1916 (heth->Init).DuplexMode |
1917 macinit.ChecksumOffload |
1918 macinit.RetryTransmission |
1919 macinit.AutomaticPadCRCStrip |
1920 macinit.BackOffLimit |
1921 macinit.DeferralCheck);
1923 /* Write to ETHERNET MACCR */
1924 (heth->Instance)->MACCR = (uint32_t)tmpreg;
1926 /* Wait until the write operation will be taken into account:
1927 at least four TX_CLK/RX_CLK clock cycles */
1928 tmpreg = (heth->Instance)->MACCR;
1929 HAL_Delay(ETH_REG_WRITE_DELAY);
1930 (heth->Instance)->MACCR = tmpreg;
1932 /*----------------------- ETHERNET MACFFR Configuration --------------------*/
1933 /* Set the RA bit according to ETH ReceiveAll value */
1934 /* Set the SAF and SAIF bits according to ETH SourceAddrFilter value */
1935 /* Set the PCF bit according to ETH PassControlFrames value */
1936 /* Set the DBF bit according to ETH BroadcastFramesReception value */
1937 /* Set the DAIF bit according to ETH DestinationAddrFilter value */
1938 /* Set the PR bit according to ETH PromiscuousMode value */
1939 /* Set the PM, HMC and HPF bits according to ETH MulticastFramesFilter value */
1940 /* Set the HUC and HPF bits according to ETH UnicastFramesFilter value */
1941 /* Write to ETHERNET MACFFR */
1942 (heth->Instance)->MACFFR = (uint32_t)(macinit.ReceiveAll |
1943 macinit.SourceAddrFilter |
1944 macinit.PassControlFrames |
1945 macinit.BroadcastFramesReception |
1946 macinit.DestinationAddrFilter |
1947 macinit.PromiscuousMode |
1948 macinit.MulticastFramesFilter |
1949 macinit.UnicastFramesFilter);
1951 /* Wait until the write operation will be taken into account:
1952 at least four TX_CLK/RX_CLK clock cycles */
1953 tmpreg = (heth->Instance)->MACFFR;
1954 HAL_Delay(ETH_REG_WRITE_DELAY);
1955 (heth->Instance)->MACFFR = tmpreg;
1957 /*--------------- ETHERNET MACHTHR and MACHTLR Configuration --------------*/
1958 /* Write to ETHERNET MACHTHR */
1959 (heth->Instance)->MACHTHR = (uint32_t)macinit.HashTableHigh;
1961 /* Write to ETHERNET MACHTLR */
1962 (heth->Instance)->MACHTLR = (uint32_t)macinit.HashTableLow;
1963 /*----------------------- ETHERNET MACFCR Configuration -------------------*/
1965 /* Get the ETHERNET MACFCR value */
1966 tmpreg = (heth->Instance)->MACFCR;
1967 /* Clear xx bits */
1968 tmpreg &= ETH_MACFCR_CLEAR_MASK;
1970 /* Set the PT bit according to ETH PauseTime value */
1971 /* Set the DZPQ bit according to ETH ZeroQuantaPause value */
1972 /* Set the PLT bit according to ETH PauseLowThreshold value */
1973 /* Set the UP bit according to ETH UnicastPauseFrameDetect value */
1974 /* Set the RFE bit according to ETH ReceiveFlowControl value */
1975 /* Set the TFE bit according to ETH TransmitFlowControl value */
1976 tmpreg |= (uint32_t)((macinit.PauseTime << 16) |
1977 macinit.ZeroQuantaPause |
1978 macinit.PauseLowThreshold |
1979 macinit.UnicastPauseFrameDetect |
1980 macinit.ReceiveFlowControl |
1981 macinit.TransmitFlowControl);
1983 /* Write to ETHERNET MACFCR */
1984 (heth->Instance)->MACFCR = (uint32_t)tmpreg;
1986 /* Wait until the write operation will be taken into account:
1987 at least four TX_CLK/RX_CLK clock cycles */
1988 tmpreg = (heth->Instance)->MACFCR;
1989 HAL_Delay(ETH_REG_WRITE_DELAY);
1990 (heth->Instance)->MACFCR = tmpreg;
1992 /*----------------------- ETHERNET MACVLANTR Configuration ----------------*/
1993 /* Set the ETV bit according to ETH VLANTagComparison value */
1994 /* Set the VL bit according to ETH VLANTagIdentifier value */
1995 (heth->Instance)->MACVLANTR = (uint32_t)(macinit.VLANTagComparison |
1996 macinit.VLANTagIdentifier);
1998 /* Wait until the write operation will be taken into account:
1999 at least four TX_CLK/RX_CLK clock cycles */
2000 tmpreg = (heth->Instance)->MACVLANTR;
2001 HAL_Delay(ETH_REG_WRITE_DELAY);
2002 (heth->Instance)->MACVLANTR = tmpreg;
2004 /* Ethernet DMA default initialization ************************************/
2005 dmainit.DropTCPIPChecksumErrorFrame = ETH_DROPTCPIPCHECKSUMERRORFRAME_ENABLE;
2006 dmainit.ReceiveStoreForward = ETH_RECEIVESTOREFORWARD_ENABLE;
2007 dmainit.FlushReceivedFrame = ETH_FLUSHRECEIVEDFRAME_ENABLE;
2008 dmainit.TransmitStoreForward = ETH_TRANSMITSTOREFORWARD_ENABLE;
2009 dmainit.TransmitThresholdControl = ETH_TRANSMITTHRESHOLDCONTROL_64BYTES;
2010 dmainit.ForwardErrorFrames = ETH_FORWARDERRORFRAMES_DISABLE;
2011 dmainit.ForwardUndersizedGoodFrames = ETH_FORWARDUNDERSIZEDGOODFRAMES_DISABLE;
2012 dmainit.ReceiveThresholdControl = ETH_RECEIVEDTHRESHOLDCONTROL_64BYTES;
2013 dmainit.SecondFrameOperate = ETH_SECONDFRAMEOPERARTE_ENABLE;
2014 dmainit.AddressAlignedBeats = ETH_ADDRESSALIGNEDBEATS_ENABLE;
2015 dmainit.FixedBurst = ETH_FIXEDBURST_ENABLE;
2016 dmainit.RxDMABurstLength = ETH_RXDMABURSTLENGTH_32BEAT;
2017 dmainit.TxDMABurstLength = ETH_TXDMABURSTLENGTH_32BEAT;
2018 dmainit.EnhancedDescriptorFormat = ETH_DMAENHANCEDDESCRIPTOR_ENABLE;
2019 dmainit.DescriptorSkipLength = 0x0;
2020 dmainit.DMAArbitration = ETH_DMAARBITRATION_ROUNDROBIN_RXTX_1_1;
2022 /* Get the ETHERNET DMAOMR value */
2023 tmpreg = (heth->Instance)->DMAOMR;
2024 /* Clear xx bits */
2025 tmpreg &= ETH_DMAOMR_CLEAR_MASK;
2027 /* Set the DT bit according to ETH DropTCPIPChecksumErrorFrame value */
2028 /* Set the RSF bit according to ETH ReceiveStoreForward value */
2029 /* Set the DFF bit according to ETH FlushReceivedFrame value */
2030 /* Set the TSF bit according to ETH TransmitStoreForward value */
2031 /* Set the TTC bit according to ETH TransmitThresholdControl value */
2032 /* Set the FEF bit according to ETH ForwardErrorFrames value */
2033 /* Set the FUF bit according to ETH ForwardUndersizedGoodFrames value */
2034 /* Set the RTC bit according to ETH ReceiveThresholdControl value */
2035 /* Set the OSF bit according to ETH SecondFrameOperate value */
2036 tmpreg |= (uint32_t)(dmainit.DropTCPIPChecksumErrorFrame |
2037 dmainit.ReceiveStoreForward |
2038 dmainit.FlushReceivedFrame |
2039 dmainit.TransmitStoreForward |
2040 dmainit.TransmitThresholdControl |
2041 dmainit.ForwardErrorFrames |
2042 dmainit.ForwardUndersizedGoodFrames |
2043 dmainit.ReceiveThresholdControl |
2044 dmainit.SecondFrameOperate);
2046 /* Write to ETHERNET DMAOMR */
2047 (heth->Instance)->DMAOMR = (uint32_t)tmpreg;
2049 /* Wait until the write operation will be taken into account:
2050 at least four TX_CLK/RX_CLK clock cycles */
2051 tmpreg = (heth->Instance)->DMAOMR;
2052 HAL_Delay(ETH_REG_WRITE_DELAY);
2053 (heth->Instance)->DMAOMR = tmpreg;
2055 /*----------------------- ETHERNET DMABMR Configuration ------------------*/
2056 /* Set the AAL bit according to ETH AddressAlignedBeats value */
2057 /* Set the FB bit according to ETH FixedBurst value */
2058 /* Set the RPBL and 4*PBL bits according to ETH RxDMABurstLength value */
2059 /* Set the PBL and 4*PBL bits according to ETH TxDMABurstLength value */
2060 /* Set the Enhanced DMA descriptors bit according to ETH EnhancedDescriptorFormat value*/
2061 /* Set the DSL bit according to ETH DesciptorSkipLength value */
2062 /* Set the PR and DA bits according to ETH DMAArbitration value */
2063 (heth->Instance)->DMABMR = (uint32_t)(dmainit.AddressAlignedBeats |
2064 dmainit.FixedBurst |
2065 dmainit.RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
2066 dmainit.TxDMABurstLength |
2067 dmainit.EnhancedDescriptorFormat |
2068 (dmainit.DescriptorSkipLength << 2) |
2069 dmainit.DMAArbitration |
2070 ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
2072 /* Wait until the write operation will be taken into account:
2073 at least four TX_CLK/RX_CLK clock cycles */
2074 tmpreg = (heth->Instance)->DMABMR;
2075 HAL_Delay(ETH_REG_WRITE_DELAY);
2076 (heth->Instance)->DMABMR = tmpreg;
2078 if((heth->Init).RxMode == ETH_RXINTERRUPT_MODE)
2080 /* Enable the Ethernet Rx Interrupt */
2081 __HAL_ETH_DMA_ENABLE_IT((heth), ETH_DMA_IT_NIS | ETH_DMA_IT_R);
2084 /* Initialize MAC address in ethernet MAC */
2085 ETH_MACAddressConfig(heth, ETH_MAC_ADDRESS0, heth->Init.MACAddr);
2089 * @brief Configures the selected MAC address.
2090 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2091 * the configuration information for ETHERNET module
2092 * @param MacAddr The MAC address to configure
2093 * This parameter can be one of the following values:
2094 * @arg ETH_MAC_Address0: MAC Address0
2095 * @arg ETH_MAC_Address1: MAC Address1
2096 * @arg ETH_MAC_Address2: MAC Address2
2097 * @arg ETH_MAC_Address3: MAC Address3
2098 * @param Addr Pointer to MAC address buffer data (6 bytes)
2099 * @retval HAL status
2101 static void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr)
2103 uint32_t tmpreg;
2105 /* Check the parameters */
2106 assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
2108 /* Calculate the selected MAC address high register */
2109 tmpreg = ((uint32_t)Addr[5] << 8) | (uint32_t)Addr[4];
2110 /* Load the selected MAC address high register */
2111 (*(__IO uint32_t *)((uint32_t)(ETH_MAC_ADDR_HBASE + MacAddr))) = tmpreg;
2112 /* Calculate the selected MAC address low register */
2113 tmpreg = ((uint32_t)Addr[3] << 24) | ((uint32_t)Addr[2] << 16) | ((uint32_t)Addr[1] << 8) | Addr[0];
2115 /* Load the selected MAC address low register */
2116 (*(__IO uint32_t *)((uint32_t)(ETH_MAC_ADDR_LBASE + MacAddr))) = tmpreg;
2120 * @brief Enables the MAC transmission.
2121 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2122 * the configuration information for ETHERNET module
2123 * @retval None
2125 static void ETH_MACTransmissionEnable(ETH_HandleTypeDef *heth)
2127 __IO uint32_t tmpreg = 0;
2129 /* Enable the MAC transmission */
2130 (heth->Instance)->MACCR |= ETH_MACCR_TE;
2132 /* Wait until the write operation will be taken into account:
2133 at least four TX_CLK/RX_CLK clock cycles */
2134 tmpreg = (heth->Instance)->MACCR;
2135 HAL_Delay(ETH_REG_WRITE_DELAY);
2136 (heth->Instance)->MACCR = tmpreg;
2140 * @brief Disables the MAC transmission.
2141 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2142 * the configuration information for ETHERNET module
2143 * @retval None
2145 static void ETH_MACTransmissionDisable(ETH_HandleTypeDef *heth)
2147 __IO uint32_t tmpreg = 0;
2149 /* Disable the MAC transmission */
2150 (heth->Instance)->MACCR &= ~ETH_MACCR_TE;
2152 /* Wait until the write operation will be taken into account:
2153 at least four TX_CLK/RX_CLK clock cycles */
2154 tmpreg = (heth->Instance)->MACCR;
2155 HAL_Delay(ETH_REG_WRITE_DELAY);
2156 (heth->Instance)->MACCR = tmpreg;
2160 * @brief Enables the MAC reception.
2161 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2162 * the configuration information for ETHERNET module
2163 * @retval None
2165 static void ETH_MACReceptionEnable(ETH_HandleTypeDef *heth)
2167 __IO uint32_t tmpreg = 0;
2169 /* Enable the MAC reception */
2170 (heth->Instance)->MACCR |= ETH_MACCR_RE;
2172 /* Wait until the write operation will be taken into account:
2173 at least four TX_CLK/RX_CLK clock cycles */
2174 tmpreg = (heth->Instance)->MACCR;
2175 HAL_Delay(ETH_REG_WRITE_DELAY);
2176 (heth->Instance)->MACCR = tmpreg;
2180 * @brief Disables the MAC reception.
2181 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2182 * the configuration information for ETHERNET module
2183 * @retval None
2185 static void ETH_MACReceptionDisable(ETH_HandleTypeDef *heth)
2187 __IO uint32_t tmpreg = 0;
2189 /* Disable the MAC reception */
2190 (heth->Instance)->MACCR &= ~ETH_MACCR_RE;
2192 /* Wait until the write operation will be taken into account:
2193 at least four TX_CLK/RX_CLK clock cycles */
2194 tmpreg = (heth->Instance)->MACCR;
2195 HAL_Delay(ETH_REG_WRITE_DELAY);
2196 (heth->Instance)->MACCR = tmpreg;
2200 * @brief Enables the DMA transmission.
2201 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2202 * the configuration information for ETHERNET module
2203 * @retval None
2205 static void ETH_DMATransmissionEnable(ETH_HandleTypeDef *heth)
2207 /* Enable the DMA transmission */
2208 (heth->Instance)->DMAOMR |= ETH_DMAOMR_ST;
2212 * @brief Disables the DMA transmission.
2213 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2214 * the configuration information for ETHERNET module
2215 * @retval None
2217 static void ETH_DMATransmissionDisable(ETH_HandleTypeDef *heth)
2219 /* Disable the DMA transmission */
2220 (heth->Instance)->DMAOMR &= ~ETH_DMAOMR_ST;
2224 * @brief Enables the DMA reception.
2225 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2226 * the configuration information for ETHERNET module
2227 * @retval None
2229 static void ETH_DMAReceptionEnable(ETH_HandleTypeDef *heth)
2231 /* Enable the DMA reception */
2232 (heth->Instance)->DMAOMR |= ETH_DMAOMR_SR;
2236 * @brief Disables the DMA reception.
2237 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2238 * the configuration information for ETHERNET module
2239 * @retval None
2241 static void ETH_DMAReceptionDisable(ETH_HandleTypeDef *heth)
2243 /* Disable the DMA reception */
2244 (heth->Instance)->DMAOMR &= ~ETH_DMAOMR_SR;
2248 * @brief Clears the ETHERNET transmit FIFO.
2249 * @param heth pointer to a ETH_HandleTypeDef structure that contains
2250 * the configuration information for ETHERNET module
2251 * @retval None
2253 static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef *heth)
2255 __IO uint32_t tmpreg = 0;
2257 /* Set the Flush Transmit FIFO bit */
2258 (heth->Instance)->DMAOMR |= ETH_DMAOMR_FTF;
2260 /* Wait until the write operation will be taken into account:
2261 at least four TX_CLK/RX_CLK clock cycles */
2262 tmpreg = (heth->Instance)->DMAOMR;
2263 HAL_Delay(ETH_REG_WRITE_DELAY);
2264 (heth->Instance)->DMAOMR = tmpreg;
2267 #if (USE_HAL_ETH_REGISTER_CALLBACKS == 1)
2268 static void ETH_InitCallbacksToDefault(ETH_HandleTypeDef *heth)
2270 /* Init the ETH Callback settings */
2271 heth->TxCpltCallback = HAL_ETH_TxCpltCallback; /* Legacy weak TxCpltCallback */
2272 heth->RxCpltCallback = HAL_ETH_RxCpltCallback; /* Legacy weak RxCpltCallback */
2273 heth->DMAErrorCallback = HAL_ETH_ErrorCallback; /* Legacy weak DMAErrorCallback */
2275 #endif /* USE_HAL_ETH_REGISTER_CALLBACKS */
2278 * @}
2281 #endif /* ETH */
2282 #endif /* HAL_ETH_MODULE_ENABLED */
2284 * @}
2288 * @}
2291 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/