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"
38 /**************************************************************************
40 * Configurable Options
42 **************************************************************************/
44 // You can change these to give your code its own name.
46 # define STR_MANUFACTURER L"t.m.k."
48 # define STR_MANUFACTURER LSTR(MANUFACTURER)
51 # define STR_PRODUCT L"t.m.k. keyboard"
53 # define STR_PRODUCT LSTR(PRODUCT)
57 // Mac OS-X and Linux automatically load the correct drivers. On
58 // Windows, even though the driver is supplied by Microsoft, an
59 // INF file is needed to load the driver. These numbers need to
60 // match the INF file.
62 # define VENDOR_ID 0xFEED
66 # define PRODUCT_ID 0xBABE
70 # define DEVICE_VER 0x0100
74 // USB devices are supposed to implment a halt feature, which is
75 // rarely (if ever) used. If you comment this line out, the halt
76 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
77 // This is not strictly USB compliant, but works with all major
79 #define SUPPORT_ENDPOINT_HALT
83 /**************************************************************************
85 * Endpoint Buffer Configuration
87 **************************************************************************/
89 #define ENDPOINT0_SIZE 32
91 bool remote_wakeup = false;
94 // 0:control endpoint is enabled automatically by controller.
95 static const uint8_t PROGMEM endpoint_config_table[] = {
96 // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
97 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD_SIZE) | KBD_BUFFER, // 1
99 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(MOUSE_SIZE) | MOUSE_BUFFER, // 2
103 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER, // 3
104 #ifdef EXTRAKEY_ENABLE
105 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 4
110 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD2_SIZE) | KBD2_BUFFER, // 5
118 /**************************************************************************
122 **************************************************************************/
124 // Descriptors are the data that your computer reads when it auto-detects
125 // this USB device (called "enumeration" in USB lingo). The most commonly
126 // changed items are editable at the top of this file. Changing things
127 // in here should only be done by those who've read chapter 9 of the USB
128 // spec and relevant portions of any USB class specifications!
131 static const uint8_t PROGMEM device_descriptor[] = {
133 1, // bDescriptorType
134 0x00, 0x02, // bcdUSB
136 0, // bDeviceSubClass
137 0, // bDeviceProtocol
138 ENDPOINT0_SIZE, // bMaxPacketSize0
139 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
140 LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
141 LSB(DEVICE_VER), MSB(DEVICE_VER), // bcdDevice
145 1 // bNumConfigurations
148 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
149 static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
150 0x05, 0x01, // Usage Page (Generic Desktop),
151 0x09, 0x06, // Usage (Keyboard),
152 0xA1, 0x01, // Collection (Application),
153 0x75, 0x01, // Report Size (1),
154 0x95, 0x08, // Report Count (8),
155 0x05, 0x07, // Usage Page (Key Codes),
156 0x19, 0xE0, // Usage Minimum (224),
157 0x29, 0xE7, // Usage Maximum (231),
158 0x15, 0x00, // Logical Minimum (0),
159 0x25, 0x01, // Logical Maximum (1),
160 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
161 0x95, 0x01, // Report Count (1),
162 0x75, 0x08, // Report Size (8),
163 0x81, 0x03, // Input (Constant), ;Reserved byte
164 0x95, 0x05, // Report Count (5),
165 0x75, 0x01, // Report Size (1),
166 0x05, 0x08, // Usage Page (LEDs),
167 0x19, 0x01, // Usage Minimum (1),
168 0x29, 0x05, // Usage Maximum (5),
169 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
170 0x95, 0x01, // Report Count (1),
171 0x75, 0x03, // Report Size (3),
172 0x91, 0x03, // Output (Constant), ;LED report padding
173 0x95, KBD_REPORT_KEYS, // Report Count (),
174 0x75, 0x08, // Report Size (8),
175 0x15, 0x00, // Logical Minimum (0),
176 0x25, 0xFF, // Logical Maximum(255),
177 0x05, 0x07, // Usage Page (Key Codes),
178 0x19, 0x00, // Usage Minimum (0),
179 0x29, 0xFF, // Usage Maximum (255),
180 0x81, 0x00, // Input (Data, Array),
181 0xc0 // End Collection
184 static const uint8_t PROGMEM keyboard2_hid_report_desc[] = {
185 0x05, 0x01, // Usage Page (Generic Desktop),
186 0x09, 0x06, // Usage (Keyboard),
187 0xA1, 0x01, // Collection (Application),
188 // bitmap of modifiers
189 0x75, 0x01, // Report Size (1),
190 0x95, 0x08, // Report Count (8),
191 0x05, 0x07, // Usage Page (Key Codes),
192 0x19, 0xE0, // Usage Minimum (224),
193 0x29, 0xE7, // Usage Maximum (231),
194 0x15, 0x00, // Logical Minimum (0),
195 0x25, 0x01, // Logical Maximum (1),
196 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
198 0x95, 0x05, // Report Count (5),
199 0x75, 0x01, // Report Size (1),
200 0x05, 0x08, // Usage Page (LEDs),
201 0x19, 0x01, // Usage Minimum (1),
202 0x29, 0x05, // Usage Maximum (5),
203 0x91, 0x02, // Output (Data, Variable, Absolute),
204 0x95, 0x01, // Report Count (1),
205 0x75, 0x03, // Report Size (3),
206 0x91, 0x03, // Output (Constant),
208 0x95, KBD2_REPORT_KEYS*8, // Report Count (),
209 0x75, 0x01, // Report Size (1),
210 0x15, 0x00, // Logical Minimum (0),
211 0x25, 0x01, // Logical Maximum(1),
212 0x05, 0x07, // Usage Page (Key Codes),
213 0x19, 0x00, // Usage Minimum (0),
214 0x29, KBD2_REPORT_KEYS*8-1, // Usage Maximum (),
215 0x81, 0x02, // Input (Data, Variable, Absolute),
216 0xc0 // End Collection
221 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
222 // http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
223 // http://www.keil.com/forum/15671/
224 // http://www.microsoft.com/whdc/device/input/wheel.mspx
225 static const uint8_t PROGMEM mouse_hid_report_desc[] = {
227 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
228 0x09, 0x02, // USAGE (Mouse)
229 0xa1, 0x01, // COLLECTION (Application)
230 //0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
231 0x09, 0x01, // USAGE (Pointer)
232 0xa1, 0x00, // COLLECTION (Physical)
233 // ---------------------------- Buttons
234 0x05, 0x09, // USAGE_PAGE (Button)
235 0x19, 0x01, // USAGE_MINIMUM (Button 1)
236 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
237 0x15, 0x00, // LOGICAL_MINIMUM (0)
238 0x25, 0x01, // LOGICAL_MAXIMUM (1)
239 0x75, 0x01, // REPORT_SIZE (1)
240 0x95, 0x05, // REPORT_COUNT (5)
241 0x81, 0x02, // INPUT (Data,Var,Abs)
242 0x75, 0x03, // REPORT_SIZE (3)
243 0x95, 0x01, // REPORT_COUNT (1)
244 0x81, 0x03, // INPUT (Cnst,Var,Abs)
245 // ---------------------------- X,Y position
246 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
247 0x09, 0x30, // USAGE (X)
248 0x09, 0x31, // USAGE (Y)
249 0x15, 0x81, // LOGICAL_MINIMUM (-127)
250 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
251 0x75, 0x08, // REPORT_SIZE (8)
252 0x95, 0x02, // REPORT_COUNT (2)
253 0x81, 0x06, // INPUT (Data,Var,Rel)
254 // ---------------------------- Vertical wheel
255 0x09, 0x38, // USAGE (Wheel)
256 0x15, 0x81, // LOGICAL_MINIMUM (-127)
257 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
258 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
259 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
260 0x75, 0x08, // REPORT_SIZE (8)
261 0x95, 0x01, // REPORT_COUNT (1)
262 0x81, 0x06, // INPUT (Data,Var,Rel)
263 // ---------------------------- Horizontal wheel
264 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
265 0x0a, 0x38, 0x02, // USAGE (AC Pan)
266 0x15, 0x81, // LOGICAL_MINIMUM (-127)
267 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
268 0x75, 0x08, // REPORT_SIZE (8)
269 0x95, 0x01, // REPORT_COUNT (1)
270 0x81, 0x06, // INPUT (Data,Var,Rel)
271 0xc0, // END_COLLECTION
272 0xc0, // END_COLLECTION
276 static const uint8_t PROGMEM debug_hid_report_desc[] = {
277 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
278 0x09, 0x74, // Usage 0x74
279 0xA1, 0x53, // Collection 0x53
280 0x75, 0x08, // report size = 8 bits
281 0x15, 0x00, // logical minimum = 0
282 0x26, 0xFF, 0x00, // logical maximum = 255
283 0x95, DEBUG_TX_SIZE, // report count
285 0x81, 0x02, // Input (array)
286 0xC0 // end collection
289 #ifdef EXTRAKEY_ENABLE
290 // audio controls & system controls
291 // http://www.microsoft.com/whdc/archive/w2kbd.mspx
292 static const uint8_t PROGMEM extra_hid_report_desc[] = {
294 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
295 0x09, 0x80, // USAGE (System Control)
296 0xa1, 0x01, // COLLECTION (Application)
297 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
298 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
299 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
300 0x19, 0x01, // USAGE_MINIMUM (0x1)
301 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
302 0x75, 0x10, // REPORT_SIZE (16)
303 0x95, 0x01, // REPORT_COUNT (1)
304 0x81, 0x00, // INPUT (Data,Array,Abs)
305 0xc0, // END_COLLECTION
307 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
308 0x09, 0x01, // USAGE (Consumer Control)
309 0xa1, 0x01, // COLLECTION (Application)
310 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
311 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
312 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
313 0x19, 0x01, // USAGE_MINIMUM (0x1)
314 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
315 0x75, 0x10, // REPORT_SIZE (16)
316 0x95, 0x01, // REPORT_COUNT (1)
317 0x81, 0x00, // INPUT (Data,Array,Abs)
318 0xc0, // END_COLLECTION
322 #define KBD_HID_DESC_NUM 0
323 #define KBD_HID_DESC_OFFSET (9+(9+9+7)*KBD_HID_DESC_NUM+9)
326 # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 1)
327 # define MOUSE_HID_DESC_OFFSET (9+(9+9+7)*MOUSE_HID_DESC_NUM+9)
329 # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0)
332 #define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 1)
333 #define DEBUG_HID_DESC_OFFSET (9+(9+9+7)*DEBUG_HID_DESC_NUM+9)
335 #ifdef EXTRAKEY_ENABLE
336 # define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 1)
337 # define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
339 # define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 0)
343 # define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 1)
344 # define KBD2_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
346 # define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0)
349 #define NUM_INTERFACES (KBD2_HID_DESC_NUM + 1)
350 #define CONFIG1_DESC_SIZE (9+(9+9+7)*NUM_INTERFACES)
351 static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
352 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
354 2, // bDescriptorType;
355 LSB(CONFIG1_DESC_SIZE), // wTotalLength
356 MSB(CONFIG1_DESC_SIZE),
357 NUM_INTERFACES, // bNumInterfaces
358 1, // bConfigurationValue
360 0xA0, // bmAttributes
363 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
365 4, // bDescriptorType
366 KBD_INTERFACE, // bInterfaceNumber
367 0, // bAlternateSetting
369 0x03, // bInterfaceClass (0x03 = HID)
370 0x01, // bInterfaceSubClass (0x01 = Boot)
371 0x01, // bInterfaceProtocol (0x01 = Keyboard)
373 // HID descriptor, HID 1.11 spec, section 6.2.1
375 0x21, // bDescriptorType
376 0x11, 0x01, // bcdHID
378 1, // bNumDescriptors
379 0x22, // bDescriptorType
380 sizeof(keyboard_hid_report_desc), // wDescriptorLength
382 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
384 5, // bDescriptorType
385 KBD_ENDPOINT | 0x80, // bEndpointAddress
386 0x03, // bmAttributes (0x03=intr)
387 KBD_SIZE, 0, // wMaxPacketSize
391 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
393 4, // bDescriptorType
394 MOUSE_INTERFACE, // bInterfaceNumber
395 0, // bAlternateSetting
397 0x03, // bInterfaceClass (0x03 = HID)
398 // ThinkPad T23 BIOS doesn't work with boot mouse.
399 0x00, // bInterfaceSubClass (0x01 = Boot)
400 0x00, // bInterfaceProtocol (0x02 = Mouse)
402 0x01, // bInterfaceSubClass (0x01 = Boot)
403 0x02, // bInterfaceProtocol (0x02 = Mouse)
406 // HID descriptor, HID 1.11 spec, section 6.2.1
408 0x21, // bDescriptorType
409 0x11, 0x01, // bcdHID
411 1, // bNumDescriptors
412 0x22, // bDescriptorType
413 sizeof(mouse_hid_report_desc), // wDescriptorLength
415 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
417 5, // bDescriptorType
418 MOUSE_ENDPOINT | 0x80, // bEndpointAddress
419 0x03, // bmAttributes (0x03=intr)
420 MOUSE_SIZE, 0, // wMaxPacketSize
424 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
426 4, // bDescriptorType
427 DEBUG_INTERFACE, // bInterfaceNumber
428 0, // bAlternateSetting
430 0x03, // bInterfaceClass (0x03 = HID)
431 0x00, // bInterfaceSubClass
432 0x00, // bInterfaceProtocol
434 // HID descriptor, HID 1.11 spec, section 6.2.1
436 0x21, // bDescriptorType
437 0x11, 0x01, // bcdHID
439 1, // bNumDescriptors
440 0x22, // bDescriptorType
441 sizeof(debug_hid_report_desc), // wDescriptorLength
443 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
445 5, // bDescriptorType
446 DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
447 0x03, // bmAttributes (0x03=intr)
448 DEBUG_TX_SIZE, 0, // wMaxPacketSize
451 #ifdef EXTRAKEY_ENABLE
452 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
454 4, // bDescriptorType
455 EXTRA_INTERFACE, // bInterfaceNumber
456 0, // bAlternateSetting
458 0x03, // bInterfaceClass (0x03 = HID)
459 0x00, // bInterfaceSubClass
460 0x00, // bInterfaceProtocol
462 // HID descriptor, HID 1.11 spec, section 6.2.1
464 0x21, // bDescriptorType
465 0x11, 0x01, // bcdHID
467 1, // bNumDescriptors
468 0x22, // bDescriptorType
469 sizeof(extra_hid_report_desc), // wDescriptorLength
471 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
473 5, // bDescriptorType
474 EXTRA_ENDPOINT | 0x80, // bEndpointAddress
475 0x03, // bmAttributes (0x03=intr)
476 EXTRA_SIZE, 0, // wMaxPacketSize
481 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
483 4, // bDescriptorType
484 KBD2_INTERFACE, // bInterfaceNumber
485 0, // bAlternateSetting
487 0x03, // bInterfaceClass (0x03 = HID)
488 0x00, // bInterfaceSubClass (0x01 = Boot)
489 0x00, // bInterfaceProtocol (0x01 = Keyboard)
491 // HID descriptor, HID 1.11 spec, section 6.2.1
493 0x21, // bDescriptorType
494 0x11, 0x01, // bcdHID
496 1, // bNumDescriptors
497 0x22, // bDescriptorType
498 sizeof(keyboard2_hid_report_desc), // wDescriptorLength
500 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
502 5, // bDescriptorType
503 KBD2_ENDPOINT | 0x80, // bEndpointAddress
504 0x03, // bmAttributes (0x03=intr)
505 KBD2_SIZE, 0, // wMaxPacketSize
510 // If you're desperate for a little extra code memory, these strings
511 // can be completely removed if iManufacturer, iProduct, iSerialNumber
512 // in the device desciptor are changed to zeros.
513 struct usb_string_descriptor_struct {
515 uint8_t bDescriptorType;
518 static const struct usb_string_descriptor_struct PROGMEM string0 = {
523 static const struct usb_string_descriptor_struct PROGMEM string1 = {
524 sizeof(STR_MANUFACTURER),
528 static const struct usb_string_descriptor_struct PROGMEM string2 = {
534 // This table defines which descriptor data is sent for each specific
535 // request from the host (in wValue and wIndex).
536 static const struct descriptor_list_struct {
537 uint16_t wValue; // descriptor type
541 } PROGMEM descriptor_list[] = {
543 {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
544 // CONFIGURATION descriptor
545 {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
546 // HID/REPORT descriptors
547 {0x2100, KBD_INTERFACE, config1_descriptor+KBD_HID_DESC_OFFSET, 9},
548 {0x2200, KBD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
550 {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9},
551 {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)},
553 {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
554 {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
555 #ifdef EXTRAKEY_ENABLE
556 {0x2100, EXTRA_INTERFACE, config1_descriptor+EXTRA_HID_DESC_OFFSET, 9},
557 {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)},
560 {0x2100, KBD2_INTERFACE, config1_descriptor+KBD2_HID_DESC_OFFSET, 9},
561 {0x2200, KBD2_INTERFACE, keyboard2_hid_report_desc, sizeof(keyboard2_hid_report_desc)},
563 // STRING descriptors
564 {0x0300, 0x0000, (const uint8_t *)&string0, 4},
565 {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
566 {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
568 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
571 /**************************************************************************
573 * Variables - these are the only non-stack RAM usage
575 **************************************************************************/
577 // zero when we are not configured, non-zero when enumerated
578 static volatile uint8_t usb_configuration=0;
581 /**************************************************************************
583 * Public Functions - these are the API intended for the user
585 **************************************************************************/
592 USB_FREEZE(); // enable USB
593 PLL_CONFIG(); // config PLL
594 while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
595 USB_CONFIG(); // start USB clock
596 UDCON = 0; // enable attach resistor
597 usb_configuration = 0;
598 UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
602 // return 0 if the USB is not configured, or the configuration
603 // number selected by the HOST
604 uint8_t usb_configured(void)
606 return usb_configuration && !suspend;
609 void usb_remote_wakeup(void)
611 UDCON |= (1<<RMWKUP);
616 /**************************************************************************
618 * Private Functions - not intended for general user consumption....
620 **************************************************************************/
624 // USB Device Interrupt - handle all device-level events
625 // the transmit buffer flushing is triggered by the start of frame
630 static uint8_t div4=0;
634 if (intbits & (1<<SUSPI)) {
639 if (intbits & (1<<EORSTI)) {
642 UECFG0X = EP_TYPE_CONTROL;
643 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
644 UEIENX = (1<<RXSTPE);
645 usb_configuration = 0;
647 if ((intbits & (1<<SOFI)) && usb_configuration) {
648 t = debug_flush_timer;
650 debug_flush_timer = -- t;
652 UENUM = DEBUG_TX_ENDPOINT;
653 while ((UEINTX & (1<<RWAL))) {
659 /* TODO: should keep IDLE rate on each keyboard interface */
661 if (!keyboard_nkro && usb_keyboard_idle_config && (++div4 & 3) == 0) {
663 if (usb_keyboard_idle_config && (++div4 & 3) == 0) {
665 UENUM = KBD_ENDPOINT;
666 if (UEINTX & (1<<RWAL)) {
667 usb_keyboard_idle_count++;
668 if (usb_keyboard_idle_count == usb_keyboard_idle_config) {
669 usb_keyboard_idle_count = 0;
670 /* TODO: fix keyboard_report inconsistency */
671 /* To avoid Mac SET_IDLE behaviour.
672 UEDATX = keyboard_report_prev->mods;
674 uint8_t keys = usb_keyboard_protocol ? KBD_REPORT_KEYS : 6;
675 for (uint8_t i=0; i<keys; i++) {
676 UEDATX = keyboard_report_prev->keys[i];
688 // Misc functions to wait for ready and send/receive packets
689 static inline void usb_wait_in_ready(void)
691 while (!(UEINTX & (1<<TXINI))) ;
693 static inline void usb_send_in(void)
695 UEINTX = ~(1<<TXINI);
697 static inline void usb_wait_receive_out(void)
699 while (!(UEINTX & (1<<RXOUTI))) ;
701 static inline void usb_ack_out(void)
703 UEINTX = ~(1<<RXOUTI);
708 // USB Endpoint Interrupt - endpoint 0 is handled here. The
709 // other endpoints are manipulated by the user-callable
710 // functions, and the start-of-frame interrupt.
717 uint8_t i, n, len, en;
718 uint8_t bmRequestType;
724 const uint8_t *desc_addr;
729 if (intbits & (1<<RXSTPI)) {
730 bmRequestType = UEDATX;
733 wValue |= (UEDATX << 8);
735 wIndex |= (UEDATX << 8);
737 wLength |= (UEDATX << 8);
738 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
739 if (bRequest == GET_DESCRIPTOR) {
740 list = (const uint8_t *)descriptor_list;
742 if (i >= NUM_DESC_LIST) {
743 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
746 desc_val = pgm_read_word(list);
747 if (desc_val != wValue) {
748 list += sizeof(struct descriptor_list_struct);
752 desc_val = pgm_read_word(list);
753 if (desc_val != wIndex) {
754 list += sizeof(struct descriptor_list_struct)-2;
758 desc_addr = (const uint8_t *)pgm_read_word(list);
760 desc_length = pgm_read_byte(list);
763 len = (wLength < 256) ? wLength : 255;
764 if (len > desc_length) len = desc_length;
766 // wait for host ready for IN packet
769 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
770 if (i & (1<<RXOUTI)) return; // abort
772 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
773 for (i = n; i; i--) {
774 UEDATX = pgm_read_byte(desc_addr++);
778 } while (len || n == ENDPOINT0_SIZE);
781 if (bRequest == SET_ADDRESS) {
784 UDADDR = wValue | (1<<ADDEN);
787 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
788 usb_configuration = wValue;
790 cfg = endpoint_config_table;
791 for (i=1; i<=MAX_ENDPOINT; i++) {
793 en = pgm_read_byte(cfg++);
796 UECFG0X = pgm_read_byte(cfg++);
797 UECFG1X = pgm_read_byte(cfg++);
806 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
808 UEDATX = usb_configuration;
813 if (bRequest == GET_STATUS) {
816 #ifdef SUPPORT_ENDPOINT_HALT
817 if (bmRequestType == 0x82) {
819 if (UECONX & (1<<STALLRQ)) i = 1;
828 if (bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) {
829 #ifdef SUPPORT_ENDPOINT_HALT
830 if (bmRequestType == 0x02 && wValue == ENDPOINT_HALT) {
832 if (i >= 1 && i <= MAX_ENDPOINT) {
835 if (bRequest == SET_FEATURE) {
836 UECONX = (1<<STALLRQ)|(1<<EPEN);
838 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
846 if (bmRequestType == 0x00 && wValue == DEVICE_REMOTE_WAKEUP) {
847 if (bRequest == SET_FEATURE) {
848 remote_wakeup = true;
850 remote_wakeup = false;
856 if (wIndex == KBD_INTERFACE) {
857 if (bmRequestType == 0xA1) {
858 if (bRequest == HID_GET_REPORT) {
860 UEDATX = keyboard_report->mods;
862 for (i=0; i<6; i++) {
863 UEDATX = keyboard_report->keys[i];
868 if (bRequest == HID_GET_IDLE) {
870 UEDATX = usb_keyboard_idle_config;
874 if (bRequest == HID_GET_PROTOCOL) {
876 UEDATX = usb_keyboard_protocol;
881 if (bmRequestType == 0x21) {
882 if (bRequest == HID_SET_REPORT) {
883 usb_wait_receive_out();
884 usb_keyboard_leds = UEDATX;
889 if (bRequest == HID_SET_IDLE) {
890 usb_keyboard_idle_config = (wValue >> 8);
891 usb_keyboard_idle_count = 0;
892 //usb_wait_in_ready();
896 if (bRequest == HID_SET_PROTOCOL) {
897 usb_keyboard_protocol = wValue;
898 //usb_wait_in_ready();
905 if (wIndex == MOUSE_INTERFACE) {
906 if (bmRequestType == 0xA1) {
907 if (bRequest == HID_GET_REPORT) {
908 if (wValue == HID_REPORT_INPUT) {
917 if (wValue == HID_REPORT_FEATURE) {
924 if (bRequest == HID_GET_PROTOCOL) {
926 UEDATX = usb_mouse_protocol;
931 if (bmRequestType == 0x21) {
932 if (bRequest == HID_SET_PROTOCOL) {
933 usb_mouse_protocol = wValue;
940 if (wIndex == DEBUG_INTERFACE) {
941 if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
944 // wait for host ready for IN packet
947 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
948 if (i & (1<<RXOUTI)) return; // abort
950 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
951 for (i = n; i; i--) {
956 } while (len || n == ENDPOINT0_SIZE);
961 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall