hid_keyboard_template.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_hid.h"
  8. #define USBD_VID 0xffff
  9. #define USBD_PID 0xffff
  10. #define USBD_MAX_POWER 100
  11. #define USBD_LANGID_STRING 1033
  12. #define HID_INT_EP 0x81
  13. #define HID_INT_EP_SIZE 8
  14. #define HID_INT_EP_INTERVAL 10
  15. #define USB_HID_CONFIG_DESC_SIZ 34
  16. #define HID_KEYBOARD_REPORT_DESC_SIZE 63
  17. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  18. static const uint8_t device_descriptor[] = {
  19. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01)
  20. };
  21. static const uint8_t config_descriptor[] = {
  22. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  23. /************** Descriptor of Joystick Mouse interface ****************/
  24. /* 09 */
  25. 0x09, /* bLength: Interface Descriptor size */
  26. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  27. 0x00, /* bInterfaceNumber: Number of Interface */
  28. 0x00, /* bAlternateSetting: Alternate setting */
  29. 0x01, /* bNumEndpoints */
  30. 0x03, /* bInterfaceClass: HID */
  31. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  32. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  33. 0, /* iInterface: Index of string descriptor */
  34. /******************** Descriptor of Joystick Mouse HID ********************/
  35. /* 18 */
  36. 0x09, /* bLength: HID Descriptor size */
  37. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  38. 0x11, /* bcdHID: HID Class Spec release number */
  39. 0x01,
  40. 0x00, /* bCountryCode: Hardware target country */
  41. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  42. 0x22, /* bDescriptorType */
  43. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  44. 0x00,
  45. /******************** Descriptor of Mouse endpoint ********************/
  46. /* 27 */
  47. 0x07, /* bLength: Endpoint Descriptor size */
  48. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  49. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  50. 0x03, /* bmAttributes: Interrupt endpoint */
  51. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  52. 0x00,
  53. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  54. /* 34 */
  55. };
  56. static const uint8_t device_quality_descriptor[] = {
  57. ///////////////////////////////////////
  58. /// device qualifier descriptor
  59. ///////////////////////////////////////
  60. 0x0a,
  61. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  62. 0x00,
  63. 0x02,
  64. 0x00,
  65. 0x00,
  66. 0x00,
  67. 0x40,
  68. 0x00,
  69. 0x00,
  70. };
  71. static const char *string_descriptors[] = {
  72. (const char[]){ 0x09, 0x04 }, /* Langid */
  73. "CherryUSB", /* Manufacturer */
  74. "CherryUSB HID DEMO", /* Product */
  75. "2022123456", /* Serial Number */
  76. };
  77. static const uint8_t *device_descriptor_callback(uint8_t speed)
  78. {
  79. return device_descriptor;
  80. }
  81. static const uint8_t *config_descriptor_callback(uint8_t speed)
  82. {
  83. return config_descriptor;
  84. }
  85. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  86. {
  87. return device_quality_descriptor;
  88. }
  89. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  90. {
  91. if (index > 3) {
  92. return NULL;
  93. }
  94. return string_descriptors[index];
  95. }
  96. const struct usb_descriptor hid_descriptor = {
  97. .device_descriptor_callback = device_descriptor_callback,
  98. .config_descriptor_callback = config_descriptor_callback,
  99. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  100. .string_descriptor_callback = string_descriptor_callback
  101. };
  102. #else
  103. static const uint8_t hid_descriptor[] = {
  104. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  105. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  106. /************** Descriptor of Joystick Mouse interface ****************/
  107. /* 09 */
  108. 0x09, /* bLength: Interface Descriptor size */
  109. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  110. 0x00, /* bInterfaceNumber: Number of Interface */
  111. 0x00, /* bAlternateSetting: Alternate setting */
  112. 0x01, /* bNumEndpoints */
  113. 0x03, /* bInterfaceClass: HID */
  114. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  115. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  116. 0, /* iInterface: Index of string descriptor */
  117. /******************** Descriptor of Joystick Mouse HID ********************/
  118. /* 18 */
  119. 0x09, /* bLength: HID Descriptor size */
  120. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  121. 0x11, /* bcdHID: HID Class Spec release number */
  122. 0x01,
  123. 0x00, /* bCountryCode: Hardware target country */
  124. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  125. 0x22, /* bDescriptorType */
  126. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  127. 0x00,
  128. /******************** Descriptor of Mouse endpoint ********************/
  129. /* 27 */
  130. 0x07, /* bLength: Endpoint Descriptor size */
  131. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  132. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  133. 0x03, /* bmAttributes: Interrupt endpoint */
  134. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  135. 0x00,
  136. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  137. /* 34 */
  138. ///////////////////////////////////////
  139. /// string0 descriptor
  140. ///////////////////////////////////////
  141. USB_LANGID_INIT(USBD_LANGID_STRING),
  142. ///////////////////////////////////////
  143. /// string1 descriptor
  144. ///////////////////////////////////////
  145. 0x14, /* bLength */
  146. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  147. 'C', 0x00, /* wcChar0 */
  148. 'h', 0x00, /* wcChar1 */
  149. 'e', 0x00, /* wcChar2 */
  150. 'r', 0x00, /* wcChar3 */
  151. 'r', 0x00, /* wcChar4 */
  152. 'y', 0x00, /* wcChar5 */
  153. 'U', 0x00, /* wcChar6 */
  154. 'S', 0x00, /* wcChar7 */
  155. 'B', 0x00, /* wcChar8 */
  156. ///////////////////////////////////////
  157. /// string2 descriptor
  158. ///////////////////////////////////////
  159. 0x26, /* bLength */
  160. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  161. 'C', 0x00, /* wcChar0 */
  162. 'h', 0x00, /* wcChar1 */
  163. 'e', 0x00, /* wcChar2 */
  164. 'r', 0x00, /* wcChar3 */
  165. 'r', 0x00, /* wcChar4 */
  166. 'y', 0x00, /* wcChar5 */
  167. 'U', 0x00, /* wcChar6 */
  168. 'S', 0x00, /* wcChar7 */
  169. 'B', 0x00, /* wcChar8 */
  170. ' ', 0x00, /* wcChar9 */
  171. 'H', 0x00, /* wcChar10 */
  172. 'I', 0x00, /* wcChar11 */
  173. 'D', 0x00, /* wcChar12 */
  174. ' ', 0x00, /* wcChar13 */
  175. 'D', 0x00, /* wcChar14 */
  176. 'E', 0x00, /* wcChar15 */
  177. 'M', 0x00, /* wcChar16 */
  178. 'O', 0x00, /* wcChar17 */
  179. ///////////////////////////////////////
  180. /// string3 descriptor
  181. ///////////////////////////////////////
  182. 0x16, /* bLength */
  183. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  184. '2', 0x00, /* wcChar0 */
  185. '0', 0x00, /* wcChar1 */
  186. '2', 0x00, /* wcChar2 */
  187. '2', 0x00, /* wcChar3 */
  188. '1', 0x00, /* wcChar4 */
  189. '2', 0x00, /* wcChar5 */
  190. '3', 0x00, /* wcChar6 */
  191. '4', 0x00, /* wcChar7 */
  192. '5', 0x00, /* wcChar8 */
  193. '6', 0x00, /* wcChar9 */
  194. #ifdef CONFIG_USB_HS
  195. ///////////////////////////////////////
  196. /// device qualifier descriptor
  197. ///////////////////////////////////////
  198. 0x0a,
  199. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  200. 0x00,
  201. 0x02,
  202. 0x00,
  203. 0x00,
  204. 0x00,
  205. 0x40,
  206. 0x00,
  207. 0x00,
  208. #endif
  209. 0x00
  210. };
  211. #endif
  212. /* USB HID device Configuration Descriptor */
  213. static uint8_t hid_desc[9] __ALIGN_END = {
  214. /* 18 */
  215. 0x09, /* bLength: HID Descriptor size */
  216. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  217. 0x11, /* bcdHID: HID Class Spec release number */
  218. 0x01,
  219. 0x00, /* bCountryCode: Hardware target country */
  220. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  221. 0x22, /* bDescriptorType */
  222. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  223. 0x00,
  224. };
  225. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  226. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  227. 0x09, 0x06, // USAGE (Keyboard)
  228. 0xa1, 0x01, // COLLECTION (Application)
  229. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  230. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  231. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  232. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  233. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  234. 0x75, 0x01, // REPORT_SIZE (1)
  235. 0x95, 0x08, // REPORT_COUNT (8)
  236. 0x81, 0x02, // INPUT (Data,Var,Abs)
  237. 0x95, 0x01, // REPORT_COUNT (1)
  238. 0x75, 0x08, // REPORT_SIZE (8)
  239. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  240. 0x95, 0x05, // REPORT_COUNT (5)
  241. 0x75, 0x01, // REPORT_SIZE (1)
  242. 0x05, 0x08, // USAGE_PAGE (LEDs)
  243. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  244. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  245. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  246. 0x95, 0x01, // REPORT_COUNT (1)
  247. 0x75, 0x03, // REPORT_SIZE (3)
  248. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  249. 0x95, 0x06, // REPORT_COUNT (6)
  250. 0x75, 0x08, // REPORT_SIZE (8)
  251. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  252. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  253. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  254. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  255. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  256. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  257. 0xc0 // END_COLLECTION
  258. };
  259. #define HID_STATE_IDLE 0
  260. #define HID_STATE_BUSY 1
  261. /*!< hid state ! Data can be sent only when state is idle */
  262. static volatile uint8_t hid_state = HID_STATE_IDLE;
  263. static void usbd_event_handler(uint8_t busid, uint8_t event)
  264. {
  265. switch (event) {
  266. case USBD_EVENT_RESET:
  267. break;
  268. case USBD_EVENT_CONNECTED:
  269. break;
  270. case USBD_EVENT_DISCONNECTED:
  271. break;
  272. case USBD_EVENT_RESUME:
  273. break;
  274. case USBD_EVENT_SUSPEND:
  275. break;
  276. case USBD_EVENT_CONFIGURED:
  277. hid_state = HID_STATE_IDLE;
  278. break;
  279. case USBD_EVENT_SET_REMOTE_WAKEUP:
  280. break;
  281. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  288. {
  289. hid_state = HID_STATE_IDLE;
  290. }
  291. static struct usbd_endpoint hid_in_ep = {
  292. .ep_cb = usbd_hid_int_callback,
  293. .ep_addr = HID_INT_EP
  294. };
  295. struct usbd_interface intf0;
  296. void hid_keyboard_init(uint8_t busid, uintptr_t reg_base)
  297. {
  298. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  299. usbd_desc_register(busid, &hid_descriptor);
  300. #else
  301. usbd_desc_register(busid, hid_descriptor);
  302. #endif
  303. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  304. usbd_add_endpoint(busid, &hid_in_ep);
  305. usbd_initialize(busid, reg_base, usbd_event_handler);
  306. }
  307. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
  308. void hid_keyboard_test(uint8_t busid)
  309. {
  310. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  311. if(usb_device_is_configured(busid) == false) {
  312. return;
  313. }
  314. memcpy(write_buffer, sendbuffer, 8);
  315. hid_state = HID_STATE_BUSY;
  316. usbd_ep_start_write(busid, HID_INT_EP, write_buffer, 8);
  317. while (hid_state == HID_STATE_BUSY) {
  318. }
  319. }