Skip to content

Commit e5b6f93

Browse files
authored
Merge pull request #2227 from rgrr/ncm_device_new2
Rewrite of NCM device driver
2 parents 104a5da + e5d92c4 commit e5b6f93

File tree

9 files changed

+971
-490
lines changed

9 files changed

+971
-490
lines changed

examples/device/net_lwip_webserver/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ target_sources(${PROJECT} PUBLIC
7474
${LWIP}/src/netif/slipif.c
7575
${LWIP}/src/apps/http/httpd.c
7676
${LWIP}/src/apps/http/fs.c
77+
${LWIP}/src/apps/lwiperf/lwiperf.c
7778
)
7879

7980
# due to warnings from other net source, we need to prevent error from some of the warnings options

examples/device/net_lwip_webserver/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ SRC_C += \
6363
lib/lwip/src/netif/slipif.c \
6464
lib/lwip/src/apps/http/httpd.c \
6565
lib/lwip/src/apps/http/fs.c \
66+
lib/lwip/src/apps/lwiperf/lwiperf.c \
6667
lib/networking/dhserver.c \
6768
lib/networking/dnserver.c \
6869
lib/networking/rndis_reports.c

examples/device/net_lwip_webserver/skip.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mcu:LPC11UXX
22
mcu:LPC13XX
3+
mcu:LPC15XX
34
mcu:MSP430x5xx
45
mcu:NUC121
56
mcu:SAMD11
67
mcu:STM32L0
8+
mcu:STM32F0
79
mcu:KINETIS_KL
810
family:broadcom_64bit
911
family:broadcom_32bit

examples/device/net_lwip_webserver/src/lwipopts.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
#define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67))
4949

5050
#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
51-
#define TCP_SND_BUF (2 * TCP_MSS)
52-
#define TCP_WND (TCP_MSS)
51+
#define TCP_SND_BUF (4 * TCP_MSS)
52+
#define TCP_WND (4 * TCP_MSS)
5353

5454
#define ETHARP_SUPPORT_STATIC_ENTRIES 1
5555

@@ -59,7 +59,7 @@
5959

6060
#define LWIP_SINGLE_NETIF 1
6161

62-
#define PBUF_POOL_SIZE 2
62+
#define PBUF_POOL_SIZE 4
6363

6464
#define HTTPD_USE_CUSTOM_FSDATA 0
6565

examples/device/net_lwip_webserver/src/main.c

+54-67
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,17 @@ try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00
4848

4949
#include "dhserver.h"
5050
#include "dnserver.h"
51+
#include "httpd.h"
52+
#include "lwip/ethip6.h"
5153
#include "lwip/init.h"
5254
#include "lwip/timeouts.h"
53-
#include "lwip/ethip6.h"
54-
#include "httpd.h"
5555

56-
#define INIT_IP4(a,b,c,d) { PP_HTONL(LWIP_MAKEU32(a,b,c,d)) }
56+
#ifdef INCLUDE_IPERF
57+
#include "lwip/apps/lwiperf.h"
58+
#endif
59+
60+
#define INIT_IP4(a, b, c, d) \
61+
{ PP_HTONL(LWIP_MAKEU32(a, b, c, d)) }
5762

5863
/* lwip context */
5964
static struct netif netif_data;
@@ -64,44 +69,40 @@ static struct pbuf *received_frame;
6469
/* this is used by this code, ./class/net/net_driver.c, and usb_descriptors.c */
6570
/* ideally speaking, this should be generated from the hardware's unique ID (if available) */
6671
/* it is suggested that the first byte is 0x02 to indicate a link-local address */
67-
uint8_t tud_network_mac_address[6] = {0x02,0x02,0x84,0x6A,0x96,0x00};
72+
uint8_t tud_network_mac_address[6] = {0x02, 0x02, 0x84, 0x6A, 0x96, 0x00};
6873

6974
/* network parameters of this MCU */
70-
static const ip4_addr_t ipaddr = INIT_IP4(192, 168, 7, 1);
75+
static const ip4_addr_t ipaddr = INIT_IP4(192, 168, 7, 1);
7176
static const ip4_addr_t netmask = INIT_IP4(255, 255, 255, 0);
7277
static const ip4_addr_t gateway = INIT_IP4(0, 0, 0, 0);
7378

