hid_custom_inout_template.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  27. static const uint8_t device_descriptor[] = {
  28. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01)
  29. };
  30. static const uint8_t config_descriptor[] = {
  31. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  32. HID_CUSTOM_INOUT_DESCRIPTOR_INIT(0x00, 0x01, HID_CUSTOM_REPORT_DESC_SIZE, HIDRAW_OUT_EP, HIDRAW_IN_EP, HID_MAX_MPS, HIDRAW_IN_INTERVAL),
  33. };
  34. static const uint8_t device_quality_descriptor[] = {
  35. ///////////////////////////////////////
  36. /// device qualifier descriptor
  37. ///////////////////////////////////////
  38. 0x0a,
  39. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  40. 0x00,
  41. 0x02,
  42. 0x00,
  43. 0x00,
  44. 0x00,
  45. 0x40,
  46. 0x00,
  47. 0x00,
  48. };
  49. static const char *string_descriptors[] = {
  50. (const char[]){ 0x09, 0x04 }, /* Langid */
  51. "CherryUSB", /* Manufacturer */
  52. "CherryUSB HID DEMO", /* Product */
  53. "2022123456", /* Serial Number */
  54. };
  55. static const uint8_t *device_descriptor_callback(uint8_t speed)
  56. {
  57. return device_descriptor;
  58. }
  59. static const uint8_t *config_descriptor_callback(uint8_t speed)
  60. {
  61. return config_descriptor;
  62. }
  63. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  64. {
  65. return device_quality_descriptor;
  66. }
  67. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  68. {
  69. if (index > 3) {
  70. return NULL;
  71. }
  72. return string_descriptors[index];
  73. }
  74. const struct usb_descriptor hid_descriptor = {
  75. .device_descriptor_callback = device_descriptor_callback,
  76. .config_descriptor_callback = config_descriptor_callback,
  77. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  78. .string_descriptor_callback = string_descriptor_callback
  79. };
  80. #else
  81. /*!< global descriptor */
  82. static const uint8_t hid_descriptor[] = {
  83. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  84. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  85. HID_CUSTOM_INOUT_DESCRIPTOR_INIT(0x00, 0x01, HID_CUSTOM_REPORT_DESC_SIZE, HIDRAW_OUT_EP, HIDRAW_IN_EP, HID_MAX_MPS, HIDRAW_IN_INTERVAL),
  86. /*
  87. * string0 descriptor
  88. */
  89. USB_LANGID_INIT(USBD_LANGID_STRING),
  90. /*
  91. * string1 descriptor
  92. */
  93. 0x14, /* bLength */
  94. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  95. 'C', 0x00, /* wcChar0 */
  96. 'h', 0x00, /* wcChar1 */
  97. 'e', 0x00, /* wcChar2 */
  98. 'r', 0x00, /* wcChar3 */
  99. 'r', 0x00, /* wcChar4 */
  100. 'y', 0x00, /* wcChar5 */
  101. 'U', 0x00, /* wcChar6 */
  102. 'S', 0x00, /* wcChar7 */
  103. 'B', 0x00, /* wcChar8 */
  104. /*
  105. * string2 descriptor
  106. */
  107. 0x26, /* bLength */
  108. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  109. 'C', 0x00, /* wcChar0 */
  110. 'h', 0x00, /* wcChar1 */
  111. 'e', 0x00, /* wcChar2 */
  112. 'r', 0x00, /* wcChar3 */
  113. 'r', 0x00, /* wcChar4 */
  114. 'y', 0x00, /* wcChar5 */
  115. 'U', 0x00, /* wcChar6 */
  116. 'S', 0x00, /* wcChar7 */
  117. 'B', 0x00, /* wcChar8 */
  118. ' ', 0x00, /* wcChar9 */
  119. 'H', 0x00, /* wcChar10 */
  120. 'I', 0x00, /* wcChar11 */
  121. 'D', 0x00, /* wcChar12 */
  122. ' ', 0x00, /* wcChar13 */
  123. 'D', 0x00, /* wcChar14 */
  124. 'E', 0x00, /* wcChar15 */
  125. 'M', 0x00, /* wcChar16 */
  126. 'O', 0x00, /* wcChar17 */
  127. /*
  128. * string3 descriptor
  129. */
  130. 0x16, /* bLength */
  131. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  132. '2', 0x00, /* wcChar0 */
  133. '0', 0x00, /* wcChar1 */
  134. '2', 0x00, /* wcChar2 */
  135. '2', 0x00, /* wcChar3 */
  136. '1', 0x00, /* wcChar4 */
  137. '2', 0x00, /* wcChar5 */
  138. '3', 0x00, /* wcChar6 */
  139. '4', 0x00, /* wcChar7 */
  140. '5', 0x00, /* wcChar8 */
  141. '6', 0x00, /* wcChar9 */
  142. #ifdef CONFIG_USB_HS
  143. /*
  144. * device qualifier descriptor
  145. */
  146. 0x0a,
  147. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  148. 0x00,
  149. 0x02,
  150. 0x00,
  151. 0x00,
  152. 0x00,
  153. 0x40,
  154. 0x00,
  155. 0x00,
  156. #endif
  157. 0x00
  158. };
  159. #endif
  160. /*!< custom hid report descriptor */
  161. static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
  162. #ifdef CONFIG_USB_HS
  163. /* USER CODE BEGIN 0 */
  164. 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
  165. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  166. 0xa1, 0x01, /* COLLECTION (Application) */
  167. 0x85, 0x02, /* REPORT ID (0x02) */
  168. 0x09, 0x02, /* USAGE (Vendor Usage 1) */
  169. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  170. 0x25, 0xff, /*LOGICAL_MAXIMUM (255) */
  171. 0x75, 0x08, /* REPORT_SIZE (8) */
  172. 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */
  173. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  174. /* <___________________________________________________> */
  175. 0x85, 0x01, /* REPORT ID (0x01) */
  176. 0x09, 0x03, /* USAGE (Vendor Usage 1) */
  177. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  178. 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */
  179. 0x75, 0x08, /* REPORT_SIZE (8) */
  180. 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */
  181. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  182. /* USER CODE END 0 */
  183. 0xC0 /* END_COLLECTION */
  184. #else
  185. /* USER CODE BEGIN 0 */
  186. 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
  187. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  188. 0xa1, 0x01, /* COLLECTION (Application) */
  189. 0x85, 0x02, /* REPORT ID (0x02) */
  190. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  191. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  192. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  193. 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
  194. 0x75, 0x08, /* REPORT_SIZE (8) */
  195. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  196. /* <___________________________________________________> */
  197. 0x85, 0x01, /* REPORT ID (0x01) */
  198. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  199. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  200. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  201. 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
  202. 0x75, 0x08, /* REPORT_SIZE (8) */
  203. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  204. /* USER CODE END 0 */
  205. 0xC0 /* END_COLLECTION */
  206. #endif
  207. };
  208. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HID_MAX_MPS];
  209. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t send_buffer[HID_MAX_MPS];
  210. #define HID_STATE_IDLE 0
  211. #define HID_STATE_BUSY 1
  212. /*!< hid state ! Data can be sent only when state is idle */
  213. static volatile uint8_t custom_state;
  214. static void usbd_event_handler(uint8_t busid, uint8_t event)
  215. {
  216. switch (event) {
  217. case USBD_EVENT_RESET:
  218. break;
  219. case USBD_EVENT_CONNECTED:
  220. break;
  221. case USBD_EVENT_DISCONNECTED:
  222. break;
  223. case USBD_EVENT_RESUME:
  224. break;
  225. case USBD_EVENT_SUSPEND:
  226. break;
  227. case USBD_EVENT_CONFIGURED:
  228. /* setup first out ep read transfer */
  229. usbd_ep_start_read(busid, HIDRAW_OUT_EP, read_buffer, HID_MAX_MPS);
  230. break;
  231. case USBD_EVENT_SET_REMOTE_WAKEUP:
  232. break;
  233. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. static void usbd_hid_custom_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  240. {
  241. (void)busid;
  242. (void)ep;
  243. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  244. custom_state = HID_STATE_IDLE;
  245. }
  246. static void usbd_hid_custom_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  247. {
  248. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  249. usbd_ep_start_read(busid, ep, read_buffer, HID_MAX_MPS);
  250. read_buffer[0] = 0x02; /* IN: report id */
  251. usbd_ep_start_write(busid, HIDRAW_IN_EP, read_buffer, nbytes);
  252. }
  253. static struct usbd_endpoint custom_in_ep = {
  254. .ep_cb = usbd_hid_custom_in_callback,
  255. .ep_addr = HIDRAW_IN_EP
  256. };
  257. static struct usbd_endpoint custom_out_ep = {
  258. .ep_cb = usbd_hid_custom_out_callback,
  259. .ep_addr = HIDRAW_OUT_EP
  260. };
  261. /* function ------------------------------------------------------------------*/
  262. /**
  263. * @brief hid custom init
  264. * @pre none
  265. * @param[in] none
  266. * @retval none
  267. */
  268. struct usbd_interface intf0;
  269. void hid_custom_init(uint8_t busid, uintptr_t reg_base)
  270. {
  271. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  272. usbd_desc_register(busid, &hid_descriptor);
  273. #else
  274. usbd_desc_register(busid, hid_descriptor);
  275. #endif
  276. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE));
  277. usbd_add_endpoint(busid, &custom_in_ep);
  278. usbd_add_endpoint(busid, &custom_out_ep);
  279. usbd_initialize(busid, reg_base, usbd_event_handler);
  280. }