Răsfoiți Sursa

initial commit: CustomLFS

taco 1 an în urmă
părinte
comite
c5180d4588

+ 16 - 7
examples/companion_radio/DataStore.cpp

@@ -1,6 +1,12 @@
 #include <Arduino.h>
 #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 <LittleFS.h>
 #elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
-  #include <InternalFileSystem.h>
+  // #include <InternalFileSystem.h> // disabled for now, leaving here for dual fs branch
+  #include <CustomLFS.h>
+  #include <CustomLFS_SPIFlash.h>
 #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();

+ 28 - 2
examples/companion_radio/main.cpp

@@ -14,7 +14,20 @@ static uint32_t _atoi(const char* sp) {
 
 #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
   #include <InternalFileSystem.h>
-  DataStore store(InternalFS, rtc_clock);
+  #if defined(SPIFLASH)
+    #include <CustomLFS_SPIFlash.h>
+    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.h>
+    CustomLFS ExtraFS(0xD4000, 0x19000, 128);
+    DataStore store(ExtraFS, rtc_clock);
+  #else
+    DataStore store(InternalFS, rtc_clock);
+  #endif
+  #endif
 #elif defined(RP2040_PLATFORM)
   #include <LittleFS.h>
   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 <helpers/esp32/SerialWifiInterface.h>
@@ -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

+ 4 - 0
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 ---------------------
 

+ 16 - 5
src/helpers/IdentityStore.h

@@ -4,12 +4,19 @@
   #include <FS.h>
   #define FILESYSTEM  fs::FS
 #elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
-  #include <Adafruit_LittleFS.h>
-  #define FILESYSTEM  Adafruit_LittleFS
+  #if defined(SPIFLASH)
+    #include <CustomLFS_SPIFlash.h>
+    #define FILESYSTEM CustomLFS_SPIFlash
+  #elif defined(EXTRAFS)
+    #include <CustomLFS.h>
+    #define FILESYSTEM CustomLFS
+  #else
+    #include <Adafruit_LittleFS.h>
+    #define FILESYSTEM  Adafruit_LittleFS
 
-  using namespace Adafruit_LittleFS_Namespace;
+    using namespace Adafruit_LittleFS_Namespace;
+  #endif
 #endif
-
 #include <Identity.h>
 
 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);

+ 2 - 1
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}
   +<helpers/nrf52/SerialBLEInterface.cpp>
   +<../examples/companion_radio/*.cpp>