Skip to content

Commit

Permalink
Wear-Leveling driver for SN32 platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Jpe230 committed Oct 12, 2022
1 parent 4660b56 commit 2dc6b43
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
5 changes: 4 additions & 1 deletion builddefs/common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)),)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions keyboards/handwired/onekey/sn32/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
EEPROM_DRIVER = wear_leveling
WEAR_LEVELING_DRIVER = sn32
56 changes: 56 additions & 0 deletions platforms/chibios/drivers/wear_leveling/wear_leveling_sn32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2022 Jose Pablo Ramirez (@jpe230)
// SPDX-License-Identifier: GPL-2.0-or-later
#include <stdbool.h>
#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;
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2dc6b43

Please sign in to comment.