7479
/* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */
75-
static dhcp_entry_t entries[] =
76-
{
77-
/* mac ip address lease time */
78-
{ {0}, INIT_IP4(192, 168, 7, 2), 24 * 60 * 60 },
79-
{ {0}, INIT_IP4(192, 168, 7, 3), 24 * 60 * 60 },
80-
{ {0}, INIT_IP4(192, 168, 7, 4), 24 * 60 * 60 },
80+
static dhcp_entry_t entries[] = {
81+
/* mac ip address lease time */
82+
{{0}, INIT_IP4(192, 168, 7, 2), 24 * 60 * 60},
83+
{{0}, INIT_IP4(192, 168, 7, 3), 24 * 60 * 60},
84+
{{0}, INIT_IP4(192, 168, 7, 4), 24 * 60 * 60},
8185
};
8286

83-
static const dhcp_config_t dhcp_config =
84-
{
85-
.router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */
86-
.port = 67, /* listen port */
87-
.dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */
88-
"usb", /* dns suffix */
89-
TU_ARRAY_SIZE(entries), /* num entry */
90-
entries /* entries */
87+
static const dhcp_config_t dhcp_config = {
88+
.router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */
89+
.port = 67, /* listen port */
90+
.dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */
91+
"usb", /* dns suffix */
92+
TU_ARRAY_SIZE(entries), /* num entry */
93+
entries /* entries */
9194
};
92-
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
93-
{
94-
(void)netif;
9595

96-
for (;;)
97-
{
96+
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) {
97+
(void) netif;
98+
99+
for (;;) {
98100
/* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */
99101
if (!tud_ready())
100102
return ERR_USE;
101103

102104
/* if the network driver can accept another packet, we make it happen */
103-
if (tud_network_can_xmit(p->tot_len))
104-
{
105+
if (tud_network_can_xmit(p->tot_len)) {
105106
tud_network_xmit(p, 0 /* unused for this example */);
106107
return ERR_OK;
107108
}
@@ -111,20 +112,17 @@ static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
111112
}
112113
}
113114

114-
static err_t ip4_output_fn(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr)
115-
{
115+
static err_t ip4_output_fn(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr) {
116116
return etharp_output(netif, p, addr);
117117
}
118118

119119
#if LWIP_IPV6
120-
static err_t ip6_output_fn(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr)
121-
{
120+
static err_t ip6_output_fn(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr) {
122121
return ethip6_output(netif, p, addr);
123122
}
124123
#endif
125124

126-
static err_t netif_init_cb(struct netif *netif)
127-
{
125+
static err_t netif_init_cb(struct netif *netif) {
128126
LWIP_ASSERT("netif != NULL", (netif != NULL));
129127
netif->mtu = CFG_TUD_NET_MTU;
130128
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
@@ -139,8 +137,7 @@ static err_t netif_init_cb(struct netif *netif)
139137
return ERR_OK;
140138
}
141139

142-
static void init_lwip(void)
143-
{
140+
static void init_lwip(void) {
144141
struct netif *netif = &netif_data;
145142

146143
lwip_init();
@@ -158,28 +155,23 @@ static void init_lwip(void)
158155
}
159156

160157
/* handle any DNS requests from dns-server */
161-
bool dns_query_proc(const char *name, ip4_addr_t *addr)
162-
{
163-
if (0 == strcmp(name, "tiny.usb"))
164-
{
158+
bool dns_query_proc(const char *name, ip4_addr_t *addr) {
159+
if (0 == strcmp(name, "tiny.usb")) {
165160
*addr = ipaddr;
166161
return true;
167162
}
168163
return false;
169164
}
170165

171-
bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
172-
{
166+
bool tud_network_recv_cb(const uint8_t *src, uint16_t size) {
173167
/* this shouldn't happen, but if we get another packet before
174168
parsing the previous, we must signal our inability to accept it */
175169
if (received_frame) return false;
176170

177-
if (size)
178-
{
171+
if (size) {
179172
struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);
180173

181-
if (p)
182-
{
174+
if (p) {
183175
/* pbuf_alloc() has already initialized struct; all we need to do is copy the data */
184176
memcpy(p->payload, src, size);
185177

@@ -191,20 +183,17 @@ bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
191183
return true;
192184
}
193185

194-
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg)
195-
{
196-
struct pbuf *p = (struct pbuf *)ref;
186+
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
187+
struct pbuf *p = (struct pbuf *) ref;
197188

198-
(void)arg; /* unused for this example */
189+
(void) arg; /* unused for this example */
199190

200191
return pbuf_copy_partial(p, dst, p->tot_len, 0);
201192
}
202193

203-
static void service_traffic(void)
204-
{
194+
static void service_traffic(void) {
205195
/* handle any packet received by tud_network_recv_cb() */
206-
if (received_frame)
207-
{
196+
if (received_frame) {
208197
ethernet_input(received_frame, &netif_data);
209198
pbuf_free(received_frame);
210199
received_frame = NULL;
@@ -214,18 +203,15 @@ static void service_traffic(void)
214203
sys_check_timeouts();
215204
}
216205

217-
void tud_network_init_cb(void)
218-
{
206+
void tud_network_init_cb(void) {
219207
/* if the network is re-initializing and we have a leftover packet, we must do a cleanup */
220-
if (received_frame)
221-
{
208+
if (received_frame) {
222209
pbuf_free(received_frame);
223210
received_frame = NULL;
224211
}
225212
}
226213

227-
int main(void)
228-
{
214+
int main(void) {
229215
/* initialize TinyUSB */
230216
board_init();
231217

@@ -243,8 +229,12 @@ int main(void)
243229
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK);
244230
httpd_init();
245231

246-
while (1)
247-
{
232+
#ifdef INCLUDE_IPERF
233+
// test with: iperf -c 192.168.7.1 -e -i 1 -M 5000 -l 8192 -r
234+
lwiperf_start_tcp_server_default(NULL, NULL);
235+
#endif
236+
237+
while (1) {
248238
tud_task();
249239
service_traffic();
250240
}
@@ -253,17 +243,14 @@ int main(void)
253243
}
254244

255245
/* lwip has provision for using a mutex, when applicable */
256-
sys_prot_t sys_arch_protect(void)
257-
{
246+
sys_prot_t sys_arch_protect(void) {
258247
return 0;
259248
}
260-
void sys_arch_unprotect(sys_prot_t pval)
261-
{
262-
(void)pval;
249+
void sys_arch_unprotect(sys_prot_t pval) {
250+
(void) pval;
263251
}
264252

265253
/* lwip needs a millisecond time source, and the TinyUSB board support code has one available */
266-
uint32_t sys_now(void)
267-
{
254+
uint32_t sys_now(void) {
268255
return board_millis();
269256
}

0 commit comments

Comments
 (0)