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
24 #include <avr/pgmspace.h>
25 #include <avr/interrupt.h>
26 #include "usb_device.h"
27 #include "usb_keyboard.h"
28 #include "usb_debug.h"
31 /**************************************************************************
33 * Configurable Options
35 **************************************************************************/
37 // You can change these to give your code its own name.
38 #define STR_MANUFACTURER L"t.m.k."
39 #define STR_PRODUCT L"t.m.k. keyboard"
42 // Mac OS-X and Linux automatically load the correct drivers. On
43 // Windows, even though the driver is supplied by Microsoft, an
44 // INF file is needed to load the driver. These numbers need to
45 // match the INF file.
46 #define VENDOR_ID 0xFEED
47 #define PRODUCT_ID 0xBABE
50 // USB devices are supposed to implment a halt feature, which is
51 // rarely (if ever) used. If you comment this line out, the halt
52 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
53 // This is not strictly USB compliant, but works with all major
55 #define SUPPORT_ENDPOINT_HALT
59 /**************************************************************************
61 * Endpoint Buffer Configuration
63 **************************************************************************/
65 #define ENDPOINT0_SIZE 32
67 static const uint8_t PROGMEM endpoint_config_table
[] = {
70 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(KEYBOARD_SIZE
) | KEYBOARD_BUFFER
,
71 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(DEBUG_TX_SIZE
) | DEBUG_TX_BUFFER
75 /**************************************************************************
79 **************************************************************************/
81 // Descriptors are the data that your computer reads when it auto-detects
82 // this USB device (called "enumeration" in USB lingo). The most commonly
83 // changed items are editable at the top of this file. Changing things
84 // in here should only be done by those who've read chapter 9 of the USB
85 // spec and relevant portions of any USB class specifications!
88 static uint8_t PROGMEM device_descriptor
[] = {
95 ENDPOINT0_SIZE
, // bMaxPacketSize0
96 LSB(VENDOR_ID
), MSB(VENDOR_ID
), // idVendor
97 LSB(PRODUCT_ID
), MSB(PRODUCT_ID
), // idProduct
98 0x00, 0x01, // bcdDevice
102 1 // bNumConfigurations
105 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
106 static uint8_t PROGMEM keyboard_hid_report_desc
[] = {
107 0x05, 0x01, // Usage Page (Generic Desktop),
108 0x09, 0x06, // Usage (Keyboard),
109 0xA1, 0x01, // Collection (Application),
110 0x75, 0x01, // Report Size (1),
111 0x95, 0x08, // Report Count (8),
112 0x05, 0x07, // Usage Page (Key Codes),
113 0x19, 0xE0, // Usage Minimum (224),
114 0x29, 0xE7, // Usage Maximum (231),
115 0x15, 0x00, // Logical Minimum (0),
116 0x25, 0x01, // Logical Maximum (1),
117 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
118 0x95, 0x01, // Report Count (1),
119 0x75, 0x08, // Report Size (8),
120 0x81, 0x03, // Input (Constant), ;Reserved byte
121 0x95, 0x05, // Report Count (5),
122 0x75, 0x01, // Report Size (1),
123 0x05, 0x08, // Usage Page (LEDs),
124 0x19, 0x01, // Usage Minimum (1),
125 0x29, 0x05, // Usage Maximum (5),
126 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
127 0x95, 0x01, // Report Count (1),
128 0x75, 0x03, // Report Size (3),
129 0x91, 0x03, // Output (Constant), ;LED report padding
130 0x95, 0x06, // Report Count (6),
131 0x75, 0x08, // Report Size (8),
132 0x15, 0x00, // Logical Minimum (0),
133 0x25, 0x68, // Logical Maximum(104),
134 0x05, 0x07, // Usage Page (Key Codes),
135 0x19, 0x00, // Usage Minimum (0),
136 0x29, 0x68, // Usage Maximum (104),
137 0x81, 0x00, // Input (Data, Array),
138 0xc0 // End Collection
141 static uint8_t PROGMEM debug_hid_report_desc
[] = {
142 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
143 0x09, 0x74, // Usage 0x74
144 0xA1, 0x53, // Collection 0x53
145 0x75, 0x08, // report size = 8 bits
146 0x15, 0x00, // logical minimum = 0
147 0x26, 0xFF, 0x00, // logical maximum = 255
148 0x95, DEBUG_TX_SIZE
, // report count
150 0x81, 0x02, // Input (array)
151 0xC0 // end collection
154 #define CONFIG1_DESC_SIZE (9+9+9+7+9+9+7)
155 #define KEYBOARD_HID_DESC_OFFSET (9+9)
156 #define DEBUG_HID_DESC_OFFSET (9+9+9+7+9)
157 static uint8_t PROGMEM config1_descriptor
[CONFIG1_DESC_SIZE
] = {
158 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
160 2, // bDescriptorType;
161 LSB(CONFIG1_DESC_SIZE
), // wTotalLength
162 MSB(CONFIG1_DESC_SIZE
),
164 1, // bConfigurationValue
166 0xC0, // bmAttributes
168 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
170 4, // bDescriptorType
171 KEYBOARD_INTERFACE
, // bInterfaceNumber
172 0, // bAlternateSetting
174 0x03, // bInterfaceClass (0x03 = HID)
175 0x01, // bInterfaceSubClass (0x01 = Boot)
176 0x01, // bInterfaceProtocol (0x01 = Keyboard)
178 // HID interface descriptor, HID 1.11 spec, section 6.2.1
180 0x21, // bDescriptorType
181 0x11, 0x01, // bcdHID
183 1, // bNumDescriptors
184 0x22, // bDescriptorType
185 sizeof(keyboard_hid_report_desc
), // wDescriptorLength
187 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
189 5, // bDescriptorType
190 KEYBOARD_ENDPOINT
| 0x80, // bEndpointAddress
191 0x03, // bmAttributes (0x03=intr)
192 KEYBOARD_SIZE
, 0, // wMaxPacketSize
194 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
196 4, // bDescriptorType
197 DEBUG_INTERFACE
, // bInterfaceNumber
198 0, // bAlternateSetting
200 0x03, // bInterfaceClass (0x03 = HID)
201 0x00, // bInterfaceSubClass
202 0x00, // bInterfaceProtocol
204 // HID interface descriptor, HID 1.11 spec, section 6.2.1
206 0x21, // bDescriptorType
207 0x11, 0x01, // bcdHID
209 1, // bNumDescriptors
210 0x22, // bDescriptorType
211 sizeof(debug_hid_report_desc
), // wDescriptorLength
213 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
215 5, // bDescriptorType
216 DEBUG_TX_ENDPOINT
| 0x80, // bEndpointAddress
217 0x03, // bmAttributes (0x03=intr)
218 DEBUG_TX_SIZE
, 0, // wMaxPacketSize
222 // If you're desperate for a little extra code memory, these strings
223 // can be completely removed if iManufacturer, iProduct, iSerialNumber
224 // in the device desciptor are changed to zeros.
225 struct usb_string_descriptor_struct
{
227 uint8_t bDescriptorType
;
230 static struct usb_string_descriptor_struct PROGMEM string0
= {
235 static struct usb_string_descriptor_struct PROGMEM string1
= {
236 sizeof(STR_MANUFACTURER
),
240 static struct usb_string_descriptor_struct PROGMEM string2
= {
246 // This table defines which descriptor data is sent for each specific
247 // request from the host (in wValue and wIndex).
248 static struct descriptor_list_struct
{
249 uint16_t wValue
; // descriptor type
253 } PROGMEM descriptor_list
[] = {
255 {0x0100, 0x0000, device_descriptor
, sizeof(device_descriptor
)},
256 // CONFIGURATION descriptor
257 {0x0200, 0x0000, config1_descriptor
, sizeof(config1_descriptor
)},
259 {0x2200, KEYBOARD_INTERFACE
, keyboard_hid_report_desc
, sizeof(keyboard_hid_report_desc
)},
260 {0x2100, KEYBOARD_INTERFACE
, config1_descriptor
+KEYBOARD_HID_DESC_OFFSET
, 9},
262 {0x2200, DEBUG_INTERFACE
, debug_hid_report_desc
, sizeof(debug_hid_report_desc
)},
263 {0x2100, DEBUG_INTERFACE
, config1_descriptor
+DEBUG_HID_DESC_OFFSET
, 9},
265 {0x0300, 0x0000, (const uint8_t *)&string0
, 4},
266 {0x0301, 0x0409, (const uint8_t *)&string1
, sizeof(STR_MANUFACTURER
)},
267 {0x0302, 0x0409, (const uint8_t *)&string2
, sizeof(STR_PRODUCT
)}
269 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
272 /**************************************************************************
274 * Variables - these are the only non-stack RAM usage
276 **************************************************************************/
278 // zero when we are not configured, non-zero when enumerated
279 static volatile uint8_t usb_configuration
=0;
282 /**************************************************************************
284 * Public Functions - these are the API intended for the user
286 **************************************************************************/
293 USB_FREEZE(); // enable USB
294 PLL_CONFIG(); // config PLL
295 while (!(PLLCSR
& (1<<PLOCK
))) ; // wait for PLL lock
296 USB_CONFIG(); // start USB clock
297 UDCON
= 0; // enable attach resistor
298 usb_configuration
= 0;
299 UDIEN
= (1<<EORSTE
)|(1<<SOFE
);
303 // return 0 if the USB is not configured, or the configuration
304 // number selected by the HOST
305 uint8_t usb_configured(void)
307 return usb_configuration
;
312 /**************************************************************************
314 * Private Functions - not intended for general user consumption....
316 **************************************************************************/
320 // USB Device Interrupt - handle all device-level events
321 // the transmit buffer flushing is triggered by the start of frame
325 uint8_t intbits
, t
, i
;
326 static uint8_t div4
=0;
330 if (intbits
& (1<<EORSTI
)) {
333 UECFG0X
= EP_TYPE_CONTROL
;
334 UECFG1X
= EP_SIZE(ENDPOINT0_SIZE
) | EP_SINGLE_BUFFER
;
335 UEIENX
= (1<<RXSTPE
);
336 usb_configuration
= 0;
338 if ((intbits
& (1<<SOFI
)) && usb_configuration
) {
339 t
= debug_flush_timer
;
341 debug_flush_timer
= -- t
;
343 UENUM
= DEBUG_TX_ENDPOINT
;
344 while ((UEINTX
& (1<<RWAL
))) {
350 if (keyboard_idle_config
&& (++div4
& 3) == 0) {
351 UENUM
= KEYBOARD_ENDPOINT
;
352 if (UEINTX
& (1<<RWAL
)) {
353 keyboard_idle_count
++;
354 if (keyboard_idle_count
== keyboard_idle_config
) {
355 keyboard_idle_count
= 0;
356 UEDATX
= keyboard_modifier_keys
;
358 for (i
=0; i
<6; i
++) {
359 UEDATX
= keyboard_keys
[i
];
370 // Misc functions to wait for ready and send/receive packets
371 static inline void usb_wait_in_ready(void)
373 while (!(UEINTX
& (1<<TXINI
))) ;
375 static inline void usb_send_in(void)
377 UEINTX
= ~(1<<TXINI
);
379 static inline void usb_wait_receive_out(void)
381 while (!(UEINTX
& (1<<RXOUTI
))) ;
383 static inline void usb_ack_out(void)
385 UEINTX
= ~(1<<RXOUTI
);
390 // USB Endpoint Interrupt - endpoint 0 is handled here. The
391 // other endpoints are manipulated by the user-callable
392 // functions, and the start-of-frame interrupt.
399 uint8_t i
, n
, len
, en
;
400 uint8_t bmRequestType
;
406 const uint8_t *desc_addr
;
411 if (intbits
& (1<<RXSTPI
)) {
412 bmRequestType
= UEDATX
;
415 wValue
|= (UEDATX
<< 8);
417 wIndex
|= (UEDATX
<< 8);
419 wLength
|= (UEDATX
<< 8);
420 UEINTX
= ~((1<<RXSTPI
) | (1<<RXOUTI
) | (1<<TXINI
));
421 if (bRequest
== GET_DESCRIPTOR
) {
422 list
= (const uint8_t *)descriptor_list
;
424 if (i
>= NUM_DESC_LIST
) {
425 UECONX
= (1<<STALLRQ
)|(1<<EPEN
); //stall
428 desc_val
= pgm_read_word(list
);
429 if (desc_val
!= wValue
) {
430 list
+= sizeof(struct descriptor_list_struct
);
434 desc_val
= pgm_read_word(list
);
435 if (desc_val
!= wIndex
) {
436 list
+= sizeof(struct descriptor_list_struct
)-2;
440 desc_addr
= (const uint8_t *)pgm_read_word(list
);
442 desc_length
= pgm_read_byte(list
);
445 len
= (wLength
< 256) ? wLength
: 255;
446 if (len
> desc_length
) len
= desc_length
;
448 // wait for host ready for IN packet
451 } while (!(i
& ((1<<TXINI
)|(1<<RXOUTI
))));
452 if (i
& (1<<RXOUTI
)) return; // abort
454 n
= len
< ENDPOINT0_SIZE
? len
: ENDPOINT0_SIZE
;
455 for (i
= n
; i
; i
--) {
456 UEDATX
= pgm_read_byte(desc_addr
++);
460 } while (len
|| n
== ENDPOINT0_SIZE
);
463 if (bRequest
== SET_ADDRESS
) {
466 UDADDR
= wValue
| (1<<ADDEN
);
469 if (bRequest
== SET_CONFIGURATION
&& bmRequestType
== 0) {
470 usb_configuration
= wValue
;
472 cfg
= endpoint_config_table
;
473 for (i
=1; i
<5; i
++) {
475 en
= pgm_read_byte(cfg
++);
478 UECFG0X
= pgm_read_byte(cfg
++);
479 UECFG1X
= pgm_read_byte(cfg
++);
486 if (bRequest
== GET_CONFIGURATION
&& bmRequestType
== 0x80) {
488 UEDATX
= usb_configuration
;
493 if (bRequest
== GET_STATUS
) {
496 #ifdef SUPPORT_ENDPOINT_HALT
497 if (bmRequestType
== 0x82) {
499 if (UECONX
& (1<<STALLRQ
)) i
= 1;
508 #ifdef SUPPORT_ENDPOINT_HALT
509 if ((bRequest
== CLEAR_FEATURE
|| bRequest
== SET_FEATURE
)
510 && bmRequestType
== 0x02 && wValue
== 0) {
512 if (i
>= 1 && i
<= MAX_ENDPOINT
) {
515 if (bRequest
== SET_FEATURE
) {
516 UECONX
= (1<<STALLRQ
)|(1<<EPEN
);
518 UECONX
= (1<<STALLRQC
)|(1<<RSTDT
)|(1<<EPEN
);
526 if (wIndex
== KEYBOARD_INTERFACE
) {
527 if (bmRequestType
== 0xA1) {
528 if (bRequest
== HID_GET_REPORT
) {
530 UEDATX
= keyboard_modifier_keys
;
532 for (i
=0; i
<6; i
++) {
533 UEDATX
= keyboard_keys
[i
];
538 if (bRequest
== HID_GET_IDLE
) {
540 UEDATX
= keyboard_idle_config
;
544 if (bRequest
== HID_GET_PROTOCOL
) {
546 UEDATX
= keyboard_protocol
;
551 if (bmRequestType
== 0x21) {
552 if (bRequest
== HID_SET_REPORT
) {
553 usb_wait_receive_out();
554 keyboard_leds
= UEDATX
;
559 if (bRequest
== HID_SET_IDLE
) {
560 keyboard_idle_config
= (wValue
>> 8);
561 keyboard_idle_count
= 0;
562 //usb_wait_in_ready();
566 if (bRequest
== HID_SET_PROTOCOL
) {
567 keyboard_protocol
= wValue
;
568 //usb_wait_in_ready();
574 if (wIndex
== DEBUG_INTERFACE
) {
575 if (bRequest
== HID_GET_REPORT
&& bmRequestType
== 0xA1) {
578 // wait for host ready for IN packet
581 } while (!(i
& ((1<<TXINI
)|(1<<RXOUTI
))));
582 if (i
& (1<<RXOUTI
)) return; // abort
584 n
= len
< ENDPOINT0_SIZE
? len
: ENDPOINT0_SIZE
;
585 for (i
= n
; i
; i
--) {
590 } while (len
|| n
== ENDPOINT0_SIZE
);
595 UECONX
= (1<<STALLRQ
) | (1<<EPEN
); // stall