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
25 #include <avr/pgmspace.h>
26 #include <avr/interrupt.h>
28 #include "usb_keyboard.h"
29 #include "usb_mouse.h"
30 #include "usb_debug.h"
33 /**************************************************************************
35 * Configurable Options
37 **************************************************************************/
39 // You can change these to give your code its own name.
40 #define STR_MANUFACTURER L"t.m.k."
41 #define STR_PRODUCT L"t.m.k. keyboard"
44 // Mac OS-X and Linux automatically load the correct drivers. On
45 // Windows, even though the driver is supplied by Microsoft, an
46 // INF file is needed to load the driver. These numbers need to
47 // match the INF file.
48 #define VENDOR_ID 0xFEED
49 #define PRODUCT_ID 0xBABE
52 // USB devices are supposed to implment a halt feature, which is
53 // rarely (if ever) used. If you comment this line out, the halt
54 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
55 // This is not strictly USB compliant, but works with all major
57 #define SUPPORT_ENDPOINT_HALT
61 /**************************************************************************
63 * Endpoint Buffer Configuration
65 **************************************************************************/
67 #define ENDPOINT0_SIZE 32
69 // 0:control endpoint is enabled automatically by controller.
70 static const uint8_t PROGMEM endpoint_config_table
[] = {
71 // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
72 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(KEYBOARD_SIZE
) | KEYBOARD_BUFFER
, // 1
73 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(MOUSE_SIZE
) | MOUSE_BUFFER
, // 2
74 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(DEBUG_TX_SIZE
) | DEBUG_TX_BUFFER
, // 3
81 /**************************************************************************
85 **************************************************************************/
87 // Descriptors are the data that your computer reads when it auto-detects
88 // this USB device (called "enumeration" in USB lingo). The most commonly
89 // changed items are editable at the top of this file. Changing things
90 // in here should only be done by those who've read chapter 9 of the USB
91 // spec and relevant portions of any USB class specifications!
94 static uint8_t PROGMEM device_descriptor
[] = {
100 0, // bDeviceProtocol
101 ENDPOINT0_SIZE
, // bMaxPacketSize0
102 LSB(VENDOR_ID
), MSB(VENDOR_ID
), // idVendor
103 LSB(PRODUCT_ID
), MSB(PRODUCT_ID
), // idProduct
104 0x00, 0x01, // bcdDevice
108 1 // bNumConfigurations
111 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
112 static uint8_t PROGMEM keyboard_hid_report_desc
[] = {
113 0x05, 0x01, // Usage Page (Generic Desktop),
114 0x09, 0x06, // Usage (Keyboard),
115 0xA1, 0x01, // Collection (Application),
116 0x75, 0x01, // Report Size (1),
117 0x95, 0x08, // Report Count (8),
118 0x05, 0x07, // Usage Page (Key Codes),
119 0x19, 0xE0, // Usage Minimum (224),
120 0x29, 0xE7, // Usage Maximum (231),
121 0x15, 0x00, // Logical Minimum (0),
122 0x25, 0x01, // Logical Maximum (1),
123 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
124 0x95, 0x01, // Report Count (1),
125 0x75, 0x08, // Report Size (8),
126 0x81, 0x03, // Input (Constant), ;Reserved byte
127 0x95, 0x05, // Report Count (5),
128 0x75, 0x01, // Report Size (1),
129 0x05, 0x08, // Usage Page (LEDs),
130 0x19, 0x01, // Usage Minimum (1),
131 0x29, 0x05, // Usage Maximum (5),
132 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
133 0x95, 0x01, // Report Count (1),
134 0x75, 0x03, // Report Size (3),
135 0x91, 0x03, // Output (Constant), ;LED report padding
136 0x95, 0x06, // Report Count (6),
137 0x75, 0x08, // Report Size (8),
138 0x15, 0x00, // Logical Minimum (0),
139 0x25, 0x68, // Logical Maximum(104),
140 0x05, 0x07, // Usage Page (Key Codes),
141 0x19, 0x00, // Usage Minimum (0),
142 0x29, 0x68, // Usage Maximum (104),
143 0x81, 0x00, // Input (Data, Array),
144 0xc0 // End Collection
147 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
148 static uint8_t PROGMEM mouse_hid_report_desc
[] = {
149 0x05, 0x01, // Usage Page (Generic Desktop)
150 0x09, 0x02, // Usage (Mouse)
151 0xA1, 0x01, // Collection (Application)
152 0x05, 0x09, // Usage Page (Button)
153 0x19, 0x01, // Usage Minimum (Button #1)
154 0x29, 0x03, // Usage Maximum (Button #3)
155 0x15, 0x00, // Logical Minimum (0)
156 0x25, 0x01, // Logical Maximum (1)
157 0x95, 0x03, // Report Count (3)
158 0x75, 0x01, // Report Size (1)
159 0x81, 0x02, // Input (Data, Variable, Absolute)
160 0x95, 0x01, // Report Count (1)
161 0x75, 0x05, // Report Size (5)
162 0x81, 0x03, // Input (Constant)
163 0x05, 0x01, // Usage Page (Generic Desktop)
164 0x09, 0x30, // Usage (X)
165 0x09, 0x31, // Usage (Y)
166 0x15, 0x81, // Logical Minimum (-127)
167 0x25, 0x7F, // Logical Maximum (127)
168 0x75, 0x08, // Report Size (8),
169 0x95, 0x02, // Report Count (2),
170 0x81, 0x06, // Input (Data, Variable, Relative)
171 0x09, 0x38, // Usage (Wheel)
172 0x95, 0x01, // Report Count (1),
173 0x81, 0x06, // Input (Data, Variable, Relative)
174 0xC0 // End Collection
177 static uint8_t PROGMEM debug_hid_report_desc
[] = {
178 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
179 0x09, 0x74, // Usage 0x74
180 0xA1, 0x53, // Collection 0x53
181 0x75, 0x08, // report size = 8 bits
182 0x15, 0x00, // logical minimum = 0
183 0x26, 0xFF, 0x00, // logical maximum = 255
184 0x95, DEBUG_TX_SIZE
, // report count
186 0x81, 0x02, // Input (array)
187 0xC0 // end collection
190 #define CONFIG1_DESC_SIZE (9+(9+9+7)+(9+9+7)+(9+9+7))
191 #define KEYBOARD_HID_DESC_OFFSET (9+9)
192 #define MOUSE_HID_DESC_OFFSET (9+(9+9+7)+9)
193 #define DEBUG_HID_DESC_OFFSET (9+(9+9+7)+(9+9+7)+9)
194 static uint8_t PROGMEM config1_descriptor
[CONFIG1_DESC_SIZE
] = {
195 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
197 2, // bDescriptorType;
198 LSB(CONFIG1_DESC_SIZE
), // wTotalLength
199 MSB(CONFIG1_DESC_SIZE
),
201 1, // bConfigurationValue
203 0xC0, // bmAttributes
206 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
208 4, // bDescriptorType
209 KEYBOARD_INTERFACE
, // bInterfaceNumber
210 0, // bAlternateSetting
212 0x03, // bInterfaceClass (0x03 = HID)
213 0x01, // bInterfaceSubClass (0x01 = Boot)
214 0x01, // bInterfaceProtocol (0x01 = Keyboard)
216 // HID descriptor, HID 1.11 spec, section 6.2.1
218 0x21, // bDescriptorType
219 0x11, 0x01, // bcdHID
221 1, // bNumDescriptors
222 0x22, // bDescriptorType
223 sizeof(keyboard_hid_report_desc
), // wDescriptorLength
225 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
227 5, // bDescriptorType
228 KEYBOARD_ENDPOINT
| 0x80, // bEndpointAddress
229 0x03, // bmAttributes (0x03=intr)
230 KEYBOARD_SIZE
, 0, // wMaxPacketSize
233 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
235 4, // bDescriptorType
236 MOUSE_INTERFACE
, // bInterfaceNumber
237 0, // bAlternateSetting
239 0x03, // bInterfaceClass (0x03 = HID)
240 0x01, // bInterfaceSubClass (0x01 = Boot)
241 0x02, // bInterfaceProtocol (0x02 = Mouse)
243 // HID descriptor, HID 1.11 spec, section 6.2.1
245 0x21, // bDescriptorType
246 0x11, 0x01, // bcdHID
248 1, // bNumDescriptors
249 0x22, // bDescriptorType
250 sizeof(mouse_hid_report_desc
), // wDescriptorLength
252 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
254 5, // bDescriptorType
255 MOUSE_ENDPOINT
| 0x80, // bEndpointAddress
256 0x03, // bmAttributes (0x03=intr)
257 4, 0, // wMaxPacketSize
260 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
262 4, // bDescriptorType
263 DEBUG_INTERFACE
, // bInterfaceNumber
264 0, // bAlternateSetting
266 0x03, // bInterfaceClass (0x03 = HID)
267 0x00, // bInterfaceSubClass
268 0x00, // bInterfaceProtocol
270 // HID descriptor, HID 1.11 spec, section 6.2.1
272 0x21, // bDescriptorType
273 0x11, 0x01, // bcdHID
275 1, // bNumDescriptors
276 0x22, // bDescriptorType
277 sizeof(debug_hid_report_desc
), // wDescriptorLength
279 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
281 5, // bDescriptorType
282 DEBUG_TX_ENDPOINT
| 0x80, // bEndpointAddress
283 0x03, // bmAttributes (0x03=intr)
284 DEBUG_TX_SIZE
, 0, // wMaxPacketSize
288 // If you're desperate for a little extra code memory, these strings
289 // can be completely removed if iManufacturer, iProduct, iSerialNumber
290 // in the device desciptor are changed to zeros.
291 struct usb_string_descriptor_struct
{
293 uint8_t bDescriptorType
;
296 static struct usb_string_descriptor_struct PROGMEM string0
= {
301 static struct usb_string_descriptor_struct PROGMEM string1
= {
302 sizeof(STR_MANUFACTURER
),
306 static struct usb_string_descriptor_struct PROGMEM string2
= {
312 // This table defines which descriptor data is sent for each specific
313 // request from the host (in wValue and wIndex).
314 static struct descriptor_list_struct
{
315 uint16_t wValue
; // descriptor type
319 } PROGMEM descriptor_list
[] = {
321 {0x0100, 0x0000, device_descriptor
, sizeof(device_descriptor
)},
322 // CONFIGURATION descriptor
323 {0x0200, 0x0000, config1_descriptor
, sizeof(config1_descriptor
)},
325 {0x2200, KEYBOARD_INTERFACE
, keyboard_hid_report_desc
, sizeof(keyboard_hid_report_desc
)},
326 {0x2100, KEYBOARD_INTERFACE
, config1_descriptor
+KEYBOARD_HID_DESC_OFFSET
, 9},
328 {0x2200, MOUSE_INTERFACE
, mouse_hid_report_desc
, sizeof(mouse_hid_report_desc
)},
329 {0x2100, MOUSE_INTERFACE
, config1_descriptor
+MOUSE_HID_DESC_OFFSET
, 9},
331 {0x2200, DEBUG_INTERFACE
, debug_hid_report_desc
, sizeof(debug_hid_report_desc
)},
332 {0x2100, DEBUG_INTERFACE
, config1_descriptor
+DEBUG_HID_DESC_OFFSET
, 9},
334 {0x0300, 0x0000, (const uint8_t *)&string0
, 4},
335 {0x0301, 0x0409, (const uint8_t *)&string1
, sizeof(STR_MANUFACTURER
)},
336 {0x0302, 0x0409, (const uint8_t *)&string2
, sizeof(STR_PRODUCT
)}
338 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
341 /**************************************************************************
343 * Variables - these are the only non-stack RAM usage
345 **************************************************************************/
347 // zero when we are not configured, non-zero when enumerated
348 static volatile uint8_t usb_configuration
=0;
351 /**************************************************************************
353 * Public Functions - these are the API intended for the user
355 **************************************************************************/
362 USB_FREEZE(); // enable USB
363 PLL_CONFIG(); // config PLL
364 while (!(PLLCSR
& (1<<PLOCK
))) ; // wait for PLL lock
365 USB_CONFIG(); // start USB clock
366 UDCON
= 0; // enable attach resistor
367 usb_configuration
= 0;
368 UDIEN
= (1<<EORSTE
)|(1<<SOFE
);
372 // return 0 if the USB is not configured, or the configuration
373 // number selected by the HOST
374 uint8_t usb_configured(void)
376 return usb_configuration
;
381 /**************************************************************************
383 * Private Functions - not intended for general user consumption....
385 **************************************************************************/
389 // USB Device Interrupt - handle all device-level events
390 // the transmit buffer flushing is triggered by the start of frame
394 uint8_t intbits
, t
, i
;
395 static uint8_t div4
=0;
399 if (intbits
& (1<<EORSTI
)) {
402 UECFG0X
= EP_TYPE_CONTROL
;
403 UECFG1X
= EP_SIZE(ENDPOINT0_SIZE
) | EP_SINGLE_BUFFER
;
404 UEIENX
= (1<<RXSTPE
);
405 usb_configuration
= 0;
407 if ((intbits
& (1<<SOFI
)) && usb_configuration
) {
408 t
= debug_flush_timer
;
410 debug_flush_timer
= -- t
;
412 UENUM
= DEBUG_TX_ENDPOINT
;
413 while ((UEINTX
& (1<<RWAL
))) {
419 if (keyboard_idle_config
&& (++div4
& 3) == 0) {
420 UENUM
= KEYBOARD_ENDPOINT
;
421 if (UEINTX
& (1<<RWAL
)) {
422 keyboard_idle_count
++;
423 if (keyboard_idle_count
== keyboard_idle_config
) {
424 keyboard_idle_count
= 0;
425 UEDATX
= keyboard_modifier_keys
;
427 for (i
=0; i
<6; i
++) {
428 UEDATX
= keyboard_keys
[i
];
439 // Misc functions to wait for ready and send/receive packets
440 static inline void usb_wait_in_ready(void)
442 while (!(UEINTX
& (1<<TXINI
))) ;
444 static inline void usb_send_in(void)
446 UEINTX
= ~(1<<TXINI
);
448 static inline void usb_wait_receive_out(void)
450 while (!(UEINTX
& (1<<RXOUTI
))) ;
452 static inline void usb_ack_out(void)
454 UEINTX
= ~(1<<RXOUTI
);
459 // USB Endpoint Interrupt - endpoint 0 is handled here. The
460 // other endpoints are manipulated by the user-callable
461 // functions, and the start-of-frame interrupt.
468 uint8_t i
, n
, len
, en
;
469 uint8_t bmRequestType
;
475 const uint8_t *desc_addr
;
480 if (intbits
& (1<<RXSTPI
)) {
481 bmRequestType
= UEDATX
;
484 wValue
|= (UEDATX
<< 8);
486 wIndex
|= (UEDATX
<< 8);
488 wLength
|= (UEDATX
<< 8);
489 UEINTX
= ~((1<<RXSTPI
) | (1<<RXOUTI
) | (1<<TXINI
));
490 if (bRequest
== GET_DESCRIPTOR
) {
491 list
= (const uint8_t *)descriptor_list
;
493 if (i
>= NUM_DESC_LIST
) {
494 UECONX
= (1<<STALLRQ
)|(1<<EPEN
); //stall
497 desc_val
= pgm_read_word(list
);
498 if (desc_val
!= wValue
) {
499 list
+= sizeof(struct descriptor_list_struct
);
503 desc_val
= pgm_read_word(list
);
504 if (desc_val
!= wIndex
) {
505 list
+= sizeof(struct descriptor_list_struct
)-2;
509 desc_addr
= (const uint8_t *)pgm_read_word(list
);
511 desc_length
= pgm_read_byte(list
);
514 len
= (wLength
< 256) ? wLength
: 255;
515 if (len
> desc_length
) len
= desc_length
;
517 // wait for host ready for IN packet
520 } while (!(i
& ((1<<TXINI
)|(1<<RXOUTI
))));
521 if (i
& (1<<RXOUTI
)) return; // abort
523 n
= len
< ENDPOINT0_SIZE
? len
: ENDPOINT0_SIZE
;
524 for (i
= n
; i
; i
--) {
525 UEDATX
= pgm_read_byte(desc_addr
++);
529 } while (len
|| n
== ENDPOINT0_SIZE
);
532 if (bRequest
== SET_ADDRESS
) {
535 UDADDR
= wValue
| (1<<ADDEN
);
538 if (bRequest
== SET_CONFIGURATION
&& bmRequestType
== 0) {
539 usb_configuration
= wValue
;
541 cfg
= endpoint_config_table
;
542 for (i
=1; i
<=6; i
++) {
544 en
= pgm_read_byte(cfg
++);
547 UECFG0X
= pgm_read_byte(cfg
++);
548 UECFG1X
= pgm_read_byte(cfg
++);
555 if (bRequest
== GET_CONFIGURATION
&& bmRequestType
== 0x80) {
557 UEDATX
= usb_configuration
;
562 if (bRequest
== GET_STATUS
) {
565 #ifdef SUPPORT_ENDPOINT_HALT
566 if (bmRequestType
== 0x82) {
568 if (UECONX
& (1<<STALLRQ
)) i
= 1;
577 #ifdef SUPPORT_ENDPOINT_HALT
578 if ((bRequest
== CLEAR_FEATURE
|| bRequest
== SET_FEATURE
)
579 && bmRequestType
== 0x02 && wValue
== 0) {
581 if (i
>= 1 && i
<= MAX_ENDPOINT
) {
584 if (bRequest
== SET_FEATURE
) {
585 UECONX
= (1<<STALLRQ
)|(1<<EPEN
);
587 UECONX
= (1<<STALLRQC
)|(1<<RSTDT
)|(1<<EPEN
);
595 if (wIndex
== KEYBOARD_INTERFACE
) {
596 if (bmRequestType
== 0xA1) {
597 if (bRequest
== HID_GET_REPORT
) {
599 UEDATX
= keyboard_modifier_keys
;
601 for (i
=0; i
<6; i
++) {
602 UEDATX
= keyboard_keys
[i
];
607 if (bRequest
== HID_GET_IDLE
) {
609 UEDATX
= keyboard_idle_config
;
613 if (bRequest
== HID_GET_PROTOCOL
) {
615 UEDATX
= keyboard_protocol
;
620 if (bmRequestType
== 0x21) {
621 if (bRequest
== HID_SET_REPORT
) {
622 usb_wait_receive_out();
623 keyboard_leds
= UEDATX
;
628 if (bRequest
== HID_SET_IDLE
) {
629 keyboard_idle_config
= (wValue
>> 8);
630 keyboard_idle_count
= 0;
631 //usb_wait_in_ready();
635 if (bRequest
== HID_SET_PROTOCOL
) {
636 keyboard_protocol
= wValue
;
637 //usb_wait_in_ready();
643 if (wIndex
== MOUSE_INTERFACE
) {
644 if (bmRequestType
== 0xA1) {
645 if (bRequest
== HID_GET_REPORT
) {
647 UEDATX
= mouse_buttons
;
654 if (bRequest
== HID_GET_PROTOCOL
) {
656 UEDATX
= mouse_protocol
;
661 if (bmRequestType
== 0x21) {
662 if (bRequest
== HID_SET_PROTOCOL
) {
663 mouse_protocol
= wValue
;
669 if (wIndex
== DEBUG_INTERFACE
) {
670 if (bRequest
== HID_GET_REPORT
&& bmRequestType
== 0xA1) {
673 // wait for host ready for IN packet
676 } while (!(i
& ((1<<TXINI
)|(1<<RXOUTI
))));
677 if (i
& (1<<RXOUTI
)) return; // abort
679 n
= len
< ENDPOINT0_SIZE
? len
: ENDPOINT0_SIZE
;
680 for (i
= n
; i
; i
--) {
685 } while (len
|| n
== ENDPOINT0_SIZE
);
690 UECONX
= (1<<STALLRQ
) | (1<<EPEN
); // stall