hid_keyboard_template.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_CONFIG_SIZE 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_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  23. HID_KEYBOARD_DESCRIPTOR_INIT(0x00, 0x01, HID_KEYBOARD_REPORT_DESC_SIZE, HID_INT_EP, HID_INT_EP_SIZE, HID_INT_EP_INTERVAL),
  24. };
  25. static const uint8_t device_quality_descriptor[] = {
  26. ///////////////////////////////////////
  27. /// device qualifier descriptor
  28. ///////////////////////////////////////
  29. 0x0a,
  30. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  31. 0x00,
  32. 0x02,
  33. 0x00,
  34. 0x00,
  35. 0x00,
  36. 0x40,
  37. 0x00,
  38. 0x00,
  39. };
  40. static const char *string_descriptors[] = {
  41. (const char[]){ 0x09, 0x04 }, /* Langid */
  42. "CherryUSB", /* Manufacturer */
  43. "CherryUSB HID DEMO", /* Product */
  44. "2022123456", /* Serial Number */
  45. };
  46. static const uint8_t *device_descriptor_callback(uint8_t speed)
  47. {
  48. return device_descriptor;
  49. }
  50. static const uint8_t *config_descriptor_callback(uint8_t speed)
  51. {
  52. return config_descriptor;
  53. }
  54. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  55. {
  56. return device_quality_descriptor;
  57. }
  58. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  59. {
  60. if (index > 3) {
  61. return NULL;
  62. }
  63. return string_descriptors[index];
  64. }
  65. const struct usb_descriptor hid_descriptor = {
  66. .device_descriptor_callback = device_descriptor_callback,
  67. .config_descriptor_callback = config_descriptor_callback,
  68. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  69. .string_descriptor_callback = string_descriptor_callback
  70. };
  71. #else
  72. static const uint8_t hid_descriptor[] = {
  73. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  74. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  75. HID_KEYBOARD_DESCRIPTOR_INIT(0x00, 0x01, HID_KEYBOARD_REPORT_DESC_SIZE, HID_INT_EP, HID_INT_EP_SIZE, HID_INT_EP_INTERVAL),
  76. ///////////////////////////////////////
  77. /// string0 descriptor
  78. ///////////////////////////////////////
  79. USB_LANGID_INIT(USBD_LANGID_STRING),
  80. ///////////////////////////////////////
  81. /// string1 descriptor
  82. ///////////////////////////////////////
  83. 0x14, /* bLength */
  84. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  85. 'C', 0x00, /* wcChar0 */
  86. 'h', 0x00, /* wcChar1 */
  87. 'e', 0x00, /* wcChar2 */
  88. 'r', 0x00, /* wcChar3 */
  89. 'r', 0x00, /* wcChar4 */
  90. 'y', 0x00, /* wcChar5 */
  91. 'U', 0x00, /* wcChar6 */
  92. 'S', 0x00, /* wcChar7 */
  93. 'B', 0x00, /* wcChar8 */
  94. ///////////////////////////////////////
  95. /// string2 descriptor
  96. ///////////////////////////////////////
  97. 0x26, /* bLength */
  98. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  99. 'C', 0x00, /* wcChar0 */
  100. 'h', 0x00, /* wcChar1 */
  101. 'e', 0x00, /* wcChar2 */
  102. 'r', 0x00, /* wcChar3 */
  103. 'r', 0x00, /* wcChar4 */
  104. 'y', 0x00, /* wcChar5 */
  105. 'U', 0x00, /* wcChar6 */
  106. 'S', 0x00, /* wcChar7 */
  107. 'B', 0x00, /* wcChar8 */
  108. ' ', 0x00, /* wcChar9 */
  109. 'H', 0x00, /* wcChar10 */
  110. 'I', 0x00, /* wcChar11 */
  111. 'D', 0x00, /* wcChar12 */
  112. ' ', 0x00, /* wcChar13 */
  113. 'D', 0x00, /* wcChar14 */
  114. 'E', 0x00, /* wcChar15 */
  115. 'M', 0x00, /* wcChar16 */
  116. 'O', 0x00, /* wcChar17 */
  117. ///////////////////////////////////////
  118. /// string3 descriptor
  119. ///////////////////////////////////////
  120. 0x16, /* bLength */
  121. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  122. '2', 0x00, /* wcChar0 */
  123. '0', 0x00, /* wcChar1 */
  124. '2', 0x00, /* wcChar2 */
  125. '2', 0x00, /* wcChar3 */
  126. '1', 0x00, /* wcChar4 */
  127. '2', 0x00, /* wcChar5 */
  128. '3', 0x00, /* wcChar6 */
  129. '4', 0x00, /* wcChar7 */
  130. '5', 0x00, /* wcChar8 */
  131. '6', 0x00, /* wcChar9 */
  132. #ifdef CONFIG_USB_HS
  133. ///////////////////////////////////////
  134. /// device qualifier descriptor
  135. ///////////////////////////////////////
  136. 0x0a,
  137. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  138. 0x00,
  139. 0x02,
  140. 0x00,
  141. 0x00,
  142. 0x00,
  143. 0x40,
  144. 0x00,
  145. 0x00,
  146. #endif
  147. 0x00
  148. };
  149. #endif
  150. /* USB HID device Configuration Descriptor */
  151. static uint8_t hid_desc[9] __ALIGN_END = {
  152. /* 18 */
  153. 0x09, /* bLength: HID Descriptor size */
  154. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  155. 0x11, /* bcdHID: HID Class Spec release number */
  156. 0x01,
  157. 0x00, /* bCountryCode: Hardware target country */
  158. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  159. 0x22, /* bDescriptorType */
  160. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  161. 0x00,
  162. };
  163. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  164. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  165. 0x09, 0x06, // USAGE (Keyboard)
  166. 0xa1, 0x01, // COLLECTION (Application)
  167. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  168. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  169. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  170. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  171. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  172. 0x75, 0x01, // REPORT_SIZE (1)
  173. 0x95, 0x08, // REPORT_COUNT (8)
  174. 0x81, 0x02, // INPUT (Data,Var,Abs)
  175. 0x95, 0x01, // REPORT_COUNT (1)
  176. 0x75, 0x08, // REPORT_SIZE (8)
  177. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  178. 0x95, 0x05, // REPORT_COUNT (5)
  179. 0x75, 0x01, // REPORT_SIZE (1)
  180. 0x05, 0x08, // USAGE_PAGE (LEDs)
  181. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  182. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  183. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  184. 0x95, 0x01, // REPORT_COUNT (1)
  185. 0x75, 0x03, // REPORT_SIZE (3)
  186. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  187. 0x95, 0x06, // REPORT_COUNT (6)
  188. 0x75, 0x08, // REPORT_SIZE (8)
  189. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  190. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  191. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  192. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  193. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  194. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  195. 0xc0 // END_COLLECTION
  196. };
  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 hid_state = HID_STATE_IDLE;
  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. hid_state = HID_STATE_IDLE;
  216. break;
  217. case USBD_EVENT_SET_REMOTE_WAKEUP:
  218. break;
  219. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  226. {
  227. hid_state = HID_STATE_IDLE;
  228. }
  229. static struct usbd_endpoint hid_in_ep = {
  230. .ep_cb = usbd_hid_int_callback,
  231. .ep_addr = HID_INT_EP
  232. };
  233. struct usbd_interface intf0;
  234. void hid_keyboard_init(uint8_t busid, uintptr_t reg_base)
  235. {
  236. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  237. usbd_desc_register(busid, &hid_descriptor);
  238. #else
  239. usbd_desc_register(busid, hid_descriptor);
  240. #endif
  241. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  242. usbd_add_endpoint(busid, &hid_in_ep);
  243. usbd_initialize(busid, reg_base, usbd_event_handler);
  244. }
  245. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
  246. void hid_keyboard_test(uint8_t busid)
  247. {
  248. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  249. if(usb_device_is_configured(busid) == false) {
  250. return;
  251. }
  252. memcpy(write_buffer, sendbuffer, 8);
  253. hid_state = HID_STATE_BUSY;
  254. usbd_ep_start_write(busid, HID_INT_EP, write_buffer, 8);
  255. while (hid_state == HID_STATE_BUSY) {
  256. }
  257. }