cdc_acm_template.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "usbd_core.h"
  2. #include "usbd_cdc.h"
  3. /*!< endpoint address */
  4. #define CDC_IN_EP 0x81
  5. #define CDC_OUT_EP 0x02
  6. #define CDC_INT_EP 0x83
  7. #define USBD_VID 0xFFFF
  8. #define USBD_PID 0xFFFF
  9. #define USBD_MAX_POWER 100
  10. #define USBD_LANGID_STRING 1033
  11. /*!< config descriptor size */
  12. #define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN)
  13. /*!< global descriptor */
  14. static const uint8_t cdc_descriptor[] = {
  15. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
  16. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  17. CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, 0x02),
  18. ///////////////////////////////////////
  19. /// string0 descriptor
  20. ///////////////////////////////////////
  21. USB_LANGID_INIT(USBD_LANGID_STRING),
  22. ///////////////////////////////////////
  23. /// string1 descriptor
  24. ///////////////////////////////////////
  25. 0x14, /* bLength */
  26. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  27. 'C', 0x00, /* wcChar0 */
  28. 'h', 0x00, /* wcChar1 */
  29. 'e', 0x00, /* wcChar2 */
  30. 'r', 0x00, /* wcChar3 */
  31. 'r', 0x00, /* wcChar4 */
  32. 'y', 0x00, /* wcChar5 */
  33. 'U', 0x00, /* wcChar6 */
  34. 'S', 0x00, /* wcChar7 */
  35. 'B', 0x00, /* wcChar8 */
  36. ///////////////////////////////////////
  37. /// string2 descriptor
  38. ///////////////////////////////////////
  39. 0x26, /* bLength */
  40. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  41. 'C', 0x00, /* wcChar0 */
  42. 'h', 0x00, /* wcChar1 */
  43. 'e', 0x00, /* wcChar2 */
  44. 'r', 0x00, /* wcChar3 */
  45. 'r', 0x00, /* wcChar4 */
  46. 'y', 0x00, /* wcChar5 */
  47. 'U', 0x00, /* wcChar6 */
  48. 'S', 0x00, /* wcChar7 */
  49. 'B', 0x00, /* wcChar8 */
  50. ' ', 0x00, /* wcChar9 */
  51. 'C', 0x00, /* wcChar10 */
  52. 'D', 0x00, /* wcChar11 */
  53. 'C', 0x00, /* wcChar12 */
  54. ' ', 0x00, /* wcChar13 */
  55. 'D', 0x00, /* wcChar14 */
  56. 'E', 0x00, /* wcChar15 */
  57. 'M', 0x00, /* wcChar16 */
  58. 'O', 0x00, /* wcChar17 */
  59. ///////////////////////////////////////
  60. /// string3 descriptor
  61. ///////////////////////////////////////
  62. 0x16, /* bLength */
  63. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  64. '2', 0x00, /* wcChar0 */
  65. '0', 0x00, /* wcChar1 */
  66. '2', 0x00, /* wcChar2 */
  67. '2', 0x00, /* wcChar3 */
  68. '1', 0x00, /* wcChar4 */
  69. '2', 0x00, /* wcChar5 */
  70. '3', 0x00, /* wcChar6 */
  71. '4', 0x00, /* wcChar7 */
  72. '5', 0x00, /* wcChar8 */
  73. '6', 0x00, /* wcChar9 */
  74. #ifdef CONFIG_USB_HS
  75. ///////////////////////////////////////
  76. /// device qualifier descriptor
  77. ///////////////////////////////////////
  78. 0x0a,
  79. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  80. 0x00,
  81. 0x02,
  82. 0x02,
  83. 0x02,
  84. 0x01,
  85. 0x40,
  86. 0x01,
  87. 0x00,
  88. #endif
  89. 0x00
  90. };
  91. /*!< class */
  92. usbd_class_t cdc_class;
  93. /*!< interface one */
  94. usbd_interface_t cdc_cmd_intf;
  95. /*!< interface two */
  96. usbd_interface_t cdc_data_intf;
  97. #ifdef CONFIG_USB_HS
  98. #define CDC_BULK_SIZE 512
  99. #else
  100. #define CDC_BULK_SIZE 64
  101. #endif
  102. /* function ------------------------------------------------------------------*/
  103. void usbd_cdc_acm_out(uint8_t ep)
  104. {
  105. uint8_t data[CDC_BULK_SIZE];
  106. uint32_t read_byte;
  107. usbd_ep_read(ep, data, CDC_BULK_SIZE, &read_byte);
  108. for (uint32_t i = 0; i < read_byte; i++) {
  109. USB_LOG_RAW("%02x ", data[i]);
  110. }
  111. USB_LOG_RAW("\r\n");
  112. USB_LOG_RAW("read len:%d\r\n", read_byte);
  113. usbd_ep_read(ep, NULL, 0, NULL);
  114. }
  115. void usbd_cdc_acm_in(uint8_t ep)
  116. {
  117. USB_LOG_RAW("in\r\n");
  118. }
  119. /*!< endpoint call back */
  120. usbd_endpoint_t cdc_out_ep = {
  121. .ep_addr = CDC_OUT_EP,
  122. .ep_cb = usbd_cdc_acm_out
  123. };
  124. usbd_endpoint_t cdc_in_ep = {
  125. .ep_addr = CDC_IN_EP,
  126. .ep_cb = usbd_cdc_acm_in
  127. };
  128. /* function ------------------------------------------------------------------*/
  129. void cdc_acm_init(void)
  130. {
  131. usbd_desc_register(cdc_descriptor);
  132. /*!< add interface */
  133. usbd_cdc_add_acm_interface(&cdc_class, &cdc_cmd_intf);
  134. usbd_cdc_add_acm_interface(&cdc_class, &cdc_data_intf);
  135. /*!< interface add endpoint */
  136. usbd_interface_add_endpoint(&cdc_data_intf, &cdc_out_ep);
  137. usbd_interface_add_endpoint(&cdc_data_intf, &cdc_in_ep);
  138. usbd_initialize();
  139. }
  140. volatile uint8_t dtr_enable = 0;
  141. void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr)
  142. {
  143. if (dtr) {
  144. dtr_enable = 1;
  145. } else {
  146. dtr_enable = 0;
  147. }
  148. }
  149. void cdc_acm_data_send_with_dtr_test(void)
  150. {
  151. if (dtr_enable) {
  152. uint8_t data_buffer[CDC_BULK_SIZE] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };
  153. memset(&data_buffer[10],'a',CDC_BULK_SIZE - 10);
  154. data_buffer[63] = 'b';
  155. usbd_ep_write(CDC_IN_EP, data_buffer, CDC_BULK_SIZE, NULL);// test if zlp is work.
  156. usbd_ep_write(CDC_IN_EP, NULL, 0, NULL);
  157. }
  158. }