AnsweredAssumed Answered

Managing Ethernet cable events with lwIP

Question asked by giuseppe.scarpi on Jan 5, 2017
Latest reply on Feb 10, 2017 by kaltofen.jupp

I am currently working on a project that uses Etherne with CubeMX and lwIP.

During the development, I found rather difficult to manage the cases when the physical link becomes invalid (e.g. cable unplugged), or it is not valid at startup.

In theory, lwIP offers the functions netif_set_up() and netif_set_down() that inform the stack of the link state. In practice, this seems not to always work.

I'd like to share my experience if someone else is interested:

 

0) comment out the standard CubeMX initialisation MX_LWIP_Init(). This is of paramount importance!

 

1) add a network interface by calling netif_add(). You need it to check the link state in the next step.

 

2) call

   HAL_ETH_ReadPHYRegister(&heth, PHY_BSR, &phyreg);

where heth is the CubeMX-generated instance of Eth, and phyreg is an uint32_t

 

3) if (phyreg & PHY_LINKED_STATUS) is not zero, then the link is valid and you can proceed with other operations:

 netif_set_up()

 dhcp_start()

   ...

   then jump to 5)

 

4) If the link is not valid, don't proceed with network initialisation!! Instead, repeat the test periodically until the link is found valid. At this point, you need to remove the existing network interface and create a new one:

  netif_remove()

 netif_add()

   ... and so on

 

5) once the initialisation is completed, you should still monitor the link state. In case the link went down, call netif_set_down(). When the link returns up, call netif_set_up(). Don't remove/add the network interface or your connection will no longer work!

Outcomes