aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xkeyboards/nk65/config.h2
-rw-r--r--tmk_core/common/chibios/eeprom_stm32.c110
-rw-r--r--tmk_core/common/chibios/eeprom_stm32_defs.h61
-rw-r--r--tmk_core/common/test/eeprom_stm32_tests.cpp2
-rw-r--r--tmk_core/common/test/hal.h18
-rw-r--r--tmk_core/common/test/rules.mk4
6 files changed, 120 insertions, 77 deletions
diff --git a/keyboards/nk65/config.h b/keyboards/nk65/config.h
index a178307cc..3268e8ebf 100755
--- a/keyboards/nk65/config.h
+++ b/keyboards/nk65/config.h
@@ -151,7 +151,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
151#ifndef FLASHSIZE_BASE 151#ifndef FLASHSIZE_BASE
152# define FLASHSIZE_BASE ((uint32_t)0x1FFFF7CCU) /*!< FLASH Size register base address */ 152# define FLASHSIZE_BASE ((uint32_t)0x1FFFF7CCU) /*!< FLASH Size register base address */
153#endif 153#endif
154#define EEPROM_START_ADDRESS 154#define FEE_MCU_FLASH_SIZE_IGNORE_CHECK
155#define FEE_MCU_FLASH_SIZE \ 155#define FEE_MCU_FLASH_SIZE \
156({ \ 156({ \
157 uint16_t (*flash_size) = (uint16_t*)FLASHSIZE_BASE; \ 157 uint16_t (*flash_size) = (uint16_t*)FLASHSIZE_BASE; \
diff --git a/tmk_core/common/chibios/eeprom_stm32.c b/tmk_core/common/chibios/eeprom_stm32.c
index 64d7d79ba..1fdf8c1e2 100644
--- a/tmk_core/common/chibios/eeprom_stm32.c
+++ b/tmk_core/common/chibios/eeprom_stm32.c
@@ -51,13 +51,13 @@
51 * 51 *
52 * The following configuration defines can be set: 52 * The following configuration defines can be set:
53 * 53 *
54 * FEE_DENSITY_PAGES # Total number of pages to use for eeprom simulation (Compact + Write log) 54 * FEE_PAGE_COUNT # Total number of pages to use for eeprom simulation (Compact + Write log)
55 * FEE_DENSITY_BYTES # Size of simulated eeprom. (Defaults to half the space allocated by FEE_DENSITY_PAGES) 55 * FEE_DENSITY_BYTES # Size of simulated eeprom. (Defaults to half the space allocated by FEE_PAGE_COUNT)
56 * NOTE: The current implementation does not include page swapping, 56 * NOTE: The current implementation does not include page swapping,
57 * and FEE_DENSITY_BYTES will consume that amount of RAM as a cached view of actual EEPROM contents. 57 * and FEE_DENSITY_BYTES will consume that amount of RAM as a cached view of actual EEPROM contents.
58 * 58 *
59 * The maximum size of FEE_DENSITY_BYTES is currently 16384. The write log size equals 59 * The maximum size of FEE_DENSITY_BYTES is currently 16384. The write log size equals
60 * FEE_DENSITY_PAGES * FEE_PAGE_SIZE - FEE_DENSITY_BYTES. 60 * FEE_PAGE_COUNT * FEE_PAGE_SIZE - FEE_DENSITY_BYTES.
61 * The larger the write log, the less frequently the compacted area needs to be rewritten. 61 * The larger the write log, the less frequently the compacted area needs to be rewritten.
62 * 62 *
63 * 63 *
@@ -132,6 +132,11 @@
132 * 132 *
133 */ 133 */
134 134
135#include "eeprom_stm32_defs.h"
136#if !defined(FEE_PAGE_SIZE) || !defined(FEE_PAGE_COUNT) || !defined(FEE_MCU_FLASH_SIZE) || !defined(FEE_PAGE_BASE_ADDRESS)
137# error "not implemented."
138#endif
139
135/* These bits are used for optimizing encoding of bytes, 0 and 1 */ 140/* These bits are used for optimizing encoding of bytes, 0 and 1 */
136#define FEE_WORD_ENCODING 0x8000 141#define FEE_WORD_ENCODING 0x8000
137#define FEE_VALUE_NEXT 0x6000 142#define FEE_VALUE_NEXT 0x6000
@@ -139,65 +144,19 @@
139#define FEE_VALUE_ENCODED 0x2000 144#define FEE_VALUE_ENCODED 0x2000
140#define FEE_BYTE_RANGE 0x80 145#define FEE_BYTE_RANGE 0x80
141 146
142// HACK ALERT. This definition may not match your processor 147/* Addressable range 16KByte: 0 <-> (0x1FFF << 1) */
143// To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc 148#define FEE_ADDRESS_MAX_SIZE 0x4000
144#if defined(EEPROM_EMU_STM32F303xC)
145# define MCU_STM32F303CC
146#elif defined(EEPROM_EMU_STM32F103xB)
147# define MCU_STM32F103RB
148#elif defined(EEPROM_EMU_STM32F072xB)
149# define MCU_STM32F072CB
150#elif defined(EEPROM_EMU_STM32F042x6)
151# define MCU_STM32F042K6
152#elif !defined(FEE_PAGE_SIZE) || !defined(FEE_DENSITY_PAGES) || !defined(FEE_MCU_FLASH_SIZE)
153# error "not implemented."
154#endif
155
156#if !defined(FEE_PAGE_SIZE) || !defined(FEE_DENSITY_PAGES)
157# if defined(MCU_STM32F103RB) || defined(MCU_STM32F042K6)
158# ifndef FEE_PAGE_SIZE
159# define FEE_PAGE_SIZE 0x400 // Page size = 1KByte
160# endif
161# ifndef FEE_DENSITY_PAGES
162# define FEE_DENSITY_PAGES 2 // How many pages are used
163# endif
164# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) || defined(MCU_STM32F103RD) || defined(MCU_STM32F303CC) || defined(MCU_STM32F072CB)
165# ifndef FEE_PAGE_SIZE
166# define FEE_PAGE_SIZE 0x800 // Page size = 2KByte
167# endif
168# ifndef FEE_DENSITY_PAGES
169# define FEE_DENSITY_PAGES 4 // How many pages are used
170# endif
171# else
172# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
173# endif
174#endif
175 149
176#ifndef FEE_MCU_FLASH_SIZE 150/* Flash word value after erase */
177# if defined(MCU_STM32F103RB) || defined(MCU_STM32F072CB) 151#define FEE_EMPTY_WORD ((uint16_t)0xFFFF)
178# define FEE_MCU_FLASH_SIZE 128 // Size in Kb
179# elif defined(MCU_STM32F042K6)
180# define FEE_MCU_FLASH_SIZE 32 // Size in Kb
181# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE)
182# define FEE_MCU_FLASH_SIZE 512 // Size in Kb
183# elif defined(MCU_STM32F103RD)
184# define FEE_MCU_FLASH_SIZE 384 // Size in Kb
185# elif defined(MCU_STM32F303CC)
186# define FEE_MCU_FLASH_SIZE 256 // Size in Kb
187# else
188# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
189# endif
190#endif
191 152
192/* Size of combined compacted eeprom and write log pages */ 153/* Size of combined compacted eeprom and write log pages */
193#define FEE_DENSITY_MAX_SIZE (FEE_DENSITY_PAGES * FEE_PAGE_SIZE) 154#define FEE_DENSITY_MAX_SIZE (FEE_PAGE_COUNT * FEE_PAGE_SIZE)
194/* Addressable range 16KByte: 0 <-> (0x1FFF << 1) */
195#define FEE_ADDRESS_MAX_SIZE 0x4000
196 155
197#ifndef EEPROM_START_ADDRESS /* *TODO: Get rid of this check */ 156#ifndef FEE_MCU_FLASH_SIZE_IGNORE_CHECK /* *TODO: Get rid of this check */
198# if FEE_DENSITY_MAX_SIZE > (FEE_MCU_FLASH_SIZE * 1024) 157# if FEE_DENSITY_MAX_SIZE > (FEE_MCU_FLASH_SIZE * 1024)
199# pragma message STR(FEE_DENSITY_MAX_SIZE) " > " STR(FEE_MCU_FLASH_SIZE * 1024) 158# pragma message STR(FEE_DENSITY_MAX_SIZE) " > " STR(FEE_MCU_FLASH_SIZE * 1024)
200# error emulated eeprom: FEE_DENSITY_PAGES is greater than available flash size 159# error emulated eeprom: FEE_DENSITY_MAX_SIZE is greater than available flash size
201# endif 160# endif
202#endif 161#endif
203 162
@@ -220,27 +179,32 @@
220# endif 179# endif
221#else 180#else
222/* Default to half of allocated space used for emulated eeprom, half for write log */ 181/* Default to half of allocated space used for emulated eeprom, half for write log */
223# define FEE_DENSITY_BYTES (FEE_DENSITY_PAGES * FEE_PAGE_SIZE / 2) 182# define FEE_DENSITY_BYTES (FEE_PAGE_COUNT * FEE_PAGE_SIZE / 2)
224#endif 183#endif
225 184
226/* Size of write log */ 185/* Size of write log */
227#define FEE_WRITE_LOG_BYTES (FEE_DENSITY_PAGES * FEE_PAGE_SIZE - FEE_DENSITY_BYTES) 186#ifdef FEE_WRITE_LOG_BYTES
187# if ((FEE_DENSITY_BYTES + FEE_WRITE_LOG_BYTES) > FEE_DENSITY_MAX_SIZE)
188# pragma message STR(FEE_DENSITY_BYTES) " + " STR(FEE_WRITE_LOG_BYTES) " > " STR(FEE_DENSITY_MAX_SIZE)
189# error emulated eeprom: FEE_WRITE_LOG_BYTES exceeds remaining FEE_DENSITY_MAX_SIZE
190# endif
191# if ((FEE_WRITE_LOG_BYTES) % 2) == 1
192# error emulated eeprom: FEE_WRITE_LOG_BYTES must be even
193# endif
194#else
195/* Default to use all remaining space */
196# define FEE_WRITE_LOG_BYTES (FEE_PAGE_COUNT * FEE_PAGE_SIZE - FEE_DENSITY_BYTES)
197#endif
228 198
229/* Start of the emulated eeprom compacted flash area */ 199/* Start of the emulated eeprom compacted flash area */
230#ifndef FEE_FLASH_BASE 200#define FEE_COMPACTED_BASE_ADDRESS FEE_PAGE_BASE_ADDRESS
231# define FEE_FLASH_BASE 0x8000000
232#endif
233#define FEE_PAGE_BASE_ADDRESS ((uintptr_t)(FEE_FLASH_BASE) + FEE_MCU_FLASH_SIZE * 1024 - FEE_WRITE_LOG_BYTES - FEE_DENSITY_BYTES)
234/* End of the emulated eeprom compacted flash area */ 201/* End of the emulated eeprom compacted flash area */
235#define FEE_PAGE_LAST_ADDRESS (FEE_PAGE_BASE_ADDRESS + FEE_DENSITY_BYTES) 202#define FEE_COMPACTED_LAST_ADDRESS (FEE_COMPACTED_BASE_ADDRESS + FEE_DENSITY_BYTES)
236/* Start of the emulated eeprom write log */ 203/* Start of the emulated eeprom write log */
237#define FEE_WRITE_LOG_BASE_ADDRESS FEE_PAGE_LAST_ADDRESS 204#define FEE_WRITE_LOG_BASE_ADDRESS FEE_COMPACTED_LAST_ADDRESS
238/* End of the emulated eeprom write log */ 205/* End of the emulated eeprom write log */
239#define FEE_WRITE_LOG_LAST_ADDRESS (FEE_WRITE_LOG_BASE_ADDRESS + FEE_WRITE_LOG_BYTES) 206#define FEE_WRITE_LOG_LAST_ADDRESS (FEE_WRITE_LOG_BASE_ADDRESS + FEE_WRITE_LOG_BYTES)
240 207
241/* Flash word value after erase */
242#define FEE_EMPTY_WORD ((uint16_t)0xFFFF)
243
244#if defined(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) && (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR >= FEE_DENSITY_BYTES) 208#if defined(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) && (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR >= FEE_DENSITY_BYTES)
245# error emulated eeprom: DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is greater than the FEE_DENSITY_BYTES available 209# error emulated eeprom: DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is greater than the FEE_DENSITY_BYTES available
246#endif 210#endif
@@ -313,9 +277,9 @@ void print_eeprom(void) {
313 277
314uint16_t EEPROM_Init(void) { 278uint16_t EEPROM_Init(void) {
315 /* Load emulated eeprom contents from compacted flash into memory */ 279 /* Load emulated eeprom contents from compacted flash into memory */
316 uint16_t *src = (uint16_t *)FEE_PAGE_BASE_ADDRESS; 280 uint16_t *src = (uint16_t *)FEE_COMPACTED_BASE_ADDRESS;
317 uint16_t *dest = (uint16_t *)DataBuf; 281 uint16_t *dest = (uint16_t *)DataBuf;
318 for (; src < (uint16_t *)FEE_PAGE_LAST_ADDRESS; ++src, ++dest) { 282 for (; src < (uint16_t *)FEE_COMPACTED_LAST_ADDRESS; ++src, ++dest) {
319 *dest = ~*src; 283 *dest = ~*src;
320 } 284 }
321 285
@@ -390,7 +354,7 @@ uint16_t EEPROM_Init(void) {
390static void eeprom_clear(void) { 354static void eeprom_clear(void) {
391 FLASH_Unlock(); 355 FLASH_Unlock();
392 356
393 for (uint16_t page_num = 0; page_num < FEE_DENSITY_PAGES; ++page_num) { 357 for (uint16_t page_num = 0; page_num < FEE_PAGE_COUNT; ++page_num) {
394 eeprom_printf("FLASH_ErasePage(0x%04x)\n", (uint32_t)(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE))); 358 eeprom_printf("FLASH_ErasePage(0x%04x)\n", (uint32_t)(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)));
395 FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)); 359 FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE));
396 } 360 }
@@ -421,9 +385,9 @@ static uint8_t eeprom_compact(void) {
421 385
422 /* Write emulated eeprom contents from memory to compacted flash */ 386 /* Write emulated eeprom contents from memory to compacted flash */
423 uint16_t *src = (uint16_t *)DataBuf; 387 uint16_t *src = (uint16_t *)DataBuf;
424 uintptr_t dest = FEE_PAGE_BASE_ADDRESS; 388 uintptr_t dest = FEE_COMPACTED_BASE_ADDRESS;
425 uint16_t value; 389 uint16_t value;
426 for (; dest < FEE_PAGE_LAST_ADDRESS; ++src, dest += 2) { 390 for (; dest < FEE_COMPACTED_LAST_ADDRESS; ++src, dest += 2) {
427 value = *src; 391 value = *src;
428 if (value) { 392 if (value) {
429 eeprom_printf("FLASH_ProgramHalfWord(0x%04x, 0x%04x)\n", (uint32_t)dest, ~value); 393 eeprom_printf("FLASH_ProgramHalfWord(0x%04x, 0x%04x)\n", (uint32_t)dest, ~value);
@@ -444,7 +408,7 @@ static uint8_t eeprom_compact(void) {
444 408
445static uint8_t eeprom_write_direct_entry(uint16_t Address) { 409static uint8_t eeprom_write_direct_entry(uint16_t Address) {
446 /* Check if we can just write this directly to the compacted flash area */ 410 /* Check if we can just write this directly to the compacted flash area */
447 uintptr_t directAddress = FEE_PAGE_BASE_ADDRESS + (Address & 0xFFFE); 411 uintptr_t directAddress = FEE_COMPACTED_BASE_ADDRESS + (Address & 0xFFFE);
448 if (*(uint16_t *)directAddress == FEE_EMPTY_WORD) { 412 if (*(uint16_t *)directAddress == FEE_EMPTY_WORD) {
449 /* Write the value directly to the compacted area without a log entry */ 413 /* Write the value directly to the compacted area without a log entry */
450 uint16_t value = ~*(uint16_t *)(&DataBuf[Address & 0xFFFE]); 414 uint16_t value = ~*(uint16_t *)(&DataBuf[Address & 0xFFFE]);
diff --git a/tmk_core/common/chibios/eeprom_stm32_defs.h b/tmk_core/common/chibios/eeprom_stm32_defs.h
new file mode 100644
index 000000000..a775ec201
--- /dev/null
+++ b/tmk_core/common/chibios/eeprom_stm32_defs.h
@@ -0,0 +1,61 @@
1/* Copyright 2021 QMK
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 3 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#pragma once
17
18#include <hal.h>
19
20#if !defined(FEE_PAGE_SIZE) || !defined(FEE_PAGE_COUNT)
21# if defined(STM32F103xB) || defined(STM32F042x6)
22# ifndef FEE_PAGE_SIZE
23# define FEE_PAGE_SIZE 0x400 // Page size = 1KByte
24# endif
25# ifndef FEE_PAGE_COUNT
26# define FEE_PAGE_COUNT 2 // How many pages are used
27# endif
28# elif defined(STM32F103xE) || defined(STM32F303xC) || defined(STM32F072xB)
29# ifndef FEE_PAGE_SIZE
30# define FEE_PAGE_SIZE 0x800 // Page size = 2KByte
31# endif
32# ifndef FEE_PAGE_COUNT
33# define FEE_PAGE_COUNT 4 // How many pages are used
34# endif
35# endif
36#endif
37
38#if !defined(FEE_MCU_FLASH_SIZE)
39# if defined(STM32F042x6)
40# define FEE_MCU_FLASH_SIZE 32 // Size in Kb
41# elif defined(STM32F103xB) || defined(STM32F072xB)
42# define FEE_MCU_FLASH_SIZE 128 // Size in Kb
43# elif defined(STM32F303xC)
44# define FEE_MCU_FLASH_SIZE 256 // Size in Kb
45# elif defined(STM32F103xE)
46# define FEE_MCU_FLASH_SIZE 512 // Size in Kb
47# endif
48#endif
49
50/* Start of the emulated eeprom */
51#if !defined(FEE_PAGE_BASE_ADDRESS)
52# if 0
53/* TODO: Add support for F4 */
54# else
55# ifndef FEE_FLASH_BASE
56# define FEE_FLASH_BASE 0x8000000
57# endif
58/* Default to end of flash */
59# define FEE_PAGE_BASE_ADDRESS ((uintptr_t)(FEE_FLASH_BASE) + FEE_MCU_FLASH_SIZE * 1024 - (FEE_PAGE_COUNT * FEE_PAGE_SIZE))
60# endif
61#endif
diff --git a/tmk_core/common/test/eeprom_stm32_tests.cpp b/tmk_core/common/test/eeprom_stm32_tests.cpp
index aa84492b8..5bc8d8790 100644
--- a/tmk_core/common/test/eeprom_stm32_tests.cpp
+++ b/tmk_core/common/test/eeprom_stm32_tests.cpp
@@ -46,7 +46,7 @@ extern "C" {
46 * 46 *
47 */ 47 */
48 48
49#define EEPROM_SIZE (FEE_PAGE_SIZE * FEE_DENSITY_PAGES / 2) 49#define EEPROM_SIZE (FEE_PAGE_SIZE * FEE_PAGE_COUNT / 2)
50#define LOG_SIZE EEPROM_SIZE 50#define LOG_SIZE EEPROM_SIZE
51#define LOG_BASE (MOCK_FLASH_SIZE - LOG_SIZE) 51#define LOG_BASE (MOCK_FLASH_SIZE - LOG_SIZE)
52#define EEPROM_BASE (LOG_BASE - EEPROM_SIZE) 52#define EEPROM_BASE (LOG_BASE - EEPROM_SIZE)
diff --git a/tmk_core/common/test/hal.h b/tmk_core/common/test/hal.h
new file mode 100644
index 000000000..2d268ad54
--- /dev/null
+++ b/tmk_core/common/test/hal.h
@@ -0,0 +1,18 @@
1/* Copyright 2021 QMK
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 3 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#pragma once
17
18// Just here to please eeprom tests
diff --git a/tmk_core/common/test/rules.mk b/tmk_core/common/test/rules.mk
index e47e5880c..48632a095 100644
--- a/tmk_core/common/test/rules.mk
+++ b/tmk_core/common/test/rules.mk
@@ -3,12 +3,12 @@ eeprom_stm32_tiny_DEFS := $(eeprom_stm32_DEFS) \
3 -DFEE_MCU_FLASH_SIZE=1 \ 3 -DFEE_MCU_FLASH_SIZE=1 \
4 -DMOCK_FLASH_SIZE=1024 \ 4 -DMOCK_FLASH_SIZE=1024 \
5 -DFEE_PAGE_SIZE=512 \ 5 -DFEE_PAGE_SIZE=512 \
6 -DFEE_DENSITY_PAGES=1 6 -DFEE_PAGE_COUNT=1
7eeprom_stm32_large_DEFS := $(eeprom_stm32_DEFS) \ 7eeprom_stm32_large_DEFS := $(eeprom_stm32_DEFS) \
8 -DFEE_MCU_FLASH_SIZE=64 \ 8 -DFEE_MCU_FLASH_SIZE=64 \
9 -DMOCK_FLASH_SIZE=65536 \ 9 -DMOCK_FLASH_SIZE=65536 \
10 -DFEE_PAGE_SIZE=2048 \ 10 -DFEE_PAGE_SIZE=2048 \
11 -DFEE_DENSITY_PAGES=16 11 -DFEE_PAGE_COUNT=16
12 12
13eeprom_stm32_INC := \ 13eeprom_stm32_INC := \
14 $(TMK_PATH)/common/chibios/ 14 $(TMK_PATH)/common/chibios/