From 20c257daa25fb8f4fa666c88b8c42b05b4ef9f38 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 30 May 2019 16:16:57 +0700 Subject: [PATCH] more internal flash clean up --- cores/nRF5/Arduino.h | 1 + cores/nRF5/flash/flash_nrf5x.c | 10 ++ cores/nRF5/flash/flash_nrf5x.h | 9 + .../examples/LFS_Format/LFS_Format.ino | 12 +- .../examples/LFS_ReadWrite/LFS_ReadWrite.ino | 4 +- .../src/Adafruit_LittleFS.cpp | 157 ++++++++---------- .../Adafruit_LittleFS/src/Adafruit_LittleFS.h | 10 +- 7 files changed, 104 insertions(+), 99 deletions(-) diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 8ac06a86..9e7b4f80 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -66,6 +66,7 @@ void suspendLoop(void); #include "utility/debug.h" #include "utility/utilities.h" #include "utility/AdaCallback.h" +#include "flash/flash_nrf5x.h" #ifdef USE_TINYUSB #include "Adafruit_TinyUSB_Core.h" diff --git a/cores/nRF5/flash/flash_nrf5x.c b/cores/nRF5/flash/flash_nrf5x.c index 2a2edb6b..6c7014a3 100644 --- a/cores/nRF5/flash/flash_nrf5x.c +++ b/cores/nRF5/flash/flash_nrf5x.c @@ -82,6 +82,16 @@ bool flash_nrf5x_erase(uint32_t addr) return fal_erase(addr); } +bool flash_nrf5x_erase_all(void) +{ + for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE ) + { + VERIFY( flash_nrf5x_erase(addr) ); + } + + return true; +} + //--------------------------------------------------------------------+ // HAL for caching //--------------------------------------------------------------------+ diff --git a/cores/nRF5/flash/flash_nrf5x.h b/cores/nRF5/flash/flash_nrf5x.h index b6cb4656..57cd2676 100644 --- a/cores/nRF5/flash/flash_nrf5x.h +++ b/cores/nRF5/flash/flash_nrf5x.h @@ -29,12 +29,21 @@ #define FLASH_NRF52_PAGE_SIZE 4096 +#ifdef NRF52840_XXAA +#define LFS_FLASH_ADDR 0xED000 +#else +#define LFS_FLASH_ADDR 0x6D000 +#endif + +#define LFS_FLASH_TOTAL_SIZE (7*FLASH_NRF52_PAGE_SIZE) + #ifdef __cplusplus extern "C" { #endif void flash_nrf5x_flush (void); bool flash_nrf5x_erase(uint32_t addr); +bool flash_nrf5x_erase_all(void); uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len); uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len); diff --git a/libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino b/libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino index 63eb8c95..4d447c7e 100644 --- a/libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino +++ b/libraries/Adafruit_LittleFS/examples/LFS_Format/LFS_Format.ino @@ -35,11 +35,15 @@ void setup() // Initialize Internal File System InternalFS.begin(); - Serial.print("Formating ..."); + Serial.print("Formating ... "); + delay(1); // for message appear on monitor - // Format without erase - // Pass true for full external flash erasing (take time) - InternalFS.format(true); + // Erase all sectors of internal flash region for Filesystem. + // Low level format + flash_nrf5x_erase_all(); + + // Format + InternalFS.format(); Serial.println("Done"); } diff --git a/libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino b/libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino index 4a423d79..89b530e1 100644 --- a/libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino +++ b/libraries/Adafruit_LittleFS/examples/LFS_ReadWrite/LFS_ReadWrite.ino @@ -70,11 +70,11 @@ void setup() Serial.println("Failed!"); } } + + Serial.println("Done"); } // the loop function runs over and over again forever void loop() { - digitalToggle(LED_RED); - delay(1000); } diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp index 96d99c39..98c698f8 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp @@ -30,56 +30,12 @@ using namespace Adafruit_LittleFS_Namespace; -#ifdef NRF52840_XXAA -#define LFS_FLASH_ADDR 0xED000 -#else -#define LFS_FLASH_ADDR 0x6D000 -#endif - -#define LFS_FLASH_TOTAL_SIZE (7*FLASH_NRF52_PAGE_SIZE) #define LFS_BLOCK_SIZE 128 //--------------------------------------------------------------------+ // LFS Disk IO //--------------------------------------------------------------------+ -#if CFG_DEBUG - -#define VERIFY_LFS(...) _GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__, dbg_strerr_lfs) -#define PRINT_LFS_ERR(_err) VERIFY_MESS(_err, dbg_strerr_lfs) - -const char* dbg_strerr_lfs (int32_t err) -{ - switch ( err ) - { - case LFS_ERR_OK : return "LFS_ERR_OK"; - case LFS_ERR_IO : return "LFS_ERR_IO"; - case LFS_ERR_CORRUPT : return "LFS_ERR_CORRUPT"; - case LFS_ERR_NOENT : return "LFS_ERR_NOENT"; - case LFS_ERR_EXIST : return "LFS_ERR_EXIST"; - case LFS_ERR_NOTDIR : return "LFS_ERR_NOTDIR"; - case LFS_ERR_ISDIR : return "LFS_ERR_ISDIR"; - case LFS_ERR_NOTEMPTY : return "LFS_ERR_NOTEMPTY"; - case LFS_ERR_BADF : return "LFS_ERR_BADF"; - case LFS_ERR_INVAL : return "LFS_ERR_INVAL"; - case LFS_ERR_NOSPC : return "LFS_ERR_NOSPC"; - case LFS_ERR_NOMEM : return "LFS_ERR_NOMEM"; - - default: - static char errcode[10]; - sprintf(errcode, "%d", err); - return errcode; - } - - return NULL; -} - -#endif - -//--------------------------------------------------------------------+ -// flash API -//--------------------------------------------------------------------+ - static inline uint32_t lba2addr(uint32_t block) { return ((uint32_t) LFS_FLASH_ADDR) + block * LFS_BLOCK_SIZE; @@ -95,7 +51,7 @@ static int _internal_flash_read (const struct lfs_config *c, lfs_block_t block, return 0; } - // Program a region in a block. The block must have previously +// 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, @@ -139,9 +95,7 @@ static int _internal_flash_sync (const struct lfs_config *c) return 0; } -//--------------------------------------------------------------------+ -// Implementation -//--------------------------------------------------------------------+ + static struct lfs_config _InternalFSConfig = { .context = NULL, @@ -165,6 +119,11 @@ static struct lfs_config _InternalFSConfig = Adafruit_LittleFS InternalFS(&_InternalFSConfig); + +//--------------------------------------------------------------------+ +// Implementation +//--------------------------------------------------------------------+ + Adafruit_LittleFS::Adafruit_LittleFS (void) : Adafruit_LittleFS(NULL) { @@ -172,11 +131,9 @@ Adafruit_LittleFS::Adafruit_LittleFS (void) } Adafruit_LittleFS::Adafruit_LittleFS (struct lfs_config* cfg) - { + varclr(&_lfs); _lfs_cfg = cfg; - - _begun = false; _mounted = false; } @@ -185,65 +142,57 @@ Adafruit_LittleFS::~Adafruit_LittleFS () } +// Initialize and mount the file system +// Return true if mounted successfully else probably corrupted. +// User should format the disk and try again bool Adafruit_LittleFS::begin (struct lfs_config * cfg) { - if ( _begun ) return true; + if ( _mounted ) return true; if (cfg) _lfs_cfg = cfg; if (!_lfs_cfg) return false; - _begun = true; - - int err = lfs_mount(&_lfs, _lfs_cfg); - - // reformat if we can't mount the filesystem - if ( LFS_ERR_CORRUPT == err ) - { - LOG_LV1("IFLASH", "Format internal file system"); - this->format(false); - } else { - _mounted = true; - } - - return true; -} - -void Adafruit_LittleFS::_flash_erase_all() -{ - for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE ) - { - flash_nrf5x_erase(addr); - } -} - - -bool Adafruit_LittleFS::format (bool eraseall) -{ - if ( eraseall ) { - _flash_erase_all(); - } - if(_mounted) { - VERIFY_LFS(lfs_unmount(&_lfs), false); - } - VERIFY_LFS(lfs_format(&_lfs, _lfs_cfg), false); VERIFY_LFS(lfs_mount(&_lfs, _lfs_cfg), false); - _mounted = true; return true; } +// Tear down and unmount file system +void Adafruit_LittleFS::end(void) +{ + if (!_mounted) return; + + _mounted = false; + VERIFY_LFS(lfs_unmount(&_lfs), ); +} + +bool Adafruit_LittleFS::format (void) +{ + // if already mounted: umount -> format -> remount + if(_mounted) VERIFY_LFS(lfs_unmount(&_lfs), false); + + VERIFY_LFS(lfs_format(&_lfs, _lfs_cfg), false); + + if (_mounted) VERIFY_LFS(lfs_mount(&_lfs, _lfs_cfg), false); + + return true; +} + +// Open a file or folder Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath, uint8_t mode) { return Adafruit_LittleFS_Namespace::File(filepath, mode, *this); } +// Check if file or folder exists bool Adafruit_LittleFS::exists (char const *filepath) { struct lfs_info info; return 0 == lfs_stat(&_lfs, filepath, &info); } +// Create a directory, create intermediate parent if needed bool Adafruit_LittleFS::mkdir (char const *filepath) { const char* slash = filepath; @@ -275,18 +224,21 @@ bool Adafruit_LittleFS::mkdir (char const *filepath) return true; } +// Remove a file bool Adafruit_LittleFS::remove (char const *filepath) { VERIFY_LFS(lfs_remove(&_lfs, filepath), false); return true; } +// Remove a folder bool Adafruit_LittleFS::rmdir (char const *filepath) { VERIFY_LFS(lfs_remove(&_lfs, filepath)); return true; } +// Remove a folder recursively bool Adafruit_LittleFS::rmdir_r (char const *filepath) { /* adafruit: lfs is modified to remove non-empty folder, @@ -295,3 +247,34 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath) VERIFY_LFS(lfs_remove(&_lfs, filepath)); return true; } + +//------------- Debug -------------// +#if CFG_DEBUG + +const char* dbg_strerr_lfs (int32_t err) +{ + switch ( err ) + { + case LFS_ERR_OK : return "LFS_ERR_OK"; + case LFS_ERR_IO : return "LFS_ERR_IO"; + case LFS_ERR_CORRUPT : return "LFS_ERR_CORRUPT"; + case LFS_ERR_NOENT : return "LFS_ERR_NOENT"; + case LFS_ERR_EXIST : return "LFS_ERR_EXIST"; + case LFS_ERR_NOTDIR : return "LFS_ERR_NOTDIR"; + case LFS_ERR_ISDIR : return "LFS_ERR_ISDIR"; + case LFS_ERR_NOTEMPTY : return "LFS_ERR_NOTEMPTY"; + case LFS_ERR_BADF : return "LFS_ERR_BADF"; + case LFS_ERR_INVAL : return "LFS_ERR_INVAL"; + case LFS_ERR_NOSPC : return "LFS_ERR_NOSPC"; + case LFS_ERR_NOMEM : return "LFS_ERR_NOMEM"; + + default: + static char errcode[10]; + sprintf(errcode, "%d", err); + return errcode; + } + + return NULL; +} + +#endif diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h index 21bf4e43..d3d5ff0f 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h @@ -40,7 +40,8 @@ class Adafruit_LittleFS Adafruit_LittleFS (struct lfs_config* cfg); virtual ~Adafruit_LittleFS (); - bool begin (struct lfs_config * cfg = NULL); + bool begin(struct lfs_config * cfg = NULL); + void end(void); // Open the specified file/directory with the supplied mode (e.g. read or // write, etc). Returns a File object for interacting with the file. @@ -63,8 +64,8 @@ class Adafruit_LittleFS // Delete a folder (recursively) bool rmdir_r (char const *filepath); - // format whole file system - bool format (bool eraseall); + // format file system + bool format (void); lfs_t* getFS(void) { @@ -72,11 +73,8 @@ class Adafruit_LittleFS } protected: - bool _begun; bool _mounted; - virtual void _flash_erase_all(); - private: struct lfs_config* _lfs_cfg; lfs_t _lfs;