updated to sdfatlib2010902
parent
46f80e82d9
commit
64f2121ab1
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,489 @@
|
||||
/* Arduino SdFat Library
|
||||
* Copyright (C) 2009 by William Greiman
|
||||
*
|
||||
* This file is part of the Arduino SdFat Library
|
||||
*
|
||||
* This Library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Arduino SdFat Library. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef SdBaseFile_h
|
||||
#define SdBaseFile_h
|
||||
/**
|
||||
* \file
|
||||
* \brief SdBaseFile class
|
||||
*/
|
||||
#include <avr/pgmspace.h>
|
||||
#if ARDUINO < 100
|
||||
#include <WProgram.h>
|
||||
#else // ARDUINO
|
||||
#include <Arduino.h>
|
||||
#endif // ARDUINO
|
||||
#include "SdFatConfig.h"
|
||||
#include "SdVolume.h"
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* \struct fpos_t
|
||||
* \brief internal type for istream
|
||||
* do not use in user apps
|
||||
*/
|
||||
struct fpos_t {
|
||||
/** stream position */
|
||||
uint32_t position;
|
||||
/** cluster for position */
|
||||
uint32_t cluster;
|
||||
fpos_t() : position(0), cluster(0) {}
|
||||
};
|
||||
|
||||
// use the gnu style oflag in open()
|
||||
/** open() oflag for reading */
|
||||
uint8_t const O_READ = 0X01;
|
||||
/** open() oflag - same as O_IN */
|
||||
uint8_t const O_RDONLY = O_READ;
|
||||
/** open() oflag for write */
|
||||
uint8_t const O_WRITE = 0X02;
|
||||
/** open() oflag - same as O_WRITE */
|
||||
uint8_t const O_WRONLY = O_WRITE;
|
||||
/** open() oflag for reading and writing */
|
||||
uint8_t const O_RDWR = (O_READ | O_WRITE);
|
||||
/** open() oflag mask for access modes */
|
||||
uint8_t const O_ACCMODE = (O_READ | O_WRITE);
|
||||
/** The file offset shall be set to the end of the file prior to each write. */
|
||||
uint8_t const O_APPEND = 0X04;
|
||||
/** synchronous writes - call sync() after each write */
|
||||
uint8_t const O_SYNC = 0X08;
|
||||
/** truncate the file to zero length */
|
||||
uint8_t const O_TRUNC = 0X10;
|
||||
/** set the initial position at the end of the file */
|
||||
uint8_t const O_AT_END = 0X20;
|
||||
/** create the file if nonexistent */
|
||||
uint8_t const O_CREAT = 0X40;
|
||||
/** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
|
||||
uint8_t const O_EXCL = 0X80;
|
||||
|
||||
// SdBaseFile class static and const definitions
|
||||
// flags for ls()
|
||||
/** ls() flag to print modify date */
|
||||
uint8_t const LS_DATE = 1;
|
||||
/** ls() flag to print file size */
|
||||
uint8_t const LS_SIZE = 2;
|
||||
/** ls() flag for recursive list of subdirectories */
|
||||
uint8_t const LS_R = 4;
|
||||
|
||||
|
||||
// flags for timestamp
|
||||
/** set the file's last access date */
|
||||
uint8_t const T_ACCESS = 1;
|
||||
/** set the file's creation date and time */
|
||||
uint8_t const T_CREATE = 2;
|
||||
/** Set the file's write date and time */
|
||||
uint8_t const T_WRITE = 4;
|
||||
// values for type_
|
||||
/** This file has not been opened. */
|
||||
uint8_t const FAT_FILE_TYPE_CLOSED = 0;
|
||||
/** A normal file */
|
||||
uint8_t const FAT_FILE_TYPE_NORMAL = 1;
|
||||
/** A FAT12 or FAT16 root directory */
|
||||
uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
|
||||
/** A FAT32 root directory */
|
||||
uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
|
||||
/** A subdirectory file*/
|
||||
uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
|
||||
/** Test value for directory type */
|
||||
uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;
|
||||
|
||||
/** date field for FAT directory entry
|
||||
* \param[in] year [1980,2107]
|
||||
* \param[in] month [1,12]
|
||||
* \param[in] day [1,31]
|
||||
*
|
||||
* \return Packed date for dir_t entry.
|
||||
*/
|
||||
static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
|
||||
return (year - 1980) << 9 | month << 5 | day;
|
||||
}
|
||||
/** year part of FAT directory date field
|
||||
* \param[in] fatDate Date in packed dir format.
|
||||
*
|
||||
* \return Extracted year [1980,2107]
|
||||
*/
|
||||
static inline uint16_t FAT_YEAR(uint16_t fatDate) {
|
||||
return 1980 + (fatDate >> 9);
|
||||
}
|
||||
/** month part of FAT directory date field
|
||||
* \param[in] fatDate Date in packed dir format.
|
||||
*
|
||||
* \return Extracted month [1,12]
|
||||
*/
|
||||
static inline uint8_t FAT_MONTH(uint16_t fatDate) {
|
||||
return (fatDate >> 5) & 0XF;
|
||||
}
|
||||
/** day part of FAT directory date field
|
||||
* \param[in] fatDate Date in packed dir format.
|
||||
*
|
||||
* \return Extracted day [1,31]
|
||||
*/
|
||||
static inline uint8_t FAT_DAY(uint16_t fatDate) {
|
||||
return fatDate & 0X1F;
|
||||
}
|
||||
/** time field for FAT directory entry
|
||||
* \param[in] hour [0,23]
|
||||
* \param[in] minute [0,59]
|
||||
* \param[in] second [0,59]
|
||||
*
|
||||
* \return Packed time for dir_t entry.
|
||||
*/
|
||||
static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
|
||||
return hour << 11 | minute << 5 | second >> 1;
|
||||
}
|
||||
/** hour part of FAT directory time field
|
||||
* \param[in] fatTime Time in packed dir format.
|
||||
*
|
||||
* \return Extracted hour [0,23]
|
||||
*/
|
||||
static inline uint8_t FAT_HOUR(uint16_t fatTime) {
|
||||
return fatTime >> 11;
|
||||
}
|
||||
/** minute part of FAT directory time field
|
||||
* \param[in] fatTime Time in packed dir format.
|
||||
*
|
||||
* \return Extracted minute [0,59]
|
||||
*/
|
||||
static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
|
||||
return(fatTime >> 5) & 0X3F;
|
||||
}
|
||||
/** second part of FAT directory time field
|
||||
* Note second/2 is stored in packed time.
|
||||
*
|
||||
* \param[in] fatTime Time in packed dir format.
|
||||
*
|
||||
* \return Extracted second [0,58]
|
||||
*/
|
||||
static inline uint8_t FAT_SECOND(uint16_t fatTime) {
|
||||
return 2*(fatTime & 0X1F);
|
||||
}
|
||||
/** Default date for file timestamps is 1 Jan 2000 */
|
||||
uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
|
||||
/** Default time for file timestamp is 1 am */
|
||||
uint16_t const FAT_DEFAULT_TIME = (1 << 11);
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* \class SdBaseFile
|
||||
* \brief Base class for SdFile with Print and C++ streams.
|
||||
*/
|
||||
class SdBaseFile {
|
||||
public:
|
||||
/** Create an instance. */
|
||||
SdBaseFile() : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {}
|
||||
SdBaseFile(const char* path, uint8_t oflag);
|
||||
~SdBaseFile() {if(isOpen()) close();}
|
||||
/**
|
||||
* writeError is set to true if an error occurs during a write().
|
||||
* Set writeError to false before calling print() and/or write() and check
|
||||
* for true after calls to print() and/or write().
|
||||
*/
|
||||
bool writeError;
|
||||
//----------------------------------------------------------------------------
|
||||
// helpers for stream classes
|
||||
/** get position for streams
|
||||
* \param[out] pos struct to receive position
|
||||
*/
|
||||
void getpos(fpos_t* pos);
|
||||
/** set position for streams
|
||||
* \param[out] pos struct with value for new position
|
||||
*/
|
||||
void setpos(fpos_t* pos);
|
||||
//----------------------------------------------------------------------------
|
||||
bool close();
|
||||
bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
|
||||
bool createContiguous(SdBaseFile* dirFile,
|
||||
const char* path, uint32_t size);
|
||||
/** \return The current cluster number for a file or directory. */
|
||||
uint32_t curCluster() const {return curCluster_;}
|
||||
/** \return The current position for a file or directory. */
|
||||
uint32_t curPosition() const {return curPosition_;}
|
||||
/** \return Current working directory */
|
||||
static SdBaseFile* cwd() {return cwd_;}
|
||||
/** Set the date/time callback function
|
||||
*
|
||||
* \param[in] dateTime The user's call back function. The callback
|
||||
* function is of the form:
|
||||
*
|
||||
* \code
|
||||
* void dateTime(uint16_t* date, uint16_t* time) {
|
||||
* uint16_t year;
|
||||
* uint8_t month, day, hour, minute, second;
|
||||
*
|
||||
* // User gets date and time from GPS or real-time clock here
|
||||
*
|
||||
* // return date using FAT_DATE macro to format fields
|
||||
* *date = FAT_DATE(year, month, day);
|
||||
*
|
||||
* // return time using FAT_TIME macro to format fields
|
||||
* *time = FAT_TIME(hour, minute, second);
|
||||
* }
|
||||
* \endcode
|
||||
*
|
||||
* Sets the function that is called when a file is created or when
|
||||
* a file's directory entry is modified by sync(). All timestamps,
|
||||
* access, creation, and modify, are set when a file is created.
|
||||
* sync() maintains the last access date and last modify date/time.
|
||||
*
|
||||
* See the timestamp() function.
|
||||
*/
|
||||
static void dateTimeCallback(
|
||||
void (*dateTime)(uint16_t* date, uint16_t* time)) {
|
||||
dateTime_ = dateTime;
|
||||
}
|
||||
/** Cancel the date/time callback function. */
|
||||
static void dateTimeCallbackCancel() {dateTime_ = 0;}
|
||||
bool dirEntry(dir_t* dir);
|
||||
static void dirName(const dir_t& dir, char* name);
|
||||
bool exists(const char* name);
|
||||
int16_t fgets(char* str, int16_t num, char* delim = 0);
|
||||
/** \return The total number of bytes in a file or directory. */
|
||||
uint32_t fileSize() const {return fileSize_;}
|
||||
/** \return The first cluster number for a file or directory. */
|
||||
uint32_t firstCluster() const {return firstCluster_;}
|
||||
bool getFilename(char* name);
|
||||
/** \return True if this is a directory else false. */
|
||||
bool isDir() const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
|
||||
/** \return True if this is a normal file else false. */
|
||||
bool isFile() const {return type_ == FAT_FILE_TYPE_NORMAL;}
|
||||
/** \return True if this is an open file/directory else false. */
|
||||
bool isOpen() const {return type_ != FAT_FILE_TYPE_CLOSED;}
|
||||
/** \return True if this is a subdirectory else false. */
|
||||
bool isSubDir() const {return type_ == FAT_FILE_TYPE_SUBDIR;}
|
||||
/** \return True if this is the root directory. */
|
||||
bool isRoot() const {
|
||||
return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32;
|
||||
}
|
||||
void ls(Print* pr, uint8_t flags = 0, uint8_t indent = 0);
|
||||
void ls(uint8_t flags = 0);
|
||||
bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
|
||||
// alias for backward compactability
|
||||
bool makeDir(SdBaseFile* dir, const char* path) {
|
||||
return mkdir(dir, path, false);
|
||||
}
|
||||
bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
|
||||
bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
|
||||
bool open(const char* path, uint8_t oflag = O_READ);
|
||||
bool openNext(SdBaseFile* dirFile, uint8_t oflag);
|
||||
bool openRoot(SdVolume* vol);
|
||||
int peek();
|
||||
static void printFatDate(uint16_t fatDate);
|
||||
static void printFatDate(Print* pr, uint16_t fatDate);
|
||||
static void printFatTime(uint16_t fatTime);
|
||||
static void printFatTime(Print* pr, uint16_t fatTime);
|
||||
bool printName();
|
||||
int16_t read();
|
||||
int16_t read(void* buf, uint16_t nbyte);
|
||||
int8_t readDir(dir_t* dir);
|
||||
static bool remove(SdBaseFile* dirFile, const char* path);
|
||||
bool remove();
|
||||
/** Set the file's current position to zero. */
|
||||
void rewind() {seekSet(0);}
|
||||
bool rename(SdBaseFile* dirFile, const char* newPath);
|
||||
bool rmdir();
|
||||
// for backward compatibility
|
||||
bool rmDir() {return rmdir();}
|
||||
bool rmRfStar();
|
||||
/** Set the files position to current position + \a pos. See seekSet().
|
||||
* \param[in] offset The new position in bytes from the current position.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool seekCur(int32_t offset) {
|
||||
return seekSet(curPosition_ + offset);
|
||||
}
|
||||
/** Set the files position to end-of-file + \a offset. See seekSet().
|
||||
* \param[in] offset The new position in bytes from end-of-file.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool seekEnd(int32_t offset = 0) {return seekSet(fileSize_ + offset);}
|
||||
bool seekSet(uint32_t pos);
|
||||
bool sync();
|
||||
bool timestamp(SdBaseFile* file);
|
||||
bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
|
||||
uint8_t hour, uint8_t minute, uint8_t second);
|
||||
/** Type of file. You should use isFile() or isDir() instead of type()
|
||||
* if possible.
|
||||
*
|
||||
* \return The file or directory type.
|
||||
*/
|
||||
uint8_t type() const {return type_;}
|
||||
bool truncate(uint32_t size);
|
||||
/** \return SdVolume that contains this file. */
|
||||
SdVolume* volume() const {return vol_;}
|
||||
int16_t write(const void* buf, uint16_t nbyte);
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
// allow SdFat to set cwd_
|
||||
friend class SdFat;
|
||||
// global pointer to cwd dir
|
||||
static SdBaseFile* cwd_;
|
||||
// data time callback function
|
||||
static void (*dateTime_)(uint16_t* date, uint16_t* time);
|
||||
// bits defined in flags_
|
||||
// should be 0X0F
|
||||
static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
|
||||
// sync of directory entry required
|
||||
static uint8_t const F_FILE_DIR_DIRTY = 0X80;
|
||||
|
||||
// private data
|
||||
uint8_t flags_; // See above for definition of flags_ bits
|
||||
uint8_t fstate_; // error and eof indicator
|
||||
uint8_t type_; // type of file see above for values
|
||||
uint32_t curCluster_; // cluster for current file position
|
||||
uint32_t curPosition_; // current file position in bytes from beginning
|
||||
uint32_t dirBlock_; // block for this files directory entry
|
||||
uint8_t dirIndex_; // index of directory entry in dirBlock
|
||||
uint32_t fileSize_; // file size in bytes
|
||||
uint32_t firstCluster_; // first cluster of file
|
||||
SdVolume* vol_; // volume where file is located
|
||||
|
||||
/** experimental don't use */
|
||||
bool openParent(SdBaseFile* dir);
|
||||
// private functions
|
||||
bool addCluster();
|
||||
bool addDirCluster();
|
||||
dir_t* cacheDirEntry(uint8_t action);
|
||||
int8_t lsPrintNext(Print *pr, uint8_t flags, uint8_t indent);
|
||||
static bool make83Name(const char* str, uint8_t* name, const char** ptr);
|
||||
bool mkdir(SdBaseFile* parent, const uint8_t dname[11]);
|
||||
bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
|
||||
bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
|
||||
dir_t* readDirCache();
|
||||
//------------------------------------------------------------------------------
|
||||
// to be deleted
|
||||
static void printDirName(const dir_t& dir,
|
||||
uint8_t width, bool printSlash);
|
||||
static void printDirName(Print* pr, const dir_t& dir,
|
||||
uint8_t width, bool printSlash);
|
||||
//------------------------------------------------------------------------------
|
||||
// Deprecated functions - suppress cpplint warnings with NOLINT comment
|
||||
#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
|
||||
public:
|
||||
/** \deprecated Use:
|
||||
* bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
|
||||
* \param[out] bgnBlock the first block address for the file.
|
||||
* \param[out] endBlock the last block address for the file.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
|
||||
return contiguousRange(&bgnBlock, &endBlock);
|
||||
}
|
||||
/** \deprecated Use:
|
||||
* bool createContiguous(SdBaseFile* dirFile,
|
||||
* const char* path, uint32_t size)
|
||||
* \param[in] dirFile The directory where the file will be created.
|
||||
* \param[in] path A path with a valid DOS 8.3 file name.
|
||||
* \param[in] size The desired file size.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool createContiguous(SdBaseFile& dirFile, // NOLINT
|
||||
const char* path, uint32_t size) {
|
||||
return createContiguous(&dirFile, path, size);
|
||||
}
|
||||
/** \deprecated Use:
|
||||
* static void dateTimeCallback(
|
||||
* void (*dateTime)(uint16_t* date, uint16_t* time));
|
||||
* \param[in] dateTime The user's call back function.
|
||||
*/
|
||||
static void dateTimeCallback(
|
||||
void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
|
||||
oldDateTime_ = dateTime;
|
||||
dateTime_ = dateTime ? oldToNew : 0;
|
||||
}
|
||||
/** \deprecated Use: bool dirEntry(dir_t* dir);
|
||||
* \param[out] dir Location for return of the file's directory entry.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT
|
||||
/** \deprecated Use:
|
||||
* bool mkdir(SdBaseFile* dir, const char* path);
|
||||
* \param[in] dir An open SdFat instance for the directory that will contain
|
||||
* the new directory.
|
||||
* \param[in] path A path with a valid 8.3 DOS name for the new directory.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool mkdir(SdBaseFile& dir, const char* path) { // NOLINT
|
||||
return mkdir(&dir, path);
|
||||
}
|
||||
/** \deprecated Use:
|
||||
* bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
|
||||
* \param[in] dirFile An open SdFat instance for the directory containing the
|
||||
* file to be opened.
|
||||
* \param[in] path A path with a valid 8.3 DOS name for the file.
|
||||
* \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
|
||||
* OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool open(SdBaseFile& dirFile, // NOLINT
|
||||
const char* path, uint8_t oflag) {
|
||||
return open(&dirFile, path, oflag);
|
||||
}
|
||||
/** \deprecated Do not use in new apps
|
||||
* \param[in] dirFile An open SdFat instance for the directory containing the
|
||||
* file to be opened.
|
||||
* \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool open(SdBaseFile& dirFile, const char* path) { // NOLINT
|
||||
return open(dirFile, path, O_RDWR);
|
||||
}
|
||||
/** \deprecated Use:
|
||||
* bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
|
||||
* \param[in] dirFile An open SdFat instance for the directory.
|
||||
* \param[in] index The \a index of the directory entry for the file to be
|
||||
* opened. The value for \a index is (directory file position)/32.
|
||||
* \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
|
||||
* OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
|
||||
return open(&dirFile, index, oflag);
|
||||
}
|
||||
/** \deprecated Use: bool openRoot(SdVolume* vol);
|
||||
* \param[in] vol The FAT volume containing the root directory to be opened.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT
|
||||
/** \deprecated Use: int8_t readDir(dir_t* dir);
|
||||
* \param[out] dir The dir_t struct that will receive the data.
|
||||
* \return bytes read for success zero for eof or -1 for failure.
|
||||
*/
|
||||
int8_t readDir(dir_t& dir) {return readDir(&dir);} // NOLINT
|
||||
/** \deprecated Use:
|
||||
* static uint8_t remove(SdBaseFile* dirFile, const char* path);
|
||||
* \param[in] dirFile The directory that contains the file.
|
||||
* \param[in] path The name of the file to be removed.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
static bool remove(SdBaseFile& dirFile, const char* path) { // NOLINT
|
||||
return remove(&dirFile, path);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
// rest are private
|
||||
private:
|
||||
static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
|
||||
static void oldToNew(uint16_t* date, uint16_t* time) {
|
||||
uint16_t d;
|
||||
uint16_t t;
|
||||
oldDateTime_(d, t);
|
||||
*date = d;
|
||||
*time = t;
|
||||
}
|
||||
#endif // ALLOW_DEPRECATED_FUNCTIONS
|
||||
};
|
||||
|
||||
#endif // SdBaseFile_h
|
@ -0,0 +1,329 @@
|
||||
/* Arduino SdFat Library
|
||||
* Copyright (C) 2009 by William Greiman
|
||||
*
|
||||
* This file is part of the Arduino SdFat Library
|
||||
*
|
||||
* This Library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Arduino SdFat Library. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "SdFat.h"
|
||||
#include "SdFatUtil.h"
|
||||
//------------------------------------------------------------------------------
|
||||
/** Change a volume's working directory to root
|
||||
*
|
||||
* Changes the volume's working directory to the SD's root directory.
|
||||
* Optionally set the current working directory to the volume's
|
||||
* working directory.
|
||||
*
|
||||
* \param[in] set_cwd Set the current working directory to this volume's
|
||||
* working directory if true.
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure.
|
||||
*/
|
||||
bool SdFat::chdir(bool set_cwd) {
|
||||
if (set_cwd) SdBaseFile::cwd_ = &vwd_;
|
||||
vwd_.close();
|
||||
return vwd_.openRoot(&vol_);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Change a volume's working directory
|
||||
*
|
||||
* Changes the volume working directory to the \a path subdirectory.
|
||||
* Optionally set the current working directory to the volume's
|
||||
* working directory.
|
||||
*
|
||||
* Example: If the volume's working directory is "/DIR", chdir("SUB")
|
||||
* will change the volume's working directory from "/DIR" to "/DIR/SUB".
|
||||
*
|
||||
* If path is "/", the volume's working directory will be changed to the
|
||||
* root directory
|
||||
*
|
||||
* \param[in] path The name of the subdirectory.
|
||||
*
|
||||
* \param[in] set_cwd Set the current working directory to this volume's
|
||||
* working directory if true.
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure.
|
||||
*/
|
||||
bool SdFat::chdir(const char *path, bool set_cwd) {
|
||||
SdBaseFile dir;
|
||||
if (path[0] == '/' && path[1] == '\0') return chdir(set_cwd);
|
||||
if (!dir.open(&vwd_, path, O_READ)) goto fail;
|
||||
if (!dir.isDir()) goto fail;
|
||||
vwd_ = dir;
|
||||
if (set_cwd) SdBaseFile::cwd_ = &vwd_;
|
||||
return true;
|
||||
|
||||
fail:
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Set the current working directory to a volume's working directory.
|
||||
*
|
||||
* This is useful with multiple SD cards.
|
||||
*
|
||||
* The current working directory is changed to this volume's working directory.
|
||||
*
|
||||
* This is like the Windows/DOS \<drive letter>: command.
|
||||
*/
|
||||
void SdFat::chvol() {
|
||||
SdBaseFile::cwd_ = &vwd_;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print any SD error code and halt. */
|
||||
void SdFat::errorHalt() {
|
||||
errorPrint();
|
||||
while (1);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print msg, any SD error code, and halt.
|
||||
*
|
||||
* \param[in] msg Message to print.
|
||||
*/
|
||||
void SdFat::errorHalt(char const* msg) {
|
||||
errorPrint(msg);
|
||||
while (1);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print msg, any SD error code, and halt.
|
||||
*
|
||||
* \param[in] msg Message in program space (flash memory) to print.
|
||||
*/
|
||||
void SdFat::errorHalt_P(PGM_P msg) {
|
||||
errorPrint_P(msg);
|
||||
while (1);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print any SD error code. */
|
||||
void SdFat::errorPrint() {
|
||||
if (!card_.errorCode()) return;
|
||||
PgmPrint("SD errorCode: 0X");
|
||||
Serial.println(card_.errorCode(), HEX);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print msg, any SD error code.
|
||||
*
|
||||
* \param[in] msg Message to print.
|
||||
*/
|
||||
void SdFat::errorPrint(char const* msg) {
|
||||
PgmPrint("error: ");
|
||||
Serial.println(msg);
|
||||
errorPrint();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print msg, any SD error code.
|
||||
*
|
||||
* \param[in] msg Message in program space (flash memory) to print.
|
||||
*/
|
||||
void SdFat::errorPrint_P(PGM_P msg) {
|
||||
PgmPrint("error: ");
|
||||
SerialPrintln_P(msg);
|
||||
errorPrint();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* Test for the existence of a file.
|
||||
*
|
||||
* \param[in] name Name of the file to be tested for.
|
||||
*
|
||||
* \return true if the file exists else false.
|
||||
*/
|
||||
bool SdFat::exists(const char* name) {
|
||||
return vwd_.exists(name);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* Initialize an SdFat object.
|
||||
*
|
||||
* Initializes the SD card, SD volume, and root directory.
|
||||
*
|
||||
* \param[in] sckRateID value for SPI SCK rate. See Sd2Card::init().
|
||||
* \param[in] chipSelectPin SD chip select pin. See Sd2Card::init().
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure.
|
||||
*/
|
||||
bool SdFat::init(uint8_t sckRateID, uint8_t chipSelectPin) {
|
||||
return card_.init(sckRateID, chipSelectPin) && vol_.init(&card_) && chdir(1);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print error details and halt after SdFat::init() fails. */
|
||||
void SdFat::initErrorHalt() {
|
||||
initErrorPrint();
|
||||
while (1);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**Print message, error details, and halt after SdFat::init() fails.
|
||||
*
|
||||
* \param[in] msg Message to print.
|
||||
*/
|
||||
void SdFat::initErrorHalt(char const *msg) {
|
||||
Serial.println(msg);
|
||||
initErrorHalt();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**Print message, error details, and halt after SdFat::init() fails.
|
||||
*
|
||||
* \param[in] msg Message in program space (flash memory) to print.
|
||||
*/
|
||||
void SdFat::initErrorHalt_P(PGM_P msg) {
|
||||
SerialPrintln_P(msg);
|
||||
initErrorHalt();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Print error details after SdFat::init() fails. */
|
||||
void SdFat::initErrorPrint() {
|
||||
if (card_.errorCode()) {
|
||||
PgmPrintln("Can't access SD card. Do not reformat.");
|
||||
if (card_.errorCode() == SD_CARD_ERROR_CMD0) {
|
||||
PgmPrintln("No card, wrong chip select pin, or SPI problem?");
|
||||
}
|
||||
errorPrint();
|
||||
} else if (vol_.fatType() == 0) {
|
||||
PgmPrintln("Invalid format, reformat SD.");
|
||||
} else if (!vwd_.isOpen()) {
|
||||
PgmPrintln("Can't open root directory.");
|
||||
} else {
|
||||
PgmPrintln("No error found.");
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**Print message and error details and halt after SdFat::init() fails.
|
||||
*
|
||||
* \param[in] msg Message to print.
|
||||
*/
|
||||
void SdFat::initErrorPrint(char const *msg) {
|
||||
Serial.println(msg);
|
||||
initErrorPrint();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**Print message and error details after SdFat::init() fails.
|
||||
*
|
||||
* \param[in] msg Message in program space (flash memory) to print.
|
||||
*/
|
||||
void SdFat::initErrorPrint_P(PGM_P msg) {
|
||||
SerialPrintln_P(msg);
|
||||
initErrorHalt();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** List the directory contents of the volume working directory to Serial.
|
||||
*
|
||||
* \param[in] flags The inclusive OR of
|
||||
*
|
||||
* LS_DATE - %Print file modification date
|
||||
*
|
||||
* LS_SIZE - %Print file size.
|
||||
*
|
||||
* LS_R - Recursive list of subdirectories.
|
||||
*/
|
||||
void SdFat::ls(uint8_t flags) {
|
||||
vwd_.ls(&Serial, flags);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** List the directory contents of the volume working directory to Serial.
|
||||
*
|
||||
* \param[in] pr Print stream for list.
|
||||
*
|
||||
* \param[in] flags The inclusive OR of
|
||||
*
|
||||
* LS_DATE - %Print file modification date
|
||||
*
|
||||
* LS_SIZE - %Print file size.
|
||||
*
|
||||
* LS_R - Recursive list of subdirectories.
|
||||
*/
|
||||
void SdFat::ls(Print* pr, uint8_t flags) {
|
||||
vwd_.ls(pr, flags);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Make a subdirectory in the volume working directory.
|
||||
*
|
||||
* \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
|
||||
*
|
||||
* \param[in] pFlag Create missing parent directories if true.
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure.
|
||||
*/
|
||||
bool SdFat::mkdir(const char* path, bool pFlag) {
|
||||
SdBaseFile sub;
|
||||
return sub.mkdir(&vwd_, path, pFlag);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Remove a file from the volume working directory.
|
||||
*
|
||||
* \param[in] path A path with a valid 8.3 DOS name for the file.
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure.
|
||||
*/
|
||||
bool SdFat::remove(const char* path) {
|
||||
return SdBaseFile::remove(&vwd_, path);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Rename a file or subdirectory.
|
||||
*
|
||||
* \param[in] oldPath Path name to the file or subdirectory to be renamed.
|
||||
*
|
||||
* \param[in] newPath New path name of the file or subdirectory.
|
||||
*
|
||||
* The \a newPath object must not exist before the rename call.
|
||||
*
|
||||
* The file to be renamed must not be open. The directory entry may be
|
||||
* moved and file system corruption could occur if the file is accessed by
|
||||
* a file object that was opened before the rename() call.
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure.
|
||||
*/
|
||||
bool SdFat::rename(const char *oldPath, const char *newPath) {
|
||||
SdBaseFile file;
|
||||
if (!file.open(oldPath, O_READ)) return false;
|
||||
return file.rename(&vwd_, newPath);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Remove a subdirectory from the volume's working directory.
|
||||
*
|
||||
* \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
|
||||
*
|
||||
* The subdirectory file will be removed only if it is empty.
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure.
|
||||
*/
|
||||
bool SdFat::rmdir(const char* path) {
|
||||
SdBaseFile sub;
|
||||
if (!sub.open(path, O_READ)) return false;
|
||||
return sub.rmdir();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** Truncate a file to a specified length. The current file position
|
||||
* will be maintained if it is less than or equal to \a length otherwise
|
||||
* it will be set to end of file.
|
||||
*
|
||||
* \param[in] path A path with a valid 8.3 DOS name for the file.
|
||||
* \param[in] length The desired length for the file.
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure.
|
||||
* Reasons for failure include file is read only, file is a directory,
|
||||
* \a length is greater than the current file size or an I/O error occurs.
|
||||
*/
|
||||
bool SdFat::truncate(const char* path, uint32_t length) {
|
||||
SdBaseFile file;
|
||||
if (!file.open(path, O_WRITE)) return false;
|
||||
return file.truncate(length);
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/* Arduino SdFat Library
|
||||
* Copyright (C) 2009 by William Greiman
|
||||
*
|
||||
* This file is part of the Arduino SdFat Library
|
||||
*
|
||||
* This Library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Arduino SdFat Library. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* \file
|
||||
* \brief configuration definitions
|
||||
*/
|
||||
#ifndef SdFatConfig_h
|
||||
#define SdFatConfig_h
|
||||
#include <stdint.h>
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* To use multiple SD cards set USE_MULTIPLE_CARDS nonzero.
|
||||
*
|
||||
* Using multiple cards costs 400 - 500 bytes of flash.
|
||||
*
|
||||
* Each card requires about 550 bytes of SRAM so use of a Mega is recommended.
|
||||
*/
|
||||
#define USE_MULTIPLE_CARDS 0
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* Call flush for endl if ENDL_CALLS_FLUSH is nonzero
|
||||
*
|
||||
* The standard for iostreams is to call flush. This is very costly for
|
||||
* SdFat. Each call to flush causes 2048 bytes of I/O to the SD.
|
||||
*
|
||||
* SdFat has a single 512 byte buffer for SD I/O so it must write the current
|
||||
* data block to the SD, read the directory block from the SD, update the
|
||||
* directory entry, write the directory block to the SD and read the data
|
||||
* block back into the buffer.
|
||||
*
|
||||
* The SD flash memory controller is not designed for this many rewrites
|
||||
* so performance may be reduced by more than a factor of 100.
|
||||
*
|
||||
* If ENDL_CALLS_FLUSH is zero, you must call flush and/or close to force
|
||||
* all data to be written to the SD.
|
||||
*/
|
||||
#define ENDL_CALLS_FLUSH 0
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* Allow use of deprecated functions if ALLOW_DEPRECATED_FUNCTIONS is nonzero
|
||||
*/
|
||||
#define ALLOW_DEPRECATED_FUNCTIONS 1
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* Allow FAT12 volumes if FAT12_SUPPORT is nonzero.
|
||||
* FAT12 has not been well tested.
|
||||
*/
|
||||
#define FAT12_SUPPORT 0
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* SPI init rate for SD initialization commands. Must be 5 (F_CPU/64)
|
||||
* or 6 (F_CPU/128).
|
||||
*/
|
||||
#define SPI_SD_INIT_RATE 5
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* Set the SS pin high for hardware SPI. If SS is chip select for another SPI
|
||||
* device this will disable that device during the SD init phase.
|
||||
*/
|
||||
#define SET_SPI_SS_HIGH 1
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* Define MEGA_SOFT_SPI nonzero to use software SPI on Mega Arduinos.
|
||||
* Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
|
||||
*
|
||||
* MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
|
||||
* on Mega Arduinos. Software SPI works well with GPS Shield V1.1
|
||||
* but many SD cards will fail with GPS Shield V1.0.
|
||||
*/
|
||||
#define MEGA_SOFT_SPI 0
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* Set USE_SOFTWARE_SPI nonzero to always use software SPI.
|
||||
*/
|
||||
#define USE_SOFTWARE_SPI 0
|
||||
// define software SPI pins so Mega can use unmodified 168/328 shields
|
||||
/** Software SPI chip select pin for the SD */
|
||||
uint8_t const SOFT_SPI_CS_PIN = 10;
|
||||
/** Software SPI Master Out Slave In pin */
|
||||
uint8_t const SOFT_SPI_MOSI_PIN = 11;
|
||||
/** Software SPI Master In Slave Out pin */
|
||||
uint8_t const SOFT_SPI_MISO_PIN = 12;
|
||||
/** Software SPI Clock pin */
|
||||
uint8_t const SOFT_SPI_SCK_PIN = 13;
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* The __cxa_pure_virtual function is an error handler that is invoked when
|
||||
* a pure virtual function is called.
|
||||
*/
|
||||
#define USE_CXA_PURE_VIRTUAL 1
|
||||
#endif // SdFatConfig_h
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,74 @@
|
||||
/* Arduino SdFat Library
|
||||
* Copyright (C) 2008 by William Greiman
|
||||
*
|
||||
* This file is part of the Arduino SdFat Library
|
||||
*
|
||||
* This Library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Arduino SdFat Library. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "SdFatUtil.h"
|
||||
//------------------------------------------------------------------------------
|
||||
/** Amount of free RAM
|
||||
* \return The number of free bytes.
|
||||
*/
|
||||
int SdFatUtil::FreeRam() {
|
||||
extern int __bss_end;
|
||||
extern int* __brkval;
|
||||
int free_memory;
|
||||
if (reinterpret_cast<int>(__brkval) == 0) {
|
||||
// if no heap use from end of bss section
|
||||
free_memory = reinterpret_cast<int>(&free_memory)
|
||||
- reinterpret_cast<int>(&__bss_end);
|
||||
} else {
|
||||
// use from top of stack to heap
|
||||
free_memory = reinterpret_cast<int>(&free_memory)
|
||||
- reinterpret_cast<int>(__brkval);
|
||||
}
|
||||
return free_memory;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print a string in flash memory.
|
||||
*
|
||||
* \param[in] pr Print object for output.
|
||||
* \param[in] str Pointer to string stored in flash memory.
|
||||
*/
|
||||
void SdFatUtil::print_P(Print* pr, PGM_P str) {
|
||||
for (uint8_t c; (c = pgm_read_byte(str)); str++) pr->write(c);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print a string in flash memory followed by a CR/LF.
|
||||
*
|
||||
* \param[in] pr Print object for output.
|
||||
* \param[in] str Pointer to string stored in flash memory.
|
||||
*/
|
||||
void SdFatUtil::println_P(Print* pr, PGM_P str) {
|
||||
print_P(pr, str);
|
||||
pr->println();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print a string in flash memory to Serial.
|
||||
*
|
||||
* \param[in] str Pointer to string stored in flash memory.
|
||||
*/
|
||||
void SdFatUtil::SerialPrint_P(PGM_P str) {
|
||||
print_P(&Serial, str);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/** %Print a string in flash memory to Serial followed by a CR/LF.
|
||||
*
|
||||
* \param[in] str Pointer to string stored in flash memory.
|
||||
*/
|
||||
void SdFatUtil::SerialPrintln_P(PGM_P str) {
|
||||
println_P(&Serial, str);
|
||||
}
|
@ -1,202 +0,0 @@
|
||||
/* Arduino SdFat Library
|
||||
* Copyright (C) 2009 by William Greiman
|
||||
*
|
||||
* This file is part of the Arduino SdFat Library
|
||||
*
|
||||
* This Library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Arduino SdFat Library. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
\mainpage Arduino SdFat Library
|
||||
<CENTER>Copyright © 2009 by William Greiman
|
||||
</CENTER>
|
||||
|
||||
\section Intro Introduction
|
||||
The Arduino SdFat Library is a minimal implementation of FAT16 and FAT32
|
||||
file systems on SD flash memory cards. Standard SD and high capacity
|
||||
SDHC cards are supported.
|
||||
|
||||
The SdFat only supports short 8.3 names.
|
||||
|
||||
The main classes in SdFat are Sd2Card, SdVolume, and SdFile.
|
||||
|
||||
The Sd2Card class supports access to standard SD cards and SDHC cards. Most
|
||||
applications will only need to call the Sd2Card::init() member function.
|
||||
|
||||
The SdVolume class supports FAT16 and FAT32 partitions. Most applications
|
||||
will only need to call the SdVolume::init() member function.
|
||||
|
||||
The SdFile class provides file access functions such as open(), read(),
|
||||
remove(), write(), close() and sync(). This class supports access to the root
|
||||
directory and subdirectories.
|
||||
|
||||
A number of example are provided in the SdFat/examples folder. These were
|
||||
developed to test SdFat and illustrate its use.
|
||||
|
||||
SdFat was developed for high speed data recording. SdFat was used to implement
|
||||
an audio record/play class, WaveRP, for the Adafruit Wave Shield. This
|
||||
application uses special Sd2Card calls to write to contiguous files in raw mode.
|
||||
These functions reduce write latency so that audio can be recorded with the
|
||||
small amount of RAM in the Arduino.
|
||||
|
||||
\section SDcard SD\SDHC Cards
|
||||
|
||||
Arduinos access SD cards using the cards SPI protocol. PCs, Macs, and
|
||||
most consumer devices use the 4-bit parallel SD protocol. A card that
|
||||
functions well on A PC or Mac may not work well on the Arduino.
|
||||
|
||||
Most cards have good SPI read performance but cards vary widely in SPI
|
||||
write performance. Write performance is limited by how efficiently the
|
||||
card manages internal erase/remapping operations. The Arduino cannot
|
||||
optimize writes to reduce erase operations because of its limit RAM.
|
||||
|
||||
SanDisk cards generally have good write performance. They seem to have
|
||||
more internal RAM buffering than other cards and therefore can limit
|
||||
the number of flash erase operations that the Arduino forces due to its
|
||||
limited RAM.
|
||||
|
||||
\section Hardware Hardware Configuration
|
||||
|
||||
SdFat was developed using an
|
||||
<A HREF = "http://www.adafruit.com/"> Adafruit Industries</A>
|
||||
<A HREF = "http://www.ladyada.net/make/waveshield/"> Wave Shield</A>.
|
||||
|
||||
The hardware interface to the SD card should not use a resistor based level
|
||||
shifter. SdFat sets the SPI bus frequency to 8 MHz which results in signal
|
||||
rise times that are too slow for the edge detectors in many newer SD card
|
||||
controllers when resistor voltage dividers are used.
|
||||
|
||||
The 5 to 3.3 V level shifter for 5 V Arduinos should be IC based like the
|
||||
74HC4050N based circuit shown in the file SdLevel.png. The Adafruit Wave Shield
|
||||
uses a 74AHC125N. Gravitech sells SD and MicroSD Card Adapters based on the
|
||||
74LCX245.
|
||||
|
||||
If you are using a resistor based level shifter and are having problems try
|
||||
setting the SPI bus frequency to 4 MHz. This can be done by using
|
||||
card.init(SPI_HALF_SPEED) to initialize the SD card.
|
||||
|
||||
\section comment Bugs and Comments
|
||||
|
||||
If you wish to report bugs or have comments, send email to fat16lib@sbcglobal.net.
|
||||
|
||||
\section SdFatClass SdFat Usage
|
||||
|
||||
SdFat uses a slightly restricted form of short names.
|
||||
Only printable ASCII characters are supported. No characters with code point
|
||||
values greater than 127 are allowed. Space is not allowed even though space
|
||||
was allowed in the API of early versions of DOS.
|
||||
|
||||
Short names are limited to 8 characters followed by an optional period (.)
|
||||
and extension of up to 3 characters. The characters may be any combination
|
||||
of letters and digits. The following special characters are also allowed:
|
||||
|
||||
$ % ' - _ @ ~ ` ! ( ) { } ^ # &
|
||||
|
||||
Short names are always converted to upper case and their original case
|
||||
value is lost.
|
||||
|
||||
\note
|
||||
The Arduino Print class uses character
|
||||
at a time writes so it was necessary to use a \link SdFile::sync() sync() \endlink
|
||||
function to control when data is written to the SD card.
|
||||
|
||||
\par
|
||||
An application which writes to a file using \link Print::print() print()\endlink,
|
||||
\link Print::println() println() \endlink
|
||||
or \link SdFile::write write() \endlink must call \link SdFile::sync() sync() \endlink
|
||||
at the appropriate time to force data and directory information to be written
|
||||
to the SD Card. Data and directory information are also written to the SD card
|
||||
when \link SdFile::close() close() \endlink is called.
|
||||
|
||||
\par
|
||||
Applications must use care calling \link SdFile::sync() sync() \endlink
|
||||
since 2048 bytes of I/O is required to update file and
|
||||
directory information. This includes writing the current data block, reading
|
||||
the block that contains the directory entry for update, writing the directory
|
||||
block back and reading back the current data block.
|
||||
|
||||
It is possible to open a file with two or more instances of SdFile. A file may
|
||||
be corrupted if data is written to the file by more than one instance of SdFile.
|
||||
|
||||
\section HowTo How to format SD Cards as FAT Volumes
|
||||
|
||||
You should use a freshly formatted SD card for best performance. FAT
|
||||
file systems become slower if many files have been created and deleted.
|
||||
This is because the directory entry for a deleted file is marked as deleted,
|
||||
but is not deleted. When a new file is created, these entries must be scanned
|
||||
before creating the file, a flaw in the FAT design. Also files can become
|
||||
fragmented which causes reads and writes to be slower.
|
||||
|
||||
Microsoft operating systems support removable media formatted with a
|
||||
Master Boot Record, MBR, or formatted as a super floppy with a FAT Boot Sector
|
||||
in block zero.
|
||||
|
||||
Microsoft operating systems expect MBR formatted removable media
|
||||
to have only one partition. The first partition should be used.
|
||||
|
||||
Microsoft operating systems do not support partitioning SD flash cards.
|
||||
If you erase an SD card with a program like KillDisk, Most versions of
|
||||
Windows will format the card as a super floppy.
|
||||
|
||||
The best way to restore an SD card's format is to use SDFormatter
|
||||
which can be downloaded from:
|
||||
|
||||
http://www.sdcard.org/consumers/formatter/
|
||||
|
||||
SDFormatter aligns flash erase boundaries with file
|
||||
system structures which reduces write latency and file system overhead.
|
||||
|
||||
SDFormatter does not have an option for FAT type so it may format
|
||||
small cards as FAT12.
|
||||
|
||||
After the MBR is restored by SDFormatter you may need to reformat small
|
||||
cards that have been formatted FAT12 to force the volume type to be FAT16.
|
||||
|
||||
If you reformat the SD card with an OS utility, choose a cluster size that
|
||||
will result in:
|
||||
|
||||
4084 < CountOfClusters && CountOfClusters < 65525
|
||||
|
||||
The volume will then be FAT16.
|
||||
|
||||
If you are formatting an SD card on OS X or Linux, be sure to use the first
|
||||
partition. Format this partition with a cluster count in above range.
|
||||
|
||||
\section References References
|
||||
|
||||
Adafruit Industries:
|
||||
|
||||
http://www.adafruit.com/
|
||||
|
||||
http://www.ladyada.net/make/waveshield/
|
||||
|
||||
The Arduino site:
|
||||
|
||||
http://www.arduino.cc/
|
||||
|
||||
For more information about FAT file systems see:
|
||||
|
||||
http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
|
||||
|
||||
For information about using SD cards as SPI devices see:
|
||||
|
||||
http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
|
||||
|
||||
The ATmega328 datasheet:
|
||||
|
||||
http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf
|
||||
|
||||
|
||||
*/
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,42 @@
|
||||
/* Arduino SdFat Library
|
||||
* Copyright (C) 2009 by William Greiman
|
||||
*
|
||||
* This file is part of the Arduino SdFat Library
|
||||
*
|
||||
* This Library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Arduino SdFat Library. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* \file
|
||||
* \brief SdFile class
|
||||
*/
|
||||
#include "SdBaseFile.h"
|
||||
#ifndef SdFile_h
|
||||
#define SdFile_h
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* \class SdFile
|
||||
* \brief SdBaseFile with Print.
|
||||
*/
|
||||
class SdFile : public SdBaseFile, public Print {
|
||||
public:
|
||||
SdFile() {}
|
||||
SdFile(const char* name, uint8_t oflag);
|
||||
void write(uint8_t b);
|
||||
int16_t write(const void* buf, uint16_t nbyte);
|
||||
void write(const char* str);
|
||||
void write_P(PGM_P str);
|
||||
void writeln_P(PGM_P str);
|
||||
};
|
||||
#endif // SdFile_h
|
@ -0,0 +1,211 @@
|
||||
/* Arduino SdFat Library
|
||||
* Copyright (C) 2009 by William Greiman
|
||||
*
|
||||
* This file is part of the Arduino SdFat Library
|
||||
*
|
||||
* This Library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with the Arduino SdFat Library. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef SdVolume_h
|
||||
#define SdVolume_h
|
||||
/**
|
||||
* \file
|
||||
* \brief SdVolume class
|
||||
*/
|
||||
#include "SdFatConfig.h"
|
||||
#include "Sd2Card.h"
|
||||
#include "SdFatStructs.h"
|
||||
|
||||
//==============================================================================
|
||||
// SdVolume class
|
||||
/**
|
||||
* \brief Cache for an SD data block
|
||||
*/
|
||||
union cache_t {
|
||||
/** Used to access cached file data blocks. */
|
||||
uint8_t data[512];
|
||||
/** Used to access cached FAT16 entries. */
|
||||
uint16_t fat16[256];
|
||||
/** Used to access cached FAT32 entries. */
|
||||
uint32_t fat32[128];
|
||||
/** Used to access cached directory entries. */
|
||||
dir_t dir[16];
|
||||
/** Used to access a cached Master Boot Record. */
|
||||
mbr_t mbr;
|
||||
/** Used to access to a cached FAT boot sector. */
|
||||
fat_boot_t fbs;
|
||||
/** Used to access to a cached FAT32 boot sector. */
|
||||
fat32_boot_t fbs32;
|
||||
/** Used to access to a cached FAT32 FSINFO sector. */
|
||||
fat32_fsinfo_t fsinfo;
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* \class SdVolume
|
||||
* \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
|
||||
*/
|
||||
class SdVolume {
|
||||
public:
|
||||
/** Create an instance of SdVolume */
|
||||
SdVolume() : fatType_(0) {}
|
||||
/** Clear the cache and returns a pointer to the cache. Used by the WaveRP
|
||||
* recorder to do raw write to the SD card. Not for normal apps.
|
||||
* \return A pointer to the cache buffer or zero if an error occurs.
|
||||
*/
|
||||
cache_t* cacheClear() {
|
||||
if (!cacheFlush()) return 0;
|
||||
cacheBlockNumber_ = 0XFFFFFFFF;
|
||||
return &cacheBuffer_;
|
||||
}
|
||||
/** Initialize a FAT volume. Try partition one first then try super
|
||||
* floppy format.
|
||||
*
|
||||
* \param[in] dev The Sd2Card where the volume is located.
|
||||
*
|
||||
* \return The value one, true, is returned for success and
|
||||
* the value zero, false, is returned for failure. Reasons for
|
||||
* failure include not finding a valid partition, not finding a valid
|
||||
* FAT file system or an I/O error.
|
||||
*/
|
||||
bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
|
||||
bool init(Sd2Card* dev, uint8_t part);
|
||||
|
||||
// inline functions that return volume info
|
||||
/** \return The volume's cluster size in blocks. */
|
||||
uint8_t blocksPerCluster() const {return blocksPerCluster_;}
|
||||
/** \return The number of blocks in one FAT. */
|
||||
uint32_t blocksPerFat() const {return blocksPerFat_;}
|
||||
/** \return The total number of clusters in the volume. */
|
||||
uint32_t clusterCount() const {return clusterCount_;}
|
||||
/** \return The shift count required to multiply by blocksPerCluster. */
|
||||
uint8_t clusterSizeShift() const {return clusterSizeShift_;}
|
||||
/** \return The logical block number for the start of file data. */
|
||||
uint32_t dataStartBlock() const {return dataStartBlock_;}
|
||||
/** \return The number of FAT structures on the volume. */
|
||||
uint8_t fatCount() const {return fatCount_;}
|
||||
/** \return The logical block number for the start of the first FAT. */
|
||||
uint32_t fatStartBlock() const {return fatStartBlock_;}
|
||||
/** \return The FAT type of the volume. Values are 12, 16 or 32. */
|
||||
uint8_t fatType() const {return fatType_;}
|
||||
int32_t freeClusterCount();
|
||||
/** \return The number of entries in the root directory for FAT16 volumes. */
|
||||
uint32_t rootDirEntryCount() const {return rootDirEntryCount_;}
|
||||
/** \return The logical block number for the start of the root directory
|
||||
on FAT16 volumes or the first cluster number on FAT32 volumes. */
|
||||
uint32_t rootDirStart() const {return rootDirStart_;}
|
||||
/** Sd2Card object for this volume
|
||||
* \return pointer to Sd2Card object.
|
||||
*/
|
||||
Sd2Card* sdCard() {return sdCard_;}
|
||||
/** Debug access to FAT table
|
||||
*
|
||||
* \param[in] n cluster number.
|
||||
* \param[out] v value of entry
|
||||
* \return true for success or false for failure
|
||||
*/
|
||||
bool dbgFat(uint32_t n, uint32_t* v) {return fatGet(n, v);}
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
// Allow SdBaseFile access to SdVolume private data.
|
||||
friend class SdBaseFile;
|
||||
|
||||
// value for dirty argument in cacheRawBlock to indicate read from cache
|
||||
static bool const CACHE_FOR_READ = false;
|
||||
// value for dirty argument in cacheRawBlock to indicate write to cache
|
||||
static bool const CACHE_FOR_WRITE = true;
|
||||
|
||||
#if USE_MULTIPLE_CARDS
|
||||
cache_t cacheBuffer_; // 512 byte cache for device blocks
|
||||
uint32_t cacheBlockNumber_; // Logical number of block in the cache
|
||||
Sd2Card* sdCard_; // Sd2Card object for cache
|
||||
bool cacheDirty_; // cacheFlush() will write block if true
|
||||
uint32_t cacheMirrorBlock_; // block number for mirror FAT
|
||||
#else // USE_MULTIPLE_CARDS
|
||||
static cache_t cacheBuffer_; // 512 byte cache for device blocks
|
||||
static uint32_t cacheBlockNumber_; // Logical number of block in the cache
|
||||
static Sd2Card* sdCard_; // Sd2Card object for cache
|
||||
static bool cacheDirty_; // cacheFlush() will write block if true
|
||||
static uint32_t cacheMirrorBlock_; // block number for mirror FAT
|
||||
#endif // USE_MULTIPLE_CARDS
|
||||
uint32_t allocSearchStart_; // start cluster for alloc search
|
||||
uint8_t blocksPerCluster_; // cluster size in blocks
|
||||
uint32_t blocksPerFat_; // FAT size in blocks
|
||||
uint32_t clusterCount_; // clusters in one FAT
|
||||
uint8_t clusterSizeShift_; // shift to convert cluster count to block count
|
||||
uint32_t dataStartBlock_; // first data block number
|
||||
uint8_t fatCount_; // number of FATs on volume
|
||||
uint32_t fatStartBlock_; // start block for first FAT
|
||||
uint8_t fatType_; // volume type (12, 16, OR 32)
|
||||
uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
|
||||
uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
|
||||
//----------------------------------------------------------------------------
|
||||
bool allocContiguous(uint32_t count, uint32_t* curCluster);
|
||||
uint8_t blockOfCluster(uint32_t position) const {
|
||||
return (position >> 9) & (blocksPerCluster_ - 1);}
|
||||
uint32_t clusterStartBlock(uint32_t cluster) const {
|
||||
return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);}
|
||||
uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
|
||||
return clusterStartBlock(cluster) + blockOfCluster(position);}
|
||||
cache_t *cache() {return &cacheBuffer_;}
|
||||
uint32_t cacheBlockNumber() {return cacheBlockNumber_;}
|
||||
#if USE_MULTIPLE_CARDS
|
||||
bool cacheFlush();
|
||||
bool cacheRawBlock(uint32_t blockNumber, bool dirty);
|
||||
#else // USE_MULTIPLE_CARDS
|
||||
static bool cacheFlush();
|
||||
static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
|
||||
#endif // USE_MULTIPLE_CARDS
|
||||
// used by SdBaseFile write to assign cache to SD location
|
||||
void cacheSetBlockNumber(uint32_t blockNumber, bool dirty) {
|
||||
cacheDirty_ = dirty;
|
||||
cacheBlockNumber_ = blockNumber;
|
||||
}
|
||||
void cacheSetDirty() {cacheDirty_ |= CACHE_FOR_WRITE;}
|
||||
bool chainSize(uint32_t beginCluster, uint32_t* size);
|
||||
bool fatGet(uint32_t cluster, uint32_t* value);
|
||||
bool fatPut(uint32_t cluster, uint32_t value);
|
||||
bool fatPutEOC(uint32_t cluster) {
|
||||
return fatPut(cluster, 0x0FFFFFFF);
|
||||
}
|
||||
bool freeChain(uint32_t cluster);
|
||||
bool isEOC(uint32_t cluster) const {
|
||||
if (FAT12_SUPPORT && fatType_ == 12) return cluster >= FAT12EOC_MIN;
|
||||
if (fatType_ == 16) return cluster >= FAT16EOC_MIN;
|
||||
return cluster >= FAT32EOC_MIN;
|
||||
}
|
||||
bool readBlock(uint32_t block, uint8_t* dst) {
|
||||
return sdCard_->readBlock(block, dst);}
|
||||
bool writeBlock(uint32_t block, const uint8_t* dst) {
|
||||
return sdCard_->writeBlock(block, dst);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
// Deprecated functions - suppress cpplint warnings with NOLINT comment
|
||||
#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
|
||||
public:
|
||||
/** \deprecated Use: bool SdVolume::init(Sd2Card* dev);
|
||||
* \param[in] dev The SD card where the volume is located.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool init(Sd2Card& dev) {return init(&dev);} // NOLINT
|
||||
/** \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol);
|
||||
* \param[in] dev The SD card where the volume is located.
|
||||
* \param[in] part The partition to be used.
|
||||
* \return true for success or false for failure.
|
||||
*/
|
||||
bool init(Sd2Card& dev, uint8_t part) { // NOLINT
|
||||
return init(&dev, part);
|
||||
}
|
||||
#endif // ALLOW_DEPRECATED_FUNCTIONS
|
||||
};
|
||||
#endif // SdVolume
|
@ -1,58 +0,0 @@
|
||||
files to compare manually:
|
||||
planner.cpp
|
||||
stepper.cpp
|
||||
temperature.cpp
|
||||
|
||||
---
|
||||
things that changed:
|
||||
* planner.cpp
|
||||
estimate_acc_distance now works with floats.
|
||||
in calculate_trapezoid:for_block
|
||||
long acceleration_rate=(long)((float)acceleration*8.388608) is gone
|
||||
so is block_>acceleration_rate
|
||||
void planner_reverse_pass:
|
||||
some stuff I don't understand right now changed
|
||||
in planner_forward_pass:
|
||||
done: BLOCK_BUFFER_SIZE is now necessarily power of 2 (aka 8 16, 32). Inportant to document this somewhere.
|
||||
no more inline in void plan_discard_current_block()
|
||||
no more inline in plan_get_current_block()
|
||||
in plan_buffer_line(...)
|
||||
the long target[4]; and calculations of thoose should go after the while(block_buffer_tail==..). if the axis_steps_per_unit are changed from the gcode (M92) the calculation for the currently planned buffer move will be corrupt, because Target is calculated with one value, and the stuff afterwards with another. At least this solved the problem I had with the M92 E* changes in the code. Very sure about this, I took me 20min to find this as the solution for the bug I was hunting.
|
||||
around if(feed_rate<minimumfeedrate) this only should be done if it is not a pure extrusion. I think there is a bug right now.
|
||||
~line 447 blockcount=
|
||||
not sure if this also works if the difference is negative, as it would happen if the ringbuffer runs over the end and start at 0.
|
||||
~line 507 tmp_aceleration. not sure whats going on, but a lot changed.
|
||||
|
||||
|
||||
* stepper.cpp
|
||||
~214: if (busy) should be a echoln, maybe
|
||||
~331: great, The Z_M_PIN checks are in :)
|
||||
|
||||
*temperature.cpp
|
||||
done: enum for heater, bed,
|
||||
manage_heater() is seriously different.
|
||||
done: if tem_meas_ready ==true->!true+return?
|
||||
done #define K1 0.95 maybe in the configuration.h?
|
||||
semi-done: PID-C checking needed. Untested but added.
|
||||
----
|
||||
|
||||
still needed to finish the merge, before testin!
|
||||
|
||||
manage_heater
|
||||
ISR
|
||||
movement planner
|
||||
|
||||
TODO:
|
||||
|
||||
remove traveling at maxpseed
|
||||
remove Simplelcd
|
||||
|
||||
remove DEBUG_STEPS?
|
||||
|
||||
block_t
|
||||
pid_dt ->0.1 whats the changes to the PID, checking needed
|
||||
|
||||
|
||||
----
|
||||
second merge saturday morning:
|
||||
done: PID_dt->0.1
|
Loading…
Reference in New Issue