From 56e098d76e7d2a843a66f3a6e409f1d38fe7feac Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 13 Jan 2011 01:26:33 +0900 Subject: ADB to USB keyboard converter --- adb/Makefile | 82 +++++++++++++++++++++++++ adb/README | 19 ++++++ adb/config.h | 47 +++++++++++++++ adb/keymap.c | 135 +++++++++++++++++++++++++++++++++++++++++ adb/matrix.c | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 477 insertions(+) create mode 100644 adb/Makefile create mode 100644 adb/README create mode 100644 adb/config.h create mode 100644 adb/keymap.c create mode 100644 adb/matrix.c (limited to 'adb') diff --git a/adb/Makefile b/adb/Makefile new file mode 100644 index 000000000..c27c75e15 --- /dev/null +++ b/adb/Makefile @@ -0,0 +1,82 @@ +# Hey Emacs, this is a -*- makefile -*- +#---------------------------------------------------------------------------- +# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al. +# +# Released to the Public Domain +# +# Additional material for this makefile was written by: +# Peter Fleury +# Tim Henigan +# Colin O'Flynn +# Reiner Patommel +# Markus Pfaff +# Sander Pool +# Frederik Rouleau +# Carlos Lamas +# +#---------------------------------------------------------------------------- +# 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, using avrdude. +# Please customize the avrdude settings below first! +# +# 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 = tmk_adb + +# Directory common source filess exist +COMMON_DIR = .. + +# Directory keyboard dependent files exist +TARGET_DIR = . + +# keyboard dependent files +TARGET_SRC = keymap.c \ + matrix.c \ + adb.c + + +# MCU name, you MUST set this to match the board you are using +# type "make clean" after changing this, so all files will be rebuilt +#MCU = at90usb162 # Teensy 1.0 +MCU = atmega32u4 # Teensy 2.0 +#MCU = at90usb646 # Teensy++ 1.0 +#MCU = at90usb1286 # Teensy++ 2.0 + + +# Processor frequency. +# Normally the first thing your program should do is set the clock prescaler, +# so your program will run at the correct speed. You should also set this +# variable to same clock speed. The _delay_ms() macro uses this, and many +# examples use this variable to calculate timings. Do not add a "UL" here. +F_CPU = 16000000 + + +# Build Options +# comment out to disable the options. +# +MOUSEKEY_ENABLE = yes # Mouse keys +#PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support +USB_EXTRA_ENABLE = yes # Enhanced feature for Windows(Audio control and System control) +#USB_NKRO_ENABLE = yes # USB Nkey Rollover + + +include $(COMMON_DIR)/Makefile.common diff --git a/adb/README b/adb/README new file mode 100644 index 000000000..19d9f8fa1 --- /dev/null +++ b/adb/README @@ -0,0 +1,19 @@ +ADB to USB keyboard converter +============================= + +This firmware converts ADB keyboard protocol to USB. + +Build +----- +0. Connect ADB keyboard to Teensy by 3 lines(Vcc, GND, Data). + PSW line is optional. See ADB.txt for details. +1. Define following macros for ADB connection in config.h: + ADB_PORT + ADB_PIN + ADB_DDR + ADB_DATA_BIT + ADB_PSW_BIT +2. make +3. program Teensy. + +EOF diff --git a/adb/config.h b/adb/config.h new file mode 100644 index 000000000..099a24bfa --- /dev/null +++ b/adb/config.h @@ -0,0 +1,47 @@ +#ifndef CONFIG_H +#define CONFIG_H + +/* controller configuration */ +#include "controller_teensy.h" + +#define VENDOR_ID 0xFEED +#define PRODUCT_ID 0x0ADB +#define MANUFACTURER t.m.k. +#define PRODUCT ADB keyboard converter +#define DESCRIPTION convert ADB keyboard to USB + +/* matrix size */ +#define MATRIX_ROWS 16 // keycode bit: 3-0 +#define MATRIX_COLS 8 // keycode bit: 6-4 +/* define if matrix has ghost */ +//#define MATRIX_HAS_GHOST + +/* USB NKey Rollover */ +#ifdef USB_NKRO_ENABLE +#endif + +/* mouse keys */ +#ifdef MOUSEKEY_ENABLE +# define MOUSEKEY_DELAY_TIME 192 +#endif + +/* PS/2 mouse */ +#ifdef PS2_MOUSE_ENABLE +# define PS2_CLOCK_PORT PORTF +# define PS2_CLOCK_PIN PINF +# define PS2_CLOCK_DDR DDRF +# define PS2_CLOCK_BIT 0 +# define PS2_DATA_PORT PORTF +# define PS2_DATA_PIN PINF +# define PS2_DATA_DDR DDRF +# define PS2_DATA_BIT 1 +#endif + +/* ADB port setting */ +#define ADB_PORT PORTF +#define ADB_PIN PINF +#define ADB_DDR DDRF +#define ADB_DATA_BIT 0 +//#define ADB_PSW_BIT 1 // optional + +#endif diff --git a/adb/keymap.c b/adb/keymap.c new file mode 100644 index 000000000..8a9640482 --- /dev/null +++ b/adb/keymap.c @@ -0,0 +1,135 @@ +/* + * Keymap for ADB keyboard + */ +#include +#include +#include +#include "usb_keyboard.h" +#include "usb_keycodes.h" +#include "print.h" +#include "debug.h" +#include "util.h" +#include "keymap_skel.h" + + +// Convert physical keyboard layout to matrix array. +// This is a macro to define keymap easily in keyboard layout form. + +// TODO: ADB to USB default keymap +// TODO: keymap macro template for m0116/m0115 +/* Apple Keyboard m0116 + K7F, \ + K35, K12, K13, K14, K15, K17, K16, K1A, K1C, K19, K1D, K1B, K18, K33, K47, K51, K4B, K43, \ + K30, K0C, K0D, K0E, K0F, K10, K11, K20, K22, K1F, K23, K21, K1E, K59, K5B, K5C, K4E, \ + K36, K00, K01, K02, K03, K05, K04, K26, K28, K25, K29, K27, K24, K56, K57, K58, K45, \ + K38, K06, K07, K08, K09, K0B, K2D, K2E, K2B, K2F, K2C, K7B, K53, K54, K55, \ + K39, K3A, K37, K32, K31, K2A, K3B, K3C, K3D, K3E, K52, K41, K4C \ +*/ + +/* no tenkey + K7F, \ + K35, K12, K13, K14, K15, K17, K16, K1A, K1C, K19, K1D, K1B, K18, K33, \ + K30, K0C, K0D, K0E, K0F, K10, K11, K20, K22, K1F, K23, K21, K1E, \ + K36, K00, K01, K02, K03, K05, K04, K26, K28, K25, K29, K27, K24, \ + K38, K06, K07, K08, K09, K0B, K2D, K2E, K2B, K2F, K2C, K7B, \ + K39, K3A, K37, K32, K31, K2A, K3B, K3C, K3D, K3E \ +*/ +#define KEYMAP( \ + K7F, \ + K35, K12, K13, K14, K15, K17, K16, K1A, K1C, K19, K1D, K1B, K18, K33, \ + K30, K0C, K0D, K0E, K0F, K10, K11, K20, K22, K1F, K23, K21, K1E, \ + K36, K00, K01, K02, K03, K05, K04, K26, K28, K25, K29, K27, K24, \ + K38, K06, K07, K08, K09, K0B, K2D, K2E, K2B, K2F, K2C, K7B, \ + K39, K3A, K37, K32, K31, K2A, K3B, K3C, K3D, K3E \ +) { \ + { K00, K01, K02, K03, K04, K05, K06, K07 }, \ + { K08, K09, 000, K0B, K0C, K0D, K0E, K0F }, \ + { K10, K11, K12, K13, K14, K15, K16, K17 }, \ + { K18, K19, K1A, K1B, K1C, K1D, K1E, K1F }, \ + { K20, K21, K22, K23, K24, K25, K26, K27 }, \ + { K28, K29, K2A, K2B, K2C, K2D, K2E, K2F }, \ + { K30, K31, K32, K33, 000, K35, K36, K37 }, \ + { K38, K39, K3A, K3B, K3C, K3D, K3E, 000 }, \ + { 000, 000, 000, 000, 000, 000, 000, 000 }, \ + { 000, 000, 000, 000, 000, 000, 000, 000 }, \ + { 000, 000, 000, 000, 000, 000, 000, 000 }, \ + { 000, 000, 000, 000, 000, 000, 000, 000 }, \ + { 000, 000, 000, 000, 000, 000, 000, 000 }, \ + { 000, 000, 000, 000, 000, 000, 000, 000 }, \ + { 000, 000, 000, 000, 000, 000, 000, 000 }, \ + { 000, 000, 000, K7B, 000, 000, 000, K7F }, \ +} + +#define KEYCODE(layer, row, col) (pgm_read_byte(&keymaps[(layer)][(row)][(col)])) + + +// Assign Fn key(0-7) to a layer to which switch with the Fn key pressed. +static const uint8_t PROGMEM fn_layer[] = { + 0, // FN_0 + 0, // FN_1 + 0, // FN_2 + 0, // FN_3 + 0, // FN_4 + 0, // FN_5 + 0, // FN_6 + 0 // FN_7 +}; + +// Assign Fn key(0-7) to a keycode sent when release Fn key without use of the layer. +// See layer.c for details. +static const uint8_t PROGMEM fn_keycode[] = { + KB_NO, // FN_0 + KB_NO, // FN_1 + KB_NO, // FN_2 + KB_NO, // FN_3 + KB_NO, // FN_4 + KB_NO, // FN_5 + KB_NO, // FN_6 + KB_NO // FN_7 +}; + +static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + /* Layer 0: Default Layer + * ,---------------------------------------------------------. + * |Esc| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backs| + * |---------------------------------------------------------| + * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| | + * |-----------------------------------------------------' | + * |Contro| A| S| D| F| G| H| J| K| L|Fn3| '|Return| + * |---------------------------------------------------------| + * |Shift | Z| X| C| V| B| N| M| ,| .| /|Shift | + * |---------------------------------------------------------| + * |Shi|Alt|Gui l `| | \|Lef|Rig|Dow|Up | + * `---------------------------------------------------------' + */ + KEYMAP(KB_PWR, \ + KB_ESC, KB_1, KB_2, KB_3, KB_4, KB_5, KB_6, KB_7, KB_8, KB_9, KB_0, KB_MINS,KB_EQL, KB_BSPC, \ + KB_TAB, KB_Q, KB_W, KB_E, KB_R, KB_T, KB_Y, KB_U, KB_I, KB_O, KB_P, KB_LBRC,KB_RBRC, \ + KB_LCTL,KB_A, KB_S, KB_D, KB_F, KB_G, KB_H, KB_J, KB_K, KB_L, KB_SCLN,KB_QUOT,KB_ENT, \ + KB_LSFT,KB_Z, KB_X, KB_C, KB_V, KB_B, KB_N, KB_M, KB_COMM,KB_DOT, KB_SLSH,KB_RSFT, \ + KB_LSFT,KB_LALT,KB_LGUI,KB_GRV, KB_SPC, KB_BSLS,KB_LEFT,KB_RGHT,KB_DOWN,KB_UP), + +}; + + +uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col) +{ + return KEYCODE(layer, row, col); +} + +uint8_t keymap_fn_layer(uint8_t fn_bits) +{ + return pgm_read_byte(&fn_layer[biton(fn_bits)]); +} + +uint8_t keymap_fn_keycode(uint8_t fn_bits) +{ + return pgm_read_byte(&fn_keycode[(biton(fn_bits))]); +} + +// define a condition to enter special function mode +bool keymap_is_special_mode(uint8_t fn_bits) +{ + //return (usb_keyboard_mods == (BIT_LCTRL | BIT_LSHIFT | BIT_LALT | BIT_LGUI)); + return (usb_keyboard_mods == (BIT_RSHIFT)); +} diff --git a/adb/matrix.c b/adb/matrix.c new file mode 100644 index 000000000..d0643371e --- /dev/null +++ b/adb/matrix.c @@ -0,0 +1,194 @@ +/* + * scan matrix + */ +#include +#include +#include +#include +#include "print.h" +#include "util.h" +#include "debug.h" +#include "adb.h" +#include "matrix_skel.h" + + +#if (MATRIX_COLS > 16) +# error "MATRIX_COLS must not exceed 16" +#endif +#if (MATRIX_ROWS > 255) +# error "MATRIX_ROWS must not exceed 255" +#endif + + +static bool _matrix_is_modified = false; + +// matrix state buffer(1:on, 0:off) +#if (MATRIX_COLS <= 8) +static uint8_t *matrix; +static uint8_t _matrix0[MATRIX_ROWS]; +#else +static uint16_t *matrix; +static uint16_t _matrix0[MATRIX_ROWS]; +#endif + +#ifdef MATRIX_HAS_GHOST +static bool matrix_has_ghost_in_row(uint8_t row); +#endif +static void _register_key(uint8_t key); + + +inline +uint8_t matrix_rows(void) +{ + return MATRIX_ROWS; +} + +inline +uint8_t matrix_cols(void) +{ + return MATRIX_COLS; +} + +void matrix_init(void) +{ + adb_host_init(); + + // initialize matrix state: all keys off + for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; + matrix = _matrix0; + + print_enable = true; + debug_enable = true; + debug_matrix = true; + debug_keyboard = true; + debug_mouse = true; + print("debug enabled.\n"); + return; +} + +uint8_t matrix_scan(void) +{ + uint16_t codes; + uint8_t key0, key1; + + _matrix_is_modified = false; + + codes = adb_host_kbd_recv(); + key0 = codes>>8; + key1 = codes&0xFF; + if (debug_matrix) { + //print("adb_host_kbd_recv: "); phex16(codes); print("\n"); + } + + if (codes == 0) { // no keys + return 0; + } else if (key0 == 0xFF && key1 != 0xFF) { // error + return codes&0xFF; + } else { + _matrix_is_modified = true; + _register_key(key0); + if (key1 != 0xFF) // key1 is 0xFF when no second key. + _register_key(key1); + } + + return 1; +} + +bool matrix_is_modified(void) +{ + return _matrix_is_modified; +} + +inline +bool matrix_has_ghost(void) +{ +#ifdef MATRIX_HAS_GHOST + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + if (matrix_has_ghost_in_row(i)) + return true; + } +#endif + return false; +} + +inline +bool matrix_is_on(uint8_t row, uint8_t col) +{ + return (matrix[row] & (1<>3)&0x0F; + if (key&0x80) { + matrix[row] &= ~(1<