From 97263d797624c1c821343b1b5905ab62660c1136 Mon Sep 17 00:00:00 2001 From: Peter Andersson Date: Thu, 30 Oct 2014 19:43:21 +0100 Subject: [PATCH] Specifying a non-zero SPI start physical address does not work #2 --- src/spiffs_gc.c | 12 ++++++------ src/spiffs_hydrogen.c | 2 +- src/spiffs_nucleus.c | 8 ++++---- src/test/params_test.h | 14 ++++++++++---- src/test/test_spiffs.c | 32 +++++++++++++++++++++++++++----- 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/spiffs_gc.c b/src/spiffs_gc.c index f4dc27b..0db7fb5 100644 --- a/src/spiffs_gc.c +++ b/src/spiffs_gc.c @@ -52,7 +52,7 @@ s32_t spiffs_gc_quick( s32_t res = SPIFFS_OK; u32_t blocks = fs->block_count; spiffs_block_ix cur_block = 0; - u32_t cur_block_addr = SPIFFS_CFG_PHYS_ADDR(fs); + u32_t cur_block_addr = 0; int cur_entry = 0; spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work; @@ -194,7 +194,7 @@ s32_t spiffs_gc_erase_page_stats( while (res == SPIFFS_OK && obj_lookup_page < SPIFFS_OBJ_LOOKUP_PAGES(fs)) { int entry_offset = obj_lookup_page * entries_per_page; res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, - 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work); + 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work); // check each entry while (res == SPIFFS_OK && cur_entry - entry_offset < entries_per_page && cur_entry < SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs)) { @@ -223,7 +223,7 @@ s32_t spiffs_gc_find_candidate( s32_t res = SPIFFS_OK; u32_t blocks = fs->block_count; spiffs_block_ix cur_block = 0; - u32_t cur_block_addr = SPIFFS_CFG_PHYS_ADDR(fs); + u32_t cur_block_addr = 0; spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work; int cur_entry = 0; @@ -383,7 +383,7 @@ s32_t spiffs_gc_clean(spiffs *fs, spiffs_block_ix bix) { while (scan && res == SPIFFS_OK && obj_lookup_page < SPIFFS_OBJ_LOOKUP_PAGES(fs)) { int entry_offset = obj_lookup_page * entries_per_page; res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, - 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), + 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work); // check each entry while (scan && res == SPIFFS_OK && @@ -420,7 +420,7 @@ s32_t spiffs_gc_clean(spiffs *fs, spiffs_block_ix bix) { SPIFFS_CHECK_RES(res); // move wipes obj_lu, reload it res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, - 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), + 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work); SPIFFS_CHECK_RES(res); } else { @@ -461,7 +461,7 @@ s32_t spiffs_gc_clean(spiffs *fs, spiffs_block_ix bix) { spiffs_cb_object_event(fs, 0, SPIFFS_EV_IX_UPD, obj_id, p_hdr.span_ix, new_pix, 0); // move wipes obj_lu, reload it res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, - 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), + 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work); SPIFFS_CHECK_RES(res); } else { diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 539e679..6080d56 100644 --- a/src/spiffs_hydrogen.c +++ b/src/spiffs_hydrogen.c @@ -697,7 +697,7 @@ s32_t SPIFFS_vis(spiffs *fs) { while (res == SPIFFS_OK && obj_lookup_page < SPIFFS_OBJ_LOOKUP_PAGES(fs)) { int entry_offset = obj_lookup_page * entries_per_page; res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, - 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work); + 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work); // check each entry while (res == SPIFFS_OK && cur_entry - entry_offset < entries_per_page && cur_entry < SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs)) { diff --git a/src/spiffs_nucleus.c b/src/spiffs_nucleus.c index 652d8b4..834b764 100644 --- a/src/spiffs_nucleus.c +++ b/src/spiffs_nucleus.c @@ -130,7 +130,7 @@ s32_t spiffs_obj_lu_find_entry_visitor( s32_t res = SPIFFS_OK; s32_t entry_count = fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs); spiffs_block_ix cur_block = starting_block; - u32_t cur_block_addr = SPIFFS_BLOCK_TO_PADDR(fs, starting_block); + u32_t cur_block_addr = starting_block * SPIFFS_CFG_LOG_BLOCK_SZ(fs); spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work; int cur_entry = starting_lu_entry; @@ -140,11 +140,11 @@ s32_t spiffs_obj_lu_find_entry_visitor( if (cur_entry >= SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) - 1) { cur_entry = 0; cur_block++; - cur_block_addr = SPIFFS_BLOCK_TO_PADDR(fs, cur_block); + cur_block_addr = cur_block * SPIFFS_CFG_LOG_BLOCK_SZ(fs); if (cur_block >= fs->block_count) { // block wrap cur_block = 0; - cur_block_addr = SPIFFS_CFG_PHYS_ADDR(fs); + cur_block_addr = 0; } } @@ -203,7 +203,7 @@ s32_t spiffs_obj_lu_find_entry_visitor( } else { // block wrap cur_block = 0; - cur_block_addr = SPIFFS_CFG_PHYS_ADDR(fs); + cur_block_addr = 0; } } } // per block diff --git a/src/test/params_test.h b/src/test/params_test.h index 3c7c6bc..241367f 100644 --- a/src/test/params_test.h +++ b/src/test/params_test.h @@ -8,10 +8,16 @@ #ifndef PARAMS_TEST_H_ #define PARAMS_TEST_H_ -#define FLASH_SIZE (2*1024*1024) -#define SECTOR_SIZE 65536 -#define LOG_BLOCK (SECTOR_SIZE*2) -#define LOG_PAGE (SECTOR_SIZE/256) +// total emulated spi flash size +#define PHYS_FLASH_SIZE (16*1024*1024) +// spiffs file system size +#define SPIFFS_FLASH_SIZE (2*1024*1024) +// spiffs file system offset in emulated spi flash +#define SPIFFS_PHYS_ADDR (4*1024*1024) + +#define SECTOR_SIZE 65536 +#define LOG_BLOCK (SECTOR_SIZE*2) +#define LOG_PAGE (SECTOR_SIZE/256) #define FD_BUF_SIZE 64*6 #define CACHE_BUF_SIZE (LOG_PAGE + 32)*8 diff --git a/src/test/test_spiffs.c b/src/test/test_spiffs.c index 6fd0242..92d3e63 100644 --- a/src/test/test_spiffs.c +++ b/src/test/test_spiffs.c @@ -24,9 +24,9 @@ #include #include -static unsigned char area[FLASH_SIZE]; +static unsigned char area[PHYS_FLASH_SIZE]; -static int erases[sizeof(area)/SECTOR_SIZE]; +static int erases[SPIFFS_FLASH_SIZE/SECTOR_SIZE]; static char _path[256]; static u32_t bytes_rd = 0; static u32_t bytes_wr = 0; @@ -80,6 +80,14 @@ static s32_t _read(u32_t addr, u32_t size, u8_t *dst) { return SPIFFS_ERR_TEST; } } + if (addr < SPIFFS_PHYS_ADDR) { + printf("FATAL read addr too low %08x < %08x\n", addr, SPIFFS_PHYS_ADDR); + exit(0); + } + if (addr + size > SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE) { + printf("FATAL read addr too high %08x + %08x > %08x\n", addr, size, SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE); + exit(0); + } memcpy(dst, &area[addr], size); return 0; } @@ -97,6 +105,16 @@ static s32_t _write(u32_t addr, u32_t size, u8_t *src) { return SPIFFS_ERR_TEST; } } + + if (addr < SPIFFS_PHYS_ADDR) { + printf("FATAL write addr too low %08x < %08x\n", addr, SPIFFS_PHYS_ADDR); + exit(0); + } + if (addr + size > SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE) { + printf("FATAL write addr too high %08x + %08x > %08x\n", addr, size, SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE); + exit(0); + } + for (i = 0; i < size; i++) { if (((addr + i) & (LOG_PAGE-1)) != offsetof(spiffs_page_header, flags)) { if (check_valid_flash && ((area[addr + i] ^ src[i]) & src[i])) { @@ -294,16 +312,17 @@ static void spiffs_check_cb_f(spiffs_check_type type, spiffs_check_report report } void fs_reset() { - memset(area, 0xff, sizeof(area)); + memset(area, 0xcc, sizeof(area)); + memset(&area[SPIFFS_PHYS_ADDR], 0xff, SPIFFS_FLASH_SIZE); spiffs_config c; c.hal_erase_f = _erase; c.hal_read_f = _read; c.hal_write_f = _write; c.log_block_size = LOG_BLOCK; c.log_page_size = LOG_PAGE; - c.phys_addr = 0; + c.phys_addr = SPIFFS_PHYS_ADDR; c.phys_erase_block = SECTOR_SIZE; - c.phys_size = sizeof(area); + c.phys_size = SPIFFS_FLASH_SIZE; memset(erases,0,sizeof(erases)); memset(_cache,0,sizeof(_cache)); @@ -548,6 +567,9 @@ void _teardown() { printf(" fs consistency check:\n"); SPIFFS_check(FS); clear_test_path(); + + //hexdump_mem(&area[SPIFFS_PHYS_ADDR - 16], 32); + //hexdump_mem(&area[SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE - 16], 32); } u32_t tfile_get_size(tfile_size s) {