2 ******************************************************************************
3 * @file stm32f4xx_hal_eth.c
4 * @author MCD Application Team
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
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 (#)Declare a ETH_HandleTypeDef handle structure, for example:
21 ETH_HandleTypeDef heth;
23 (#)Fill parameters of Init structure in heth handle
25 (#)Call HAL_ETH_Init() API to initialize the Ethernet peripheral (MAC, DMA, ...)
27 (#)Initialize the ETH low level resources through the HAL_ETH_MspInit() API:
28 (##) Enable the Ethernet interface clock using
29 (+++) __ETHMAC_CLK_ENABLE();
30 (+++) __ETHMACTX_CLK_ENABLE();
31 (+++) __ETHMACRX_CLK_ENABLE();
33 (##) Initialize the related GPIO clocks
34 (##) Configure Ethernet pin-out
35 (##) Configure Ethernet NVIC interrupt (IT mode)
37 (#)Initialize Ethernet DMA Descriptors in chain mode and point to allocated buffers:
38 (##) HAL_ETH_DMATxDescListInit(); for Transmission process
39 (##) HAL_ETH_DMARxDescListInit(); for Reception process
41 (#)Enable MAC and DMA transmission and reception:
44 (#)Prepare ETH DMA TX Descriptors and give the hand to ETH DMA to transfer
45 the frame to MAC TX FIFO:
46 (##) HAL_ETH_TransmitFrame();
48 (#)Poll for a received frame in ETH RX DMA Descriptors and get received
50 (##) HAL_ETH_GetReceivedFrame(); (should be called into an infinite loop)
52 (#) Get a received frame when an ETH RX interrupt occurs:
53 (##) HAL_ETH_GetReceivedFrame_IT(); (called in IT mode only)
55 (#) Communicate with external PHY device:
56 (##) Read a specific register from the PHY
57 HAL_ETH_ReadPHYRegister();
58 (##) Write data to a specific RHY register:
59 HAL_ETH_WritePHYRegister();
61 (#) Configure the Ethernet MAC after ETH peripheral initialization
62 HAL_ETH_ConfigMAC(); all MAC parameters should be filled.
64 (#) Configure the Ethernet DMA after ETH peripheral initialization
65 HAL_ETH_ConfigDMA(); all DMA parameters should be filled.
68 ******************************************************************************
71 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
73 * Redistribution and use in source and binary forms, with or without modification,
74 * are permitted provided that the following conditions are met:
75 * 1. Redistributions of source code must retain the above copyright notice,
76 * this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright notice,
78 * this list of conditions and the following disclaimer in the documentation
79 * and/or other materials provided with the distribution.
80 * 3. Neither the name of STMicroelectronics nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
84 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
85 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
87 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
90 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
91 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
92 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
93 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
95 ******************************************************************************
98 /* Includes ------------------------------------------------------------------*/
99 #include "stm32f4xx_hal.h"
101 /** @addtogroup STM32F4xx_HAL_Driver
106 * @brief ETH HAL module driver
110 #ifdef HAL_ETH_MODULE_ENABLED
112 #if defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
114 /* Private typedef -----------------------------------------------------------*/
115 /* Private define ------------------------------------------------------------*/
116 #define LINKED_STATE_TIMEOUT_VALUE ((uint32_t)2000) /* 2000 ms */
117 #define AUTONEGO_COMPLETED_TIMEOUT_VALUE ((uint32_t)1000) /* 1000 ms */
119 /* Private macro -------------------------------------------------------------*/
120 /* Private variables ---------------------------------------------------------*/
121 /* Private function prototypes -----------------------------------------------*/
122 static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err);
123 static void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr);
124 static void ETH_MACReceptionEnable(ETH_HandleTypeDef *heth);
125 static void ETH_MACReceptionDisable(ETH_HandleTypeDef *heth);
126 static void ETH_MACTransmissionEnable(ETH_HandleTypeDef *heth);
127 static void ETH_MACTransmissionDisable(ETH_HandleTypeDef *heth);
128 static void ETH_DMATransmissionEnable(ETH_HandleTypeDef *heth);
129 static void ETH_DMATransmissionDisable(ETH_HandleTypeDef *heth);
130 static void ETH_DMAReceptionEnable(ETH_HandleTypeDef *heth);
131 static void ETH_DMAReceptionDisable(ETH_HandleTypeDef *heth);
132 static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef *heth);
134 /* Private functions ---------------------------------------------------------*/
136 /** @defgroup ETH_Private_Functions
140 /** @defgroup ETH_Group1 Initialization and de-initialization functions
141 * @brief Initialization and Configuration functions
144 ===============================================================================
145 ##### Initialization and de-initialization functions #####
146 ===============================================================================
147 [..] This section provides functions allowing to:
148 (+) Initialize and configure the Ethernet peripheral
149 (+) De-initialize the Ethernet peripheral
156 * @brief Initializes the Ethernet MAC and DMA according to default
158 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
159 * the configuration information for ETHERNET module
162 HAL_StatusTypeDef HAL_ETH_Init(ETH_HandleTypeDef *heth)
164 uint32_t tmpreg = 0, phyreg = 0;
165 uint32_t hclk = 60000000;
166 uint32_t tickstart = 0;
167 uint32_t err = ETH_SUCCESS;
169 /* Check the ETH peripheral state */
175 /* Check parameters */
176 assert_param(IS_ETH_AUTONEGOTIATION(heth->Init.AutoNegotiation));
177 assert_param(IS_ETH_RX_MODE(heth->Init.RxMode));
178 assert_param(IS_ETH_CHECKSUM_MODE(heth->Init.ChecksumMode));
179 assert_param(IS_ETH_MEDIA_INTERFACE(heth->Init.MediaInterface));
181 if(heth->State == HAL_ETH_STATE_RESET)
183 /* Init the low level hardware : GPIO, CLOCK, NVIC. */
184 HAL_ETH_MspInit(heth);
187 /* Enable SYSCFG Clock */
188 __SYSCFG_CLK_ENABLE();
190 /* Select MII or RMII Mode*/
191 SYSCFG->PMC &= ~(SYSCFG_PMC_MII_RMII_SEL);
192 SYSCFG->PMC |= (uint32_t)heth->Init.MediaInterface;
194 /* Ethernet Software reset */
195 /* Set the SWR bit: resets all MAC subsystem internal registers and logic */
196 /* After reset all the registers holds their respective reset values */
197 (heth->Instance)->DMABMR |= ETH_DMABMR_SR;
199 /* Wait for software reset */
200 while (((heth->Instance)->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)
204 /*-------------------------------- MAC Initialization ----------------------*/
205 /* Get the ETHERNET MACMIIAR value */
206 tmpreg = (heth->Instance)->MACMIIAR;
207 /* Clear CSR Clock Range CR[2:0] bits */
208 tmpreg &= MACMIIAR_CR_MASK;
210 /* Get hclk frequency value */
211 hclk = HAL_RCC_GetHCLKFreq();
213 /* Set CR bits depending on hclk value */
214 if((hclk >= 20000000)&&(hclk < 35000000))
216 /* CSR Clock Range between 20-35 MHz */
217 tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div16;
219 else if((hclk >= 35000000)&&(hclk < 60000000))
221 /* CSR Clock Range between 35-60 MHz */
222 tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div26;
224 else if((hclk >= 60000000)&&(hclk < 100000000))
226 /* CSR Clock Range between 60-100 MHz */
227 tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div42;
229 else if((hclk >= 100000000)&&(hclk < 150000000))
231 /* CSR Clock Range between 100-150 MHz */
232 tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div62;
234 else /* ((hclk >= 150000000)&&(hclk <= 168000000)) */
236 /* CSR Clock Range between 150-168 MHz */
237 tmpreg |= (uint32_t)ETH_MACMIIAR_CR_Div102;
240 /* Write to ETHERNET MAC MIIAR: Configure the ETHERNET CSR Clock Range */
241 (heth->Instance)->MACMIIAR = (uint32_t)tmpreg;
243 /*-------------------- PHY initialization and configuration ----------------*/
244 /* Put the PHY in reset mode */
245 if((HAL_ETH_WritePHYRegister(heth, PHY_BCR, PHY_RESET)) != HAL_OK)
247 /* In case of write timeout */
250 /* Config MAC and DMA */
251 ETH_MACDMAConfig(heth, err);
253 /* Set the ETH peripheral state to READY */
254 heth->State = HAL_ETH_STATE_READY;
256 /* Return HAL_ERROR */
260 /* Delay to assure PHY reset */
261 HAL_Delay(PHY_RESET_DELAY);
263 if((heth->Init).AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE)
266 tickstart = HAL_GetTick();
268 /* We wait for linked status */
271 HAL_ETH_ReadPHYRegister(heth, PHY_BSR, &phyreg);
273 /* Check for the Timeout */
274 if((HAL_GetTick() - tickstart ) > LINKED_STATE_TIMEOUT_VALUE)
276 /* In case of write timeout */
279 /* Config MAC and DMA */
280 ETH_MACDMAConfig(heth, err);
282 heth->State= HAL_ETH_STATE_READY;
284 /* Process Unlocked */
289 } while (((phyreg & PHY_LINKED_STATUS) != PHY_LINKED_STATUS));
292 /* Enable Auto-Negotiation */
293 if((HAL_ETH_WritePHYRegister(heth, PHY_BCR, PHY_AUTONEGOTIATION)) != HAL_OK)
295 /* In case of write timeout */
298 /* Config MAC and DMA */
299 ETH_MACDMAConfig(heth, err);
301 /* Set the ETH peripheral state to READY */
302 heth->State = HAL_ETH_STATE_READY;
304 /* Return HAL_ERROR */
309 tickstart = HAL_GetTick();
311 /* Wait until the auto-negotiation will be completed */
314 HAL_ETH_ReadPHYRegister(heth, PHY_BSR, &phyreg);
316 /* Check for the Timeout */
317 if((HAL_GetTick() - tickstart ) > AUTONEGO_COMPLETED_TIMEOUT_VALUE)
319 /* In case of write timeout */
322 /* Config MAC and DMA */
323 ETH_MACDMAConfig(heth, err);
325 heth->State= HAL_ETH_STATE_READY;
327 /* Process Unlocked */
333 } while (((phyreg & PHY_AUTONEGO_COMPLETE) != PHY_AUTONEGO_COMPLETE));
335 /* Read the result of the auto-negotiation */
336 if((HAL_ETH_ReadPHYRegister(heth, PHY_SR, &phyreg)) != HAL_OK)
338 /* In case of write timeout */
341 /* Config MAC and DMA */
342 ETH_MACDMAConfig(heth, err);
344 /* Set the ETH peripheral state to READY */
345 heth->State = HAL_ETH_STATE_READY;
347 /* Return HAL_ERROR */
351 /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
352 if((phyreg & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
354 /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
355 (heth->Init).DuplexMode = ETH_MODE_FULLDUPLEX;
359 /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
360 (heth->Init).DuplexMode = ETH_MODE_HALFDUPLEX;
362 /* Configure the MAC with the speed fixed by the auto-negotiation process */
363 if((phyreg & PHY_SPEED_STATUS) == PHY_SPEED_STATUS)
365 /* Set Ethernet speed to 10M following the auto-negotiation */
366 (heth->Init).Speed = ETH_SPEED_10M;
370 /* Set Ethernet speed to 100M following the auto-negotiation */
371 (heth->Init).Speed = ETH_SPEED_100M;
374 else /* AutoNegotiation Disable */
376 /* Check parameters */
377 assert_param(IS_ETH_SPEED(heth->Init.Speed));
378 assert_param(IS_ETH_DUPLEX_MODE(heth->Init.DuplexMode));
380 /* Set MAC Speed and Duplex Mode */
381 if(HAL_ETH_WritePHYRegister(heth, PHY_BCR, ((uint16_t)((heth->Init).DuplexMode >> 3) |
382 (uint16_t)((heth->Init).Speed >> 1))) != HAL_OK)
384 /* In case of write timeout */
387 /* Config MAC and DMA */
388 ETH_MACDMAConfig(heth, err);
390 /* Set the ETH peripheral state to READY */
391 heth->State = HAL_ETH_STATE_READY;
393 /* Return HAL_ERROR */
397 /* Delay to assure PHY configuration */
398 HAL_Delay(PHY_CONFIG_DELAY);
401 /* Config MAC and DMA */
402 ETH_MACDMAConfig(heth, err);
404 /* Set ETH HAL State to Ready */
405 heth->State= HAL_ETH_STATE_READY;
407 /* Return function status */
412 * @brief De-Initializes the ETH peripheral.
413 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
414 * the configuration information for ETHERNET module
417 HAL_StatusTypeDef HAL_ETH_DeInit(ETH_HandleTypeDef *heth)
419 /* Set the ETH peripheral state to BUSY */
420 heth->State = HAL_ETH_STATE_BUSY;
422 /* De-Init the low level hardware : GPIO, CLOCK, NVIC. */
423 HAL_ETH_MspDeInit(heth);
425 /* Set ETH HAL state to Disabled */
426 heth->State= HAL_ETH_STATE_RESET;
431 /* Return function status */
436 * @brief Initializes the DMA Tx descriptors in chain mode.
437 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
438 * the configuration information for ETHERNET module
439 * @param DMATxDescTab: Pointer to the first Tx desc list
440 * @param TxBuff: Pointer to the first TxBuffer list
441 * @param TxBuffCount: Number of the used Tx desc in the list
444 HAL_StatusTypeDef HAL_ETH_DMATxDescListInit(ETH_HandleTypeDef *heth, ETH_DMADescTypeDef *DMATxDescTab, uint8_t *TxBuff, uint32_t TxBuffCount)
447 ETH_DMADescTypeDef *dmatxdesc;
452 /* Set the ETH peripheral state to BUSY */
453 heth->State = HAL_ETH_STATE_BUSY;
455 /* Set the DMATxDescToSet pointer with the first one of the DMATxDescTab list */
456 heth->TxDesc = DMATxDescTab;
458 /* Fill each DMATxDesc descriptor with the right values */
459 for(i=0; i < TxBuffCount; i++)
461 /* Get the pointer on the ith member of the Tx Desc list */
462 dmatxdesc = DMATxDescTab + i;
464 /* Set Second Address Chained bit */
465 dmatxdesc->Status = ETH_DMATXDESC_TCH;
467 /* Set Buffer1 address pointer */
468 dmatxdesc->Buffer1Addr = (uint32_t)(&TxBuff[i*ETH_TX_BUF_SIZE]);
470 if ((heth->Init).ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)
472 /* Set the DMA Tx descriptors checksum insertion */
473 dmatxdesc->Status |= ETH_DMATXDESC_CHECKSUMTCPUDPICMPFULL;
476 /* Initialize the next descriptor with the Next Descriptor Polling Enable */
477 if(i < (TxBuffCount-1))
479 /* Set next descriptor address register with next descriptor base address */
480 dmatxdesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1);
484 /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
485 dmatxdesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
489 /* Set Transmit Descriptor List Address Register */
490 (heth->Instance)->DMATDLAR = (uint32_t) DMATxDescTab;
492 /* Set ETH HAL State to Ready */
493 heth->State= HAL_ETH_STATE_READY;
495 /* Process Unlocked */
498 /* Return function status */
503 * @brief Initializes the DMA Rx descriptors in chain mode.
504 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
505 * the configuration information for ETHERNET module
506 * @param DMARxDescTab: Pointer to the first Rx desc list
507 * @param RxBuff: Pointer to the first RxBuffer list
508 * @param RxBuffCount: Number of the used Rx desc in the list
511 HAL_StatusTypeDef HAL_ETH_DMARxDescListInit(ETH_HandleTypeDef *heth, ETH_DMADescTypeDef *DMARxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
514 ETH_DMADescTypeDef *DMARxDesc;
519 /* Set the ETH peripheral state to BUSY */
520 heth->State = HAL_ETH_STATE_BUSY;
522 /* Set the Ethernet RxDesc pointer with the first one of the DMARxDescTab list */
523 heth->RxDesc = DMARxDescTab;
525 /* Fill each DMARxDesc descriptor with the right values */
526 for(i=0; i < RxBuffCount; i++)
528 /* Get the pointer on the ith member of the Rx Desc list */
529 DMARxDesc = DMARxDescTab+i;
531 /* Set Own bit of the Rx descriptor Status */
532 DMARxDesc->Status = ETH_DMARXDESC_OWN;
534 /* Set Buffer1 size and Second Address Chained bit */
535 DMARxDesc->ControlBufferSize = ETH_DMARXDESC_RCH | ETH_RX_BUF_SIZE;
537 /* Set Buffer1 address pointer */
538 DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*ETH_RX_BUF_SIZE]);
540 if((heth->Init).RxMode == ETH_RXINTERRUPT_MODE)
542 /* Enable Ethernet DMA Rx Descriptor interrupt */
543 DMARxDesc->ControlBufferSize &= ~ETH_DMARXDESC_DIC;
546 /* Initialize the next descriptor with the Next Descriptor Polling Enable */
547 if(i < (RxBuffCount-1))
549 /* Set next descriptor address register with next descriptor base address */
550 DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab+i+1);
554 /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
555 DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
559 /* Set Receive Descriptor List Address Register */
560 (heth->Instance)->DMARDLAR = (uint32_t) DMARxDescTab;
562 /* Set ETH HAL State to Ready */
563 heth->State= HAL_ETH_STATE_READY;
565 /* Process Unlocked */
568 /* Return function status */
573 * @brief Initializes the ETH MSP.
574 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
575 * the configuration information for ETHERNET module
578 __weak void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)
580 /* NOTE : This function Should not be modified, when the callback is needed,
581 the HAL_ETH_MspInit could be implemented in the user file
586 * @brief DeInitializes ETH MSP.
587 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
588 * the configuration information for ETHERNET module
591 __weak void HAL_ETH_MspDeInit(ETH_HandleTypeDef *heth)
593 /* NOTE : This function Should not be modified, when the callback is needed,
594 the HAL_ETH_MspDeInit could be implemented in the user file
602 /** @defgroup ETH_Group2 IO operation functions
603 * @brief Data transfers functions
606 ==============================================================================
607 ##### IO operation functions #####
608 ==============================================================================
609 [..] This section provides functions allowing to:
611 HAL_ETH_TransmitFrame();
613 HAL_ETH_GetReceivedFrame();
614 HAL_ETH_GetReceivedFrame_IT();
615 (+) Read from an External PHY register
616 HAL_ETH_ReadPHYRegister();
617 (+) Write to an External PHY register
618 HAL_ETH_WritePHYRegister();
626 * @brief Sends an Ethernet frame.
627 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
628 * the configuration information for ETHERNET module
629 * @param FrameLength: Amount of data to be sent
632 HAL_StatusTypeDef HAL_ETH_TransmitFrame(ETH_HandleTypeDef *heth, uint32_t FrameLength)
634 uint32_t bufcount = 0, size = 0, i = 0;
639 /* Set the ETH peripheral state to BUSY */
640 heth->State = HAL_ETH_STATE_BUSY;
642 if (FrameLength == 0)
644 /* Set ETH HAL state to READY */
645 heth->State = HAL_ETH_STATE_READY;
647 /* Process Unlocked */
653 /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
654 if(((heth->TxDesc)->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
657 heth->State = HAL_ETH_STATE_BUSY_TX;
659 /* Process Unlocked */
665 /* Get the number of needed Tx buffers for the current frame */
666 if (FrameLength > ETH_TX_BUF_SIZE)
668 bufcount = FrameLength/ETH_TX_BUF_SIZE;
669 if (FrameLength % ETH_TX_BUF_SIZE)
680 /* Set LAST and FIRST segment */
681 heth->TxDesc->Status |=ETH_DMATXDESC_FS|ETH_DMATXDESC_LS;
683 heth->TxDesc->ControlBufferSize = (FrameLength & ETH_DMATXDESC_TBS1);
684 /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
685 heth->TxDesc->Status |= ETH_DMATXDESC_OWN;
686 /* Point to next descriptor */
687 heth->TxDesc= (ETH_DMADescTypeDef *)(heth->TxDesc->Buffer2NextDescAddr);
691 for (i=0; i< bufcount; i++)
693 /* Clear FIRST and LAST segment bits */
694 heth->TxDesc->Status &= ~(ETH_DMATXDESC_FS | ETH_DMATXDESC_LS);
698 /* Setting the first segment bit */
699 heth->TxDesc->Status |= ETH_DMATXDESC_FS;
703 heth->TxDesc->ControlBufferSize = (ETH_TX_BUF_SIZE & ETH_DMATXDESC_TBS1);
705 if (i == (bufcount-1))
707 /* Setting the last segment bit */
708 heth->TxDesc->Status |= ETH_DMATXDESC_LS;
709 size = FrameLength - (bufcount-1)*ETH_TX_BUF_SIZE;
710 heth->TxDesc->ControlBufferSize = (size & ETH_DMATXDESC_TBS1);
713 /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
714 heth->TxDesc->Status |= ETH_DMATXDESC_OWN;
715 /* point to next descriptor */
716 heth->TxDesc = (ETH_DMADescTypeDef *)(heth->TxDesc->Buffer2NextDescAddr);
720 /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
721 if (((heth->Instance)->DMASR & ETH_DMASR_TBUS) != (uint32_t)RESET)
723 /* Clear TBUS ETHERNET DMA flag */
724 (heth->Instance)->DMASR = ETH_DMASR_TBUS;
725 /* Resume DMA transmission*/
726 (heth->Instance)->DMATPDR = 0;
729 /* Set ETH HAL State to Ready */
730 heth->State = HAL_ETH_STATE_READY;
732 /* Process Unlocked */
735 /* Return function status */
740 * @brief Checks for received frames.
741 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
742 * the configuration information for ETHERNET module
745 HAL_StatusTypeDef HAL_ETH_GetReceivedFrame(ETH_HandleTypeDef *heth)
747 uint32_t framelength = 0;
752 /* Check the ETH state to BUSY */
753 heth->State = HAL_ETH_STATE_BUSY;
755 /* Check if segment is not owned by DMA */
756 /* (((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET) && ((heth->RxDesc->Status & ETH_DMARXDESC_LS) != (uint32_t)RESET)) */
757 if(((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET))
759 /* Check if last segment */
760 if(((heth->RxDesc->Status & ETH_DMARXDESC_LS) != (uint32_t)RESET))
762 /* increment segment count */
763 (heth->RxFrameInfos).SegCount++;
765 /* Check if last segment is first segment: one segment contains the frame */
766 if ((heth->RxFrameInfos).SegCount == 1)
768 (heth->RxFrameInfos).FSRxDesc =heth->RxDesc;
771 heth->RxFrameInfos.LSRxDesc = heth->RxDesc;
773 /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
774 framelength = (((heth->RxDesc)->Status & ETH_DMARXDESC_FL) >> ETH_DMARXDESC_FRAMELENGTHSHIFT) - 4;
775 heth->RxFrameInfos.length = framelength;
777 /* Get the address of the buffer start address */
778 heth->RxFrameInfos.buffer = ((heth->RxFrameInfos).FSRxDesc)->Buffer1Addr;
779 /* point to next descriptor */
780 heth->RxDesc = (ETH_DMADescTypeDef*) ((heth->RxDesc)->Buffer2NextDescAddr);
782 /* Set HAL State to Ready */
783 heth->State = HAL_ETH_STATE_READY;
785 /* Process Unlocked */
788 /* Return function status */
791 /* Check if first segment */
792 else if((heth->RxDesc->Status & ETH_DMARXDESC_FS) != (uint32_t)RESET)
794 (heth->RxFrameInfos).FSRxDesc = heth->RxDesc;
795 (heth->RxFrameInfos).LSRxDesc = HAL_NULL;
796 (heth->RxFrameInfos).SegCount = 1;
797 /* Point to next descriptor */
798 heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
800 /* Check if intermediate segment */
803 (heth->RxFrameInfos).SegCount++;
804 /* Point to next descriptor */
805 heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
809 /* Set ETH HAL State to Ready */
810 heth->State = HAL_ETH_STATE_READY;
812 /* Process Unlocked */
815 /* Return function status */
820 * @brief Gets the Received frame in interrupt mode.
821 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
822 * the configuration information for ETHERNET module
825 HAL_StatusTypeDef HAL_ETH_GetReceivedFrame_IT(ETH_HandleTypeDef *heth)
827 uint32_t descriptorscancounter = 0;
832 /* Set ETH HAL State to BUSY */
833 heth->State = HAL_ETH_STATE_BUSY;
835 /* Scan descriptors owned by CPU */
836 while (((heth->RxDesc->Status & ETH_DMARXDESC_OWN) == (uint32_t)RESET) && (descriptorscancounter < ETH_RXBUFNB))
838 /* Just for security */
839 descriptorscancounter++;
841 /* Check if first segment in frame */
842 /* ((heth->RxDesc->Status & ETH_DMARXDESC_FS) != (uint32_t)RESET) && ((heth->RxDesc->Status & ETH_DMARXDESC_LS) == (uint32_t)RESET)) */
843 if((heth->RxDesc->Status & (ETH_DMARXDESC_FS | ETH_DMARXDESC_LS)) == (uint32_t)ETH_DMARXDESC_FS)
845 heth->RxFrameInfos.FSRxDesc = heth->RxDesc;
846 heth->RxFrameInfos.SegCount = 1;
847 /* Point to next descriptor */
848 heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
850 /* Check if intermediate segment */
851 /* ((heth->RxDesc->Status & ETH_DMARXDESC_LS) == (uint32_t)RESET)&& ((heth->RxDesc->Status & ETH_DMARXDESC_FS) == (uint32_t)RESET)) */
852 else if ((heth->RxDesc->Status & (ETH_DMARXDESC_LS | ETH_DMARXDESC_FS)) == (uint32_t)RESET)
854 /* Increment segment count */
855 (heth->RxFrameInfos.SegCount)++;
856 /* Point to next descriptor */
857 heth->RxDesc = (ETH_DMADescTypeDef*)(heth->RxDesc->Buffer2NextDescAddr);
859 /* Should be last segment */
863 heth->RxFrameInfos.LSRxDesc = heth->RxDesc;
865 /* Increment segment count */
866 (heth->RxFrameInfos.SegCount)++;
868 /* Check if last segment is first segment: one segment contains the frame */
869 if ((heth->RxFrameInfos.SegCount) == 1)
871 heth->RxFrameInfos.FSRxDesc = heth->RxDesc;
874 /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
875 heth->RxFrameInfos.length = (((heth->RxDesc)->Status & ETH_DMARXDESC_FL) >> ETH_DMARXDESC_FRAMELENGTHSHIFT) - 4;
877 /* Get the address of the buffer start address */
878 heth->RxFrameInfos.buffer =((heth->RxFrameInfos).FSRxDesc)->Buffer1Addr;
880 /* Point to next descriptor */
881 heth->RxDesc = (ETH_DMADescTypeDef*) (heth->RxDesc->Buffer2NextDescAddr);
883 /* Set HAL State to Ready */
884 heth->State = HAL_ETH_STATE_READY;
886 /* Process Unlocked */
889 /* Return function status */
894 /* Set HAL State to Ready */
895 heth->State = HAL_ETH_STATE_READY;
897 /* Process Unlocked */
900 /* Return function status */
905 * @brief This function handles ETH interrupt request.
906 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
907 * the configuration information for ETHERNET module
910 void HAL_ETH_IRQHandler(ETH_HandleTypeDef *heth)
913 if (__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_R))
915 /* Receive complete callback */
916 HAL_ETH_RxCpltCallback(heth);
918 /* Clear the Eth DMA Rx IT pending bits */
919 __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_R);
921 /* Set HAL State to Ready */
922 heth->State = HAL_ETH_STATE_READY;
924 /* Process Unlocked */
928 /* Frame transmitted */
929 else if (__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_T))
931 /* Transfer complete callback */
932 HAL_ETH_TxCpltCallback(heth);
934 /* Clear the Eth DMA Tx IT pending bits */
935 __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_T);
937 /* Set HAL State to Ready */
938 heth->State = HAL_ETH_STATE_READY;
940 /* Process Unlocked */
944 /* Clear the interrupt flags */
945 __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_IT_NIS);
948 if(__HAL_ETH_DMA_GET_FLAG(heth, ETH_DMA_FLAG_AIS))
950 /* Ethernet Error callback */
951 HAL_ETH_ErrorCallback(heth);
953 /* Clear the interrupt flags */
954 __HAL_ETH_DMA_CLEAR_IT(heth, ETH_DMA_FLAG_AIS);
956 /* Set HAL State to Ready */
957 heth->State = HAL_ETH_STATE_READY;
959 /* Process Unlocked */
965 * @brief Tx Transfer completed callbacks.
966 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
967 * the configuration information for ETHERNET module
970 __weak void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *heth)
972 /* NOTE : This function Should not be modified, when the callback is needed,
973 the HAL_ETH_TxCpltCallback could be implemented in the user file
978 * @brief Rx Transfer completed callbacks.
979 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
980 * the configuration information for ETHERNET module
983 __weak void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
985 /* NOTE : This function Should not be modified, when the callback is needed,
986 the HAL_ETH_TxCpltCallback could be implemented in the user file
991 * @brief Ethernet transfer error callbacks
992 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
993 * the configuration information for ETHERNET module
996 __weak void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *heth)
998 /* NOTE : This function Should not be modified, when the callback is needed,
999 the HAL_ETH_TxCpltCallback could be implemented in the user file
1004 * @brief Reads a PHY register
1005 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1006 * the configuration information for ETHERNET module
1007 * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
1008 * This parameter can be one of the following values:
1009 * PHY_BCR: Transceiver Basic Control Register,
1010 * PHY_BSR: Transceiver Basic Status Register.
1011 * More PHY register could be read depending on the used PHY
1012 * @param RegValue: PHY register value
1013 * @retval HAL status
1015 HAL_StatusTypeDef HAL_ETH_ReadPHYRegister(ETH_HandleTypeDef *heth, uint16_t PHYReg, uint32_t *RegValue)
1017 uint32_t tmpreg = 0;
1018 uint32_t tickstart = 0;
1020 /* Check parameters */
1021 assert_param(IS_ETH_PHY_ADDRESS(heth->Init.PhyAddress));
1023 /* Check the ETH peripheral state */
1024 if(heth->State == HAL_ETH_STATE_BUSY_RD)
1028 /* Set ETH HAL State to BUSY_RD */
1029 heth->State = HAL_ETH_STATE_BUSY_RD;
1031 /* Get the ETHERNET MACMIIAR value */
1032 tmpreg = heth->Instance->MACMIIAR;
1034 /* Keep only the CSR Clock Range CR[2:0] bits value */
1035 tmpreg &= ~MACMIIAR_CR_MASK;
1037 /* Prepare the MII address register value */
1038 tmpreg |=(((uint32_t)heth->Init.PhyAddress << 11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
1039 tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
1040 tmpreg &= ~ETH_MACMIIAR_MW; /* Set the read mode */
1041 tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
1043 /* Write the result value into the MII Address register */
1044 heth->Instance->MACMIIAR = tmpreg;
1047 tickstart = HAL_GetTick();
1049 /* Check for the Busy flag */
1050 while((tmpreg & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB)
1052 /* Check for the Timeout */
1053 if((HAL_GetTick() - tickstart ) > PHY_READ_TO)
1055 heth->State= HAL_ETH_STATE_READY;
1057 /* Process Unlocked */
1063 tmpreg = heth->Instance->MACMIIAR;
1066 /* Get MACMIIDR value */
1067 *RegValue = (uint16_t)(heth->Instance->MACMIIDR);
1069 /* Set ETH HAL State to READY */
1070 heth->State = HAL_ETH_STATE_READY;
1072 /* Return function status */
1077 * @brief Writes to a PHY register.
1078 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1079 * the configuration information for ETHERNET module
1080 * @param PHYReg: PHY register address, is the index of one of the 32 PHY register.
1081 * This parameter can be one of the following values:
1082 * PHY_BCR: Transceiver Control Register.
1083 * More PHY register could be written depending on the used PHY
1084 * @param RegValue: the value to write
1085 * @retval HAL status
1087 HAL_StatusTypeDef HAL_ETH_WritePHYRegister(ETH_HandleTypeDef *heth, uint16_t PHYReg, uint32_t RegValue)
1089 uint32_t tmpreg = 0;
1090 uint32_t tickstart = 0;
1092 /* Check parameters */
1093 assert_param(IS_ETH_PHY_ADDRESS(heth->Init.PhyAddress));
1095 /* Check the ETH peripheral state */
1096 if(heth->State == HAL_ETH_STATE_BUSY_WR)
1100 /* Set ETH HAL State to BUSY_WR */
1101 heth->State = HAL_ETH_STATE_BUSY_WR;
1103 /* Get the ETHERNET MACMIIAR value */
1104 tmpreg = heth->Instance->MACMIIAR;
1106 /* Keep only the CSR Clock Range CR[2:0] bits value */
1107 tmpreg &= ~MACMIIAR_CR_MASK;
1109 /* Prepare the MII register address value */
1110 tmpreg |=(((uint32_t)heth->Init.PhyAddress<<11) & ETH_MACMIIAR_PA); /* Set the PHY device address */
1111 tmpreg |=(((uint32_t)PHYReg<<6) & ETH_MACMIIAR_MR); /* Set the PHY register address */
1112 tmpreg |= ETH_MACMIIAR_MW; /* Set the write mode */
1113 tmpreg |= ETH_MACMIIAR_MB; /* Set the MII Busy bit */
1115 /* Give the value to the MII data register */
1116 heth->Instance->MACMIIDR = (uint16_t)RegValue;
1118 /* Write the result value into the MII Address register */
1119 heth->Instance->MACMIIAR = tmpreg;
1122 tickstart = HAL_GetTick();
1124 /* Check for the Busy flag */
1125 while((tmpreg & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB)
1127 /* Check for the Timeout */
1128 if((HAL_GetTick() - tickstart ) > PHY_WRITE_TO)
1130 heth->State= HAL_ETH_STATE_READY;
1132 /* Process Unlocked */
1138 tmpreg = heth->Instance->MACMIIAR;
1141 /* Set ETH HAL State to READY */
1142 heth->State = HAL_ETH_STATE_READY;
1144 /* Return function status */
1152 /** @defgroup ETH_Group3 Peripheral Control functions
1153 * @brief Peripheral Control functions
1156 ===============================================================================
1157 ##### Peripheral Control functions #####
1158 ===============================================================================
1159 [..] This section provides functions allowing to:
1160 (+) Enable MAC and DMA transmission and reception.
1162 (+) Disable MAC and DMA transmission and reception.
1164 (+) Set the MAC configuration in runtime mode
1165 HAL_ETH_ConfigMAC();
1166 (+) Set the DMA configuration in runtime mode
1167 HAL_ETH_ConfigDMA();
1174 * @brief Enables Ethernet MAC and DMA reception/transmission
1175 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1176 * the configuration information for ETHERNET module
1177 * @retval HAL status
1179 HAL_StatusTypeDef HAL_ETH_Start(ETH_HandleTypeDef *heth)
1181 /* Process Locked */
1184 /* Set the ETH peripheral state to BUSY */
1185 heth->State = HAL_ETH_STATE_BUSY;
1187 /* Enable transmit state machine of the MAC for transmission on the MII */
1188 ETH_MACTransmissionEnable(heth);
1190 /* Enable receive state machine of the MAC for reception from the MII */
1191 ETH_MACReceptionEnable(heth);
1193 /* Flush Transmit FIFO */
1194 ETH_FlushTransmitFIFO(heth);
1196 /* Start DMA transmission */
1197 ETH_DMATransmissionEnable(heth);
1199 /* Start DMA reception */
1200 ETH_DMAReceptionEnable(heth);
1202 /* Set the ETH state to READY*/
1203 heth->State= HAL_ETH_STATE_READY;
1205 /* Process Unlocked */
1208 /* Return function status */
1213 * @brief Stop Ethernet MAC and DMA reception/transmission
1214 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1215 * the configuration information for ETHERNET module
1216 * @retval HAL status
1218 HAL_StatusTypeDef HAL_ETH_Stop(ETH_HandleTypeDef *heth)
1220 /* Process Locked */
1223 /* Set the ETH peripheral state to BUSY */
1224 heth->State = HAL_ETH_STATE_BUSY;
1226 /* Stop DMA transmission */
1227 ETH_DMATransmissionDisable(heth);
1229 /* Stop DMA reception */
1230 ETH_DMAReceptionDisable(heth);
1232 /* Disable receive state machine of the MAC for reception from the MII */
1233 ETH_MACReceptionDisable(heth);
1235 /* Flush Transmit FIFO */
1236 ETH_FlushTransmitFIFO(heth);
1238 /* Disable transmit state machine of the MAC for transmission on the MII */
1239 ETH_MACTransmissionDisable(heth);
1241 /* Set the ETH state*/
1242 heth->State = HAL_ETH_STATE_READY;
1244 /* Process Unlocked */
1247 /* Return function status */
1252 * @brief Set ETH MAC Configuration.
1253 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1254 * the configuration information for ETHERNET module
1255 * @param macconf: MAC Configuration structure
1256 * @retval HAL status
1258 HAL_StatusTypeDef HAL_ETH_ConfigMAC(ETH_HandleTypeDef *heth, ETH_MACInitTypeDef *macconf)
1260 uint32_t tmpreg = 0;
1262 /* Process Locked */
1265 /* Set the ETH peripheral state to BUSY */
1266 heth->State= HAL_ETH_STATE_BUSY;
1268 assert_param(IS_ETH_SPEED(heth->Init.Speed));
1269 assert_param(IS_ETH_DUPLEX_MODE(heth->Init.DuplexMode));
1271 if (macconf != HAL_NULL)
1273 /* Check the parameters */
1274 assert_param(IS_ETH_WATCHDOG(macconf->Watchdog));
1275 assert_param(IS_ETH_JABBER(macconf->Jabber));
1276 assert_param(IS_ETH_INTER_FRAME_GAP(macconf->InterFrameGap));
1277 assert_param(IS_ETH_CARRIER_SENSE(macconf->CarrierSense));
1278 assert_param(IS_ETH_RECEIVE_OWN(macconf->ReceiveOwn));
1279 assert_param(IS_ETH_LOOPBACK_MODE(macconf->LoopbackMode));
1280 assert_param(IS_ETH_CHECKSUM_OFFLOAD(macconf->ChecksumOffload));
1281 assert_param(IS_ETH_RETRY_TRANSMISSION(macconf->RetryTransmission));
1282 assert_param(IS_ETH_AUTOMATIC_PADCRC_STRIP(macconf->AutomaticPadCRCStrip));
1283 assert_param(IS_ETH_BACKOFF_LIMIT(macconf->BackOffLimit));
1284 assert_param(IS_ETH_DEFERRAL_CHECK(macconf->DeferralCheck));
1285 assert_param(IS_ETH_RECEIVE_ALL(macconf->ReceiveAll));
1286 assert_param(IS_ETH_SOURCE_ADDR_FILTER(macconf->SourceAddrFilter));
1287 assert_param(IS_ETH_CONTROL_FRAMES(macconf->PassControlFrames));
1288 assert_param(IS_ETH_BROADCAST_FRAMES_RECEPTION(macconf->BroadcastFramesReception));
1289 assert_param(IS_ETH_DESTINATION_ADDR_FILTER(macconf->DestinationAddrFilter));
1290 assert_param(IS_ETH_PROMISCIOUS_MODE(macconf->PromiscuousMode));
1291 assert_param(IS_ETH_MULTICAST_FRAMES_FILTER(macconf->MulticastFramesFilter));
1292 assert_param(IS_ETH_UNICAST_FRAMES_FILTER(macconf->UnicastFramesFilter));
1293 assert_param(IS_ETH_PAUSE_TIME(macconf->PauseTime));
1294 assert_param(IS_ETH_ZEROQUANTA_PAUSE(macconf->ZeroQuantaPause));
1295 assert_param(IS_ETH_PAUSE_LOW_THRESHOLD(macconf->PauseLowThreshold));
1296 assert_param(IS_ETH_UNICAST_PAUSE_FRAME_DETECT(macconf->UnicastPauseFrameDetect));
1297 assert_param(IS_ETH_RECEIVE_FLOWCONTROL(macconf->ReceiveFlowControl));
1298 assert_param(IS_ETH_TRANSMIT_FLOWCONTROL(macconf->TransmitFlowControl));
1299 assert_param(IS_ETH_VLAN_TAG_COMPARISON(macconf->VLANTagComparison));
1300 assert_param(IS_ETH_VLAN_TAG_IDENTIFIER(macconf->VLANTagIdentifier));
1302 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1303 /* Get the ETHERNET MACCR value */
1304 tmpreg = (heth->Instance)->MACCR;
1305 /* Clear WD, PCE, PS, TE and RE bits */
1306 tmpreg &= MACCR_CLEAR_MASK;
1308 tmpreg |= (uint32_t)(macconf->Watchdog |
1310 macconf->InterFrameGap |
1311 macconf->CarrierSense |
1312 (heth->Init).Speed |
1313 macconf->ReceiveOwn |
1314 macconf->LoopbackMode |
1315 (heth->Init).DuplexMode |
1316 macconf->ChecksumOffload |
1317 macconf->RetryTransmission |
1318 macconf->AutomaticPadCRCStrip |
1319 macconf->BackOffLimit |
1320 macconf->DeferralCheck);
1322 /* Write to ETHERNET MACCR */
1323 (heth->Instance)->MACCR = (uint32_t)tmpreg;
1325 /* Wait until the write operation will be taken into account :
1326 at least four TX_CLK/RX_CLK clock cycles */
1327 tmpreg = (heth->Instance)->MACCR;
1328 HAL_Delay(ETH_REG_WRITE_DELAY);
1329 (heth->Instance)->MACCR = tmpreg;
1331 /*----------------------- ETHERNET MACFFR Configuration --------------------*/
1332 /* Write to ETHERNET MACFFR */
1333 (heth->Instance)->MACFFR = (uint32_t)(macconf->ReceiveAll |
1334 macconf->SourceAddrFilter |
1335 macconf->PassControlFrames |
1336 macconf->BroadcastFramesReception |
1337 macconf->DestinationAddrFilter |
1338 macconf->PromiscuousMode |
1339 macconf->MulticastFramesFilter |
1340 macconf->UnicastFramesFilter);
1342 /* Wait until the write operation will be taken into account :
1343 at least four TX_CLK/RX_CLK clock cycles */
1344 tmpreg = (heth->Instance)->MACFFR;
1345 HAL_Delay(ETH_REG_WRITE_DELAY);
1346 (heth->Instance)->MACFFR = tmpreg;
1348 /*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
1349 /* Write to ETHERNET MACHTHR */
1350 (heth->Instance)->MACHTHR = (uint32_t)macconf->HashTableHigh;
1352 /* Write to ETHERNET MACHTLR */
1353 (heth->Instance)->MACHTLR = (uint32_t)macconf->HashTableLow;
1354 /*----------------------- ETHERNET MACFCR Configuration --------------------*/
1356 /* Get the ETHERNET MACFCR value */
1357 tmpreg = (heth->Instance)->MACFCR;
1359 tmpreg &= MACFCR_CLEAR_MASK;
1361 tmpreg |= (uint32_t)((macconf->PauseTime << 16) |
1362 macconf->ZeroQuantaPause |
1363 macconf->PauseLowThreshold |
1364 macconf->UnicastPauseFrameDetect |
1365 macconf->ReceiveFlowControl |
1366 macconf->TransmitFlowControl);
1368 /* Write to ETHERNET MACFCR */
1369 (heth->Instance)->MACFCR = (uint32_t)tmpreg;
1371 /* Wait until the write operation will be taken into account :
1372 at least four TX_CLK/RX_CLK clock cycles */
1373 tmpreg = (heth->Instance)->MACFCR;
1374 HAL_Delay(ETH_REG_WRITE_DELAY);
1375 (heth->Instance)->MACFCR = tmpreg;
1377 /*----------------------- ETHERNET MACVLANTR Configuration -----------------*/
1378 (heth->Instance)->MACVLANTR = (uint32_t)(macconf->VLANTagComparison |
1379 macconf->VLANTagIdentifier);
1381 /* Wait until the write operation will be taken into account :
1382 at least four TX_CLK/RX_CLK clock cycles */
1383 tmpreg = (heth->Instance)->MACVLANTR;
1384 HAL_Delay(ETH_REG_WRITE_DELAY);
1385 (heth->Instance)->MACVLANTR = tmpreg;
1387 else /* macconf == NULL : here we just configure Speed and Duplex mode */
1389 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1390 /* Get the ETHERNET MACCR value */
1391 tmpreg = (heth->Instance)->MACCR;
1393 /* Clear FES and DM bits */
1394 tmpreg &= ~((uint32_t)0x00004800);
1396 tmpreg |= (uint32_t)(heth->Init.Speed | heth->Init.DuplexMode);
1398 /* Write to ETHERNET MACCR */
1399 (heth->Instance)->MACCR = (uint32_t)tmpreg;
1401 /* Wait until the write operation will be taken into account:
1402 at least four TX_CLK/RX_CLK clock cycles */
1403 tmpreg = (heth->Instance)->MACCR;
1404 HAL_Delay(ETH_REG_WRITE_DELAY);
1405 (heth->Instance)->MACCR = tmpreg;
1408 /* Set the ETH state to Ready */
1409 heth->State= HAL_ETH_STATE_READY;
1411 /* Process Unlocked */
1414 /* Return function status */
1419 * @brief Sets ETH DMA Configuration.
1420 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1421 * the configuration information for ETHERNET module
1422 * @param dmaconf: DMA Configuration structure
1423 * @retval HAL status
1425 HAL_StatusTypeDef HAL_ETH_ConfigDMA(ETH_HandleTypeDef *heth, ETH_DMAInitTypeDef *dmaconf)
1427 uint32_t tmpreg = 0;
1429 /* Process Locked */
1432 /* Set the ETH peripheral state to BUSY */
1433 heth->State= HAL_ETH_STATE_BUSY;
1435 /* Check parameters */
1436 assert_param(IS_ETH_DROP_TCPIP_CHECKSUM_FRAME(dmaconf->DropTCPIPChecksumErrorFrame));
1437 assert_param(IS_ETH_RECEIVE_STORE_FORWARD(dmaconf->ReceiveStoreForward));
1438 assert_param(IS_ETH_FLUSH_RECEIVE_FRAME(dmaconf->FlushReceivedFrame));
1439 assert_param(IS_ETH_TRANSMIT_STORE_FORWARD(dmaconf->TransmitStoreForward));
1440 assert_param(IS_ETH_TRANSMIT_THRESHOLD_CONTROL(dmaconf->TransmitThresholdControl));
1441 assert_param(IS_ETH_FORWARD_ERROR_FRAMES(dmaconf->ForwardErrorFrames));
1442 assert_param(IS_ETH_FORWARD_UNDERSIZED_GOOD_FRAMES(dmaconf->ForwardUndersizedGoodFrames));
1443 assert_param(IS_ETH_RECEIVE_THRESHOLD_CONTROL(dmaconf->ReceiveThresholdControl));
1444 assert_param(IS_ETH_SECOND_FRAME_OPERATE(dmaconf->SecondFrameOperate));
1445 assert_param(IS_ETH_ADDRESS_ALIGNED_BEATS(dmaconf->AddressAlignedBeats));
1446 assert_param(IS_ETH_FIXED_BURST(dmaconf->FixedBurst));
1447 assert_param(IS_ETH_RXDMA_BURST_LENGTH(dmaconf->RxDMABurstLength));
1448 assert_param(IS_ETH_TXDMA_BURST_LENGTH(dmaconf->TxDMABurstLength));
1449 assert_param(IS_ETH_ENHANCED_DESCRIPTOR_FORMAT(dmaconf->EnhancedDescriptorFormat));
1450 assert_param(IS_ETH_DMA_DESC_SKIP_LENGTH(dmaconf->DescriptorSkipLength));
1451 assert_param(IS_ETH_DMA_ARBITRATION_ROUNDROBIN_RXTX(dmaconf->DMAArbitration));
1453 /*----------------------- ETHERNET DMAOMR Configuration --------------------*/
1454 /* Get the ETHERNET DMAOMR value */
1455 tmpreg = (heth->Instance)->DMAOMR;
1457 tmpreg &= DMAOMR_CLEAR_MASK;
1459 tmpreg |= (uint32_t)(dmaconf->DropTCPIPChecksumErrorFrame |
1460 dmaconf->ReceiveStoreForward |
1461 dmaconf->FlushReceivedFrame |
1462 dmaconf->TransmitStoreForward |
1463 dmaconf->TransmitThresholdControl |
1464 dmaconf->ForwardErrorFrames |
1465 dmaconf->ForwardUndersizedGoodFrames |
1466 dmaconf->ReceiveThresholdControl |
1467 dmaconf->SecondFrameOperate);
1469 /* Write to ETHERNET DMAOMR */
1470 (heth->Instance)->DMAOMR = (uint32_t)tmpreg;
1472 /* Wait until the write operation will be taken into account:
1473 at least four TX_CLK/RX_CLK clock cycles */
1474 tmpreg = (heth->Instance)->DMAOMR;
1475 HAL_Delay(ETH_REG_WRITE_DELAY);
1476 (heth->Instance)->DMAOMR = tmpreg;
1478 /*----------------------- ETHERNET DMABMR Configuration --------------------*/
1479 (heth->Instance)->DMABMR = (uint32_t)(dmaconf->AddressAlignedBeats |
1480 dmaconf->FixedBurst |
1481 dmaconf->RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
1482 dmaconf->TxDMABurstLength |
1483 dmaconf->EnhancedDescriptorFormat |
1484 (dmaconf->DescriptorSkipLength << 2) |
1485 dmaconf->DMAArbitration |
1486 ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
1488 /* Wait until the write operation will be taken into account:
1489 at least four TX_CLK/RX_CLK clock cycles */
1490 tmpreg = (heth->Instance)->DMABMR;
1491 HAL_Delay(ETH_REG_WRITE_DELAY);
1492 (heth->Instance)->DMABMR = tmpreg;
1494 /* Set the ETH state to Ready */
1495 heth->State= HAL_ETH_STATE_READY;
1497 /* Process Unlocked */
1500 /* Return function status */
1508 /** @defgroup ETH_Group4 Peripheral State functions
1509 * @brief Peripheral State functions
1512 ===============================================================================
1513 ##### Peripheral State functions #####
1514 ===============================================================================
1516 This subsection permits to get in run-time the status of the peripheral
1518 (+) Get the ETH handle state:
1527 * @brief Return the ETH HAL state
1528 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1529 * the configuration information for ETHERNET module
1532 HAL_ETH_StateTypeDef HAL_ETH_GetState(ETH_HandleTypeDef *heth)
1534 /* Return ETH state */
1543 * @brief Configures Ethernet MAC and DMA with default parameters.
1544 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1545 * the configuration information for ETHERNET module
1546 * @param err: Ethernet Init error
1547 * @retval HAL status
1549 static void ETH_MACDMAConfig(ETH_HandleTypeDef *heth, uint32_t err)
1551 ETH_MACInitTypeDef macinit;
1552 ETH_DMAInitTypeDef dmainit;
1553 uint32_t tmpreg = 0;
1555 if (err != ETH_SUCCESS) /* Auto-negotiation failed */
1557 /* Set Ethernet duplex mode to Full-duplex */
1558 (heth->Init).DuplexMode = ETH_MODE_FULLDUPLEX;
1560 /* Set Ethernet speed to 100M */
1561 (heth->Init).Speed = ETH_SPEED_100M;
1564 /* Ethernet MAC default initialization **************************************/
1565 macinit.Watchdog = ETH_WATCHDOG_ENABLE;
1566 macinit.Jabber = ETH_JABBER_ENABLE;
1567 macinit.InterFrameGap = ETH_INTERFRAMEGAP_96BIT;
1568 macinit.CarrierSense = ETH_CARRIERSENCE_ENABLE;
1569 macinit.ReceiveOwn = ETH_RECEIVEOWN_ENABLE;
1570 macinit.LoopbackMode = ETH_LOOPBACKMODE_DISABLE;
1571 if(heth->Init.ChecksumMode == ETH_CHECKSUM_BY_HARDWARE)
1573 macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_ENABLE;
1577 macinit.ChecksumOffload = ETH_CHECKSUMOFFLAOD_DISABLE;
1579 macinit.RetryTransmission = ETH_RETRYTRANSMISSION_DISABLE;
1580 macinit.AutomaticPadCRCStrip = ETH_AUTOMATICPADCRCSTRIP_DISABLE;
1581 macinit.BackOffLimit = ETH_BACKOFFLIMIT_10;
1582 macinit.DeferralCheck = ETH_DEFFERRALCHECK_DISABLE;
1583 macinit.ReceiveAll = ETH_RECEIVEAll_DISABLE;
1584 macinit.SourceAddrFilter = ETH_SOURCEADDRFILTER_DISABLE;
1585 macinit.PassControlFrames = ETH_PASSCONTROLFRAMES_BLOCKALL;
1586 macinit.BroadcastFramesReception = ETH_BROADCASTFRAMESRECEPTION_ENABLE;
1587 macinit.DestinationAddrFilter = ETH_DESTINATIONADDRFILTER_NORMAL;
1588 macinit.PromiscuousMode = ETH_PROMISCIOUSMODE_DISABLE;
1589 macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_PERFECT;
1590 macinit.UnicastFramesFilter = ETH_UNICASTFRAMESFILTER_PERFECT;
1591 macinit.HashTableHigh = 0x0;
1592 macinit.HashTableLow = 0x0;
1593 macinit.PauseTime = 0x0;
1594 macinit.ZeroQuantaPause = ETH_ZEROQUANTAPAUSE_DISABLE;
1595 macinit.PauseLowThreshold = ETH_PAUSELOWTHRESHOLD_MINUS4;
1596 macinit.UnicastPauseFrameDetect = ETH_UNICASTPAUSEFRAMEDETECT_DISABLE;
1597 macinit.ReceiveFlowControl = ETH_RECEIVEFLOWCONTROL_DISABLE;
1598 macinit.TransmitFlowControl = ETH_TRANSMITFLOWCONTROL_DISABLE;
1599 macinit.VLANTagComparison = ETH_VLANTAGCOMPARISON_16BIT;
1600 macinit.VLANTagIdentifier = 0x0;
1602 /*------------------------ ETHERNET MACCR Configuration --------------------*/
1603 /* Get the ETHERNET MACCR value */
1604 tmpreg = (heth->Instance)->MACCR;
1605 /* Clear WD, PCE, PS, TE and RE bits */
1606 tmpreg &= MACCR_CLEAR_MASK;
1607 /* Set the WD bit according to ETH Watchdog value */
1608 /* Set the JD: bit according to ETH Jabber value */
1609 /* Set the IFG bit according to ETH InterFrameGap value */
1610 /* Set the DCRS bit according to ETH CarrierSense value */
1611 /* Set the FES bit according to ETH Speed value */
1612 /* Set the DO bit according to ETH ReceiveOwn value */
1613 /* Set the LM bit according to ETH LoopbackMode value */
1614 /* Set the DM bit according to ETH Mode value */
1615 /* Set the IPCO bit according to ETH ChecksumOffload value */
1616 /* Set the DR bit according to ETH RetryTransmission value */
1617 /* Set the ACS bit according to ETH AutomaticPadCRCStrip value */
1618 /* Set the BL bit according to ETH BackOffLimit value */
1619 /* Set the DC bit according to ETH DeferralCheck value */
1620 tmpreg |= (uint32_t)(macinit.Watchdog |
1622 macinit.InterFrameGap |
1623 macinit.CarrierSense |
1624 (heth->Init).Speed |
1625 macinit.ReceiveOwn |
1626 macinit.LoopbackMode |
1627 (heth->Init).DuplexMode |
1628 macinit.ChecksumOffload |
1629 macinit.RetryTransmission |
1630 macinit.AutomaticPadCRCStrip |
1631 macinit.BackOffLimit |
1632 macinit.DeferralCheck);
1634 /* Write to ETHERNET MACCR */
1635 (heth->Instance)->MACCR = (uint32_t)tmpreg;
1637 /* Wait until the write operation will be taken into account:
1638 at least four TX_CLK/RX_CLK clock cycles */
1639 tmpreg = (heth->Instance)->MACCR;
1640 HAL_Delay(ETH_REG_WRITE_DELAY);
1641 (heth->Instance)->MACCR = tmpreg;
1643 /*----------------------- ETHERNET MACFFR Configuration --------------------*/
1644 /* Set the RA bit according to ETH ReceiveAll value */
1645 /* Set the SAF and SAIF bits according to ETH SourceAddrFilter value */
1646 /* Set the PCF bit according to ETH PassControlFrames value */
1647 /* Set the DBF bit according to ETH BroadcastFramesReception value */
1648 /* Set the DAIF bit according to ETH DestinationAddrFilter value */
1649 /* Set the PR bit according to ETH PromiscuousMode value */
1650 /* Set the PM, HMC and HPF bits according to ETH MulticastFramesFilter value */
1651 /* Set the HUC and HPF bits according to ETH UnicastFramesFilter value */
1652 /* Write to ETHERNET MACFFR */
1653 (heth->Instance)->MACFFR = (uint32_t)(macinit.ReceiveAll |
1654 macinit.SourceAddrFilter |
1655 macinit.PassControlFrames |
1656 macinit.BroadcastFramesReception |
1657 macinit.DestinationAddrFilter |
1658 macinit.PromiscuousMode |
1659 macinit.MulticastFramesFilter |
1660 macinit.UnicastFramesFilter);
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)->MACFFR;
1665 HAL_Delay(ETH_REG_WRITE_DELAY);
1666 (heth->Instance)->MACFFR = tmpreg;
1668 /*--------------- ETHERNET MACHTHR and MACHTLR Configuration --------------*/
1669 /* Write to ETHERNET MACHTHR */
1670 (heth->Instance)->MACHTHR = (uint32_t)macinit.HashTableHigh;
1672 /* Write to ETHERNET MACHTLR */
1673 (heth->Instance)->MACHTLR = (uint32_t)macinit.HashTableLow;
1674 /*----------------------- ETHERNET MACFCR Configuration -------------------*/
1676 /* Get the ETHERNET MACFCR value */
1677 tmpreg = (heth->Instance)->MACFCR;
1679 tmpreg &= MACFCR_CLEAR_MASK;
1681 /* Set the PT bit according to ETH PauseTime value */
1682 /* Set the DZPQ bit according to ETH ZeroQuantaPause value */
1683 /* Set the PLT bit according to ETH PauseLowThreshold value */
1684 /* Set the UP bit according to ETH UnicastPauseFrameDetect value */
1685 /* Set the RFE bit according to ETH ReceiveFlowControl value */
1686 /* Set the TFE bit according to ETH TransmitFlowControl value */
1687 tmpreg |= (uint32_t)((macinit.PauseTime << 16) |
1688 macinit.ZeroQuantaPause |
1689 macinit.PauseLowThreshold |
1690 macinit.UnicastPauseFrameDetect |
1691 macinit.ReceiveFlowControl |
1692 macinit.TransmitFlowControl);
1694 /* Write to ETHERNET MACFCR */
1695 (heth->Instance)->MACFCR = (uint32_t)tmpreg;
1697 /* Wait until the write operation will be taken into account:
1698 at least four TX_CLK/RX_CLK clock cycles */
1699 tmpreg = (heth->Instance)->MACFCR;
1700 HAL_Delay(ETH_REG_WRITE_DELAY);
1701 (heth->Instance)->MACFCR = tmpreg;
1703 /*----------------------- ETHERNET MACVLANTR Configuration ----------------*/
1704 /* Set the ETV bit according to ETH VLANTagComparison value */
1705 /* Set the VL bit according to ETH VLANTagIdentifier value */
1706 (heth->Instance)->MACVLANTR = (uint32_t)(macinit.VLANTagComparison |
1707 macinit.VLANTagIdentifier);
1709 /* Wait until the write operation will be taken into account:
1710 at least four TX_CLK/RX_CLK clock cycles */
1711 tmpreg = (heth->Instance)->MACVLANTR;
1712 HAL_Delay(ETH_REG_WRITE_DELAY);
1713 (heth->Instance)->MACVLANTR = tmpreg;
1715 /* Ethernet DMA default initialization ************************************/
1716 dmainit.DropTCPIPChecksumErrorFrame = ETH_DROPTCPIPCHECKSUMERRORFRAME_ENABLE;
1717 dmainit.ReceiveStoreForward = ETH_RECEIVESTOREFORWARD_ENABLE;
1718 dmainit.FlushReceivedFrame = ETH_FLUSHRECEIVEDFRAME_ENABLE;
1719 dmainit.TransmitStoreForward = ETH_TRANSMITSTOREFORWARD_ENABLE;
1720 dmainit.TransmitThresholdControl = ETH_TRANSMITTHRESHOLDCONTROL_64BYTES;
1721 dmainit.ForwardErrorFrames = ETH_FORWARDERRORFRAMES_DISABLE;
1722 dmainit.ForwardUndersizedGoodFrames = ETH_FORWARDUNDERSIZEDGOODFRAMES_DISABLE;
1723 dmainit.ReceiveThresholdControl = ETH_RECEIVEDTHRESHOLDCONTROL_64BYTES;
1724 dmainit.SecondFrameOperate = ETH_SECONDFRAMEOPERARTE_ENABLE;
1725 dmainit.AddressAlignedBeats = ETH_ADDRESSALIGNEDBEATS_ENABLE;
1726 dmainit.FixedBurst = ETH_FIXEDBURST_ENABLE;
1727 dmainit.RxDMABurstLength = ETH_RXDMABURSTLENGTH_32BEAT;
1728 dmainit.TxDMABurstLength = ETH_TXDMABURSTLENGTH_32BEAT;
1729 dmainit.EnhancedDescriptorFormat = ETH_DMAENHANCEDDESCRIPTOR_ENABLE;
1730 dmainit.DescriptorSkipLength = 0x0;
1731 dmainit.DMAArbitration = ETH_DMAARBITRATION_ROUNDROBIN_RXTX_1_1;
1733 /* Get the ETHERNET DMAOMR value */
1734 tmpreg = (heth->Instance)->DMAOMR;
1736 tmpreg &= DMAOMR_CLEAR_MASK;
1738 /* Set the DT bit according to ETH DropTCPIPChecksumErrorFrame value */
1739 /* Set the RSF bit according to ETH ReceiveStoreForward value */
1740 /* Set the DFF bit according to ETH FlushReceivedFrame value */
1741 /* Set the TSF bit according to ETH TransmitStoreForward value */
1742 /* Set the TTC bit according to ETH TransmitThresholdControl value */
1743 /* Set the FEF bit according to ETH ForwardErrorFrames value */
1744 /* Set the FUF bit according to ETH ForwardUndersizedGoodFrames value */
1745 /* Set the RTC bit according to ETH ReceiveThresholdControl value */
1746 /* Set the OSF bit according to ETH SecondFrameOperate value */
1747 tmpreg |= (uint32_t)(dmainit.DropTCPIPChecksumErrorFrame |
1748 dmainit.ReceiveStoreForward |
1749 dmainit.FlushReceivedFrame |
1750 dmainit.TransmitStoreForward |
1751 dmainit.TransmitThresholdControl |
1752 dmainit.ForwardErrorFrames |
1753 dmainit.ForwardUndersizedGoodFrames |
1754 dmainit.ReceiveThresholdControl |
1755 dmainit.SecondFrameOperate);
1757 /* Write to ETHERNET DMAOMR */
1758 (heth->Instance)->DMAOMR = (uint32_t)tmpreg;
1760 /* Wait until the write operation will be taken into account:
1761 at least four TX_CLK/RX_CLK clock cycles */
1762 tmpreg = (heth->Instance)->DMAOMR;
1763 HAL_Delay(ETH_REG_WRITE_DELAY);
1764 (heth->Instance)->DMAOMR = tmpreg;
1766 /*----------------------- ETHERNET DMABMR Configuration ------------------*/
1767 /* Set the AAL bit according to ETH AddressAlignedBeats value */
1768 /* Set the FB bit according to ETH FixedBurst value */
1769 /* Set the RPBL and 4*PBL bits according to ETH RxDMABurstLength value */
1770 /* Set the PBL and 4*PBL bits according to ETH TxDMABurstLength value */
1771 /* Set the Enhanced DMA descriptors bit according to ETH EnhancedDescriptorFormat value*/
1772 /* Set the DSL bit according to ETH DesciptorSkipLength value */
1773 /* Set the PR and DA bits according to ETH DMAArbitration value */
1774 (heth->Instance)->DMABMR = (uint32_t)(dmainit.AddressAlignedBeats |
1775 dmainit.FixedBurst |
1776 dmainit.RxDMABurstLength | /* !! if 4xPBL is selected for Tx or Rx it is applied for the other */
1777 dmainit.TxDMABurstLength |
1778 dmainit.EnhancedDescriptorFormat |
1779 (dmainit.DescriptorSkipLength << 2) |
1780 dmainit.DMAArbitration |
1781 ETH_DMABMR_USP); /* Enable use of separate PBL for Rx and Tx */
1783 /* Wait until the write operation will be taken into account:
1784 at least four TX_CLK/RX_CLK clock cycles */
1785 tmpreg = (heth->Instance)->DMABMR;
1786 HAL_Delay(ETH_REG_WRITE_DELAY);
1787 (heth->Instance)->DMABMR = tmpreg;
1789 if((heth->Init).RxMode == ETH_RXINTERRUPT_MODE)
1791 /* Enable the Ethernet Rx Interrupt */
1792 __HAL_ETH_DMA_ENABLE_IT((heth), ETH_DMA_IT_NIS | ETH_DMA_IT_R);
1795 /* Initialize MAC address in ethernet MAC */
1796 ETH_MACAddressConfig(heth, ETH_MAC_ADDRESS0, heth->Init.MACAddr);
1800 * @brief Configures the selected MAC address.
1801 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1802 * the configuration information for ETHERNET module
1803 * @param MacAddr: The MAC address to configure
1804 * This parameter can be one of the following values:
1805 * @arg ETH_MAC_Address0: MAC Address0
1806 * @arg ETH_MAC_Address1: MAC Address1
1807 * @arg ETH_MAC_Address2: MAC Address2
1808 * @arg ETH_MAC_Address3: MAC Address3
1809 * @param Addr: Pointer to MAC address buffer data (6 bytes)
1810 * @retval HAL status
1812 static void ETH_MACAddressConfig(ETH_HandleTypeDef *heth, uint32_t MacAddr, uint8_t *Addr)
1816 /* Check the parameters */
1817 assert_param(IS_ETH_MAC_ADDRESS0123(MacAddr));
1819 /* Calculate the selected MAC address high register */
1820 tmpreg = ((uint32_t)Addr[5] << 8) | (uint32_t)Addr[4];
1821 /* Load the selected MAC address high register */
1822 (*(__IO uint32_t *)((uint32_t)(ETH_MAC_ADDR_HBASE + MacAddr))) = tmpreg;
1823 /* Calculate the selected MAC address low register */
1824 tmpreg = ((uint32_t)Addr[3] << 24) | ((uint32_t)Addr[2] << 16) | ((uint32_t)Addr[1] << 8) | Addr[0];
1826 /* Load the selected MAC address low register */
1827 (*(__IO uint32_t *)((uint32_t)(ETH_MAC_ADDR_LBASE + MacAddr))) = tmpreg;
1831 * @brief Enables the MAC transmission.
1832 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1833 * the configuration information for ETHERNET module
1836 static void ETH_MACTransmissionEnable(ETH_HandleTypeDef *heth)
1838 __IO uint32_t tmpreg = 0;
1840 /* Enable the MAC transmission */
1841 (heth->Instance)->MACCR |= ETH_MACCR_TE;
1843 /* Wait until the write operation will be taken into account:
1844 at least four TX_CLK/RX_CLK clock cycles */
1845 tmpreg = (heth->Instance)->MACCR;
1846 HAL_Delay(ETH_REG_WRITE_DELAY);
1847 (heth->Instance)->MACCR = tmpreg;
1851 * @brief Disables the MAC transmission.
1852 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1853 * the configuration information for ETHERNET module
1856 static void ETH_MACTransmissionDisable(ETH_HandleTypeDef *heth)
1858 __IO uint32_t tmpreg = 0;
1860 /* Disable the MAC transmission */
1861 (heth->Instance)->MACCR &= ~ETH_MACCR_TE;
1863 /* Wait until the write operation will be taken into account:
1864 at least four TX_CLK/RX_CLK clock cycles */
1865 tmpreg = (heth->Instance)->MACCR;
1866 HAL_Delay(ETH_REG_WRITE_DELAY);
1867 (heth->Instance)->MACCR = tmpreg;
1871 * @brief Enables the MAC reception.
1872 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1873 * the configuration information for ETHERNET module
1876 static void ETH_MACReceptionEnable(ETH_HandleTypeDef *heth)
1878 __IO uint32_t tmpreg = 0;
1880 /* Enable the MAC reception */
1881 (heth->Instance)->MACCR |= ETH_MACCR_RE;
1883 /* Wait until the write operation will be taken into account:
1884 at least four TX_CLK/RX_CLK clock cycles */
1885 tmpreg = (heth->Instance)->MACCR;
1886 HAL_Delay(ETH_REG_WRITE_DELAY);
1887 (heth->Instance)->MACCR = tmpreg;
1891 * @brief Disables the MAC reception.
1892 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1893 * the configuration information for ETHERNET module
1896 static void ETH_MACReceptionDisable(ETH_HandleTypeDef *heth)
1898 __IO uint32_t tmpreg = 0;
1900 /* Disable the MAC reception */
1901 (heth->Instance)->MACCR &= ~ETH_MACCR_RE;
1903 /* Wait until the write operation will be taken into account:
1904 at least four TX_CLK/RX_CLK clock cycles */
1905 tmpreg = (heth->Instance)->MACCR;
1906 HAL_Delay(ETH_REG_WRITE_DELAY);
1907 (heth->Instance)->MACCR = tmpreg;
1911 * @brief Enables the DMA transmission.
1912 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1913 * the configuration information for ETHERNET module
1916 static void ETH_DMATransmissionEnable(ETH_HandleTypeDef *heth)
1918 /* Enable the DMA transmission */
1919 (heth->Instance)->DMAOMR |= ETH_DMAOMR_ST;
1923 * @brief Disables the DMA transmission.
1924 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1925 * the configuration information for ETHERNET module
1928 static void ETH_DMATransmissionDisable(ETH_HandleTypeDef *heth)
1930 /* Disable the DMA transmission */
1931 (heth->Instance)->DMAOMR &= ~ETH_DMAOMR_ST;
1935 * @brief Enables the DMA reception.
1936 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1937 * the configuration information for ETHERNET module
1940 static void ETH_DMAReceptionEnable(ETH_HandleTypeDef *heth)
1942 /* Enable the DMA reception */
1943 (heth->Instance)->DMAOMR |= ETH_DMAOMR_SR;
1947 * @brief Disables the DMA reception.
1948 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1949 * the configuration information for ETHERNET module
1952 static void ETH_DMAReceptionDisable(ETH_HandleTypeDef *heth)
1954 /* Disable the DMA reception */
1955 (heth->Instance)->DMAOMR &= ~ETH_DMAOMR_SR;
1959 * @brief Clears the ETHERNET transmit FIFO.
1960 * @param heth: pointer to a ETH_HandleTypeDef structure that contains
1961 * the configuration information for ETHERNET module
1964 static void ETH_FlushTransmitFIFO(ETH_HandleTypeDef *heth)
1966 __IO uint32_t tmpreg = 0;
1968 /* Set the Flush Transmit FIFO bit */
1969 (heth->Instance)->DMAOMR |= ETH_DMAOMR_FTF;
1971 /* Wait until the write operation will be taken into account:
1972 at least four TX_CLK/RX_CLK clock cycles */
1973 tmpreg = (heth->Instance)->DMAOMR;
1974 HAL_Delay(ETH_REG_WRITE_DELAY);
1975 (heth->Instance)->DMAOMR = tmpreg;
1982 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
1983 #endif /* HAL_ETH_MODULE_ENABLED */
1992 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/