qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

eeconfig.c (10293B)


      1 #include <string.h>
      2 #include <stdint.h>
      3 #include <stdbool.h>
      4 #include "debug.h"
      5 #include "eeconfig.h"
      6 #include "action_layer.h"
      7 #include "nvm_eeconfig.h"
      8 #include "keycode_config.h"
      9 
     10 #ifdef BACKLIGHT_ENABLE
     11 #    include "backlight.h"
     12 #endif // BACKLIGHT_ENABLE
     13 
     14 #ifdef AUDIO_ENABLE
     15 #    include "audio.h"
     16 #endif // AUDIO_ENABLE
     17 
     18 #ifdef RGBLIGHT_ENABLE
     19 #    include "rgblight.h"
     20 #endif // RGBLIGHT_ENABLE
     21 
     22 #ifdef RGB_MATRIX_ENABLE
     23 #    include "rgb_matrix_types.h"
     24 #endif // RGB_MATRIX_ENABLE
     25 
     26 #ifdef LED_MATRIX_ENABLE
     27 #    include "led_matrix_types.h"
     28 #endif // LED_MATRIX_ENABLE
     29 
     30 #ifdef UNICODE_COMMON_ENABLE
     31 #    include "unicode.h"
     32 #endif // UNICODE_COMMON_ENABLE
     33 
     34 #ifdef HAPTIC_ENABLE
     35 #    include "haptic.h"
     36 #endif // HAPTIC_ENABLE
     37 
     38 #ifdef CONNECTION_ENABLE
     39 #    include "connection.h"
     40 #endif // CONNECTION_ENABLE
     41 
     42 #ifdef VIA_ENABLE
     43 bool via_eeprom_is_valid(void);
     44 void via_eeprom_set_valid(bool valid);
     45 void eeconfig_init_via(void);
     46 #else
     47 void dynamic_keymap_reset(void);
     48 #endif // VIA_ENABLE
     49 
     50 #ifndef NKRO_DEFAULT_ON
     51 #    define NKRO_DEFAULT_ON false
     52 #endif
     53 
     54 __attribute__((weak)) void eeconfig_init_user(void) {
     55 #if (EECONFIG_USER_DATA_SIZE) == 0
     56     // Reset user EEPROM value to blank, rather than to a set value
     57     eeconfig_update_user(0);
     58 #endif // (EECONFIG_USER_DATA_SIZE) == 0
     59 }
     60 
     61 __attribute__((weak)) void eeconfig_init_kb(void) {
     62 #if (EECONFIG_KB_DATA_SIZE) == 0
     63     // Reset Keyboard EEPROM value to blank, rather than to a set value
     64     eeconfig_update_kb(0);
     65 #endif // (EECONFIG_KB_DATA_SIZE) == 0
     66 
     67     eeconfig_init_user();
     68 }
     69 
     70 void eeconfig_init_quantum(void) {
     71     nvm_eeconfig_erase();
     72 
     73     eeconfig_enable();
     74 
     75     debug_config_t debug_config = {0};
     76     eeconfig_update_debug(&debug_config);
     77 
     78     default_layer_state = (layer_state_t)1 << 0;
     79     eeconfig_update_default_layer(default_layer_state);
     80 
     81     keymap_config_t keymap_config = {
     82         .swap_control_capslock    = false,
     83         .capslock_to_control      = false,
     84         .swap_lalt_lgui           = false,
     85         .swap_ralt_rgui           = false,
     86         .no_gui                   = false,
     87         .swap_grave_esc           = false,
     88         .swap_backslash_backspace = false,
     89         .nkro                     = NKRO_DEFAULT_ON,
     90         .swap_lctl_lgui           = false,
     91         .swap_rctl_rgui           = false,
     92         .oneshot_enable           = true, // Enable oneshot by default
     93         .swap_escape_capslock     = false,
     94         .autocorrect_enable       = true, // Enable autocorrect by default
     95     };
     96     eeconfig_update_keymap(&keymap_config);
     97 
     98 #ifdef BACKLIGHT_ENABLE
     99     backlight_config_t backlight_config = {0};
    100     eeconfig_update_backlight(&backlight_config);
    101 #endif // BACKLIGHT_ENABLE
    102 
    103 #ifdef AUDIO_ENABLE
    104     audio_config_t audio_config = {0};
    105     eeconfig_update_audio(&audio_config);
    106 #endif // AUDIO_ENABLE
    107 
    108 #ifdef RGBLIGHT_ENABLE
    109     extern void eeconfig_update_rgblight_default(void);
    110     eeconfig_update_rgblight_default();
    111 #endif // RGBLIGHT_ENABLE
    112 
    113 #ifdef UNICODE_COMMON_ENABLE
    114     unicode_config_t unicode_config = {0};
    115     eeconfig_update_unicode_mode(&unicode_config);
    116 #endif // UNICODE_COMMON_ENABLE
    117 
    118 #ifdef STENO_ENABLE
    119     eeconfig_update_steno_mode(0);
    120 #endif // STENO_ENABLE
    121 
    122 #ifdef RGB_MATRIX_ENABLE
    123     extern void eeconfig_update_rgb_matrix_default(void);
    124     eeconfig_update_rgb_matrix_default();
    125 #endif
    126 
    127 #ifdef LED_MATRIX_ENABLE
    128     extern void eeconfig_update_led_matrix_default(void);
    129     eeconfig_update_led_matrix_default();
    130 #endif // LED_MATRIX_ENABLE
    131 
    132 #ifdef HAPTIC_ENABLE
    133     haptic_config_t haptic_config = {0};
    134     eeconfig_update_haptic(&haptic_config);
    135     haptic_reset();
    136 #endif // HAPTIC_ENABLE
    137 
    138 #ifdef CONNECTION_ENABLE
    139     extern void eeconfig_update_connection_default(void);
    140     eeconfig_update_connection_default();
    141 #endif // CONNECTION_ENABLE
    142 
    143 #if (EECONFIG_KB_DATA_SIZE) > 0
    144     eeconfig_init_kb_datablock();
    145 #endif // (EECONFIG_KB_DATA_SIZE) > 0
    146 
    147 #if (EECONFIG_USER_DATA_SIZE) > 0
    148     eeconfig_init_user_datablock();
    149 #endif // (EECONFIG_USER_DATA_SIZE) > 0
    150 
    151 #if defined(VIA_ENABLE)
    152     // Invalidate VIA eeprom config, and then reset.
    153     // Just in case if power is lost mid init, this makes sure that it gets
    154     // properly re-initialized.
    155     eeconfig_init_via();
    156 #elif defined(DYNAMIC_KEYMAP_ENABLE)
    157     dynamic_keymap_reset();
    158 #endif
    159 
    160     eeconfig_init_kb();
    161 
    162 #ifdef RGB_MATRIX_ENABLE
    163     extern void eeconfig_force_flush_rgb_matrix(void);
    164     eeconfig_force_flush_rgb_matrix();
    165 #endif // RGB_MATRIX_ENABLE
    166 #ifdef LED_MATRIX_ENABLE
    167     extern void eeconfig_force_flush_led_matrix(void);
    168     eeconfig_force_flush_led_matrix();
    169 #endif // LED_MATRIX_ENABLE
    170 }
    171 
    172 void eeconfig_init(void) {
    173     eeconfig_init_quantum();
    174 }
    175 
    176 void eeconfig_enable(void) {
    177     nvm_eeconfig_enable();
    178 }
    179 
    180 void eeconfig_disable(void) {
    181     nvm_eeconfig_disable();
    182 }
    183 
    184 bool eeconfig_is_enabled(void) {
    185     bool is_eeprom_enabled = nvm_eeconfig_is_enabled();
    186 #ifdef VIA_ENABLE
    187     if (is_eeprom_enabled) {
    188         is_eeprom_enabled = via_eeprom_is_valid();
    189     }
    190 #endif // VIA_ENABLE
    191     return is_eeprom_enabled;
    192 }
    193 
    194 bool eeconfig_is_disabled(void) {
    195     bool is_eeprom_disabled = nvm_eeconfig_is_disabled();
    196 #ifdef VIA_ENABLE
    197     if (!is_eeprom_disabled) {
    198         is_eeprom_disabled = !via_eeprom_is_valid();
    199     }
    200 #endif // VIA_ENABLE
    201     return is_eeprom_disabled;
    202 }
    203 
    204 void eeconfig_read_debug(debug_config_t *debug_config) {
    205     nvm_eeconfig_read_debug(debug_config);
    206 }
    207 void eeconfig_update_debug(const debug_config_t *debug_config) {
    208     nvm_eeconfig_update_debug(debug_config);
    209 }
    210 
    211 layer_state_t eeconfig_read_default_layer(void) {
    212     return nvm_eeconfig_read_default_layer();
    213 }
    214 void eeconfig_update_default_layer(layer_state_t state) {
    215     nvm_eeconfig_update_default_layer(state);
    216 }
    217 
    218 void eeconfig_read_keymap(keymap_config_t *keymap_config) {
    219     nvm_eeconfig_read_keymap(keymap_config);
    220 }
    221 void eeconfig_update_keymap(const keymap_config_t *keymap_config) {
    222     nvm_eeconfig_update_keymap(keymap_config);
    223 }
    224 
    225 #ifdef AUDIO_ENABLE
    226 void eeconfig_read_audio(audio_config_t *audio_config) {
    227     nvm_eeconfig_read_audio(audio_config);
    228 }
    229 void eeconfig_update_audio(const audio_config_t *audio_config) {
    230     nvm_eeconfig_update_audio(audio_config);
    231 }
    232 #endif // AUDIO_ENABLE
    233 
    234 #ifdef UNICODE_COMMON_ENABLE
    235 void eeconfig_read_unicode_mode(unicode_config_t *unicode_config) {
    236     return nvm_eeconfig_read_unicode_mode(unicode_config);
    237 }
    238 void eeconfig_update_unicode_mode(const unicode_config_t *unicode_config) {
    239     nvm_eeconfig_update_unicode_mode(unicode_config);
    240 }
    241 #endif // UNICODE_COMMON_ENABLE
    242 
    243 #ifdef BACKLIGHT_ENABLE
    244 void eeconfig_read_backlight(backlight_config_t *backlight_config) {
    245     nvm_eeconfig_read_backlight(backlight_config);
    246 }
    247 void eeconfig_update_backlight(const backlight_config_t *backlight_config) {
    248     nvm_eeconfig_update_backlight(backlight_config);
    249 }
    250 #endif // BACKLIGHT_ENABLE
    251 
    252 #ifdef STENO_ENABLE
    253 uint8_t eeconfig_read_steno_mode(void) {
    254     return nvm_eeconfig_read_steno_mode();
    255 }
    256 void eeconfig_update_steno_mode(uint8_t val) {
    257     nvm_eeconfig_update_steno_mode(val);
    258 }
    259 #endif // STENO_ENABLE
    260 
    261 #ifdef RGB_MATRIX_ENABLE
    262 void eeconfig_read_rgb_matrix(rgb_config_t *rgb_matrix_config) {
    263     nvm_eeconfig_read_rgb_matrix(rgb_matrix_config);
    264 }
    265 void eeconfig_update_rgb_matrix(const rgb_config_t *rgb_matrix_config) {
    266     nvm_eeconfig_update_rgb_matrix(rgb_matrix_config);
    267 }
    268 #endif // RGB_MATRIX_ENABLE
    269 
    270 #ifdef LED_MATRIX_ENABLE
    271 void eeconfig_read_led_matrix(led_eeconfig_t *led_matrix_config) {
    272     nvm_eeconfig_read_led_matrix(led_matrix_config);
    273 }
    274 void eeconfig_update_led_matrix(const led_eeconfig_t *led_matrix_config) {
    275     nvm_eeconfig_update_led_matrix(led_matrix_config);
    276 }
    277 #endif // LED_MATRIX_ENABLE
    278 
    279 #ifdef RGBLIGHT_ENABLE
    280 void eeconfig_read_rgblight(rgblight_config_t *rgblight_config) {
    281     nvm_eeconfig_read_rgblight(rgblight_config);
    282 }
    283 void eeconfig_update_rgblight(const rgblight_config_t *rgblight_config) {
    284     nvm_eeconfig_update_rgblight(rgblight_config);
    285 }
    286 #endif // RGBLIGHT_ENABLE
    287 
    288 #if (EECONFIG_KB_DATA_SIZE) == 0
    289 uint32_t eeconfig_read_kb(void) {
    290     return nvm_eeconfig_read_kb();
    291 }
    292 void eeconfig_update_kb(uint32_t val) {
    293     nvm_eeconfig_update_kb(val);
    294 }
    295 #endif // (EECONFIG_KB_DATA_SIZE) == 0
    296 
    297 #if (EECONFIG_USER_DATA_SIZE) == 0
    298 uint32_t eeconfig_read_user(void) {
    299     return nvm_eeconfig_read_user();
    300 }
    301 void eeconfig_update_user(uint32_t val) {
    302     nvm_eeconfig_update_user(val);
    303 }
    304 #endif // (EECONFIG_USER_DATA_SIZE) == 0
    305 
    306 #ifdef HAPTIC_ENABLE
    307 void eeconfig_read_haptic(haptic_config_t *haptic_config) {
    308     nvm_eeconfig_read_haptic(haptic_config);
    309 }
    310 void eeconfig_update_haptic(const haptic_config_t *haptic_config) {
    311     nvm_eeconfig_update_haptic(haptic_config);
    312 }
    313 #endif // HAPTIC_ENABLE
    314 
    315 #ifdef CONNECTION_ENABLE
    316 void eeconfig_read_connection(connection_config_t *config) {
    317     nvm_eeconfig_read_connection(config);
    318 }
    319 void eeconfig_update_connection(const connection_config_t *config) {
    320     nvm_eeconfig_update_connection(config);
    321 }
    322 #endif // CONNECTION_ENABLE
    323 
    324 bool eeconfig_read_handedness(void) {
    325     return nvm_eeconfig_read_handedness();
    326 }
    327 void eeconfig_update_handedness(bool val) {
    328     nvm_eeconfig_update_handedness(val);
    329 }
    330 
    331 #if (EECONFIG_KB_DATA_SIZE) > 0
    332 bool eeconfig_is_kb_datablock_valid(void) {
    333     return nvm_eeconfig_is_kb_datablock_valid();
    334 }
    335 uint32_t eeconfig_read_kb_datablock(void *data, uint32_t offset, uint32_t length) {
    336     return nvm_eeconfig_read_kb_datablock(data, offset, length);
    337 }
    338 uint32_t eeconfig_update_kb_datablock(const void *data, uint32_t offset, uint32_t length) {
    339     return nvm_eeconfig_update_kb_datablock(data, offset, length);
    340 }
    341 __attribute__((weak)) void eeconfig_init_kb_datablock(void) {
    342     nvm_eeconfig_init_kb_datablock();
    343 }
    344 #endif // (EECONFIG_KB_DATA_SIZE) > 0
    345 
    346 #if (EECONFIG_USER_DATA_SIZE) > 0
    347 bool eeconfig_is_user_datablock_valid(void) {
    348     return nvm_eeconfig_is_user_datablock_valid();
    349 }
    350 uint32_t eeconfig_read_user_datablock(void *data, uint32_t offset, uint32_t length) {
    351     return nvm_eeconfig_read_user_datablock(data, offset, length);
    352 }
    353 uint32_t eeconfig_update_user_datablock(const void *data, uint32_t offset, uint32_t length) {
    354     return nvm_eeconfig_update_user_datablock(data, offset, length);
    355 }
    356 __attribute__((weak)) void eeconfig_init_user_datablock(void) {
    357     nvm_eeconfig_init_user_datablock();
    358 }
    359 #endif // (EECONFIG_USER_DATA_SIZE) > 0