From 70305da5e79cbe26897b98c6236997bf3c94a74c Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 25 Oct 2018 17:21:23 +0700 Subject: [PATCH] add filesystem format() --- cores/nRF5/flash/flash_qspi.c | 7 +++ cores/nRF5/flash/flash_qspi.h | 3 +- .../External_Format/External_Format.ino | 53 +++++++++++++++++++ .../Internal_Format/Internal_Format.ino | 53 +++++++++++++++++++ libraries/FileSystem/src/ExternalFS.cpp | 19 +++++-- libraries/FileSystem/src/ExternalFS.h | 1 + libraries/FileSystem/src/FileIO.h | 3 ++ libraries/FileSystem/src/InternalFS.cpp | 29 +++++++--- libraries/FileSystem/src/InternalFS.h | 1 + 9 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 libraries/FileSystem/examples/External_Format/External_Format.ino create mode 100644 libraries/FileSystem/examples/Internal_Format/Internal_Format.ino diff --git a/cores/nRF5/flash/flash_qspi.c b/cores/nRF5/flash/flash_qspi.c index 8d5649cb..0ad75b6d 100644 --- a/cores/nRF5/flash/flash_qspi.c +++ b/cores/nRF5/flash/flash_qspi.c @@ -228,6 +228,13 @@ bool fal_qspi_erase (uint32_t addr) return true; } +bool flash_qspi_chiperase (void) +{ + VERIFY(_flash_dev); + VERIFY(NRFX_SUCCESS == nrfx_qspi_erase(NRF_QSPI_ERASE_LEN_ALL, 0)); + return true; +} + static uint32_t fal_qspi_program (uint32_t dst, void const * src, uint32_t len) { VERIFY(_flash_dev, 0); diff --git a/cores/nRF5/flash/flash_qspi.h b/cores/nRF5/flash/flash_qspi.h index 606bda5f..24cdca44 100644 --- a/cores/nRF5/flash/flash_qspi.h +++ b/cores/nRF5/flash/flash_qspi.h @@ -46,9 +46,10 @@ #endif void flash_qspi_init (void); +uint32_t flash_qspi_size (void); void flash_qspi_flush (void); bool flash_qspi_erase (uint32_t addr); -uint32_t flash_qspi_size (void); +bool flash_qspi_chiperase (void); uint32_t flash_qspi_write (uint32_t dst, void const * src, uint32_t len); uint32_t flash_qspi_read (void* dst, uint32_t src, uint32_t len); diff --git a/libraries/FileSystem/examples/External_Format/External_Format.ino b/libraries/FileSystem/examples/External_Format/External_Format.ino new file mode 100644 index 00000000..55408076 --- /dev/null +++ b/libraries/FileSystem/examples/External_Format/External_Format.ino @@ -0,0 +1,53 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include + +// the setup function runs once when you press reset or power the board +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("ExternalFS Format Example"); + Serial.println(); + + // Wait for user input to run. + Serial.println("This sketch will destroy all of your data in External Flash!"); + Serial.print("Enter any keys to continue:"); + while ( !Serial.available() ) + { + delay(1); + } + Serial.println(); + Serial.println(); + + // Initialize Internal File System + ExternalFS.begin(); + + Serial.print("Formating ..."); + delay(1); + + // Format without erase + ExternalFS.format(true); + + Serial.println("Done"); + delay(1); +} + +// the loop function runs over and over again forever +void loop() +{ + // nothing to do +} diff --git a/libraries/FileSystem/examples/Internal_Format/Internal_Format.ino b/libraries/FileSystem/examples/Internal_Format/Internal_Format.ino new file mode 100644 index 00000000..4602c031 --- /dev/null +++ b/libraries/FileSystem/examples/Internal_Format/Internal_Format.ino @@ -0,0 +1,53 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include + +// the setup function runs once when you press reset or power the board +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("InternalFS Format Example"); + Serial.println(); + + // Wait for user input to run. + Serial.println("This sketch will destroy all of your data in External Flash!"); + Serial.print("Enter any keys to continue:"); + while ( !Serial.available() ) + { + delay(1); + } + Serial.println(); + Serial.println(); + + // Initialize Internal File System + InternalFS.begin(); + + Serial.print("Formating ..."); + delay(1); + + // Format without erase + InternalFS.format(true); + + Serial.println("Done"); + delay(1); +} + +// the loop function runs over and over again forever +void loop() +{ + // nothing to do +} diff --git a/libraries/FileSystem/src/ExternalFS.cpp b/libraries/FileSystem/src/ExternalFS.cpp index 0b696f15..c4c69ed8 100644 --- a/libraries/FileSystem/src/ExternalFS.cpp +++ b/libraries/FileSystem/src/ExternalFS.cpp @@ -39,6 +39,7 @@ #include #include #include "ExternalFS.h" +#include "flash/flash_qspi.h" // up to 11 characters #define VOLUME_LABEL "BLUEFRUIT" @@ -135,11 +136,7 @@ bool FatFS::begin (void) if ( FR_NO_FILESYSTEM == err ) { LOG_LV1("FATFS", "No file system, format it"); - - uint8_t workbuf[FF_MAX_SS]; - VERIFY_FAT(f_mkfs("", FM_FAT | FM_SFD, 4096, workbuf, FF_MAX_SS), false); - - VERIFY_FAT(f_setlabel(VOLUME_LABEL), false); + this->format(false); // FatFile readme; // readme.open("readme.txt", FAT_FILE_WRITE | FAT_FILE_CREATE_ALWAYS); @@ -156,6 +153,18 @@ bool FatFS::begin (void) return true; } +bool FatFS::format (bool eraseall) +{ + if ( eraseall ) VERIFY(flash_qspi_chiperase()); + + uint8_t workbuf[FF_MAX_SS]; + VERIFY_FAT(f_mkfs("", FM_FAT | FM_SFD, 4096, workbuf, FF_MAX_SS), false); + VERIFY_FAT(f_setlabel(VOLUME_LABEL), false); + VERIFY_FAT(f_mount(_fs, "", 1), false); + + return true; +} + File FatFS::_open_file (char const *filepath, uint8_t mode) { BluefuritLib::File file(*this); diff --git a/libraries/FileSystem/src/ExternalFS.h b/libraries/FileSystem/src/ExternalFS.h index d0385454..9439eaf1 100644 --- a/libraries/FileSystem/src/ExternalFS.h +++ b/libraries/FileSystem/src/ExternalFS.h @@ -56,6 +56,7 @@ class FatFS: public BluefuritLib::FileSystemClass bool remove (char const *filepath); bool rmdir (char const *filepath); bool rmdir_r (char const *filepath); + bool format (bool eraseall); 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); diff --git a/libraries/FileSystem/src/FileIO.h b/libraries/FileSystem/src/FileIO.h index 193bfe2c..4c5bb2c9 100644 --- a/libraries/FileSystem/src/FileIO.h +++ b/libraries/FileSystem/src/FileIO.h @@ -118,6 +118,9 @@ class FileSystemClass // Delete a folder (recursively) virtual bool rmdir_r (char const *filepath) = 0; + // format whole file system + virtual bool format (bool eraseall); + //--------------------------------------------------------------------+ // Internal API, shouldn't call directly //--------------------------------------------------------------------+ diff --git a/libraries/FileSystem/src/InternalFS.cpp b/libraries/FileSystem/src/InternalFS.cpp index 9f7edd3b..3ccaf6b7 100644 --- a/libraries/FileSystem/src/InternalFS.cpp +++ b/libraries/FileSystem/src/InternalFS.cpp @@ -40,13 +40,13 @@ #include "flash/flash_nrf5x.h" #ifdef NRF52840_XXAA -#define LFS_FLASH_ADDR 0xED000 +#define LFS_FLASH_ADDR 0xED000 #else -#define LFS_FLASH_ADDR 0x6D000 +#define LFS_FLASH_ADDR 0x6D000 #endif -#define LFS_FLASH_SIZE (7*FLASH_NRF52_PAGE_SIZE) -#define LFS_BLOCK_SIZE 128 +#define LFS_FLASH_TOTAL_SIZE (7*FLASH_NRF52_PAGE_SIZE) +#define LFS_BLOCK_SIZE 128 //--------------------------------------------------------------------+ // LFS Disk IO @@ -182,7 +182,7 @@ LittleFS::LittleFS (void) _lfs_cfg.read_size = LFS_BLOCK_SIZE; _lfs_cfg.prog_size = LFS_BLOCK_SIZE; _lfs_cfg.block_size = LFS_BLOCK_SIZE; - _lfs_cfg.block_count = LFS_FLASH_SIZE / LFS_BLOCK_SIZE; + _lfs_cfg.block_count = LFS_FLASH_TOTAL_SIZE / LFS_BLOCK_SIZE; _lfs_cfg.lookahead = 128; _begun = false; @@ -204,13 +204,28 @@ bool LittleFS::begin (void) if ( LFS_ERR_CORRUPT == err ) { LOG_LV1("IFLASH", "Format internal file system"); - VERIFY_LFS(lfs_format(&_lfs, &_lfs_cfg), false); - VERIFY_LFS(lfs_mount(&_lfs, &_lfs_cfg), false); + this->format(false); } return true; } +bool LittleFS::format (bool eraseall) +{ + if ( eraseall ) + { + for ( uint32_t addr = LFS_FLASH_ADDR; addr < LFS_FLASH_ADDR + LFS_FLASH_TOTAL_SIZE; addr += FLASH_NRF52_PAGE_SIZE ) + { + flash_nrf5x_erase(addr); + } + } + + VERIFY_LFS(lfs_format(&_lfs, &_lfs_cfg), false); + VERIFY_LFS(lfs_mount(&_lfs, &_lfs_cfg), false); + + return true; +} + BluefuritLib::File LittleFS::_open_file (char const *filepath, uint8_t mode) { BluefuritLib::File file(*this); diff --git a/libraries/FileSystem/src/InternalFS.h b/libraries/FileSystem/src/InternalFS.h index 2381c8a4..29c576a3 100644 --- a/libraries/FileSystem/src/InternalFS.h +++ b/libraries/FileSystem/src/InternalFS.h @@ -56,6 +56,7 @@ class LittleFS: public BluefuritLib::FileSystemClass bool remove (char const *filepath); bool rmdir (char const *filepath); bool rmdir_r (char const *filepath); + bool format (bool eraseall); 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);