hid_keyboard_template.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. static const uint8_t device_descriptor[] = {
  18. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01)
  19. };
  20. static const uint8_t config_descriptor[] = {
  21. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  22. HID_KEYBOARD_DESCRIPTOR_INIT(0x00, 0x01, HID_KEYBOARD_REPORT_DESC_SIZE, HID_INT_EP, HID_INT_EP_SIZE, HID_INT_EP_INTERVAL),
  23. };
  24. static const uint8_t device_quality_descriptor[] = {
  25. ///////////////////////////////////////
  26. /// device qualifier descriptor
  27. ///////////////////////////////////////
  28. 0x0a,
  29. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  30. 0x00,
  31. 0x02,
  32. 0x00,
  33. 0x00,
  34. 0x00,
  35. 0x40,
  36. 0x00,
  37. 0x00,
  38. };
  39. static const char *string_descriptors[] = {
  40. (const char[]){ 0x09, 0x04 }, /* Langid */
  41. "CherryUSB", /* Manufacturer */
  42. "CherryUSB HID DEMO", /* Product */
  43. "2022123456", /* Serial Number */
  44. };
  45. static const uint8_t *device_descriptor_callback(uint8_t speed)
  46. {
  47. return device_descriptor;
  48. }
  49. static const uint8_t *config_descriptor_callback(uint8_t speed)
  50. {
  51. return config_descriptor;
  52. }
  53. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  54. {
  55. return device_quality_descriptor;
  56. }
  57. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  58. {
  59. if (index > 3) {
  60. return NULL;
  61. }
  62. return string_descriptors[index];
  63. }
  64. const struct usb_descriptor hid_descriptor = {
  65. .device_descriptor_callback = device_descriptor_callback,
  66. .config_descriptor_callback = config_descriptor_callback,
  67. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  68. .string_descriptor_callback = string_descriptor_callback
  69. };
  70. /* USB HID device Configuration Descriptor */
  71. static uint8_t hid_desc[9] __ALIGN_END = {
  72. /* 18 */
  73. 0x09, /* bLength: HID Descriptor size */
  74. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  75. 0x11, /* bcdHID: HID Class Spec release number */
  76. 0x01,
  77. 0x00, /* bCountryCode: Hardware target country */
  78. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  79. 0x22, /* bDescriptorType */
  80. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  81. 0x00,
  82. };
  83. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  84. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  85. 0x09, 0x06, // USAGE (Keyboard)
  86. 0xa1, 0x01, // COLLECTION (Application)
  87. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  88. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  89. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  90. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  91. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  92. 0x75, 0x01, // REPORT_SIZE (1)
  93. 0x95, 0x08, // REPORT_COUNT (8)
  94. 0x81, 0x02, // INPUT (Data,Var,Abs)
  95. 0x95, 0x01, // REPORT_COUNT (1)
  96. 0x75, 0x08, // REPORT_SIZE (8)
  97. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  98. 0x95, 0x05, // REPORT_COUNT (5)
  99. 0x75, 0x01, // REPORT_SIZE (1)
  100. 0x05, 0x08, // USAGE_PAGE (LEDs)
  101. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  102. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  103. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  104. 0x95, 0x01, // REPORT_COUNT (1)
  105. 0x75, 0x03, // REPORT_SIZE (3)
  106. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  107. 0x95, 0x06, // REPORT_COUNT (6)
  108. 0x75, 0x08, // REPORT_SIZE (8)
  109. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  110. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  111. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  112. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  113. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  114. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  115. 0xc0 // END_COLLECTION
  116. };
  117. #define HID_STATE_IDLE 0
  118. #define HID_STATE_BUSY 1
  119. /*!< hid state ! Data can be sent only when state is idle */
  120. static volatile uint8_t hid_state = HID_STATE_IDLE;
  121. static void usbd_event_handler(uint8_t busid, uint8_t event)
  122. {
  123. switch (event) {
  124. case USBD_EVENT_RESET:
  125. break;
  126. case USBD_EVENT_CONNECTED:
  127. break;
  128. case USBD_EVENT_DISCONNECTED:
  129. break;
  130. case USBD_EVENT_RESUME:
  131. break;
  132. case USBD_EVENT_SUSPEND:
  133. break;
  134. case USBD_EVENT_CONFIGURED:
  135. hid_state = HID_STATE_IDLE;
  136. break;
  137. case USBD_EVENT_SET_REMOTE_WAKEUP:
  138. break;
  139. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  146. {
  147. hid_state = HID_STATE_IDLE;
  148. }
  149. static struct usbd_endpoint hid_in_ep = {
  150. .ep_cb = usbd_hid_int_callback,
  151. .ep_addr = HID_INT_EP
  152. };
  153. struct usbd_interface intf0;
  154. void hid_keyboard_init(uint8_t busid, uintptr_t reg_base)
  155. {
  156. usbd_desc_register(busid, &hid_descriptor);
  157. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  158. usbd_add_endpoint(busid, &hid_in_ep);
  159. usbd_initialize(busid, reg_base, usbd_event_handler);
  160. }
  161. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
  162. void hid_keyboard_test(uint8_t busid)
  163. {
  164. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  165. if(usb_device_is_configured(busid) == false) {
  166. return;
  167. }
  168. memcpy(write_buffer, sendbuffer, 8);
  169. hid_state = HID_STATE_BUSY;
  170. usbd_ep_start_write(busid, HID_INT_EP, write_buffer, 8);
  171. while (hid_state == HID_STATE_BUSY) {
  172. }
  173. }