From c5180d4588b226f91933f6f2d31f312b610d6346 Mon Sep 17 00:00:00 2001 From: taco Date: Tue, 29 Jul 2025 20:06:35 +1000 Subject: [PATCH] initial commit: CustomLFS --- examples/companion_radio/DataStore.cpp | 23 ++++++++++++++------ examples/companion_radio/main.cpp | 30 ++++++++++++++++++++++++-- platformio.ini | 4 ++++ src/helpers/IdentityStore.h | 21 +++++++++++++----- variants/xiao_nrf52/platformio.ini | 3 ++- 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index b5d70edc..8e774d91 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -1,6 +1,12 @@ #include #include "DataStore.h" +#if defined(EXTRAFS) || defined(SPIFLASH) + #define MAX_BLOBRECS 100 +#else + #define MAX_BLOBRECS 20 +#endif + DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&clock), #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) identity_store(fs, "") @@ -41,7 +47,9 @@ void DataStore::begin() { #elif defined(RP2040_PLATFORM) #include #elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) - #include + // #include // disabled for now, leaving here for dual fs branch + #include + #include #endif #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) @@ -51,9 +59,9 @@ int _countLfsBlock(void *p, lfs_block_t block){ return 0; } -lfs_ssize_t _getLfsUsedBlockCount() { +lfs_ssize_t _getLfsUsedBlockCount(FILESYSTEM* fs) { lfs_size_t size = 0; - lfs_traverse(InternalFS._getFS(), _countLfsBlock, &size); + lfs_traverse(fs->_getFS(), _countLfsBlock, &size); return size; } #endif @@ -67,8 +75,8 @@ uint32_t DataStore::getStorageUsedKb() const { _fs->info(info); return info.usedBytes / 1024; #elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) - const lfs_config* config = InternalFS._getFS()->cfg; - int usedBlockCount = _getLfsUsedBlockCount(); + const lfs_config* config = _fs->_getFS()->cfg; + int usedBlockCount = _getLfsUsedBlockCount(_fs); int usedBytes = config->block_size * usedBlockCount; return usedBytes / 1024; #else @@ -85,7 +93,7 @@ uint32_t DataStore::getStorageTotalKb() const { _fs->info(info); return info.totalBytes / 1024; #elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) - const lfs_config* config = InternalFS._getFS()->cfg; + const lfs_config* config = _fs->_getFS()->cfg; int totalBytes = config->block_size * config->block_count; return totalBytes / 1024; #else @@ -109,6 +117,7 @@ bool DataStore::removeFile(const char* filename) { bool DataStore::formatFileSystem() { #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) + // InternalFS.format(); // leaving as placeholder to remind for dual fs branch return _fs->format(); #elif defined(RP2040_PLATFORM) return LittleFS.format(); @@ -336,7 +345,7 @@ void DataStore::checkAdvBlobFile() { if (file) { BlobRec zeroes; memset(&zeroes, 0, sizeof(zeroes)); - for (int i = 0; i < 20; i++) { // pre-allocate to fixed size + for (int i = 0; i < MAX_BLOBRECS; i++) { // pre-allocate to fixed size file.write((uint8_t *) &zeroes, sizeof(zeroes)); } file.close(); diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 1d5ec564..4900d3bc 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -14,7 +14,20 @@ static uint32_t _atoi(const char* sp) { #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) #include - DataStore store(InternalFS, rtc_clock); + #if defined(SPIFLASH) + #include + const int chipSelect = PIN_QSPI_CS; + SPIClass SPI_2(NRF_SPIM2, PIN_QSPI_IO1, PIN_QSPI_SCK, PIN_QSPI_IO0); + DataStore store(FlashFS, rtc_clock); + #else + #if defined(EXTRAFS) + #include + CustomLFS ExtraFS(0xD4000, 0x19000, 128); + DataStore store(ExtraFS, rtc_clock); + #else + DataStore store(InternalFS, rtc_clock); + #endif + #endif #elif defined(RP2040_PLATFORM) #include DataStore store(LittleFS, rtc_clock); @@ -23,6 +36,7 @@ static uint32_t _atoi(const char* sp) { DataStore store(SPIFFS, rtc_clock); #endif + #ifdef ESP32 #ifdef WIFI_SSID #include @@ -117,7 +131,19 @@ void setup() { fast_rng.begin(radio_get_rng_seed()); #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) - InternalFS.begin(); + #if defined(SPIFLASH) + if (!FlashFS.begin(chipSelect, SPI_2)) { + // debug output might not be available at this point, might be too early. maybe should fall back to InternalFS here? + MESH_DEBUG_PRINTLN("CustomLFS_SPIFlash: failed to initialize"); + } else { + MESH_DEBUG_PRINTLN("CustomLFS_SPIFlash: initialized successfully"); + } + #else + InternalFS.begin(); + #if defined(EXTRAFS) + ExtraFS.begin(); + #endif + #endif store.begin(); the_mesh.begin( #ifdef DISPLAY_CLASS diff --git a/platformio.ini b/platformio.ini index 1c89465f..5fe46986 100644 --- a/platformio.ini +++ b/platformio.ini @@ -78,6 +78,10 @@ platform = nordicnrf52 build_flags = ${arduino_base.build_flags} -D NRF52_PLATFORM -D LFS_NO_ASSERT=1 + -D EXTRAFS=1 +lib_deps = + ${arduino_base.lib_deps} + https://github.com/oltaco/CustomLFS ; ----------------- RP2040 --------------------- diff --git a/src/helpers/IdentityStore.h b/src/helpers/IdentityStore.h index e81290e7..15219a44 100644 --- a/src/helpers/IdentityStore.h +++ b/src/helpers/IdentityStore.h @@ -4,12 +4,19 @@ #include #define FILESYSTEM fs::FS #elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) - #include - #define FILESYSTEM Adafruit_LittleFS + #if defined(SPIFLASH) + #include + #define FILESYSTEM CustomLFS_SPIFlash + #elif defined(EXTRAFS) + #include + #define FILESYSTEM CustomLFS + #else + #include + #define FILESYSTEM Adafruit_LittleFS - using namespace Adafruit_LittleFS_Namespace; + using namespace Adafruit_LittleFS_Namespace; + #endif #endif - #include class IdentityStore { @@ -18,7 +25,11 @@ class IdentityStore { public: IdentityStore(FILESYSTEM& fs, const char* dir): _fs(&fs), _dir(dir) { } - void begin() { if (_dir && _dir[0] == '/') { _fs->mkdir(_dir); } } + void begin() { + if (_dir && _dir[0] == '/') { _fs->mkdir(_dir); } + + + } bool load(const char *name, mesh::LocalIdentity& id); bool load(const char *name, mesh::LocalIdentity& id, char display_name[], int max_name_sz); bool save(const char *name, const mesh::LocalIdentity& id); diff --git a/variants/xiao_nrf52/platformio.ini b/variants/xiao_nrf52/platformio.ini index fd4c362b..3717c26d 100644 --- a/variants/xiao_nrf52/platformio.ini +++ b/variants/xiao_nrf52/platformio.ini @@ -65,7 +65,8 @@ build_flags = -D OFFLINE_QUEUE_SIZE=256 ; -D BLE_DEBUG_LOGGING=1 ; -D MESH_PACKET_LOGGING=1 -; -D MESH_DEBUG=1 + -D MESH_DEBUG=1 + -D SPIFLASH=1 build_src_filter = ${Xiao_nrf52.build_src_filter} + +<../examples/companion_radio/*.cpp>