eeconfig.c 11 KB

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