Skip to content

Commit 241d0ff

Browse files
Earle F. Philhower, IIIEarle F. Philhower, III
authored andcommitted
Add FS::setConfig to set FS-specific options
Add a new call, FS::setConfig(const {SDFS,SPIFFS}Config *cfg), which takes a FS-specific configuration object and copies any special settings on a per-FS basis. The call is only valid on unmounted filesystems, and checks the type of object passed in matches the FS being configured. Updates the docs and tests to utilize this new configuration method.
1 parent 1b95e5d commit 241d0ff

File tree

10 files changed

+156
-54
lines changed

10 files changed

+156
-54
lines changed

cores/esp8266/FS.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,19 @@ bool Dir::rewind() {
220220
return _impl->rewind();
221221
}
222222

223-
bool FS::begin(const FSConfig *cfg) {
223+
bool FS::setConfig(const FSConfig *cfg) {
224+
if (!_impl || !cfg) {
225+
return false;
226+
}
227+
228+
return _impl->setConfig(cfg);
229+
}
230+
231+
bool FS::begin() {
224232
if (!_impl) {
225233
return false;
226234
}
227-
return _impl->begin(cfg);
235+
return _impl->begin();
228236
}
229237

230238
void FS::end() {

cores/esp8266/FS.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,38 @@ class FSConfig
123123
{
124124
public:
125125
FSConfig(bool autoFormat = true) {
126-
_autoFormat = autoFormat;
126+
_type = FSConfig::fsid::FSId;
127+
_autoFormat = autoFormat;
127128
}
128129

129-
bool _autoFormat;
130+
enum fsid { FSId = 0x00000000 };
131+
FSConfig setAutoFormat(bool val = true) {
132+
_autoFormat = val;
133+
return *this;
134+
}
135+
136+
uint32_t _type;
137+
bool _autoFormat;
138+
};
139+
140+
class SPIFFSConfig : public FSConfig
141+
{
142+
public:
143+
SPIFFSConfig(bool autoFormat = true) {
144+
_type = SPIFFSConfig::fsid::FSId;
145+
_autoFormat = autoFormat;
146+
}
147+
enum fsid { FSId = 0x53504946 };
130148
};
131149

132150
class FS
133151
{
134152
public:
135153
FS(FSImplPtr impl) : _impl(impl) { }
136154

137-
bool begin(const FSConfig *cfg = nullptr);
155+
bool setConfig(const FSConfig *cfg);
156+
157+
bool begin();
138158
void end();
139159

140160
bool format();

cores/esp8266/FSImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class DirImpl {
6969
class FSImpl {
7070
public:
7171
virtual ~FSImpl () { }
72-
virtual bool begin(const FSConfig *cfg) = 0;
72+
virtual bool setConfig(const FSConfig *cfg) = 0;
73+
virtual bool begin() = 0;
7374
virtual void end() = 0;
7475
virtual bool format() = 0;
7576
virtual bool info(FSInfo& info) = 0;

cores/esp8266/spiffs_api.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,16 @@ class SPIFFSImpl : public FSImpl
136136
return false;
137137
}
138138

139-
bool begin(const FSConfig *cfg) override
139+
bool setConfig(const FSConfig *cfg) override
140+
{
141+
if ((cfg->_type != SPIFFSConfig::fsid::FSId) || (SPIFFS_mounted(&_fs) != 0)) {
142+
return false;
143+
}
144+
_cfg = *static_cast<const SPIFFSConfig *>(cfg);
145+
return true;
146+
}
147+
148+
bool begin() override
140149
{
141150
#if defined(ARDUINO) && !defined(CORE_MOCK)
142151
if (&_SPIFFS_end <= &_SPIFFS_start)
@@ -152,7 +161,7 @@ class SPIFFSImpl : public FSImpl
152161
if (_tryMount()) {
153162
return true;
154163
}
155-
if (!cfg || cfg->_autoFormat) {
164+
if (_cfg._autoFormat) {
156165
auto rc = SPIFFS_format(&_fs);
157166
if (rc != SPIFFS_OK) {
158167
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
@@ -296,6 +305,8 @@ class SPIFFSImpl : public FSImpl
296305
std::unique_ptr<uint8_t[]> _workBuf;
297306
std::unique_ptr<uint8_t[]> _fdsBuf;
298307
std::unique_ptr<uint8_t[]> _cacheBuf;
308+
309+
SPIFFSConfig _cfg;
299310
};
300311

301312
#define CHECKFD() while (_fd == 0) { panic(); }
@@ -531,5 +542,4 @@ class SPIFFSDirImpl : public DirImpl
531542
bool _valid;
532543
};
533544

534-
535545
#endif//spiffs_api_h

doc/filesystem.rst

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ directory into ESP8266 flash file system.
125125
File system object (SPIFFS)
126126
---------------------------
127127

128+
setConfig
129+
~~~~~~~~~
130+
131+
.. code:: cpp
132+
133+
SPIFFSConfig cfg;
134+
cfg.setAutoFormat(false);
135+
SPIFFS.setConfig(&cfg);
136+
137+
This method allows you to configure the parameters of a filesystem
138+
before mounting. All filesystems have their own ``*Config`` (i.e.
139+
``SDFSConfig`` or ``SPIFFSConfig`` with their custom set of options.
140+
All filesystems allow explicitly enabling/disabling formatting when
141+
mounts fail. If you do not call this ``setConfig`` method before
142+
perforing ``begin()``, you will get the filesystem's default
143+
behavior and configuration. By default, SPIFFS will autoformat the
144+
filesystem if it cannot mount it, while SDFS will not.
145+
128146
begin
129147
~~~~~
130148

@@ -137,15 +155,6 @@ other FS APIs are used. Returns *true* if file system was mounted
137155
successfully, false otherwise. With no options it will format SPIFFS
138156
if it is unable to mount it on the first try.
139157

140-
.. code:: cpp
141-
142-
auto cfg = FSConfig(false);
143-
SPIFFS.begin(&cfg);
144-
145-
If a ``FSConfig`` object is passed in, with the autoFormat parameter
146-
set to ``false``, then if the filesystem is unable to be mounted it
147-
will return an error code instead of trying to format it.
148-
149158
end
150159
~~~
151160

libraries/SD/src/SD.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class SDClass {
3333
public:
3434
boolean begin(uint8_t csPin, SPISettings cfg = SPI_HALF_SPEED) {
3535
auto fsCfg = SDFSConfig(csPin, cfg);
36-
return (boolean)SDFS.begin(&fsCfg);
36+
SDFS.setConfig(&fsCfg);
37+
return (boolean)SDFS.begin();
3738
}
3839

3940
void end(bool endSPI = true) {

libraries/SDFS/src/SDFS.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ bool SDFSImpl::format() {
146146
return false;
147147
}
148148
SDFSFormatter formatter;
149-
bool ret = formatter.format(&_fs, _csPin, _spiSettings);
149+
bool ret = formatter.format(&_fs, _cfg._csPin, _cfg._spiSettings);
150150
return ret;
151151
}
152152

libraries/SDFS/src/SDFS.h

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,50 @@ class SDFSDirImpl;
4444
class SDFSConfig : public FSConfig
4545
{
4646
public:
47-
SDFSConfig(uint8_t cs, SPISettings spi) {
47+
SDFSConfig() {
48+
_type = SDFSConfig::fsid::FSId;
4849
_autoFormat = false;
49-
_cs = cs;
50-
_spi = spi;
50+
_csPin = 4;
51+
_spiSettings = SD_SCK_MHZ(10);
52+
_part = 0;
53+
}
54+
SDFSConfig(uint8_t csPin, SPISettings spi) {
55+
_type = SDFSConfig::fsid::FSId;
56+
_autoFormat = false;
57+
_csPin = csPin;
58+
_spiSettings = spi;
59+
_part = 0;
60+
}
61+
62+
enum fsid { FSId = 0x53444653 };
63+
64+
SDFSConfig setAutoFormat(bool val = true) {
65+
_autoFormat = val;
66+
return *this;
67+
}
68+
SDFSConfig setCSPin(uint8_t pin) {
69+
_csPin = pin;
70+
return *this;
71+
}
72+
SDFSConfig setSPI(SPISettings spi) {
73+
_spiSettings = spi;
74+
return *this;
75+
}
76+
SDFSConfig setPart(uint8_t part) {
77+
_part = part;
78+
return *this;
5179
}
5280

53-
uint8_t _cs;
54-
SPISettings _spi;
81+
// Inherit _type and _autoFormat
82+
uint8_t _csPin;
83+
uint8_t _part;
84+
SPISettings _spiSettings;
5585
};
5686

5787
class SDFSImpl : public FSImpl
5888
{
5989
public:
60-
SDFSImpl() : _part(0), _csPin(-1), _spiSettings(SPI_FULL_SPEED), _mounted(false)
90+
SDFSImpl() : _mounted(false)
6191
{
6292
}
6393

@@ -98,20 +128,23 @@ class SDFSImpl : public FSImpl
98128
return _mounted ?_fs.rmdir(path) : false;
99129
}
100130

101-
bool begin(const FSConfig *cfg) override {
131+
bool setConfig(const FSConfig *cfg) override
132+
{
133+
if ((cfg->_type != SDFSConfig::fsid::FSId) || _mounted) {
134+
return false;
135+
}
136+
_cfg = *static_cast<const SDFSConfig *>(cfg);
137+
return true;
138+
}
139+
140+
bool begin() override {
102141
if (_mounted) {
103142
end();
104143
}
105-
if (!cfg) {
106-
return false; // You need to tell me the CS and SPI setup or I can't do anything!
107-
}
108-
const SDFSConfig *myCfg = static_cast<const SDFSConfig *>(cfg);
109-
_csPin = myCfg->_cs;
110-
_spiSettings = myCfg->_spi;
111-
_mounted = _fs.begin(_csPin, _spiSettings);
112-
if (!_mounted && myCfg->_autoFormat) {
144+
_mounted = _fs.begin(_cfg._csPin, _cfg._spiSettings);
145+
if (!_mounted && _cfg._autoFormat) {
113146
format();
114-
_mounted = _fs.begin(_csPin, _spiSettings);
147+
_mounted = _fs.begin(_cfg._csPin, _cfg._spiSettings);
115148
}
116149
return _mounted;
117150
}
@@ -123,12 +156,6 @@ class SDFSImpl : public FSImpl
123156

124157
bool format() override;
125158

126-
// SDFS-only configuration calls
127-
void setSDFSIOConfig(int8_t csPin, SPISettings spiConfig) {
128-
_csPin = csPin;
129-
_spiSettings = spiConfig;
130-
}
131-
132159
protected:
133160
friend class SDFileImpl;
134161
friend class SDFSDirImpl;
@@ -159,9 +186,7 @@ class SDFSImpl : public FSImpl
159186
}
160187

161188
sdfat::SdFat _fs;
162-
uint8_t _part;
163-
int8_t _csPin;
164-
SPISettings _spiSettings;
189+
SDFSConfig _cfg;
165190
bool _mounted;
166191
};
167192

libraries/esp8266/keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ SPIFFS KEYWORD1
9393
SDFS KEYWORD1
9494
File KEYWORD1
9595
Dir KEYWORD1
96-
FSConfig KEYWORD1
96+
SPIFFSConfig KEYWORD1
9797

9898
# Seek enums
9999
SeekSet LITERAL1

tests/host/fs/test_fs.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,41 @@ static std::set<String> listDir (const char* path)
5151
return result;
5252
}
5353

54+
TEST_CASE("SPIFFS checks the config object passed in", "[fs]")
55+
{
56+
SPIFFS_MOCK_DECLARE(64, 8, 512, false);
57+
FSConfig f;
58+
SPIFFSConfig s;
59+
SDFSConfig d;
60+
61+
REQUIRE_FALSE(SPIFFS.setConfig(&f));
62+
REQUIRE(SPIFFS.setConfig(&s));
63+
REQUIRE_FALSE(SPIFFS.setConfig(&d));
64+
}
65+
66+
TEST_CASE("SDFS checks the config object passed in", "[fs]")
67+
{
68+
SDFS_MOCK_DECLARE();
69+
FSConfig f;
70+
SPIFFSConfig s;
71+
SDFSConfig d;
72+
73+
REQUIRE_FALSE(SDFS.setConfig(&f));
74+
REQUIRE_FALSE(SDFS.setConfig(&s));
75+
REQUIRE(SDFS.setConfig(&d));
76+
}
77+
5478
TEST_CASE("FS can begin","[fs]")
5579
{
5680
SPIFFS_MOCK_DECLARE(64, 8, 512, false);
81+
SPIFFSConfig cfg;
82+
cfg.setAutoFormat(false);
83+
SPIFFS.setConfig(&cfg);
84+
REQUIRE_FALSE(SPIFFS.begin());
85+
cfg.setAutoFormat(true);
86+
SPIFFS.setConfig(&cfg);
5787
REQUIRE(SPIFFS.begin());
88+
REQUIRE_FALSE(SPIFFS.setConfig(&cfg)); // Can't change config of mounted FS
5889
}
5990

6091
TEST_CASE("FS can't begin with zero size","[fs]")
@@ -191,11 +222,11 @@ TEST_CASE("#1819 Can list all files with openDir(\"\")", "[fs][bugreport]")
191222
TEST_CASE("SDFS", "[sdfs]")
192223
{
193224
SDFS_MOCK_DECLARE();
194-
SDFS.end();
195225
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
196-
SDFS.begin(&cfg);
226+
SDFS.setConfig(&cfg);
197227
REQUIRE(SDFS.format());
198-
REQUIRE(SDFS.begin(&cfg));
228+
REQUIRE(SDFS.begin());
229+
REQUIRE_FALSE(SDFS.setConfig(&cfg)); // Can't change config of mounted fs
199230
REQUIRE(SDFS.mkdir("/happy/face"));
200231
REQUIRE(SDFS.mkdir("/happy/nose"));
201232
REQUIRE(SDFS.rmdir("/happy/face"));
@@ -210,8 +241,7 @@ TEST_CASE("Files.ino example", "[sd]")
210241
SDFS_MOCK_DECLARE();
211242
SDFS.end();
212243
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
213-
SDFS.begin(&cfg);
214-
SDFS.end();
244+
SDFS.setConfig(&cfg);
215245
REQUIRE(SDFS.format());
216246
REQUIRE(SD.begin(4));
217247
REQUIRE_FALSE(SD.exists("example.txt"));
@@ -246,10 +276,8 @@ static String readFileSD(const char* name)
246276
TEST_CASE("Listfiles.ino example", "[sd]")
247277
{
248278
SDFS_MOCK_DECLARE();
249-
SDFS.end();
250279
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
251-
SDFS.begin(&cfg);
252-
SDFS.end();
280+
SDFS.setConfig(&cfg);
253281
REQUIRE(SDFS.format());
254282
REQUIRE(SD.begin(4));
255283

0 commit comments

Comments
 (0)