Skip to content

Commit c5180d4

Browse files
committed
initial commit: CustomLFS
1 parent 0ebca4b commit c5180d4

File tree

5 files changed

+66
-15
lines changed

5 files changed

+66
-15
lines changed

examples/companion_radio/DataStore.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#include <Arduino.h>
22
#include "DataStore.h"
33

4+
#if defined(EXTRAFS) || defined(SPIFLASH)
5+
#define MAX_BLOBRECS 100
6+
#else
7+
#define MAX_BLOBRECS 20
8+
#endif
9+
410
DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&clock),
511
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
612
identity_store(fs, "")
@@ -41,7 +47,9 @@ void DataStore::begin() {
4147
#elif defined(RP2040_PLATFORM)
4248
#include <LittleFS.h>
4349
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
44-
#include <InternalFileSystem.h>
50+
// #include <InternalFileSystem.h> // disabled for now, leaving here for dual fs branch
51+
#include <CustomLFS.h>
52+
#include <CustomLFS_SPIFlash.h>
4553
#endif
4654

4755
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
@@ -51,9 +59,9 @@ int _countLfsBlock(void *p, lfs_block_t block){
5159
return 0;
5260
}
5361

54-
lfs_ssize_t _getLfsUsedBlockCount() {
62+
lfs_ssize_t _getLfsUsedBlockCount(FILESYSTEM* fs) {
5563
lfs_size_t size = 0;
56-
lfs_traverse(InternalFS._getFS(), _countLfsBlock, &size);
64+
lfs_traverse(fs->_getFS(), _countLfsBlock, &size);
5765
return size;
5866
}
5967
#endif
@@ -67,8 +75,8 @@ uint32_t DataStore::getStorageUsedKb() const {
6775
_fs->info(info);
6876
return info.usedBytes / 1024;
6977
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
70-
const lfs_config* config = InternalFS._getFS()->cfg;
71-
int usedBlockCount = _getLfsUsedBlockCount();
78+
const lfs_config* config = _fs->_getFS()->cfg;
79+
int usedBlockCount = _getLfsUsedBlockCount(_fs);
7280
int usedBytes = config->block_size * usedBlockCount;
7381
return usedBytes / 1024;
7482
#else
@@ -85,7 +93,7 @@ uint32_t DataStore::getStorageTotalKb() const {
8593
_fs->info(info);
8694
return info.totalBytes / 1024;
8795
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
88-
const lfs_config* config = InternalFS._getFS()->cfg;
96+
const lfs_config* config = _fs->_getFS()->cfg;
8997
int totalBytes = config->block_size * config->block_count;
9098
return totalBytes / 1024;
9199
#else
@@ -109,6 +117,7 @@ bool DataStore::removeFile(const char* filename) {
109117

110118
bool DataStore::formatFileSystem() {
111119
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
120+
// InternalFS.format(); // leaving as placeholder to remind for dual fs branch
112121
return _fs->format();
113122
#elif defined(RP2040_PLATFORM)
114123
return LittleFS.format();
@@ -336,7 +345,7 @@ void DataStore::checkAdvBlobFile() {
336345
if (file) {
337346
BlobRec zeroes;
338347
memset(&zeroes, 0, sizeof(zeroes));
339-
for (int i = 0; i < 20; i++) { // pre-allocate to fixed size
348+
for (int i = 0; i < MAX_BLOBRECS; i++) { // pre-allocate to fixed size
340349
file.write((uint8_t *) &zeroes, sizeof(zeroes));
341350
}
342351
file.close();

examples/companion_radio/main.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,20 @@ static uint32_t _atoi(const char* sp) {
1414

1515
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
1616
#include <InternalFileSystem.h>
17-
DataStore store(InternalFS, rtc_clock);
17+
#if defined(SPIFLASH)
18+
#include <CustomLFS_SPIFlash.h>
19+
const int chipSelect = PIN_QSPI_CS;
20+
SPIClass SPI_2(NRF_SPIM2, PIN_QSPI_IO1, PIN_QSPI_SCK, PIN_QSPI_IO0);
21+
DataStore store(FlashFS, rtc_clock);
22+
#else
23+
#if defined(EXTRAFS)
24+
#include <CustomLFS.h>
25+
CustomLFS ExtraFS(0xD4000, 0x19000, 128);
26+
DataStore store(ExtraFS, rtc_clock);
27+
#else
28+
DataStore store(InternalFS, rtc_clock);
29+
#endif
30+
#endif
1831
#elif defined(RP2040_PLATFORM)
1932
#include <LittleFS.h>
2033
DataStore store(LittleFS, rtc_clock);
@@ -23,6 +36,7 @@ static uint32_t _atoi(const char* sp) {
2336
DataStore store(SPIFFS, rtc_clock);
2437
#endif
2538

39+
2640
#ifdef ESP32
2741
#ifdef WIFI_SSID
2842
#include <helpers/esp32/SerialWifiInterface.h>
@@ -117,7 +131,19 @@ void setup() {
117131
fast_rng.begin(radio_get_rng_seed());
118132

119133
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
120-
InternalFS.begin();
134+
#if defined(SPIFLASH)
135+
if (!FlashFS.begin(chipSelect, SPI_2)) {
136+
// debug output might not be available at this point, might be too early. maybe should fall back to InternalFS here?
137+
MESH_DEBUG_PRINTLN("CustomLFS_SPIFlash: failed to initialize");
138+
} else {
139+
MESH_DEBUG_PRINTLN("CustomLFS_SPIFlash: initialized successfully");
140+
}
141+
#else
142+
InternalFS.begin();
143+
#if defined(EXTRAFS)
144+
ExtraFS.begin();
145+
#endif
146+
#endif
121147
store.begin();
122148
the_mesh.begin(
123149
#ifdef DISPLAY_CLASS

platformio.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ platform = nordicnrf52
7878
build_flags = ${arduino_base.build_flags}
7979
-D NRF52_PLATFORM
8080
-D LFS_NO_ASSERT=1
81+
-D EXTRAFS=1
82+
lib_deps =
83+
${arduino_base.lib_deps}
84+
https://github.com/oltaco/CustomLFS
8185

8286
; ----------------- RP2040 ---------------------
8387

src/helpers/IdentityStore.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
#include <FS.h>
55
#define FILESYSTEM fs::FS
66
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
7-
#include <Adafruit_LittleFS.h>
8-
#define FILESYSTEM Adafruit_LittleFS
7+
#if defined(SPIFLASH)
8+
#include <CustomLFS_SPIFlash.h>
9+
#define FILESYSTEM CustomLFS_SPIFlash
10+
#elif defined(EXTRAFS)
11+
#include <CustomLFS.h>
12+
#define FILESYSTEM CustomLFS
13+
#else
14+
#include <Adafruit_LittleFS.h>
15+
#define FILESYSTEM Adafruit_LittleFS
916

10-
using namespace Adafruit_LittleFS_Namespace;
17+
using namespace Adafruit_LittleFS_Namespace;
18+
#endif
1119
#endif
12-
1320
#include <Identity.h>
1421

1522
class IdentityStore {
@@ -18,7 +25,11 @@ class IdentityStore {
1825
public:
1926
IdentityStore(FILESYSTEM& fs, const char* dir): _fs(&fs), _dir(dir) { }
2027

21-
void begin() { if (_dir && _dir[0] == '/') { _fs->mkdir(_dir); } }
28+
void begin() {
29+
if (_dir && _dir[0] == '/') { _fs->mkdir(_dir); }
30+
31+
32+
}
2233
bool load(const char *name, mesh::LocalIdentity& id);
2334
bool load(const char *name, mesh::LocalIdentity& id, char display_name[], int max_name_sz);
2435
bool save(const char *name, const mesh::LocalIdentity& id);

variants/xiao_nrf52/platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ build_flags =
6565
-D OFFLINE_QUEUE_SIZE=256
6666
; -D BLE_DEBUG_LOGGING=1
6767
; -D MESH_PACKET_LOGGING=1
68-
; -D MESH_DEBUG=1
68+
-D MESH_DEBUG=1
69+
-D SPIFLASH=1
6970
build_src_filter = ${Xiao_nrf52.build_src_filter}
7071
+<helpers/nrf52/SerialBLEInterface.cpp>
7172
+<../examples/companion_radio/*.cpp>

0 commit comments

Comments
 (0)