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