Skip to content

Commit 70b0e7c

Browse files
committed
stm32/eth: Consolidate DHCP restart logic and fix static IP.
Static IP configured before active(True) was being overwritten when MAC speed/duplex reconfiguration occurred (~2s after link up). The MAC reconfiguration code restarted DHCP whenever a DHCP struct existed, without checking if a static IP was configured. Created eth_dhcp_restart_if_needed() helper function that checks if IP is 0.0.0.0 before restarting DHCP. Consolidated 3 locations with duplicated DHCP restart logic to use this helper, reducing code size by 30 lines while fixing the bug. This ensures static IP is preserved through MAC reconfiguration, cable unplug/replug, and interface activation. Signed-off-by: Andrew Leech <[email protected]>
1 parent f329f99 commit 70b0e7c

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

ports/stm32/eth.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,16 @@ static void eth_lwip_init(eth_t *self) {
768768
MICROPY_PY_LWIP_EXIT
769769
}
770770

771+
// Restart DHCP if no static IP is configured
772+
// Used when link comes up, MAC is reconfigured, or interface starts
773+
static void eth_dhcp_restart_if_needed(struct netif *netif) {
774+
if (netif_is_up(netif) && ip4_addr_isany_val(*netif_ip4_addr(netif))) {
775+
if (netif_dhcp_data(netif)) {
776+
dhcp_stop(netif);
777+
}
778+
dhcp_start(netif);
779+
}
780+
}
771781

772782
void eth_phy_link_status_poll() {
773783
eth_t *self = &eth_instance;
@@ -795,14 +805,8 @@ void eth_phy_link_status_poll() {
795805
self->mac_speed_configured = false;
796806
self->autoneg_start_ms = mp_hal_ticks_ms();
797807

798-
// Start or restart DHCP if interface is up and no static IP
799-
if (netif_is_up(netif) && ip4_addr_isany_val(*netif_ip4_addr(netif))) {
800-
// No static IP configured, ensure DHCP is started fresh
801-
if (netif_dhcp_data(netif)) {
802-
dhcp_stop(netif); // Clean up any existing DHCP state
803-
}
804-
dhcp_start(netif);
805-
}
808+
// Start or restart DHCP if no static IP configured
809+
eth_dhcp_restart_if_needed(netif);
806810
} else {
807811
// Cable is physically disconnected
808812
netif_set_link_down(netif);
@@ -897,14 +901,10 @@ void eth_phy_link_status_poll() {
897901
// Mark as configured
898902
self->mac_speed_configured = true;
899903

900-
// Restart DHCP since MAC was reconfigured
904+
// Restart DHCP if no static IP configured (since MAC was reconfigured)
901905
struct netif *netif = &self->netif;
902906
MICROPY_PY_LWIP_ENTER
903-
if (netif_is_up(netif) && netif_dhcp_data(netif)) {
904-
// Use stop+start instead of renew to handle all DHCP states reliably
905-
dhcp_stop(netif);
906-
dhcp_start(netif);
907-
}
907+
eth_dhcp_restart_if_needed(netif);
908908
MICROPY_PY_LWIP_EXIT
909909
}
910910
}
@@ -992,12 +992,8 @@ int eth_start(eth_t *self) {
992992
// Do an initial link status poll after PHY has had time to initialize
993993
eth_phy_link_status_poll();
994994

995-
// Start DHCP if no static IP has been configured
996-
if (ip4_addr_isany_val(*netif_ip4_addr(n))) {
997-
// No static IP configured, start DHCP regardless of link status
998-
// DHCP will wait for link to come up before sending packets
999-
dhcp_start(n);
1000-
}
995+
// Start DHCP if no static IP configured
996+
eth_dhcp_restart_if_needed(n);
1001997

1002998
MICROPY_PY_LWIP_EXIT
1003999

0 commit comments

Comments
 (0)