eeconfig.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include <string.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include "eeprom.h"
  5. #include "eeconfig.h"
  6. #include "action_layer.h"
  7. #if defined(EEPROM_DRIVER)
  8. # include "eeprom_driver.h"
  9. #endif
  10. #if defined(HAPTIC_ENABLE)
  11. # include "haptic.h"
  12. #endif
  13. #if defined(VIA_ENABLE)
  14. bool via_eeprom_is_valid(void);
  15. void via_eeprom_set_valid(bool valid);
  16. void eeconfig_init_via(void);
  17. #elif defined(DYNAMIC_KEYMAP_ENABLE)
  18. bool dynamic_keymap_is_valid(void);
  19. void dynamic_keymap_reset(void);
  20. void dynamic_keymap_macro_reset(void);
  21. #endif
  22. /** \brief eeconfig enable
  23. *
  24. * FIXME: needs doc
  25. */
  26. __attribute__((weak)) void eeconfig_init_user(void) {
  27. #if (EECONFIG_USER_DATA_SIZE) == 0
  28. // Reset user EEPROM value to blank, rather than to a set value
  29. eeconfig_update_user(0);
  30. #endif
  31. }
  32. __attribute__((weak)) void eeconfig_init_kb(void) {
  33. #if (EECONFIG_KB_DATA_SIZE) == 0
  34. // Reset Keyboard EEPROM value to blank, rather than to a set value
  35. eeconfig_update_kb(0);
  36. #endif
  37. eeconfig_init_user();
  38. }
  39. /*
  40. * FIXME: needs doc
  41. */
  42. void eeconfig_init_quantum(void) {
  43. #if defined(EEPROM_DRIVER)
  44. eeprom_driver_erase();
  45. #endif
  46. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
  47. eeprom_update_byte(EECONFIG_DEBUG, 0);
  48. eeprom_update_byte(EECONFIG_DEFAULT_LAYER, 0);
  49. default_layer_state = 0;
  50. // Enable oneshot and autocorrect by default: 0b0001 0100 0000 0000
  51. eeprom_update_word(EECONFIG_KEYMAP, 0x1400);
  52. eeprom_update_byte(EECONFIG_BACKLIGHT, 0);
  53. eeprom_update_byte(EECONFIG_AUDIO, 0xFF); // On by default
  54. eeprom_update_dword(EECONFIG_RGBLIGHT, 0);
  55. eeprom_update_byte(EECONFIG_STENOMODE, 0);
  56. eeprom_update_dword(EECONFIG_HAPTIC, 0);
  57. eeprom_update_byte(EECONFIG_VELOCIKEY, 0);
  58. eeprom_update_dword(EECONFIG_RGB_MATRIX, 0);
  59. eeprom_update_word(EECONFIG_RGB_MATRIX_EXTENDED, 0);
  60. #if defined(HAPTIC_ENABLE)
  61. haptic_reset();
  62. #else
  63. // this is used in case haptic is disabled, but we still want sane defaults
  64. // in the haptic configuration eeprom. All zero will trigger a haptic_reset
  65. // when a haptic-enabled firmware is loaded onto the keyboard.
  66. eeprom_update_dword(EECONFIG_HAPTIC, 0);
  67. #endif
  68. #if (EECONFIG_KB_DATA_SIZE) > 0
  69. eeconfig_init_kb_datablock();
  70. #endif
  71. #if (EECONFIG_USER_DATA_SIZE) > 0
  72. eeconfig_init_user_datablock();
  73. #endif
  74. #if defined(VIA_ENABLE)
  75. // Invalidate VIA eeprom config, and then reset.
  76. // Just in case if power is lost mid init, this makes sure that it pets
  77. // properly re-initialized.
  78. via_eeprom_set_valid(false);
  79. eeconfig_init_via();
  80. #elif defined(DYNAMIC_KEYMAP_ENABLE)
  81. dynamic_keymap_reset();
  82. dynamic_keymap_macro_reset();
  83. #endif
  84. eeconfig_init_kb();
  85. }
  86. /** \brief eeconfig initialization
  87. *
  88. * FIXME: needs doc
  89. */
  90. void eeconfig_init(void) {
  91. eeconfig_init_quantum();
  92. }
  93. /** \brief eeconfig enable
  94. *
  95. * FIXME: needs doc
  96. */
  97. void eeconfig_enable(void) {
  98. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
  99. }
  100. /** \brief eeconfig disable
  101. *
  102. * FIXME: needs doc
  103. */
  104. void eeconfig_disable(void) {
  105. #if defined(EEPROM_DRIVER)
  106. eeprom_driver_erase();
  107. #endif
  108. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER_OFF);
  109. }
  110. /** \brief eeconfig is enabled
  111. *
  112. * FIXME: needs doc
  113. */
  114. bool eeconfig_is_enabled(void) {
  115. bool is_eeprom_enabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
  116. #if defined(VIA_ENABLE)
  117. if (is_eeprom_enabled) {
  118. is_eeprom_enabled = via_eeprom_is_valid();
  119. }
  120. #elif defined(DYNAMIC_KEYMAP_ENABLE)
  121. if (is_eeprom_enabled) {
  122. is_eeprom_enabled = dynamic_keymap_is_valid();
  123. }
  124. #endif
  125. return is_eeprom_enabled;
  126. }
  127. /** \brief eeconfig is disabled
  128. *
  129. * FIXME: needs doc
  130. */
  131. bool eeconfig_is_disabled(void) {
  132. bool is_eeprom_disabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER_OFF);
  133. #if defined(VIA_ENABLE)
  134. if (!is_eeprom_disabled) {
  135. is_eeprom_disabled = !via_eeprom_is_valid();
  136. }
  137. #elif defined(DYNAMIC_KEYMAP_ENABLE)
  138. if (!is_eeprom_disabled) {
  139. is_eeprom_disabled = !dynamic_keymap_is_valid();
  140. }
  141. #endif
  142. return is_eeprom_disabled;
  143. }
  144. /** \brief eeconfig read debug
  145. *
  146. * FIXME: needs doc
  147. */
  148. uint8_t eeconfig_read_debug(void) {
  149. return eeprom_read_byte(EECONFIG_DEBUG);
  150. }
  151. /** \brief eeconfig update debug
  152. *
  153. * FIXME: needs doc
  154. */
  155. void eeconfig_update_debug(uint8_t val) {
  156. eeprom_update_byte(EECONFIG_DEBUG, val);
  157. }
  158. /** \brief eeconfig read default layer
  159. *
  160. * FIXME: needs doc
  161. */
  162. uint8_t eeconfig_read_default_layer(void) {
  163. return eeprom_read_byte(EECONFIG_DEFAULT_LAYER);
  164. }
  165. /** \brief eeconfig update default layer
  166. *
  167. * FIXME: needs doc
  168. */
  169. void eeconfig_update_default_layer(uint8_t val) {
  170. eeprom_update_byte(EECONFIG_DEFAULT_LAYER, val);
  171. }
  172. /** \brief eeconfig read keymap
  173. *
  174. * FIXME: needs doc
  175. */
  176. uint16_t eeconfig_read_keymap(void) {
  177. return eeprom_read_word(EECONFIG_KEYMAP);
  178. }
  179. /** \brief eeconfig update keymap
  180. *
  181. * FIXME: needs doc
  182. */
  183. void eeconfig_update_keymap(uint16_t val) {
  184. eeprom_update_word(EECONFIG_KEYMAP, val);
  185. }
  186. /** \brief eeconfig read audio
  187. *
  188. * FIXME: needs doc
  189. */
  190. uint8_t eeconfig_read_audio(void) {
  191. return eeprom_read_byte(EECONFIG_AUDIO);
  192. }
  193. /** \brief eeconfig update audio
  194. *
  195. * FIXME: needs doc
  196. */
  197. void eeconfig_update_audio(uint8_t val) {
  198. eeprom_update_byte(EECONFIG_AUDIO, val);
  199. }
  200. #if (EECONFIG_KB_DATA_SIZE) == 0
  201. /** \brief eeconfig read kb
  202. *
  203. * FIXME: needs doc
  204. */
  205. uint32_t eeconfig_read_kb(void) {
  206. return eeprom_read_dword(EECONFIG_KEYBOARD);
  207. }
  208. /** \brief eeconfig update kb
  209. *
  210. * FIXME: needs doc
  211. */
  212. void eeconfig_update_kb(uint32_t val) {
  213. eeprom_update_dword(EECONFIG_KEYBOARD, val);
  214. }
  215. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  216. #if (EECONFIG_USER_DATA_SIZE) == 0
  217. /** \brief eeconfig read user
  218. *
  219. * FIXME: needs doc
  220. */
  221. uint32_t eeconfig_read_user(void) {
  222. return eeprom_read_dword(EECONFIG_USER);
  223. }
  224. /** \brief eeconfig update user
  225. *
  226. * FIXME: needs doc
  227. */
  228. void eeconfig_update_user(uint32_t val) {
  229. eeprom_update_dword(EECONFIG_USER, val);
  230. }
  231. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  232. /** \brief eeconfig read haptic
  233. *
  234. * FIXME: needs doc
  235. */
  236. uint32_t eeconfig_read_haptic(void) {
  237. return eeprom_read_dword(EECONFIG_HAPTIC);
  238. }
  239. /** \brief eeconfig update haptic
  240. *
  241. * FIXME: needs doc
  242. */
  243. void eeconfig_update_haptic(uint32_t val) {
  244. eeprom_update_dword(EECONFIG_HAPTIC, val);
  245. }
  246. /** \brief eeconfig read split handedness
  247. *
  248. * FIXME: needs doc
  249. */
  250. bool eeconfig_read_handedness(void) {
  251. return !!eeprom_read_byte(EECONFIG_HANDEDNESS);
  252. }
  253. /** \brief eeconfig update split handedness
  254. *
  255. * FIXME: needs doc
  256. */
  257. void eeconfig_update_handedness(bool val) {
  258. eeprom_update_byte(EECONFIG_HANDEDNESS, !!val);
  259. }
  260. #if (EECONFIG_KB_DATA_SIZE) > 0
  261. /** \brief eeconfig assert keyboard data block version
  262. *
  263. * FIXME: needs doc
  264. */
  265. bool eeconfig_is_kb_datablock_valid(void) {
  266. return eeprom_read_dword(EECONFIG_KEYBOARD) == (EECONFIG_KB_DATA_VERSION);
  267. }
  268. /** \brief eeconfig read keyboard data block
  269. *
  270. * FIXME: needs doc
  271. */
  272. void eeconfig_read_kb_datablock(void *data) {
  273. if (eeconfig_is_kb_datablock_valid()) {
  274. eeprom_read_block(data, EECONFIG_KB_DATABLOCK, (EECONFIG_KB_DATA_SIZE));
  275. } else {
  276. memset(data, 0, (EECONFIG_KB_DATA_SIZE));
  277. }
  278. }
  279. /** \brief eeconfig update keyboard data block
  280. *
  281. * FIXME: needs doc
  282. */
  283. void eeconfig_update_kb_datablock(const void *data) {
  284. eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION));
  285. eeprom_update_block(data, EECONFIG_KB_DATABLOCK, (EECONFIG_KB_DATA_SIZE));
  286. }
  287. /** \brief eeconfig init keyboard data block
  288. *
  289. * FIXME: needs doc
  290. */
  291. __attribute__((weak)) void eeconfig_init_kb_datablock(void) {
  292. uint8_t dummy_kb[(EECONFIG_KB_DATA_SIZE)] = {0};
  293. eeconfig_update_kb_datablock(dummy_kb);
  294. }
  295. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  296. #if (EECONFIG_USER_DATA_SIZE) > 0
  297. /** \brief eeconfig assert user data block version
  298. *
  299. * FIXME: needs doc
  300. */
  301. bool eeconfig_is_user_datablock_valid(void) {
  302. return eeprom_read_dword(EECONFIG_USER) == (EECONFIG_USER_DATA_VERSION);
  303. }
  304. /** \brief eeconfig read user data block
  305. *
  306. * FIXME: needs doc
  307. */
  308. void eeconfig_read_user_datablock(void *data) {
  309. if (eeconfig_is_user_datablock_valid()) {
  310. eeprom_read_block(data, EECONFIG_USER_DATABLOCK, (EECONFIG_USER_DATA_SIZE));
  311. } else {
  312. memset(data, 0, (EECONFIG_USER_DATA_SIZE));
  313. }
  314. }
  315. /** \brief eeconfig update user data block
  316. *
  317. * FIXME: needs doc
  318. */
  319. void eeconfig_update_user_datablock(const void *data) {
  320. eeprom_update_dword(EECONFIG_USER, (EECONFIG_USER_DATA_VERSION));
  321. eeprom_update_block(data, EECONFIG_USER_DATABLOCK, (EECONFIG_USER_DATA_SIZE));
  322. }
  323. /** \brief eeconfig init user data block
  324. *
  325. * FIXME: needs doc
  326. */
  327. __attribute__((weak)) void eeconfig_init_user_datablock(void) {
  328. uint8_t dummy_user[(EECONFIG_USER_DATA_SIZE)] = {0};
  329. eeconfig_update_user_datablock(dummy_user);
  330. }
  331. #endif // (EECONFIG_USER_DATA_SIZE) > 0