aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--converter/adb_usb/Makefile4
-rw-r--r--converter/adb_usb/Makefile.blargg131
-rw-r--r--converter/adb_usb/adb_blargg.c216
-rw-r--r--converter/adb_usb/adb_blargg.h38
-rw-r--r--converter/adb_usb/led.c2
-rw-r--r--converter/adb_usb/matrix.c8
-rw-r--r--protocol/adb.c18
7 files changed, 13 insertions, 404 deletions
diff --git a/converter/adb_usb/Makefile b/converter/adb_usb/Makefile
index 372ef6c09..211416742 100644
--- a/converter/adb_usb/Makefile
+++ b/converter/adb_usb/Makefile
@@ -94,7 +94,7 @@ ARCH = AVR8
94F_USB = $(F_CPU) 94F_USB = $(F_CPU)
95 95
96# Interrupt driven control endpoint task(+60) 96# Interrupt driven control endpoint task(+60)
97#OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT 97OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
98 98
99 99
100# Boot Section Size in *bytes* 100# Boot Section Size in *bytes*
@@ -109,7 +109,7 @@ OPT_DEFS += -DBOOTLOADER_SIZE=4096
109# Build Options 109# Build Options
110# comment out to disable the options. 110# comment out to disable the options.
111# 111#
112BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) 112#BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000)
113MOUSEKEY_ENABLE = yes # Mouse keys(+4700) 113MOUSEKEY_ENABLE = yes # Mouse keys(+4700)
114EXTRAKEY_ENABLE = yes # Audio control and System control(+450) 114EXTRAKEY_ENABLE = yes # Audio control and System control(+450)
115CONSOLE_ENABLE = yes # Console for debug(+400) 115CONSOLE_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 edce82d69..000000000
--- a/converter/adb_usb/Makefile.blargg
+++ /dev/null
@@ -1,131 +0,0 @@
1#----------------------------------------------------------------------------
2# On command line:
3#
4# make all = Make software.
5#
6# make clean = Clean out built project files.
7#
8# make coff = Convert ELF to AVR COFF.
9#
10# make extcoff = Convert ELF to AVR Extended COFF.
11#
12# make program = Download the hex file to the device.
13# Please customize your programmer settings(PROGRAM_CMD)
14#
15# make teensy = Download the hex file to the device, using teensy_loader_cli.
16# (must have teensy_loader_cli installed).
17#
18# make dfu = Download the hex file to the device, using dfu-programmer (must
19# have dfu-programmer installed).
20#
21# make flip = Download the hex file to the device, using Atmel FLIP (must
22# have Atmel FLIP installed).
23#
24# make dfu-ee = Download the eeprom file to the device, using dfu-programmer
25# (must have dfu-programmer installed).
26#
27# make flip-ee = Download the eeprom file to the device, using Atmel FLIP
28# (must have Atmel FLIP installed).
29#
30# make debug = Start either simulavr or avarice as specified for debugging,
31# with avr-gdb or avr-insight as the front end for debugging.
32#
33# make filename.s = Just compile filename.c into the assembler code only.
34#
35# make filename.i = Create a preprocessed source file for use in submitting
36# bug reports to the GCC project.
37#
38# To rebuild project do "make clean" then "make all".
39#----------------------------------------------------------------------------
40
41# Target file name (without extension).
42TARGET = adb_usb_blargg
43
44# Directory common source filess exist
45TOP_DIR = ../..
46
47# Directory keyboard dependent files exist
48TARGET_DIR = .
49
50# project specific files
51SRC = keymap.c \
52 matrix.c \
53 led.c \
54 adb_blargg.c
55
56CONFIG_H = config.h
57
58
59# MCU name
60#MCU = at90usb1287
61MCU = atmega32u4
62
63# Processor frequency.
64# This will define a symbol, F_CPU, in all source code files equal to the
65# processor frequency in Hz. You can then use this symbol in your source code to
66# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
67# automatically to create a 32-bit value in your source code.
68#
69# This will be an integer division of F_USB below, as it is sourced by
70# F_USB after it has run through any CPU prescalers. Note that this value
71# does not *change* the processor frequency - it should merely be updated to
72# reflect the processor speed set externally so that the code can use accurate
73# software delays.
74F_CPU = 16000000
75
76
77#
78# LUFA specific
79#
80# Target architecture (see library "Board Types" documentation).
81ARCH = AVR8
82
83# Input clock frequency.
84# This will define a symbol, F_USB, in all source code files equal to the
85# input clock frequency (before any prescaling is performed) in Hz. This value may
86# differ from F_CPU if prescaling is used on the latter, and is required as the
87# raw input clock is fed directly to the PLL sections of the AVR for high speed
88# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
89# at the end, this will be done automatically to create a 32-bit value in your
90# source code.
91#
92# If no clock division is performed on the input clock inside the AVR (via the
93# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
94F_USB = $(F_CPU)
95
96# Interrupt driven control endpoint task(+60)
97#OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
98
99
100# Boot Section Size in *bytes*
101# Teensy halfKay 512
102# Teensy++ halfKay 1024
103# Atmel DFU loader 4096
104# LUFA bootloader 4096
105# USBaspLoader 2048
106OPT_DEFS += -DBOOTLOADER_SIZE=4096
107
108
109# Build Options
110# comment out to disable the options.
111#
112BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000)
113MOUSEKEY_ENABLE = yes # Mouse keys(+4700)
114EXTRAKEY_ENABLE = yes # Audio control and System control(+450)
115CONSOLE_ENABLE = yes # Console for debug(+400)
116COMMAND_ENABLE = yes # Commands for debug and configuration
117#SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend
118#NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA
119
120
121# Optimize size but this may cause error "relocation truncated to fit"
122#EXTRALDFLAGS = -Wl,--relax
123
124# Search Path
125VPATH += $(TARGET_DIR)
126VPATH += $(TOP_DIR)
127
128include $(TOP_DIR)/protocol/lufa.mk
129include $(TOP_DIR)/protocol.mk
130include $(TOP_DIR)/common.mk
131include $(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 963758c53..000000000
--- a/converter/adb_usb/adb_blargg.c
+++ /dev/null
@@ -1,216 +0,0 @@
1// Bit-banged implementation without any use of interrupts.
2// Data pin must have external 1K pull-up resistor.
3// Operates data pin as open-collector output.
4
5#include "adb_blargg.h"
6
7#ifdef HAVE_CONFIG_H
8 #include "config.h"
9#endif
10
11#include <avr/io.h>
12#include <avr/interrupt.h>
13#include <util/delay.h>
14
15// Copyright 2011 Jun WAKO <wakojun@gmail.com>
16// Copyright 2013 Shay Green <gblargg@gmail.com>
17// See bottom of file for license
18
19typedef uint8_t byte;
20
21// Make loop iteration take us total, including cyc overhead of loop logic
22#define delay_loop_usec( us, cyc ) \
23 __builtin_avr_delay_cycles( (unsigned long) (F_CPU / 1e6 * (us) + 0.5) - (cyc) )
24
25#if !defined(ADB_PORT) || \
26 !defined(ADB_PIN) || \
27 !defined(ADB_DDR) || \
28 !defined(ADB_DATA_BIT)
29 #error
30#endif
31
32enum { data_mask = 1<<ADB_DATA_BIT };
33
34enum { adb_cmd_read = 0x2C };
35enum { adb_cmd_write = 0x28 };
36
37// gcc is very unreliable for inlining, so use macros
38#define data_lo() (ADB_DDR |= data_mask)
39#define data_hi() (ADB_DDR &= ~data_mask)
40#define data_in() (ADB_PIN & data_mask)
41
42static void place_bit( byte bit )
43{
44 // 100 us bit cell time
45 data_lo();
46 _delay_us( 35 );
47
48 // Difference between a 0 and 1 bit is just this 30us portion in the middle
49 if ( bit )
50 data_hi();
51 _delay_us( 30 );
52
53 data_hi();
54 _delay_us( 35 );
55}
56
57static void place_bit0( void ) { place_bit( 0 ); }
58static void place_bit1( void ) { place_bit( 1 ); }
59
60static void send_byte( byte data )
61{
62 for ( byte n = 8; n; n-- )
63 {
64 place_bit( data & 0x80 );
65 data <<= 1;
66 }
67}
68
69static void command( byte cmd )
70{
71 data_lo();
72 _delay_us( 800 );
73 place_bit1();
74 send_byte( cmd );
75 place_bit0();
76}
77
78void adb_host_init( void )
79{
80 // Always keep port output 0, then just toggle DDR to be GND or leave it floating (high).
81 ADB_DDR &= ~data_mask;
82 ADB_PORT &= ~data_mask;
83
84 #ifdef ADB_PSW_BIT
85 // Weak pull-up
86 ADB_PORT |= (1<<ADB_PSW_BIT);
87 ADB_DDR &= ~(1<<ADB_PSW_BIT);
88 #endif
89}
90
91bool adb_host_psw( void )
92{
93 #ifdef ADB_PSW_BIT
94 return (ADB_PIN & (1<<ADB_PSW_BIT)) != 0;
95 #else
96 return true;
97 #endif
98}
99
100// Waits while data == val, or until us timeout expires. Returns remaining time,
101// zero if timed out.
102static byte while_data( byte us, byte data )
103{
104 while ( data_in() == data )
105 {
106 delay_loop_usec( 1 /* us period */, 7 /* cycles loop overhead */ );
107 if ( !--us )
108 break;
109 }
110 return us;
111}
112
113static byte while_lo( byte us ) { return while_data( us, 0 ); }
114static byte while_hi( byte us ) { return while_data( us, data_mask ); }
115
116static uint16_t adb_host_talk( byte cmd )
117{
118 command( cmd );
119 _delay_us( 5 );
120 if ( !while_hi( 260 - 5 ) ) // avg 160
121 return adb_host_nothing;
122
123 // Receive start bit and 16 data bits.
124 // Doing them all in loop allows consistent error checking
125 uint16_t data = 0;
126 byte n = 17;
127 do
128 {
129 data <<= 1;
130 enum { timeout = 130 }; // maximum bit cell time
131
132 byte lo = while_lo( timeout );
133 if ( !lo )
134 goto error; // timeout
135
136 byte hi = while_hi( lo );
137 if ( !hi )
138 goto error; // timeout
139
140 if ( timeout-lo < lo-hi )
141 data |= 1;
142 else if ( n == 17 )
143 goto error; // start bit is wrong
144 }
145 while ( --n );
146
147 // duration must be split in two due to 255 limit
148 if ( !while_lo( 255 ) && !while_lo( 351 - 255 ) )
149 goto error;
150
151 if ( while_hi( 91 ) )
152 goto error;
153
154 return data;
155
156error:
157 return adb_host_error;
158}
159
160uint16_t adb_host_kbd_recv( void )
161{
162 return adb_host_talk( adb_cmd_read + 0 );
163}
164
165uint16_t adb_host_kbd_modifiers( void )
166{
167 return adb_host_talk( adb_cmd_read + 2 );
168}
169
170void adb_host_listen( byte cmd, byte data_h, byte data_l )
171{
172 command( cmd );
173 _delay_us( 200 );
174
175 place_bit1();
176 send_byte( data_h );
177 send_byte( data_l );
178 place_bit0();
179}
180
181void adb_host_kbd_led( byte led )
182{
183 adb_host_listen( adb_cmd_write + 2, 0, led & 0x07 );
184}
185
186/* This software is licensed with a Modified BSD License.
187All of this is supposed to be Free Software, Open Source, DFSG-free,
188GPL-compatible, and OK to use in both free and proprietary applications.
189Additions and corrections to this file are welcome.
190
191Redistribution and use in source and binary forms, with or without
192modification, are permitted provided that the following conditions are met:
193
194* Redistributions of source code must retain the above copyright
195 notice, this list of conditions and the following disclaimer.
196
197* Redistributions in binary form must reproduce the above copyright
198 notice, this list of conditions and the following disclaimer in
199 the documentation and/or other materials provided with the
200 distribution.
201
202* Neither the name of the copyright holders nor the names of
203 contributors may be used to endorse or promote products derived
204 from this software without specific prior written permission.
205
206THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
207AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
208IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
212SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
213INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
214CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
215ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
216POSSIBILITY OF SUCH DAMAGE. */
diff --git a/converter/adb_usb/adb_blargg.h b/converter/adb_usb/adb_blargg.h
deleted file mode 100644
index 2542cb549..000000000
--- a/converter/adb_usb/adb_blargg.h
+++ /dev/null
@@ -1,38 +0,0 @@
1// Basic support for ADB keyboard
2
3#ifndef ADB_BLARGG_H
4#define ADB_BLARGG_H
5
6#include <stdint.h>
7#include <stdbool.h>
8
9// Sets up ADB bus. Doesn't send anything to keyboard.
10void adb_host_init( void );
11
12// Receives key press event from keyboard.
13// 0xKKFF: one key changed state
14// 0xKKKK: two keys changed state
15enum { adb_host_nothing = 0 }; // no keys changed state
16enum { adb_host_error = 0xFFFE }; // receive error
17uint16_t adb_host_kbd_recv( void );
18
19// Current state of keyboard modifiers and a few other keys
20// Returns adb_host_nothing if keyboard didn't respond.
21// Returns adb_host_error if error receiving.
22uint16_t adb_host_kbd_modifiers( void );
23
24// Sends command and two bytes of data to keyboard
25void adb_host_listen( uint8_t cmd, uint8_t data_h, uint8_t data_l );
26
27// Sets keyboard LEDs. Note that bits are inverted here, so 1 means off, 0 means on.
28void adb_host_kbd_led( uint8_t led );
29
30// State of power switch (false = pressed), or true if unsupported
31bool adb_host_psw( void );
32
33
34// Legacy support
35#define ADB_POWER 0x7F
36#define ADB_CAPS 0x39
37
38#endif
diff --git a/converter/adb_usb/led.c b/converter/adb_usb/led.c
index 1e7911f94..3ee64a8e7 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/>.
23 23
24void led_set(uint8_t usb_led) 24void led_set(uint8_t usb_led)
25{ 25{
26 // need a wait to send command without miss
27 _delay_ms(100);
28 adb_host_kbd_led(~usb_led); 26 adb_host_kbd_led(~usb_led);
29} 27}
diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c
index 54be2b0f5..d1b67d38d 100644
--- a/converter/adb_usb/matrix.c
+++ b/converter/adb_usb/matrix.c
@@ -79,9 +79,9 @@ void matrix_init(void)
79 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; 79 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
80 80
81 debug_enable = true; 81 debug_enable = true;
82 debug_matrix = true; 82 //debug_matrix = true;
83 debug_keyboard = true; 83 //debug_keyboard = true;
84 debug_mouse = true; 84 //debug_mouse = true;
85 print("debug enabled.\n"); 85 print("debug enabled.\n");
86 return; 86 return;
87} 87}
@@ -108,7 +108,7 @@ uint8_t matrix_scan(void)
108 } else if (codes == 0xFFFF) { // power key release 108 } else if (codes == 0xFFFF) { // power key release
109 register_key(0xFF); 109 register_key(0xFF);
110 } else if (key0 == 0xFF) { // error 110 } else if (key0 == 0xFF) { // error
111 xprintf("adb_host_kbd_recv: ERROR(%02X)\n", codes); 111 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
112 return key1; 112 return key1;
113 } else { 113 } else {
114 register_key(key0); 114 register_key(key0);
diff --git a/protocol/adb.c b/protocol/adb.c
index 750f4b965..a4783f36e 100644
--- a/protocol/adb.c
+++ b/protocol/adb.c
@@ -41,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE.
41#include <avr/io.h> 41#include <avr/io.h>
42#include <avr/interrupt.h> 42#include <avr/interrupt.h>
43#include "adb.h" 43#include "adb.h"
44#include "debug.h"
45 44
46 45
47// GCC doesn't inline functions normally 46// GCC doesn't inline functions normally
@@ -125,16 +124,15 @@ bool adb_host_psw(void)
125uint16_t adb_host_kbd_recv(void) 124uint16_t adb_host_kbd_recv(void)
126{ 125{
127 uint16_t data = 0; 126 uint16_t data = 0;
127 cli();
128 attention(); 128 attention();
129 send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00) 129 send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00)
130 place_bit0(); // Stopbit(0) 130 place_bit0(); // Stopbit(0)
131 if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us) 131 if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
132 sei();
132 return 0; // No data to send 133 return 0; // No data to send
133 } 134 }
134 135
135 // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck
136 // TODO: is this needed anymore with improved timing?
137 //cli();
138 uint8_t n = 17; // start bit + 16 data bits 136 uint8_t n = 17; // start bit + 16 data bits
139 do { 137 do {
140 uint8_t lo = (uint8_t) wait_data_hi(130); 138 uint8_t lo = (uint8_t) wait_data_hi(130);
@@ -153,10 +151,8 @@ uint16_t adb_host_kbd_recv(void)
153 data |= 1; 151 data |= 1;
154 } 152 }
155 else if (n == 17) { 153 else if (n == 17) {
156 // Service Request
157 dprintf("Startbit ERROR\n");
158 sei(); 154 sei();
159 return -2; 155 return -20;
160 } 156 }
161 } 157 }
162 while ( --n ); 158 while ( --n );
@@ -164,21 +160,20 @@ uint16_t adb_host_kbd_recv(void)
164 // Stop bit can't be checked normally since it could have service request lenghtening 160 // Stop bit can't be checked normally since it could have service request lenghtening
165 // and its high state never goes low. 161 // and its high state never goes low.
166 if (!wait_data_hi(351) || wait_data_lo(91)) { 162 if (!wait_data_hi(351) || wait_data_lo(91)) {
167 dprintf("Stopbit ERROR\n");
168 sei(); 163 sei();
169 return -3; 164 return -21;
170 } 165 }
171 sei(); 166 sei();
172 return data; 167 return data;
173 168
174error: 169error:
175 dprintf("Bit ERROR\n");
176 sei(); 170 sei();
177 return -4; 171 return -n;
178} 172}
179 173
180void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l) 174void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
181{ 175{
176 cli();
182 attention(); 177 attention();
183 send_byte(cmd); 178 send_byte(cmd);
184 place_bit0(); // Stopbit(0) 179 place_bit0(); // Stopbit(0)
@@ -187,6 +182,7 @@ void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
187 send_byte(data_h); 182 send_byte(data_h);
188 send_byte(data_l); 183 send_byte(data_l);
189 place_bit0(); // Stopbit(0); 184 place_bit0(); // Stopbit(0);
185 sei();
190} 186}
191 187
192// send state of LEDs 188// send state of LEDs