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)
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
26 #include "usb_keyboard_serial.h"
29 // ----- Functions -----
31 // Set the avr into firmware reload mode
32 void usb_debug_reload()
35 // Disable watchdog, if enabled
36 // Disable all peripherals
39 USBCON = (1 << FRZCLK); // Disable USB
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");
71 // WDT Setup for software reset the chip
79 /**************************************************************************
81 * Configurable Options
83 **************************************************************************/
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 */
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 */
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
111 /**************************************************************************
115 **************************************************************************/
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!
124 static const uint8_t PROGMEM device_descriptor[] = {
126 1, // bDescriptorType
127 0x00, 0x02, // bcdUSB
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
138 1 // bNumConfigurations
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
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 ---
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 // --- Keyboard HID ---
208 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
210 4, // bDescriptorType
211 KEYBOARD_INTERFACE, // bInterfaceNumber
212 0, // bAlternateSetting
214 0x03, // bInterfaceClass (0x03 = HID)
215 0x01, // bInterfaceSubClass (0x01 = Boot)
216 0x01, // bInterfaceProtocol (0x01 = Keyboard)
219 // HID interface descriptor, HID 1.11 spec, section 6.2.1
221 0x21, // bDescriptorType
222 0x11, 0x01, // bcdHID
224 1, // bNumDescriptors
225 0x22, // bDescriptorType
226 LSB(sizeof(keyboard_hid_report_desc)), // wDescriptorLength
227 MSB(sizeof(keyboard_hid_report_desc)),
229 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
231 5, // bDescriptorType
232 KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
233 0x03, // bmAttributes (0x03=intr)
234 KEYBOARD_SIZE, 0, // wMaxPacketSize
235 KEYBOARD_INTERVAL, // bInterval
237 // --- Serial CDC ---
239 // interface association descriptor, USB ECN, Table 9-Z
241 11, // bDescriptorType
242 CDC_STATUS_INTERFACE, // bFirstInterface
243 2, // bInterfaceCount
244 0x02, // bFunctionClass
245 0x02, // bFunctionSubClass
246 0x01, // bFunctionProtocol
249 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
251 4, // bDescriptorType
252 CDC_STATUS_INTERFACE, // bInterfaceNumber
253 0, // bAlternateSetting
255 0x02, // bInterfaceClass
256 0x02, // bInterfaceSubClass
257 0x01, // bInterfaceProtocol
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
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
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
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
286 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
288 5, // bDescriptorType
289 CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
290 0x03, // bmAttributes (0x03=intr)
291 CDC_ACM_SIZE, 0, // wMaxPacketSize
294 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
296 4, // bDescriptorType
297 CDC_DATA_INTERFACE, // bInterfaceNumber
298 0, // bAlternateSetting
300 0x0A, // bInterfaceClass
301 0x00, // bInterfaceSubClass
302 0x00, // bInterfaceProtocol
305 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
307 5, // bDescriptorType
308 CDC_RX_ENDPOINT, // bEndpointAddress
309 0x02, // bmAttributes (0x02=bulk)
310 CDC_RX_SIZE, 0, // wMaxPacketSize
313 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
315 5, // bDescriptorType
316 CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
317 0x02, // bmAttributes (0x02=bulk)
318 CDC_TX_SIZE, 0, // wMaxPacketSize
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 {
327 uint8_t bDescriptorType;
330 static const struct usb_string_descriptor_struct PROGMEM string0 = {
335 static const struct usb_string_descriptor_struct PROGMEM string1 = {
336 sizeof(STR_MANUFACTURER),
340 static const struct usb_string_descriptor_struct PROGMEM string2 = {
345 static const struct usb_string_descriptor_struct PROGMEM string3 = {
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 {
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)}
368 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
371 /**************************************************************************
373 * Variables - these are the only non-stack RAM usage
375 **************************************************************************/
377 // zero when we are not configured, non-zero when enumerated
378 static volatile uint8_t usb_configuration=0;
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;
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;
391 /**************************************************************************
393 * Public Functions - these are the API intended for the user
395 **************************************************************************/
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);
411 // Disable watchdog timer after possible software reset
412 //wdt_init(); // XXX Not working...seems to be ok without this, not sure though
415 // return 0 if the USB is not configured, or the configuration
416 // number selected by the HOST
417 uint8_t usb_configured(void)
419 return usb_configuration;
422 // send the contents of USBKeys_Array and USBKeys_Modifiers
423 int8_t usb_keyboard_send(void)
425 uint8_t i, intr_state, timeout;
427 if (!usb_configuration) return -1;
430 UENUM = KEYBOARD_ENDPOINT;
431 timeout = UDFNUML + 50;
433 // are we ready to transmit?
434 if (UEINTX & (1<<RWAL)) break;
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
443 UENUM = KEYBOARD_ENDPOINT;
445 UEDATX = USBKeys_Modifiers;
447 for (i=0; i<6; i++) {
448 UEDATX = USBKeys_Array[i];
451 USBKeys_Idle_Count = 0;
456 // get the next character, or -1 if nothing received
457 int16_t usb_serial_getchar(void)
459 uint8_t c, intr_state;
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!
466 if (!usb_configuration) {
470 UENUM = CDC_RX_ENDPOINT;
473 if (!(c & (1<<RWAL))) {
475 if (c & (1<<RXOUTI)) {
482 // take one byte out of the buffer
484 // if buffer completely used, release it
485 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
490 // number of bytes available in the receive buffer
491 uint8_t usb_serial_available(void)
493 uint8_t n=0, i, intr_state;
497 if (usb_configuration) {
498 UENUM = CDC_RX_ENDPOINT;
502 if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
509 // discard any buffered input
510 void usb_serial_flush_input(void)
514 if (usb_configuration) {
517 UENUM = CDC_RX_ENDPOINT;
518 while ((UEINTX & (1<<RWAL))) {
525 // transmit a character. 0 returned on success, -1 on error
526 int8_t usb_serial_putchar(uint8_t c)
528 uint8_t timeout, intr_state;
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!
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))) {
544 transmit_previous_timeout = 0;
546 // wait for the FIFO to be ready to accept data
547 timeout = UDFNUML + TRANSMIT_TIMEOUT;
549 // are we ready to transmit?
550 if (UEINTX & (1<<RWAL)) break;
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;
558 // has the USB gone offline?
559 if (!usb_configuration) return -1;
560 // get ready to try checking again
563 UENUM = CDC_TX_ENDPOINT;
565 // actually write the byte into the FIFO
567 // if this completed a packet, transmit it now!
568 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
569 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
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)
581 if (!usb_configuration) return -1;
584 UENUM = CDC_TX_ENDPOINT;
585 if (!(UEINTX & (1<<RWAL))) {
590 // actually write the byte into the FIFO
592 // if this completed a packet, transmit it now!
593 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
594 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
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)
611 int8_t usb_serial_write(const char *buffer, uint16_t size)
613 uint8_t timeout, intr_state, write_size;
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!
622 UENUM = CDC_TX_ENDPOINT;
623 // if we gave up due to timeout before, don't wait again
625 if (transmit_previous_timeout) {
626 if (!(UEINTX & (1<<RWAL))) {
630 transmit_previous_timeout = 0;
633 // each iteration of this loop transmits a packet
635 // wait for the FIFO to be ready to accept data
636 timeout = UDFNUML + TRANSMIT_TIMEOUT;
638 // are we ready to transmit?
639 if (UEINTX & (1<<RWAL)) break;
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;
647 // has the USB gone offline?
648 if (!usb_configuration) return -4;
649 // get ready to try checking again
652 UENUM = CDC_TX_ENDPOINT;
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;
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++;
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++;
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++;
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++;
732 case 1: UEDATX = *buffer++;
735 // if this completed a packet, transmit it now!
736 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
737 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
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)
753 if (transmit_flush_timer) {
754 UENUM = CDC_TX_ENDPOINT;
756 transmit_flush_timer = 0;
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
766 uint32_t usb_serial_get_baud(void)
768 uint32_t *baud = (uint32_t*)cdc_line_coding;
771 uint8_t usb_serial_get_stopbits(void)
773 return cdc_line_coding[4];
775 uint8_t usb_serial_get_paritytype(void)
777 return cdc_line_coding[5];
779 uint8_t usb_serial_get_numbits(void)
781 return cdc_line_coding[6];
783 uint8_t usb_serial_get_control(void)
785 return cdc_line_rtsdtr;
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)
800 if (!usb_configuration) {
801 // we're not enumerated/configured
806 UENUM = CDC_ACM_ENDPOINT;
807 if (!(UEINTX & (1<<RWAL))) {
809 // TODO; should this try to abort the previously
810 // buffered message??
818 UEDATX = 0; // 0 seems to work nicely. what if this is 1??
830 /**************************************************************************
832 * Private Functions - not intended for general user consumption....
834 **************************************************************************/
838 // USB Device Interrupt - handle all device-level events
839 // the transmit buffer flushing is triggered by the start of frame
843 uint8_t intbits, t_cdc, i;
844 static uint8_t div4=0;
848 if (intbits & (1<<EORSTI)) {
851 UECFG0X = EP_TYPE_CONTROL;
852 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
853 UEIENX = (1<<RXSTPE);
854 usb_configuration = 0;
857 if ((intbits & (1<<SOFI)) && usb_configuration) {
858 t_cdc = transmit_flush_timer;
860 transmit_flush_timer = --t_cdc;
862 UENUM = CDC_TX_ENDPOINT;
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;
874 for (i=0; i<6; i++) {
875 UEDATX = USBKeys_Array[i];
886 // Misc functions to wait for ready and send/receive packets
887 static inline void usb_wait_in_ready(void)
889 while (!(UEINTX & (1<<TXINI))) ;
891 static inline void usb_send_in(void)
893 UEINTX = ~(1<<TXINI);
895 static inline void usb_wait_receive_out(void)
897 while (!(UEINTX & (1<<RXOUTI))) ;
899 static inline void usb_ack_out(void)
901 UEINTX = ~(1<<RXOUTI);
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.
915 uint8_t i, n, len, en;
917 uint8_t bmRequestType;
923 const uint8_t *desc_addr;
928 if (intbits & (1<<RXSTPI)) {
929 bmRequestType = UEDATX;
932 wValue |= (UEDATX << 8);
934 wIndex |= (UEDATX << 8);
936 wLength |= (UEDATX << 8);
937 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
938 if (bRequest == GET_DESCRIPTOR) {
939 list = (const uint8_t *)descriptor_list;
941 if (i >= NUM_DESC_LIST) {
942 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
945 desc_val = pgm_read_word(list);
946 if (desc_val != wValue) {
947 list += sizeof(struct descriptor_list_struct);
951 desc_val = pgm_read_word(list);
952 if (desc_val != wIndex) {
953 list += sizeof(struct descriptor_list_struct)-2;
957 desc_addr = (const uint8_t *)pgm_read_word(list);
959 desc_length = pgm_read_byte(list);
962 len = (wLength < 256) ? wLength : 255;
963 if (len > desc_length) len = desc_length;
965 // wait for host ready for IN packet
968 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
969 if (i & (1<<RXOUTI)) return; // abort
971 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
972 for (i = n; i; i--) {
973 UEDATX = pgm_read_byte(desc_addr++);
977 } while (len || n == ENDPOINT0_SIZE);
980 if (bRequest == SET_ADDRESS) {
983 UDADDR = wValue | (1<<ADDEN);
986 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
987 usb_configuration = wValue;
989 transmit_flush_timer = 0;
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
994 en = pgm_read_byte(cfg++);
997 UECFG0X = pgm_read_byte(cfg++);
998 UECFG1X = pgm_read_byte(cfg++);
1005 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
1006 usb_wait_in_ready();
1007 UEDATX = usb_configuration;
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++) {
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++) {
1033 if (bRequest == CDC_SET_CONTROL_LINE_STATE && bmRequestType == 0x21) {
1034 cdc_line_rtsdtr = wValue;
1035 usb_wait_in_ready();
1040 if (bRequest == GET_STATUS) {
1041 usb_wait_in_ready();
1043 #ifdef SUPPORT_ENDPOINT_HALT
1044 if (bmRequestType == 0x82) {
1046 if (UECONX & (1<<STALLRQ)) i = 1;
1056 #ifdef SUPPORT_ENDPOINT_HALT
1057 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
1058 && bmRequestType == 0x02 && wValue == 0) {
1060 if (i >= 1 && i <= MAX_ENDPOINT) {
1063 if (bRequest == SET_FEATURE) {
1064 UECONX = (1<<STALLRQ)|(1<<EPEN);
1066 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
1075 if (wIndex == KEYBOARD_INTERFACE) {
1076 if (bmRequestType == 0xA1) {
1077 if (bRequest == HID_GET_REPORT) {
1078 usb_wait_in_ready();
1079 UEDATX = USBKeys_Modifiers;
1081 for (i=0; i<6; i++) {
1082 UEDATX = USBKeys_Array[i];
1087 if (bRequest == HID_GET_IDLE) {
1088 usb_wait_in_ready();
1089 UEDATX = USBKeys_Idle_Config;
1093 if (bRequest == HID_GET_PROTOCOL) {
1094 usb_wait_in_ready();
1095 UEDATX = USBKeys_Protocol;
1100 if (bmRequestType == 0x21) {
1101 if (bRequest == HID_SET_REPORT) {
1102 usb_wait_receive_out();
1103 USBKeys_LEDs = UEDATX;
1108 if (bRequest == HID_SET_IDLE) {
1109 USBKeys_Idle_Config = (wValue >> 8);
1110 USBKeys_Idle_Count = 0;
1111 //usb_wait_in_ready();
1115 if (bRequest == HID_SET_PROTOCOL) {
1116 USBKeys_Protocol = wValue;
1117 //usb_wait_in_ready();
1124 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall