From be072391e6950b688a37ffbf28ad6df362ae5abb Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 21 Jan 2019 17:11:27 +0100 Subject: [PATCH] Initial support for KBDFans KBD4x KBDFans' KBD4x is a DIY kit with underglow: https://kbdfans.cn/collections/diy-kit/products/kbd4x-custom-mechanical-keyboard-hot-swap-diy-kit Signed-off-by: Gergely Nagy --- examples/Devices/KBD4x/.gitignore | 1 + .../Devices/KBD4x/.kaleidoscope-builder.conf | 26 +++++ examples/Devices/KBD4x/KBD4x.ino | 96 +++++++++++++++++++ examples/Devices/KBD4x/Makefile | 55 +++++++++++ src/Kaleidoscope-Hardware-KBDFans-KBD4x.h | 23 +++++ src/kaleidoscope/hardware/kbdfans/KBD4x.cpp | 62 ++++++++++++ src/kaleidoscope/hardware/kbdfans/KBD4x.h | 89 +++++++++++++++++ 7 files changed, 352 insertions(+) create mode 100644 examples/Devices/KBD4x/.gitignore create mode 100644 examples/Devices/KBD4x/.kaleidoscope-builder.conf create mode 100644 examples/Devices/KBD4x/KBD4x.ino create mode 100644 examples/Devices/KBD4x/Makefile create mode 100644 src/Kaleidoscope-Hardware-KBDFans-KBD4x.h create mode 100644 src/kaleidoscope/hardware/kbdfans/KBD4x.cpp create mode 100644 src/kaleidoscope/hardware/kbdfans/KBD4x.h diff --git a/examples/Devices/KBD4x/.gitignore b/examples/Devices/KBD4x/.gitignore new file mode 100644 index 00000000..9b1960e7 --- /dev/null +++ b/examples/Devices/KBD4x/.gitignore @@ -0,0 +1 @@ +output/ \ No newline at end of file diff --git a/examples/Devices/KBD4x/.kaleidoscope-builder.conf b/examples/Devices/KBD4x/.kaleidoscope-builder.conf new file mode 100644 index 00000000..f03e9e10 --- /dev/null +++ b/examples/Devices/KBD4x/.kaleidoscope-builder.conf @@ -0,0 +1,26 @@ +flash_over_usb() { + sleep 1s + + dfu-programmer ${MCU} erase + dfu-programmer ${MCU} flash "${HEX_FILE_PATH}" + dfu-programmer ${MCU} start +} + +flash () { + prepare_to_flash + + # This is defined in the (optional) user config. + # shellcheck disable=SC2154 + ${preFlash_HOOKS} + + flash_over_usb || flash_over_usb + + # This is defined in the (optional) user config. + # shellcheck disable=SC2154 + ${postFlash_HOOKS} +} + +NO_RESET=1 + +DEFAULT_SKETCH="KBD4x" +BOARD="kbd4x" diff --git a/examples/Devices/KBD4x/KBD4x.ino b/examples/Devices/KBD4x/KBD4x.ino new file mode 100644 index 00000000..b0b24974 --- /dev/null +++ b/examples/Devices/KBD4x/KBD4x.ino @@ -0,0 +1,96 @@ +/* -*- mode: c++ -*- + * KBD4x -- A very basic Kaleidoscope example for the KBDFans KBD4x keyboard + * Copyright (C) 2019 Keyboard.io, Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTabILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Kaleidoscope.h" +#include "Kaleidoscope-Macros.h" + +enum { + _QWERTY, + _FN +}; + +enum { + RESET +}; + +#define MO(n) ShiftToLayer(n) + +/* *INDENT-OFF* */ +KEYMAPS( + +/* Qwerty + * ,-------------------------------------------------------------------------. + * | Esc | Q | W | E | R | T | Y | U | I | O | P | Bksp | + * |------+-----+-----+-----+-----+-----------+-----+-----+-----+-----+------| + * | Tab | A | S | D | F | G | H | J | K | L | ; | " | + * |------+-----+-----+-----+-----+-----|-----+-----+-----+-----+-----+------| + * | Shift| Z | X | C | V | B | N | M | , | . | Up |Enter | + * |------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------| + * | Ctrl | GUI | 1 | 2 | 3 | Space | FN | / | Lft | Dn |Right | + * `-------------------------------------------------------------------------' + */ + +[_QWERTY] = KEYMAP( + Key_Escape ,Key_Q ,Key_W ,Key_E ,Key_R ,Key_T ,Key_Y ,Key_U ,Key_I ,Key_O ,Key_P ,Key_Backspace + ,Key_Tab ,Key_A ,Key_S ,Key_D ,Key_F ,Key_G ,Key_H ,Key_J ,Key_K ,Key_L ,Key_Semicolon ,Key_Quote + ,Key_LeftShift ,Key_Z ,Key_X ,Key_C ,Key_V ,Key_B ,Key_N ,Key_M ,Key_Comma ,Key_Period ,Key_UpArrow ,Key_Enter + ,Key_LeftControl ,Key_LeftGui ,Key_1 ,Key_2 ,Key_3 ,Key_Space ,MO(_FN) ,Key_Slash ,Key_LeftArrow ,Key_DownArrow ,Key_RightArrow +), + +/* Fn + * ,-----------------------------------------------------------------------------------. + * | ` | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | | + * |------+------+------+------+------+-------------+------+------+------+------+------| + * | | | | | | | | | | | | RST | + * |------+------+------+------+------+------|------+------+------+------+------+------| + * | | | | | | | | | | | | | + * |------+------+------+------+------+------+------+------+------+------+------+------| + * | | | | | | | | | | | | + * `-----------------------------------------------------------------------------------' + */ +[_FN] = KEYMAP( + Key_Backtick ,Key_1 ,Key_2 ,Key_3 ,Key_4 ,Key_5 ,Key_6 ,Key_7 ,Key_8 ,Key_9 ,Key_0 ,___ + ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,M(RESET) + ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ + ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ ,___ +) +); +/* *INDENT-ON* */ + +KALEIDOSCOPE_INIT_PLUGINS(Macros); + +const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { + switch (macroIndex) { + case RESET: + KBD4x.resetDevice(); + break; + default: + break; + } + + return MACRO_NONE; +} + +void setup() { + Kaleidoscope.setup(); +} + +void loop() { + Kaleidoscope.loop(); +} diff --git a/examples/Devices/KBD4x/Makefile b/examples/Devices/KBD4x/Makefile new file mode 100644 index 00000000..c408b29b --- /dev/null +++ b/examples/Devices/KBD4x/Makefile @@ -0,0 +1,55 @@ +# This stub makefile for a Kaleidoscope example pulls in all the targets +# required to build the example + +UNAME_S := $(shell uname -s) + +ifeq ($(UNAME_S),Darwin) +SKETCHBOOK_DIR ?= $(HOME)/Documents/Arduino +PACKAGE_DIR ?= $(HOME)/Library/Arduino15 +else +SKETCHBOOK_DIR ?= $(HOME)/Arduino +PACKAGE_DIR ?= $(HOME)/.arduino15 +endif + + +ARDUINO_INSTALLED_ENV=$(shell ls -dt $(PACKAGE_DIR)/packages/keyboardio/hardware/avr 2>/dev/null |head -n 1) +MANUALLY_INSTALLED_ENV=$(shell ls -dt $(SKETCHBOOK_DIR)/hardware/keyboardio/avr 2>/dev/null |head -n 1) + + + +ifneq ("$(wildcard $(ARDUINO_INSTALLED_ENV)/boards.txt)","") + +ifneq ("$(wildcard $(MANUALLY_INSTALLED_ENV)/boards.txt)","") + +$(info ***************************************************************************) +$(info It appears that you have installed two copies of Kaleidoscope. One copy was) +$(info installed using Arduino's "Board Manager", while the other was installed by) +$(info hand, probably using "git".) +$(info ) +$(info This will likely cause some trouble as you try to build keyboard firmware) +$(info using Kaleidoscope. You may want to remove either: ) +$(info ) +$(info $(PACKAGE_DIR)/packages/keyboardio/ which was installed using Arduino) +$(info ) +$(info or) +$(info ) +$(info $(SKETCHBOOK_DIR)/hardware/keyboardio/ which was installed by hand.) +$(info ) +$(info ***************************************************************************) +$(info ) + +endif + +BOARD_HARDWARE_PATH = $(ARDUINO_INSTALLED_ENV) +KALEIDOSCOPE_PLUGIN_MAKEFILE_DIR ?= build-tools/makefiles/ +KALEIDOSCOPE_BUILDER_DIR ?= $(ARDUINO_INSTALLED_ENV)/libraries/Kaleidoscope/bin/ + + + +endif + + +BOARD_HARDWARE_PATH ?= $(SKETCHBOOK_DIR)/hardware +KALEIDOSCOPE_PLUGIN_MAKEFILE_DIR ?= keyboardio/avr/build-tools/makefiles/ + +include $(BOARD_HARDWARE_PATH)/$(KALEIDOSCOPE_PLUGIN_MAKEFILE_DIR)/rules.mk diff --git a/src/Kaleidoscope-Hardware-KBDFans-KBD4x.h b/src/Kaleidoscope-Hardware-KBDFans-KBD4x.h new file mode 100644 index 00000000..4490cf97 --- /dev/null +++ b/src/Kaleidoscope-Hardware-KBDFans-KBD4x.h @@ -0,0 +1,23 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-Hardware-KBDFans-KBD4x -- KBD4x hardware support for Kaleidoscope + * Copyright (C) 2019 Keyboard.io, Inc + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#define KALEIDOSCOPE_WITH_ATMEGA_KEYBOARD 1 + +#include "kaleidoscope/hardware/kbdfans/KBD4x.h" diff --git a/src/kaleidoscope/hardware/kbdfans/KBD4x.cpp b/src/kaleidoscope/hardware/kbdfans/KBD4x.cpp new file mode 100644 index 00000000..83081118 --- /dev/null +++ b/src/kaleidoscope/hardware/kbdfans/KBD4x.cpp @@ -0,0 +1,62 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-Hardware-KBDFans-KBD4x -- KBD4x hardware support for Kaleidoscope + * Copyright (C) 2019 Keyboard.io, Inc + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of version 3 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef ARDUINO_AVR_KBD4X + +#include +#include +#include + + +namespace kaleidoscope { +namespace hardware { +namespace kbdfans { + +ATMEGA_KEYBOARD_DATA(KBD4x); +constexpr int8_t KBD4x::led_count; + +#define BOOTLOADER_RESET_KEY 0xB007B007 +uint32_t reset_key __attribute__ ((section (".noinit"))); + +/* + * This function runs before main(), and jumps to the bootloader after a reset + * initiated by .resetDevice(). + */ +void _bootloader_jump_after_watchdog_reset() + __attribute__((used, naked, section(".init3"))); +void _bootloader_jump_after_watchdog_reset() { + if ((MCUSR & (1<> 1))(); + } +} + +void KBD4x::resetDevice() { + reset_key = BOOTLOADER_RESET_KEY; + wdt_enable(WDTO_250MS); + for (;;); +} + +} +} +} + +HARDWARE_IMPLEMENTATION KeyboardHardware; +kaleidoscope::hardware::kbdfans::KBD4x &KBD4x = KeyboardHardware; + +#endif diff --git a/src/kaleidoscope/hardware/kbdfans/KBD4x.h b/src/kaleidoscope/hardware/kbdfans/KBD4x.h new file mode 100644 index 00000000..85925bf9 --- /dev/null +++ b/src/kaleidoscope/hardware/kbdfans/KBD4x.h @@ -0,0 +1,89 @@ +/* -*- mode: c++ -*- + * Kaleidoscope-Hardware-KBDFans-KBD4x -- KBD4x hardware support for Kaleidoscope + * Copyright (C) 2019 Keyboard.io, Inc + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of version 3 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#ifdef ARDUINO_AVR_KBD4X + +#include +#define HARDWARE_IMPLEMENTATION kaleidoscope::hardware::kbdfans::KBD4x +#include "Kaleidoscope-HIDAdaptor-KeyboardioHID.h" + +#include "kaleidoscope/macro_helpers.h" +#include "kaleidoscope/hardware/avr/pins_and_ports.h" + +#include "kaleidoscope/hardware/ATMegaKeyboard.h" + +namespace kaleidoscope { +namespace hardware { +namespace kbdfans { +class KBD4x: public kaleidoscope::hardware::ATMegaKeyboard { + public: + KBD4x(void) { + /* These two lines here are the result of many hours spent chasing ghosts. + * These are great lines, and we love them dearly, for they make a set of + * pins that would otherwise be reserved for JTAG accessible from the + * firmware. + * + * Most AVR chips that get put into keyboards have the JTAG port disabled in + * fuses, but some do not. When they're used for JTAG, then no matter what + * we do in the firmware, they will not be affected. So in case JTAG is not + * disabled in fuses, we want to do that in the firmware. Luckily for us, + * that's doable, we just have to write the JTD bit into MCUCR twice within + * four cycles. These two lines do just that. + * + * For more information, see the ATMega16U4/ATMega32U4 datasheet, the + * following sections: + * - 2.2.7 (PIN Descriptions; PIN F) + * - 7.8.7 (On-chip Debug System) + * - 26.5.1 (MCU Control Register – MCUCR) + */ + MCUCR |= (1 << JTD); + MCUCR |= (1 << JTD); + } + + ATMEGA_KEYBOARD_CONFIG( + ROW_PIN_LIST({ PIN_D0, PIN_D1, PIN_D2, PIN_D3 }), + COL_PIN_LIST({ PIN_F0, PIN_F1, PIN_F4, PIN_F5, PIN_F6, PIN_F7, PIN_B3, PIN_B1, PIN_B0, PIN_D5, PIN_B7, PIN_C7 }) + ); + + static constexpr int8_t led_count = 0; + + void resetDevice(); +}; + +#define KEYMAP( \ + R0C0, R0C1, R0C2, R0C3, R0C4, R0C5, R0C6, R0C7, R0C8, R0C9, R0C10, R0C11, \ + R1C0, R1C1, R1C2, R1C3, R1C4, R1C5, R1C6, R1C7, R1C8, R1C9, R1C10, R1C11, \ + R2C0, R2C1, R2C2, R2C3, R2C4, R2C5, R2C6, R2C7, R2C8, R2C9, R2C10, R2C11, \ + R3C0, R3C1, R3C2, R3C3, R3C4, R3C5, R3C7, R3C8, R3C9, R3C10, R3C11 \ + ) { \ + { R0C0, R0C1, R0C2, R0C3, R0C4, R0C5, R0C6, R0C7, R0C8, R0C9, R0C10, R0C11, }, \ + { R1C0, R1C1, R1C2, R1C3, R1C4, R1C5, R1C6, R1C7, R1C8, R1C9, R1C10, R1C11, }, \ + { R2C0, R2C1, R2C2, R2C3, R2C4, R2C5, R2C6, R2C7, R2C8, R2C9, R2C10, R2C11, }, \ + { R3C0, R3C1, R3C2, R3C3, R3C4, R3C5, R3C5, R3C7, R3C8, R3C9, R3C10, R3C11, } \ + } +} + +} +} + +#include "kaleidoscope/hardware/key_indexes.h" + +extern kaleidoscope::hardware::kbdfans::KBD4x &KBD4x; + +#endif