@@ -48,12 +48,17 @@ try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00
48
48
49
49
#include "dhserver.h"
50
50
#include "dnserver.h"
51
+ #include "httpd.h"
52
+ #include "lwip/ethip6.h"
51
53
#include "lwip/init.h"
52
54
#include "lwip/timeouts.h"
53
- #include "lwip/ethip6.h"
54
- #include "httpd.h"
55
55
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)) }
57
62
58
63
/* lwip context */
59
64
static struct netif netif_data ;
@@ -64,44 +69,40 @@ static struct pbuf *received_frame;
64
69
/* this is used by this code, ./class/net/net_driver.c, and usb_descriptors.c */
65
70
/* ideally speaking, this should be generated from the hardware's unique ID (if available) */
66
71
/* 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 };
68
73
69
74
/* 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 );
71
76
static const ip4_addr_t netmask = INIT_IP4 (255 , 255 , 255 , 0 );
72
77
static const ip4_addr_t gateway = INIT_IP4 (0 , 0 , 0 , 0 );
73
78
74
79
/* 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 },
81
85
};
82
86
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 */
91
94
};
92
- static err_t linkoutput_fn (struct netif * netif , struct pbuf * p )
93
- {
94
- (void )netif ;
95
95
96
- for (;;)
97
- {
96
+ static err_t linkoutput_fn (struct netif * netif , struct pbuf * p ) {
97
+ (void ) netif ;
98
+
99
+ for (;;) {
98
100
/* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */
99
101
if (!tud_ready ())
100
102
return ERR_USE ;
101
103
102
104
/* 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 )) {
105
106
tud_network_xmit (p , 0 /* unused for this example */ );
106
107
return ERR_OK ;
107
108
}
@@ -111,20 +112,17 @@ static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
111
112
}
112
113
}
113
114
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 ) {
116
116
return etharp_output (netif , p , addr );
117
117
}
118
118
119
119
#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 ) {
122
121
return ethip6_output (netif , p , addr );
123
122
}
124
123
#endif
125
124
126
- static err_t netif_init_cb (struct netif * netif )
127
- {
125
+ static err_t netif_init_cb (struct netif * netif ) {
128
126
LWIP_ASSERT ("netif != NULL" , (netif != NULL ));
129
127
netif -> mtu = CFG_TUD_NET_MTU ;
130
128
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)
139
137
return ERR_OK ;
140
138
}
141
139
142
- static void init_lwip (void )
143
- {
140
+ static void init_lwip (void ) {
144
141
struct netif * netif = & netif_data ;
145
142
146
143
lwip_init ();
@@ -158,28 +155,23 @@ static void init_lwip(void)
158
155
}
159
156
160
157
/* 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" )) {
165
160
* addr = ipaddr ;
166
161
return true;
167
162
}
168
163
return false;
169
164
}
170
165
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 ) {
173
167
/* this shouldn't happen, but if we get another packet before
174
168
parsing the previous, we must signal our inability to accept it */
175
169
if (received_frame ) return false;
176
170
177
- if (size )
178
- {
171
+ if (size ) {
179
172
struct pbuf * p = pbuf_alloc (PBUF_RAW , size , PBUF_POOL );
180
173
181
- if (p )
182
- {
174
+ if (p ) {
183
175
/* pbuf_alloc() has already initialized struct; all we need to do is copy the data */
184
176
memcpy (p -> payload , src , size );
185
177
@@ -191,20 +183,17 @@ bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
191
183
return true;
192
184
}
193
185
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 ;
197
188
198
- (void )arg ; /* unused for this example */
189
+ (void ) arg ; /* unused for this example */
199
190
200
191
return pbuf_copy_partial (p , dst , p -> tot_len , 0 );
201
192
}
202
193
203
- static void service_traffic (void )
204
- {
194
+ static void service_traffic (void ) {
205
195
/* handle any packet received by tud_network_recv_cb() */
206
- if (received_frame )
207
- {
196
+ if (received_frame ) {
208
197
ethernet_input (received_frame , & netif_data );
209
198
pbuf_free (received_frame );
210
199
received_frame = NULL ;
@@ -214,18 +203,15 @@ static void service_traffic(void)
214
203
sys_check_timeouts ();
215
204
}
216
205
217
- void tud_network_init_cb (void )
218
- {
206
+ void tud_network_init_cb (void ) {
219
207
/* 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 ) {
222
209
pbuf_free (received_frame );
223
210
received_frame = NULL ;
224
211
}
225
212
}
226
213
227
- int main (void )
228
- {
214
+ int main (void ) {
229
215
/* initialize TinyUSB */
230
216
board_init ();
231
217
@@ -243,8 +229,12 @@ int main(void)
243
229
while (dnserv_init (IP_ADDR_ANY , 53 , dns_query_proc ) != ERR_OK );
244
230
httpd_init ();
245
231
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 ) {
248
238
tud_task ();
249
239
service_traffic ();
250
240
}
@@ -253,17 +243,14 @@ int main(void)
253
243
}
254
244
255
245
/* 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 ) {
258
247
return 0 ;
259
248
}
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 ;
263
251
}
264
252
265
253
/* 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 ) {
268
255
return board_millis ();
269
256
}
0 commit comments