eeconfig.c 8.1 KB

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