eeconfig.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // 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) {
  83. eeconfig_init_quantum();
  84. }
  85. /** \brief eeconfig enable
  86. *
  87. * FIXME: needs doc
  88. */
  89. void eeconfig_enable(void) {
  90. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
  91. }
  92. /** \brief eeconfig disable
  93. *
  94. * FIXME: needs doc
  95. */
  96. void eeconfig_disable(void) {
  97. #if defined(EEPROM_DRIVER)
  98. eeprom_driver_erase();
  99. #endif
  100. eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER_OFF);
  101. }
  102. /** \brief eeconfig is enabled
  103. *
  104. * FIXME: needs doc
  105. */
  106. bool eeconfig_is_enabled(void) {
  107. bool is_eeprom_enabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER);
  108. #ifdef VIA_ENABLE
  109. if (is_eeprom_enabled) {
  110. is_eeprom_enabled = via_eeprom_is_valid();
  111. }
  112. #endif
  113. return is_eeprom_enabled;
  114. }
  115. /** \brief eeconfig is disabled
  116. *
  117. * FIXME: needs doc
  118. */
  119. bool eeconfig_is_disabled(void) {
  120. bool is_eeprom_disabled = (eeprom_read_word(EECONFIG_MAGIC) == EECONFIG_MAGIC_NUMBER_OFF);
  121. #ifdef VIA_ENABLE
  122. if (!is_eeprom_disabled) {
  123. is_eeprom_disabled = !via_eeprom_is_valid();
  124. }
  125. #endif
  126. return is_eeprom_disabled;
  127. }
  128. /** \brief eeconfig read debug
  129. *
  130. * FIXME: needs doc
  131. */
  132. uint8_t eeconfig_read_debug(void) {
  133. return eeprom_read_byte(EECONFIG_DEBUG);
  134. }
  135. /** \brief eeconfig update debug
  136. *
  137. * FIXME: needs doc
  138. */
  139. void eeconfig_update_debug(uint8_t val) {
  140. eeprom_update_byte(EECONFIG_DEBUG, val);
  141. }
  142. /** \brief eeconfig read default layer
  143. *
  144. * FIXME: needs doc
  145. */
  146. uint8_t eeconfig_read_default_layer(void) {
  147. return eeprom_read_byte(EECONFIG_DEFAULT_LAYER);
  148. }
  149. /** \brief eeconfig update default layer
  150. *
  151. * FIXME: needs doc
  152. */
  153. void eeconfig_update_default_layer(uint8_t val) {
  154. eeprom_update_byte(EECONFIG_DEFAULT_LAYER, val);
  155. }
  156. /** \brief eeconfig read keymap
  157. *
  158. * FIXME: needs doc
  159. */
  160. uint16_t eeconfig_read_keymap(void) {
  161. return (eeprom_read_byte(EECONFIG_KEYMAP_LOWER_BYTE) | (eeprom_read_byte(EECONFIG_KEYMAP_UPPER_BYTE) << 8));
  162. }
  163. /** \brief eeconfig update keymap
  164. *
  165. * FIXME: needs doc
  166. */
  167. void eeconfig_update_keymap(uint16_t val) {
  168. eeprom_update_byte(EECONFIG_KEYMAP_LOWER_BYTE, val & 0xFF);
  169. eeprom_update_byte(EECONFIG_KEYMAP_UPPER_BYTE, (val >> 8) & 0xFF);
  170. }
  171. /** \brief eeconfig read audio
  172. *
  173. * FIXME: needs doc
  174. */
  175. uint8_t eeconfig_read_audio(void) {
  176. return eeprom_read_byte(EECONFIG_AUDIO);
  177. }
  178. /** \brief eeconfig update audio
  179. *
  180. * FIXME: needs doc
  181. */
  182. void eeconfig_update_audio(uint8_t val) {
  183. eeprom_update_byte(EECONFIG_AUDIO, val);
  184. }
  185. /** \brief eeconfig read kb
  186. *
  187. * FIXME: needs doc
  188. */
  189. uint32_t eeconfig_read_kb(void) {
  190. return eeprom_read_dword(EECONFIG_KEYBOARD);
  191. }
  192. /** \brief eeconfig update kb
  193. *
  194. * FIXME: needs doc
  195. */
  196. void eeconfig_update_kb(uint32_t val) {
  197. eeprom_update_dword(EECONFIG_KEYBOARD, val);
  198. }
  199. /** \brief eeconfig read user
  200. *
  201. * FIXME: needs doc
  202. */
  203. uint32_t eeconfig_read_user(void) {
  204. return eeprom_read_dword(EECONFIG_USER);
  205. }
  206. /** \brief eeconfig update user
  207. *
  208. * FIXME: needs doc
  209. */
  210. void eeconfig_update_user(uint32_t val) {
  211. eeprom_update_dword(EECONFIG_USER, val);
  212. }
  213. /** \brief eeconfig read haptic
  214. *
  215. * FIXME: needs doc
  216. */
  217. uint32_t eeconfig_read_haptic(void) {
  218. return eeprom_read_dword(EECONFIG_HAPTIC);
  219. }
  220. /** \brief eeconfig update haptic
  221. *
  222. * FIXME: needs doc
  223. */
  224. void eeconfig_update_haptic(uint32_t val) {
  225. eeprom_update_dword(EECONFIG_HAPTIC, val);
  226. }
  227. /** \brief eeconfig read split handedness
  228. *
  229. * FIXME: needs doc
  230. */
  231. bool eeconfig_read_handedness(void) {
  232. return !!eeprom_read_byte(EECONFIG_HANDEDNESS);
  233. }
  234. /** \brief eeconfig update split handedness
  235. *
  236. * FIXME: needs doc
  237. */
  238. void eeconfig_update_handedness(bool val) {
  239. eeprom_update_byte(EECONFIG_HANDEDNESS, !!val);
  240. }