How to configure and ping Ethernet on STM32

This tutorial is the start of the Ethernet series in STM32. Today we will simply see how to configure the Hardware.

For some of the MCUs, this will be as easy as the default setup, but for others, this part could be very complicated. Specially the Cortex M7 Series MCUs, where the cacheable region causes data coherency issues.

So I will try to explain the process in the best possible way. To better understand the result of different settings, I would suggest you to watch the Video at the end of this post.

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 Hardware, while other have RMII.

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.

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.

Check out the Video Below




Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

Subscribe
Notify of

26 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Malik
2 months ago

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.

guest
4 months ago

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!

seat
4 months ago

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!

Mahmood
1 year ago

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.

Benedito
Reply to  Mahmood
9 months ago

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.

MG
1 year ago

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?

Hieule
2 years ago

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?

Ehsan
2 years ago

Hi,
The Videos and the documentation are very helpful.
Thanks a lot!

jan
2 years ago

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

Sajad
2 years ago

Him
How I can run this project with SPI of STM32H7?

Thamanoon Kedwiriyakarn
3 years ago

Hi,
Thank you very much
I am new for ethernet, thanks for sharing.

Sen
3 years ago

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?

Sen
Reply to  Sen
3 years ago

Hi, Its working. Sorry, I didn’t check your second video on ethernet.

Thanks again for the content.

Mohan Anande
Reply to  Sen
3 years ago

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 ?

manel
3 years ago

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

blorke
3 years ago

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.

Last edited 3 years ago by blorke
blorke
Reply to  blorke
3 years ago

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 .

blorke
Reply to  admin
3 years ago

thanks a lot.

Ganesan_Guru
Reply to  blorke
2 years ago

Hi blorke,

Is there any breakthrough in your attempts? As I’m planning for the same dude

Gaurali
Reply to  blorke
2 years ago

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.

PolishCanuck
3 years ago

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.

/* Initialize all configured peripherals */ 
MX_GPIO_Init(); 
/* USER CODE BEGIN 2 */ 
HAL_GPIO_WritePin(EN_ETH_GPIO_Port, EN_ETH_Pin, GPIO_PIN_SET); 
MX_LWIP_Init(); 
/* USER CODE END 2 */
Last edited 3 years ago by PolishCanuck
Mahmut Kocaman
3 years ago
Hello. I have read your content thoroughly. It may be the most descriptive information I've seen on the internet. I am also working on data exchange with ethernet. I was able to get to the pinging part and I can ping successfully. However, I was not able to send or retrieve the data I wanted to a server or computer. I look forward to your opening a topic where we can send and receive data to a server or computer over ethernet (DP83848 and STM32F429). I wish you good work.
Mahmut Kocaman
Reply to  admin
3 years ago
I'm looking into those issues right now, thank you.
Mahyar
Reply to  Mahmut Kocaman
1 year ago

me toooo😬