]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/avr/usb_keyboard_serial.c
Adding CLI and CDC Serial support for Teensy 2.0 and Teensy 2.0++
[kiibohd-controller.git] / Output / pjrcUSB / avr / usb_keyboard_serial.c
1 /* USB Keyboard and CDC Serial Device for Teensy USB Development Board
2  * Copyright (c) 2009 PJRC.COM, LLC
3  * Modifications by Jacob Alexander (2011-2014)
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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
21  * THE SOFTWARE.
22  */
23
24
25 // Local Includes
26 #include "usb_keyboard_serial.h"
27
28
29 // ----- Functions -----
30
31 // Set the avr into firmware reload mode
32 void usb_debug_reload()
33 {
34         cli();
35         // Disable watchdog, if enabled
36         // Disable all peripherals
37
38         UDCON = 1;
39         USBCON = (1 << FRZCLK);  // Disable USB
40         UCSR1B = 0;
41         _delay_ms( 5 );
42
43 #if defined(__AVR_AT90USB162__)                // Teensy 1.0
44         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
45         TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
46         DDRB = 0; DDRC = 0; DDRD = 0;
47         PORTB = 0; PORTC = 0; PORTD = 0;
48         asm volatile("jmp 0x3E00");
49 #elif defined(__AVR_ATmega32U4__)              // Teensy 2.0
50         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
51         TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
52         DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
53         PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
54         asm volatile("jmp 0x7E00");
55 #elif defined(__AVR_AT90USB646__)              // Teensy++ 1.0
56         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
57         TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
58         DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
59         PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
60         asm volatile("jmp 0xFC00");
61 #elif defined(__AVR_AT90USB1286__)             // Teensy++ 2.0
62         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
63         TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
64         DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
65         PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
66         asm volatile("jmp 0x1FC00");
67 #endif
68 }
69
70
71 // WDT Setup for software reset the chip
72 void wdt_init(void)
73 {
74         MCUSR = 0;
75         wdt_disable();
76 }
77
78
79 /**************************************************************************
80  *
81  *  Configurable Options
82  *
83  **************************************************************************/
84
85 // When you write data, it goes into a USB endpoint buffer, which
86 // is transmitted to the PC when it becomes full, or after a timeout
87 // with no more writes.  Even if you write in exactly packet-size
88 // increments, this timeout is used to send a "zero length packet"
89 // that tells the PC no more data is expected and it should pass
90 // any buffered data to the application that may be waiting.  If
91 // you want data sent immediately, call usb_serial_flush_output().
92 #define TRANSMIT_FLUSH_TIMEOUT  5   /* in milliseconds */
93
94 // If the PC is connected but not "listening", this is the length
95 // of time before usb_serial_getchar() returns with an error.  This
96 // is roughly equivilant to a real UART simply transmitting the
97 // bits on a wire where nobody is listening, except you get an error
98 // code which you can ignore for serial-like discard of data, or
99 // use to know your data wasn't sent.
100 #define TRANSMIT_TIMEOUT        25   /* in milliseconds */
101
102 // USB devices are supposed to implment a halt feature, which is
103 // rarely (if ever) used.  If you comment this line out, the halt
104 // code will be removed, saving 116 bytes of space (gcc 4.3.0).
105 // This is not strictly USB compliant, but works with all major
106 // operating systems.
107 #define SUPPORT_ENDPOINT_HALT
108
109
110
111 /**************************************************************************
112  *
113  *  Descriptor Data
114  *
115  **************************************************************************/
116
117 // Descriptors are the data that your computer reads when it auto-detects
118 // this USB device (called "enumeration" in USB lingo).  The most commonly
119 // changed items are editable at the top of this file.  Changing things
120 // in here should only be done by those who've read chapter 9 of the USB
121 // spec and relevant portions of any USB class specifications!
122
123
124 static const uint8_t PROGMEM device_descriptor[] = {
125         18,                                     // bLength
126         1,                                      // bDescriptorType
127         0x00, 0x02,                             // bcdUSB
128         0,                                      // bDeviceClass
129         0,                                      // bDeviceSubClass
130         0,                                      // bDeviceProtocol
131         ENDPOINT0_SIZE,                         // bMaxPacketSize0
132         LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
133         LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
134         0x00, 0x01,                             // bcdDevice
135         1,                                      // iManufacturer
136         2,                                      // iProduct
137         3,                                      // iSerialNumber
138         1                                       // bNumConfigurations
139 };
140
141 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
142 static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
143         0x05, 0x01,          // Usage Page (Generic Desktop),
144         0x09, 0x06,          // Usage (Keyboard),
145         0xA1, 0x01,          // Collection (Application),
146         0x75, 0x01,          //   Report Size (1),
147         0x95, 0x08,          //   Report Count (8),
148         0x05, 0x07,          //   Usage Page (Key Codes),
149         0x19, 0xE0,          //   Usage Minimum (224),
150         0x29, 0xE7,          //   Usage Maximum (231),
151         0x15, 0x00,          //   Logical Minimum (0),
152         0x25, 0x01,          //   Logical Maximum (1),
153         0x81, 0x02,          //   Input (Data, Variable, Absolute), ;Modifier byte
154         0x95, 0x08,          //  Report Count (8),
155         0x75, 0x01,          //  Report Size (1),
156         0x15, 0x00,          //  Logical Minimum (0),
157         0x25, 0x01,          //  Logical Maximum (1),
158         0x05, 0x0C,          //  Usage Page (Consumer),
159         0x09, 0xE9,          //  Usage (Volume Increment),
160         0x09, 0xEA,          //  Usage (Volume Decrement),
161         0x09, 0xE2,          //  Usage (Mute),
162         0x09, 0xCD,          //  Usage (Play/Pause),
163         0x09, 0xB5,          //  Usage (Scan Next Track),
164         0x09, 0xB6,          //  Usage (Scan Previous Track),
165         0x09, 0xB7,          //  Usage (Stop),
166         0x09, 0xB8,          //  Usage (Eject),
167         0x81, 0x02,          //  Input (Data, Variable, Absolute), ;Media keys
168         0x95, 0x05,          //  Report Count (5),
169         0x75, 0x01,          //  Report Size (1),
170         0x05, 0x08,          //  Usage Page (LEDs),
171         0x19, 0x01,          //  Usage Minimum (1),
172         0x29, 0x05,          //  Usage Maximum (5),
173         0x91, 0x02,          //  Output (Data, Variable, Absolute), ;LED report
174         0x95, 0x01,          //  Report Count (1),
175         0x75, 0x03,          //  Report Size (3),
176         0x91, 0x03,          //  Output (Constant),                 ;LED report padding
177         0x95, 0x06,          //  Report Count (6),
178         0x75, 0x08,          //  Report Size (8),
179         0x15, 0x00,          //  Logical Minimum (0),
180         0x25, 0x7F,          //  Logical Maximum(104),
181         0x05, 0x07,          //  Usage Page (Key Codes),
182         0x19, 0x00,          //  Usage Minimum (0),
183         0x29, 0x7F,          //  Usage Maximum (104),
184         0x81, 0x00,          //  Input (Data, Array),                ;Normal keys
185         0xc0                 // End Collection
186 };
187
188 // <Configuration> + <Keyboard HID> + <Serial CDC>
189 #define CONFIG1_DESC_SIZE        (9 + 9+9+7 + 8+9+5+5+4+5+7+9+7+7)
190 #define KEYBOARD_HID_DESC_OFFSET (9 + 9)
191 #define SERIAL_CDC_DESC_OFFSET   (9 + 9+9+7)
192 static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
193 // --- Configuration ---
194 // - 9 bytes -
195         // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
196         9,                                      // bLength;
197         2,                                      // bDescriptorType;
198         LSB(CONFIG1_DESC_SIZE),                 // wTotalLength
199         MSB(CONFIG1_DESC_SIZE),
200         3,                                      // bNumInterfaces
201         1,                                      // bConfigurationValue
202         0,                                      // iConfiguration
203         0xC0,                                   // bmAttributes
204         50,                                     // bMaxPower
205
206 // --- Keyboard HID ---
207 // - 9 bytes -
208         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
209         9,                                      // bLength
210         4,                                      // bDescriptorType
211         KEYBOARD_INTERFACE,                     // bInterfaceNumber
212         0,                                      // bAlternateSetting
213         1,                                      // bNumEndpoints
214         0x03,                                   // bInterfaceClass (0x03 = HID)
215         0x01,                                   // bInterfaceSubClass (0x01 = Boot)
216         0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
217         0,                                      // iInterface
218 // - 9 bytes -
219         // HID interface descriptor, HID 1.11 spec, section 6.2.1
220         9,                                      // bLength
221         0x21,                                   // bDescriptorType
222         0x11, 0x01,                             // bcdHID
223         0,                                      // bCountryCode
224         1,                                      // bNumDescriptors
225         0x22,                                   // bDescriptorType
226         LSB(sizeof(keyboard_hid_report_desc)),  // wDescriptorLength
227         MSB(sizeof(keyboard_hid_report_desc)),
228 // - 7 bytes -
229         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
230         7,                                      // bLength
231         5,                                      // bDescriptorType
232         KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
233         0x03,                                   // bmAttributes (0x03=intr)
234         KEYBOARD_SIZE, 0,                       // wMaxPacketSize
235         KEYBOARD_INTERVAL,                      // bInterval
236
237 // --- Serial CDC ---
238 // - 8 bytes -
239         // interface association descriptor, USB ECN, Table 9-Z
240         8,                                      // bLength
241         11,                                     // bDescriptorType
242         CDC_STATUS_INTERFACE,                   // bFirstInterface
243         2,                                      // bInterfaceCount
244         0x02,                                   // bFunctionClass
245         0x02,                                   // bFunctionSubClass
246         0x01,                                   // bFunctionProtocol
247         4,                                      // iFunction
248 // - 9 bytes -
249         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
250         9,                                      // bLength
251         4,                                      // bDescriptorType
252         CDC_STATUS_INTERFACE,                   // bInterfaceNumber
253         0,                                      // bAlternateSetting
254         1,                                      // bNumEndpoints
255         0x02,                                   // bInterfaceClass
256         0x02,                                   // bInterfaceSubClass
257         0x01,                                   // bInterfaceProtocol
258         0,                                      // iInterface
259 // - 5 bytes -
260         // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
261         5,                                      // bFunctionLength
262         0x24,                                   // bDescriptorType
263         0x00,                                   // bDescriptorSubtype
264         0x10, 0x01,                             // bcdCDC
265 // - 5 bytes -
266         // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
267         5,                                      // bFunctionLength
268         0x24,                                   // bDescriptorType
269         0x01,                                   // bDescriptorSubtype
270         0x01,                                   // bmCapabilities
271         1,                                      // bDataInterface
272 // - 4 bytes -
273         // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
274         4,                                      // bFunctionLength
275         0x24,                                   // bDescriptorType
276         0x02,                                   // bDescriptorSubtype
277         0x06,                                   // bmCapabilities
278 // - 5 bytes -
279         // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
280         5,                                      // bFunctionLength
281         0x24,                                   // bDescriptorType
282         0x06,                                   // bDescriptorSubtype
283         CDC_STATUS_INTERFACE,                   // bMasterInterface
284         CDC_DATA_INTERFACE,                     // bSlaveInterface0
285 // - 7 bytes -
286         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
287         7,                                      // bLength
288         5,                                      // bDescriptorType
289         CDC_ACM_ENDPOINT | 0x80,                // bEndpointAddress
290         0x03,                                   // bmAttributes (0x03=intr)
291         CDC_ACM_SIZE, 0,                        // wMaxPacketSize
292         64,                                     // bInterval
293 // - 9 bytes -
294         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
295         9,                                      // bLength
296         4,                                      // bDescriptorType
297         CDC_DATA_INTERFACE,                     // bInterfaceNumber
298         0,                                      // bAlternateSetting
299         2,                                      // bNumEndpoints
300         0x0A,                                   // bInterfaceClass
301         0x00,                                   // bInterfaceSubClass
302         0x00,                                   // bInterfaceProtocol
303         0,                                      // iInterface
304 // - 7 bytes -
305         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
306         7,                                      // bLength
307         5,                                      // bDescriptorType
308         CDC_RX_ENDPOINT,                        // bEndpointAddress
309         0x02,                                   // bmAttributes (0x02=bulk)
310         CDC_RX_SIZE, 0,                         // wMaxPacketSize
311         0,                                      // bInterval
312 // - 7 bytes -
313         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
314         7,                                      // bLength
315         5,                                      // bDescriptorType
316         CDC_TX_ENDPOINT | 0x80,                 // bEndpointAddress
317         0x02,                                   // bmAttributes (0x02=bulk)
318         CDC_TX_SIZE, 0,                         // wMaxPacketSize
319         0,                                      // bInterval
320 };
321
322 // If you're desperate for a little extra code memory, these strings
323 // can be completely removed if iManufacturer, iProduct, iSerialNumber
324 // in the device desciptor are changed to zeros.
325 struct usb_string_descriptor_struct {
326         uint8_t bLength;
327         uint8_t bDescriptorType;
328         int16_t wString[];
329 };
330 static const struct usb_string_descriptor_struct PROGMEM string0 = {
331         4,
332         3,
333         {0x0409}
334 };
335 static const struct usb_string_descriptor_struct PROGMEM string1 = {
336         sizeof(STR_MANUFACTURER),
337         3,
338         STR_MANUFACTURER
339 };
340 static const struct usb_string_descriptor_struct PROGMEM string2 = {
341         sizeof(STR_PRODUCT),
342         3,
343         STR_PRODUCT
344 };
345 static const struct usb_string_descriptor_struct PROGMEM string3 = {
346         sizeof(STR_SERIAL),
347         3,
348         STR_SERIAL
349 };
350
351 // This table defines which descriptor data is sent for each specific
352 // request from the host (in wValue and wIndex).
353 static const struct descriptor_list_struct {
354         uint16_t        wValue;
355         uint16_t        wIndex;
356         const uint8_t   *addr;
357         uint8_t         length;
358 } PROGMEM descriptor_list[] = {
359         {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
360         {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
361         {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
362         {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
363         {0x0300, 0x0000, (const uint8_t *)&string0, 4},
364         {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
365         {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
366         {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)}
367 };
368 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
369
370
371 /**************************************************************************
372  *
373  *  Variables - these are the only non-stack RAM usage
374  *
375  **************************************************************************/
376
377 // zero when we are not configured, non-zero when enumerated
378 static volatile uint8_t usb_configuration=0;
379
380 // the time remaining before we transmit any partially full
381 // packet, or send a zero length packet.
382 static volatile uint8_t transmit_flush_timer=0;
383 static uint8_t transmit_previous_timeout=0;
384
385 // serial port settings (baud rate, control signals, etc) set
386 // by the PC.  These are ignored, but kept in RAM.
387 static uint8_t cdc_line_coding[7]={0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
388 static uint8_t cdc_line_rtsdtr=0;
389
390
391 /**************************************************************************
392  *
393  *  Public Functions - these are the API intended for the user
394  *
395  **************************************************************************/
396
397
398 // initialize USB
399 void usb_init(void)
400 {
401         HW_CONFIG();
402         USB_FREEZE();                           // enable USB
403         PLL_CONFIG();                           // config PLL
404         while (!(PLLCSR & (1<<PLOCK))) ;        // wait for PLL lock
405         USB_CONFIG();                           // start USB clock
406         UDCON = 0;                              // enable attach resistor
407         usb_configuration = 0;
408         UDIEN = (1<<EORSTE)|(1<<SOFE);
409         sei();
410
411         // Disable watchdog timer after possible software reset
412         //wdt_init(); // XXX Not working...seems to be ok without this, not sure though
413 }
414
415 // return 0 if the USB is not configured, or the configuration
416 // number selected by the HOST
417 uint8_t usb_configured(void)
418 {
419         return usb_configuration;
420 }
421
422 // send the contents of USBKeys_Array and USBKeys_Modifiers
423 int8_t usb_keyboard_send(void)
424 {
425         uint8_t i, intr_state, timeout;
426
427         if (!usb_configuration) return -1;
428         intr_state = SREG;
429         cli();
430         UENUM = KEYBOARD_ENDPOINT;
431         timeout = UDFNUML + 50;
432         while (1) {
433                 // are we ready to transmit?
434                 if (UEINTX & (1<<RWAL)) break;
435                 SREG = intr_state;
436                 // has the USB gone offline?
437                 if (!usb_configuration) return -1;
438                 // have we waited too long?
439                 if (UDFNUML == timeout) return -1;
440                 // get ready to try checking again
441                 intr_state = SREG;
442                 cli();
443                 UENUM = KEYBOARD_ENDPOINT;
444         }
445         UEDATX = USBKeys_Modifiers;
446         UEDATX = 0;
447         for (i=0; i<6; i++) {
448                 UEDATX = USBKeys_Array[i];
449         }
450         UEINTX = 0x3A;
451         USBKeys_Idle_Count = 0;
452         SREG = intr_state;
453         return 0;
454 }
455
456 // get the next character, or -1 if nothing received
457 int16_t usb_serial_getchar(void)
458 {
459         uint8_t c, intr_state;
460
461         // interrupts are disabled so these functions can be
462         // used from the main program or interrupt context,
463         // even both in the same program!
464         intr_state = SREG;
465         cli();
466         if (!usb_configuration) {
467                 SREG = intr_state;
468                 return -1;
469         }
470         UENUM = CDC_RX_ENDPOINT;
471         retry:
472         c = UEINTX;
473         if (!(c & (1<<RWAL))) {
474                 // no data in buffer
475                 if (c & (1<<RXOUTI)) {
476                         UEINTX = 0x6B;
477                         goto retry;
478                 }
479                 SREG = intr_state;
480                 return -2;
481         }
482         // take one byte out of the buffer
483         c = UEDATX;
484         // if buffer completely used, release it
485         if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
486         SREG = intr_state;
487         return c;
488 }
489
490 // number of bytes available in the receive buffer
491 uint8_t usb_serial_available(void)
492 {
493         uint8_t n=0, i, intr_state;
494
495         intr_state = SREG;
496         cli();
497         if (usb_configuration) {
498                 UENUM = CDC_RX_ENDPOINT;
499                 n = UEBCLX;
500                 if (!n) {
501                         i = UEINTX;
502                         if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
503                 }
504         }
505         SREG = intr_state;
506         return n;
507 }
508
509 // discard any buffered input
510 void usb_serial_flush_input(void)
511 {
512         uint8_t intr_state;
513
514         if (usb_configuration) {
515                 intr_state = SREG;
516                 cli();
517                 UENUM = CDC_RX_ENDPOINT;
518                 while ((UEINTX & (1<<RWAL))) {
519                         UEINTX = 0x6B;
520                 }
521                 SREG = intr_state;
522         }
523 }
524
525 // transmit a character.  0 returned on success, -1 on error
526 int8_t usb_serial_putchar(uint8_t c)
527 {
528         uint8_t timeout, intr_state;
529
530         // if we're not online (enumerated and configured), error
531         if (!usb_configuration) return -1;
532         // interrupts are disabled so these functions can be
533         // used from the main program or interrupt context,
534         // even both in the same program!
535         intr_state = SREG;
536         cli();
537         UENUM = CDC_TX_ENDPOINT;
538         // if we gave up due to timeout before, don't wait again
539         if (transmit_previous_timeout) {
540                 if (!(UEINTX & (1<<RWAL))) {
541                         SREG = intr_state;
542                         return -1;
543                 }
544                 transmit_previous_timeout = 0;
545         }
546         // wait for the FIFO to be ready to accept data
547         timeout = UDFNUML + TRANSMIT_TIMEOUT;
548         while (1) {
549                 // are we ready to transmit?
550                 if (UEINTX & (1<<RWAL)) break;
551                 SREG = intr_state;
552                 // have we waited too long?  This happens if the user
553                 // is not running an application that is listening
554                 if (UDFNUML == timeout) {
555                         transmit_previous_timeout = 1;
556                         return -1;
557                 }
558                 // has the USB gone offline?
559                 if (!usb_configuration) return -1;
560                 // get ready to try checking again
561                 intr_state = SREG;
562                 cli();
563                 UENUM = CDC_TX_ENDPOINT;
564         }
565         // actually write the byte into the FIFO
566         UEDATX = c;
567         // if this completed a packet, transmit it now!
568         if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
569         transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
570         SREG = intr_state;
571         return 0;
572 }
573
574
575 // transmit a character, but do not wait if the buffer is full,
576 //   0 returned on success, -1 on buffer full or error
577 int8_t usb_serial_putchar_nowait(uint8_t c)
578 {
579         uint8_t intr_state;
580
581         if (!usb_configuration) return -1;
582         intr_state = SREG;
583         cli();
584         UENUM = CDC_TX_ENDPOINT;
585         if (!(UEINTX & (1<<RWAL))) {
586                 // buffer is full
587                 SREG = intr_state;
588                 return -2;
589         }
590         // actually write the byte into the FIFO
591         UEDATX = c;
592                 // if this completed a packet, transmit it now!
593         if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
594         transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
595         SREG = intr_state;
596         return 0;
597 }
598
599 // transmit a buffer.
600 //  0 returned on success, -1 on error
601 // This function is optimized for speed!  Each call takes approx 6.1 us overhead
602 // plus 0.25 us per byte.  12 Mbit/sec USB has 8.67 us per-packet overhead and
603 // takes 0.67 us per byte.  If called with 64 byte packet-size blocks, this function
604 // can transmit at full USB speed using 43% CPU time.  The maximum theoretical speed
605 // is 19 packets per USB frame, or 1216 kbytes/sec.  However, bulk endpoints have the
606 // lowest priority, so any other USB devices will likely reduce the speed.  Speed
607 // can also be limited by how quickly the PC-based software reads data, as the host
608 // controller in the PC will not allocate bandwitdh without a pending read request.
609 // (thanks to Victor Suarez for testing and feedback and initial code)
610
611 int8_t usb_serial_write(const char *buffer, uint16_t size)
612 {
613         uint8_t timeout, intr_state, write_size;
614
615         // if we're not online (enumerated and configured), error
616         if (!usb_configuration) return -1;
617         // interrupts are disabled so these functions can be
618         // used from the main program or interrupt context,
619         // even both in the same program!
620         intr_state = SREG;
621         cli();
622         UENUM = CDC_TX_ENDPOINT;
623         // if we gave up due to timeout before, don't wait again
624         /*
625         if (transmit_previous_timeout) {
626                 if (!(UEINTX & (1<<RWAL))) {
627                         SREG = intr_state;
628                         return -2;
629                 }
630                 transmit_previous_timeout = 0;
631         }
632         */
633         // each iteration of this loop transmits a packet
634         while (size) {
635                 // wait for the FIFO to be ready to accept data
636                 timeout = UDFNUML + TRANSMIT_TIMEOUT;
637                 while (1) {
638                         // are we ready to transmit?
639                         if (UEINTX & (1<<RWAL)) break;
640                         SREG = intr_state;
641                         // have we waited too long?  This happens if the user
642                         // is not running an application that is listening
643                         if (UDFNUML == timeout) {
644                                 transmit_previous_timeout = 1;
645                                 return -3;
646                         }
647                         // has the USB gone offline?
648                         if (!usb_configuration) return -4;
649                         // get ready to try checking again
650                         intr_state = SREG;
651                         cli();
652                         UENUM = CDC_TX_ENDPOINT;
653                 }
654
655                 // compute how many bytes will fit into the next packet
656                 write_size = CDC_TX_SIZE - UEBCLX;
657                 if (write_size > size) write_size = size;
658                 size -= write_size;
659
660                 // write the packet
661                 switch (write_size) {
662                         #if (CDC_TX_SIZE == 64)
663                         case 64: UEDATX = *buffer++;
664                         case 63: UEDATX = *buffer++;
665                         case 62: UEDATX = *buffer++;
666                         case 61: UEDATX = *buffer++;
667                         case 60: UEDATX = *buffer++;
668                         case 59: UEDATX = *buffer++;
669                         case 58: UEDATX = *buffer++;
670                         case 57: UEDATX = *buffer++;
671                         case 56: UEDATX = *buffer++;
672                         case 55: UEDATX = *buffer++;
673                         case 54: UEDATX = *buffer++;
674                         case 53: UEDATX = *buffer++;
675                         case 52: UEDATX = *buffer++;
676                         case 51: UEDATX = *buffer++;
677                         case 50: UEDATX = *buffer++;
678                         case 49: UEDATX = *buffer++;
679                         case 48: UEDATX = *buffer++;
680                         case 47: UEDATX = *buffer++;
681                         case 46: UEDATX = *buffer++;
682                         case 45: UEDATX = *buffer++;
683                         case 44: UEDATX = *buffer++;
684                         case 43: UEDATX = *buffer++;
685                         case 42: UEDATX = *buffer++;
686                         case 41: UEDATX = *buffer++;
687                         case 40: UEDATX = *buffer++;
688                         case 39: UEDATX = *buffer++;
689                         case 38: UEDATX = *buffer++;
690                         case 37: UEDATX = *buffer++;
691                         case 36: UEDATX = *buffer++;
692                         case 35: UEDATX = *buffer++;
693                         case 34: UEDATX = *buffer++;
694                         case 33: UEDATX = *buffer++;
695                         #endif
696                         #if (CDC_TX_SIZE >= 32)
697                         case 32: UEDATX = *buffer++;
698                         case 31: UEDATX = *buffer++;
699                         case 30: UEDATX = *buffer++;
700                         case 29: UEDATX = *buffer++;
701                         case 28: UEDATX = *buffer++;
702                         case 27: UEDATX = *buffer++;
703                         case 26: UEDATX = *buffer++;
704                         case 25: UEDATX = *buffer++;
705                         case 24: UEDATX = *buffer++;
706                         case 23: UEDATX = *buffer++;
707                         case 22: UEDATX = *buffer++;
708                         case 21: UEDATX = *buffer++;
709                         case 20: UEDATX = *buffer++;
710                         case 19: UEDATX = *buffer++;
711                         case 18: UEDATX = *buffer++;
712                         case 17: UEDATX = *buffer++;
713                         #endif
714                         #if (CDC_TX_SIZE >= 16)
715                         case 16: UEDATX = *buffer++;
716                         case 15: UEDATX = *buffer++;
717                         case 14: UEDATX = *buffer++;
718                         case 13: UEDATX = *buffer++;
719                         case 12: UEDATX = *buffer++;
720                         case 11: UEDATX = *buffer++;
721                         case 10: UEDATX = *buffer++;
722                         case  9: UEDATX = *buffer++;
723                         #endif
724                         case  8: UEDATX = *buffer++;
725                         case  7: UEDATX = *buffer++;
726                         case  6: UEDATX = *buffer++;
727                         case  5: UEDATX = *buffer++;
728                         case  4: UEDATX = *buffer++;
729                         case  3: UEDATX = *buffer++;
730                         case  2: UEDATX = *buffer++;
731                         default:
732                         case  1: UEDATX = *buffer++;
733                         case  0: break;
734                 }
735                 // if this completed a packet, transmit it now!
736                 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
737                 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
738                 SREG = intr_state;
739         }
740         return 0;
741 }
742
743 // immediately transmit any buffered output.
744 // This doesn't actually transmit the data - that is impossible!
745 // USB devices only transmit when the host allows, so the best
746 // we can do is release the FIFO buffer for when the host wants it
747 void usb_serial_flush_output(void)
748 {
749         uint8_t intr_state;
750
751         intr_state = SREG;
752         cli();
753         if (transmit_flush_timer) {
754                 UENUM = CDC_TX_ENDPOINT;
755                 UEINTX = 0x3A;
756                 transmit_flush_timer = 0;
757         }
758         SREG = intr_state;
759 }
760
761 // functions to read the various async serial settings.  These
762 // aren't actually used by USB at all (communication is always
763 // at full USB speed), but they are set by the host so we can
764 // set them properly if we're converting the USB to a real serial
765 // communication
766 uint32_t usb_serial_get_baud(void)
767 {
768         uint32_t *baud = (uint32_t*)cdc_line_coding;
769         return *baud;
770 }
771 uint8_t usb_serial_get_stopbits(void)
772 {
773         return cdc_line_coding[4];
774 }
775 uint8_t usb_serial_get_paritytype(void)
776 {
777         return cdc_line_coding[5];
778 }
779 uint8_t usb_serial_get_numbits(void)
780 {
781         return cdc_line_coding[6];
782 }
783 uint8_t usb_serial_get_control(void)
784 {
785         return cdc_line_rtsdtr;
786 }
787
788 // write the control signals, DCD, DSR, RI, etc
789 // There is no CTS signal.  If software on the host has transmitted
790 // data to you but you haven't been calling the getchar function,
791 // it remains buffered (either here or on the host) and can not be
792 // lost because you weren't listening at the right time, like it
793 // would in real serial communication.
794 int8_t usb_serial_set_control(uint8_t signals)
795 {
796         uint8_t intr_state;
797
798         intr_state = SREG;
799         cli();
800         if (!usb_configuration) {
801                 // we're not enumerated/configured
802                 SREG = intr_state;
803                 return -1;
804         }
805
806         UENUM = CDC_ACM_ENDPOINT;
807         if (!(UEINTX & (1<<RWAL))) {
808                 // unable to write
809                 // TODO; should this try to abort the previously
810                 // buffered message??
811                 SREG = intr_state;
812                 return -1;
813         }
814         UEDATX = 0xA1;
815         UEDATX = 0x20;
816         UEDATX = 0;
817         UEDATX = 0;
818         UEDATX = 0; // 0 seems to work nicely.  what if this is 1??
819         UEDATX = 0;
820         UEDATX = 1;
821         UEDATX = 0;
822         UEDATX = signals;
823         UEINTX = 0x3A;
824         SREG = intr_state;
825         return 0;
826 }
827
828
829
830 /**************************************************************************
831  *
832  *  Private Functions - not intended for general user consumption....
833  *
834  **************************************************************************/
835
836
837
838 // USB Device Interrupt - handle all device-level events
839 // the transmit buffer flushing is triggered by the start of frame
840 //
841 ISR(USB_GEN_vect)
842 {
843         uint8_t intbits, t_cdc, i;
844         static uint8_t div4=0;
845
846         intbits = UDINT;
847         UDINT = 0;
848         if (intbits & (1<<EORSTI)) {
849                 UENUM = 0;
850                 UECONX = 1;
851                 UECFG0X = EP_TYPE_CONTROL;
852                 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
853                 UEIENX = (1<<RXSTPE);
854                 usb_configuration = 0;
855                 cdc_line_rtsdtr = 0;
856         }
857         if ((intbits & (1<<SOFI)) && usb_configuration) {
858                 t_cdc = transmit_flush_timer;
859                 if (t_cdc) {
860                         transmit_flush_timer = --t_cdc;
861                         if (!t_cdc) {
862                                 UENUM = CDC_TX_ENDPOINT;
863                                 UEINTX = 0x3A;
864                         }
865                 }
866                 if (USBKeys_Idle_Config && (++div4 & 3) == 0) {
867                         UENUM = KEYBOARD_ENDPOINT;
868                         if (UEINTX & (1<<RWAL)) {
869                                 USBKeys_Idle_Count++;
870                                 if (USBKeys_Idle_Count == USBKeys_Idle_Config) {
871                                         USBKeys_Idle_Count = 0;
872                                         UEDATX = USBKeys_Modifiers;
873                                         UEDATX = 0;
874                                         for (i=0; i<6; i++) {
875                                                 UEDATX = USBKeys_Array[i];
876                                         }
877                                         UEINTX = 0x3A;
878                                 }
879                         }
880                 }
881         }
882 }
883
884
885
886 // Misc functions to wait for ready and send/receive packets
887 static inline void usb_wait_in_ready(void)
888 {
889         while (!(UEINTX & (1<<TXINI))) ;
890 }
891 static inline void usb_send_in(void)
892 {
893         UEINTX = ~(1<<TXINI);
894 }
895 static inline void usb_wait_receive_out(void)
896 {
897         while (!(UEINTX & (1<<RXOUTI))) ;
898 }
899 static inline void usb_ack_out(void)
900 {
901         UEINTX = ~(1<<RXOUTI);
902 }
903
904
905
906 // USB Endpoint Interrupt - endpoint 0 is handled here.  The
907 // other endpoints are manipulated by the user-callable
908 // functions, and the start-of-frame interrupt.
909 //
910 ISR(USB_COM_vect)
911 {
912         uint8_t intbits;
913         const uint8_t *list;
914         const uint8_t *cfg;
915         uint8_t i, n, len, en;
916         uint8_t *p;
917         uint8_t bmRequestType;
918         uint8_t bRequest;
919         uint16_t wValue;
920         uint16_t wIndex;
921         uint16_t wLength;
922         uint16_t desc_val;
923         const uint8_t *desc_addr;
924         uint8_t desc_length;
925
926         UENUM = 0;
927         intbits = UEINTX;
928         if (intbits & (1<<RXSTPI)) {
929                 bmRequestType = UEDATX;
930                 bRequest = UEDATX;
931                 wValue = UEDATX;
932                 wValue |= (UEDATX << 8);
933                 wIndex = UEDATX;
934                 wIndex |= (UEDATX << 8);
935                 wLength = UEDATX;
936                 wLength |= (UEDATX << 8);
937                 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
938                 if (bRequest == GET_DESCRIPTOR) {
939                         list = (const uint8_t *)descriptor_list;
940                         for (i=0; ; i++) {
941                                 if (i >= NUM_DESC_LIST) {
942                                         UECONX = (1<<STALLRQ)|(1<<EPEN);  //stall
943                                         return;
944                                 }
945                                 desc_val = pgm_read_word(list);
946                                 if (desc_val != wValue) {
947                                         list += sizeof(struct descriptor_list_struct);
948                                         continue;
949                                 }
950                                 list += 2;
951                                 desc_val = pgm_read_word(list);
952                                 if (desc_val != wIndex) {
953                                         list += sizeof(struct descriptor_list_struct)-2;
954                                         continue;
955                                 }
956                                 list += 2;
957                                 desc_addr = (const uint8_t *)pgm_read_word(list);
958                                 list += 2;
959                                 desc_length = pgm_read_byte(list);
960                                 break;
961                         }
962                         len = (wLength < 256) ? wLength : 255;
963                         if (len > desc_length) len = desc_length;
964                         do {
965                                 // wait for host ready for IN packet
966                                 do {
967                                         i = UEINTX;
968                                 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
969                                 if (i & (1<<RXOUTI)) return;    // abort
970                                 // send IN packet
971                                 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
972                                 for (i = n; i; i--) {
973                                         UEDATX = pgm_read_byte(desc_addr++);
974                                 }
975                                 len -= n;
976                                 usb_send_in();
977                         } while (len || n == ENDPOINT0_SIZE);
978                         return;
979                 }
980                 if (bRequest == SET_ADDRESS) {
981                         usb_send_in();
982                         usb_wait_in_ready();
983                         UDADDR = wValue | (1<<ADDEN);
984                         return;
985                 }
986                 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
987                         usb_configuration = wValue;
988                         cdc_line_rtsdtr = 0;
989                         transmit_flush_timer = 0;
990                         usb_send_in();
991                         cfg = endpoint_config_table;
992                         for (i=1; i<6; i++) { // 4+1 of 7 endpoints are used // XXX Important to change if more endpoints are used
993                                 UENUM = i;
994                                 en = pgm_read_byte(cfg++);
995                                 UECONX = en;
996                                 if (en) {
997                                         UECFG0X = pgm_read_byte(cfg++);
998                                         UECFG1X = pgm_read_byte(cfg++);
999                                 }
1000                         }
1001                         UERST = 0x1E;
1002                         UERST = 0;
1003                         return;
1004                 }
1005                 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
1006                         usb_wait_in_ready();
1007                         UEDATX = usb_configuration;
1008                         usb_send_in();
1009                         return;
1010                 }
1011
1012                 if (bRequest == CDC_GET_LINE_CODING && bmRequestType == 0xA1) {
1013                         usb_wait_in_ready();
1014                         p = cdc_line_coding;
1015                         for (i=0; i<7; i++) {
1016                                 UEDATX = *p++;
1017                         }
1018                         usb_send_in();
1019                         return;
1020                 }
1021
1022                 if (bRequest == CDC_SET_LINE_CODING && bmRequestType == 0x21) {
1023                         usb_wait_receive_out();
1024                         p = cdc_line_coding;
1025                         for (i=0; i<7; i++) {
1026                                 *p++ = UEDATX;
1027                         }
1028                         usb_ack_out();
1029                         usb_send_in();
1030                         return;
1031                 }
1032
1033                 if (bRequest == CDC_SET_CONTROL_LINE_STATE && bmRequestType == 0x21) {
1034                         cdc_line_rtsdtr = wValue;
1035                         usb_wait_in_ready();
1036                         usb_send_in();
1037                         return;
1038                 }
1039
1040                 if (bRequest == GET_STATUS) {
1041                         usb_wait_in_ready();
1042                         i = 0;
1043                         #ifdef SUPPORT_ENDPOINT_HALT
1044                         if (bmRequestType == 0x82) {
1045                                 UENUM = wIndex;
1046                                 if (UECONX & (1<<STALLRQ)) i = 1;
1047                                 UENUM = 0;
1048                         }
1049                         #endif
1050                         UEDATX = i;
1051                         UEDATX = 0;
1052                         usb_send_in();
1053                         return;
1054                 }
1055
1056                 #ifdef SUPPORT_ENDPOINT_HALT
1057                 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
1058                   && bmRequestType == 0x02 && wValue == 0) {
1059                         i = wIndex & 0x7F;
1060                         if (i >= 1 && i <= MAX_ENDPOINT) {
1061                                 usb_send_in();
1062                                 UENUM = i;
1063                                 if (bRequest == SET_FEATURE) {
1064                                         UECONX = (1<<STALLRQ)|(1<<EPEN);
1065                                 } else {
1066                                         UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
1067                                         UERST = (1 << i);
1068                                         UERST = 0;
1069                                 }
1070                                 return;
1071                         }
1072                 }
1073                 #endif
1074
1075                 if (wIndex == KEYBOARD_INTERFACE) {
1076                         if (bmRequestType == 0xA1) {
1077                                 if (bRequest == HID_GET_REPORT) {
1078                                         usb_wait_in_ready();
1079                                         UEDATX = USBKeys_Modifiers;
1080                                         UEDATX = 0;
1081                                         for (i=0; i<6; i++) {
1082                                                 UEDATX = USBKeys_Array[i];
1083                                         }
1084                                         usb_send_in();
1085                                         return;
1086                                 }
1087                                 if (bRequest == HID_GET_IDLE) {
1088                                         usb_wait_in_ready();
1089                                         UEDATX = USBKeys_Idle_Config;
1090                                         usb_send_in();
1091                                         return;
1092                                 }
1093                                 if (bRequest == HID_GET_PROTOCOL) {
1094                                         usb_wait_in_ready();
1095                                         UEDATX = USBKeys_Protocol;
1096                                         usb_send_in();
1097                                         return;
1098                                 }
1099                         }
1100                         if (bmRequestType == 0x21) {
1101                                 if (bRequest == HID_SET_REPORT) {
1102                                         usb_wait_receive_out();
1103                                         USBKeys_LEDs = UEDATX;
1104                                         usb_ack_out();
1105                                         usb_send_in();
1106                                         return;
1107                                 }
1108                                 if (bRequest == HID_SET_IDLE) {
1109                                         USBKeys_Idle_Config = (wValue >> 8);
1110                                         USBKeys_Idle_Count = 0;
1111                                         //usb_wait_in_ready();
1112                                         usb_send_in();
1113                                         return;
1114                                 }
1115                                 if (bRequest == HID_SET_PROTOCOL) {
1116                                         USBKeys_Protocol = wValue;
1117                                         //usb_wait_in_ready();
1118                                         usb_send_in();
1119                                         return;
1120                                 }
1121                         }
1122                 }
1123         }
1124         UECONX = (1<<STALLRQ) | (1<<EPEN);      // stall
1125 }
1126