Skip to content

Commit c659bdc

Browse files
committed
update(demo): update rndis & ecm device for rtthread
Signed-off-by: sakumisu <[email protected]>
1 parent 40f164e commit c659bdc

File tree

2 files changed

+177
-90
lines changed

2 files changed

+177
-90
lines changed

demo/cdc_ecm_template.c

Lines changed: 112 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
*/
66
#include "usbd_core.h"
77
#include "usbd_cdc_ecm.h"
8-
#include "dhserver.h"
9-
#include "dnserver.h"
10-
#include "netif/etharp.h"
11-
#include "lwip/init.h"
12-
#include "lwip/netif.h"
13-
#include "lwip/pbuf.h"
148

159
#ifndef CONFIG_USBDEV_CDC_ECM_USING_LWIP
1610
#error "Please enable CONFIG_USBDEV_CDC_ECM_USING_LWIP for this demo"
@@ -40,6 +34,9 @@
4034
/* str idx = 4 is for mac address: aa:bb:cc:dd:ee:ff*/
4135
#define CDC_ECM_MAC_STRING_INDEX 4
4236

37+
/* Ethernet Maximum Segment size, typically 1514 bytes */
38+
#define CONFIG_CDC_ECM_ETH_MAX_SEGSZE 1514U
39+
4340
#ifdef CONFIG_USBDEV_ADVANCE_DESC
4441
static const uint8_t device_descriptor[] = {
4542
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
@@ -206,11 +203,101 @@ static const uint8_t cdc_ecm_descriptor[] = {
206203

207204
const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
208205

206+
volatile bool cdc_ecm_tx_done = false;
207+
208+
void usbd_cdc_ecm_data_send_done(uint32_t len)
209+
{
210+
cdc_ecm_tx_done = true; // suggest you to use semaphore in os
211+
}
212+
213+
#ifdef RT_USING_LWIP
214+
215+
#ifndef RT_LWIP_DHCP
216+
#error cdc_ecm must enable RT_LWIP_DHCP
217+
#endif
218+
219+
#ifndef LWIP_USING_DHCPD
220+
#error cdc_ecm must enable LWIP_USING_DHCPD
221+
#endif
222+
223+
#include <rtthread.h>
224+
#include <rtdevice.h>
225+
#include <netif/ethernetif.h>
226+
#include <dhcp_server.h>
227+
228+
struct eth_device cdc_ecm_dev;
229+
230+
static rt_err_t rt_usbd_cdc_ecm_control(rt_device_t dev, int cmd, void *args)
231+
{
232+
switch (cmd) {
233+
case NIOCTL_GADDR:
234+
235+
/* get mac address */
236+
if (args) {
237+
uint8_t *mac_dev = (uint8_t *)args;
238+
rt_memcpy(mac_dev, mac, 6);
239+
mac_dev[5] = ~mac_dev[5]; /* device mac can't same as host. */
240+
} else
241+
return -RT_ERROR;
242+
243+
break;
244+
245+
default:
246+
break;
247+
}
248+
249+
return RT_EOK;
250+
}
251+
252+
struct pbuf *rt_usbd_cdc_ecm_eth_rx(rt_device_t dev)
253+
{
254+
return usbd_cdc_ecm_eth_rx();
255+
}
256+
257+
rt_err_t rt_usbd_cdc_ecm_eth_tx(rt_device_t dev, struct pbuf *p)
258+
{
259+
int ret;
260+
261+
cdc_ecm_tx_done = false;
262+
ret = usbd_cdc_ecm_eth_tx(p);
263+
if (ret == 0) {
264+
while (!cdc_ecm_tx_done) {
265+
}
266+
return RT_EOK;
267+
} else
268+
return -RT_ERROR;
269+
}
270+
271+
void cdc_ecm_lwip_init(void)
272+
{
273+
cdc_ecm_dev.parent.control = rt_usbd_cdc_ecm_control;
274+
cdc_ecm_dev.eth_rx = rt_usbd_cdc_ecm_eth_rx;
275+
cdc_ecm_dev.eth_tx = rt_usbd_cdc_ecm_eth_tx;
276+
277+
eth_device_init(&cdc_ecm_dev, "u0");
278+
279+
eth_device_linkchange(&cdc_ecm_dev, RT_FALSE);
280+
}
281+
282+
void usbd_cdc_ecm_data_recv_done(uint32_t len)
283+
{
284+
eth_device_ready(&cdc_ecm_dev);
285+
}
286+
287+
#else
288+
#include "netif/etharp.h"
289+
#include "lwip/init.h"
290+
#include "lwip/netif.h"
291+
#include "lwip/pbuf.h"
292+
293+
#include "dhserver.h"
294+
#include "dnserver.h"
295+
209296
/*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
210-
#define IP_ADDR0 (uint8_t)192
211-
#define IP_ADDR1 (uint8_t)168
212-
#define IP_ADDR2 (uint8_t)7
213-
#define IP_ADDR3 (uint8_t)1
297+
#define IP_ADDR0 (uint8_t)192
298+
#define IP_ADDR1 (uint8_t)168
299+
#define IP_ADDR2 (uint8_t)7
300+
#define IP_ADDR3 (uint8_t)1
214301

215302
/*NETMASK*/
216303
#define NETMASK_ADDR0 (uint8_t)255
@@ -219,10 +306,10 @@ const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
219306
#define NETMASK_ADDR3 (uint8_t)0
220307

221308
/*Gateway Address*/
222-
#define GW_ADDR0 (uint8_t)0
223-
#define GW_ADDR1 (uint8_t)0
224-
#define GW_ADDR2 (uint8_t)0
225-
#define GW_ADDR3 (uint8_t)0
309+
#define GW_ADDR0 (uint8_t)0
310+
#define GW_ADDR1 (uint8_t)0
311+
#define GW_ADDR2 (uint8_t)0
312+
#define GW_ADDR3 (uint8_t)0
226313

227314
const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
228315
const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
@@ -255,22 +342,11 @@ static bool dns_query_proc(const char *name, ip_addr_t *addr)
255342
return false;
256343
}
257344

258-
volatile bool cdc_ecm_tx_done = false;
259-
260-
void usbd_cdc_ecm_data_send_done(uint32_t len)
261-
{
262-
cdc_ecm_tx_done = true; // suggest you to use semaphore in os
263-
}
264-
265-
void usbd_cdc_ecm_data_recv_done(uint32_t len)
266-
{
267-
}
268-
269345
static struct netif cdc_ecm_netif; //network interface
270346

271347
/* Network interface name */
272-
#define IFNAME0 'E'
273-
#define IFNAME1 'X'
348+
#define IFNAME0 'E'
349+
#define IFNAME1 'X'
274350

275351
err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
276352
{
@@ -339,10 +415,15 @@ void cdc_ecm_lwip_init(void)
339415
}
340416
}
341417

