eeconfig.c 5.9 KB

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