eeconfig.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #endif
  18. /** \brief eeconfig enable
  19. *
  20. * FIXME: needs doc
  21. */
  22. __attribute__((weak)) void eeconfig_init_user(void) {
  23. #if (EECONFIG_USER_DATA_SIZE) == 0
  24. // Reset user EEPROM value to blank, rather than to a set value
  25. eeconfig_update_user(0);
  26. #endif
  27. }
  28. __attribute__((weak)) void eeconfig_init_kb(void) {
  29. #if (EECONFIG_KB_DATA_SIZE) == 0
  30. // Reset Keyboard EEPROM value to blank, rather than to a set value
  31. eeconfig_update_kb(0);
  32. #endif
  33. eeconfig_init_user();
  34. }
  35. /*
  36. * FIXME: needs doc
  37. */
  38. void eeconfig_init_quantum(void) {
  39. #if defined(EEPROM_DRIVER)
  40. eeprom_driver_erase();
  41. #endif
  42. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
  43. eeprom_update_byte(EECONFIG_DEBUG, 0);
  44. eeprom_update_byte(EECONFIG_DEFAULT_LAYER, 0);
  45. default_layer_state = 0;
  46. // Enable oneshot and autocorrect by default: 0b0001 0100 0000 0000
  47. eeprom_update_word(EECONFIG_KEYMAP, 0x1400);
  48. eeprom_update_byte(EECONFIG_BACKLIGHT, 0);
  49. eeprom_update_byte(EECONFIG_AUDIO, 0xFF); // On by default
  50. eeprom_update_dword(EECONFIG_RGBLIGHT, 0);
  51. eeprom_update_byte(EECONFIG_STENOMODE, 0);
  52. eeprom_update_dword(EECONFIG_HAPTIC, 0);
  53. eeprom_update_byte(EECONFIG_VELOCIKEY, 0);
  54. eeprom_update_dword(EECONFIG_RGB_MATRIX, 0);
  55. eeprom_update_word(EECONFIG_RGB_MATRIX_EXTENDED, 0);
  56. #if defined(HAPTIC_ENABLE)
  57. haptic_reset();
  58. #else
  59. // this is used in case haptic is disabled, but we still want sane defaults
  60. // in the haptic configuration eeprom. All zero will trigger a haptic_reset
  61. // when a haptic-enabled firmware is loaded onto the keyboard.
  62. eeprom_update_dword(EECONFIG_HAPTIC, 0);
  63. #endif
  64. #if (EECONFIG_KB_DATA_SIZE) > 0
  65. eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION));
  66. uint8_t dummy_kb[(EECONFIG_KB_DATA_SIZE)] = {0};
  67. eeprom_update_block(EECONFIG_KB_DATABLOCK, dummy_kb, (EECONFIG_KB_DATA_SIZE));
  68. #endif
  69. #if (EECONFIG_USER_DATA_SIZE) > 0
  70. eeprom_update_dword(EECONFIG_USER, (EECONFIG_USER_DATA_VERSION));
  71. uint8_t dummy_user[(EECONFIG_USER_DATA_SIZE)] = {0};
  72. eeprom_update_block(EECONFIG_USER_DATABLOCK, dummy_user, (EECONFIG_USER_DATA_SIZE));
  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. #endif
  81. eeconfig_init_kb();
  82. }
  83. /** \brief eeconfig initialization
  84. *
  85. * FIXME: needs doc
  86. */
  87. void eeconfig_init(void) {
  88. eeconfig_init_quantum();
  89. }
  90. /** \brief eeconfig enable
  91. *
  92. * FIXME: needs doc
  93. */
  94. void eeconfig_enable(void) {
  95. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
  96. }
  97. /** \brief eeconfig disable
  98. *
  99. * FIXME: needs doc
  100. */
  101. void eeconfig_disable(void) {
  102. #if defined(EEPROM_DRIVER)
  103. eeprom_driver_erase();
  104. #endif
  105. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER_OFF);
  106. }
  107. /** \brief eeconfig is enabled
  108. *
  109. * FIXME: needs doc
  110. */
  111. bool eeconfig_is_enabled(void) {
  112. bool is_eeprom_enabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
  113. #ifdef VIA_ENABLE
  114. if (is_eeprom_enabled) {
  115. is_eeprom_enabled = via_eeprom_is_valid();
  116. }
  117. #endif
  118. return is_eeprom_enabled;
  119. }
  120. /** \brief eeconfig is disabled
  121. *
  122. * FIXME: needs doc
  123. */
  124. bool eeconfig_is_disabled(void) {
  125. bool is_eeprom_disabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER_OFF);
  126. #ifdef VIA_ENABLE
  127. if (!is_eeprom_disabled) {
  128. is_eeprom_disabled = !via_eeprom_is_valid();
  129. }
  130. #endif
  131. return is_eeprom_disabled;
  132. }
  133. /** \brief eeconfig read debug
  134. *
  135. * FIXME: needs doc
  136. */
  137. uint8_t eeconfig_read_debug(void) {
  138. return eeprom_read_byte(EECONFIG_DEBUG);
  139. }
  140. /** \brief eeconfig update debug
  141. *
  142. * FIXME: needs doc
  143. */
  144. void eeconfig_update_debug(uint8_t val) {
  145. eeprom_update_byte(EECONFIG_DEBUG, val);
  146. }
  147. /** \brief eeconfig read default layer
  148. *
  149. * FIXME: needs doc
  150. */
  151. uint8_t eeconfig_read_default_layer(void) {
  152. return eeprom_read_byte(EECONFIG_DEFAULT_LAYER);
  153. }
  154. /** \brief eeconfig update default layer
  155. *
  156. * FIXME: needs doc
  157. */
  158. void eeconfig_update_default_layer(uint8_t val) {
  159. eeprom_update_byte(EECONFIG_DEFAULT_LAYER, val);
  160. }
  161. /** \brief eeconfig read keymap
  162. *
  163. * FIXME: needs doc
  164. */
  165. uint16_t eeconfig_read_keymap(void) {
  166. return eeprom_read_word(EECONFIG_KEYMAP);
  167. }
  168. /** \brief eeconfig update keymap
  169. *
  170. * FIXME: needs doc
  171. */
  172. void eeconfig_update_keymap(uint16_t val) {
  173. eeprom_update_word(EECONFIG_KEYMAP, val);
  174. }
  175. /** \brief eeconfig read audio
  176. *
  177. * FIXME: needs doc
  178. */
  179. uint8_t eeconfig_read_audio(void) {
  180. return eeprom_read_byte(EECONFIG_AUDIO);
  181. }
  182. /** \brief eeconfig update audio
  183. *
  184. * FIXME: needs doc
  185. */
  186. void eeconfig_update_audio(uint8_t val) {
  187. eeprom_update_byte(EECONFIG_AUDIO, val);
  188. }
  189. #if (EECONFIG_KB_DATA_SIZE) == 0
  190. /** \brief eeconfig read kb
  191. *
  192. * FIXME: needs doc
  193. */
  194. uint32_t eeconfig_read_kb(void) {
  195. return eeprom_read_dword(EECONFIG_KEYBOARD);
  196. }
  197. /** \brief eeconfig update kb
  198. *
  199. * FIXME: needs doc
  200. */
  201. void eeconfig_update_kb(uint32_t val) {
  202. eeprom_update_dword(EECONFIG_KEYBOARD, val);
  203. }
  204. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  205. #if (EECONFIG_USER_DATA_SIZE) == 0
  206. /** \brief eeconfig read user
  207. *
  208. * FIXME: needs doc
  209. */
  210. uint32_t eeconfig_read_user(void) {
  211. return eeprom_read_dword(EECONFIG_USER);
  212. }
  213. /** \brief eeconfig update user
  214. *
  215. * FIXME: needs doc
  216. */
  217. void eeconfig_update_user(uint32_t val) {
  218. eeprom_update_dword(EECONFIG_USER, val);
  219. }
  220. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  221. /** \brief eeconfig read haptic
  222. *
  223. * FIXME: needs doc
  224. */
  225. uint32_t eeconfig_read_haptic(void) {
  226. return eeprom_read_dword(EECONFIG_HAPTIC);
  227. }
  228. /** \brief eeconfig update haptic
  229. *
  230. * FIXME: needs doc
  231. */
  232. void eeconfig_update_haptic(uint32_t val) {
  233. eeprom_update_dword(EECONFIG_HAPTIC, val);
  234. }
  235. /** \brief eeconfig read split handedness
  236. *
  237. * FIXME: needs doc
  238. */
  239. bool eeconfig_read_handedness(void) {
  240. return !!eeprom_read_byte(EECONFIG_HANDEDNESS);
  241. }
  242. /** \brief eeconfig update split handedness
  243. *
  244. * FIXME: needs doc
  245. */
  246. void eeconfig_update_handedness(bool val) {
  247. eeprom_update_byte(EECONFIG_HANDEDNESS, !!val);
  248. }
  249. #if (EECONFIG_KB_DATA_SIZE) > 0
  250. /** \brief eeconfig read keyboard data block
  251. *
  252. * FIXME: needs doc
  253. */
  254. void eeconfig_read_kb_datablock(void *data) {
  255. if (eeprom_read_dword(EECONFIG_KEYBOARD) == (EECONFIG_KB_DATA_VERSION)) {
  256. eeprom_read_block(data, EECONFIG_KB_DATABLOCK, (EECONFIG_KB_DATA_SIZE));
  257. } else {
  258. memset(data, 0, (EECONFIG_KB_DATA_SIZE));
  259. }
  260. }
  261. /** \brief eeconfig update keyboard data block
  262. *
  263. * FIXME: needs doc
  264. */
  265. void eeconfig_update_kb_datablock(const void *data) {
  266. eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION));
  267. eeprom_update_block(data, EECONFIG_KB_DATABLOCK, (EECONFIG_KB_DATA_SIZE));
  268. }
  269. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  270. #if (EECONFIG_USER_DATA_SIZE) > 0
  271. /** \brief eeconfig read user data block
  272. *
  273. * FIXME: needs doc
  274. */
  275. void eeconfig_read_user_datablock(void *data) {
  276. if (eeprom_read_dword(EECONFIG_USER) == (EECONFIG_USER_DATA_VERSION)) {
  277. eeprom_read_block(data, EECONFIG_USER_DATABLOCK, (EECONFIG_USER_DATA_SIZE));
  278. } else {
  279. memset(data, 0, (EECONFIG_USER_DATA_SIZE));
  280. }
  281. }
  282. /** \brief eeconfig update user data block
  283. *
  284. * FIXME: needs doc
  285. */
  286. void eeconfig_update_user_datablock(const void *data) {
  287. eeprom_update_dword(EECONFIG_USER, (EECONFIG_USER_DATA_VERSION));
  288. eeprom_update_block(data, EECONFIG_USER_DATABLOCK, (EECONFIG_USER_DATA_SIZE));
  289. }
  290. #endif // (EECONFIG_USER_DATA_SIZE) > 0