Skip to content

Commit

Permalink
Replace ESP8266SdFat w/SdFat 2.3.0, add SDIO,ExFAT
Browse files Browse the repository at this point in the history
Remove ESP8266SdFat fork and replaces with upstream SdFat to simplify
things.  This 2.3.0 version adds SDIO support and enables exFAT support.
Also moved FAT LFNs to 256 chars, same as LittleFS.
  • Loading branch information
earlephilhower committed Jan 17, 2025
1 parent 4785c16 commit c099104
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 32 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
[submodule "libraries/LittleFS/lib/littlefs"]
path = libraries/LittleFS/lib/littlefs
url = https://github.com/littlefs-project/littlefs.git
[submodule "libraries/SdFat"]
path = libraries/ESP8266SdFat
url = https://github.com/earlephilhower/ESP8266SdFat.git
[submodule "libraries/Keyboard"]
path = libraries/HID_Keyboard
url = https://github.com/earlephilhower/Keyboard.git
Expand Down Expand Up @@ -49,3 +46,6 @@
[submodule "libraries/ESPHost"]
path = libraries/ESPHost
url = https://github.com/Networking-for-Arduino/ESPHost.git
[submodule "libraries/SdFat"]
path = libraries/SdFat
url = https://github.com/greiman/SdFat.git
1 change: 0 additions & 1 deletion libraries/ESP8266SdFat
Submodule ESP8266SdFat deleted from 9e1457
4 changes: 2 additions & 2 deletions libraries/SD/examples/CardInfo/CardInfo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void setup() {
Serial.println();

// print the type and size of the first FAT-type volume
uint32_t volumesize;
uint64_t volumesize;
Serial.print("Volume type is: FAT");
Serial.println(SD.fatType(), DEC);

Expand All @@ -130,7 +130,7 @@ void setup() {
Serial.println((float)volumesize / 1024.0);

Serial.print("Card size: ");
Serial.println((float)SD.size() / 1000);
Serial.println((float)SD.size64() / 1000);

FSInfo fs_info;
SDFS.info(fs_info);
Expand Down
8 changes: 4 additions & 4 deletions libraries/SDFS/src/SDFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ FileImplPtr SDFSImpl::open(const char* path, OpenMode openMode, AccessMode acces
}
free(pathStr);
}
File32 fd = _fs.open(path, flags);
FsFile fd = _fs.open(path, flags);
if (!fd) {
DEBUGV("SDFSImpl::openFile: fd=%p path=`%s` openMode=%d accessMode=%d",
&fd, path, openMode, accessMode);
return FileImplPtr();
}
auto sharedFd = std::make_shared<File32>(fd);
auto sharedFd = std::make_shared<FsFile>(fd);
return std::make_shared<SDFSFileImpl>(this, sharedFd, path);
}

