X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=protocol%2Fadb.c;h=bbff66df03613bdd3d54232a53ad7a9dee64eb28;hb=703ca9de213955443f8713901cc96d482633931f;hp=155d223fe7ed1f0c438a2ef5226d7b4f88c5ea7e;hpb=e8e8f93bcf0cdc8d72d35662db5a1da4716762b8;p=max%2Ftmk_keyboard.git diff --git a/protocol/adb.c b/protocol/adb.c index 155d223f..bbff66df 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, @@ -40,12 +41,13 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include "adb.h" -#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< + * Thanks a lot, blargg! + * + * */ + +// 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; + cli(); attention(); send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00) place_bit0(); // Stopbit(0) + if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored + sei(); + return -30; // something wrong + } if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us) + sei(); return 0; // No data to send } - if (!read_bit()) { // Startbit(1) - // Service Request - dprintf("Startbit ERROR\n"); - return -2; + + 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) { + sei(); + return -20; + } } + while ( --n ); - // 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(); - - if (stop) { - dprintf("Stopbit ERROR\n"); - return -3; + // 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)) { + sei(); + return -21; } + sei(); return data; + +error: + sei(); + 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) @@ -128,6 +186,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 @@ -140,23 +199,6 @@ void adb_host_kbd_led(uint8_t led) } -static inline void data_lo() -{ - ADB_DDR |= (1<