mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-01 03:26:58 +03:00
SD Filesystem compatible with 8266 File, using latest SdFat (#5525)
* Add a FAT filesystem for SD cards to Arduino FS Arduino forked a copy of SD lib several years ago, put their own wrapper around it, and it's been languishing in our ESP8266 libraries ever since as SD. It doesn't support long file names, has class names which conflict with the ESP8266 internal names, and hasn't been updated in ages. The original author of the SD library has continued work in the meantime, and produced a very feature rich implementation of SdFat. It unfortunately also conflicts with the class names we use in ESP8266 Arduino and has a different API than the internal SPIFFS or proposed LittleFS filesystem objects. This PR puts a wrapper around the latest and greatest SdFat library, by forking it and wrapping its classes in a private namespace "sdfat," and making as thin a wrapper as possible around it to conform to the ESP8266 FS, File, and Dir classes. This PR also removes the Arduino SD.h class library and rewrites it using the new SDFS filesystem to make everything in the ESP8266 Arduino core compatible with each other. By doing so it lets us use a single interface for anything needing a file instead of multiple ones (see SDWebServer and how a different object is needed vs. one serving from SPIFFS even though the logic is all the same). Same for BearSSL's CertStores and probably a few others I've missed, cleaning up our code base significantly. Like LittleFS, silently create directories when a file is created with a subdirectory specifier ("/path/to/file.txt") if they do not yet exist. Adds a blacklist of sketches to skip in the CI process (because SdFat has many examples which do not build properly on the ESP8266). Now that LittleFS and SDFS have directory support, the FS needs to be able to communicate whether a name is one or the other. Add a simple bool FS::isDirectory() and bool FS::isFile() method. SPIFFS doesn't have directories, so if it's valid it's a file and reported as such. Add ::mkdir/::rmdir to the FS class to allow users to make and destroy subdirectories. SPIFFS directory operations will, of course, fail and return false. Emulate a 16MB SD card and allow test runner to exercise it by using a custom SdFat HOST_MOCK-enabled object. Throw out the original Arduino SD.h class and rewrite from scratch using only the ESP8266 native SDFS calls. This makes "SD" based applications compatible with normal ESP8266 "File" and "FS" and "SPIFFS" operations. The only major visible change for users is that long filenames now are fully supported and work without any code changes. If there are static arrays of 11 bytes for old 8.3 names in code, they will need to be adjusted. While it is recommended to use the more powerful SDFS class to access SD cards, this SD.h wrapper allows for use of existing Arduino libraries which are built to only with with that SD class. Additional helper functions added to ESP8266 native Filesystem:: classes to help support this portability. The rewrite is good enough to run the original SDWebServer and SD example code without any changes. * 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. * 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. * Add ::truncate to File interface Fixes #3846 * Use polledTimeout for formatting yields, cleanup Use the new polledTimeout class to ensure a yield every 5ms while formatting. Add in default case handling and some debug messages when invalid inputs specified. * Make setConfig take const& ref, cleaner code setConfig now can take a parameter defined directly in the call by using a const &ref to it, leading to one less line of code to write and cleaner reading of the code. Also clean up SDFS implementation pointer definition.
This commit is contained in:
committed by
GitHub
parent
61a8a6b14e
commit
b1da9eda46
@ -1,154 +1,133 @@
|
||||
/*
|
||||
SD.h - A thin shim for Arduino ESP8266 Filesystems
|
||||
Copyright (c) 2019 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
SD - a slightly more friendly wrapper for sdfatlib
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library aims to expose a subset of SD card functionality
|
||||
in the form of a higher level "wrapper" object.
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
License: GNU General Public License V3
|
||||
(Because sdfatlib is licensed with this.)
|
||||
|
||||
(C) Copyright 2010 SparkFun Electronics
|
||||
|
||||
*/
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __SD_H__
|
||||
#define __SD_H__
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <FS.h>
|
||||
#include <SDFS.h>
|
||||
|
||||
#include <utility/SdFat.h>
|
||||
#include <utility/SdFatUtil.h>
|
||||
|
||||
#define FILE_READ O_READ
|
||||
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT)
|
||||
|
||||
class File : public Stream {
|
||||
private:
|
||||
char _name[13]; // our name
|
||||
SdFile *_file; // underlying file pointer
|
||||
|
||||
public:
|
||||
File(SdFile f, const char *name); // wraps an underlying SdFile
|
||||
File(void); // 'empty' constructor
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
virtual int read();
|
||||
virtual size_t readBytes(char *buffer, size_t length);
|
||||
virtual int peek();
|
||||
virtual int available();
|
||||
virtual void flush();
|
||||
int read(void *buf, uint16_t nbyte);
|
||||
boolean seek(uint32_t pos);
|
||||
uint32_t position();
|
||||
uint32_t size();
|
||||
void close();
|
||||
operator bool();
|
||||
char * name();
|
||||
|
||||
boolean isDirectory(void);
|
||||
File openNextFile(uint8_t mode = O_RDONLY);
|
||||
void rewindDirectory(void);
|
||||
|
||||
template<typename T> size_t write(T &src){
|
||||
uint8_t obuf[512];
|
||||
size_t doneLen = 0;
|
||||
size_t sentLen;
|
||||
int i;
|
||||
|
||||
while (src.available() > 512){
|
||||
src.read(obuf, 512);
|
||||
sentLen = write(obuf, 512);
|
||||
doneLen = doneLen + sentLen;
|
||||
if(sentLen != 512){
|
||||
return doneLen;
|
||||
}
|
||||
}
|
||||
|
||||
size_t leftLen = src.available();
|
||||
src.read(obuf, leftLen);
|
||||
sentLen = write(obuf, leftLen);
|
||||
doneLen = doneLen + sentLen;
|
||||
return doneLen;
|
||||
}
|
||||
|
||||
using Print::write;
|
||||
};
|
||||
#undef FILE_READ
|
||||
#define FILE_READ sdfat::O_READ
|
||||
#undef FILE_WRITE
|
||||
#define FILE_WRITE (sdfat::O_READ | sdfat::O_WRITE | sdfat::O_CREAT)
|
||||
|
||||
class SDClass {
|
||||
|
||||
private:
|
||||
// These are required for initialisation and use of sdfatlib
|
||||
Sd2Card card;
|
||||
SdVolume volume;
|
||||
SdFile root;
|
||||
|
||||
// my quick&dirty iterator, should be replaced
|
||||
SdFile getParentDir(const char *filepath, int *indx);
|
||||
public:
|
||||
// This needs to be called to set up the connection to the SD card
|
||||
// before other methods are used.
|
||||
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, uint32_t speed = SPI_HALF_SPEED);
|
||||
boolean begin(uint8_t csPin, SPISettings cfg = SPI_HALF_SPEED) {
|
||||
SDFS.setConfig(SDFSConfig(csPin, cfg));
|
||||
return (boolean)SDFS.begin();
|
||||
}
|
||||
|
||||
/*
|
||||
In the following sequence:
|
||||
//Insert SD Card A
|
||||
SD.begin()
|
||||
//do operations
|
||||
//remove card A
|
||||
//insert SD card B
|
||||
SD.end()
|
||||
void end(bool endSPI = true) {
|
||||
SDFS.end();
|
||||
if (endSPI) {
|
||||
SPI.end();
|
||||
}
|
||||
}
|
||||
|
||||
It is possible that card A becomes corrupted due to removal before calling SD.end().
|
||||
It is possible that card B becomes corrupted if there were ops pending for card A at the time SD.end() is called.
|
||||
File open(const char *filename, uint8_t mode = FILE_READ) {
|
||||
return SDFS.open(filename, getMode(mode));
|
||||
}
|
||||
|
||||
Call SD.end() or SD.end(true) to shut everything down.
|
||||
Call SD.end(false) to shut everything but the SPI object down.
|
||||
*/
|
||||
void end(bool endSPI = true);
|
||||
File open(const String &filename, uint8_t mode = FILE_READ) {
|
||||
return open(filename.c_str(), mode);
|
||||
}
|
||||
|
||||
boolean exists(const char *filepath) {
|
||||
return (boolean)SDFS.exists(filepath);
|
||||
}
|
||||
|
||||
// Open the specified file/directory with the supplied mode (e.g. read or
|
||||
// write, etc). Returns a File object for interacting with the file.
|
||||
// Note that currently only one file can be open at a time.
|
||||
File open(const char *filename, uint8_t mode = FILE_READ);
|
||||
File open(const String &filename, uint8_t mode = FILE_READ) { return open( filename.c_str(), mode ); }
|
||||
boolean exists(const String &filepath) {
|
||||
return (boolean)SDFS.exists(filepath.c_str());
|
||||
}
|
||||
|
||||
// Methods to determine if the requested file path exists.
|
||||
boolean exists(const char *filepath);
|
||||
boolean exists(const String &filepath) { return exists(filepath.c_str()); }
|
||||
boolean mkdir(const char *filepath) {
|
||||
return (boolean)SDFS.mkdir(filepath);
|
||||
}
|
||||
|
||||
// Create the requested directory heirarchy--if intermediate directories
|
||||
// do not exist they will be created.
|
||||
boolean mkdir(const char *filepath);
|
||||
boolean mkdir(const String &filepath) { return mkdir(filepath.c_str()); }
|
||||
boolean mkdir(const String &filepath) {
|
||||
return (boolean)SDFS.mkdir(filepath.c_str());
|
||||
}
|
||||
|
||||
// Delete the file.
|
||||
boolean remove(const char *filepath);
|
||||
boolean remove(const String &filepath) { return remove(filepath.c_str()); }
|
||||
|
||||
boolean rmdir(const char *filepath);
|
||||
boolean rmdir(const String &filepath) { return rmdir(filepath.c_str()); }
|
||||
boolean remove(const char *filepath) {
|
||||
return (boolean)SDFS.remove(filepath);
|
||||
}
|
||||
|
||||
boolean remove(const String &filepath) {
|
||||
return remove(filepath.c_str());
|
||||
}
|
||||
|
||||
boolean rmdir(const char *filepath) {
|
||||
return (boolean)SDFS.rmdir(filepath);
|
||||
}
|
||||
|
||||
boolean rmdir(const String &filepath) {
|
||||
return rmdir(filepath.c_str());
|
||||
}
|
||||
|
||||
uint8_t type() {
|
||||
return 0;//card.type();
|
||||
}
|
||||
|
||||
uint8_t fatType() {
|
||||
return 0;//volume.fatType();
|
||||
}
|
||||
|
||||
size_t blocksPerCluster() {
|
||||
return 0;//volume.blocksPerCluster();
|
||||
}
|
||||
|
||||
size_t totalClusters() {
|
||||
return 0;//volume.clusterCount();
|
||||
}
|
||||
|
||||
size_t blockSize() {
|
||||
return 512;
|
||||
}
|
||||
|
||||
size_t totalBlocks() {
|
||||
return 0;//(totalClusters() / blocksPerCluster());
|
||||
}
|
||||
|
||||
size_t clusterSize() {
|
||||
return 0;//blocksPerCluster() * blockSize();
|
||||
}
|
||||
|
||||
size_t size() {
|
||||
return 0;//(clusterSize() * totalClusters());
|
||||
}
|
||||
|
||||
uint8_t type(){ return card.type(); }
|
||||
uint8_t fatType(){ return volume.fatType(); }
|
||||
size_t blocksPerCluster(){ return volume.blocksPerCluster(); }
|
||||
size_t totalClusters(){ return volume.clusterCount(); }
|
||||
size_t blockSize(){ return (size_t)0x200; }
|
||||
size_t totalBlocks(){ return (totalClusters() / blocksPerCluster()); }
|
||||
size_t clusterSize(){ return blocksPerCluster() * blockSize(); }
|
||||
size_t size(){ return (clusterSize() * totalClusters()); }
|
||||
private:
|
||||
const char *getMode(uint8_t mode) {
|
||||
bool read = (mode & sdfat::O_READ) ? true : false;
|
||||
bool write = (mode & sdfat::O_WRITE) ? true : false;
|
||||
bool append = (mode & sdfat::O_APPEND) ? true : false;
|
||||
if ( read & !write ) { return "r"; }
|
||||
else if ( !read & write & !append ) { return "w+"; }
|
||||
else if ( !read & write & append ) { return "a"; }
|
||||
else if ( read & write & !append ) { return "w+"; } // may be a bug in FS::mode interpretation, "r+" seems proper
|
||||
else if ( read & write & append ) { return "a+"; }
|
||||
else { return "r"; }
|
||||
}
|
||||
|
||||
// This is used to determine the mode used to open a file
|
||||
// it's here because it's the easiest place to pass the
|
||||
// information through the directory walking function. But
|
||||
// it's probably not the best place for it.
|
||||
// It shouldn't be set directly--it is set via the parameters to `open`.
|
||||
int fileOpenMode;
|
||||
|
||||
friend class File;
|
||||
friend boolean callback_openPath(SdFile&, char *, boolean, void *);
|
||||
};
|
||||
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SD)
|
||||
|
Reference in New Issue
Block a user