-SRC += keyboard.c \
+SRC += host.c \
+ keyboard.c \
command.c \
layer.c \
timer.c \
print.c \
util.c
+
# Option modules
ifdef MOUSEKEY_ENABLE
SRC += mousekey.c
+ OPT_DEFS += -DMOUSEKEY_ENABLE
endif
ifdef PS2_MOUSE_ENABLE
SRC += ps2.c \
ps2_mouse.c
+ OPT_DEFS += -DPS2_MOUSE_ENABLE
+endif
+
+ifdef USB_EXTRA_ENABLE
+ OPT_DEFS += -DUSB_EXTRA_ENABLE
+endif
+
+ifdef USB_NKRO_ENABLE
+ OPT_DEFS += -DUSB_NKRO_ENABLE
+endif
+
+ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
+ OPT_DEFS += -DUSB_MOUSE_ENABLE
endif
+
include $(COMMON_DIR)/Makefile.rules
-# Following variables need to be set in <target>/Makefile:
-# TARGET
-# COMMON_DIR
-# TARGET_DIR
-# TARGET_SRC
-# MCU
-# F_CPU
+OPT_DEFS += -DHOST_PJRC
-
-# List C source files here. (C dependencies are automatically generated.)
SRC = usb_keyboard.c \
usb_debug.c \
usb.c \
jump_bootloader.c
SRC += $(TARGET_SRC)
+
+# C source file search path
+VPATH = $(TARGET_DIR):$(COMMON_DIR):$(COMMON_DIR)/pjrc
+
+
# Option modules
ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
SRC += usb_mouse.c
ifdef USB_EXTRA_ENABLE
SRC += usb_extra.c
endif
-
-
-# C source file search path
-VPATH = $(TARGET_DIR):$(COMMON_DIR):$(COMMON_DIR)/pjrc
CSTANDARD = -std=gnu99
-ifdef MOUSEKEY_ENABLE
- OPT_DEFS += -DMOUSEKEY_ENABLE
-endif
-ifdef PS2_MOUSE_ENABLE
- OPT_DEFS += -DPS2_MOUSE_ENABLE
-endif
-ifdef USB_EXTRA_ENABLE
- OPT_DEFS += -DUSB_EXTRA_ENABLE
-endif
-ifdef USB_NKRO_ENABLE
- OPT_DEFS += -DUSB_NKRO_ENABLE
-endif
-ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
- OPT_DEFS += -DUSB_MOUSE_ENABLE
-endif
-
# Place -D or -U options here for C sources
CDEFS = -DF_CPU=$(F_CPU)UL
CDEFS += $(OPT_DEFS)
-# Following variables need to be set in <target>/Makefile:
-# TARGET
-# COMMON_DIR
-# TARGET_DIR
-# TARGET_SRC
-# MCU
-# F_CPU
+OPT_DEFS += -DHOST_VUSB
-
-# List C source files here. (C dependencies are automatically generated.)
SRC = usbdrv.c \
usbdrvasm.S \
oddebug.c
--- /dev/null
+#include <stdint.h>
+#include <stdbool.h>
+#include <util/delay.h>
+#include "usb_keycodes.h"
+#include "host.h"
+#include "print.h"
+#include "debug.h"
+#include "util.h"
+#include "timer.h"
+#include "layer.h"
+#include "matrix_skel.h"
+#include "command.h"
+
+#ifdef HOST_PJRC
+# include "jump_bootloader.h"
+# include "usb_keyboard.h"
+# ifdef USB_EXTRA_ENABLE
+# include "usb_extra.h"
+# endif
+#endif
+
+
+static void help(void);
+static void switch_layer(uint8_t layer);
+
+
+uint8_t command_proc(void)
+{
+ if (!IS_COMMAND())
+ return 0;
+
+ uint8_t processed = 1;
+ bool last_print_enable = print_enable;
+ print_enable = true;
+ switch (keyboard_report->keys[0]) {
+ case KB_H:
+ help();
+ break;
+ case KB_B:
+#ifdef HOST_PJRC
+ host_clear_keyboard_report();
+ host_send_keyboard_report();
+ print("jump to bootloader...\n");
+ _delay_ms(1000);
+ jump_bootloader(); // not return
+#endif
+ break;
+ case KB_D:
+ debug_enable = !debug_enable;
+ if (debug_enable) {
+ last_print_enable = true;
+ print("debug enabled.\n");
+ debug_matrix = true;
+ debug_keyboard = true;
+ debug_mouse = true;
+ } else {
+ print("debug disabled.\n");
+ last_print_enable = false;
+ debug_matrix = false;
+ debug_keyboard = false;
+ debug_mouse = false;
+ }
+ break;
+ case KB_X: // debug matrix toggle
+ debug_matrix = !debug_matrix;
+ if (debug_matrix)
+ print("debug matrix enabled.\n");
+ else
+ print("debug matrix disabled.\n");
+ break;
+ case KB_K: // debug keyboard toggle
+ debug_keyboard = !debug_keyboard;
+ if (debug_keyboard)
+ print("debug keyboard enabled.\n");
+ else
+ print("debug keyboard disabled.\n");
+ break;
+ case KB_M: // debug mouse toggle
+ debug_mouse = !debug_mouse;
+ if (debug_mouse)
+ print("debug mouse enabled.\n");
+ else
+ print("debug mouse disabled.\n");
+ break;
+ case KB_V: // print version & information
+ print(STR(DESCRIPTION) "\n");
+ break;
+ case KB_T: // print timer
+ print("timer: "); phex16(timer_count); print("\n");
+ break;
+ case KB_P: // print toggle
+ if (print_enable) {
+ print("print disabled.\n");
+ last_print_enable = false;
+ } else {
+ last_print_enable = true;
+ print("print enabled.\n");
+ }
+ break;
+ case KB_S:
+#ifdef HOST_PJRC
+ print("UDCON: "); phex(UDCON); print("\n");
+ print("UDIEN: "); phex(UDIEN); print("\n");
+ print("UDINT: "); phex(UDINT); print("\n");
+ print("host_keyboard_leds:"); phex(host_keyboard_leds()); print("\n");
+#endif
+ break;
+#ifdef USB_NKRO_ENABLE
+ case KB_N:
+ keyboard_nkro = !keyboard_nkro;
+ if (keyboard_nkro)
+ print("USB_NKRO: enabled\n");
+ else
+ print("USB_NKRO: disabled\n");
+ break;
+#endif
+#ifdef USB_EXTRA_ENABLE
+ case KB_ESC:
+ if (suspend && remote_wakeup) {
+ usb_remote_wakeup();
+ } else {
+ usb_extra_system_send(SYSTEM_POWER_DOWN);
+ }
+ break;
+#endif
+ case KB_BSPC:
+ matrix_init();
+ print("clear matrix\n");
+ break;
+ case KB_0:
+ switch_layer(0);
+ break;
+ case KB_1:
+ switch_layer(1);
+ break;
+ case KB_2:
+ switch_layer(2);
+ break;
+ case KB_3:
+ switch_layer(3);
+ break;
+ case KB_4:
+ switch_layer(4);
+ break;
+ default:
+ processed = 0;
+ }
+ if (processed)
+ _delay_ms(500);
+ print_enable = last_print_enable;
+ return processed;
+}
+
+static void help(void)
+{
+ print("b: jump to bootloader\n");
+ print("d: toggle debug enable\n");
+ print("x: toggle matrix debug\n");
+ print("k: toggle keyboard debug\n");
+ print("m: toggle mouse debug\n");
+ print("p: toggle print enable\n");
+ print("v: print version\n");
+ print("t: print timer count\n");
+ print("s: print status\n");
+#ifdef USB_NKRO_ENABLE
+ print("n: toggle USB_NKRO\n");
+#endif
+ print("Backspace: clear matrix\n");
+ print("ESC: power down/wake up\n");
+ print("0: switch to Layer0 \n");
+ print("1: switch to Layer1 \n");
+ print("2: switch to Layer2 \n");
+ print("3: switch to Layer3 \n");
+ print("4: switch to Layer4 \n");
+}
+
+static void switch_layer(uint8_t layer)
+{
+ print("current_layer: "); phex(current_layer); print("\n");
+ print("default_layer: "); phex(default_layer); print("\n");
+ current_layer = layer;
+ default_layer = layer;
+ print("switch to Layer: "); phex(layer); print("\n");
+}
--- /dev/null
+#ifndef COMMAND_H
+#define COMMAND
+
+uint8_t command_proc(void);
+
+#endif
# keyboard dependent files
TARGET_SRC = tmk.c \
- host_pjrc.c \
keymap.c \
matrix.c \
led.c
/* matrix size */
#define MATRIX_ROWS 8
#define MATRIX_COLS 8
+
/* define if matrix has ghost */
//#define MATRIX_HAS_GHOST
+/* key combination for command */
+#define IS_COMMAND() (keyboard_report->mods == (BIT_LSHIFT | BIT_RSHIFT))
+
+
/* USB NKey Rollover */
#ifdef USB_NKRO_ENABLE
#endif
--- /dev/null
+#include "stdint.h"
+#include "led.h"
+
+
+/* HHKB has no LEDs */
+void led_set(uint8_t usb_led)
+{
+}
+
#define MOUSE_BTN5 (1<<4)
-#define REPORT_KEYS 6
+#if defined(HOST_PJRC)
+# include "usb.h"
+# if defined(KBD2_REPORT_KEYS) && KBD2_REPORT_KEYS > KBD_REPORT_KEYS
+# define REPORT_KEYS KBD2_REPORT_KEYS
+# else
+# define REPORT_KEYS KBD_REPORT_KEYS
+# endif
+#elif defined(HOST_VUSB)
+# define REPORT_KEYS 6
+#endif
+
typedef struct {
uint8_t mods;
uint8_t rserved;
uint8_t buttons;
int8_t x;
int8_t y;
-/*
int8_t v;
int8_t h;
- */
} report_mouse_t;
+#ifdef USB_NKRO_ENABLE
+extern bool keyboard_nkro;
+#endif
+
extern report_keyboard_t *keyboard_report;
extern report_keyboard_t *keyboard_report_prev;
+uint8_t host_keyboard_leds(void);
+
/* keyboard report operations */
void host_add_key(uint8_t key);
void host_add_mod_bit(uint8_t mod);
void host_send_keyboard_report(void);
-void host_send_mouse_report(void);
void host_mouse_send(report_mouse_t *report);
#endif
#endif
-static uint8_t last_led = 0;
+static uint8_t last_leds = 0;
void keyboard_init(void)
ps2_mouse_usb_send();
#endif
- if (last_led != host_keyboard_led()) {
- led_set(host_keyboard_led());
- last_led = host_keyboard_led();
+ if (last_leds != host_keyboard_leds()) {
+ keyboard_set_leds(host_keyboard_leds());
+ last_leds = host_keyboard_leds();
}
}
+
+void keyboard_set_leds(uint8_t leds)
+{
+ led_set(leds);
+}
#ifndef KEYBOARD_H
#define KEYBOARD_H
+#include <stdint.h>
+
void keyboard_init(void);
void keyboard_proc(void);
+void keyboard_set_leds(uint8_t leds);
#endif
-
-
#include "timer.h"
#include "layer.h"
+
/*
* Parameters:
* ENTER_DELAY |=======|
# define MOUSEKEY_DELAY_TIME 255
#endif
+// acceleration parameters
+uint8_t mousekey_move_unit = 2;
+uint8_t mousekey_resolution = 5;
+
static inline uint8_t move_unit(void)
{
- uint16_t unit = 10 + (mousekey_repeat);
- return (unit > 127 ? 127 : unit);
+ uint16_t unit = 5 + mousekey_repeat*2;
+ return (unit > 63 ? 63 : unit);
}
void mousekey_decode(uint8_t code)
else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
-/*
else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
else if (code == KB_MS_WH_UP) report.v += 1;
else if (code == KB_MS_WH_DOWN) report.v -= 1;
else if (code == KB_MS_WH_LEFT) report.h -= 1;
else if (code == KB_MS_WH_RIGHT)report.h += 1;
-*/
}
bool mousekey_changed(void)
{
return (report.buttons != report_prev.buttons ||
- report.x != report_prev.x ||
- report.y != report_prev.y ||
- report.x || report.y);
- //return (report.buttons != report_prev.buttons || report.x || report.y);
+ report.x || report.y || report.v || report.h);
}
void mousekey_send(void)
if (!mousekey_changed()) {
mousekey_repeat = 0;
+ mousekey_clear_report();
return;
}
// send immediately when buttun state is changed
if (report.buttons == report_prev.buttons) {
- // TODO: delay parameter setting
- if ((timer_elapsed(last_timer) < (mousekey_repeat == 1 ? 20 : 5))) {
+ if (timer_elapsed(last_timer) < 5) {
+ mousekey_clear_report();
return;
}
}
+ if (mousekey_repeat != 0xFF) {
+ mousekey_repeat++;
+ }
+
if (report.x && report.y) {
report.x *= 0.7;
report.y *= 0.7;
}
- /*
- print("mousekey_repeat: "); phex(mousekey_repeat); print("\n");
- print("timer: "); phex16(timer_read()); print("\n");
- print("last_timer: "); phex16(last_timer); print("\n");
- print("mousekey: "); phex(report.buttons); print(" "); phex(report.x); print(" "); phex(report.y); print("\n");
- */
-
mousekey_debug();
-
host_mouse_send(&report);
- report_prev.buttons = report.buttons;
- report_prev.x = report.x;
- report_prev.y = report.y;
- if (mousekey_repeat != 0xFF) mousekey_repeat++;
+ report_prev = report;
last_timer = timer_read();
mousekey_clear_report();
}
report.buttons = 0;
report.x = 0;
report.y = 0;
+ report.v = 0;
+ report.h = 0;
}
static void mousekey_debug(void)
phex(report.buttons); print("|");
phex(report.x); print(" ");
phex(report.y); print(" ");
-/*
phex(report.v); print(" ");
phex(report.h);
-*/
+ phex(mousekey_repeat);
print("\n");
}
--- /dev/null
+#include <stdint.h>
+#include "usb_keycodes.h"
+#include "usb_keyboard.h"
+#include "usb_mouse.h"
+#include "debug.h"
+#include "host.h"
+
+
+#ifdef USB_NKRO_ENABLE
+bool keyboard_nkro = false;
+#endif
+
+static report_keyboard_t report0;
+static report_keyboard_t report1;
+report_keyboard_t *keyboard_report = &report0;
+report_keyboard_t *keyboard_report_prev = &report1;
+
+static inline void add_key_byte(uint8_t code);
+static inline void add_key_bit(uint8_t code);
+
+
+uint8_t host_keyboard_leds(void)
+{
+ return usb_keyboard_leds;
+}
+
+/* keyboard report operations */
+void host_add_key(uint8_t key)
+{
+#ifdef USB_NKRO_ENABLE
+ if (keyboard_nkro) {
+ add_key_bit(key);
+ return;
+ }
+#endif
+ add_key_byte(key);
+}
+
+void host_add_mod_bit(uint8_t mod)
+{
+ keyboard_report->mods |= mod;
+}
+
+void host_set_mods(uint8_t mods)
+{
+ keyboard_report->mods = mods;
+}
+
+void host_add_code(uint8_t code)
+{
+ if (IS_MOD(code)) {
+ host_add_mod_bit(MOD_BIT(code));
+ } else {
+ host_add_key(code);
+ }
+}
+
+void host_swap_keyboard_report(void)
+{
+ report_keyboard_t *tmp = keyboard_report_prev;
+ keyboard_report_prev = keyboard_report;
+ keyboard_report = tmp;
+}
+
+void host_clear_keyboard_report(void)
+{
+ keyboard_report->mods = 0;
+ for (int8_t i = 0; i < REPORT_KEYS; i++) {
+ keyboard_report->keys[i] = 0;
+ }
+}
+
+uint8_t host_has_anykey(void)
+{
+ uint8_t cnt = 0;
+ for (int i = 0; i < REPORT_KEYS; i++) {
+ if (keyboard_report->keys[i])
+ cnt++;
+ }
+ return cnt;
+}
+
+uint8_t *host_get_keys(void)
+{
+ return keyboard_report->keys;
+}
+
+uint8_t host_get_mods(void)
+{
+ return keyboard_report->mods;
+}
+
+
+void host_send_keyboard_report(void)
+{
+ usb_keyboard_send_report(keyboard_report);
+}
+
+void host_mouse_send(report_mouse_t *report)
+{
+ usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
+}
+
+
+static inline void add_key_byte(uint8_t code)
+{
+ // TODO: fix ugly code
+ int8_t i = 0;
+ int8_t empty = -1;
+ for (; i < REPORT_KEYS; i++) {
+ if (keyboard_report_prev->keys[i] == code) {
+ keyboard_report->keys[i] = code;
+ break;
+ }
+ if (empty == -1 &&
+ keyboard_report_prev->keys[i] == 0 &&
+ keyboard_report->keys[i] == 0) {
+ empty = i;
+ }
+ }
+ if (i == REPORT_KEYS) {
+ if (empty != -1) {
+ keyboard_report->keys[empty] = code;
+ }
+ }
+}
+
+static inline void add_key_bit(uint8_t code)
+{
+ if ((code>>3) < REPORT_KEYS) {
+ keyboard_report->keys[code>>3] |= 1<<(code&7);
+ } else {
+ debug("add_key_bit: can't add: "); phex(code); debug("\n");
+ }
+}
usb_keyboard_idle_count++;
if (usb_keyboard_idle_count == usb_keyboard_idle_config) {
usb_keyboard_idle_count = 0;
- UEDATX = usb_keyboard_mods;
+ UEDATX = keyboard_report->mods;
UEDATX = 0;
for (i=0; i<6; i++) {
- UEDATX = usb_keyboard_keys[i];
+ UEDATX = keyboard_report->keys[i];
}
UEINTX = 0x3A;
}
if (bmRequestType == 0xA1) {
if (bRequest == HID_GET_REPORT) {
usb_wait_in_ready();
- UEDATX = usb_keyboard_mods;
+ UEDATX = keyboard_report->mods;
UEDATX = 0;
for (i=0; i<6; i++) {
- UEDATX = usb_keyboard_keys[i];
+ UEDATX = keyboard_report->keys[i];
}
usb_send_in();
return;
void usb_remote_wakeup(void);
-
-
#define EP_TYPE_CONTROL 0x00
#define EP_TYPE_BULK_IN 0x81
#define EP_TYPE_BULK_OUT 0x80
#define ENDPOINT_HALT 0
#define TEST_MODE 2
+
+/*------------------------------------------------------------------*
+ * Keyboard descriptor setting
+ *------------------------------------------------------------------*/
+#define KBD_INTERFACE 0
+#define KBD_ENDPOINT 1
+#define KBD_SIZE 8
+#define KBD_BUFFER EP_DOUBLE_BUFFER
+#define KBD_REPORT_KEYS (KBD_SIZE - 2)
+
+// secondary keyboard
+#ifdef USB_NKRO_ENABLE
+#define KBD2_INTERFACE 4
+#define KBD2_ENDPOINT 5
+#define KBD2_SIZE 16
+#define KBD2_BUFFER EP_DOUBLE_BUFFER
+#define KBD2_REPORT_KEYS (KBD2_SIZE - 1)
+#endif
+
#endif
#include "print.h"
#include "debug.h"
#include "util.h"
+#include "host.h"
-// keyboard report.
-static usb_keyboard_report_t _report0 = { {0}, 0, false };
-static usb_keyboard_report_t _report1 = { {0}, 0, false };
-usb_keyboard_report_t *usb_keyboard_report = &_report0;
-usb_keyboard_report_t *usb_keyboard_report_prev = &_report1;
-
// protocol setting from the host. We use exactly the same report
// either way, so this variable only stores the setting since we
// are required to be able to report which setting is in use.
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
volatile uint8_t usb_keyboard_leds=0;
-// enable USB NKRO
-bool usb_keyboard_nkro = false;
+static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
-int8_t usb_keyboard_send(void)
-{
- return usb_keyboard_send_report(usb_keyboard_report);
-}
-static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
-int8_t usb_keyboard_send_report(usb_keyboard_report_t *report)
+int8_t usb_keyboard_send_report(report_keyboard_t *report)
{
int8_t result = 0;
#ifdef USB_NKRO_ENABLE
- if (usb_keyboard_nkro)
- result = _send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
+ if (keyboard_nkro)
+ result = send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
else
#endif
{
if (usb_keyboard_protocol)
- result = _send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
+ result = send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
else
- result = _send_report(report, KBD_ENDPOINT, 0, 6);
+ result = send_report(report, KBD_ENDPOINT, 0, 6);
}
if (result) return result;
usb_keyboard_idle_count = 0;
- report->is_sent =true;
usb_keyboard_print_report(report);
return 0;
}
-void usb_keyboard_swap_report(void) {
- usb_keyboard_report_t *tmp = usb_keyboard_report_prev;
- usb_keyboard_report_prev = usb_keyboard_report;
- usb_keyboard_report = tmp;
-}
-
-void usb_keyboard_clear_report(void) {
- usb_keyboard_clear_keys();
- usb_keyboard_clear_mods();
- usb_keyboard_report->is_sent = false;
-}
-
-void usb_keyboard_clear_keys(void) {
- for (int i = 0; i < KEYS_MAX; i++) usb_keyboard_report->keys[i] = 0;
-}
-
-void usb_keyboard_clear_mods(void)
-{
- usb_keyboard_report->mods = 0;
-}
-
-void usb_keyboard_set_keys(uint8_t *keys)
-{
- for (int i = 0; i < KEYS_MAX; i++)
- usb_keyboard_report->keys[i] = keys[i];
-}
-
-void usb_keyboard_set_mods(uint8_t mods)
-{
- usb_keyboard_report->mods = mods;
-}
-
-void usb_keyboard_add_code(uint8_t code)
-{
- if (IS_MOD(code)) {
- usb_keyboard_add_mod(code);
- } else {
- usb_keyboard_add_key(code);
- }
-}
-
-static inline void _add_key_byte(uint8_t code);
-static inline void _add_key_bit(uint8_t code);
-void usb_keyboard_add_key(uint8_t code)
-{
-#ifdef USB_NKRO_ENABLE
- if (usb_keyboard_nkro) {
- _add_key_bit(code);
- return;
- }
-#endif
- _add_key_byte(code);
-}
-
-void usb_keyboard_add_mod(uint8_t code)
-{
- usb_keyboard_report->mods |= MOD_BIT(code);
-}
-
-void usb_keyboard_del_code(uint8_t code)
-{
- if (IS_MOD(code)) {
- usb_keyboard_del_mod(code);
- } else {
- usb_keyboard_del_key(code);
- }
-}
-
-void usb_keyboard_del_key(uint8_t code)
-{
-#ifdef USB_NKRO_ENABLE
- if ((code>>3) < KEYS_MAX) {
- usb_keyboard_keys[code>>3] &= ~(1<<(code&7));
- }
-#else
- for (int i = 0; i < KEYS_MAX; i++) {
- if (usb_keyboard_report->keys[i] == code) {
- usb_keyboard_report->keys[i] = KB_NO;
- return;
- }
- }
-#endif
-}
-
-void usb_keyboard_del_mod(uint8_t code)
-{
- usb_keyboard_report->mods &= ~MOD_BIT(code);
-}
-
-bool usb_keyboard_is_sent(void)
-{
- return usb_keyboard_report->is_sent;
-}
-
-bool usb_keyboard_has_key(void)
-{
- uint8_t keys = 0;
- for (int i = 0; i < KEYS_MAX; i++) keys |= usb_keyboard_report->keys[i];
- return keys ? true : false;
-}
-
-bool usb_keyboard_has_mod(void)
-{
- return usb_keyboard_report->mods ? true : false;
-}
-
-uint8_t usb_keyboard_get_key(void)
-{
-#ifdef USB_NKRO_ENABLE
- if (usb_keyboard_nkro) {
- uint8_t i = 0;
- for (; i < KEYS_MAX && !usb_keyboard_keys[i]; i++);
- return i<<3 | biton(usb_keyboard_keys[i]);
- }
-#endif
- return usb_keyboard_keys[0];
-}
-
-void usb_keyboard_print_report(usb_keyboard_report_t *report)
+void usb_keyboard_print_report(report_keyboard_t *report)
{
if (!debug_keyboard) return;
print("keys: ");
- for (int i = 0; i < KEYS_MAX; i++) { phex(report->keys[i]); print(" "); }
+ for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
print(" mods: "); phex(report->mods); print("\n");
}
-static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
+static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
{
uint8_t intr_state, timeout;
UENUM = endpoint;
}
UEDATX = report->mods;
- if (!usb_keyboard_nkro)
+ if (!keyboard_nkro)
UEDATX = 0;
for (uint8_t i = keys_start; i < keys_end; i++) {
UEDATX = report->keys[i];
SREG = intr_state;
return 0;
}
-
-static inline void _add_key_byte(uint8_t code)
-{
- // TODO: fix ugly code
- int8_t i = 0;
- int8_t empty = -1;
- for (; i < KEYS_MAX; i++) {
- if (usb_keyboard_keys_prev[i] == code) {
- usb_keyboard_keys[i] = code;
- break;
- }
- if (empty == -1 &&
- usb_keyboard_keys_prev[i] == 0 &&
- usb_keyboard_keys[i] == 0) {
- empty = i;
- }
- }
- if (i == KEYS_MAX) {
- if (empty != -1) {
- usb_keyboard_keys[empty] = code;
- }
- }
-}
-
-static inline void _add_key_bit(uint8_t code)
-{
- if ((code>>3) < KEYS_MAX) {
- usb_keyboard_keys[code>>3] |= 1<<(code&7);
- }
-}
-
#include <stdint.h>
#include <stdbool.h>
#include "usb.h"
+#include "host.h"
-#define KBD_INTERFACE 0
-#define KBD_ENDPOINT 1
-#define KBD_SIZE 8
-#define KBD_BUFFER EP_DOUBLE_BUFFER
-#define KBD_REPORT_KEYS (KBD_SIZE - 2)
-
-// secondary keyboard
-#ifdef USB_NKRO_ENABLE
-#define KBD2_INTERFACE 4
-#define KBD2_ENDPOINT 5
-#define KBD2_SIZE 16
-#define KBD2_BUFFER EP_DOUBLE_BUFFER
-#define KBD2_REPORT_KEYS (KBD2_SIZE - 1)
-#endif
-
-#if defined(KBD2_REPORT_KEYS) && KBD2_REPORT_KEYS > KBD_REPORT_KEYS
-#define KEYS_MAX KBD2_REPORT_KEYS
-#else
-#define KEYS_MAX KBD_REPORT_KEYS
-#endif
-
-
-typedef struct report {
- uint8_t keys[KEYS_MAX];
- uint8_t mods;
- bool is_sent;
-} usb_keyboard_report_t;
-
-
-#define usb_keyboard_keys usb_keyboard_report->keys
-#define usb_keyboard_mods usb_keyboard_report->mods
-#define usb_keyboard_keys_prev usb_keyboard_report_prev->keys
-#define usb_keyboard_mods_prev usb_keyboard_report_prev->mods
-
-
-extern usb_keyboard_report_t *usb_keyboard_report;
-extern usb_keyboard_report_t *usb_keyboard_report_prev;
extern uint8_t usb_keyboard_protocol;
extern uint8_t usb_keyboard_idle_config;
extern uint8_t usb_keyboard_idle_count;
extern volatile uint8_t usb_keyboard_leds;
-extern bool usb_keyboard_nkro;
-
-
-int8_t usb_keyboard_send(void);
-int8_t usb_keyboard_send_report(usb_keyboard_report_t *report);
-
-void usb_keyboard_swap_report(void);
-
-void usb_keyboard_clear_report(void);
-void usb_keyboard_clear_keys(void);
-void usb_keyboard_clear_mods(void);
-
-void usb_keyboard_set_keys(uint8_t *keys);
-void usb_keyboard_set_mods(uint8_t mods);
-
-void usb_keyboard_add_code(uint8_t code);
-void usb_keyboard_add_key(uint8_t code);
-void usb_keyboard_add_mod(uint8_t code);
-
-void usb_keyboard_del_code(uint8_t code);
-void usb_keyboard_del_key(uint8_t code);
-void usb_keyboard_del_mod(uint8_t code);
-
-bool usb_keyboard_is_sent(void);
-bool usb_keyboard_has_key(void);
-bool usb_keyboard_has_mod(void);
-uint8_t usb_keyboard_get_key(void);
-void usb_keyboard_print_report(usb_keyboard_report_t *report);
+int8_t usb_keyboard_send_report(report_keyboard_t *report);
+void usb_keyboard_print_report(report_keyboard_t *report);
#endif
matrix.c \
led.c \
ps2.c \
- host_vusb.c \
usart_print.c
OPT_DEFS = -DDEBUG_LEVEL=0
--- /dev/null
+PS/2 to USB keyboard converter with V-USB
+=========================================
+2011/02/07
+
+NOT COMPLETED
+development was suspended.
+
+TODO:
+PS/2 library: losts data from keyboard occasionally,
+ should use interrupt thoroughly for communication.
+
+Code cleaning: merge code changed here to other subprojects and common modules.
/* matrix size */
#define MATRIX_ROWS 32 // keycode bit: 3-0
#define MATRIX_COLS 8 // keycode bit: 6-4
-/* define if matrix has ghost */
-//#define MATRIX_HAS_GHOST
+
+/* key combination for command */
+#define IS_COMMAND() ( \
+ keyboard_report->mods == (BIT_LSHIFT | BIT_RSHIFT) || \
+ keyboard_report->mods == (BIT_LCTRL | BIT_RSHIFT) \
+)
+
/* USB NKey Rollover */
#ifdef USB_NKRO_ENABLE
# define MOUSEKEY_DELAY_TIME 255
#endif
-#define IS_COMMAND() ( \
- keyboard_report->mods == (BIT_LSHIFT | BIT_RSHIFT) || \
- keyboard_report->mods == (BIT_LCTRL | BIT_RSHIFT) \
-)
-
/* PS/2 lines */
#define PS2_CLOCK_PORT PORTD
+++ /dev/null
-#include "usbdrv.h"
-#include "usbconfig.h"
-#include "print.h"
-#include "usb_keycodes.h"
-#include "host.h"
-#include "host_vusb.h"
-#include "debug.h"
-
-
-static report_keyboard_t report0;
-static report_keyboard_t report1;
-report_keyboard_t *keyboard_report = &report0;
-report_keyboard_t *keyboard_report_prev = &report1;
-
-static uint8_t keyboard_led = 0;
-static uchar idleRate = 0;
-
-uint8_t host_keyboard_led(void)
-{
- return keyboard_led;
-}
-
-
-/*------------------------------------------------------------------*
- * Keyboard report operations
- *------------------------------------------------------------------*/
-void host_add_key(uint8_t code)
-{
- int8_t i = 0;
- int8_t empty = -1;
- for (; i < REPORT_KEYS; i++) {
- if (keyboard_report_prev->keys[i] == code) {
- keyboard_report->keys[i] = code;
- break;
- }
- if (empty == -1 && keyboard_report_prev->keys[i] == KB_NO && keyboard_report->keys[i] == KB_NO) {
- empty = i;
- }
- }
- if (i == REPORT_KEYS && empty != -1) {
- keyboard_report->keys[empty] = code;
- }
-}
-
-void host_add_mod_bit(uint8_t mod)
-{
- keyboard_report->mods |= mod;
-}
-
-void host_set_mods(uint8_t mods)
-{
- keyboard_report->mods = mods;
-}
-
-void host_add_code(uint8_t code)
-{
- if (IS_MOD(code)) {
- host_add_mod_bit(MOD_BIT(code));
- } else {
- host_add_key(code);
- }
-}
-
-void host_swap_keyboard_report(void)
-{
- report_keyboard_t *tmp = keyboard_report_prev;
- keyboard_report_prev = keyboard_report;
- keyboard_report = tmp;
-}
-
-void host_clear_keyboard_report(void)
-{
- keyboard_report->mods = 0;
- for (int8_t i = 0; i < REPORT_KEYS; i++) {
- keyboard_report->keys[i] = 0;
- }
-}
-
-uint8_t host_has_anykey(void)
-{
- uint8_t cnt = 0;
- for (int i = 0; i < REPORT_KEYS; i++) {
- if (keyboard_report->keys[i])
- cnt++;
- }
- return cnt;
-}
-
-uint8_t *host_get_keys(void)
-{
- return keyboard_report->keys;
-}
-
-uint8_t host_get_mods(void)
-{
- return keyboard_report->mods;
-}
-
-
-/*------------------------------------------------------------------*
- * Keyboard report send buffer
- *------------------------------------------------------------------*/
-#define KBUF_SIZE 16
-static report_keyboard_t kbuf[KBUF_SIZE];
-static uint8_t kbuf_head = 0;
-static uint8_t kbuf_tail = 0;
-
-void host_vusb_keyboard_send(void)
-{
- while (usbInterruptIsReady() && kbuf_head != kbuf_tail) {
- usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
- kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
- }
-}
-
-void host_send_keyboard_report(void)
-{
- uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
- if (next != kbuf_tail) {
- kbuf[kbuf_head] = *keyboard_report;
- kbuf_head = next;
- } else {
- debug("kbuf: full\n");
- }
-}
-
-
-void host_mouse_send(report_mouse_t *report)
-{
- if (usbInterruptIsReady3()) {
- usbSetInterrupt3((void *)report, sizeof(*report));
- } else {
- debug("Int3 not ready\n");
- }
-}
-
-
-
-/*------------------------------------------------------------------*
- * Request from host *
- *------------------------------------------------------------------*/
-static struct {
- uint16_t len;
- enum {
- NONE,
- SET_LED
- } kind;
-} last_req;
-
-usbMsgLen_t usbFunctionSetup(uchar data[8])
-{
-usbRequest_t *rq = (void *)data;
-
- if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
- 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);
- }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
- debug(" GET_IDLE: ");
- debug_hex(idleRate);
- usbMsgPtr = &idleRate;
- return 1;
- }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
- idleRate = rq->wValue.bytes[1];
- debug(" SET_IDLE: ");
- debug_hex(idleRate);
- }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
- //debug(" SET_REPORT: ");
- if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
- last_req.kind = SET_LED;
- last_req.len = rq->wLength.word;
- }
- return USB_NO_MSG; // to get data in usbFunctionWrite
- }
- debug("\n");
- }else{
- debug("VENDOR\n");
- /* no vendor specific requests implemented */
- }
- return 0; /* default for not implemented requests: return no data back to host */
-}
-
-uchar usbFunctionWrite(uchar *data, uchar len)
-{
- if (last_req.len == 0) {
- return -1;
- }
- switch (last_req.kind) {
- case SET_LED:
- //debug("SET_LED\n");
- keyboard_led = data[0];
- last_req.len = 0;
- return 1;
- break;
- case NONE:
- default:
- return -1;
- break;
- }
- return 1;
-}
-
-
-
-/*------------------------------------------------------------------*
- * Descriptors *
- *------------------------------------------------------------------*/
-
-/*
- * Report Descriptor for keyboard
- *
- * from an example in HID spec appendix
- */
-PROGMEM uchar keyboard_hid_report[] = {
- 0x05, 0x01, // Usage Page (Generic Desktop),
- 0x09, 0x06, // Usage (Keyboard),
- 0xA1, 0x01, // Collection (Application),
- 0x75, 0x01, // Report Size (1),
- 0x95, 0x08, // Report Count (8),
- 0x05, 0x07, // Usage Page (Key Codes),
- 0x19, 0xE0, // Usage Minimum (224),
- 0x29, 0xE7, // Usage Maximum (231),
- 0x15, 0x00, // Logical Minimum (0),
- 0x25, 0x01, // Logical Maximum (1),
- 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
- 0x95, 0x01, // Report Count (1),
- 0x75, 0x08, // Report Size (8),
- 0x81, 0x03, // Input (Constant), ;Reserved byte
- 0x95, 0x05, // Report Count (5),
- 0x75, 0x01, // Report Size (1),
- 0x05, 0x08, // Usage Page (LEDs),
- 0x19, 0x01, // Usage Minimum (1),
- 0x29, 0x05, // Usage Maximum (5),
- 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
- 0x95, 0x01, // Report Count (1),
- 0x75, 0x03, // Report Size (3),
- 0x91, 0x03, // Output (Constant), ;LED report padding
- 0x95, 0x06, // Report Count (6),
- 0x75, 0x08, // Report Size (8),
- 0x15, 0x00, // Logical Minimum (0),
- 0x25, 0xFF, // Logical Maximum(255),
- 0x05, 0x07, // Usage Page (Key Codes),
- 0x19, 0x00, // Usage Minimum (0),
- 0x29, 0xFF, // Usage Maximum (255),
- 0x81, 0x00, // Input (Data, Array),
- 0xc0 // End Collection
-};
-
-/*
- * Report Descriptor for mouse
- *
- * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
- * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
- * http://www.keil.com/forum/15671/
- * http://www.microsoft.com/whdc/device/input/wheel.mspx
- */
-PROGMEM uchar mouse_hid_report[] = {
- /* from HID 1.11 spec example */
- 0x05, 0x01, // Usage Page (Generic Desktop),
- 0x09, 0x02, // Usage (Mouse),
- 0xA1, 0x01, // Collection (Application),
- 0x09, 0x01, // Usage (Pointer),
- 0xA1, 0x00, // Collection (Physical),
- 0x05, 0x09, // Usage Page (Buttons),
- 0x19, 0x01, // Usage Minimum (01),
- 0x29, 0x03, // Usage Maximun (03),
- 0x15, 0x00, // Logical Minimum (0),
- 0x25, 0x01, // Logical Maximum (1),
- 0x95, 0x03, // Report Count (3),
- 0x75, 0x01, // Report Size (1),
- 0x81, 0x02, // Input (Data, Variable, Absolute), ;3 button bits
- 0x95, 0x01, // Report Count (1),
- 0x75, 0x05, // Report Size (5),
- 0x81, 0x01, // Input (Constant), ;5 bit padding
- 0x05, 0x01, // Usage Page (Generic Desktop),
- 0x09, 0x30, // Usage (X),
- 0x09, 0x31, // Usage (Y),
- 0x15, 0x81, // Logical Minimum (-127),
- 0x25, 0x7F, // Logical Maximum (127),
- 0x75, 0x08, // Report Size (8),
- 0x95, 0x02, // Report Count (2),
- 0x81, 0x06, // Input (Data, Variable, Relative), ;2 position bytes (X & Y)
- 0xC0, // End Collection,
- 0xC0, // End Collection
-/*
- 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
- 0x09, 0x02, // USAGE (Mouse)
- 0xa1, 0x01, // COLLECTION (Application)
- 0x09, 0x02, // USAGE (Mouse)
- 0xa1, 0x02, // COLLECTION (Logical)
- 0x09, 0x01, // USAGE (Pointer)
- 0xa1, 0x00, // COLLECTION (Physical)
- // ------------------------------ Buttons
- 0x05, 0x09, // USAGE_PAGE (Button)
- 0x19, 0x01, // USAGE_MINIMUM (Button 1)
- 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
- 0x15, 0x00, // LOGICAL_MINIMUM (0)
- 0x25, 0x01, // LOGICAL_MAXIMUM (1)
- 0x75, 0x01, // REPORT_SIZE (1)
- 0x95, 0x05, // REPORT_COUNT (5)
- 0x81, 0x02, // INPUT (Data,Var,Abs)
- // ------------------------------ Padding
- 0x75, 0x03, // REPORT_SIZE (3)
- 0x95, 0x01, // REPORT_COUNT (1)
- 0x81, 0x03, // INPUT (Cnst,Var,Abs)
- // ------------------------------ X,Y position
- 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
- 0x09, 0x30, // USAGE (X)
- 0x09, 0x31, // USAGE (Y)
- 0x15, 0x81, // LOGICAL_MINIMUM (-127)
- 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
- 0x75, 0x08, // REPORT_SIZE (8)
- 0x95, 0x02, // REPORT_COUNT (2)
- 0x81, 0x06, // INPUT (Data,Var,Rel)
- 0xa1, 0x02, // COLLECTION (Logical)
- // ------------------------------ Vertical wheel res multiplier
- 0x09, 0x48, // USAGE (Resolution Multiplier)
- 0x15, 0x00, // LOGICAL_MINIMUM (0)
- 0x25, 0x01, // LOGICAL_MAXIMUM (1)
- 0x35, 0x01, // PHYSICAL_MINIMUM (1)
- 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
- 0x75, 0x02, // REPORT_SIZE (2)
- 0x95, 0x01, // REPORT_COUNT (1)
- 0xa4, // PUSH
- 0xb1, 0x02, // FEATURE (Data,Var,Abs)
- // ------------------------------ Vertical wheel
- 0x09, 0x38, // USAGE (Wheel)
- 0x15, 0x81, // LOGICAL_MINIMUM (-127)
- 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
- 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
- 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
- 0x75, 0x08, // REPORT_SIZE (8)
- 0x81, 0x06, // INPUT (Data,Var,Rel)
- 0xc0, // END_COLLECTION
- 0xa1, 0x02, // COLLECTION (Logical)
- // ------------------------------ Horizontal wheel res multiplier
- 0x09, 0x48, // USAGE (Resolution Multiplier)
- 0xb4, // POP
- 0xb1, 0x02, // FEATURE (Data,Var,Abs)
- // ------------------------------ Padding for Feature report
- 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
- 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
- 0x75, 0x04, // REPORT_SIZE (4)
- 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
- // ------------------------------ Horizontal wheel
- 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
- 0x0a, 0x38, 0x02, // USAGE (AC Pan)
- 0x15, 0x81, // LOGICAL_MINIMUM (-127)
- 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
- 0x75, 0x08, // REPORT_SIZE (8)
- 0x81, 0x06, // INPUT (Data,Var,Rel)
- 0xc0, // END_COLLECTION
- 0xc0, // END_COLLECTION
- 0xc0, // END_COLLECTION
- 0xc0 // END_COLLECTION
-*/
-};
-
-
-/*
- * Descriptor for compite device: Keyboard + Mouse
- *
- * contains: device, interface, HID and endpoint descriptors
- */
-#if USB_CFG_DESCR_PROPS_CONFIGURATION
-PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
- 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
- USBDESCR_CONFIG, /* descriptor type */
- 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
- //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
- /* total length of data returned (including inlined descriptors) */
- 2, /* number of interfaces in this configuration */
- 1, /* index of this configuration */
- 0, /* configuration name string index */
-#if USB_CFG_IS_SELF_POWERED
- (1 << 7) | USBATTR_SELFPOWER, /* attributes */
-#else
- (1 << 7), /* attributes */
-#endif
- USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
-
- /*
- * Keyboard interface
- */
- /* Interface descriptor */
- 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
- USBDESCR_INTERFACE, /* descriptor type */
- 0, /* index of this interface */
- 0, /* alternate setting for this interface */
- USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
- USB_CFG_INTERFACE_CLASS,
- USB_CFG_INTERFACE_SUBCLASS,
- USB_CFG_INTERFACE_PROTOCOL,
- 0, /* string index for interface */
- /* HID descriptor */
- 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
- USBDESCR_HID, /* descriptor type: HID */
- 0x01, 0x01, /* BCD representation of HID version */
- 0x00, /* target country code */
- 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
- 0x22, /* descriptor type: report */
- sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
- /* Endpoint descriptor */
-#if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
- 7, /* sizeof(usbDescrEndpoint) */
- USBDESCR_ENDPOINT, /* descriptor type = endpoint */
- (char)0x81, /* IN endpoint number 1 */
- 0x03, /* attrib: Interrupt endpoint */
- 8, 0, /* maximum packet size */
- USB_CFG_INTR_POLL_INTERVAL, /* in ms */
-#endif
-
- /*
- * Mouse interface
- */
- /* Interface descriptor */
- 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
- USBDESCR_INTERFACE, /* descriptor type */
- 1, /* index of this interface */
- 0, /* alternate setting for this interface */
- USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
- 0x03, /* CLASS: HID */
- 0, /* SUBCLASS: none */
- 0, /* PROTOCOL: none */
- 0, /* string index for interface */
- /* HID descriptor */
- 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
- USBDESCR_HID, /* descriptor type: HID */
- 0x01, 0x01, /* BCD representation of HID version */
- 0x00, /* target country code */
- 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
- 0x22, /* descriptor type: report */
- sizeof(mouse_hid_report), 0, /* total length of report descriptor */
-#if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
- /* Endpoint descriptor */
- 7, /* sizeof(usbDescrEndpoint) */
- USBDESCR_ENDPOINT, /* descriptor type = endpoint */
- (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
- 0x03, /* attrib: Interrupt endpoint */
- 8, 0, /* maximum packet size */
- USB_CFG_INTR_POLL_INTERVAL, /* in ms */
-#endif
-};
-#endif
-
-
-USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
-{
- usbMsgLen_t len = 0;
-
- debug("usbFunctionDescriptor: ");
- debug_hex(rq->bmRequestType); debug(" ");
- debug_hex(rq->bRequest); debug(" ");
- debug_hex16(rq->wValue.word); debug(" ");
- debug_hex16(rq->wIndex.word); debug(" ");
- debug_hex16(rq->wLength.word); debug("\n");
-
- switch (rq->wValue.bytes[1]) {
-#if USB_CFG_DESCR_PROPS_CONFIGURATION
- case USBDESCR_CONFIG:
- usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
- len = sizeof(usbDescriptorConfiguration);
- break;
-#endif
- case USBDESCR_HID:
- usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 18);
- len = 9;
- break;
- case USBDESCR_HID_REPORT:
- /* interface index */
- switch (rq->wIndex.word) {
- case 0:
- usbMsgPtr = keyboard_hid_report;
- len = sizeof(keyboard_hid_report);
- break;
- case 1:
- usbMsgPtr = mouse_hid_report;
- len = sizeof(mouse_hid_report);
- break;
- }
- break;
- }
- debug("desc len: "); debug_hex(len); debug("\n");
- return len;
-}
+++ /dev/null
-#ifndef HOST_VUSB_H
-#define HOST_VUSB_H
-
-void host_vusb_keyboard_send(void);
-
-#endif
--- /dev/null
+#include "usbdrv.h"
+#include "usbconfig.h"
+#include "print.h"
+#include "usb_keycodes.h"
+#include "host.h"
+#include "host_vusb.h"
+#include "debug.h"
+
+
+static report_keyboard_t report0;
+static report_keyboard_t report1;
+report_keyboard_t *keyboard_report = &report0;
+report_keyboard_t *keyboard_report_prev = &report1;
+
+static uint8_t keyboard_leds = 0;
+static uchar idleRate = 0;
+
+uint8_t host_keyboard_leds(void)
+{
+ return keyboard_leds;
+}
+
+
+/*------------------------------------------------------------------*
+ * Keyboard report operations
+ *------------------------------------------------------------------*/
+void host_add_key(uint8_t code)
+{
+ int8_t i = 0;
+ int8_t empty = -1;
+ for (; i < REPORT_KEYS; i++) {
+ if (keyboard_report_prev->keys[i] == code) {
+ keyboard_report->keys[i] = code;
+ break;
+ }
+ if (empty == -1 && keyboard_report_prev->keys[i] == KB_NO && keyboard_report->keys[i] == KB_NO) {
+ empty = i;
+ }
+ }
+ if (i == REPORT_KEYS && empty != -1) {
+ keyboard_report->keys[empty] = code;
+ }
+}
+
+void host_add_mod_bit(uint8_t mod)
+{
+ keyboard_report->mods |= mod;
+}
+
+void host_set_mods(uint8_t mods)
+{
+ keyboard_report->mods = mods;
+}
+
+void host_add_code(uint8_t code)
+{
+ if (IS_MOD(code)) {
+ host_add_mod_bit(MOD_BIT(code));
+ } else {
+ host_add_key(code);
+ }
+}
+
+void host_swap_keyboard_report(void)
+{
+ report_keyboard_t *tmp = keyboard_report_prev;
+ keyboard_report_prev = keyboard_report;
+ keyboard_report = tmp;
+}
+
+void host_clear_keyboard_report(void)
+{
+ keyboard_report->mods = 0;
+ for (int8_t i = 0; i < REPORT_KEYS; i++) {
+ keyboard_report->keys[i] = 0;
+ }
+}
+
+uint8_t host_has_anykey(void)
+{
+ uint8_t cnt = 0;
+ for (int i = 0; i < REPORT_KEYS; i++) {
+ if (keyboard_report->keys[i])
+ cnt++;
+ }
+ return cnt;
+}
+
+uint8_t *host_get_keys(void)
+{
+ return keyboard_report->keys;
+}
+
+uint8_t host_get_mods(void)
+{
+ return keyboard_report->mods;
+}
+
+
+/*------------------------------------------------------------------*
+ * Keyboard report send buffer
+ *------------------------------------------------------------------*/
+#define KBUF_SIZE 16
+static report_keyboard_t kbuf[KBUF_SIZE];
+static uint8_t kbuf_head = 0;
+static uint8_t kbuf_tail = 0;
+
+void host_vusb_keyboard_send(void)
+{
+ while (usbInterruptIsReady() && kbuf_head != kbuf_tail) {
+ usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
+ kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
+ }
+}
+
+void host_send_keyboard_report(void)
+{
+ uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
+ if (next != kbuf_tail) {
+ kbuf[kbuf_head] = *keyboard_report;
+ kbuf_head = next;
+ } else {
+ debug("kbuf: full\n");
+ }
+}
+
+
+void host_mouse_send(report_mouse_t *report)
+{
+ if (usbInterruptIsReady3()) {
+ usbSetInterrupt3((void *)report, sizeof(*report));
+ } else {
+ debug("Int3 not ready\n");
+ }
+}
+
+
+
+/*------------------------------------------------------------------*
+ * Request from host *
+ *------------------------------------------------------------------*/
+static struct {
+ uint16_t len;
+ enum {
+ NONE,
+ SET_LED
+ } kind;
+} last_req;
+
+usbMsgLen_t usbFunctionSetup(uchar data[8])
+{
+usbRequest_t *rq = (void *)data;
+
+ if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
+ 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);
+ }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
+ debug(" GET_IDLE: ");
+ debug_hex(idleRate);
+ usbMsgPtr = &idleRate;
+ return 1;
+ }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
+ idleRate = rq->wValue.bytes[1];
+ debug(" SET_IDLE: ");
+ debug_hex(idleRate);
+ }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
+ //debug(" SET_REPORT: ");
+ if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
+ last_req.kind = SET_LED;
+ last_req.len = rq->wLength.word;
+ }
+ return USB_NO_MSG; // to get data in usbFunctionWrite
+ }
+ debug("\n");
+ }else{
+ debug("VENDOR\n");
+ /* no vendor specific requests implemented */
+ }
+ return 0; /* default for not implemented requests: return no data back to host */
+}
+
+uchar usbFunctionWrite(uchar *data, uchar len)
+{
+ if (last_req.len == 0) {
+ return -1;
+ }
+ switch (last_req.kind) {
+ case SET_LED:
+ //debug("SET_LED\n");
+ keyboard_leds = data[0];
+ last_req.len = 0;
+ return 1;
+ break;
+ case NONE:
+ default:
+ return -1;
+ break;
+ }
+ return 1;
+}
+
+
+
+/*------------------------------------------------------------------*
+ * Descriptors *
+ *------------------------------------------------------------------*/
+
+/*
+ * Report Descriptor for keyboard
+ *
+ * from an example in HID spec appendix
+ */
+PROGMEM uchar keyboard_hid_report[] = {
+ 0x05, 0x01, // Usage Page (Generic Desktop),
+ 0x09, 0x06, // Usage (Keyboard),
+ 0xA1, 0x01, // Collection (Application),
+ 0x75, 0x01, // Report Size (1),
+ 0x95, 0x08, // Report Count (8),
+ 0x05, 0x07, // Usage Page (Key Codes),
+ 0x19, 0xE0, // Usage Minimum (224),
+ 0x29, 0xE7, // Usage Maximum (231),
+ 0x15, 0x00, // Logical Minimum (0),
+ 0x25, 0x01, // Logical Maximum (1),
+ 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
+ 0x95, 0x01, // Report Count (1),
+ 0x75, 0x08, // Report Size (8),
+ 0x81, 0x03, // Input (Constant), ;Reserved byte
+ 0x95, 0x05, // Report Count (5),
+ 0x75, 0x01, // Report Size (1),
+ 0x05, 0x08, // Usage Page (LEDs),
+ 0x19, 0x01, // Usage Minimum (1),
+ 0x29, 0x05, // Usage Maximum (5),
+ 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
+ 0x95, 0x01, // Report Count (1),
+ 0x75, 0x03, // Report Size (3),
+ 0x91, 0x03, // Output (Constant), ;LED report padding
+ 0x95, 0x06, // Report Count (6),
+ 0x75, 0x08, // Report Size (8),
+ 0x15, 0x00, // Logical Minimum (0),
+ 0x25, 0xFF, // Logical Maximum(255),
+ 0x05, 0x07, // Usage Page (Key Codes),
+ 0x19, 0x00, // Usage Minimum (0),
+ 0x29, 0xFF, // Usage Maximum (255),
+ 0x81, 0x00, // Input (Data, Array),
+ 0xc0 // End Collection
+};
+
+/*
+ * Report Descriptor for mouse
+ *
+ * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
+ * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
+ * http://www.keil.com/forum/15671/
+ * http://www.microsoft.com/whdc/device/input/wheel.mspx
+ */
+PROGMEM uchar mouse_hid_report[] = {
+ /* from HID 1.11 spec example */
+ 0x05, 0x01, // Usage Page (Generic Desktop),
+ 0x09, 0x02, // Usage (Mouse),
+ 0xA1, 0x01, // Collection (Application),
+ 0x09, 0x01, // Usage (Pointer),
+ 0xA1, 0x00, // Collection (Physical),
+ 0x05, 0x09, // Usage Page (Buttons),
+ 0x19, 0x01, // Usage Minimum (01),
+ 0x29, 0x03, // Usage Maximun (03),
+ 0x15, 0x00, // Logical Minimum (0),
+ 0x25, 0x01, // Logical Maximum (1),
+ 0x95, 0x03, // Report Count (3),
+ 0x75, 0x01, // Report Size (1),
+ 0x81, 0x02, // Input (Data, Variable, Absolute), ;3 button bits
+ 0x95, 0x01, // Report Count (1),
+ 0x75, 0x05, // Report Size (5),
+ 0x81, 0x01, // Input (Constant), ;5 bit padding
+ 0x05, 0x01, // Usage Page (Generic Desktop),
+ 0x09, 0x30, // Usage (X),
+ 0x09, 0x31, // Usage (Y),
+ 0x15, 0x81, // Logical Minimum (-127),
+ 0x25, 0x7F, // Logical Maximum (127),
+ 0x75, 0x08, // Report Size (8),
+ 0x95, 0x02, // Report Count (2),
+ 0x81, 0x06, // Input (Data, Variable, Relative), ;2 position bytes (X & Y)
+ 0xC0, // End Collection,
+ 0xC0, // End Collection
+/*
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
+ 0x09, 0x02, // USAGE (Mouse)
+ 0xa1, 0x01, // COLLECTION (Application)
+ 0x09, 0x02, // USAGE (Mouse)
+ 0xa1, 0x02, // COLLECTION (Logical)
+ 0x09, 0x01, // USAGE (Pointer)
+ 0xa1, 0x00, // COLLECTION (Physical)
+ // ------------------------------ Buttons
+ 0x05, 0x09, // USAGE_PAGE (Button)
+ 0x19, 0x01, // USAGE_MINIMUM (Button 1)
+ 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x01, // LOGICAL_MAXIMUM (1)
+ 0x75, 0x01, // REPORT_SIZE (1)
+ 0x95, 0x05, // REPORT_COUNT (5)
+ 0x81, 0x02, // INPUT (Data,Var,Abs)
+ // ------------------------------ Padding
+ 0x75, 0x03, // REPORT_SIZE (3)
+ 0x95, 0x01, // REPORT_COUNT (1)
+ 0x81, 0x03, // INPUT (Cnst,Var,Abs)
+ // ------------------------------ X,Y position
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
+ 0x09, 0x30, // USAGE (X)
+ 0x09, 0x31, // USAGE (Y)
+ 0x15, 0x81, // LOGICAL_MINIMUM (-127)
+ 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x95, 0x02, // REPORT_COUNT (2)
+ 0x81, 0x06, // INPUT (Data,Var,Rel)
+ 0xa1, 0x02, // COLLECTION (Logical)
+ // ------------------------------ Vertical wheel res multiplier
+ 0x09, 0x48, // USAGE (Resolution Multiplier)
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x01, // LOGICAL_MAXIMUM (1)
+ 0x35, 0x01, // PHYSICAL_MINIMUM (1)
+ 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
+ 0x75, 0x02, // REPORT_SIZE (2)
+ 0x95, 0x01, // REPORT_COUNT (1)
+ 0xa4, // PUSH
+ 0xb1, 0x02, // FEATURE (Data,Var,Abs)
+ // ------------------------------ Vertical wheel
+ 0x09, 0x38, // USAGE (Wheel)
+ 0x15, 0x81, // LOGICAL_MINIMUM (-127)
+ 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
+ 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
+ 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x81, 0x06, // INPUT (Data,Var,Rel)
+ 0xc0, // END_COLLECTION
+ 0xa1, 0x02, // COLLECTION (Logical)
+ // ------------------------------ Horizontal wheel res multiplier
+ 0x09, 0x48, // USAGE (Resolution Multiplier)
+ 0xb4, // POP
+ 0xb1, 0x02, // FEATURE (Data,Var,Abs)
+ // ------------------------------ Padding for Feature report
+ 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
+ 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
+ 0x75, 0x04, // REPORT_SIZE (4)
+ 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
+ // ------------------------------ Horizontal wheel
+ 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
+ 0x0a, 0x38, 0x02, // USAGE (AC Pan)
+ 0x15, 0x81, // LOGICAL_MINIMUM (-127)
+ 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x81, 0x06, // INPUT (Data,Var,Rel)
+ 0xc0, // END_COLLECTION
+ 0xc0, // END_COLLECTION
+ 0xc0, // END_COLLECTION
+ 0xc0 // END_COLLECTION
+*/
+};
+
+
+/*
+ * Descriptor for compite device: Keyboard + Mouse
+ *
+ * contains: device, interface, HID and endpoint descriptors
+ */
+#if USB_CFG_DESCR_PROPS_CONFIGURATION
+PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
+ 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
+ USBDESCR_CONFIG, /* descriptor type */
+ 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
+ //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
+ /* total length of data returned (including inlined descriptors) */
+ 2, /* number of interfaces in this configuration */
+ 1, /* index of this configuration */
+ 0, /* configuration name string index */
+#if USB_CFG_IS_SELF_POWERED
+ (1 << 7) | USBATTR_SELFPOWER, /* attributes */
+#else
+ (1 << 7), /* attributes */
+#endif
+ USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
+
+ /*
+ * Keyboard interface
+ */
+ /* Interface descriptor */
+ 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
+ USBDESCR_INTERFACE, /* descriptor type */
+ 0, /* index of this interface */
+ 0, /* alternate setting for this interface */
+ USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
+ USB_CFG_INTERFACE_CLASS,
+ USB_CFG_INTERFACE_SUBCLASS,
+ USB_CFG_INTERFACE_PROTOCOL,
+ 0, /* string index for interface */
+ /* HID descriptor */
+ 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
+ USBDESCR_HID, /* descriptor type: HID */
+ 0x01, 0x01, /* BCD representation of HID version */
+ 0x00, /* target country code */
+ 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
+ 0x22, /* descriptor type: report */
+ sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
+ /* Endpoint descriptor */
+#if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
+ 7, /* sizeof(usbDescrEndpoint) */
+ USBDESCR_ENDPOINT, /* descriptor type = endpoint */
+ (char)0x81, /* IN endpoint number 1 */
+ 0x03, /* attrib: Interrupt endpoint */
+ 8, 0, /* maximum packet size */
+ USB_CFG_INTR_POLL_INTERVAL, /* in ms */
+#endif
+
+ /*
+ * Mouse interface
+ */
+ /* Interface descriptor */
+ 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
+ USBDESCR_INTERFACE, /* descriptor type */
+ 1, /* index of this interface */
+ 0, /* alternate setting for this interface */
+ USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
+ 0x03, /* CLASS: HID */
+ 0, /* SUBCLASS: none */
+ 0, /* PROTOCOL: none */
+ 0, /* string index for interface */
+ /* HID descriptor */
+ 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
+ USBDESCR_HID, /* descriptor type: HID */
+ 0x01, 0x01, /* BCD representation of HID version */
+ 0x00, /* target country code */
+ 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
+ 0x22, /* descriptor type: report */
+ sizeof(mouse_hid_report), 0, /* total length of report descriptor */
+#if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
+ /* Endpoint descriptor */
+ 7, /* sizeof(usbDescrEndpoint) */
+ USBDESCR_ENDPOINT, /* descriptor type = endpoint */
+ (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
+ 0x03, /* attrib: Interrupt endpoint */
+ 8, 0, /* maximum packet size */
+ USB_CFG_INTR_POLL_INTERVAL, /* in ms */
+#endif
+};
+#endif
+
+
+USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
+{
+ usbMsgLen_t len = 0;
+
+ debug("usbFunctionDescriptor: ");
+ debug_hex(rq->bmRequestType); debug(" ");
+ debug_hex(rq->bRequest); debug(" ");
+ debug_hex16(rq->wValue.word); debug(" ");
+ debug_hex16(rq->wIndex.word); debug(" ");
+ debug_hex16(rq->wLength.word); debug("\n");
+
+ switch (rq->wValue.bytes[1]) {
+#if USB_CFG_DESCR_PROPS_CONFIGURATION
+ case USBDESCR_CONFIG:
+ usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
+ len = sizeof(usbDescriptorConfiguration);
+ break;
+#endif
+ case USBDESCR_HID:
+ usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 18);
+ len = 9;
+ break;
+ case USBDESCR_HID_REPORT:
+ /* interface index */
+ switch (rq->wIndex.word) {
+ case 0:
+ usbMsgPtr = keyboard_hid_report;
+ len = sizeof(keyboard_hid_report);
+ break;
+ case 1:
+ usbMsgPtr = mouse_hid_report;
+ len = sizeof(mouse_hid_report);
+ break;
+ }
+ break;
+ }
+ debug("desc len: "); debug_hex(len); debug("\n");
+ return len;
+}
--- /dev/null
+#ifndef HOST_VUSB_H
+#define HOST_VUSB_H
+
+void host_vusb_keyboard_send(void);
+
+#endif