Expand All @@ -88,7 +88,7 @@ DirImplPtr SDFSImpl::openDir(const char* path) {
}
// At this point we have a name of "/blah/blah/blah" or "blah" or ""
// If that references a directory, just open it and we're done.
File32 dirFile;
FsFile dirFile;
const char *filter = "";
if (!pathStr[0]) {
// openDir("") === openDir("/")
Expand Down Expand Up @@ -133,7 +133,7 @@ DirImplPtr SDFSImpl::openDir(const char* path) {
DEBUGV("SDFSImpl::openDir: path=`%s`\n", path);
return DirImplPtr();
}
auto sharedDir = std::make_shared<File32>(dirFile);
auto sharedDir = std::make_shared<FsFile>(dirFile);
auto ret = std::make_shared<SDFSDirImpl>(filter, this, sharedDir, pathStr);
free(pathStr);
return ret;
Expand Down
44 changes: 23 additions & 21 deletions libraries/SDFS/src/SDFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ class SDFSImpl : public FSImpl {
return false;
}
bzero(st, sizeof(*st));
File32 f;
f = _fs.open(path, O_RDONLY);
FsFile f = _fs.open(path, O_RDONLY);
if (!f) {
return false;
}
Expand All @@ -169,8 +168,8 @@ class SDFSImpl : public FSImpl {
if (f.getCreateDateTime(&date, &time)) {
st->ctime = FatToTimeT(date, time);
}
if (f.getAccessDate(&date)) {
st->atime = FatToTimeT(date, 0);
if (f.getAccessDateTime(&date, &time)) {
st->atime = FatToTimeT(date, time);
}
f.close();
return true;
Expand Down Expand Up @@ -272,7 +271,7 @@ class SDFSImpl : public FSImpl {

class SDFSFileImpl : public FileImpl {
public:
SDFSFileImpl(SDFSImpl *fs, std::shared_ptr<File32> fd, const char *name)
SDFSFileImpl(SDFSImpl *fs, std::shared_ptr<FsFile> fd, const char *name)
: _fs(fs), _fd(fd), _opened(true) {
_name = std::shared_ptr<char>(new char[strlen(name) + 1], std::default_delete<char[]>());
strcpy(_name.get(), name);
Expand All @@ -284,7 +283,7 @@ class SDFSFileImpl : public FileImpl {
}

int availableForWrite() override {
return _opened ? _fd->availableSpaceForWrite() : 0;
return _opened ? _fd->availableForWrite() : 0;
}

size_t write(const uint8_t *buf, size_t size) override {
Expand Down Expand Up @@ -373,9 +372,9 @@ class SDFSFileImpl : public FileImpl {
time_t getLastWrite() override {
time_t ftime = 0;
if (_opened && _fd) {
DirFat_t tmp;
if (_fd.get()->dirEntry(&tmp)) {
ftime = SDFSImpl::FatToTimeT(*(uint16_t*)tmp.modifyDate, *(uint16_t*)tmp.modifyTime);
uint16_t date, time;
if (_fd.get()->getModifyDateTime(&date, &time)) {
ftime = SDFSImpl::FatToTimeT(date, time);
}
}
return ftime;
Expand All @@ -384,24 +383,24 @@ class SDFSFileImpl : public FileImpl {
time_t getCreationTime() override {
time_t ftime = 0;
if (_opened && _fd) {
DirFat_t tmp;
if (_fd.get()->dirEntry(&tmp)) {
ftime = SDFSImpl::FatToTimeT(*(uint16_t*)tmp.createDate, *(uint16_t*)tmp.createTime);
uint16_t date, time;
if (_fd.get()->getCreateDateTime(&date, &time)) {
ftime = SDFSImpl::FatToTimeT(date, time);
}
}
return ftime;
}

protected:
SDFSImpl* _fs;
std::shared_ptr<File32> _fd;
std::shared_ptr<FsFile> _fd;
std::shared_ptr<char> _name;
bool _opened;
};

class SDFSDirImpl : public DirImpl {
public:
SDFSDirImpl(const String& pattern, SDFSImpl* fs, std::shared_ptr<File32> dir, const char *dirPath = nullptr)
SDFSDirImpl(const String& pattern, SDFSImpl* fs, std::shared_ptr<FsFile> dir, const char *dirPath = nullptr)
: _pattern(pattern), _fs(fs), _dir(dir), _valid(false), _dirPath(nullptr) {
if (dirPath) {
_dirPath = std::shared_ptr<char>(new char[strlen(dirPath) + 1], std::default_delete<char[]>());
Expand Down Expand Up @@ -466,19 +465,22 @@ class SDFSDirImpl : public DirImpl {
bool next() override {
const int n = _pattern.length();
do {
File32 file;
FsFile file;
file.openNext(_dir.get(), O_READ);
if (file) {
_valid = 1;
_size = file.fileSize();
_isFile = file.isFile();
_isDirectory = file.isDir();
DirFat_t tmp;
if (file.dirEntry(&tmp)) {
_time = SDFSImpl::FatToTimeT(*(uint16_t*)tmp.modifyDate, *(uint16_t*)tmp.modifyTime);
_creation = SDFSImpl::FatToTimeT(*(uint16_t*)tmp.createDate, *(uint16_t*)tmp.createTime);
uint16_t date, time;
if (file.getModifyDateTime(&date, &time)) {
_time = SDFSImpl::FatToTimeT(date, time);
} else {
_time = 0;
}
if (file.getCreateDateTime(&date, &time)) {
_creation = SDFSImpl::FatToTimeT(date, time);
} else {
_creation = 0;
}
file.getName(_lfn, sizeof(_lfn));
Expand All @@ -499,9 +501,9 @@ class SDFSDirImpl : public DirImpl {
protected:
String _pattern;
SDFSImpl* _fs;
std::shared_ptr<File32> _dir;
std::shared_ptr<FsFile> _dir;
bool _valid;
char _lfn[64];
char _lfn[256];
time_t _time;
time_t _creation;
std::shared_ptr<char> _dirPath;
Expand Down
1 change: 1 addition & 0 deletions libraries/SdFat
Submodule SdFat added at 67e264
3 changes: 2 additions & 1 deletion platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ compiler.warning_flags.all=-Wall -Wextra -Werror=return-type -Wno-ignored-qualif

compiler.netdefines={build.libpicowdefs} -DLWIP_IGMP=1 -DLWIP_CHECKSUM_CTRL_PER_NETIF=1
compiler.psramdefines={build.psram_cs} {build.psram_freq}
compiler.defines={build.led} {build.usbstack_flags} {build.usbpid} {build.usbvid} {build.usbpwr} {compiler.psramdefines} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' {compiler.netdefines} {build.variantdefines} -DARDUINO_VARIANT="{build.variant}" -DPICO_FLASH_SIZE_BYTES={build.flash_total} "@{runtime.platform.path}/lib/{build.chip}/platform_def.txt"
compiler.defines={build.led} {build.usbstack_flags} {build.usbpid} {build.usbvid} {build.usbpwr} {compiler.psramdefines} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}' {compiler.netdefines} {build.sdfatdefines} {build.variantdefines} -DARDUINO_VARIANT="{build.variant}" -DPICO_FLASH_SIZE_BYTES={build.flash_total} "@{runtime.platform.path}/lib/{build.chip}/platform_def.txt"
compiler.includes="-iprefix{runtime.platform.path}/" "@{runtime.platform.path}/lib/{build.chip}/platform_inc.txt" "@{runtime.platform.path}/lib/core_inc.txt" "-I{runtime.platform.path}/include"
compiler.flags={build.toolchainopts} -ffunction-sections -fdata-sections {build.flags.exceptions} {build.flags.stackprotect} {build.picodebugflags}
compiler.wrap="@{runtime.platform.path}/lib/{build.chip}/platform_wrap.txt" "@{runtime.platform.path}/lib/core_wrap.txt"
Expand Down Expand Up @@ -114,6 +114,7 @@ build.espwifitype=
build.debugscript=picoprobe_cmsis_dap.tcl
build.picodebugflags=
build.variantdefines=
build.sdfatdefines=-DFILE_COPY_CONSTRUCTOR_SELECT=FILE_COPY_CONSTRUCTOR_PUBLIC -DUSE_UTF8_LONG_NAMES=1 -DSDFAT_FILE_TYPE=3 -DDISABLE_FS_H_WARNING=1

# Allow Pico boards to be auto-discovered by the IDE
#discovery.rp2040.pattern={runtime.tools.pqt-python3.path}/python3 -I "{runtime.platform.path}/tools/pluggable_discovery.py"
Expand Down
8 changes: 8 additions & 0 deletions tools/platformio-build.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ def is_pio_build():
"-std=gnu++17",
],

CPPDEFINES=[
# SdFat definitions required for SDFS
("FILE_COPY_CONSTRUCTOR_SELECT", "FILE_COPY_CONSTRUCTOR_PUBLIC"),
("USE_UTF8_LONG_NAMES", "1"),
("SDFAT_FILE_TYPE", "3"),
("DISABLE_FS_H_WARNING", "1")
]

CPPPATH=[
os.path.join(FRAMEWORK_DIR, "cores", "rp2040"),
os.path.join(FRAMEWORK_DIR, "cores", "rp2040", "api", "deprecated"),
Expand Down

0 comments on commit c099104

Please sign in to comment.