diff --git a/common.mk b/common.mk index f6e9f262..06f57465 100644 --- a/common.mk +++ b/common.mk @@ -69,6 +69,11 @@ ifdef KEYMAP_SECTION_ENABLE EXTRALDFLAGS = -Wl,-L$(TOP_DIR),-Tldscript_keymap_avr5.x endif +ifdef KEYMAP_IN_EEPROM_ENABLE + SRC += $(COMMON_DIR)/keymap_in_eeprom.c + OPT_DEFS += -DKEYMAP_IN_EEPROM_ENABLE +endif + ifdef LED_MATRIX_ENABLE SRC += $(COMMON_DIR)/led_matrix.c OPT_DEFS += -DLED_MATRIX_ENABLE diff --git a/common/bootmagic.c b/common/bootmagic.c index 311a02f2..1668ee49 100644 --- a/common/bootmagic.c +++ b/common/bootmagic.c @@ -7,6 +7,7 @@ #include "keymap.h" #include "action_layer.h" #include "eeconfig.h" +#include "keymap_in_eeprom.h" #include "bootmagic.h" @@ -32,6 +33,9 @@ void bootmagic(void) if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) { eeconfig_disable(); eeconfig_init(); +#ifdef KEYMAP_IN_EEPROM_ENABLE + write_keymap_to_eeprom(); +#endif } /* bootloader */ diff --git a/common/keyboard.c b/common/keyboard.c index 7928741a..08f86be3 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -32,6 +32,7 @@ along with this program. If not, see . #include "eeconfig.h" #include "backlight.h" #include "breathing_led.h" +#include "keymap_in_eeprom.h" #ifdef MOUSEKEY_ENABLE # include "mousekey.h" #endif @@ -84,6 +85,10 @@ void keyboard_init(void) #ifdef BREATHING_LED_ENABLE breathing_led_init(); #endif + +#ifdef KEYMAP_IN_EEPROM_ENABLE + keymap_in_eeprom_init(); +#endif } /* diff --git a/common/keymap_in_eeprom.c b/common/keymap_in_eeprom.c new file mode 100644 index 00000000..145b5e61 --- /dev/null +++ b/common/keymap_in_eeprom.c @@ -0,0 +1,110 @@ +/* +Copyright 2013,2014 Kai Ryu + +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 . +*/ + +#include +#include +#include "eeconfig.h" +#include "keymap_in_eeprom.h" +#include "debug.h" + +#ifdef KEYMAP_IN_EEPROM_ENABLE + +void keymap_in_eeprom_init(void) { + if (!check_keymap_in_eeprom()) { + write_keymap_to_eeprom(); + } +} + +void keymap_in_eeprom_disable(void) { + eeprom_write_word(EECONFIG_KEYMAP_CHECKSUM, eeprom_read_word(EECONFIG_KEYMAP_CHECKSUM) + 1); +} + +bool check_keymap_in_eeprom(void) { + uint16_t checksum_in_eeprom = eeprom_read_word(EECONFIG_KEYMAP_CHECKSUM); + uint16_t checksum = EECONFIG_MAGIC_NUMBER; + for (uint16_t i = 0; i < KEYMAP_SIZE; i++) { + uint8_t byte = eeprom_read_byte((uint8_t *)EECONFIG_KEYMAP_FN_ACTIONS + i); + uint16_t word = (i & 1) ? byte << 8 : byte; + checksum += word; + } +#ifdef DEBUG + eeprom_write_word(EECONFIG_KEYMAP_DEBUG, checksum); +#endif + return (checksum_in_eeprom == checksum); +} + +void write_keymap_to_eeprom(void) { + uint16_t checksum = EECONFIG_MAGIC_NUMBER; + const uint16_t *fn_actions = fn_actions_pointer(); + const uint8_t *keymaps = keymaps_pointer(); + // write fn_actions + if (fn_actions != NULL) { + uint16_t fn_actions_count_in_flash = fn_actions_count(); + for (uint16_t i = 0; i < FN_ACTIONS_COUNT; i++) { + uint16_t fn_action = 0; + if (i < fn_actions_count_in_flash) { + fn_action = pgm_read_word(fn_actions + i); + } + eeconfig_write_keymap_fn_action(i, fn_action); + checksum += fn_action; + } + } + // write keymaps + if (keymaps != NULL) { + uint16_t keys_count_in_flash = keys_count(); + for (uint16_t i = 0; i < KEYS_COUNT; i++) { + uint8_t keymap = 0; + if (i < keys_count_in_flash) { + keymap = pgm_read_byte(keymaps + i); + } + eeconfig_write_keymap_key_by_index(i, keymap); + uint16_t keymap_word = keymap; + if (i & 1) { + keymap_word = keymap << 8; + } + checksum += keymap_word; + } + } + // write checksum + eeprom_write_word(EECONFIG_KEYMAP_CHECKSUM, checksum); +} + +uint16_t eeconfig_read_keymap_fn_action(uint8_t index) { + return eeprom_read_word(EECONFIG_KEYMAP_FN_ACTIONS + index); +} + +void eeconfig_write_keymap_fn_action(uint8_t index, uint16_t fn_action) { + return eeprom_write_word(EECONFIG_KEYMAP_FN_ACTIONS + index, fn_action); +} + +uint8_t eeconfig_read_keymap_key(uint8_t layer, uint8_t row, uint8_t col) { + return eeprom_read_byte(EECONFIG_KEYMAP_KEYMAPS + KEY_OFFSET(layer, row, col)); +} + +void eeconfig_write_keymap_key(uint8_t layer, uint8_t row, uint8_t col, uint8_t key) { + return eeprom_write_byte(EECONFIG_KEYMAP_KEYMAPS + KEY_OFFSET(layer, row, col), key); +} + +uint8_t eeconfig_read_keymap_key_by_index(uint16_t index) { + return eeprom_read_byte(EECONFIG_KEYMAP_KEYMAPS + index); +} + +void eeconfig_write_keymap_key_by_index(uint16_t index, uint8_t key) { + return eeprom_write_byte(EECONFIG_KEYMAP_KEYMAPS + index, key); +} + +#endif diff --git a/common/keymap_in_eeprom.h b/common/keymap_in_eeprom.h new file mode 100644 index 00000000..769dfa2e --- /dev/null +++ b/common/keymap_in_eeprom.h @@ -0,0 +1,68 @@ +/* +Copyright 2013,2014 Kai Ryu + +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 . +*/ + +#ifndef KEYMAP_IN_EEPROM_H +#define KEYMAP_IN_EEPROM_H + +#ifdef KEYMAP_IN_EEPROM_ENABLE + +#include +#include + +#define EECONFIG_KEYMAP_IN_EEPROM 0x10 +#ifndef FN_ACTIONS_COUNT +#define FN_ACTIONS_COUNT 32 +#endif +#ifndef KEYMAPS_COUNT +#define KEYMAPS_COUNT 1 +#endif +#define KEYS_COUNT (KEYMAPS_COUNT * MATRIX_ROWS * MATRIX_COLS) + +typedef struct { + uint16_t checksum; + uint16_t fn_actions[FN_ACTIONS_COUNT]; + uint8_t keymaps[KEYMAPS_COUNT][MATRIX_ROWS][MATRIX_COLS]; +} keymap_in_eeprom_t; + +#define EECONFIG_KEYMAP_CHECKSUM (uint16_t *)(EECONFIG_KEYMAP_IN_EEPROM) +#define EECONFIG_KEYMAP_FN_ACTIONS (uint16_t *)(EECONFIG_KEYMAP_CHECKSUM + 1) +#define EECONFIG_KEYMAP_KEYMAPS (uint8_t *)(EECONFIG_KEYMAP_FN_ACTIONS + FN_ACTIONS_COUNT) +#define EECONFIG_KEYMAP_DEBUG (uint16_t *)(EECONFIG_KEYMAP_CHECKSUM - 1) + +#define KEYMAP_SIZE (sizeof(uint16_t) * FN_ACTIONS_COUNT + sizeof(uint8_t) * KEYS_COUNT) +#define KEYMAP_WORD_SIZE ((KEYMAP_SIZE + 1) / 2) +#define KEY_OFFSET(layer, row, col) (layer * MATRIX_ROWS * MATRIX_COLS + row * MATRIX_COLS + col) + +void keymap_in_eeprom_init(void); +void keymap_in_eeprom_disable(void); +bool check_keymap_in_eeprom(void); +void write_keymap_to_eeprom(void); +uint8_t eeconfig_read_keymap_key(uint8_t layer, uint8_t row, uint8_t col); +void eeconfig_write_keymap_key(uint8_t layer, uint8_t row, uint8_t col, uint8_t key); +uint8_t eeconfig_read_keymap_key_by_index(uint16_t index); +void eeconfig_write_keymap_key_by_index(uint16_t index, uint8_t key); +uint16_t eeconfig_read_keymap_fn_action(uint8_t index); +void eeconfig_write_keymap_fn_action(uint8_t index, uint16_t fn_action); + +const uint8_t* keymaps_pointer(void); +const uint16_t* fn_actions_pointer(void); +uint16_t keys_count(void); +uint16_t fn_actions_count(void); + +#endif + +#endif diff --git a/doc/build.md b/doc/build.md index bfe5de9f..c7522e4d 100644 --- a/doc/build.md +++ b/doc/build.md @@ -133,6 +133,7 @@ Optional. Note that ***comment out*** to disable them. SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend #NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA #BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality + #KEYMAP_IN_EEPROM_ENABLE = yes # Read keymap from eeprom ### 3. Programmer Optional. Set proper command for your controller, bootloader and programmer. This command can be used with `make program`. Not needed if you use `FLIP`, `dfu-programmer` or `Teensy Loader`.