cancel
Showing results for 
Search instead for 
Did you mean: 

Bug found in the cubeMX code generation at MX_LWIP functions implementations for ethernet comms

genisuvi
Associate III

I'm using stm32f207zg mcu with LAN8742 device for an ethernet based application that implements a server with multiple client connections. We are using non RTOS, LwIP with RAW interface and HAL drivers. 

During validation test we realized that link was unable to initiate when ethernet wire is not connected. 

When ethernet connector is unplugged when server starts, if the connector is plugged after this, link stills down. We were trying to call the MX_LWIP_Init function after link up detection but it doesn't work.

The source code generated by CubeMx:

 

 

void MX_LWIP_Init(void)
{
  /* IP addresses initialization */
  IP_ADDRESS[0] = 192;
  IP_ADDRESS[1] = 168;
  IP_ADDRESS[2] = 1;
  IP_ADDRESS[3] = 32;
  NETMASK_ADDRESS[0] = 255;
  NETMASK_ADDRESS[1] = 255;
  NETMASK_ADDRESS[2] = 255;
  NETMASK_ADDRESS[3] = 0;
  GATEWAY_ADDRESS[0] = 192;
  GATEWAY_ADDRESS[1] = 168;
  GATEWAY_ADDRESS[2] = 1;
  GATEWAY_ADDRESS[3] = 1;

/* USER CODE BEGIN IP_ADDRESSES */
  IP_ADDRESS[3] = App_GPIO_ReadAdrBus();
#ifdef USE_DHCP
	ip_addr_set_zero_ip4(&ipaddr);
	ip_addr_set_zero_ip4(&netmask);
	ip_addr_set_zero_ip4(&gw);
#else

	/*geni*/
	uint8_t ip[4] = { IP_ADDR0, IP_ADDR1, IP_ADDR2, 0 };//3 primers bytes d'ip per defecte i quart sense definir
	uint32_t *pIpAdd = NULL;//contenidor punter de paraules --> la funcio getip no admet contenidors no declarats com a punters

	pIpAdd = HL_FlashGetIP();//retorna punter que apunta on hi ha les adreces --> elements del array buffer de 4 elements

	if ((pIpAdd[0]==0) && (pIpAdd[1]==0) && (pIpAdd[2]==0) && (pIpAdd[3]==0)) //si encara no s'ha enregistrat cap ip
	{
		//el 4rt digit s'agafa per defecte del switch
		ip[3] = App_GPIO_ReadAdrBus();//llegeix del switch pcb el valor del 4 digit
		IP_ADDR4(&ipaddr, ip[0], ip[1], ip[2], ip[3]);
	}
	else
		IP_ADDR4(&ipaddr, (uint8_t)pIpAdd[0], (uint8_t)pIpAdd[1], (uint8_t)pIpAdd[2], (uint8_t)pIpAdd[3]);

	IP_ADDR4(&netmask,NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);//todo: lo mismo que ip con la mask
	IP_ADDR4(&gw,GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
#endif /* USE_DHCP */
/* USER CODE END IP_ADDRESSES */

  /* Initilialize the LwIP stack without RTOS */
  lwip_init();

  /* IP addresses initialization without DHCP (IPv4) */
  IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);

  /* add the network interface (IPv4/IPv6) without RTOS */
  netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);

  /* Registers the default network interface */
  netif_set_default(&gnetif);

  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);
  }

  /* Set the link callback function, this function is called on change of link status*/
  netif_set_link_callback(&gnetif, ethernetif_update_config);

  /* Create the Ethernet link handler thread */

/* USER CODE BEGIN 3 */

/* USER CODE END 3 */
}

 

 

 

The wrong part of code is alocated after adding the network interface (IPv4/IPv6) without RTOS sentence with this statements sequence:

 

 

 

  /* Registers the default network interface */
  netif_set_default(&gnetif);

  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);
  }

 

 

Corrections:

Do this instead of doing the before sequence at MX_Lwip_Init():

 

 

/* We must always bring the network interface up connection or not... */
  netif_set_up(&gnetif);

 

 

You must add this function call to the MX_Lwip_process():

 

 

// check the link status
	ethernetif_set_link(&gnetif);

 

 

We found those hints thanks to this old post:

It seems that ST hasn't done anything for fixing this issue yet  (3 years ago).

Be care! 

1 REPLY 1
MWB_CHa
ST Employee

Hi @genisuvi 

Thanks for your suggestion.

Indeed, the STM32F2 firmware hasn't been updated for a while. STM32F4 and STM32F7 got multiple updates to fix similar issues.