|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765 |
-
-
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include <stdint.h>
- #include "usb_common.h"
- #include "usb_private.h"
- #include "usb_api.h"
- #include "wiring.h"
-
-
- int usb_rawhid_class::available(void)
- {
- uint8_t n=0, intr_state;
-
- intr_state = SREG;
- cli();
- if (usb_configuration) {
- UENUM = RAWHID_RX_ENDPOINT;
- n = UEBCLX;
- }
- SREG = intr_state;
- return n;
- }
-
-
- int usb_rawhid_class::recv(void *ptr, uint16_t timeout)
- {
- uint8_t *buffer = (uint8_t *)ptr;
- uint8_t intr_state;
-
-
- if (!usb_configuration) return -1;
- intr_state = SREG;
- cli();
- rawhid_rx_timeout_count = timeout;
- UENUM = RAWHID_RX_ENDPOINT;
-
- while (1) {
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
- if (rawhid_rx_timeout_count == 0) return 0;
- if (!usb_configuration) return -1;
- intr_state = SREG;
- cli();
- UENUM = RAWHID_RX_ENDPOINT;
- }
-
- #if (RAWHID_RX_SIZE >= 64)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 63)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 62)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 61)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 60)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 59)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 58)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 57)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 56)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 55)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 54)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 53)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 52)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 51)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 50)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 49)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 48)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 47)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 46)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 45)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 44)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 43)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 42)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 41)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 40)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 39)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 38)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 37)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 36)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 35)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 34)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 33)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 32)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 31)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 30)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 29)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 28)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 27)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 26)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 25)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 24)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 23)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 22)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 21)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 20)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 19)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 18)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 17)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 16)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 15)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 14)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 13)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 12)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 11)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 10)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 9)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 8)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 7)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 6)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 5)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 4)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 3)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 2)
- *buffer++ = UEDATX;
- #endif
- #if (RAWHID_RX_SIZE >= 1)
- *buffer++ = UEDATX;
- #endif
-
- UEINTX = 0x6B;
- SREG = intr_state;
- return RAWHID_RX_SIZE;
- }
-
-
-
- int usb_rawhid_class::send(const void *ptr, uint16_t timeout)
- {
- const uint8_t *buffer = (const uint8_t *)ptr;
- uint8_t intr_state;
-
-
- if (!usb_configuration) return -1;
- intr_state = SREG;
- cli();
- rawhid_tx_timeout_count = timeout;
- UENUM = RAWHID_TX_ENDPOINT;
-
- while (1) {
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
- if (rawhid_tx_timeout_count == 0) return 0;
- if (!usb_configuration) return -1;
- intr_state = SREG;
- cli();
- UENUM = RAWHID_TX_ENDPOINT;
- }
-
- #if (RAWHID_TX_SIZE >= 64)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 63)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 62)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 61)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 60)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 59)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 58)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 57)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 56)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 55)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 54)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 53)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 52)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 51)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 50)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 49)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 48)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 47)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 46)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 45)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 44)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 43)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 42)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 41)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 40)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 39)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 38)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 37)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 36)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 35)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 34)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 33)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 32)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 31)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 30)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 29)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 28)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 27)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 26)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 25)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 24)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 23)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 22)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 21)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 20)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 19)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 18)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 17)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 16)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 15)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 14)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 13)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 12)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 11)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 10)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 9)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 8)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 7)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 6)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 5)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 4)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 3)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 2)
- UEDATX = *buffer++;
- #endif
- #if (RAWHID_TX_SIZE >= 1)
- UEDATX = *buffer++;
- #endif
-
- UEINTX = 0x3A;
- SREG = intr_state;
- return RAWHID_TX_SIZE;
- }
-
-
-
-
-
-
- static volatile uint8_t prev_byte=0;
-
- void usb_serial_class::begin(long speed)
- {
-
- 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;
- }
- prev_byte = 0;
- }
-
- void usb_serial_class::end()
- {
- usb_shutdown();
- delay(25);
- }
-
-
-
-
- int usb_serial_class::available()
- {
- uint8_t c;
-
- c = prev_byte;
- if (c) return 1;
- c = readnext();
- if (c) {
- prev_byte = c;
- return 1;
- }
- return 0;
- }
-
-
- int usb_serial_class::read()
- {
- uint8_t c;
-
- c = prev_byte;
- if (c) {
- prev_byte = 0;
- return c;
- }
- c = readnext();
- if (c) return c;
- return -1;
- }
-
- int usb_serial_class::peek()
- {
- uint8_t c;
-
- c = prev_byte;
- if (c) return c;
- c = readnext();
- if (c) {
- prev_byte = c;
- return c;
- }
- return -1;
- }
-
-
- uint8_t usb_serial_class::readnext(void)
- {
- uint8_t c, intr_state;
-
-
-
-
- intr_state = SREG;
- cli();
- if (!usb_configuration) {
- SREG = intr_state;
- return 0;
- }
- UENUM = DEBUG_RX_ENDPOINT;
- try_again:
- if (!(UEINTX & (1<<RWAL))) {
-
- SREG = intr_state;
- return 0;
- }
-
- c = UEDATX;
- if (c == 0) {
-
-
- UEINTX = 0x6B;
- goto try_again;
- }
-
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
- SREG = intr_state;
- return c;
- }
-
-
- void usb_serial_class::flush()
- {
- uint8_t intr_state;
-
- if (usb_configuration) {
- intr_state = SREG;
- cli();
- UENUM = DEBUG_RX_ENDPOINT;
- while ((UEINTX & (1<<RWAL))) {
- UEINTX = 0x6B;
- }
- SREG = intr_state;
- }
- prev_byte = 0;
- }
-
-
- #if ARDUINO >= 100
- size_t usb_serial_class::write(uint8_t c)
- #else
- void usb_serial_class::write(uint8_t c)
- #endif
- {
-
- uint8_t timeout, intr_state;
-
-
- if (!usb_configuration) goto error;
-
-
-
- intr_state = SREG;
- cli();
- UENUM = DEBUG_TX_ENDPOINT;
-
- #if 0
-
- if (previous_timeout) {
- if (!(UEINTX & (1<<RWAL))) {
- SREG = intr_state;
- return;
- }
- previous_timeout = 0;
- }
- #endif
-
- timeout = UDFNUML + TRANSMIT_TIMEOUT;
- while (1) {
-
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
-
-
- if (UDFNUML == timeout) {
-
- goto error;
- }
-
- if (!usb_configuration) goto error;
-
- intr_state = SREG;
- cli();
- UENUM = DEBUG_TX_ENDPOINT;
- }
-
- UEDATX = c;
-
- if (!(UEINTX & (1<<RWAL))) {
- UEINTX = 0x3A;
- debug_flush_timer = 0;
- } else {
- debug_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
- }
- SREG = intr_state;
- #if ARDUINO >= 100
- return 1;
- #endif
- error:
- #if ARDUINO >= 100
- setWriteError();
- return 0;
- #else
- return;
- #endif
- }
-
-
-
-
-
-
-
-
- void usb_serial_class::send_now(void)
- {
- uint8_t intr_state;
-
- intr_state = SREG;
- cli();
- if (debug_flush_timer) {
- UENUM = DEBUG_TX_ENDPOINT;
- while ((UEINTX & (1<<RWAL))) {
- UEDATX = 0;
- }
- UEINTX = 0x3A;
- debug_flush_timer = 0;
- }
- SREG = intr_state;
- }
-
- uint32_t usb_serial_class::baud(void)
- {
- return ((uint32_t)DEBUG_TX_SIZE * 10000 / DEBUG_TX_INTERVAL);
- }
-
- uint8_t usb_serial_class::stopbits(void)
- {
- return 1;
- }
-
- uint8_t usb_serial_class::paritytype(void)
- {
- return 0;
- }
-
- uint8_t usb_serial_class::numbits(void)
- {
- return 8;
- }
-
- uint8_t usb_serial_class::dtr(void)
- {
- return 1;
- }
-
- uint8_t usb_serial_class::rts(void)
- {
- return 1;
- }
-
- usb_serial_class::operator bool()
- {
- if (usb_configuration) return true;
- return false;
- }
-
-
-
-
-
- usb_serial_class Serial = usb_serial_class();
- usb_rawhid_class RawHID = usb_rawhid_class();
-
|