STM32 Ethernet #1. Connection

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

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 of two (out of many) possible configurations

  • The LEFT hardware uses the RMII pinout, while the RIGHT one is using MII pinout.
  • On the LEFT one 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.
  • On the RIGHT side, the MCU is letting us configure the memory for DMA descriptors, and the Rx Buffer.
  • Assuming that each DMA Descriptor takes 32 bits (MAXIMUM Possible), the RX Descriptor and Tx Descriptor have the size of 128 Bytes (32×4).
  • The Memory between them is spaced accordingly.
  • The Rx Buffer have the size of 1524×4 (Rx DMA Descriptor Length).
  • The memories are allocated in the SRAM region, where we can modify the properties 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.
  • On the RIGHT, I am using 10KB memory for the Heap.
  • This 10 KB is enough for our few applications in the beginning. Later we will increase it if needed to.
  • Other than this, some MCUs will allow to fix the location for this Heap.
  • The location is at an offset of 0x2000 in the SRAM, and the calculation for the same is shown below.

MPU Configuration

Now we have the DMA Descriptors in the SRAM Region. And 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 others, this is a must, or else you will get hardfault.

Remember that during the configuration, we set up everything in the SRAM (0x30000000). The total size of the RAM was around 18 KB. So we will configure the MPU keeping this information in mind.

  • Here we have selected the 32 KB region, since it’s the least size available after 16 KB. And our region is 18 KB.
  • 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.





The CODE

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.

/* USER CODE BEGIN 0 */

extern struct netif gnetif;

/* USER CODE END 0 */

...........MAIN..........

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

	  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
To download the code, click DOWNLOAD button and view the Ad. The project will download after the Ad is finished.

21 Comments. Leave new

  • 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?

    Reply
  • 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?

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

    Reply
  • 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

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

    Reply
  • Thamanoon Kedwiriyakarn
    February 13, 2022 8:21 AM

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

    Reply
  • 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?

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

      Thanks again for the content.

      Reply
      • 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 ?

        Reply
  • 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

    Reply
  • 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.

    Reply
    • 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 .

      Reply
    • Hi blorke,

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

      Reply
    • 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.

      Reply
  • 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 */
    
    Reply
  • Mahmut Kocaman
    July 28, 2021 2:21 PM
    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.
    
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

keyboard_arrow_up

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add controllerstech.com to your ad blocking whitelist or disable your adblocking software.

×