view usb_serial.c @ 7:ef38703286dd

add ++echo command, bigger queues, infinite timeout on usb
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Fri, 11 Apr 2014 17:21:26 +0100
parents 6dfcd8e5b8df
children 1969a435a7a2
line wrap: on
line source
1 /* USB Serial Example for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/usb_serial.html
3 * Copyright (c) 2008,2010,2011 PJRC.COM, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
24 // Version 1.0: Initial Release
25 // Version 1.1: support Teensy++
26 // Version 1.2: fixed usb_serial_available
27 // Version 1.3: added transmit bandwidth test
28 // Version 1.4: added usb_serial_write
29 // Version 1.5: add support for Teensy 2.0
30 // Version 1.6: fix zero length packet bug
31 // Version 1.7: fix usb_serial_set_control
33 #define USB_SERIAL_PRIVATE_INCLUDE
34 #include "usb_serial.h"
37 /**************************************************************************
38 *
39 * Configurable Options
40 *
41 **************************************************************************/
43 // You can change these to give your code its own name. On Windows,
44 // these are only used before an INF file (driver install) is loaded.
45 #define STR_MANUFACTURER L"Frank Kingswood"
46 #define STR_PRODUCT L"USB-GPIB-32U4"
48 // All USB serial devices are supposed to have a serial number
49 // (according to Microsoft). On windows, a new COM port is created
50 // for every unique serial/vendor/product number combination. If
51 // you program 2 identical boards with 2 different serial numbers
52 // and they are assigned COM7 and COM8, each will always get the
53 // same COM port number because Windows remembers serial numbers.
54 //
55 // On Mac OS-X, a device file is created automatically which
56 // incorperates the serial number, eg, /dev/cu-usbmodem12341
57 //
58 // Linux by default ignores the serial number, and creates device
59 // files named /dev/ttyACM0, /dev/ttyACM1... in the order connected.
60 // Udev rules (in /etc/udev/rules.d) can define persistent device
61 // names linked to this serial number, as well as permissions, owner
62 // and group settings.
63 #define STR_SERIAL_NUMBER L"324"
65 // Mac OS-X and Linux automatically load the correct drivers. On
66 // Windows, even though the driver is supplied by Microsoft, an
67 // INF file is needed to load the driver. These numbers need to
68 // match the INF file.
69 #define VENDOR_ID 0x16C0
70 #define PRODUCT_ID 0x047A
72 // When you write data, it goes into a USB endpoint buffer, which
73 // is transmitted to the PC when it becomes full, or after a timeout
74 // with no more writes. Even if you write in exactly packet-size
75 // increments, this timeout is used to send a "zero length packet"
76 // that tells the PC no more data is expected and it should pass
77 // any buffered data to the application that may be waiting. If
78 // you want data sent immediately, call usb_serial_flush_output().
79 #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
81 // If the PC is connected but not "listening", this is the length
82 // of time before usb_serial_getchar() returns with an error. This
83 // is roughly equivilant to a real UART simply transmitting the
84 // bits on a wire where nobody is listening, except you get an error
85 // code which you can ignore for serial-like discard of data, or
86 // use to know your data wasn't sent.
87 #define TRANSMIT_TIMEOUT 100 /* in milliseconds */
89 // USB devices are supposed to implment a halt feature, which is
90 // rarely (if ever) used. If you comment this line out, the halt
91 // code will be removed, saving 116 bytes of space (gcc 4.3.0).
92 // This is not strictly USB compliant, but works with all major
93 // operating systems.
94 #define SUPPORT_ENDPOINT_HALT
98 /**************************************************************************
99 *
100 * Endpoint Buffer Configuration
101 *
102 **************************************************************************/
104 // These buffer sizes are best for most applications, but perhaps if you
105 // want more buffering on some endpoint at the expense of others, this
106 // is where you can make such changes. The AT90USB162 has only 176 bytes
107 // of DPRAM (USB buffers) and only endpoints 3 & 4 can double buffer.
109 #define ENDPOINT0_SIZE 16
110 #define CDC_ACM_ENDPOINT 2
111 #define CDC_RX_ENDPOINT 3
112 #define CDC_TX_ENDPOINT 4
113 #if defined(__AVR_AT90USB162__)
114 #define CDC_ACM_SIZE 16
115 #define CDC_ACM_BUFFER EP_SINGLE_BUFFER
116 #define CDC_RX_SIZE 32
117 #define CDC_RX_BUFFER EP_DOUBLE_BUFFER
118 #define CDC_TX_SIZE 32
119 #define CDC_TX_BUFFER EP_DOUBLE_BUFFER
120 #else
121 #define CDC_ACM_SIZE 16
122 #define CDC_ACM_BUFFER EP_SINGLE_BUFFER
123 #define CDC_RX_SIZE 64
124 #define CDC_RX_BUFFER EP_DOUBLE_BUFFER
125 #define CDC_TX_SIZE 64
126 #define CDC_TX_BUFFER EP_DOUBLE_BUFFER
127 #endif
129 static const uint8_t PROGMEM endpoint_config_table[] = {
130 0,
131 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(CDC_ACM_SIZE) | CDC_ACM_BUFFER,
132 1, EP_TYPE_BULK_OUT, EP_SIZE(CDC_RX_SIZE) | CDC_RX_BUFFER,
133 1, EP_TYPE_BULK_IN, EP_SIZE(CDC_TX_SIZE) | CDC_TX_BUFFER
134 };
137 /**************************************************************************
138 *
139 * Descriptor Data
140 *
141 **************************************************************************/
143 // Descriptors are the data that your computer reads when it auto-detects
144 // this USB device (called "enumeration" in USB lingo). The most commonly
145 // changed items are editable at the top of this file. Changing things
146 // in here should only be done by those who've read chapter 9 of the USB
147 // spec and relevant portions of any USB class specifications!
149 static const uint8_t PROGMEM device_descriptor[] = {
150 18, // bLength
151 1, // bDescriptorType
152 0x00, 0x02, // bcdUSB
153 2, // bDeviceClass
154 0, // bDeviceSubClass
155 0, // bDeviceProtocol
156 ENDPOINT0_SIZE, // bMaxPacketSize0
157 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
158 LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
159 0x00, 0x01, // bcdDevice
160 1, // iManufacturer
161 2, // iProduct
162 3, // iSerialNumber
163 1 // bNumConfigurations
164 };
166 #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
167 static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
168 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
169 9, // bLength;
170 2, // bDescriptorType;
171 LSB(CONFIG1_DESC_SIZE), // wTotalLength
172 MSB(CONFIG1_DESC_SIZE),
173 2, // bNumInterfaces
174 1, // bConfigurationValue
175 0, // iConfiguration
176 0xC0, // bmAttributes
177 50, // bMaxPower
178 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
179 9, // bLength
180 4, // bDescriptorType
181 0, // bInterfaceNumber
182 0, // bAlternateSetting
183 1, // bNumEndpoints
184 0x02, // bInterfaceClass
185 0x02, // bInterfaceSubClass
186 0x01, // bInterfaceProtocol
187 0, // iInterface
188 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
189 5, // bFunctionLength
190 0x24, // bDescriptorType
191 0x00, // bDescriptorSubtype
192 0x10, 0x01, // bcdCDC
193 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
194 5, // bFunctionLength
195 0x24, // bDescriptorType
196 0x01, // bDescriptorSubtype
197 0x01, // bmCapabilities
198 1, // bDataInterface
199 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
200 4, // bFunctionLength
201 0x24, // bDescriptorType
202 0x02, // bDescriptorSubtype
203 0x06, // bmCapabilities
204 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
205 5, // bFunctionLength
206 0x24, // bDescriptorType
207 0x06, // bDescriptorSubtype
208 0, // bMasterInterface
209 1, // bSlaveInterface0
210 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
211 7, // bLength
212 5, // bDescriptorType
213 CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
214 0x03, // bmAttributes (0x03=intr)
215 CDC_ACM_SIZE, 0, // wMaxPacketSize
216 64, // bInterval
217 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
218 9, // bLength
219 4, // bDescriptorType
220 1, // bInterfaceNumber
221 0, // bAlternateSetting
222 2, // bNumEndpoints
223 0x0A, // bInterfaceClass
224 0x00, // bInterfaceSubClass
225 0x00, // bInterfaceProtocol
226 0, // iInterface
227 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
228 7, // bLength
229 5, // bDescriptorType
230 CDC_RX_ENDPOINT, // bEndpointAddress
231 0x02, // bmAttributes (0x02=bulk)
232 CDC_RX_SIZE, 0, // wMaxPacketSize
233 0, // bInterval
234 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
235 7, // bLength
236 5, // bDescriptorType
237 CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
238 0x02, // bmAttributes (0x02=bulk)
239 CDC_TX_SIZE, 0, // wMaxPacketSize
240 0 // bInterval
241 };
243 // If you're desperate for a little extra code memory, these strings
244 // can be completely removed if iManufacturer, iProduct, iSerialNumber
245 // in the device desciptor are changed to zeros.
246 struct usb_string_descriptor_struct {
247 uint8_t bLength;
248 uint8_t bDescriptorType;
249 int16_t wString[];
250 };
251 static const struct usb_string_descriptor_struct PROGMEM string0 = {
252 4,
253 3,
254 {0x0409}
255 };
256 static const struct usb_string_descriptor_struct PROGMEM string1 = {
257 sizeof(STR_MANUFACTURER),
258 3,
259 STR_MANUFACTURER
260 };
261 static const struct usb_string_descriptor_struct PROGMEM string2 = {
262 sizeof(STR_PRODUCT),
263 3,
264 STR_PRODUCT
265 };
266 static const struct usb_string_descriptor_struct PROGMEM string3 = {
267 sizeof(STR_SERIAL_NUMBER),
268 3,
269 STR_SERIAL_NUMBER
270 };
272 // This table defines which descriptor data is sent for each specific
273 // request from the host (in wValue and wIndex).
274 static const struct descriptor_list_struct {
275 uint16_t wValue;
276 uint16_t wIndex;
277 const uint8_t *addr;
278 uint8_t length;
279 } PROGMEM descriptor_list[] = {
280 {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
281 {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
282 {0x0300, 0x0000, (const uint8_t *)&string0, 4},
283 {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
284 {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
285 {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL_NUMBER)}
286 };
287 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
290 /**************************************************************************
291 *
292 * Variables - these are the only non-stack RAM usage
293 *
294 **************************************************************************/
296 // zero when we are not configured, non-zero when enumerated
297 static volatile uint8_t usb_configuration=0;
299 // the time remaining before we transmit any partially full
300 // packet, or send a zero length packet.
301 static volatile uint8_t transmit_flush_timer=0;
302 static uint8_t transmit_previous_timeout=0;
304 // serial port settings (baud rate, control signals, etc) set
305 // by the PC. These are ignored, but kept in RAM.
306 static uint8_t cdc_line_coding[7]={0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
307 static uint8_t cdc_line_rtsdtr=0;
310 /**************************************************************************
311 *
312 * Public Functions - these are the API intended for the user
313 *
314 **************************************************************************/
316 // initialize USB serial
317 void usb_init(void)
318 {
319 HW_CONFIG();
320 USB_FREEZE(); // enable USB
321 PLL_CONFIG(); // config PLL, 16 MHz xtal
322 while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
323 USB_CONFIG(); // start USB clock
324 UDCON = 0; // enable attach resistor
325 usb_configuration = 0;
326 cdc_line_rtsdtr = 0;
327 UDIEN = (1<<EORSTE)|(1<<SOFE);
328 sei();
329 }
331 // return 0 if the USB is not configured, or the configuration
332 // number selected by the HOST
333 uint8_t usb_configured(void)
334 {
335 return usb_configuration;
336 }
338 // get the next character, or -1 if nothing received
339 int16_t usb_serial_getchar(void)
340 {
341 uint8_t c, intr_state;
343 // interrupts are disabled so these functions can be
344 // used from the main program or interrupt context,
345 // even both in the same program!
346 intr_state = SREG;
347 cli();
348 if (!usb_configuration) {
349 SREG = intr_state;
350 return -1;
351 }
352 UENUM = CDC_RX_ENDPOINT;
353 retry:
354 c = UEINTX;
355 if (!(c & (1<<RWAL))) {
356 // no data in buffer
357 if (c & (1<<RXOUTI)) {
358 UEINTX = 0x6B;
359 goto retry;
360 }
361 SREG = intr_state;
362 return -1;
363 }
364 // take one byte out of the buffer
365 c = UEDATX;
366 // if buffer completely used, release it
367 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
368 SREG = intr_state;
369 return c;
370 }
372 // number of bytes available in the receive buffer
373 uint8_t usb_serial_available(void)
374 {
375 uint8_t n=0, i, intr_state;
377 intr_state = SREG;
378 cli();
379 if (usb_configuration) {
380 UENUM = CDC_RX_ENDPOINT;
381 n = UEBCLX;
382 if (!n) {
383 i = UEINTX;
384 if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
385 }
386 }
387 SREG = intr_state;
388 return n;
389 }
391 // discard any buffered input
392 void usb_serial_flush_input(void)
393 {
394 uint8_t intr_state;
396 if (usb_configuration) {
397 intr_state = SREG;
398 cli();
399 UENUM = CDC_RX_ENDPOINT;
400 while ((UEINTX & (1<<RWAL))) {
401 UEINTX = 0x6B;
402 }
403 SREG = intr_state;
404 }
405 }
407 // transmit a character. 0 returned on success, -1 on timeout or -9 on error
408 int8_t usb_serial_putchar(uint8_t c)
409 {
410 uint8_t timeout, intr_state;
412 // if we're not online (enumerated and configured), error
413 if (!usb_configuration)
414 return -9;
416 // interrupts are disabled so these functions can be
417 // used from the main program or interrupt context,
418 // even both in the same program!
419 intr_state = SREG;
420 cli();
421 UENUM = CDC_TX_ENDPOINT;
422 // if we gave up due to timeout before, don't wait again
423 if (transmit_previous_timeout) {
424 if (!(UEINTX & (1<<RWAL))) {
425 SREG = intr_state;
426 return -1;
427 }
428 transmit_previous_timeout = 0;
429 }
430 // wait for the FIFO to be ready to accept data
431 timeout = UDFNUML + TRANSMIT_TIMEOUT;
432 while (1) {
433 // are we ready to transmit?
434 if (UEINTX & (1<<RWAL)) break;
435 SREG = intr_state;
436 // have we waited too long? This happens if the user
437 // is not running an application that is listening
438 if (UDFNUML == timeout) {
439 transmit_previous_timeout = 1;
440 return -1;
441 }
442 // has the USB gone offline?
443 if (!usb_configuration)
444 return -9;
445 // get ready to try checking again
446 intr_state = SREG;
447 cli();
448 UENUM = CDC_TX_ENDPOINT;
449 }
450 // actually write the byte into the FIFO
451 UEDATX = c;
452 // if this completed a packet, transmit it now!
453 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
454 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
455 SREG = intr_state;
456 return 0;
457 }
460 // transmit a character, but do not wait if the buffer is full,
461 // 0 returned on success, -1 on timeout, -2 on buffer full or -9 for error
462 int8_t usb_serial_putchar_nowait(uint8_t c)
463 {
464 uint8_t intr_state;
466 if (!usb_configuration) return -1;
467 intr_state = SREG;
468 cli();
469 UENUM = CDC_TX_ENDPOINT;
470 if (!(UEINTX & (1<<RWAL))) {
471 // buffer is full
472 SREG = intr_state;
473 return -2;
474 }
475 // actually write the byte into the FIFO
476 UEDATX = c;
477 // if this completed a packet, transmit it now!
478 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
479 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
480 SREG = intr_state;
481 return 0;
482 }
484 // Send a string to the USB serial port.
485 int8_t usb_serial_puts(const char *string)
486 { char c;
488 while((c=*string++)!=0)
489 {
490 int8_t r=usb_serial_putchar(c);
491 if(r)
492 return r;
493 }
494 return 0;
495 }
497 // Send a string to the USB serial port.
498 // The string must be in program memory memory.
499 int8_t usb_serial_puts_P(const char *string)
500 { char c;
502 while((c=pgm_read_byte(string++))!=0)
503 {
504 int8_t r=usb_serial_putchar(c);
505 if(r)
506 return r;
507 }
508 return 0;
509 }
511 // transmit a buffer.
512 // 0 returned on success, -1 on error
513 // This function is optimized for speed! Each call takes approx 6.1 us overhead
514 // plus 0.25 us per byte. 12 Mbit/sec USB has 8.67 us per-packet overhead and
515 // takes 0.67 us per byte. If called with 64 byte packet-size blocks, this function
516 // can transmit at full USB speed using 43% CPU time. The maximum theoretical speed
517 // is 19 packets per USB frame, or 1216 kbytes/sec. However, bulk endpoints have the
518 // lowest priority, so any other USB devices will likely reduce the speed. Speed
519 // can also be limited by how quickly the PC-based software reads data, as the host
520 // controller in the PC will not allocate bandwitdh without a pending read request.
521 // (thanks to Victor Suarez for testing and feedback and initial code)
523 int8_t usb_serial_write(const uint8_t *buffer, uint16_t size)
524 {
525 uint8_t timeout, intr_state, write_size;
527 // if we're not online (enumerated and configured), error
528 if (!usb_configuration) return -1;
529 // interrupts are disabled so these functions can be
530 // used from the main program or interrupt context,
531 // even both in the same program!
532 intr_state = SREG;
533 cli();
534 UENUM = CDC_TX_ENDPOINT;
535 // if we gave up due to timeout before, don't wait again
536 if (transmit_previous_timeout) {
537 if (!(UEINTX & (1<<RWAL))) {
538 SREG = intr_state;
539 return -1;
540 }
541 transmit_previous_timeout = 0;
542 }
543 // each iteration of this loop transmits a packet
544 while (size) {
545 // wait for the FIFO to be ready to accept data
546 timeout = UDFNUML + TRANSMIT_TIMEOUT;
547 while (1) {
548 // are we ready to transmit?
549 if (UEINTX & (1<<RWAL)) break;
550 SREG = intr_state;
551 // have we waited too long? This happens if the user
552 // is not running an application that is listening
553 if (UDFNUML == timeout) {
554 transmit_previous_timeout = 1;
555 return -1;
556 }
557 // has the USB gone offline?
558 if (!usb_configuration) return -1;
559 // get ready to try checking again
560 intr_state = SREG;
561 cli();
562 UENUM = CDC_TX_ENDPOINT;
563 }
565 // compute how many bytes will fit into the next packet
566 write_size = CDC_TX_SIZE - UEBCLX;
567 if (write_size > size) write_size = size;
568 size -= write_size;
570 // write the packet
571 switch(write_size%8) {
572 case 0: do { write_size-=8;
573 UEDATX = buffer[0];
574 case 7: UEDATX = buffer[1];
575 case 6: UEDATX = buffer[2];
576 case 5: UEDATX = buffer[3];
577 case 4: UEDATX = buffer[4];
578 case 3: UEDATX = buffer[5];
579 case 2: UEDATX = buffer[6];
580 case 1: UEDATX = buffer[7];
581 buffer+=8;
582 } while(write_size>=8);
583 }
585 // if this completed a packet, transmit it now!
586 if (!(UEINTX & (1<<RWAL)))
587 UEINTX = 0x3A;
588 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
589 SREG = intr_state;
590 }
591 return 0;
592 }
595 // immediately transmit any buffered output.
596 // This doesn't actually transmit the data - that is impossible!
597 // USB devices only transmit when the host allows, so the best
598 // we can do is release the FIFO buffer for when the host wants it
599 void usb_serial_flush_output(void)
600 {
601 uint8_t intr_state;
603 intr_state = SREG;
604 cli();
605 if (transmit_flush_timer) {
606 UENUM = CDC_TX_ENDPOINT;
607 UEINTX = 0x3A;
608 transmit_flush_timer = 0;
609 }
610 SREG = intr_state;
611 }
613 // functions to read the various async serial settings. These
614 // aren't actually used by USB at all (communication is always
615 // at full USB speed), but they are set by the host so we can
616 // set them properly if we're converting the USB to a real serial
617 // communication
618 uint32_t usb_serial_get_baud(void)
619 {
620 const uint32_t *p=(const uint32_t *)cdc_line_coding;
621 return *p;
622 }
623 uint8_t usb_serial_get_stopbits(void)
624 {
625 return cdc_line_coding[4];
626 }
627 uint8_t usb_serial_get_paritytype(void)
628 {
629 return cdc_line_coding[5];
630 }
631 uint8_t usb_serial_get_numbits(void)
632 {
633 return cdc_line_coding[6];
634 }
635 uint8_t usb_serial_get_control(void)
636 {
637 return cdc_line_rtsdtr;
638 }
639 // write the control signals, DCD, DSR, RI, etc
640 // There is no CTS signal. If software on the host has transmitted
641 // data to you but you haven't been calling the getchar function,
642 // it remains buffered (either here or on the host) and can not be
643 // lost because you weren't listening at the right time, like it
644 // would in real serial communication.
645 int8_t usb_serial_set_control(uint8_t signals)
646 {
647 uint8_t intr_state;
649 intr_state = SREG;
650 cli();
651 if (!usb_configuration) {
652 // we're not enumerated/configured
653 SREG = intr_state;
654 return -1;
655 }
657 UENUM = CDC_ACM_ENDPOINT;
658 if (!(UEINTX & (1<<RWAL))) {
659 // unable to write
660 // TODO; should this try to abort the previously
661 // buffered message??
662 SREG = intr_state;
663 return -1;
664 }
665 UEDATX = 0xA1;
666 UEDATX = 0x20;
667 UEDATX = 0;
668 UEDATX = 0;
669 UEDATX = 0; // 0 seems to work nicely. what if this is 1??
670 UEDATX = 0;
671 UEDATX = 1;
672 UEDATX = 0;
673 UEDATX = signals;
674 UEINTX = 0x3A;
675 SREG = intr_state;
676 return 0;
677 }
681 /**************************************************************************
682 *
683 * Private Functions - not intended for general user consumption....
684 *
685 **************************************************************************/
688 // USB Device Interrupt - handle all device-level events
689 // the transmit buffer flushing is triggered by the start of frame
690 //
691 ISR(USB_GEN_vect)
692 {
693 uint8_t intbits, t;
695 intbits = UDINT;
696 UDINT = 0;
697 if (intbits & (1<<EORSTI)) {
698 UENUM = 0;
699 UECONX = 1;
700 UECFG0X = EP_TYPE_CONTROL;
701 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
702 UEIENX = (1<<RXSTPE);
703 usb_configuration = 0;
704 cdc_line_rtsdtr = 0;
705 }
706 if (intbits & (1<<SOFI)) {
707 if (usb_configuration) {
708 t = transmit_flush_timer;
709 if (t) {
710 transmit_flush_timer = --t;
711 if (!t) {
712 UENUM = CDC_TX_ENDPOINT;
713 UEINTX = 0x3A;
714 }
715 }
716 }
717 }
718 }
721 // Misc functions to wait for ready and send/receive packets
722 static inline void usb_wait_in_ready(void)
723 {
724 while (!(UEINTX & (1<<TXINI))) ;
725 }
726 static inline void usb_send_in(void)
727 {
728 UEINTX = ~(1<<TXINI);
729 }
730 static inline void usb_wait_receive_out(void)
731 {
732 while (!(UEINTX & (1<<RXOUTI))) ;
733 }
734 static inline void usb_ack_out(void)
735 {
736 UEINTX = ~(1<<RXOUTI);
737 }
741 // USB Endpoint Interrupt - endpoint 0 is handled here. The
742 // other endpoints are manipulated by the user-callable
743 // functions, and the start-of-frame interrupt.
744 //
745 ISR(USB_COM_vect)
746 {
747 uint8_t intbits;
748 const uint8_t *list;
749 const uint8_t *cfg;
750 uint8_t i, n, len, en;
751 uint8_t *p;
752 uint8_t bmRequestType;
753 uint8_t bRequest;
754 uint16_t wValue;
755 uint16_t wIndex;
756 uint16_t wLength;
757 uint16_t desc_val;
758 const uint8_t *desc_addr;
759 uint8_t desc_length;
761 UENUM = 0;
762 intbits = UEINTX;
763 if (intbits & (1<<RXSTPI)) {
764 bmRequestType = UEDATX;
765 bRequest = UEDATX;
766 wValue = UEDATX;
767 wValue |= (UEDATX << 8);
768 wIndex = UEDATX;
769 wIndex |= (UEDATX << 8);
770 wLength = UEDATX;
771 wLength |= (UEDATX << 8);
772 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
773 if (bRequest == GET_DESCRIPTOR) {
774 list = (const uint8_t *)descriptor_list;
775 for (i=0; ; i++) {
776 if (i >= NUM_DESC_LIST) {
777 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
778 return;
779 }
780 desc_val = pgm_read_word(list);
781 if (desc_val != wValue) {
782 list += sizeof(struct descriptor_list_struct);
783 continue;
784 }
785 list += 2;
786 desc_val = pgm_read_word(list);
787 if (desc_val != wIndex) {
788 list += sizeof(struct descriptor_list_struct)-2;
789 continue;
790 }
791 list += 2;
792 desc_addr = (const uint8_t *)pgm_read_word(list);
793 list += 2;
794 desc_length = pgm_read_byte(list);
795 break;
796 }
797 len = (wLength < 256) ? wLength : 255;
798 if (len > desc_length) len = desc_length;
799 do {
800 // wait for host ready for IN packet
801 do {
802 i = UEINTX;
803 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
804 if (i & (1<<RXOUTI)) return; // abort
805 // send IN packet
806 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
807 for (i = n; i; i--) {
808 UEDATX = pgm_read_byte(desc_addr++);
809 }
810 len -= n;
811 usb_send_in();
812 } while (len || n == ENDPOINT0_SIZE);
813 return;
814 }
815 if (bRequest == SET_ADDRESS) {
816 usb_send_in();
817 usb_wait_in_ready();
818 UDADDR = wValue | (1<<ADDEN);
819 return;
820 }
821 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
822 usb_configuration = wValue;
823 cdc_line_rtsdtr = 0;
824 transmit_flush_timer = 0;
825 usb_send_in();
826 cfg = endpoint_config_table;
827 for (i=1; i<5; i++) {
828 UENUM = i;
829 en = pgm_read_byte(cfg++);
830 UECONX = en;
831 if (en) {
832 UECFG0X = pgm_read_byte(cfg++);
833 UECFG1X = pgm_read_byte(cfg++);
834 }
835 }
836 UERST = 0x1E;
837 UERST = 0;
838 return;
839 }
840 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
841 usb_wait_in_ready();
842 UEDATX = usb_configuration;
843 usb_send_in();
844 return;
845 }
846 if (bRequest == CDC_GET_LINE_CODING && bmRequestType == 0xA1) {
847 usb_wait_in_ready();
848 p = cdc_line_coding;
849 for (i=0; i<7; i++) {
850 UEDATX = *p++;
851 }
852 usb_send_in();
853 return;
854 }
855 if (bRequest == CDC_SET_LINE_CODING && bmRequestType == 0x21) {
856 usb_wait_receive_out();
857 p = cdc_line_coding;
858 for (i=0; i<7; i++) {
859 *p++ = UEDATX;
860 }
861 usb_ack_out();
862 usb_send_in();
863 return;
864 }
865 if (bRequest == CDC_SET_CONTROL_LINE_STATE && bmRequestType == 0x21) {
866 cdc_line_rtsdtr = wValue;
867 usb_wait_in_ready();
868 usb_send_in();
869 return;
870 }
871 if (bRequest == GET_STATUS) {
872 usb_wait_in_ready();
873 i = 0;
874 #ifdef SUPPORT_ENDPOINT_HALT
875 if (bmRequestType == 0x82) {
876 UENUM = wIndex;
877 if (UECONX & (1<<STALLRQ)) i = 1;
878 UENUM = 0;
879 }
880 #endif
881 UEDATX = i;
882 UEDATX = 0;
883 usb_send_in();
884 return;
885 }
886 #ifdef SUPPORT_ENDPOINT_HALT
887 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) && bmRequestType == 0x02 && wValue == 0) {
888 i = wIndex & 0x7F;
889 if (i >= 1 && i <= MAX_ENDPOINT) {
890 usb_send_in();
891 UENUM = i;
892 if (bRequest == SET_FEATURE) {
893 UECONX = (1<<STALLRQ)|(1<<EPEN);
894 } else {
895 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
896 UERST = (1 << i);
897 UERST = 0;
898 }
899 return;
900 }
901 }
902 #endif
903 }
904 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
905 }
907 /* ----- EOF usb_serial.c ----- */