How to configure and ping Ethernet on STM32
Learn how to configure Ethernet on STM32 boards with CubeMX and LWIP. This first tutorial covers hardware setup, pin mapping, PHY address, DMA descriptors, MPU settings, and a successful ping test. Ideal for STM32 F7/H7 users. The project is available for download at the end of the post.

This tutorial marks the beginning of the STM32 Ethernet series. In this first part, we will focus on configuring the hardware setup.
For some microcontrollers, the configuration is relatively straightforward using the default settings. However, for others—particularly those based on the Cortex-M7 architecture—this process can become more complex due to issues like data coherency arising from cacheable memory regions.
I will do my best to explain each step clearly and thoroughly. To gain a deeper understanding of the impact of various settings, I highly recommend watching the video included at the end of this post.
VIDEO TUTORIAL
You can check the video to see the complete explanation and working of this project.
Check out the Video Below
Introducing the Ethernet Peripheral on STM32
The Ethernet peripheral on STM32 microcontrollers enables high-speed wired communication based on the IEEE 802.3 standard. It allows STM32 devices to connect directly to a local area network (LAN) and communicate with other networked devices using TCP/IP or UDP protocols, typically via the LWIP (Lightweight IP) stack. STM32 Ethernet is supported on a range of STM32 series, including STM32F4, F7, and H7 families, offering integrated MAC (Media Access Controller) and support for external PHY transceivers.
This peripheral is ideal for applications requiring reliable and fast data exchange, such as industrial automation, IoT gateways, networked sensors, and embedded web servers.
Some of its important features are:
- Integrated 10/100 Mbps MAC: The built-in Ethernet MAC supports both 10 Mbps and 100 Mbps communication speeds in full-duplex and half-duplex modes.
- DMA Support for Efficient Data Transfer: It includes dedicated DMA (Direct Memory Access) channels to offload data transfer tasks, reducing CPU load and improving real-time performance.
- IEEE 1588 Time Stamp Support (on selected MCUs): Some STM32 MCUs offer hardware support for IEEE 1588 Precision Time Protocol (PTP), enabling accurate time synchronization across networked devices.
- Flexible PHY Interface (MII/RMII): The MAC supports both MII (Media Independent Interface) and RMII (Reduced MII), allowing connection with a wide range of external Ethernet PHY transceivers.
CubeMX CONFIGURATION
After enabling the Ethernet, you must cross check the pins configured by the CubeMX with the schematic of the board. Most of the times, the pins are configured incorrectly and it becomes one of the most popular error for the ethernet to not work.
Ethernet Configuration
There are many types of configurations available with different MCUs. Some MCUs let you configure the memory in the CubeMX, while others don’t. Some of the Boards have the MII Type Hardware, while other have RMII Type.
Below is the picture showing different configuration available in different ST Boards.
- The First board (Nucleo F207ZG) and second board (Disco F7508) uses the RMII pinout, while the third board (Disco H745) uses the MII pinout.
- On the first board we have the option to choose the PHY Address. This should be set to 0, if you are using the on board LAN Port, and it should be 1 in case of the external module.
- The 2nd and 3rd boards allow us to configure the memory addresses while the first one does not. If your board does not allow the memory configuration, you can simply skip those steps.
- The H745 discovery board is letting us configure the addresses for Tx and Rx DMA descriptors, and for the Rx Buffer as well. But the F7508 Discovery board does not let us configure the address for the Rx Buffer.
- Assuming that each DMA Descriptor takes 32 bits (MAXIMUM Possible), the RX Descriptor and Tx Descriptor have a maximum size of 128 Bytes (32×4) each.
- The Memory between them is spaced keeping this 128 bytes in mind.
- The Rx Buffer length is set to 1524 Bytes, but the number of RX buffers to be used can be defined later in the LWIP configuration. By default the cubeMX sets the number to 12 buffers. This would make the total length of Rx Buffer around 18KB.
- The memories are allocated in the SRAM region, whose properties can be modified later in the MPU.
LWIP Configuration
The LightWeight IP can be enabled in the middleware section. If the MCU does not let you enable it, make sure the cache (DCache and ICache) are enabled.
The most of the Configuration in the LWIP remains same. Except, some MCUs let us choose the address for the Heap.
- Here we are going to disable the DHCP, and configure a static IP for our ethernet module. I have set the IP 192.168.0.123 for the board.
- In the Key Option tab, I am using 5KB memory for the Heap. The location for this heap is defined as 0x30004000.
- In the Platform Settings tab, set the PHY as LAN8742.
MPU Configuration
We have the DMA Descriptors in the SRAM Region. This is why we need to configure the MPU.
If your MCU didn’t let you choose the memory region, then probably you don’t need to do it. But for the cortex M7 devices, this is a must, or else you will get hardfault.
Remember that during the configuration, we set up everything in the SRAM (0x30000000). The complete memory structure is shown in the image below.
Below is the image showing the MPU configuration for the above Region.
- Here I have selected the 32 KB region so that it will cover our total RAM region, which is around 24KB.
- The rest of the configuration is to set the region as non-cacheable region.
- This would prevent the cache coherency issue between the CPU and the DMA.
- This is explained in the cortex M7 playlist, so do check that out.
Other Configuration
If you are connecting the STM32 board to the Router, there is nothing you need to do at the computer end. But if you are connecting the ethernet cable directly to the computer, you need to configure your computer’s ethernet as per the images shown below.
Below is the configuration for a Windows computer.
Below is the configuration for Mac.
THE CODE
Once the project is generated, open the LWIP -> Target -> ethernetif.c file. Here you will some memory locations that needs to be defined in the flash script file.
We need to define these memory locations in the flash script file, as per the configuration done in the cubeMX.
Flash script Modification
Below is the code is for the H745 Discovery board according to the configuration done in the CubeMX.
.lwip_sec (NOLOAD) : {
. = ABSOLUTE(0x30000000);
*(.RxDecripSection)
. = ABSOLUTE(0x30000080);
*(.TxDecripSection)
. = ABSOLUTE(0x30000100);
*(.Rx_PoolSection)
} >RAM_D2
Below is the code is for the F7508 Discovery board according to the configuration done in the cubeMX.
.lwip_sec (NOLOAD) :
{
. = ABSOLUTE(0x2004C000);
*(.RxDecripSection)
. = ABSOLUTE(0x2004C0A0);
*(.TxDecripSection)
} >RAM
The main file
Since this tutorial is more focused on connection part, there is not much in the code. We will just test the ping to our IP Address, and the code for the same is shown below.
extern struct netif gnetif;
int main()
{
int wait = 10000000;
while (wait-- >0); // Wait for sometime before initializing the system
MPU_Config();
SCB_EnableICache();
SCB_EnableDCache();
HAL_Init();
HAL_Delay (1000); // wait for some time here
SystemClock_Config();
MX_GPIO_Init();
MX_LWIP_Init();
while (1)
{
ethernetif_input(&gnetif);
sys_check_timeouts();
}
}
RESULT
Below is the result from the ping test.
You can see the ping is working on the IP Address that we set during the configuration.
PROJECT DOWNLOAD
Info
You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.
You May Also Like
🙏 Support Us by Disabling Adblock
We rely on ad revenue to keep Controllerstech free and regularly updated. If you enjoy the content and find it helpful, please consider whitelisting our website in your ad blocker.
We promise to keep ads minimal and non-intrusive.
Thank you for your support! 💙
Hello,
I am using STM32H750B-DK Board and I am unable to ping it with your code, Could you provide me any points to look for.
Device is working properly , No Hard fault or any thing like that. Your help would be appreciated.
hello. i see your tutorials. a strange thing for me is that when I configure ETH and then add Lwip as a middleware in stmcube, there is no initializer for ETH peripheral in main func or anywhere else. even in your code I see no clue of initializing eth and ethernet dma. so my question is how is this gonna work? because this code does not work for me and the ping times out!
hello. i see your tutorials. a strange thing for me is that when I configure ETH and then add Lwip as a middleware in stmcube, there is no initializer for ETH peripheral in main func or anywhere else. even in your code I see no clue of initializing eth and ethernet dma. so my question is how is this gonna work? because this code does not work for me and the ping times out!
Hello. Thanks for sharing your knowledge.
I’m trying to connect a stm32H7 to lan8720 using freertos and lwip. I couldn’t communicate any packet. actually my code get stuck somewhere in lwip_init. I tried to trace it but it passed the error in debugging mode. could you help me with that? just a clue would be enough. thanks.
I had a similar issue with STM32F746 and it ended up that I configured my ETH as MII and when I checked up the board schematics, it only supported RMII. I corrected that and it has worked as a charm.
HI SIR I AM TRYING TO RECEIVE 13824 BYTES IN STM32F429VET6 IN RAWTCP IHV INCREASED HEAP SIZE MEMORY TO 15*1024?WHAT ELSE I NEED TO CHANGE I N LWIP?
Hi, thank for tutorial is very helpful
I’m building code in NUCLEO H7 144 board and ping OK
But when i reconnect the “Lan wire” – ping is not work, i must reset the power of kit and then work propely.
Have you get any comment for me?
Hi,
The Videos and the documentation are very helpful.
Thanks a lot!
Some parts needs to be updated, tutorial doesn’t work with MX 6.7.0 and STM32CubeH7 Firmware Package V1.11.0 ,thank you in advance
Him
How I can run this project with SPI of STM32H7?
Hi,
Thank you very much
I am new for ethernet, thanks for sharing.
Hi,
Thank you for sharing the detailed information. I am trying to implement the above Ping program using STM32f427VG on a custom designed PCB. In cubemx device configuration tool, I am not seeing a setting under ETH parameters to fill in the Tx/Rx descriptor details as in the tutorial. What could be the possible reason?
Hi, Its working. Sorry, I didn’t check your second video on ethernet.
Thanks again for the content.
Hello, I am also using the STM32F407VG but facing some issue describe below:
My code is working while debugging only and not outside of debug condition.
can you help me ?
hello
first of all thank you
I am working with lan8720 and stm32f767zi. I generated code using stm32cubeid with lwip and eth driver. Code is compiled successful and I connect lan8720 with stm32f767 as per RMII mode. I put MX_LWIP_Init() in main like you Now I flash the programme led is blinking but Ethernet didn’t get IP ,ethernet link not up i can t ping the ip adress .So any one can help me what configuration required while generating code . If any one have sample code please share with me .
Thanks
Hello,
First of all, thanks for all your examples it’s really helpful.
I’m trying to reproduce this example on STM32F439ZI ,
but it doesn’t work when i’m trying to ping it from my computer , when it comes to the STM32F439ZI i dont have access to the MPU Configuration i suppose it’s not a problem .
In ethernetif_input my pbuff is all time == Null then nothing happens, i m also supposed to see my STM at 192.168… when i use netstat -a right ?
If you have any idea from where my problem could be.
Best regards.
Ok then i don’t know why but when i try this with my STM connected in local on my computer it wont work but when i try this directly connected to my router it work .
If you are not using the router, you need to manually configure the ethernet. Check the discord group, I have shared the settings there.
thanks a lot.
Hi blorke,
Is there any breakthrough in your attempts? As I’m planning for the same dude
Did you get the solution? I’m currently working on an ethernet project on STM32F439ZIT6 and I cannot get it through. It would be very helpful if you can guide me on this.
Hi I wanted to thank you very much for this and many other videos that helped me a lot in understanding STM32 boards. I wanted to add to this video that if you’re using an external ethernet module, whose power is controlled by a pin on your board, then you should move MX_LWIP_Init() below the moment you call the HAL_GPIO_WritePin function.
UDP Server and client coming next week. I hope that would help you
me toooo😬