cdc_acm_template.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 0x02
  11. #define CDC_INT_EP 0x83
  12. #define USBD_VID 0xFFFF
  13. #define USBD_PID 0xFFFF
  14. #define USBD_MAX_POWER 100
  15. #define USBD_LANGID_STRING 1033
  16. /*!< config descriptor size */
  17. #define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN)
  18. #ifdef CONFIG_USB_HS
  19. #define CDC_MAX_MPS 512
  20. #else
  21. #define CDC_MAX_MPS 64
  22. #endif
  23. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  24. static const uint8_t device_descriptor[] = {
  25. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
  26. };
  27. static const uint8_t config_descriptor[] = {
  28. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  29. CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02)
  30. };
  31. static const uint8_t device_quality_descriptor[] = {
  32. ///////////////////////////////////////
  33. /// device qualifier descriptor
  34. ///////////////////////////////////////
  35. 0x0a,
  36. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  37. 0x00,
  38. 0x02,
  39. 0x00,
  40. 0x00,
  41. 0x00,
  42. 0x40,
  43. 0x00,
  44. 0x00,
  45. };
  46. static const char *string_descriptors[] = {
  47. (const char[]){ 0x09, 0x04 }, /* Langid */
  48. "CherryUSB", /* Manufacturer */
  49. "CherryUSB CDC DEMO", /* Product */
  50. "2022123456", /* Serial Number */
  51. };
  52. static const uint8_t *device_descriptor_callback(uint8_t speed)
  53. {
  54. return device_descriptor;
  55. }
  56. static const uint8_t *config_descriptor_callback(uint8_t speed)
  57. {
  58. return config_descriptor;
  59. }
  60. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  61. {
  62. return device_quality_descriptor;
  63. }
  64. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  65. {
  66. if (index > 3) {
  67. return NULL;
  68. }
  69. return string_descriptors[index];
  70. }
  71. const struct usb_descriptor cdc_descriptor = {
  72. .device_descriptor_callback = device_descriptor_callback,
  73. .config_descriptor_callback = config_descriptor_callback,
  74. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  75. .string_descriptor_callback = string_descriptor_callback
  76. };
  77. #else
  78. /*!< global descriptor */
  79. static const uint8_t cdc_descriptor[] = {
  80. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
  81. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  82. CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
  83. ///////////////////////////////////////
  84. /// string0 descriptor
  85. ///////////////////////////////////////
  86. USB_LANGID_INIT(USBD_LANGID_STRING),
  87. ///////////////////////////////////////
  88. /// string1 descriptor
  89. ///////////////////////////////////////
  90. 0x14, /* bLength */
  91. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  92. 'C', 0x00, /* wcChar0 */
  93. 'h', 0x00, /* wcChar1 */
  94. 'e', 0x00, /* wcChar2 */
  95. 'r', 0x00, /* wcChar3 */
  96. 'r', 0x00, /* wcChar4 */
  97. 'y', 0x00, /* wcChar5 */
  98. 'U', 0x00, /* wcChar6 */
  99. 'S', 0x00, /* wcChar7 */
  100. 'B', 0x00, /* wcChar8 */
  101. ///////////////////////////////////////
  102. /// string2 descriptor
  103. ///////////////////////////////////////
  104. 0x26, /* bLength */
  105. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  106. 'C', 0x00, /* wcChar0 */
  107. 'h', 0x00, /* wcChar1 */
  108. 'e', 0x00, /* wcChar2 */
  109. 'r', 0x00, /* wcChar3 */
  110. 'r', 0x00, /* wcChar4 */
  111. 'y', 0x00, /* wcChar5 */
  112. 'U', 0x00, /* wcChar6 */
  113. 'S', 0x00, /* wcChar7 */
  114. 'B', 0x00, /* wcChar8 */
  115. ' ', 0x00, /* wcChar9 */
  116. 'C', 0x00, /* wcChar10 */
  117. 'D', 0x00, /* wcChar11 */
  118. 'C', 0x00, /* wcChar12 */
  119. ' ', 0x00, /* wcChar13 */
  120. 'D', 0x00, /* wcChar14 */
  121. 'E', 0x00, /* wcChar15 */
  122. 'M', 0x00, /* wcChar16 */
  123. 'O', 0x00, /* wcChar17 */
  124. ///////////////////////////////////////
  125. /// string3 descriptor
  126. ///////////////////////////////////////
  127. 0x16, /* bLength */
  128. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  129. '2', 0x00, /* wcChar0 */
  130. '0', 0x00, /* wcChar1 */
  131. '2', 0x00, /* wcChar2 */
  132. '2', 0x00, /* wcChar3 */
  133. '1', 0x00, /* wcChar4 */
  134. '2', 0x00, /* wcChar5 */
  135. '3', 0x00, /* wcChar6 */
  136. '4', 0x00, /* wcChar7 */
  137. '5', 0x00, /* wcChar8 */
  138. '6', 0x00, /* wcChar9 */
  139. #ifdef CONFIG_USB_HS
  140. ///////////////////////////////////////
  141. /// device qualifier descriptor
  142. ///////////////////////////////////////
  143. 0x0a,
  144. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  145. 0x00,
  146. 0x02,
  147. 0x00,
  148. 0x00,
  149. 0x00,
  150. 0x40,
  151. 0x00,
  152. 0x00,
  153. #endif
  154. 0x00
  155. };
  156. #endif
  157. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048]; /* 2048 is only for test speed , please use CDC_MAX_MPS for common*/
  158. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048];
  159. volatile bool ep_tx_busy_flag = false;
  160. static void usbd_event_handler(uint8_t busid, uint8_t event)
  161. {
  162. switch (event) {
  163. case USBD_EVENT_RESET:
  164. break;
  165. case USBD_EVENT_CONNECTED:
  166. break;
  167. case USBD_EVENT_DISCONNECTED:
  168. break;
  169. case USBD_EVENT_RESUME:
  170. break;
  171. case USBD_EVENT_SUSPEND:
  172. break;
  173. case USBD_EVENT_CONFIGURED:
  174. ep_tx_busy_flag = false;
  175. /* setup first out ep read transfer */
  176. usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048);
  177. break;
  178. case USBD_EVENT_SET_REMOTE_WAKEUP:
  179. break;
  180. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  187. {
  188. USB_LOG_RAW("actual out len:%d\r\n", nbytes);
  189. // for (int i = 0; i < 100; i++) {
  190. // printf("%02x ", read_buffer[i]);
  191. // }
  192. // printf("\r\n");
  193. /* setup next out ep read transfer */
  194. usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048);
  195. }
  196. void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  197. {
  198. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  199. if ((nbytes % usbd_get_ep_mps(busid, ep)) == 0 && nbytes) {
  200. /* send zlp */
  201. usbd_ep_start_write(busid, CDC_IN_EP, NULL, 0);
  202. } else {
  203. ep_tx_busy_flag = false;
  204. }
  205. }
  206. /*!< endpoint call back */
  207. struct usbd_endpoint cdc_out_ep = {
  208. .ep_addr = CDC_OUT_EP,
  209. .ep_cb = usbd_cdc_acm_bulk_out
  210. };
  211. struct usbd_endpoint cdc_in_ep = {
  212. .ep_addr = CDC_IN_EP,
  213. .ep_cb = usbd_cdc_acm_bulk_in
  214. };
  215. static struct usbd_interface intf0;
  216. static struct usbd_interface intf1;
  217. void cdc_acm_init(uint8_t busid, uintptr_t reg_base)
  218. {
  219. const uint8_t data[10] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };
  220. memcpy(&write_buffer[0], data, 10);
  221. memset(&write_buffer[10], 'a', 2038);
  222. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  223. usbd_desc_register(busid, &cdc_descriptor);
  224. #else
  225. usbd_desc_register(busid, cdc_descriptor);
  226. #endif
  227. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf0));
  228. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf1));
  229. usbd_add_endpoint(busid, &cdc_out_ep);
  230. usbd_add_endpoint(busid, &cdc_in_ep);
  231. usbd_initialize(busid, reg_base, usbd_event_handler);
  232. }
  233. volatile uint8_t dtr_enable = 0;
  234. void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr)
  235. {
  236. if (dtr) {
  237. dtr_enable = 1;
  238. } else {
  239. dtr_enable = 0;
  240. }
  241. }
  242. void cdc_acm_data_send_with_dtr_test(uint8_t busid)
  243. {
  244. if (dtr_enable) {
  245. ep_tx_busy_flag = true;
  246. usbd_ep_start_write(busid, CDC_IN_EP, write_buffer, 2048);
  247. while (ep_tx_busy_flag) {
  248. }
  249. }
  250. }