Skip to main content
Visitor II
June 18, 2020
Question

STM32F427VI - HardFault_Handler because of ethernet configuration

  • June 18, 2020
  • 2 replies
  • 2713 views

Hi all, I moved a code that was working on STM32F427VI and somehow I am facing a new issue that I have never see before.

My code works in debug and release when the unit is compiled from Visual Studio and the Ethernet cable is connected or disconnected. But, after I reset the unit (power off/power on) the unit is working when the ethernet cable is connected. If the cable is disconnected go over the while true inside HardFault_Handler. I think that the problem is on that function:

bool configureNetwork(network_config& netconfig)
{
	struct ip4_addr ipaddr;
	struct ip4_addr netmask;
	struct ip4_addr gw;
	struct ip4_addr dns;
	
	network_config::setIPAddr(&ipaddr, netconfig.ip);
	network_config::setIPAddr(&netmask, netconfig.mask);
	network_config::setIPAddr(&gw, netconfig.gateway);
	network_config::setIPAddr(&dns, netconfig.dns);
	
	if (gnetStarted) {
		handleDHCP(netconfig.isDynamic());
		if (curDynamic == false) {
			netif_set_addr(&gnetif, &ipaddr, &netmask, &gw);			
			dns_setserver(0, &dns);
		}
	}
	else {
		STARTUP_TRACE.Write("before: netif_add\r\n", 1000);
		netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
		STARTUP_TRACE.Write("after: netif_add\r\n", 1000);
		/* Registers the default network interface */
		netif_set_default(&gnetif);
		
		netif_set_status_callback(&gnetif, netstatus_callback);
 
		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);
		}
		handleDHCP(netconfig.isDynamic());
		if (curDynamic == false) {
			dns_setserver(0, &dns);
		}
		
		netif_set_link_callback(&gnetif, ethernetif_update_config);
		
		gnetStarted = true;
		sys_timeout(LINK_CHECK_INTERVAL, second_timeout, NULL);
	}	
		
	return true;
		
}

I have this conclusion becasue when I remark this function, the hardFaultHandler is called even when the unit is in debug on visual studio. So the result is the same.

I don't understand what is the difference when the mcu is conencted to the debugger and why the handler is called when the cable is disconnected. I have this issues on the Ethernet for the first time.

Any idea?

    This topic has been closed for replies.

    2 replies

    Super User
    June 18, 2020

    Attach a debugger and see where the code is getting stuck. Instrument the hard fault handler to give information about the error state.

    As.51Author
    Visitor II
    June 18, 2020

    The code reachs here

    extern "C" void HardFault_Handler(void)
    {
     /* USER CODE BEGIN HardFault_IRQn 0 */
     
     /* USER CODE END HardFault_IRQn 0 */
     while (1)
     {
     /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    	STARTUP_TRACE.Write("HardFault_Handler:stm32f4xx_it\r\n", 1000); 	 
     /* USER CODE END W1_HardFault_IRQn 0 */
     }
    }

    I think that I have a problem when I set the lwip interface as dynamic (DHCP_ and the IP = 0 (when the IP is loaded from flash from previous runniung).

    In this case the &ethernet_input is not init in the correct mode and then I got an error here: err = netif->input(p, netif); becasue it is null.

     if (p == NULL) return;
     
     /* entry point to the LwIP stack */
     err = netif->input(p, netif);
     if (err != ERR_OK)
     {
     LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
     pbuf_free(p);
     p = NULL; 
     }
    }

    Graduate II
    June 20, 2020

    ST has never had a working link detection code. If you're using RTOS, then your whole code is broken as it doesn't follow lwIP API rules. The same is true for ST's non-working HAL/Cube bloatware:

    https://community.st.com/s/question/0D50X0000BOtfhnSQB/how-to-make-ethernet-and-lwip-working-on-stm32

    As.51Author
    Visitor II
    June 21, 2020

    The same code works excellent on my previous ST MCU...

    As.51Author
    Visitor II
    June 21, 2020

    I got those messages when the cable is disconnected on DHCP

    Assertion "netif is not up, old style port?" failed at line 727 in C:/Users/alex_s/STM32Cube/Repository/STM32Cube_FW_F4_V1.24.1/Middlewares/Third_Party/LwIP/src/core/ipv4/dhcp.c

    Assertion "unknown dns_table entry state:" failed at line 1079 in C:/Users/alex_s/STM32Cube/Repository/STM32Cube_FW_F4_V1.24.1/Middlewares/Third_Party/LwIP/src/core/dns.c