Skip to content

Commit 1b95e5d

Browse files
Earle F. Philhower, IIIEarle F. Philhower, III
Earle F. Philhower, III
authored and
Earle F. Philhower, III
committed
Add a FSConfig and SDFSConfig param to FS.begin()
Allows for configuration values to be passed into a filesystem via the begin method. By default, a FS will receive a nullptr and should so whatever is appropriate. The base FSConfig class has one parameter, _autoFormat, set by the default constructor to true. For SPIFFS, you can now disable auto formatting on mount failure by passing in a FSConfig(false) object. For SDFS a SDFSConfig parameter can be passed into config specifying the chip select and SPI configuration. If nothing is passed in, the begin will fail since there are no safe default values here.
1 parent 4556403 commit 1b95e5d

File tree

10 files changed

+71
-28
lines changed

10 files changed

+71
-28
lines changed

cores/esp8266/FS.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ bool Dir::rewind() {
220220
return _impl->rewind();
221221
}
222222

223-
bool FS::begin() {
223+
bool FS::begin(const FSConfig *cfg) {
224224
if (!_impl) {
225225
return false;
226226
}
227-
return _impl->begin();
227+
return _impl->begin(cfg);
228228
}
229229

230230
void FS::end() {

cores/esp8266/FS.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,22 @@ struct FSInfo {
119119
size_t maxPathLength;
120120
};
121121

122+
class FSConfig
123+
{
124+
public:
125+
FSConfig(bool autoFormat = true) {
126+
_autoFormat = autoFormat;
127+
}
128+
129+
bool _autoFormat;
130+
};
131+
122132
class FS
123133
{
124134
public:
125135
FS(FSImplPtr impl) : _impl(impl) { }
126136

127-
bool begin();
137+
bool begin(const FSConfig *cfg = nullptr);
128138
void end();
129139

130140
bool format();
@@ -166,6 +176,7 @@ using fs::SeekSet;
166176
using fs::SeekCur;
167177
using fs::SeekEnd;
168178
using fs::FSInfo;
179+
using fs::FSConfig;
169180
#endif //FS_NO_GLOBALS
170181

171182
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)

cores/esp8266/FSImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class DirImpl {
6969
class FSImpl {
7070
public:
7171
virtual ~FSImpl () { }
72-
virtual bool begin() = 0;
72+
virtual bool begin(const FSConfig *cfg) = 0;
7373
virtual void end() = 0;
7474
virtual bool format() = 0;
7575
virtual bool info(FSInfo& info) = 0;

cores/esp8266/spiffs_api.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class SPIFFSImpl : public FSImpl
136136
return false;
137137
}
138138

139-
bool begin() override
139+
bool begin(const FSConfig *cfg) override
140140
{
141141
#if defined(ARDUINO) && !defined(CORE_MOCK)
142142
if (&_SPIFFS_end <= &_SPIFFS_start)
@@ -152,12 +152,16 @@ class SPIFFSImpl : public FSImpl
152152
if (_tryMount()) {
153153
return true;
154154
}
155-
auto rc = SPIFFS_format(&_fs);
156-
if (rc != SPIFFS_OK) {
157-
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
155+
if (!cfg || cfg->_autoFormat) {
156+
auto rc = SPIFFS_format(&_fs);
157+
if (rc != SPIFFS_OK) {
158+
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
159+
return false;
160+
}
161+
return _tryMount();
162+
} else {
158163
return false;
159164
}
160-
return _tryMount();
161165
}
162166

163167
void end() override

doc/filesystem.rst

+11-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,17 @@ begin
134134
135135
This method mounts SPIFFS file system. It must be called before any
136136
other FS APIs are used. Returns *true* if file system was mounted
137-
successfully, false otherwise.
137+
successfully, false otherwise. With no options it will format SPIFFS
138+
if it is unable to mount it on the first try.
139+
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.
138148

139149
end
140150
~~~

libraries/SD/src/SD.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
class SDClass {
3333
public:
3434
boolean begin(uint8_t csPin, SPISettings cfg = SPI_HALF_SPEED) {
35-
setSDFSConfig(csPin, cfg);
36-
return (boolean)SDFS.begin();
35+
auto fsCfg = SDFSConfig(csPin, cfg);
36+
return (boolean)SDFS.begin(&fsCfg);
3737
}
3838

3939
void end(bool endSPI = true) {
@@ -44,8 +44,7 @@ class SDClass {
4444
}
4545

4646
File open(const char *filename, uint8_t mode = FILE_READ) {
47-
File f = SDFS.open(filename, getMode(mode));
48-
return f;
47+
return SDFS.open(filename, getMode(mode));
4948
}
5049

5150
File open(const String &filename, uint8_t mode = FILE_READ) {

libraries/SDFS/src/SDFS.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ using namespace fs;
3434
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SDFS)
3535
static sdfs::SDFSImpl *_SDFSImpl = new sdfs::SDFSImpl();
3636
FS SDFS = FS(FSImplPtr(_SDFSImpl));
37-
38-
// Global configuration functions
39-
void setSDFSConfig(int8_t csPin, SPISettings spiConfig) {
40-
_SDFSImpl->setSDFSIOConfig(csPin, spiConfig);
41-
}
4237
#endif
4338

4439
namespace sdfs {

libraries/SDFS/src/SDFS.h

+23-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ namespace sdfs {
4141

4242
class SDFSFileImpl;
4343
class SDFSDirImpl;
44+
class SDFSConfig : public FSConfig
45+
{
46+
public:
47+
SDFSConfig(uint8_t cs, SPISettings spi) {
48+
_autoFormat = false;
49+
_cs = cs;
50+
_spi = spi;
51+
}
52+
53+
uint8_t _cs;
54+
SPISettings _spi;
55+
};
4456

4557
class SDFSImpl : public FSImpl
4658
{
@@ -86,14 +98,20 @@ class SDFSImpl : public FSImpl
8698
return _mounted ?_fs.rmdir(path) : false;
8799
}
88100

89-
bool begin() override {
101+
bool begin(const FSConfig *cfg) override {
90102
if (_mounted) {
91103
end();
92104
}
93-
if (_csPin >= 0) {
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) {
113+
format();
94114
_mounted = _fs.begin(_csPin, _spiSettings);
95-
} else {
96-
_mounted = _fs.begin();
97115
}
98116
return _mounted;
99117
}
@@ -347,7 +365,7 @@ class SDFSDirImpl : public DirImpl
347365

348366
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SDFS)
349367
extern FS SDFS;
350-
extern void setSDFSConfig(int8_t csPin, SPISettings spiConfig = SD_SCK_MHZ(10));
368+
using sdfs::SDFSConfig;
351369
#endif
352370

353371
#endif // SDFS.h

libraries/esp8266/keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ SPIFFS KEYWORD1
9393
SDFS KEYWORD1
9494
File KEYWORD1
9595
Dir KEYWORD1
96+
FSConfig KEYWORD1
9697

9798
# Seek enums
9899
SeekSet LITERAL1

tests/host/fs/test_fs.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,11 @@ TEST_CASE("#1819 Can list all files with openDir(\"\")", "[fs][bugreport]")
191191
TEST_CASE("SDFS", "[sdfs]")
192192
{
193193
SDFS_MOCK_DECLARE();
194-
setSDFSConfig(0);
195194
SDFS.end();
195+
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
196+
SDFS.begin(&cfg);
196197
REQUIRE(SDFS.format());
197-
REQUIRE(SDFS.begin());
198+
REQUIRE(SDFS.begin(&cfg));
198199
REQUIRE(SDFS.mkdir("/happy/face"));
199200
REQUIRE(SDFS.mkdir("/happy/nose"));
200201
REQUIRE(SDFS.rmdir("/happy/face"));
@@ -207,7 +208,9 @@ TEST_CASE("SDFS", "[sdfs]")
207208
TEST_CASE("Files.ino example", "[sd]")
208209
{
209210
SDFS_MOCK_DECLARE();
210-
setSDFSConfig(0);
211+
SDFS.end();
212+
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
213+
SDFS.begin(&cfg);
211214
SDFS.end();
212215
REQUIRE(SDFS.format());
213216
REQUIRE(SD.begin(4));
@@ -243,7 +246,9 @@ static String readFileSD(const char* name)
243246
TEST_CASE("Listfiles.ino example", "[sd]")
244247
{
245248
SDFS_MOCK_DECLARE();
246-
setSDFSConfig(0);
249+
SDFS.end();
250+
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
251+
SDFS.begin(&cfg);
247252
SDFS.end();
248253
REQUIRE(SDFS.format());
249254
REQUIRE(SD.begin(4));

0 commit comments

Comments
 (0)