view usb_serial.c @ 0:6dfcd8e5b8df

first release
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Tue, 25 Mar 2014 20:31:00 +0000
parents
children ef38703286dd
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 25 /* 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 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) return -1;
414 // interrupts are disabled so these functions can be
415 // used from the main program or interrupt context,
416 // even both in the same program!
417 intr_state = SREG;
418 cli();
419 UENUM = CDC_TX_ENDPOINT;
420 // if we gave up due to timeout before, don't wait again
421 if (transmit_previous_timeout) {
422 if (!(UEINTX & (1<<RWAL))) {
423 SREG = intr_state;
424 return -1;
425 }
426 transmit_previous_timeout = 0;
427 }
428 // wait for the FIFO to be ready to accept data
429 timeout = UDFNUML + TRANSMIT_TIMEOUT;
430 while (1) {
431 // are we ready to transmit?
432 if (UEINTX & (1<<RWAL)) break;
433 SREG = intr_state;
434 // have we waited too long? This happens if the user
435 // is not running an application that is listening
436 if (UDFNUML == timeout) {
437 transmit_previous_timeout = 1;
438 return -1;
439 }
440 // has the USB gone offline?
441 if (!usb_configuration) return -1;
442 // get ready to try checking again
443 intr_state = SREG;
444 cli();
445 UENUM = CDC_TX_ENDPOINT;
446 }
447 // actually write the byte into the FIFO
448 UEDATX = c;
449 // if this completed a packet, transmit it now!
450 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
451 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
452 SREG = intr_state;
453 return 0;
454 }
457 // transmit a character, but do not wait if the buffer is full,
458 // 0 returned on success, -1 on buffer full or error
459 int8_t usb_serial_putchar_nowait(uint8_t c)
460 {
461 uint8_t intr_state;
463 if (!usb_configuration) return -1;
464 intr_state = SREG;
465 cli();
466 UENUM = CDC_TX_ENDPOINT;
467 if (!(UEINTX & (1<<RWAL))) {
468 // buffer is full
469 SREG = intr_state;
470 return -1;
471 }
472 // actually write the byte into the FIFO
473 UEDATX = c;
474 // if this completed a packet, transmit it now!
475 if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
476 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
477 SREG = intr_state;
478 return 0;
479 }
481 // Send a string to the USB serial port.
482 int8_t usb_serial_puts(const char *string)
483 { char c;
485 while((c=*string++)!=0)
486 {
487 int8_t r=usb_serial_putchar(c);
488 if(r)
489 return r;
490 }
491 return 0;
492 }
494 // Send a string to the USB serial port.
495 // The string must be in program memory memory.
496 int8_t usb_serial_puts_P(const char *string)
497 { char c;
499 while((c=pgm_read_byte(string++))!=0)
500 {
501 int8_t r=usb_serial_putchar(c);
502 if(r)
503 return r;
504 }
505 return 0;
506 }
508 // transmit a buffer.
509 // 0 returned on success, -1 on error
510 // This function is optimized for speed! Each call takes approx 6.1 us overhead
511 // plus 0.25 us per byte. 12 Mbit/sec USB has 8.67 us per-packet overhead and
512 // takes 0.67 us per byte. If called with 64 byte packet-size blocks, this function
513 // can transmit at full USB speed using 43% CPU time. The maximum theoretical speed
514 // is 19 packets per USB frame, or 1216 kbytes/sec. However, bulk endpoints have the
515 // lowest priority, so any other USB devices will likely reduce the speed. Speed
516 // can also be limited by how quickly the PC-based software reads data, as the host
517 // controller in the PC will not allocate bandwitdh without a pending read request.
518 // (thanks to Victor Suarez for testing and feedback and initial code)
520 int8_t usb_serial_write(const uint8_t *buffer, uint16_t size)
521 {
522 uint8_t timeout, intr_state, write_size;
524 // if we're not online (enumerated and configured), error
525 if (!usb_configuration) return -1;
526 // interrupts are disabled so these functions can be
527 // used from the main program or interrupt context,
528 // even both in the same program!
529 intr_state = SREG;
530 cli();
531 UENUM = CDC_TX_ENDPOINT;
532 // if we gave up due to timeout before, don't wait again
533 if (transmit_previous_timeout) {
534 if (!(UEINTX & (1<<RWAL))) {
535 SREG = intr_state;
536 return -1;
537 }
538 transmit_previous_timeout = 0;
539 }
540 // each iteration of this loop transmits a packet
541 while (size) {
542 // wait for the FIFO to be ready to accept data
543 timeout = UDFNUML + TRANSMIT_TIMEOUT;
544 while (1) {
545 // are we ready to transmit?
546 if (UEINTX & (1<<RWAL)) break;
547 SREG = intr_state;
548 // have we waited too long? This happens if the user
549 // is not running an application that is listening
550 if (UDFNUML == timeout) {
551 transmit_previous_timeout = 1;
552 return -1;
553 }
554 // has the USB gone offline?
555 if (!usb_configuration) return -1;
556 // get ready to try checking again
557 intr_state = SREG;
558 cli();
559 UENUM = CDC_TX_ENDPOINT;
560 }
562 // compute how many bytes will fit into the next packet
563 write_size = CDC_TX_SIZE - UEBCLX;
564 if (write_size > size) write_size = size;
565 size -= write_size;
567 // write the packet
568 switch(write_size%8) {
569 case 0: do { write_size-=8;
570 UEDATX = buffer[0];
571 case 7: UEDATX = buffer[1];
572 case 6: UEDATX = buffer[2];
573 case 5: UEDATX = buffer[3];
574 case 4: UEDATX = buffer[4];
575 case 3: UEDATX = buffer[5];
576 case 2: UEDATX = buffer[6];
577 case 1: UEDATX = buffer[7];
578 buffer+=8;
579 } while(write_size>=8);
580 }
582 // if this completed a packet, transmit it now!
583 if (!(UEINTX & (1<<RWAL)))
584 UEINTX = 0x3A;
585 transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
586 SREG = intr_state;
587 }
588 return 0;
589 }
592 // immediately transmit any buffered output.
593 // This doesn't actually transmit the data - that is impossible!
594 // USB devices only transmit when the host allows, so the best
595 // we can do is release the FIFO buffer for when the host wants it
596 void usb_serial_flush_output(void)
597 {
598 uint8_t intr_state;
600 intr_state = SREG;
601 cli();
602 if (transmit_flush_timer) {
603 UENUM = CDC_TX_ENDPOINT;
604 UEINTX = 0x3A;
605 transmit_flush_timer = 0;
606 }
607 SREG = intr_state;
608 }
610 // functions to read the various async serial settings. These
611 // aren't actually used by USB at all (communication is always
612 // at full USB speed), but they are set by the host so we can
613 // set them properly if we're converting the USB to a real serial
614 // communication
615 uint32_t usb_serial_get_baud(void)
616 {
617 const uint32_t *p=(const uint32_t *)cdc_line_coding;
618 return *p;
619 }
620 uint8_t usb_serial_get_stopbits(void)
621 {
622 return cdc_line_coding[4];
623 }
624 uint8_t usb_serial_get_paritytype(void)
625 {
626 return cdc_line_coding[5];
627 }
628 uint8_t usb_serial_get_numbits(void)
629 {
630 return cdc_line_coding[6];
631 }
632 uint8_t usb_serial_get_control(void)
633 {
634 return cdc_line_rtsdtr;
635 }
636 // write the control signals, DCD, DSR, RI, etc
637 // There is no CTS signal. If software on the host has transmitted
638 // data to you but you haven't been calling the getchar function,
639 // it remains buffered (either here or on the host) and can not be
640 // lost because you weren't listening at the right time, like it
641 // would in real serial communication.
642 int8_t usb_serial_set_control(uint8_t signals)
643 {
644 uint8_t intr_state;
646 intr_state = SREG;
647 cli();
648 if (!usb_configuration) {
649 // we're not enumerated/configured
650 SREG = intr_state;
651 return -1;
652 }
654 UENUM = CDC_ACM_ENDPOINT;
655 if (!(UEINTX & (1<<RWAL))) {
656 // unable to write
657 // TODO; should this try to abort the previously
658 // buffered message??
659 SREG = intr_state;
660 return -1;
661 }
662 UEDATX = 0xA1;
663 UEDATX = 0x20;
664 UEDATX = 0;
665 UEDATX = 0;
666 UEDATX = 0; // 0 seems to work nicely. what if this is 1??
667 UEDATX = 0;
668 UEDATX = 1;
669 UEDATX = 0;
670 UEDATX = signals;
671 UEINTX = 0x3A;
672 SREG = intr_state;
673 return 0;
674 }
678 /**************************************************************************
679 *
680 * Private Functions - not intended for general user consumption....
681 *
682 **************************************************************************/
685 // USB Device Interrupt - handle all device-level events
686 // the transmit buffer flushing is triggered by the start of frame
687 //
688 ISR(USB_GEN_vect)
689 {
690 uint8_t intbits, t;
692 intbits = UDINT;
693 UDINT = 0;
694 if (intbits & (1<<EORSTI)) {
695 UENUM = 0;
696 UECONX = 1;
697 UECFG0X = EP_TYPE_CONTROL;
698 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
699 UEIENX = (1<<RXSTPE);
700 usb_configuration = 0;
701 cdc_line_rtsdtr = 0;
702 }
703 if (intbits & (1<<SOFI)) {
704 if (usb_configuration) {
705 t = transmit_flush_timer;
706 if (t) {
707 transmit_flush_timer = --t;
708 if (!t) {
709 UENUM = CDC_TX_ENDPOINT;
710 UEINTX = 0x3A;
711 }
712 }
713 }
714 }
715 }
718 // Misc functions to wait for ready and send/receive packets
719 static inline void usb_wait_in_ready(void)
720 {
721 while (!(UEINTX & (1<<TXINI))) ;
722 }
723 static inline void usb_send_in(void)
724 {
725 UEINTX = ~(1<<TXINI);
726 }
727 static inline void usb_wait_receive_out(void)
728 {
729 while (!(UEINTX & (1<<RXOUTI))) ;
730 }
731 static inline void usb_ack_out(void)
732 {
733 UEINTX = ~(1<<RXOUTI);
734 }
738 // USB Endpoint Interrupt - endpoint 0 is handled here. The
739 // other endpoints are manipulated by the user-callable
740 // functions, and the start-of-frame interrupt.
741 //
742 ISR(USB_COM_vect)
743 {
744 uint8_t intbits;
745 const uint8_t *list;
746 const uint8_t *cfg;
747 uint8_t i, n, len, en;
748 uint8_t *p;
749 uint8_t bmRequestType;
750 uint8_t bRequest;
751 uint16_t wValue;
752 uint16_t wIndex;
753 uint16_t wLength;
754 uint16_t desc_val;
755 const uint8_t *desc_addr;
756 uint8_t desc_length;
758 UENUM = 0;
759 intbits = UEINTX;
760 if (intbits & (1<<RXSTPI)) {
761 bmRequestType = UEDATX;
762 bRequest = UEDATX;
763 wValue = UEDATX;
764 wValue |= (UEDATX << 8);
765 wIndex = UEDATX;
766 wIndex |= (UEDATX << 8);
767 wLength = UEDATX;
768 wLength |= (UEDATX << 8);
769 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
770 if (bRequest == GET_DESCRIPTOR) {
771 list = (const uint8_t *)descriptor_list;
772 for (i=0; ; i++) {
773 if (i >= NUM_DESC_LIST) {
774 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
775 return;
776 }
777 desc_val = pgm_read_word(list);
778 if (desc_val != wValue) {
779 list += sizeof(struct descriptor_list_struct);
780 continue;
781 }
782 list += 2;
783 desc_val = pgm_read_word(list);
784 if (desc_val != wIndex) {
785 list += sizeof(struct descriptor_list_struct)-2;
786 continue;
787 }
788 list += 2;
789 desc_addr = (const uint8_t *)pgm_read_word(list);
790 list += 2;
791 desc_length = pgm_read_byte(list);
792 break;
793 }
794 len = (wLength < 256) ? wLength : 255;
795 if (len > desc_length) len = desc_length;
796 do {
797 // wait for host ready for IN packet
798 do {
799 i = UEINTX;
800 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
801 if (i & (1<<RXOUTI)) return; // abort
802 // send IN packet
803 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
804 for (i = n; i; i--) {
805 UEDATX = pgm_read_byte(desc_addr++);
806 }
807 len -= n;
808 usb_send_in();
809 } while (len || n == ENDPOINT0_SIZE);
810 return;
811 }
812 if (bRequest == SET_ADDRESS) {
813 usb_send_in();
814 usb_wait_in_ready();
815 UDADDR = wValue | (1<<ADDEN);
816 return;
817 }
818 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
819 usb_configuration = wValue;
820 cdc_line_rtsdtr = 0;
821 transmit_flush_timer = 0;
822 usb_send_in();
823 cfg = endpoint_config_table;
824 for (i=1; i<5; i++) {
825 UENUM = i;
826 en = pgm_read_byte(cfg++);
827 UECONX = en;
828 if (en) {
829 UECFG0X = pgm_read_byte(cfg++);
830 UECFG1X = pgm_read_byte(cfg++);
831 }
832 }
833 UERST = 0x1E;
834 UERST = 0;
835 return;
836 }
837 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
838 usb_wait_in_ready();
839 UEDATX = usb_configuration;
840 usb_send_in();
841 return;
842 }
843 if (bRequest == CDC_GET_LINE_CODING && bmRequestType == 0xA1) {
844 usb_wait_in_ready();
845 p = cdc_line_coding;
846 for (i=0; i<7; i++) {
847 UEDATX = *p++;
848 }
849 usb_send_in();
850 return;
851 }
852 if (bRequest == CDC_SET_LINE_CODING && bmRequestType == 0x21) {
853 usb_wait_receive_out();
854 p = cdc_line_coding;
855 for (i=0; i<7; i++) {
856 *p++ = UEDATX;
857 }
858 usb_ack_out();
859 usb_send_in();
860 return;
861 }
862 if (bRequest == CDC_SET_CONTROL_LINE_STATE && bmRequestType == 0x21) {
863 cdc_line_rtsdtr = wValue;
864 usb_wait_in_ready();
865 usb_send_in();
866 return;
867 }
868 if (bRequest == GET_STATUS) {
869 usb_wait_in_ready();
870 i = 0;
871 #ifdef SUPPORT_ENDPOINT_HALT
872 if (bmRequestType == 0x82) {
873 UENUM = wIndex;
874 if (UECONX & (1<<STALLRQ)) i = 1;
875 UENUM = 0;
876 }
877 #endif
878 UEDATX = i;
879 UEDATX = 0;
880 usb_send_in();
881 return;
882 }
883 #ifdef SUPPORT_ENDPOINT_HALT
884 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) && bmRequestType == 0x02 && wValue == 0) {
885 i = wIndex & 0x7F;
886 if (i >= 1 && i <= MAX_ENDPOINT) {
887 usb_send_in();
888 UENUM = i;
889 if (bRequest == SET_FEATURE) {
890 UECONX = (1<<STALLRQ)|(1<<EPEN);
891 } else {
892 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
893 UERST = (1 << i);
894 UERST = 0;
895 }
896 return;
897 }
898 }
899 #endif
900 }
901 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
902 }
904 /* ----- EOF usb_serial.c ----- */