summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilba <Jason.S.Williams@gmail.com>2020-01-04 07:52:00 +1100
committerFlorian Didron <fdidron@users.noreply.github.com>2020-02-26 10:15:12 +0900
commit9b9e5e1d474c4386830544a9487c1977fed93e31 (patch)
treefbf734d5fd6af4110a0b2d2d252fa6e14a3b716d
parente41ab50016345f7813d250772457a1463e0cd973 (diff)
VIA configurator refactor
-rw-r--r--common_features.mk40
-rw-r--r--quantum/dynamic_keymap.c55
-rw-r--r--quantum/quantum.c7
-rw-r--r--quantum/quantum.h8
-rw-r--r--quantum/via.c400
-rw-r--r--quantum/via.h151
-rw-r--r--show_options.mk3
-rw-r--r--tmk_core/common/eeconfig.h2
-rw-r--r--tmk_core/common/keyboard.c6
9 files changed, 628 insertions, 44 deletions
diff --git a/common_features.mk b/common_features.mk
index e6634712f0..1ccffc41bd 100644
--- a/common_features.mk
+++ b/common_features.mk
@@ -82,19 +82,19 @@ endif
ifeq ($(strip $(UCIS_ENABLE)), yes)
OPT_DEFS += -DUCIS_ENABLE
- UNICODE_COMMON = yes
+ UNICODE_COMMON := yes
SRC += $(QUANTUM_DIR)/process_keycode/process_ucis.c
endif
ifeq ($(strip $(UNICODEMAP_ENABLE)), yes)
OPT_DEFS += -DUNICODEMAP_ENABLE
- UNICODE_COMMON = yes
+ UNICODE_COMMON := yes
SRC += $(QUANTUM_DIR)/process_keycode/process_unicodemap.c
endif
ifeq ($(strip $(UNICODE_ENABLE)), yes)
OPT_DEFS += -DUNICODE_ENABLE
- UNICODE_COMMON = yes
+ UNICODE_COMMON := yes
SRC += $(QUANTUM_DIR)/process_keycode/process_unicode.c
endif
@@ -107,13 +107,13 @@ ifeq ($(strip $(RGBLIGHT_ENABLE)), yes)
OPT_DEFS += -DRGBLIGHT_ENABLE
SRC += $(QUANTUM_DIR)/color.c
SRC += $(QUANTUM_DIR)/rgblight.c
- CIE1931_CURVE = yes
- LED_BREATHING_TABLE = yes
- RGB_KEYCODES_ENABLE = yes
+ CIE1931_CURVE := yes
+ LED_BREATHING_TABLE := yes
+ RGB_KEYCODES_ENABLE := yes
ifeq ($(strip $(RGBLIGHT_CUSTOM_DRIVER)), yes)
OPT_DEFS += -DRGBLIGHT_CUSTOM_DRIVER
else
- WS2812_DRIVER_REQUIRED = yes
+ WS2812_DRIVER_REQUIRED := yes
endif
endif
@@ -147,12 +147,12 @@ endif
SRC += $(QUANTUM_DIR)/color.c
SRC += $(QUANTUM_DIR)/rgb_matrix.c
SRC += $(QUANTUM_DIR)/rgb_matrix_drivers.c
- CIE1931_CURVE = yes
- RGB_KEYCODES_ENABLE = yes
+ CIE1931_CURVE := yes
+ RGB_KEYCODES_ENABLE := yes
endif
ifeq ($(strip $(RGB_MATRIX_ENABLE)), yes)
- RGB_MATRIX_ENABLE = IS31FL3731
+ RGB_MATRIX_ENABLE := IS31FL3731
endif
ifeq ($(strip $(RGB_MATRIX_ENABLE)), IS31FL3731)
@@ -178,7 +178,7 @@ endif
ifeq ($(strip $(RGB_MATRIX_ENABLE)), WS2812)
OPT_DEFS += -DWS2812
- WS2812_DRIVER_REQUIRED = yes
+ WS2812_DRIVER_REQUIRED := yes
endif
ifeq ($(strip $(RGB_MATRIX_CUSTOM_KB)), yes)
@@ -232,12 +232,12 @@ endif
endif
ifeq ($(strip $(LCD_ENABLE)), yes)
- CIE1931_CURVE = yes
+ CIE1931_CURVE := yes
endif
# backward compat
ifeq ($(strip $(BACKLIGHT_CUSTOM_DRIVER)), yes)
- BACKLIGHT_DRIVER = custom
+ BACKLIGHT_DRIVER := custom
endif
VALID_BACKLIGHT_TYPES := pwm software custom
@@ -250,7 +250,7 @@ ifeq ($(strip $(BACKLIGHT_ENABLE)), yes)
endif
ifeq ($(strip $(VISUALIZER_ENABLE)), yes)
- CIE1931_CURVE = yes
+ CIE1931_CURVE := yes
endif
COMMON_VPATH += $(QUANTUM_DIR)/backlight
@@ -294,12 +294,12 @@ endif
ifeq ($(strip $(CIE1931_CURVE)), yes)
OPT_DEFS += -DUSE_CIE1931_CURVE
- LED_TABLES = yes
+ LED_TABLES := yes
endif
ifeq ($(strip $(LED_BREATHING_TABLE)), yes)
OPT_DEFS += -DUSE_LED_BREATHING_TABLE
- LED_TABLES = yes
+ LED_TABLES := yes
endif
ifeq ($(strip $(LED_TABLES)), yes)
@@ -356,6 +356,14 @@ ifeq ($(strip $(ORYX_ENABLE)), yes)
OPT_DEFS += -DORYX_ENABLE
endif
+ifeq ($(strip $(VIA_ENABLE)), yes)
+ DYNAMIC_KEYMAP_ENABLE := yes
+ RAW_ENABLE := yes
+ BOOTMAGIC_ENABLE := lite
+ SRC += $(QUANTUM_DIR)/via.c
+ OPT_DEFS += -DVIA_ENABLE
+endif
+
ifeq ($(strip $(DYNAMIC_KEYMAP_ENABLE)), yes)
OPT_DEFS += -DDYNAMIC_KEYMAP_ENABLE
SRC += $(QUANTUM_DIR)/dynamic_keymap.c
diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c
index ca056f630e..11d28592d6 100644
--- a/quantum/dynamic_keymap.c
+++ b/quantum/dynamic_keymap.c
@@ -20,28 +20,37 @@
#include "progmem.h" // to read default from flash
#include "quantum.h" // for send_string()
#include "dynamic_keymap.h"
-
-#ifdef DYNAMIC_KEYMAP_ENABLE
-
-# ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
-# error DYNAMIC_KEYMAP_EEPROM_ADDR not defined
-# endif
-
-# ifndef DYNAMIC_KEYMAP_LAYER_COUNT
-# error DYNAMIC_KEYMAP_LAYER_COUNT not defined
-# endif
-
-# ifndef DYNAMIC_KEYMAP_MACRO_COUNT
-# error DYNAMIC_KEYMAP_MACRO_COUNT not defined
-# endif
-
-# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
-# error DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR not defined
-# endif
-
-# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
-# error DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE not defined
-# endif
+#include "via.h" // for default VIA_EEPROM_ADDR_END
+
+#ifndef DYNAMIC_KEYMAP_LAYER_COUNT
+# define DYNAMIC_KEYMAP_LAYER_COUNT 4
+#endif
+
+#ifndef DYNAMIC_KEYMAP_MACRO_COUNT
+# define DYNAMIC_KEYMAP_MACRO_COUNT 16
+#endif
+
+// If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h,
+// default it start after VIA_EEPROM_CUSTOM_ADDR+VIA_EEPROM_CUSTOM_SIZE
+#ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
+# ifdef VIA_EEPROM_CUSTOM_CONFIG_ADDR
+# define DYNAMIC_KEYMAP_EEPROM_ADDR (VIA_EEPROM_CUSTOM_CONFIG_ADDR+VIA_EEPROM_CUSTOM_CONFIG_SIZE)
+# else
+# error DYNAMIC_KEYMAP_EEPROM_ADDR not defined
+# endif
+#endif
+
+// Dynamic macro starts after dynamic keymaps
+#ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
+# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR+(DYNAMIC_KEYMAP_LAYER_COUNT*MATRIX_ROWS*MATRIX_COLS*2))
+#endif
+
+// Dynamic macro uses up all remaining memory
+// Assumes 1K EEPROM on ATMega32U4
+// Override for anything different
+#ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
+# define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (1024-DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR)
+#endif
uint8_t dynamic_keymap_get_layer_count(void) { return DYNAMIC_KEYMAP_LAYER_COUNT; }
@@ -208,5 +217,3 @@ void dynamic_keymap_macro_send(uint8_t id) {
send_string(data);
}
}
-
-#endif // DYNAMIC_KEYMAP_ENABLE
diff --git a/quantum/quantum.c b/quantum/quantum.c
index 755686b341..c25877b8c6 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -210,6 +210,9 @@ bool process_record_quantum(keyrecord_t *record) {
#if defined(RGB_MATRIX_ENABLE)
process_rgb_matrix(keycode, record) &&
#endif
+#if defined(VIA_ENABLE)
+ process_record_via(keycode, record) &&
+#endif
process_record_kb(keycode, record) &&
#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
process_midi(keycode, record) &&
@@ -555,9 +558,7 @@ __attribute__((weak)) void bootmagic_lite(void) {
// We need multiple scans because debouncing can't be turned off.
matrix_scan();
-#if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
- wait_ms(DEBOUNCING_DELAY * 2);
-#elif defined(DEBOUNCE) && DEBOUNCE > 0
+#if defined(DEBOUNCE) && DEBOUNCE > 0
wait_ms(DEBOUNCE * 2);
#else
wait_ms(30);
diff --git a/quantum/quantum.h b/quantum/quantum.h
index 7548ed8b93..318c17a13e 100644
--- a/quantum/quantum.h
+++ b/quantum/quantum.h
@@ -170,6 +170,14 @@ extern layer_state_t layer_state;
#include "process_dynamic_macro.h"
#endif
+#ifdef DYNAMIC_KEYMAP_ENABLE
+# include "dynamic_keymap.h"
+#endif
+
+#ifdef VIA_ENABLE
+# include "via.h"
+#endif
+
// Function substitutions to ease GPIO manipulation
#if defined(__AVR__)
typedef uint8_t pin_t;
diff --git a/quantum/via.c b/quantum/via.c
new file mode 100644
index 0000000000..64b05324a1
--- /dev/null
+++ b/quantum/via.c
@@ -0,0 +1,400 @@
+/* Copyright 2019 Jason Williams (Wilba)
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef RAW_ENABLE
+# error "RAW_ENABLE is not enabled"
+#endif
+
+#ifndef DYNAMIC_KEYMAP_ENABLE
+# error "DYNAMIC_KEYMAP_ENABLE is not enabled"
+#endif
+
+#include "quantum.h"
+
+#include "via.h"
+#include "raw_hid.h"
+#include "dynamic_keymap.h"
+#include "tmk_core/common/eeprom.h"
+#include "version.h" // for QMK_BUILDDATE used in EEPROM magic
+
+// Can be called in an overriding via_init_kb() to test if keyboard level code usage of
+// EEPROM is invalid and use/save defaults.
+bool via_eeprom_is_valid(void)
+{
+ char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
+ uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
+ uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
+ uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
+
+ return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
+ eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
+ eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
+}
+
+// Sets VIA/keyboard level usage of EEPROM to valid/invalid
+// Keyboard level code (eg. via_init_kb()) should not call this
+void via_eeprom_set_valid(bool valid)
+{
+ char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
+ uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
+ uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
+ uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
+
+ eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
+ eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
+ eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
+}
+
+// Flag QMK and VIA/keyboard level EEPROM as invalid.
+// Used in bootmagic_lite() and VIA command handler.
+// Keyboard level code should not need to call this.
+void via_eeprom_reset(void)
+{
+ // Set the VIA specific EEPROM state as invalid.
+ via_eeprom_set_valid(false);
+ // Set the TMK/QMK EEPROM state as invalid.
+ eeconfig_disable();
+}
+
+// Override bootmagic_lite() so it can flag EEPROM as invalid
+// as well as jump to bootloader, thus performing a "factory reset"
+// of dynamic keymaps and optionally backlight/other settings.
+void bootmagic_lite(void)
+{
+ // The lite version of TMK's bootmagic based on Wilba.
+ // 100% less potential for accidentally making the
+ // keyboard do stupid things.
+
+ // We need multiple scans because debouncing can't be turned off.
+ matrix_scan();
+#if defined(DEBOUNCE) && DEBOUNCE > 0
+ wait_ms(DEBOUNCE * 2);
+#else
+ wait_ms(30);
+#endif
+ matrix_scan();
+
+ // If the Esc and space bar are held down on power up,
+ // reset the EEPROM valid state and jump to bootloader.
+ // Assumes Esc is at [0,0].
+ // This isn't very generalized, but we need something that doesn't
+ // rely on user's keymaps in firmware or EEPROM.
+ if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
+ // This is the only difference from the default implementation.
+ via_eeprom_reset();
+ // Jump to bootloader.
+ bootloader_jump();
+ }
+}
+
+// Override this at the keyboard code level to check
+// VIA's EEPROM valid state and reset to defaults as needed.
+// Used by keyboards that store their own state in EEPROM,
+// for backlight, rotary encoders, etc.
+// The override should not set via_eeprom_set_valid(true) as
+// the caller also needs to check the valid state.
+__attribute__((weak)) void via_init_kb(void) {
+}
+
+// Called by QMK core to initialize dynamic keymaps etc.
+void via_init(void)
+{
+ // Let keyboard level test EEPROM valid state,
+ // but not set it valid, it is done here.
+ via_init_kb();
+
+ // If the EEPROM has the magic, the data is good.
+ // OK to load from EEPROM.
+ if (via_eeprom_is_valid()) {
+ } else {
+ // This resets the layout options
+ via_set_layout_options(0);
+ // This resets the keymaps in EEPROM to what is in flash.
+ dynamic_keymap_reset();
+ // This resets the macros in EEPROM to nothing.
+ dynamic_keymap_macro_reset();
+ // Save the magic number last, in case saving was interrupted
+ via_eeprom_set_valid(true);
+ }
+}
+
+// This is generalized so the layout options EEPROM usage can be
+// variable, between 1 and 4 bytes.
+uint32_t via_get_layout_options(void)
+{
+ uint32_t value = 0;
+ // Start at the most significant byte
+ void * source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
+ for ( uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++ ) {
+ value = value << 8;
+ value |= eeprom_read_byte(source);
+ source++;
+ }
+ return value;
+}
+
+void via_set_layout_options(uint32_t value)
+{
+ // Start at the least significant byte
+ void * target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR+VIA_EEPROM_LAYOUT_OPTIONS_SIZE-1);
+ for ( uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++ ) {
+ eeprom_update_byte(target, value & 0xFF );
+ value = value >> 8;
+ target--;
+ }
+}
+
+// Called by QMK core to process VIA-specific keycodes.
+bool process_record_via(uint16_t keycode, keyrecord_t *record)
+{
+ // Handle macros
+ if (record->event.pressed) {
+ if ( keycode >= MACRO00 && keycode <= MACRO15 )
+ {
+ uint8_t id = keycode - MACRO00;
+ dynamic_keymap_macro_send(id);
+ return false;
+ }
+ }
+
+ // TODO: ideally this would be generalized and refactored into
+ // QMK core as advanced keycodes, until then, the simple case
+ // can be available here to keyboards using VIA
+ switch(keycode) {
+ case FN_MO13:
+ if (record->event.pressed) {
+ layer_on(1);
+ update_tri_layer(1, 2, 3);
+ } else {
+ layer_off(1);
+ update_tri_layer(1, 2, 3);
+ }
+ return false;
+ break;
+ case FN_MO23:
+ if (record->event.pressed) {
+ layer_on(2);
+ update_tri_layer(1, 2, 3);
+ } else {
+ layer_off(2);
+ update_tri_layer(1, 2, 3);
+ }
+ return false;
+ break;
+ }
+ return true;
+}
+
+// Keyboard level code can override this to handle custom messages from VIA.
+// See raw_hid_receive() implementation.
+// DO NOT call raw_hid_send() in the overide function.
+__attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
+ uint8_t *command_id = &(data[0]);
+ *command_id = id_unhandled;
+}
+
+// VIA handles received HID messages first, and will route to
+// raw_hid_receive_kb() for command IDs that are not handled here.
+// This gives the keyboard code level the ability to handle the command
+// specifically.
+//
+// raw_hid_send() is called at the end, with the same buffer, which was
+// possibly modified with returned values.
+void raw_hid_receive( uint8_t *data, uint8_t length )
+{
+ uint8_t *command_id = &(data[0]);
+ uint8_t *command_data = &(data[1]);
+ switch ( *command_id )
+ {
+ case id_get_protocol_version:
+ {
+ command_data[0] = VIA_PROTOCOL_VERSION >> 8;
+ command_data[1] = VIA_PROTOCOL_VERSION & 0xFF;
+ break;
+ }
+ case id_get_keyboard_value:
+ {
+ switch ( command_data[0] )
+ {
+ case id_uptime:
+ {
+ uint32_t value = timer_read32();
+ command_data[1] = (value >> 24 ) & 0xFF;
+ command_data[2] = (value >> 16 ) & 0xFF;
+ command_data[3] = (value >> 8 ) & 0xFF;
+ command_data[4] = value & 0xFF;
+ break;
+ }
+ case id_layout_options:
+ {
+ uint32_t value = via_get_layout_options();
+ command_data[1] = (value >> 24 ) & 0xFF;
+ command_data[2] = (value >> 16 ) & 0xFF;
+ command_data[3] = (value >> 8 ) & 0xFF;
+ command_data[4] = value & 0xFF;
+ break;
+ }
+ case id_switch_matrix_state:
+ {
+#if ( (MATRIX_COLS/8+1)*MATRIX_ROWS <= 28 )
+ uint8_t i = 1;
+ for ( uint8_t row=0; row<MATRIX_ROWS; row++ ) {
+ matrix_row_t value = matrix_get_row(row);
+#if (MATRIX_COLS > 24)
+ command_data[i++] = (value >> 24 ) & 0xFF;
+#endif
+#if (MATRIX_COLS > 16)
+ command_data[i++] = (value >> 16 ) & 0xFF;
+#endif
+#if (MATRIX_COLS > 8)
+ command_data[i++] = (value >> 8 ) & 0xFF;
+#endif
+ command_data[i++] = value & 0xFF;
+ }
+#endif
+ break;
+ }
+ default:
+ {
+ raw_hid_receive_kb(data,length);
+ break;
+ }
+ }
+ break;
+ }
+ case id_set_keyboard_value:
+ {
+ switch ( command_data[0] )
+ {
+ case id_layout_options:
+ {
+ uint32_t value = ( (uint32_t)command_data[1] << 24 ) |
+ ( (uint32_t)command_data[2] << 16 ) |
+ ( (uint32_t)command_data[3] << 8 ) |
+ (uint32_t)command_data[4];
+ via_set_layout_options(value);
+ break;
+ }
+ default:
+ {
+ raw_hid_receive_kb(data,length);
+ break;
+ }
+ }
+ break;
+ }
+ case id_dynamic_keymap_get_keycode:
+ {
+ uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
+ command_data[3] = keycode >> 8;
+ command_data[4] = keycode & 0xFF;
+ break;
+ }
+ case id_dynamic_keymap_set_keycode:
+ {
+ dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
+ break;
+ }
+ case id_dynamic_keymap_reset:
+ {
+ dynamic_keymap_reset();
+ break;
+ }
+ case id_backlight_config_set_value:
+ case id_backlight_config_get_value:
+ case id_backlight_config_save:
+ {
+ raw_hid_receive_kb(data, length);
+ break;
+ }
+ case id_dynamic_keymap_macro_get_count:
+ {
+ command_data[0] = dynamic_keymap_macro_get_count();
+ break;
+ }
+ case id_dynamic_keymap_macro_get_buffer_size:
+ {
+ uint16_t size = dynamic_keymap_macro_get_buffer_size();
+ command_data[0] = size >> 8;
+ command_data[1] = size & 0xFF;
+ break;
+ }
+ case id_dynamic_keymap_macro_get_buffer:
+ {
+ uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
+ uint16_t size = command_data[2]; // size <= 28
+ dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
+ break;
+ }
+ case id_dynamic_keymap_macro_set_buffer:
+ {
+ uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
+ uint16_t size = command_data[2]; // size <= 28
+ dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
+ break;
+ }
+ case id_dynamic_keymap_macro_reset:
+ {
+ dynamic_keymap_macro_reset();
+ break;
+ }
+ case id_dynamic_keymap_get_layer_count:
+ {
+ command_data[0] = dynamic_keymap_get_layer_count();
+ break;
+ }
+ case id_dynamic_keymap_get_buffer:
+ {
+ uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
+ uint16_t size = command_data[2]; // size <= 28
+ dynamic_keymap_get_buffer( offset, size, &command_data[3] );
+ break;
+ }
+ case id_dynamic_keymap_set_buffer:
+ {
+ uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
+ uint16_t size = command_data[2]; // size <= 28
+ dynamic_keymap_set_buffer( offset, size, &command_data[3] );
+ break;
+ }
+ case id_eeprom_reset:
+ {
+ via_eeprom_reset();
+ break;
+ }
+ case id_bootloader_jump:
+ {
+ // Need to send data back before the jump
+ // Informs host that the command is handled
+ raw_hid_send( data, length );
+ // Give host time to read it
+ wait_ms(100);
+ bootloader_jump();
+ break;
+ }
+ default:
+ {
+ // The command ID is not known
+ // Return the unhandled state
+ *command_id = id_unhandled;
+ break;
+ }
+ }
+
+ // Return the same buffer, optionally with values changed
+ // (i.e. returning state to the host, or the unhandled state).
+ raw_hid_send( data, length );
+}
diff --git a/quantum/via.h b/quantum/via.h
new file mode 100644
index 0000000000..f9a8017b24
--- /dev/null
+++ b/quantum/via.h
@@ -0,0 +1,151 @@
+/* Copyright 2019 Jason Williams (Wilba)
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <tmk_core/common/eeconfig.h> // for EECONFIG_SIZE
+
+// Keyboard level code can change where VIA stores the magic.
+// The magic is the build date YYMMDD encoded as BCD in 3 bytes,
+// thus installing firmware built on a different date to the one
+// already installed can be detected and the EEPROM data is reset.
+// The only reason this is important is in case EEPROM usage changes
+// and the EEPROM was not explicitly reset by bootmagic lite.
+#ifndef VIA_EEPROM_MAGIC_ADDR
+# define VIA_EEPROM_MAGIC_ADDR (EECONFIG_SIZE)
+#endif
+
+#define VIA_EEPROM_LAYOUT_OPTIONS_ADDR (VIA_EEPROM_MAGIC_ADDR+3)
+
+// Changing the layout options size after release will invalidate EEPROM,
+// but this is something that should be set correctly on initial implementation.
+// 1 byte is enough for most uses (i.e. 8 binary states, or 6 binary + 1 ternary/quaternary )
+#ifndef VIA_EEPROM_LAYOUT_OPTIONS_SIZE
+# define VIA_EEPROM_LAYOUT_OPTIONS_SIZE 1
+#endif
+
+// The end of the EEPROM memory used by VIA
+// By default, dynamic keymaps will start at this if there is no
+// custom config
+#define VIA_EEPROM_CUSTOM_CONFIG_ADDR (VIA_EEPROM_LAYOUT_OPTIONS_ADDR+VIA_EEPROM_LAYOUT_OPTIONS_SIZE)
+
+#ifndef VIA_EEPROM_CUSTOM_CONFIG_SIZE
+# define VIA_EEPROM_CUSTOM_CONFIG_SIZE 0
+#endif
+
+// This is changed only when the command IDs change,
+// so VIA Configurator can detect compatible firmware.
+#define VIA_PROTOCOL_VERSION 0x0009
+
+enum via_command_id
+{
+ id_get_protocol_version = 0x01, // always 0x01
+ id_get_keyboard_value,
+ id_set_keyboard_value,
+ id_dynamic_keymap_get_keycode,
+ id_dynamic_keymap_set_keycode,
+ id_dynamic_keymap_reset,
+ id_backlight_config_set_value,
+ id_backlight_config_get_value,
+ id_backlight_config_save,
+ id_eeprom_reset,
+ id_bootloader_jump,
+ id_dynamic_keymap_macro_get_count,
+ id_dynamic_keymap_macro_get_buffer_size,
+ id_dynamic_keymap_macro_get_buffer,
+ id_dynamic_keymap_macro_set_buffer,
+ id_dynamic_keymap_macro_reset,
+ id_dynamic_keymap_get_layer_count,
+ id_dynamic_keymap_get_buffer,
+ id_dynamic_keymap_set_buffer,
+ id_unhandled = 0xFF,
+};
+
+enum via_keyboard_value_id
+{
+ id_uptime = 0x01,
+ id_layout_options,
+ id_switch_matrix_state
+};
+
+// Can't use SAFE_RANGE here, it might change if someone adds
+// new values to enum quantum_keycodes.
+// Need to keep checking 0x5F10 is still in the safe range.
+// TODO: merge this into quantum_keycodes
+// Backlight keycodes are in range 0x5F00-0x5F0F
+enum via_keycodes {
+ FN_MO13 = 0x5F10,
+ FN_MO23,
+ MACRO00,
+ MACRO01,
+ MACRO02,
+ MACRO03,
+ MACRO04,
+ MACRO05,
+ MACRO06,
+ MACRO07,
+ MACRO08,
+ MACRO09,
+ MACRO10,
+ MACRO11,
+ MACRO12,
+ MACRO13,
+ MACRO14,
+ MACRO15,
+};
+
+enum user_keycodes {
+ USER00 = 0x5F80,
+ USER01,
+ USER02,
+ USER03,
+ USER04,
+ USER05,
+ USER06,
+ USER07,
+ USER08,
+ USER09,
+ USER10,
+ USER11,
+ USER12,
+ USER13,
+ USER14,
+ USER15,
+};
+
+// Can be called in an overriding via_init_kb() to test if keyboard level code usage of
+// EEPROM is invalid and use/save defaults.
+bool via_eeprom_is_valid(void);
+
+// Sets VIA/keyboard level usage of EEPROM to valid/invalid
+// Keyboard level code (eg. via_init_kb()) should not call this
+void via_eeprom_set_valid(bool valid);
+
+// Flag QMK and VIA/keyboard level EEPROM as invalid.
+// Used in bootmagic_lite() and VIA command handler.
+// Keyboard level code should not need to call this.
+void via_eeprom_reset(void);
+
+// Called by QMK core to initialize dynamic keymaps etc.
+void via_init(void);
+
+// Used by VIA to store and retrieve the layout options.
+uint32_t via_get_layout_options(void);
+void via_set_layout_options(uint32_t value);
+
+// Called by QMK core to process VIA-specific keycodes.
+bool process_record_via(uint16_t keycode, keyrecord_t *record);
+
diff --git a/show_options.mk b/show_options.mk
index 63ab3b0d7b..003169d4cb 100644
--- a/show_options.mk
+++ b/show_options.mk
@@ -10,7 +10,8 @@ BUILD_OPTION_NAMES = \
DEBOUNCE_TYPE \
SPLIT_KEYBOARD \
DYNAMIC_KEYMAP_ENABLE \
- USB_HID_ENABLE
+ USB_HID_ENABLE \
+ VIA_ENABLE
HARDWARE_OPTION_NAMES = \
SLEEP_LED_ENABLE \
diff --git a/tmk_core/common/eeconfig.h b/tmk_core/common/eeconfig.h
index a7a78b352e..cfa053c17a 100644
--- a/tmk_core/common/eeconfig.h
+++ b/tmk_core/common/eeconfig.h
@@ -48,6 +48,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define EECONFIG_RGB_MATRIX_SPEED (uint8_t *)32
// TODO: Combine these into a single word and single block of EEPROM
#define EECONFIG_KEYMAP_UPPER_BYTE (uint8_t *)33
+// Size of EEPROM being used, other code can refer to this for available EEPROM
+#define EECONFIG_SIZE 34
/* debug bit */
#define EECONFIG_DEBUG_ENABLE (1 << 0)
#define EECONFIG_DEBUG_MATRIX (1 << 1)
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index 091b76bdfa..2a61985f98 100644
--- a/tmk_core/common/keyboard.c
+++ b/tmk_core/common/keyboard.c
@@ -83,6 +83,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifdef VELOCIKEY_ENABLE
# include "velocikey.h"
#endif
+#ifdef VIA_ENABLE
+# include "via.h"
+#endif
// Only enable this if console is enabled to print to
#if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE)
@@ -212,6 +215,9 @@ __attribute__((weak)) bool is_keyboard_master(void) { return true; }
void keyboard_init(void) {
timer_init();
matrix_init();
+#ifdef VIA_ENABLE
+ via_init();
+#endif
#ifdef QWIIC_ENABLE
qwiic_init();
#endif