|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
-
-
- #include <avr/io.h>
- #include <stdint.h>
- #include "usb_common.h"
- #include "usb_private.h"
- #include "usb_api.h"
- #include "wiring.h"
-
-
-
- void usb_serial_class::begin(long speed)
- {
-
- peek_buf = -1;
- usb_init();
- uint16_t begin_wait = (uint16_t)millis();
- while (1) {
-
- if (usb_configuration) {
- delay(200);
- return;
- }
-
- if (usb_suspended) {
- uint16_t begin_suspend = (uint16_t)millis();
- while (usb_suspended) {
-
-
-
- if ((uint16_t)millis() - begin_suspend > 250) {
- return;
- }
- }
- }
-
-
- if ((uint16_t)millis() - begin_wait > 2500) return;
- }
- }
-
- void usb_serial_class::end()
- {
- usb_shutdown();
- delay(25);
- }
-
-
- int usb_serial_class::available()
- {
- uint8_t n=0, i, intr_state;
-
- intr_state = SREG;
- cli();
- if (usb_configuration) {
- UENUM = CDC_RX_ENDPOINT;
- n = UEBCLX;
- if (!n) {
- i = UEINTX;
- if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
- }
- }
- SREG = intr_state;
- if (peek_buf >= 0 && n < 255) n++;
- return n;
- }
-
- int usb_serial_class::peek()
- {
- if (peek_buf < 0) peek_buf = read();
- return peek_buf;
- }
-
-
- int usb_serial_class::read(void)
- {
- uint8_t c, intr_state;
-
- if (peek_buf >= 0) {
- c = peek_buf;
- peek_buf = -1;
- return c;
- }
-
-
-
- intr_state = SREG;
- cli();
- if (!usb_configuration) {
- SREG = intr_state;
- return -1;
- }
- UENUM = CDC_RX_ENDPOINT;
- retry:
- c = UEINTX;
- if (!(c & (1<<RWAL))) {
-
- if (c & (1<<RXOUTI)) {
- UEINTX = 0x6B;
- goto retry;
- }
- SREG = intr_state;
- return -1;
- }
-
- c = UEDATX;
-
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
- SREG = intr_state;
- return c;
- }
-
- size_t usb_serial_class::readBytes(char *buffer, size_t length)
- {
- size_t count=0;
- unsigned long startMillis;
- uint8_t num, intr_state;
-
- startMillis = millis();
- if (length <= 0) return 0;
- if (peek_buf >= 0) {
- *buffer++ = peek_buf;
- peek_buf = -1;
- length--;
- if (length == 0) return 1;
- count = 1;
- }
- do {
- intr_state = SREG;
- cli();
- if (!usb_configuration) {
- SREG = intr_state;
- break;
- }
- UENUM = CDC_RX_ENDPOINT;
- if (!(UEINTX & (1<<RXOUTI))) {
- SREG = intr_state;
- break;
- }
- num = UEBCLX;
- if (num > length) num = length;
- for (uint8_t i=0; i < num; i++) {
- *buffer++ = UEDATX;
- }
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
- SREG = intr_state;
- count += num;
- length -= num;
- if (length == 0) return count;
- } while(millis() - startMillis < _timeout);
- setReadError();
- return count;
- }
-
-
- void usb_serial_class::flush()
- {
- send_now();
- }
-
-
-
- void usb_serial_class::clear()
- {
- uint8_t intr_state;
-
- if (usb_configuration) {
- intr_state = SREG;
- cli();
- UENUM = CDC_RX_ENDPOINT;
- while ((UEINTX & (1<<RWAL))) {
- UEINTX = 0x6B;
- }
- SREG = intr_state;
- }
- peek_buf = -1;
- }
-
-
- #if 0
-
- void usb_serial_class::write(uint8_t c)
- {
- uint8_t timeout, intr_state;
-
-
- if (!usb_configuration) return;
-
-
-
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
-
- if (transmit_previous_timeout) {
- if (!(UEINTX & (1<<RWAL))) {
- SREG = intr_state;
- return;
- }
- transmit_previous_timeout = 0;
- }
-
- timeout = UDFNUML + TRANSMIT_TIMEOUT;
- while (1) {
-
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
-
-
- if (UDFNUML == timeout) {
- transmit_previous_timeout = 1;
- return;
- }
-
- if (!usb_configuration) return;
-
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
- }
-
- UEDATX = c;
-
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
- transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
- SREG = intr_state;
- }
- #endif
-
-
- int usb_serial_class::availableForWrite()
- {
- uint8_t intr_state, write_size;
-
- if (!usb_configuration) return 0;
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
- write_size = CDC_TX_SIZE - UEBCLX;
- SREG = intr_state;
- return write_size;
- }
-
-
- size_t usb_serial_class::write(const uint8_t *buffer, uint16_t size)
- {
- uint8_t timeout, intr_state, write_size;
- size_t count=0;
-
-
- if (!usb_configuration) {
- setWriteError();
- goto end;
- }
-
-
-
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
-
- if (transmit_previous_timeout) {
- if (!(UEINTX & (1<<RWAL))) {
- SREG = intr_state;
- setWriteError();
- goto end;
- }
- transmit_previous_timeout = 0;
- }
-
- while (size) {
-
- timeout = UDFNUML + TRANSMIT_TIMEOUT;
- while (1) {
-
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
-
-
- if (UDFNUML == timeout) {
- transmit_previous_timeout = 1;
- setWriteError();
- goto end;
- }
-
- if (!usb_configuration) {
- setWriteError();
- goto end;
- }
-
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
- }
-
-
- write_size = CDC_TX_SIZE - UEBCLX;
- if (write_size > size) write_size = size;
- size -= write_size;
- count += write_size;
-
- #define ASM_COPY1(src, dest, tmp) "ld " tmp ", " src "\n\t" "st " dest ", " tmp "\n\t"
- #define ASM_COPY2(src, dest, tmp) ASM_COPY1(src, dest, tmp) ASM_COPY1(src, dest, tmp)
- #define ASM_COPY4(src, dest, tmp) ASM_COPY2(src, dest, tmp) ASM_COPY2(src, dest, tmp)
- #define ASM_COPY8(src, dest, tmp) ASM_COPY4(src, dest, tmp) ASM_COPY4(src, dest, tmp)
-
- #if 1
-
- do {
- uint8_t tmp;
- asm volatile(
- "L%=begin:" "\n\t"
- "ldi r30, %4" "\n\t"
- "sub r30, %3" "\n\t"
- "cpi r30, %4" "\n\t"
- "brsh L%=err" "\n\t"
- "lsl r30" "\n\t"
- "clr r31" "\n\t"
- "subi r30, lo8(-(pm(L%=table)))" "\n\t"
- "sbci r31, hi8(-(pm(L%=table)))" "\n\t"
- "ijmp" "\n\t"
- "L%=err:" "\n\t"
- "rjmp L%=end" "\n\t"
- "L%=table:" "\n\t"
- #if (CDC_TX_SIZE == 64)
- ASM_COPY8("Y+", "X", "%1")
- ASM_COPY8("Y+", "X", "%1")
- ASM_COPY8("Y+", "X", "%1")
- ASM_COPY8("Y+", "X", "%1")
- #endif
- #if (CDC_TX_SIZE >= 32)
- ASM_COPY8("Y+", "X", "%1")
- ASM_COPY8("Y+", "X", "%1")
- #endif
- #if (CDC_TX_SIZE >= 16)
- ASM_COPY8("Y+", "X", "%1")
- #endif
- ASM_COPY8("Y+", "X", "%1")
- "L%=end:" "\n\t"
- : "+y" (buffer), "=r" (tmp)
- : "x" (&UEDATX), "r" (write_size), "M" (CDC_TX_SIZE)
- : "r30", "r31"
- );
- } while (0);
- #endif
-
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
- transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
- }
- SREG = intr_state;
- end:
- return count;
- }
-
-
-
-
-
-
-
-
-
-
- void usb_serial_class::send_now(void)
- {
- uint8_t intr_state;
-
- intr_state = SREG;
- cli();
- if (usb_configuration && transmit_flush_timer) {
- UENUM = CDC_TX_ENDPOINT;
- UEINTX = 0x3A;
- transmit_flush_timer = 0;
- }
- SREG = intr_state;
- }
-
- uint32_t usb_serial_class::baud(void)
- {
-
- return (uint32_t)cdc_line_coding[0]
- | ((uint32_t)cdc_line_coding[1] << 8)
- | ((uint32_t)cdc_line_coding[2] << 16)
- | ((uint32_t)cdc_line_coding[3] << 24);
- }
-
- uint8_t usb_serial_class::stopbits(void)
- {
- return cdc_line_coding[4];
- }
-
- uint8_t usb_serial_class::paritytype(void)
- {
- return cdc_line_coding[5];
- }
-
- uint8_t usb_serial_class::numbits(void)
- {
- return cdc_line_coding[6];
- }
-
- uint8_t usb_serial_class::dtr(void)
- {
- return (cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0;
- }
-
- uint8_t usb_serial_class::rts(void)
- {
- return (cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0;
- }
-
- usb_serial_class::operator bool()
- {
- if (usb_configuration &&
- (cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS))) {
- return true;
- }
- return false;
- }
-
-
-
-
-
- usb_serial_class Serial = usb_serial_class();
-
|