8000 SD Filesystem compatible with 8266 File, using latest SdFat by earlephilhower · Pull Request #5525 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

SD Filesystem compatible with 8266 File, using latest SdFat #5525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4556403
Add a FAT filesystem for SD cards to Arduino FS
earlephilhower Dec 19, 2018
1b95e5d
Add a FSConfig and SDFSConfig param to FS.begin()
Jan 2, 2019
241d0ff
Add FS::setConfig to set FS-specific options
Jan 3, 2019
2b49e7a
Merge branch 'master' into SDFS
earlephilhower Jan 9, 2019
7fcbfdc
Fix platformio.sh test skipping merge error
Jan 9, 2019
a4dadb2
Merge branch 'master' into SDFS
earlephilhower Jan 16, 2019
edd9476
Fix merge error with SPIFFS_MOCK
earlephilhower Jan 17, 2019
9a82f00
Merge branch 'master' into SDFS
earlephilhower Jan 18, 2019
dc977b0
Merge branch 'master' into SDFS
earlephilhower Jan 28, 2019
ef2374c
Update to fix SPI transferBytes issue
earlephilhower Jan 28, 2019
c8acb84
Add ::truncate to File interface
earlephilhower Jan 28, 2019
d215b66
Merge branch 'master' into SDFS
earlephilhower Feb 1, 2019
ce148dd
Merge branch 'master' of https://github.com/esp8266/Arduino into SDFS
earlephilhower Feb 8, 2019
0a0bbc4
Merge branch 'master' into SDFS
earlephilhower Feb 8, 2019
652415d
Merge branch 'master' into SDFS
d-a-v Feb 18, 2019
85946ee
Merge branch 'master' into SDFS
d-a-v Feb 21, 2019
6b0a3f4
Update comments and MAX_PATH to 260
earlephilhower Feb 25, 2019
69bebd7
Merge branch 'master' into SDFS
earlephilhower Feb 25, 2019
1bc7377
Use polledTimeout for formatting yields, cleanup
earlephilhower Feb 27, 2019
cc3cc70
Add assert on illegal file open mode
earlephilhower Feb 27, 2019
e0c26bd
Merge branch 'master' into SDFS
earlephilhower Feb 27, 2019
35ec9ec
Make setConfig take const& ref, cleaner code
earlephilhower Feb 27, 2019
e53f26c
Merge branch 'master' into SDFS
devyte Mar 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
Earle F. Philhower, III authored and Earle F. Philhower, III committed Jan 3, 2019
commit 241d0ffdff492e5d309c7df17062cf1754d3126f
12 changes: 10 additions & 2 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,19 @@ bool Dir::rewind() {
return _impl->rewind();
}

