eeconfig.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Copyright 2013 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <stddef.h> // offsetof
  18. #include "eeprom.h"
  19. #include "util.h"
  20. #ifndef EECONFIG_MAGIC_NUMBER
  21. # define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEE5 // When changing, decrement this value to avoid future re-init issues
  22. #endif
  23. #define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF
  24. // Dummy struct only used to calculate offsets
  25. typedef struct PACKED {
  26. uint16_t magic;
  27. uint8_t debug;
  28. uint8_t default_layer;
  29. uint16_t keymap;
  30. uint8_t backlight;
  31. uint8_t audio;
  32. uint32_t rgblight;
  33. uint8_t unicode;
  34. uint8_t steno;
  35. uint8_t handedness;
  36. uint32_t keyboard;
  37. uint32_t user;
  38. union { // Mutually exclusive
  39. uint32_t led_matrix;
  40. uint64_t rgb_matrix;
  41. };
  42. uint32_t haptic;
  43. uint8_t rgblight_ext;
  44. uint32_t keymap_hash;
  45. } eeprom_core_t;
  46. /* EEPROM parameter address */
  47. #define EECONFIG_MAGIC (uint16_t *)(offsetof(eeprom_core_t, magic))
  48. #define EECONFIG_DEBUG (uint8_t *)(offsetof(eeprom_core_t, debug))
  49. #define EECONFIG_DEFAULT_LAYER (uint8_t *)(offsetof(eeprom_core_t, default_layer))
  50. #define EECONFIG_KEYMAP (uint16_t *)(offsetof(eeprom_core_t, keymap))
  51. #define EECONFIG_BACKLIGHT (uint8_t *)(offsetof(eeprom_core_t, backlight))
  52. #define EECONFIG_AUDIO (uint8_t *)(offsetof(eeprom_core_t, audio))
  53. #define EECONFIG_RGBLIGHT (uint32_t *)(offsetof(eeprom_core_t, rgblight))
  54. #define EECONFIG_UNICODEMODE (uint8_t *)(offsetof(eeprom_core_t, unicode))
  55. #define EECONFIG_STENOMODE (uint8_t *)(offsetof(eeprom_core_t, steno))
  56. #define EECONFIG_HANDEDNESS (uint8_t *)(offsetof(eeprom_core_t, handedness))
  57. #define EECONFIG_KEYBOARD (uint32_t *)(offsetof(eeprom_core_t, keyboard))
  58. #define EECONFIG_USER (uint32_t *)(offsetof(eeprom_core_t, user))
  59. #define EECONFIG_LED_MATRIX (uint32_t *)(offsetof(eeprom_core_t, led_matrix))
  60. #define EECONFIG_RGB_MATRIX (uint64_t *)(offsetof(eeprom_core_t, rgb_matrix))
  61. #define EECONFIG_HAPTIC (uint32_t *)(offsetof(eeprom_core_t, haptic))
  62. #define EECONFIG_RGBLIGHT_EXTENDED (uint8_t *)(offsetof(eeprom_core_t, rgblight_ext))
  63. #define EECONFIG_KEYMAP_HASH (uint32_t *)(offsetof(eeprom_core_t, keymap_hash))
  64. // Size of EEPROM being used for core data storage
  65. #define EECONFIG_BASE_SIZE ((uint8_t)sizeof(eeprom_core_t))
  66. // Size of EEPROM dedicated to keyboard- and user-specific data
  67. #ifndef EECONFIG_KB_DATA_SIZE
  68. # define EECONFIG_KB_DATA_SIZE 0
  69. #endif
  70. #ifndef EECONFIG_KB_DATA_VERSION
  71. # define EECONFIG_KB_DATA_VERSION (EECONFIG_KB_DATA_SIZE)
  72. #endif
  73. #ifndef EECONFIG_USER_DATA_SIZE
  74. # define EECONFIG_USER_DATA_SIZE 0
  75. #endif
  76. #ifndef EECONFIG_USER_DATA_VERSION
  77. # define EECONFIG_USER_DATA_VERSION (EECONFIG_USER_DATA_SIZE)
  78. #endif
  79. #define EECONFIG_KB_DATABLOCK ((uint8_t *)(EECONFIG_BASE_SIZE))
  80. #define EECONFIG_USER_DATABLOCK ((uint8_t *)((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE)))
  81. // Size of EEPROM being used, other code can refer to this for available EEPROM
  82. #define EECONFIG_SIZE ((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE) + (EECONFIG_USER_DATA_SIZE))
  83. /* debug bit */
  84. #define EECONFIG_DEBUG_ENABLE (1 << 0)
  85. #define EECONFIG_DEBUG_MATRIX (1 << 1)
  86. #define EECONFIG_DEBUG_KEYBOARD (1 << 2)
  87. #define EECONFIG_DEBUG_MOUSE (1 << 3)
  88. /* keyconf bit */
  89. #define EECONFIG_KEYMAP_SWAP_CONTROL_CAPSLOCK (1 << 0)
  90. #define EECONFIG_KEYMAP_CAPSLOCK_TO_CONTROL (1 << 1)
  91. #define EECONFIG_KEYMAP_SWAP_LALT_LGUI (1 << 2)
  92. #define EECONFIG_KEYMAP_SWAP_RALT_RGUI (1 << 3)
  93. #define EECONFIG_KEYMAP_NO_GUI (1 << 4)
  94. #define EECONFIG_KEYMAP_SWAP_GRAVE_ESC (1 << 5)
  95. #define EECONFIG_KEYMAP_SWAP_BACKSLASH_BACKSPACE (1 << 6)
  96. #define EECONFIG_KEYMAP_NKRO (1 << 7)
  97. bool eeconfig_is_enabled(void);
  98. bool eeconfig_is_disabled(void);
  99. void eeconfig_init(void);
  100. void eeconfig_init_quantum(void);
  101. void eeconfig_init_kb(void);
  102. void eeconfig_init_user(void);
  103. void eeconfig_enable(void);
  104. void eeconfig_disable(void);
  105. uint8_t eeconfig_read_debug(void);
  106. void eeconfig_update_debug(uint8_t val);
  107. uint8_t eeconfig_read_default_layer(void);
  108. void eeconfig_update_default_layer(uint8_t val);
  109. uint16_t eeconfig_read_keymap(void);
  110. void eeconfig_update_keymap(uint16_t val);
  111. #ifdef AUDIO_ENABLE
  112. uint8_t eeconfig_read_audio(void);
  113. void eeconfig_update_audio(uint8_t val);
  114. #endif
  115. #if (EECONFIG_KB_DATA_SIZE) == 0
  116. uint32_t eeconfig_read_kb(void);
  117. void eeconfig_update_kb(uint32_t val);
  118. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  119. #if (EECONFIG_USER_DATA_SIZE) == 0
  120. uint32_t eeconfig_read_user(void);
  121. void eeconfig_update_user(uint32_t val);
  122. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  123. #ifdef HAPTIC_ENABLE
  124. uint32_t eeconfig_read_haptic(void);
  125. void eeconfig_update_haptic(uint32_t val);
  126. #endif
  127. bool eeconfig_read_handedness(void);
  128. void eeconfig_update_handedness(bool val);
  129. #if (EECONFIG_KB_DATA_SIZE) > 0
  130. bool eeconfig_is_kb_datablock_valid(void);
  131. void eeconfig_read_kb_datablock(void *data);
  132. void eeconfig_update_kb_datablock(const void *data);
  133. void eeconfig_init_kb_datablock(void);
  134. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  135. #if (EECONFIG_USER_DATA_SIZE) > 0
  136. bool eeconfig_is_user_datablock_valid(void);
  137. void eeconfig_read_user_datablock(void *data);
  138. void eeconfig_update_user_datablock(const void *data);
  139. void eeconfig_init_user_datablock(void);
  140. #endif // (EECONFIG_USER_DATA_SIZE) > 0
  141. // Any "checked" debounce variant used requires implementation of:
  142. // -- bool eeconfig_check_valid_##name(void)
  143. // -- void eeconfig_post_flush_##name(void)
  144. #define EECONFIG_DEBOUNCE_HELPER_CHECKED(name, offset, config) \
  145. static uint8_t dirty_##name = false; \
  146. \
  147. bool eeconfig_check_valid_##name(void); \
  148. void eeconfig_post_flush_##name(void); \
  149. \
  150. static inline void eeconfig_init_##name(void) { \
  151. dirty_##name = true; \
  152. if (eeconfig_check_valid_##name()) { \
  153. eeprom_read_block(&config, offset, sizeof(config)); \
  154. dirty_##name = false; \
  155. } \
  156. } \
  157. static inline void eeconfig_flush_##name(bool force) { \
  158. if (force || dirty_##name) { \
  159. eeprom_update_block(&config, offset, sizeof(config)); \
  160. eeconfig_post_flush_##name(); \
  161. dirty_##name = false; \
  162. } \
  163. } \
  164. static inline void eeconfig_flush_##name##_task(uint16_t timeout) { \
  165. static uint16_t flush_timer = 0; \
  166. if (timer_elapsed(flush_timer) > timeout) { \
  167. eeconfig_flush_##name(false); \
  168. flush_timer = timer_read(); \
  169. } \
  170. } \
  171. static inline void eeconfig_flag_##name(bool v) { \
  172. dirty_##name |= v; \
  173. } \
  174. static inline void eeconfig_write_##name(typeof(config) *conf) { \
  175. if (memcmp(&config, conf, sizeof(config)) != 0) { \
  176. memcpy(&config, conf, sizeof(config)); \
  177. eeconfig_flag_##name(true); \
  178. } \
  179. }
  180. #define EECONFIG_DEBOUNCE_HELPER(name, offset, config) \
  181. EECONFIG_DEBOUNCE_HELPER_CHECKED(name, offset, config) \
  182. \
  183. bool eeconfig_check_valid_##name(void) { \
  184. return true; \
  185. } \
  186. void eeconfig_post_flush_##name(void) {}