• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "usbd_core.h"
2 #include "usbd_cdc_ecm.h"
3 
4 /*!< endpoint address */
5 #define CDC_IN_EP          0x81
6 #define CDC_OUT_EP         0x02
7 #define CDC_INT_EP         0x83
8 
9 #define USBD_VID           0xFFFF
10 #define USBD_PID           0xFFFF
11 #define USBD_MAX_POWER     100
12 #define USBD_LANGID_STRING 1033
13 
14 /*!< config descriptor size */
15 #define USB_CONFIG_SIZE    (9 + CDC_ECM_DESCRIPTOR_LEN)
16 
17 #ifdef CONFIG_USB_HS
18 #define CDC_MAX_MPS 512
19 #else
20 #define CDC_MAX_MPS 64
21 #endif
22 
23 #define CDC_ECM_ETH_STATISTICS_BITMAP 0x00000000
24 
25 /* str idx = 4 is for mac address: aa:bb:cc:dd:ee:ff*/
26 #define CDC_ECM_MAC_STRING_INDEX      4
27 
28 /*!< global descriptor */
29 static const uint8_t cdc_ecm_descriptor[] = {
30     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
31     USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
32     CDC_ECM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, CDC_ECM_ETH_STATISTICS_BITMAP, CONFIG_CDC_ECM_ETH_MAX_SEGSZE, 0, 0, CDC_ECM_MAC_STRING_INDEX),
33     ///////////////////////////////////////
34     /// string0 descriptor
35     ///////////////////////////////////////
36     USB_LANGID_INIT(USBD_LANGID_STRING),
37     ///////////////////////////////////////
38     /// string1 descriptor
39     ///////////////////////////////////////
40     0x14,                       /* bLength */
41     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
42     'C', 0x00,                  /* wcChar0 */
43     'h', 0x00,                  /* wcChar1 */
44     'e', 0x00,                  /* wcChar2 */
45     'r', 0x00,                  /* wcChar3 */
46     'r', 0x00,                  /* wcChar4 */
47     'y', 0x00,                  /* wcChar5 */
48     'U', 0x00,                  /* wcChar6 */
49     'S', 0x00,                  /* wcChar7 */
50     'B', 0x00,                  /* wcChar8 */
51     ///////////////////////////////////////
52     /// string2 descriptor
53     ///////////////////////////////////////
54     0x2E,                       /* bLength */
55     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
56     'C', 0x00,                  /* wcChar0 */
57     'h', 0x00,                  /* wcChar1 */
58     'e', 0x00,                  /* wcChar2 */
59     'r', 0x00,                  /* wcChar3 */
60     'r', 0x00,                  /* wcChar4 */
61     'y', 0x00,                  /* wcChar5 */
62     'U', 0x00,                  /* wcChar6 */
63     'S', 0x00,                  /* wcChar7 */
64     'B', 0x00,                  /* wcChar8 */
65     ' ', 0x00,                  /* wcChar9 */
66     'C', 0x00,                  /* wcChar10 */
67     'D', 0x00,                  /* wcChar11 */
68     'C', 0x00,                  /* wcChar12 */
69     ' ', 0x00,                  /* wcChar13 */
70     'E', 0x00,                  /* wcChar14 */
71     'C', 0x00,                  /* wcChar15 */
72     'M', 0x00,                  /* wcChar16 */
73     ' ', 0x00,                  /* wcChar17 */
74     'D', 0x00,                  /* wcChar18 */
75     'E', 0x00,                  /* wcChar19 */
76     'M', 0x00,                  /* wcChar20 */
77     'O', 0x00,                  /* wcChar21 */
78     ///////////////////////////////////////
79     /// string3 descriptor
80     ///////////////////////////////////////
81     0x16,                       /* bLength */
82     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
83     '2', 0x00,                  /* wcChar0 */
84     '0', 0x00,                  /* wcChar1 */
85     '2', 0x00,                  /* wcChar2 */
86     '2', 0x00,                  /* wcChar3 */
87     '1', 0x00,                  /* wcChar4 */
88     '2', 0x00,                  /* wcChar5 */
89     '3', 0x00,                  /* wcChar6 */
90     '4', 0x00,                  /* wcChar7 */
91     '5', 0x00,                  /* wcChar8 */
92     '6', 0x00,                  /* wcChar9 */
93     ///////////////////////////////////////
94     /// string4 descriptor
95     ///////////////////////////////////////
96     0x1A,                       /* bLength */
97     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
98     'a', 0x00,                  /* wcChar0 */
99     'a', 0x00,                  /* wcChar1 */
100     'b', 0x00,                  /* wcChar2 */
101     'b', 0x00,                  /* wcChar3 */
102     'c', 0x00,                  /* wcChar4 */
103     'c', 0x00,                  /* wcChar5 */
104     'd', 0x00,                  /* wcChar6 */
105     'd', 0x00,                  /* wcChar7 */
106     'e', 0x00,                  /* wcChar8 */
107     'e', 0x00,                  /* wcChar9 */
108     'f', 0x00,                  /* wcChar10 */
109     'f', 0x00,                  /* wcChar11 */
110 #ifdef CONFIG_USB_HS
111     ///////////////////////////////////////
112     /// device qualifier descriptor
113     ///////////////////////////////////////
114     0x0a,
115     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
116     0x00,
117     0x02,
118     0x02,
119     0x02,
120     0x01,
121     0x40,
122     0x01,
123     0x00,
124 #endif
125     0x00
126 };
127 
128 const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
129 
130 /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
131 #define IP_ADDR0 (uint8_t)192
132 #define IP_ADDR1 (uint8_t)168
133 #define IP_ADDR2 (uint8_t)123
134 #define IP_ADDR3 (uint8_t)100
135 
136 /*NETMASK*/
137 #define NETMASK_ADDR0 (uint8_t)255
138 #define NETMASK_ADDR1 (uint8_t)255
139 #define NETMASK_ADDR2 (uint8_t)255
140 #define NETMASK_ADDR3 (uint8_t)0
141 
142 /*Gateway Address*/
143 #define GW_ADDR0 (uint8_t)192
144 #define GW_ADDR1 (uint8_t)168
145 #define GW_ADDR2 (uint8_t)123
146 #define GW_ADDR3 (uint8_t)1
147 
148 #include "netif/etharp.h"
149 #include "lwip/init.h"
150 #include "lwip/netif.h"
151 #include "lwip/pbuf.h"
152 
153 const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
154 const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
155 const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
156 
157 static struct netif cdc_ecm_netif; //network interface
158 
159 /* Network interface name */
160 #define IFNAME0 'E'
161 #define IFNAME1 'X'
162 
linkoutput_fn(struct netif * netif,struct pbuf * p)163 static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
164 {
165     static int ret;
166 
167     ret = usbd_cdc_ecm_eth_tx(p);
168     if (ret == 0)
169         return ERR_OK;
170     else
171         return ERR_BUF;
172 }
173 
cdc_ecm_if_init(struct netif * netif)174 err_t cdc_ecm_if_init(struct netif *netif)
175 {
176     LWIP_ASSERT("netif != NULL", (netif != NULL));
177 
178     netif->mtu = 1500;
179     netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
180     netif->state = NULL;
181     netif->name[0] = IFNAME0;
182     netif->name[1] = IFNAME1;
183     netif->output = etharp_output;
184     netif->linkoutput = linkoutput_fn;
185     return ERR_OK;
186 }
187 
cdc_ecm_if_input(struct netif * netif)188 err_t cdc_ecm_if_input(struct netif *netif)
189 {
190     static err_t err;
191     static struct pbuf *p;
192 
193     p = usbd_cdc_ecm_eth_rx();
194     if (p != NULL) {
195         err = netif->input(p, netif);
196         if (err != ERR_OK) {
197             pbuf_free(p);
198         }
199     } else {
200         return ERR_BUF;
201     }
202     return err;
203 }
204 
cdc_ecm_lwip_init(void)205 void cdc_ecm_lwip_init(void)
206 {
207     struct netif *netif = &cdc_ecm_netif;
208 
209     lwip_init();
210 
211     netif->hwaddr_len = 6;
212     memcpy(netif->hwaddr, mac, 6);
213 
214     netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, cdc_ecm_if_init, netif_input);
215     netif_set_default(netif);
216     while (!netif_is_up(netif)) {
217     }
218 
219     // while (dhserv_init(&dhcp_config)) {}
220 
221     // while (dnserv_init(&ipaddr, PORT_DNS, dns_query_proc)) {}
222 }
223 
usbd_cdc_ecm_data_recv_done(void)224 void usbd_cdc_ecm_data_recv_done(void)
225 {
226 }
227 
cdc_ecm_input_poll(void)228 void cdc_ecm_input_poll(void)
229 {
230     cdc_ecm_if_input(&cdc_ecm_netif);
231 }
232 
usbd_event_handler(uint8_t event)233 void usbd_event_handler(uint8_t event)
234 {
235     switch (event) {
236         case USBD_EVENT_RESET:
237             break;
238         case USBD_EVENT_CONNECTED:
239             break;
240         case USBD_EVENT_DISCONNECTED:
241             break;
242         case USBD_EVENT_RESUME:
243             break;
244         case USBD_EVENT_SUSPEND:
245             break;
246         case USBD_EVENT_CONFIGURED:
247             break;
248         case USBD_EVENT_SET_REMOTE_WAKEUP:
249             break;
250         case USBD_EVENT_CLR_REMOTE_WAKEUP:
251             break;
252 
253         default:
254             break;
255     }
256 }
257 
258 struct usbd_interface intf0;
259 struct usbd_interface intf1;
260 
261 /* ecm only supports in linux, and you should input the following command
262  *
263  * sudo ifconfig enxaabbccddeeff up
264  * sudo dhcpclient enxaabbccddeeff
265 */
cdc_ecm_init(void)266 void cdc_ecm_init(void)
267 {
268     cdc_ecm_lwip_init();
269 
270     usbd_desc_register(cdc_ecm_descriptor);
271     usbd_add_interface(usbd_cdc_ecm_init_intf(&intf0, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP));
272     usbd_add_interface(usbd_cdc_ecm_init_intf(&intf1, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP));
273     usbd_initialize();
274 }