cdc_acm_multi_template.c 9.9 KB

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