hid_custom_inout_template.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /*!< global descriptor */
  36. static const uint8_t hid_descriptor[] = {
  37. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  38. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  39. /************** Descriptor of Custom interface *****************/
  40. 0x09, /* bLength: Interface Descriptor size */
  41. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  42. 0x00, /* bInterfaceNumber: Number of Interface */
  43. 0x00, /* bAlternateSetting: Alternate setting */
  44. 0x02, /* bNumEndpoints */
  45. 0x03, /* bInterfaceClass: HID */
  46. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  47. 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  48. 0, /* iInterface: Index of string descriptor */
  49. /******************** Descriptor of Custom HID ********************/
  50. 0x09, /* bLength: HID Descriptor size */
  51. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  52. 0x11, /* bcdHID: HID Class Spec release number */
  53. 0x01,
  54. 0x00, /* bCountryCode: Hardware target country */
  55. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  56. 0x22, /* bDescriptorType */
  57. HID_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  58. 0x00,
  59. /******************** Descriptor of Custom in endpoint ********************/
  60. 0x07, /* bLength: Endpoint Descriptor size */
  61. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  62. HIDRAW_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */
  63. 0x03, /* bmAttributes: Interrupt endpoint */
  64. WBVAL(HIDRAW_IN_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
  65. HIDRAW_IN_INTERVAL, /* bInterval: Polling Interval */
  66. /******************** Descriptor of Custom out endpoint ********************/
  67. 0x07, /* bLength: Endpoint Descriptor size */
  68. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  69. HIDRAW_OUT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  70. 0x03, /* bmAttributes: Interrupt endpoint */
  71. WBVAL(HIDRAW_OUT_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
  72. HIDRAW_OUT_EP_INTERVAL, /* bInterval: Polling Interval */
  73. /* 73 */
  74. /*
  75. * string0 descriptor
  76. */
  77. USB_LANGID_INIT(USBD_LANGID_STRING),
  78. /*
  79. * string1 descriptor
  80. */
  81. 0x14, /* bLength */
  82. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  83. 'C', 0x00, /* wcChar0 */
  84. 'h', 0x00, /* wcChar1 */
  85. 'e', 0x00, /* wcChar2 */
  86. 'r', 0x00, /* wcChar3 */
  87. 'r', 0x00, /* wcChar4 */
  88. 'y', 0x00, /* wcChar5 */
  89. 'U', 0x00, /* wcChar6 */
  90. 'S', 0x00, /* wcChar7 */
  91. 'B', 0x00, /* wcChar8 */
  92. /*
  93. * string2 descriptor
  94. */
  95. 0x26, /* bLength */
  96. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  97. 'C', 0x00, /* wcChar0 */
  98. 'h', 0x00, /* wcChar1 */
  99. 'e', 0x00, /* wcChar2 */
  100. 'r', 0x00, /* wcChar3 */
  101. 'r', 0x00, /* wcChar4 */
  102. 'y', 0x00, /* wcChar5 */
  103. 'U', 0x00, /* wcChar6 */
  104. 'S', 0x00, /* wcChar7 */
  105. 'B', 0x00, /* wcChar8 */
  106. ' ', 0x00, /* wcChar9 */
  107. 'H', 0x00, /* wcChar10 */
  108. 'I', 0x00, /* wcChar11 */
  109. 'D', 0x00, /* wcChar12 */
  110. ' ', 0x00, /* wcChar13 */
  111. 'D', 0x00, /* wcChar14 */
  112. 'E', 0x00, /* wcChar15 */
  113. 'M', 0x00, /* wcChar16 */
  114. 'O', 0x00, /* wcChar17 */
  115. /*
  116. * string3 descriptor
  117. */
  118. 0x16, /* bLength */
  119. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  120. '2', 0x00, /* wcChar0 */
  121. '0', 0x00, /* wcChar1 */
  122. '2', 0x00, /* wcChar2 */
  123. '2', 0x00, /* wcChar3 */
  124. '1', 0x00, /* wcChar4 */
  125. '2', 0x00, /* wcChar5 */
  126. '3', 0x00, /* wcChar6 */
  127. '4', 0x00, /* wcChar7 */
  128. '5', 0x00, /* wcChar8 */
  129. '6', 0x00, /* wcChar9 */
  130. #ifdef CONFIG_USB_HS
  131. /*
  132. * device qualifier descriptor
  133. */
  134. 0x0a,
  135. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  136. 0x00,
  137. 0x02,
  138. 0x00,
  139. 0x00,
  140. 0x00,
  141. 0x40,
  142. 0x00,
  143. 0x00,
  144. #endif
  145. 0x00
  146. };
  147. /*!< custom hid report descriptor */
  148. static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
  149. #ifdef CONFIG_USB_HS
  150. /* USER CODE BEGIN 0 */
  151. 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
  152. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  153. 0xa1, 0x01, /* COLLECTION (Application) */
  154. 0x85, 0x02, /* REPORT ID (0x02) */
  155. 0x09, 0x02, /* USAGE (Vendor Usage 1) */
  156. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  157. 0x25, 0xff, /*LOGICAL_MAXIMUM (255) */
  158. 0x75, 0x08, /* REPORT_SIZE (8) */
  159. 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */
  160. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  161. /* <___________________________________________________> */
  162. 0x85, 0x01, /* REPORT ID (0x01) */
  163. 0x09, 0x03, /* USAGE (Vendor Usage 1) */
  164. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  165. 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */
  166. 0x75, 0x08, /* REPORT_SIZE (8) */
  167. 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */
  168. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  169. /* USER CODE END 0 */
  170. 0xC0 /* END_COLLECTION */
  171. #else
  172. /* USER CODE BEGIN 0 */
  173. 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
  174. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  175. 0xa1, 0x01, /* COLLECTION (Application) */
  176. 0x85, 0x02, /* REPORT ID (0x02) */
  177. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  178. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  179. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  180. 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
  181. 0x75, 0x08, /* REPORT_SIZE (8) */
  182. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  183. /* <___________________________________________________> */
  184. 0x85, 0x01, /* REPORT ID (0x01) */
  185. 0x09, 0x01, /* USAGE (Vendor Usage 1) */
  186. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  187. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  188. 0x95, 0x40 - 1, /* REPORT_COUNT (63) */
  189. 0x75, 0x08, /* REPORT_SIZE (8) */
  190. 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
  191. /* USER CODE END 0 */
  192. 0xC0 /* END_COLLECTION */
  193. #endif
  194. };
  195. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HIDRAW_OUT_EP_SIZE];
  196. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t send_buffer[HIDRAW_IN_EP_SIZE];
  197. #define HID_STATE_IDLE 0
  198. #define HID_STATE_BUSY 1
  199. /*!< hid state ! Data can be sent only when state is idle */
  200. static volatile uint8_t custom_state;
  201. static void usbd_event_handler(uint8_t busid, uint8_t event)
  202. {
  203. switch (event) {
  204. case USBD_EVENT_RESET:
  205. break;
  206. case USBD_EVENT_CONNECTED:
  207. break;
  208. case USBD_EVENT_DISCONNECTED:
  209. break;
  210. case USBD_EVENT_RESUME:
  211. break;
  212. case USBD_EVENT_SUSPEND:
  213. break;
  214. case USBD_EVENT_CONFIGURED:
  215. /* setup first out ep read transfer */
  216. usbd_ep_start_read(busid, HIDRAW_OUT_EP, read_buffer, HIDRAW_OUT_EP_SIZE);
  217. break;
  218. case USBD_EVENT_SET_REMOTE_WAKEUP:
  219. break;
  220. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. static void usbd_hid_custom_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  227. {
  228. (void)busid;
  229. (void)ep;
  230. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  231. custom_state = HID_STATE_IDLE;
  232. }
  233. static void usbd_hid_custom_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  234. {
  235. USB_LOG_RAW("actual out len:%d\r\n", nbytes);
  236. usbd_ep_start_read(busid, ep, read_buffer, HIDRAW_IN_EP_SIZE);
  237. read_buffer[0] = 0x02; /* IN: report id */
  238. usbd_ep_start_write(busid, HIDRAW_IN_EP, read_buffer, nbytes);
  239. }
  240. static struct usbd_endpoint custom_in_ep = {
  241. .ep_cb = usbd_hid_custom_in_callback,
  242. .ep_addr = HIDRAW_IN_EP
  243. };
  244. static struct usbd_endpoint custom_out_ep = {
  245. .ep_cb = usbd_hid_custom_out_callback,
  246. .ep_addr = HIDRAW_OUT_EP
  247. };
  248. /* function ------------------------------------------------------------------*/
  249. /**
  250. * @brief hid custom init
  251. * @pre none
  252. * @param[in] none
  253. * @retval none
  254. */
  255. struct usbd_interface intf0;
  256. void hid_custom_init(uint8_t busid, uintptr_t reg_base)
  257. {
  258. usbd_desc_register(busid, hid_descriptor);
  259. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE));
  260. usbd_add_endpoint(busid, &custom_in_ep);
  261. usbd_add_endpoint(busid, &custom_out_ep);
  262. usbd_initialize(busid, reg_base, usbd_event_handler);
  263. }