diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp index 2622b809..96d99c39 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp @@ -76,35 +76,105 @@ const char* dbg_strerr_lfs (int32_t err) #endif +//--------------------------------------------------------------------+ +// flash API +//--------------------------------------------------------------------+ + static inline uint32_t lba2addr(uint32_t block) { return ((uint32_t) LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE; } +static int _internal_flash_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 _internal_flash_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 _internal_flash_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 Adafruit_LittleFS::_iflash_prog (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, - lfs_size_t size) -{ - return ((Adafruit_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 Adafruit_LittleFS::_iflash_erase (const struct lfs_config *c, lfs_block_t block) -{ - return ((Adafruit_LittleFS *)c->context)->_flash_erase(block); -} - -// Sync the state of the underlying block device. Negative error codes -// are propogated to the user. -int Adafruit_LittleFS::_iflash_sync (const struct lfs_config *c) -{ - return ((Adafruit_LittleFS *)c->context)->_flash_sync(); -} diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h index 3506fdea..21bf4e43 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h @@ -37,9 +37,10 @@ class Adafruit_LittleFS { public: Adafruit_LittleFS (void); + Adafruit_LittleFS (struct lfs_config* cfg); virtual ~Adafruit_LittleFS (); - bool begin (void); + bool begin (struct lfs_config * cfg = NULL); // Open the specified file/directory with the supplied mode (e.g. read or // write, etc). Returns a File object for interacting with the file. @@ -74,24 +75,11 @@ class Adafruit_LittleFS bool _begun; bool _mounted; - Adafruit_LittleFS (uint32_t read_size, uint32_t prog_size, uint32_t block_size, uint32_t block_count, uint32_t lookahead); - - // Internal API: shouldn't be used by Arduino sketch - // 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 (); private: - struct lfs_config _lfs_cfg; + struct lfs_config* _lfs_cfg; lfs_t _lfs; - - 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 Adafruit_LittleFS InternalFS;