wear_leveling_legacy.c (1821B)
1 // Copyright 2022 Nick Brassel (@tzarc) 2 // SPDX-License-Identifier: GPL-2.0-or-later 3 #include <stdbool.h> 4 #include <hal.h> 5 #include "timer.h" 6 #include "wear_leveling.h" 7 #include "wear_leveling_legacy_config.h" 8 #include "wear_leveling_internal.h" 9 #include "legacy_flash_ops.h" 10 11 bool backing_store_init(void) { 12 bs_dprintf("Init\n"); 13 return true; 14 } 15 16 bool backing_store_unlock(void) { 17 bs_dprintf("Unlock\n"); 18 FLASH_Unlock(); 19 return true; 20 } 21 22 bool backing_store_erase(void) { 23 #ifdef WEAR_LEVELING_DEBUG_OUTPUT 24 uint32_t start = timer_read32(); 25 #endif 26 27 bool ret = true; 28 FLASH_Status status; 29 for (int i = 0; i < (WEAR_LEVELING_LEGACY_EMULATION_PAGE_COUNT); ++i) { 30 status = FLASH_ErasePage(WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS + (i * (WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE))); 31 if (status != FLASH_COMPLETE) { 32 ret = false; 33 } 34 } 35 36 bs_dprintf("Backing store erase took %ldms to complete\n", ((long)(timer_read32() - start))); 37 return ret; 38 } 39 40 bool backing_store_write(uint32_t address, backing_store_int_t value) { 41 uint32_t offset = ((WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS) + address); 42 bs_dprintf("Write "); 43 wl_dump(offset, &value, sizeof(backing_store_int_t)); 44 return FLASH_ProgramHalfWord(offset, ~value) == FLASH_COMPLETE; 45 } 46 47 bool backing_store_lock(void) { 48 bs_dprintf("Lock \n"); 49 FLASH_Lock(); 50 return true; 51 } 52 53 bool backing_store_read(uint32_t address, backing_store_int_t* value) { 54 uint32_t offset = ((WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS) + address); 55 backing_store_int_t* loc = (backing_store_int_t*)offset; 56 *value = ~(*loc); 57 bs_dprintf("Read "); 58 wl_dump(offset, loc, sizeof(backing_store_int_t)); 59 return true; 60 }