@ -0,0 +1,23 @@ | |||
// Copyright 2022 Nick Brassel (@tzarc) | |||
// SPDX-License-Identifier: GPL-2.0-or-later | |||
#include <stdint.h> | |||
#include <string.h> | |||
#include "eeprom_driver.h" | |||
#include "wear_leveling.h" | |||
void eeprom_driver_init(void) { | |||
wear_leveling_init(); | |||
} | |||
void eeprom_driver_erase(void) { | |||
wear_leveling_erase(); | |||
} | |||
void eeprom_read_block(void *buf, const void *addr, size_t len) { | |||
wear_leveling_read((uint32_t)addr, buf, len); | |||
} | |||
void eeprom_write_block(const void *buf, void *addr, size_t len) { | |||
wear_leveling_write((uint32_t)addr, buf, len); | |||
} |
@ -0,0 +1,101 @@ | |||
// Copyright 2022 Nick Brassel (@tzarc) | |||
// SPDX-License-Identifier: GPL-2.0-or-later | |||
#include <stdbool.h> | |||
#include <hal.h> | |||
#include "util.h" | |||
#include "timer.h" | |||
#include "wear_leveling.h" | |||
#include "wear_leveling_internal.h" | |||
#ifndef WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT | |||
# define WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT 32 | |||
#endif // WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT | |||
bool backing_store_init(void) { | |||
bs_dprintf("Init\n"); | |||
flash_init(); | |||
return true; | |||
} | |||
bool backing_store_unlock(void) { | |||
bs_dprintf("Unlock\n"); | |||
// No-op -- handled by the flash driver as it is. | |||
return true; | |||
} | |||
bool backing_store_erase(void) { | |||
#ifdef WEAR_LEVELING_DEBUG_OUTPUT | |||
uint32_t start = timer_read32(); | |||
#endif | |||
bool ret = true; | |||
for (int i = 0; i < (WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT); ++i) { | |||
flash_status_t status = flash_erase_block(((WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET) + i) * (EXTERNAL_FLASH_BLOCK_SIZE)); | |||
if (status != FLASH_STATUS_SUCCESS) { | |||
ret = false; | |||
break; | |||
} | |||
} | |||
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) { | |||
return backing_store_write_bulk(address, &value, 1); | |||
} | |||
bool backing_store_lock(void) { | |||
bs_dprintf("Lock \n"); | |||
// No-op -- handled by the flash driver as it is. | |||
return true; | |||
} | |||
bool backing_store_read(uint32_t address, backing_store_int_t *value) { | |||
return backing_store_read_bulk(address, value, 1); | |||
} | |||
bool backing_store_read_bulk(uint32_t address, backing_store_int_t *values, size_t item_count) { | |||
bs_dprintf("Read "); | |||
uint32_t offset = (WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET) * (EXTERNAL_FLASH_BLOCK_SIZE) + address; | |||
flash_status_t status = flash_read_block(offset, values, sizeof(backing_store_int_t) * item_count); | |||
if (status == FLASH_STATUS_SUCCESS) { | |||
for (size_t i = 0; i < item_count; ++i) { | |||
values[i] = ~values[i]; | |||
} | |||
wl_dump(offset, values, sizeof(backing_store_int_t) * item_count); | |||
} | |||
return status == FLASH_STATUS_SUCCESS; | |||
} | |||
bool backing_store_write_bulk(uint32_t address, backing_store_int_t *values, size_t item_count) { | |||
uint32_t offset = (WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET) * (EXTERNAL_FLASH_BLOCK_SIZE) + address; | |||
size_t index = 0; | |||
backing_store_int_t temp[WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT]; | |||
do { | |||
// Copy out the block of data we want to transmit first | |||
size_t this_loop = MIN(item_count, WEAR_LEVELING_EXTERNAL_FLASH_BULK_COUNT); | |||
for (size_t i = 0; i < this_loop; ++i) { | |||
temp[i] = values[index + i]; | |||
} | |||
bs_dprintf("Write "); | |||
wl_dump(offset, temp, sizeof(backing_store_int_t) * this_loop); | |||
// Take the complement instead | |||
for (size_t i = 0; i < this_loop; ++i) { | |||
temp[i] = ~temp[i]; | |||
} | |||
// Write out the block | |||
if (flash_write_block(offset, temp, sizeof(backing_store_int_t) * this_loop) != FLASH_STATUS_SUCCESS) { | |||
return false; | |||
} | |||
offset += this_loop * sizeof(backing_store_int_t); | |||
index += this_loop; | |||
item_count -= this_loop; | |||
} while (item_count > 0); | |||
return true; | |||
} |
@ -0,0 +1,34 @@ | |||
// Copyright 2022 Nick Brassel (@tzarc) | |||
// SPDX-License-Identifier: GPL-2.0-or-later | |||
#pragma once | |||
#ifndef __ASSEMBLER__ | |||
# include <stdlib.h> | |||
# include <stdint.h> | |||
# include "flash_spi.h" | |||
#endif | |||
// Use 1 block -- check the config for the SPI flash to determine how big it is | |||
#ifndef WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT | |||
# define WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT 1 | |||
#endif // WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT | |||
// Start at the first block of the external flash | |||
#ifndef WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET | |||
# define WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET 0 | |||
#endif // WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_OFFSET | |||
// 8-byte writes by default | |||
#ifndef BACKING_STORE_WRITE_SIZE | |||
# define BACKING_STORE_WRITE_SIZE 8 | |||
#endif | |||
// The space allocated by the block | |||
#ifndef WEAR_LEVELING_BACKING_SIZE | |||
# define WEAR_LEVELING_BACKING_SIZE ((EXTERNAL_FLASH_BLOCK_SIZE) * (WEAR_LEVELING_EXTERNAL_FLASH_BLOCK_COUNT)) | |||
#endif // WEAR_LEVELING_BACKING_SIZE | |||
// Use half of the backing size for logical EEPROM | |||
#ifndef WEAR_LEVELING_LOGICAL_SIZE | |||
# define WEAR_LEVELING_LOGICAL_SIZE ((WEAR_LEVELING_BACKING_SIZE) / 2) | |||
#endif // WEAR_LEVELING_LOGICAL_SIZE |
@ -0,0 +1,140 @@ | |||
// Copyright 2022 Nick Brassel (@tzarc) | |||
// SPDX-License-Identifier: GPL-2.0-or-later | |||
#include <stdbool.h> | |||
#include <hal.h> | |||
#include "timer.h" | |||
#include "wear_leveling.h" | |||
#include "wear_leveling_internal.h" | |||
static flash_offset_t base_offset = UINT32_MAX; | |||
#if defined(WEAR_LEVELING_EFL_FIRST_SECTOR) | |||
static flash_sector_t first_sector = WEAR_LEVELING_EFL_FIRST_SECTOR; | |||
#else // defined(WEAR_LEVELING_EFL_FIRST_SECTOR) | |||
static flash_sector_t first_sector = UINT16_MAX; | |||
#endif // defined(WEAR_LEVELING_EFL_FIRST_SECTOR) | |||
static flash_sector_t sector_count = UINT16_MAX; | |||
static BaseFlash * flash; | |||
#ifndef WEAR_LEVELING_EFL_FIRST_SECTOR | |||
// "Automatic" detection of the flash size -- ideally ChibiOS would have this already, but alas, it doesn't. | |||
static inline uint32_t detect_flash_size(void) { | |||
# if defined(WEAR_LEVELING_EFL_FLASH_SIZE) | |||
return WEAR_LEVELING_EFL_FLASH_SIZE; | |||
# elif defined(FLASH_BANK_SIZE) | |||
return FLASH_BANK_SIZE; | |||
# elif defined(FLASH_SIZE) | |||
return FLASH_SIZE; | |||
# elif defined(FLASHSIZE_BASE) | |||
# if defined(QMK_MCU_SERIES_STM32F0XX) || defined(QMK_MCU_SERIES_STM32F1XX) || defined(QMK_MCU_SERIES_STM32F3XX) || defined(QMK_MCU_SERIES_STM32F4XX) || defined(QMK_MCU_SERIES_STM32G4XX) || defined(QMK_MCU_SERIES_STM32L0XX) || defined(QMK_MCU_SERIES_STM32L4XX) || defined(QMK_MCU_SERIES_GD32VF103) | |||
return ((*(uint32_t *)FLASHSIZE_BASE) & 0xFFFFU) << 10U; // this register has the flash size in kB, so we convert it to bytes | |||
# elif defined(QMK_MCU_SERIES_STM32L1XX) | |||
# error This MCU family has an uncommon flash size register definition and has not been implemented. Perhaps try using the true EEPROM on the MCU instead? | |||
# endif | |||
# else | |||
# error Unknown flash size definition. | |||
return 0; | |||
# endif | |||
} | |||
#endif // WEAR_LEVELING_EFL_FIRST_SECTOR | |||
bool backing_store_init(void) { | |||
bs_dprintf("Init\n"); | |||
flash = (BaseFlash *)&EFLD1; | |||
const flash_descriptor_t *desc = flashGetDescriptor(flash); | |||
uint32_t counter = 0; | |||
#if defined(WEAR_LEVELING_EFL_FIRST_SECTOR) | |||
// Work out how many sectors we want to use, working forwards from the first sector specified | |||
for (flash_sector_t i = 0; i < desc->sectors_count - first_sector; ++i) { | |||
counter += flashGetSectorSize(flash, first_sector + i); | |||
if (counter >= (WEAR_LEVELING_BACKING_SIZE)) { | |||
sector_count = i + 1; | |||
base_offset = flashGetSectorOffset(flash, first_sector); | |||
break; | |||
} | |||
} | |||
if (sector_count == UINT16_MAX || base_offset >= flash_size) { | |||
// We didn't get the required number of sectors. Can't do anything here. Fault. | |||
chSysHalt("Invalid sector count intended to be used with wear_leveling"); | |||
} | |||
#else // defined(WEAR_LEVELING_EFL_FIRST_SECTOR) | |||
// Work out how many sectors we want to use, working backwards from the end of the flash | |||
uint32_t flash_size = detect_flash_size(); | |||
flash_sector_t last_sector = desc->sectors_count; | |||
for (flash_sector_t i = 0; i < desc->sectors_count; ++i) { | |||
first_sector = desc->sectors_count - i - 1; | |||
if (flashGetSectorOffset(flash, first_sector) >= flash_size) { | |||
last_sector = first_sector; | |||
continue; | |||
} | |||
counter += flashGetSectorSize(flash, first_sector); | |||
if (counter >= (WEAR_LEVELING_BACKING_SIZE)) { | |||
sector_count = last_sector - first_sector; | |||
base_offset = flashGetSectorOffset(flash, first_sector); | |||
break; | |||
} | |||
} | |||
#endif // defined(WEAR_LEVELING_EFL_FIRST_SECTOR) | |||
return true; | |||
} | |||
bool backing_store_unlock(void) { | |||
bs_dprintf("Unlock\n"); | |||
return eflStart(&EFLD1, NULL) == HAL_RET_SUCCESS; | |||
} | |||
bool backing_store_erase(void) { | |||
#ifdef WEAR_LEVELING_DEBUG_OUTPUT | |||
uint32_t start = timer_read32(); | |||
#endif | |||
bool ret = true; | |||
flash_error_t status; | |||
for (int i = 0; i < sector_count; ++i) { | |||
// Kick off the sector erase | |||
status = flashStartEraseSector(flash, first_sector + i); | |||
if (status != FLASH_NO_ERROR && status != FLASH_BUSY_ERASING) { | |||
ret = false; | |||
} | |||
// Wait for the erase to complete | |||
status = flashWaitErase(flash); | |||
if (status != FLASH_NO_ERROR && status != FLASH_BUSY_ERASING) { | |||
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 = (base_offset + address); | |||
bs_dprintf("Write "); | |||
wl_dump(offset, &value, sizeof(value)); | |||
value = ~value; | |||
return flashProgram(flash, offset, sizeof(value), (const uint8_t *)&value) == FLASH_NO_ERROR; | |||
} | |||
bool backing_store_lock(void) { | |||
bs_dprintf("Lock \n"); | |||
eflStop(&EFLD1); | |||
return true; | |||
} | |||
bool backing_store_read(uint32_t address, backing_store_int_t *value) { | |||
uint32_t offset = (base_offset + address); | |||
backing_store_int_t *loc = (backing_store_int_t *)offset; | |||
*value = ~(*loc); | |||
bs_dprintf("Read "); | |||
wl_dump(offset, value, sizeof(backing_store_int_t)); | |||
return true; | |||
} |
@ -0,0 +1,50 @@ | |||
// Copyright 2022 Nick Brassel (@tzarc) | |||
// SPDX-License-Identifier: GPL-2.0-or-later | |||
#pragma once | |||
#ifndef __ASSEMBLER__ | |||
# include <hal.h> | |||
#endif | |||
// Work out how many bytes per write to internal flash | |||
#ifndef BACKING_STORE_WRITE_SIZE | |||
// These need to match EFL's XXXXXX_FLASH_LINE_SIZE, see associated code in `lib/chibios/os/hal/ports/**/hal_efl_lld.c`, | |||
// or associated `stm32_registry.h` for the MCU in question (or equivalent for the family). | |||
# if defined(QMK_MCU_SERIES_GD32VF103) | |||
# define BACKING_STORE_WRITE_SIZE 2 // from hal_efl_lld.c | |||
# elif defined(QMK_MCU_FAMILY_NUC123) | |||
# define BACKING_STORE_WRITE_SIZE 4 // from hal_efl_lld.c | |||
# elif defined(QMK_MCU_FAMILY_STM32) | |||
# if defined(STM32_FLASH_LINE_SIZE) // from some family's stm32_registry.h file | |||
# define BACKING_STORE_WRITE_SIZE (STM32_FLASH_LINE_SIZE) | |||
# else | |||
# if defined(QMK_MCU_SERIES_STM32F1XX) | |||
# define BACKING_STORE_WRITE_SIZE 2 // from hal_efl_lld.c | |||
# elif defined(QMK_MCU_SERIES_STM32F3XX) | |||
# define BACKING_STORE_WRITE_SIZE 2 // from hal_efl_lld.c | |||
# elif defined(QMK_MCU_SERIES_STM32F4XX) | |||
# define BACKING_STORE_WRITE_SIZE (1 << STM32_FLASH_PSIZE) // from hal_efl_lld.c | |||
# elif defined(QMK_MCU_SERIES_STM32L4XX) | |||
# define BACKING_STORE_WRITE_SIZE 8 // from hal_efl_lld.c | |||
# elif defined(QMK_MCU_SERIES_STM32G0XX) | |||
# define BACKING_STORE_WRITE_SIZE 8 // from hal_efl_lld.c | |||
# elif defined(QMK_MCU_SERIES_STM32G4XX) | |||
# define BACKING_STORE_WRITE_SIZE 8 // from hal_efl_lld.c | |||
# else | |||
# error "ChibiOS hasn't defined STM32_FLASH_LINE_SIZE, and could not automatically determine BACKING_STORE_WRITE_SIZE" // normally defined in stm32_registry.h, should be set by STM32_FLASH_LINE_SIZE | |||
# endif | |||
# endif | |||
# else | |||
# error "Could not automatically determine BACKING_STORE_WRITE_SIZE" | |||
# endif | |||
#endif | |||
// 2kB backing space allocated | |||
#ifndef WEAR_LEVELING_BACKING_SIZE | |||
# define WEAR_LEVELING_BACKING_SIZE 2048 | |||
#endif // WEAR_LEVELING_BACKING_SIZE | |||
// 1kB logical EEPROM | |||
#ifndef WEAR_LEVELING_LOGICAL_SIZE | |||
# define WEAR_LEVELING_LOGICAL_SIZE 1024 | |||
#endif // WEAR_LEVELING_LOGICAL_SIZE |
@ -0,0 +1,59 @@ | |||
// Copyright 2022 Nick Brassel (@tzarc) | |||
// SPDX-License-Identifier: GPL-2.0-or-later | |||
#include <stdbool.h> | |||
#include <hal.h> | |||
#include "timer.h" | |||
#include "wear_leveling.h" | |||
#include "wear_leveling_internal.h" | |||
#include "flash_stm32.h" | |||
bool backing_store_init(void) { | |||
bs_dprintf("Init\n"); | |||
return true; | |||
} | |||
bool backing_store_unlock(void) { | |||
bs_dprintf("Unlock\n"); | |||
FLASH_Unlock(); | |||
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_LEGACY_EMULATION_PAGE_COUNT); ++i) { | |||
status = FLASH_ErasePage(WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS + (i * (WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE))); | |||
if (status != FLASH_COMPLETE) { | |||
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_LEGACY_EMULATION_BASE_PAGE_ADDRESS) + address); | |||
bs_dprintf("Write "); | |||
wl_dump(offset, &value, sizeof(backing_store_int_t)); | |||
return FLASH_ProgramHalfWord(offset, ~value) == FLASH_COMPLETE; | |||
} | |||
bool backing_store_lock(void) { | |||
bs_dprintf("Lock \n"); | |||
FLASH_Lock(); | |||
return true; | |||
} | |||
bool backing_store_read(uint32_t address, backing_store_int_t* value) { | |||
uint32_t offset = ((WEAR_LEVELING_LEGACY_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; | |||
} |
@ -0,0 +1,67 @@ | |||
// Copyright 2022 Nick Brassel (@tzarc) | |||
// SPDX-License-Identifier: GPL-2.0-or-later | |||
#pragma once | |||
// Work out the page size to use | |||
#ifndef WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE | |||
# if defined(QMK_MCU_STM32F042) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE 1024 | |||
# elif defined(QMK_MCU_STM32F070) || defined(QMK_MCU_STM32F072) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE 2048 | |||
# elif defined(QMK_MCU_STM32F401) || defined(QMK_MCU_STM32F411) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE 16384 | |||
# endif | |||
#endif | |||
// Work out how much flash space we have | |||
#ifndef WEAR_LEVELING_LEGACY_EMULATION_FLASH_SIZE | |||
# define WEAR_LEVELING_LEGACY_EMULATION_FLASH_SIZE ((*(uint32_t *)FLASHSIZE_BASE) & 0xFFFFU) // in kB | |||
#endif | |||
// The base location of program memory | |||
#ifndef WEAR_LEVELING_LEGACY_EMULATION_FLASH_BASE | |||
# define WEAR_LEVELING_LEGACY_EMULATION_FLASH_BASE 0x08000000 | |||
#endif | |||
// The number of pages to use | |||
#ifndef WEAR_LEVELING_LEGACY_EMULATION_PAGE_COUNT | |||
# if defined(QMK_MCU_STM32F042) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_PAGE_COUNT 2 | |||
# elif defined(QMK_MCU_STM32F070) || defined(QMK_MCU_STM32F072) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_PAGE_COUNT 1 | |||
# elif defined(QMK_MCU_STM32F401) || defined(QMK_MCU_STM32F411) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_PAGE_COUNT 1 | |||
# endif | |||
#endif | |||
// The origin of the emulated eeprom | |||
#ifndef WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS | |||
# if defined(QMK_MCU_STM32F042) || defined(QMK_MCU_STM32F070) || defined(QMK_MCU_STM32F072) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS ((uintptr_t)(WEAR_LEVELING_LEGACY_EMULATION_FLASH_BASE) + WEAR_LEVELING_LEGACY_EMULATION_FLASH_SIZE * 1024 - (WEAR_LEVELING_LEGACY_EMULATION_PAGE_COUNT * WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE)) | |||
# elif defined(QMK_MCU_STM32F401) || defined(QMK_MCU_STM32F411) | |||
# if defined(BOOTLOADER_STM32) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS (WEAR_LEVELING_LEGACY_EMULATION_FLASH_BASE + (1 * (WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE))) // +16k | |||
# elif defined(BOOTLOADER_TINYUF2) | |||
# define WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS (WEAR_LEVELING_LEGACY_EMULATION_FLASH_BASE + (3 * (WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE))) // +48k | |||
# endif | |||
# endif | |||
#endif | |||
// 2-byte writes | |||
#ifndef BACKING_STORE_WRITE_SIZE | |||
# define BACKING_STORE_WRITE_SIZE 2 | |||
#endif | |||
// The amount of space to use for the entire set of emulation | |||
#ifndef WEAR_LEVELING_BACKING_SIZE | |||
# if defined(QMK_MCU_STM32F042) || defined(QMK_MCU_STM32F070) || defined(QMK_MCU_STM32F072) | |||
# define WEAR_LEVELING_BACKING_SIZE 2048 | |||
# elif defined(QMK_MCU_STM32F401) || defined(QMK_MCU_STM32F411) | |||
# define WEAR_LEVELING_BACKING_SIZE 16384 | |||
# endif | |||
#endif | |||
// The logical amount of eeprom available | |||
#ifndef WEAR_LEVELING_LOGICAL_SIZE | |||
# define WEAR_LEVELING_LOGICAL_SIZE 1024 | |||
#endif |