From c18c52f551545b46a28902c69730eefbdb75577d Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 14 Oct 2013 23:37:05 +0900 Subject: [PATCH] Merge blargg's fix into adb.c - --- converter/adb_usb/led.c | 5 +- converter/adb_usb/matrix.c | 7 ++ protocol/adb.c | 208 +++++++++++++++++-------------------- 3 files changed, 108 insertions(+), 112 deletions(-) diff --git a/converter/adb_usb/led.c b/converter/adb_usb/led.c index 0e162f37..1e7911f9 100644 --- a/converter/adb_usb/led.c +++ b/converter/adb_usb/led.c @@ -15,12 +15,15 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "stdint.h" +#include +#include #include "adb.h" #include "led.h" 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 7e585697..54be2b0f 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -67,6 +67,13 @@ uint8_t matrix_cols(void) void matrix_init(void) { adb_host_init(); + // wait for keyboard to boot up and receive command + _delay_ms(1000); + // Enable keyboard left/right modifier distinction + // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11) + // upper byte: reserved bits 0000, device address 0010 + // lower byte: device handler 00000011 + adb_host_listen(0x2B,0x02,0x03); // initialize matrix state: all keys off for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; diff --git a/protocol/adb.c b/protocol/adb.c index f706255a..750f4b96 100644 --- a/protocol/adb.c +++ b/protocol/adb.c @@ -1,5 +1,6 @@ /* Copyright 2011 Jun WAKO +Copyright 2013 Shay Green This software is licensed with a Modified BSD License. All of this is supposed to be Free Software, Open Source, DFSG-free, @@ -43,9 +44,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "debug.h" -static inline void data_lo(void); -static inline void data_hi(void); -static inline bool data_in(void); +// GCC doesn't inline functions normally +#define data_lo() (ADB_DDR |= (1< * */ + +// ADB Bit Cells +// +// bit cell time: 70-130us +// low part of bit0: 60-70% of bit cell +// low part of bit1: 30-40% of bit cell +// +// bit cell time 70us 130us +// -------------------------------------------- +// low part of bit0 42-49 78-91 +// high part of bit0 21-28 39-52 +// low part of bit1 21-28 39-52 +// high part of bit1 42-49 78-91 +// +// +// bit0: +// 70us bit cell: +// ____________~~~~~~ +// 42-49 21-28 +// +// 130us bit cell: +// ____________~~~~~~ +// 78-91 39-52 +// +// bit1: +// 70us bit cell: +// ______~~~~~~~~~~~~ +// 21-28 42-49 +// +// 130us bit cell: +// ______~~~~~~~~~~~~ +// 39-52 78-91 +// +// [from Apple IIgs Hardware Reference Second Edition] + uint16_t adb_host_kbd_recv(void) { uint16_t data = 0; @@ -100,24 +131,50 @@ uint16_t adb_host_kbd_recv(void) if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us) return 0; // No data to send } - if (!read_bit()) { // Startbit(1) - // Service Request - dprintf("Startbit ERROR\n"); - return -2; - } - + // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck - cli(); - data = read_byte(); - data = (data<<8) | read_byte(); - uint8_t stop = read_bit(); // Stopbit(0) - sei(); + // 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); + if (!lo) + goto error; + + uint8_t hi = (uint8_t) wait_data_lo(lo); + if (!hi) + goto error; + + hi = lo - hi; + lo = 130 - lo; + + data <<= 1; + if (lo < hi) { + data |= 1; + } + else if (n == 17) { + // Service Request + dprintf("Startbit ERROR\n"); + sei(); + return -2; + } + } + while ( --n ); - if (stop) { + // 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; } + sei(); return data; + +error: + dprintf("Bit ERROR\n"); + sei(); + return -4; } void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l) @@ -142,23 +199,6 @@ void adb_host_kbd_led(uint8_t led) } -static inline void data_lo() -{ - ADB_DDR |= (1<