eeconfig.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. } eeprom_core_t;
  45. /* EEPROM parameter address */
  46. #define EECONFIG_MAGIC (uint16_t *)(offsetof(eeprom_core_t, magic))
  47. #define EECONFIG_DEBUG (uint8_t *)(offsetof(eeprom_core_t, debug))
  48. #define EECONFIG_DEFAULT_LAYER (uint8_t *)(offsetof(eeprom_core_t, default_layer))
  49. #define EECONFIG_KEYMAP (uint16_t *)(offsetof(eeprom_core_t, keymap))
  50. #define EECONFIG_BACKLIGHT (uint8_t *)(offsetof(eeprom_core_t, backlight))
  51. #define EECONFIG_AUDIO (uint8_t *)(offsetof(eeprom_core_t, audio))
  52. #define EECONFIG_RGBLIGHT (uint32_t *)(offsetof(eeprom_core_t, rgblight))
  53. #define EECONFIG_UNICODEMODE (uint8_t *)(offsetof(eeprom_core_t, unicode))
  54. #define EECONFIG_STENOMODE (uint8_t *)(offsetof(eeprom_core_t, steno))
  55. #define EECONFIG_HANDEDNESS (uint8_t *)(offsetof(eeprom_core_t, handedness))
  56. #define EECONFIG_KEYBOARD (uint32_t *)(offsetof(eeprom_core_t, keyboard))
  57. #define EECONFIG_USER (uint32_t *)(offsetof(eeprom_core_t, user))
  58. #define EECONFIG_LED_MATRIX (uint32_t *)(offsetof(eeprom_core_t, led_matrix))
  59. #define EECONFIG_RGB_MATRIX (uint64_t *)(offsetof(eeprom_core_t, rgb_matrix))
  60. #define EECONFIG_HAPTIC (uint32_t *)(offsetof(eeprom_core_t, haptic))
  61. #define EECONFIG_RGBLIGHT_EXTENDED (uint8_t *)(offsetof(eeprom_core_t, rgblight_ext))
  62. // Size of EEPROM being used for core data storage
  63. #define EECONFIG_BASE_SIZE ((uint8_t)sizeof(eeprom_core_t))
  64. // Size of EEPROM dedicated to keyboard- and user-specific data
  65. #ifndef EECONFIG_KB_DATA_SIZE
  66. # define EECONFIG_KB_DATA_SIZE 0
  67. #endif
  68. #ifndef EECONFIG_KB_DATA_VERSION
  69. # define EECONFIG_KB_DATA_VERSION (EECONFIG_KB_DATA_SIZE)
  70. #endif
  71. #ifndef EECONFIG_USER_DATA_SIZE
  72. # define EECONFIG_USER_DATA_SIZE 0
  73. #endif
  74. #ifndef EECONFIG_USER_DATA_VERSION
  75. # define EECONFIG_USER_DATA_VERSION (EECONFIG_USER_DATA_SIZE)
  76. #endif
  77. #define EECONFIG_KB_DATABLOCK ((uint8_t *)(EECONFIG_BASE_SIZE))
  78. #define EECONFIG_USER_DATABLOCK ((uint8_t *)((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE)))
  79. // Size of EEPROM being used, other code can refer to this for available EEPROM
  80. #define EECONFIG_SIZE ((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE) + (EECONFIG_USER_DATA_SIZE))
  81. /* debug bit */
  82. #define EECONFIG_DEBUG_ENABLE (1 << 0)
  83. #define EECONFIG_DEBUG_MATRIX (1 << 1)
  84. #define EECONFIG_DEBUG_KEYBOARD (1 << 2)
  85. #define EECONFIG_DEBUG_MOUSE (1 << 3)
  86. /* keyconf bit */
  87. #define EECONFIG_KEYMAP_SWAP_CONTROL_CAPSLOCK (1 << 0)
  88. #define EECONFIG_KEYMAP_CAPSLOCK_TO_CONTROL (1 << 1)
  89. #define EECONFIG_KEYMAP_SWAP_LALT_LGUI (1 << 2)
  90. #define EECONFIG_KEYMAP_SWAP_RALT_RGUI (1 << 3)
  91. #define EECONFIG_KEYMAP_NO_GUI (1 << 4)
  92. #define EECONFIG_KEYMAP_SWAP_GRAVE_ESC (1 << 5)
  93. #define EECONFIG_KEYMAP_SWAP_BACKSLASH_BACKSPACE (1 << 6)
  94. #define EECONFIG_KEYMAP_NKRO (1 << 7)
  95. bool eeconfig_is_enabled(void);
  96. bool eeconfig_is_disabled(void);
  97. void eeconfig_init(void);
  98. void eeconfig_init_quantum(void);
  99. void eeconfig_init_kb(void);
  100. void eeconfig_init_user(void);
  101. void eeconfig_enable(void);
  102. void eeconfig_disable(void);
  103. uint8_t eeconfig_read_debug(void);
  104. void eeconfig_update_debug(uint8_t val);
  105. uint8_t eeconfig_read_default_layer(void);
  106. void eeconfig_update_default_layer(uint8_t val);
  107. uint16_t eeconfig_read_keymap(void);
  108. void eeconfig_update_keymap(uint16_t val);
  109. #ifdef AUDIO_ENABLE
  110. uint8_t eeconfig_read_audio(void);
  111. void eeconfig_update_audio(uint8_t val);
  112. #endif
  113. #if (EECONFIG_KB_DATA_SIZE) == 0
  114. uint32_t eeconfig_read_kb(void);
  115. void eeconfig_update_kb(uint32_t val);
  116. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  117. #if (EECONFIG_USER_DATA_SIZE) == 0
  118. uint32_t eeconfig_read_user(void);
  119. void eeconfig_update_user(uint32_t val);
  120. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  121. #ifdef HAPTIC_ENABLE
  122. uint32_t eeconfig_read_haptic(void);
  123. void eeconfig_update_haptic(uint32_t val);
  124. #endif
  125. bool eeconfig_read_handedness(void);
  126. void eeconfig_update_handedness(bool val);
  127. #if (EECONFIG_KB_DATA_SIZE) > 0
  128. bool eeconfig_is_kb_datablock_valid(void);
  129. void eeconfig_read_kb_datablock(void *data);
  130. void eeconfig_update_kb_datablock(const void *data);
  131. void eeconfig_init_kb_datablock(void);
  132. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  133. #if (EECONFIG_USER_DATA_SIZE) > 0
  134. bool eeconfig_is_user_datablock_valid(void);
  135. void eeconfig_read_user_datablock(void *data);
  136. void eeconfig_update_user_datablock(const void *data);
  137. void eeconfig_init_user_datablock(void);
  138. #endif // (EECONFIG_USER_DATA_SIZE) > 0
  139. // Any "checked" debounce variant used requires implementation of:
  140. // -- bool eeconfig_check_valid_##name(void)
  141. // -- void eeconfig_post_flush_##name(void)
  142. #define EECONFIG_DEBOUNCE_HELPER_CHECKED(name, offset, config) \
  143. static uint8_t dirty_##name = false; \
  144. \
  145. bool eeconfig_check_valid_##name(void); \
  146. void eeconfig_post_flush_##name(void); \
  147. \
  148. static inline void eeconfig_init_##name(void) { \
  149. dirty_##name = true; \
  150. if (eeconfig_check_valid_##name()) { \
  151. eeprom_read_block(&config, offset, sizeof(config)); \
  152. dirty_##name = false; \
  153. } \
  154. } \
  155. static inline void eeconfig_flush_##name(bool force) { \
  156. if (force || dirty_##name) { \
  157. eeprom_update_block(&config, offset, sizeof(config)); \
  158. eeconfig_post_flush_##name(); \
  159. dirty_##name = false; \
  160. } \
  161. } \
  162. static inline void eeconfig_flush_##name##_task(uint16_t timeout) { \
  163. static uint16_t flush_timer = 0; \
  164. if (timer_elapsed(flush_timer) > timeout) { \
  165. eeconfig_flush_##name(false); \
  166. flush_timer = timer_read(); \
  167. } \
  168. } \
  169. static inline void eeconfig_flag_##name(bool v) { \
  170. dirty_##name |= v; \
  171. } \
  172. static inline void eeconfig_write_##name(typeof(config) *conf) { \
  173. if (memcmp(&config, conf, sizeof(config)) != 0) { \
  174. memcpy(&config, conf, sizeof(config)); \
  175. eeconfig_flag_##name(true); \
  176. } \
  177. }
  178. #define EECONFIG_DEBOUNCE_HELPER(name, offset, config) \
  179. EECONFIG_DEBOUNCE_HELPER_CHECKED(name, offset, config) \
  180. \
  181. bool eeconfig_check_valid_##name(void) { \
  182. return true; \
  183. } \
  184. void eeconfig_post_flush_##name(void) {}