1 #include "usbd_core.h"
2 #include "usbd_hid.h"
3
4 /*!< endpoint address */
5 #define HID_INT_EP 0x81
6 #define HID_INT_EP_SIZE 4
7 #define HID_INT_EP_INTERVAL 10
8
9 #define USBD_VID 0xffff
10 #define USBD_PID 0xffff
11 #define USBD_MAX_POWER 100
12 #define USBD_LANGID_STRING 1033
13
14 /*!< config descriptor size */
15 #define USB_HID_CONFIG_DESC_SIZ 34
16 /*!< report descriptor size */
17 #define HID_MOUSE_REPORT_DESC_SIZE 74
18
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
24 /************** Descriptor of Joystick Mouse interface ****************/
25 /* 09 */
26 0x09, /* bLength: Interface Descriptor size */
27 USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
28 0x00, /* bInterfaceNumber: Number of Interface */
29 0x00, /* bAlternateSetting: Alternate setting */
30 0x01, /* bNumEndpoints */
31 0x03, /* bInterfaceClass: HID */
32 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
33 0x02, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
34 0, /* iInterface: Index of string descriptor */
35 /******************** Descriptor of Joystick Mouse HID ********************/
36 /* 18 */
37 0x09, /* bLength: HID Descriptor size */
38 HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
39 0x11, /* bcdHID: HID Class Spec release number */
40 0x01,
41 0x00, /* bCountryCode: Hardware target country */
42 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
43 0x22, /* bDescriptorType */
44 HID_MOUSE_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
45 0x00,
46 /******************** Descriptor of Mouse endpoint ********************/
47 /* 27 */
48 0x07, /* bLength: Endpoint Descriptor size */
49 USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
50 HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
51 0x03, /* bmAttributes: Interrupt endpoint */
52 HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
53 0x00,
54 HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
55 /* 34 */
56 ///////////////////////////////////////
57 /// string0 descriptor
58 ///////////////////////////////////////
59 USB_LANGID_INIT(USBD_LANGID_STRING),
60 ///////////////////////////////////////
61 /// string1 descriptor
62 ///////////////////////////////////////
63 0x14, /* bLength */
64 USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
65 'C', 0x00, /* wcChar0 */
66 'h', 0x00, /* wcChar1 */
67 'e', 0x00, /* wcChar2 */
68 'r', 0x00, /* wcChar3 */
69 'r', 0x00, /* wcChar4 */
70 'y', 0x00, /* wcChar5 */
71 'U', 0x00, /* wcChar6 */
72 'S', 0x00, /* wcChar7 */
73 'B', 0x00, /* wcChar8 */
74 ///////////////////////////////////////
75 /// string2 descriptor
76 ///////////////////////////////////////
77 0x26, /* bLength */
78 USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
79 'C', 0x00, /* wcChar0 */
80 'h', 0x00, /* wcChar1 */
81 'e', 0x00, /* wcChar2 */
82 'r', 0x00, /* wcChar3 */
83 'r', 0x00, /* wcChar4 */
84 'y', 0x00, /* wcChar5 */
85 'U', 0x00, /* wcChar6 */
86 'S', 0x00, /* wcChar7 */
87 'B', 0x00, /* wcChar8 */
88 ' ', 0x00, /* wcChar9 */
89 'H', 0x00, /* wcChar10 */
90 'I', 0x00, /* wcChar11 */
91 'D', 0x00, /* wcChar12 */
92 ' ', 0x00, /* wcChar13 */
93 'D', 0x00, /* wcChar14 */
94 'E', 0x00, /* wcChar15 */
95 'M', 0x00, /* wcChar16 */
96 'O', 0x00, /* wcChar17 */
97 ///////////////////////////////////////
98 /// string3 descriptor
99 ///////////////////////////////////////
100 0x16, /* bLength */
101 USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
102 '2', 0x00, /* wcChar0 */
103 '0', 0x00, /* wcChar1 */
104 '2', 0x00, /* wcChar2 */
105 '2', 0x00, /* wcChar3 */
106 '1', 0x00, /* wcChar4 */
107 '2', 0x00, /* wcChar5 */
108 '3', 0x00, /* wcChar6 */
109 '4', 0x00, /* wcChar7 */
110 '5', 0x00, /* wcChar8 */
111 '6', 0x00, /* wcChar9 */
112 #ifdef CONFIG_USB_HS
113 ///////////////////////////////////////
114 /// device qualifier descriptor
115 ///////////////////////////////////////
116 0x0a,
117 USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
118 0x00,
119 0x02,
120 0x00,
121 0x00,
122 0x00,
123 0x40,
124 0x01,
125 0x00,
126 #endif
127 0x00
128 };
129
130 /*!< hid mouse report descriptor */
131 static const uint8_t hid_mouse_report_desc[HID_MOUSE_REPORT_DESC_SIZE] = {
132 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
133 0x09, 0x02, // USAGE (Mouse)
134 0xA1, 0x01, // COLLECTION (Application)
135 0x09, 0x01, // USAGE (Pointer)
136
137 0xA1, 0x00, // COLLECTION (Physical)
138 0x05, 0x09, // USAGE_PAGE (Button)
139 0x19, 0x01, // USAGE_MINIMUM (Button 1)
140 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
141
142 0x15, 0x00, // LOGICAL_MINIMUM (0)
143 0x25, 0x01, // LOGICAL_MAXIMUM (1)
144 0x95, 0x03, // REPORT_COUNT (3)
145 0x75, 0x01, // REPORT_SIZE (1)
146
147 0x81, 0x02, // INPUT (Data,Var,Abs)
148 0x95, 0x01, // REPORT_COUNT (1)
149 0x75, 0x05, // REPORT_SIZE (5)
150 0x81, 0x01, // INPUT (Cnst,Var,Abs)
151
152 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
153 0x09, 0x30, // USAGE (X)
154 0x09, 0x31, // USAGE (Y)
155 0x09, 0x38,
156
157 0x15, 0x81, // LOGICAL_MINIMUM (-127)
158 0x25, 0x7F, // LOGICAL_MAXIMUM (127)
159 0x75, 0x08, // REPORT_SIZE (8)
160 0x95, 0x03, // REPORT_COUNT (2)
161
162 0x81, 0x06, // INPUT (Data,Var,Rel)
163 0xC0, 0x09,
164 0x3c, 0x05,
165 0xff, 0x09,
166
167 0x01, 0x15,
168 0x00, 0x25,
169 0x01, 0x75,
170 0x01, 0x95,
171
172 0x02, 0xb1,
173 0x22, 0x75,
174 0x06, 0x95,
175 0x01, 0xb1,
176
177 0x01, 0xc0 // END_COLLECTION
178 };
179
180 /*!< mouse report struct */
181 struct hid_mouse {
182 uint8_t buttons;
183 int8_t x;
184 int8_t y;
185 int8_t wheel;
186 };
187
188 /*!< mouse report */
189 static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX struct hid_mouse mouse_cfg;
190
191 #define HID_STATE_IDLE 0
192 #define HID_STATE_BUSY 1
193
194 /*!< hid state ! Data can be sent only when state is idle */
195 static volatile uint8_t hid_state = HID_STATE_IDLE;
196
usbd_event_handler(uint8_t event)197 void usbd_event_handler(uint8_t event)
198 {
199 switch (event) {
200 case USBD_EVENT_RESET:
201 break;
202 case USBD_EVENT_CONNECTED:
203 break;
204 case USBD_EVENT_DISCONNECTED:
205 break;
206 case USBD_EVENT_RESUME:
207 break;
208 case USBD_EVENT_SUSPEND:
209 break;
210 case USBD_EVENT_CONFIGURED:
211 break;
212 case USBD_EVENT_SET_REMOTE_WAKEUP:
213 break;
214 case USBD_EVENT_CLR_REMOTE_WAKEUP:
215 break;
216
217 default:
218 break;
219 }
220 }
221
222 /* function ------------------------------------------------------------------*/
usbd_hid_int_callback(uint8_t ep,uint32_t nbytes)223 static void usbd_hid_int_callback(uint8_t ep, uint32_t nbytes)
224 {
225 hid_state = HID_STATE_IDLE;
226 }
227
228 /*!< endpoint call back */
229 static struct usbd_endpoint hid_in_ep = {
230 .ep_cb = usbd_hid_int_callback,
231 .ep_addr = HID_INT_EP
232 };
233
234 struct usbd_interface intf0;
235
hid_mouse_init(void)236 void hid_mouse_init(void)
237 {
238 usbd_desc_register(hid_descriptor);
239 usbd_add_interface(usbd_hid_init_intf(&intf0, hid_mouse_report_desc, HID_MOUSE_REPORT_DESC_SIZE));
240 usbd_add_endpoint(&hid_in_ep);
241
242 usbd_initialize();
243
244 /*!< init mouse report data */
245 mouse_cfg.buttons = 0;
246 mouse_cfg.wheel = 0;
247 mouse_cfg.x = 0;
248 mouse_cfg.y = 0;
249 }
250
251 /**
252 * @brief hid mouse test
253 * @pre none
254 * @param[in] none
255 * @retval none
256 */
hid_mouse_test(void)257 void hid_mouse_test(void)
258 {
259 int counter = 0;
260 while (counter < 1000) {
261 /*!< move mouse pointer */
262 mouse_cfg.x += 40;
263 mouse_cfg.y += 0;
264
265 int ret = usbd_ep_start_write(HID_INT_EP, (uint8_t *)&mouse_cfg, 4);
266 if (ret < 0) {
267 return;
268 }
269 hid_state = HID_STATE_BUSY;
270 while (hid_state == HID_STATE_BUSY) {
271 }
272
273 counter++;
274 }
275 }
276