eeconfig.c 8.6 KB

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