cdc_acm_template.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. static const uint8_t device_descriptor[] = {
  24. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01)
  25. };
  26. static const uint8_t config_descriptor[] = {
  27. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  28. CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02)
  29. };
  30. static const uint8_t device_quality_descriptor[] = {
  31. ///////////////////////////////////////
  32. /// device qualifier descriptor
  33. ///////////////////////////////////////
  34. 0x0a,
  35. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  36. 0x00,
  37. 0x02,
  38. 0x00,
  39. 0x00,
  40. 0x00,
  41. 0x40,
  42. 0x00,
  43. 0x00,
  44. };
  45. static const char *string_descriptors[] = {
  46. (const char[]){ 0x09, 0x04 }, /* Langid */
  47. "CherryUSB", /* Manufacturer */
  48. "CherryUSB CDC DEMO", /* Product */
  49. "2022123456", /* Serial Number */
  50. };
  51. static const uint8_t *device_descriptor_callback(uint8_t speed)
  52. {
  53. return device_descriptor;
  54. }
  55. static const uint8_t *config_descriptor_callback(uint8_t speed)
  56. {
  57. return config_descriptor;
  58. }
  59. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  60. {
  61. return device_quality_descriptor;
  62. }
  63. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  64. {
  65. if (index > 3) {
  66. return NULL;
  67. }
  68. return string_descriptors[index];
  69. }
  70. const struct usb_descriptor cdc_descriptor = {
  71. .device_descriptor_callback = device_descriptor_callback,
  72. .config_descriptor_callback = config_descriptor_callback,
  73. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  74. .string_descriptor_callback = string_descriptor_callback
  75. };
  76. 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*/
  77. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048];
  78. volatile bool ep_tx_busy_flag = false;
  79. static void usbd_event_handler(uint8_t busid, uint8_t event)
  80. {
  81. switch (event) {
  82. case USBD_EVENT_RESET:
  83. break;
  84. case USBD_EVENT_CONNECTED:
  85. break;
  86. case USBD_EVENT_DISCONNECTED:
  87. break;
  88. case USBD_EVENT_RESUME:
  89. break;
  90. case USBD_EVENT_SUSPEND:
  91. break;
  92. case USBD_EVENT_CONFIGURED:
  93. ep_tx_busy_flag = false;
  94. /* setup first out ep read transfer */
  95. usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048);
  96. break;
  97. case USBD_EVENT_SET_REMOTE_WAKEUP:
  98. break;
  99. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  106. {
  107. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  108. // for (int i = 0; i < 100; i++) {
  109. // printf("%02x ", read_buffer[i]);
  110. // }
  111. // printf("\r\n");
  112. /* setup next out ep read transfer */
  113. usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048);
  114. }
  115. void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  116. {
  117. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  118. if ((nbytes % usbd_get_ep_mps(busid, ep)) == 0 && nbytes) {
  119. /* send zlp */
  120. usbd_ep_start_write(busid, CDC_IN_EP, NULL, 0);
  121. } else {
  122. ep_tx_busy_flag = false;
  123. }
  124. }
  125. /*!< endpoint call back */
  126. struct usbd_endpoint cdc_out_ep = {
  127. .ep_addr = CDC_OUT_EP,
  128. .ep_cb = usbd_cdc_acm_bulk_out
  129. };
  130. struct usbd_endpoint cdc_in_ep = {
  131. .ep_addr = CDC_IN_EP,
  132. .ep_cb = usbd_cdc_acm_bulk_in
  133. };
  134. static struct usbd_interface intf0;
  135. static struct usbd_interface intf1;
  136. void cdc_acm_init(uint8_t busid, uintptr_t reg_base)
  137. {
  138. const uint8_t data[10] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };
  139. memcpy(&write_buffer[0], data, 10);
  140. memset(&write_buffer[10], 'a', 2038);
  141. usbd_desc_register(busid, &cdc_descriptor);
  142. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf0));
  143. usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf1));
  144. usbd_add_endpoint(busid, &cdc_out_ep);
  145. usbd_add_endpoint(busid, &cdc_in_ep);
  146. usbd_initialize(busid, reg_base, usbd_event_handler);
  147. }
  148. volatile uint8_t dtr_enable = 0;
  149. void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr)
  150. {
  151. if (dtr) {
  152. dtr_enable = 1;
  153. } else {
  154. dtr_enable = 0;
  155. }
  156. }
  157. void cdc_acm_data_send_with_dtr_test(uint8_t busid)
  158. {
  159. if (dtr_enable) {
  160. ep_tx_busy_flag = true;
  161. usbd_ep_start_write(busid, CDC_IN_EP, write_buffer, 2048);
  162. while (ep_tx_busy_flag) {
  163. }
  164. }
  165. }