Skip to content

Commit 6ee0622

Browse files
pi-anlclaude
andcommitted
stm32/eth: Restructure LWIP initialization for static IP support.
This restructures the LWIP network interface initialization to allow static IP configuration before active(True) is called, improving the user experience and network configuration workflow. Changes: - Split netif initialization into early (eth_init) and late phases - eth_netif_init_early() sets up netif structure in eth_init() - eth_lwip_init() adds netif to network stack in eth_start() - DHCP only starts if no static IP is configured (0.0.0.0) - eth_stop() removes from stack but preserves netif for reuse Workflow: 1. eth_init() -> netif structure ready for IP configuration 2. User can set static IP via ipconfig() before active(True) 3. eth_start() -> adds to network stack, starts DHCP if needed 4. eth_stop() -> removes from network stack, preserves config This allows the common pattern: eth = network.LAN() eth.ipconfig(addr='192.168.1.100', ...) # Set static IP eth.active(True) # Start networking 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> Signed-off-by: Andrew Leech <[email protected]>
1 parent aa03dcd commit 6ee0622

File tree

1 file changed

+32
-65
lines changed

1 file changed

+32
-65
lines changed

ports/stm32/eth.c

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ eth_t eth_instance;
132132

133133
static void eth_mac_deinit(eth_t *self);
134134
static void eth_process_frame(eth_t *self, size_t len, const uint8_t *buf);
135-
static void eth_netif_init_early(eth_t *self);
135+
static void eth_lwip_init(eth_t *self);
136136
static void eth_phy_link_status_poll(eth_t *self);
137137
static void eth_phy_configure_autoneg(eth_t *self);
138138

@@ -230,8 +230,8 @@ int eth_init(eth_t *self, int mac_idx, uint32_t phy_addr, int phy_type) {
230230
__HAL_RCC_ETH_CLK_ENABLE();
231231
#endif
232232

233-
// Initialize netif structure early so static IP can be configured before active(True)
234-
eth_netif_init_early(self);
233+
// Initialize netif and register with LWIP
234+
eth_lwip_init(self);
235235

236236
return 0;
237237
}
@@ -791,7 +791,8 @@ static err_t eth_netif_init(struct netif *netif) {
791791
return ERR_OK;
792792
}
793793

794-
static void eth_netif_init_early(eth_t *self) {
794+
static void eth_lwip_init(eth_t *self) {
795+
MICROPY_PY_LWIP_ENTER
795796
// Initialize netif structure but don't add to network stack yet
796797
struct netif *n = &self->netif;
797798
n->name[0] = 'e';
@@ -805,48 +806,19 @@ static void eth_netif_init_early(eth_t *self) {
805806
IP_ADDR4(&ipconfig[2], 192, 168, 0, 1); // Gateway
806807
IP_ADDR4(&ipconfig[3], 8, 8, 8, 8); // DNS
807808

808-
netif_set_addr(n, ip_2_ip4(&ipconfig[0]), ip_2_ip4(&ipconfig[1]), ip_2_ip4(&ipconfig[2]));
809+
netif_add(n, ip_2_ip4(&ipconfig[0]), ip_2_ip4(&ipconfig[1]), ip_2_ip4(&ipconfig[2]), self, eth_netif_init, ethernet_input);
810+
netif_set_hostname(n, mod_network_hostname_data);
811+
812+
ip_addr_t dns_addr;
813+
IP_ADDR4(&dns_addr, 8, 8, 8, 8);
814+
dns_setserver(0, &dns_addr);
809815

810816
// Initialize DHCP structure
811817
dhcp_set_struct(n, &self->dhcp_struct);
812-
}
813-
814-
static void eth_lwip_init(eth_t *self) {
815-
MICROPY_PY_LWIP_ENTER
816-
817-
struct netif *n = &self->netif;
818-
819-
// Add netif to network stack (only if not already added)
820-
if (netif_find(n->name) == NULL) {
821-
ip_addr_t dns_addr;
822-
IP_ADDR4(&dns_addr, 8, 8, 8, 8);
823-
824-
netif_add(n, netif_ip4_addr(n), netif_ip4_netmask(n), netif_ip4_gw(n), self, eth_netif_init, ethernet_input);
825-
netif_set_hostname(n, mod_network_hostname_data);
826-
netif_set_default(n);
827-
netif_set_up(n);
828-
829-
dns_setserver(0, &dns_addr);
830-
831-
#if LWIP_IPV6
832-
netif_create_ip6_linklocal_address(n, 1);
833-
#endif
834-
}
835-
836-
MICROPY_PY_LWIP_EXIT
837-
}
838-
839-
static void eth_start_dhcp_if_needed(eth_t *self) {
840-
MICROPY_PY_LWIP_ENTER
841-
struct netif *netif = &self->netif;
842-
843-
// Check if a static IP address has been configured
844-
if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
845-
// No static IP configured, start DHCP
846-
dhcp_start(netif);
847-
}
848-
// If static IP is already configured, don't start DHCP
849818

819+
#if LWIP_IPV6
820+
netif_create_ip6_linklocal_address(n, 1);
821+
#endif
850822
MICROPY_PY_LWIP_EXIT
851823
}
852824

@@ -898,25 +870,6 @@ static void eth_phy_link_status_poll(eth_t *self) {
898870
}
899871
}
900872

901-
static void eth_lwip_deinit(eth_t *self) {
902-
MICROPY_PY_LWIP_ENTER
903-
struct netif *netif = &self->netif;
904-
905-
// Stop DHCP if running
906-
if (netif_dhcp_data(netif)) {
907-
dhcp_stop(netif);
908-
}
909-
910-
// Remove from network stack but keep netif structure for reuse
911-
for (struct netif *n = netif_list; n != NULL; n = n->next) {
912-
if (n == netif) {
913-
netif_remove(n);
914-
break;
915-
}
916-
}
917-
MICROPY_PY_LWIP_EXIT
918-
}
919-
920873
static void eth_process_frame(eth_t *self, size_t len, const uint8_t *buf) {
921874
eth_trace(self, len, buf, NETUTILS_TRACE_NEWLINE);
922875

@@ -967,11 +920,20 @@ int eth_start(eth_t *self) {
967920
return ret;
968921
}
969922

970-
// Initialize LWIP netif (only once, safe to call multiple times)
971-
eth_lwip_init(self);
923+
MICROPY_PY_LWIP_ENTER
924+
struct netif *n = &self->netif;
925+
926+
// Enable the interface in LWIP
927+
netif_set_default(n);
928+
netif_set_up(n);
972929

973930
// Start DHCP if no static IP has been configured
974-
eth_start_dhcp_if_needed(self);
931+
if (ip4_addr_isany_val(*netif_ip4_addr(n))) {
932+
// No static IP configured, start DHCP
933+
dhcp_start(n);
934+
}
935+
936+
MICROPY_PY_LWIP_EXIT
975937

976938
// Do an initial link status poll
977939
eth_phy_link_status_poll(self);
@@ -981,7 +943,12 @@ int eth_start(eth_t *self) {
981943
}
982944

983945
int eth_stop(eth_t *self) {
984-
eth_lwip_deinit(self);
946+
// Stop DHCP if running
947+
if (netif_dhcp_data(&self->netif)) {
948+
dhcp_stop(&self->netif);
949+
}
950+
netif_set_down(&self->netif);
951+
985952
eth_mac_deinit(self);
986953
self->enabled = false;
987954
return 0;

0 commit comments

Comments
 (0)