eeconfig.c 6.4 KB

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