diff --git a/py/Makefile b/py/Makefile new file mode 100644 index 0000000..9b3f92f --- /dev/null +++ b/py/Makefile @@ -0,0 +1,18 @@ +SRC = ../src + +SOURCE_FILES = $(SRC)/spiffs_cache.c \ + $(SRC)/spiffs_check.c \ + $(SRC)/spiffs_gc.c \ + $(SRC)/spiffs_hydrogen.c \ + $(SRC)/spiffs_nucleus.c \ + python_ops.c + +INCLUDES = -I . \ + -I $(SRC)/ \ + -I $(SRC)/default + +spiffs_.so: $(SOURCE_FILES) + $(CC) -g3 -fPIC -shared $(INCLUDES) -o $@ $(SOURCE_FILES) -lm + +clean: + rm -rf spiffs_.so *~ *.pyc diff --git a/py/params_test.h b/py/params_test.h new file mode 100644 index 0000000..e81ff38 --- /dev/null +++ b/py/params_test.h @@ -0,0 +1,8 @@ +#include + +typedef int32_t s32_t; +typedef uint32_t u32_t; +typedef int16_t s16_t; +typedef uint16_t u16_t; +typedef int8_t s8_t; +typedef uint8_t u8_t; diff --git a/py/python_ops.c b/py/python_ops.c new file mode 100644 index 0000000..8cfab5d --- /dev/null +++ b/py/python_ops.c @@ -0,0 +1,88 @@ +#include +#include + +#include "spiffs.h" + +void *my_spiffs_mount(int phys_size, + int phys_addr, + int phys_erase_block, + int log_page_size, + int log_block_size, + s32_t (read_cb)(u32_t addr, u32_t size, u8_t *dst), + s32_t (write_cb)(u32_t addr, u32_t size, u8_t *src), + s32_t (erase_cb)(u32_t addr, u32_t size) + ) +{ + +#define WORK_BUF_SIZE (log_page_size*2) +#define SPIFFS_FDS_SIZE (32*4) +#define SPIFFS_CACHE_BUF_SIZE (phys_size) //(log_page_size+32)*128) + + struct alloced { + spiffs fs; + uint8_t spiffs_work_buf[WORK_BUF_SIZE]; + uint8_t spiffs_fds[SPIFFS_FDS_SIZE]; + uint8_t spiffs_cache_buf[SPIFFS_CACHE_BUF_SIZE]; + }; + + struct alloced *d = malloc(sizeof(struct alloced)); + spiffs *pfs = &d->fs; + + spiffs_config cfg; + cfg.phys_size = phys_size; // use all spi flash + cfg.phys_addr = phys_addr; // start spiffs at start of spi flash + cfg.phys_erase_block = phys_erase_block; // according to datasheet + cfg.log_block_size = log_block_size; // let us not complicate things + cfg.log_page_size = log_page_size; // as we said + + cfg.hal_read_f = read_cb; + cfg.hal_write_f = write_cb; + cfg.hal_erase_f = erase_cb; + + int res = SPIFFS_mount(pfs, + &cfg, + d->spiffs_work_buf, + d->spiffs_fds, + SPIFFS_FDS_SIZE, + d->spiffs_cache_buf, + SPIFFS_CACHE_BUF_SIZE, + 0); + + return res?NULL:(void *)pfs; +} + +int my_spiffs_umount(spiffs *fs) +{ + SPIFFS_clearerr(fs); + SPIFFS_unmount(fs); + + int ret = SPIFFS_errno(fs); + free(fs); + + return ret; +} + +int my_dir(spiffs *fs, + void (entry_cb)(char *name, int size, int id)) +{ + + spiffs_DIR d; + struct spiffs_dirent e; + struct spiffs_dirent *pe = &e; + + int ret; + + SPIFFS_clearerr(fs); + + if (NULL==SPIFFS_opendir(fs, "/", &d)) goto done; + + while ((pe = SPIFFS_readdir(&d, pe))) { + entry_cb(pe->name, pe->size, pe->obj_id); + } + + if (SPIFFS_closedir(&d)) goto done; + SPIFFS_clearerr(fs); + + done: + return SPIFFS_errno(fs); +} diff --git a/py/sp_test.py b/py/sp_test.py new file mode 100755 index 0000000..685c31b --- /dev/null +++ b/py/sp_test.py @@ -0,0 +1,70 @@ +#!/usr/bin/python + +"Test spiffs filesystem with a range of parameters" + +import spiffs + +class SpiffsInstr(spiffs.SpiffsCharsBack): + "Keeps count of issued reads / writes / erases" + def __init__(self, *args, **kwargs): + self.reset_counters() + supe = super(SpiffsInstr, self) + + self.super_read = supe.on_read + self.super_write = supe.on_write + self.super_erase = supe.on_erase + + supe.__init__(*args, **kwargs) + + def reset_counters(self): + self.read_requests = [] + self.write_requests = [] + self.erase_requests = [] + + def on_read(self, addr, size): + self.read_requests.append([addr, size]) + return self.super_read(addr, size) + def on_write(self, addr, data): + self.write_requests.append([addr, len(data)]) + return self.super_write(addr, data) + def on_erase(self, addr, size): + self.erase_requests.append([addr, size]) + return self.super_erase(addr, size) + +# Physical parameters +flash_size = 8*1024*1024 +erase_size = 256 + +print "filesystem size =",flash_size + +header = "log_block_size","log_page_size","reads","read_bytes","writes","written_bytes" +print '| %s |'%(' | '.join(header)) +header2 = ['-'*len(x) for x in header] +print '| %s |'%(' | '.join(header2)) + +for log2_log_block_size in range(14,19): + log_block_size = 1<> tf, "Test message",x, + + print s.dir() + print s.open("Testfile").read() + + print s.dir() + s.remove("Testfile") + + print s.dir() + +if __name__=="__main__": + + file('/tmp/back.bin','w').write('\xff'*8*1024*1024) + + with file('/tmp/back.bin','r+wb') as bf: + s = SpiffsFileBack(bf) + _tests(s) + _destructive_tests(s) + + loc=['\xff']*4*1024*1024 + s = SpiffsCharsBack(loc) + _tests(s) + _destructive_tests(s)