From 4410efc1e349bc5e098f8263b4d652651f26d585 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 Apr 2019 22:33:49 +1100 Subject: [PATCH] stm32/network_wiznet5k: Add ability to trace Ethernet TX and RX frames. Via: nic.config(trace=2|4) --- ports/stm32/network_wiznet5k.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ports/stm32/network_wiznet5k.c b/ports/stm32/network_wiznet5k.c index 9db42b7874..99b8b68536 100644 --- a/ports/stm32/network_wiznet5k.c +++ b/ports/stm32/network_wiznet5k.c @@ -32,12 +32,16 @@ #if MICROPY_PY_WIZNET5K && MICROPY_PY_LWIP +#include "lib/netutils/netutils.h" #include "drivers/wiznet5k/ethernet/socket.h" #include "lwip/err.h" #include "lwip/dns.h" #include "lwip/dhcp.h" #include "netif/etharp.h" +#define TRACE_ETH_TX (0x0002) +#define TRACE_ETH_RX (0x0004) + /*******************************************************************************/ // Wiznet5k Ethernet driver in MACRAW mode @@ -48,6 +52,7 @@ typedef struct _wiznet5k_obj_t { mp_hal_pin_obj_t cs; mp_hal_pin_obj_t rst; uint8_t eth_frame[1514]; + uint32_t trace_flags; struct netif netif; struct dhcp dhcp_struct; } wiznet5k_obj_t; @@ -176,6 +181,9 @@ STATIC uint16_t wiznet5k_recv_ethernet(wiznet5k_obj_t *self) { STATIC err_t wiznet5k_netif_output(struct netif *netif, struct pbuf *p) { wiznet5k_obj_t *self = netif->state; pbuf_copy_partial(p, self->eth_frame, p->tot_len, 0); + if (self->trace_flags & TRACE_ETH_TX) { + netutils_ethernet_trace(MP_PYTHON_PRINTER, p->tot_len, self->eth_frame, NETUTILS_TRACE_IS_TX | NETUTILS_TRACE_NEWLINE); + } wiznet5k_send_ethernet(self, p->tot_len, self->eth_frame); return ERR_OK; } @@ -223,6 +231,9 @@ STATIC void wiznet5k_lwip_poll(void *self_in, struct netif *netif) { wiznet5k_obj_t *self = self_in; uint16_t len; while ((len = wiznet5k_recv_ethernet(self)) > 0) { + if (self->trace_flags & TRACE_ETH_RX) { + netutils_ethernet_trace(MP_PYTHON_PRINTER, len, self->eth_frame, NETUTILS_TRACE_NEWLINE); + } struct pbuf *p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); if (p != NULL) { pbuf_take(p, self->eth_frame, len); @@ -260,6 +271,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size wiznet5k_obj.spi = spi; wiznet5k_obj.cs = cs; wiznet5k_obj.rst = rst; + wiznet5k_obj.trace_flags = 0; // Return wiznet5k object return MP_OBJ_FROM_PTR(&wiznet5k_obj); @@ -381,7 +393,22 @@ STATIC mp_obj_t wiznet5k_config(size_t n_args, const mp_obj_t *args, mp_map_t *k if (n_args != 1) { mp_raise_TypeError("can't specify pos and kw args"); } - mp_raise_ValueError("unknown config param"); + + for (size_t i = 0; i < kwargs->alloc; ++i) { + if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) { + mp_map_elem_t *e = &kwargs->table[i]; + switch (mp_obj_str_get_qstr(e->key)) { + case MP_QSTR_trace: { + self->trace_flags = mp_obj_get_int(e->value); + break; + } + default: + mp_raise_ValueError("unknown config param"); + } + } + } + + return mp_const_none; } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wiznet5k_config_obj, 1, wiznet5k_config);