12 #define HID_IN_KEYBOARD_PACKET_SIZE 8
13 #define HID_IN_MOUSE_PACKET_SIZE 4
14 #define HID_IN_JOYSTICK_PACKET_SIZE 20
16 #define HID_KEYBOARD_INTERFACE_NUMBER 0
17 #define HID_MOUSE_INTERFACE_NUMBER 1
18 #define HID_JOYSTICK_INTERFACE_NUMBER 2
20 #define HID_KEYBOARD_ENDPOINT 1
21 #define HID_KEYBOARD_FIFO USBF1 // This must match HID_KEYBOARD_ENDPOINT!
23 #define HID_MOUSE_ENDPOINT 2
24 #define HID_MOUSE_FIFO USBF2 // This must match HID_MOUSE_ENDPOINT!
26 #define HID_JOYSTICK_ENDPOINT 3
27 #define HID_JOYSTICK_FIFO USBF3 // This must match HID_JOYSTICK_ENDPOINT!
35 #define HID_SUBCLASS_BOOT 1
38 #define HID_PROTOCOL_KEYBOARD 1
39 #define HID_PROTOCOL_MOUSE 2
42 #define HID_DESCRIPTOR_TYPE_HID 0x21
43 #define HID_DESCRIPTOR_TYPE_REPORT 0x22
46 #define HID_COUNTRY_NOT_LOCALIZED 0
49 #define HID_USAGE_PAGE 0x05
50 #define HID_USAGE 0x09
51 #define HID_COLLECTION 0xA1
52 #define HID_END_COLLECTION 0xC0
53 #define HID_REPORT_COUNT 0x95
54 #define HID_REPORT_SIZE 0x75
55 #define HID_USAGE_MIN 0x19
56 #define HID_USAGE_MAX 0x29
57 #define HID_LOGICAL_MIN 0x15
58 #define HID_LOGICAL_MIN_2 0x16 // 2-byte data
59 #define HID_LOGICAL_MAX 0x25
60 #define HID_LOGICAL_MAX_2 0x26 // 2-byte data
61 #define HID_INPUT 0x81
62 #define HID_OUTPUT 0x91
65 #define HID_USAGE_PAGE_GENERIC_DESKTOP 0x01
66 #define HID_USAGE_PAGE_KEY_CODES 0x07
67 #define HID_USAGE_PAGE_LEDS 0x08
68 #define HID_USAGE_PAGE_BUTTONS 0x09
71 #define HID_USAGE_POINTER 0x01
72 #define HID_USAGE_MOUSE 0x02
73 #define HID_USAGE_JOYSTICK 0x04
74 #define HID_USAGE_KEYBOARD 0x06
75 #define HID_USAGE_X 0x30
76 #define HID_USAGE_Y 0x31
77 #define HID_USAGE_Z 0x32
78 #define HID_USAGE_RX 0x33
79 #define HID_USAGE_RY 0x34
80 #define HID_USAGE_RZ 0x35
81 #define HID_USAGE_SLIDER 0x36
82 #define HID_USAGE_DIAL 0x37
83 #define HID_USAGE_WHEEL 0x38
86 #define HID_COLLECTION_PHYSICAL 0
87 #define HID_COLLECTION_APPLICATION 1
90 #define HID_ITEM_CONSTANT 0x1
91 #define HID_ITEM_VARIABLE 0x2
92 #define HID_ITEM_RELATIVE 0x4
95 #define HID_REQUEST_GET_REPORT 0x1
96 #define HID_REQUEST_GET_IDLE 0x2
97 #define HID_REQUEST_GET_PROTOCOL 0x3
98 #define HID_REQUEST_SET_REPORT 0x9
99 #define HID_REQUEST_SET_IDLE 0xA
100 #define HID_REQUEST_SET_PROTOCOL 0xB
103 #define HID_REPORT_TYPE_INPUT 1
104 #define HID_REPORT_TYPE_OUTPUT 2
105 #define HID_REPORT_TYPE_FEATURE 3
108 #define HID_PROTOCOL_BOOT 0
109 #define HID_PROTOCOL_REPORT 1
116 USB_DESCRIPTOR_TYPE_DEVICE,
137 HID_USAGE_PAGE, HID_USAGE_PAGE_GENERIC_DESKTOP,
138 HID_USAGE, HID_USAGE_KEYBOARD,
139 HID_COLLECTION, HID_COLLECTION_APPLICATION,
143 HID_USAGE_PAGE, HID_USAGE_PAGE_KEY_CODES,
148 HID_INPUT, HID_ITEM_VARIABLE,
152 HID_INPUT, HID_ITEM_CONSTANT,
156 HID_USAGE_PAGE, HID_USAGE_PAGE_LEDS,
159 HID_OUTPUT, HID_ITEM_VARIABLE,
163 HID_USAGE_PAGE, HID_USAGE_PAGE_KEY_CODES,
167 HID_LOGICAL_MAX_2, 231, 0,
179 HID_USAGE_PAGE, HID_USAGE_PAGE_GENERIC_DESKTOP,
180 HID_USAGE, HID_USAGE_MOUSE,
181 HID_COLLECTION, HID_COLLECTION_APPLICATION,
183 HID_USAGE, HID_USAGE_POINTER,
184 HID_COLLECTION, HID_COLLECTION_PHYSICAL,
188 HID_USAGE_PAGE, HID_USAGE_PAGE_BUTTONS,
193 HID_INPUT, HID_ITEM_VARIABLE,
197 HID_USAGE_PAGE, HID_USAGE_PAGE_GENERIC_DESKTOP,
198 HID_USAGE, HID_USAGE_X,
199 HID_USAGE, HID_USAGE_Y,
200 HID_USAGE, HID_USAGE_WHEEL,
201 HID_LOGICAL_MIN, -127,
202 HID_LOGICAL_MAX, 127,
203 HID_INPUT, (HID_ITEM_VARIABLE | HID_ITEM_RELATIVE),
215 HID_USAGE_PAGE, HID_USAGE_PAGE_GENERIC_DESKTOP,
216 HID_USAGE, HID_USAGE_JOYSTICK,
217 HID_COLLECTION, HID_COLLECTION_APPLICATION,
219 HID_USAGE, HID_USAGE_POINTER,
220 HID_COLLECTION, HID_COLLECTION_PHYSICAL,
224 HID_USAGE, HID_USAGE_X,
225 HID_USAGE, HID_USAGE_Y,
226 HID_USAGE, HID_USAGE_Z,
227 HID_USAGE, HID_USAGE_RX,
228 HID_USAGE, HID_USAGE_RY,
229 HID_USAGE, HID_USAGE_RZ,
230 HID_USAGE, HID_USAGE_SLIDER,
231 HID_USAGE, HID_USAGE_DIAL,
232 HID_LOGICAL_MIN_2, 0x01, 0x80,
233 HID_LOGICAL_MAX_2, 0xFF, 0x7F,
234 HID_INPUT, HID_ITEM_VARIABLE,
238 HID_REPORT_COUNT, 32,
240 HID_USAGE_PAGE, HID_USAGE_PAGE_BUTTONS,
245 HID_INPUT, HID_ITEM_VARIABLE,
254 uint8 keyboard_hid[9];
262 uint8 joystick_hid[9];
264 } usbConfigurationDescriptor
269 USB_DESCRIPTOR_TYPE_CONFIGURATION,
279 USB_DESCRIPTOR_TYPE_INTERFACE,
280 HID_KEYBOARD_INTERFACE_NUMBER,
285 HID_PROTOCOL_KEYBOARD,
289 sizeof(usbConfigurationDescriptor.keyboard_hid),
290 HID_DESCRIPTOR_TYPE_HID,
292 HID_COUNTRY_NOT_LOCALIZED,
294 HID_DESCRIPTOR_TYPE_REPORT,
295 sizeof(keyboardReportDescriptor), 0
299 USB_DESCRIPTOR_TYPE_ENDPOINT,
300 USB_ENDPOINT_ADDRESS_IN | HID_KEYBOARD_ENDPOINT,
301 USB_TRANSFER_TYPE_INTERRUPT,
302 HID_IN_KEYBOARD_PACKET_SIZE,
307 USB_DESCRIPTOR_TYPE_INTERFACE,
308 HID_MOUSE_INTERFACE_NUMBER,
317 sizeof(usbConfigurationDescriptor.mouse_hid),
318 HID_DESCRIPTOR_TYPE_HID,
320 HID_COUNTRY_NOT_LOCALIZED,
322 HID_DESCRIPTOR_TYPE_REPORT,
323 sizeof(mouseReportDescriptor), 0
327 USB_DESCRIPTOR_TYPE_ENDPOINT,
328 USB_ENDPOINT_ADDRESS_IN | HID_MOUSE_ENDPOINT,
329 USB_TRANSFER_TYPE_INTERRUPT,
330 HID_IN_MOUSE_PACKET_SIZE,
335 USB_DESCRIPTOR_TYPE_INTERFACE,
336 HID_JOYSTICK_INTERFACE_NUMBER,
345 sizeof(usbConfigurationDescriptor.joystick_hid),
346 HID_DESCRIPTOR_TYPE_HID,
348 HID_COUNTRY_NOT_LOCALIZED,
350 HID_DESCRIPTOR_TYPE_REPORT,
351 sizeof(joystickReportDescriptor), 0
355 USB_DESCRIPTOR_TYPE_ENDPOINT,
356 USB_ENDPOINT_ADDRESS_IN | HID_JOYSTICK_ENDPOINT,
357 USB_TRANSFER_TYPE_INTERRUPT,
358 HID_IN_JOYSTICK_PACKET_SIZE,
365 DEFINE_STRING_DESCRIPTOR(manufacturer, 18, 'P','o','l','o','l','u',' ','C','o','r','p','o','r','a','t','i','o','n')
367 DEFINE_STRING_DESCRIPTOR(keyboardName, 14, 'W','i','x','e','l',' ','K','e','y','b','o','a','r','d')
368 DEFINE_STRING_DESCRIPTOR(mouseName, 11, 'W','i','x','e','l',' ','M','o','u','s','e')
369 DEFINE_STRING_DESCRIPTOR(joystickName, 14, 'W','i','x','e','l',' ','J','o','y','s','t','i','c','k')
385 BIT hidKeyboardProtocol = HID_PROTOCOL_REPORT;
386 BIT hidMouseProtocol = HID_PROTOCOL_REPORT;
410 case HID_REQUEST_GET_REPORT:
416 case HID_KEYBOARD_INTERFACE_NUMBER:
420 case HID_MOUSE_INTERFACE_NUMBER:
424 case HID_JOYSTICK_INTERFACE_NUMBER:
432 case HID_REQUEST_SET_REPORT:
440 case HID_REQUEST_GET_IDLE:
443 response = hidKeyboardIdleDuration / 4;
449 case HID_REQUEST_SET_IDLE:
459 case HID_REQUEST_GET_PROTOCOL:
462 case HID_KEYBOARD_INTERFACE_NUMBER:
463 response = hidKeyboardProtocol;
467 case HID_MOUSE_INTERFACE_NUMBER:
468 response = hidMouseProtocol;
476 case HID_REQUEST_SET_PROTOCOL:
479 case HID_KEYBOARD_INTERFACE_NUMBER:
483 case HID_MOUSE_INTERFACE_NUMBER:
510 case HID_DESCRIPTOR_TYPE_HID:
514 case HID_KEYBOARD_INTERFACE_NUMBER:
515 usbControlRead(
sizeof(usbConfigurationDescriptor.keyboard_hid), (
uint8 XDATA *)&usbConfigurationDescriptor.keyboard_hid);
518 case HID_MOUSE_INTERFACE_NUMBER:
522 case HID_JOYSTICK_INTERFACE_NUMBER:
523 usbControlRead(
sizeof(usbConfigurationDescriptor.joystick_hid), (
uint8 XDATA *)&usbConfigurationDescriptor.joystick_hid);
528 case HID_DESCRIPTOR_TYPE_REPORT:
532 case HID_KEYBOARD_INTERFACE_NUMBER:
536 case HID_MOUSE_INTERFACE_NUMBER:
540 case HID_JOYSTICK_INTERFACE_NUMBER:
567 USBINDEX = HID_KEYBOARD_ENDPOINT;
569 if ((usbHidKeyboardInputUpdated || (hidKeyboardIdleDuration && ((
uint16)(
getMs() - hidKeyboardLastReportTime) > hidKeyboardIdleDuration))) && !(USBCSIL & USBCSIL_INPKT_RDY))
572 USBCSIL |= USBCSIL_INPKT_RDY;
573 usbHidKeyboardInputUpdated = 0;
574 hidKeyboardLastReportTime =
getMs();
577 USBINDEX = HID_MOUSE_ENDPOINT;
579 if (usbHidMouseInputUpdated && !(USBCSIL & USBCSIL_INPKT_RDY)) {
581 USBCSIL |= USBCSIL_INPKT_RDY;
582 usbHidMouseInputUpdated = 0;
585 USBINDEX = HID_JOYSTICK_ENDPOINT;
587 if (usbHidJoystickInputUpdated && !(USBCSIL & USBCSIL_INPKT_RDY)) {
589 USBCSIL |= USBCSIL_INPKT_RDY;
590 usbHidJoystickInputUpdated = 0;
733 if (asciiChar & 0x80){
return 0; }
734 return hidKeyCode[asciiChar];
HID_KEYBOARD_IN_REPORT XDATA usbHidKeyboardInput
HID_KEYBOARD_OUT_REPORT XDATA usbHidKeyboardOutput
struct USB_DESCRIPTOR_DEVICE USB_DESCRIPTOR_DEVICE
void usbCallbackInitEndpoints(void)
uint8 CODE usbStringDescriptorCount
#define USB_LANGUAGE_EN_US
#define USB_EP0_PACKET_SIZE
enum USB_DEVICE_STATES XDATA usbDeviceState
void usbControlWrite(uint16 bytesCount, uint8 XDATA *source)
USB_DESCRIPTOR_DEVICE CODE usbDeviceDescriptor
struct USB_DESCRIPTOR_CONFIGURATION USB_DESCRIPTOR_CONFIGURATION
BIT usbHidMouseInputUpdated
struct USB_DESCRIPTOR_INTERFACE USB_DESCRIPTOR_INTERFACE
struct USB_DESCRIPTOR_ENDPOINT USB_DESCRIPTOR_ENDPOINT
USB_SETUP_PACKET XDATA usbSetupPacket
#define USB_VENDOR_ID_POLOLU
void usbWriteFifo(uint8 endpointNumber, uint8 count, const uint8 XDATA *buffer)
uint16 CODE serialNumberStringDescriptor[]
void usbInitEndpointIn(uint8 endpointNumber, uint8 maxPacketSize)
HID_MOUSE_IN_REPORT XDATA usbHidMouseInput
void usbCallbackSetupHandler(void)
HID_JOYSTICK_IN_REPORT XDATA usbHidJoystickInput
void usbControlAcknowledge(void)
BIT usbHidJoystickInputUpdated
BIT usbHidKeyboardInputUpdated
void usbCallbackControlWriteHandler(void)
void usbControlRead(uint16 bytesCount, uint8 XDATA *source)
#define DEFINE_STRING_DESCRIPTOR(name, char_count,...)
void usbCallbackClassDescriptorHandler(void)
uint16 CODE *CODE usbStringDescriptors[]
uint8 usbHidKeyCodeFromAsciiChar(char asciiChar)