From 8a9a778375c081be8ae30de52f3f016572915e2a Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Fri, 10 Oct 2025 12:12:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9net=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E4=B8=BA=E6=94=AF=E6=8C=81=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/net_lwip_webserver/src/lwipopts.h | 20 ++++++++--- examples/device/net_lwip_webserver/src/main.c | 33 +++++++++++-------- .../net_lwip_webserver/src/tusb_config.h | 2 +- lib/networking/dnserver.c | 3 +- src/osal/osal_rtthread.h | 17 +++++++--- 5 files changed, 49 insertions(+), 26 deletions(-) diff --git a/examples/device/net_lwip_webserver/src/lwipopts.h b/examples/device/net_lwip_webserver/src/lwipopts.h index 85028585b..9ae8973b4 100644 --- a/examples/device/net_lwip_webserver/src/lwipopts.h +++ b/examples/device/net_lwip_webserver/src/lwipopts.h @@ -3,6 +3,16 @@ #include +#define LWIP_MULTICAST_PING 1 +#define LWIP_BROADCAST_PING 1 +#define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67)) +#define ETHARP_SUPPORT_STATIC_ENTRIES 1 +#define LWIP_SINGLE_NETIF 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define PBUF_POOL_SIZE 4 +#define LWIP_CHECKSUM_ON_COPY 1 + + #define ERRNO 1 #define LWIP_SOCKET_SELECT 1 @@ -346,7 +356,7 @@ #ifdef RT_LWIP_TCP_SND_BUF #define TCP_SND_BUF RT_LWIP_TCP_SND_BUF #else -#define TCP_SND_BUF (TCP_MSS * 2) +#define TCP_SND_BUF (TCP_MSS * 4) #endif /* TCP sender buffer space (pbufs). This must be at least = 2 * @@ -363,7 +373,7 @@ #ifdef RT_LWIP_TCP_WND #define TCP_WND RT_LWIP_TCP_WND #else -#define TCP_WND (TCP_MSS * 2) +#define TCP_WND (TCP_MSS * 4) #endif /* Maximum number of retransmissions of data segments. */ @@ -379,8 +389,8 @@ #define TCPIP_THREAD_STACKSIZE RT_LWIP_TCPTHREAD_STACKSIZE #else #define TCPIP_MBOX_SIZE 8 -#define TCPIP_THREAD_PRIO 7 -#define TCPIP_THREAD_STACKSIZE 4096 +#define TCPIP_THREAD_PRIO 5 +#define TCPIP_THREAD_STACKSIZE 4096*2 #endif #define TCPIP_THREAD_NAME "tcpip" #define DEFAULT_TCP_RECVMBOX_SIZE 10 @@ -435,7 +445,7 @@ #define LWIP_UDP 0 #endif -#define LWIP_UDPLITE 0 +#define LWIP_UDPLITE 1 #define UDP_TTL 255 #define DEFAULT_UDP_RECVMBOX_SIZE 1 diff --git a/examples/device/net_lwip_webserver/src/main.c b/examples/device/net_lwip_webserver/src/main.c index ee92d6ad0..9d5a798e9 100644 --- a/examples/device/net_lwip_webserver/src/main.c +++ b/examples/device/net_lwip_webserver/src/main.c @@ -85,7 +85,7 @@ uint8_t tud_network_mac_address[6] = {0x02, 0x02, 0x84, 0x6A, 0x96, 0x00}; /* network parameters of this MCU */ static const ip4_addr_t ipaddr = INIT_IP4(192, 168, 7, 1); static const ip4_addr_t netmask = INIT_IP4(255, 255, 255, 0); -static const ip4_addr_t gateway = INIT_IP4(0, 0, 0, 0); +static const ip4_addr_t gateway = INIT_IP4(192, 168, 7, 1); /* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */ static dhcp_entry_t entries[] = { @@ -96,7 +96,7 @@ static dhcp_entry_t entries[] = { }; static const dhcp_config_t dhcp_config = { - .router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */ + .router = INIT_IP4(192, 168, 7, 1), /* router address (if any) */ .port = 67, /* listen port */ .dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */ "usb", /* dns suffix */ @@ -108,14 +108,16 @@ static const dhcp_config_t dhcp_config = { -static int tud_output_fn(void *t){ +static void tud_output_fn(void *t){ struct pbuf *p = t; - /* if the network driver can accept another packet, we make it happen */ + rt_kprintf("arrive %s:%d\n", __FILE__, __LINE__); + /* if the network driver can accept another packet, we make it happen */ if (tud_network_can_xmit(p->tot_len)) { tud_network_xmit(p, 0 /* unused for this example */); - return ERR_OK; + rt_kprintf("send %d bytes\n", p->tot_len); + // return ERR_OK; } - return ERR_USE; + // return ERR_USE; } @@ -129,7 +131,10 @@ static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) { if (!tud_ready()) return ERR_USE; /*rc this packet will be sent in usbd task */ - return usbd_defer_func_wait(tud_output_fn, p); + rt_kprintf("arrive %s:%d\n", __FILE__, __LINE__); + usbd_defer_func(tud_output_fn, p, false); + return ERR_OK; + // return usbd_defer_func_wait(tud_output_fn, p); /* transfer execution to TinyUSB in the hopes that it will finish transmitting the prior packet */ /*rc: if use rtos, this function still will be called? */ // tud_task(); @@ -216,7 +221,7 @@ bool tud_network_recv_cb(const uint8_t *src, uint16_t size) { struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL); if (p == NULL) { - TU_LOG(2,"ERROR: Failed to allocate pbuf of size %d\n", size); + rt_kprintf("ERROR: Failed to allocate pbuf of size %d\n", size); return false; } @@ -305,7 +310,7 @@ void usb_irq_enable(){ static void tusb_thread_entry(void *parameter) { - TU_LOG(2, "TinyUSB task started\n"); + rt_kprintf( "TinyUSB task started\n"); (void) parameter; while (1) { #if CFG_TUH_ENABLED @@ -353,9 +358,9 @@ int init_tinyusb(void) { while (dhserv_init(&dhcp_config) != ERR_OK){ rt_thread_mdelay(10); } - while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK){ - rt_thread_mdelay(10); - } + // while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK){ + // rt_thread_mdelay(10); + // } httpd_init(); #ifdef INCLUDE_IPERF @@ -371,8 +376,8 @@ int init_tinyusb(void) { #ifdef RT_USING_HEAP tid = rt_thread_create("tusb", tusb_thread_entry, RT_NULL, - 2048, - 1, 10); + 4096, + 7, 10); if (tid == RT_NULL) #else rt_err_t result; diff --git a/examples/device/net_lwip_webserver/src/tusb_config.h b/examples/device/net_lwip_webserver/src/tusb_config.h index 4d5dbf5ae..8d6497cfd 100644 --- a/examples/device/net_lwip_webserver/src/tusb_config.h +++ b/examples/device/net_lwip_webserver/src/tusb_config.h @@ -62,7 +62,7 @@ extern "C" { #endif #ifndef CFG_TUSB_DEBUG - #define CFG_TUSB_DEBUG 3 + #define CFG_TUSB_DEBUG 0 #endif #ifdef CFG_TUSB_DEBUG #define CFG_TUSB_DEBUG_PRINTF rt_kprintf diff --git a/lib/networking/dnserver.c b/lib/networking/dnserver.c index 25bbf4875..fd521b7c0 100644 --- a/lib/networking/dnserver.c +++ b/lib/networking/dnserver.c @@ -139,7 +139,8 @@ static void udp_recv_proc(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip4_addr_t host_addr; dns_answer_t *answer; - (void)arg; + rt_kprintf("DNS query\n"); + (void)arg; if (p->len <= sizeof(dns_header_t)) goto error; header = (dns_header_t *)p->payload; diff --git a/src/osal/osal_rtthread.h b/src/osal/osal_rtthread.h index 3f3f4d53e..0193e2de8 100644 --- a/src/osal/osal_rtthread.h +++ b/src/osal/osal_rtthread.h @@ -134,12 +134,13 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hd // role device/host is used by OS NONE for mutex (disable usb isr) only #define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ - static _type _name##_##buf[_depth]; \ - osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf }; + static uint32_t _name##_##buf[((sizeof(_type)+sizeof(void *))*_depth+3)/4]; \ + osal_queue_def_t _name = { .item_sz = sizeof(_type), \ + .pool_size=sizeof(_name##_##buf), .buf = _name##_##buf }; typedef struct { - uint16_t depth; uint16_t item_sz; + uint16_t pool_size; void *buf; struct rt_messagequeue sq; @@ -149,7 +150,7 @@ typedef rt_mq_t osal_queue_t; TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t *qdef) { rt_mq_init(&(qdef->sq), "tusb", qdef->buf, qdef->item_sz, - qdef->item_sz * qdef->depth, RT_IPC_FLAG_PRIO); + qdef->pool_size, RT_IPC_FLAG_FIFO); return &(qdef->sq); } @@ -162,7 +163,13 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, v #if RT_VERSION_MAJOR >= 5 return rt_mq_recv(qhdl, data, qhdl->msg_size, tick) > 0; #else - return rt_mq_recv(qhdl, data, qhdl->msg_size, tick) == RT_EOK; + rt_err_t ret = rt_mq_recv(qhdl, data, qhdl->msg_size, tick); + if (ret == RT_EOK){ + return true; + }else{ + rt_kprintf("osal_queue_receive failed ret=%d\n", ret); + return false; + } #endif /* RT_VERSION_MAJOR >= 5 */ }