hid_custom_inout_template.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include "usbd_core.h"
  8. #include "usbd_hid.h"
  9. #define HIDRAW_IN_EP 0x81
  10. #define HIDRAW_OUT_EP 0x02
  11. #define USBD_VID 0xffff
  12. #define USBD_PID 0xffff
  13. #define USBD_MAX_POWER 100
  14. #define USBD_LANGID_STRING 1033
  15. /*!< config descriptor size */
  16. #define USB_CONFIG_SIZE (9 + 9 + 9 + 7 + 7)
  17. /*!< custom hid report descriptor size */
  18. #define HID_CUSTOM_REPORT_DESC_SIZE 38
  19. #ifdef CONFIG_USB_HS
  20. #define HID_MAX_MPS 1024
  21. #define HIDRAW_IN_INTERVAL 1
  22. #else
  23. #define HID_MAX_MPS 64
  24. #define HIDRAW_IN_INTERVAL 1
  25. #endif
  26. static const uint8_t device_descriptor[] = {
  27. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01)
  28. };
  29. static const uint8_t config_descriptor[] = {
  30. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  31. HID_CUSTOM_INOUT_DESCRIPTOR_INIT(0x00, 0x01, HID_CUSTOM_REPORT_DESC_SIZE, HIDRAW_OUT_EP, HIDRAW_IN_EP, HID_MAX_MPS, HIDRAW_IN_INTERVAL),
  32. };
  33. static const uint8_t device_quality_descriptor[] = {
  34. ///////////////////////////////////////
  35. /// device qualifier descriptor
  36. ///////////////////////////////////////
  37. 0x0a,
  38. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  39. 0x00,
  40. 0x02,
  41. 0x00,
  42. 0x00,
  43. 0x00,
  44. 0x40,
  45. 0x00,
  46. 0x00,
  47. };
  48. static const char *string_descriptors[] = {
  49. (const char[]){ 0x09, 0x04 }, /* Langid */
  50. "CherryUSB", /* Manufacturer */
  51. "CherryUSB HID DEMO", /* Product */
  52. "2022123456", /* Serial Number */
  53. };
  54. static const uint8_t *device_descriptor_callback(uint8_t speed)
  55. {
  56. return device_descriptor;
  57. }
  58. static const uint8_t *config_descriptor_callback(uint8_t speed)
  59. {
  60. return config_descriptor;
  61. }
  62. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  63. {
  64. return device_quality_descriptor;
  65. }
  66. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  67. {
  68. if (index > 3) {
  69. return NULL;
  70. }
  71. return string_descriptors[index];
  72. }
  73. const struct usb_descriptor hid_descriptor = {
  74. .device_descriptor_callback = device_descriptor_callback,
  75. .config_descriptor_callback = config_descriptor_callback,
  76. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  77. .string_descriptor_callback = string_descriptor_callback
  78. };
  79. /*!< custom hid report descriptor */
  80. static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
  81. #ifdef CONFIG_USB_HS
  82. /* USER CODE BEGIN 0 */
  83. 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
  84. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  85. 0xa1, 0x01, /* COLLECTION (Application) */
  86. 0x85, 0x02, /* REPORT ID (0x02) */
  87. 0x09, 0x02, /* USAGE (Vendor Usage 1) */
  88. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  89. 0x25, 0xff, /*LOGICAL_MAXIMUM (255) */
  90. 0x75, 0x08, /* REPORT_SIZE (8) */
  91. 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */
  92. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  93. /* <___________________________________________________> */
  94. 0x85, 0x01, /* REPORT ID (0x01) */
  95. 0x09, 0x03, /* USAGE (Vendor Usage 1) */
  96. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  97. 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */
  98. 0x75, 0x08, /* REPORT_SIZE (8) */
  99. 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */
  100. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  101. /* USER CODE END 0 */
  102. 0xC0 /* END_COLLECTION */
  103. #else
  104. /* USER CODE BEGIN 0 */
  105. 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
  106. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  107. 0xa1, 0x01, /* COLLECTION (Application) */
  108. 0x85, 0x02, /* REPORT ID (0x02) */
  109. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  110. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  111. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  112. 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
  113. 0x75, 0x08, /* REPORT_SIZE (8) */
  114. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  115. /* <___________________________________________________> */
  116. 0x85, 0x01, /* REPORT ID (0x01) */
  117. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  118. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  119. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  120. 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
  121. 0x75, 0x08, /* REPORT_SIZE (8) */
  122. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  123. /* USER CODE END 0 */
  124. 0xC0 /* END_COLLECTION */
  125. #endif
  126. };
  127. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HID_MAX_MPS];
  128. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t send_buffer[HID_MAX_MPS];
  129. #define HID_STATE_IDLE 0
  130. #define HID_STATE_BUSY 1
  131. /*!< hid state ! Data can be sent only when state is idle */
  132. static volatile uint8_t custom_state;
  133. static void usbd_event_handler(uint8_t busid, uint8_t event)
  134. {
  135. switch (event) {
  136. case USBD_EVENT_RESET:
  137. break;
  138. case USBD_EVENT_CONNECTED:
  139. break;
  140. case USBD_EVENT_DISCONNECTED:
  141. break;
  142. case USBD_EVENT_RESUME:
  143. break;
  144. case USBD_EVENT_SUSPEND:
  145. break;
  146. case USBD_EVENT_CONFIGURED:
  147. /* setup first out ep read transfer */
  148. usbd_ep_start_read(busid, HIDRAW_OUT_EP, read_buffer, HID_MAX_MPS);
  149. break;
  150. case USBD_EVENT_SET_REMOTE_WAKEUP:
  151. break;
  152. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. static void usbd_hid_custom_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  159. {
  160. (void)busid;
  161. (void)ep;
  162. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  163. custom_state = HID_STATE_IDLE;
  164. }
  165. static void usbd_hid_custom_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  166. {
  167. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  168. usbd_ep_start_read(busid, ep, read_buffer, HID_MAX_MPS);
  169. read_buffer[0] = 0x02; /* IN: report id */
  170. usbd_ep_start_write(busid, HIDRAW_IN_EP, read_buffer, nbytes);
  171. }
  172. static struct usbd_endpoint custom_in_ep = {
  173. .ep_cb = usbd_hid_custom_in_callback,
  174. .ep_addr = HIDRAW_IN_EP
  175. };
  176. static struct usbd_endpoint custom_out_ep = {
  177. .ep_cb = usbd_hid_custom_out_callback,
  178. .ep_addr = HIDRAW_OUT_EP
  179. };
  180. /* function ------------------------------------------------------------------*/
  181. /**
  182. * @brief hid custom init
  183. * @pre none
  184. * @param[in] none
  185. * @retval none
  186. */
  187. struct usbd_interface intf0;
  188. void hid_custom_init(uint8_t busid, uintptr_t reg_base)
  189. {
  190. usbd_desc_register(busid, &hid_descriptor);
  191. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE));
  192. usbd_add_endpoint(busid, &custom_in_ep);
  193. usbd_add_endpoint(busid, &custom_out_ep);
  194. usbd_initialize(busid, reg_base, usbd_event_handler);
  195. }