eeconfig.c 6.7 KB

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