2024-07-19 07:03 AM - edited 2024-07-19 07:24 AM
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?
2024-07-22 02:11 AM
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.
2024-07-29 06:29 PM
Thank you for your response @Semer CHERNI
My project uses version 6.12.0 of STM32CubeMX.
My objective is to enable DHCP to connect my board to a router or, preferably, to a PC with DHCP server software installed. Then, I want to use Wireshark to observe the IP address assigned by the DHCP server to my STM32F746G-DISCO board and perform a simple ping to verify the correct reception of packets.
If this works, it will be a good start. Please note that I am not using FreeRTOS