eeconfig.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "eeprom.h"
  4. #include "eeconfig.h"
  5. #include "action_layer.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. /** \brief eeconfig enable
  18. *
  19. * FIXME: needs doc
  20. */
  21. __attribute__((weak)) void eeconfig_init_user(void) {
  22. // Reset user EEPROM value to blank, rather than to a set value
  23. eeconfig_update_user(0);
  24. }
  25. __attribute__((weak)) void eeconfig_init_kb(void) {
  26. // Reset Keyboard EEPROM value to blank, rather than to a set value
  27. eeconfig_update_kb(0);
  28. eeconfig_init_user();
  29. }
  30. /*
  31. * FIXME: needs doc
  32. */
  33. void eeconfig_init_quantum(void) {
  34. #if defined(EEPROM_DRIVER)
  35. eeprom_driver_erase();
  36. #endif
  37. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
  38. eeprom_update_byte(EECONFIG_DEBUG, 0);
  39. eeprom_update_byte(EECONFIG_DEFAULT_LAYER, 0);
  40. default_layer_state = 0;
  41. // Enable oneshot and autocorrect by default: 0b0001 0100 0000 0000
  42. eeprom_update_word(EECONFIG_KEYMAP, 0x1400);
  43. eeprom_update_byte(EECONFIG_BACKLIGHT, 0);
  44. eeprom_update_byte(EECONFIG_AUDIO, 0xFF); // On by default
  45. eeprom_update_dword(EECONFIG_RGBLIGHT, 0);
  46. eeprom_update_byte(EECONFIG_STENOMODE, 0);
  47. eeprom_update_dword(EECONFIG_HAPTIC, 0);
  48. eeprom_update_byte(EECONFIG_VELOCIKEY, 0);
  49. eeprom_update_dword(EECONFIG_RGB_MATRIX, 0);
  50. eeprom_update_word(EECONFIG_RGB_MATRIX_EXTENDED, 0);
  51. #if defined(HAPTIC_ENABLE)
  52. haptic_reset();
  53. #else
  54. // this is used in case haptic is disabled, but we still want sane defaults
  55. // in the haptic configuration eeprom. All zero will trigger a haptic_reset
  56. // when a haptic-enabled firmware is loaded onto the keyboard.
  57. eeprom_update_dword(EECONFIG_HAPTIC, 0);
  58. #endif
  59. #if defined(VIA_ENABLE)
  60. // Invalidate VIA eeprom config, and then reset.
  61. // Just in case if power is lost mid init, this makes sure that it pets
  62. // properly re-initialized.
  63. via_eeprom_set_valid(false);
  64. eeconfig_init_via();
  65. #endif
  66. eeconfig_init_kb();
  67. }
  68. /** \brief eeconfig initialization
  69. *
  70. * FIXME: needs doc
  71. */
  72. void eeconfig_init(void) {
  73. eeconfig_init_quantum();
  74. }
  75. /** \brief eeconfig enable
  76. *
  77. * FIXME: needs doc
  78. */
  79. void eeconfig_enable(void) {
  80. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
  81. }
  82. /** \brief eeconfig disable
  83. *
  84. * FIXME: needs doc
  85. */
  86. void eeconfig_disable(void) {
  87. #if defined(EEPROM_DRIVER)
  88. eeprom_driver_erase();
  89. #endif
  90. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER_OFF);
  91. }
  92. /** \brief eeconfig is enabled
  93. *
  94. * FIXME: needs doc
  95. */
  96. bool eeconfig_is_enabled(void) {
  97. bool is_eeprom_enabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
  98. #ifdef VIA_ENABLE
  99. if (is_eeprom_enabled) {
  100. is_eeprom_enabled = via_eeprom_is_valid();
  101. }
  102. #endif
  103. return is_eeprom_enabled;
  104. }
  105. /** \brief eeconfig is disabled
  106. *
  107. * FIXME: needs doc
  108. */
  109. bool eeconfig_is_disabled(void) {
  110. bool is_eeprom_disabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER_OFF);
  111. #ifdef VIA_ENABLE
  112. if (!is_eeprom_disabled) {
  113. is_eeprom_disabled = !via_eeprom_is_valid();
  114. }
  115. #endif
  116. return is_eeprom_disabled;
  117. }
  118. /** \brief eeconfig read debug
  119. *
  120. * FIXME: needs doc
  121. */
  122. uint8_t eeconfig_read_debug(void) {
  123. return eeprom_read_byte(EECONFIG_DEBUG);
  124. }
  125. /** \brief eeconfig update debug
  126. *
  127. * FIXME: needs doc
  128. */
  129. void eeconfig_update_debug(uint8_t val) {
  130. eeprom_update_byte(EECONFIG_DEBUG, val);
  131. }
  132. /** \brief eeconfig read default layer
  133. *
  134. * FIXME: needs doc
  135. */
  136. uint8_t eeconfig_read_default_layer(void) {
  137. return eeprom_read_byte(EECONFIG_DEFAULT_LAYER);
  138. }
  139. /** \brief eeconfig update default layer
  140. *
  141. * FIXME: needs doc
  142. */
  143. void eeconfig_update_default_layer(uint8_t val) {
  144. eeprom_update_byte(EECONFIG_DEFAULT_LAYER, val);
  145. }
  146. /** \brief eeconfig read keymap
  147. *
  148. * FIXME: needs doc
  149. */
  150. uint16_t eeconfig_read_keymap(void) {
  151. return eeprom_read_word(EECONFIG_KEYMAP);
  152. }
  153. /** \brief eeconfig update keymap
  154. *
  155. * FIXME: needs doc
  156. */
  157. void eeconfig_update_keymap(uint16_t val) {
  158. eeprom_update_word(EECONFIG_KEYMAP, val);
  159. }
  160. /** \brief eeconfig read audio
  161. *
  162. * FIXME: needs doc
  163. */
  164. uint8_t eeconfig_read_audio(void) {
  165. return eeprom_read_byte(EECONFIG_AUDIO);
  166. }
  167. /** \brief eeconfig update audio
  168. *
  169. * FIXME: needs doc
  170. */
  171. void eeconfig_update_audio(uint8_t val) {
  172. eeprom_update_byte(EECONFIG_AUDIO, val);
  173. }
  174. /** \brief eeconfig read kb
  175. *
  176. * FIXME: needs doc
  177. */
  178. uint32_t eeconfig_read_kb(void) {
  179. return eeprom_read_dword(EECONFIG_KEYBOARD);
  180. }
  181. /** \brief eeconfig update kb
  182. *
  183. * FIXME: needs doc
  184. */
  185. void eeconfig_update_kb(uint32_t val) {
  186. eeprom_update_dword(EECONFIG_KEYBOARD, val);
  187. }
  188. /** \brief eeconfig read user
  189. *
  190. * FIXME: needs doc
  191. */
  192. uint32_t eeconfig_read_user(void) {
  193. return eeprom_read_dword(EECONFIG_USER);
  194. }
  195. /** \brief eeconfig update user
  196. *
  197. * FIXME: needs doc
  198. */
  199. void eeconfig_update_user(uint32_t val) {
  200. eeprom_update_dword(EECONFIG_USER, val);
  201. }
  202. /** \brief eeconfig read haptic
  203. *
  204. * FIXME: needs doc
  205. */
  206. uint32_t eeconfig_read_haptic(void) {
  207. return eeprom_read_dword(EECONFIG_HAPTIC);
  208. }
  209. /** \brief eeconfig update haptic
  210. *
  211. * FIXME: needs doc
  212. */
  213. void eeconfig_update_haptic(uint32_t val) {
  214. eeprom_update_dword(EECONFIG_HAPTIC, val);
  215. }
  216. /** \brief eeconfig read split handedness
  217. *
  218. * FIXME: needs doc
  219. */
  220. bool eeconfig_read_handedness(void) {
  221. return !!eeprom_read_byte(EECONFIG_HANDEDNESS);
  222. }
  223. /** \brief eeconfig update split handedness
  224. *
  225. * FIXME: needs doc
  226. */
  227. void eeconfig_update_handedness(bool val) {
  228. eeprom_update_byte(EECONFIG_HANDEDNESS, !!val);
  229. }