hid_custom_inout_template.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. /*!< hidraw in endpoint */
  10. #define HIDRAW_IN_EP 0x81
  11. #ifdef CONFIG_USB_HS
  12. #define HIDRAW_IN_EP_SIZE 1024
  13. #define HIDRAW_IN_INTERVAL 4
  14. #else
  15. #define HIDRAW_IN_EP_SIZE 64
  16. #define HIDRAW_IN_INTERVAL 10
  17. #endif
  18. /*!< hidraw out endpoint */
  19. #define HIDRAW_OUT_EP 0x02
  20. #ifdef CONFIG_USB_HS
  21. #define HIDRAW_OUT_EP_SIZE 1024
  22. #define HIDRAW_OUT_EP_INTERVAL 4
  23. #else
  24. #define HIDRAW_OUT_EP_SIZE 64
  25. #define HIDRAW_OUT_EP_INTERVAL 10
  26. #endif
  27. #define USBD_VID 0xffff
  28. #define USBD_PID 0xffff
  29. #define USBD_MAX_POWER 100
  30. #define USBD_LANGID_STRING 1033
  31. /*!< config descriptor size */
  32. #define USB_HID_CONFIG_DESC_SIZ (9 + 9 + 9 + 7 + 7)
  33. /*!< custom hid report descriptor size */
  34. #define HID_CUSTOM_REPORT_DESC_SIZE 38
  35. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  36. static const uint8_t device_descriptor[] = {
  37. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01)
  38. };
  39. static const uint8_t config_descriptor[] = {
  40. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  41. /************** Descriptor of Custom interface *****************/
  42. 0x09, /* bLength: Interface Descriptor size */
  43. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  44. 0x00, /* bInterfaceNumber: Number of Interface */
  45. 0x00, /* bAlternateSetting: Alternate setting */
  46. 0x02, /* bNumEndpoints */
  47. 0x03, /* bInterfaceClass: HID */
  48. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  49. 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  50. 0, /* iInterface: Index of string descriptor */
  51. /******************** Descriptor of Custom HID ********************/
  52. 0x09, /* bLength: HID Descriptor size */
  53. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  54. 0x11, /* bcdHID: HID Class Spec release number */
  55. 0x01,
  56. 0x00, /* bCountryCode: Hardware target country */
  57. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  58. 0x22, /* bDescriptorType */
  59. HID_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  60. 0x00,
  61. /******************** Descriptor of Custom in endpoint ********************/
  62. 0x07, /* bLength: Endpoint Descriptor size */
  63. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  64. HIDRAW_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */
  65. 0x03, /* bmAttributes: Interrupt endpoint */
  66. WBVAL(HIDRAW_IN_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
  67. HIDRAW_IN_INTERVAL, /* bInterval: Polling Interval */
  68. /******************** Descriptor of Custom out endpoint ********************/
  69. 0x07, /* bLength: Endpoint Descriptor size */
  70. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  71. HIDRAW_OUT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  72. 0x03, /* bmAttributes: Interrupt endpoint */
  73. WBVAL(HIDRAW_OUT_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
  74. HIDRAW_OUT_EP_INTERVAL, /* bInterval: Polling Interval */
  75. /* 73 */
  76. };
  77. static const uint8_t device_quality_descriptor[] = {
  78. ///////////////////////////////////////
  79. /// device qualifier descriptor
  80. ///////////////////////////////////////
  81. 0x0a,
  82. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  83. 0x00,
  84. 0x02,
  85. 0x00,
  86. 0x00,
  87. 0x00,
  88. 0x40,
  89. 0x00,
  90. 0x00,
  91. };
  92. static const char *string_descriptors[] = {
  93. (const char[]){ 0x09, 0x04 }, /* Langid */
  94. "CherryUSB", /* Manufacturer */
  95. "CherryUSB HID DEMO", /* Product */
  96. "2022123456", /* Serial Number */
  97. };
  98. static const uint8_t *device_descriptor_callback(uint8_t speed)
  99. {
  100. return device_descriptor;
  101. }
  102. static const uint8_t *config_descriptor_callback(uint8_t speed)
  103. {
  104. return config_descriptor;
  105. }
  106. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  107. {
  108. return device_quality_descriptor;
  109. }
  110. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  111. {
  112. if (index > 3) {
  113. return NULL;
  114. }
  115. return string_descriptors[index];
  116. }
  117. const struct usb_descriptor hid_descriptor = {
  118. .device_descriptor_callback = device_descriptor_callback,
  119. .config_descriptor_callback = config_descriptor_callback,
  120. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  121. .string_descriptor_callback = string_descriptor_callback
  122. };
  123. #else
  124. /*!< global descriptor */
  125. static const uint8_t hid_descriptor[] = {
  126. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  127. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  128. /************** Descriptor of Custom interface *****************/
  129. 0x09, /* bLength: Interface Descriptor size */
  130. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  131. 0x00, /* bInterfaceNumber: Number of Interface */
  132. 0x00, /* bAlternateSetting: Alternate setting */
  133. 0x02, /* bNumEndpoints */
  134. 0x03, /* bInterfaceClass: HID */
  135. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  136. 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  137. 0, /* iInterface: Index of string descriptor */
  138. /******************** Descriptor of Custom HID ********************/
  139. 0x09, /* bLength: HID Descriptor size */
  140. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  141. 0x11, /* bcdHID: HID Class Spec release number */
  142. 0x01,
  143. 0x00, /* bCountryCode: Hardware target country */
  144. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  145. 0x22, /* bDescriptorType */
  146. HID_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  147. 0x00,
  148. /******************** Descriptor of Custom in endpoint ********************/
  149. 0x07, /* bLength: Endpoint Descriptor size */
  150. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  151. HIDRAW_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */
  152. 0x03, /* bmAttributes: Interrupt endpoint */
  153. WBVAL(HIDRAW_IN_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
  154. HIDRAW_IN_INTERVAL, /* bInterval: Polling Interval */
  155. /******************** Descriptor of Custom out endpoint ********************/
  156. 0x07, /* bLength: Endpoint Descriptor size */
  157. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  158. HIDRAW_OUT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  159. 0x03, /* bmAttributes: Interrupt endpoint */
  160. WBVAL(HIDRAW_OUT_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
  161. HIDRAW_OUT_EP_INTERVAL, /* bInterval: Polling Interval */
  162. /* 73 */
  163. /*
  164. * string0 descriptor
  165. */
  166. USB_LANGID_INIT(USBD_LANGID_STRING),
  167. /*
  168. * string1 descriptor
  169. */
  170. 0x14, /* bLength */
  171. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  172. 'C', 0x00, /* wcChar0 */
  173. 'h', 0x00, /* wcChar1 */
  174. 'e', 0x00, /* wcChar2 */
  175. 'r', 0x00, /* wcChar3 */
  176. 'r', 0x00, /* wcChar4 */
  177. 'y', 0x00, /* wcChar5 */
  178. 'U', 0x00, /* wcChar6 */
  179. 'S', 0x00, /* wcChar7 */
  180. 'B', 0x00, /* wcChar8 */
  181. /*
  182. * string2 descriptor
  183. */
  184. 0x26, /* bLength */
  185. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  186. 'C', 0x00, /* wcChar0 */
  187. 'h', 0x00, /* wcChar1 */
  188. 'e', 0x00, /* wcChar2 */
  189. 'r', 0x00, /* wcChar3 */
  190. 'r', 0x00, /* wcChar4 */
  191. 'y', 0x00, /* wcChar5 */
  192. 'U', 0x00, /* wcChar6 */
  193. 'S', 0x00, /* wcChar7 */
  194. 'B', 0x00, /* wcChar8 */
  195. ' ', 0x00, /* wcChar9 */
  196. 'H', 0x00, /* wcChar10 */
  197. 'I', 0x00, /* wcChar11 */
  198. 'D', 0x00, /* wcChar12 */
  199. ' ', 0x00, /* wcChar13 */
  200. 'D', 0x00, /* wcChar14 */
  201. 'E', 0x00, /* wcChar15 */
  202. 'M', 0x00, /* wcChar16 */
  203. 'O', 0x00, /* wcChar17 */
  204. /*
  205. * string3 descriptor
  206. */
  207. 0x16, /* bLength */
  208. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  209. '2', 0x00, /* wcChar0 */
  210. '0', 0x00, /* wcChar1 */
  211. '2', 0x00, /* wcChar2 */
  212. '2', 0x00, /* wcChar3 */
  213. '1', 0x00, /* wcChar4 */
  214. '2', 0x00, /* wcChar5 */
  215. '3', 0x00, /* wcChar6 */
  216. '4', 0x00, /* wcChar7 */
  217. '5', 0x00, /* wcChar8 */
  218. '6', 0x00, /* wcChar9 */
  219. #ifdef CONFIG_USB_HS
  220. /*
  221. * device qualifier descriptor
  222. */
  223. 0x0a,
  224. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  225. 0x00,
  226. 0x02,
  227. 0x00,
  228. 0x00,
  229. 0x00,
  230. 0x40,
  231. 0x00,
  232. 0x00,
  233. #endif
  234. 0x00
  235. };
  236. #endif
  237. /*!< custom hid report descriptor */
  238. static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
  239. #ifdef CONFIG_USB_HS
  240. /* USER CODE BEGIN 0 */
  241. 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
  242. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  243. 0xa1, 0x01, /* COLLECTION (Application) */
  244. 0x85, 0x02, /* REPORT ID (0x02) */
  245. 0x09, 0x02, /* USAGE (Vendor Usage 1) */
  246. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  247. 0x25, 0xff, /*LOGICAL_MAXIMUM (255) */
  248. 0x75, 0x08, /* REPORT_SIZE (8) */
  249. 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */
  250. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  251. /* <___________________________________________________> */
  252. 0x85, 0x01, /* REPORT ID (0x01) */
  253. 0x09, 0x03, /* USAGE (Vendor Usage 1) */
  254. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  255. 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */
  256. 0x75, 0x08, /* REPORT_SIZE (8) */
  257. 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */
  258. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  259. /* USER CODE END 0 */
  260. 0xC0 /* END_COLLECTION */
  261. #else
  262. /* USER CODE BEGIN 0 */
  263. 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
  264. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  265. 0xa1, 0x01, /* COLLECTION (Application) */
  266. 0x85, 0x02, /* REPORT ID (0x02) */
  267. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  268. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  269. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  270. 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
  271. 0x75, 0x08, /* REPORT_SIZE (8) */
  272. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  273. /* <___________________________________________________> */
  274. 0x85, 0x01, /* REPORT ID (0x01) */
  275. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  276. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  277. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  278. 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
  279. 0x75, 0x08, /* REPORT_SIZE (8) */
  280. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  281. /* USER CODE END 0 */
  282. 0xC0 /* END_COLLECTION */
  283. #endif
  284. };
  285. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HIDRAW_OUT_EP_SIZE];
  286. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t send_buffer[HIDRAW_IN_EP_SIZE];
  287. #define HID_STATE_IDLE 0
  288. #define HID_STATE_BUSY 1
  289. /*!< hid state ! Data can be sent only when state is idle */
  290. static volatile uint8_t custom_state;
  291. static void usbd_event_handler(uint8_t busid, uint8_t event)
  292. {
  293. switch (event) {
  294. case USBD_EVENT_RESET:
  295. break;
  296. case USBD_EVENT_CONNECTED:
  297. break;
  298. case USBD_EVENT_DISCONNECTED:
  299. break;
  300. case USBD_EVENT_RESUME:
  301. break;
  302. case USBD_EVENT_SUSPEND:
  303. break;
  304. case USBD_EVENT_CONFIGURED:
  305. /* setup first out ep read transfer */
  306. usbd_ep_start_read(busid, HIDRAW_OUT_EP, read_buffer, HIDRAW_OUT_EP_SIZE);
  307. break;
  308. case USBD_EVENT_SET_REMOTE_WAKEUP:
  309. break;
  310. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. static void usbd_hid_custom_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  317. {
  318. (void)busid;
  319. (void)ep;
  320. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  321. custom_state = HID_STATE_IDLE;
  322. }
  323. static void usbd_hid_custom_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  324. {
  325. USB_LOG_RAW("actual out len:%d\r\n", nbytes);
  326. usbd_ep_start_read(busid, ep, read_buffer, HIDRAW_IN_EP_SIZE);
  327. read_buffer[0] = 0x02; /* IN: report id */
  328. usbd_ep_start_write(busid, HIDRAW_IN_EP, read_buffer, nbytes);
  329. }
  330. static struct usbd_endpoint custom_in_ep = {
  331. .ep_cb = usbd_hid_custom_in_callback,
  332. .ep_addr = HIDRAW_IN_EP
  333. };
  334. static struct usbd_endpoint custom_out_ep = {
  335. .ep_cb = usbd_hid_custom_out_callback,
  336. .ep_addr = HIDRAW_OUT_EP
  337. };
  338. /* function ------------------------------------------------------------------*/
  339. /**
  340. * @brief hid custom init
  341. * @pre none
  342. * @param[in] none
  343. * @retval none
  344. */
  345. struct usbd_interface intf0;
  346. void hid_custom_init(uint8_t busid, uintptr_t reg_base)
  347. {
  348. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  349. usbd_desc_register(busid, &hid_descriptor);
  350. #else
  351. usbd_desc_register(busid, hid_descriptor);
  352. #endif
  353. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE));
  354. usbd_add_endpoint(busid, &custom_in_ep);
  355. usbd_add_endpoint(busid, &custom_out_ep);
  356. usbd_initialize(busid, reg_base, usbd_event_handler);
  357. }