/* USB API for Teensy USB Development Board * http://www.pjrc.com/teensy/teensyduino.html * Copyright (c) 2008 PJRC.COM, LLC * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include "usb_common.h" #include "usb_private.h" #include "usb_api.h" #include "wiring.h" // Public Methods ////////////////////////////////////////////////////////////// void usb_serial_class::begin(long speed) { // make sure USB is initialized peek_buf = -1; usb_init(); uint16_t begin_wait = (uint16_t)millis(); while (1) { // wait for the host to finish enumeration if (usb_configuration) { delay(200); // a little time for host to load a driver return; } // or for suspend mode (powered without USB) if (usb_suspended) { uint16_t begin_suspend = (uint16_t)millis(); while (usb_suspended) { // must remain suspended for a while, because // normal USB enumeration causes brief suspend // states, typically under 0.1 second if ((uint16_t)millis() - begin_suspend > 250) { return; } } } // ... or a timout (powered by a USB power adaptor that // wiggles the data lines to keep a USB device charging) if ((uint16_t)millis() - begin_wait > 2500) return; } } void usb_serial_class::end() { usb_shutdown(); delay(25); } // number of bytes available in the receive buffer 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<= 0 && n < 255) n++; return n; } int usb_serial_class::peek() { if (peek_buf < 0) peek_buf = read(); return peek_buf; } // get the next character, or -1 if nothing received int usb_serial_class::read(void) { uint8_t c, intr_state; if (peek_buf >= 0) { c = peek_buf; peek_buf = -1; return c; } // interrupts are disabled so these functions can be // used from the main program or interrupt context, // even both in the same program! intr_state = SREG; cli(); if (!usb_configuration) { SREG = intr_state; return -1; } UENUM = CDC_RX_ENDPOINT; retry: c = UEINTX; if (!(c & (1< 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 // write the packet 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 this completed a packet, transmit it now! if (!(UEINTX & (1<> 8; if (msb >= 0xC2) { if (msb <= 0xDF) { n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6); } else if (msb == 0xF0) { presskey(n, 0); return; } else if (msb == 0xE0) { presskey(0, n); return; } else if (msb == 0xE2) { //press_system_key(n); return; } else if (msb >= 0xE4 && msb <= 0xE7) { //press_consumer_key(n & 0x3FF); return; } else { return; } } KEYCODE_TYPE keycode = unicode_to_keycode(n); if (!keycode) return; #ifdef DEADKEYS_MASK KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode); if (deadkeycode) { modrestore = keyboard_report_data[0]; if (modrestore) { keyboard_report_data[0] = 0; send_now(); } // TODO: test if operating systems recognize // deadkey sequences when other keys are held mod = keycode_to_modifier(deadkeycode); key = keycode_to_key(deadkeycode); presskey(key, mod); releasekey(key, mod); } #endif mod = keycode_to_modifier(keycode); key = keycode_to_key(keycode); presskey(key, mod | modrestore); } void usb_keyboard_class::release(uint16_t n) { uint8_t key, mod, msb; msb = n >> 8; if (msb >= 0xC2) { if (msb <= 0xDF) { n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6); } else if (msb == 0xF0) { releasekey(n, 0); return; } else if (msb == 0xE0) { releasekey(0, n); return; } else if (msb == 0xE2) { //release_system_key(n); return; } else if (msb >= 0xE4 && msb <= 0xE7) { //release_consumer_key(n & 0x3FF); return; } else { return; } } KEYCODE_TYPE keycode = unicode_to_keycode(n); if (!keycode) return; mod = keycode_to_modifier(keycode); key = keycode_to_key(keycode); releasekey(key, mod); } void usb_keyboard_class::presskey(uint8_t key, uint8_t modifier) { bool send_required = false; uint8_t i; if (modifier) { if ((keyboard_report_data[0] & modifier) != modifier) { keyboard_report_data[0] |= modifier; send_required = true; } } if (key) { for (i=2; i < 8; i++) { if (keyboard_report_data[i] == key) goto end; } for (i=2; i < 8; i++) { if (keyboard_report_data[i] == 0) { keyboard_report_data[i] = key; send_required = true; goto end; } } } end: if (send_required) send_now(); } void usb_keyboard_class::releasekey(uint8_t key, uint8_t modifier) { bool send_required = false; uint8_t i; if (modifier) { if ((keyboard_report_data[0] & modifier) != 0) { keyboard_report_data[0] &= ~modifier; send_required = true; } } if (key) { for (i=2; i < 8; i++) { if (keyboard_report_data[i] == key) { keyboard_report_data[i] = 0; send_required = true; } } } if (send_required) send_now(); } void usb_keyboard_class::releaseAll(void) { uint8_t i, anybits; anybits = keyboard_report_data[0]; for (i=2; i < 8; i++) { anybits |= keyboard_report_data[i]; keyboard_report_data[i] = 0; } if (!anybits) return; keyboard_report_data[0] = 0; send_now(); } void usb_mouse_class::move(int8_t x, int8_t y, int8_t wheel, int8_t horiz) { uint8_t intr_state, timeout; if (!usb_configuration) return; if (x == -128) x = -127; if (y == -128) y = -127; if (wheel == -128) wheel = -127; if (horiz == -128) horiz = -127; intr_state = SREG; cli(); UENUM = MOUSE_ENDPOINT; timeout = UDFNUML + 50; while (1) { // are we ready to transmit? if (UEINTX & (1<