bool FS::begin(const FSConfig *cfg) {
bool FS::setConfig(const FSConfig *cfg) {
if (!_impl || !cfg) {
return false;
}

return _impl->setConfig(cfg);
}

bool FS::begin() {
if (!_impl) {
return false;
}
return _impl->begin(cfg);
return _impl->begin();
}

void FS::end() {
Expand Down
26 changes: 23 additions & 3 deletions cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,38 @@ class FSConfig
{
public:
FSConfig(bool autoFormat = true) {
_autoFormat = autoFormat;
_type = FSConfig::fsid::FSId;
_autoFormat = autoFormat;
}

bool _autoFormat;
enum fsid { FSId = 0x00000000 };
FSConfig setAutoFormat(bool val = true) {
_autoFormat = val;
return *this;
}

uint32_t _type;
bool _autoFormat;
};

class SPIFFSConfig : public FSConfig
{
public:
SPIFFSConfig(bool autoFormat = true) {
_type = SPIFFSConfig::fsid::FSId;
_autoFormat = autoFormat;
}
enum fsid { FSId = 0x53504946 };
};

class FS
{
public:
FS(FSImplPtr impl) : _impl(impl) { }

bool begin(const FSConfig *cfg = nullptr);
bool setConfig(const FSConfig *cfg);

bool begin();
void end();

bool format();
Expand Down
3 changes: 2 additions & 1 deletion cores/esp8266/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class DirImpl {
class FSImpl {
public:
virtual ~FSImpl () { }
virtual bool begin(const FSConfig *cfg) = 0;
virtual bool setConfig(const FSConfig *cfg) = 0;
virtual bool begin() = 0;
virtual void end() = 0;
virtual bool format() = 0;
virtual bool info(FSInfo& info) = 0;
Expand Down
16 changes: 13 additions & 3 deletions cores/esp8266/spiffs_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,16 @@ class SPIFFSImpl : public FSImpl
return false;
}

bool begin(const FSConfig *cfg) override
bool setConfig(const FSConfig *cfg) override
{
if ((cfg->_type != SPIFFSConfig::fsid::FSId) || (SPIFFS_mounted(&_fs) != 0)) {
return false;
}
_cfg = *static_cast<const SPIFFSConfig *>(cfg);
return true;
}

bool begin() override
{
#if defined(ARDUINO) && !defined(CORE_MOCK)
if (&_SPIFFS_end <= &_SPIFFS_start)
Expand All @@ -152,7 +161,7 @@ class SPIFFSImpl : public FSImpl
if (_tryMount()) {
return true;
}
if (!cfg || cfg->_autoFormat) {
if (_cfg._autoFormat) {
auto rc = SPIFFS_format(&_fs);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
Expand Down Expand Up @@ -296,6 +305,8 @@ class SPIFFSImpl : public FSImpl
std::unique_ptr<uint8_t[]> _workBuf;
std::unique_ptr<uint8_t[]> _fdsBuf;
std::unique_ptr<uint8_t[]> _cacheBuf;

SPIFFSConfig _cfg;
};

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


#endif//spiffs_api_h
27 changes: 18 additions & 9 deletions doc/filesystem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ directory into ESP8266 flash file system.
File system object (SPIFFS)
---------------------------

setConfig
~~~~~~~~~

.. code:: cpp

SPIFFSConfig cfg;
cfg.setAutoFormat(false);
SPIFFS.setConfig(&cfg);

This method allows you to configure the parameters of a filesystem
before mounting. All filesystems have their own ``*Config`` (i.e.
``SDFSConfig`` or ``SPIFFSConfig`` with their custom set of options.
All filesystems allow explicitly enabling/disabling formatting when
mounts fail. If you do not call this ``setConfig`` method before
perforing ``begin()``, you will get the filesystem's default
behavior and configuration. By default, SPIFFS will autoformat the
filesystem if it cannot mount it, while SDFS will not.

begin
~~~~~

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

.. code:: cpp

auto cfg = FSConfig(false);
SPIFFS.begin(&cfg);

If a ``FSConfig`` object is passed in, with the autoFormat parameter
set to ``false``, then if the filesystem is unable to be mounted it
will return an error code instead of trying to format it.

end
~~~

Expand Down
3 changes: 2 additions & 1 deletion libraries/SD/src/SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class SDClass {
public:
boolean begin(uint8_t csPin, SPISettings cfg = SPI_HALF_SPEED) {
auto fsCfg = SDFSConfig(csPin, cfg);
return (boolean)SDFS.begin(&fsCfg);
SDFS.setConfig(&fsCfg);
return (boolean)SDFS.begin();
}

void end(bool endSPI = true) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/SDFS/src/SDFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ bool SDFSImpl::format() {
return false;
}
SDFSFormatter formatter;
bool ret = formatter.format(&_fs, _csPin, _spiSettings);
bool ret = formatter.format(&_fs, _cfg._csPin, _cfg._spiSettings);
return ret;
}

Expand Down
75 changes: 50 additions & 25 deletions libraries/SDFS/src/SDFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,50 @@ class SDFSDirImpl;
class SDFSConfig : public FSConfig
{
public:
SDFSConfig(uint8_t cs, SPISettings spi) {
SDFSConfig() {
_type = SDFSConfig::fsid::FSId;
_autoFormat = false;
_cs = cs;
_spi = spi;
_csPin = 4;
_spiSettings = SD_SCK_MHZ(10);
_part = 0;
}
SDFSConfig(uint8_t csPin, SPISettings spi) {
_type = SDFSConfig::fsid::FSId;
_autoFormat = false;
_csPin = csPin;
_spiSettings = spi;
_part = 0;
}

enum fsid { FSId = 0x53444653 };

SDFSConfig setAutoFormat(bool val = true) {
_autoFormat = val;
return *this;
}
SDFSConfig setCSPin(uint8_t pin) {
_csPin = pin;
return *this;
}
SDFSConfig setSPI(SPISettings spi) {
_spiSettings = spi;
return *this;
}
SDFSConfig setPart(uint8_t part) {
_part = part;
return *this;
}

uint8_t _cs;
SPISettings _spi;
// Inherit _type and _autoFormat
uint8_t _csPin;
uint8_t _part;
SPISettings _spiSettings;
};

class SDFSImpl : public FSImpl
{
public:
SDFSImpl() : _part(0), _csPin(-1), _spiSettings(SPI_FULL_SPEED), _mounted(false)
SDFSImpl() : _mounted(false)
{
}

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

bool begin(const FSConfig *cfg) override {
bool setConfig(const FSConfig *cfg) override
{
if ((cfg->_type != SDFSConfig::fsid::FSId) || _mounted) {
return false;
}
_cfg = *static_cast<const SDFSConfig *>(cfg);
return true;
}

bool begin() override {
if (_mounted) {
end();
}
if (!cfg) {
return false; // You need to tell me the CS and SPI setup or I can't do anything!
}
const SDFSConfig *myCfg = static_cast<const SDFSConfig *>(cfg);
_csPin = myCfg->_cs;
_spiSettings = myCfg->_spi;
_mounted = _fs.begin(_csPin, _spiSettings);
if (!_mounted && myCfg->_autoFormat) {
_mounted = _fs.begin(_cfg._csPin, _cfg._spiSettings);
if (!_mounted && _cfg._autoFormat) {
format();
_mounted = _fs.begin(_csPin, _spiSettings);
_mounted = _fs.begin(_cfg._csPin, _cfg._spiSettings);
}
return _mounted;
}
Expand All @@ -123,12 +156,6 @@ class SDFSImpl : public FSImpl

bool format() override;

// SDFS-only configuration calls
void setSDFSIOConfig(int8_t csPin, SPISettings spiConfig) {
_csPin = csPin;
_spiSettings = spiConfig;
}

protected:
friend class SDFileImpl;
friend class SDFSDirImpl;
Expand Down Expand Up @@ -159,9 +186,7 @@ class SDFSImpl : public FSImpl
}

sdfat::SdFat _fs;
uint8_t _part;
int8_t _csPin;
SPISettings _spiSettings;
SDFSConfig _cfg;
bool _mounted;
};

Expand Down
2 changes: 1 addition & 1 deletion libraries/esp8266/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ SPIFFS KEYWORD1
SDFS KEYWORD1
File KEYWORD1
Dir KEYWORD1
FSConfig KEYWORD1
SPIFFSConfig KEYWORD1

# Seek enums
SeekSet LITERAL1
Expand Down
44 changes: 36 additions & 8 deletions tests/host/fs/test_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,41 @@ static std::set<String> listDir (const char* path)
return result;
}

TEST_CASE("SPIFFS checks the config object passed in", "[fs]")
{
SPIFFS_MOCK_DECLARE(64, 8, 512, false);
FSConfig f;
SPIFFSConfig s;
SDFSConfig d;

REQUIRE_FALSE(SPIFFS.setConfig(&f));
REQUIRE(SPIFFS.setConfig(&s));
REQUIRE_FALSE(SPIFFS.setConfig(&d));
}

TEST_CASE("SDFS checks the config object passed in", "[fs]")
{
SDFS_MOCK_DECLARE();
FSConfig f;
SPIFFSConfig s;
SDFSConfig d;

REQUIRE_FALSE(SDFS.setConfig(&f));
REQUIRE_FALSE(SDFS.setConfig(&s));
REQUIRE(SDFS.setConfig(&d));
}

TEST_CASE("FS can begin","[fs]")
{
SPIFFS_MOCK_DECLARE(64, 8, 512, false);
SPIFFSConfig cfg;
cfg.setAutoFormat(false);
SPIFFS.setConfig(&cfg);
REQUIRE_FALSE(SPIFFS.begin());
cfg.setAutoFormat(true);
SPIFFS.setConfig(&cfg);
REQUIRE(SPIFFS.begin());
REQUIRE_FALSE(SPIFFS.setConfig(&cfg)); // Can't change config of mounted FS
}

TEST_CASE("FS can't begin with zero size","[fs]")
Expand Down Expand Up @@ -191,11 +222,11 @@ TEST_CASE("#1819 Can list all files with openDir(\"\")", "[fs][bugreport]")
TEST_CASE("SDFS", "[sdfs]")
{
SDFS_MOCK_DECLARE();
SDFS.end();
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
SDFS.begin(&cfg);
SDFS.setConfig(&cfg);
REQUIRE(SDFS.format());
REQUIRE(SDFS.begin(&cfg));
REQUIRE(SDFS.begin());
REQUIRE_FALSE(SDFS.setConfig(&cfg)); // Can't change config of mounted fs
REQUIRE(SDFS.mkdir("/happy/face"));
REQUIRE(SDFS.mkdir("/happy/nose"));
REQUIRE(SDFS.rmdir("/happy/face"));
Expand All @@ -210,8 +241,7 @@ TEST_CASE("Files.ino example", "[sd]")
SDFS_MOCK_DECLARE();
SDFS.end();
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
SDFS.begin(&cfg);
SDFS.end();
SDFS.setConfig(&cfg);
REQUIRE(SDFS.format());
REQUIRE(SD.begin(4));
REQUIRE_FALSE(SD.exists("example.txt"));
Expand Down Expand Up @@ -246,10 +276,8 @@ static String readFileSD(const char* name)
TEST_CASE("Listfiles.ino example", "[sd]")
{
SDFS_MOCK_DECLARE();
SDFS.end();
auto cfg = SDFSConfig(0, SD_SCK_MHZ(1));
SDFS.begin(&cfg);
SDFS.end();
SDFS.setConfig(&cfg);
REQUIRE(SDFS.format());
REQUIRE(SD.begin(4));

Expand Down
0