cdc_ecm_template.c 8.6 KB

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