Skip to content

Commit 2dc6b43

Browse files
committed
Wear-Leveling driver for SN32 platform
1 parent 4660b56 commit 2dc6b43

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

builddefs/common_features.mk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ else
249249
endif
250250
endif
251251

252-
VALID_WEAR_LEVELING_DRIVER_TYPES := custom embedded_flash spi_flash rp2040_flash legacy
252+
VALID_WEAR_LEVELING_DRIVER_TYPES := custom embedded_flash spi_flash rp2040_flash legacy sn32
253253
WEAR_LEVELING_DRIVER ?= none
254254
ifneq ($(strip $(WEAR_LEVELING_DRIVER)),none)
255255
ifeq ($(filter $(WEAR_LEVELING_DRIVER),$(VALID_WEAR_LEVELING_DRIVER_TYPES)),)
@@ -277,6 +277,9 @@ ifneq ($(strip $(WEAR_LEVELING_DRIVER)),none)
277277
COMMON_VPATH += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/flash
278278
SRC += flash_stm32.c wear_leveling_legacy.c
279279
POST_CONFIG_H += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/wear_leveling/wear_leveling_legacy_config.h
280+
else ifeq ($(strip $(WEAR_LEVELING_DRIVER)), sn32)
281+
SRC += wear_leveling_sn32.c
282+
POST_CONFIG_H += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/wear_leveling/wear_leveling_sn32_config.h
280283
endif
281284
endif
282285
endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
EEPROM_DRIVER = wear_leveling
2+
WEAR_LEVELING_DRIVER = sn32
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2022 Jose Pablo Ramirez (@jpe230)
2+
// SPDX-License-Identifier: GPL-2.0-or-later
3+
#include <stdbool.h>
4+
#include "timer.h"
5+
#include "wear_leveling.h"
6+
#include "wear_leveling_internal.h"
7+
#include "Flash.h"
8+
9+
bool backing_store_init(void) {
10+
bs_dprintf("Init\n");
11+
return true;
12+
}
13+
14+
bool backing_store_unlock(void) {
15+
bs_dprintf("Unlock\n");
16+
return true;
17+
}
18+
19+
bool backing_store_erase(void) {
20+
#ifdef WEAR_LEVELING_DEBUG_OUTPUT
21+
uint32_t start = timer_read32();
22+
#endif
23+
24+
bool ret = true;
25+
FLASH_Status status;
26+
for (int i = 0; i < (WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT); ++i) {
27+
status = FLASH_EraseSector(WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS + (i * WEAR_LEVELING_SN32_PAGE_SIZE));
28+
if (status != FLASH_FAIL) {
29+
ret = false;
30+
}
31+
}
32+
33+
bs_dprintf("Backing store erase took %ldms to complete\n", ((long)(timer_read32() - start)));
34+
return ret;
35+
}
36+
37+
bool backing_store_write(uint32_t address, backing_store_int_t value) {
38+
uint32_t offset = ((WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS) + address);
39+
bs_dprintf("Write ");
40+
wl_dump(offset, &value, sizeof(backing_store_int_t));
41+
return FLASH_ProgramDWord(offset & 0xFFFFFFFC, value) == FLASH_OKAY;
42+
}
43+
44+
bool backing_store_lock(void) {
45+
bs_dprintf("Lock \n");
46+
return true;
47+
}
48+
49+
bool backing_store_read(uint32_t address, backing_store_int_t* value) {
50+
uint32_t offset = ((WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS) + address);
51+
backing_store_int_t* loc = (backing_store_int_t*)offset;
52+
*value = *loc;
53+
bs_dprintf("Read ");
54+
wl_dump(offset, loc, sizeof(backing_store_int_t));
55+
return true;
56+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2022 Jose Pablo Ramirez (@jpe230)
2+
// SPDX-License-Identifier: GPL-2.0-or-later
3+
#pragma once
4+
5+
// Work out the page size to use
6+
#ifndef WEAR_LEVELING_SN32_PAGE_SIZE
7+
# if defined(QMK_MCU_SERIES_SN32F240B)
8+
# define WEAR_LEVELING_SN32_PAGE_SIZE 64
9+
# elif defined(QMK_MCU_SERIES_SN32F240)
10+
# define WEAR_LEVELING_SN32_PAGE_SIZE 64
11+
# endif
12+
#endif
13+
14+
// Number of pages we have
15+
#ifndef WEAR_LEVELING_SN32_EMULATION_TOTAL_PAGE
16+
# if defined(QMK_MCU_SERIES_SN32F240B)
17+
# define WEAR_LEVELING_SN32_EMULATION_TOTAL_PAGE 1024
18+
# elif defined(QMK_MCU_SERIES_SN32F260)
19+
# define WEAR_LEVELING_SN32_EMULATION_TOTAL_PAGE 480
20+
# endif
21+
#endif
22+
23+
// The number of pages to use
24+
#ifndef WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT
25+
# if defined(QMK_MCU_SERIES_SN32F240B)
26+
# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 23
27+
# elif defined(QMK_MCU_SERIES_SN32F260)
28+
# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 23
29+
# endif
30+
#endif
31+
32+
// The origin of the emulated eeprom
33+
#ifndef WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS
34+
# define WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS ((uint32_t)(WEAR_LEVELING_SN32_PAGE_SIZE * WEAR_LEVELING_SN32_EMULATION_TOTAL_PAGE - ((WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT + 1) * WEAR_LEVELING_SN32_PAGE_SIZE)))
35+
#endif
36+
37+
// 4-byte writes
38+
#ifndef BACKING_STORE_WRITE_SIZE
39+
# define BACKING_STORE_WRITE_SIZE 4
40+
#endif
41+
42+
// The amount of space to use for the entire set of emulation
43+
#ifndef WEAR_LEVELING_BACKING_SIZE
44+
# define WEAR_LEVELING_BACKING_SIZE ((WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT)*WEAR_LEVELING_SN32_PAGE_SIZE)
45+
#endif
46+
47+
// The logical amount of eeprom available
48+
#ifndef WEAR_LEVELING_LOGICAL_SIZE
49+
# define WEAR_LEVELING_LOGICAL_SIZE ((WEAR_LEVELING_BACKING_SIZE) / 2)
50+
#endif

0 commit comments

Comments
 (0)