eeconfig.c 6.0 KB

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