From 503e9d2264694369b9d6faaad85a5f9ec9f81b47 Mon Sep 17 00:00:00 2001 From: Henry Howland <43282194+HLCHowland@users.noreply.github.com> Date: Wed, 13 Jul 2022 21:36:33 -0500 Subject: [PATCH 01/10] Merge pull request #1 from PlanetaryRobotics/time_synchronization Time synchronization --- src/apps/sntp/sntp.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/apps/sntp/sntp.c b/src/apps/sntp/sntp.c index 0e7f36520..ef482baa0 100644 --- a/src/apps/sntp/sntp.c +++ b/src/apps/sntp/sntp.c @@ -126,6 +126,10 @@ # endif #endif /* !SNTP_FRAC_TO_US */ + + +// static void SNTP_SET_SYSTEM_TIME(u32_t sec); + /* Configure behaviour depending on native, microsecond or second precision. * Treat NTP timestamps as signed two's-complement integers. This way, * timestamps that have the MSB set simply become negative offsets from @@ -137,11 +141,18 @@ # define SNTP_SET_SYSTEM_TIME_NTP(s, f) \ SNTP_SET_SYSTEM_TIME_US((u32_t)((s) + DIFF_SEC_1970_2036), SNTP_FRAC_TO_US(f)) # else -# define SNTP_SET_SYSTEM_TIME_NTP(s, f) \ - SNTP_SET_SYSTEM_TIME((u32_t)((s) + DIFF_SEC_1970_2036)) +// # define SNTP_SET_SYSTEM_TIME_NTP(s, f) SNTP_SET_SYSTEM_TIME((u32_t)((s) + DIFF_SEC_1970_2036)) +# define SNTP_SET_SYSTEM_TIME_NTP(s, f) sntp_set_system_time((u32_t)((s) + DIFF_SEC_1970_2036)) # endif #endif /* !SNTP_SET_SYSTEM_TIME_NTP */ +static void sntp_set_system_time(u32_t sec) +{ + Time_setUnixEpoch(sec); +} + + + /* Get the system time either natively as NTP timestamp or convert from * Unix time in seconds and microseconds. Take care to avoid overflow if the * microsecond value is at the maximum of 999999. Also add 0.5 us fudge to @@ -945,4 +956,4 @@ sntp_getservername(u8_t idx) } #endif /* SNTP_SERVER_DNS */ -#endif /* LWIP_UDP */ +#endif /* LWIP_UDP */ \ No newline at end of file From 6ab305fe9bb89135bf3f662a379e8c8ab725a6ba Mon Sep 17 00:00:00 2001 From: Saksham Khurana <8430313+sakshamkhurana@users.noreply.github.com> Date: Mon, 25 Jul 2022 14:46:45 -0400 Subject: [PATCH 02/10] Merge pull request #2 from PlanetaryRobotics/origin/lwip_dma SLIP functionality to process multiple bytes at a time --- src/netif/slipif.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/netif/slipif.c b/src/netif/slipif.c index c8e4eb371..ae19f3e00 100644 --- a/src/netif/slipif.c +++ b/src/netif/slipif.c @@ -307,10 +307,15 @@ slipif_rxbyte(struct netif *netif, u8_t c) * @param c received character */ static void -slipif_rxbyte_input(struct netif *netif, u8_t c) +slipif_rxbyte_input(struct netif *netif, u8_t *c) { struct pbuf *p; - p = slipif_rxbyte(netif, c); + u16_t i = 0; + for(i = 0; (i == 0) || ((i < 100) && (p == NULL)); i++) + { + p = slipif_rxbyte(netif, c[i]); + } + if (p != NULL) { if (netif->input(p, netif) != ERR_OK) { pbuf_free(p); @@ -329,13 +334,13 @@ slipif_rxbyte_input(struct netif *netif, u8_t c) static void slipif_loop_thread(void *nf) { - u8_t c; + u8_t c[500]; struct netif *netif = (struct netif *)nf; struct slipif_priv *priv = (struct slipif_priv *)netif->state; while (1) { - if (sio_read(priv->sd, &c, 1) > 0) { - slipif_rxbyte_input(netif, c); + if (sio_read(priv->sd, c, 100) > 0) { + slipif_rxbyte_input(netif, c); } } } From d276e76acb71f6bbc4130dd6c877d451e7f5d1bf Mon Sep 17 00:00:00 2001 From: Saksham Khurana Date: Mon, 25 Jul 2022 17:30:42 -0400 Subject: [PATCH 03/10] Only comments for slip DMA change and clang format --- src/netif/slipif.c | 621 ++++++++++++++++++++++----------------------- 1 file changed, 310 insertions(+), 311 deletions(-) diff --git a/src/netif/slipif.c b/src/netif/slipif.c index ae19f3e00..b5009d26b 100644 --- a/src/netif/slipif.c +++ b/src/netif/slipif.c @@ -38,13 +38,13 @@ * Simon Goldschmidt */ - /** * @defgroup slipif SLIP * @ingroup netifs * * This is an arch independent SLIP netif. The specific serial hooks must be - * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send + * provided by another file. They are sio_open, sio_read/sio_tryread and + * sio_send * * Usage: This netif can be used in three ways: * 1. For NO_SYS==0, an RX thread can be used which blocks on sio_read() @@ -59,20 +59,21 @@ * */ -#include "netif/slipif.h" -#include "lwip/opt.h" - #include "lwip/def.h" +#include "lwip/opt.h" #include "lwip/pbuf.h" -#include "lwip/stats.h" +#include "lwip/sio.h" #include "lwip/snmp.h" +#include "lwip/stats.h" #include "lwip/sys.h" -#include "lwip/sio.h" +#include "netif/slipif.h" -#define SLIP_END 0xC0 /* 0300: start and end of every packet */ -#define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */ -#define SLIP_ESC_END 0xDC /* 0334: following escape: original byte is 0xC0 (END) */ -#define SLIP_ESC_ESC 0xDD /* 0335: following escape: original byte is 0xDB (ESC) */ +#define SLIP_END 0xC0 /* 0300: start and end of every packet */ +#define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */ +#define SLIP_ESC_END \ + 0xDC /* 0334: following escape: original byte is 0xC0 (END) */ +#define SLIP_ESC_ESC \ + 0xDD /* 0335: following escape: original byte is 0xDB (ESC) */ /** Maximum packet size that is received by this netif */ #ifndef SLIP_MAX_SIZE @@ -87,19 +88,21 @@ #define SLIP_SIO_SPEED(sio_fd) 0 #endif -enum slipif_recv_state { - SLIP_RECV_NORMAL, - SLIP_RECV_ESCAPE +enum slipif_recv_state +{ + SLIP_RECV_NORMAL, + SLIP_RECV_ESCAPE }; struct slipif_priv { - sio_fd_t sd; - /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */ - struct pbuf *p, *q; - u8_t state; - u16_t i, recved; + sio_fd_t sd; + /* q is the whole pbuf chain for a packet, p is the current pbuf in the + * chain */ + struct pbuf *p, *q; + u8_t state; + u16_t i, recved; #if SLIP_RX_FROM_ISR - struct pbuf *rxpackets; + struct pbuf *rxpackets; #endif }; @@ -110,51 +113,51 @@ struct slipif_priv { * * @param netif the lwip network interface structure for this slipif * @param p the pbuf chain packet to send - * @return always returns ERR_OK since the serial layer does not provide return values + * @return always returns ERR_OK since the serial layer does not provide return + * values */ -static err_t -slipif_output(struct netif *netif, struct pbuf *p) -{ - struct slipif_priv *priv; - struct pbuf *q; - u16_t i; - u8_t c; - - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - LWIP_ASSERT("p != NULL", (p != NULL)); - - LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output: sending %"U16_F" bytes\n", p->tot_len)); - priv = (struct slipif_priv *)netif->state; - - /* Send pbuf out on the serial I/O device. */ - /* Start with packet delimiter. */ - sio_send(SLIP_END, priv->sd); - - for (q = p; q != NULL; q = q->next) { - for (i = 0; i < q->len; i++) { - c = ((u8_t *)q->payload)[i]; - switch (c) { - case SLIP_END: - /* need to escape this byte (0xC0 -> 0xDB, 0xDC) */ - sio_send(SLIP_ESC, priv->sd); - sio_send(SLIP_ESC_END, priv->sd); - break; - case SLIP_ESC: - /* need to escape this byte (0xDB -> 0xDB, 0xDD) */ - sio_send(SLIP_ESC, priv->sd); - sio_send(SLIP_ESC_ESC, priv->sd); - break; - default: - /* normal byte - no need for escaping */ - sio_send(c, priv->sd); - break; - } +static err_t slipif_output(struct netif *netif, struct pbuf *p) { + struct slipif_priv *priv; + struct pbuf *q; + u16_t i; + u8_t c; + + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + LWIP_ASSERT("p != NULL", (p != NULL)); + + LWIP_DEBUGF(SLIP_DEBUG, + ("slipif_output: sending %" U16_F " bytes\n", p->tot_len)); + priv = (struct slipif_priv *)netif->state; + + /* Send pbuf out on the serial I/O device. */ + /* Start with packet delimiter. */ + sio_send(SLIP_END, priv->sd); + + for (q = p; q != NULL; q = q->next) { + for (i = 0; i < q->len; i++) { + c = ((u8_t *)q->payload)[i]; + switch (c) { + case SLIP_END: + /* need to escape this byte (0xC0 -> 0xDB, 0xDC) */ + sio_send(SLIP_ESC, priv->sd); + sio_send(SLIP_ESC_END, priv->sd); + break; + case SLIP_ESC: + /* need to escape this byte (0xDB -> 0xDB, 0xDD) */ + sio_send(SLIP_ESC, priv->sd); + sio_send(SLIP_ESC_ESC, priv->sd); + break; + default: + /* normal byte - no need for escaping */ + sio_send(c, priv->sd); + break; + } + } } - } - /* End with packet delimiter. */ - sio_send(SLIP_END, priv->sd); - return ERR_OK; + /* End with packet delimiter. */ + sio_send(SLIP_END, priv->sd); + return ERR_OK; } #if LWIP_IPV4 @@ -166,13 +169,13 @@ slipif_output(struct netif *netif, struct pbuf *p) * @param netif the lwip network interface structure for this slipif * @param p the pbuf chain packet to send * @param ipaddr the ip address to send the packet to (not used for slipif) - * @return always returns ERR_OK since the serial layer does not provide return values + * @return always returns ERR_OK since the serial layer does not provide return + * values */ -static err_t -slipif_output_v4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr) -{ - LWIP_UNUSED_ARG(ipaddr); - return slipif_output(netif, p); +static err_t slipif_output_v4(struct netif *netif, struct pbuf *p, + const ip4_addr_t *ipaddr) { + LWIP_UNUSED_ARG(ipaddr); + return slipif_output(netif, p); } #endif /* LWIP_IPV4 */ @@ -185,13 +188,13 @@ slipif_output_v4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr) * @param netif the lwip network interface structure for this slipif * @param p the pbuf chain packet to send * @param ipaddr the ip address to send the packet to (not used for slipif) - * @return always returns ERR_OK since the serial layer does not provide return values + * @return always returns ERR_OK since the serial layer does not provide return + * values */ -static err_t -slipif_output_v6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr) -{ - LWIP_UNUSED_ARG(ipaddr); - return slipif_output(netif, p); +static err_t slipif_output_v6(struct netif *netif, struct pbuf *p, + const ip6_addr_t *ipaddr) { + LWIP_UNUSED_ARG(ipaddr); + return slipif_output(netif, p); } #endif /* LWIP_IPV6 */ @@ -203,102 +206,105 @@ slipif_output_v6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr) * return a complete packet, NULL is returned before - used for polling) * @return The IP packet when SLIP_END is received */ -static struct pbuf * -slipif_rxbyte(struct netif *netif, u8_t c) -{ - struct slipif_priv *priv; - struct pbuf *t; - - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - - priv = (struct slipif_priv *)netif->state; - - switch (priv->state) { - case SLIP_RECV_NORMAL: - switch (c) { - case SLIP_END: - if (priv->recved > 0) { - /* Received whole packet. */ - /* Trim the pbuf to the size of the received packet. */ - pbuf_realloc(priv->q, priv->recved); - - LINK_STATS_INC(link.recv); - - LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet (%"U16_F" bytes)\n", priv->recved)); - t = priv->q; - priv->p = priv->q = NULL; - priv->i = priv->recved = 0; - return t; - } - return NULL; - case SLIP_ESC: - priv->state = SLIP_RECV_ESCAPE; - return NULL; - default: - break; - } /* end switch (c) */ - break; - case SLIP_RECV_ESCAPE: - /* un-escape END or ESC bytes, leave other bytes - (although that would be a protocol error) */ - switch (c) { - case SLIP_ESC_END: - c = SLIP_END; - break; - case SLIP_ESC_ESC: - c = SLIP_ESC; - break; +static struct pbuf *slipif_rxbyte(struct netif *netif, u8_t c) { + struct slipif_priv *priv; + struct pbuf *t; + + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + + priv = (struct slipif_priv *)netif->state; + + switch (priv->state) { + case SLIP_RECV_NORMAL: + switch (c) { + case SLIP_END: + if (priv->recved > 0) { + /* Received whole packet. */ + /* Trim the pbuf to the size of the received packet. */ + pbuf_realloc(priv->q, priv->recved); + + LINK_STATS_INC(link.recv); + + LWIP_DEBUGF(SLIP_DEBUG, + ("slipif: Got packet (%" U16_F " bytes)\n", + priv->recved)); + t = priv->q; + priv->p = priv->q = NULL; + priv->i = priv->recved = 0; + return t; + } + return NULL; + case SLIP_ESC: + priv->state = SLIP_RECV_ESCAPE; + return NULL; + default: + break; + } /* end switch (c) */ + break; + case SLIP_RECV_ESCAPE: + /* un-escape END or ESC bytes, leave other bytes + (although that would be a protocol error) */ + switch (c) { + case SLIP_ESC_END: + c = SLIP_END; + break; + case SLIP_ESC_ESC: + c = SLIP_ESC; + break; + default: + break; + } + priv->state = SLIP_RECV_NORMAL; + break; default: - break; - } - priv->state = SLIP_RECV_NORMAL; - break; - default: - break; - } /* end switch (priv->state) */ - - /* byte received, packet not yet completely received */ - if (priv->p == NULL) { - /* allocate a new pbuf */ - LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n")); - priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN - PBUF_LINK_ENCAPSULATION_HLEN), PBUF_POOL); + break; + } /* end switch (priv->state) */ + /* byte received, packet not yet completely received */ if (priv->p == NULL) { - LINK_STATS_INC(link.drop); - LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n")); - /* don't process any further since we got no pbuf to receive to */ - return NULL; + /* allocate a new pbuf */ + LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n")); + priv->p = pbuf_alloc( + PBUF_LINK, + (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN - PBUF_LINK_ENCAPSULATION_HLEN), + PBUF_POOL); + + if (priv->p == NULL) { + LINK_STATS_INC(link.drop); + LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n")); + /* don't process any further since we got no pbuf to receive to */ + return NULL; + } + + if (priv->q != NULL) { + /* 'chain' the pbuf to the existing chain */ + pbuf_cat(priv->q, priv->p); + } else { + /* p is the first pbuf in the chain */ + priv->q = priv->p; + } } - if (priv->q != NULL) { - /* 'chain' the pbuf to the existing chain */ - pbuf_cat(priv->q, priv->p); - } else { - /* p is the first pbuf in the chain */ - priv->q = priv->p; - } - } - - /* this automatically drops bytes if > SLIP_MAX_SIZE */ - if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) { - ((u8_t *)priv->p->payload)[priv->i] = c; - priv->recved++; - priv->i++; - if (priv->i >= priv->p->len) { - /* on to the next pbuf */ - priv->i = 0; - if (priv->p->next != NULL && priv->p->next->len > 0) { - /* p is a chain, on to the next in the chain */ - priv->p = priv->p->next; - } else { - /* p is a single pbuf, set it to NULL so next time a new - * pbuf is allocated */ - priv->p = NULL; - } + /* this automatically drops bytes if > SLIP_MAX_SIZE */ + if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) { + ((u8_t *)priv->p->payload)[priv->i] = c; + priv->recved++; + priv->i++; + if (priv->i >= priv->p->len) { + /* on to the next pbuf */ + priv->i = 0; + if (priv->p->next != NULL && priv->p->next->len > 0) { + /* p is a chain, on to the next in the chain */ + priv->p = priv->p->next; + } else { + /* p is a single pbuf, set it to NULL so next time a new + * pbuf is allocated */ + priv->p = NULL; + } + } } - } - return NULL; + return NULL; } /** Like slipif_rxbyte, but passes completed packets to netif->input @@ -306,21 +312,27 @@ slipif_rxbyte(struct netif *netif, u8_t c) * @param netif The lwip network interface structure for this slipif * @param c received character */ -static void -slipif_rxbyte_input(struct netif *netif, u8_t *c) -{ - struct pbuf *p; - u16_t i = 0; - for(i = 0; (i == 0) || ((i < 100) && (p == NULL)); i++) - { - p = slipif_rxbyte(netif, c[i]); - } - - if (p != NULL) { - if (netif->input(p, netif) != ERR_OK) { - pbuf_free(p); +static void slipif_rxbyte_input(struct netif *netif, u8_t *c) { + struct pbuf *p; + u16_t i = 0; + /** Walk through every character that DMA populated. + * i = 0 condition takes care of p being non-null before the loop starts. + * This is in accordance to original logic where p was populated by + * slipif_rxbyte before checking for the non-null condition. The remaining + * iterations work as per original SLIP logic and loop breaks at p != NULL. + * i < 100 condition ensure we are not reading memory that was not supposed + * to be written by the DMA. + */ + for (i = 0; (i == 0) || ((i < 100) && (p == NULL)); i++) { + p = slipif_rxbyte(netif, c[i]); + } + + /** Original slip logic, preceeded by p = slipif_rxbyte statement. */ + if (p != NULL) { + if (netif->input(p, netif) != ERR_OK) { + pbuf_free(p); + } } - } } #if SLIP_USE_RX_THREAD @@ -331,18 +343,16 @@ slipif_rxbyte_input(struct netif *netif, u8_t *c) * * @param nf the lwip network interface structure for this slipif */ -static void -slipif_loop_thread(void *nf) -{ - u8_t c[500]; - struct netif *netif = (struct netif *)nf; - struct slipif_priv *priv = (struct slipif_priv *)netif->state; - - while (1) { - if (sio_read(priv->sd, c, 100) > 0) { - slipif_rxbyte_input(netif, c); +static void slipif_loop_thread(void *nf) { + u8_t c[500]; + struct netif *netif = (struct netif *)nf; + struct slipif_priv *priv = (struct slipif_priv *)netif->state; + + while (1) { + if (sio_read(priv->sd, c, 100) > 0) { + slipif_rxbyte_input(netif, c); + } } - } } #endif /* SLIP_USE_RX_THREAD */ @@ -361,64 +371,63 @@ slipif_loop_thread(void *nf) * @note If netif->state is interpreted as an u8_t serial port number. * */ -err_t -slipif_init(struct netif *netif) -{ - struct slipif_priv *priv; - u8_t sio_num; +err_t slipif_init(struct netif *netif) { + struct slipif_priv *priv; + u8_t sio_num; - LWIP_ASSERT("slipif needs an input callback", netif->input != NULL); + LWIP_ASSERT("slipif needs an input callback", netif->input != NULL); - /* netif->state contains serial port number */ - sio_num = LWIP_PTR_NUMERIC_CAST(u8_t, netif->state); + /* netif->state contains serial port number */ + sio_num = LWIP_PTR_NUMERIC_CAST(u8_t, netif->state); - LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)sio_num)); + LWIP_DEBUGF(SLIP_DEBUG, + ("slipif_init: netif->num=%" U16_F "\n", (u16_t)sio_num)); - /* Allocate private data */ - priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv)); - if (!priv) { - return ERR_MEM; - } + /* Allocate private data */ + priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv)); + if (!priv) { + return ERR_MEM; + } - netif->name[0] = 's'; - netif->name[1] = 'l'; + netif->name[0] = 's'; + netif->name[1] = 'l'; #if LWIP_IPV4 - netif->output = slipif_output_v4; + netif->output = slipif_output_v4; #endif /* LWIP_IPV4 */ #if LWIP_IPV6 - netif->output_ip6 = slipif_output_v6; + netif->output_ip6 = slipif_output_v6; #endif /* LWIP_IPV6 */ - netif->mtu = SLIP_MAX_SIZE; - - /* Try to open the serial port. */ - priv->sd = sio_open(sio_num); - if (!priv->sd) { - /* Opening the serial port failed. */ - mem_free(priv); - return ERR_IF; - } - - /* Initialize private data */ - priv->p = NULL; - priv->q = NULL; - priv->state = SLIP_RECV_NORMAL; - priv->i = 0; - priv->recved = 0; + netif->mtu = SLIP_MAX_SIZE; + + /* Try to open the serial port. */ + priv->sd = sio_open(sio_num); + if (!priv->sd) { + /* Opening the serial port failed. */ + mem_free(priv); + return ERR_IF; + } + + /* Initialize private data */ + priv->p = NULL; + priv->q = NULL; + priv->state = SLIP_RECV_NORMAL; + priv->i = 0; + priv->recved = 0; #if SLIP_RX_FROM_ISR - priv->rxpackets = NULL; + priv->rxpackets = NULL; #endif - netif->state = priv; + netif->state = priv; - /* initialize the snmp variables and counters inside the struct netif */ - MIB2_INIT_NETIF(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd)); + /* initialize the snmp variables and counters inside the struct netif */ + MIB2_INIT_NETIF(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd)); #if SLIP_USE_RX_THREAD - /* Create a thread to poll the serial line. */ - sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif, - SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO); + /* Create a thread to poll the serial line. */ + sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif, + SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO); #endif /* SLIP_USE_RX_THREAD */ - return ERR_OK; + return ERR_OK; } /** @@ -427,20 +436,18 @@ slipif_init(struct netif *netif) * * @param netif The lwip network interface structure for this slipif */ -void -slipif_poll(struct netif *netif) -{ - u8_t c; - struct slipif_priv *priv; +void slipif_poll(struct netif *netif) { + u8_t c; + struct slipif_priv *priv; - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - priv = (struct slipif_priv *)netif->state; + priv = (struct slipif_priv *)netif->state; - while (sio_tryread(priv->sd, &c, 1) > 0) { - slipif_rxbyte_input(netif, c); - } + while (sio_tryread(priv->sd, &c, 1) > 0) { + slipif_rxbyte_input(netif, c); + } } #if SLIP_RX_FROM_ISR @@ -450,38 +457,36 @@ slipif_poll(struct netif *netif) * * @param netif The lwip network interface structure for this slipif */ -void -slipif_process_rxqueue(struct netif *netif) -{ - struct slipif_priv *priv; - SYS_ARCH_DECL_PROTECT(old_level); +void slipif_process_rxqueue(struct netif *netif) { + struct slipif_priv *priv; + SYS_ARCH_DECL_PROTECT(old_level); - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - priv = (struct slipif_priv *)netif->state; + priv = (struct slipif_priv *)netif->state; - SYS_ARCH_PROTECT(old_level); - while (priv->rxpackets != NULL) { - struct pbuf *p = priv->rxpackets; + SYS_ARCH_PROTECT(old_level); + while (priv->rxpackets != NULL) { + struct pbuf *p = priv->rxpackets; #if SLIP_RX_QUEUE - /* dequeue packet */ - struct pbuf *q = p; - while ((q->len != q->tot_len) && (q->next != NULL)) { - q = q->next; - } - priv->rxpackets = q->next; - q->next = NULL; -#else /* SLIP_RX_QUEUE */ - priv->rxpackets = NULL; + /* dequeue packet */ + struct pbuf *q = p; + while ((q->len != q->tot_len) && (q->next != NULL)) { + q = q->next; + } + priv->rxpackets = q->next; + q->next = NULL; +#else /* SLIP_RX_QUEUE */ + priv->rxpackets = NULL; #endif /* SLIP_RX_QUEUE */ - SYS_ARCH_UNPROTECT(old_level); - if (netif->input(p, netif) != ERR_OK) { - pbuf_free(p); + SYS_ARCH_UNPROTECT(old_level); + if (netif->input(p, netif) != ERR_OK) { + pbuf_free(p); + } + SYS_ARCH_PROTECT(old_level); } - SYS_ARCH_PROTECT(old_level); - } - SYS_ARCH_UNPROTECT(old_level); + SYS_ARCH_UNPROTECT(old_level); } /** Like slipif_rxbyte, but queues completed packets. @@ -489,34 +494,32 @@ slipif_process_rxqueue(struct netif *netif) * @param netif The lwip network interface structure for this slipif * @param data Received serial byte */ -static void -slipif_rxbyte_enqueue(struct netif *netif, u8_t data) -{ - struct pbuf *p; - struct slipif_priv *priv = (struct slipif_priv *)netif->state; - SYS_ARCH_DECL_PROTECT(old_level); - - p = slipif_rxbyte(netif, data); - if (p != NULL) { - SYS_ARCH_PROTECT(old_level); - if (priv->rxpackets != NULL) { +static void slipif_rxbyte_enqueue(struct netif *netif, u8_t data) { + struct pbuf *p; + struct slipif_priv *priv = (struct slipif_priv *)netif->state; + SYS_ARCH_DECL_PROTECT(old_level); + + p = slipif_rxbyte(netif, data); + if (p != NULL) { + SYS_ARCH_PROTECT(old_level); + if (priv->rxpackets != NULL) { #if SLIP_RX_QUEUE - /* queue multiple pbufs */ - struct pbuf *q = p; - while (q->next != NULL) { - q = q->next; - } - q->next = p; - } else { -#else /* SLIP_RX_QUEUE */ - pbuf_free(priv->rxpackets); - } - { + /* queue multiple pbufs */ + struct pbuf *q = p; + while (q->next != NULL) { + q = q->next; + } + q->next = p; + } else { +#else /* SLIP_RX_QUEUE */ + pbuf_free(priv->rxpackets); + } + { #endif /* SLIP_RX_QUEUE */ - priv->rxpackets = p; + priv->rxpackets = p; + } + SYS_ARCH_UNPROTECT(old_level); } - SYS_ARCH_UNPROTECT(old_level); - } } /** @@ -529,12 +532,10 @@ slipif_rxbyte_enqueue(struct netif *netif, u8_t data) * @param netif The lwip network interface structure for this slipif * @param data received character */ -void -slipif_received_byte(struct netif *netif, u8_t data) -{ - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - slipif_rxbyte_enqueue(netif, data); +void slipif_received_byte(struct netif *netif, u8_t data) { + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + slipif_rxbyte_enqueue(netif, data); } /** @@ -548,16 +549,14 @@ slipif_received_byte(struct netif *netif, u8_t data) * @param data received character * @param len Number of received characters */ -void -slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len) -{ - u8_t i; - u8_t *rxdata = data; - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - - for (i = 0; i < len; i++, rxdata++) { - slipif_rxbyte_enqueue(netif, *rxdata); - } +void slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len) { + u8_t i; + u8_t *rxdata = data; + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + + for (i = 0; i < len; i++, rxdata++) { + slipif_rxbyte_enqueue(netif, *rxdata); + } } #endif /* SLIP_RX_FROM_ISR */ From a859f6ed3439a052a2ca826edb6074c0d5368cd6 Mon Sep 17 00:00:00 2001 From: mlang25 <89793126+mlang25@users.noreply.github.com> Date: Fri, 10 Feb 2023 19:15:23 -0500 Subject: [PATCH 04/10] Change the SLIP to use DMA and not byte by byte --- contrib/ports/unix/port/include/netif/sio.h | 3 +- src/include/lwip/sio.h | 9 +- src/netif/slipif.c | 638 ++++++++++---------- 3 files changed, 340 insertions(+), 310 deletions(-) diff --git a/contrib/ports/unix/port/include/netif/sio.h b/contrib/ports/unix/port/include/netif/sio.h index b9ff131b7..9ab011e12 100644 --- a/contrib/ports/unix/port/include/netif/sio.h +++ b/contrib/ports/unix/port/include/netif/sio.h @@ -40,7 +40,7 @@ void sio_expect_string(u8_t *str, sio_status_t * siostat); * @param str pointer to a zero terminated string * @param siostat siostatus struct, contains sio instance data, given by sio_open */ -void sio_send_string(u8_t *str, sio_status_t * siostat); +//void sio_send_string(u8_t *str, sio_status_t * siostat); /** * Flush outbuffer (send everything in buffer now), useful if some layer below is @@ -57,4 +57,3 @@ void sio_flush( sio_status_t * siostat ); void sio_change_baud( sioBaudrates baud, sio_status_t * siostat ); #endif - diff --git a/src/include/lwip/sio.h b/src/include/lwip/sio.h index 12d2a7e9b..e273b7ce4 100644 --- a/src/include/lwip/sio.h +++ b/src/include/lwip/sio.h @@ -135,8 +135,15 @@ u32_t sio_write(sio_fd_t fd, const u8_t *data, u32_t len); void sio_read_abort(sio_fd_t fd); #endif +/** +* Write a char to output data stream +* @param str pointer to a zero terminated string +* @param siostat siostatus struct, contains sio instance data, given by sio_open +*/ +void sio_send_string(u8_t *str, sio_fd_t * siostat, u16_t len); + #ifdef __cplusplus } #endif -#endif /* SIO_H */ +#endif /* SIO_H */ \ No newline at end of file diff --git a/src/netif/slipif.c b/src/netif/slipif.c index c8e4eb371..29ee7b1b6 100644 --- a/src/netif/slipif.c +++ b/src/netif/slipif.c @@ -38,13 +38,13 @@ * Simon Goldschmidt */ - /** * @defgroup slipif SLIP * @ingroup netifs * * This is an arch independent SLIP netif. The specific serial hooks must be - * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send + * provided by another file. They are sio_open, sio_read/sio_tryread and + * sio_send * * Usage: This netif can be used in three ways: * 1. For NO_SYS==0, an RX thread can be used which blocks on sio_read() @@ -59,20 +59,21 @@ * */ -#include "netif/slipif.h" -#include "lwip/opt.h" - #include "lwip/def.h" +#include "lwip/opt.h" #include "lwip/pbuf.h" -#include "lwip/stats.h" +#include "lwip/sio.h" #include "lwip/snmp.h" +#include "lwip/stats.h" #include "lwip/sys.h" -#include "lwip/sio.h" +#include "netif/slipif.h" -#define SLIP_END 0xC0 /* 0300: start and end of every packet */ -#define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */ -#define SLIP_ESC_END 0xDC /* 0334: following escape: original byte is 0xC0 (END) */ -#define SLIP_ESC_ESC 0xDD /* 0335: following escape: original byte is 0xDB (ESC) */ +#define SLIP_END 0xC0 /* 0300: start and end of every packet */ +#define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */ +#define SLIP_ESC_END \ + 0xDC /* 0334: following escape: original byte is 0xC0 (END) */ +#define SLIP_ESC_ESC \ + 0xDD /* 0335: following escape: original byte is 0xDB (ESC) */ /** Maximum packet size that is received by this netif */ #ifndef SLIP_MAX_SIZE @@ -87,19 +88,21 @@ #define SLIP_SIO_SPEED(sio_fd) 0 #endif -enum slipif_recv_state { - SLIP_RECV_NORMAL, - SLIP_RECV_ESCAPE +enum slipif_recv_state +{ + SLIP_RECV_NORMAL, + SLIP_RECV_ESCAPE }; struct slipif_priv { - sio_fd_t sd; - /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */ - struct pbuf *p, *q; - u8_t state; - u16_t i, recved; + sio_fd_t sd; + /* q is the whole pbuf chain for a packet, p is the current pbuf in the + * chain */ + struct pbuf *p, *q; + u8_t state; + u16_t i, recved; #if SLIP_RX_FROM_ISR - struct pbuf *rxpackets; + struct pbuf *rxpackets; #endif }; @@ -110,51 +113,71 @@ struct slipif_priv { * * @param netif the lwip network interface structure for this slipif * @param p the pbuf chain packet to send - * @return always returns ERR_OK since the serial layer does not provide return values + * @return always returns ERR_OK since the serial layer does not provide return + * values */ -static err_t -slipif_output(struct netif *netif, struct pbuf *p) -{ - struct slipif_priv *priv; - struct pbuf *q; - u16_t i; - u8_t c; - - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - LWIP_ASSERT("p != NULL", (p != NULL)); - - LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output: sending %"U16_F" bytes\n", p->tot_len)); - priv = (struct slipif_priv *)netif->state; - - /* Send pbuf out on the serial I/O device. */ - /* Start with packet delimiter. */ - sio_send(SLIP_END, priv->sd); - - for (q = p; q != NULL; q = q->next) { - for (i = 0; i < q->len; i++) { - c = ((u8_t *)q->payload)[i]; - switch (c) { - case SLIP_END: - /* need to escape this byte (0xC0 -> 0xDB, 0xDC) */ - sio_send(SLIP_ESC, priv->sd); - sio_send(SLIP_ESC_END, priv->sd); - break; - case SLIP_ESC: - /* need to escape this byte (0xDB -> 0xDB, 0xDD) */ - sio_send(SLIP_ESC, priv->sd); - sio_send(SLIP_ESC_ESC, priv->sd); - break; - default: - /* normal byte - no need for escaping */ - sio_send(c, priv->sd); - break; - } +static err_t slipif_output(struct netif *netif, struct pbuf *p) { + struct slipif_priv *priv; + struct pbuf *q; + u16_t i; + u8_t c; + u8_t array[1500] = {0}; + u16_t byte_counter = 0; + u8_t escape = 0; + + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + LWIP_ASSERT("p != NULL", (p != NULL)); + + LWIP_DEBUGF(SLIP_DEBUG, + ("slipif_output: sending %" U16_F " bytes\n", p->tot_len)); + priv = (struct slipif_priv *)netif->state; + + /* Send pbuf out on the serial I/O device. */ + /* Start with packet delimiter. */ + array[byte_counter] = SLIP_END; + byte_counter++; + //sio_send(SLIP_END, priv->sd); + + for (q = p; q != NULL; q = q->next) { + for (i = 0; i < q->len; i++) { + c = ((u8_t *)q->payload)[i]; + switch (c) { + case SLIP_END: + /* need to escape this byte (0xC0 -> 0xDB, 0xDC) */ + //sio_send(SLIP_ESC, priv->sd); + //sio_send(SLIP_ESC_END, priv->sd); + array[byte_counter] = SLIP_ESC; + byte_counter++; + array[byte_counter] = SLIP_ESC_END; + byte_counter++; + escape++; + break; + case SLIP_ESC: + /* need to escape this byte (0xDB -> 0xDB, 0xDD) */ + //sio_send(SLIP_ESC, priv->sd); + //sio_send(SLIP_ESC_ESC, priv->sd); + array[byte_counter] = SLIP_ESC; + byte_counter++; + array[byte_counter] = SLIP_ESC_ESC; + byte_counter++; + escape++; + break; + default: + /* normal byte - no need for escaping */ + //sio_send(c, priv->sd); + array[byte_counter] = c; + byte_counter++; + break; + } + } } - } - /* End with packet delimiter. */ - sio_send(SLIP_END, priv->sd); - return ERR_OK; + /* End with packet delimiter. */ + //sio_send(SLIP_END, priv->sd); + array[byte_counter] = SLIP_END; + byte_counter++; + sio_send_string(array,priv->sd,byte_counter); + return ERR_OK; } #if LWIP_IPV4 @@ -166,13 +189,13 @@ slipif_output(struct netif *netif, struct pbuf *p) * @param netif the lwip network interface structure for this slipif * @param p the pbuf chain packet to send * @param ipaddr the ip address to send the packet to (not used for slipif) - * @return always returns ERR_OK since the serial layer does not provide return values + * @return always returns ERR_OK since the serial layer does not provide return + * values */ -static err_t -slipif_output_v4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr) -{ - LWIP_UNUSED_ARG(ipaddr); - return slipif_output(netif, p); +static err_t slipif_output_v4(struct netif *netif, struct pbuf *p, + const ip4_addr_t *ipaddr) { + LWIP_UNUSED_ARG(ipaddr); + return slipif_output(netif, p); } #endif /* LWIP_IPV4 */ @@ -185,13 +208,13 @@ slipif_output_v4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr) * @param netif the lwip network interface structure for this slipif * @param p the pbuf chain packet to send * @param ipaddr the ip address to send the packet to (not used for slipif) - * @return always returns ERR_OK since the serial layer does not provide return values + * @return always returns ERR_OK since the serial layer does not provide return + * values */ -static err_t -slipif_output_v6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr) -{ - LWIP_UNUSED_ARG(ipaddr); - return slipif_output(netif, p); +static err_t slipif_output_v6(struct netif *netif, struct pbuf *p, + const ip6_addr_t *ipaddr) { + LWIP_UNUSED_ARG(ipaddr); + return slipif_output(netif, p); } #endif /* LWIP_IPV6 */ @@ -203,102 +226,105 @@ slipif_output_v6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr) * return a complete packet, NULL is returned before - used for polling) * @return The IP packet when SLIP_END is received */ -static struct pbuf * -slipif_rxbyte(struct netif *netif, u8_t c) -{ - struct slipif_priv *priv; - struct pbuf *t; - - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - - priv = (struct slipif_priv *)netif->state; - - switch (priv->state) { - case SLIP_RECV_NORMAL: - switch (c) { - case SLIP_END: - if (priv->recved > 0) { - /* Received whole packet. */ - /* Trim the pbuf to the size of the received packet. */ - pbuf_realloc(priv->q, priv->recved); - - LINK_STATS_INC(link.recv); - - LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet (%"U16_F" bytes)\n", priv->recved)); - t = priv->q; - priv->p = priv->q = NULL; - priv->i = priv->recved = 0; - return t; - } - return NULL; - case SLIP_ESC: - priv->state = SLIP_RECV_ESCAPE; - return NULL; - default: - break; - } /* end switch (c) */ - break; - case SLIP_RECV_ESCAPE: - /* un-escape END or ESC bytes, leave other bytes - (although that would be a protocol error) */ - switch (c) { - case SLIP_ESC_END: - c = SLIP_END; - break; - case SLIP_ESC_ESC: - c = SLIP_ESC; - break; +static struct pbuf *slipif_rxbyte(struct netif *netif, u8_t c) { + struct slipif_priv *priv; + struct pbuf *t; + + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + + priv = (struct slipif_priv *)netif->state; + + switch (priv->state) { + case SLIP_RECV_NORMAL: + switch (c) { + case SLIP_END: + if (priv->recved > 0) { + /* Received whole packet. */ + /* Trim the pbuf to the size of the received packet. */ + pbuf_realloc(priv->q, priv->recved); + + LINK_STATS_INC(link.recv); + + LWIP_DEBUGF(SLIP_DEBUG, + ("slipif: Got packet (%" U16_F " bytes)\n", + priv->recved)); + t = priv->q; + priv->p = priv->q = NULL; + priv->i = priv->recved = 0; + return t; + } + return NULL; + case SLIP_ESC: + priv->state = SLIP_RECV_ESCAPE; + return NULL; + default: + break; + } /* end switch (c) */ + break; + case SLIP_RECV_ESCAPE: + /* un-escape END or ESC bytes, leave other bytes + (although that would be a protocol error) */ + switch (c) { + case SLIP_ESC_END: + c = SLIP_END; + break; + case SLIP_ESC_ESC: + c = SLIP_ESC; + break; + default: + break; + } + priv->state = SLIP_RECV_NORMAL; + break; default: - break; - } - priv->state = SLIP_RECV_NORMAL; - break; - default: - break; - } /* end switch (priv->state) */ - - /* byte received, packet not yet completely received */ - if (priv->p == NULL) { - /* allocate a new pbuf */ - LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n")); - priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN - PBUF_LINK_ENCAPSULATION_HLEN), PBUF_POOL); + break; + } /* end switch (priv->state) */ + /* byte received, packet not yet completely received */ if (priv->p == NULL) { - LINK_STATS_INC(link.drop); - LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n")); - /* don't process any further since we got no pbuf to receive to */ - return NULL; + /* allocate a new pbuf */ + LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n")); + priv->p = pbuf_alloc( + PBUF_LINK, + (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN - PBUF_LINK_ENCAPSULATION_HLEN), + PBUF_POOL); + + if (priv->p == NULL) { + LINK_STATS_INC(link.drop); + LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n")); + /* don't process any further since we got no pbuf to receive to */ + return NULL; + } + + if (priv->q != NULL) { + /* 'chain' the pbuf to the existing chain */ + pbuf_cat(priv->q, priv->p); + } else { + /* p is the first pbuf in the chain */ + priv->q = priv->p; + } } - if (priv->q != NULL) { - /* 'chain' the pbuf to the existing chain */ - pbuf_cat(priv->q, priv->p); - } else { - /* p is the first pbuf in the chain */ - priv->q = priv->p; - } - } - - /* this automatically drops bytes if > SLIP_MAX_SIZE */ - if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) { - ((u8_t *)priv->p->payload)[priv->i] = c; - priv->recved++; - priv->i++; - if (priv->i >= priv->p->len) { - /* on to the next pbuf */ - priv->i = 0; - if (priv->p->next != NULL && priv->p->next->len > 0) { - /* p is a chain, on to the next in the chain */ - priv->p = priv->p->next; - } else { - /* p is a single pbuf, set it to NULL so next time a new - * pbuf is allocated */ - priv->p = NULL; - } + /* this automatically drops bytes if > SLIP_MAX_SIZE */ + if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) { + ((u8_t *)priv->p->payload)[priv->i] = c; + priv->recved++; + priv->i++; + if (priv->i >= priv->p->len) { + /* on to the next pbuf */ + priv->i = 0; + if (priv->p->next != NULL && priv->p->next->len > 0) { + /* p is a chain, on to the next in the chain */ + priv->p = priv->p->next; + } else { + /* p is a single pbuf, set it to NULL so next time a new + * pbuf is allocated */ + priv->p = NULL; + } + } } - } - return NULL; + return NULL; } /** Like slipif_rxbyte, but passes completed packets to netif->input @@ -306,16 +332,27 @@ slipif_rxbyte(struct netif *netif, u8_t c) * @param netif The lwip network interface structure for this slipif * @param c received character */ -static void -slipif_rxbyte_input(struct netif *netif, u8_t c) -{ - struct pbuf *p; - p = slipif_rxbyte(netif, c); - if (p != NULL) { - if (netif->input(p, netif) != ERR_OK) { - pbuf_free(p); +static void slipif_rxbyte_input(struct netif *netif, u8_t *c) { + struct pbuf *p; + u16_t i = 0; + /** Walk through every character that DMA populated. + * i = 0 condition takes care of p being non-null before the loop starts. + * This is in accordance to original logic where p was populated by + * slipif_rxbyte before checking for the non-null condition. The remaining + * iterations work as per original SLIP logic and loop breaks at p != NULL. + * i < 100 condition ensure we are not reading memory that was not supposed + * to be written by the DMA. + */ + for (i = 0; (i == 0) || ((i < 100) && (p == NULL)); i++) { + p = slipif_rxbyte(netif, c[i]); + } + + /** Original slip logic, preceeded by p = slipif_rxbyte statement. */ + if (p != NULL) { + if (netif->input(p, netif) != ERR_OK) { + pbuf_free(p); + } } - } } #if SLIP_USE_RX_THREAD @@ -326,18 +363,16 @@ slipif_rxbyte_input(struct netif *netif, u8_t c) * * @param nf the lwip network interface structure for this slipif */ -static void -slipif_loop_thread(void *nf) -{ - u8_t c; - struct netif *netif = (struct netif *)nf; - struct slipif_priv *priv = (struct slipif_priv *)netif->state; - - while (1) { - if (sio_read(priv->sd, &c, 1) > 0) { - slipif_rxbyte_input(netif, c); +static void slipif_loop_thread(void *nf) { + u8_t c[500]; + struct netif *netif = (struct netif *)nf; + struct slipif_priv *priv = (struct slipif_priv *)netif->state; + + while (1) { + if (sio_read(priv->sd, c, 100) > 0) { + slipif_rxbyte_input(netif, c); + } } - } } #endif /* SLIP_USE_RX_THREAD */ @@ -356,64 +391,63 @@ slipif_loop_thread(void *nf) * @note If netif->state is interpreted as an u8_t serial port number. * */ -err_t -slipif_init(struct netif *netif) -{ - struct slipif_priv *priv; - u8_t sio_num; +err_t slipif_init(struct netif *netif) { + struct slipif_priv *priv; + u8_t sio_num; - LWIP_ASSERT("slipif needs an input callback", netif->input != NULL); + LWIP_ASSERT("slipif needs an input callback", netif->input != NULL); - /* netif->state contains serial port number */ - sio_num = LWIP_PTR_NUMERIC_CAST(u8_t, netif->state); + /* netif->state contains serial port number */ + sio_num = LWIP_PTR_NUMERIC_CAST(u8_t, netif->state); - LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)sio_num)); + LWIP_DEBUGF(SLIP_DEBUG, + ("slipif_init: netif->num=%" U16_F "\n", (u16_t)sio_num)); - /* Allocate private data */ - priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv)); - if (!priv) { - return ERR_MEM; - } + /* Allocate private data */ + priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv)); + if (!priv) { + return ERR_MEM; + } - netif->name[0] = 's'; - netif->name[1] = 'l'; + netif->name[0] = 's'; + netif->name[1] = 'l'; #if LWIP_IPV4 - netif->output = slipif_output_v4; + netif->output = slipif_output_v4; #endif /* LWIP_IPV4 */ #if LWIP_IPV6 - netif->output_ip6 = slipif_output_v6; + netif->output_ip6 = slipif_output_v6; #endif /* LWIP_IPV6 */ - netif->mtu = SLIP_MAX_SIZE; - - /* Try to open the serial port. */ - priv->sd = sio_open(sio_num); - if (!priv->sd) { - /* Opening the serial port failed. */ - mem_free(priv); - return ERR_IF; - } - - /* Initialize private data */ - priv->p = NULL; - priv->q = NULL; - priv->state = SLIP_RECV_NORMAL; - priv->i = 0; - priv->recved = 0; + netif->mtu = SLIP_MAX_SIZE; + + /* Try to open the serial port. */ + priv->sd = sio_open(sio_num); + if (!priv->sd) { + /* Opening the serial port failed. */ + mem_free(priv); + return ERR_IF; + } + + /* Initialize private data */ + priv->p = NULL; + priv->q = NULL; + priv->state = SLIP_RECV_NORMAL; + priv->i = 0; + priv->recved = 0; #if SLIP_RX_FROM_ISR - priv->rxpackets = NULL; + priv->rxpackets = NULL; #endif - netif->state = priv; + netif->state = priv; - /* initialize the snmp variables and counters inside the struct netif */ - MIB2_INIT_NETIF(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd)); + /* initialize the snmp variables and counters inside the struct netif */ + MIB2_INIT_NETIF(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd)); #if SLIP_USE_RX_THREAD - /* Create a thread to poll the serial line. */ - sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif, - SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO); + /* Create a thread to poll the serial line. */ + sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif, + SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO); #endif /* SLIP_USE_RX_THREAD */ - return ERR_OK; + return ERR_OK; } /** @@ -422,20 +456,18 @@ slipif_init(struct netif *netif) * * @param netif The lwip network interface structure for this slipif */ -void -slipif_poll(struct netif *netif) -{ - u8_t c; - struct slipif_priv *priv; +void slipif_poll(struct netif *netif) { + u8_t c; + struct slipif_priv *priv; - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - priv = (struct slipif_priv *)netif->state; + priv = (struct slipif_priv *)netif->state; - while (sio_tryread(priv->sd, &c, 1) > 0) { - slipif_rxbyte_input(netif, c); - } + while (sio_tryread(priv->sd, &c, 1) > 0) { + slipif_rxbyte_input(netif, c); + } } #if SLIP_RX_FROM_ISR @@ -445,38 +477,36 @@ slipif_poll(struct netif *netif) * * @param netif The lwip network interface structure for this slipif */ -void -slipif_process_rxqueue(struct netif *netif) -{ - struct slipif_priv *priv; - SYS_ARCH_DECL_PROTECT(old_level); +void slipif_process_rxqueue(struct netif *netif) { + struct slipif_priv *priv; + SYS_ARCH_DECL_PROTECT(old_level); - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - priv = (struct slipif_priv *)netif->state; + priv = (struct slipif_priv *)netif->state; - SYS_ARCH_PROTECT(old_level); - while (priv->rxpackets != NULL) { - struct pbuf *p = priv->rxpackets; + SYS_ARCH_PROTECT(old_level); + while (priv->rxpackets != NULL) { + struct pbuf *p = priv->rxpackets; #if SLIP_RX_QUEUE - /* dequeue packet */ - struct pbuf *q = p; - while ((q->len != q->tot_len) && (q->next != NULL)) { - q = q->next; - } - priv->rxpackets = q->next; - q->next = NULL; -#else /* SLIP_RX_QUEUE */ - priv->rxpackets = NULL; + /* dequeue packet */ + struct pbuf *q = p; + while ((q->len != q->tot_len) && (q->next != NULL)) { + q = q->next; + } + priv->rxpackets = q->next; + q->next = NULL; +#else /* SLIP_RX_QUEUE */ + priv->rxpackets = NULL; #endif /* SLIP_RX_QUEUE */ - SYS_ARCH_UNPROTECT(old_level); - if (netif->input(p, netif) != ERR_OK) { - pbuf_free(p); + SYS_ARCH_UNPROTECT(old_level); + if (netif->input(p, netif) != ERR_OK) { + pbuf_free(p); + } + SYS_ARCH_PROTECT(old_level); } - SYS_ARCH_PROTECT(old_level); - } - SYS_ARCH_UNPROTECT(old_level); + SYS_ARCH_UNPROTECT(old_level); } /** Like slipif_rxbyte, but queues completed packets. @@ -484,34 +514,32 @@ slipif_process_rxqueue(struct netif *netif) * @param netif The lwip network interface structure for this slipif * @param data Received serial byte */ -static void -slipif_rxbyte_enqueue(struct netif *netif, u8_t data) -{ - struct pbuf *p; - struct slipif_priv *priv = (struct slipif_priv *)netif->state; - SYS_ARCH_DECL_PROTECT(old_level); - - p = slipif_rxbyte(netif, data); - if (p != NULL) { - SYS_ARCH_PROTECT(old_level); - if (priv->rxpackets != NULL) { +static void slipif_rxbyte_enqueue(struct netif *netif, u8_t data) { + struct pbuf *p; + struct slipif_priv *priv = (struct slipif_priv *)netif->state; + SYS_ARCH_DECL_PROTECT(old_level); + + p = slipif_rxbyte(netif, data); + if (p != NULL) { + SYS_ARCH_PROTECT(old_level); + if (priv->rxpackets != NULL) { #if SLIP_RX_QUEUE - /* queue multiple pbufs */ - struct pbuf *q = p; - while (q->next != NULL) { - q = q->next; - } - q->next = p; - } else { -#else /* SLIP_RX_QUEUE */ - pbuf_free(priv->rxpackets); - } - { + /* queue multiple pbufs */ + struct pbuf *q = p; + while (q->next != NULL) { + q = q->next; + } + q->next = p; + } else { +#else /* SLIP_RX_QUEUE */ + pbuf_free(priv->rxpackets); + } + { #endif /* SLIP_RX_QUEUE */ - priv->rxpackets = p; + priv->rxpackets = p; + } + SYS_ARCH_UNPROTECT(old_level); } - SYS_ARCH_UNPROTECT(old_level); - } } /** @@ -524,12 +552,10 @@ slipif_rxbyte_enqueue(struct netif *netif, u8_t data) * @param netif The lwip network interface structure for this slipif * @param data received character */ -void -slipif_received_byte(struct netif *netif, u8_t data) -{ - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - slipif_rxbyte_enqueue(netif, data); +void slipif_received_byte(struct netif *netif, u8_t data) { + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + slipif_rxbyte_enqueue(netif, data); } /** @@ -543,16 +569,14 @@ slipif_received_byte(struct netif *netif, u8_t data) * @param data received character * @param len Number of received characters */ -void -slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len) -{ - u8_t i; - u8_t *rxdata = data; - LWIP_ASSERT("netif != NULL", (netif != NULL)); - LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); - - for (i = 0; i < len; i++, rxdata++) { - slipif_rxbyte_enqueue(netif, *rxdata); - } +void slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len) { + u8_t i; + u8_t *rxdata = data; + LWIP_ASSERT("netif != NULL", (netif != NULL)); + LWIP_ASSERT("netif->state != NULL", (netif->state != NULL)); + + for (i = 0; i < len; i++, rxdata++) { + slipif_rxbyte_enqueue(netif, *rxdata); + } } -#endif /* SLIP_RX_FROM_ISR */ +#endif /* SLIP_RX_FROM_ISR */ \ No newline at end of file From bf87e1fabb9f73aecf495b27b510601e8e6298b7 Mon Sep 17 00:00:00 2001 From: mlang25 <89793126+mlang25@users.noreply.github.com> Date: Wed, 15 Feb 2023 18:31:38 -0500 Subject: [PATCH 05/10] Remove mutex for tcpip core --- src/api/tcpip.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/api/tcpip.c b/src/api/tcpip.c index 3aecbd432..fedda2ed4 100644 --- a/src/api/tcpip.c +++ b/src/api/tcpip.c @@ -443,9 +443,14 @@ tcpip_send_msg_wait_sem(tcpip_callback_fn fn, void *apimsg, sys_sem_t *sem) { #if LWIP_TCPIP_CORE_LOCKING LWIP_UNUSED_ARG(sem); - LOCK_TCPIP_CORE(); + /* NOTE: Hacky solution to the tx2i and iOBC contention issue. + Originally tx2i and iobc must wait for other to finish before the + other one begins transmisson. + Removing the lock allows then to run simulaniously + Might result in bad checksum or courrpt packets occastionally*/ + //LOCK_TCPIP_CORE(); fn(apimsg); - UNLOCK_TCPIP_CORE(); + //UNLOCK_TCPIP_CORE(); return ERR_OK; #else /* LWIP_TCPIP_CORE_LOCKING */ TCPIP_MSG_VAR_DECLARE(msg); From f0a669f52728c48f85663f8b826b79a38d0ac03c Mon Sep 17 00:00:00 2001 From: mlang25 <89793126+mlang25@users.noreply.github.com> Date: Tue, 21 Feb 2023 16:28:16 -0500 Subject: [PATCH 06/10] Add intergration with subsecond time --- src/apps/sntp/sntp.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/apps/sntp/sntp.c b/src/apps/sntp/sntp.c index ef482baa0..c7d79f5c6 100644 --- a/src/apps/sntp/sntp.c +++ b/src/apps/sntp/sntp.c @@ -60,6 +60,8 @@ #include "lwip/ip_addr.h" #include "lwip/pbuf.h" #include "lwip/dhcp.h" +#include "time_manager/time_manager.h" + #include #include @@ -149,6 +151,8 @@ static void sntp_set_system_time(u32_t sec) { Time_setUnixEpoch(sec); + SetZeroSubseconds(); + } From 45dabe8c9eb5052bda836cb0af6b3aac2ae876f4 Mon Sep 17 00:00:00 2001 From: mlang25 <89793126+mlang25@users.noreply.github.com> Date: Tue, 28 Feb 2023 15:36:06 -0500 Subject: [PATCH 07/10] fix include path for eclipse --- src/apps/sntp/sntp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apps/sntp/sntp.c b/src/apps/sntp/sntp.c index c7d79f5c6..5dec7bf2b 100644 --- a/src/apps/sntp/sntp.c +++ b/src/apps/sntp/sntp.c @@ -60,7 +60,7 @@ #include "lwip/ip_addr.h" #include "lwip/pbuf.h" #include "lwip/dhcp.h" -#include "time_manager/time_manager.h" +#include "time_manager.h" #include From 8ac6e57a53deac1e0be730509afeda8a63fd1305 Mon Sep 17 00:00:00 2001 From: laptop <89793126+mlang25@users.noreply.github.com> Date: Wed, 1 Mar 2023 09:32:15 -0500 Subject: [PATCH 08/10] Only sync once --- src/apps/sntp/sntp.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/apps/sntp/sntp.c b/src/apps/sntp/sntp.c index 5dec7bf2b..53b0e797a 100644 --- a/src/apps/sntp/sntp.c +++ b/src/apps/sntp/sntp.c @@ -152,6 +152,10 @@ static void sntp_set_system_time(u32_t sec) { Time_setUnixEpoch(sec); SetZeroSubseconds(); + + //TODO: Remove before merge + //Added at the request for endurance testing + sntp_stop(); } From 7e1fa13378b2496f5bcac40eb76a1461b51ec584 Mon Sep 17 00:00:00 2001 From: mlang25 <89793126+mlang25@users.noreply.github.com> Date: Wed, 12 Apr 2023 18:11:13 -0400 Subject: [PATCH 09/10] set sntp to listen. working in flatsat 2.0 --- src/apps/sntp/sntp.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/apps/sntp/sntp.c b/src/apps/sntp/sntp.c index 53b0e797a..d458307af 100644 --- a/src/apps/sntp/sntp.c +++ b/src/apps/sntp/sntp.c @@ -144,17 +144,17 @@ SNTP_SET_SYSTEM_TIME_US((u32_t)((s) + DIFF_SEC_1970_2036), SNTP_FRAC_TO_US(f)) # else // # define SNTP_SET_SYSTEM_TIME_NTP(s, f) SNTP_SET_SYSTEM_TIME((u32_t)((s) + DIFF_SEC_1970_2036)) -# define SNTP_SET_SYSTEM_TIME_NTP(s, f) sntp_set_system_time((u32_t)((s) + DIFF_SEC_1970_2036)) +# define SNTP_SET_SYSTEM_TIME_NTP(s, f) sntp_set_system_time((u32_t)((s) + DIFF_SEC_1970_2036),f) # endif #endif /* !SNTP_SET_SYSTEM_TIME_NTP */ -static void sntp_set_system_time(u32_t sec) +static void sntp_set_system_time(u32_t sec, u32_t useconds) { Time_setUnixEpoch(sec); - SetZeroSubseconds(); + //set subseconds. usecond is micro so divide by 1000 to get miliseconds + SetSubseconds(useconds / 1000); - //TODO: Remove before merge - //Added at the request for endurance testing + //Added to only sync one sntp_stop(); } @@ -701,6 +701,9 @@ sntp_init(void) #endif #endif /* SNTP_SERVER_ADDRESS */ + //set listen mode. Tx2i must set the mode field to be broadcast + sntp_opmode = SNTP_OPMODE_LISTENONLY; + if (sntp_pcb == NULL) { sntp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY); LWIP_ASSERT("Failed to allocate udp pcb for sntp client", sntp_pcb != NULL); From e53f131ff8762dacc574616974098903b33a7f0a Mon Sep 17 00:00:00 2001 From: mlang25 <89793126+mlang25@users.noreply.github.com> Date: Wed, 12 Apr 2023 18:30:23 -0400 Subject: [PATCH 10/10] convert from ntp frac to subsecond --- src/apps/sntp/sntp.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/apps/sntp/sntp.c b/src/apps/sntp/sntp.c index d458307af..518dc5c6a 100644 --- a/src/apps/sntp/sntp.c +++ b/src/apps/sntp/sntp.c @@ -148,11 +148,15 @@ # endif #endif /* !SNTP_SET_SYSTEM_TIME_NTP */ -static void sntp_set_system_time(u32_t sec, u32_t useconds) +static void sntp_set_system_time(u32_t sec, u32_t frac) { + + //convert ntp frac to microseconds + //useconds is divided by 1000 to get ms + u32_t subsecond = SNTP_FRAC_TO_US(frac) / 1000; Time_setUnixEpoch(sec); - //set subseconds. usecond is micro so divide by 1000 to get miliseconds - SetSubseconds(useconds / 1000); + //set subseconds (milliseconds) + SetSubseconds(subsecond); //Added to only sync one sntp_stop(); @@ -348,7 +352,6 @@ sntp_process(const struct sntp_timestamps *timestamps) #endif /* SNTP_COMP_ROUNDTRIP */ SNTP_SET_SYSTEM_TIME_NTP(sec, frac); - LWIP_UNUSED_ARG(frac); /* might be unused if only seconds are set */ LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s, %" U32_F " us\n", sntp_format_time(sec), SNTP_FRAC_TO_US(frac))); }