[136] | 1 | /* Arduino SdFat Library
|
---|
| 2 | * Copyright (C) 2009 by William Greiman
|
---|
| 3 | *
|
---|
| 4 | * This file is part of the Arduino SdFat Library
|
---|
| 5 | *
|
---|
| 6 | * This Library is free software: you can redistribute it and/or modify
|
---|
| 7 | * it under the terms of the GNU General Public License as published by
|
---|
| 8 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 9 | * (at your option) any later version.
|
---|
| 10 | *
|
---|
| 11 | * This Library is distributed in the hope that it will be useful,
|
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | * GNU General Public License for more details.
|
---|
| 15 | *
|
---|
| 16 | * You should have received a copy of the GNU General Public License
|
---|
| 17 | * along with the Arduino SdFat Library. If not, see
|
---|
| 18 | * <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #ifndef SdFat_h
|
---|
| 21 | #define SdFat_h
|
---|
| 22 | /**
|
---|
| 23 | * \file
|
---|
| 24 | * SdFile and SdVolume classes
|
---|
| 25 | */
|
---|
| 26 | #ifdef __AVR__
|
---|
| 27 | #include <avr/pgmspace.h>
|
---|
| 28 | #endif
|
---|
| 29 | #include "Sd2Card.h"
|
---|
| 30 | #include "FatStructs.h"
|
---|
| 31 | #include "Print.h"
|
---|
| 32 | //------------------------------------------------------------------------------
|
---|
| 33 | /**
|
---|
| 34 | * Allow use of deprecated functions if non-zero
|
---|
| 35 | */
|
---|
| 36 | #define ALLOW_DEPRECATED_FUNCTIONS 1
|
---|
| 37 | //------------------------------------------------------------------------------
|
---|
| 38 | // forward declaration since SdVolume is used in SdFile
|
---|
| 39 | class SdVolume;
|
---|
| 40 | //==============================================================================
|
---|
| 41 | // SdFile class
|
---|
| 42 |
|
---|
| 43 | // flags for ls()
|
---|
| 44 | /** ls() flag to print modify date */
|
---|
| 45 | uint8_t const LS_DATE = 1;
|
---|
| 46 | /** ls() flag to print file size */
|
---|
| 47 | uint8_t const LS_SIZE = 2;
|
---|
| 48 | /** ls() flag for recursive list of subdirectories */
|
---|
| 49 | uint8_t const LS_R = 4;
|
---|
| 50 |
|
---|
| 51 | // use the gnu style oflag in open()
|
---|
| 52 | /** open() oflag for reading */
|
---|
| 53 | uint8_t const O_READ = 0X01;
|
---|
| 54 | /** open() oflag - same as O_READ */
|
---|
| 55 | uint8_t const O_RDONLY = O_READ;
|
---|
| 56 | /** open() oflag for write */
|
---|
| 57 | uint8_t const O_WRITE = 0X02;
|
---|
| 58 | /** open() oflag - same as O_WRITE */
|
---|
| 59 | uint8_t const O_WRONLY = O_WRITE;
|
---|
| 60 | /** open() oflag for reading and writing */
|
---|
| 61 | uint8_t const O_RDWR = (O_READ | O_WRITE);
|
---|
| 62 | /** open() oflag mask for access modes */
|
---|
| 63 | uint8_t const O_ACCMODE = (O_READ | O_WRITE);
|
---|
| 64 | /** The file offset shall be set to the end of the file prior to each write. */
|
---|
| 65 | uint8_t const O_APPEND = 0X04;
|
---|
| 66 | /** synchronous writes - call sync() after each write */
|
---|
| 67 | uint8_t const O_SYNC = 0X08;
|
---|
| 68 | /** create the file if nonexistent */
|
---|
| 69 | uint8_t const O_CREAT = 0X10;
|
---|
| 70 | /** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
|
---|
| 71 | uint8_t const O_EXCL = 0X20;
|
---|
| 72 | /** truncate the file to zero length */
|
---|
| 73 | uint8_t const O_TRUNC = 0X40;
|
---|
| 74 |
|
---|
| 75 | // flags for timestamp
|
---|
| 76 | /** set the file's last access date */
|
---|
| 77 | uint8_t const T_ACCESS = 1;
|
---|
| 78 | /** set the file's creation date and time */
|
---|
| 79 | uint8_t const T_CREATE = 2;
|
---|
| 80 | /** Set the file's write date and time */
|
---|
| 81 | uint8_t const T_WRITE = 4;
|
---|
| 82 | // values for type_
|
---|
| 83 | /** This SdFile has not been opened. */
|
---|
| 84 | uint8_t const FAT_FILE_TYPE_CLOSED = 0;
|
---|
| 85 | /** SdFile for a file */
|
---|
| 86 | uint8_t const FAT_FILE_TYPE_NORMAL = 1;
|
---|
| 87 | /** SdFile for a FAT16 root directory */
|
---|
| 88 | uint8_t const FAT_FILE_TYPE_ROOT16 = 2;
|
---|
| 89 | /** SdFile for a FAT32 root directory */
|
---|
| 90 | uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
|
---|
| 91 | /** SdFile for a subdirectory */
|
---|
| 92 | uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
|
---|
| 93 | /** Test value for directory type */
|
---|
| 94 | uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT16;
|
---|
| 95 |
|
---|
| 96 | /** date field for FAT directory entry */
|
---|
| 97 | static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
|
---|
| 98 | return (year - 1980) << 9 | month << 5 | day;
|
---|
| 99 | }
|
---|
| 100 | /** year part of FAT directory date field */
|
---|
| 101 | static inline uint16_t FAT_YEAR(uint16_t fatDate) {
|
---|
| 102 | return 1980 + (fatDate >> 9);
|
---|
| 103 | }
|
---|
| 104 | /** month part of FAT directory date field */
|
---|
| 105 | static inline uint8_t FAT_MONTH(uint16_t fatDate) {
|
---|
| 106 | return (fatDate >> 5) & 0XF;
|
---|
| 107 | }
|
---|
| 108 | /** day part of FAT directory date field */
|
---|
| 109 | static inline uint8_t FAT_DAY(uint16_t fatDate) {
|
---|
| 110 | return fatDate & 0X1F;
|
---|
| 111 | }
|
---|
| 112 | /** time field for FAT directory entry */
|
---|
| 113 | static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
|
---|
| 114 | return hour << 11 | minute << 5 | second >> 1;
|
---|
| 115 | }
|
---|
| 116 | /** hour part of FAT directory time field */
|
---|
| 117 | static inline uint8_t FAT_HOUR(uint16_t fatTime) {
|
---|
| 118 | return fatTime >> 11;
|
---|
| 119 | }
|
---|
| 120 | /** minute part of FAT directory time field */
|
---|
| 121 | static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
|
---|
| 122 | return(fatTime >> 5) & 0X3F;
|
---|
| 123 | }
|
---|
| 124 | /** second part of FAT directory time field */
|
---|
| 125 | static inline uint8_t FAT_SECOND(uint16_t fatTime) {
|
---|
| 126 | return 2*(fatTime & 0X1F);
|
---|
| 127 | }
|
---|
| 128 | /** Default date for file timestamps is 1 Jan 2000 */
|
---|
| 129 | uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
|
---|
| 130 | /** Default time for file timestamp is 1 am */
|
---|
| 131 | uint16_t const FAT_DEFAULT_TIME = (1 << 11);
|
---|
| 132 | //------------------------------------------------------------------------------
|
---|
| 133 | /**
|
---|
| 134 | * \class SdFile
|
---|
| 135 | * \brief Access FAT16 and FAT32 files on SD and SDHC cards.
|
---|
| 136 | */
|
---|
| 137 | class SdFile : public Print {
|
---|
| 138 | public:
|
---|
| 139 | /** Create an instance of SdFile. */
|
---|
| 140 | SdFile(void) : type_(FAT_FILE_TYPE_CLOSED) {}
|
---|
| 141 | /**
|
---|
| 142 | * writeError is set to true if an error occurs during a write().
|
---|
| 143 | * Set writeError to false before calling print() and/or write() and check
|
---|
| 144 | * for true after calls to print() and/or write().
|
---|
| 145 | */
|
---|
| 146 | //bool writeError;
|
---|
| 147 | /**
|
---|
| 148 | * Cancel unbuffered reads for this file.
|
---|
| 149 | * See setUnbufferedRead()
|
---|
| 150 | */
|
---|
| 151 | void clearUnbufferedRead(void) {
|
---|
| 152 | flags_ &= ~F_FILE_UNBUFFERED_READ;
|
---|
| 153 | }
|
---|
| 154 | uint8_t close(void);
|
---|
| 155 | uint8_t contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
|
---|
| 156 | uint8_t createContiguous(SdFile* dirFile,
|
---|
| 157 | const char* fileName, uint32_t size);
|
---|
| 158 | /** \return The current cluster number for a file or directory. */
|
---|
| 159 | uint32_t curCluster(void) const {return curCluster_;}
|
---|
| 160 | /** \return The current position for a file or directory. */
|
---|
| 161 | uint32_t curPosition(void) const {return curPosition_;}
|
---|
| 162 | /**
|
---|
| 163 | * Set the date/time callback function
|
---|
| 164 | *
|
---|
| 165 | * \param[in] dateTime The user's call back function. The callback
|
---|
| 166 | * function is of the form:
|
---|
| 167 | *
|
---|
| 168 | * \code
|
---|
| 169 | * void dateTime(uint16_t* date, uint16_t* time) {
|
---|
| 170 | * uint16_t year;
|
---|
| 171 | * uint8_t month, day, hour, minute, second;
|
---|
| 172 | *
|
---|
| 173 | * // User gets date and time from GPS or real-time clock here
|
---|
| 174 | *
|
---|
| 175 | * // return date using FAT_DATE macro to format fields
|
---|
| 176 | * *date = FAT_DATE(year, month, day);
|
---|
| 177 | *
|
---|
| 178 | * // return time using FAT_TIME macro to format fields
|
---|
| 179 | * *time = FAT_TIME(hour, minute, second);
|
---|
| 180 | * }
|
---|
| 181 | * \endcode
|
---|
| 182 | *
|
---|
| 183 | * Sets the function that is called when a file is created or when
|
---|
| 184 | * a file's directory entry is modified by sync(). All timestamps,
|
---|
| 185 | * access, creation, and modify, are set when a file is created.
|
---|
| 186 | * sync() maintains the last access date and last modify date/time.
|
---|
| 187 | *
|
---|
| 188 | * See the timestamp() function.
|
---|
| 189 | */
|
---|
| 190 | static void dateTimeCallback(
|
---|
| 191 | void (*dateTime)(uint16_t* date, uint16_t* time)) {
|
---|
| 192 | dateTime_ = dateTime;
|
---|
| 193 | }
|
---|
| 194 | /**
|
---|
| 195 | * Cancel the date/time callback function.
|
---|
| 196 | */
|
---|
| 197 | static void dateTimeCallbackCancel(void) {
|
---|
| 198 | // use explicit zero since NULL is not defined for Sanguino
|
---|
| 199 | dateTime_ = 0;
|
---|
| 200 | }
|
---|
| 201 | /** \return Address of the block that contains this file's directory. */
|
---|
| 202 | uint32_t dirBlock(void) const {return dirBlock_;}
|
---|
| 203 | uint8_t dirEntry(dir_t* dir);
|
---|
| 204 | /** \return Index of this file's directory in the block dirBlock. */
|
---|
| 205 | uint8_t dirIndex(void) const {return dirIndex_;}
|
---|
| 206 | static void dirName(const dir_t& dir, char* name);
|
---|
| 207 | /** \return The total number of bytes in a file or directory. */
|
---|
| 208 | uint32_t fileSize(void) const {return fileSize_;}
|
---|
| 209 | /** \return The first cluster number for a file or directory. */
|
---|
| 210 | uint32_t firstCluster(void) const {return firstCluster_;}
|
---|
| 211 | /** \return True if this is a SdFile for a directory else false. */
|
---|
| 212 | uint8_t isDir(void) const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
|
---|
| 213 | /** \return True if this is a SdFile for a file else false. */
|
---|
| 214 | uint8_t isFile(void) const {return type_ == FAT_FILE_TYPE_NORMAL;}
|
---|
| 215 | /** \return True if this is a SdFile for an open file/directory else false. */
|
---|
| 216 | uint8_t isOpen(void) const {return type_ != FAT_FILE_TYPE_CLOSED;}
|
---|
| 217 | /** \return True if this is a SdFile for a subdirectory else false. */
|
---|
| 218 | uint8_t isSubDir(void) const {return type_ == FAT_FILE_TYPE_SUBDIR;}
|
---|
| 219 | /** \return True if this is a SdFile for the root directory. */
|
---|
| 220 | uint8_t isRoot(void) const {
|
---|
| 221 | return type_ == FAT_FILE_TYPE_ROOT16 || type_ == FAT_FILE_TYPE_ROOT32;
|
---|
| 222 | }
|
---|
| 223 | void ls(uint8_t flags = 0, uint8_t indent = 0);
|
---|
| 224 | uint8_t makeDir(SdFile* dir, const char* dirName);
|
---|
| 225 | uint8_t open(SdFile* dirFile, uint16_t index, uint8_t oflag);
|
---|
| 226 | uint8_t open(SdFile* dirFile, const char* fileName, uint8_t oflag);
|
---|
| 227 |
|
---|
| 228 | uint8_t openRoot(SdVolume* vol);
|
---|
| 229 | static void printDirName(const dir_t& dir, uint8_t width);
|
---|
| 230 | static void printFatDate(uint16_t fatDate);
|
---|
| 231 | static void printFatTime(uint16_t fatTime);
|
---|
| 232 | static void printTwoDigits(uint8_t v);
|
---|
| 233 | /**
|
---|
| 234 | * Read the next byte from a file.
|
---|
| 235 | *
|
---|
| 236 | * \return For success read returns the next byte in the file as an int.
|
---|
| 237 | * If an error occurs or end of file is reached -1 is returned.
|
---|
| 238 | */
|
---|
| 239 | int16_t read(void) {
|
---|
| 240 | uint8_t b;
|
---|
| 241 | return read(&b, 1) == 1 ? b : -1;
|
---|
| 242 | }
|
---|
| 243 | int16_t read(void* buf, uint16_t nbyte);
|
---|
| 244 | int8_t readDir(dir_t* dir);
|
---|
| 245 | static uint8_t remove(SdFile* dirFile, const char* fileName);
|
---|
| 246 | uint8_t remove(void);
|
---|
| 247 | /** Set the file's current position to zero. */
|
---|
| 248 | void rewind(void) {
|
---|
| 249 | curPosition_ = curCluster_ = 0;
|
---|
| 250 | }
|
---|
| 251 | uint8_t rmDir(void);
|
---|
| 252 | uint8_t rmRfStar(void);
|
---|
| 253 | /** Set the files position to current position + \a pos. See seekSet(). */
|
---|
| 254 | uint8_t seekCur(uint32_t pos) {
|
---|
| 255 | return seekSet(curPosition_ + pos);
|
---|
| 256 | }
|
---|
| 257 | /**
|
---|
| 258 | * Set the files current position to end of file. Useful to position
|
---|
| 259 | * a file for append. See seekSet().
|
---|
| 260 | */
|
---|
| 261 | uint8_t seekEnd(void) {return seekSet(fileSize_);}
|
---|
| 262 | uint8_t seekSet(uint32_t pos);
|
---|
| 263 | /**
|
---|
| 264 | * Use unbuffered reads to access this file. Used with Wave
|
---|
| 265 | * Shield ISR. Used with Sd2Card::partialBlockRead() in WaveRP.
|
---|
| 266 | *
|
---|
| 267 | * Not recommended for normal applications.
|
---|
| 268 | */
|
---|
| 269 | void setUnbufferedRead(void) {
|
---|
| 270 | if (isFile()) flags_ |= F_FILE_UNBUFFERED_READ;
|
---|
| 271 | }
|
---|
| 272 | uint8_t timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
|
---|
| 273 | uint8_t hour, uint8_t minute, uint8_t second);
|
---|
| 274 | uint8_t sync(void);
|
---|
| 275 | /** Type of this SdFile. You should use isFile() or isDir() instead of type()
|
---|
| 276 | * if possible.
|
---|
| 277 | *
|
---|
| 278 | * \return The file or directory type.
|
---|
| 279 | */
|
---|
| 280 | uint8_t type(void) const {return type_;}
|
---|
| 281 | uint8_t truncate(uint32_t size);
|
---|
| 282 | /** \return Unbuffered read flag. */
|
---|
| 283 | uint8_t unbufferedRead(void) const {
|
---|
| 284 | return flags_ & F_FILE_UNBUFFERED_READ;
|
---|
| 285 | }
|
---|
| 286 | /** \return SdVolume that contains this file. */
|
---|
| 287 | SdVolume* volume(void) const {return vol_;}
|
---|
| 288 | size_t write(uint8_t b);
|
---|
| 289 | size_t write(const void* buf, uint16_t nbyte);
|
---|
| 290 | size_t write(const char* str);
|
---|
| 291 | #ifdef __AVR__
|
---|
| 292 | void write_P(PGM_P str);
|
---|
| 293 | void writeln_P(PGM_P str);
|
---|
| 294 | #endif
|
---|
| 295 | //------------------------------------------------------------------------------
|
---|
| 296 | #if ALLOW_DEPRECATED_FUNCTIONS
|
---|
| 297 | // Deprecated functions - suppress cpplint warnings with NOLINT comment
|
---|
| 298 | /** \deprecated Use:
|
---|
| 299 | * uint8_t SdFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
|
---|
| 300 | */
|
---|
| 301 | uint8_t contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) { // NOLINT
|
---|
| 302 | return contiguousRange(&bgnBlock, &endBlock);
|
---|
| 303 | }
|
---|
| 304 | /** \deprecated Use:
|
---|
| 305 | * uint8_t SdFile::createContiguous(SdFile* dirFile,
|
---|
| 306 | * const char* fileName, uint32_t size)
|
---|
| 307 | */
|
---|
| 308 | uint8_t createContiguous(SdFile& dirFile, // NOLINT
|
---|
| 309 | const char* fileName, uint32_t size) {
|
---|
| 310 | return createContiguous(&dirFile, fileName, size);
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /**
|
---|
| 314 | * \deprecated Use:
|
---|
| 315 | * static void SdFile::dateTimeCallback(
|
---|
| 316 | * void (*dateTime)(uint16_t* date, uint16_t* time));
|
---|
| 317 | */
|
---|
| 318 | static void dateTimeCallback(
|
---|
| 319 | void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
|
---|
| 320 | oldDateTime_ = dateTime;
|
---|
| 321 | dateTime_ = dateTime ? oldToNew : 0;
|
---|
| 322 | }
|
---|
| 323 | /** \deprecated Use: uint8_t SdFile::dirEntry(dir_t* dir); */
|
---|
| 324 | uint8_t dirEntry(dir_t& dir) {return dirEntry(&dir);} // NOLINT
|
---|
| 325 | /** \deprecated Use:
|
---|
| 326 | * uint8_t SdFile::makeDir(SdFile* dir, const char* dirName);
|
---|
| 327 | */
|
---|
| 328 | uint8_t makeDir(SdFile& dir, const char* dirName) { // NOLINT
|
---|
| 329 | return makeDir(&dir, dirName);
|
---|
| 330 | }
|
---|
| 331 | /** \deprecated Use:
|
---|
| 332 | * uint8_t SdFile::open(SdFile* dirFile, const char* fileName, uint8_t oflag);
|
---|
| 333 | */
|
---|
| 334 | uint8_t open(SdFile& dirFile, // NOLINT
|
---|
| 335 | const char* fileName, uint8_t oflag) {
|
---|
| 336 | return open(&dirFile, fileName, oflag);
|
---|
| 337 | }
|
---|
| 338 | /** \deprecated Do not use in new apps */
|
---|
| 339 | uint8_t open(SdFile& dirFile, const char* fileName) { // NOLINT
|
---|
| 340 | return open(dirFile, fileName, O_RDWR);
|
---|
| 341 | }
|
---|
| 342 | /** \deprecated Use:
|
---|
| 343 | * uint8_t SdFile::open(SdFile* dirFile, uint16_t index, uint8_t oflag);
|
---|
| 344 | */
|
---|
| 345 | uint8_t open(SdFile& dirFile, uint16_t index, uint8_t oflag) { // NOLINT
|
---|
| 346 | return open(&dirFile, index, oflag);
|
---|
| 347 | }
|
---|
| 348 | /** \deprecated Use: uint8_t SdFile::openRoot(SdVolume* vol); */
|
---|
| 349 | uint8_t openRoot(SdVolume& vol) {return openRoot(&vol);} // NOLINT
|
---|
| 350 |
|
---|
| 351 | /** \deprecated Use: int8_t SdFile::readDir(dir_t* dir); */
|
---|
| 352 | int8_t readDir(dir_t& dir) {return readDir(&dir);} // NOLINT
|
---|
| 353 | /** \deprecated Use:
|
---|
| 354 | * static uint8_t SdFile::remove(SdFile* dirFile, const char* fileName);
|
---|
| 355 | */
|
---|
| 356 | static uint8_t remove(SdFile& dirFile, const char* fileName) { // NOLINT
|
---|
| 357 | return remove(&dirFile, fileName);
|
---|
| 358 | }
|
---|
| 359 | //------------------------------------------------------------------------------
|
---|
| 360 | // rest are private
|
---|
| 361 | private:
|
---|
| 362 | static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
|
---|
| 363 | static void oldToNew(uint16_t* date, uint16_t* time) {
|
---|
| 364 | uint16_t d;
|
---|
| 365 | uint16_t t;
|
---|
| 366 | oldDateTime_(d, t);
|
---|
| 367 | *date = d;
|
---|
| 368 | *time = t;
|
---|
| 369 | }
|
---|
| 370 | #endif // ALLOW_DEPRECATED_FUNCTIONS
|
---|
| 371 | private:
|
---|
| 372 | // bits defined in flags_
|
---|
| 373 | // should be 0XF
|
---|
| 374 | static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
|
---|
| 375 | // available bits
|
---|
| 376 | static uint8_t const F_UNUSED = 0X30;
|
---|
| 377 | // use unbuffered SD read
|
---|
| 378 | static uint8_t const F_FILE_UNBUFFERED_READ = 0X40;
|
---|
| 379 | // sync of directory entry required
|
---|
| 380 | static uint8_t const F_FILE_DIR_DIRTY = 0X80;
|
---|
| 381 |
|
---|
| 382 | // make sure F_OFLAG is ok
|
---|
| 383 | #if ((F_UNUSED | F_FILE_UNBUFFERED_READ | F_FILE_DIR_DIRTY) & F_OFLAG)
|
---|
| 384 | #error flags_ bits conflict
|
---|
| 385 | #endif // flags_ bits
|
---|
| 386 |
|
---|
| 387 | // private data
|
---|
| 388 | uint8_t flags_; // See above for definition of flags_ bits
|
---|
| 389 | uint8_t type_; // type of file see above for values
|
---|
| 390 | uint32_t curCluster_; // cluster for current file position
|
---|
| 391 | uint32_t curPosition_; // current file position in bytes from beginning
|
---|
| 392 | uint32_t dirBlock_; // SD block that contains directory entry for file
|
---|
| 393 | uint8_t dirIndex_; // index of entry in dirBlock 0 <= dirIndex_ <= 0XF
|
---|
| 394 | uint32_t fileSize_; // file size in bytes
|
---|
| 395 | uint32_t firstCluster_; // first cluster of file
|
---|
| 396 | SdVolume* vol_; // volume where file is located
|
---|
| 397 |
|
---|
| 398 | // private functions
|
---|
| 399 | uint8_t addCluster(void);
|
---|
| 400 | uint8_t addDirCluster(void);
|
---|
| 401 | dir_t* cacheDirEntry(uint8_t action);
|
---|
| 402 | static void (*dateTime_)(uint16_t* date, uint16_t* time);
|
---|
| 403 | static uint8_t make83Name(const char* str, uint8_t* name);
|
---|
| 404 | uint8_t openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
|
---|
| 405 | dir_t* readDirCache(void);
|
---|
| 406 | };
|
---|
| 407 | //==============================================================================
|
---|
| 408 | // SdVolume class
|
---|
| 409 | /**
|
---|
| 410 | * \brief Cache for an SD data block
|
---|
| 411 | */
|
---|
| 412 | union cache_t {
|
---|
| 413 | /** Used to access cached file data blocks. */
|
---|
| 414 | uint8_t data[512];
|
---|
| 415 | /** Used to access cached FAT16 entries. */
|
---|
| 416 | uint16_t fat16[256];
|
---|
| 417 | /** Used to access cached FAT32 entries. */
|
---|
| 418 | uint32_t fat32[128];
|
---|
| 419 | /** Used to access cached directory entries. */
|
---|
| 420 | dir_t dir[16];
|
---|
| 421 | /** Used to access a cached MasterBoot Record. */
|
---|
| 422 | mbr_t mbr;
|
---|
| 423 | /** Used to access to a cached FAT boot sector. */
|
---|
| 424 | fbs_t fbs;
|
---|
| 425 | };
|
---|
| 426 | //------------------------------------------------------------------------------
|
---|
| 427 | /**
|
---|
| 428 | * \class SdVolume
|
---|
| 429 | * \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
|
---|
| 430 | */
|
---|
| 431 | class SdVolume {
|
---|
| 432 | public:
|
---|
| 433 | /** Create an instance of SdVolume */
|
---|
| 434 | SdVolume(void) :allocSearchStart_(2), fatType_(0) {}
|
---|
| 435 | /** Clear the cache and returns a pointer to the cache. Used by the WaveRP
|
---|
| 436 | * recorder to do raw write to the SD card. Not for normal apps.
|
---|
| 437 | */
|
---|
| 438 | static uint8_t* cacheClear(void) {
|
---|
| 439 | cacheFlush();
|
---|
| 440 | cacheBlockNumber_ = 0XFFFFFFFF;
|
---|
| 441 | return cacheBuffer_.data;
|
---|
| 442 | }
|
---|
| 443 | /**
|
---|
| 444 | * Initialize a FAT volume. Try partition one first then try super
|
---|
| 445 | * floppy format.
|
---|
| 446 | *
|
---|
| 447 | * \param[in] dev The Sd2Card where the volume is located.
|
---|
| 448 | *
|
---|
| 449 | * \return The value one, true, is returned for success and
|
---|
| 450 | * the value zero, false, is returned for failure. Reasons for
|
---|
| 451 | * failure include not finding a valid partition, not finding a valid
|
---|
| 452 | * FAT file system or an I/O error.
|
---|
| 453 | */
|
---|
| 454 | uint8_t init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
|
---|
| 455 | uint8_t init(Sd2Card* dev, uint8_t part);
|
---|
| 456 |
|
---|
| 457 | // inline functions that return volume info
|
---|
| 458 | /** \return The volume's cluster size in blocks. */
|
---|
| 459 | uint8_t blocksPerCluster(void) const {return blocksPerCluster_;}
|
---|
| 460 | /** \return The number of blocks in one FAT. */
|
---|
| 461 | uint32_t blocksPerFat(void) const {return blocksPerFat_;}
|
---|
| 462 | /** \return The total number of clusters in the volume. */
|
---|
| 463 | uint32_t clusterCount(void) const {return clusterCount_;}
|
---|
| 464 | /** \return The shift count required to multiply by blocksPerCluster. */
|
---|
| 465 | uint8_t clusterSizeShift(void) const {return clusterSizeShift_;}
|
---|
| 466 | /** \return The logical block number for the start of file data. */
|
---|
| 467 | uint32_t dataStartBlock(void) const {return dataStartBlock_;}
|
---|
| 468 | /** \return The number of FAT structures on the volume. */
|
---|
| 469 | uint8_t fatCount(void) const {return fatCount_;}
|
---|
| 470 | /** \return The logical block number for the start of the first FAT. */
|
---|
| 471 | uint32_t fatStartBlock(void) const {return fatStartBlock_;}
|
---|
| 472 | /** \return The FAT type of the volume. Values are 12, 16 or 32. */
|
---|
| 473 | uint8_t fatType(void) const {return fatType_;}
|
---|
| 474 | /** \return The number of entries in the root directory for FAT16 volumes. */
|
---|
| 475 | uint32_t rootDirEntryCount(void) const {return rootDirEntryCount_;}
|
---|
| 476 | /** \return The logical block number for the start of the root directory
|
---|
| 477 | on FAT16 volumes or the first cluster number on FAT32 volumes. */
|
---|
| 478 | uint32_t rootDirStart(void) const {return rootDirStart_;}
|
---|
| 479 | /** return a pointer to the Sd2Card object for this volume */
|
---|
| 480 | static Sd2Card* sdCard(void) {return sdCard_;}
|
---|
| 481 | //------------------------------------------------------------------------------
|
---|
| 482 | #if ALLOW_DEPRECATED_FUNCTIONS
|
---|
| 483 | // Deprecated functions - suppress cpplint warnings with NOLINT comment
|
---|
| 484 | /** \deprecated Use: uint8_t SdVolume::init(Sd2Card* dev); */
|
---|
| 485 | uint8_t init(Sd2Card& dev) {return init(&dev);} // NOLINT
|
---|
| 486 |
|
---|
| 487 | /** \deprecated Use: uint8_t SdVolume::init(Sd2Card* dev, uint8_t vol); */
|
---|
| 488 | uint8_t init(Sd2Card& dev, uint8_t part) { // NOLINT
|
---|
| 489 | return init(&dev, part);
|
---|
| 490 | }
|
---|
| 491 | #endif // ALLOW_DEPRECATED_FUNCTIONS
|
---|
| 492 | //------------------------------------------------------------------------------
|
---|
| 493 | private:
|
---|
| 494 | // Allow SdFile access to SdVolume private data.
|
---|
| 495 | friend class SdFile;
|
---|
| 496 |
|
---|
| 497 | // value for action argument in cacheRawBlock to indicate read from cache
|
---|
| 498 | static uint8_t const CACHE_FOR_READ = 0;
|
---|
| 499 | // value for action argument in cacheRawBlock to indicate cache dirty
|
---|
| 500 | static uint8_t const CACHE_FOR_WRITE = 1;
|
---|
| 501 |
|
---|
| 502 | static cache_t cacheBuffer_; // 512 byte cache for device blocks
|
---|
| 503 | static uint32_t cacheBlockNumber_; // Logical number of block in the cache
|
---|
| 504 | static Sd2Card* sdCard_; // Sd2Card object for cache
|
---|
| 505 | static uint8_t cacheDirty_; // cacheFlush() will write block if true
|
---|
| 506 | static uint32_t cacheMirrorBlock_; // block number for mirror FAT
|
---|
| 507 | //
|
---|
| 508 | uint32_t allocSearchStart_; // start cluster for alloc search
|
---|
| 509 | uint8_t blocksPerCluster_; // cluster size in blocks
|
---|
| 510 | uint32_t blocksPerFat_; // FAT size in blocks
|
---|
| 511 | uint32_t clusterCount_; // clusters in one FAT
|
---|
| 512 | uint8_t clusterSizeShift_; // shift to convert cluster count to block count
|
---|
| 513 | uint32_t dataStartBlock_; // first data block number
|
---|
| 514 | uint8_t fatCount_; // number of FATs on volume
|
---|
| 515 | uint32_t fatStartBlock_; // start block for first FAT
|
---|
| 516 | uint8_t fatType_; // volume type (12, 16, OR 32)
|
---|
| 517 | uint16_t rootDirEntryCount_; // number of entries in FAT16 root dir
|
---|
| 518 | uint32_t rootDirStart_; // root start block for FAT16, cluster for FAT32
|
---|
| 519 | //----------------------------------------------------------------------------
|
---|
| 520 | uint8_t allocContiguous(uint32_t count, uint32_t* curCluster);
|
---|
| 521 | uint8_t blockOfCluster(uint32_t position) const {
|
---|
| 522 | return (position >> 9) & (blocksPerCluster_ - 1);}
|
---|
| 523 | uint32_t clusterStartBlock(uint32_t cluster) const {
|
---|
| 524 | return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);}
|
---|
| 525 | uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
|
---|
| 526 | return clusterStartBlock(cluster) + blockOfCluster(position);}
|
---|
| 527 | static uint8_t cacheFlush(void);
|
---|
| 528 | static uint8_t cacheRawBlock(uint32_t blockNumber, uint8_t action);
|
---|
| 529 | static void cacheSetDirty(void) {cacheDirty_ |= CACHE_FOR_WRITE;}
|
---|
| 530 | static uint8_t cacheZeroBlock(uint32_t blockNumber);
|
---|
| 531 | uint8_t chainSize(uint32_t beginCluster, uint32_t* size) const;
|
---|
| 532 | uint8_t fatGet(uint32_t cluster, uint32_t* value) const;
|
---|
| 533 | uint8_t fatPut(uint32_t cluster, uint32_t value);
|
---|
| 534 | uint8_t fatPutEOC(uint32_t cluster) {
|
---|
| 535 | return fatPut(cluster, 0x0FFFFFFF);
|
---|
| 536 | }
|
---|
| 537 | uint8_t freeChain(uint32_t cluster);
|
---|
| 538 | uint8_t isEOC(uint32_t cluster) const {
|
---|
| 539 | return cluster >= (fatType_ == 16 ? FAT16EOC_MIN : FAT32EOC_MIN);
|
---|
| 540 | }
|
---|
| 541 | uint8_t readBlock(uint32_t block, uint8_t* dst) {
|
---|
| 542 | return sdCard_->readBlock(block, dst);}
|
---|
| 543 | uint8_t readData(uint32_t block, uint16_t offset,
|
---|
| 544 | uint16_t count, uint8_t* dst) {
|
---|
| 545 | return sdCard_->readData(block, offset, count, dst);
|
---|
| 546 | }
|
---|
| 547 | uint8_t writeBlock(uint32_t block, const uint8_t* dst) {
|
---|
| 548 | return sdCard_->writeBlock(block, dst);
|
---|
| 549 | }
|
---|
| 550 | };
|
---|
| 551 | #endif // SdFat_h
|
---|