From 6c170b1d240b0979a5240ffd59f75dfe4f0c86f1 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Sat, 5 Nov 2022 06:20:11 +1100 Subject: [PATCH] Formalise keyboard- and user-specific EEPROM blocks (#18874) * Formalise keyboard and user eeconfig datablocks. * Add debounce helper that checks a condition and issues post-write function invocation. * Naming. --- quantum/dynamic_keymap.c | 4 +-- quantum/eeconfig.c | 66 +++++++++++++++++++++++++++++++++++ quantum/eeconfig.h | 74 +++++++++++++++++++++++++++++++++++----- 3 files changed, 133 insertions(+), 11 deletions(-) diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index f78f46a26c53..ce201a81f04a 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -28,9 +28,7 @@ # include "via.h" // for VIA_EEPROM_CONFIG_END # define DYNAMIC_KEYMAP_EEPROM_START (VIA_EEPROM_CONFIG_END) #else -# ifndef DYNAMIC_KEYMAP_EEPROM_START -# define DYNAMIC_KEYMAP_EEPROM_START (EECONFIG_SIZE) -# endif +# define DYNAMIC_KEYMAP_EEPROM_START (EECONFIG_SIZE) #endif #ifdef ENCODER_ENABLE diff --git a/quantum/eeconfig.c b/quantum/eeconfig.c index 880b868ae0ec..6796958c73a0 100644 --- a/quantum/eeconfig.c +++ b/quantum/eeconfig.c @@ -1,3 +1,4 @@ +#include #include #include #include "eeprom.h" @@ -26,13 +27,17 @@ void dynamic_keymap_reset(void); * FIXME: needs doc */ __attribute__((weak)) void eeconfig_init_user(void) { +#if (EECONFIG_USER_DATA_SIZE) == 0 // Reset user EEPROM value to blank, rather than to a set value eeconfig_update_user(0); +#endif } __attribute__((weak)) void eeconfig_init_kb(void) { +#if (EECONFIG_KB_DATA_SIZE) == 0 // Reset Keyboard EEPROM value to blank, rather than to a set value eeconfig_update_kb(0); +#endif eeconfig_init_user(); } @@ -67,6 +72,19 @@ void eeconfig_init_quantum(void) { // when a haptic-enabled firmware is loaded onto the keyboard. eeprom_update_dword(EECONFIG_HAPTIC, 0); #endif + +#if (EECONFIG_KB_DATA_SIZE) > 0 + eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION)); + uint8_t dummy_kb[(EECONFIG_KB_DATA_SIZE)] = {0}; + eeprom_update_block(EECONFIG_KB_DATABLOCK, dummy_kb, (EECONFIG_KB_DATA_SIZE)); +#endif + +#if (EECONFIG_USER_DATA_SIZE) > 0 + eeprom_update_dword(EECONFIG_USER, (EECONFIG_USER_DATA_VERSION)); + uint8_t dummy_user[(EECONFIG_USER_DATA_SIZE)] = {0}; + eeprom_update_block(EECONFIG_USER_DATABLOCK, dummy_user, (EECONFIG_USER_DATA_SIZE)); +#endif + #if defined(VIA_ENABLE) // Invalidate VIA eeprom config, and then reset. // Just in case if power is lost mid init, this makes sure that it pets @@ -203,6 +221,7 @@ void eeconfig_update_audio(uint8_t val) { eeprom_update_byte(EECONFIG_AUDIO, val); } +#if (EECONFIG_KB_DATA_SIZE) == 0 /** \brief eeconfig read kb * * FIXME: needs doc @@ -217,7 +236,9 @@ uint32_t eeconfig_read_kb(void) { void eeconfig_update_kb(uint32_t val) { eeprom_update_dword(EECONFIG_KEYBOARD, val); } +#endif // (EECONFIG_KB_DATA_SIZE) == 0 +#if (EECONFIG_USER_DATA_SIZE) == 0 /** \brief eeconfig read user * * FIXME: needs doc @@ -232,6 +253,7 @@ uint32_t eeconfig_read_user(void) { void eeconfig_update_user(uint32_t val) { eeprom_update_dword(EECONFIG_USER, val); } +#endif // (EECONFIG_USER_DATA_SIZE) == 0 /** \brief eeconfig read haptic * @@ -262,3 +284,47 @@ bool eeconfig_read_handedness(void) { void eeconfig_update_handedness(bool val) { eeprom_update_byte(EECONFIG_HANDEDNESS, !!val); } + +#if (EECONFIG_KB_DATA_SIZE) > 0 +/** \brief eeconfig read keyboard data block + * + * FIXME: needs doc + */ +void eeconfig_read_kb_datablock(void *data) { + if (eeprom_read_dword(EECONFIG_KEYBOARD) == (EECONFIG_KB_DATA_VERSION)) { + eeprom_read_block(data, EECONFIG_KB_DATABLOCK, (EECONFIG_KB_DATA_SIZE)); + } else { + memset(data, 0, (EECONFIG_KB_DATA_SIZE)); + } +} +/** \brief eeconfig update keyboard data block + * + * FIXME: needs doc + */ +void eeconfig_update_kb_datablock(const void *data) { + eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION)); + eeprom_update_block(data, EECONFIG_KB_DATABLOCK, (EECONFIG_KB_DATA_SIZE)); +} +#endif // (EECONFIG_KB_DATA_SIZE) > 0 + +#if (EECONFIG_USER_DATA_SIZE) > 0 +/** \brief eeconfig read user data block + * + * FIXME: needs doc + */ +void eeconfig_read_user_datablock(void *data) { + if (eeprom_read_dword(EECONFIG_USER) == (EECONFIG_USER_DATA_VERSION)) { + eeprom_read_block(data, EECONFIG_USER_DATABLOCK, (EECONFIG_USER_DATA_SIZE)); + } else { + memset(data, 0, (EECONFIG_USER_DATA_SIZE)); + } +} +/** \brief eeconfig update user data block + * + * FIXME: needs doc + */ +void eeconfig_update_user_datablock(const void *data) { + eeprom_update_dword(EECONFIG_USER, (EECONFIG_USER_DATA_VERSION)); + eeprom_update_block(data, EECONFIG_USER_DATABLOCK, (EECONFIG_USER_DATA_SIZE)); +} +#endif // (EECONFIG_USER_DATA_SIZE) > 0 diff --git a/quantum/eeconfig.h b/quantum/eeconfig.h index 95b416883ede..5615fb0ae2f8 100644 --- a/quantum/eeconfig.h +++ b/quantum/eeconfig.h @@ -30,7 +30,6 @@ along with this program. If not, see . #define EECONFIG_DEBUG (uint8_t *)2 #define EECONFIG_DEFAULT_LAYER (uint8_t *)3 #define EECONFIG_KEYMAP (uint16_t *)4 -#define EECONFIG_MOUSEKEY_ACCEL (uint8_t *)5 #define EECONFIG_BACKLIGHT (uint8_t *)6 #define EECONFIG_AUDIO (uint8_t *)7 #define EECONFIG_RGBLIGHT (uint32_t *)8 @@ -53,8 +52,28 @@ along with this program. If not, see . #define EECONFIG_KEYMAP_HASH (uint32_t *)34 +// Size of EEPROM being used for core data storage +#define EECONFIG_BASE_SIZE 38 + +// Size of EEPROM dedicated to keyboard- and user-specific data +#ifndef EECONFIG_KB_DATA_SIZE +# define EECONFIG_KB_DATA_SIZE 0 +#endif +#ifndef EECONFIG_KB_DATA_VERSION +# define EECONFIG_KB_DATA_VERSION (EECONFIG_KB_DATA_SIZE) +#endif +#ifndef EECONFIG_USER_DATA_SIZE +# define EECONFIG_USER_DATA_SIZE 0 +#endif +#ifndef EECONFIG_USER_DATA_VERSION +# define EECONFIG_USER_DATA_VERSION (EECONFIG_USER_DATA_SIZE) +#endif + +#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 38 +#define EECONFIG_SIZE ((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE) + (EECONFIG_USER_DATA_SIZE)) /* debug bit */ #define EECONFIG_DEBUG_ENABLE (1 << 0) @@ -98,10 +117,15 @@ uint8_t eeconfig_read_audio(void); void eeconfig_update_audio(uint8_t val); #endif +#if (EECONFIG_KB_DATA_SIZE) == 0 uint32_t eeconfig_read_kb(void); void eeconfig_update_kb(uint32_t val); +#endif // (EECONFIG_KB_DATA_SIZE) == 0 + +#if (EECONFIG_USER_DATA_SIZE) == 0 uint32_t eeconfig_read_user(void); void eeconfig_update_user(uint32_t val); +#endif // (EECONFIG_USER_DATA_SIZE) == 0 #ifdef HAPTIC_ENABLE uint32_t eeconfig_read_haptic(void); @@ -111,16 +135,40 @@ void eeconfig_update_haptic(uint32_t val); bool eeconfig_read_handedness(void); void eeconfig_update_handedness(bool val); -#define EECONFIG_DEBOUNCE_HELPER(name, offset, config) \ +#if (EECONFIG_KB_DATA_SIZE) > 0 +bool eeconfig_is_kb_datablock_valid(void); +void eeconfig_read_kb_datablock(void *data); +void eeconfig_update_kb_datablock(const void *data); +void eeconfig_init_kb_datablock(void); +#endif // (EECONFIG_KB_DATA_SIZE) > 0 + +#if (EECONFIG_USER_DATA_SIZE) > 0 +bool eeconfig_is_user_datablock_valid(void); +void eeconfig_read_user_datablock(void *data); +void eeconfig_update_user_datablock(const void *data); +void eeconfig_init_user_datablock(void); +#endif // (EECONFIG_USER_DATA_SIZE) > 0 + +// Any "checked" debounce variant used requires implementation of: +// -- bool eeconfig_check_valid_##name(void) +// -- void eeconfig_post_flush_##name(void) +#define EECONFIG_DEBOUNCE_HELPER_CHECKED(name, offset, config) \ static uint8_t dirty_##name = false; \ \ + bool eeconfig_check_valid_##name(void); \ + void eeconfig_post_flush_##name(void); \ + \ static inline void eeconfig_init_##name(void) { \ - eeprom_read_block(&config, offset, sizeof(config)); \ - dirty_##name = false; \ + dirty_##name = true; \ + if (eeconfig_check_valid_##name()) { \ + eeprom_read_block(&config, offset, sizeof(config)); \ + dirty_##name = false; \ + } \ } \ static inline void eeconfig_flush_##name(bool force) { \ if (force || dirty_##name) { \ eeprom_update_block(&config, offset, sizeof(config)); \ + eeconfig_post_flush_##name(); \ dirty_##name = false; \ } \ } \ @@ -134,7 +182,17 @@ void eeconfig_update_handedness(bool val); static inline void eeconfig_flag_##name(bool v) { \ dirty_##name |= v; \ } \ - static inline void eeconfig_write_##name(typeof(config) conf) { \ - memcpy(&config, &conf, sizeof(config)); \ - eeconfig_flag_##name(true); \ + static inline void eeconfig_write_##name(typeof(config) *conf) { \ + if (memcmp(&config, conf, sizeof(config)) != 0) { \ + memcpy(&config, conf, sizeof(config)); \ + eeconfig_flag_##name(true); \ + } \ } + +#define EECONFIG_DEBOUNCE_HELPER(name, offset, config) \ + EECONFIG_DEBOUNCE_HELPER_CHECKED(name, offset, config) \ + \ + bool eeconfig_check_valid_##name(void) { \ + return true; \ + } \ + void eeconfig_post_flush_##name(void) {}