hid_keyboard_template.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. static const uint8_t hid_descriptor[] = {
  18. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  19. USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  20. /************** Descriptor of Joystick Mouse interface ****************/
  21. /* 09 */
  22. 0x09, /* bLength: Interface Descriptor size */
  23. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  24. 0x00, /* bInterfaceNumber: Number of Interface */
  25. 0x00, /* bAlternateSetting: Alternate setting */
  26. 0x01, /* bNumEndpoints */
  27. 0x03, /* bInterfaceClass: HID */
  28. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  29. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  30. 0, /* iInterface: Index of string descriptor */
  31. /******************** Descriptor of Joystick Mouse HID ********************/
  32. /* 18 */
  33. 0x09, /* bLength: HID Descriptor size */
  34. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  35. 0x11, /* bcdHID: HID Class Spec release number */
  36. 0x01,
  37. 0x00, /* bCountryCode: Hardware target country */
  38. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  39. 0x22, /* bDescriptorType */
  40. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  41. 0x00,
  42. /******************** Descriptor of Mouse endpoint ********************/
  43. /* 27 */
  44. 0x07, /* bLength: Endpoint Descriptor size */
  45. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  46. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  47. 0x03, /* bmAttributes: Interrupt endpoint */
  48. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  49. 0x00,
  50. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  51. /* 34 */
  52. ///////////////////////////////////////
  53. /// string0 descriptor
  54. ///////////////////////////////////////
  55. USB_LANGID_INIT(USBD_LANGID_STRING),
  56. ///////////////////////////////////////
  57. /// string1 descriptor
  58. ///////////////////////////////////////
  59. 0x14, /* bLength */
  60. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  61. 'C', 0x00, /* wcChar0 */
  62. 'h', 0x00, /* wcChar1 */
  63. 'e', 0x00, /* wcChar2 */
  64. 'r', 0x00, /* wcChar3 */
  65. 'r', 0x00, /* wcChar4 */
  66. 'y', 0x00, /* wcChar5 */
  67. 'U', 0x00, /* wcChar6 */
  68. 'S', 0x00, /* wcChar7 */
  69. 'B', 0x00, /* wcChar8 */
  70. ///////////////////////////////////////
  71. /// string2 descriptor
  72. ///////////////////////////////////////
  73. 0x26, /* bLength */
  74. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  75. 'C', 0x00, /* wcChar0 */
  76. 'h', 0x00, /* wcChar1 */
  77. 'e', 0x00, /* wcChar2 */
  78. 'r', 0x00, /* wcChar3 */
  79. 'r', 0x00, /* wcChar4 */
  80. 'y', 0x00, /* wcChar5 */
  81. 'U', 0x00, /* wcChar6 */
  82. 'S', 0x00, /* wcChar7 */
  83. 'B', 0x00, /* wcChar8 */
  84. ' ', 0x00, /* wcChar9 */
  85. 'H', 0x00, /* wcChar10 */
  86. 'I', 0x00, /* wcChar11 */
  87. 'D', 0x00, /* wcChar12 */
  88. ' ', 0x00, /* wcChar13 */
  89. 'D', 0x00, /* wcChar14 */
  90. 'E', 0x00, /* wcChar15 */
  91. 'M', 0x00, /* wcChar16 */
  92. 'O', 0x00, /* wcChar17 */
  93. ///////////////////////////////////////
  94. /// string3 descriptor
  95. ///////////////////////////////////////
  96. 0x16, /* bLength */
  97. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  98. '2', 0x00, /* wcChar0 */
  99. '0', 0x00, /* wcChar1 */
  100. '2', 0x00, /* wcChar2 */
  101. '2', 0x00, /* wcChar3 */
  102. '1', 0x00, /* wcChar4 */
  103. '2', 0x00, /* wcChar5 */
  104. '3', 0x00, /* wcChar6 */
  105. '4', 0x00, /* wcChar7 */
  106. '5', 0x00, /* wcChar8 */
  107. '6', 0x00, /* wcChar9 */
  108. #ifdef CONFIG_USB_HS
  109. ///////////////////////////////////////
  110. /// device qualifier descriptor
  111. ///////////////////////////////////////
  112. 0x0a,
  113. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  114. 0x00,
  115. 0x02,
  116. 0x00,
  117. 0x00,
  118. 0x00,
  119. 0x40,
  120. 0x01,
  121. 0x00,
  122. #endif
  123. 0x00
  124. };
  125. /* USB HID device Configuration Descriptor */
  126. static uint8_t hid_desc[9] __ALIGN_END = {
  127. /* 18 */
  128. 0x09, /* bLength: HID Descriptor size */
  129. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  130. 0x11, /* bcdHID: HID Class Spec release number */
  131. 0x01,
  132. 0x00, /* bCountryCode: Hardware target country */
  133. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  134. 0x22, /* bDescriptorType */
  135. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  136. 0x00,
  137. };
  138. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  139. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  140. 0x09, 0x06, // USAGE (Keyboard)
  141. 0xa1, 0x01, // COLLECTION (Application)
  142. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  143. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  144. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  145. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  146. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  147. 0x75, 0x01, // REPORT_SIZE (1)
  148. 0x95, 0x08, // REPORT_COUNT (8)
  149. 0x81, 0x02, // INPUT (Data,Var,Abs)
  150. 0x95, 0x01, // REPORT_COUNT (1)
  151. 0x75, 0x08, // REPORT_SIZE (8)
  152. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  153. 0x95, 0x05, // REPORT_COUNT (5)
  154. 0x75, 0x01, // REPORT_SIZE (1)
  155. 0x05, 0x08, // USAGE_PAGE (LEDs)
  156. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  157. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  158. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  159. 0x95, 0x01, // REPORT_COUNT (1)
  160. 0x75, 0x03, // REPORT_SIZE (3)
  161. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  162. 0x95, 0x06, // REPORT_COUNT (6)
  163. 0x75, 0x08, // REPORT_SIZE (8)
  164. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  165. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  166. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  167. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  168. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  169. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  170. 0xc0 // END_COLLECTION
  171. };
  172. #define HID_STATE_IDLE 0
  173. #define HID_STATE_BUSY 1
  174. /*!< hid state ! Data can be sent only when state is idle */
  175. static volatile uint8_t hid_state = HID_STATE_IDLE;
  176. static void usbd_event_handler(uint8_t busid, uint8_t event)
  177. {
  178. switch (event) {
  179. case USBD_EVENT_RESET:
  180. break;
  181. case USBD_EVENT_CONNECTED:
  182. break;
  183. case USBD_EVENT_DISCONNECTED:
  184. break;
  185. case USBD_EVENT_RESUME:
  186. break;
  187. case USBD_EVENT_SUSPEND:
  188. break;
  189. case USBD_EVENT_CONFIGURED:
  190. hid_state = HID_STATE_IDLE;
  191. break;
  192. case USBD_EVENT_SET_REMOTE_WAKEUP:
  193. break;
  194. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  201. {
  202. hid_state = HID_STATE_IDLE;
  203. }
  204. static struct usbd_endpoint hid_in_ep = {
  205. .ep_cb = usbd_hid_int_callback,
  206. .ep_addr = HID_INT_EP
  207. };
  208. struct usbd_interface intf0;
  209. void hid_keyboard_init(uint8_t busid, uint32_t reg_base)
  210. {
  211. usbd_desc_register(busid, hid_descriptor);
  212. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  213. usbd_add_endpoint(busid, &hid_in_ep);
  214. usbd_initialize(busid, reg_base, usbd_event_handler);
  215. }
  216. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
  217. void hid_keyboard_test(uint8_t busid)
  218. {
  219. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  220. memcpy(write_buffer, sendbuffer, 8);
  221. int ret = usbd_ep_start_write(busid, HID_INT_EP, write_buffer, 8);
  222. if (ret < 0) {
  223. return;
  224. }
  225. hid_state = HID_STATE_BUSY;
  226. while (hid_state == HID_STATE_BUSY) {
  227. }
  228. }