eeconfig.c 5.8 KB

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