examples: New board_usb
[simavr] / examples / board_usb / at90usb162_cdc_loopback.c
1 /* vim: set sts=4:sw=4:ts=4:noexpandtab
2  *
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
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  */
25
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
34
35 #define USB_SERIAL_PRIVATE_INCLUDE
36 #include "at90usb162_cdc_loopback.h"
37
38 /**************************************************************************
39  *
40  *  Configurable Options
41  *
42  **************************************************************************/
43
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"
48
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.
55 //
56 // On Mac OS-X, a device file is created automatically which
57 // incorperates the serial number, eg, /dev/cu-usbmodem12341
58 //
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"
65
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
72
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 */
81
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 */
89
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
94 // operating systems.
95 #define SUPPORT_ENDPOINT_HALT
96
97
98
99 /**************************************************************************
100  *
101  *  Endpoint Buffer Configuration
102  *
103  **************************************************************************/
104
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.
109
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
121 #else
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
128 #endif
129
130 static const uint8_t PROGMEM endpoint_config_table[] = {
131         0,
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
135 };
136
137
138 /**************************************************************************
139  *
140  *  Descriptor Data
141  *
142  **************************************************************************/
143
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!
149
150 static uint8_t PROGMEM device_descriptor[] = {
151         18,                                     // bLength
152         1,                                      // bDescriptorType
153         0x00, 0x02,                             // bcdUSB
154         2,                                      // bDeviceClass
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
161         1,                                      // iManufacturer
162         2,                                      // iProduct
163         3,                                      // iSerialNumber
164         1                                       // bNumConfigurations
165 };
166
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
170         9,                                      // bLength;
171         2,                                      // bDescriptorType;
172         LSB(CONFIG1_DESC_SIZE),                 // wTotalLength
173         MSB(CONFIG1_DESC_SIZE),
174         2,                                      // bNumInterfaces
175         1,                                      // bConfigurationValue
176         0,                                      // iConfiguration
177         0xC0,                                   // bmAttributes
178         50,                                     // bMaxPower
179         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
180         9,                                      // bLength
181         4,                                      // bDescriptorType
182         0,                                      // bInterfaceNumber
183         0,                                      // bAlternateSetting
184         1,                                      // bNumEndpoints
185         0x02,                                   // bInterfaceClass
186         0x02,                                   // bInterfaceSubClass
187         0x01,                                   // bInterfaceProtocol
188         0,                                      // iInterface
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
199         1,                                      // bDataInterface
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
212         7,                                      // bLength
213         5,                                      // bDescriptorType
214         CDC_ACM_ENDPOINT | 0x80,                // bEndpointAddress
215         0x03,                                   // bmAttributes (0x03=intr)
216         CDC_ACM_SIZE, 0,                        // wMaxPacketSize
217         64,                                     // bInterval
218         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
219         9,                                      // bLength
220         4,                                      // bDescriptorType
221         1,                                      // bInterfaceNumber
222         0,                                      // bAlternateSetting
223         2,                                      // bNumEndpoints
224         0x0A,                                   // bInterfaceClass
225         0x00,                                   // bInterfaceSubClass
226         0x00,                                   // bInterfaceProtocol
227         0,                                      // iInterface
228         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
229         7,                                      // bLength
230         5,                                      // bDescriptorType
231         CDC_RX_ENDPOINT,                        // bEndpointAddress
232         0x02,                                   // bmAttributes (0x02=bulk)
233         CDC_RX_SIZE, 0,                         // wMaxPacketSize
234         0,                                      // bInterval
235         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
236         7,                                      // bLength
237         5,                                      // bDescriptorType
238         CDC_TX_ENDPOINT | 0x80,                 // bEndpointAddress
239         0x02,                                   // bmAttributes (0x02=bulk)
240         CDC_TX_SIZE, 0,                         // wMaxPacketSize
241         0                                       // bInterval
242 };
243
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 {
248         uint8_t bLength;
249         uint8_t bDescriptorType;
250         int16_t wString[];
251 };
252 static struct usb_string_descriptor_struct PROGMEM string0 = {
253         4,
254         3,
255         {0x0409}
256 };
257 static struct usb_string_descriptor_struct PROGMEM string1 = {
258         sizeof(STR_MANUFACTURER),
259         3,
260         STR_MANUFACTURER
261 };
262 static struct usb_string_descriptor_struct PROGMEM string2 = {
263         sizeof(STR_PRODUCT),
264         3,
265         STR_PRODUCT
266 };
267 static struct usb_string_descriptor_struct PROGMEM string3 = {
268         sizeof(STR_SERIAL_NUMBER),
269         3,
270         STR_SERIAL_NUMBER
271 };
272
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 {
276         uint16_t        wValue;
277         uint16_t        wIndex;
278         const uint8_t   *addr;
279         uint8_t         length;
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)}
287 };
288 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
289
290
291 /**************************************************************************
292  *
293  *  Variables - these are the only non-stack RAM usage
294  *
295  **************************************************************************/
296
297 // zero when we are not configured, non-zero when enumerated
298 static volatile uint8_t usb_configuration=0;
299
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;
304
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;
309
310
311 /**************************************************************************
312  *
313  *  Public Functions - these are the API intended for the user
314  *
315  **************************************************************************/
316
317 // initialize USB serial
318 void usb_init(void)
319 {
320         sei();
321         HW_CONFIG();
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;
328         cdc_line_rtsdtr = 0;
329                 UDIEN = (1<<EORSTE)|(1<<SOFE);
330 }
331
332 // return 0 if the USB is not configured, or the configuration
333 // number selected by the HOST
334 uint8_t usb_configured(void)
335 {
336         return usb_configuration;
337 }
338
339 // get the next character, or -1 if nothing received
340 int16_t usb_serial_getchar(void)
341 {
342         uint8_t c, intr_state;
343
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!
347         intr_state = SREG;
348         cli();
349         if (!usb_configuration) {
350                 SREG = intr_state;
351                 return -1;
352         }
353         UENUM = CDC_RX_ENDPOINT;
354         retry:
355         c = UEINTX;
356         if (!(c & (1<<RWAL))) {
357                 // no data in buffer
358                 if (c & (1<<RXOUTI)) {
359                         UEINTX = 0x6B;
360                         goto retry;
361                 }
362                 SREG = intr_state;
363                 return -1;
364         }
365         // take one byte out of the buffer
366         c = UEDATX;
367         // if buffer completely used, release it
368         if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
369         SREG = intr_state;
370         return c;
371 }
372
373 // number of bytes available in the receive buffer
374 uint8_t usb_serial_available(void)
375 {
376         uint8_t n=0, i, intr_state;
377
378         intr_state = SREG;
379         cli();
380         if (usb_configuration) {
381                 UENUM = CDC_RX_ENDPOINT;
382                 n = UEBCLX;
383                 if (!n) {
384                         i = UEINTX;
385                         if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
386                 }
387         }
388         SREG = intr_state;
389         return n;
390 }
391
392 // discard any buffered input
393 void usb_serial_flush_input(void)
394 {
395         uint8_t intr_state;
396
397         if (usb_configuration) {
398                 intr_state = SREG;
399                 cli();
400                 UENUM = CDC_RX_ENDPOINT;
401                 while ((UEINTX & (1<<RWAL))) {
402                         UEINTX = 0x6B;
403                 }
404                 SREG = intr_state;
405         }
406 }
407
408 // transmit a character.  0 returned on success, -1 on error
409 int8_t usb_serial_putchar(uint8_t c)
410 {
411         uint8_t timeout, intr_state;
412
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!
418         intr_state = SREG;
419         cli();
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))) {
424                         SREG = intr_state;
425                         return -1;
426                 }
427                 transmit_previous_timeout = 0;
428         }
429         // wait for the FIFO to be ready to accept data
430         timeout = UDFNUML + TRANSMIT_TIMEOUT;
431         while (1) {
432                 // are we ready to transmit?
433                 if (UEINTX & (1<<RWAL)) break;
434                 SREG = intr_state;
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;
439                         return -1;
440                 }
441                 // has the USB gone offline?
442                 if (!usb_configuration) return -1;
443                 // get ready to try checking again
444                 intr_state = SREG;
445                 cli();
446                 UENUM = CDC_TX_ENDPOINT;
447         }
448         // actually write the byte into the FIFO
449         UEDATX = c;
450         // if this completed a packet, transmit it now!
451         if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
452         transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
453         SREG = intr_state;
454         return 0;
455 }
456
457
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)
461 {
462         uint8_t intr_state;
463
464         if (!usb_configuration) return -1;
465         intr_state = SREG;
466         cli();
467         UENUM = CDC_TX_ENDPOINT;
468         if (!(UEINTX & (1<<RWAL))) {
469                 // buffer is full
470                 SREG = intr_state;
471                 return -1;
472         }
473         // actually write the byte into the FIFO
474         UEDATX = c;
475                 // if this completed a packet, transmit it now!
476         if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
477         transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
478         SREG = intr_state;
479         return 0;
480 }
481
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)
493
494 int8_t usb_serial_write(const uint8_t *buffer, uint16_t size)
495 {
496         uint8_t timeout, intr_state, write_size;
497
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!
503         intr_state = SREG;
504         cli();
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))) {
509                         SREG = intr_state;
510                         return -1;
511                 }
512                 transmit_previous_timeout = 0;
513         }
514         // each iteration of this loop transmits a packet
515         while (size) {
516                 // wait for the FIFO to be ready to accept data
517                 timeout = UDFNUML + TRANSMIT_TIMEOUT;
518                 while (1) {
519                         // are we ready to transmit?
520                         if (UEINTX & (1<<RWAL)) break;
521                         SREG = intr_state;
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;
526                                 return -1;
527                         }
528                         // has the USB gone offline?
529                         if (!usb_configuration) return -1;
530                         // get ready to try checking again
531                         intr_state = SREG;
532                         cli();
533                         UENUM = CDC_TX_ENDPOINT;
534                 }
535
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;
539                 size -= write_size;
540
541                 // write the packet
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++;
576                         #endif
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++;
594                         #endif
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++;
604                         #endif
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++;
612                         default:
613                         case  1: UEDATX = *buffer++;
614                         case  0: break;
615                 }
616                 // if this completed a packet, transmit it now!
617                 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
618                 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
619                 SREG = intr_state;
620         }
621         return 0;
622 }
623
624
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)
630 {
631         uint8_t intr_state;
632
633         intr_state = SREG;
634         cli();
635         if (transmit_flush_timer) {
636                 UENUM = CDC_TX_ENDPOINT;
637                 UEINTX = 0x3A;
638                 transmit_flush_timer = 0;
639         }
640         SREG = intr_state;
641 }
642
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
647 // communication
648 // uint32_t usb_serial_get_baud(void)
649 // {
650 //      return *(uint32_t *)cdc_line_coding;
651 // }
652 uint8_t usb_serial_get_stopbits(void)
653 {
654         return cdc_line_coding[4];
655 }
656 uint8_t usb_serial_get_paritytype(void)
657 {
658         return cdc_line_coding[5];
659 }
660 uint8_t usb_serial_get_numbits(void)
661 {
662         return cdc_line_coding[6];
663 }
664 uint8_t usb_serial_get_control(void)
665 {
666         return cdc_line_rtsdtr;
667 }
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)
675 {
676         uint8_t intr_state;
677
678         intr_state = SREG;
679         cli();
680         if (!usb_configuration) {
681                 // we're not enumerated/configured
682                 SREG = intr_state;
683                 return -1;
684         }
685
686         UENUM = CDC_ACM_ENDPOINT;
687         if (!(UEINTX & (1<<RWAL))) {
688                 // unable to write
689                 // TODO; should this try to abort the previously
690                 // buffered message??
691                 SREG = intr_state;
692                 return -1;
693         }
694         UEDATX = 0xA1;
695         UEDATX = 0x20;
696         UEDATX = 0;
697         UEDATX = 0;
698         UEDATX = 0; // 0 seems to work nicely.  what if this is 1??
699         UEDATX = 0;
700         UEDATX = 1;
701         UEDATX = 0;
702         UEDATX = signals;
703         UEINTX = 0x3A;
704         SREG = intr_state;
705         return 0;
706 }
707
708
709
710 /**************************************************************************
711  *
712  *  Private Functions - not intended for general user consumption....
713  *
714  **************************************************************************/
715
716
717 // USB Device Interrupt - handle all device-level events
718 // the transmit buffer flushing is triggered by the start of frame
719 //
720 ISR(USB_GEN_vect, ISR_BLOCK)
721 {
722         uint8_t intbits, t;
723                 intbits = UDINT;
724                 UDINT = 0;
725         if (intbits & (1<<EORSTI)) {
726                 UENUM = 0;
727                 UECONX = 1;
728                 UECFG0X = EP_TYPE_CONTROL;
729                 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
730                 UEIENX = (1<<RXSTPE);
731                 usb_configuration = 0;
732                 cdc_line_rtsdtr = 0;
733         }
734         if (intbits & (1<<SOFI)) {
735                 if (usb_configuration) {
736                         t = transmit_flush_timer;
737                         if (t) {
738                                 transmit_flush_timer = --t;
739                                 if (!t) {
740                                         UENUM = CDC_TX_ENDPOINT;
741                                         UEINTX = 0x3A;
742                                 }
743                         }
744                 }
745         }
746 }
747
748
749 // Misc functions to wait for ready and send/receive packets
750 static inline void usb_wait_in_ready(void)
751 {
752         while (!(UEINTX & (1<<TXINI))) ;
753 }
754 static inline void usb_send_in(void)
755 {
756         UEINTX = ~(1<<TXINI);
757 }
758 static inline void usb_wait_receive_out(void)
759 {
760         while (!(UEINTX & (1<<RXOUTI))) ;
761 }
762 static inline void usb_ack_out(void)
763 {
764         UEINTX = ~(1<<RXOUTI);
765 }
766
767
768
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.
772 //
773 ISR(USB_COM_vect, ISR_BLOCK)
774 {
775         uint8_t intbits;
776         const uint8_t *list;
777         const uint8_t *cfg;
778         uint8_t i, n, len, en;
779         uint8_t *p;
780         uint8_t bmRequestType;
781         uint8_t bRequest;
782         uint16_t wValue;
783         uint16_t wIndex;
784         uint16_t wLength;
785         uint16_t desc_val;
786         const uint8_t *desc_addr;
787         uint8_t desc_length;
788
789         UENUM = 0;
790         intbits = UEINTX;
791         if (intbits & (1<<RXSTPI)) {
792                 bmRequestType = UEDATX;
793                 bRequest = UEDATX;
794                 wValue = UEDATX;
795                 wValue |= (UEDATX << 8);
796                 wIndex = UEDATX;
797                 wIndex |= (UEDATX << 8);
798                 wLength = UEDATX;
799                 wLength |= (UEDATX << 8);
800                 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
801                 if (bRequest == GET_DESCRIPTOR) {
802                         list = (const uint8_t *)descriptor_list;
803                         for (i=0; ; i++) {
804                                 if (i >= NUM_DESC_LIST) {
805                                         UECONX = (1<<STALLRQ)|(1<<EPEN);  //stall
806                                         return;
807                                 }
808                                 desc_val = pgm_read_word(list);
809                                 if (desc_val != wValue) {
810                                         list += sizeof(struct descriptor_list_struct);
811                                         continue;
812                                 }
813                                 list += 2;
814                                 desc_val = pgm_read_word(list);
815                                 if (desc_val != wIndex) {
816                                         list += sizeof(struct descriptor_list_struct)-2;
817                                         continue;
818                                 }
819                                 list += 2;
820                                 desc_addr = (const uint8_t *)pgm_read_word(list);
821                                 list += 2;
822                                 desc_length = pgm_read_byte(list);
823                                 break;
824                         }
825                         len = (wLength < 256) ? wLength : 255;
826                         if (len > desc_length) len = desc_length;
827                         do {
828                                 // wait for host ready for IN packet
829                                 do {
830                                         i = UEINTX;
831                                 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
832                                 if (i & (1<<RXOUTI))
833                                 {
834                                         return; // abort
835                                 }
836                                 // send IN packet
837                                 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
838                                 for (i = n; i; i--) {
839                                         UEDATX = pgm_read_byte(desc_addr++);
840                                 }
841                                 len -= n;
842                                 usb_send_in();
843                         } while (len || n == ENDPOINT0_SIZE);
844                         return;
845                 }
846                 if (bRequest == SET_ADDRESS) {
847                         usb_send_in();
848                         usb_wait_in_ready();
849                         UDADDR = wValue | (1<<ADDEN);
850                         return;
851                 }
852                 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
853                         usb_configuration = wValue;
854                         cdc_line_rtsdtr = 0;
855                         transmit_flush_timer = 0;
856                         usb_send_in();
857                         cfg = endpoint_config_table;
858                         for (i=1; i<5; i++) {
859                                 UENUM = i;
860                                 en = pgm_read_byte(cfg++);
861                                 UECONX = en;
862                                 if (en) {
863                                         UECFG0X = pgm_read_byte(cfg++);
864                                         UECFG1X = pgm_read_byte(cfg++);
865                                 }
866                         }
867                                 UERST = 0x1E;
868                                 UERST = 0;
869                         return;
870                 }
871                 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
872                         usb_wait_in_ready();
873                         UEDATX = usb_configuration;
874                         usb_send_in();
875                         return;
876                 }
877                 if (bRequest == CDC_GET_LINE_CODING && bmRequestType == 0xA1) {
878                         usb_wait_in_ready();
879                         p = cdc_line_coding;
880                         for (i=0; i<7; i++) {
881                                 UEDATX = *p++;
882                         }
883                         usb_send_in();
884                         return;
885                 }
886                 if (bRequest == CDC_SET_LINE_CODING && bmRequestType == 0x21) {
887                         usb_wait_receive_out();
888                         p = cdc_line_coding;
889                         for (i=0; i<7; i++) {
890                                 *p++ = UEDATX;
891                         }
892                         usb_ack_out();
893                         usb_send_in();
894                         return;
895                 }
896                 if (bRequest == CDC_SET_CONTROL_LINE_STATE && bmRequestType == 0x21) {
897                         cdc_line_rtsdtr = wValue;
898                         usb_wait_in_ready();
899                         usb_send_in();
900                         return;
901                 }
902                 if (bRequest == GET_STATUS) {
903                         usb_wait_in_ready();
904                         i = 0;
905                         #ifdef SUPPORT_ENDPOINT_HALT
906                         if (bmRequestType == 0x82) {
907                                 UENUM = wIndex;
908                                 if (UECONX & (1<<STALLRQ)) i = 1;
909                                 UENUM = 0;
910                         }
911                         #endif
912                         UEDATX = i;
913                         UEDATX = 0;
914                         usb_send_in();
915                         return;
916                 }
917                 #ifdef SUPPORT_ENDPOINT_HALT
918                 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
919                   && bmRequestType == 0x02 && wValue == 0) {
920                         i = wIndex & 0x7F;
921                         if (i >= 1 && i <= MAX_ENDPOINT) {
922                                 usb_send_in();
923                                 UENUM = i;
924                                 if (bRequest == SET_FEATURE) {
925                                         UECONX = (1<<STALLRQ)|(1<<EPEN);
926                                 } else {
927                                         UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
928                                         UERST = (1 << i);
929                                         UERST = 0;
930                                 }
931                                 return;
932                         }
933                 }
934                 #endif
935         }
936         UECONX = (1<<STALLRQ) | (1<<EPEN);      // stall
937 }
938
939
940 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
941
942 #if 0
943 #include <stdio.h>
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);
946 stdout=&dwout;
947 #endif
948
949 // Very simple character echo test
950 int main(void)
951 {
952         CPU_PRESCALE(0);
953         usb_init();
954         while(usb_configured()==0);
955         while (1) {
956                 int n = usb_serial_getchar();
957                 if (n >= 0) usb_serial_putchar(n);
958         }
959 }