1 /* vim: set sts=4:sw=4:ts=4:noexpandtab
3 * USB Serial Example for Teensy USB Development Board
4 * http://www.pjrc.com/teensy/usb_serial.html
5 * Copyright (c) 2008,2010,2011 PJRC.COM, LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // Version 1.0: Initial Release
27 // Version 1.1: support Teensy++
28 // Version 1.2: fixed usb_serial_available
29 // Version 1.3: added transmit bandwidth test
30 // Version 1.4: added usb_serial_write
31 // Version 1.5: add support for Teensy 2.0
32 // Version 1.6: fix zero length packet bug
33 // Version 1.7: fix usb_serial_set_control
35 #define USB_SERIAL_PRIVATE_INCLUDE
36 #include "at90usb162_cdc_loopback.h"
38 /**************************************************************************
40 * Configurable Options
42 **************************************************************************/
44 // You can change these to give your code its own name. On Windows,
45 // these are only used before an INF file (driver install) is loaded.
46 #define STR_MANUFACTURER L"Your Name"
47 #define STR_PRODUCT L"USB Serial"
49 // All USB serial devices are supposed to have a serial number
50 // (according to Microsoft). On windows, a new COM port is created
51 // for every unique serial/vendor/product number combination. If
52 // you program 2 identical boards with 2 different serial numbers
53 // and they are assigned COM7 and COM8, each will always get the
54 // same COM port number because Windows remembers serial numbers.
56 // On Mac OS-X, a device file is created automatically which
57 // incorperates the serial number, eg, /dev/cu-usbmodem12341
59 // Linux by default ignores the serial number, and creates device
60 // files named /dev/ttyACM0, /dev/ttyACM1... in the order connected.
61 // Udev rules (in /etc/udev/rules.d) can define persistent device
62 // names linked to this serial number, as well as permissions, owner
63 // and group settings.
64 #define STR_SERIAL_NUMBER L"12345"
66 // Mac OS-X and Linux automatically load the correct drivers. On
67 // Windows, even though the driver is supplied by Microsoft, an
68 // INF file is needed to load the driver. These numbers need to
69 // match the INF file.
70 #define VENDOR_ID 0x16C0
71 #define PRODUCT_ID 0x047A
73 // When you write data, it goes into a USB endpoint buffer, which
74 // is transmitted to the PC when it becomes full, or after a timeout
75 // with no more writes. Even if you write in exactly packet-size
76 // increments, this timeout is used to send a "zero length packet"
77 // that tells the PC no more data is expected and it should pass
78 // any buffered data to the application that may be waiting. If
79 // you want data sent immediately, call usb_serial_flush_output().
80 #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
82 // If the PC is connected but not "listening", this is the length
83 // of time before usb_serial_getchar() returns with an error. This
84 // is roughly equivilant to a real UART simply transmitting the
85 // bits on a wire where nobody is listening, except you get an error
86 // code which you can ignore for serial-like discard of data, or
87 // use to know your data wasn't sent.
88 #define TRANSMIT_TIMEOUT 25 /* in milliseconds */
90 // USB devices are supposed to implment a halt feature, which is
91 // rarely (if ever) used. If you comment this line out, the halt
92 // code will be removed, saving 116 bytes of space (gcc 4.3.0).
93 // This is not strictly USB compliant, but works with all major
95 #define SUPPORT_ENDPOINT_HALT
99 /**************************************************************************
101 * Endpoint Buffer Configuration
103 **************************************************************************/
105 // These buffer sizes are best for most applications, but perhaps if you
106 // want more buffering on some endpoint at the expense of others, this
107 // is where you can make such changes. The AT90USB162 has only 176 bytes
108 // of DPRAM (USB buffers) and only endpoints 3 & 4 can double buffer.
110 #define ENDPOINT0_SIZE 16
111 #define CDC_ACM_ENDPOINT 2
112 #define CDC_RX_ENDPOINT 3
113 #define CDC_TX_ENDPOINT 4
114 #if defined(__AVR_AT90USB162__)
115 #define CDC_ACM_SIZE 16
116 #define CDC_ACM_BUFFER EP_SINGLE_BUFFER
117 #define CDC_RX_SIZE 32
118 #define CDC_RX_BUFFER EP_DOUBLE_BUFFER
119 #define CDC_TX_SIZE 32
120 #define CDC_TX_BUFFER EP_DOUBLE_BUFFER
122 #define CDC_ACM_SIZE 16
123 #define CDC_ACM_BUFFER EP_SINGLE_BUFFER
124 #define CDC_RX_SIZE 64
125 #define CDC_RX_BUFFER EP_DOUBLE_BUFFER
126 #define CDC_TX_SIZE 64
127 #define CDC_TX_BUFFER EP_DOUBLE_BUFFER
130 static const uint8_t PROGMEM endpoint_config_table[] = {
132 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(CDC_ACM_SIZE) | CDC_ACM_BUFFER,
133 1, EP_TYPE_BULK_OUT, EP_SIZE(CDC_RX_SIZE) | CDC_RX_BUFFER,
134 1, EP_TYPE_BULK_IN, EP_SIZE(CDC_TX_SIZE) | CDC_TX_BUFFER
138 /**************************************************************************
142 **************************************************************************/
144 // Descriptors are the data that your computer reads when it auto-detects
145 // this USB device (called "enumeration" in USB lingo). The most commonly
146 // changed items are editable at the top of this file. Changing things
147 // in here should only be done by those who've read chapter 9 of the USB
148 // spec and relevant portions of any USB class specifications!
150 static uint8_t PROGMEM device_descriptor[] = {
152 1, // bDescriptorType
153 0x00, 0x02, // bcdUSB
155 0, // bDeviceSubClass
156 0, // bDeviceProtocol
157 ENDPOINT0_SIZE, // bMaxPacketSize0
158 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
159 LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
160 0x00, 0x01, // bcdDevice
164 1 // bNumConfigurations
167 #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
168 static uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
169 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
171 2, // bDescriptorType;
172 LSB(CONFIG1_DESC_SIZE), // wTotalLength
173 MSB(CONFIG1_DESC_SIZE),
175 1, // bConfigurationValue
177 0xC0, // bmAttributes
179 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
181 4, // bDescriptorType
182 0, // bInterfaceNumber
183 0, // bAlternateSetting
185 0x02, // bInterfaceClass
186 0x02, // bInterfaceSubClass
187 0x01, // bInterfaceProtocol
189 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
190 5, // bFunctionLength
191 0x24, // bDescriptorType
192 0x00, // bDescriptorSubtype
193 0x10, 0x01, // bcdCDC
194 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
195 5, // bFunctionLength
196 0x24, // bDescriptorType
197 0x01, // bDescriptorSubtype
198 0x01, // bmCapabilities
200 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
201 4, // bFunctionLength
202 0x24, // bDescriptorType
203 0x02, // bDescriptorSubtype
204 0x06, // bmCapabilities
205 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
206 5, // bFunctionLength
207 0x24, // bDescriptorType
208 0x06, // bDescriptorSubtype
209 0, // bMasterInterface
210 1, // bSlaveInterface0
211 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
213 5, // bDescriptorType
214 CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
215 0x03, // bmAttributes (0x03=intr)
216 CDC_ACM_SIZE, 0, // wMaxPacketSize
218 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
220 4, // bDescriptorType
221 1, // bInterfaceNumber
222 0, // bAlternateSetting
224 0x0A, // bInterfaceClass
225 0x00, // bInterfaceSubClass
226 0x00, // bInterfaceProtocol
228 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
230 5, // bDescriptorType
231 CDC_RX_ENDPOINT, // bEndpointAddress
232 0x02, // bmAttributes (0x02=bulk)
233 CDC_RX_SIZE, 0, // wMaxPacketSize
235 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
237 5, // bDescriptorType
238 CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
239 0x02, // bmAttributes (0x02=bulk)
240 CDC_TX_SIZE, 0, // wMaxPacketSize
244 // If you're desperate for a little extra code memory, these strings
245 // can be completely removed if iManufacturer, iProduct, iSerialNumber
246 // in the device desciptor are changed to zeros.
247 struct usb_string_descriptor_struct {
249 uint8_t bDescriptorType;
252 static struct usb_string_descriptor_struct PROGMEM string0 = {
257 static struct usb_string_descriptor_struct PROGMEM string1 = {
258 sizeof(STR_MANUFACTURER),
262 static struct usb_string_descriptor_struct PROGMEM string2 = {
267 static struct usb_string_descriptor_struct PROGMEM string3 = {
268 sizeof(STR_SERIAL_NUMBER),
273 // This table defines which descriptor data is sent for each specific
274 // request from the host (in wValue and wIndex).
275 static struct descriptor_list_struct {
280 } PROGMEM descriptor_list[] = {
281 {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
282 {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
283 {0x0300, 0x0000, (const uint8_t *)&string0, 4},
284 {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
285 {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
286 {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL_NUMBER)}
288 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
291 /**************************************************************************
293 * Variables - these are the only non-stack RAM usage
295 **************************************************************************/
297 // zero when we are not configured, non-zero when enumerated
298 static volatile uint8_t usb_configuration=0;
300 // the time remaining before we transmit any partially full
301 // packet, or send a zero length packet.
302 static volatile uint8_t transmit_flush_timer=0;
303 static uint8_t transmit_previous_timeout=0;
305 // serial port settings (baud rate, control signals, etc) set
306 // by the PC. These are ignored, but kept in RAM.
307 static uint8_t cdc_line_coding[7]={0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
308 static uint8_t cdc_line_rtsdtr=0;
311 /**************************************************************************
313 * Public Functions - these are the API intended for the user
315 **************************************************************************/
317 // initialize USB serial
322 USB_FREEZE(); // enable USB
323 PLL_CONFIG(); // config PLL, 16 MHz xtal
324 while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
325 USB_CONFIG(); // start USB clock
326 UDCON = 0; // enable attach resistor
327 usb_configuration = 0;
329 UDIEN = (1<<EORSTE)|(1<<SOFE);
332 // return 0 if the USB is not configured, or the configuration
333 // number selected by the HOST
334 uint8_t usb_configured(void)
336 return usb_configuration;
339 // get the next character, or -1 if nothing received
340 int16_t usb_serial_getchar(void)
342 uint8_t c, intr_state;
344 // interrupts are disabled so these functions can be
345 // used from the main program or interrupt context,
346 // even both in the same program!
349 if (!usb_configuration) {
353 UENUM = CDC_RX_ENDPOINT;
356 if (!(c & (1<<RWAL))) {
358 if (c & (1<<RXOUTI)) {
365 // take one byte out of the buffer
367 // if buffer completely used, release it
368 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
373 // number of bytes available in the receive buffer
374 uint8_t usb_serial_available(void)
376 uint8_t n=0, i, intr_state;
380 if (usb_configuration) {
381 UENUM = CDC_RX_ENDPOINT;
385 if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
392 // discard any buffered input
393 void usb_serial_flush_input(void)
397 if (usb_configuration) {
400 UENUM = CDC_RX_ENDPOINT;
401 while ((UEINTX & (1<<RWAL))) {
408 // transmit a character. 0 returned on success, -1 on error
409 int8_t usb_serial_putchar(uint8_t c)
411 uint8_t timeout, intr_state;
413 // if we're not online (enumerated and configured), error
414 if (!usb_configuration) return -1;
415 // interrupts are disabled so these functions can be
416 // used from the main program or interrupt context,
417 // even both in the same program!
420 UENUM = CDC_TX_ENDPOINT;
421 // if we gave up due to timeout before, don't wait again
422 if (transmit_previous_timeout) {
423 if (!(UEINTX & (1<<RWAL))) {
427 transmit_previous_timeout = 0;
429 // wait for the FIFO to be ready to accept data
430 timeout = UDFNUML + TRANSMIT_TIMEOUT;
432 // are we ready to transmit?
433 if (UEINTX & (1<<RWAL)) break;
435 // have we waited too long? This happens if the user
436 // is not running an application that is listening
437 if (UDFNUML == timeout) {
438 transmit_previous_timeout = 1;
441 // has the USB gone offline?
442 if (!usb_configuration) return -1;
443 // get ready to try checking again
446 UENUM = CDC_TX_ENDPOINT;
448 // actually write the byte into the FIFO
450 // if this completed a packet, transmit it now!
451 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
452 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
458 // transmit a character, but do not wait if the buffer is full,
459 // 0 returned on success, -1 on buffer full or error
460 int8_t usb_serial_putchar_nowait(uint8_t c)
464 if (!usb_configuration) return -1;
467 UENUM = CDC_TX_ENDPOINT;
468 if (!(UEINTX & (1<<RWAL))) {
473 // actually write the byte into the FIFO
475 // if this completed a packet, transmit it now!
476 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
477 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
482 // transmit a buffer.
483 // 0 returned on success, -1 on error
484 // This function is optimized for speed! Each call takes approx 6.1 us overhead
485 // plus 0.25 us per byte. 12 Mbit/sec USB has 8.67 us per-packet overhead and
486 // takes 0.67 us per byte. If called with 64 byte packet-size blocks, this function
487 // can transmit at full USB speed using 43% CPU time. The maximum theoretical speed
488 // is 19 packets per USB frame, or 1216 kbytes/sec. However, bulk endpoints have the
489 // lowest priority, so any other USB devices will likely reduce the speed. Speed
490 // can also be limited by how quickly the PC-based software reads data, as the host
491 // controller in the PC will not allocate bandwitdh without a pending read request.
492 // (thanks to Victor Suarez for testing and feedback and initial code)
494 int8_t usb_serial_write(const uint8_t *buffer, uint16_t size)
496 uint8_t timeout, intr_state, write_size;
498 // if we're not online (enumerated and configured), error
499 if (!usb_configuration) return -1;
500 // interrupts are disabled so these functions can be
501 // used from the main program or interrupt context,
502 // even both in the same program!
505 UENUM = CDC_TX_ENDPOINT;
506 // if we gave up due to timeout before, don't wait again
507 if (transmit_previous_timeout) {
508 if (!(UEINTX & (1<<RWAL))) {
512 transmit_previous_timeout = 0;
514 // each iteration of this loop transmits a packet
516 // wait for the FIFO to be ready to accept data
517 timeout = UDFNUML + TRANSMIT_TIMEOUT;
519 // are we ready to transmit?
520 if (UEINTX & (1<<RWAL)) break;
522 // have we waited too long? This happens if the user
523 // is not running an application that is listening
524 if (UDFNUML == timeout) {
525 transmit_previous_timeout = 1;
528 // has the USB gone offline?
529 if (!usb_configuration) return -1;
530 // get ready to try checking again
533 UENUM = CDC_TX_ENDPOINT;
536 // compute how many bytes will fit into the next packet
537 write_size = CDC_TX_SIZE - UEBCLX;
538 if (write_size > size) write_size = size;
542 switch (write_size) {
543 #if (CDC_TX_SIZE == 64)
544 case 64: UEDATX = *buffer++;
545 case 63: UEDATX = *buffer++;
546 case 62: UEDATX = *buffer++;
547 case 61: UEDATX = *buffer++;
548 case 60: UEDATX = *buffer++;
549 case 59: UEDATX = *buffer++;
550 case 58: UEDATX = *buffer++;
551 case 57: UEDATX = *buffer++;
552 case 56: UEDATX = *buffer++;
553 case 55: UEDATX = *buffer++;
554 case 54: UEDATX = *buffer++;
555 case 53: UEDATX = *buffer++;
556 case 52: UEDATX = *buffer++;
557 case 51: UEDATX = *buffer++;
558 case 50: UEDATX = *buffer++;
559 case 49: UEDATX = *buffer++;
560 case 48: UEDATX = *buffer++;
561 case 47: UEDATX = *buffer++;
562 case 46: UEDATX = *buffer++;
563 case 45: UEDATX = *buffer++;
564 case 44: UEDATX = *buffer++;
565 case 43: UEDATX = *buffer++;
566 case 42: UEDATX = *buffer++;
567 case 41: UEDATX = *buffer++;
568 case 40: UEDATX = *buffer++;
569 case 39: UEDATX = *buffer++;
570 case 38: UEDATX = *buffer++;
571 case 37: UEDATX = *buffer++;
572 case 36: UEDATX = *buffer++;
573 case 35: UEDATX = *buffer++;
574 case 34: UEDATX = *buffer++;
575 case 33: UEDATX = *buffer++;
577 #if (CDC_TX_SIZE >= 32)
578 case 32: UEDATX = *buffer++;
579 case 31: UEDATX = *buffer++;
580 case 30: UEDATX = *buffer++;
581 case 29: UEDATX = *buffer++;
582 case 28: UEDATX = *buffer++;
583 case 27: UEDATX = *buffer++;
584 case 26: UEDATX = *buffer++;
585 case 25: UEDATX = *buffer++;
586 case 24: UEDATX = *buffer++;
587 case 23: UEDATX = *buffer++;
588 case 22: UEDATX = *buffer++;
589 case 21: UEDATX = *buffer++;
590 case 20: UEDATX = *buffer++;
591 case 19: UEDATX = *buffer++;
592 case 18: UEDATX = *buffer++;
593 case 17: UEDATX = *buffer++;
595 #if (CDC_TX_SIZE >= 16)
596 case 16: UEDATX = *buffer++;
597 case 15: UEDATX = *buffer++;
598 case 14: UEDATX = *buffer++;
599 case 13: UEDATX = *buffer++;
600 case 12: UEDATX = *buffer++;
601 case 11: UEDATX = *buffer++;
602 case 10: UEDATX = *buffer++;
603 case 9: UEDATX = *buffer++;
605 case 8: UEDATX = *buffer++;
606 case 7: UEDATX = *buffer++;
607 case 6: UEDATX = *buffer++;
608 case 5: UEDATX = *buffer++;
609 case 4: UEDATX = *buffer++;
610 case 3: UEDATX = *buffer++;
611 case 2: UEDATX = *buffer++;
613 case 1: UEDATX = *buffer++;
616 // if this completed a packet, transmit it now!
617 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
618 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
625 // immediately transmit any buffered output.
626 // This doesn't actually transmit the data - that is impossible!
627 // USB devices only transmit when the host allows, so the best
628 // we can do is release the FIFO buffer for when the host wants it
629 void usb_serial_flush_output(void)
635 if (transmit_flush_timer) {
636 UENUM = CDC_TX_ENDPOINT;
638 transmit_flush_timer = 0;
643 // functions to read the various async serial settings. These
644 // aren't actually used by USB at all (communication is always
645 // at full USB speed), but they are set by the host so we can
646 // set them properly if we're converting the USB to a real serial
648 // uint32_t usb_serial_get_baud(void)
650 // return *(uint32_t *)cdc_line_coding;
652 uint8_t usb_serial_get_stopbits(void)
654 return cdc_line_coding[4];
656 uint8_t usb_serial_get_paritytype(void)
658 return cdc_line_coding[5];
660 uint8_t usb_serial_get_numbits(void)
662 return cdc_line_coding[6];
664 uint8_t usb_serial_get_control(void)
666 return cdc_line_rtsdtr;
668 // write the control signals, DCD, DSR, RI, etc
669 // There is no CTS signal. If software on the host has transmitted
670 // data to you but you haven't been calling the getchar function,
671 // it remains buffered (either here or on the host) and can not be
672 // lost because you weren't listening at the right time, like it
673 // would in real serial communication.
674 int8_t usb_serial_set_control(uint8_t signals)
680 if (!usb_configuration) {
681 // we're not enumerated/configured
686 UENUM = CDC_ACM_ENDPOINT;
687 if (!(UEINTX & (1<<RWAL))) {
689 // TODO; should this try to abort the previously
690 // buffered message??
698 UEDATX = 0; // 0 seems to work nicely. what if this is 1??
710 /**************************************************************************
712 * Private Functions - not intended for general user consumption....
714 **************************************************************************/
717 // USB Device Interrupt - handle all device-level events
718 // the transmit buffer flushing is triggered by the start of frame
720 ISR(USB_GEN_vect, ISR_BLOCK)
725 if (intbits & (1<<EORSTI)) {
728 UECFG0X = EP_TYPE_CONTROL;
729 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
730 UEIENX = (1<<RXSTPE);
731 usb_configuration = 0;
734 if (intbits & (1<<SOFI)) {
735 if (usb_configuration) {
736 t = transmit_flush_timer;
738 transmit_flush_timer = --t;
740 UENUM = CDC_TX_ENDPOINT;
749 // Misc functions to wait for ready and send/receive packets
750 static inline void usb_wait_in_ready(void)
752 while (!(UEINTX & (1<<TXINI))) ;
754 static inline void usb_send_in(void)
756 UEINTX = ~(1<<TXINI);
758 static inline void usb_wait_receive_out(void)
760 while (!(UEINTX & (1<<RXOUTI))) ;
762 static inline void usb_ack_out(void)
764 UEINTX = ~(1<<RXOUTI);
769 // USB Endpoint Interrupt - endpoint 0 is handled here. The
770 // other endpoints are manipulated by the user-callable
771 // functions, and the start-of-frame interrupt.
773 ISR(USB_COM_vect, ISR_BLOCK)
778 uint8_t i, n, len, en;
780 uint8_t bmRequestType;
786 const uint8_t *desc_addr;
791 if (intbits & (1<<RXSTPI)) {
792 bmRequestType = UEDATX;
795 wValue |= (UEDATX << 8);
797 wIndex |= (UEDATX << 8);
799 wLength |= (UEDATX << 8);
800 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
801 if (bRequest == GET_DESCRIPTOR) {
802 list = (const uint8_t *)descriptor_list;
804 if (i >= NUM_DESC_LIST) {
805 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
808 desc_val = pgm_read_word(list);
809 if (desc_val != wValue) {
810 list += sizeof(struct descriptor_list_struct);
814 desc_val = pgm_read_word(list);
815 if (desc_val != wIndex) {
816 list += sizeof(struct descriptor_list_struct)-2;
820 desc_addr = (const uint8_t *)pgm_read_word(list);
822 desc_length = pgm_read_byte(list);
825 len = (wLength < 256) ? wLength : 255;
826 if (len > desc_length) len = desc_length;
828 // wait for host ready for IN packet
831 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
837 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
838 for (i = n; i; i--) {
839 UEDATX = pgm_read_byte(desc_addr++);
843 } while (len || n == ENDPOINT0_SIZE);
846 if (bRequest == SET_ADDRESS) {
849 UDADDR = wValue | (1<<ADDEN);
852 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
853 usb_configuration = wValue;
855 transmit_flush_timer = 0;
857 cfg = endpoint_config_table;
858 for (i=1; i<5; i++) {
860 en = pgm_read_byte(cfg++);
863 UECFG0X = pgm_read_byte(cfg++);
864 UECFG1X = pgm_read_byte(cfg++);
871 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
873 UEDATX = usb_configuration;
877 if (bRequest == CDC_GET_LINE_CODING && bmRequestType == 0xA1) {
880 for (i=0; i<7; i++) {
886 if (bRequest == CDC_SET_LINE_CODING && bmRequestType == 0x21) {
887 usb_wait_receive_out();
889 for (i=0; i<7; i++) {
896 if (bRequest == CDC_SET_CONTROL_LINE_STATE && bmRequestType == 0x21) {
897 cdc_line_rtsdtr = wValue;
902 if (bRequest == GET_STATUS) {
905 #ifdef SUPPORT_ENDPOINT_HALT
906 if (bmRequestType == 0x82) {
908 if (UECONX & (1<<STALLRQ)) i = 1;
917 #ifdef SUPPORT_ENDPOINT_HALT
918 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
919 && bmRequestType == 0x02 && wValue == 0) {
921 if (i >= 1 && i <= MAX_ENDPOINT) {
924 if (bRequest == SET_FEATURE) {
925 UECONX = (1<<STALLRQ)|(1<<EPEN);
927 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
936 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
940 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
944 static int dw_putchar(char __c, FILE *__stream) { DWDR = __c;(void)__stream; return __c;}
945 FILE dwout = FDEV_SETUP_STREAM(dw_putchar, NULL, _FDEV_SETUP_WRITE);
949 // Very simple character echo test
954 while(usb_configured()==0);
956 int n = usb_serial_getchar();
957 if (n >= 0) usb_serial_putchar(n);