418+
void usbd_cdc_ecm_data_recv_done(uint32_t len)
419+
{
420+
}
421+
342422
void cdc_ecm_input_poll(void)
343423
{
344424
cdc_ecm_if_input(&cdc_ecm_netif);
345425
}
426+
#endif
346427

347428
static void usbd_event_handler(uint8_t busid, uint8_t event)
348429
{
@@ -358,6 +439,10 @@ static void usbd_event_handler(uint8_t busid, uint8_t event)
358439
case USBD_EVENT_SUSPEND:
359440
break;
360441
case USBD_EVENT_CONFIGURED:
442+
#ifdef RT_USING_LWIP
443+
eth_device_linkchange(&cdc_ecm_dev, RT_TRUE);
444+
dhcpd_start("u0");
445+
#endif
361446
break;
362447
case USBD_EVENT_SET_REMOTE_WAKEUP:
363448
break;

demo/cdc_rndis_template.c

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
#include "usbd_core.h"
77
#include "usbd_rndis.h"
8-
#include "dhserver.h"
9-
#include "dnserver.h"
108

119
#ifndef CONFIG_USBDEV_RNDIS_USING_LWIP
1210
#error "Please enable CONFIG_USBDEV_RNDIS_USING_LWIP for this demo"
@@ -178,55 +176,6 @@ static const uint8_t cdc_rndis_descriptor[] = {
178176

179177
const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
180178

181-
/*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
182-
#define IP_ADDR0 (uint8_t)192
183-
#define IP_ADDR1 (uint8_t)168
184-
#define IP_ADDR2 (uint8_t)7
185-
#define IP_ADDR3 (uint8_t)1
186-
187-
/*NETMASK*/
188-
#define NETMASK_ADDR0 (uint8_t)255
189-
#define NETMASK_ADDR1 (uint8_t)255
190-
#define NETMASK_ADDR2 (uint8_t)255
191-
#define NETMASK_ADDR3 (uint8_t)0
192-
193-
/*Gateway Address*/
194-
#define GW_ADDR0 (uint8_t)0
195-
#define GW_ADDR1 (uint8_t)0
196-
#define GW_ADDR2 (uint8_t)0
197-
#define GW_ADDR3 (uint8_t)0
198-
199-
const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
200-
const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
201-
const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
202-
203-
#define NUM_DHCP_ENTRY 3
204-
205-
static dhcp_entry_t entries[NUM_DHCP_ENTRY] = {
206-
/* mac ip address subnet mask lease time */
207-
{ { 0 }, { 192, 168, 7, 2 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
208-
{ { 0 }, { 192, 168, 7, 3 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
209-
{ { 0 }, { 192, 168, 7, 4 }, { 255, 255, 255, 0 }, 24 * 60 * 60 }
210-
};
211-
212-
static dhcp_config_t dhcp_config = {
213-
{ 192, 168, 7, 1 }, /* server address */
214-
67, /* port */
215-
{ 192, 168, 7, 1 }, /* dns server */
216-
"cherry", /* dns suffix */
217-
NUM_DHCP_ENTRY, /* num entry */
218-
entries /* entries */
219-
};
220-
221-
static bool dns_query_proc(const char *name, ip_addr_t *addr)
222-
{
223-
if (strcmp(name, "rndis.cherry") == 0 || strcmp(name, "www.rndis.cherry") == 0) {
224-
addr->addr = ipaddr.addr;
225-
return true;
226-
}
227-
return false;
228-
}
229-
230179
volatile bool rndis_tx_done = false;
231180

232181
void usbd_rndis_data_send_done(uint32_t len)
@@ -235,9 +184,19 @@ void usbd_rndis_data_send_done(uint32_t len)
235184
}
236185

237186
#ifdef RT_USING_LWIP
187+
188+
#ifndef RT_LWIP_DHCP
189+
#error rndis must enable RT_LWIP_DHCP
190+
#endif
191+
192+
#ifndef LWIP_USING_DHCPD
193+
#error rndis must enable LWIP_USING_DHCPD
194+
#endif
195+
238196
#include <rtthread.h>
239197
#include <rtdevice.h>
240198
#include <netif/ethernetif.h>
199+
#include <dhcp_server.h>
241200

242201
struct eth_device rndis_dev;
243202

@@ -291,15 +250,6 @@ void rndis_lwip_init(void)
291250
eth_device_init(&rndis_dev, "u0");
292251

293252
eth_device_linkchange(&rndis_dev, RT_FALSE);
294-
295-
while (!netif_is_up(rndis_dev.netif)) {
296-
}
297-
298-
while (dhserv_init(&dhcp_config)) {
299-
}
300-
301-
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc)) {
302-
}
303253
}
304254

305255
void usbd_rndis_data_recv_done(uint32_t len)
@@ -313,11 +263,63 @@ void usbd_rndis_data_recv_done(uint32_t len)
313263
#include "lwip/netif.h"
314264
#include "lwip/pbuf.h"
315265

266+
#include "dhserver.h"
267+
#include "dnserver.h"
268+
269+
/*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
270+
#define IP_ADDR0 (uint8_t)192
271+
#define IP_ADDR1 (uint8_t)168
272+
#define IP_ADDR2 (uint8_t)7
273+
#define IP_ADDR3 (uint8_t)1
274+
275+
/*NETMASK*/
276+
#define NETMASK_ADDR0 (uint8_t)255
277+
#define NETMASK_ADDR1 (uint8_t)255
278+
#define NETMASK_ADDR2 (uint8_t)255
279+
#define NETMASK_ADDR3 (uint8_t)0
280+
281+
/*Gateway Address*/
282+
#define GW_ADDR0 (uint8_t)0
283+
#define GW_ADDR1 (uint8_t)0
284+
#define GW_ADDR2 (uint8_t)0
285+
#define GW_ADDR3 (uint8_t)0
286+
287+
const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
288+
const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
289+
const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
290+
291+
#define NUM_DHCP_ENTRY 3
292+
293+
static dhcp_entry_t entries[NUM_DHCP_ENTRY] = {
294+
/* mac ip address subnet mask lease time */
295+
{ { 0 }, { 192, 168, 7, 2 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
296+
{ { 0 }, { 192, 168, 7, 3 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
297+
{ { 0 }, { 192, 168, 7, 4 }, { 255, 255, 255, 0 }, 24 * 60 * 60 }
298+
};
299+
300+
static dhcp_config_t dhcp_config = {
301+
{ 192, 168, 7, 1 }, /* server address */
302+
67, /* port */
303+
{ 192, 168, 7, 1 }, /* dns server */
304+
"cherry", /* dns suffix */
305+
NUM_DHCP_ENTRY, /* num entry */
306+
entries /* entries */
307+
};
308+
309+
static bool dns_query_proc(const char *name, ip_addr_t *addr)
310+
{
311+
if (strcmp(name, "rndis.cherry") == 0 || strcmp(name, "www.rndis.cherry") == 0) {
312+
addr->addr = ipaddr.addr;
313+
return true;
314+
}
315+
return false;
316+
}
317+
316318
static struct netif rndis_netif; //network interface
317319

318320
/* Network interface name */
319-
#define IFNAME0 'E'
320-
#define IFNAME1 'X'
321+
#define IFNAME0 'E'
322+
#define IFNAME1 'X'
321323

322324
err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
323325
{
@@ -388,7 +390,6 @@ void rndis_lwip_init(void)
388390

389391
void usbd_rndis_data_recv_done(uint32_t len)
390392
{
391-
392393
}
393394

394395
void rndis_input_poll(void)
@@ -413,6 +414,7 @@ static void usbd_event_handler(uint8_t busid, uint8_t event)
413414
case USBD_EVENT_CONFIGURED:
414415
#ifdef RT_USING_LWIP
415416
eth_device_linkchange(&rndis_dev, RT_TRUE);
417+
dhcpd_start("u0");
416418
#endif
417419
break;
418420
case USBD_EVENT_SET_REMOTE_WAKEUP:

0 commit comments

Comments
 (0)