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"
35 /**************************************************************************
37 * Configurable Options
39 **************************************************************************/
41 // You can change these to give your code its own name.
43 # define STR_MANUFACTURER L"t.m.k."
45 # define STR_MANUFACTURER LSTR(MANUFACTURER)
48 # define STR_PRODUCT L"t.m.k. keyboard"
50 # define STR_PRODUCT LSTR(PRODUCT)
54 // Mac OS-X and Linux automatically load the correct drivers. On
55 // Windows, even though the driver is supplied by Microsoft, an
56 // INF file is needed to load the driver. These numbers need to
57 // match the INF file.
59 # define VENDOR_ID 0xFEED
63 # define PRODUCT_ID 0xBABE
67 // USB devices are supposed to implment a halt feature, which is
68 // rarely (if ever) used. If you comment this line out, the halt
69 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
70 // This is not strictly USB compliant, but works with all major
72 #define SUPPORT_ENDPOINT_HALT
76 /**************************************************************************
78 * Endpoint Buffer Configuration
80 **************************************************************************/
82 #define ENDPOINT0_SIZE 32
84 // 0:control endpoint is enabled automatically by controller.
85 static const uint8_t PROGMEM endpoint_config_table
[] = {
86 // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
87 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(KEYBOARD_SIZE
) | KEYBOARD_BUFFER
, // 1
88 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(MOUSE_SIZE
) | MOUSE_BUFFER
, // 2
89 1, EP_TYPE_INTERRUPT_IN
, EP_SIZE(DEBUG_TX_SIZE
) | DEBUG_TX_BUFFER
, // 3
96 /**************************************************************************
100 **************************************************************************/
102 // Descriptors are the data that your computer reads when it auto-detects
103 // this USB device (called "enumeration" in USB lingo). The most commonly
104 // changed items are editable at the top of this file. Changing things
105 // in here should only be done by those who've read chapter 9 of the USB
106 // spec and relevant portions of any USB class specifications!
109 static uint8_t PROGMEM device_descriptor
[] = {
111 1, // bDescriptorType
112 0x00, 0x02, // bcdUSB
114 0, // bDeviceSubClass
115 0, // bDeviceProtocol
116 ENDPOINT0_SIZE
, // bMaxPacketSize0
117 LSB(VENDOR_ID
), MSB(VENDOR_ID
), // idVendor
118 LSB(PRODUCT_ID
), MSB(PRODUCT_ID
), // idProduct
119 0x00, 0x01, // bcdDevice
123 1 // bNumConfigurations
126 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
127 static uint8_t PROGMEM keyboard_hid_report_desc
[] = {
128 0x05, 0x01, // Usage Page (Generic Desktop),
129 0x09, 0x06, // Usage (Keyboard),
130 0xA1, 0x01, // Collection (Application),
131 0x75, 0x01, // Report Size (1),
132 0x95, 0x08, // Report Count (8),
133 0x05, 0x07, // Usage Page (Key Codes),
134 0x19, 0xE0, // Usage Minimum (224),
135 0x29, 0xE7, // Usage Maximum (231),
136 0x15, 0x00, // Logical Minimum (0),
137 0x25, 0x01, // Logical Maximum (1),
138 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
139 0x95, 0x01, // Report Count (1),
140 0x75, 0x08, // Report Size (8),
141 0x81, 0x03, // Input (Constant), ;Reserved byte
142 0x95, 0x05, // Report Count (5),
143 0x75, 0x01, // Report Size (1),
144 0x05, 0x08, // Usage Page (LEDs),
145 0x19, 0x01, // Usage Minimum (1),
146 0x29, 0x05, // Usage Maximum (5),
147 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
148 0x95, 0x01, // Report Count (1),
149 0x75, 0x03, // Report Size (3),
150 0x91, 0x03, // Output (Constant), ;LED report padding
151 0x95, 0x06, // Report Count (6),
152 0x75, 0x08, // Report Size (8),
153 0x15, 0x00, // Logical Minimum (0),
154 0x25, 0x68, // Logical Maximum(104),
155 0x05, 0x07, // Usage Page (Key Codes),
156 0x19, 0x00, // Usage Minimum (0),
157 0x29, 0x68, // Usage Maximum (104),
158 0x81, 0x00, // Input (Data, Array),
159 0xc0 // End Collection
162 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
163 // http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
164 // http://www.keil.com/forum/15671/
165 // http://www.microsoft.com/whdc/device/input/wheel.mspx
166 static uint8_t PROGMEM mouse_hid_report_desc
[] = {
167 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
168 0x09, 0x02, // USAGE (Mouse)
169 0xa1, 0x01, // COLLECTION (Application)
170 0x09, 0x02, // USAGE (Mouse)
171 0xa1, 0x02, // COLLECTION (Logical)
172 0x09, 0x01, // USAGE (Pointer)
173 0xa1, 0x00, // COLLECTION (Physical)
174 // ------------------------------ Buttons
175 0x05, 0x09, // USAGE_PAGE (Button)
176 0x19, 0x01, // USAGE_MINIMUM (Button 1)
177 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
178 0x15, 0x00, // LOGICAL_MINIMUM (0)
179 0x25, 0x01, // LOGICAL_MAXIMUM (1)
180 0x75, 0x01, // REPORT_SIZE (1)
181 0x95, 0x05, // REPORT_COUNT (5)
182 0x81, 0x02, // INPUT (Data,Var,Abs)
183 // ------------------------------ Padding
184 0x75, 0x03, // REPORT_SIZE (3)
185 0x95, 0x01, // REPORT_COUNT (1)
186 0x81, 0x03, // INPUT (Cnst,Var,Abs)
187 // ------------------------------ X,Y position
188 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
189 0x09, 0x30, // USAGE (X)
190 0x09, 0x31, // USAGE (Y)
191 0x15, 0x81, // LOGICAL_MINIMUM (-127)
192 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
193 0x75, 0x08, // REPORT_SIZE (8)
194 0x95, 0x02, // REPORT_COUNT (2)
195 0x81, 0x06, // INPUT (Data,Var,Rel)
196 0xa1, 0x02, // COLLECTION (Logical)
197 // ------------------------------ Vertical wheel res multiplier
198 0x09, 0x48, // USAGE (Resolution Multiplier)
199 0x15, 0x00, // LOGICAL_MINIMUM (0)
200 0x25, 0x01, // LOGICAL_MAXIMUM (1)
201 0x35, 0x01, // PHYSICAL_MINIMUM (1)
202 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
203 0x75, 0x02, // REPORT_SIZE (2)
204 0x95, 0x01, // REPORT_COUNT (1)
206 0xb1, 0x02, // FEATURE (Data,Var,Abs)
207 // ------------------------------ Vertical wheel
208 0x09, 0x38, // USAGE (Wheel)
209 0x15, 0x81, // LOGICAL_MINIMUM (-127)
210 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
211 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
212 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
213 0x75, 0x08, // REPORT_SIZE (8)
214 0x81, 0x06, // INPUT (Data,Var,Rel)
215 0xc0, // END_COLLECTION
216 0xa1, 0x02, // COLLECTION (Logical)
217 // ------------------------------ Horizontal wheel res multiplier
218 0x09, 0x48, // USAGE (Resolution Multiplier)
220 0xb1, 0x02, // FEATURE (Data,Var,Abs)
221 // ------------------------------ Padding for Feature report
222 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
223 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
224 0x75, 0x04, // REPORT_SIZE (4)
225 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
226 // ------------------------------ Horizontal wheel
227 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
228 0x0a, 0x38, 0x02, // USAGE (AC Pan)
229 0x15, 0x81, // LOGICAL_MINIMUM (-127)
230 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
231 0x75, 0x08, // REPORT_SIZE (8)
232 0x81, 0x06, // INPUT (Data,Var,Rel)
233 0xc0, // END_COLLECTION
234 0xc0, // END_COLLECTION
235 0xc0, // END_COLLECTION
236 0xc0 // END_COLLECTION
239 static uint8_t PROGMEM debug_hid_report_desc
[] = {
240 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
241 0x09, 0x74, // Usage 0x74
242 0xA1, 0x53, // Collection 0x53
243 0x75, 0x08, // report size = 8 bits
244 0x15, 0x00, // logical minimum = 0
245 0x26, 0xFF, 0x00, // logical maximum = 255
246 0x95, DEBUG_TX_SIZE
, // report count
248 0x81, 0x02, // Input (array)
249 0xC0 // end collection
252 #define CONFIG1_DESC_SIZE (9+(9+9+7)+(9+9+7)+(9+9+7))
253 #define KEYBOARD_HID_DESC_OFFSET (9+9)
254 #define MOUSE_HID_DESC_OFFSET (9+(9+9+7)+9)
255 #define DEBUG_HID_DESC_OFFSET (9+(9+9+7)+(9+9+7)+9)
256 static uint8_t PROGMEM config1_descriptor
[CONFIG1_DESC_SIZE
] = {
257 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
259 2, // bDescriptorType;
260 LSB(CONFIG1_DESC_SIZE
), // wTotalLength
261 MSB(CONFIG1_DESC_SIZE
),
263 1, // bConfigurationValue
265 0xC0, // bmAttributes
268 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
270 4, // bDescriptorType
271 KEYBOARD_INTERFACE
, // bInterfaceNumber
272 0, // bAlternateSetting
274 0x03, // bInterfaceClass (0x03 = HID)
275 0x01, // bInterfaceSubClass (0x01 = Boot)
276 0x01, // bInterfaceProtocol (0x01 = Keyboard)
278 // HID descriptor, HID 1.11 spec, section 6.2.1
280 0x21, // bDescriptorType
281 0x11, 0x01, // bcdHID
283 1, // bNumDescriptors
284 0x22, // bDescriptorType
285 sizeof(keyboard_hid_report_desc
), // wDescriptorLength
287 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
289 5, // bDescriptorType
290 KEYBOARD_ENDPOINT
| 0x80, // bEndpointAddress
291 0x03, // bmAttributes (0x03=intr)
292 KEYBOARD_SIZE
, 0, // wMaxPacketSize
295 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
297 4, // bDescriptorType
298 MOUSE_INTERFACE
, // bInterfaceNumber
299 0, // bAlternateSetting
301 0x03, // bInterfaceClass (0x03 = HID)
302 0x01, // bInterfaceSubClass (0x01 = Boot)
303 0x02, // bInterfaceProtocol (0x02 = Mouse)
305 // HID descriptor, HID 1.11 spec, section 6.2.1
307 0x21, // bDescriptorType
308 0x11, 0x01, // bcdHID
310 1, // bNumDescriptors
311 0x22, // bDescriptorType
312 sizeof(mouse_hid_report_desc
), // wDescriptorLength
314 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
316 5, // bDescriptorType
317 MOUSE_ENDPOINT
| 0x80, // bEndpointAddress
318 0x03, // bmAttributes (0x03=intr)
319 MOUSE_SIZE
, 0, // wMaxPacketSize
322 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
324 4, // bDescriptorType
325 DEBUG_INTERFACE
, // bInterfaceNumber
326 0, // bAlternateSetting
328 0x03, // bInterfaceClass (0x03 = HID)
329 0x00, // bInterfaceSubClass
330 0x00, // bInterfaceProtocol
332 // HID descriptor, HID 1.11 spec, section 6.2.1
334 0x21, // bDescriptorType
335 0x11, 0x01, // bcdHID
337 1, // bNumDescriptors
338 0x22, // bDescriptorType
339 sizeof(debug_hid_report_desc
), // wDescriptorLength
341 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
343 5, // bDescriptorType
344 DEBUG_TX_ENDPOINT
| 0x80, // bEndpointAddress
345 0x03, // bmAttributes (0x03=intr)
346 DEBUG_TX_SIZE
, 0, // wMaxPacketSize
350 // If you're desperate for a little extra code memory, these strings
351 // can be completely removed if iManufacturer, iProduct, iSerialNumber
352 // in the device desciptor are changed to zeros.
353 struct usb_string_descriptor_struct
{
355 uint8_t bDescriptorType
;
358 static struct usb_string_descriptor_struct PROGMEM string0
= {
363 static struct usb_string_descriptor_struct PROGMEM string1
= {
364 sizeof(STR_MANUFACTURER
),
368 static struct usb_string_descriptor_struct PROGMEM string2
= {
374 // This table defines which descriptor data is sent for each specific
375 // request from the host (in wValue and wIndex).
376 static struct descriptor_list_struct
{
377 uint16_t wValue
; // descriptor type
381 } PROGMEM descriptor_list
[] = {
383 {0x0100, 0x0000, device_descriptor
, sizeof(device_descriptor
)},
384 // CONFIGURATION descriptor
385 {0x0200, 0x0000, config1_descriptor
, sizeof(config1_descriptor
)},
387 {0x2200, KEYBOARD_INTERFACE
, keyboard_hid_report_desc
, sizeof(keyboard_hid_report_desc
)},
388 {0x2100, KEYBOARD_INTERFACE
, config1_descriptor
+KEYBOARD_HID_DESC_OFFSET
, 9},
390 {0x2200, MOUSE_INTERFACE
, mouse_hid_report_desc
, sizeof(mouse_hid_report_desc
)},
391 {0x2100, MOUSE_INTERFACE
, config1_descriptor
+MOUSE_HID_DESC_OFFSET
, 9},
393 {0x2200, DEBUG_INTERFACE
, debug_hid_report_desc
, sizeof(debug_hid_report_desc
)},
394 {0x2100, DEBUG_INTERFACE
, config1_descriptor
+DEBUG_HID_DESC_OFFSET
, 9},
396 {0x0300, 0x0000, (const uint8_t *)&string0
, 4},
397 {0x0301, 0x0409, (const uint8_t *)&string1
, sizeof(STR_MANUFACTURER
)},
398 {0x0302, 0x0409, (const uint8_t *)&string2
, sizeof(STR_PRODUCT
)}
400 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
403 /**************************************************************************
405 * Variables - these are the only non-stack RAM usage
407 **************************************************************************/
409 // zero when we are not configured, non-zero when enumerated
410 static volatile uint8_t usb_configuration
=0;
413 /**************************************************************************
415 * Public Functions - these are the API intended for the user
417 **************************************************************************/
424 USB_FREEZE(); // enable USB
425 PLL_CONFIG(); // config PLL
426 while (!(PLLCSR
& (1<<PLOCK
))) ; // wait for PLL lock
427 USB_CONFIG(); // start USB clock
428 UDCON
= 0; // enable attach resistor
429 usb_configuration
= 0;
430 UDIEN
= (1<<EORSTE
)|(1<<SOFE
);
434 // return 0 if the USB is not configured, or the configuration
435 // number selected by the HOST
436 uint8_t usb_configured(void)
438 return usb_configuration
;
443 /**************************************************************************
445 * Private Functions - not intended for general user consumption....
447 **************************************************************************/
451 // USB Device Interrupt - handle all device-level events
452 // the transmit buffer flushing is triggered by the start of frame
456 uint8_t intbits
, t
, i
;
457 static uint8_t div4
=0;
461 if (intbits
& (1<<EORSTI
)) {
464 UECFG0X
= EP_TYPE_CONTROL
;
465 UECFG1X
= EP_SIZE(ENDPOINT0_SIZE
) | EP_SINGLE_BUFFER
;
466 UEIENX
= (1<<RXSTPE
);
467 usb_configuration
= 0;
469 if ((intbits
& (1<<SOFI
)) && usb_configuration
) {
470 t
= debug_flush_timer
;
472 debug_flush_timer
= -- t
;
474 UENUM
= DEBUG_TX_ENDPOINT
;
475 while ((UEINTX
& (1<<RWAL
))) {
481 if (usb_keyboard_idle_config
&& (++div4
& 3) == 0) {
482 UENUM
= KEYBOARD_ENDPOINT
;
483 if (UEINTX
& (1<<RWAL
)) {
484 usb_keyboard_idle_count
++;
485 if (usb_keyboard_idle_count
== usb_keyboard_idle_config
) {
486 usb_keyboard_idle_count
= 0;
487 UEDATX
= usb_keyboard_mods
;
489 for (i
=0; i
<6; i
++) {
490 UEDATX
= usb_keyboard_keys
[i
];
501 // Misc functions to wait for ready and send/receive packets
502 static inline void usb_wait_in_ready(void)
504 while (!(UEINTX
& (1<<TXINI
))) ;
506 static inline void usb_send_in(void)
508 UEINTX
= ~(1<<TXINI
);
510 static inline void usb_wait_receive_out(void)
512 while (!(UEINTX
& (1<<RXOUTI
))) ;
514 static inline void usb_ack_out(void)
516 UEINTX
= ~(1<<RXOUTI
);
521 // USB Endpoint Interrupt - endpoint 0 is handled here. The
522 // other endpoints are manipulated by the user-callable
523 // functions, and the start-of-frame interrupt.
530 uint8_t i
, n
, len
, en
;
531 uint8_t bmRequestType
;
537 const uint8_t *desc_addr
;
542 if (intbits
& (1<<RXSTPI
)) {
543 bmRequestType
= UEDATX
;
546 wValue
|= (UEDATX
<< 8);
548 wIndex
|= (UEDATX
<< 8);
550 wLength
|= (UEDATX
<< 8);
551 UEINTX
= ~((1<<RXSTPI
) | (1<<RXOUTI
) | (1<<TXINI
));
552 if (bRequest
== GET_DESCRIPTOR
) {
553 list
= (const uint8_t *)descriptor_list
;
555 if (i
>= NUM_DESC_LIST
) {
556 UECONX
= (1<<STALLRQ
)|(1<<EPEN
); //stall
559 desc_val
= pgm_read_word(list
);
560 if (desc_val
!= wValue
) {
561 list
+= sizeof(struct descriptor_list_struct
);
565 desc_val
= pgm_read_word(list
);
566 if (desc_val
!= wIndex
) {
567 list
+= sizeof(struct descriptor_list_struct
)-2;
571 desc_addr
= (const uint8_t *)pgm_read_word(list
);
573 desc_length
= pgm_read_byte(list
);
576 len
= (wLength
< 256) ? wLength
: 255;
577 if (len
> desc_length
) len
= desc_length
;
579 // wait for host ready for IN packet
582 } while (!(i
& ((1<<TXINI
)|(1<<RXOUTI
))));
583 if (i
& (1<<RXOUTI
)) return; // abort
585 n
= len
< ENDPOINT0_SIZE
? len
: ENDPOINT0_SIZE
;
586 for (i
= n
; i
; i
--) {
587 UEDATX
= pgm_read_byte(desc_addr
++);
591 } while (len
|| n
== ENDPOINT0_SIZE
);
594 if (bRequest
== SET_ADDRESS
) {
597 UDADDR
= wValue
| (1<<ADDEN
);
600 if (bRequest
== SET_CONFIGURATION
&& bmRequestType
== 0) {
601 usb_configuration
= wValue
;
603 cfg
= endpoint_config_table
;
604 for (i
=1; i
<=6; i
++) {
606 en
= pgm_read_byte(cfg
++);
609 UECFG0X
= pgm_read_byte(cfg
++);
610 UECFG1X
= pgm_read_byte(cfg
++);
617 if (bRequest
== GET_CONFIGURATION
&& bmRequestType
== 0x80) {
619 UEDATX
= usb_configuration
;
624 if (bRequest
== GET_STATUS
) {
627 #ifdef SUPPORT_ENDPOINT_HALT
628 if (bmRequestType
== 0x82) {
630 if (UECONX
& (1<<STALLRQ
)) i
= 1;
639 #ifdef SUPPORT_ENDPOINT_HALT
640 if ((bRequest
== CLEAR_FEATURE
|| bRequest
== SET_FEATURE
)
641 && bmRequestType
== 0x02 && wValue
== 0) {
643 if (i
>= 1 && i
<= MAX_ENDPOINT
) {
646 if (bRequest
== SET_FEATURE
) {
647 UECONX
= (1<<STALLRQ
)|(1<<EPEN
);
649 UECONX
= (1<<STALLRQC
)|(1<<RSTDT
)|(1<<EPEN
);
657 if (wIndex
== KEYBOARD_INTERFACE
) {
658 if (bmRequestType
== 0xA1) {
659 if (bRequest
== HID_GET_REPORT
) {
661 UEDATX
= usb_keyboard_mods
;
663 for (i
=0; i
<6; i
++) {
664 UEDATX
= usb_keyboard_keys
[i
];
669 if (bRequest
== HID_GET_IDLE
) {
671 UEDATX
= usb_keyboard_idle_config
;
675 if (bRequest
== HID_GET_PROTOCOL
) {
677 UEDATX
= usb_keyboard_protocol
;
682 if (bmRequestType
== 0x21) {
683 if (bRequest
== HID_SET_REPORT
) {
684 usb_wait_receive_out();
685 usb_keyboard_leds
= UEDATX
;
690 if (bRequest
== HID_SET_IDLE
) {
691 usb_keyboard_idle_config
= (wValue
>> 8);
692 usb_keyboard_idle_count
= 0;
693 //usb_wait_in_ready();
697 if (bRequest
== HID_SET_PROTOCOL
) {
698 usb_keyboard_protocol
= wValue
;
699 //usb_wait_in_ready();
705 if (wIndex
== MOUSE_INTERFACE
) {
706 if (bmRequestType
== 0xA1) {
707 if (bRequest
== HID_GET_REPORT
) {
708 if (wValue
== HID_REPORT_INPUT
) {
710 UEDATX
= mouse_buttons
;
717 if (wValue
== HID_REPORT_FEATURE
) {
724 if (bRequest
== HID_GET_PROTOCOL
) {
726 UEDATX
= mouse_protocol
;
731 if (bmRequestType
== 0x21) {
732 if (bRequest
== HID_SET_PROTOCOL
) {
733 mouse_protocol
= wValue
;
739 if (wIndex
== DEBUG_INTERFACE
) {
740 if (bRequest
== HID_GET_REPORT
&& bmRequestType
== 0xA1) {
743 // wait for host ready for IN packet
746 } while (!(i
& ((1<<TXINI
)|(1<<RXOUTI
))));
747 if (i
& (1<<RXOUTI
)) return; // abort
749 n
= len
< ENDPOINT0_SIZE
? len
: ENDPOINT0_SIZE
;
750 for (i
= n
; i
; i
--) {
755 } while (len
|| n
== ENDPOINT0_SIZE
);
760 UECONX
= (1<<STALLRQ
) | (1<<EPEN
); // stall