1
0

cdc_ecm_template.c 10 KB

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