[Keyboard] Update adb_usb converter to non-avr specific code

This commit is contained in:
Drashna Jael're 2023-10-24 11:41:31 -07:00
parent a1539ccaf6
commit 220ac82af5
No known key found for this signature in database
GPG Key ID: DBA1FD3A860D1B11
7 changed files with 139 additions and 142 deletions

View File

@ -37,23 +37,36 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdbool.h>
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "converter/adb_usb/config.h"
#include "gpio.h"
#include "wait.h"
#include "adb.h"
#include "print.h"
// GCC doesn't inline functions normally
#define data_lo() (ADB_DDR |= (1 << ADB_DATA_BIT))
#define data_hi() (ADB_DDR &= ~(1 << ADB_DATA_BIT))
#define data_in() (ADB_PIN & (1 << ADB_DATA_BIT))
#define data_lo() setPinOutput(ADB_DATA_PIN)
#define data_hi() setPinInput(ADB_DATA_PIN)
#define data_in() readPin(ADB_DATA_PIN)
#ifdef ADB_PSW_BIT
#ifdef ADB_PSW_PIN
static inline void psw_lo(void);
static inline void psw_hi(void);
static inline bool psw_in(void);
#endif
#ifdef __AVR__
# include <avr/io.h>
# include <avr/interrupt.h>
# define disable_interrupts() cli()
# define enable_interrupts() sei()
# define CLOCK_SPEED F_CPU
#else
# define disable_interrupts() chSysLock()
# define enable_interrupts() chSysUnlock()
# define CLOCK_SPEED CPU_CLOCK
#endif
static inline void attention(void);
static inline void place_bit0(void);
static inline void place_bit1(void);
@ -62,15 +75,17 @@ static inline uint16_t wait_data_lo(uint16_t us);
static inline uint16_t wait_data_hi(uint16_t us);
void adb_host_init(void) {
ADB_PORT &= ~(1 << ADB_DATA_BIT);
writePinLow(ADB_DATA_PIN);
data_hi();
#ifdef ADB_PSW_BIT
#ifdef ADB_PSW_PIN
psw_hi();
#endif
}
#ifdef ADB_PSW_BIT
bool adb_host_psw(void) { return psw_in(); }
#ifdef ADB_PSW_PIN
bool adb_host_psw(void) {
return psw_in();
}
#endif
/*
@ -81,17 +96,22 @@ bool adb_host_psw(void) { return psw_in(); }
* <http://geekhack.org/index.php?topic=14290.msg1068919#msg1068919>
* <http://geekhack.org/index.php?topic=14290.msg1070139#msg1070139>
*/
uint16_t adb_host_kbd_recv(void) { return adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_0); }
uint16_t adb_host_kbd_recv(void) {
return adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_0);
}
#ifdef ADB_MOUSE_ENABLE
uint16_t adb_host_mouse_recv(void) { return adb_host_talk(ADB_ADDR_MOUSE, ADB_REG_0); }
uint16_t adb_host_mouse_recv(void) {
return adb_host_talk(ADB_ADDR_MOUSE, ADB_REG_0);
}
#endif
// This sends Talk command to read data from register and returns length of the data.
uint8_t adb_host_talk_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len) {
for (int8_t i = 0; i < len; i++) buf[i] = 0;
for (int8_t i = 0; i < len; i++)
buf[i] = 0;
cli();
disable_interrupts();
attention();
send_byte((addr << 4) | ADB_CMD_TALK | reg);
place_bit0(); // Stopbit(0)
@ -141,23 +161,23 @@ uint8_t adb_host_talk_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len)
// http://ww1.microchip.com/downloads/en/AppNotes/00591b.pdf
if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored
xprintf("R");
sei();
enable_interrupts();
return 0;
}
if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
sei();
enable_interrupts();
return 0; // No data from device(not error);
}
// start bit(1)
if (!wait_data_hi(40)) {
xprintf("S");
sei();
enable_interrupts();
return 0;
}
if (!wait_data_lo(100)) {
xprintf("s");
sei();
enable_interrupts();
return 0;
}
@ -187,7 +207,7 @@ uint8_t adb_host_talk_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len)
} while (++n);
error:
sei();
enable_interrupts();
return n / 8;
}
@ -200,19 +220,19 @@ uint16_t adb_host_talk(uint8_t addr, uint8_t reg) {
}
void adb_host_listen_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len) {
cli();
disable_interrupts();
attention();
send_byte((addr << 4) | ADB_CMD_LISTEN | reg);
place_bit0(); // Stopbit(0)
// TODO: Service Request
_delay_us(200); // Tlt/Stop to Start
wait_us(200); // Tlt/Stop to Start
place_bit1(); // Startbit(1)
for (int8_t i = 0; i < len; i++) {
send_byte(buf[i]);
// xprintf("%02X ", buf[i]);
}
place_bit0(); // Stopbit(0);
sei();
enable_interrupts();
}
void adb_host_listen(uint8_t addr, uint8_t reg, uint8_t data_h, uint8_t data_l) {
@ -221,12 +241,12 @@ void adb_host_listen(uint8_t addr, uint8_t reg, uint8_t data_h, uint8_t data_l)
}
void adb_host_flush(uint8_t addr) {
cli();
disable_interrupts();
attention();
send_byte((addr << 4) | ADB_CMD_FLUSH);
place_bit0(); // Stopbit(0)
_delay_us(200); // Tlt/Stop to Start
sei();
wait_us(200); // Tlt/Stop to Start
enable_interrupts();
}
// send state of LEDs
@ -237,40 +257,38 @@ void adb_host_kbd_led(uint8_t led) {
adb_host_listen(ADB_ADDR_KEYBOARD, ADB_REG_2, 0, led & 0x07);
}
#ifdef ADB_PSW_BIT
#ifdef ADB_PSW_PIN
static inline void psw_lo(void) {
ADB_DDR |= (1 << ADB_PSW_BIT);
ADB_PORT &= ~(1 << ADB_PSW_BIT);
setPinOutput(ADB_PSW_PIN);
writePinLow(ADB_PSW_PIN);
}
static inline void psw_hi(void) {
ADB_PORT |= (1 << ADB_PSW_BIT);
ADB_DDR &= ~(1 << ADB_PSW_BIT);
setPinInputHigh(ADB_PSW_PIN);
}
static inline bool psw_in(void) {
ADB_PORT |= (1 << ADB_PSW_BIT);
ADB_DDR &= ~(1 << ADB_PSW_BIT);
return ADB_PIN & (1 << ADB_PSW_BIT);
setPinInputHigh(ADB_PSW_PIN);
return readPin(ADB_PSW_PIN);
}
#endif
static inline void attention(void) {
data_lo();
_delay_us(800 - 35); // bit1 holds lo for 35 more
wait_us(800 - 35); // bit1 holds lo for 35 more
place_bit1();
}
static inline void place_bit0(void) {
data_lo();
_delay_us(65);
wait_us(65);
data_hi();
_delay_us(35);
wait_us(35);
}
static inline void place_bit1(void) {
data_lo();
_delay_us(35);
wait_us(35);
data_hi();
_delay_us(65);
wait_us(65);
}
static inline void send_byte(uint8_t data) {
@ -282,12 +300,16 @@ static inline void send_byte(uint8_t data) {
}
}
static inline void adb_wait(void) {
wait_us(1 - (6 * 1000000.0 / CLOCK_SPEED));
}
// These are carefully coded to take 6 cycles of overhead.
// inline asm approach became too convoluted
static inline uint16_t wait_data_lo(uint16_t us) {
do {
if (!data_in()) break;
_delay_us(1 - (6 * 1000000.0 / F_CPU));
adb_wait();
} while (--us);
return us;
}
@ -295,7 +317,7 @@ static inline uint16_t wait_data_lo(uint16_t us) {
static inline uint16_t wait_data_hi(uint16_t us) {
do {
if (data_in()) break;
_delay_us(1 - (6 * 1000000.0 / F_CPU));
adb_wait();
} while (--us);
return us;
}

View File

@ -40,10 +40,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <stdint.h>
#include <stdbool.h>
#if !(defined(ADB_PORT) && defined(ADB_PIN) && defined(ADB_DDR) && defined(ADB_DATA_BIT))
# error "ADB port setting is required in config.h"
#endif
#define ADB_POWER 0x7F
#define ADB_CAPS 0x39

View File

@ -24,8 +24,5 @@ Ported to QMK by Peter Roe <pete@13bit.me>
#define MATRIX_COLS 8 // keycode bit: 6-4
/* ADB port setting */
#define ADB_PORT PORTD
#define ADB_PIN PIND
#define ADB_DDR DDRD
#define ADB_DATA_BIT 0
//#define ADB_PSW_BIT 1 // optional
#define ADB_DATA_PIN D0
// #define ADB_PSW_PIN D1

View File

@ -16,12 +16,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <util/delay.h>
#include "adb.h"
#include "led.h"
void led_set(uint8_t usb_led)
{
void led_set(uint8_t usb_led) {
adb_host_kbd_led(~usb_led);
}

View File

@ -19,9 +19,8 @@ Ported to QMK by Peter Roe <pete@13bit.me>
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <util/delay.h>
#include "print.h"
#include "wait.h"
#include "util.h"
#include "debug.h"
#include "adb.h"
@ -42,33 +41,27 @@ static matrix_row_t matrix[MATRIX_ROWS];
static void register_key(uint8_t key);
__attribute__ ((weak))
void matrix_init_kb(void) {
__attribute__((weak)) void matrix_init_kb(void) {
matrix_init_user();
}
__attribute__ ((weak))
void matrix_scan_kb(void) {
__attribute__((weak)) void matrix_scan_kb(void) {
matrix_scan_user();
}
__attribute__ ((weak))
void matrix_init_user(void) {
}
__attribute__((weak)) void matrix_init_user(void) {}
__attribute__ ((weak))
void matrix_scan_user(void) {
}
__attribute__((weak)) void matrix_scan_user(void) {}
void matrix_init(void)
{
void matrix_init(void) {
adb_host_init();
// wait for keyboard to boot up and receive command
_delay_ms(2000);
wait_ms(2000);
// initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
for (uint8_t i = 0; i < MATRIX_ROWS; i++)
matrix[i] = 0x00;
// debug_enable = true;
// debug_matrix = true;
@ -80,20 +73,13 @@ void matrix_init(void)
}
#ifdef ADB_MOUSE_ENABLE
#ifdef MAX
#undef MAX
#endif
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
static report_mouse_t mouse_report = {};
void housekeeping_task_kb(void) {
adb_mouse_task();
}
void adb_mouse_task(void)
{
void adb_mouse_task(void) {
uint16_t codes;
int16_t x, y;
static int8_t mouseacc;
@ -112,20 +98,16 @@ void adb_mouse_task(void)
return;
};
// Bit sixteen is button.
if (~codes & (1 << 15))
mouse_report.buttons |= MOUSE_BTN1;
if (codes & (1 << 15))
mouse_report.buttons &= ~MOUSE_BTN1;
if (~codes & (1 << 15)) mouse_report.buttons |= MOUSE_BTN1;
if (codes & (1 << 15)) mouse_report.buttons &= ~MOUSE_BTN1;
// lower seven bits are movement, as signed int_7.
// low byte is X-axis, high byte is Y.
y = (codes >> 8 & 0x3F);
x = (codes >> 0 & 0x3F);
// bit seven and fifteen is negative
// usb does not use int_8, but int_7 (measuring distance) with sign-bit.
if (codes & (1 << 6))
x = (x-0x40);
if (codes & (1 << 14))
y = (y-0x40);
if (codes & (1 << 6)) x = (x - 0x40);
if (codes & (1 << 14)) y = (y - 0x40);
// Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
x *= mouseacc;
y *= mouseacc;
@ -135,12 +117,18 @@ void adb_mouse_task(void)
mouse_report.x = -MAX(-MAX(x, -127), -127);
mouse_report.y = -MAX(-MAX(y, -127), -127);
if (debug_mouse) {
print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
print("adb_host_mouse_recv: ");
print_bin16(codes);
print("\n");
print("adb_mouse raw: [");
print_hex8(mouseacc); print(" ");
print_hex8(mouse_report.buttons); print("|");
print_decs(mouse_report.x); print(" ");
print_decs(mouse_report.y); print("]\n");
print_hex8(mouseacc);
print(" ");
print_hex8(mouse_report.buttons);
print("|");
print_decs(mouse_report.x);
print(" ");
print_decs(mouse_report.y);
print("]\n");
}
// Send result by usb.
host_mouse_send(&mouse_report);
@ -150,8 +138,7 @@ void adb_mouse_task(void)
}
#endif
uint8_t matrix_scan(void)
{
uint8_t matrix_scan(void) {
/* extra_key is volatile and more convoluted than necessary because gcc refused
to generate valid code otherwise. Making extra_key uint8_t and constructing codes
here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
@ -168,8 +155,7 @@ uint8_t matrix_scan(void)
codes = extra_key;
extra_key = 0xFFFF;
if ( codes == 0xFFFF )
{
if (codes == 0xFFFF) {
// polling with 12ms interval
if (timer_elapsed(tick_ms) < 12) return 0;
tick_ms = timer_read();
@ -181,7 +167,9 @@ uint8_t matrix_scan(void)
key1 = codes & 0xFF;
if (debug_matrix && codes) {
print("adb_host_kbd_recv: "); print_hex16(codes); print("\n");
print("adb_host_kbd_recv: ");
print_hex16(codes);
print("\n");
}
if (codes == 0) { // no keys
@ -245,19 +233,13 @@ uint8_t matrix_scan(void)
return 1;
}
void matrix_print(void){
void matrix_print(void) {}
}
inline
matrix_row_t matrix_get_row(uint8_t row)
{
inline matrix_row_t matrix_get_row(uint8_t row) {
return matrix[row];
}
inline
static void register_key(uint8_t key)
{
inline static void register_key(uint8_t key) {
uint8_t col, row;
col = key & 0x07;
row = (key >> 3) & 0x0F;

View File

@ -0,0 +1,5 @@
ifeq ($(strip $(ADB_MOUSE_ENABLE)), yes)
OPT_DEFS += -DADB_MOUSE_ENABLE
MOUSE_ENABLE := yes
endif

View File

@ -2,6 +2,4 @@ CUSTOM_MATRIX = yes
SRC += matrix.c adb.c led.c
# OPT_DEFS += -DADB_MOUSE_ENABLE -DMOUSE_ENABLE
DEFAULT_FOLDER = converter/adb_usb/rev1