cdc_acm_rttchardev_template.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2025, 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. static void usbd_event_handler(uint8_t busid, uint8_t event)
  158. {
  159. switch (event) {
  160. case USBD_EVENT_RESET:
  161. break;
  162. case USBD_EVENT_CONNECTED:
  163. break;
  164. case USBD_EVENT_DISCONNECTED:
  165. break;
  166. case USBD_EVENT_RESUME:
  167. break;
  168. case USBD_EVENT_SUSPEND:
  169. break;
  170. case USBD_EVENT_CONFIGURED:
  171. break;
  172. case USBD_EVENT_SET_REMOTE_WAKEUP:
  173. break;
  174. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180. extern void usbd_cdc_acm_serial_init(uint8_t busid, uint8_t in_ep, uint8_t out_ep);
  181. void cdc_acm_chardev_init(uint8_t busid, uintptr_t reg_base)
  182. {
  183. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  184. usbd_desc_register(busid, &cdc_descriptor);
  185. #else
  186. usbd_desc_register(busid, cdc_descriptor);
  187. #endif
  188. usbd_cdc_acm_serial_init(busid, CDC_IN_EP, CDC_OUT_EP);
  189. usbd_initialize(busid, reg_base, usbd_event_handler);
  190. }
  191. static int cdc_acm_enter(int argc, char **argv)
  192. {
  193. (void)argc;
  194. (void)argv;
  195. finsh_set_device("usb-acm0");
  196. rt_console_set_device("usb-acm0");
  197. return 0;
  198. }
  199. MSH_CMD_EXPORT(cdc_acm_enter, cdc_acm_enter);
  200. static int cdc_acm_exit(int argc, char **argv)
  201. {
  202. (void)argc;
  203. (void)argv;
  204. finsh_set_device(RT_CONSOLE_DEVICE_NAME);
  205. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  206. return 0;
  207. }
  208. MSH_CMD_EXPORT(cdc_acm_exit, cdc_acm_exit);