Skip to content

Commit 05231c2

Browse files
committed
stm32/eth: Fix DHCP startup timing and link-dependent activation.
Fix DHCP not working when ipconfig(dhcp4=True) is called after active(True). The issue was caused by starting DHCP before link status was determined and not properly handling DHCP startup when the link comes up later. Changes: - Move link status poll before LWIP interface setup in eth_start() - Only start DHCP if link is already up when interface starts - Start DHCP automatically when link comes up if no static IP set - Ensure DHCP works correctly with cable connect/disconnect cycles This restores the expected behavior where users can call: eth = network.LAN() eth.active(True) eth.ipconfig(dhcp4=True) Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent 9f36735 commit 05231c2

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

ports/stm32/eth.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -827,9 +827,15 @@ void eth_phy_link_status_poll() {
827827
eth_phy_configure_autoneg(self);
828828
}
829829

830-
// Restart DHCP if interface is up and DHCP is enabled
831-
if (netif_is_up(netif) && netif_dhcp_data(netif)) {
832-
dhcp_renew(netif);
830+
// Start or restart DHCP if interface is up
831+
if (netif_is_up(netif)) {
832+
if (netif_dhcp_data(netif)) {
833+
// DHCP already running, renew lease
834+
dhcp_renew(netif);
835+
} else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
836+
// No static IP and no DHCP running, start DHCP
837+
dhcp_start(netif);
838+
}
833839
}
834840
} else {
835841
// Cable is physically disconnected
@@ -910,17 +916,21 @@ int eth_start(eth_t *self) {
910916
netif_set_default(n);
911917
netif_set_up(n);
912918

913-
// Start DHCP if no static IP has been configured
919+
// Do an initial link status poll after PHY has had time to initialize
920+
eth_phy_link_status_poll();
921+
922+
// Start DHCP if no static IP has been configured and link is up
914923
if (ip4_addr_isany_val(*netif_ip4_addr(n))) {
915-
// No static IP configured, start DHCP
916-
dhcp_start(n);
924+
// No static IP configured, start DHCP if link is up
925+
if (n->flags & NETIF_FLAG_LINK_UP) {
926+
dhcp_start(n);
927+
}
928+
// Note: If link is down, DHCP will be started later when link comes up
929+
// via the dhcp_renew() call in eth_phy_link_status_poll()
917930
}
918931

919932
MICROPY_PY_LWIP_EXIT
920933

921-
// Do an initial link status poll after PHY has had time to initialize
922-
eth_phy_link_status_poll();
923-
924934
self->enabled = true;
925935
return 0;
926936
}

0 commit comments

Comments
 (0)