diff --git a/py/python_ops.c b/py/python_ops.c index e69e860..8cfab5d 100644 --- a/py/python_ops.c +++ b/py/python_ops.c @@ -11,15 +11,22 @@ void *my_spiffs_mount(int phys_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) - ) { + ) +{ - spiffs *fs = malloc(sizeof(spiffs)); - - uint8_t *spiffs_work_buf = malloc(log_page_size*2); +#define WORK_BUF_SIZE (log_page_size*2) #define SPIFFS_FDS_SIZE (32*4) - uint8_t *spiffs_fds = malloc(SPIFFS_FDS_SIZE); -#define SPIFFS_CACHE_BUF_SIZE ((log_page_size+32)*4) - uint8_t *spiffs_cache_buf = malloc(SPIFFS_CACHE_BUF_SIZE); +#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 @@ -32,17 +39,27 @@ void *my_spiffs_mount(int phys_size, cfg.hal_write_f = write_cb; cfg.hal_erase_f = erase_cb; - int res = SPIFFS_mount(fs, + int res = SPIFFS_mount(pfs, &cfg, - spiffs_work_buf, - spiffs_fds, + d->spiffs_work_buf, + d->spiffs_fds, SPIFFS_FDS_SIZE, - spiffs_cache_buf, + d->spiffs_cache_buf, SPIFFS_CACHE_BUF_SIZE, 0); - //printf("mount res: %i\n", res); - return res?NULL:(void *)fs; + 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, diff --git a/py/spiffs.py b/py/spiffs.py index 9334751..a1c2676 100755 --- a/py/spiffs.py +++ b/py/spiffs.py @@ -119,15 +119,23 @@ class Spiffs(object): self._wcb = RWCallback(self._on_write) self._ecb = EraseCallback(self._on_erase) - self.fs = spiffs_lib.my_spiffs_mount(self.phys_size, - self.phys_addr, - self.phys_erase_block, - self.log_page_size, - self.log_block_size, - self._rcb, - self._wcb, - self._ecb) + spiffs_lib.my_spiffs_mount.restype = ctypes.c_void_p + fs = spiffs_lib.my_spiffs_mount(self.phys_size, + self.phys_addr, + self.phys_erase_block, + self.log_page_size, + self.log_block_size, + self._rcb, + self._wcb, + self._ecb) + + self.fs = ctypes.c_void_p(fs) + + def unmount(self): + res = spiffs_lib.my_spiffs_umount(self.fs) + if res<0: raise SpiffsException(res) + self.fs = None def _error_print_traceback(f): def ret(self, *args): @@ -334,6 +342,48 @@ class SpiffsFileBack(Spiffs): self.back_fd.seek(addr) self.back_fd.write('\xff'*size) +def _destructive_tests(spiffs_mount): + """Tests that the callbacks are implemented OK. + These tests will damage the filesystem.""" + + print "destructive test" + s = spiffs_mount + + addr = s.phys_erase_block + size = s.phys_erase_block + old_data = s.on_read(addr, size) + s.on_erase(addr, size) + erased = s.on_read(addr, size) + assert erased == '\xff'*size + print "erased ok" + + data = ''.join(chr(x & 0xff) for x in range(size)) + s.on_write(addr,size,data) + readback = s.on_read(addr,size) + assert readback==data + print "wrote ok" + + data2 = '\0'*10 + s.on_write(addr+10,len(data2),data2) + readback = s.on_read(addr,size) + assert readback[20:]==data[20:] + assert readback[:10]==data[:10] + print "part write ok" + assert readback[10:20]==data2 + print "part write ok" + + s.on_write(addr+size, size, data) + s.on_erase(addr,size) + readback = s.on_read(addr+size,size) + assert readback==data + print "erase bounded ok" + + s.on_write(addr,size,data) + data2 = ''.join(chr(0xff^ord(c)) for c in data) + s.on_write(addr,size,data2) + readback = s.on_read(addr,size) + assert readback=='\0'*size + print "AND ok" def _tests(spiffs_mount): s = spiffs_mount @@ -365,7 +415,9 @@ if __name__=="__main__": 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)