cancel
Showing results for 
Search instead for 
Did you mean: 

LWIP DHCP Configuration on CubeMX

KDZ7
Associate

 

Hi,

I am trying to configure DHCP on my STM32F746G-DISCO board, but despite several attempts and even after setting up my DHCP server or configuring my router, I can't get a dynamic IP for my MCU. I can make it work with a static IP, but I would like to test it with a dynamic IP.

The project is simple: just a UDP server. I am not using an OS, just a small piece of code that initializes and waits for the DHCP to assign an IP before continuing. However, on Wireshark, I see that the DHCP Discover message is sent repeatedly without ever receiving a DHCP Offer or DHCP Request response.

There aren't even any tutorials showing how to configure just DHCP on CubeMX, I can't find anything on this topic. Most examples use a static IP.

This is the part of the code where we notice the DHCP anomaly

 

 

 

void udpServer_init(void) {
    struct udp_pcb *upcb;
    
    upcb = udp_new();
    if (upcb == NULL) {
        printf("Error creating UDP PCB!\n");
        return;
    }
    
    if (udp_bind(upcb, IP_ADDR_ANY, 5555) != ERR_OK) {
        udp_remove(upcb);
        printf("Error UDP Bind!\n");
        return;
    }
    
    udp_recv(upcb, udp_receive_callback, NULL);
    
    while (!dhcp_supplied_address(&gnetif)) {
        HAL_Delay(100);
    }
    
    struct dhcp *dhcp = netif_dhcp_data(&gnetif);
    printf("DHCP IP address: %s\n", ip4addr_ntoa(&dhcp->offered_ip_addr));
    printf("DHCP Subnet mask: %s\n", ip4addr_ntoa(&dhcp->offered_sn_mask));
    printf("DHCP Default gateway: %s\n", ip4addr_ntoa(&dhcp->offered_gw_addr));
}

 

 

 

Note that dhcp_start(&gnetif); was correctly executed earlier in MX_LWIP_Init() as follows:

 

 

int main() {

.......

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LWIP_Init();
/* USER CODE BEGIN 2 */
udpServer_init();
/* USER CODE END 2 */
........
...
  while (1)
  {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
    MX_LWIP_Process();
  }
return 0
}

 

 

The code stays stuck in the loop, proving it never receives an IP via DHCP.

Do you have any suggestions or advice to resolve this issue?


 

1 REPLY 1
Semer CHERNI
ST Employee

Hello @KDZ7 

First let me thank you for posting.

Could you provide the version of STM32CubeMX used to generate the project and the FW version.

And, it would be helpful if you share the ioc file.

BR,
Semer.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.