2024-02-16 10:12 AM - edited 2024-02-16 10:39 AM
I am using a Nucleo-F429ZI with FreeRtos and LWIP.
I am having an issue with LWIP when I start with a disconnected Ethernet Cable and then when I connect it back when the firmware is running.
I have a thread with the following code.
1 - The gnetif structure is initialized by MX_LWIP_Init(). But the function check
netif_is_up(&gnetif)
returns that the link is up even though the cable is disconnected.
2 - When I plug the cable back, the DHCP function does not acquire an IP Address. It seems stuck somewhere
3 - When the cable is connected and then I disconnect it, the gnetif.ip_addr.addr stays with the initial DHCP address instead of resetting to 0.
Any thoughts on what the issues might be ?
MX_LWIP_Init();
/* USER CODE BEGIN 5 */
firstTime = 1;
/* Infinite loop */
for(;;)
{
while (timer_update == 0);
timer_update = 0;
if (netif_is_up(&gnetif)) {
printf("Main Link is Up\n");
} else {
printf("Main Link is Down\n");
continue;
}
local_IP = gnetif.ip_addr.addr;
if (local_IP == 0) continue;
printf("IP Address: %s\n", ip4addr_ntoa(&gnetif.ip_addr));
if (client == NULL) client = mqtt_client_new();
osDelay(100);
if(client != NULL) {
if (!mqtt_client_is_connected(client)) {
printf("Connecting to Client\n\r");
corin_do_connect(client,local_IP);
} else {
corin_publish(client,"This is a Test");
}
osDelay(100);
}
}
Solved! Go to Solution.
2024-02-16 01:10 PM
So I finally found the issue. The EthLink thread in lwip.c was set with a priority belowNormal and with my other thread ended up starving. So by setting it to Normal, it worked fine
memset(&attributes, 0x0, sizeof(osThreadAttr_t));
attributes.name = "EthLink";
attributes.stack_size = INTERFACE_THREAD_STACK_SIZE;
attributes.priority = osPriorityNormal;
osThreadNew(ethernet_link_thread, &gnetif, &attributes);
2024-02-16 11:10 AM - edited 2024-02-16 11:14 AM
So I thought I figured out the issue for number 1 by adding the following to lwip.c in the MX_LWIP_Init function but now the link never updates to Link Up when I plug the cable back in
/* We must always bring the network interface up connection or not... */
if (netif_is_link_up(&gnetif))
{
/* When the netif is fully configured this function must be called */
netif_set_up(&gnetif);
}
else
{
/* When the netif link is down this function must be called */
netif_set_down(&gnetif);
}
2024-02-16 11:45 AM
This is usually an issue with the ethernetif_set_link() function/task that does not properly call netif_set_link_up() and netif_set_link_down() as the interface goes up and down (i.e. cable plugged and unplugged). Something like this:
// WARNING - HAND TYPED MAY NOT BE 100% CORRECT
// but you get the idea
// Inside ethernetif_set_link()
for(;;)
{
HAL_ETH_ReadPHYRegister(..., ®value );
regvalue &= PHY_LINKED_STATUS;
if ( !netif_is_link_up(link_arg->netif) && (regvalue) )
{
netif_set_link_up(link_arg->netif);
}
else if ( netif_is_link_up(link_arg->netif) && (!regvalue) )
{
netif_set_link_down(link_arg->netif);
}
// Other stuff may be here
}
2024-02-16 01:10 PM
So I finally found the issue. The EthLink thread in lwip.c was set with a priority belowNormal and with my other thread ended up starving. So by setting it to Normal, it worked fine
memset(&attributes, 0x0, sizeof(osThreadAttr_t));
attributes.name = "EthLink";
attributes.stack_size = INTERFACE_THREAD_STACK_SIZE;
attributes.priority = osPriorityNormal;
osThreadNew(ethernet_link_thread, &gnetif, &attributes);