Skip to content

Commit b44cf0e

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 8e1dbeb commit b44cf0e

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);
@@ -898,14 +902,10 @@ void eth_phy_link_status_poll() {
898902
self->mac_speed_configured = true;
899903
self->configured_phy_speed = phy_speed;
900904

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

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

1003999
MICROPY_PY_LWIP_EXIT
10041000

0 commit comments

Comments
 (0)