Recently I had the opportunity to work on some project using STM32 microcontroller and Ethernet interface. Probably like other people undertaking this topic, I encountered a problem – the lack of “simple” tutorials. In most of the available sources, connecting the Ethernet interface and STM32 chips is described quite confusingly, and in my opinion, there is a lack of instructions describing how to configure this functionality in the simplest possible way, without unnecessary exploration of network aspects. That’s why I decided to prepare this and related materials covering the topic of Ethernet on STM32. As a first step, I will show you how to perform the initial configuration so that the microcontroller is visible on the network.
In the projects I use the Nucleo F767ZI board, the codes are available for download on my GitHub profile.
Warning of new firmware version

When preparing the codes for this tutorial, I used CubeMX V6.12.1. If anyone wants to use the finished project that I provide on my GitHub profile, they will probably encounter a warning about the new firmware version. In that case, I recommend going ahead and using version 6.12.1.
ETH and LWIP configuration

The first step is to activate the ETH interface in the Connectivity tab and select the RMII configuration. All other parameters should stay in the default, you don’t need to change anything in them.

Under the Middleware tab, locate and activate LWIP. To use the Ethernet interface, you also need to select Driver_PHY in the Platform Settings tab. This is nothing more than the chip soldered on the Nucleo board that supports the network interface. We select LAN8742 from the available list in both places.

The next step is to set the IP address. Usually, networked devices use the so-called DHCP, that is, they get their own unique address automatically. However, if you are initially playing with the network and STM32, it is better to use a permanently set address. To do this, switch the LWIP_DHCP (DHCP Module) option to Disabled, and set the appropriate IP addresses in the IP Addres Settings options.
- IP_ADDRESS – this is the address that the Nucleo board will receive, enter here a string of digits available on the network to which you want to connect,
- NETMASK_ADDRESS – the subnet mask, by default it is 255.255.255.000,
- GATEWAY_ADDRESS – the address of the router, by default it is the first address in the network.
Thus prepared, the project can be generated.
Program code
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
extern struct netif gnetif;
/* USER CODE END 0 */
In the main.c file, which contains the main program executed by the STM32, we include a netif structure declaration named gnetif. This is a basic structure defined in the built-in LwIP (Lightweight IP) library that handles the network stack.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
ethernetif_input(&gnetif);
sys_check_timeouts();
}
In the main while loop we place the instructions ethernetif_input(&gnetif); and sys_check_timeouts();. Their task is to sequentially receive all the data from the Ethernet port and assign it to the network stack, in other words, pass it to the gnetif structure to which the pointer is placed in the argument. The second command checks and handles all the timeouts associated with the LwIP stack.
The program prepared in this way should be saved in the memory of the microcontroller, remembering to connect the Ethernet cable to the board beforehand. Although by doing it later, nothing bad will happen.
Startup

After launching, you need to open the command line to see if the program is working properly. To start with, you can use the arp a- command, which will return the addresses available on the network. As you can see, one of them is 192.168.8.200, which is the one assigned to the Nucleo board, this means that this one is visible on the network. Next, we use the ping command by adding the IP address of the STM32 board. After clicking enter, you should get the result, as in the graphic above. The computer sent four data packets, which were received correctly.
As you can see, the basic setup of a network interface for STM32 is quite simple. In the following materials, I will describe basic communication via UDP and TCP.