diff --git a/libraries/FileSystem/src/InternalFS.cpp b/libraries/FileSystem/src/InternalFS.cpp index e580569b..5064f787 100644 --- a/libraries/FileSystem/src/InternalFS.cpp +++ b/libraries/FileSystem/src/InternalFS.cpp @@ -118,79 +118,30 @@ static inline uint32_t lba2addr(uint32_t block) return ((uint32_t) LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE; } -static int _iflash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) -{ - (void) c; - - uint32_t addr = lba2addr(block) + off; - flash_nrf5x_read(buffer, addr, size); - - return 0; -} - -// Program a region in a block. The block must have previously -// been erased. Negative error codes are propogated to the user. -// May return LFS_ERR_CORRUPT if the block should be considered bad. -static int _iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, - lfs_size_t size) -{ - (void) c; - - uint32_t addr = lba2addr(block) + off; - flash_nrf5x_write(addr, buffer, size); - - return 0; -} - -// Erase a block. A block must be erased before being programmed. -// The state of an erased block is undefined. Negative error codes -// are propogated to the user. -// May return LFS_ERR_CORRUPT if the block should be considered bad. -static int _iflash_erase (const struct lfs_config *c, lfs_block_t block) -{ - (void) c; - - uint32_t addr = lba2addr(block); - - // implement as write 0xff to whole block address - for(int i=0; i context)->_flash_read(block, off, buffer, size); +} + +// Program a region in a block. The block must have previously +// been erased. Negative error codes are propogated to the user. +// May return LFS_ERR_CORRUPT if the block should be considered bad. +int LittleFS::_iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, + lfs_size_t size) +{ + return ((LittleFS *)c->context)->_flash_prog(block, off, buffer, size); +} + +// Erase a block. A block must be erased before being programmed. +// The state of an erased block is undefined. Negative error codes +// are propogated to the user. +// May return LFS_ERR_CORRUPT if the block should be considered bad. +int LittleFS::_iflash_erase (const struct lfs_config *c, lfs_block_t block) +{ + return ((LittleFS *)c->context)->_flash_erase(block); +} + +// Sync the state of the underlying block device. Negative error codes +// are propogated to the user. +int LittleFS::_iflash_sync (const struct lfs_config *c) +{ + return ((LittleFS *)c->context)->_flash_sync(); +} diff --git a/libraries/FileSystem/src/InternalFS.h b/libraries/FileSystem/src/InternalFS.h index 248bd9e8..03086487 100644 --- a/libraries/FileSystem/src/InternalFS.h +++ b/libraries/FileSystem/src/InternalFS.h @@ -58,6 +58,12 @@ class LittleFS: public BluefruitFS::FileSystemClass bool rmdir_r (char const *filepath); bool format (bool eraseall); + protected: + bool _begun; + bool _mounted; + + LittleFS (lfs_size_t read_size, lfs_size_t prog_size, lfs_size_t block_size, lfs_size_t block_count, lfs_size_t lookahead); + // Internal API: shouldn't be used by Arduino sketch virtual size_t _f_write (void* fhdl, uint8_t const *buf, size_t size); virtual int _f_read (void* fhdl, void *buf, uint16_t nbyte); @@ -67,17 +73,26 @@ class LittleFS: public BluefruitFS::FileSystemClass virtual uint32_t _f_position (void* fhdl); virtual uint32_t _f_size (void* fhdl); + // Raw flash opperations, override to use an external flash chip + virtual int _flash_read (lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); + virtual int _flash_prog (lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); + virtual int _flash_erase (lfs_block_t block); + virtual void _flash_erase_all(); + virtual int _flash_sync (); + virtual BluefruitFS::File _f_openNextFile (void* fhdl, char const* cwd, uint8_t mode); virtual void _f_rewindDirectory (void* fhdl); - private: struct lfs_config _lfs_cfg; lfs_t _lfs; - bool _begun; - bool _mounted; BluefruitFS::File _open_file (char const *filepath, uint8_t mode); BluefruitFS::File _open_dir (char const *filepath); + + static int _iflash_read (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); + static int _iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); + static int _iflash_erase (const struct lfs_config *c, lfs_block_t block); + static int _iflash_sync (const struct lfs_config *c); }; extern LittleFS InternalFS;