diff options
author | Donald Kjer <don.kjer@gmail.com> | 2021-08-23 15:15:34 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-23 23:15:34 +0100 |
commit | e756a21636149ad47c19c659d04be93cf3071dab (patch) | |
tree | aa350c7209c5375f8a3c400353969d2ce0ed3748 | |
parent | 2481e109a0f79b4cdcecab4a6bf6755fb5eda3fc (diff) | |
download | qmk_firmware-e756a21636149ad47c19c659d04be93cf3071dab.tar.gz qmk_firmware-e756a21636149ad47c19c659d04be93cf3071dab.zip |
eeprom_stm32: implement high density wear leveling (#12567)
* eeprom_stm32: implement wear leveling
Update EECONFIG_MAGIC_NUMBER
eeprom_stm32: check emulated eeprom size is large enough
* eeprom_stm32: Increasing simulated EEPROM density on stm32
* Adding utility script to decode emulated eeprom
* Adding unit tests
* Applying qmk cformat changes
* cleaned up flash mocking
* Fix for stm32eeprom_parser.py checking via signature with wrong base
* Fix for nk65 keyboard
Co-authored-by: Ilya Zhuravlev <whatever@xyz.is>
Co-authored-by: zvecr <git@zvecr.com>
-rw-r--r-- | build_test.mk | 1 | ||||
-rwxr-xr-x | keyboards/nk65/config.h | 3 | ||||
-rw-r--r-- | quantum/eeconfig.h | 2 | ||||
-rw-r--r-- | testlist.mk | 1 | ||||
-rw-r--r-- | tmk_core/common/chibios/eeprom_stm32.c | 802 | ||||
-rw-r--r-- | tmk_core/common/chibios/eeprom_stm32.h | 61 | ||||
-rw-r--r-- | tmk_core/common/chibios/flash_stm32.h | 7 | ||||
-rw-r--r-- | tmk_core/common/test/eeprom_stm32_tests.cpp | 438 | ||||
-rw-r--r-- | tmk_core/common/test/flash_stm32_mock.c | 50 | ||||
-rw-r--r-- | tmk_core/common/test/rules.mk | 23 | ||||
-rw-r--r-- | tmk_core/common/test/testlist.mk | 1 | ||||
-rwxr-xr-x | util/stm32eeprom_parser.py | 317 |
12 files changed, 1529 insertions, 177 deletions
diff --git a/build_test.mk b/build_test.mk index b6b878217..037577bf9 100644 --- a/build_test.mk +++ b/build_test.mk | |||
@@ -56,6 +56,7 @@ include $(TMK_PATH)/common.mk | |||
56 | include $(QUANTUM_PATH)/debounce/tests/rules.mk | 56 | include $(QUANTUM_PATH)/debounce/tests/rules.mk |
57 | include $(QUANTUM_PATH)/sequencer/tests/rules.mk | 57 | include $(QUANTUM_PATH)/sequencer/tests/rules.mk |
58 | include $(QUANTUM_PATH)/serial_link/tests/rules.mk | 58 | include $(QUANTUM_PATH)/serial_link/tests/rules.mk |
59 | include $(TMK_PATH)/common/test/rules.mk | ||
59 | ifneq ($(filter $(FULL_TESTS),$(TEST)),) | 60 | ifneq ($(filter $(FULL_TESTS),$(TEST)),) |
60 | include build_full_test.mk | 61 | include build_full_test.mk |
61 | endif | 62 | endif |
diff --git a/keyboards/nk65/config.h b/keyboards/nk65/config.h index fa14e9ffb..a178307cc 100755 --- a/keyboards/nk65/config.h +++ b/keyboards/nk65/config.h | |||
@@ -148,6 +148,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
148 | * both 128kb and 256kb versions of F303. | 148 | * both 128kb and 256kb versions of F303. |
149 | * Register 0x1FFFF7CC holds the size of the flash memory. | 149 | * Register 0x1FFFF7CC holds the size of the flash memory. |
150 | */ | 150 | */ |
151 | #ifndef FLASHSIZE_BASE | ||
152 | # define FLASHSIZE_BASE ((uint32_t)0x1FFFF7CCU) /*!< FLASH Size register base address */ | ||
153 | #endif | ||
151 | #define EEPROM_START_ADDRESS | 154 | #define EEPROM_START_ADDRESS |
152 | #define FEE_MCU_FLASH_SIZE \ | 155 | #define FEE_MCU_FLASH_SIZE \ |
153 | ({ \ | 156 | ({ \ |
diff --git a/quantum/eeconfig.h b/quantum/eeconfig.h index a88071729..bd39971b2 100644 --- a/quantum/eeconfig.h +++ b/quantum/eeconfig.h | |||
@@ -21,7 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
21 | #include <stdbool.h> | 21 | #include <stdbool.h> |
22 | 22 | ||
23 | #ifndef EECONFIG_MAGIC_NUMBER | 23 | #ifndef EECONFIG_MAGIC_NUMBER |
24 | # define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEEA // When changing, decrement this value to avoid future re-init issues | 24 | # define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEE9 // When changing, decrement this value to avoid future re-init issues |
25 | #endif | 25 | #endif |
26 | #define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF | 26 | #define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF |
27 | 27 | ||
diff --git a/testlist.mk b/testlist.mk index d256f4c81..b66b93d29 100644 --- a/testlist.mk +++ b/testlist.mk | |||
@@ -4,6 +4,7 @@ FULL_TESTS := $(TEST_LIST) | |||
4 | include $(ROOT_DIR)/quantum/debounce/tests/testlist.mk | 4 | include $(ROOT_DIR)/quantum/debounce/tests/testlist.mk |
5 | include $(ROOT_DIR)/quantum/sequencer/tests/testlist.mk | 5 | include $(ROOT_DIR)/quantum/sequencer/tests/testlist.mk |
6 | include $(ROOT_DIR)/quantum/serial_link/tests/testlist.mk | 6 | include $(ROOT_DIR)/quantum/serial_link/tests/testlist.mk |
7 | include $(ROOT_DIR)/tmk_core/common/test/testlist.mk | ||
7 | 8 | ||
8 | define VALIDATE_TEST_LIST | 9 | define VALIDATE_TEST_LIST |
9 | ifneq ($1,) | 10 | ifneq ($1,) |
diff --git a/tmk_core/common/chibios/eeprom_stm32.c b/tmk_core/common/chibios/eeprom_stm32.c index ea5198972..5bf852fde 100644 --- a/tmk_core/common/chibios/eeprom_stm32.c +++ b/tmk_core/common/chibios/eeprom_stm32.c | |||
@@ -14,185 +14,751 @@ | |||
14 | * Artur F. | 14 | * Artur F. |
15 | * | 15 | * |
16 | * Modifications for QMK and STM32F303 by Yiancar | 16 | * Modifications for QMK and STM32F303 by Yiancar |
17 | * Modifications to add flash wear leveling by Ilya Zhuravlev | ||
18 | * Modifications to increase flash density by Don Kjer | ||
17 | */ | 19 | */ |
18 | 20 | ||
19 | #include <stdio.h> | 21 | #include <stdio.h> |
20 | #include <string.h> | 22 | #include <stdbool.h> |
23 | #include "debug.h" | ||
21 | #include "eeprom_stm32.h" | 24 | #include "eeprom_stm32.h" |
22 | /***************************************************************************** | 25 | #include "flash_stm32.h" |
23 | * Allows to use the internal flash to store non volatile data. To initialize | 26 | |
24 | * the functionality use the EEPROM_Init() function. Be sure that by reprogramming | 27 | /* |
25 | * of the controller just affected pages will be deleted. In other case the non | 28 | * We emulate eeprom by writing a snapshot compacted view of eeprom contents, |
26 | * volatile data will be lost. | 29 | * followed by a write log of any change since that snapshot: |
27 | ******************************************************************************/ | 30 | * |
31 | * === SIMULATED EEPROM CONTENTS === | ||
32 | * | ||
33 | * ┌─ Compacted ┬ Write Log ─┐ | ||
34 | * │............│[BYTE][BYTE]│ | ||
35 | * │FFFF....FFFF│[WRD0][WRD1]│ | ||
36 | * │FFFFFFFFFFFF│[WORD][NEXT]│ | ||
37 | * │....FFFFFFFF│[BYTE][WRD0]│ | ||
38 | * ├────────────┼────────────┤ | ||
39 | * └──PAGE_BASE │ │ | ||
40 | * PAGE_LAST─┴─WRITE_BASE │ | ||
41 | * WRITE_LAST ┘ | ||
42 | * | ||
43 | * Compacted contents are the 1's complement of the actual EEPROM contents. | ||
44 | * e.g. An 'FFFF' represents a '0000' value. | ||
45 | * | ||
46 | * The size of the 'compacted' area is equal to the size of the 'emulated' eeprom. | ||
47 | * The size of the compacted-area and write log are configurable, and the combined | ||
48 | * size of Compacted + WriteLog is a multiple FEE_PAGE_SIZE, which is MCU dependent. | ||
49 | * Simulated Eeprom contents are located at the end of available flash space. | ||
50 | * | ||
51 | * The following configuration defines can be set: | ||
52 | * | ||
53 | * FEE_DENSITY_PAGES # Total number of pages to use for eeprom simulation (Compact + Write log) | ||
54 | * FEE_DENSITY_BYTES # Size of simulated eeprom. (Defaults to half the space allocated by FEE_DENSITY_PAGES) | ||
55 | * NOTE: The current implementation does not include page swapping, | ||
56 | * and FEE_DENSITY_BYTES will consume that amount of RAM as a cached view of actual EEPROM contents. | ||
57 | * | ||
58 | * The maximum size of FEE_DENSITY_BYTES is currently 16384. The write log size equals | ||
59 | * FEE_DENSITY_PAGES * FEE_PAGE_SIZE - FEE_DENSITY_BYTES. | ||
60 | * The larger the write log, the less frequently the compacted area needs to be rewritten. | ||
61 | * | ||
62 | * | ||
63 | * *** General Algorithm *** | ||
64 | * | ||
65 | * During initialization: | ||
66 | * The contents of the Compacted-flash area are loaded and the 1's complement value | ||
67 | * is cached into memory (e.g. 0xFFFF in Flash represents 0x0000 in cache). | ||
68 | * Write log entries are processed until a 0xFFFF is reached. | ||
69 | * Each log entry updates a byte or word in the cache. | ||
70 | * | ||
71 | * During reads: | ||
72 | * EEPROM contents are given back directly from the cache in memory. | ||
73 | * | ||
74 | * During writes: | ||
75 | * The contents of the cache is updated first. | ||
76 | * If the Compacted-flash area corresponding to the write address is unprogrammed, the 1's complement of the value is written directly into Compacted-flash | ||
77 | * Otherwise: | ||
78 | * If the write log is full, erase both the Compacted-flash area and the Write log, then write cached contents to the Compacted-flash area. | ||
79 | * Otherwise a Write log entry is constructed and appended to the next free position in the Write log. | ||
80 | * | ||
81 | * | ||
82 | * *** Write Log Structure *** | ||
83 | * | ||
84 | * Write log entries allow for optimized byte writes to addresses below 128. Writing 0 or 1 words are also optimized when word-aligned. | ||
85 | * | ||
86 | * === WRITE LOG ENTRY FORMATS === | ||
87 | * | ||
88 | * ╔═══ Byte-Entry ══╗ | ||
89 | * ║0XXXXXXX║YYYYYYYY║ | ||
90 | * ║ └──┬──┘║└──┬───┘║ | ||
91 | * ║ Address║ Value ║ | ||
92 | * ╚════════╩════════╝ | ||
93 | * 0 <= Address < 0x80 (128) | ||
94 | * | ||
95 | * ╔ Word-Encoded 0 ╗ | ||
96 | * ║100XXXXXXXXXXXXX║ | ||
97 | * ║ │└─────┬─────┘║ | ||
98 | * ║ │Address >> 1 ║ | ||
99 | * ║ └── Value: 0 ║ | ||
100 | * ╚════════════════╝ | ||
101 | * 0 <= Address <= 0x3FFE (16382) | ||
102 | * | ||
103 | * ╔ Word-Encoded 1 ╗ | ||
104 | * ║101XXXXXXXXXXXXX║ | ||
105 | * ║ │└─────┬─────┘║ | ||
106 | * ║ │Address >> 1 ║ | ||
107 | * ║ └── Value: 1 ║ | ||
108 | * ╚════════════════╝ | ||
109 | * 0 <= Address <= 0x3FFE (16382) | ||
110 | * | ||
111 | * ╔═══ Reserved ═══╗ | ||
112 | * ║110XXXXXXXXXXXXX║ | ||
113 | * ╚════════════════╝ | ||
114 | * | ||
115 | * ╔═══════════ Word-Next ═══════════╗ | ||
116 | * ║111XXXXXXXXXXXXX║YYYYYYYYYYYYYYYY║ | ||
117 | * ║ └─────┬─────┘║└───────┬──────┘║ | ||
118 | * ║(Address-128)>>1║ ~Value ║ | ||
119 | * ╚════════════════╩════════════════╝ | ||
120 | * ( 0 <= Address < 0x0080 (128): Reserved) | ||
121 | * 0x80 <= Address <= 0x3FFE (16382) | ||
122 | * | ||
123 | * Write Log entry ranges: | ||
124 | * 0x0000 ... 0x7FFF - Byte-Entry; address is (Entry & 0x7F00) >> 4; value is (Entry & 0xFF) | ||
125 | * 0x8000 ... 0x9FFF - Word-Encoded 0; address is (Entry & 0x1FFF) << 1; value is 0 | ||
126 | * 0xA000 ... 0xBFFF - Word-Encoded 1; address is (Entry & 0x1FFF) << 1; value is 1 | ||
127 | * 0xC000 ... 0xDFFF - Reserved | ||
128 | * 0xE000 ... 0xFFBF - Word-Next; address is (Entry & 0x1FFF) << 1 + 0x80; value is ~(Next_Entry) | ||
129 | * 0xFFC0 ... 0xFFFE - Reserved | ||
130 | * 0xFFFF - Unprogrammed | ||
131 | * | ||
132 | */ | ||
28 | 133 | ||
29 | /* Private macro -------------------------------------------------------------*/ | 134 | /* These bits are used for optimizing encoding of bytes, 0 and 1 */ |
30 | /* Private variables ---------------------------------------------------------*/ | 135 | #define FEE_WORD_ENCODING 0x8000 |
31 | /* Functions -----------------------------------------------------------------*/ | 136 | #define FEE_VALUE_NEXT 0x6000 |
137 | #define FEE_VALUE_RESERVED 0x4000 | ||
138 | #define FEE_VALUE_ENCODED 0x2000 | ||
139 | #define FEE_BYTE_RANGE 0x80 | ||
140 | |||
141 | // HACK ALERT. This definition may not match your processor | ||
142 | // To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc | ||
143 | #if defined(EEPROM_EMU_STM32F303xC) | ||
144 | # define MCU_STM32F303CC | ||
145 | #elif defined(EEPROM_EMU_STM32F103xB) | ||
146 | # define MCU_STM32F103RB | ||
147 | #elif defined(EEPROM_EMU_STM32F072xB) | ||
148 | # define MCU_STM32F072CB | ||
149 | #elif defined(EEPROM_EMU_STM32F042x6) | ||
150 | # define MCU_STM32F042K6 | ||
151 | #elif !defined(FEE_PAGE_SIZE) || !defined(FEE_DENSITY_PAGES) || !defined(FEE_MCU_FLASH_SIZE) | ||
152 | # error "not implemented." | ||
153 | #endif | ||
154 | |||
155 | #if !defined(FEE_PAGE_SIZE) || !defined(FEE_DENSITY_PAGES) | ||
156 | # if defined(MCU_STM32F103RB) || defined(MCU_STM32F042K6) | ||
157 | # ifndef FEE_PAGE_SIZE | ||
158 | # define FEE_PAGE_SIZE 0x400 // Page size = 1KByte | ||
159 | # endif | ||
160 | # ifndef FEE_DENSITY_PAGES | ||
161 | # define FEE_DENSITY_PAGES 2 // How many pages are used | ||
162 | # endif | ||
163 | # elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) || defined(MCU_STM32F103RD) || defined(MCU_STM32F303CC) || defined(MCU_STM32F072CB) | ||
164 | # ifndef FEE_PAGE_SIZE | ||
165 | # define FEE_PAGE_SIZE 0x800 // Page size = 2KByte | ||
166 | # endif | ||
167 | # ifndef FEE_DENSITY_PAGES | ||
168 | # define FEE_DENSITY_PAGES 4 // How many pages are used | ||
169 | # endif | ||
170 | # else | ||
171 | # error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." | ||
172 | # endif | ||
173 | #endif | ||
174 | |||
175 | #ifndef FEE_MCU_FLASH_SIZE | ||
176 | # if defined(MCU_STM32F103RB) || defined(MCU_STM32F072CB) | ||
177 | # define FEE_MCU_FLASH_SIZE 128 // Size in Kb | ||
178 | # elif defined(MCU_STM32F042K6) | ||
179 | # define FEE_MCU_FLASH_SIZE 32 // Size in Kb | ||
180 | # elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) | ||
181 | # define FEE_MCU_FLASH_SIZE 512 // Size in Kb | ||
182 | # elif defined(MCU_STM32F103RD) | ||
183 | # define FEE_MCU_FLASH_SIZE 384 // Size in Kb | ||
184 | # elif defined(MCU_STM32F303CC) | ||
185 | # define FEE_MCU_FLASH_SIZE 256 // Size in Kb | ||
186 | # else | ||
187 | # error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." | ||
188 | # endif | ||
189 | #endif | ||
190 | |||
191 | #define FEE_XSTR(x) FEE_STR(x) | ||
192 | #define FEE_STR(x) #x | ||
193 | |||
194 | /* Size of combined compacted eeprom and write log pages */ | ||
195 | #define FEE_DENSITY_MAX_SIZE (FEE_DENSITY_PAGES * FEE_PAGE_SIZE) | ||
196 | /* Addressable range 16KByte: 0 <-> (0x1FFF << 1) */ | ||
197 | #define FEE_ADDRESS_MAX_SIZE 0x4000 | ||
198 | |||
199 | #ifndef EEPROM_START_ADDRESS /* *TODO: Get rid of this check */ | ||
200 | # if FEE_DENSITY_MAX_SIZE > (FEE_MCU_FLASH_SIZE * 1024) | ||
201 | # pragma message FEE_XSTR(FEE_DENSITY_MAX_SIZE) " > " FEE_XSTR(FEE_MCU_FLASH_SIZE * 1024) | ||
202 | # error emulated eeprom: FEE_DENSITY_PAGES is greater than available flash size | ||
203 | # endif | ||
204 | #endif | ||
205 | |||
206 | /* Size of emulated eeprom */ | ||
207 | #ifdef FEE_DENSITY_BYTES | ||
208 | # if (FEE_DENSITY_BYTES > FEE_DENSITY_MAX_SIZE) | ||
209 | # pragma message FEE_XSTR(FEE_DENSITY_BYTES) " > " FEE_XSTR(FEE_DENSITY_MAX_SIZE) | ||
210 | # error emulated eeprom: FEE_DENSITY_BYTES exceeds FEE_DENSITY_MAX_SIZE | ||
211 | # endif | ||
212 | # if (FEE_DENSITY_BYTES == FEE_DENSITY_MAX_SIZE) | ||
213 | # pragma message FEE_XSTR(FEE_DENSITY_BYTES) " == " FEE_XSTR(FEE_DENSITY_MAX_SIZE) | ||
214 | # warning emulated eeprom: FEE_DENSITY_BYTES leaves no room for a write log. This will greatly increase the flash wear rate! | ||
215 | # endif | ||
216 | # if FEE_DENSITY_BYTES > FEE_ADDRESS_MAX_SIZE | ||
217 | # pragma message FEE_XSTR(FEE_DENSITY_BYTES) " > " FEE_XSTR(FEE_ADDRESS_MAX_SIZE) | ||
218 | # error emulated eeprom: FEE_DENSITY_BYTES is greater than FEE_ADDRESS_MAX_SIZE allows | ||
219 | # endif | ||
220 | # if ((FEE_DENSITY_BYTES) % 2) == 1 | ||
221 | # error emulated eeprom: FEE_DENSITY_BYTES must be even | ||
222 | # endif | ||
223 | #else | ||
224 | /* Default to half of allocated space used for emulated eeprom, half for write log */ | ||
225 | # define FEE_DENSITY_BYTES (FEE_DENSITY_PAGES * FEE_PAGE_SIZE / 2) | ||
226 | #endif | ||
227 | |||
228 | /* Size of write log */ | ||
229 | #define FEE_WRITE_LOG_BYTES (FEE_DENSITY_PAGES * FEE_PAGE_SIZE - FEE_DENSITY_BYTES) | ||
230 | |||
231 | /* Start of the emulated eeprom compacted flash area */ | ||
232 | #ifndef FEE_FLASH_BASE | ||
233 | # define FEE_FLASH_BASE 0x8000000 | ||
234 | #endif | ||
235 | #define FEE_PAGE_BASE_ADDRESS ((uintptr_t)(FEE_FLASH_BASE) + FEE_MCU_FLASH_SIZE * 1024 - FEE_WRITE_LOG_BYTES - FEE_DENSITY_BYTES) | ||
236 | /* End of the emulated eeprom compacted flash area */ | ||
237 | #define FEE_PAGE_LAST_ADDRESS (FEE_PAGE_BASE_ADDRESS + FEE_DENSITY_BYTES) | ||
238 | /* Start of the emulated eeprom write log */ | ||
239 | #define FEE_WRITE_LOG_BASE_ADDRESS FEE_PAGE_LAST_ADDRESS | ||
240 | /* End of the emulated eeprom write log */ | ||
241 | #define FEE_WRITE_LOG_LAST_ADDRESS (FEE_WRITE_LOG_BASE_ADDRESS + FEE_WRITE_LOG_BYTES) | ||
242 | |||
243 | /* Flash word value after erase */ | ||
244 | #define FEE_EMPTY_WORD ((uint16_t)0xFFFF) | ||
245 | |||
246 | #if defined(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) && (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR >= FEE_DENSITY_BYTES) | ||
247 | # error emulated eeprom: DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is greater than the FEE_DENSITY_BYTES available | ||
248 | #endif | ||
249 | |||
250 | /* In-memory contents of emulated eeprom for faster access */ | ||
251 | /* *TODO: Implement page swapping */ | ||
252 | static uint16_t WordBuf[FEE_DENSITY_BYTES / 2]; | ||
253 | static uint8_t *DataBuf = (uint8_t *)WordBuf; | ||
254 | |||
255 | /* Pointer to the first available slot within the write log */ | ||
256 | static uint16_t *empty_slot; | ||
257 | |||
258 | // #define DEBUG_EEPROM_OUTPUT | ||
259 | |||
260 | /* | ||
261 | * Debug print utils | ||
262 | */ | ||
263 | |||
264 | #if defined(DEBUG_EEPROM_OUTPUT) | ||
265 | |||
266 | # define debug_eeprom debug_enable | ||
267 | # define eeprom_println(s) println(s) | ||
268 | # define eeprom_printf(fmt, ...) xprintf(fmt, ##__VA_ARGS__); | ||
269 | |||
270 | #else /* NO_DEBUG */ | ||
271 | |||
272 | # define debug_eeprom false | ||
273 | # define eeprom_println(s) | ||
274 | # define eeprom_printf(fmt, ...) | ||
275 | |||
276 | #endif /* NO_DEBUG */ | ||
277 | |||
278 | void print_eeprom(void) { | ||
279 | #ifndef NO_DEBUG | ||
280 | int empty_rows = 0; | ||
281 | for (uint16_t i = 0; i < FEE_DENSITY_BYTES; i++) { | ||
282 | if (i % 16 == 0) { | ||
283 | if (i >= FEE_DENSITY_BYTES - 16) { | ||
284 | /* Make sure we display the last row */ | ||
285 | empty_rows = 0; | ||
286 | } | ||
287 | /* Check if this row is uninitialized */ | ||
288 | ++empty_rows; | ||
289 | for (uint16_t j = 0; j < 16; j++) { | ||
290 | if (DataBuf[i + j]) { | ||
291 | empty_rows = 0; | ||
292 | break; | ||
293 | } | ||
294 | } | ||
295 | if (empty_rows > 1) { | ||
296 | /* Repeat empty row */ | ||
297 | if (empty_rows == 2) { | ||
298 | /* Only display the first repeat empty row */ | ||
299 | println("*"); | ||
300 | } | ||
301 | i += 15; | ||
302 | continue; | ||
303 | } | ||
304 | xprintf("%04x", i); | ||
305 | } | ||
306 | if (i % 8 == 0) print(" "); | ||
307 | |||
308 | xprintf(" %02x", DataBuf[i]); | ||
309 | if ((i + 1) % 16 == 0) { | ||
310 | println(""); | ||
311 | } | ||
312 | } | ||
313 | #endif | ||
314 | } | ||
32 | 315 | ||
33 | uint8_t DataBuf[FEE_PAGE_SIZE]; | ||
34 | /***************************************************************************** | ||
35 | * Delete Flash Space used for user Data, deletes the whole space between | ||
36 | * RW_PAGE_BASE_ADDRESS and the last uC Flash Page | ||
37 | ******************************************************************************/ | ||
38 | uint16_t EEPROM_Init(void) { | 316 | uint16_t EEPROM_Init(void) { |
39 | // unlock flash | 317 | /* Load emulated eeprom contents from compacted flash into memory */ |
40 | FLASH_Unlock(); | 318 | uint16_t *src = (uint16_t *)FEE_PAGE_BASE_ADDRESS; |
319 | uint16_t *dest = (uint16_t *)DataBuf; | ||
320 | for (; src < (uint16_t *)FEE_PAGE_LAST_ADDRESS; ++src, ++dest) { | ||
321 | *dest = ~*src; | ||
322 | } | ||
323 | |||
324 | if (debug_eeprom) { | ||
325 | println("EEPROM_Init Compacted Pages:"); | ||
326 | print_eeprom(); | ||
327 | println("EEPROM_Init Write Log:"); | ||
328 | } | ||
329 | |||
330 | /* Replay write log */ | ||
331 | uint16_t *log_addr; | ||
332 | for (log_addr = (uint16_t *)FEE_WRITE_LOG_BASE_ADDRESS; log_addr < (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS; ++log_addr) { | ||
333 | uint16_t address = *log_addr; | ||
334 | if (address == FEE_EMPTY_WORD) { | ||
335 | break; | ||
336 | } | ||
337 | /* Check for lowest 128-bytes optimization */ | ||
338 | if (!(address & FEE_WORD_ENCODING)) { | ||
339 | uint8_t bvalue = (uint8_t)address; | ||
340 | address >>= 8; | ||
341 | DataBuf[address] = bvalue; | ||
342 | eeprom_printf("DataBuf[0x%02x] = 0x%02x;\n", address, bvalue); | ||
343 | } else { | ||
344 | uint16_t wvalue; | ||
345 | /* Check if value is in next word */ | ||
346 | if ((address & FEE_VALUE_NEXT) == FEE_VALUE_NEXT) { | ||
347 | /* Read value from next word */ | ||
348 | if (++log_addr >= (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS) { | ||
349 | break; | ||
350 | } | ||
351 | wvalue = ~*log_addr; | ||
352 | if (!wvalue) { | ||
353 | eeprom_printf("Incomplete write at log_addr: 0x%04x;\n", (uint32_t)log_addr); | ||
354 | /* Possibly incomplete write. Ignore and continue */ | ||
355 | continue; | ||
356 | } | ||
357 | address &= 0x1FFF; | ||
358 | address <<= 1; | ||
359 | /* Writes to addresses less than 128 are byte log entries */ | ||
360 | address += FEE_BYTE_RANGE; | ||
361 | } else { | ||
362 | /* Reserved for future use */ | ||
363 | if (address & FEE_VALUE_RESERVED) { | ||
364 | eeprom_printf("Reserved encoded value at log_addr: 0x%04x;\n", (uint32_t)log_addr); | ||
365 | continue; | ||
366 | } | ||
367 | /* Optimization for 0 or 1 values. */ | ||
368 | wvalue = (address & FEE_VALUE_ENCODED) >> 13; | ||
369 | address &= 0x1FFF; | ||
370 | address <<= 1; | ||
371 | } | ||
372 | if (address < FEE_DENSITY_BYTES) { | ||
373 | eeprom_printf("DataBuf[0x%04x] = 0x%04x;\n", address, wvalue); | ||
374 | *(uint16_t *)(&DataBuf[address]) = wvalue; | ||
375 | } else { | ||
376 | eeprom_printf("DataBuf[0x%04x] cannot be set to 0x%04x [BAD ADDRESS]\n", address, wvalue); | ||
377 | } | ||
378 | } | ||
379 | } | ||
41 | 380 | ||
42 | // Clear Flags | 381 | empty_slot = log_addr; |
43 | // FLASH_ClearFlag(FLASH_SR_EOP|FLASH_SR_PGERR|FLASH_SR_WRPERR); | 382 | |
383 | if (debug_eeprom) { | ||
384 | println("EEPROM_Init Final DataBuf:"); | ||
385 | print_eeprom(); | ||
386 | } | ||
44 | 387 | ||
45 | return FEE_DENSITY_BYTES; | 388 | return FEE_DENSITY_BYTES; |
46 | } | 389 | } |
47 | /***************************************************************************** | ||
48 | * Erase the whole reserved Flash Space used for user Data | ||
49 | ******************************************************************************/ | ||
50 | void EEPROM_Erase(void) { | ||
51 | int page_num = 0; | ||
52 | 390 | ||
53 | // delete all pages from specified start page to the last page | 391 | /* Clear flash contents (doesn't touch in-memory DataBuf) */ |
54 | do { | 392 | static void eeprom_clear(void) { |
393 | FLASH_Unlock(); | ||
394 | |||
395 | for (uint16_t page_num = 0; page_num < FEE_DENSITY_PAGES; ++page_num) { | ||
396 | eeprom_printf("FLASH_ErasePage(0x%04x)\n", (uint32_t)(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE))); | ||
55 | FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)); | 397 | FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)); |
56 | page_num++; | 398 | } |
57 | } while (page_num < FEE_DENSITY_PAGES); | 399 | |
400 | FLASH_Lock(); | ||
401 | |||
402 | empty_slot = (uint16_t *)FEE_WRITE_LOG_BASE_ADDRESS; | ||
403 | eeprom_printf("eeprom_clear empty_slot: 0x%08x\n", (uint32_t)empty_slot); | ||
58 | } | 404 | } |
59 | /***************************************************************************** | ||
60 | * Writes once data byte to flash on specified address. If a byte is already | ||
61 | * written, the whole page must be copied to a buffer, the byte changed and | ||
62 | * the manipulated buffer written after PageErase. | ||
63 | *******************************************************************************/ | ||
64 | uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) { | ||
65 | FLASH_Status FlashStatus = FLASH_COMPLETE; | ||
66 | 405 | ||
67 | uint32_t page; | 406 | /* Erase emulated eeprom */ |
68 | int i; | 407 | void EEPROM_Erase(void) { |
408 | eeprom_println("EEPROM_Erase"); | ||
409 | /* Erase compacted pages and write log */ | ||
410 | eeprom_clear(); | ||
411 | /* re-initialize to reset DataBuf */ | ||
412 | EEPROM_Init(); | ||
413 | } | ||
69 | 414 | ||
70 | // exit if desired address is above the limit (e.G. under 2048 Bytes for 4 pages) | 415 | /* Compact write log */ |
71 | if (Address > FEE_DENSITY_BYTES) { | 416 | static uint8_t eeprom_compact(void) { |
72 | return 0; | 417 | /* Erase compacted pages and write log */ |
418 | eeprom_clear(); | ||
419 | |||
420 | FLASH_Unlock(); | ||
421 | |||
422 | FLASH_Status final_status = FLASH_COMPLETE; | ||
423 | |||
424 | /* Write emulated eeprom contents from memory to compacted flash */ | ||
425 | uint16_t *src = (uint16_t *)DataBuf; | ||
426 | uintptr_t dest = FEE_PAGE_BASE_ADDRESS; | ||
427 | uint16_t value; | ||
428 | for (; dest < FEE_PAGE_LAST_ADDRESS; ++src, dest += 2) { | ||
429 | value = *src; | ||
430 | if (value) { | ||
431 | eeprom_printf("FLASH_ProgramHalfWord(0x%04x, 0x%04x)\n", (uint32_t)dest, ~value); | ||
432 | FLASH_Status status = FLASH_ProgramHalfWord(dest, ~value); | ||
433 | if (status != FLASH_COMPLETE) final_status = status; | ||
434 | } | ||
73 | } | 435 | } |
74 | 436 | ||
75 | // calculate which page is affected (Pagenum1/Pagenum2...PagenumN) | 437 | FLASH_Lock(); |
76 | page = FEE_ADDR_OFFSET(Address) / FEE_PAGE_SIZE; | ||
77 | 438 | ||
78 | // if current data is 0xFF, the byte is empty, just overwrite with the new one | 439 | if (debug_eeprom) { |
79 | if ((*(__IO uint16_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) == FEE_EMPTY_WORD) { | 440 | println("eeprom_compacted:"); |
80 | FlashStatus = FLASH_ProgramHalfWord(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address), (uint16_t)(0x00FF & DataByte)); | 441 | print_eeprom(); |
442 | } | ||
443 | |||
444 | return final_status; | ||
445 | } | ||
446 | |||
447 | static uint8_t eeprom_write_direct_entry(uint16_t Address) { | ||
448 | /* Check if we can just write this directly to the compacted flash area */ | ||
449 | uintptr_t directAddress = FEE_PAGE_BASE_ADDRESS + (Address & 0xFFFE); | ||
450 | if (*(uint16_t *)directAddress == FEE_EMPTY_WORD) { | ||
451 | /* Write the value directly to the compacted area without a log entry */ | ||
452 | uint16_t value = ~*(uint16_t *)(&DataBuf[Address & 0xFFFE]); | ||
453 | /* Early exit if a write isn't needed */ | ||
454 | if (value == FEE_EMPTY_WORD) return FLASH_COMPLETE; | ||
455 | |||
456 | FLASH_Unlock(); | ||
457 | |||
458 | eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x) [DIRECT]\n", (uint32_t)directAddress, value); | ||
459 | FLASH_Status status = FLASH_ProgramHalfWord(directAddress, value); | ||
460 | |||
461 | FLASH_Lock(); | ||
462 | return status; | ||
463 | } | ||
464 | return 0; | ||
465 | } | ||
466 | |||
467 | static uint8_t eeprom_write_log_word_entry(uint16_t Address) { | ||
468 | FLASH_Status final_status = FLASH_COMPLETE; | ||
469 | |||
470 | uint16_t value = *(uint16_t *)(&DataBuf[Address]); | ||
471 | eeprom_printf("eeprom_write_log_word_entry(0x%04x): 0x%04x\n", Address, value); | ||
472 | |||
473 | /* MSB signifies the lowest 128-byte optimization is not in effect */ | ||
474 | uint16_t encoding = FEE_WORD_ENCODING; | ||
475 | uint8_t entry_size; | ||
476 | if (value <= 1) { | ||
477 | encoding |= value << 13; | ||
478 | entry_size = 2; | ||
81 | } else { | 479 | } else { |
82 | // Copy Page to a buffer | 480 | encoding |= FEE_VALUE_NEXT; |
83 | memcpy(DataBuf, (uint8_t *)FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE), FEE_PAGE_SIZE); // !!! Calculate base address for the desired page | 481 | entry_size = 4; |
482 | /* Writes to addresses less than 128 are byte log entries */ | ||
483 | Address -= FEE_BYTE_RANGE; | ||
484 | } | ||
485 | |||
486 | /* if we can't find an empty spot, we must compact emulated eeprom */ | ||
487 | if (empty_slot > (uint16_t *)(FEE_WRITE_LOG_LAST_ADDRESS - entry_size)) { | ||
488 | /* compact the write log into the compacted flash area */ | ||
489 | return eeprom_compact(); | ||
490 | } | ||
491 | |||
492 | /* Word log writes should be word-aligned. Take back a bit */ | ||
493 | Address >>= 1; | ||
494 | Address |= encoding; | ||
495 | |||
496 | /* ok we found a place let's write our data */ | ||
497 | FLASH_Unlock(); | ||
498 | |||
499 | /* address */ | ||
500 | eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, Address); | ||
501 | final_status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, Address); | ||
502 | |||
503 | /* value */ | ||
504 | if (encoding == (FEE_WORD_ENCODING | FEE_VALUE_NEXT)) { | ||
505 | eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, ~value); | ||
506 | FLASH_Status status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, ~value); | ||
507 | if (status != FLASH_COMPLETE) final_status = status; | ||
508 | } | ||
509 | |||
510 | FLASH_Lock(); | ||
84 | 511 | ||
85 | // check if new data is differ to current data, return if not, proceed if yes | 512 | return final_status; |
86 | if (DataByte == *(__IO uint8_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) { | 513 | } |
87 | return 0; | 514 | |
515 | static uint8_t eeprom_write_log_byte_entry(uint16_t Address) { | ||
516 | eeprom_printf("eeprom_write_log_byte_entry(0x%04x): 0x%02x\n", Address, DataBuf[Address]); | ||
517 | |||
518 | /* if couldn't find an empty spot, we must compact emulated eeprom */ | ||
519 | if (empty_slot >= (uint16_t *)FEE_WRITE_LOG_LAST_ADDRESS) { | ||
520 | /* compact the write log into the compacted flash area */ | ||
521 | return eeprom_compact(); | ||
522 | } | ||
523 | |||
524 | /* ok we found a place let's write our data */ | ||
525 | FLASH_Unlock(); | ||
526 | |||
527 | /* Pack address and value into the same word */ | ||
528 | uint16_t value = (Address << 8) | DataBuf[Address]; | ||
529 | |||
530 | /* write to flash */ | ||
531 | eeprom_printf("FLASH_ProgramHalfWord(0x%08x, 0x%04x)\n", (uint32_t)empty_slot, value); | ||
532 | FLASH_Status status = FLASH_ProgramHalfWord((uintptr_t)empty_slot++, value); | ||
533 | |||
534 | FLASH_Lock(); | ||
535 | |||
536 | return status; | ||
537 | } | ||
538 | |||
539 | uint8_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) { | ||
540 | /* if the address is out-of-bounds, do nothing */ | ||
541 | if (Address >= FEE_DENSITY_BYTES) { | ||
542 | eeprom_printf("EEPROM_WriteDataByte(0x%04x, 0x%02x) [BAD ADDRESS]\n", Address, DataByte); | ||
543 | return FLASH_BAD_ADDRESS; | ||
544 | } | ||
545 | |||
546 | /* if the value is the same, don't bother writing it */ | ||
547 | if (DataBuf[Address] == DataByte) { | ||
548 | eeprom_printf("EEPROM_WriteDataByte(0x%04x, 0x%02x) [SKIP SAME]\n", Address, DataByte); | ||
549 | return 0; | ||
550 | } | ||
551 | |||
552 | /* keep DataBuf cache in sync */ | ||
553 | DataBuf[Address] = DataByte; | ||
554 | eeprom_printf("EEPROM_WriteDataByte DataBuf[0x%04x] = 0x%02x\n", Address, DataBuf[Address]); | ||
555 | |||
556 | /* perform the write into flash memory */ | ||
557 | /* First, attempt to write directly into the compacted flash area */ | ||
558 | FLASH_Status status = eeprom_write_direct_entry(Address); | ||
559 | if (!status) { | ||
560 | /* Otherwise append to the write log */ | ||
561 | if (Address < FEE_BYTE_RANGE) { | ||
562 | status = eeprom_write_log_byte_entry(Address); | ||
563 | } else { | ||
564 | status = eeprom_write_log_word_entry(Address & 0xFFFE); | ||
88 | } | 565 | } |
566 | } | ||
567 | if (status != 0 && status != FLASH_COMPLETE) { | ||
568 | eeprom_printf("EEPROM_WriteDataByte [STATUS == %d]\n", status); | ||
569 | } | ||
570 | return status; | ||
571 | } | ||
89 | 572 | ||
90 | // manipulate desired data byte in temp data array if new byte is differ to the current | 573 | uint8_t EEPROM_WriteDataWord(uint16_t Address, uint16_t DataWord) { |
91 | DataBuf[FEE_ADDR_OFFSET(Address) % FEE_PAGE_SIZE] = DataByte; | 574 | /* if the address is out-of-bounds, do nothing */ |
575 | if (Address >= FEE_DENSITY_BYTES) { | ||
576 | eeprom_printf("EEPROM_WriteDataWord(0x%04x, 0x%04x) [BAD ADDRESS]\n", Address, DataWord); | ||
577 | return FLASH_BAD_ADDRESS; | ||
578 | } | ||
92 | 579 | ||
93 | // Erase Page | 580 | /* Check for word alignment */ |
94 | FlashStatus = FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)); | 581 | FLASH_Status final_status = FLASH_COMPLETE; |
582 | if (Address % 2) { | ||
583 | final_status = EEPROM_WriteDataByte(Address, DataWord); | ||
584 | FLASH_Status status = EEPROM_WriteDataByte(Address + 1, DataWord >> 8); | ||
585 | if (status != FLASH_COMPLETE) final_status = status; | ||
586 | if (final_status != 0 && final_status != FLASH_COMPLETE) { | ||
587 | eeprom_printf("EEPROM_WriteDataWord [STATUS == %d]\n", final_status); | ||
588 | } | ||
589 | return final_status; | ||
590 | } | ||
591 | |||
592 | /* if the value is the same, don't bother writing it */ | ||
593 | uint16_t oldValue = *(uint16_t *)(&DataBuf[Address]); | ||
594 | if (oldValue == DataWord) { | ||
595 | eeprom_printf("EEPROM_WriteDataWord(0x%04x, 0x%04x) [SKIP SAME]\n", Address, DataWord); | ||
596 | return 0; | ||
597 | } | ||
598 | |||
599 | /* keep DataBuf cache in sync */ | ||
600 | *(uint16_t *)(&DataBuf[Address]) = DataWord; | ||
601 | eeprom_printf("EEPROM_WriteDataWord DataBuf[0x%04x] = 0x%04x\n", Address, *(uint16_t *)(&DataBuf[Address])); | ||
95 | 602 | ||
96 | // Write new data (whole page) to flash if data has been changed | 603 | /* perform the write into flash memory */ |
97 | for (i = 0; i < (FEE_PAGE_SIZE / 2); i++) { | 604 | /* First, attempt to write directly into the compacted flash area */ |
98 | if ((__IO uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)]) != 0xFFFF) { | 605 | final_status = eeprom_write_direct_entry(Address); |
99 | FlashStatus = FLASH_ProgramHalfWord((FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)) + (i * 2), (uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)])); | 606 | if (!final_status) { |
607 | /* Otherwise append to the write log */ | ||
608 | /* Check if we need to fall back to byte write */ | ||
609 | if (Address < FEE_BYTE_RANGE) { | ||
610 | final_status = FLASH_COMPLETE; | ||
611 | /* Only write a byte if it has changed */ | ||
612 | if ((uint8_t)oldValue != (uint8_t)DataWord) { | ||
613 | final_status = eeprom_write_log_byte_entry(Address); | ||
100 | } | 614 | } |
615 | FLASH_Status status = FLASH_COMPLETE; | ||
616 | /* Only write a byte if it has changed */ | ||
617 | if ((oldValue >> 8) != (DataWord >> 8)) { | ||
618 | status = eeprom_write_log_byte_entry(Address + 1); | ||
619 | } | ||
620 | if (status != FLASH_COMPLETE) final_status = status; | ||
621 | } else { | ||
622 | final_status = eeprom_write_log_word_entry(Address); | ||
101 | } | 623 | } |
102 | } | 624 | } |
103 | return FlashStatus; | 625 | if (final_status != 0 && final_status != FLASH_COMPLETE) { |
626 | eeprom_printf("EEPROM_WriteDataWord [STATUS == %d]\n", final_status); | ||
627 | } | ||
628 | return final_status; | ||
104 | } | 629 | } |
105 | /***************************************************************************** | 630 | |
106 | * Read once data byte from a specified address. | ||
107 | *******************************************************************************/ | ||
108 | uint8_t EEPROM_ReadDataByte(uint16_t Address) { | 631 | uint8_t EEPROM_ReadDataByte(uint16_t Address) { |
109 | uint8_t DataByte = 0xFF; | 632 | uint8_t DataByte = 0xFF; |
110 | 633 | ||
111 | // Get Byte from specified address | 634 | if (Address < FEE_DENSITY_BYTES) { |
112 | DataByte = (*(__IO uint8_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))); | 635 | DataByte = DataBuf[Address]; |
636 | } | ||
637 | |||
638 | eeprom_printf("EEPROM_ReadDataByte(0x%04x): 0x%02x\n", Address, DataByte); | ||
113 | 639 | ||
114 | return DataByte; | 640 | return DataByte; |
115 | } | 641 | } |
116 | 642 | ||
643 | uint16_t EEPROM_ReadDataWord(uint16_t Address) { | ||
644 | uint16_t DataWord = 0xFFFF; | ||
645 | |||
646 | if (Address < FEE_DENSITY_BYTES - 1) { | ||
647 | /* Check word alignment */ | ||
648 | if (Address % 2) { | ||
649 | DataWord = DataBuf[Address] | (DataBuf[Address + 1] << 8); | ||
650 | } else { | ||
651 | DataWord = *(uint16_t *)(&DataBuf[Address]); | ||
652 | } | ||
653 | } | ||
654 | |||
655 | eeprom_printf("EEPROM_ReadDataWord(0x%04x): 0x%04x\n", Address, DataWord); | ||
656 | |||
657 | return DataWord; | ||
658 | } | ||
659 | |||
117 | /***************************************************************************** | 660 | /***************************************************************************** |
118 | * Wrap library in AVR style functions. | 661 | * Wrap library in AVR style functions. |
119 | *******************************************************************************/ | 662 | *******************************************************************************/ |
120 | uint8_t eeprom_read_byte(const uint8_t *Address) { | 663 | uint8_t eeprom_read_byte(const uint8_t *Address) { return EEPROM_ReadDataByte((const uintptr_t)Address); } |
121 | const uint16_t p = (const uint32_t)Address; | ||
122 | return EEPROM_ReadDataByte(p); | ||
123 | } | ||
124 | 664 | ||
125 | void eeprom_write_byte(uint8_t *Address, uint8_t Value) { | 665 | void eeprom_write_byte(uint8_t *Address, uint8_t Value) { EEPROM_WriteDataByte((uintptr_t)Address, Value); } |
126 | uint16_t p = (uint32_t)Address; | ||
127 | EEPROM_WriteDataByte(p, Value); | ||
128 | } | ||
129 | 666 | ||
130 | void eeprom_update_byte(uint8_t *Address, uint8_t Value) { | 667 | void eeprom_update_byte(uint8_t *Address, uint8_t Value) { EEPROM_WriteDataByte((uintptr_t)Address, Value); } |
131 | uint16_t p = (uint32_t)Address; | ||
132 | EEPROM_WriteDataByte(p, Value); | ||
133 | } | ||
134 | 668 | ||
135 | uint16_t eeprom_read_word(const uint16_t *Address) { | 669 | uint16_t eeprom_read_word(const uint16_t *Address) { return EEPROM_ReadDataWord((const uintptr_t)Address); } |
136 | const uint16_t p = (const uint32_t)Address; | ||
137 | return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8); | ||
138 | } | ||
139 | 670 | ||
140 | void eeprom_write_word(uint16_t *Address, uint16_t Value) { | 671 | void eeprom_write_word(uint16_t *Address, uint16_t Value) { EEPROM_WriteDataWord((uintptr_t)Address, Value); } |
141 | uint16_t p = (uint32_t)Address; | ||
142 | EEPROM_WriteDataByte(p, (uint8_t)Value); | ||
143 | EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8)); | ||
144 | } | ||
145 | 672 | ||
146 | void eeprom_update_word(uint16_t *Address, uint16_t Value) { | 673 | void eeprom_update_word(uint16_t *Address, uint16_t Value) { EEPROM_WriteDataWord((uintptr_t)Address, Value); } |
147 | uint16_t p = (uint32_t)Address; | ||
148 | EEPROM_WriteDataByte(p, (uint8_t)Value); | ||
149 | EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8)); | ||
150 | } | ||
151 | 674 | ||
152 | uint32_t eeprom_read_dword(const uint32_t *Address) { | 675 | uint32_t eeprom_read_dword(const uint32_t *Address) { |
153 | const uint16_t p = (const uint32_t)Address; | 676 | const uint16_t p = (const uintptr_t)Address; |
154 | return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8) | (EEPROM_ReadDataByte(p + 2) << 16) | (EEPROM_ReadDataByte(p + 3) << 24); | 677 | /* Check word alignment */ |
678 | if (p % 2) { | ||
679 | /* Not aligned */ | ||
680 | return (uint32_t)EEPROM_ReadDataByte(p) | (uint32_t)(EEPROM_ReadDataWord(p + 1) << 8) | (uint32_t)(EEPROM_ReadDataByte(p + 3) << 24); | ||
681 | } else { | ||
682 | /* Aligned */ | ||
683 | return EEPROM_ReadDataWord(p) | (EEPROM_ReadDataWord(p + 2) << 16); | ||
684 | } | ||
155 | } | 685 | } |
156 | 686 | ||
157 | void eeprom_write_dword(uint32_t *Address, uint32_t Value) { | 687 | void eeprom_write_dword(uint32_t *Address, uint32_t Value) { |
158 | uint16_t p = (const uint32_t)Address; | 688 | uint16_t p = (const uintptr_t)Address; |
159 | EEPROM_WriteDataByte(p, (uint8_t)Value); | 689 | /* Check word alignment */ |
160 | EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8)); | 690 | if (p % 2) { |
161 | EEPROM_WriteDataByte(p + 2, (uint8_t)(Value >> 16)); | 691 | /* Not aligned */ |
162 | EEPROM_WriteDataByte(p + 3, (uint8_t)(Value >> 24)); | ||
163 | } | ||
164 | |||
165 | void eeprom_update_dword(uint32_t *Address, uint32_t Value) { | ||
166 | uint16_t p = (const uint32_t)Address; | ||
167 | uint32_t existingValue = EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8) | (EEPROM_ReadDataByte(p + 2) << 16) | (EEPROM_ReadDataByte(p + 3) << 24); | ||
168 | if (Value != existingValue) { | ||
169 | EEPROM_WriteDataByte(p, (uint8_t)Value); | 692 | EEPROM_WriteDataByte(p, (uint8_t)Value); |
170 | EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8)); | 693 | EEPROM_WriteDataWord(p + 1, (uint16_t)(Value >> 8)); |
171 | EEPROM_WriteDataByte(p + 2, (uint8_t)(Value >> 16)); | ||
172 | EEPROM_WriteDataByte(p + 3, (uint8_t)(Value >> 24)); | 694 | EEPROM_WriteDataByte(p + 3, (uint8_t)(Value >> 24)); |
695 | } else { | ||
696 | /* Aligned */ | ||
697 | EEPROM_WriteDataWord(p, (uint16_t)Value); | ||
698 | EEPROM_WriteDataWord(p + 2, (uint16_t)(Value >> 16)); | ||
173 | } | 699 | } |
174 | } | 700 | } |
175 | 701 | ||
702 | void eeprom_update_dword(uint32_t *Address, uint32_t Value) { eeprom_write_dword(Address, Value); } | ||
703 | |||
176 | void eeprom_read_block(void *buf, const void *addr, size_t len) { | 704 | void eeprom_read_block(void *buf, const void *addr, size_t len) { |
177 | const uint8_t *p = (const uint8_t *)addr; | 705 | const uint8_t *src = (const uint8_t *)addr; |
178 | uint8_t * dest = (uint8_t *)buf; | 706 | uint8_t * dest = (uint8_t *)buf; |
179 | while (len--) { | 707 | |
180 | *dest++ = eeprom_read_byte(p++); | 708 | /* Check word alignment */ |
709 | if (len && (uintptr_t)src % 2) { | ||
710 | /* Read the unaligned first byte */ | ||
711 | *dest++ = eeprom_read_byte(src++); | ||
712 | --len; | ||
713 | } | ||
714 | |||
715 | uint16_t value; | ||
716 | bool aligned = ((uintptr_t)dest % 2 == 0); | ||
717 | while (len > 1) { | ||
718 | value = eeprom_read_word((uint16_t *)src); | ||
719 | if (aligned) { | ||
720 | *(uint16_t *)dest = value; | ||
721 | dest += 2; | ||
722 | } else { | ||
723 | *dest++ = value; | ||
724 | *dest++ = value >> 8; | ||
725 | } | ||
726 | src += 2; | ||
727 | len -= 2; | ||
728 | } | ||
729 | if (len) { | ||
730 | *dest = eeprom_read_byte(src); | ||
181 | } | 731 | } |
182 | } | 732 | } |
183 | 733 | ||
184 | void eeprom_write_block(const void *buf, void *addr, size_t len) { | 734 | void eeprom_write_block(const void *buf, void *addr, size_t len) { |
185 | uint8_t * p = (uint8_t *)addr; | 735 | uint8_t * dest = (uint8_t *)addr; |
186 | const uint8_t *src = (const uint8_t *)buf; | 736 | const uint8_t *src = (const uint8_t *)buf; |
187 | while (len--) { | 737 | |
188 | eeprom_write_byte(p++, *src++); | 738 | /* Check word alignment */ |
739 | if (len && (uintptr_t)dest % 2) { | ||
740 | /* Write the unaligned first byte */ | ||
741 | eeprom_write_byte(dest++, *src++); | ||
742 | --len; | ||
189 | } | 743 | } |
190 | } | ||
191 | 744 | ||
192 | void eeprom_update_block(const void *buf, void *addr, size_t len) { | 745 | uint16_t value; |
193 | uint8_t * p = (uint8_t *)addr; | 746 | bool aligned = ((uintptr_t)src % 2 == 0); |
194 | const uint8_t *src = (const uint8_t *)buf; | 747 | while (len > 1) { |
195 | while (len--) { | 748 | if (aligned) { |
196 | eeprom_write_byte(p++, *src++); | 749 | value = *(uint16_t *)src; |
750 | } else { | ||
751 | value = *(uint8_t *)src | (*(uint8_t *)(src + 1) << 8); | ||
752 | } | ||
753 | eeprom_write_word((uint16_t *)dest, value); | ||
754 | dest += 2; | ||
755 | src += 2; | ||
756 | len -= 2; | ||
757 | } | ||
758 | |||
759 | if (len) { | ||
760 | eeprom_write_byte(dest, *src); | ||
197 | } | 761 | } |
198 | } | 762 | } |
763 | |||
764 | void eeprom_update_block(const void *buf, void *addr, size_t len) { eeprom_write_block(buf, addr, len); } | ||
diff --git a/tmk_core/common/chibios/eeprom_stm32.h b/tmk_core/common/chibios/eeprom_stm32.h index 4dac7c1b5..8fcfb556b 100644 --- a/tmk_core/common/chibios/eeprom_stm32.h +++ b/tmk_core/common/chibios/eeprom_stm32.h | |||
@@ -23,62 +23,11 @@ | |||
23 | 23 | ||
24 | #pragma once | 24 | #pragma once |
25 | 25 | ||
26 | #include <ch.h> | ||
27 | #include <hal.h> | ||
28 | #include "flash_stm32.h" | ||
29 | |||
30 | // HACK ALERT. This definition may not match your processor | ||
31 | // To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc | ||
32 | #if defined(EEPROM_EMU_STM32F303xC) | ||
33 | # define MCU_STM32F303CC | ||
34 | #elif defined(EEPROM_EMU_STM32F103xB) | ||
35 | # define MCU_STM32F103RB | ||
36 | #elif defined(EEPROM_EMU_STM32F072xB) | ||
37 | # define MCU_STM32F072CB | ||
38 | #elif defined(EEPROM_EMU_STM32F042x6) | ||
39 | # define MCU_STM32F042K6 | ||
40 | #else | ||
41 | # error "not implemented." | ||
42 | #endif | ||
43 | |||
44 | #ifndef EEPROM_PAGE_SIZE | ||
45 | # if defined(MCU_STM32F103RB) || defined(MCU_STM32F042K6) | ||
46 | # define FEE_PAGE_SIZE (uint16_t)0x400 // Page size = 1KByte | ||
47 | # define FEE_DENSITY_PAGES 2 // How many pages are used | ||
48 | # elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) || defined(MCU_STM32F103RD) || defined(MCU_STM32F303CC) || defined(MCU_STM32F072CB) | ||
49 | # define FEE_PAGE_SIZE (uint16_t)0x800 // Page size = 2KByte | ||
50 | # define FEE_DENSITY_PAGES 4 // How many pages are used | ||
51 | # else | ||
52 | # error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." | ||
53 | # endif | ||
54 | #endif | ||
55 | |||
56 | #ifndef EEPROM_START_ADDRESS | ||
57 | # if defined(MCU_STM32F103RB) || defined(MCU_STM32F072CB) | ||
58 | # define FEE_MCU_FLASH_SIZE 128 // Size in Kb | ||
59 | # elif defined(MCU_STM32F042K6) | ||
60 | # define FEE_MCU_FLASH_SIZE 32 // Size in Kb | ||
61 | # elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) | ||
62 | # define FEE_MCU_FLASH_SIZE 512 // Size in Kb | ||
63 | # elif defined(MCU_STM32F103RD) | ||
64 | # define FEE_MCU_FLASH_SIZE 384 // Size in Kb | ||
65 | # elif defined(MCU_STM32F303CC) | ||
66 | # define FEE_MCU_FLASH_SIZE 256 // Size in Kb | ||
67 | # else | ||
68 | # error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." | ||
69 | # endif | ||
70 | #endif | ||
71 | |||
72 | // DONT CHANGE | ||
73 | // Choose location for the first EEPROM Page address on the top of flash | ||
74 | #define FEE_PAGE_BASE_ADDRESS ((uint32_t)(0x8000000 + FEE_MCU_FLASH_SIZE * 1024 - FEE_DENSITY_PAGES * FEE_PAGE_SIZE)) | ||
75 | #define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 2) * FEE_DENSITY_PAGES - 1) | ||
76 | #define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) | ||
77 | #define FEE_EMPTY_WORD ((uint16_t)0xFFFF) | ||
78 | #define FEE_ADDR_OFFSET(Address) (Address * 2) // 1Byte per Word will be saved to preserve Flash | ||
79 | |||
80 | // Use this function to initialize the functionality | ||
81 | uint16_t EEPROM_Init(void); | 26 | uint16_t EEPROM_Init(void); |
82 | void EEPROM_Erase(void); | 27 | void EEPROM_Erase(void); |
83 | uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte); | 28 | uint8_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte); |
29 | uint8_t EEPROM_WriteDataWord(uint16_t Address, uint16_t DataWord); | ||
84 | uint8_t EEPROM_ReadDataByte(uint16_t Address); | 30 | uint8_t EEPROM_ReadDataByte(uint16_t Address); |
31 | uint16_t EEPROM_ReadDataWord(uint16_t Address); | ||
32 | |||
33 | void print_eeprom(void); | ||
diff --git a/tmk_core/common/chibios/flash_stm32.h b/tmk_core/common/chibios/flash_stm32.h index 90d5bff47..9c6a7cc50 100644 --- a/tmk_core/common/chibios/flash_stm32.h +++ b/tmk_core/common/chibios/flash_stm32.h | |||
@@ -22,8 +22,11 @@ | |||
22 | extern "C" { | 22 | extern "C" { |
23 | #endif | 23 | #endif |
24 | 24 | ||
25 | #include <ch.h> | 25 | #include <stdint.h> |
26 | #include <hal.h> | 26 | |
27 | #ifdef FLASH_STM32_MOCKED | ||
28 | extern uint8_t FlashBuf[MOCK_FLASH_SIZE]; | ||
29 | #endif | ||
27 | 30 | ||
28 | typedef enum { FLASH_BUSY = 1, FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_ERROR_OPT, FLASH_COMPLETE, FLASH_TIMEOUT, FLASH_BAD_ADDRESS } FLASH_Status; | 31 | typedef enum { FLASH_BUSY = 1, FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_ERROR_OPT, FLASH_COMPLETE, FLASH_TIMEOUT, FLASH_BAD_ADDRESS } FLASH_Status; |
29 | 32 | ||
diff --git a/tmk_core/common/test/eeprom_stm32_tests.cpp b/tmk_core/common/test/eeprom_stm32_tests.cpp new file mode 100644 index 000000000..aa84492b8 --- /dev/null +++ b/tmk_core/common/test/eeprom_stm32_tests.cpp | |||
@@ -0,0 +1,438 @@ | |||
1 | /* Copyright 2021 by Don Kjer | ||
2 | * | ||
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 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include "gtest/gtest.h" | ||
18 | |||
19 | extern "C" { | ||
20 | #include "flash_stm32.h" | ||
21 | #include "eeprom_stm32.h" | ||
22 | #include "eeprom.h" | ||
23 | } | ||
24 | |||
25 | /* Mock Flash Parameters: | ||
26 | * | ||
27 | * === Large Layout === | ||
28 | * flash size: 65536 | ||
29 | * page size: 2048 | ||
30 | * density pages: 16 | ||
31 | * Simulated EEPROM size: 16384 | ||
32 | * | ||
33 | * FlashBuf Layout: | ||
34 | * [Unused | Compact | Write Log ] | ||
35 | * [0......|32768......|49152......65535] | ||
36 | * | ||
37 | * === Tiny Layout === | ||
38 | * flash size: 1024 | ||
39 | * page size: 512 | ||
40 | * density pages: 1 | ||
41 | * Simulated EEPROM size: 256 | ||
42 | * | ||
43 | * FlashBuf Layout: | ||
44 | * [Unused | Compact | Write Log ] | ||
45 | * [0......|512......|768......1023] | ||
46 | * | ||
47 | */ | ||
48 | |||
49 | #define EEPROM_SIZE (FEE_PAGE_SIZE * FEE_DENSITY_PAGES / 2) | ||
50 | #define LOG_SIZE EEPROM_SIZE | ||
51 | #define LOG_BASE (MOCK_FLASH_SIZE - LOG_SIZE) | ||
52 | #define EEPROM_BASE (LOG_BASE - EEPROM_SIZE) | ||
53 | |||
54 | /* Log encoding helpers */ | ||
55 | #define BYTE_VALUE(addr, value) (((addr) << 8) | (value)) | ||
56 | #define WORD_ZERO(addr) (0x8000 | ((addr) >> 1)) | ||
57 | #define WORD_ONE(addr) (0xA000 | ((addr) >> 1)) | ||
58 | #define WORD_NEXT(addr) (0xE000 | (((addr)-0x80) >> 1)) | ||
59 | |||
60 | class EepromStm32Test : public testing::Test { | ||
61 | public: | ||
62 | EepromStm32Test() {} | ||
63 | ~EepromStm32Test() {} | ||
64 | |||
65 | protected: | ||
66 | void SetUp() override { EEPROM_Erase(); } | ||
67 | |||
68 | void TearDown() override { | ||
69 | #ifdef EEPROM_DEBUG | ||
70 | dumpEepromDataBuf(); | ||
71 | #endif | ||
72 | } | ||
73 | }; | ||
74 | |||
75 | TEST_F(EepromStm32Test, TestErase) { | ||
76 | EEPROM_WriteDataByte(0, 0x42); | ||
77 | EEPROM_Erase(); | ||
78 | EXPECT_EQ(EEPROM_ReadDataByte(0), 0); | ||
79 | EXPECT_EQ(EEPROM_ReadDataByte(1), 0); | ||
80 | } | ||
81 | |||
82 | TEST_F(EepromStm32Test, TestReadGarbage) { | ||
83 | uint8_t garbage = 0x3c; | ||
84 | for (int i = 0; i < MOCK_FLASH_SIZE; ++i) { | ||
85 | garbage ^= 0xa3; | ||
86 | garbage += i; | ||
87 | FlashBuf[i] = garbage; | ||
88 | } | ||
89 | EEPROM_Init(); // Just verify we don't crash | ||
90 | } | ||
91 | |||
92 | TEST_F(EepromStm32Test, TestWriteBadAddress) { | ||
93 | EXPECT_EQ(EEPROM_WriteDataByte(EEPROM_SIZE, 0x42), FLASH_BAD_ADDRESS); | ||
94 | EXPECT_EQ(EEPROM_WriteDataWord(EEPROM_SIZE - 1, 0xbeef), FLASH_BAD_ADDRESS); | ||
95 | EXPECT_EQ(EEPROM_WriteDataWord(EEPROM_SIZE, 0xbeef), FLASH_BAD_ADDRESS); | ||
96 | } | ||
97 | |||
98 | TEST_F(EepromStm32Test, TestReadBadAddress) { | ||
99 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE), 0xFF); | ||
100 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 1), 0xFFFF); | ||
101 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE), 0xFFFF); | ||
102 | EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 4)), 0); | ||
103 | EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 3)), 0xFF000000); | ||
104 | EXPECT_EQ(eeprom_read_dword((uint32_t*)EEPROM_SIZE), 0xFFFFFFFF); | ||
105 | } | ||
106 | |||
107 | TEST_F(EepromStm32Test, TestReadByte) { | ||
108 | /* Direct compacted-area baseline: Address < 0x80 */ | ||
109 | FlashBuf[EEPROM_BASE + 2] = ~0xef; | ||
110 | FlashBuf[EEPROM_BASE + 3] = ~0xbe; | ||
111 | /* Direct compacted-area baseline: Address >= 0x80 */ | ||
112 | FlashBuf[EEPROM_BASE + EEPROM_SIZE - 2] = ~0x78; | ||
113 | FlashBuf[EEPROM_BASE + EEPROM_SIZE - 1] = ~0x56; | ||
114 | /* Check values */ | ||
115 | EEPROM_Init(); | ||
116 | EXPECT_EQ(EEPROM_ReadDataByte(2), 0xef); | ||
117 | EXPECT_EQ(EEPROM_ReadDataByte(3), 0xbe); | ||
118 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 2), 0x78); | ||
119 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 1), 0x56); | ||
120 | /* Write Log byte value */ | ||
121 | FlashBuf[LOG_BASE] = 0x65; | ||
122 | FlashBuf[LOG_BASE + 1] = 3; | ||
123 | /* Write Log word value */ | ||
124 | *(uint16_t*)&FlashBuf[LOG_BASE + 2] = WORD_NEXT(EEPROM_SIZE - 2); | ||
125 | *(uint16_t*)&FlashBuf[LOG_BASE + 4] = ~0x9abc; | ||
126 | /* Check values */ | ||
127 | EEPROM_Init(); | ||
128 | EXPECT_EQ(EEPROM_ReadDataByte(2), 0xef); | ||
129 | EXPECT_EQ(EEPROM_ReadDataByte(3), 0x65); | ||
130 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 2), 0xbc); | ||
131 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 1), 0x9a); | ||
132 | } | ||
133 | |||
134 | TEST_F(EepromStm32Test, TestWriteByte) { | ||
135 | /* Direct compacted-area baseline: Address < 0x80 */ | ||
136 | EEPROM_WriteDataByte(2, 0xef); | ||
137 | EEPROM_WriteDataByte(3, 0xbe); | ||
138 | /* Direct compacted-area baseline: Address >= 0x80 */ | ||
139 | EEPROM_WriteDataByte(EEPROM_SIZE - 2, 0x78); | ||
140 | EEPROM_WriteDataByte(EEPROM_SIZE - 1, 0x56); | ||
141 | /* Check values */ | ||
142 | /* First write in each aligned word should have been direct */ | ||
143 | EXPECT_EQ(FlashBuf[EEPROM_BASE + 2], (uint8_t)~0xef); | ||
144 | EXPECT_EQ(FlashBuf[EEPROM_BASE + EEPROM_SIZE - 2], (uint8_t)~0x78); | ||
145 | |||
146 | /* Second write per aligned word requires a log entry */ | ||
147 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE], BYTE_VALUE(3, 0xbe)); | ||
148 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 2], WORD_NEXT(EEPROM_SIZE - 1)); | ||
149 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 4], (uint16_t)~0x5678); | ||
150 | } | ||
151 | |||
152 | TEST_F(EepromStm32Test, TestByteRoundTrip) { | ||
153 | /* Direct compacted-area: Address < 0x80 */ | ||
154 | EEPROM_WriteDataWord(0, 0xdead); | ||
155 | EEPROM_WriteDataByte(2, 0xef); | ||
156 | EEPROM_WriteDataByte(3, 0xbe); | ||
157 | /* Direct compacted-area: Address >= 0x80 */ | ||
158 | EEPROM_WriteDataByte(EEPROM_SIZE - 2, 0x78); | ||
159 | EEPROM_WriteDataByte(EEPROM_SIZE - 1, 0x56); | ||
160 | /* Check values */ | ||
161 | EEPROM_Init(); | ||
162 | EXPECT_EQ(EEPROM_ReadDataByte(0), 0xad); | ||
163 | EXPECT_EQ(EEPROM_ReadDataByte(1), 0xde); | ||
164 | EXPECT_EQ(EEPROM_ReadDataByte(2), 0xef); | ||
165 | EXPECT_EQ(EEPROM_ReadDataByte(3), 0xbe); | ||
166 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 2), 0x78); | ||
167 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 1), 0x56); | ||
168 | /* Write log entries */ | ||
169 | EEPROM_WriteDataByte(2, 0x80); | ||
170 | EEPROM_WriteDataByte(EEPROM_SIZE - 2, 0x3c); | ||
171 | /* Check values */ | ||
172 | EEPROM_Init(); | ||
173 | EXPECT_EQ(EEPROM_ReadDataByte(2), 0x80); | ||
174 | EXPECT_EQ(EEPROM_ReadDataByte(3), 0xbe); | ||
175 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 2), 0x3c); | ||
176 | EXPECT_EQ(EEPROM_ReadDataByte(EEPROM_SIZE - 1), 0x56); | ||
177 | } | ||
178 | |||
179 | TEST_F(EepromStm32Test, TestReadWord) { | ||
180 | /* Direct compacted-area baseline: Address < 0x80 */ | ||
181 | FlashBuf[EEPROM_BASE + 0] = ~0xad; | ||
182 | FlashBuf[EEPROM_BASE + 1] = ~0xde; | ||
183 | /* Direct compacted-area baseline: Address >= 0x80 */ | ||
184 | FlashBuf[EEPROM_BASE + 200] = ~0xcd; | ||
185 | FlashBuf[EEPROM_BASE + 201] = ~0xab; | ||
186 | FlashBuf[EEPROM_BASE + EEPROM_SIZE - 4] = ~0x34; | ||
187 | FlashBuf[EEPROM_BASE + EEPROM_SIZE - 3] = ~0x12; | ||
188 | FlashBuf[EEPROM_BASE + EEPROM_SIZE - 2] = ~0x78; | ||
189 | FlashBuf[EEPROM_BASE + EEPROM_SIZE - 1] = ~0x56; | ||
190 | /* Check values */ | ||
191 | EEPROM_Init(); | ||
192 | EXPECT_EQ(EEPROM_ReadDataWord(0), 0xdead); | ||
193 | EXPECT_EQ(EEPROM_ReadDataWord(200), 0xabcd); | ||
194 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 4), 0x1234); | ||
195 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 2), 0x5678); | ||
196 | /* Write Log word zero-encoded */ | ||
197 | *(uint16_t*)&FlashBuf[LOG_BASE] = WORD_ZERO(200); | ||
198 | /* Write Log word one-encoded */ | ||
199 | *(uint16_t*)&FlashBuf[LOG_BASE + 2] = WORD_ONE(EEPROM_SIZE - 4); | ||
200 | /* Write Log word value */ | ||
201 | *(uint16_t*)&FlashBuf[LOG_BASE + 4] = WORD_NEXT(EEPROM_SIZE - 2); | ||
202 | *(uint16_t*)&FlashBuf[LOG_BASE + 6] = ~0x9abc; | ||
203 | /* Check values */ | ||
204 | EEPROM_Init(); | ||
205 | EXPECT_EQ(EEPROM_ReadDataWord(200), 0); | ||
206 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 4), 1); | ||
207 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 2), 0x9abc); | ||
208 | } | ||
209 | |||
210 | TEST_F(EepromStm32Test, TestWriteWord) { | ||
211 | /* Direct compacted-area: Address < 0x80 */ | ||
212 | EEPROM_WriteDataWord(0, 0xdead); // Aligned | ||
213 | EEPROM_WriteDataWord(3, 0xbeef); // Unaligned | ||
214 | /* Direct compacted-area: Address >= 0x80 */ | ||
215 | EEPROM_WriteDataWord(200, 0xabcd); // Aligned | ||
216 | EEPROM_WriteDataWord(203, 0x9876); // Unaligned | ||
217 | EEPROM_WriteDataWord(EEPROM_SIZE - 4, 0x1234); | ||
218 | EEPROM_WriteDataWord(EEPROM_SIZE - 2, 0x5678); | ||
219 | /* Write Log word zero-encoded */ | ||
220 | EEPROM_WriteDataWord(EEPROM_SIZE - 4, 0); | ||
221 | /* Write Log word one-encoded */ | ||
222 | EEPROM_WriteDataWord(EEPROM_SIZE - 2, 1); | ||
223 | /* Write Log word value aligned */ | ||
224 | EEPROM_WriteDataWord(200, 0x4321); // Aligned | ||
225 | /* Write Log word value unaligned */ | ||
226 | EEPROM_WriteDataByte(202, 0x3c); // Set neighboring byte | ||
227 | EEPROM_WriteDataWord(203, 0xcdef); // Unaligned | ||
228 | /* Check values */ | ||
229 | /* Direct compacted-area */ | ||
230 | EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE], (uint16_t)~0xdead); | ||
231 | EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE + 3], (uint16_t)~0xbeef); | ||
232 | EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE + 200], (uint16_t)~0xabcd); | ||
233 | EXPECT_EQ(FlashBuf[EEPROM_BASE + 203], (uint8_t)~0x76); | ||
234 | EXPECT_EQ(FlashBuf[EEPROM_BASE + 204], (uint8_t)~0x98); | ||
235 | EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE + EEPROM_SIZE - 4], (uint16_t)~0x1234); | ||
236 | EXPECT_EQ(*(uint16_t*)&FlashBuf[EEPROM_BASE + EEPROM_SIZE - 2], (uint16_t)~0x5678); | ||
237 | /* Write Log word zero-encoded */ | ||
238 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE], WORD_ZERO(EEPROM_SIZE - 4)); | ||
239 | /* Write Log word one-encoded */ | ||
240 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 2], WORD_ONE(EEPROM_SIZE - 2)); | ||
241 | /* Write Log word value aligned */ | ||
242 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 4], WORD_NEXT(200)); | ||
243 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 6], (uint16_t)~0x4321); | ||
244 | /* Write Log word value unaligned */ | ||
245 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 8], WORD_NEXT(202)); | ||
246 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 10], (uint16_t)~0x763c); | ||
247 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 12], WORD_NEXT(202)); | ||
248 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 14], (uint16_t)~0xef3c); | ||
249 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 16], WORD_NEXT(204)); | ||
250 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 18], (uint16_t)~0x00cd); | ||
251 | } | ||
252 | |||
253 | TEST_F(EepromStm32Test, TestWordRoundTrip) { | ||
254 | /* Direct compacted-area: Address < 0x80 */ | ||
255 | EEPROM_WriteDataWord(0, 0xdead); // Aligned | ||
256 | EEPROM_WriteDataWord(3, 0xbeef); // Unaligned | ||
257 | /* Direct compacted-area: Address >= 0x80 */ | ||
258 | EEPROM_WriteDataWord(200, 0xabcd); // Aligned | ||
259 | EEPROM_WriteDataWord(203, 0x9876); // Unaligned | ||
260 | EEPROM_WriteDataWord(EEPROM_SIZE - 4, 0x1234); | ||
261 | EEPROM_WriteDataWord(EEPROM_SIZE - 2, 0x5678); | ||
262 | /* Check values */ | ||
263 | EEPROM_Init(); | ||
264 | EXPECT_EQ(EEPROM_ReadDataWord(0), 0xdead); | ||
265 | EXPECT_EQ(EEPROM_ReadDataWord(3), 0xbeef); | ||
266 | EXPECT_EQ(EEPROM_ReadDataWord(200), 0xabcd); | ||
267 | EXPECT_EQ(EEPROM_ReadDataWord(203), 0x9876); | ||
268 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 4), 0x1234); | ||
269 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 2), 0x5678); | ||
270 | |||
271 | /* Write Log word zero-encoded */ | ||
272 | EEPROM_WriteDataWord(EEPROM_SIZE - 4, 0); | ||
273 | /* Write Log word one-encoded */ | ||
274 | EEPROM_WriteDataWord(EEPROM_SIZE - 2, 1); | ||
275 | /* Write Log word value aligned */ | ||
276 | EEPROM_WriteDataWord(200, 0x4321); // Aligned | ||
277 | /* Write Log word value unaligned */ | ||
278 | EEPROM_WriteDataByte(202, 0x3c); // Set neighboring byte | ||
279 | EEPROM_WriteDataWord(203, 0xcdef); // Unaligned | ||
280 | /* Check values */ | ||
281 | EEPROM_Init(); | ||
282 | EXPECT_EQ(EEPROM_ReadDataWord(200), 0x4321); | ||
283 | EXPECT_EQ(EEPROM_ReadDataByte(202), 0x3c); | ||
284 | EXPECT_EQ(EEPROM_ReadDataWord(203), 0xcdef); | ||
285 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 4), 0); | ||
286 | EXPECT_EQ(EEPROM_ReadDataWord(EEPROM_SIZE - 2), 1); | ||
287 | } | ||
288 | |||
289 | TEST_F(EepromStm32Test, TestByteWordBoundary) { | ||
290 | /* Direct compacted-area write */ | ||
291 | EEPROM_WriteDataWord(0x7e, 0xdead); | ||
292 | EEPROM_WriteDataWord(0x80, 0xbeef); | ||
293 | /* Byte log entry */ | ||
294 | EEPROM_WriteDataByte(0x7f, 0x3c); | ||
295 | /* Word log entry */ | ||
296 | EEPROM_WriteDataByte(0x80, 0x18); | ||
297 | /* Check values */ | ||
298 | EEPROM_Init(); | ||
299 | EXPECT_EQ(EEPROM_ReadDataWord(0x7e), 0x3cad); | ||
300 | EXPECT_EQ(EEPROM_ReadDataWord(0x80), 0xbe18); | ||
301 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE], BYTE_VALUE(0x7f, 0x3c)); | ||
302 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 2], WORD_NEXT(0x80)); | ||
303 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 4], (uint16_t)~0xbe18); | ||
304 | /* Byte log entries */ | ||
305 | EEPROM_WriteDataWord(0x7e, 0xcafe); | ||
306 | /* Check values */ | ||
307 | EEPROM_Init(); | ||
308 | EXPECT_EQ(EEPROM_ReadDataWord(0x7e), 0xcafe); | ||
309 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 6], BYTE_VALUE(0x7e, 0xfe)); | ||
310 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 8], BYTE_VALUE(0x7f, 0xca)); | ||
311 | /* Byte and Word log entries */ | ||
312 | EEPROM_WriteDataWord(0x7f, 0xba5e); | ||
313 | /* Check values */ | ||
314 | EEPROM_Init(); | ||
315 | EXPECT_EQ(EEPROM_ReadDataWord(0x7f), 0xba5e); | ||
316 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 10], BYTE_VALUE(0x7f, 0x5e)); | ||
317 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 12], WORD_NEXT(0x80)); | ||
318 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 14], (uint16_t)~0xbeba); | ||
319 | /* Word log entry */ | ||
320 | EEPROM_WriteDataWord(0x80, 0xf00d); | ||
321 | /* Check values */ | ||
322 | EEPROM_Init(); | ||
323 | EXPECT_EQ(EEPROM_ReadDataWord(0x80), 0xf00d); | ||
324 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 16], WORD_NEXT(0x80)); | ||
325 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + 18], (uint16_t)~0xf00d); | ||
326 | } | ||
327 | |||
328 | TEST_F(EepromStm32Test, TestDWordRoundTrip) { | ||
329 | /* Direct compacted-area: Address < 0x80 */ | ||
330 | eeprom_write_dword((uint32_t*)0, 0xdeadbeef); // Aligned | ||
331 | eeprom_write_dword((uint32_t*)9, 0x12345678); // Unaligned | ||
332 | /* Direct compacted-area: Address >= 0x80 */ | ||
333 | eeprom_write_dword((uint32_t*)200, 0xfacef00d); | ||
334 | eeprom_write_dword((uint32_t*)(EEPROM_SIZE - 4), 0xba5eba11); // Aligned | ||
335 | eeprom_write_dword((uint32_t*)(EEPROM_SIZE - 9), 0xcafed00d); // Unaligned | ||
336 | /* Check direct values */ | ||
337 | EEPROM_Init(); | ||
338 | EXPECT_EQ(eeprom_read_dword((uint32_t*)0), 0xdeadbeef); | ||
339 | EXPECT_EQ(eeprom_read_dword((uint32_t*)9), 0x12345678); | ||
340 | EXPECT_EQ(eeprom_read_dword((uint32_t*)200), 0xfacef00d); | ||
341 | EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 4)), 0xba5eba11); // Aligned | ||
342 | EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 9)), 0xcafed00d); // Unaligned | ||
343 | /* Write Log byte encoded */ | ||
344 | eeprom_write_dword((uint32_t*)0, 0xdecafbad); | ||
345 | eeprom_write_dword((uint32_t*)9, 0x87654321); | ||
346 | /* Write Log word encoded */ | ||
347 | eeprom_write_dword((uint32_t*)200, 1); | ||
348 | /* Write Log word value aligned */ | ||
349 | eeprom_write_dword((uint32_t*)(EEPROM_SIZE - 4), 0xdeadc0de); // Aligned | ||
350 | eeprom_write_dword((uint32_t*)(EEPROM_SIZE - 9), 0x6789abcd); // Unaligned | ||
351 | /* Check log values */ | ||
352 | EEPROM_Init(); | ||
353 | EXPECT_EQ(eeprom_read_dword((uint32_t*)0), 0xdecafbad); | ||
354 | EXPECT_EQ(eeprom_read_dword((uint32_t*)9), 0x87654321); | ||
355 | EXPECT_EQ(eeprom_read_dword((uint32_t*)200), 1); | ||
356 | EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 4)), 0xdeadc0de); // Aligned | ||
357 | EXPECT_EQ(eeprom_read_dword((uint32_t*)(EEPROM_SIZE - 9)), 0x6789abcd); // Unaligned | ||
358 | } | ||
359 | |||
360 | TEST_F(EepromStm32Test, TestBlockRoundTrip) { | ||
361 | char src0[] = "0123456789abcdef"; | ||
362 | void* src1 = (void*)&src0[1]; | ||
363 | /* Various alignments of src & dst, Address < 0x80 */ | ||
364 | eeprom_write_block(src0, (void*)0, sizeof(src0)); | ||
365 | eeprom_write_block(src0, (void*)21, sizeof(src0)); | ||
366 | eeprom_write_block(src1, (void*)40, sizeof(src0) - 1); | ||
367 | eeprom_write_block(src1, (void*)61, sizeof(src0) - 1); | ||
368 | /* Various alignments of src & dst, Address >= 0x80 */ | ||
369 | eeprom_write_block(src0, (void*)140, sizeof(src0)); | ||
370 | eeprom_write_block(src0, (void*)161, sizeof(src0)); | ||
371 | eeprom_write_block(src1, (void*)180, sizeof(src0) - 1); | ||
372 | eeprom_write_block(src1, (void*)201, sizeof(src0) - 1); | ||
373 | |||
374 | /* Check values */ | ||
375 | EEPROM_Init(); | ||
376 | |||
377 | char dstBuf[256] = {0}; | ||
378 | char* dst0a = (char*)dstBuf; | ||
379 | char* dst0b = (char*)&dstBuf[20]; | ||
380 | char* dst1a = (char*)&dstBuf[41]; | ||
381 | char* dst1b = (char*)&dstBuf[61]; | ||
382 | char* dst0c = (char*)&dstBuf[80]; | ||
383 | char* dst0d = (char*)&dstBuf[100]; | ||
384 | char* dst1c = (char*)&dstBuf[121]; | ||
385 | char* dst1d = (char*)&dstBuf[141]; | ||
386 | eeprom_read_block((void*)dst0a, (void*)0, sizeof(src0)); | ||
387 | eeprom_read_block((void*)dst0b, (void*)21, sizeof(src0)); | ||
388 | eeprom_read_block((void*)dst1a, (void*)40, sizeof(src0) - 1); | ||
389 | eeprom_read_block((void*)dst1b, (void*)61, sizeof(src0) - 1); | ||
390 | eeprom_read_block((void*)dst0c, (void*)140, sizeof(src0)); | ||
391 | eeprom_read_block((void*)dst0d, (void*)161, sizeof(src0)); | ||
392 | eeprom_read_block((void*)dst1c, (void*)180, sizeof(src0) - 1); | ||
393 | eeprom_read_block((void*)dst1d, (void*)201, sizeof(src0) - 1); | ||
394 | EXPECT_EQ(strcmp((char*)src0, dst0a), 0); | ||
395 | EXPECT_EQ(strcmp((char*)src0, dst0b), 0); | ||
396 | EXPECT_EQ(strcmp((char*)src0, dst0c), 0); | ||
397 | EXPECT_EQ(strcmp((char*)src0, dst0d), 0); | ||
398 | EXPECT_EQ(strcmp((char*)src1, dst1a), 0); | ||
399 | EXPECT_EQ(strcmp((char*)src1, dst1b), 0); | ||
400 | EXPECT_EQ(strcmp((char*)src1, dst1c), 0); | ||
401 | EXPECT_EQ(strcmp((char*)src1, dst1d), 0); | ||
402 | } | ||
403 | |||
404 | TEST_F(EepromStm32Test, TestCompaction) { | ||
405 | /* Direct writes */ | ||
406 | eeprom_write_dword((uint32_t*)0, 0xdeadbeef); | ||
407 | eeprom_write_byte((uint8_t*)4, 0x3c); | ||
408 | eeprom_write_word((uint16_t*)6, 0xd00d); | ||
409 | eeprom_write_dword((uint32_t*)150, 0xcafef00d); | ||
410 | eeprom_write_dword((uint32_t*)200, 0x12345678); | ||
411 | /* Fill write log entries */ | ||
412 | uint32_t i; | ||
413 | uint32_t val = 0xd8453c6b; | ||
414 | for (i = 0; i < (LOG_SIZE / (sizeof(uint32_t) * 2)); i++) { | ||
415 | val ^= 0x593ca5b3; | ||
416 | val += i; | ||
417 | eeprom_write_dword((uint32_t*)200, val); | ||
418 | } | ||
419 | /* Check values pre-compaction */ | ||
420 | EEPROM_Init(); | ||
421 | EXPECT_EQ(eeprom_read_dword((uint32_t*)0), 0xdeadbeef); | ||
422 | EXPECT_EQ(eeprom_read_byte((uint8_t*)4), 0x3c); | ||
423 | EXPECT_EQ(eeprom_read_word((uint16_t*)6), 0xd00d); | ||
424 | EXPECT_EQ(eeprom_read_dword((uint32_t*)150), 0xcafef00d); | ||
425 | EXPECT_EQ(eeprom_read_dword((uint32_t*)200), val); | ||
426 | EXPECT_NE(*(uint16_t*)&FlashBuf[LOG_BASE], 0xFFFF); | ||
427 | EXPECT_NE(*(uint16_t*)&FlashBuf[LOG_BASE + LOG_SIZE - 2], 0xFFFF); | ||
428 | /* Run compaction */ | ||
429 | eeprom_write_byte((uint8_t*)4, 0x1f); | ||
430 | EEPROM_Init(); | ||
431 | EXPECT_EQ(eeprom_read_dword((uint32_t*)0), 0xdeadbeef); | ||
432 | EXPECT_EQ(eeprom_read_byte((uint8_t*)4), 0x1f); | ||
433 | EXPECT_EQ(eeprom_read_word((uint16_t*)6), 0xd00d); | ||
434 | EXPECT_EQ(eeprom_read_dword((uint32_t*)150), 0xcafef00d); | ||
435 | EXPECT_EQ(eeprom_read_dword((uint32_t*)200), val); | ||
436 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE], 0xFFFF); | ||
437 | EXPECT_EQ(*(uint16_t*)&FlashBuf[LOG_BASE + LOG_SIZE - 2], 0xFFFF); | ||
438 | } | ||
diff --git a/tmk_core/common/test/flash_stm32_mock.c b/tmk_core/common/test/flash_stm32_mock.c new file mode 100644 index 000000000..1b81d81f9 --- /dev/null +++ b/tmk_core/common/test/flash_stm32_mock.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* Copyright 2021 by Don Kjer | ||
2 | * | ||
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 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include <string.h> | ||
18 | #include <stdbool.h> | ||
19 | #include "flash_stm32.h" | ||
20 | |||
21 | uint8_t FlashBuf[MOCK_FLASH_SIZE] = {0}; | ||
22 | |||
23 | static bool flash_locked = true; | ||
24 | |||
25 | FLASH_Status FLASH_ErasePage(uint32_t Page_Address) { | ||
26 | if (flash_locked) return FLASH_ERROR_WRP; | ||
27 | Page_Address -= (uintptr_t)FlashBuf; | ||
28 | Page_Address -= (Page_Address % FEE_PAGE_SIZE); | ||
29 | if (Page_Address >= MOCK_FLASH_SIZE) return FLASH_BAD_ADDRESS; | ||
30 | memset(&FlashBuf[Page_Address], '\xff', FEE_PAGE_SIZE); | ||
31 | return FLASH_COMPLETE; | ||
32 | } | ||
33 | |||
34 | FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) { | ||
35 | if (flash_locked) return FLASH_ERROR_WRP; | ||
36 | Address -= (uintptr_t)FlashBuf; | ||
37 | if (Address >= MOCK_FLASH_SIZE) return FLASH_BAD_ADDRESS; | ||
38 | uint16_t oldData = *(uint16_t*)&FlashBuf[Address]; | ||
39 | if (oldData == 0xFFFF || Data == 0) { | ||
40 | *(uint16_t*)&FlashBuf[Address] = Data; | ||
41 | return FLASH_COMPLETE; | ||
42 | } else { | ||
43 | return FLASH_ERROR_PG; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) { return FLASH_COMPLETE; } | ||
48 | void FLASH_Unlock(void) { flash_locked = false; } | ||
49 | void FLASH_Lock(void) { flash_locked = true; } | ||
50 | void FLASH_ClearFlag(uint32_t FLASH_FLAG) {} | ||
diff --git a/tmk_core/common/test/rules.mk b/tmk_core/common/test/rules.mk new file mode 100644 index 000000000..e47e5880c --- /dev/null +++ b/tmk_core/common/test/rules.mk | |||
@@ -0,0 +1,23 @@ | |||
1 | eeprom_stm32_DEFS := -DFLASH_STM32_MOCKED -DNO_PRINT -DFEE_FLASH_BASE=FlashBuf | ||
2 | eeprom_stm32_tiny_DEFS := $(eeprom_stm32_DEFS) \ | ||
3 | -DFEE_MCU_FLASH_SIZE=1 \ | ||
4 | -DMOCK_FLASH_SIZE=1024 \ | ||
5 | -DFEE_PAGE_SIZE=512 \ | ||
6 | -DFEE_DENSITY_PAGES=1 | ||
7 | eeprom_stm32_large_DEFS := $(eeprom_stm32_DEFS) \ | ||
8 | -DFEE_MCU_FLASH_SIZE=64 \ | ||
9 | -DMOCK_FLASH_SIZE=65536 \ | ||
10 | -DFEE_PAGE_SIZE=2048 \ | ||
11 | -DFEE_DENSITY_PAGES=16 | ||
12 | |||
13 | eeprom_stm32_INC := \ | ||
14 | $(TMK_PATH)/common/chibios/ | ||
15 | eeprom_stm32_tiny_INC := $(eeprom_stm32_INC) | ||
16 | eeprom_stm32_large_INC := $(eeprom_stm32_INC) | ||
17 | |||
18 | eeprom_stm32_SRC := \ | ||
19 | $(TMK_PATH)/common/test/eeprom_stm32_tests.cpp \ | ||
20 | $(TMK_PATH)/common/test/flash_stm32_mock.c \ | ||
21 | $(TMK_PATH)/common/chibios/eeprom_stm32.c | ||
22 | eeprom_stm32_tiny_SRC := $(eeprom_stm32_SRC) | ||
23 | eeprom_stm32_large_SRC := $(eeprom_stm32_SRC) | ||
diff --git a/tmk_core/common/test/testlist.mk b/tmk_core/common/test/testlist.mk new file mode 100644 index 000000000..51a9638bb --- /dev/null +++ b/tmk_core/common/test/testlist.mk | |||
@@ -0,0 +1 @@ | |||
TEST_LIST += eeprom_stm32_tiny eeprom_stm32_large | |||
diff --git a/util/stm32eeprom_parser.py b/util/stm32eeprom_parser.py new file mode 100755 index 000000000..b124f713d --- /dev/null +++ b/util/stm32eeprom_parser.py | |||
@@ -0,0 +1,317 @@ | |||
1 | #!/usr/bin/env python | ||
2 | # | ||
3 | # Copyright 2021 Don Kjer | ||
4 | # | ||
5 | # This program is free software: you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License as published by | ||
7 | # the Free Software Foundation, either version 2 of the License, or | ||
8 | # (at your option) any later version. | ||
9 | # | ||
10 | # This program is distributed in the hope that it will be useful, | ||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | # GNU General Public License for more details. | ||
14 | # | ||
15 | # You should have received a copy of the GNU General Public License | ||
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | # | ||
18 | |||
19 | from __future__ import print_function | ||
20 | |||
21 | import argparse | ||
22 | from struct import pack, unpack | ||
23 | import os, sys | ||
24 | |||
25 | MAGIC_FEEA = '\xea\xff\xfe\xff' | ||
26 | |||
27 | MAGIC_FEE9 = '\x16\x01' | ||
28 | EMPTY_WORD = '\xff\xff' | ||
29 | WORD_ENCODING = 0x8000 | ||
30 | VALUE_NEXT = 0x6000 | ||
31 | VALUE_RESERVED = 0x4000 | ||
32 | VALUE_ENCODED = 0x2000 | ||
33 | BYTE_RANGE = 0x80 | ||
34 | |||
35 | CHUNK_SIZE = 1024 | ||
36 | |||
37 | STRUCT_FMTS = { | ||
38 | 1: 'B', | ||
39 | 2: 'H', | ||
40 | 4: 'I' | ||
41 | } | ||
42 | PRINTABLE='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ ' | ||
43 | |||
44 | EECONFIG_V1 = [ | ||
45 | ("MAGIC", 0, 2), | ||
46 | ("DEBUG", 2, 1), | ||
47 | ("DEFAULT_LAYER", 3, 1), | ||
48 | ("KEYMAP", 4, 1), | ||
49 | ("MOUSEKEY_ACCEL", 5, 1), | ||
50 | ("BACKLIGHT", 6, 1), | ||
51 | ("AUDIO", 7, 1), | ||
52 | ("RGBLIGHT", 8, 4), | ||
53 | ("UNICODEMODE", 12, 1), | ||
54 | ("STENOMODE", 13, 1), | ||
55 | ("HANDEDNESS", 14, 1), | ||
56 | ("KEYBOARD", 15, 4), | ||
57 | ("USER", 19, 4), | ||
58 | ("VELOCIKEY", 23, 1), | ||
59 | ("HAPTIC", 24, 4), | ||
60 | ("MATRIX", 28, 4), | ||
61 | ("MATRIX_EXTENDED", 32, 2), | ||
62 | ("KEYMAP_UPPER_BYTE", 34, 1), | ||
63 | ] | ||
64 | VIABASE_V1 = 35 | ||
65 | |||
66 | VERBOSE = False | ||
67 | |||
68 | def parseArgs(): | ||
69 | parser = argparse.ArgumentParser(description='Decode an STM32 emulated eeprom dump') | ||
70 | parser.add_argument('-s', '--size', type=int, | ||
71 | help='Size of the emulated eeprom (default: input_size / 2)') | ||
72 | parser.add_argument('-o', '--output', help='File to write decoded eeprom to') | ||
73 | parser.add_argument('-y', '--layout-options-size', type=int, | ||
74 | help='VIA layout options size (default: 1)', default=1) | ||
75 | parser.add_argument('-t', '--custom-config-size', type=int, | ||
76 | help='VIA custom config size (default: 0)', default=0) | ||
77 | parser.add_argument('-l', '--layers', type=int, | ||
78 | help='VIA keyboard layers (default: 4)', default=4) | ||
79 | parser.add_argument('-r', '--rows', type=int, help='VIA matrix rows') | ||
80 | parser.add_argument('-c', '--cols', type=int, help='VIA matrix columns') | ||
81 | parser.add_argument('-m', '--macros', type=int, | ||
82 | help='VIA macro count (default: 16)', default=16) | ||
83 | parser.add_argument('-C', '--canonical', action='store_true', | ||
84 | help='Canonical hex+ASCII display.') | ||
85 | parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output') | ||
86 | parser.add_argument('input', help='Raw contents of the STM32 flash area used to emulate eeprom') | ||
87 | return parser.parse_args() | ||
88 | |||
89 | |||
90 | def decodeEepromFEEA(in_file, size): | ||
91 | decoded=size*[None] | ||
92 | pos = 0 | ||
93 | while True: | ||
94 | chunk = in_file.read(CHUNK_SIZE) | ||
95 | for i in range(0, len(chunk), 2): | ||
96 | decoded[pos] = unpack('B', chunk[i])[0] | ||
97 | pos += 1 | ||
98 | if pos >= size: | ||
99 | break | ||
100 | |||
101 | if len(chunk) < CHUNK_SIZE or pos >= size: | ||
102 | break | ||
103 | return decoded | ||
104 | |||
105 | def decodeEepromFEE9(in_file, size): | ||
106 | decoded=size*[None] | ||
107 | pos = 0 | ||
108 | # Read compacted flash | ||
109 | while True: | ||
110 | read_size = min(size - pos, CHUNK_SIZE) | ||
111 | chunk = in_file.read(read_size) | ||
112 | for i in range(len(chunk)): | ||
113 | decoded[pos] = unpack('B', chunk[i])[0] ^ 0xFF | ||
114 | pos += 1 | ||
115 | if pos >= size: | ||
116 | break | ||
117 | |||
118 | if len(chunk) < read_size or pos >= size: | ||
119 | break | ||
120 | if VERBOSE: | ||
121 | print("COMPACTED EEPROM:") | ||
122 | dumpBinary(decoded, True) | ||
123 | print("WRITE LOG:") | ||
124 | # Read write log | ||
125 | while True: | ||
126 | entry = in_file.read(2) | ||
127 | if len(entry) < 2: | ||
128 | print("Partial log address at position 0x%04x" % pos, file=sys.stderr) | ||
129 | break | ||
130 | pos += 2 | ||
131 | |||
132 | if entry == EMPTY_WORD: | ||
133 | break | ||
134 | |||
135 | be_entry = unpack('>H', entry)[0] | ||
136 | entry = unpack('H', entry)[0] | ||
137 | if not (entry & WORD_ENCODING): | ||
138 | address = entry >> 8 | ||
139 | decoded[address] = entry & 0xFF | ||
140 | if VERBOSE: | ||
141 | print("[0x%04x]: BYTE 0x%02x = 0x%02x" % (be_entry, address, decoded[address])) | ||
142 | else: | ||
143 | if (entry & VALUE_NEXT) == VALUE_NEXT: | ||
144 | # Read next word as value | ||
145 | value = in_file.read(2) | ||
146 | if len(value) < 2: | ||
147 | print("Partial log value at position 0x%04x" % pos, file=sys.stderr) | ||
148 | break | ||
149 | pos += 2 | ||
150 | address = entry & 0x1FFF | ||
151 | address <<= 1 | ||
152 | address += BYTE_RANGE | ||
153 | decoded[address] = unpack('B', value[0])[0] ^ 0xFF | ||
154 | decoded[address+1] = unpack('B', value[1])[0] ^ 0xFF | ||
155 | be_value = unpack('>H', value)[0] | ||
156 | if VERBOSE: | ||
157 | print("[0x%04x 0x%04x]: WORD 0x%04x = 0x%02x%02x" % (be_entry, be_value, address, decoded[address+1], decoded[address])) | ||
158 | else: | ||
159 | # Reserved for future use | ||
160 | if entry & VALUE_RESERVED: | ||
161 | if VERBOSE: | ||
162 | print("[0x%04x]: RESERVED 0x%04x" % (be_entry, address)) | ||
163 | continue | ||
164 | address = entry & 0x1FFF | ||
165 | address <<= 1 | ||
166 | decoded[address] = (entry & VALUE_ENCODED) >> 13 | ||
167 | decoded[address+1] = 0 | ||
168 | if VERBOSE: | ||
169 | print("[0x%04x]: ENCODED 0x%04x = 0x%02x%02x" % (be_entry, address, decoded[address+1], decoded[address])) | ||
170 | |||
171 | return decoded | ||
172 | |||
173 | def dumpBinary(data, canonical): | ||
174 | def display(pos, row): | ||
175 | print("%04x" % pos, end='') | ||
176 | for i in range(len(row)): | ||
177 | if i % 8 == 0: | ||
178 | print(" ", end='') | ||
179 | char = row[i] | ||
180 | if char is None: | ||
181 | print(" ", end='') | ||
182 | else: | ||
183 | print(" %02x" % row[i], end='') | ||
184 | if canonical: | ||
185 | print(" |", end='') | ||
186 | for i in range(len(row)): | ||
187 | char = row[i] | ||
188 | if char is None: | ||
189 | char = " " | ||
190 | else: | ||
191 | char = chr(char) | ||
192 | if char not in PRINTABLE: | ||
193 | char = "." | ||
194 | print(char, end='') | ||
195 | print("|", end='') | ||
196 | |||
197 | print("") | ||
198 | |||
199 | size = len(data) | ||
200 | empty_rows = 0 | ||
201 | prev_row = '' | ||
202 | first_repeat = True | ||
203 | for pos in range(0, size, 16): | ||
204 | row=data[pos:pos+16] | ||
205 | row[len(row):16] = (16-len(row))*[None] | ||
206 | if row == prev_row: | ||
207 | if first_repeat: | ||
208 | print("*") | ||
209 | first_repeat = False | ||
210 | else: | ||
211 | first_repeat = True | ||
212 | display(pos, row) | ||
213 | prev_row = row | ||
214 | print("%04x" % (pos+16)) | ||
215 | |||
216 | def dumpEeconfig(data, eeconfig): | ||
217 | print("EECONFIG:") | ||
218 | for (name, pos, length) in eeconfig: | ||
219 | fmt = STRUCT_FMTS[length] | ||
220 | value = unpack(fmt, ''.join([chr(x) for x in data[pos:pos+length]]))[0] | ||
221 | print(("%%04x %%s = 0x%%0%dx" % (length * 2)) % (pos, name, value)) | ||
222 | |||
223 | def dumpVia(data, base, layers, cols, rows, macros, | ||
224 | layout_options_size, custom_config_size): | ||
225 | magicYear = data[base + 0] | ||
226 | magicMonth = data[base + 1] | ||
227 | magicDay = data[base + 2] | ||
228 | # Sanity check | ||
229 | if not 10 <= magicYear <= 0x99 or \ | ||
230 | not 0 <= magicMonth <= 0x12 or \ | ||
231 | not 0 <= magicDay <= 0x31: | ||
232 | print("ERROR: VIA Signature is not valid; Year:%x, Month:%x, Day:%x" % (magicYear, magicMonth, magicDay)) | ||
233 | return | ||
234 | if cols is None or rows is None: | ||
235 | print("ERROR: VIA dump requires specifying --rows and --cols", file=sys.stderr) | ||
236 | return 2 | ||
237 | print("VIA:") | ||
238 | # Decode magic | ||
239 | print("%04x MAGIC = 20%02x-%02x-%02x" % (base, magicYear, magicMonth, magicDay)) | ||
240 | # Decode layout options | ||
241 | options = 0 | ||
242 | pos = base + 3 | ||
243 | for i in range(base+3, base+3+layout_options_size): | ||
244 | options = options << 8 | ||
245 | options |= data[i] | ||
246 | print(("%%04x LAYOUT_OPTIONS = 0x%%0%dx" % (layout_options_size * 2)) % (pos, options)) | ||
247 | pos += layout_options_size + custom_config_size | ||
248 | # Decode keycodes | ||
249 | keymap_size = layers * rows * cols * 2 | ||
250 | if (pos + keymap_size) >= (len(data) - 1): | ||
251 | print("ERROR: VIA keymap requires %d bytes, but only %d available" % (keymap_size, len(data) - pos)) | ||
252 | return 3 | ||
253 | for layer in range(layers): | ||
254 | print("%s LAYER %d %s" % ('-'*int(cols*2.5), layer, '-'*int(cols*2.5))) | ||
255 | for row in range(rows): | ||
256 | print("%04x | " % pos, end='') | ||
257 | for col in range(cols): | ||
258 | keycode = (data[pos] << 8) | (data[pos+1]) | ||
259 | print(" %04x" % keycode, end='') | ||
260 | pos += 2 | ||
261 | print("") | ||
262 | # Decode macros | ||
263 | for macro_num in range(macros): | ||
264 | macro = "" | ||
265 | macro_pos = pos | ||
266 | while pos < len(data): | ||
267 | char = chr(data[pos]) | ||
268 | pos += 1 | ||
269 | if char == '\x00': | ||
270 | print("%04x MACRO[%d] = '%s'" % (macro_pos, macro_num, macro)) | ||
271 | break | ||
272 | else: | ||
273 | macro += char | ||
274 | return 0 | ||
275 | |||
276 | |||
277 | def decodeSTM32Eeprom(input, canonical, size=None, output=None, **kwargs): | ||
278 | input_size = os.path.getsize(input) | ||
279 | if size is None: | ||
280 | size = input_size >> 1 | ||
281 | |||
282 | # Read the first few bytes to check magic signature | ||
283 | with open(input, 'rb') as in_file: | ||
284 | magic=in_file.read(4) | ||
285 | in_file.seek(0) | ||
286 | |||
287 | if magic == MAGIC_FEEA: | ||
288 | decoded = decodeEepromFEEA(in_file, size) | ||
289 | eeconfig = EECONFIG_V1 | ||
290 | via_base = VIABASE_V1 | ||
291 | elif magic[:2] == MAGIC_FEE9: | ||
292 | decoded = decodeEepromFEE9(in_file, size) | ||
293 | eeconfig = EECONFIG_V1 | ||
294 | via_base = VIABASE_V1 | ||
295 | else: | ||
296 | print("Unknown magic signature: %s" % " ".join(["0x%02x" % ord(x) for x in magic]), file=sys.stderr) | ||
297 | return 1 | ||
298 | |||
299 | if output is not None: | ||
300 | with open(output, 'wb') as out_file: | ||
301 | out_file.write(pack('%dB' % len(decoded), *decoded)) | ||
302 | print("DECODED EEPROM:") | ||
303 | dumpBinary(decoded, canonical) | ||
304 | dumpEeconfig(decoded, eeconfig) | ||
305 | if kwargs['rows'] is not None and kwargs['cols'] is not None: | ||
306 | return dumpVia(decoded, via_base, **kwargs) | ||
307 | |||
308 | return 0 | ||
309 | |||
310 | def main(): | ||
311 | global VERBOSE | ||
312 | kwargs = vars(parseArgs()) | ||
313 | VERBOSE = kwargs.pop('verbose') | ||
314 | return decodeSTM32Eeprom(**kwargs) | ||
315 | |||
316 | if __name__ == '__main__': | ||
317 | sys.exit(main()) | ||