hid_mouse_template.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "usbd_core.h"
  2. #include "usbd_hid.h"
  3. #define HID_STATE_IDLE 0
  4. #define HID_STATE_BUSY 1
  5. /*!< endpoint address */
  6. #define HID_INT_EP 0x81
  7. #define HID_INT_EP_SIZE 8
  8. #define HID_INT_EP_INTERVAL 10
  9. #define USBD_VID 0xffff
  10. #define USBD_PID 0xffff
  11. #define USBD_MAX_POWER 100
  12. #define USBD_LANGID_STRING 1033
  13. /*!< hid state ! Data can be sent only when state is idle */
  14. uint8_t hid_state = HID_STATE_IDLE;
  15. /*!< config descriptor size */
  16. #define USB_HID_CONFIG_DESC_SIZ 34
  17. /*!< report descriptor size */
  18. #define HID_MOUSE_REPORT_DESC_SIZE 74
  19. /*!< global descriptor */
  20. const uint8_t hid_descriptor[] = {
  21. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01),
  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. 0x02, /* 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_MOUSE_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. /// string0 descriptor
  57. ///////////////////////////////////////
  58. USB_LANGID_INIT(USBD_LANGID_STRING),
  59. ///////////////////////////////////////
  60. /// string1 descriptor
  61. ///////////////////////////////////////
  62. 0x14, /* bLength */
  63. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  64. 'C', 0x00, /* wcChar0 */
  65. 'h', 0x00, /* wcChar1 */
  66. 'e', 0x00, /* wcChar2 */
  67. 'r', 0x00, /* wcChar3 */
  68. 'r', 0x00, /* wcChar4 */
  69. 'y', 0x00, /* wcChar5 */
  70. 'U', 0x00, /* wcChar6 */
  71. 'S', 0x00, /* wcChar7 */
  72. 'B', 0x00, /* wcChar8 */
  73. ///////////////////////////////////////
  74. /// string2 descriptor
  75. ///////////////////////////////////////
  76. 0x26, /* bLength */
  77. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  78. 'C', 0x00, /* wcChar0 */
  79. 'h', 0x00, /* wcChar1 */
  80. 'e', 0x00, /* wcChar2 */
  81. 'r', 0x00, /* wcChar3 */
  82. 'r', 0x00, /* wcChar4 */
  83. 'y', 0x00, /* wcChar5 */
  84. 'U', 0x00, /* wcChar6 */
  85. 'S', 0x00, /* wcChar7 */
  86. 'B', 0x00, /* wcChar8 */
  87. ' ', 0x00, /* wcChar9 */
  88. 'H', 0x00, /* wcChar10 */
  89. 'I', 0x00, /* wcChar11 */
  90. 'D', 0x00, /* wcChar12 */
  91. ' ', 0x00, /* wcChar13 */
  92. 'D', 0x00, /* wcChar14 */
  93. 'E', 0x00, /* wcChar15 */
  94. 'M', 0x00, /* wcChar16 */
  95. 'O', 0x00, /* wcChar17 */
  96. ///////////////////////////////////////
  97. /// string3 descriptor
  98. ///////////////////////////////////////
  99. 0x16, /* bLength */
  100. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  101. '2', 0x00, /* wcChar0 */
  102. '0', 0x00, /* wcChar1 */
  103. '2', 0x00, /* wcChar2 */
  104. '2', 0x00, /* wcChar3 */
  105. '1', 0x00, /* wcChar4 */
  106. '2', 0x00, /* wcChar5 */
  107. '3', 0x00, /* wcChar6 */
  108. '4', 0x00, /* wcChar7 */
  109. '5', 0x00, /* wcChar8 */
  110. '6', 0x00, /* wcChar9 */
  111. #ifdef CONFIG_USB_HS
  112. ///////////////////////////////////////
  113. /// device qualifier descriptor
  114. ///////////////////////////////////////
  115. 0x0a,
  116. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  117. 0x00,
  118. 0x02,
  119. 0x00,
  120. 0x00,
  121. 0x00,
  122. 0x40,
  123. 0x01,
  124. 0x00,
  125. #endif
  126. 0x00
  127. };
  128. /*!< hid mouse report descriptor */
  129. static const uint8_t hid_mouse_report_desc[HID_MOUSE_REPORT_DESC_SIZE] = {
  130. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  131. 0x09, 0x02, // USAGE (Mouse)
  132. 0xA1, 0x01, // COLLECTION (Application)
  133. 0x09, 0x01, // USAGE (Pointer)
  134. 0xA1, 0x00, // COLLECTION (Physical)
  135. 0x05, 0x09, // USAGE_PAGE (Button)
  136. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  137. 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
  138. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  139. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  140. 0x95, 0x03, // REPORT_COUNT (3)
  141. 0x75, 0x01, // REPORT_SIZE (1)
  142. 0x81, 0x02, // INPUT (Data,Var,Abs)
  143. 0x95, 0x01, // REPORT_COUNT (1)
  144. 0x75, 0x05, // REPORT_SIZE (5)
  145. 0x81, 0x01, // INPUT (Cnst,Var,Abs)
  146. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  147. 0x09, 0x30, // USAGE (X)
  148. 0x09, 0x31, // USAGE (Y)
  149. 0x09, 0x38,
  150. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  151. 0x25, 0x7F, // LOGICAL_MAXIMUM (127)
  152. 0x75, 0x08, // REPORT_SIZE (8)
  153. 0x95, 0x03, // REPORT_COUNT (2)
  154. 0x81, 0x06, // INPUT (Data,Var,Rel)
  155. 0xC0, 0x09,
  156. 0x3c, 0x05,
  157. 0xff, 0x09,
  158. 0x01, 0x15,
  159. 0x00, 0x25,
  160. 0x01, 0x75,
  161. 0x01, 0x95,
  162. 0x02, 0xb1,
  163. 0x22, 0x75,
  164. 0x06, 0x95,
  165. 0x01, 0xb1,
  166. 0x01, 0xc0 // END_COLLECTION
  167. };
  168. /*!< mouse report struct */
  169. struct hid_mouse {
  170. uint8_t buttons;
  171. int8_t x;
  172. int8_t y;
  173. int8_t wheel;
  174. };
  175. /*!< class */
  176. static usbd_class_t hid_class;
  177. /*!< interface */
  178. static usbd_interface_t hid_intf;
  179. /*!< mouse report */
  180. static struct hid_mouse mouse_cfg;
  181. /* function ------------------------------------------------------------------*/
  182. static void usbd_hid_int_callback(uint8_t ep)
  183. {
  184. /*!< endpoint call back */
  185. /*!< transfer successfully */
  186. if (hid_state == HID_STATE_BUSY) {
  187. /*!< update the state */
  188. hid_state = HID_STATE_IDLE;
  189. }
  190. }
  191. /*!< endpoint call back */
  192. static usbd_endpoint_t hid_in_ep = {
  193. .ep_cb = usbd_hid_int_callback,
  194. .ep_addr = 0x81
  195. };
  196. /* function ------------------------------------------------------------------*/
  197. /**
  198. * @brief hid mouse init
  199. * @pre none
  200. * @param[in] none
  201. * @retval none
  202. */
  203. void hid_mouse_init(void)
  204. {
  205. usbd_desc_register(hid_descriptor);
  206. /*!< add interface */
  207. usbd_hid_add_interface(&hid_class, &hid_intf);
  208. /*!< interface add endpoint */
  209. usbd_interface_add_endpoint(&hid_intf, &hid_in_ep);
  210. /*!< register report descriptor */
  211. usbd_hid_report_descriptor_register(0, hid_mouse_report_desc, HID_MOUSE_REPORT_DESC_SIZE);
  212. usbd_initialize();
  213. /*!< init mouse report data */
  214. mouse_cfg.buttons = 0;
  215. mouse_cfg.wheel = 0;
  216. mouse_cfg.x = 0;
  217. mouse_cfg.y = 0;
  218. }
  219. /**
  220. * @brief hid mouse test
  221. * @pre none
  222. * @param[in] none
  223. * @retval none
  224. */
  225. void hid_mouse_test(void)
  226. {
  227. /*!< move mouse pointer */
  228. mouse_cfg.x += 10;
  229. mouse_cfg.y = 0;
  230. usbd_ep_write(HID_INT_EP, (uint8_t *)&mouse_cfg, 4, NULL);
  231. }