eeconfig.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. eeconfig_init_kb_datablock();
  66. #endif
  67. #if (EECONFIG_USER_DATA_SIZE) > 0
  68. eeconfig_init_user_datablock();
  69. #endif
  70. #if defined(VIA_ENABLE)
  71. // Invalidate VIA eeprom config, and then reset.
  72. // Just in case if power is lost mid init, this makes sure that it pets
  73. // properly re-initialized.
  74. via_eeprom_set_valid(false);
  75. eeconfig_init_via();
  76. #endif
  77. eeconfig_init_kb();
  78. }
  79. /** \brief eeconfig initialization
  80. *
  81. * FIXME: needs doc
  82. */
  83. void eeconfig_init(void) {
  84. eeconfig_init_quantum();
  85. }
  86. /** \brief eeconfig enable
  87. *
  88. * FIXME: needs doc
  89. */
  90. void eeconfig_enable(void) {
  91. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
  92. }
  93. /** \brief eeconfig disable
  94. *
  95. * FIXME: needs doc
  96. */
  97. void eeconfig_disable(void) {
  98. #if defined(EEPROM_DRIVER)
  99. eeprom_driver_erase();
  100. #endif
  101. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER_OFF);
  102. }
  103. /** \brief eeconfig is enabled
  104. *
  105. * FIXME: needs doc
  106. */
  107. bool eeconfig_is_enabled(void) {
  108. bool is_eeprom_enabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
  109. #ifdef VIA_ENABLE
  110. if (is_eeprom_enabled) {
  111. is_eeprom_enabled = via_eeprom_is_valid();
  112. }
  113. #endif
  114. return is_eeprom_enabled;
  115. }
  116. /** \brief eeconfig is disabled
  117. *
  118. * FIXME: needs doc
  119. */
  120. bool eeconfig_is_disabled(void) {
  121. bool is_eeprom_disabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER_OFF);
  122. #ifdef VIA_ENABLE
  123. if (!is_eeprom_disabled) {
  124. is_eeprom_disabled = !via_eeprom_is_valid();
  125. }
  126. #endif
  127. return is_eeprom_disabled;
  128. }
  129. /** \brief eeconfig read debug
  130. *
  131. * FIXME: needs doc
  132. */
  133. uint8_t eeconfig_read_debug(void) {
  134. return eeprom_read_byte(EECONFIG_DEBUG);
  135. }
  136. /** \brief eeconfig update debug
  137. *
  138. * FIXME: needs doc
  139. */
  140. void eeconfig_update_debug(uint8_t val) {
  141. eeprom_update_byte(EECONFIG_DEBUG, val);
  142. }
  143. /** \brief eeconfig read default layer
  144. *
  145. * FIXME: needs doc
  146. */
  147. uint8_t eeconfig_read_default_layer(void) {
  148. return eeprom_read_byte(EECONFIG_DEFAULT_LAYER);
  149. }
  150. /** \brief eeconfig update default layer
  151. *
  152. * FIXME: needs doc
  153. */
  154. void eeconfig_update_default_layer(uint8_t val) {
  155. eeprom_update_byte(EECONFIG_DEFAULT_LAYER, val);
  156. }
  157. /** \brief eeconfig read keymap
  158. *
  159. * FIXME: needs doc
  160. */
  161. uint16_t eeconfig_read_keymap(void) {
  162. return eeprom_read_word(EECONFIG_KEYMAP);
  163. }
  164. /** \brief eeconfig update keymap
  165. *
  166. * FIXME: needs doc
  167. */
  168. void eeconfig_update_keymap(uint16_t val) {
  169. eeprom_update_word(EECONFIG_KEYMAP, val);
  170. }
  171. /** \brief eeconfig read audio
  172. *
  173. * FIXME: needs doc
  174. */
  175. uint8_t eeconfig_read_audio(void) {
  176. return eeprom_read_byte(EECONFIG_AUDIO);
  177. }
  178. /** \brief eeconfig update audio
  179. *
  180. * FIXME: needs doc
  181. */
  182. void eeconfig_update_audio(uint8_t val) {
  183. eeprom_update_byte(EECONFIG_AUDIO, val);
  184. }
  185. #if (EECONFIG_KB_DATA_SIZE) == 0
  186. /** \brief eeconfig read kb
  187. *
  188. * FIXME: needs doc
  189. */
  190. uint32_t eeconfig_read_kb(void) {
  191. return eeprom_read_dword(EECONFIG_KEYBOARD);
  192. }
  193. /** \brief eeconfig update kb
  194. *
  195. * FIXME: needs doc
  196. */
  197. void eeconfig_update_kb(uint32_t val) {
  198. eeprom_update_dword(EECONFIG_KEYBOARD, val);
  199. }
  200. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  201. #if (EECONFIG_USER_DATA_SIZE) == 0
  202. /** \brief eeconfig read user
  203. *
  204. * FIXME: needs doc
  205. */
  206. uint32_t eeconfig_read_user(void) {
  207. return eeprom_read_dword(EECONFIG_USER);
  208. }
  209. /** \brief eeconfig update user
  210. *
  211. * FIXME: needs doc
  212. */
  213. void eeconfig_update_user(uint32_t val) {
  214. eeprom_update_dword(EECONFIG_USER, val);
  215. }
  216. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  217. /** \brief eeconfig read haptic
  218. *
  219. * FIXME: needs doc
  220. */
  221. uint32_t eeconfig_read_haptic(void) {
  222. return eeprom_read_dword(EECONFIG_HAPTIC);
  223. }
  224. /** \brief eeconfig update haptic
  225. *
  226. * FIXME: needs doc
  227. */
  228. void eeconfig_update_haptic(uint32_t val) {
  229. eeprom_update_dword(EECONFIG_HAPTIC, val);
  230. }
  231. /** \brief eeconfig read split handedness
  232. *
  233. * FIXME: needs doc
  234. */
  235. bool eeconfig_read_handedness(void) {
  236. return !!eeprom_read_byte(EECONFIG_HANDEDNESS);
  237. }
  238. /** \brief eeconfig update split handedness
  239. *
  240. * FIXME: needs doc
  241. */
  242. void eeconfig_update_handedness(bool val) {
  243. eeprom_update_byte(EECONFIG_HANDEDNESS, !!val);
  244. }
  245. #if (EECONFIG_KB_DATA_SIZE) > 0
  246. /** \brief eeconfig assert keyboard data block version
  247. *
  248. * FIXME: needs doc
  249. */
  250. bool eeconfig_is_kb_datablock_valid(void) {
  251. return eeprom_read_dword(EECONFIG_KEYBOARD) == (EECONFIG_KB_DATA_VERSION);
  252. }
  253. /** \brief eeconfig read keyboard data block
  254. *
  255. * FIXME: needs doc
  256. */
  257. void eeconfig_read_kb_datablock(void *data) {
  258. if (eeconfig_is_kb_datablock_valid()) {
  259. eeprom_read_block(data, EECONFIG_KB_DATABLOCK, (EECONFIG_KB_DATA_SIZE));
  260. } else {
  261. memset(data, 0, (EECONFIG_KB_DATA_SIZE));
  262. }
  263. }
  264. /** \brief eeconfig update keyboard data block
  265. *
  266. * FIXME: needs doc
  267. */
  268. void eeconfig_update_kb_datablock(const void *data) {
  269. eeprom_update_dword(EECONFIG_KEYBOARD, (EECONFIG_KB_DATA_VERSION));
  270. eeprom_update_block(data, EECONFIG_KB_DATABLOCK, (EECONFIG_KB_DATA_SIZE));
  271. }
  272. /** \brief eeconfig init keyboard data block
  273. *
  274. * FIXME: needs doc
  275. */
  276. __attribute__((weak)) void eeconfig_init_kb_datablock(void) {
  277. uint8_t dummy_kb[(EECONFIG_KB_DATA_SIZE)] = {0};
  278. eeconfig_update_kb_datablock(dummy_kb);
  279. }
  280. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  281. #if (EECONFIG_USER_DATA_SIZE) > 0
  282. /** \brief eeconfig assert user data block version
  283. *
  284. * FIXME: needs doc
  285. */
  286. bool eeconfig_is_user_datablock_valid(void) {
  287. return eeprom_read_dword(EECONFIG_USER) == (EECONFIG_USER_DATA_VERSION);
  288. }
  289. /** \brief eeconfig read user data block
  290. *
  291. * FIXME: needs doc
  292. */
  293. void eeconfig_read_user_datablock(void *data) {
  294. if (eeconfig_is_user_datablock_valid()) {
  295. eeprom_read_block(data, EECONFIG_USER_DATABLOCK, (EECONFIG_USER_DATA_SIZE));
  296. } else {
  297. memset(data, 0, (EECONFIG_USER_DATA_SIZE));
  298. }
  299. }
  300. /** \brief eeconfig update user data block
  301. *
  302. * FIXME: needs doc
  303. */
  304. void eeconfig_update_user_datablock(const void *data) {
  305. eeprom_update_dword(EECONFIG_USER, (EECONFIG_USER_DATA_VERSION));
  306. eeprom_update_block(data, EECONFIG_USER_DATABLOCK, (EECONFIG_USER_DATA_SIZE));
  307. }
  308. /** \brief eeconfig init user data block
  309. *
  310. * FIXME: needs doc
  311. */
  312. __attribute__((weak)) void eeconfig_init_user_datablock(void) {
  313. uint8_t dummy_user[(EECONFIG_USER_DATA_SIZE)] = {0};
  314. eeconfig_update_user_datablock(dummy_user);
  315. }
  316. #endif // (EECONFIG_USER_DATA_SIZE) > 0