diff options
Diffstat (limited to 'quantum/nvm')
-rw-r--r-- | quantum/nvm/eeprom/nvm_dynamic_keymap.c | 193 | ||||
-rw-r--r-- | quantum/nvm/eeprom/nvm_eeconfig.c | 292 | ||||
-rw-r--r-- | quantum/nvm/eeprom/nvm_eeprom_eeconfig_internal.h | 59 | ||||
-rw-r--r-- | quantum/nvm/eeprom/nvm_eeprom_via_internal.h | 22 | ||||
-rw-r--r-- | quantum/nvm/eeprom/nvm_via.c | 77 | ||||
-rw-r--r-- | quantum/nvm/nvm_dynamic_keymap.h | 27 | ||||
-rw-r--r-- | quantum/nvm/nvm_eeconfig.h | 105 | ||||
-rw-r--r-- | quantum/nvm/nvm_via.h | 17 | ||||
-rw-r--r-- | quantum/nvm/readme.md | 30 | ||||
-rw-r--r-- | quantum/nvm/rules.mk | 31 |
10 files changed, 853 insertions, 0 deletions
diff --git a/quantum/nvm/eeprom/nvm_dynamic_keymap.c b/quantum/nvm/eeprom/nvm_dynamic_keymap.c new file mode 100644 index 0000000000..5f514acc1a --- /dev/null +++ b/quantum/nvm/eeprom/nvm_dynamic_keymap.c @@ -0,0 +1,193 @@ +// Copyright 2024 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "keycodes.h" +#include "eeprom.h" +#include "dynamic_keymap.h" +#include "nvm_dynamic_keymap.h" +#include "nvm_eeprom_eeconfig_internal.h" +#include "nvm_eeprom_via_internal.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#ifdef ENCODER_ENABLE +# include "encoder.h" +#endif + +#ifdef VIA_ENABLE +# include "via.h" +# define DYNAMIC_KEYMAP_EEPROM_START (VIA_EEPROM_CONFIG_END) +#else +# define DYNAMIC_KEYMAP_EEPROM_START (EECONFIG_SIZE) +#endif + +#ifndef DYNAMIC_KEYMAP_EEPROM_MAX_ADDR +# define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR (TOTAL_EEPROM_BYTE_COUNT - 1) +#endif + +_Static_assert(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR <= (TOTAL_EEPROM_BYTE_COUNT - 1), "DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is configured to use more space than what is available for the selected EEPROM driver"); + +// Due to usage of uint16_t check for max 65535 +_Static_assert(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR <= 65535, "DYNAMIC_KEYMAP_EEPROM_MAX_ADDR must be less than 65536"); + +// If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h, +#ifndef DYNAMIC_KEYMAP_EEPROM_ADDR +# define DYNAMIC_KEYMAP_EEPROM_ADDR DYNAMIC_KEYMAP_EEPROM_START +#endif + +// Dynamic encoders starts after dynamic keymaps +#ifndef DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR +# define DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2)) +#endif + +// Dynamic macro starts after dynamic encoders, but only when using ENCODER_MAP +#ifdef ENCODER_MAP_ENABLE +# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR +# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * NUM_ENCODERS * 2 * 2)) +# endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR +#else // ENCODER_MAP_ENABLE +# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR +# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) +# endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR +#endif // ENCODER_MAP_ENABLE + +// Sanity check that dynamic keymaps fit in available EEPROM +// If there's not 100 bytes available for macros, then something is wrong. +// The keyboard should override DYNAMIC_KEYMAP_LAYER_COUNT to reduce it, +// or DYNAMIC_KEYMAP_EEPROM_MAX_ADDR to increase it, *only if* the microcontroller has +// more than the default. +_Static_assert((DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) - (DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR) >= 100, "Dynamic keymaps are configured to use more EEPROM than is available."); + +#ifndef TOTAL_EEPROM_BYTE_COUNT +# error Unknown total EEPROM size. Cannot derive maximum for dynamic keymaps. +#endif +// Dynamic macros are stored after the keymaps and use what is available +// up to and including DYNAMIC_KEYMAP_EEPROM_MAX_ADDR. +#ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE +# define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + 1) +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void nvm_dynamic_keymap_erase(void) { + // No-op, nvm_eeconfig_erase() will have already erased EEPROM if necessary. +} + +void nvm_dynamic_keymap_macro_erase(void) { + // No-op, nvm_eeconfig_erase() will have already erased EEPROM if necessary. +} + +static inline void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) { + return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2); +} + +uint16_t nvm_dynamic_keymap_read_keycode(uint8_t layer, uint8_t row, uint8_t column) { + if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return KC_NO; + void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); + // Big endian, so we can read/write EEPROM directly from host if we want + uint16_t keycode = eeprom_read_byte(address) << 8; + keycode |= eeprom_read_byte(address + 1); + return keycode; +} + +void nvm_dynamic_keymap_update_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) { + if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return; + void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); + // Big endian, so we can read/write EEPROM directly from host if we want + eeprom_update_byte(address, (uint8_t)(keycode >> 8)); + eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF)); +} + +#ifdef ENCODER_MAP_ENABLE +static void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t encoder_id) { + return ((void *)DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) + (layer * NUM_ENCODERS * 2 * 2) + (encoder_id * 2 * 2); +} + +uint16_t nvm_dynamic_keymap_read_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) { + if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return KC_NO; + void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id); + // Big endian, so we can read/write EEPROM directly from host if we want + uint16_t keycode = ((uint16_t)eeprom_read_byte(address + (clockwise ? 0 : 2))) << 8; + keycode |= eeprom_read_byte(address + (clockwise ? 0 : 2) + 1); + return keycode; +} + +void nvm_dynamic_keymap_update_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) { + if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return; + void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id); + // Big endian, so we can read/write EEPROM directly from host if we want + eeprom_update_byte(address + (clockwise ? 0 : 2), (uint8_t)(keycode >> 8)); + eeprom_update_byte(address + (clockwise ? 0 : 2) + 1, (uint8_t)(keycode & 0xFF)); +} +#endif // ENCODER_MAP_ENABLE + +void nvm_dynamic_keymap_read_buffer(uint32_t offset, uint32_t size, uint8_t *data) { + uint32_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2; + void * source = (void *)(uintptr_t)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset); + uint8_t *target = data; + for (uint32_t i = 0; i < size; i++) { + if (offset + i < dynamic_keymap_eeprom_size) { + *target = eeprom_read_byte(source); + } else { + *target = 0x00; + } + source++; + target++; + } +} + +void nvm_dynamic_keymap_update_buffer(uint32_t offset, uint32_t size, uint8_t *data) { + uint32_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2; + void * target = (void *)(uintptr_t)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset); + uint8_t *source = data; + for (uint32_t i = 0; i < size; i++) { + if (offset + i < dynamic_keymap_eeprom_size) { + eeprom_update_byte(target, *source); + } + source++; + target++; + } +} + +uint32_t nvm_dynamic_keymap_macro_size(void) { + return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE; +} + +void nvm_dynamic_keymap_macro_read_buffer(uint32_t offset, uint32_t size, uint8_t *data) { + void * source = (void *)(uintptr_t)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset); + uint8_t *target = data; + for (uint16_t i = 0; i < size; i++) { + if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) { + *target = eeprom_read_byte(source); + } else { + *target = 0x00; + } + source++; + target++; + } +} + +void nvm_dynamic_keymap_macro_update_buffer(uint32_t offset, uint32_t size, uint8_t *data) { + void * target = (void *)(uintptr_t)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset); + uint8_t *source = data; + for (uint16_t i = 0; i < size; i++) { + if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) { + eeprom_update_byte(target, *source); + } + source++; + target++; + } +} + +void nvm_dynamic_keymap_macro_reset(void) { + void * start = (void *)(uintptr_t)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR); + void * end = (void *)(uintptr_t)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE); + long remaining = end - start; + uint8_t dummy[16] = {0}; + for (int i = 0; i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE; i += sizeof(dummy)) { + int this_loop = remaining < sizeof(dummy) ? remaining : sizeof(dummy); + eeprom_update_block(dummy, start, this_loop); + start += this_loop; + remaining -= this_loop; + } +} diff --git a/quantum/nvm/eeprom/nvm_eeconfig.c b/quantum/nvm/eeprom/nvm_eeconfig.c new file mode 100644 index 0000000000..d6c388f3bc --- /dev/null +++ b/quantum/nvm/eeprom/nvm_eeconfig.c @@ -0,0 +1,292 @@ +// Copyright 2024 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#include <string.h> +#include "nvm_eeconfig.h" +#include "nvm_eeprom_eeconfig_internal.h" +#include "util.h" +#include "eeconfig.h" +#include "debug.h" +#include "eeprom.h" +#include "keycode_config.h" + +#ifdef EEPROM_DRIVER +# include "eeprom_driver.h" +#endif + +#ifdef AUDIO_ENABLE +# include "audio.h" +#endif + +#ifdef BACKLIGHT_ENABLE +# include "backlight.h" +#endif + +#ifdef RGBLIGHT_ENABLE +# include "rgblight.h" +#endif + +#ifdef RGB_MATRIX_ENABLE +# include "rgb_matrix_types.h" +#endif + +#ifdef LED_MATRIX_ENABLE +# include "led_matrix_types.h" +#endif + +#ifdef UNICODE_COMMON_ENABLE +# include "unicode.h" +#endif + +#ifdef HAPTIC_ENABLE +# include "haptic.h" +#endif + +void nvm_eeconfig_erase(void) { +#ifdef EEPROM_DRIVER + eeprom_driver_format(false); +#endif // EEPROM_DRIVER +} + +bool nvm_eeconfig_is_enabled(void) { + return eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER; +} + +bool nvm_eeconfig_is_disabled(void) { + return eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER_OFF; +} + +void nvm_eeconfig_enable(void) { + eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); +} + +void nvm_eeconfig_disable(void) { +#if defined(EEPROM_DRIVER) + eeprom_driver_format(false); +#endif + eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER_OFF); +} + +void nvm_eeconfig_read_debug(debug_config_t *debug_config) { + debug_config->raw = eeprom_read_byte(EECONFIG_DEBUG); +} +void nvm_eeconfig_update_debug(const debug_config_t *debug_config) { + eeprom_update_byte(EECONFIG_DEBUG, debug_config->raw); +} + +layer_state_t nvm_eeconfig_read_default_layer(void) { + uint8_t val = eeprom_read_byte(EECONFIG_DEFAULT_LAYER); +#ifdef DEFAULT_LAYER_STATE_IS_VALUE_NOT_BITMASK + // stored as a layer number, so convert back to bitmask + return (layer_state_t)1 << val; +#else + // stored as 8-bit-wide bitmask, so read the value directly - handling padding to 16/32 bit layer_state_t + return (layer_state_t)val; +#endif +} +void nvm_eeconfig_update_default_layer(layer_state_t state) { +#ifdef DEFAULT_LAYER_STATE_IS_VALUE_NOT_BITMASK + // stored as a layer number, so only store the highest layer + uint8_t val = get_highest_layer(state); +#else + // stored as 8-bit-wide bitmask, so write the value directly - handling truncation from 16/32 bit layer_state_t + uint8_t val = (uint8_t)state; +#endif + eeprom_update_byte(EECONFIG_DEFAULT_LAYER, val); +} + +void nvm_eeconfig_read_keymap(keymap_config_t *keymap_config) { + keymap_config->raw = eeprom_read_word(EECONFIG_KEYMAP); +} +void nvm_eeconfig_update_keymap(const keymap_config_t *keymap_config) { + eeprom_update_word(EECONFIG_KEYMAP, keymap_config->raw); +} + +#ifdef AUDIO_ENABLE +void nvm_eeconfig_read_audio(audio_config_t *audio_config) { + audio_config->raw = eeprom_read_byte(EECONFIG_AUDIO); +} +void nvm_eeconfig_update_audio(const audio_config_t *audio_config) { + eeprom_update_byte(EECONFIG_AUDIO, audio_config->raw); +} +#endif // AUDIO_ENABLE + +#ifdef UNICODE_COMMON_ENABLE +void nvm_eeconfig_read_unicode_mode(unicode_config_t *unicode_config) { + unicode_config->raw = eeprom_read_byte(EECONFIG_UNICODEMODE); +} +void nvm_eeconfig_update_unicode_mode(const unicode_config_t *unicode_config) { + eeprom_update_byte(EECONFIG_UNICODEMODE, unicode_config->raw); +} +#endif // UNICODE_COMMON_ENABLE + +#ifdef BACKLIGHT_ENABLE +void nvm_eeconfig_read_backlight(backlight_config_t *backlight_config) { + backlight_config->raw = eeprom_read_byte(EECONFIG_BACKLIGHT); +} +void nvm_eeconfig_update_backlight(const backlight_config_t *backlight_config) { + eeprom_update_byte(EECONFIG_BACKLIGHT, backlight_config->raw); +} +#endif // BACKLIGHT_ENABLE + +#ifdef STENO_ENABLE +uint8_t nvm_eeconfig_read_steno_mode(void) { + return eeprom_read_byte(EECONFIG_STENOMODE); +} +void nvm_eeconfig_update_steno_mode(uint8_t val) { + eeprom_update_byte(EECONFIG_STENOMODE, val); +} +#endif // STENO_ENABLE + +#ifdef RGBLIGHT_ENABLE +#endif // RGBLIGHT_ENABLE + +#ifdef RGB_MATRIX_ENABLE +void nvm_eeconfig_read_rgb_matrix(rgb_config_t *rgb_matrix_config) { + eeprom_read_block(rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_config_t)); +} +void nvm_eeconfig_update_rgb_matrix(const rgb_config_t *rgb_matrix_config) { + eeprom_update_block(rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_config_t)); +} +#endif // RGB_MATRIX_ENABLE + +#ifdef LED_MATRIX_ENABLE +void nvm_eeconfig_read_led_matrix(led_eeconfig_t *led_matrix_config) { + eeprom_read_block(led_matrix_config, EECONFIG_LED_MATRIX, sizeof(led_eeconfig_t)); +} +void nvm_eeconfig_update_led_matrix(const led_eeconfig_t *led_matrix_config) { + eeprom_update_block(led_matrix_config, EECONFIG_LED_MATRIX, sizeof(led_eeconfig_t)); +} +#endif // LED_MATRIX_ENABLE + +#ifdef RGBLIGHT_ENABLE +void nvm_eeconfig_read_rgblight(rgblight_config_t *rgblight_config) { + rgblight_config->raw = eeprom_read_dword(EECONFIG_RGBLIGHT); + rgblight_config->raw |= ((uint64_t)eeprom_read_byte(EECONFIG_RGBLIGHT_EXTENDED) << 32); +} +void nvm_eeconfig_update_rgblight(const rgblight_config_t *rgblight_config) { + eeprom_update_dword(EECONFIG_RGBLIGHT, rgblight_config->raw & 0xFFFFFFFF); + eeprom_update_byte(EECONFIG_RGBLIGHT_EXTENDED, (rgblight_config->raw >> 32) & 0xFF); +} +#endif // RGBLIGHT_ENABLE + +#if (EECONFIG_KB_DATA_SIZE) == 0 +uint32_t nvm_eeconfig_read_kb(void) { + return eeprom_read_dword(EECONFIG_KEYBOARD); +} +void nvm_eeconfig_update_kb(uint32_t val) { + eeprom_update_dword(EECONFIG_KEYBOARD, val); +} +#endif // (EECONFIG_KB_DATA_SIZE) == 0 + +#if (EECONFIG_USER_DATA_SIZE) == 0 +uint32_t nvm_eeconfig_read_user(void) { + return eeprom_read_dword(EECONFIG_USER); +} +void nvm_eeconfig_update_user(uint32_t val) { + eeprom_update_dword(EECONFIG_USER, val); +} +#endif // (EECONFIG_USER_DATA_SIZE) == 0 + +#ifdef HAPTIC_ENABLE +void nvm_eeconfig_read_haptic(haptic_config_t *haptic_config) { + haptic_config->raw = eeprom_read_dword(EECONFIG_HAPTIC); +} +void nvm_eeconfig_update_haptic(const haptic_config_t *haptic_config) { + eeprom_update_dword(EECONFIG_HAPTIC, haptic_config->raw); +} +#endif // HAPTIC_ENABLE + +bool nvm_eeconfig_read_handedness(void) { + return !!eeprom_read_byte(EECONFIG_HANDEDNESS); +} +void nvm_eeconfig_update_handedness(bool val) { + eeprom_update_byte(EECONFIG_HANDEDNESS, !!val); +} + +#if (EECONFIG_KB_DATA_SIZE) > 0 + +bool nvm_eeconfig_is_kb_datablock_valid(void) { + return eeprom_read_dword(EECONFIG_KEYBOARD) == (EECONFIG_KB_DATA_VERSION); +} + +uint32_t nvm_eeconfig_read_kb_datablock(void *data, uint32_t offset, uint32_t length) { + if (eeconfig_is_kb_datablock_valid()) { + void *ee_start = (void *)(uintptr_t)(EECONFIG_KB_DATABLOCK + offset); + void *ee_end = (void *)(uintptr_t)(EECONFIG_KB_DATABLOCK + MIN(EECONFIG_KB_DATA_SIZE, offset + length)); + eeprom_read_block(data, ee_start, ee_end - ee_start); + return ee_end - ee_start; + } else { + memset(data, 0, length); + return length; + } +} + +uint32_t nvm_eeconfig_update_kb_datablock(const void *data, uint32_t offset, uint32_t length) { + eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION)); + + void *ee_start = (void *)(uintptr_t)(EECONFIG_KB_DATABLOCK + offset); + void *ee_end = (void *)(uintptr_t)(EECONFIG_KB_DATABLOCK + MIN(EECONFIG_KB_DATA_SIZE, offset + length)); + eeprom_update_block(data, ee_start, ee_end - ee_start); + return ee_end - ee_start; +} + +void nvm_eeconfig_init_kb_datablock(void) { + eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION)); + + void * start = (void *)(uintptr_t)(EECONFIG_KB_DATABLOCK); + void * end = (void *)(uintptr_t)(EECONFIG_KB_DATABLOCK + EECONFIG_KB_DATA_SIZE); + long remaining = end - start; + uint8_t dummy[16] = {0}; + for (int i = 0; i < EECONFIG_KB_DATA_SIZE; i += sizeof(dummy)) { + int this_loop = remaining < sizeof(dummy) ? remaining : sizeof(dummy); + eeprom_update_block(dummy, start, this_loop); + start += this_loop; + remaining -= this_loop; + } +} + +#endif // (EECONFIG_KB_DATA_SIZE) > 0 + +#if (EECONFIG_USER_DATA_SIZE) > 0 + +bool nvm_eeconfig_is_user_datablock_valid(void) { + return eeprom_read_dword(EECONFIG_USER) == (EECONFIG_USER_DATA_VERSION); +} + +uint32_t nvm_eeconfig_read_user_datablock(void *data, uint32_t offset, uint32_t length) { + if (eeconfig_is_user_datablock_valid()) { + void *ee_start = (void *)(uintptr_t)(EECONFIG_USER_DATABLOCK + offset); + void *ee_end = (void *)(uintptr_t)(EECONFIG_USER_DATABLOCK + MIN(EECONFIG_USER_DATA_SIZE, offset + length)); + eeprom_read_block(data, ee_start, ee_end - ee_start); + return ee_end - ee_start; + } else { + memset(data, 0, length); + return length; + } +} + +uint32_t nvm_eeconfig_update_user_datablock(const void *data, uint32_t offset, uint32_t length) { + eeprom_update_dword(EECONFIG_USER, (EECONFIG_USER_DATA_VERSION)); + + void *ee_start = (void *)(uintptr_t)(EECONFIG_USER_DATABLOCK + offset); + void *ee_end = (void *)(uintptr_t)(EECONFIG_USER_DATABLOCK + MIN(EECONFIG_USER_DATA_SIZE, offset + length)); + eeprom_update_block(data, ee_start, ee_end - ee_start); + return ee_end - ee_start; +} + +void nvm_eeconfig_init_user_datablock(void) { + eeprom_update_dword(EECONFIG_USER, (EECONFIG_USER_DATA_VERSION)); + + void * start = (void *)(uintptr_t)(EECONFIG_USER_DATABLOCK); + void * end = (void *)(uintptr_t)(EECONFIG_USER_DATABLOCK + EECONFIG_USER_DATA_SIZE); + long remaining = end - start; + uint8_t dummy[16] = {0}; + for (int i = 0; i < EECONFIG_USER_DATA_SIZE; i += sizeof(dummy)) { + int this_loop = remaining < sizeof(dummy) ? remaining : sizeof(dummy); + eeprom_update_block(dummy, start, this_loop); + start += this_loop; + remaining -= this_loop; + } +} + +#endif // (EECONFIG_USER_DATA_SIZE) > 0 diff --git a/quantum/nvm/eeprom/nvm_eeprom_eeconfig_internal.h b/quantum/nvm/eeprom/nvm_eeprom_eeconfig_internal.h new file mode 100644 index 0000000000..6efbf9480b --- /dev/null +++ b/quantum/nvm/eeprom/nvm_eeprom_eeconfig_internal.h @@ -0,0 +1,59 @@ +// Copyright 2024 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include <stdint.h> +#include <stddef.h> // offsetof +#include "eeconfig.h" +#include "util.h" + +// Dummy struct only used to calculate offsets +typedef struct PACKED { + uint16_t magic; + uint8_t debug; + uint8_t default_layer; + uint16_t keymap; + uint8_t backlight; + uint8_t audio; + uint32_t rgblight; + uint8_t unicode; + uint8_t steno; + uint8_t handedness; + uint32_t keyboard; + uint32_t user; + union { // Mutually exclusive + uint32_t led_matrix; + uint64_t rgb_matrix; + }; + uint32_t haptic; + uint8_t rgblight_ext; +} eeprom_core_t; + +/* EEPROM parameter address */ +#define EECONFIG_MAGIC (uint16_t *)(offsetof(eeprom_core_t, magic)) +#define EECONFIG_DEBUG (uint8_t *)(offsetof(eeprom_core_t, debug)) +#define EECONFIG_DEFAULT_LAYER (uint8_t *)(offsetof(eeprom_core_t, default_layer)) +#define EECONFIG_KEYMAP (uint16_t *)(offsetof(eeprom_core_t, keymap)) +#define EECONFIG_BACKLIGHT (uint8_t *)(offsetof(eeprom_core_t, backlight)) +#define EECONFIG_AUDIO (uint8_t *)(offsetof(eeprom_core_t, audio)) +#define EECONFIG_RGBLIGHT (uint32_t *)(offsetof(eeprom_core_t, rgblight)) +#define EECONFIG_UNICODEMODE (uint8_t *)(offsetof(eeprom_core_t, unicode)) +#define EECONFIG_STENOMODE (uint8_t *)(offsetof(eeprom_core_t, steno)) +#define EECONFIG_HANDEDNESS (uint8_t *)(offsetof(eeprom_core_t, handedness)) +#define EECONFIG_KEYBOARD (uint32_t *)(offsetof(eeprom_core_t, keyboard)) +#define EECONFIG_USER (uint32_t *)(offsetof(eeprom_core_t, user)) +#define EECONFIG_LED_MATRIX (uint32_t *)(offsetof(eeprom_core_t, led_matrix)) +#define EECONFIG_RGB_MATRIX (uint64_t *)(offsetof(eeprom_core_t, rgb_matrix)) +#define EECONFIG_HAPTIC (uint32_t *)(offsetof(eeprom_core_t, haptic)) +#define EECONFIG_RGBLIGHT_EXTENDED (uint8_t *)(offsetof(eeprom_core_t, rgblight_ext)) + +// Size of EEPROM being used for core data storage +#define EECONFIG_BASE_SIZE ((uint8_t)sizeof(eeprom_core_t)) + +#define EECONFIG_KB_DATABLOCK ((uint8_t *)(EECONFIG_BASE_SIZE)) +#define EECONFIG_USER_DATABLOCK ((uint8_t *)((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE))) + +// Size of EEPROM being used, other code can refer to this for available EEPROM +#define EECONFIG_SIZE ((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE) + (EECONFIG_USER_DATA_SIZE)) + +_Static_assert((intptr_t)EECONFIG_HANDEDNESS == 14, "EEPROM handedness offset is incorrect"); diff --git a/quantum/nvm/eeprom/nvm_eeprom_via_internal.h b/quantum/nvm/eeprom/nvm_eeprom_via_internal.h new file mode 100644 index 0000000000..bf0d38e2ac --- /dev/null +++ b/quantum/nvm/eeprom/nvm_eeprom_via_internal.h @@ -0,0 +1,22 @@ +// Copyright 2024 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +// 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) + +// 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) + +#define VIA_EEPROM_CONFIG_END (VIA_EEPROM_CUSTOM_CONFIG_ADDR + VIA_EEPROM_CUSTOM_CONFIG_SIZE) diff --git a/quantum/nvm/eeprom/nvm_via.c b/quantum/nvm/eeprom/nvm_via.c new file mode 100644 index 0000000000..5372791fb7 --- /dev/null +++ b/quantum/nvm/eeprom/nvm_via.c @@ -0,0 +1,77 @@ +// Copyright 2024 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "eeprom.h" +#include "util.h" +#include "via.h" +#include "nvm_via.h" +#include "nvm_eeprom_eeconfig_internal.h" +#include "nvm_eeprom_via_internal.h" + +void nvm_via_erase(void) { + // No-op, nvm_eeconfig_erase() will have already erased EEPROM if necessary. +} + +void nvm_via_read_magic(uint8_t *magic0, uint8_t *magic1, uint8_t *magic2) { + if (magic0) { + *magic0 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0); + } + + if (magic1) { + *magic1 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1); + } + + if (magic2) { + *magic2 = eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2); + } +} + +void nvm_via_update_magic(uint8_t magic0, uint8_t magic1, uint8_t magic2) { + eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, magic0); + eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, magic1); + eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2, magic2); +} + +uint32_t nvm_via_read_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 nvm_via_update_layout_options(uint32_t val) { + // 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, val & 0xFF); + val = val >> 8; + target--; + } +} + +uint32_t nvm_via_read_custom_config(void *buf, uint32_t offset, uint32_t length) { +#if VIA_EEPROM_CUSTOM_CONFIG_SIZE > 0 + void *ee_start = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + offset); + void *ee_end = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + MIN(VIA_EEPROM_CUSTOM_CONFIG_SIZE, offset + length)); + eeprom_read_block(buf, ee_start, ee_end - ee_start); + return ee_end - ee_start; +#else + return 0; +#endif +} + +uint32_t nvm_via_update_custom_config(const void *buf, uint32_t offset, uint32_t length) { +#if VIA_EEPROM_CUSTOM_CONFIG_SIZE > 0 + void *ee_start = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + offset); + void *ee_end = (void *)(uintptr_t)(VIA_EEPROM_CUSTOM_CONFIG_ADDR + MIN(VIA_EEPROM_CUSTOM_CONFIG_SIZE, offset + length)); + eeprom_update_block(buf, ee_start, ee_end - ee_start); + return ee_end - ee_start; +#else + return 0; +#endif +} diff --git a/quantum/nvm/nvm_dynamic_keymap.h b/quantum/nvm/nvm_dynamic_keymap.h new file mode 100644 index 0000000000..d6e4aaee51 --- /dev/null +++ b/quantum/nvm/nvm_dynamic_keymap.h @@ -0,0 +1,27 @@ +// Copyright 2024 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +void nvm_dynamic_keymap_erase(void); +void nvm_dynamic_keymap_macro_erase(void); + +uint16_t nvm_dynamic_keymap_read_keycode(uint8_t layer, uint8_t row, uint8_t column); +void nvm_dynamic_keymap_update_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode); + +#ifdef ENCODER_MAP_ENABLE +uint16_t nvm_dynamic_keymap_read_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise); +void nvm_dynamic_keymap_update_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode); +#endif // ENCODER_MAP_ENABLE + +void nvm_dynamic_keymap_read_buffer(uint32_t offset, uint32_t size, uint8_t *data); +void nvm_dynamic_keymap_update_buffer(uint32_t offset, uint32_t size, uint8_t *data); + +uint32_t nvm_dynamic_keymap_macro_size(void); + +void nvm_dynamic_keymap_macro_read_buffer(uint32_t offset, uint32_t size, uint8_t *data); +void nvm_dynamic_keymap_macro_update_buffer(uint32_t offset, uint32_t size, uint8_t *data); + +void nvm_dynamic_keymap_macro_reset(void); diff --git a/quantum/nvm/nvm_eeconfig.h b/quantum/nvm/nvm_eeconfig.h new file mode 100644 index 0000000000..131f61d534 --- /dev/null +++ b/quantum/nvm/nvm_eeconfig.h @@ -0,0 +1,105 @@ +// Copyright 2024 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include <stdint.h> +#include <stdbool.h> +#include "action_layer.h" // layer_state_t + +#ifndef EECONFIG_MAGIC_NUMBER +# define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEE3 // When changing, decrement this value to avoid future re-init issues +#endif +#define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF + +void nvm_eeconfig_erase(void); + +bool nvm_eeconfig_is_enabled(void); +bool nvm_eeconfig_is_disabled(void); + +void nvm_eeconfig_enable(void); +void nvm_eeconfig_disable(void); + +typedef union debug_config_t debug_config_t; +void nvm_eeconfig_read_debug(debug_config_t *debug_config); +void nvm_eeconfig_update_debug(const debug_config_t *debug_config); + +layer_state_t nvm_eeconfig_read_default_layer(void); +void nvm_eeconfig_update_default_layer(layer_state_t state); + +typedef union keymap_config_t keymap_config_t; +void nvm_eeconfig_read_keymap(keymap_config_t *keymap_config); +void nvm_eeconfig_update_keymap(const keymap_config_t *keymap_config); + +#ifdef AUDIO_ENABLE +typedef union audio_config_t audio_config_t; +void nvm_eeconfig_read_audio(audio_config_t *audio_config); +void nvm_eeconfig_update_audio(const audio_config_t *audio_config); +#endif // AUDIO_ENABLE + +#ifdef UNICODE_COMMON_ENABLE +typedef union unicode_config_t unicode_config_t; +void nvm_eeconfig_read_unicode_mode(unicode_config_t *unicode_config); +void nvm_eeconfig_update_unicode_mode(const unicode_config_t *unicode_config); +#endif // UNICODE_COMMON_ENABLE + +#ifdef BACKLIGHT_ENABLE +typedef union backlight_config_t backlight_config_t; +void nvm_eeconfig_read_backlight(backlight_config_t *backlight_config); +void nvm_eeconfig_update_backlight(const backlight_config_t *backlight_config); +#endif // BACKLIGHT_ENABLE + +#ifdef STENO_ENABLE +uint8_t nvm_eeconfig_read_steno_mode(void); +void nvm_eeconfig_update_steno_mode(uint8_t val); +#endif // STENO_ENABLE + +#ifdef RGB_MATRIX_ENABLE +typedef union rgb_config_t rgb_config_t; +void nvm_eeconfig_read_rgb_matrix(rgb_config_t *rgb_matrix_config); +void nvm_eeconfig_update_rgb_matrix(const rgb_config_t *rgb_matrix_config); +#endif + +#ifdef LED_MATRIX_ENABLE +typedef union led_eeconfig_t led_eeconfig_t; +void nvm_eeconfig_read_led_matrix(led_eeconfig_t *led_matrix_config); +void nvm_eeconfig_update_led_matrix(const led_eeconfig_t *led_matrix_config); +#endif // LED_MATRIX_ENABLE + +#ifdef RGBLIGHT_ENABLE +typedef union rgblight_config_t rgblight_config_t; +void nvm_eeconfig_read_rgblight(rgblight_config_t *rgblight_config); +void nvm_eeconfig_update_rgblight(const rgblight_config_t *rgblight_config); +#endif // RGBLIGHT_ENABLE + +#if (EECONFIG_KB_DATA_SIZE) == 0 +uint32_t nvm_eeconfig_read_kb(void); +void nvm_eeconfig_update_kb(uint32_t val); +#endif // (EECONFIG_KB_DATA_SIZE) == 0 + +#if (EECONFIG_USER_DATA_SIZE) == 0 +uint32_t nvm_eeconfig_read_user(void); +void nvm_eeconfig_update_user(uint32_t val); +#endif // (EECONFIG_USER_DATA_SIZE) == 0 + +#ifdef HAPTIC_ENABLE +typedef union haptic_config_t haptic_config_t; +void nvm_eeconfig_read_haptic(haptic_config_t *haptic_config); +void nvm_eeconfig_update_haptic(const haptic_config_t *haptic_config); +#endif // HAPTIC_ENABLE + +bool nvm_eeconfig_read_handedness(void); +void nvm_eeconfig_update_handedness(bool val); + +#if (EECONFIG_KB_DATA_SIZE) > 0 +bool nvm_eeconfig_is_kb_datablock_valid(void); +uint32_t nvm_eeconfig_read_kb_datablock(void *data, uint32_t offset, uint32_t length); +uint32_t nvm_eeconfig_update_kb_datablock(const void *data, uint32_t offset, uint32_t length); +void nvm_eeconfig_init_kb_datablock(void); +#endif // (EECONFIG_KB_DATA_SIZE) > 0 + +#if (EECONFIG_USER_DATA_SIZE) > 0 +bool nvm_eeconfig_is_user_datablock_valid(void); +uint32_t nvm_eeconfig_read_user_datablock(void *data, uint32_t offset, uint32_t length); +uint32_t nvm_eeconfig_update_user_datablock(const void *data, uint32_t offset, uint32_t length); +void nvm_eeconfig_init_user_datablock(void); +#endif // (EECONFIG_USER_DATA_SIZE) > 0 diff --git a/quantum/nvm/nvm_via.h b/quantum/nvm/nvm_via.h new file mode 100644 index 0000000000..90c5e67421 --- /dev/null +++ b/quantum/nvm/nvm_via.h @@ -0,0 +1,17 @@ +// Copyright 2024 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +void nvm_via_erase(void); + +void nvm_via_read_magic(uint8_t *magic0, uint8_t *magic1, uint8_t *magic2); +void nvm_via_update_magic(uint8_t magic0, uint8_t magic1, uint8_t magic2); + +uint32_t nvm_via_read_layout_options(void); +void nvm_via_update_layout_options(uint32_t val); + +uint32_t nvm_via_read_custom_config(void *buf, uint32_t offset, uint32_t length); +uint32_t nvm_via_update_custom_config(const void *buf, uint32_t offset, uint32_t length); diff --git a/quantum/nvm/readme.md b/quantum/nvm/readme.md new file mode 100644 index 0000000000..0731695e92 --- /dev/null +++ b/quantum/nvm/readme.md @@ -0,0 +1,30 @@ +# Non-volatile Memory - Data Repositories + +This area is intentionally structured in the following way: + +``` +╰- quantum + ╰- nvm + ├- readme.md + ├- rules.mk + | + ├- nvm_eeconfig.h + ├- nvm_<<system>>.h + | + ├- eeprom + | ├- nvm_eeconfig.c + | ├- nvm_<<system>>.c + | ╰- ... + | + ├- <<another provider>> + | ├- nvm_eeconfig.c + | ├- nvm_<<system>>.c + | ╰- ... + ╰- ... +``` + +At the base `nvm` level, for every QMK core system which requires persistence there must be a corresponding `nvm_<<system>>.h` header file. This provides the data repository API to the "owner" system, and allows the underlying data persistence mechanism to be abstracted away from upper code. Any conversion to/from a `.raw` field should occur inside the `nvm_<<system>>.c` layer, with the API using values, such as structs or unions exposed to the rest of QMK. + +Each `nvm` "provider" is a corresponding child directory consisting of its name, such as `eeprom`, and corresponding `nvm_<<system>>.c` implementation files which provide the concrete implementation of the upper `nvm_<<system>>.h`. + +New systems requiring persistence can add the corresponding `nvm_<<system>>.h` file, and in most circumstances must also implement equivalent `nvm_<<system>>.c` files for every `nvm` provider. If persistence is not possible for that system, a `nvm_<<system>>.c` file with simple stubs which ignore writes and provide sane defaults must be used instead.
\ No newline at end of file diff --git a/quantum/nvm/rules.mk b/quantum/nvm/rules.mk new file mode 100644 index 0000000000..c9c1fabfd4 --- /dev/null +++ b/quantum/nvm/rules.mk @@ -0,0 +1,31 @@ +# Copyright 2024 Nick Brassel (@tzarc) +# SPDX-License-Identifier: GPL-2.0-or-later + +VPATH += $(QUANTUM_DIR)/nvm + +VALID_NVM_DRIVERS := eeprom custom none + +NVM_DRIVER ?= eeprom + +ifeq ($(filter $(NVM_DRIVER),$(VALID_NVM_DRIVERS)),) + $(call CATASTROPHIC_ERROR,Invalid NVM_DRIVER,NVM_DRIVER="$(NVM_DRIVER)" is not a valid NVM driver) +else + + # If we don't want one, fake it with transient eeprom. + ifeq ($(NVM_DRIVER),none) + NVM_DRIVER := eeprom + EEPROM_DRIVER := transient + endif + + NVM_DRIVER_UPPER := $(shell echo $(NVM_DRIVER) | tr '[:lower:]' '[:upper:]') + NVM_DRIVER_LOWER := $(shell echo $(NVM_DRIVER) | tr '[:upper:]' '[:lower:]') + + OPT_DEFS += -DNVM_DRIVER_$(NVM_DRIVER_UPPER) -DNVM_DRIVER="$(NVM_DRIVER)" + + ifneq ("$(wildcard $(QUANTUM_DIR)/nvm/$(NVM_DRIVER_LOWER))","") + COMMON_VPATH += $(QUANTUM_DIR)/nvm/$(NVM_DRIVER_LOWER) + endif + + QUANTUM_SRC += nvm_eeconfig.c + +endif |