cancel
Showing results for 
Search instead for 
Did you mean: 

LwIP not entering callback when ethernet cable (dis)connected

NicRoberts
Senior II

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 */
  }
}

 

13 REPLIES 13
LCE
Principal II

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

 

STackPointer64
ST Employee

@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.

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.

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.