cdc_acm_multi_template.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_cdc_acm.h"
  8. #if CONFIG_USBDEV_EP_NUM < 8
  9. #error endpoint number is too small for this demo, please try other chips
  10. #endif
  11. /*!< endpoint address */
  12. #define CDC_IN_EP 0x81
  13. #define CDC_OUT_EP 0x01
  14. #define CDC_INT_EP 0x85
  15. #define CDC_IN_EP2 0x82
  16. #define CDC_OUT_EP2 0x02
  17. #define CDC_INT_EP2 0x86
  18. #define CDC_IN_EP3 0x83
  19. #define CDC_OUT_EP3 0x03
  20. #define CDC_INT_EP3 0x87
  21. #define CDC_IN_EP4 0x84
  22. #define CDC_OUT_EP4 0x04
  23. #define CDC_INT_EP4 0x88
  24. #define USBD_VID 0xFFFF
  25. #define USBD_PID 0xFFFF
  26. #define USBD_MAX_POWER 100
  27. #define USBD_LANGID_STRING 1033
  28. /*!< config descriptor size */
  29. #define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN * 4)
  30. #ifdef CONFIG_USB_HS
  31. #define CDC_MAX_MPS 512
  32. #else
  33. #define CDC_MAX_MPS 64
  34. #endif
  35. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  36. static const uint8_t device_descriptor[] = {
  37. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
  38. };
  39. static const uint8_t config_descriptor[] = {
  40. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x08, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  41. CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
  42. CDC_ACM_DESCRIPTOR_INIT(0x02, CDC_INT_EP2, CDC_OUT_EP2, CDC_IN_EP2, CDC_MAX_MPS, 0x02),
  43. CDC_ACM_DESCRIPTOR_INIT(0x04, CDC_INT_EP3, CDC_OUT_EP3, CDC_IN_EP3, CDC_MAX_MPS, 0x02),
  44. CDC_ACM_DESCRIPTOR_INIT(0x06, CDC_INT_EP4, CDC_OUT_EP4, CDC_IN_EP4, CDC_MAX_MPS, 0x02)
  45. };
  46. static const uint8_t device_quality_descriptor[] = {
  47. ///////////////////////////////////////
  48. /// device qualifier descriptor
  49. ///////////////////////////////////////
  50. 0x0a,
  51. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  52. 0x00,
  53. 0x02,
  54. 0x00,
  55. 0x00,
  56. 0x00,
  57. 0x40,
  58. 0x00,
  59. 0x00,
  60. };
  61. static const char *string_descriptors[] = {
  62. (const char[]){ 0x09, 0x04 }, /* Langid */
  63. "CherryUSB", /* Manufacturer */
  64. "CherryUSB CDC MULTI DEMO", /* Product */
  65. "2022123456", /* Serial Number */
  66. };
  67. static const uint8_t *device_descriptor_callback(uint8_t speed)
  68. {
  69. return device_descriptor;
  70. }
  71. static const uint8_t *config_descriptor_callback(uint8_t speed)
  72. {
  73. return config_descriptor;
  74. }
  75. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  76. {
  77. return device_quality_descriptor;
  78. }
  79. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  80. {
  81. if (index > 3) {
  82. return NULL;
  83. }
  84. return string_descriptors[index];
  85. }
  86. const struct usb_descriptor cdc_multi_descriptor = {
  87. .device_descriptor_callback = device_descriptor_callback,
  88. .config_descriptor_callback = config_descriptor_callback,
  89. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  90. .string_descriptor_callback = string_descriptor_callback
  91. };
  92. #else
  93. /*!< global descriptor */
  94. static const uint8_t cdc_multi_descriptor[] = {
  95. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
  96. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x08, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  97. CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
  98. CDC_ACM_DESCRIPTOR_INIT(0x02, CDC_INT_EP2, CDC_OUT_EP2, CDC_IN_EP2, CDC_MAX_MPS, 0x02),
  99. CDC_ACM_DESCRIPTOR_INIT(0x04, CDC_INT_EP3, CDC_OUT_EP3, CDC_IN_EP3, CDC_MAX_MPS, 0x02),
  100. CDC_ACM_DESCRIPTOR_INIT(0x06, CDC_INT_EP4, CDC_OUT_EP4, CDC_IN_EP4, CDC_MAX_MPS, 0x02),
  101. ///////////////////////////////////////
  102. /// string0 descriptor
  103. ///////////////////////////////////////
  104. USB_LANGID_INIT(USBD_LANGID_STRING),
  105. ///////////////////////////////////////
  106. /// string1 descriptor
  107. ///////////////////////////////////////
  108. 0x14, /* bLength */
  109. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  110. 'C', 0x00, /* wcChar0 */
  111. 'h', 0x00, /* wcChar1 */
  112. 'e', 0x00, /* wcChar2 */
  113. 'r', 0x00, /* wcChar3 */
  114. 'r', 0x00, /* wcChar4 */
  115. 'y', 0x00, /* wcChar5 */
  116. 'U', 0x00, /* wcChar6 */
  117. 'S', 0x00, /* wcChar7 */
  118. 'B', 0x00, /* wcChar8 */
  119. ///////////////////////////////////////
  120. /// string2 descriptor
  121. ///////////////////////////////////////
  122. 0x26, /* bLength */
  123. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  124. 'C', 0x00, /* wcChar0 */
  125. 'h', 0x00, /* wcChar1 */
  126. 'e', 0x00, /* wcChar2 */
  127. 'r', 0x00, /* wcChar3 */
  128. 'r', 0x00, /* wcChar4 */
  129. 'y', 0x00, /* wcChar5 */
  130. 'U', 0x00, /* wcChar6 */
  131. 'S', 0x00, /* wcChar7 */
  132. 'B', 0x00, /* wcChar8 */
  133. ' ', 0x00, /* wcChar9 */
  134. 'C', 0x00, /* wcChar10 */
  135. 'D', 0x00, /* wcChar11 */
  136. 'C', 0x00, /* wcChar12 */
  137. ' ', 0x00, /* wcChar13 */
  138. 'D', 0x00, /* wcChar14 */
  139. 'E', 0x00, /* wcChar15 */
  140. 'M', 0x00, /* wcChar16 */
  141. 'O', 0x00, /* wcChar17 */
  142. ///////////////////////////////////////
  143. /// string3 descriptor
  144. ///////////////////////////////////////
  145. 0x16, /* bLength */
  146. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  147. '2', 0x00, /* wcChar0 */
  148. '0', 0x00, /* wcChar1 */
  149. '2', 0x00, /* wcChar2 */
  150. '2', 0x00, /* wcChar3 */
  151. '1', 0x00, /* wcChar4 */
  152. '2', 0x00, /* wcChar5 */
  153. '3', 0x00, /* wcChar6 */
  154. '4', 0x00, /* wcChar7 */
  155. '5', 0x00, /* wcChar8 */
  156. '6', 0x00, /* wcChar9 */
  157. #ifdef CONFIG_USB_HS
  158. ///////////////////////////////////////
  159. /// device qualifier descriptor
  160. ///////////////////////////////////////
  161. 0x0a,
  162. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  163. 0x00,
  164. 0x02,
  165. 0x00,
  166. 0x00,
  167. 0x00,
  168. 0x40,
  169. 0x00,
  170. 0x00,
  171. #endif
  172. 0x00
  173. };
  174. #endif
  175. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[4][2048];
  176. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[4][2048];
  177. volatile bool ep_tx_busy_flag = false;
  178. static void usbd_event_handler(uint8_t busid, uint8_t event)
  179. {
  180. switch (event) {
  181. case USBD_EVENT_RESET:
  182. break;
  183. case USBD_EVENT_CONNECTED:
  184. break;
  185. case USBD_EVENT_DISCONNECTED:
  186. break;
  187. case USBD_EVENT_RESUME:
  188. break;
  189. case USBD_EVENT_SUSPEND:
  190. break;
  191. case USBD_EVENT_CONFIGURED:
  192. ep_tx_busy_flag = false;
  193. /* setup first out ep read transfer */
  194. usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048);
  195. usbd_ep_start_read(busid, CDC_OUT_EP2, read_buffer, 2048);
  196. usbd_ep_start_read(busid, CDC_OUT_EP3, read_buffer, 2048);
  197. usbd_ep_start_read(busid, CDC_OUT_EP4, read_buffer, 2048);
  198. break;
  199. case USBD_EVENT_SET_REMOTE_WAKEUP:
  200. break;
  201. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  208. {
  209. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  210. /* setup next out ep read transfer */
  211. usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048);
  212. }
  213. void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  214. {
  215. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  216. if ((nbytes % CDC_MAX_MPS) == 0 && nbytes) {
  217. /* send zlp */
  218. usbd_ep_start_write(CDC_IN_EP, NULL, 0);
  219. } else {
  220. ep_tx_busy_flag = false;
  221. }
  222. }
  223. struct usbd_endpoint cdc_out_ep1 = {
  224. .ep_addr = CDC_OUT_EP,
  225. .ep_cb = usbd_cdc_acm_bulk_out
  226. };
  227. struct usbd_endpoint cdc_in_ep1 = {
  228. .ep_addr = CDC_IN_EP,
  229. .ep_cb = usbd_cdc_acm_bulk_in
  230. };
  231. struct usbd_endpoint cdc_out_ep2 = {
  232. .ep_addr = CDC_OUT_EP2,
  233. .ep_cb = usbd_cdc_acm_bulk_out
  234. };
  235. struct usbd_endpoint cdc_in_ep2 = {
  236. .ep_addr = CDC_IN_EP2,
  237. .ep_cb = usbd_cdc_acm_bulk_in
  238. };
  239. struct usbd_endpoint cdc_out_ep3 = {
  240. .ep_addr = CDC_OUT_EP3,
  241. .ep_cb = usbd_cdc_acm_bulk_out
  242. };
  243. struct usbd_endpoint cdc_in_ep3 = {
  244. .ep_addr = CDC_IN_EP3,
  245. .ep_cb = usbd_cdc_acm_bulk_in
  246. };
  247. struct usbd_endpoint cdc_out_ep4 = {
  248. .ep_addr = CDC_OUT_EP4,
  249. .ep_cb = usbd_cdc_acm_bulk_out
  250. };
  251. struct usbd_endpoint cdc_in_ep4 = {
  252. .ep_addr = CDC_IN_EP4,
  253. .ep_cb = usbd_cdc_acm_bulk_in
  254. };
  255. struct usbd_interface intf0;
  256. struct usbd_interface intf1;
  257. struct usbd_interface intf2;
  258. struct usbd_interface intf3;
  259. struct usbd_interface intf4;
  260. struct usbd_interface intf5;
  261. struct usbd_interface intf6;
  262. struct usbd_interface intf7;
  263. void cdc_acm_multi_init(uint8_t busid, uintptr_t reg_base)
  264. {
  265. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  266. usbd_desc_register(busid, &cdc_multi_descriptor);
  267. #else
  268. usbd_desc_register(busid, cdc_multi_descriptor);
  269. #endif
  270. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf0));
  271. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf1));
  272. usbd_add_endpoint(busid, &cdc_out_ep1);
  273. usbd_add_endpoint(busid, &cdc_in_ep1);
  274. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf2));
  275. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf3));
  276. usbd_add_endpoint(busid, &cdc_out_ep2);
  277. usbd_add_endpoint(busid, &cdc_in_ep2);
  278. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf4));
  279. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf5));
  280. usbd_add_endpoint(busid, &cdc_out_ep3);
  281. usbd_add_endpoint(busid, &cdc_in_ep3);
  282. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf6));
  283. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf7));
  284. usbd_add_endpoint(busid, &cdc_out_ep4);
  285. usbd_add_endpoint(busid, &cdc_in_ep4);
  286. usbd_initialize(busid, reg_base, usbd_event_handler);
  287. }