From c035c27c1bd29db5d1977860fb6720c52dbe2f92 Mon Sep 17 00:00:00 2001 From: Shayne Holmes Date: Wed, 9 Apr 2014 15:54:16 -0700 Subject: [PATCH 1/6] Adding NKRO virtual dip-switch, using existing bit in keymap byte. This takes the last, reserved bit there, but doesn't necessitate a revision to the magic number because it doesn't alter byte order. Add reference to NKRO virtual dip-switch to documentation. --- README.md | 1 + common/bootmagic.c | 8 ++++++++ common/bootmagic.h | 3 +++ common/command.c | 1 + common/eeconfig.h | 1 + common/keymap.h | 2 +- 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6596dc33..a01de8c6 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,7 @@ To avoid configuring accidentally additive salt key `KC_SPACE` also needs to be - Disable Gui(`Left Gui`) - Swap Grave and Escape(`Grave`) - Swap BackSlash and BackSpace(`Back Slash`) +- Enable NKRO on boot(`N`) #### Default Layer - Set Default Layer to 0(`0`) diff --git a/common/bootmagic.c b/common/bootmagic.c index 036d4904..642d5fac 100644 --- a/common/bootmagic.c +++ b/common/bootmagic.c @@ -5,6 +5,7 @@ #include "bootloader.h" #include "debug.h" #include "keymap.h" +#include "host.h" #include "action_layer.h" #include "eeconfig.h" #include "bootmagic.h" @@ -76,8 +77,15 @@ void bootmagic(void) if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) { keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace; } + if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) { + keymap_config.nkro = !keymap_config.nkro; + } eeconfig_write_keymap(keymap_config.raw); +#ifdef NKRO_ENABLE + keyboard_nkro = keymap_config.nkro; +#endif + /* default layer */ uint8_t default_layer = 0; if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); } diff --git a/common/bootmagic.h b/common/bootmagic.h index 7c192239..8f6618f4 100644 --- a/common/bootmagic.h +++ b/common/bootmagic.h @@ -60,6 +60,9 @@ #ifndef BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE #define BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE KC_BSLASH #endif +#ifndef BOOTMAGIC_HOST_NKRO +#define BOOTMAGIC_HOST_NKRO KC_N +#endif /* diff --git a/common/command.c b/common/command.c index f6f27695..872880ea 100644 --- a/common/command.c +++ b/common/command.c @@ -151,6 +151,7 @@ static void print_eeconfig(void) print(".no_gui: "); print_dec(kc.no_gui); print("\n"); print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n"); print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n"); + print(".nkro: "); print_dec(kc.nkro); print("\n"); #ifdef BACKLIGHT_ENABLE backlight_config_t bc; diff --git a/common/eeconfig.h b/common/eeconfig.h index e1b5ae28..3cd1a174 100644 --- a/common/eeconfig.h +++ b/common/eeconfig.h @@ -47,6 +47,7 @@ along with this program. If not, see . #define EECONFIG_KEYMAP_NO_GUI (1<<4) #define EECONFIG_KEYMAP_SWAP_GRAVE_ESC (1<<5) #define EECONFIG_KEYMAP_SWAP_BACKSLASH_BACKSPACE (1<<6) +#define EECONFIG_KEYMAP_NKRO (1<<7) bool eeconfig_is_enabled(void); diff --git a/common/keymap.h b/common/keymap.h index bf32aced..4c3019a3 100644 --- a/common/keymap.h +++ b/common/keymap.h @@ -35,7 +35,7 @@ typedef union { bool no_gui:1; bool swap_grave_esc:1; bool swap_backslash_backspace:1; - bool reserved:1; + bool nkro:1; }; } keymap_config_t; keymap_config_t keymap_config; From 41cbb1bace05d82088a12d4a33ea40921a7aa9b4 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Tue, 13 May 2014 14:57:13 +0900 Subject: [PATCH 2/6] New branch for 6KRO feature --- common.mk | 4 ++ common/action_util.c | 94 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/common.mk b/common.mk index 2ca06daa..62ac0ff7 100644 --- a/common.mk +++ b/common.mk @@ -48,6 +48,10 @@ ifdef NKRO_ENABLE OPT_DEFS += -DNKRO_ENABLE endif +ifdef USB_6KRO_ENABLE + OPT_DEFS += -DUSB_6KRO_ENABLE +endif + ifdef SLEEP_LED_ENABLE SRC += $(COMMON_DIR)/sleep_led.c OPT_DEFS += -DSLEEP_LED_ENABLE diff --git a/common/action_util.c b/common/action_util.c index 99a3adaa..5f44b381 100644 --- a/common/action_util.c +++ b/common/action_util.c @@ -30,6 +30,15 @@ static inline void del_key_bit(uint8_t code); static uint8_t real_mods = 0; static uint8_t weak_mods = 0; +#ifdef USB_6KRO_ENABLE +#define RO_ADD(a, b) ((a + b) % REPORT_KEYS) +#define RO_SUB(a, b) ((a - b + REPORT_KEYS) % REPORT_KEYS) +#define RO_INC(a) RO_ADD(a, 1) +#define RO_DEC(a) RO_SUB(a, 1) +static int8_t cb_head = 0; +static int8_t cb_tail = 0; +static int8_t cb_count = 0; +#endif // TODO: pointer variable is not needed //report_keyboard_t keyboard_report = {}; @@ -158,7 +167,18 @@ uint8_t get_first_key(void) return i<<3 | biton(keyboard_report->nkro.bits[i]); } #endif +#ifdef USB_6KRO_ENABLE + uint8_t i = cb_head; + do { + if (keyboard_report->keys[i] != 0) { + break; + } + i = RO_INC(i); + } while (i != cb_tail); + return keyboard_report->keys[i]; +#else return keyboard_report->keys[0]; +#endif } @@ -166,6 +186,52 @@ uint8_t get_first_key(void) /* local functions */ static inline void add_key_byte(uint8_t code) { +#ifdef USB_6KRO_ENABLE + int8_t i = cb_head; + int8_t empty = -1; + if (cb_count) { + do { + if (keyboard_report->keys[i] == code) { + return; + } + if (empty == -1 && keyboard_report->keys[i] == 0) { + empty = i; + } + i = RO_INC(i); + } while (i != cb_tail); + if (i == cb_tail) { + if (cb_tail == cb_head) { + // buffer is full + if (empty == -1) { + // pop head when has no empty space + cb_head = RO_INC(cb_head); + cb_count--; + } + else { + // left shift when has empty space + uint8_t offset = 1; + i = RO_INC(empty); + do { + if (keyboard_report->keys[i] != 0) { + keyboard_report->keys[empty] = keyboard_report->keys[i]; + keyboard_report->keys[i] = 0; + empty = RO_INC(empty); + } + else { + offset++; + } + i = RO_INC(i); + } while (i != cb_tail); + cb_tail = RO_SUB(cb_tail, offset); + } + } + } + } + // add to tail + keyboard_report->keys[cb_tail] = code; + cb_tail = RO_INC(cb_tail); + cb_count++; +#else int8_t i = 0; int8_t empty = -1; for (; i < REPORT_KEYS; i++) { @@ -181,15 +247,43 @@ static inline void add_key_byte(uint8_t code) keyboard_report->keys[empty] = code; } } +#endif } static inline void del_key_byte(uint8_t code) { +#ifdef USB_6KRO_ENABLE + uint8_t i = cb_head; + if (cb_count) { + do { + if (keyboard_report->keys[i] == code) { + keyboard_report->keys[i] = 0; + cb_count--; + if (cb_count == 0) { + // reset head and tail + cb_tail = cb_head = 0; + } + if (i == RO_DEC(cb_tail)) { + // left shift when next to tail + do { + cb_tail = RO_DEC(cb_tail); + if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) { + break; + } + } while (cb_tail != cb_head); + } + break; + } + i = RO_INC(i); + } while (i != cb_tail); + } +#else for (uint8_t i = 0; i < REPORT_KEYS; i++) { if (keyboard_report->keys[i] == code) { keyboard_report->keys[i] = 0; } } +#endif } #ifdef NKRO_ENABLE From dd73ce4a6b799b7a506a4df1ee0da6718f3347e6 Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 15 Jun 2014 14:04:16 +0900 Subject: [PATCH 3/6] Fix debug print of ps2_mouse --- protocol/ps2_mouse.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/protocol/ps2_mouse.c b/protocol/ps2_mouse.c index 4702f12c..c3e8b3c1 100644 --- a/protocol/ps2_mouse.c +++ b/protocol/ps2_mouse.c @@ -82,9 +82,14 @@ void ps2_mouse_task(void) mouse_report.x = ps2_host_recv_response(); mouse_report.y = ps2_host_recv_response(); } else { - if (!debug_mouse) print("ps2_mouse: fail to get mouse packet\n"); + if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n"); return; } + xprintf("%ud ", timer_read()); + print("ps2_mouse raw: ["); + phex(mouse_report.buttons); print("|"); + print_hex8((uint8_t)mouse_report.x); print(" "); + print_hex8((uint8_t)mouse_report.y); print("]\n"); /* if mouse moves or buttons state changes */ if (mouse_report.x || mouse_report.y || From d4e92b43e02ef2a88a52bc4938b9a5e5a3fdd0ef Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 10 Jul 2014 15:28:36 +0900 Subject: [PATCH 4/6] Fix READEM of hhkb and doc/build.md --- doc/build.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/doc/build.md b/doc/build.md index bfe5de9f..20702e94 100644 --- a/doc/build.md +++ b/doc/build.md @@ -5,23 +5,22 @@ Build Firmware and Program Controller Download and Install -------------------- ### 1. Install Tools -First, you need tools to build firmware and program your controller. I assume you are on Windows here. -1. **Toolchain** Install [WinAVR][winavr]. This is old but works well for this purpose. `WinAVR` is a tool set to build firmware including C compiler(gcc) and make commands. You can use [CrossPack][crosspack] instead if you are on Mac. +1. **Toolchain** On Windows install [MHV AVR Tools][mhv] for AVR GCC compiler and [Cygwin][cygwin](or [MinGW][mingw]) for shell terminal. On Mac you can use [CrossPack][crosspack]. On Linux you can install AVR GCC with your favorite package manager. -2. **Programmer** Install [Atmel FLIP][flip]. `FLIP` is a tool to program(load) firmware into AVR controller via DFU bootloader. AVR USB chips including ATmega32U4 has DFU bootloader by factory default. You can also use [dfu-programmer][dfu-prog] instead if you are on Mac or Linux. +2. **Programmer** On Windows install [Atmel FLIP][flip]. On Mac and Linux install [dfu-programmer][dfu-prog]. -3. **Driver** At first time you start DFU bootloader on Chip 'Found New Hardware Wizard' will come up on Windows. If you install device driver properly you can find chip name like 'ATmega32U4' under 'LibUSB-Win32 Devices' tree on 'Device Manager'. If not you shall need to update its driver on 'Device Manager'. You will find the driver in `FLIP` install directory like: C:\Program Files (x86)\Atmel\Flip 3.4.5\usb\. If you use `dfu-programmer` install its driver. +3. **Driver** On Windows you start DFU bootloader on the chip first time you will see 'Found New Hardware Wizard' to install driver. If you install device driver properly you can find chip name like 'ATmega32U4' under 'LibUSB-Win32 Devices' tree on 'Device Manager'. If not you shall need to update its driver on 'Device Manager'. You will find the driver in `FLIP` install directory like: C:\Program Files (x86)\Atmel\Flip 3.4.5\usb\. In case of `dfu-programmer` use its driver. If you use PJRC Teensy you don't need step 2 and 3 above, just get [Teensy loader][teensy-loader]. ### 2. Download source -You can find firmware source at github: +You can find firmware source at github: - -If you are familiar with `Git` tools you are recommended to use it but you can also download zip archive from: +If you are familiar with `Git` tools you are recommended to use it but you can also download zip archive from: - @@ -29,7 +28,7 @@ If you are familiar with `Git` tools you are recommended to use it but you can a Build firmware -------------- ### 1. Open terminal -Open terminal window to get access to commands. You can use `cmd` in Windows or `Terminal.app` on Mac OSX. In Windows press `Windows` key and `R` then enter `cmd` in 'Run command' dialog showing up. +Open terminal window to get access to commands. Use Cygwin(or MingGW) `shell terminal` in Windows or `Terminal.app` on Mac OSX. In Windows press `Windows` key and `R` then enter `cmd` in 'Run command' dialog showing up. ### 2. Change directory Move to project directory in the firmware source. @@ -100,6 +99,9 @@ You may want to use other programmer like `avrdude` with AVRISPmkII, Arduino or $ make -f Makefile. program +[cygwin]: https://www.cygwin.com/ +[mingw]: http://www.mingw.org/ +[mhv]: https://infernoembedded.com/products/avr-tools [winavr]: http://winavr.sourceforge.net/ [crosspack]: http://www.obdev.at/products/crosspack/index.html [flip]: http://www.atmel.com/tools/FLIP.aspx @@ -116,14 +118,18 @@ Makefile Options #MCU = at90usb1286 # Teensy++ 2.0 F_CPU = 16000000 +Set your MCU and its clock in Hz. + # Boot Section Size in *bytes* # Teensy halfKay 512 # Atmel DFU loader 4096 # LUFA bootloader 4096 OPT_DEFS += -DBOOTLOADER_SIZE=4096 +If you are using PJRC Teensy use `512` for `BOOTLOADER_SIZE`, otherwise use `4096` unless you are sure. + ### 2. Features -Optional. Note that ***comment out*** to disable them. +Optional. Note that ***comment out*** with `#` to disable them. BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) MOUSEKEY_ENABLE = yes # Mouse keys(+4700) @@ -152,7 +158,7 @@ Config.h Options ---------------- ### 1. Magic command key combination - #define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KB_LSHIFT) | MOD_BIT(KB_RSHIFT))) + #define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KB_LSHIFT) | MOD_BIT(KB_RSHIFT))) ### 2. Mechanical Locking Support for CapsLock From b75ece52a2cd3db075c2a792d5d7b86194aef9aa Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 20 Jul 2014 00:25:06 +0900 Subject: [PATCH 5/6] Fix NKRO code when NKRO is disable --- protocol/lufa/lufa.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protocol/lufa/lufa.c b/protocol/lufa/lufa.c index d60aecc3..16a602df 100644 --- a/protocol/lufa/lufa.c +++ b/protocol/lufa/lufa.c @@ -272,7 +272,9 @@ void EVENT_USB_Device_ControlRequest(void) // Interface switch (USB_ControlRequest.wIndex) { case KEYBOARD_INTERFACE: +#ifdef NKRO_ENABLE case NKRO_INTERFACE: +#endif Endpoint_ClearSETUP(); while (!(Endpoint_IsOUTReceived())) { From 472603fe3e66e3a025463fe401ab2ec81035669b Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 21 Jul 2014 18:17:52 +0900 Subject: [PATCH 6/6] Just ignore ADB Service Request - to support Adjustable keyboard(composite device?) --- protocol/adb.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/protocol/adb.c b/protocol/adb.c index a4783f36..f57afac9 100644 --- a/protocol/adb.c +++ b/protocol/adb.c @@ -128,6 +128,10 @@ uint16_t adb_host_kbd_recv(void) attention(); send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00) place_bit0(); // Stopbit(0) + if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored + sei(); + return -30; // something wrong + } if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us) sei(); return 0; // No data to send