From 2dc6b43ca20eafb9e75363cf9bde977bd2327761 Mon Sep 17 00:00:00 2001 From: "pablin.123.ra@gmail.com" Date: Wed, 12 Oct 2022 15:05:41 -0500 Subject: [PATCH 1/4] Wear-Leveling driver for SN32 platform --- builddefs/common_features.mk | 5 +- keyboards/handwired/onekey/sn32/rules.mk | 2 + .../wear_leveling/wear_leveling_sn32.c | 56 +++++++++++++++++++ .../wear_leveling/wear_leveling_sn32_config.h | 50 +++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c create mode 100644 platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_config.h diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index 7f07be5144d2..609d76d80082 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -249,7 +249,7 @@ else endif endif -VALID_WEAR_LEVELING_DRIVER_TYPES := custom embedded_flash spi_flash rp2040_flash legacy +VALID_WEAR_LEVELING_DRIVER_TYPES := custom embedded_flash spi_flash rp2040_flash legacy sn32 WEAR_LEVELING_DRIVER ?= none ifneq ($(strip $(WEAR_LEVELING_DRIVER)),none) ifeq ($(filter $(WEAR_LEVELING_DRIVER),$(VALID_WEAR_LEVELING_DRIVER_TYPES)),) @@ -277,6 +277,9 @@ ifneq ($(strip $(WEAR_LEVELING_DRIVER)),none) COMMON_VPATH += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/flash SRC += flash_stm32.c wear_leveling_legacy.c POST_CONFIG_H += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/wear_leveling/wear_leveling_legacy_config.h + else ifeq ($(strip $(WEAR_LEVELING_DRIVER)), sn32) + SRC += wear_leveling_sn32.c + POST_CONFIG_H += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/wear_leveling/wear_leveling_sn32_config.h endif endif endif diff --git a/keyboards/handwired/onekey/sn32/rules.mk b/keyboards/handwired/onekey/sn32/rules.mk index e69de29bb2d1..11832616931b 100644 --- a/keyboards/handwired/onekey/sn32/rules.mk +++ b/keyboards/handwired/onekey/sn32/rules.mk @@ -0,0 +1,2 @@ +EEPROM_DRIVER = wear_leveling +WEAR_LEVELING_DRIVER = sn32 diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c new file mode 100644 index 000000000000..e89b2eb7d35a --- /dev/null +++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c @@ -0,0 +1,56 @@ +// Copyright 2022 Jose Pablo Ramirez (@jpe230) +// SPDX-License-Identifier: GPL-2.0-or-later +#include +#include "timer.h" +#include "wear_leveling.h" +#include "wear_leveling_internal.h" +#include "Flash.h" + +bool backing_store_init(void) { + bs_dprintf("Init\n"); + return true; +} + +bool backing_store_unlock(void) { + bs_dprintf("Unlock\n"); + return true; +} + +bool backing_store_erase(void) { +#ifdef WEAR_LEVELING_DEBUG_OUTPUT + uint32_t start = timer_read32(); +#endif + + bool ret = true; + FLASH_Status status; + for (int i = 0; i < (WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT); ++i) { + status = FLASH_EraseSector(WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS + (i * WEAR_LEVELING_SN32_PAGE_SIZE)); + if (status != FLASH_FAIL) { + ret = false; + } + } + + bs_dprintf("Backing store erase took %ldms to complete\n", ((long)(timer_read32() - start))); + return ret; +} + +bool backing_store_write(uint32_t address, backing_store_int_t value) { + uint32_t offset = ((WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS) + address); + bs_dprintf("Write "); + wl_dump(offset, &value, sizeof(backing_store_int_t)); + return FLASH_ProgramDWord(offset & 0xFFFFFFFC, value) == FLASH_OKAY; +} + +bool backing_store_lock(void) { + bs_dprintf("Lock \n"); + return true; +} + +bool backing_store_read(uint32_t address, backing_store_int_t* value) { + uint32_t offset = ((WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS) + address); + backing_store_int_t* loc = (backing_store_int_t*)offset; + *value = *loc; + bs_dprintf("Read "); + wl_dump(offset, loc, sizeof(backing_store_int_t)); + return true; +} diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_config.h b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_config.h new file mode 100644 index 000000000000..0f3ccabf512a --- /dev/null +++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_config.h @@ -0,0 +1,50 @@ +// Copyright 2022 Jose Pablo Ramirez (@jpe230) +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +// Work out the page size to use +#ifndef WEAR_LEVELING_SN32_PAGE_SIZE +# if defined(QMK_MCU_SERIES_SN32F240B) +# define WEAR_LEVELING_SN32_PAGE_SIZE 64 +# elif defined(QMK_MCU_SERIES_SN32F240) +# define WEAR_LEVELING_SN32_PAGE_SIZE 64 +# endif +#endif + +// Number of pages we have +#ifndef WEAR_LEVELING_SN32_EMULATION_TOTAL_PAGE +# if defined(QMK_MCU_SERIES_SN32F240B) +# define WEAR_LEVELING_SN32_EMULATION_TOTAL_PAGE 1024 +# elif defined(QMK_MCU_SERIES_SN32F260) +# define WEAR_LEVELING_SN32_EMULATION_TOTAL_PAGE 480 +# endif +#endif + +// The number of pages to use +#ifndef WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT +# if defined(QMK_MCU_SERIES_SN32F240B) +# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 23 +# elif defined(QMK_MCU_SERIES_SN32F260) +# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 23 +# endif +#endif + +// The origin of the emulated eeprom +#ifndef WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS +# 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))) +#endif + +// 4-byte writes +#ifndef BACKING_STORE_WRITE_SIZE +# define BACKING_STORE_WRITE_SIZE 4 +#endif + +// The amount of space to use for the entire set of emulation +#ifndef WEAR_LEVELING_BACKING_SIZE +# define WEAR_LEVELING_BACKING_SIZE ((WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT)*WEAR_LEVELING_SN32_PAGE_SIZE) +#endif + +// The logical amount of eeprom available +#ifndef WEAR_LEVELING_LOGICAL_SIZE +# define WEAR_LEVELING_LOGICAL_SIZE ((WEAR_LEVELING_BACKING_SIZE) / 2) +#endif From 40a8d85167d98905041d740d2742e4cae1c3b8b8 Mon Sep 17 00:00:00 2001 From: "pablin.123.ra@gmail.com" Date: Wed, 12 Oct 2022 15:14:22 -0500 Subject: [PATCH 2/4] Fix typo on store_erase --- platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c index e89b2eb7d35a..f7ec1cc59582 100644 --- a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c +++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c @@ -25,7 +25,7 @@ bool backing_store_erase(void) { FLASH_Status status; for (int i = 0; i < (WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT); ++i) { status = FLASH_EraseSector(WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS + (i * WEAR_LEVELING_SN32_PAGE_SIZE)); - if (status != FLASH_FAIL) { + if (status == FLASH_FAIL) { ret = false; } } From 77f358d65c6d10ab9886ac72c7569cf161c5b57a Mon Sep 17 00:00:00 2001 From: "pablin.123.ra@gmail.com" Date: Wed, 12 Oct 2022 15:32:43 -0500 Subject: [PATCH 3/4] Rename SN32 WL driver, guard the last page --- builddefs/common_features.mk | 8 ++++---- keyboards/handwired/onekey/sn32/rules.mk | 2 +- .../{wear_leveling_sn32.c => wear_leveling_sn32_flash.c} | 0 ...ng_sn32_config.h => wear_leveling_sn32_flash_config.h} | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) rename platforms/chibios/drivers/wear_leveling/{wear_leveling_sn32.c => wear_leveling_sn32_flash.c} (100%) rename platforms/chibios/drivers/wear_leveling/{wear_leveling_sn32_config.h => wear_leveling_sn32_flash_config.h} (87%) diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index 609d76d80082..10b5fff280ea 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -249,7 +249,7 @@ else endif endif -VALID_WEAR_LEVELING_DRIVER_TYPES := custom embedded_flash spi_flash rp2040_flash legacy sn32 +VALID_WEAR_LEVELING_DRIVER_TYPES := custom embedded_flash spi_flash rp2040_flash legacy sn32_flash WEAR_LEVELING_DRIVER ?= none ifneq ($(strip $(WEAR_LEVELING_DRIVER)),none) ifeq ($(filter $(WEAR_LEVELING_DRIVER),$(VALID_WEAR_LEVELING_DRIVER_TYPES)),) @@ -277,9 +277,9 @@ ifneq ($(strip $(WEAR_LEVELING_DRIVER)),none) COMMON_VPATH += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/flash SRC += flash_stm32.c wear_leveling_legacy.c POST_CONFIG_H += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/wear_leveling/wear_leveling_legacy_config.h - else ifeq ($(strip $(WEAR_LEVELING_DRIVER)), sn32) - SRC += wear_leveling_sn32.c - POST_CONFIG_H += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/wear_leveling/wear_leveling_sn32_config.h + else ifeq ($(strip $(WEAR_LEVELING_DRIVER)), sn32_flash) + SRC += wear_leveling_sn32_flash.c + POST_CONFIG_H += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/wear_leveling/wear_leveling_sn32_flash_config.h endif endif endif diff --git a/keyboards/handwired/onekey/sn32/rules.mk b/keyboards/handwired/onekey/sn32/rules.mk index 11832616931b..047de4137dd5 100644 --- a/keyboards/handwired/onekey/sn32/rules.mk +++ b/keyboards/handwired/onekey/sn32/rules.mk @@ -1,2 +1,2 @@ EEPROM_DRIVER = wear_leveling -WEAR_LEVELING_DRIVER = sn32 +WEAR_LEVELING_DRIVER = sn32_flash diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash.c similarity index 100% rename from platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c rename to platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash.c diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_config.h b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash_config.h similarity index 87% rename from platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_config.h rename to platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash_config.h index 0f3ccabf512a..5b1b9eb7fb2e 100644 --- a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_config.h +++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash_config.h @@ -6,7 +6,7 @@ #ifndef WEAR_LEVELING_SN32_PAGE_SIZE # if defined(QMK_MCU_SERIES_SN32F240B) # define WEAR_LEVELING_SN32_PAGE_SIZE 64 -# elif defined(QMK_MCU_SERIES_SN32F240) +# elif defined(QMK_MCU_SERIES_SN32F260) # define WEAR_LEVELING_SN32_PAGE_SIZE 64 # endif #endif @@ -23,15 +23,15 @@ // The number of pages to use #ifndef WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT # if defined(QMK_MCU_SERIES_SN32F240B) -# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 23 +# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 22 # elif defined(QMK_MCU_SERIES_SN32F260) -# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 23 +# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 22 # endif #endif // The origin of the emulated eeprom #ifndef WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS -# 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))) +# 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 + 2) * WEAR_LEVELING_SN32_PAGE_SIZE))) #endif // 4-byte writes From 3f4d1cb504fa947bac8bd3e6156325c980c50ad5 Mon Sep 17 00:00:00 2001 From: "pablin.123.ra@gmail.com" Date: Wed, 12 Oct 2022 15:42:04 -0500 Subject: [PATCH 4/4] Change page count --- .../drivers/wear_leveling/wear_leveling_sn32_flash_config.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash_config.h b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash_config.h index 5b1b9eb7fb2e..7ac9b6555297 100644 --- a/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash_config.h +++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_sn32_flash_config.h @@ -23,15 +23,15 @@ // The number of pages to use #ifndef WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT # if defined(QMK_MCU_SERIES_SN32F240B) -# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 22 +# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 23 # elif defined(QMK_MCU_SERIES_SN32F260) -# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 22 +# define WEAR_LEVELING_SN32_EMULATION_PAGE_COUNT 23 # endif #endif // The origin of the emulated eeprom #ifndef WEAR_LEVELING_SN32_EMULATION_BASE_PAGE_ADDRESS -# 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 + 2) * WEAR_LEVELING_SN32_PAGE_SIZE))) +# 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))) #endif // 4-byte writes