1 /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/usb_keyboard.html
3 * Copyright (c) 2009 PJRC.COM, LLC
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include <avr/pgmspace.h>
28 #include <avr/interrupt.h>
30 #include "usb_keyboard.h"
31 #include "usb_mouse.h"
32 #include "usb_debug.h"
33 #include "usb_extra.h"
36 #include "sleep_led.h"
40 /**************************************************************************
42 * Configurable Options
44 **************************************************************************/
46 // You can change these to give your code its own name.
48 # define STR_MANUFACTURER L"t.m.k."
50 # define STR_MANUFACTURER LSTR(MANUFACTURER)
53 # define STR_PRODUCT L"t.m.k. keyboard"
55 # define STR_PRODUCT LSTR(PRODUCT)
59 // Mac OS-X and Linux automatically load the correct drivers. On
60 // Windows, even though the driver is supplied by Microsoft, an
61 // INF file is needed to load the driver. These numbers need to
62 // match the INF file.
64 # define VENDOR_ID 0xFEED
68 # define PRODUCT_ID 0xBABE
72 # define DEVICE_VER 0x0100
76 // USB devices are supposed to implment a halt feature, which is
77 // rarely (if ever) used. If you comment this line out, the halt
78 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
79 // This is not strictly USB compliant, but works with all major
81 #define SUPPORT_ENDPOINT_HALT
85 /**************************************************************************
87 * Endpoint Buffer Configuration
89 **************************************************************************/
91 #define ENDPOINT0_SIZE 32
93 bool remote_wakeup
= false;
96 // 0:control endpoint is enabled automatically by controller.
97 static const uint8_t PROGMEM endpoint_config_table
[] = {
98 // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
99 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(KBD_SIZE
) | KBD_BUFFER
, // 1
101 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(MOUSE_SIZE
) | MOUSE_BUFFER
, // 2
105 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(DEBUG_TX_SIZE
) | DEBUG_TX_BUFFER
, // 3
106 #ifdef EXTRAKEY_ENABLE
107 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(EXTRA_SIZE
) | EXTRA_BUFFER
, // 4
112 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(KBD2_SIZE
) | KBD2_BUFFER
, // 5
120 /**************************************************************************
124 **************************************************************************/
126 // Descriptors are the data that your computer reads when it auto-detects
127 // this USB device (called "enumeration" in USB lingo). The most commonly
128 // changed items are editable at the top of this file. Changing things
129 // in here should only be done by those who've read chapter 9 of the USB
130 // spec and relevant portions of any USB class specifications!
133 static const uint8_t PROGMEM device_descriptor
[] = {
135 1, // bDescriptorType
136 0x00, 0x02, // bcdUSB
138 0, // bDeviceSubClass
139 0, // bDeviceProtocol
140 ENDPOINT0_SIZE
, // bMaxPacketSize0
141 LSB(VENDOR_ID
), MSB(VENDOR_ID
), // idVendor
142 LSB(PRODUCT_ID
), MSB(PRODUCT_ID
), // idProduct
143 LSB(DEVICE_VER
), MSB(DEVICE_VER
), // bcdDevice
147 1 // bNumConfigurations
150 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
151 static const uint8_t PROGMEM keyboard_hid_report_desc
[] = {
152 0x05, 0x01, // Usage Page (Generic Desktop),
153 0x09, 0x06, // Usage (Keyboard),
154 0xA1, 0x01, // Collection (Application),
155 0x75, 0x01, // Report Size (1),
156 0x95, 0x08, // Report Count (8),
157 0x05, 0x07, // Usage Page (Key Codes),
158 0x19, 0xE0, // Usage Minimum (224),
159 0x29, 0xE7, // Usage Maximum (231),
160 0x15, 0x00, // Logical Minimum (0),
161 0x25, 0x01, // Logical Maximum (1),
162 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
163 0x95, 0x01, // Report Count (1),
164 0x75, 0x08, // Report Size (8),
165 0x81, 0x03, // Input (Constant), ;Reserved byte
166 0x95, 0x05, // Report Count (5),
167 0x75, 0x01, // Report Size (1),
168 0x05, 0x08, // Usage Page (LEDs),
169 0x19, 0x01, // Usage Minimum (1),
170 0x29, 0x05, // Usage Maximum (5),
171 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
172 0x95, 0x01, // Report Count (1),
173 0x75, 0x03, // Report Size (3),
174 0x91, 0x03, // Output (Constant), ;LED report padding
175 0x95, KBD_REPORT_KEYS
, // Report Count (),
176 0x75, 0x08, // Report Size (8),
177 0x15, 0x00, // Logical Minimum (0),
178 0x25, 0xFF, // Logical Maximum(255),
179 0x05, 0x07, // Usage Page (Key Codes),
180 0x19, 0x00, // Usage Minimum (0),
181 0x29, 0xFF, // Usage Maximum (255),
182 0x81, 0x00, // Input (Data, Array),
183 0xc0 // End Collection
186 static const uint8_t PROGMEM keyboard2_hid_report_desc
[] = {
187 0x05, 0x01, // Usage Page (Generic Desktop),
188 0x09, 0x06, // Usage (Keyboard),
189 0xA1, 0x01, // Collection (Application),
190 // bitmap of modifiers
191 0x75, 0x01, // Report Size (1),
192 0x95, 0x08, // Report Count (8),
193 0x05, 0x07, // Usage Page (Key Codes),
194 0x19, 0xE0, // Usage Minimum (224),
195 0x29, 0xE7, // Usage Maximum (231),
196 0x15, 0x00, // Logical Minimum (0),
197 0x25, 0x01, // Logical Maximum (1),
198 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
200 0x95, 0x05, // Report Count (5),
201 0x75, 0x01, // Report Size (1),
202 0x05, 0x08, // Usage Page (LEDs),
203 0x19, 0x01, // Usage Minimum (1),
204 0x29, 0x05, // Usage Maximum (5),
205 0x91, 0x02, // Output (Data, Variable, Absolute),
206 0x95, 0x01, // Report Count (1),
207 0x75, 0x03, // Report Size (3),
208 0x91, 0x03, // Output (Constant),
210 0x95, KBD2_REPORT_KEYS
*8, // Report Count (),
211 0x75, 0x01, // Report Size (1),
212 0x15, 0x00, // Logical Minimum (0),
213 0x25, 0x01, // Logical Maximum(1),
214 0x05, 0x07, // Usage Page (Key Codes),
215 0x19, 0x00, // Usage Minimum (0),
216 0x29, KBD2_REPORT_KEYS
*8-1, // Usage Maximum (),
217 0x81, 0x02, // Input (Data, Variable, Absolute),
218 0xc0 // End Collection
223 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
224 // http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
225 // http://www.keil.com/forum/15671/
226 // http://www.microsoft.com/whdc/device/input/wheel.mspx
227 static const uint8_t PROGMEM mouse_hid_report_desc
[] = {
229 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
230 0x09, 0x02, // USAGE (Mouse)
231 0xa1, 0x01, // COLLECTION (Application)
232 //0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
233 0x09, 0x01, // USAGE (Pointer)
234 0xa1, 0x00, // COLLECTION (Physical)
235 // ---------------------------- Buttons
236 0x05, 0x09, // USAGE_PAGE (Button)
237 0x19, 0x01, // USAGE_MINIMUM (Button 1)
238 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
239 0x15, 0x00, // LOGICAL_MINIMUM (0)
240 0x25, 0x01, // LOGICAL_MAXIMUM (1)
241 0x75, 0x01, // REPORT_SIZE (1)
242 0x95, 0x05, // REPORT_COUNT (5)
243 0x81, 0x02, // INPUT (Data,Var,Abs)
244 0x75, 0x03, // REPORT_SIZE (3)
245 0x95, 0x01, // REPORT_COUNT (1)
246 0x81, 0x03, // INPUT (Cnst,Var,Abs)
247 // ---------------------------- X,Y position
248 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
249 0x09, 0x30, // USAGE (X)
250 0x09, 0x31, // USAGE (Y)
251 0x15, 0x81, // LOGICAL_MINIMUM (-127)
252 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
253 0x75, 0x08, // REPORT_SIZE (8)
254 0x95, 0x02, // REPORT_COUNT (2)
255 0x81, 0x06, // INPUT (Data,Var,Rel)
256 // ---------------------------- Vertical wheel
257 0x09, 0x38, // USAGE (Wheel)
258 0x15, 0x81, // LOGICAL_MINIMUM (-127)
259 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
260 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
261 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
262 0x75, 0x08, // REPORT_SIZE (8)
263 0x95, 0x01, // REPORT_COUNT (1)
264 0x81, 0x06, // INPUT (Data,Var,Rel)
265 // ---------------------------- Horizontal wheel
266 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
267 0x0a, 0x38, 0x02, // USAGE (AC Pan)
268 0x15, 0x81, // LOGICAL_MINIMUM (-127)
269 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
270 0x75, 0x08, // REPORT_SIZE (8)
271 0x95, 0x01, // REPORT_COUNT (1)
272 0x81, 0x06, // INPUT (Data,Var,Rel)
273 0xc0, // END_COLLECTION
274 0xc0, // END_COLLECTION
278 static const uint8_t PROGMEM debug_hid_report_desc
[] = {
279 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
280 0x09, 0x74, // Usage 0x74
281 0xA1, 0x53, // Collection 0x53
282 0x75, 0x08, // report size = 8 bits
283 0x15, 0x00, // logical minimum = 0
284 0x26, 0xFF, 0x00, // logical maximum = 255
285 0x95, DEBUG_TX_SIZE
, // report count
287 0x81, 0x02, // Input (array)
288 0xC0 // end collection
291 #ifdef EXTRAKEY_ENABLE
292 // audio controls & system controls
293 // http://www.microsoft.com/whdc/archive/w2kbd.mspx
294 static const uint8_t PROGMEM extra_hid_report_desc
[] = {
296 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
297 0x09, 0x80, // USAGE (System Control)
298 0xa1, 0x01, // COLLECTION (Application)
299 0x85, REPORT_ID_SYSTEM
, // REPORT_ID (2)
300 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
301 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
302 0x19, 0x01, // USAGE_MINIMUM (0x1)
303 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
304 0x75, 0x10, // REPORT_SIZE (16)
305 0x95, 0x01, // REPORT_COUNT (1)
306 0x81, 0x00, // INPUT (Data,Array,Abs)
307 0xc0, // END_COLLECTION
309 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
310 0x09, 0x01, // USAGE (Consumer Control)
311 0xa1, 0x01, // COLLECTION (Application)
312 0x85, REPORT_ID_CONSUMER
, // REPORT_ID (3)
313 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
314 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
315 0x19, 0x01, // USAGE_MINIMUM (0x1)
316 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
317 0x75, 0x10, // REPORT_SIZE (16)
318 0x95, 0x01, // REPORT_COUNT (1)
319 0x81, 0x00, // INPUT (Data,Array,Abs)
320 0xc0, // END_COLLECTION
324 #define KBD_HID_DESC_NUM 0
325 #define KBD_HID_DESC_OFFSET (9+(9+9+7)*KBD_HID_DESC_NUM+9)
328 # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 1)
329 # define MOUSE_HID_DESC_OFFSET (9+(9+9+7)*MOUSE_HID_DESC_NUM+9)
331 # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0)
334 #define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 1)
335 #define DEBUG_HID_DESC_OFFSET (9+(9+9+7)*DEBUG_HID_DESC_NUM+9)
337 #ifdef EXTRAKEY_ENABLE
338 # define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 1)
339 # define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
341 # define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 0)
345 # define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 1)
346 # define KBD2_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
348 # define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0)
351 #define NUM_INTERFACES (KBD2_HID_DESC_NUM + 1)
352 #define CONFIG1_DESC_SIZE (9+(9+9+7)*NUM_INTERFACES)
353 static const uint8_t PROGMEM config1_descriptor
[CONFIG1_DESC_SIZE
] = {
354 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
356 2, // bDescriptorType;
357 LSB(CONFIG1_DESC_SIZE
), // wTotalLength
358 MSB(CONFIG1_DESC_SIZE
),
359 NUM_INTERFACES
, // bNumInterfaces
360 1, // bConfigurationValue
362 0xA0, // bmAttributes
365 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
367 4, // bDescriptorType
368 KBD_INTERFACE
, // bInterfaceNumber
369 0, // bAlternateSetting
371 0x03, // bInterfaceClass (0x03 = HID)
372 0x01, // bInterfaceSubClass (0x01 = Boot)
373 0x01, // bInterfaceProtocol (0x01 = Keyboard)
375 // HID descriptor, HID 1.11 spec, section 6.2.1
377 0x21, // bDescriptorType
378 0x11, 0x01, // bcdHID
380 1, // bNumDescriptors
381 0x22, // bDescriptorType
382 sizeof(keyboard_hid_report_desc
), // wDescriptorLength
384 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
386 5, // bDescriptorType
387 KBD_ENDPOINT
| 0x80, // bEndpointAddress
388 0x03, // bmAttributes (0x03=intr)
389 KBD_SIZE
, 0, // wMaxPacketSize
393 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
395 4, // bDescriptorType
396 MOUSE_INTERFACE
, // bInterfaceNumber
397 0, // bAlternateSetting
399 0x03, // bInterfaceClass (0x03 = HID)
400 // ThinkPad T23 BIOS doesn't work with boot mouse.
401 0x00, // bInterfaceSubClass (0x01 = Boot)
402 0x00, // bInterfaceProtocol (0x02 = Mouse)
404 0x01, // bInterfaceSubClass (0x01 = Boot)
405 0x02, // bInterfaceProtocol (0x02 = Mouse)
408 // HID descriptor, HID 1.11 spec, section 6.2.1
410 0x21, // bDescriptorType
411 0x11, 0x01, // bcdHID
413 1, // bNumDescriptors
414 0x22, // bDescriptorType
415 sizeof(mouse_hid_report_desc
), // wDescriptorLength
417 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
419 5, // bDescriptorType
420 MOUSE_ENDPOINT
| 0x80, // bEndpointAddress
421 0x03, // bmAttributes (0x03=intr)
422 MOUSE_SIZE
, 0, // wMaxPacketSize
426 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
428 4, // bDescriptorType
429 DEBUG_INTERFACE
, // bInterfaceNumber
430 0, // bAlternateSetting
432 0x03, // bInterfaceClass (0x03 = HID)
433 0x00, // bInterfaceSubClass
434 0x00, // bInterfaceProtocol
436 // HID descriptor, HID 1.11 spec, section 6.2.1
438 0x21, // bDescriptorType
439 0x11, 0x01, // bcdHID
441 1, // bNumDescriptors
442 0x22, // bDescriptorType
443 sizeof(debug_hid_report_desc
), // wDescriptorLength
445 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
447 5, // bDescriptorType
448 DEBUG_TX_ENDPOINT
| 0x80, // bEndpointAddress
449 0x03, // bmAttributes (0x03=intr)
450 DEBUG_TX_SIZE
, 0, // wMaxPacketSize
453 #ifdef EXTRAKEY_ENABLE
454 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
456 4, // bDescriptorType
457 EXTRA_INTERFACE
, // bInterfaceNumber
458 0, // bAlternateSetting
460 0x03, // bInterfaceClass (0x03 = HID)
461 0x00, // bInterfaceSubClass
462 0x00, // bInterfaceProtocol
464 // HID descriptor, HID 1.11 spec, section 6.2.1
466 0x21, // bDescriptorType
467 0x11, 0x01, // bcdHID
469 1, // bNumDescriptors
470 0x22, // bDescriptorType
471 sizeof(extra_hid_report_desc
), // wDescriptorLength
473 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
475 5, // bDescriptorType
476 EXTRA_ENDPOINT
| 0x80, // bEndpointAddress
477 0x03, // bmAttributes (0x03=intr)
478 EXTRA_SIZE
, 0, // wMaxPacketSize
483 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
485 4, // bDescriptorType
486 KBD2_INTERFACE
, // bInterfaceNumber
487 0, // bAlternateSetting
489 0x03, // bInterfaceClass (0x03 = HID)
490 0x00, // bInterfaceSubClass (0x01 = Boot)
491 0x00, // bInterfaceProtocol (0x01 = Keyboard)
493 // HID descriptor, HID 1.11 spec, section 6.2.1
495 0x21, // bDescriptorType
496 0x11, 0x01, // bcdHID
498 1, // bNumDescriptors
499 0x22, // bDescriptorType
500 sizeof(keyboard2_hid_report_desc
), // wDescriptorLength
502 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
504 5, // bDescriptorType
505 KBD2_ENDPOINT
| 0x80, // bEndpointAddress
506 0x03, // bmAttributes (0x03=intr)
507 KBD2_SIZE
, 0, // wMaxPacketSize
512 // If you're desperate for a little extra code memory, these strings
513 // can be completely removed if iManufacturer, iProduct, iSerialNumber
514 // in the device desciptor are changed to zeros.
515 struct usb_string_descriptor_struct
{
517 uint8_t bDescriptorType
;
520 static const struct usb_string_descriptor_struct PROGMEM string0
= {
525 static const struct usb_string_descriptor_struct PROGMEM string1
= {
526 sizeof(STR_MANUFACTURER
),
530 static const struct usb_string_descriptor_struct PROGMEM string2
= {
536 // This table defines which descriptor data is sent for each specific
537 // request from the host (in wValue and wIndex).
538 static const struct descriptor_list_struct
{
539 uint16_t wValue
; // descriptor type
543 } PROGMEM descriptor_list
[] = {
545 {0x0100, 0x0000, device_descriptor
, sizeof(device_descriptor
)},
546 // CONFIGURATION descriptor
547 {0x0200, 0x0000, config1_descriptor
, sizeof(config1_descriptor
)},
548 // HID/REPORT descriptors
549 {0x2100, KBD_INTERFACE
, config1_descriptor
+KBD_HID_DESC_OFFSET
, 9},
550 {0x2200, KBD_INTERFACE
, keyboard_hid_report_desc
, sizeof(keyboard_hid_report_desc
)},
552 {0x2100, MOUSE_INTERFACE
, config1_descriptor
+MOUSE_HID_DESC_OFFSET
, 9},
553 {0x2200, MOUSE_INTERFACE
, mouse_hid_report_desc
, sizeof(mouse_hid_report_desc
)},
555 {0x2100, DEBUG_INTERFACE
, config1_descriptor
+DEBUG_HID_DESC_OFFSET
, 9},
556 {0x2200, DEBUG_INTERFACE
, debug_hid_report_desc
, sizeof(debug_hid_report_desc
)},
557 #ifdef EXTRAKEY_ENABLE
558 {0x2100, EXTRA_INTERFACE
, config1_descriptor
+EXTRA_HID_DESC_OFFSET
, 9},
559 {0x2200, EXTRA_INTERFACE
, extra_hid_report_desc
, sizeof(extra_hid_report_desc
)},
562 {0x2100, KBD2_INTERFACE
, config1_descriptor
+KBD2_HID_DESC_OFFSET
, 9},
563 {0x2200, KBD2_INTERFACE
, keyboard2_hid_report_desc
, sizeof(keyboard2_hid_report_desc
)},
565 // STRING descriptors
566 {0x0300, 0x0000, (const uint8_t *)&string0
, 4},
567 {0x0301, 0x0409, (const uint8_t *)&string1
, sizeof(STR_MANUFACTURER
)},
568 {0x0302, 0x0409, (const uint8_t *)&string2
, sizeof(STR_PRODUCT
)}
570 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
573 /**************************************************************************
575 * Variables - these are the only non-stack RAM usage
577 **************************************************************************/
579 // zero when we are not configured, non-zero when enumerated
580 static volatile uint8_t usb_configuration
=0;
583 /**************************************************************************
585 * Public Functions - these are the API intended for the user
587 **************************************************************************/
594 USB_FREEZE(); // enable USB
595 PLL_CONFIG(); // config PLL
596 while (!(PLLCSR
& (1<<PLOCK
))) ; // wait for PLL lock
597 USB_CONFIG(); // start USB clock
598 UDCON
= 0; // enable attach resistor
599 usb_configuration
= 0;
601 UDIEN
= (1<<EORSTE
)|(1<<SOFE
)|(1<<SUSPE
)|(1<<WAKEUPE
);
605 // return 0 if the USB is not configured, or the configuration
606 // number selected by the HOST
607 uint8_t usb_configured(void)
609 return usb_configuration
&& !suspend
;
612 void usb_remote_wakeup(void)
614 UDCON
|= (1<<RMWKUP
);
619 /**************************************************************************
621 * Private Functions - not intended for general user consumption....
623 **************************************************************************/
627 // USB Device Interrupt - handle all device-level events
628 // the transmit buffer flushing is triggered by the start of frame
633 static uint8_t div4
=0;
637 if ((intbits
& (1<<SUSPI
)) && (UDIEN
& (1<<SUSPE
)) && usb_configuration
) {
638 #ifdef SLEEP_LED_ENABLE
641 UDIEN
&= ~(1<<SUSPE
);
642 UDIEN
|= (1<<WAKEUPE
);
645 if ((intbits
& (1<<WAKEUPI
)) && (UDIEN
& (1<<WAKEUPE
)) && usb_configuration
) {
646 suspend_wakeup_init();
647 #ifdef SLEEP_LED_ENABLE
650 led_set(host_keyboard_leds());
653 UDIEN
&= ~(1<<WAKEUPE
);
656 if (intbits
& (1<<EORSTI
)) {
659 UECFG0X
= EP_TYPE_CONTROL
;
660 UECFG1X
= EP_SIZE(ENDPOINT0_SIZE
) | EP_SINGLE_BUFFER
;
661 UEIENX
= (1<<RXSTPE
);
662 usb_configuration
= 0;
664 if ((intbits
& (1<<SOFI
)) && usb_configuration
) {
665 t
= debug_flush_timer
;
667 debug_flush_timer
= -- t
;
669 UENUM
= DEBUG_TX_ENDPOINT
;
670 while ((UEINTX
& (1<<RWAL
))) {
676 /* TODO: should keep IDLE rate on each keyboard interface */
678 if (!keyboard_nkro
&& usb_keyboard_idle_config
&& (++div4
& 3) == 0) {
680 if (usb_keyboard_idle_config
&& (++div4
& 3) == 0) {
682 UENUM
= KBD_ENDPOINT
;
683 if (UEINTX
& (1<<RWAL
)) {
684 usb_keyboard_idle_count
++;
685 if (usb_keyboard_idle_count
== usb_keyboard_idle_config
) {
686 usb_keyboard_idle_count
= 0;
687 /* TODO: fix keyboard_report inconsistency */
688 /* To avoid Mac SET_IDLE behaviour.
689 UEDATX = keyboard_report_prev->mods;
691 uint8_t keys = usb_keyboard_protocol ? KBD_REPORT_KEYS : 6;
692 for (uint8_t i=0; i<keys; i++) {
693 UEDATX = keyboard_report_prev->keys[i];
705 // Misc functions to wait for ready and send/receive packets
706 static inline void usb_wait_in_ready(void)
708 while (!(UEINTX
& (1<<TXINI
))) ;
710 static inline void usb_send_in(void)
712 UEINTX
= ~(1<<TXINI
);
714 static inline void usb_wait_receive_out(void)
716 while (!(UEINTX
& (1<<RXOUTI
))) ;
718 static inline void usb_ack_out(void)
720 UEINTX
= ~(1<<RXOUTI
);
725 // USB Endpoint Interrupt - endpoint 0 is handled here. The
726 // other endpoints are manipulated by the user-callable
727 // functions, and the start-of-frame interrupt.
734 uint8_t i
, n
, len
, en
;
735 uint8_t bmRequestType
;
741 const uint8_t *desc_addr
;
746 if (intbits
& (1<<RXSTPI
)) {
747 bmRequestType
= UEDATX
;
750 wValue
|= (UEDATX
<< 8);
752 wIndex
|= (UEDATX
<< 8);
754 wLength
|= (UEDATX
<< 8);
755 UEINTX
= ~((1<<RXSTPI
) | (1<<RXOUTI
) | (1<<TXINI
));
756 if (bRequest
== GET_DESCRIPTOR
) {
757 list
= (const uint8_t *)descriptor_list
;
759 if (i
>= NUM_DESC_LIST
) {
760 UECONX
= (1<<STALLRQ
)|(1<<EPEN
); //stall
763 desc_val
= pgm_read_word(list
);
764 if (desc_val
!= wValue
) {
765 list
+= sizeof(struct descriptor_list_struct
);
769 desc_val
= pgm_read_word(list
);
770 if (desc_val
!= wIndex
) {
771 list
+= sizeof(struct descriptor_list_struct
)-2;
775 desc_addr
= (const uint8_t *)pgm_read_word(list
);
777 desc_length
= pgm_read_byte(list
);
780 len
= (wLength
< 256) ? wLength
: 255;
781 if (len
> desc_length
) len
= desc_length
;
783 // wait for host ready for IN packet
786 } while (!(i
& ((1<<TXINI
)|(1<<RXOUTI
))));
787 if (i
& (1<<RXOUTI
)) return; // abort
789 n
= len
< ENDPOINT0_SIZE
? len
: ENDPOINT0_SIZE
;
790 for (i
= n
; i
; i
--) {
791 UEDATX
= pgm_read_byte(desc_addr
++);
795 } while (len
|| n
== ENDPOINT0_SIZE
);
798 if (bRequest
== SET_ADDRESS
) {
801 UDADDR
= wValue
| (1<<ADDEN
);
804 if (bRequest
== SET_CONFIGURATION
&& bmRequestType
== 0) {
805 usb_configuration
= wValue
;
807 cfg
= endpoint_config_table
;
808 for (i
=1; i
<=MAX_ENDPOINT
; i
++) {
810 en
= pgm_read_byte(cfg
++);
813 UECFG0X
= pgm_read_byte(cfg
++);
814 UECFG1X
= pgm_read_byte(cfg
++);
823 if (bRequest
== GET_CONFIGURATION
&& bmRequestType
== 0x80) {
825 UEDATX
= usb_configuration
;
830 if (bRequest
== GET_STATUS
) {
833 #ifdef SUPPORT_ENDPOINT_HALT
834 if (bmRequestType
== 0x82) {
836 if (UECONX
& (1<<STALLRQ
)) i
= 1;
845 if (bRequest
== CLEAR_FEATURE
|| bRequest
== SET_FEATURE
) {
846 #ifdef SUPPORT_ENDPOINT_HALT
847 if (bmRequestType
== 0x02 && wValue
== ENDPOINT_HALT
) {
849 if (i
>= 1 && i
<= MAX_ENDPOINT
) {
852 if (bRequest
== SET_FEATURE
) {
853 UECONX
= (1<<STALLRQ
)|(1<<EPEN
);
855 UECONX
= (1<<STALLRQC
)|(1<<RSTDT
)|(1<<EPEN
);
863 if (bmRequestType
== 0x00 && wValue
== DEVICE_REMOTE_WAKEUP
) {
864 if (bRequest
== SET_FEATURE
) {
865 remote_wakeup
= true;
867 remote_wakeup
= false;
873 if (wIndex
== KBD_INTERFACE
) {
874 if (bmRequestType
== 0xA1) {
875 if (bRequest
== HID_GET_REPORT
) {
877 UEDATX
= keyboard_report
->mods
;
879 for (i
=0; i
<6; i
++) {
880 UEDATX
= keyboard_report
->keys
[i
];
885 if (bRequest
== HID_GET_IDLE
) {
887 UEDATX
= usb_keyboard_idle_config
;
891 if (bRequest
== HID_GET_PROTOCOL
) {
893 UEDATX
= usb_keyboard_protocol
;
898 if (bmRequestType
== 0x21) {
899 if (bRequest
== HID_SET_REPORT
) {
900 usb_wait_receive_out();
901 usb_keyboard_leds
= UEDATX
;
906 if (bRequest
== HID_SET_IDLE
) {
907 usb_keyboard_idle_config
= (wValue
>> 8);
908 usb_keyboard_idle_count
= 0;
909 //usb_wait_in_ready();
913 if (bRequest
== HID_SET_PROTOCOL
) {
914 usb_keyboard_protocol
= wValue
;
915 //usb_wait_in_ready();
922 if (wIndex
== MOUSE_INTERFACE
) {
923 if (bmRequestType
== 0xA1) {
924 if (bRequest
== HID_GET_REPORT
) {
925 if (wValue
== HID_REPORT_INPUT
) {
934 if (wValue
== HID_REPORT_FEATURE
) {
941 if (bRequest
== HID_GET_PROTOCOL
) {
943 UEDATX
= usb_mouse_protocol
;
948 if (bmRequestType
== 0x21) {
949 if (bRequest
== HID_SET_PROTOCOL
) {
950 usb_mouse_protocol
= wValue
;
957 if (wIndex
== DEBUG_INTERFACE
) {
958 if (bRequest
== HID_GET_REPORT
&& bmRequestType
== 0xA1) {
961 // wait for host ready for IN packet
964 } while (!(i
& ((1<<TXINI
)|(1<<RXOUTI
))));
965 if (i
& (1<<RXOUTI
)) return; // abort
967 n
= len
< ENDPOINT0_SIZE
? len
: ENDPOINT0_SIZE
;
968 for (i
= n
; i
; i
--) {
973 } while (len
|| n
== ENDPOINT0_SIZE
);
978 UECONX
= (1<<STALLRQ
) | (1<<EPEN
); // stall