From 068c31a7ba9fc6aea33f69c0edb30ad195c320ec Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 16 May 2011 00:08:06 +0900 Subject: [PATCH] added initial V-USB support for HHKB --- hhkb/Makefile.vusb | 83 ++++++++++ hhkb/config_vusb.h | 120 ++++++++++++++ hhkb/keymap.c | 30 +--- hhkb/matrix.c | 98 ++++++++---- hhkb/usbconfig.h | 378 +++++++++++++++++++++++++++++++++++++++++++++ ps2.c | 5 + vusb/host.c | 4 +- 7 files changed, 661 insertions(+), 57 deletions(-) create mode 100644 hhkb/Makefile.vusb create mode 100644 hhkb/config_vusb.h create mode 100644 hhkb/usbconfig.h diff --git a/hhkb/Makefile.vusb b/hhkb/Makefile.vusb new file mode 100644 index 0000000000..ea89a6f6a0 --- /dev/null +++ b/hhkb/Makefile.vusb @@ -0,0 +1,83 @@ +# Target file name (without extension). +TARGET = hhkb_vusb + +# Directory common source filess exist +COMMON_DIR = .. + +# Directory keyboard dependent files exist +TARGET_DIR = . + +# keyboard dependent files +TARGET_SRC = main_vusb.c \ + keymap.c \ + matrix.c \ + led.c + +CONFIG_H = config_vusb.h + + +# V-USB debug level: To use ps2_usart.c level must be 0 +# ps2_usart.c requires USART to receive PS/2 signal. +OPT_DEFS = -DDEBUG_LEVEL=0 + + +# 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 +MCU = atmega168 + + +# 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 = 20000000 + + +# Build Options +# comment out to disable the options. +# +MOUSEKEY_ENABLE = yes # Mouse keys +#USB_EXTRA_ENABLE = yes # Enhanced feature for Windows(Audio control and System control) +#USB_NKRO_ENABLE = yes # USB Nkey Rollover + + + +#---------------- Programming Options -------------------------- +AVRDUDE = avrdude +# Type: avrdude -c ? to get a full listing. +AVRDUDE_PROGRAMMER = usbasp +AVRDUDE_PORT = +AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex +#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep + +# Uncomment the following if you want avrdude's erase cycle counter. +# Note that this counter needs to be initialized first using -Yn, +# see avrdude manual. +#AVRDUDE_ERASE_COUNTER = -y + +# Uncomment the following if you do /not/ wish a verification to be +# performed after programming the device. +#AVRDUDE_NO_VERIFY = -V + +# Increase verbosity level. Please use this when submitting bug +# reports about avrdude. See +# to submit bug reports. +#AVRDUDE_VERBOSE = -v -v + +#AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) +AVRDUDE_FLAGS = -p $(MCU) -c $(AVRDUDE_PROGRAMMER) +AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY) +AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE) +AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER) + +PROGRAM_CMD = $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) + + + +include $(COMMON_DIR)/Makefile.vusb +include $(COMMON_DIR)/Makefile.common diff --git a/hhkb/config_vusb.h b/hhkb/config_vusb.h new file mode 100644 index 0000000000..3b44b94a31 --- /dev/null +++ b/hhkb/config_vusb.h @@ -0,0 +1,120 @@ +#ifndef CONFIG_H +#define CONFIG_H + + +#define VENDOR_ID 0xFEED +#define PRODUCT_ID 0xC0FE +// TODO: share these strings with usbconfig.h +// Edit usbconfig.h to change these. +#define MANUFACTURER t.m.k. +#define PRODUCT HHKB mod +#define DESCRIPTION t.m.k. keyboard firmware for HHKB mod + + +/* matrix size */ +#define MATRIX_ROWS 8 +#define MATRIX_COLS 8 + + +/* key combination for command */ +#define IS_COMMAND() ( \ + keyboard_report->mods == (BIT_LSHIFT | BIT_RSHIFT) || \ + keyboard_report->mods == (BIT_LCTRL | BIT_RSHIFT) \ +) + + +/* mouse keys */ +#ifdef MOUSEKEY_ENABLE +# define MOUSEKEY_DELAY_TIME 255 +#endif + + +/* PS/2 lines */ +#ifdef PS2_MOUSE_ENABLE + +#define PS2_CLOCK_PORT PORTD +#define PS2_CLOCK_PIN PIND +#define PS2_CLOCK_DDR DDRD +#define PS2_CLOCK_BIT 4 +#define PS2_DATA_PORT PORTD +#define PS2_DATA_PIN PIND +#define PS2_DATA_DDR DDRD +#define PS2_DATA_BIT 0 + + +// Synchronous USART is used to receive data from keyboard. +// Use RXD pin for PS/2 DATA line and XCK for PS/2 CLOCK. +// NOTE: This is recomended strongly if you use V-USB library. +#define PS2_USE_USART + +// External or Pin Change Interrupt is used to receive data from keyboard. +// Use INT1 or PCINTxx for PS/2 CLOCK line. see below. +//#define PS2_USE_INT + + +#ifdef PS2_USE_USART +// synchronous, odd parity, 1-bit stop, 8-bit data, sample at falling edge +// set DDR of CLOCK as input to be slave +#define PS2_USART_INIT() do { \ + PS2_CLOCK_DDR &= ~(1< #include #include +#include #include #include "print.h" #include "util.h" @@ -31,29 +32,62 @@ static uint16_t _matrix0[MATRIX_ROWS]; static uint16_t _matrix1[MATRIX_ROWS]; #endif +// HHKB has no ghost and no bounce. #ifdef MATRIX_HAS_GHOST static bool matrix_has_ghost_in_row(uint8_t row); #endif -// matrix is active low. (key on: 0/key off: 1) +// Matrix I/O ports // -// HHKB has no ghost and no bounce. -// row: HC4051 select input channel(0-8) -// PB0, PB1, PB2(A, B, C) -// col: LS145 select low output line(0-8) -// PB3, PB4, PB5, PB6(A, B, C, D) -// use D as ENABLE: (enable: 0/unenable: 1) -// key: KEY: (on: 0/ off:1) -// KEY_PREV: (on: 1/ off: 0) -// PE6,PE7(KEY, KEY_PREV) -#define COL_ENABLE (1<<6) -#define KEY_SELELCT(ROW, COL) (PORTB = (PORTB&(1<<7))|COL_ENABLE|(((COL)&0x07)<<3)|((ROW)&0x07)) -#define KEY_ENABLE (PORTB &= ~COL_ENABLE) -#define KEY_UNABLE (PORTB |= COL_ENABLE) -#define KEY_STATE (PINE&(1<<6)) -#define KEY_PREV_ON (PORTE |= (1<<7)) -#define KEY_PREV_OFF (PORTE &= ~(1<<7)) +// row: HC4051[A,B,C] selects scan row0-7 +// col: LS145[A,B,C,D] selects scan col0-7 and enable(D) +// key: on: 0/off: 1 +// prev: unknown: output previous key state(negated)? + +#ifdef HOST_PJRC +// Ports for Teensy +// row: PB0-2 +// col: PB3-5,6 +// key: PE6(pull-uped) +// prev: PE7 +#define KEY_INIT() do { \ + DDRB |= 0x7F; \ + DDRE |= (1<<7); \ + DDRE &= ~(1<<6); \ + PORTE |= (1<<6); \ +} while (0) +#define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \ + (((COL) & 0x07)<<3) | \ + ((ROW) & 0x07)) +#define KEY_ENABLE() (PORTB &= ~(1<<6)) +#define KEY_UNABLE() (PORTB |= (1<<6)) +#define KEY_STATE() (PINE & (1<<6)) +#define KEY_PREV_ON() (PORTE |= (1<<7)) +#define KEY_PREV_OFF() (PORTE &= ~(1<<7)) + +#else +// Ports for V-USB +// key: PB0(pull-uped) +// prev: PB1 +// row: PB2-4 +// col: PC0-2,3 +#define KEY_INIT() do { \ + DDRB |= 0x1E; \ + DDRB &= ~(1<<0); \ + PORTB |= (1<<0); \ + DDRC |= 0x0F; \ +} while (0) +#define KEY_SELECT(ROW, COL) do { \ + PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \ + PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \ +} while (0) +#define KEY_ENABLE() (PORTC &= ~(1<<3)) +#define KEY_UNABLE() (PORTC |= (1<<3)) +#define KEY_STATE() (PINB & (1<<0)) +#define KEY_PREV_ON() (PORTB |= (1<<1)) +#define KEY_PREV_OFF() (PORTB &= ~(1<<1)) +#endif inline @@ -70,13 +104,7 @@ uint8_t matrix_cols(void) void matrix_init(void) { - // row & col output(PB0-6) - DDRB = 0xFF; - KEY_SELELCT(0, 0); - // KEY: input with pullup(PE6) - // KEY_PREV: output(PE7) - DDRE = 0xBF; - PORTE = 0x40; + KEY_INIT(); // initialize matrix state: all keys off for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; @@ -95,21 +123,31 @@ uint8_t matrix_scan(void) for (uint8_t row = 0; row < MATRIX_ROWS; row++) { for (uint8_t col = 0; col < MATRIX_COLS; col++) { - KEY_SELELCT(row, col); + KEY_SELECT(row, col); _delay_us(40); // from logic analyzer chart if (matrix_prev[row] & (1<> 8) & 0xFF) +/* USB vendor ID for the device, low byte first. If you have registered your + * own Vendor ID, define it here. Otherwise you may use one of obdev's free + * shared VID/PID pairs. Be sure to read USB-IDs-for-free.txt for rules! + * *** IMPORTANT NOTE *** + * This template uses obdev's shared VID/PID pair for Vendor Class devices + * with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand + * the implications! + */ +#define USB_CFG_DEVICE_ID (PRODUCT_ID & 0xFF), ((PRODUCT_ID >> 8) & 0xFF) +/* This is the ID of the product, low byte first. It is interpreted in the + * scope of the vendor ID. If you have registered your own VID with usb.org + * or if you have licensed a PID from somebody else, define it here. Otherwise + * you may use one of obdev's free shared VID/PID pairs. See the file + * USB-IDs-for-free.txt for details! + * *** IMPORTANT NOTE *** + * This template uses obdev's shared VID/PID pair for Vendor Class devices + * with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand + * the implications! + */ +#define USB_CFG_DEVICE_VERSION 0x00, 0x01 +/* Version number of the device: Minor number first, then major number. + */ +#define USB_CFG_VENDOR_NAME 't', '.', 'm', '.', 'k', '.' +#define USB_CFG_VENDOR_NAME_LEN 6 +/* These two values define the vendor name returned by the USB device. The name + * must be given as a list of characters under single quotes. The characters + * are interpreted as Unicode (UTF-16) entities. + * If you don't want a vendor name string, undefine these macros. + * ALWAYS define a vendor name containing your Internet domain name if you use + * obdev's free shared VID/PID pair. See the file USB-IDs-for-free.txt for + * details. + */ +#define USB_CFG_DEVICE_NAME 'H', 'H', 'K', 'B', ' ', 'm', 'o', 'd' +#define USB_CFG_DEVICE_NAME_LEN 8 +/* Same as above for the device name. If you don't want a device name, undefine + * the macros. See the file USB-IDs-for-free.txt before you assign a name if + * you use a shared VID/PID. + */ +/*#define USB_CFG_SERIAL_NUMBER 'N', 'o', 'n', 'e' */ +/*#define USB_CFG_SERIAL_NUMBER_LEN 0 */ +/* Same as above for the serial number. If you don't want a serial number, + * undefine the macros. + * It may be useful to provide the serial number through other means than at + * compile time. See the section about descriptor properties below for how + * to fine tune control over USB descriptors such as the string descriptor + * for the serial number. + */ +#define USB_CFG_DEVICE_CLASS 0 +#define USB_CFG_DEVICE_SUBCLASS 0 +/* See USB specification if you want to conform to an existing device class. + * Class 0xff is "vendor specific". + */ +#define USB_CFG_INTERFACE_CLASS 3 /* HID */ +#define USB_CFG_INTERFACE_SUBCLASS 1 /* Boot */ +#define USB_CFG_INTERFACE_PROTOCOL 1 /* Keyboard */ +/* See USB specification if you want to conform to an existing device class or + * protocol. The following classes must be set at interface level: + * HID class is 3, no subclass and protocol required (but may be useful!) + * CDC class is 2, use subclass 2 and protocol 1 for ACM + */ +#define USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH 0 +/* Define this to the length of the HID report descriptor, if you implement + * an HID device. Otherwise don't define it or define it to 0. + * If you use this define, you must add a PROGMEM character array named + * "usbHidReportDescriptor" to your code which contains the report descriptor. + * Don't forget to keep the array and this define in sync! + */ + +/* #define USB_PUBLIC static */ +/* Use the define above if you #include usbdrv.c instead of linking against it. + * This technique saves a couple of bytes in flash memory. + */ + +/* ------------------- Fine Control over USB Descriptors ------------------- */ +/* If you don't want to use the driver's default USB descriptors, you can + * provide our own. These can be provided as (1) fixed length static data in + * flash memory, (2) fixed length static data in RAM or (3) dynamically at + * runtime in the function usbFunctionDescriptor(). See usbdrv.h for more + * information about this function. + * Descriptor handling is configured through the descriptor's properties. If + * no properties are defined or if they are 0, the default descriptor is used. + * Possible properties are: + * + USB_PROP_IS_DYNAMIC: The data for the descriptor should be fetched + * at runtime via usbFunctionDescriptor(). If the usbMsgPtr mechanism is + * used, the data is in FLASH by default. Add property USB_PROP_IS_RAM if + * you want RAM pointers. + * + USB_PROP_IS_RAM: The data returned by usbFunctionDescriptor() or found + * in static memory is in RAM, not in flash memory. + * + USB_PROP_LENGTH(len): If the data is in static memory (RAM or flash), + * the driver must know the descriptor's length. The descriptor itself is + * found at the address of a well known identifier (see below). + * List of static descriptor names (must be declared PROGMEM if in flash): + * char usbDescriptorDevice[]; + * char usbDescriptorConfiguration[]; + * char usbDescriptorHidReport[]; + * char usbDescriptorString0[]; + * int usbDescriptorStringVendor[]; + * int usbDescriptorStringDevice[]; + * int usbDescriptorStringSerialNumber[]; + * Other descriptors can't be provided statically, they must be provided + * dynamically at runtime. + * + * Descriptor properties are or-ed or added together, e.g.: + * #define USB_CFG_DESCR_PROPS_DEVICE (USB_PROP_IS_RAM | USB_PROP_LENGTH(18)) + * + * The following descriptors are defined: + * USB_CFG_DESCR_PROPS_DEVICE + * USB_CFG_DESCR_PROPS_CONFIGURATION + * USB_CFG_DESCR_PROPS_STRINGS + * USB_CFG_DESCR_PROPS_STRING_0 + * USB_CFG_DESCR_PROPS_STRING_VENDOR + * USB_CFG_DESCR_PROPS_STRING_PRODUCT + * USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER + * USB_CFG_DESCR_PROPS_HID + * USB_CFG_DESCR_PROPS_HID_REPORT + * USB_CFG_DESCR_PROPS_UNKNOWN (for all descriptors not handled by the driver) + * + * Note about string descriptors: String descriptors are not just strings, they + * are Unicode strings prefixed with a 2 byte header. Example: + * int serialNumberDescriptor[] = { + * USB_STRING_DESCRIPTOR_HEADER(6), + * 'S', 'e', 'r', 'i', 'a', 'l' + * }; + */ + +#define USB_CFG_DESCR_PROPS_DEVICE 0 +#define USB_CFG_DESCR_PROPS_CONFIGURATION USB_PROP_IS_DYNAMIC +//#define USB_CFG_DESCR_PROPS_CONFIGURATION 0 +#define USB_CFG_DESCR_PROPS_STRINGS 0 +#define USB_CFG_DESCR_PROPS_STRING_0 0 +#define USB_CFG_DESCR_PROPS_STRING_VENDOR 0 +#define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0 +#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0 +//#define USB_CFG_DESCR_PROPS_HID USB_PROP_IS_DYNAMIC +#define USB_CFG_DESCR_PROPS_HID 0 +#define USB_CFG_DESCR_PROPS_HID_REPORT USB_PROP_IS_DYNAMIC +//#define USB_CFG_DESCR_PROPS_HID_REPORT 0 +#define USB_CFG_DESCR_PROPS_UNKNOWN 0 + +/* ----------------------- Optional MCU Description ------------------------ */ + +/* The following configurations have working defaults in usbdrv.h. You + * usually don't need to set them explicitly. Only if you want to run + * the driver on a device which is not yet supported or with a compiler + * which is not fully supported (such as IAR C) or if you use a differnt + * interrupt than INT0, you may have to define some of these. + */ +/* #define USB_INTR_CFG MCUCR */ +/* #define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) */ +/* #define USB_INTR_CFG_CLR 0 */ +/* #define USB_INTR_ENABLE GIMSK */ +/* #define USB_INTR_ENABLE_BIT INT0 */ +/* #define USB_INTR_PENDING GIFR */ +/* #define USB_INTR_PENDING_BIT INTF0 */ +/* #define USB_INTR_VECTOR INT0_vect */ + +#endif /* __usbconfig_h_included__ */ diff --git a/ps2.c b/ps2.c index 99749cf6ba..d5c3d4d4e1 100644 --- a/ps2.c +++ b/ps2.c @@ -227,8 +227,13 @@ uint8_t ps2_host_recv(void) return pbuf_dequeue(); } +#if 0 #define DEBUGP_INIT() do { DDRC = 0xFF; } while (0) #define DEBUGP(x) do { PORTC = x; } while (0) +#else +#define DEBUGP_INIT() +#define DEBUGP(x) +#endif ISR(PS2_INT_VECT) { static enum { diff --git a/vusb/host.c b/vusb/host.c index 901537bcb8..c49eb62ae0 100644 --- a/vusb/host.c +++ b/vusb/host.c @@ -163,8 +163,8 @@ usbRequest_t *rq = (void *)data; if(rq->bRequest == USBRQ_HID_GET_REPORT){ debug(" GET_REPORT"); /* we only have one report type, so don't look at wValue */ - usbMsgPtr = (void *)keyboard_report; - return sizeof(*keyboard_report); + usbMsgPtr = (void *)keyboard_report_prev; + return sizeof(*keyboard_report_prev); }else if(rq->bRequest == USBRQ_HID_GET_IDLE){ debug(" GET_IDLE: "); debug_hex(idleRate);