diff options
-rw-r--r-- | converter/adb_usb/Makefile | 4 | ||||
-rw-r--r-- | converter/adb_usb/Makefile.blargg | 131 | ||||
-rw-r--r-- | converter/adb_usb/adb_blargg.c | 216 | ||||
-rw-r--r-- | converter/adb_usb/adb_blargg.h | 38 | ||||
-rw-r--r-- | converter/adb_usb/led.c | 2 | ||||
-rw-r--r-- | converter/adb_usb/matrix.c | 8 | ||||
-rw-r--r-- | protocol/adb.c | 18 |
7 files changed, 13 insertions, 404 deletions
diff --git a/converter/adb_usb/Makefile b/converter/adb_usb/Makefile index 372ef6c09a..2114167424 100644 --- a/converter/adb_usb/Makefile +++ b/converter/adb_usb/Makefile @@ -94,7 +94,7 @@ ARCH = AVR8 F_USB = $(F_CPU) # Interrupt driven control endpoint task(+60) -#OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT +OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT # Boot Section Size in *bytes* @@ -109,7 +109,7 @@ OPT_DEFS += -DBOOTLOADER_SIZE=4096 # Build Options # comment out to disable the options. # -BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) +#BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) MOUSEKEY_ENABLE = yes # Mouse keys(+4700) EXTRAKEY_ENABLE = yes # Audio control and System control(+450) CONSOLE_ENABLE = yes # Console for debug(+400) diff --git a/converter/adb_usb/Makefile.blargg b/converter/adb_usb/Makefile.blargg deleted file mode 100644 index edce82d695..0000000000 --- a/converter/adb_usb/Makefile.blargg +++ /dev/null @@ -1,131 +0,0 @@ -#---------------------------------------------------------------------------- -# On command line: -# -# make all = Make software. -# -# make clean = Clean out built project files. -# -# make coff = Convert ELF to AVR COFF. -# -# make extcoff = Convert ELF to AVR Extended COFF. -# -# make program = Download the hex file to the device. -# Please customize your programmer settings(PROGRAM_CMD) -# -# make teensy = Download the hex file to the device, using teensy_loader_cli. -# (must have teensy_loader_cli installed). -# -# make dfu = Download the hex file to the device, using dfu-programmer (must -# have dfu-programmer installed). -# -# make flip = Download the hex file to the device, using Atmel FLIP (must -# have Atmel FLIP installed). -# -# make dfu-ee = Download the eeprom file to the device, using dfu-programmer -# (must have dfu-programmer installed). -# -# make flip-ee = Download the eeprom file to the device, using Atmel FLIP -# (must have Atmel FLIP installed). -# -# make debug = Start either simulavr or avarice as specified for debugging, -# with avr-gdb or avr-insight as the front end for debugging. -# -# make filename.s = Just compile filename.c into the assembler code only. -# -# make filename.i = Create a preprocessed source file for use in submitting -# bug reports to the GCC project. -# -# To rebuild project do "make clean" then "make all". -#---------------------------------------------------------------------------- - -# Target file name (without extension). -TARGET = adb_usb_blargg - -# Directory common source filess exist -TOP_DIR = ../.. - -# Directory keyboard dependent files exist -TARGET_DIR = . - -# project specific files -SRC = keymap.c \ - matrix.c \ - led.c \ - adb_blargg.c - -CONFIG_H = config.h - - -# MCU name -#MCU = at90usb1287 -MCU = atmega32u4 - -# Processor frequency. -# This will define a symbol, F_CPU, in all source code files equal to the -# processor frequency in Hz. You can then use this symbol in your source code to -# calculate timings. Do NOT tack on a 'UL' at the end, this will be done -# automatically to create a 32-bit value in your source code. -# -# This will be an integer division of F_USB below, as it is sourced by -# F_USB after it has run through any CPU prescalers. Note that this value -# does not *change* the processor frequency - it should merely be updated to -# reflect the processor speed set externally so that the code can use accurate -# software delays. -F_CPU = 16000000 - - -# -# LUFA specific -# -# Target architecture (see library "Board Types" documentation). -ARCH = AVR8 - -# Input clock frequency. -# This will define a symbol, F_USB, in all source code files equal to the -# input clock frequency (before any prescaling is performed) in Hz. This value may -# differ from F_CPU if prescaling is used on the latter, and is required as the -# raw input clock is fed directly to the PLL sections of the AVR for high speed -# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL' -# at the end, this will be done automatically to create a 32-bit value in your -# source code. -# -# If no clock division is performed on the input clock inside the AVR (via the -# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU. -F_USB = $(F_CPU) - -# Interrupt driven control endpoint task(+60) -#OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT - - -# Boot Section Size in *bytes* -# Teensy halfKay 512 -# Teensy++ halfKay 1024 -# Atmel DFU loader 4096 -# LUFA bootloader 4096 -# USBaspLoader 2048 -OPT_DEFS += -DBOOTLOADER_SIZE=4096 - - -# Build Options -# comment out to disable the options. -# -BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) -MOUSEKEY_ENABLE = yes # Mouse keys(+4700) -EXTRAKEY_ENABLE = yes # Audio control and System control(+450) -CONSOLE_ENABLE = yes # Console for debug(+400) -COMMAND_ENABLE = yes # Commands for debug and configuration -#SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend -#NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA - - -# Optimize size but this may cause error "relocation truncated to fit" -#EXTRALDFLAGS = -Wl,--relax - -# Search Path -VPATH += $(TARGET_DIR) -VPATH += $(TOP_DIR) - -include $(TOP_DIR)/protocol/lufa.mk -include $(TOP_DIR)/protocol.mk -include $(TOP_DIR)/common.mk -include $(TOP_DIR)/rules.mk diff --git a/converter/adb_usb/adb_blargg.c b/converter/adb_usb/adb_blargg.c deleted file mode 100644 index 963758c533..0000000000 --- a/converter/adb_usb/adb_blargg.c +++ /dev/null @@ -1,216 +0,0 @@ -// Bit-banged implementation without any use of interrupts. -// Data pin must have external 1K pull-up resistor. -// Operates data pin as open-collector output. - -#include "adb_blargg.h" - -#ifdef HAVE_CONFIG_H - #include "config.h" -#endif - -#include <avr/io.h> -#include <avr/interrupt.h> -#include <util/delay.h> - -// Copyright 2011 Jun WAKO <wakojun@gmail.com> -// Copyright 2013 Shay Green <gblargg@gmail.com> -// See bottom of file for license - -typedef uint8_t byte; - -// Make loop iteration take us total, including cyc overhead of loop logic -#define delay_loop_usec( us, cyc ) \ - __builtin_avr_delay_cycles( (unsigned long) (F_CPU / 1e6 * (us) + 0.5) - (cyc) ) - -#if !defined(ADB_PORT) || \ - !defined(ADB_PIN) || \ - !defined(ADB_DDR) || \ - !defined(ADB_DATA_BIT) - #error -#endif - -enum { data_mask = 1<<ADB_DATA_BIT }; - -enum { adb_cmd_read = 0x2C }; -enum { adb_cmd_write = 0x28 }; - -// gcc is very unreliable for inlining, so use macros -#define data_lo() (ADB_DDR |= data_mask) -#define data_hi() (ADB_DDR &= ~data_mask) -#define data_in() (ADB_PIN & data_mask) - -static void place_bit( byte bit ) -{ - // 100 us bit cell time - data_lo(); - _delay_us( 35 ); - - // Difference between a 0 and 1 bit is just this 30us portion in the middle - if ( bit ) - data_hi(); - _delay_us( 30 ); - - data_hi(); - _delay_us( 35 ); -} - -static void place_bit0( void ) { place_bit( 0 ); } -static void place_bit1( void ) { place_bit( 1 ); } - -static void send_byte( byte data ) -{ - for ( byte n = 8; n; n-- ) - { - place_bit( data & 0x80 ); - data <<= 1; - } -} - -static void command( byte cmd ) -{ - data_lo(); - _delay_us( 800 ); - place_bit1(); - send_byte( cmd ); - place_bit0(); -} - -void adb_host_init( void ) -{ - // Always keep port output 0, then just toggle DDR to be GND or leave it floating (high). - ADB_DDR &= ~data_mask; - ADB_PORT &= ~data_mask; - - #ifdef ADB_PSW_BIT - // Weak pull-up - ADB_PORT |= (1<<ADB_PSW_BIT); - ADB_DDR &= ~(1<<ADB_PSW_BIT); - #endif -} - -bool adb_host_psw( void ) -{ - #ifdef ADB_PSW_BIT - return (ADB_PIN & (1<<ADB_PSW_BIT)) != 0; - #else - return true; - #endif -} - -// Waits while data == val, or until us timeout expires. Returns remaining time, -// zero if timed out. -static byte while_data( byte us, byte data ) -{ - while ( data_in() == data ) - { - delay_loop_usec( 1 /* us period */, 7 /* cycles loop overhead */ ); - if ( !--us ) - break; - } - return us; -} - -static byte while_lo( byte us ) { return while_data( us, 0 ); } -static byte while_hi( byte us ) { return while_data( us, data_mask ); } - -static uint16_t adb_host_talk( byte cmd ) -{ - command( cmd ); - _delay_us( 5 ); - if ( !while_hi( 260 - 5 ) ) // avg 160 - return adb_host_nothing; - - // Receive start bit and 16 data bits. - // Doing them all in loop allows consistent error checking - uint16_t data = 0; - byte n = 17; - do - { - data <<= 1; - enum { timeout = 130 }; // maximum bit cell time - - byte lo = while_lo( timeout ); - if ( !lo ) - goto error; // timeout - - byte hi = while_hi( lo ); - if ( !hi ) - goto error; // timeout - - if ( timeout-lo < lo-hi ) - data |= 1; - else if ( n == 17 ) - goto error; // start bit is wrong - } - while ( --n ); - - // duration must be split in two due to 255 limit - if ( !while_lo( 255 ) && !while_lo( 351 - 255 ) ) - goto error; - - if ( while_hi( 91 ) ) - goto error; - - return data; - -error: - return adb_host_error; -} - -uint16_t adb_host_kbd_recv( void ) -{ - return adb_host_talk( adb_cmd_read + 0 ); -} - -uint16_t adb_host_kbd_modifiers( void ) -{ - return adb_host_talk( adb_cmd_read + 2 ); -} - -void adb_host_listen( byte cmd, byte data_h, byte data_l ) -{ - command( cmd ); - _delay_us( 200 ); - - place_bit1(); - send_byte( data_h ); - send_byte( data_l ); - place_bit0(); -} - -void adb_host_kbd_led( byte led ) -{ - adb_host_listen( adb_cmd_write + 2, 0, led & 0x07 ); -} - -/* This software is licensed with a Modified BSD License. -All of this is supposed to be Free Software, Open Source, DFSG-free, -GPL-compatible, and OK to use in both free and proprietary applications. -Additions and corrections to this file are welcome. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in - the documentation and/or other materials provided with the - distribution. - -* Neither the name of the copyright holders nor the names of - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. */ diff --git a/converter/adb_usb/adb_blargg.h b/converter/adb_usb/adb_blargg.h deleted file mode 100644 index 2542cb5496..0000000000 --- a/converter/adb_usb/adb_blargg.h +++ /dev/null @@ -1,38 +0,0 @@ -// Basic support for ADB keyboard - -#ifndef ADB_BLARGG_H -#define ADB_BLARGG_H - -#include <stdint.h> -#include <stdbool.h> - -// Sets up ADB bus. Doesn't send anything to keyboard. -void adb_host_init( void ); - -// Receives key press event from keyboard. -// 0xKKFF: one key changed state -// 0xKKKK: two keys changed state -enum { adb_host_nothing = 0 }; // no keys changed state -enum { adb_host_error = 0xFFFE }; // receive error -uint16_t adb_host_kbd_recv( void ); - -// Current state of keyboard modifiers and a few other keys -// Returns adb_host_nothing if keyboard didn't respond. -// Returns adb_host_error if error receiving. -uint16_t adb_host_kbd_modifiers( void ); - -// Sends command and two bytes of data to keyboard -void adb_host_listen( uint8_t cmd, uint8_t data_h, uint8_t data_l ); - -// Sets keyboard LEDs. Note that bits are inverted here, so 1 means off, 0 means on. -void adb_host_kbd_led( uint8_t led ); - -// State of power switch (false = pressed), or true if unsupported -bool adb_host_psw( void ); - - -// Legacy support -#define ADB_POWER 0x7F -#define ADB_CAPS 0x39 - -#endif diff --git a/converter/adb_usb/led.c b/converter/adb_usb/led.c index 1e7911f942..3ee64a8e7d 100644 --- a/converter/adb_usb/led.c +++ b/converter/adb_usb/led.c @@ -23,7 +23,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. void led_set(uint8_t usb_led) { - // need a wait to send command without miss - _delay_ms(100); adb_host_kbd_led(~usb_led); } diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index 54be2b0f57..d1b67d38d6 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -79,9 +79,9 @@ void matrix_init(void) for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; debug_enable = true; - debug_matrix = true; - debug_keyboard = true; - debug_mouse = true; + //debug_matrix = true; + //debug_keyboard = true; + //debug_mouse = true; print("debug enabled.\n"); return; } @@ -108,7 +108,7 @@ uint8_t matrix_scan(void) } else if (codes == 0xFFFF) { // power key release register_key(0xFF); } else if (key0 == 0xFF) { // error - xprintf("adb_host_kbd_recv: ERROR(%02X)\n", codes); + xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes); return key1; } else { register_key(key0); diff --git a/protocol/adb.c b/protocol/adb.c index 750f4b9650..a4783f36e5 100644 --- a/protocol/adb.c +++ b/protocol/adb.c @@ -41,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE. #include <avr/io.h> #include <avr/interrupt.h> #include "adb.h" -#include "debug.h" // GCC doesn't inline functions normally @@ -125,16 +124,15 @@ bool adb_host_psw(void) uint16_t adb_host_kbd_recv(void) { uint16_t data = 0; + cli(); attention(); send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00) place_bit0(); // Stopbit(0) if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us) + sei(); return 0; // No data to send } - // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck - // TODO: is this needed anymore with improved timing? - //cli(); uint8_t n = 17; // start bit + 16 data bits do { uint8_t lo = (uint8_t) wait_data_hi(130); @@ -153,10 +151,8 @@ uint16_t adb_host_kbd_recv(void) data |= 1; } else if (n == 17) { - // Service Request - dprintf("Startbit ERROR\n"); sei(); - return -2; + return -20; } } while ( --n ); @@ -164,21 +160,20 @@ uint16_t adb_host_kbd_recv(void) // Stop bit can't be checked normally since it could have service request lenghtening // and its high state never goes low. if (!wait_data_hi(351) || wait_data_lo(91)) { - dprintf("Stopbit ERROR\n"); sei(); - return -3; + return -21; } sei(); return data; error: - dprintf("Bit ERROR\n"); sei(); - return -4; + return -n; } void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l) { + cli(); attention(); send_byte(cmd); place_bit0(); // Stopbit(0) @@ -187,6 +182,7 @@ void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l) send_byte(data_h); send_byte(data_l); place_bit0(); // Stopbit(0); + sei(); } // send state of LEDs |