From ce8262a164d33d222de55677523593806404176e Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 3 Jun 2019 17:06:35 +1000 Subject: [PATCH] stm32/modnetwork: Replace generic netif NIC polling with specific code. It doesn't work to tie the polling of an underlying NIC driver (eg to check the NIC for pending Ethernet frames) with its associated lwIP netif. This is because most NICs are implemented with IRQs and don't need polling, because there can be multiple lwIP netif's per NIC driver, and because it restricts the use of the netif->state variable. Instead the NIC should have its own specific way of processing incoming Ethernet frame. This patch removes this generic NIC polling feature, and for the only driver that uses it (Wiznet5k) replaces it with an explicit call to the poll function (which could eventually be improved by using a proper external interrupt). --- ports/stm32/eth.c | 1 - ports/stm32/modnetwork.c | 14 +++++--------- ports/stm32/modnetwork.h | 7 ++----- ports/stm32/network_wiznet5k.c | 17 +++++++++-------- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/ports/stm32/eth.c b/ports/stm32/eth.c index 665495c174..6a1ef99afa 100644 --- a/ports/stm32/eth.c +++ b/ports/stm32/eth.c @@ -107,7 +107,6 @@ typedef struct _eth_dma_t { } eth_dma_t; typedef struct _eth_t { - mod_network_nic_type_t base; uint32_t trace_flags; struct netif netif; struct dhcp dhcp_struct; diff --git a/ports/stm32/modnetwork.c b/ports/stm32/modnetwork.c index 9d97ad4a0e..460f47257b 100644 --- a/ports/stm32/modnetwork.c +++ b/ports/stm32/modnetwork.c @@ -55,15 +55,11 @@ u32_t sys_now(void) { } STATIC void pyb_lwip_poll(void) { - // Poll all the NICs for incoming data - for (struct netif *netif = netif_list; netif != NULL; netif = netif->next) { - if (netif->flags & NETIF_FLAG_LINK_UP) { - mod_network_nic_type_t *nic = netif->state; - if (nic->poll_callback) { - nic->poll_callback(nic, netif); - } - } - } + #if MICROPY_PY_WIZNET5K + // Poll the NIC for incoming data + wiznet5k_poll(); + #endif + // Run the lwIP internal updates sys_check_timeouts(); } diff --git a/ports/stm32/modnetwork.h b/ports/stm32/modnetwork.h index 41a0017dab..0b6d0c4a7f 100644 --- a/ports/stm32/modnetwork.h +++ b/ports/stm32/modnetwork.h @@ -39,17 +39,14 @@ struct netif; -typedef struct _mod_network_nic_type_t { - mp_obj_base_t base; - void (*poll_callback)(void *data, struct netif *netif); -} mod_network_nic_type_t; - extern const mp_obj_type_t network_lan_type; extern const mp_obj_type_t mod_network_nic_type_wiznet5k; void mod_network_lwip_poll_wrapper(uint32_t ticks_ms); mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args); +void wiznet5k_poll(void); + #else struct _mod_network_socket_obj_t; diff --git a/ports/stm32/network_wiznet5k.c b/ports/stm32/network_wiznet5k.c index 3bbe639acc..796b5551a7 100644 --- a/ports/stm32/network_wiznet5k.c +++ b/ports/stm32/network_wiznet5k.c @@ -48,7 +48,7 @@ // Wiznet5k Ethernet driver in MACRAW mode typedef struct _wiznet5k_obj_t { - mod_network_nic_type_t base; + mp_obj_base_t base; mp_uint_t cris_state; const spi_t *spi; mp_hal_pin_obj_t cs; @@ -63,7 +63,6 @@ typedef struct _wiznet5k_obj_t { STATIC wiznet5k_obj_t wiznet5k_obj; STATIC void wiznet5k_lwip_init(wiznet5k_obj_t *self); -STATIC void wiznet5k_lwip_poll(void *self_in, struct netif *netif); STATIC void wiz_cris_enter(void) { wiznet5k_obj.cris_state = MICROPY_BEGIN_ATOMIC_SECTION(); @@ -176,7 +175,7 @@ STATIC uint16_t wiznet5k_recv_ethernet(wiznet5k_obj_t *self) { uint16_t port; int ret = WIZCHIP_EXPORT(recvfrom)(0, self->eth_frame, 1514, ip, &port); if (ret <= 0) { - printf("wiznet5k_lwip_poll: fatal error len=%u ret=%d\n", len, ret); + printf("wiznet5k_poll: fatal error len=%u ret=%d\n", len, ret); netif_set_link_down(&self->netif); netif_set_down(&self->netif); return 0; @@ -237,8 +236,11 @@ STATIC void wiznet5k_lwip_init(wiznet5k_obj_t *self) { self->netif.flags &= ~NETIF_FLAG_UP; } -STATIC void wiznet5k_lwip_poll(void *self_in, struct netif *netif) { - wiznet5k_obj_t *self = self_in; +void wiznet5k_poll(void) { + wiznet5k_obj_t *self = &wiznet5k_obj; + if (!(self->netif.flags & NETIF_FLAG_LINK_UP)) { + return; + } uint16_t len; while ((len = wiznet5k_recv_ethernet(self)) > 0) { if (self->trace_flags & TRACE_ETH_RX) { @@ -267,7 +269,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size mp_hal_pin_obj_t rst = pin_find(args[2]); // Access the existing object, if it has been constructed with the same hardware interface - if (wiznet5k_obj.base.base.type == &mod_network_nic_type_wiznet5k) { + if (wiznet5k_obj.base.type == &mod_network_nic_type_wiznet5k) { if (!(wiznet5k_obj.spi == spi && wiznet5k_obj.cs == cs && wiznet5k_obj.rst == rst && wiznet5k_obj.netif.flags != 0)) { wiznet5k_deinit(); @@ -275,8 +277,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size } // Init the wiznet5k object - wiznet5k_obj.base.base.type = &mod_network_nic_type_wiznet5k; - wiznet5k_obj.base.poll_callback = wiznet5k_lwip_poll; + wiznet5k_obj.base.type = &mod_network_nic_type_wiznet5k; wiznet5k_obj.cris_state = 0; wiznet5k_obj.spi = spi; wiznet5k_obj.cs = cs;