Skip to main content
Senior III
April 9, 2026
Solved

LwIP not entering callback when ethernet cable (dis)connected

  • April 9, 2026
  • 7 replies
  • 326 views

NUCLEO-F767ZI

I'm trying to use the LwIP middleware which I have enabled in MX.

First I'm trying to check if an ethernet cable is attached or not. I have setup UART3 to use printf (which works as expected)

I've set the callback in lwip.c to print when the cable is removed or plugged in or so I thought but I get no message when I connect or disconnect the cable. The LEDs on the socket light up when the cable is connected, so the board at some level does see the cable. What am I missing here?

EDIT:: LWIP_NETIF_LINK_CALLBACK was enabled in MX

// In main.c
MX_LWIP_Init();

/* USER CODE BEGIN WHILE */
while (1)
{
 MX_LWIP_Process();
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */



// In lwip.c the callback function that is registered in MX_LWIP_Init()
static void ethernet_link_status_updated(struct netif *netif)
{
 if (netif_is_up(netif))
 {
/* USER CODE BEGIN 5 */
	 printf("Cable connected\r\n");
/* USER CODE END 5 */
 }
 else /* netif is down */
 {
/* USER CODE BEGIN 6 */
	 printf("Cable NOT connected\r\n");
/* USER CODE END 6 */
 }
}

 

Best answer by STackPointer64

Have you configured the Ethernet DMA descriptors, LwIP, and the MPU correctly? If you attach your project, I can analyze it and look for misconfigurations. I also suggest that you review this knowledge base article; it will guide you through the necessary configurations. You could skip FreeRTOS and using a dedicated timer as a time base, because from the snippet you attached above, your goal is to run LwIP in polling mode.

7 replies

mbarg.1
Senior III
April 9, 2026

You do not have a connection manager - demo does not (most cases) it - there are many other cases, choices, features requiring sw management that are not handled in demo.

I usually suggest to draw a set of requirements you want in your system and compare with actual demo code to get double sure that minimum system is hat your customers can accept.

Senior III
April 9, 2026

My only requirement at the moment is to check if the cable is connected.

Technical Moderator
April 9, 2026

Hello @NicRoberts,

Have you tried setting a breakpoint on the `if` condition to check whether it is triggered?

Best regards,

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
Senior III
April 9, 2026

Going through the debug it doesn't get anywhere near, doesn't get past the memory initialisation, 

 

MX_LWIP_Init()

____lwip_init()

________mem_init()

__________mem->prev = 0;

HardFault_Handler()

Andrew Neil
Super User
April 9, 2026

@NicRoberts wrote:

doesn't get past the memory initialisation, 


So the code is not completing initialisation at all ?

Hardly surprising, then, that none of the features work!

 

Debugging Cortex-M Hard Faults

 

LwIP Debug / Diagnostics

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
STackPointer64Best answer
Technical Moderator
April 9, 2026

Have you configured the Ethernet DMA descriptors, LwIP, and the MPU correctly? If you attach your project, I can analyze it and look for misconfigurations. I also suggest that you review this knowledge base article; it will guide you through the necessary configurations. You could skip FreeRTOS and using a dedicated timer as a time base, because from the snippet you attached above, your goal is to run LwIP in polling mode.

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
Technical Moderator
April 9, 2026

Once we ensure that LwIP is working flawlessly, we can move on and debug why the LWIP_NETIF_LINK_CALLBACK function is not being triggered and does not print any message.

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
Senior III
April 9, 2026

I'll go through the link you posted. Thanks for taking a look, find attached. 

Technical Moderator
April 9, 2026

As suspected, you are missing crucial memory and cache configurations that will allow your application to work. I suggest that, after reading the article, you apply the configurations illustrated in sections 2.2, 2.4, 2.5, and 3.1. I’m pretty sure they will resolve the issue you are currently facing.

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
LCE
Principal II
April 9, 2026

Besides that stuff, what mbarg said: connection management.

The PHY must be read periodically (EthPhyLinkStatus() reads the PHY registers), otherwise how should LWIP know about any connection?

Something like this (not HAL, but you'll get the idea) is called in the loop / LwipProcess():

 

/**
 * @brief This function sets the netif link status.
 * @note This function should be included in the main loop to poll
 * for the link status update
 * @PAram pNetIf: pointer to the network interface
 * @retval None
 */
void Ethernet_Link_Periodic_Handle(struct netif *pNetIf)
{
	static uint32_t u32EthLinkTimer = 0;

	/* check PHY ethernet link every 200 ms */
	if( (HAL_GetTick() - u32EthLinkTimer) >= ETH_TIMEOUT_NETIF_LINK_PRD_HNDL_MS )
	{
		uint8_t u8LinkUp = 0;

		u32EthLinkTimer = HAL_GetTick();

		if( EthPhyLinkStatus(u8PhyPortActive, ETH_LINK_CHECK_QUICK) == HAL_OK ) u8LinkUp = 1;

		/* check if the netif link down and the PHY link is up */
		if( !netif_is_link_up(pNetIf) && (1 == u8LinkUp) )
		{
			/* network cable is connected */
			netif_set_link_up(pNetIf);
		}
		else if( netif_is_link_up(pNetIf) && (0 == u8LinkUp) )
		{
			/* network cable is disconnected */
			netif_set_link_down(pNetIf);
		}
	}
}

 

Technical Moderator
April 13, 2026

@NicRoberts, have you managed to fix the Ethernet issue?

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
Senior III
April 23, 2026

Yes thankyou for your help, been busy week only just got a chance to test it. I followed the instructions in the link you posted & its all working now.

Technical Moderator
April 23, 2026

You are most welcome!

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.