eeconfig.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, 0);
  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. // TODO: Remove once ARM has a way to configure EECONFIG_HANDEDNESS
  53. // within the emulated eeprom via dfu-util or another tool
  54. #if defined INIT_EE_HANDS_LEFT
  55. # pragma message "Faking EE_HANDS for left hand"
  56. eeprom_update_byte(EECONFIG_HANDEDNESS, 1);
  57. #elif defined INIT_EE_HANDS_RIGHT
  58. # pragma message "Faking EE_HANDS for right hand"
  59. eeprom_update_byte(EECONFIG_HANDEDNESS, 0);
  60. #endif
  61. #if defined(HAPTIC_ENABLE)
  62. haptic_reset();
  63. #else
  64. // this is used in case haptic is disabled, but we still want sane defaults
  65. // in the haptic configuration eeprom. All zero will trigger a haptic_reset
  66. // when a haptic-enabled firmware is loaded onto the keyboard.
  67. eeprom_update_dword(EECONFIG_HAPTIC, 0);
  68. #endif
  69. #if defined(VIA_ENABLE)
  70. // Invalidate VIA eeprom config, and then reset.
  71. // Just in case if power is lost mid init, this makes sure that it pets
  72. // properly re-initialized.
  73. via_eeprom_set_valid(false);
  74. eeconfig_init_via();
  75. #endif
  76. eeconfig_init_kb();
  77. }
  78. /** \brief eeconfig initialization
  79. *
  80. * FIXME: needs doc
  81. */
  82. void eeconfig_init(void) { eeconfig_init_quantum(); }
  83. /** \brief eeconfig enable
  84. *
  85. * FIXME: needs doc
  86. */
  87. void eeconfig_enable(void) { eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); }
  88. /** \brief eeconfig disable
  89. *
  90. * FIXME: needs doc
  91. */
  92. void eeconfig_disable(void) {
  93. #if defined(EEPROM_DRIVER)
  94. eeprom_driver_erase();
  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) { return eeprom_read_byte(EECONFIG_DEBUG); }
  129. /** \brief eeconfig update debug
  130. *
  131. * FIXME: needs doc
  132. */
  133. void eeconfig_update_debug(uint8_t val) { eeprom_update_byte(EECONFIG_DEBUG, val); }
  134. /** \brief eeconfig read default layer
  135. *
  136. * FIXME: needs doc
  137. */
  138. uint8_t eeconfig_read_default_layer(void) { return eeprom_read_byte(EECONFIG_DEFAULT_LAYER); }
  139. /** \brief eeconfig update default layer
  140. *
  141. * FIXME: needs doc
  142. */
  143. void eeconfig_update_default_layer(uint8_t val) { eeprom_update_byte(EECONFIG_DEFAULT_LAYER, val); }
  144. /** \brief eeconfig read keymap
  145. *
  146. * FIXME: needs doc
  147. */
  148. uint16_t eeconfig_read_keymap(void) { return (eeprom_read_byte(EECONFIG_KEYMAP_LOWER_BYTE) | (eeprom_read_byte(EECONFIG_KEYMAP_UPPER_BYTE) << 8)); }
  149. /** \brief eeconfig update keymap
  150. *
  151. * FIXME: needs doc
  152. */
  153. void eeconfig_update_keymap(uint16_t val) {
  154. eeprom_update_byte(EECONFIG_KEYMAP_LOWER_BYTE, val & 0xFF);
  155. eeprom_update_byte(EECONFIG_KEYMAP_UPPER_BYTE, (val >> 8) & 0xFF);
  156. }
  157. /** \brief eeconfig read audio
  158. *
  159. * FIXME: needs doc
  160. */
  161. uint8_t eeconfig_read_audio(void) { return eeprom_read_byte(EECONFIG_AUDIO); }
  162. /** \brief eeconfig update audio
  163. *
  164. * FIXME: needs doc
  165. */
  166. void eeconfig_update_audio(uint8_t val) { eeprom_update_byte(EECONFIG_AUDIO, val); }
  167. /** \brief eeconfig read kb
  168. *
  169. * FIXME: needs doc
  170. */
  171. uint32_t eeconfig_read_kb(void) { return eeprom_read_dword(EECONFIG_KEYBOARD); }
  172. /** \brief eeconfig update kb
  173. *
  174. * FIXME: needs doc
  175. */
  176. void eeconfig_update_kb(uint32_t val) { eeprom_update_dword(EECONFIG_KEYBOARD, val); }
  177. /** \brief eeconfig read user
  178. *
  179. * FIXME: needs doc
  180. */
  181. uint32_t eeconfig_read_user(void) { return eeprom_read_dword(EECONFIG_USER); }
  182. /** \brief eeconfig update user
  183. *
  184. * FIXME: needs doc
  185. */
  186. void eeconfig_update_user(uint32_t val) { eeprom_update_dword(EECONFIG_USER, val); }
  187. /** \brief eeconfig read haptic
  188. *
  189. * FIXME: needs doc
  190. */
  191. uint32_t eeconfig_read_haptic(void) { return eeprom_read_dword(EECONFIG_HAPTIC); }
  192. /** \brief eeconfig update haptic
  193. *
  194. * FIXME: needs doc
  195. */
  196. void eeconfig_update_haptic(uint32_t val) { eeprom_update_dword(EECONFIG_HAPTIC, val); }
  197. /** \brief eeconfig read split handedness
  198. *
  199. * FIXME: needs doc
  200. */
  201. bool eeconfig_read_handedness(void) { return !!eeprom_read_byte(EECONFIG_HANDEDNESS); }
  202. /** \brief eeconfig update split handedness
  203. *
  204. * FIXME: needs doc
  205. */
  206. void eeconfig_update_handedness(bool val) { eeprom_update_byte(EECONFIG_HANDEDNESS, !!val); }