Repetier-Firmware  0.91
src/ArduinoAVR/Repetier/SdFat.h
Go to the documentation of this file.
00001 /* Arduino SdFat Library
00002  * Copyright (C) 2012 by William Greiman
00003  *
00004  * This file is part of the Arduino SdFat Library
00005  *
00006  * This Library is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with the Arduino SdFat Library.  If not, see
00018  * <http://www.gnu.org/licenses/>.
00019  */
00020 /* Arduino SdFat Library
00021  * Copyright (C) 2012 by William Greiman
00022  *
00023  * This file is part of the Arduino SdFat Library
00024  *
00025  * This Library is free software: you can redistribute it and/or modify
00026  * it under the terms of the GNU General Public License as published by
00027  * the Free Software Foundation, either version 3 of the License, or
00028  * (at your option) any later version.
00029  *
00030  * This Library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00033  * GNU General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU General Public License
00036  * along with the Arduino SdFat Library.  If not, see
00037  * <http://www.gnu.org/licenses/>.
00038  */
00039 
00247 #ifndef SdFat_h
00248 #define SdFat_h
00249 
00253 //------------------------------------------------------------------------------
00255 #define SD_FAT_VERSION 20130629
00256 //------------------------------------------------------------------------------
00258 #if !defined(ARDUINO) || ARDUINO < 100
00259 #error Arduino IDE must be 1.0 or greater
00260 #endif  // ARDUINO < 100
00261 //------------------------------------------------------------------------------
00262 #include <stdint.h>
00263 // Based on the document:
00264 //
00265 // SD Specifications
00266 // Part 1
00267 // Physical Layer
00268 // Simplified Specification
00269 // Version 3.01
00270 // May 18, 2010
00271 //
00272 // http://www.sdcard.org/developers/tech/sdcard/pls/simplified_specs
00273 //------------------------------------------------------------------------------
00274 // SD card commands
00276 uint8_t const CMD0 = 0X00;
00278 uint8_t const CMD8 = 0X08;
00280 uint8_t const CMD9 = 0X09;
00282 uint8_t const CMD10 = 0X0A;
00284 uint8_t const CMD12 = 0X0C;
00286 uint8_t const CMD13 = 0X0D;
00288 uint8_t const CMD17 = 0X11;
00290 uint8_t const CMD18 = 0X12;
00292 uint8_t const CMD24 = 0X18;
00294 uint8_t const CMD25 = 0X19;
00296 uint8_t const CMD32 = 0X20;
00299 uint8_t const CMD33 = 0X21;
00301 uint8_t const CMD38 = 0X26;
00303 uint8_t const CMD55 = 0X37;
00305 uint8_t const CMD58 = 0X3A;
00307 uint8_t const CMD59 = 0X3B;
00310 uint8_t const ACMD23 = 0X17;
00313 uint8_t const ACMD41 = 0X29;
00314 //------------------------------------------------------------------------------
00316 uint8_t const R1_READY_STATE = 0X00;
00318 uint8_t const R1_IDLE_STATE = 0X01;
00320 uint8_t const R1_ILLEGAL_COMMAND = 0X04;
00322 uint8_t const DATA_START_BLOCK = 0XFE;
00324 uint8_t const STOP_TRAN_TOKEN = 0XFD;
00326 uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
00328 uint8_t const DATA_RES_MASK = 0X1F;
00330 uint8_t const DATA_RES_ACCEPTED = 0X05;
00331 //------------------------------------------------------------------------------
00333 typedef struct CID {
00334   // byte 0
00336   unsigned char mid;
00337   // byte 1-2
00339   char oid[2];
00340   // byte 3-7
00342   char pnm[5];
00343   // byte 8
00345   unsigned char prv_m : 4;
00347   unsigned char prv_n : 4;
00348   // byte 9-12
00350   uint32_t psn;
00351   // byte 13
00353   unsigned char mdt_year_high : 4;
00355   unsigned char reserved : 4;
00356   // byte 14
00358   unsigned char mdt_month : 4;
00360   unsigned char mdt_year_low :4;
00361   // byte 15
00363   unsigned char always1 : 1;
00365   unsigned char crc : 7;
00366 }__attribute__((packed)) cid_t;
00367 //------------------------------------------------------------------------------
00369 typedef struct CSDV1 {
00370   // byte 0
00371   unsigned char reserved1 : 6;
00372   unsigned char csd_ver : 2;
00373   // byte 1
00374   unsigned char taac;
00375   // byte 2
00376   unsigned char nsac;
00377   // byte 3
00378   unsigned char tran_speed;
00379   // byte 4
00380   unsigned char ccc_high;
00381   // byte 5
00382   unsigned char read_bl_len : 4;
00383   unsigned char ccc_low : 4;
00384   // byte 6
00385   unsigned char c_size_high : 2;
00386   unsigned char reserved2 : 2;
00387   unsigned char dsr_imp : 1;
00388   unsigned char read_blk_misalign :1;
00389   unsigned char write_blk_misalign : 1;
00390   unsigned char read_bl_partial : 1;
00391   // byte 7
00392   unsigned char c_size_mid;
00393   // byte 8
00394   unsigned char vdd_r_curr_max : 3;
00395   unsigned char vdd_r_curr_min : 3;
00396   unsigned char c_size_low :2;
00397   // byte 9
00398   unsigned char c_size_mult_high : 2;
00399   unsigned char vdd_w_cur_max : 3;
00400   unsigned char vdd_w_curr_min : 3;
00401   // byte 10
00402   unsigned char sector_size_high : 6;
00403   unsigned char erase_blk_en : 1;
00404   unsigned char c_size_mult_low : 1;
00405   // byte 11
00406   unsigned char wp_grp_size : 7;
00407   unsigned char sector_size_low : 1;
00408   // byte 12
00409   unsigned char write_bl_len_high : 2;
00410   unsigned char r2w_factor : 3;
00411   unsigned char reserved3 : 2;
00412   unsigned char wp_grp_enable : 1;
00413   // byte 13
00414   unsigned char reserved4 : 5;
00415   unsigned char write_partial : 1;
00416   unsigned char write_bl_len_low : 2;
00417   // byte 14
00418   unsigned char reserved5: 2;
00419   unsigned char file_format : 2;
00420   unsigned char tmp_write_protect : 1;
00421   unsigned char perm_write_protect : 1;
00422   unsigned char copy : 1;
00424   unsigned char file_format_grp : 1;
00425   // byte 15
00426   unsigned char always1 : 1;
00427   unsigned char crc : 7;
00428 }__attribute__((packed)) csd1_t;
00429 //------------------------------------------------------------------------------
00431 typedef struct CSDV2 {
00432   // byte 0
00433   unsigned char reserved1 : 6;
00434   unsigned char csd_ver : 2;
00435   // byte 1
00437   unsigned char taac;
00438   // byte 2
00440   unsigned char nsac;
00441   // byte 3
00442   unsigned char tran_speed;
00443   // byte 4
00444   unsigned char ccc_high;
00445   // byte 5
00447   unsigned char read_bl_len : 4;
00448   unsigned char ccc_low : 4;
00449   // byte 6
00451   unsigned char reserved2 : 4;
00452   unsigned char dsr_imp : 1;
00454   unsigned char read_blk_misalign :1;
00456   unsigned char write_blk_misalign : 1;
00458   unsigned char read_bl_partial : 1;
00459   // byte 7
00461   unsigned char c_size_high : 6;
00463   unsigned char reserved3 : 2;
00464   // byte 8
00466   unsigned char c_size_mid;
00467   // byte 9
00469   unsigned char c_size_low;
00470   // byte 10
00472   unsigned char sector_size_high : 6;
00474   unsigned char erase_blk_en : 1;
00476   unsigned char reserved4 : 1;
00477   // byte 11
00478   unsigned char wp_grp_size : 7;
00480   unsigned char sector_size_low : 1;
00481   // byte 12
00483   unsigned char write_bl_len_high : 2;
00485   unsigned char r2w_factor : 3;
00487   unsigned char reserved5 : 2;
00489   unsigned char wp_grp_enable : 1;
00490   // byte 13
00491   unsigned char reserved6 : 5;
00493   unsigned char write_partial : 1;
00495   unsigned char write_bl_len_low : 2;
00496   // byte 14
00497   unsigned char reserved7: 2;
00499   unsigned char file_format : 2;
00500   unsigned char tmp_write_protect : 1;
00501   unsigned char perm_write_protect : 1;
00502   unsigned char copy : 1;
00504   unsigned char file_format_grp : 1;
00505   // byte 15
00507   unsigned char always1 : 1;
00509   unsigned char crc : 7;
00510 }__attribute__((packed)) csd2_t;
00511 //------------------------------------------------------------------------------
00513 union csd_t {
00514   csd1_t v1;
00515   csd2_t v2;
00516 };
00517 
00518 //------------------------------------------------------------------------------
00526 #define USE_SD_CRC 2
00527 //------------------------------------------------------------------------------
00535 #define USE_MULTIPLE_CARDS 0
00536 //------------------------------------------------------------------------------
00542 #define DESTRUCTOR_CLOSES_FILE 0
00543 //------------------------------------------------------------------------------
00544 
00550 #ifdef __arm__
00551 #define USE_SEPARATE_FAT_CACHE 1
00552 #else  // __arm__
00553 #define USE_SEPARATE_FAT_CACHE 0
00554 #endif  // __arm__
00555 //------------------------------------------------------------------------------
00559 #if defined(RAMEND) && (RAMEND < 3000 || (NONLINEAR_SYSTEM && RAMEND<8000))
00560 #define USE_MULTI_BLOCK_SD_IO 0
00561 #else
00562 #define USE_MULTI_BLOCK_SD_IO 1
00563 #endif
00564 //------------------------------------------------------------------------------
00569 #define USE_ARDUINO_SPI_LIBRARY 0
00570 
00571 //------------------------------------------------------------------------------
00572 
00576 #if defined(__arm__) && defined(CORE_TEENSY)
00577 #define USE_NATIVE_MK20DX128_SPI 1
00578 #else
00579 #define USE_NATIVE_MK20DX128_SPI 0
00580 #endif
00581 //------------------------------------------------------------------------------
00585 #if defined(__arm__) && !defined(CORE_TEENSY)
00586 #define USE_NATIVE_SAM3X_SPI 1
00587 #else
00588 #define USE_NATIVE_SAM3X_SPI 0
00589 #endif
00590 //------------------------------------------------------------------------------
00602 #define USE_SERIAL_FOR_STD_OUT 0
00603 //------------------------------------------------------------------------------
00621 #define ENDL_CALLS_FLUSH 0
00622 //------------------------------------------------------------------------------
00626 #define ALLOW_DEPRECATED_FUNCTIONS 0
00627 //------------------------------------------------------------------------------
00632 #define FAT12_SUPPORT 0
00633 //------------------------------------------------------------------------------
00638 #define SPI_SD_INIT_RATE 11
00639 //------------------------------------------------------------------------------
00644 #define SET_SPI_SS_HIGH 1
00645 //------------------------------------------------------------------------------
00655 #define MEGA_SOFT_SPI 0
00656 //------------------------------------------------------------------------------
00666 #define LEONARDO_SOFT_SPI 0
00667 //------------------------------------------------------------------------------
00671 #define USE_SOFTWARE_SPI 0
00672 // define software SPI pins so Mega can use unmodified 168/328 shields
00674 uint8_t const SOFT_SPI_CS_PIN = 10;
00676 uint8_t const SOFT_SPI_MOSI_PIN = 11;
00678 uint8_t const SOFT_SPI_MISO_PIN = 12;
00680 uint8_t const SOFT_SPI_SCK_PIN = 13;
00681 //------------------------------------------------------------------------------
00682 // SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6
00684 uint8_t const SPI_FULL_SPEED = 0;
00686 uint8_t const SPI_DIV3_SPEED = 1;
00688 uint8_t const SPI_HALF_SPEED = 2;
00690 uint8_t const SPI_DIV6_SPEED = 3;
00692 uint8_t const SPI_QUARTER_SPEED = 4;
00694 uint8_t const SPI_EIGHTH_SPEED = 6;
00696 uint8_t const SPI_SIXTEENTH_SPEED = 8;
00698 const uint8_t MAX_SCK_RATE_ID = 14;
00699 //------------------------------------------------------------------------------
00701 uint16_t const SD_INIT_TIMEOUT = 2000;
00703 uint16_t const SD_ERASE_TIMEOUT = 10000;
00705 uint16_t const SD_READ_TIMEOUT = 300;
00707 uint16_t const SD_WRITE_TIMEOUT = 600;
00708 //------------------------------------------------------------------------------
00709 // SD card errors
00711 uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
00713 uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
00715 uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
00717 uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
00719 uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
00721 uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
00723 uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
00725 uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
00727 uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
00729 uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
00731 uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
00733 uint8_t const SD_CARD_ERROR_ERASE = 0XC;
00735 uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
00737 uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
00739 uint8_t const SD_CARD_ERROR_READ = 0XF;
00741 uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
00743 uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
00745 uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
00747 uint8_t const SD_CARD_ERROR_WRITE = 0X13;
00749 uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14;  // REMOVE - not used
00751 uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
00753 uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
00755 uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
00757 uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
00759 uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
00761 uint8_t const SD_CARD_ERROR_CMD59 = 0X1A;
00763 uint8_t const SD_CARD_ERROR_READ_CRC = 0X1B;
00765 uint8_t const SD_CARD_ERROR_SPI_DMA = 0X1C;
00766 //------------------------------------------------------------------------------
00767 // card types
00769 uint8_t const SD_CARD_TYPE_SD1  = 1;
00771 uint8_t const SD_CARD_TYPE_SD2  = 2;
00773 uint8_t const SD_CARD_TYPE_SDHC = 3;
00777 //------------------------------------------------------------------------------
00778 #if LEONARDO_SOFT_SPI && defined(__AVR_ATmega32U4__) && !defined(CORE_TEENSY)
00779 #define SOFTWARE_SPI
00780 #elif MEGA_SOFT_SPI&&(defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
00781 #define SOFTWARE_SPI
00782 #elif USE_SOFTWARE_SPI
00783 #define SOFTWARE_SPI
00784 #endif  // LEONARDO_SOFT_SPI
00785 //------------------------------------------------------------------------------
00786 // define default chip select pin
00787 //
00788 #ifndef SOFTWARE_SPI
00789 // hardware pin defs
00791 uint8_t const  SD_CHIP_SELECT_PIN = SDSS;
00792 #else  // SOFTWARE_SPI
00793 
00794 uint8_t const SD_CHIP_SELECT_PIN = SOFT_SPI_CS_PIN;
00795 #endif  // SOFTWARE_SPI
00796 //------------------------------------------------------------------------------
00801 class Sd2Card {
00802  public:
00804   Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
00805   uint32_t cardSize();
00806   bool erase(uint32_t firstBlock, uint32_t lastBlock);
00807   bool eraseSingleBlockEnable();
00812   void error(uint8_t code) {errorCode_ = code;}
00816   int errorCode() const {return errorCode_;}
00818   int errorData() const {return status_;}
00825   bool init(uint8_t sckRateID = SPI_FULL_SPEED,
00826     uint8_t chipSelectPin = SD_CHIP_SELECT_PIN);
00827   bool readBlock(uint32_t block, uint8_t* dst);
00837   bool readCID(cid_t* cid) {
00838     return readRegister(CMD10, cid);
00839   }
00848   bool readCSD(csd_t* csd) {
00849     return readRegister(CMD9, csd);
00850   }
00851   bool readData(uint8_t *dst);
00852   bool readStart(uint32_t blockNumber);
00853   bool readStop();
00854   bool setSckRate(uint8_t sckRateID);
00858   int type() const {return type_;}
00859   bool writeBlock(uint32_t blockNumber, const uint8_t* src);
00860   bool writeData(const uint8_t* src);
00861   bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
00862   bool writeStop();
00863 
00864  private:
00865   //----------------------------------------------------------------------------
00866   uint8_t chipSelectPin_;
00867   uint8_t errorCode_;
00868   uint8_t spiRate_;
00869   uint8_t status_;
00870   uint8_t type_;
00871   // private functions
00872   uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
00873     cardCommand(CMD55, 0);
00874     return cardCommand(cmd, arg);
00875   }
00876   uint8_t cardCommand(uint8_t cmd, uint32_t arg);
00877   bool readData(uint8_t* dst, size_t count);
00878   bool readRegister(uint8_t cmd, void* buf);
00879   void chipSelectHigh();
00880   void chipSelectLow();
00881   void type(uint8_t value) {type_ = value;}
00882   bool waitNotBusy(uint16_t timeoutMillis);
00883   bool writeData(uint8_t token, const uint8_t* src);
00884 };
00885 
00890 /*
00891  * mostly from Microsoft document fatgen103.doc
00892  * http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
00893  */
00894 //------------------------------------------------------------------------------
00896 uint8_t const BOOTSIG0 = 0X55;
00898 uint8_t const BOOTSIG1 = 0XAA;
00900 uint8_t const EXTENDED_BOOT_SIG = 0X29;
00901 //------------------------------------------------------------------------------
00909 struct partitionTable {
00915   uint8_t  boot;
00920   uint8_t  beginHead;
00925   unsigned beginSector : 6;
00927   unsigned beginCylinderHigh : 2;
00932   uint8_t  beginCylinderLow;
00937   uint8_t  type;
00942   uint8_t  endHead;
00947   unsigned endSector : 6;
00949   unsigned endCylinderHigh : 2;
00954   uint8_t  endCylinderLow;
00956   uint32_t firstSector;
00958   uint32_t totalSectors;
00959 } PACK;
00961 typedef struct partitionTable part_t;
00962 //------------------------------------------------------------------------------
00970 struct masterBootRecord {
00972   uint8_t  codeArea[440];
00974   uint32_t diskSignature;
00976   uint16_t usuallyZero;
00978   part_t   part[4];
00980   uint8_t  mbrSig0;
00982   uint8_t  mbrSig1;
00983 } PACK;
00985 typedef struct masterBootRecord mbr_t;
00986 //------------------------------------------------------------------------------
00993 struct fat_boot {
00999   uint8_t jump[3];
01004   char    oemId[8];
01010   uint16_t bytesPerSector;
01016   uint8_t  sectorsPerCluster;
01021   uint16_t reservedSectorCount;
01026   uint8_t  fatCount;
01035   uint16_t rootDirEntryCount;
01045   uint16_t totalSectors16;
01052   uint8_t  mediaType;
01058   uint16_t sectorsPerFat16;
01060   uint16_t sectorsPerTrack;
01062   uint16_t headCount;
01068   uint32_t hidddenSectors;
01075   uint32_t totalSectors32;
01084   uint8_t  driveNumber;
01086   uint8_t  reserved1;
01088   uint8_t  bootSignature;
01094   uint32_t volumeSerialNumber;
01099   char     volumeLabel[11];
01104   char     fileSystemType[8];
01106   uint8_t  bootCode[448];
01108   uint8_t  bootSectorSig0;
01110   uint8_t  bootSectorSig1;
01111 } PACK;
01113 typedef struct fat_boot fat_boot_t;
01114 //------------------------------------------------------------------------------
01121 struct fat32_boot {
01127   uint8_t jump[3];
01132   char    oemId[8];
01138   uint16_t bytesPerSector;
01144   uint8_t  sectorsPerCluster;
01149   uint16_t reservedSectorCount;
01154   uint8_t  fatCount;
01158   uint16_t rootDirEntryCount;
01162   uint16_t totalSectors16;
01169   uint8_t  mediaType;
01174   uint16_t sectorsPerFat16;
01176   uint16_t sectorsPerTrack;
01178   uint16_t headCount;
01184   uint32_t hidddenSectors;
01188   uint32_t totalSectors32;
01192   uint32_t sectorsPerFat32;
01204   uint16_t fat32Flags;
01209   uint16_t fat32Version;
01214   uint32_t fat32RootCluster;
01219   uint16_t fat32FSInfo;
01225   uint16_t fat32BackBootBlock;
01230   uint8_t  fat32Reserved[12];
01239   uint8_t  driveNumber;
01241   uint8_t  reserved1;
01243   uint8_t  bootSignature;
01249   uint32_t volumeSerialNumber;
01254   char     volumeLabel[11];
01258   char     fileSystemType[8];
01260   uint8_t  bootCode[420];
01262   uint8_t  bootSectorSig0;
01264   uint8_t  bootSectorSig1;
01265 } PACK;
01267 typedef struct fat32_boot fat32_boot_t;
01268 //------------------------------------------------------------------------------
01270 uint32_t const FSINFO_LEAD_SIG = 0x41615252;
01272 uint32_t const FSINFO_STRUCT_SIG = 0x61417272;
01279 struct fat32_fsinfo {
01281   uint32_t  leadSignature;
01283   uint8_t  reserved1[480];
01285   uint32_t  structSignature;
01293   uint32_t freeCount;
01300   uint32_t nextFree;
01302   uint8_t  reserved2[12];
01304   uint8_t  tailSignature[4];
01305 } PACK;
01307 typedef struct fat32_fsinfo fat32_fsinfo_t;
01308 //------------------------------------------------------------------------------
01309 // End Of Chain values for FAT entries
01311 uint16_t const FAT12EOC = 0XFFF;
01313 uint16_t const FAT12EOC_MIN = 0XFF8;
01315 uint16_t const FAT16EOC = 0XFFFF;
01317 uint16_t const FAT16EOC_MIN = 0XFFF8;
01319 uint32_t const FAT32EOC = 0X0FFFFFFF;
01321 uint32_t const FAT32EOC_MIN = 0X0FFFFFF8;
01323 uint32_t const FAT32MASK = 0X0FFFFFFF;
01324 
01325 // Reuse directory entries from deleted files
01326 #define SD_CARD_REUSE_FAT_ENTRIES true
01327 //------------------------------------------------------------------------------
01358 struct directoryEntry {
01364   uint8_t  name[11];
01371   uint8_t  attributes;
01376   uint8_t  reservedNT;
01382   uint8_t  creationTimeTenths;
01384   uint16_t creationTime;
01386   uint16_t creationDate;
01392   uint16_t lastAccessDate;
01397   uint16_t firstClusterHigh;
01399   uint16_t lastWriteTime;
01401   uint16_t lastWriteDate;
01403   uint16_t firstClusterLow;
01405   uint32_t fileSize;
01406 } PACK;
01407 
01408 // LONG FILENAME FAT ENTRY
01409 struct directoryVFATEntry {
01415   uint8_t  sequenceNumber;
01417   uint16_t name1[5];//UTF-16
01419   uint8_t  attributes;
01421   uint8_t  reservedNT;
01423   uint8_t  checksum;
01425   uint16_t name2[6];//UTF-16
01427   uint16_t firstClusterLow;
01429   uint16_t name3[2];//UTF-16
01430 } PACK;
01431 typedef struct directoryVFATEntry vfat_t;
01432 
01433 
01434 //------------------------------------------------------------------------------
01435 // Definitions for directory entries
01436 //
01438 typedef struct directoryEntry dir_t;
01440 uint8_t const DIR_NAME_0XE5 = 0X05;
01442 uint8_t const DIR_NAME_DELETED = 0XE5;
01444 uint8_t const DIR_NAME_FREE = 0X00;
01446 uint8_t const DIR_ATT_READ_ONLY = 0X01;
01448 uint8_t const DIR_ATT_HIDDEN = 0X02;
01450 uint8_t const DIR_ATT_SYSTEM = 0X04;
01452 uint8_t const DIR_ATT_VOLUME_ID = 0X08;
01454 uint8_t const DIR_ATT_DIRECTORY = 0X10;
01456 uint8_t const DIR_ATT_ARCHIVE = 0X20;
01459 uint8_t const DIR_ATT_LONG_NAME = 0X0F;
01461 uint8_t const DIR_ATT_LONG_NAME_MASK = 0X3F;
01463 uint8_t const DIR_ATT_DEFINED_BITS = 0X3F;
01469 static inline uint8_t DIR_IS_LONG_NAME(const dir_t* dir) {
01470   return (dir->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME;
01471 }
01473 uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
01479 static inline uint8_t DIR_IS_FILE(const dir_t* dir) {
01480   return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == 0;
01481 }
01487 static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
01488   return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == DIR_ATT_DIRECTORY;
01489 }
01495 static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
01496   return (dir->attributes & DIR_ATT_VOLUME_ID) == 0;
01497 }
01498 
01499 //==============================================================================
01500 // SdVolume class
01504 union cache_t {
01506   uint8_t  data[512];
01508   uint16_t fat16[256];
01510   uint32_t fat32[128];
01512   dir_t    dir[16];
01514   mbr_t    mbr;
01516   fat_boot_t fbs;
01518   fat32_boot_t fbs32;
01520   fat32_fsinfo_t fsinfo;
01521 } PACK;
01522 //------------------------------------------------------------------------------
01527 class SdVolume {
01528  public:
01530   SdVolume() : fatType_(0) {}
01535   cache_t* cacheClear() {
01536     if (!cacheSync()) return 0;
01537     cacheBlockNumber_ = 0XFFFFFFFF;
01538     return &cacheBuffer_;
01539   }
01550   bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
01551   bool init(Sd2Card* dev, uint8_t part);
01552 
01553   // inline functions that return volume info
01555   uint8_t blocksPerCluster() const {return blocksPerCluster_;}
01557   uint32_t blocksPerFat()  const {return blocksPerFat_;}
01559   uint32_t clusterCount() const {return clusterCount_;}
01561   uint8_t clusterSizeShift() const {return clusterSizeShift_;}
01563   uint32_t dataStartBlock() const {return dataStartBlock_;}
01565   uint8_t fatCount() const {return fatCount_;}
01567   uint32_t fatStartBlock() const {return fatStartBlock_;}
01569   uint8_t fatType() const {return fatType_;}
01570   int32_t freeClusterCount();
01572   uint32_t rootDirEntryCount() const {return rootDirEntryCount_;}
01575   uint32_t rootDirStart() const {return rootDirStart_;}
01579   Sd2Card* sdCard() {return sdCard_;}
01586   bool dbgFat(uint32_t n, uint32_t* v) {return fatGet(n, v);}
01587 //------------------------------------------------------------------------------
01588  private:
01589   // Allow SdBaseFile access to SdVolume private data.
01590   friend class SdBaseFile;
01591 //------------------------------------------------------------------------------
01592   uint32_t allocSearchStart_;   // start cluster for alloc search
01593   uint8_t blocksPerCluster_;    // cluster size in blocks
01594   uint32_t blocksPerFat_;       // FAT size in blocks
01595   uint32_t clusterCount_;       // clusters in one FAT
01596   uint8_t clusterSizeShift_;    // shift to convert cluster count to block count
01597   uint32_t dataStartBlock_;     // first data block number
01598   uint8_t fatCount_;            // number of FATs on volume
01599   uint32_t fatStartBlock_;      // start block for first FAT
01600   uint8_t fatType_;             // volume type (12, 16, OR 32)
01601   uint16_t rootDirEntryCount_;  // number of entries in FAT16 root dir
01602   uint32_t rootDirStart_;       // root start block for FAT16, cluster for FAT32
01603 //------------------------------------------------------------------------------
01604 // block caches
01605 // use of static functions save a bit of flash - maybe not worth complexity
01606 //
01607   static const uint8_t CACHE_STATUS_DIRTY = 1;
01608   static const uint8_t CACHE_STATUS_FAT_BLOCK = 2;
01609   static const uint8_t CACHE_STATUS_MASK
01610      = CACHE_STATUS_DIRTY | CACHE_STATUS_FAT_BLOCK;
01611   static const uint8_t CACHE_OPTION_NO_READ = 4;
01612   // value for option argument in cacheFetch to indicate read from cache
01613   static uint8_t const CACHE_FOR_READ = 0;
01614   // value for option argument in cacheFetch to indicate write to cache
01615   static uint8_t const CACHE_FOR_WRITE = CACHE_STATUS_DIRTY;
01616   // reserve cache block with no read
01617   static uint8_t const CACHE_RESERVE_FOR_WRITE
01618      = CACHE_STATUS_DIRTY | CACHE_OPTION_NO_READ;
01619 #if USE_MULTIPLE_CARDS
01620   cache_t cacheBuffer_;        // 512 byte cache for device blocks
01621   uint32_t cacheBlockNumber_;  // Logical number of block in the cache
01622   uint32_t cacheFatOffset_;    // offset for mirrored FAT
01623   Sd2Card* sdCard_;            // Sd2Card object for cache
01624   uint8_t cacheStatus_;        // status of cache block
01625 #if USE_SEPARATE_FAT_CACHE
01626   cache_t cacheFatBuffer_;       // 512 byte cache for FAT
01627   uint32_t cacheFatBlockNumber_;  // current Fat block number
01628   uint8_t  cacheFatStatus_;       // status of cache Fatblock
01629 #endif  // USE_SEPARATE_FAT_CACHE
01630 #else  // USE_MULTIPLE_CARDS
01631   static cache_t cacheBuffer_;        // 512 byte cache for device blocks
01632   static uint32_t cacheBlockNumber_;  // Logical number of block in the cache
01633   static uint32_t cacheFatOffset_;    // offset for mirrored FAT
01634   static uint8_t cacheStatus_;        // status of cache block
01635 #if USE_SEPARATE_FAT_CACHE
01636   static cache_t cacheFatBuffer_;       // 512 byte cache for FAT
01637   static uint32_t cacheFatBlockNumber_;  // current Fat block number
01638   static uint8_t  cacheFatStatus_;       // status of cache Fatblock
01639 #endif  // USE_SEPARATE_FAT_CACHE
01640   static Sd2Card* sdCard_;            // Sd2Card object for cache
01641 #endif  // USE_MULTIPLE_CARDS
01642 
01643   cache_t *cacheAddress() {return &cacheBuffer_;}
01644   uint32_t cacheBlockNumber() {return cacheBlockNumber_;}
01645 #if USE_MULTIPLE_CARDS
01646   cache_t* cacheFetch(uint32_t blockNumber, uint8_t options);
01647   cache_t* cacheFetchData(uint32_t blockNumber, uint8_t options);
01648   cache_t* cacheFetchFat(uint32_t blockNumber, uint8_t options);
01649   void cacheInvalidate();
01650   bool cacheSync();
01651   bool cacheWriteData();
01652   bool cacheWriteFat();
01653 #else  // USE_MULTIPLE_CARDS
01654   static cache_t* cacheFetch(uint32_t blockNumber, uint8_t options);
01655   static cache_t* cacheFetchData(uint32_t blockNumber, uint8_t options);
01656   static cache_t* cacheFetchFat(uint32_t blockNumber, uint8_t options);
01657   static void cacheInvalidate();
01658   static bool cacheSync();
01659   static bool cacheWriteData();
01660   static bool cacheWriteFat();
01661 #endif  // USE_MULTIPLE_CARDS
01662 //------------------------------------------------------------------------------
01663   bool allocContiguous(uint32_t count, uint32_t* curCluster);
01664   uint8_t blockOfCluster(uint32_t position) const {
01665           return (position >> 9) & (blocksPerCluster_ - 1);}
01666   uint32_t clusterStartBlock(uint32_t cluster) const;
01667   bool fatGet(uint32_t cluster, uint32_t* value);
01668   bool fatPut(uint32_t cluster, uint32_t value);
01669   bool fatPutEOC(uint32_t cluster) {
01670     return fatPut(cluster, 0x0FFFFFFF);
01671   }
01672   bool freeChain(uint32_t cluster);
01673   bool isEOC(uint32_t cluster) const {
01674     if (FAT12_SUPPORT && fatType_ == 12) return  cluster >= FAT12EOC_MIN;
01675     if (fatType_ == 16) return cluster >= FAT16EOC_MIN;
01676     return  cluster >= FAT32EOC_MIN;
01677   }
01678   bool readBlock(uint32_t block, uint8_t* dst) {
01679     return sdCard_->readBlock(block, dst);}
01680   bool writeBlock(uint32_t block, const uint8_t* dst) {
01681     return sdCard_->writeBlock(block, dst);
01682   }
01683 //------------------------------------------------------------------------------
01684   // Deprecated functions  - suppress cpplint warnings with NOLINT comment
01685 #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
01686 
01687  public:
01692   bool init(Sd2Card& dev) {return init(&dev);}  // NOLINT
01698   bool init(Sd2Card& dev, uint8_t part) {  // NOLINT
01699     return init(&dev, part);
01700   }
01701 #endif  // ALLOW_DEPRECATED_FUNCTIONS
01702 };
01703 
01704 //------------------------------------------------------------------------------
01710 struct FatPos_t {
01712   uint32_t position;
01714   uint32_t cluster;
01715   FatPos_t() : position(0), cluster(0) {}
01716 };
01717 
01718 // use the gnu style oflag in open()
01720 uint8_t const O_READ = 0X01;
01722 uint8_t const O_RDONLY = O_READ;
01724 uint8_t const O_WRITE = 0X02;
01726 uint8_t const O_WRONLY = O_WRITE;
01728 uint8_t const O_RDWR = (O_READ | O_WRITE);
01730 uint8_t const O_ACCMODE = (O_READ | O_WRITE);
01732 uint8_t const O_APPEND = 0X04;
01734 uint8_t const O_SYNC = 0X08;
01736 uint8_t const O_TRUNC = 0X10;
01738 uint8_t const O_AT_END = 0X20;
01740 uint8_t const O_CREAT = 0X40;
01742 uint8_t const O_EXCL = 0X80;
01743 
01744 // SdBaseFile class static and const definitions
01745 // flags for ls()
01747 uint8_t const LS_DATE = 1;
01749 uint8_t const LS_SIZE = 2;
01751 uint8_t const LS_R = 4;
01752 
01753 
01754 // flags for timestamp
01756 uint8_t const T_ACCESS = 1;
01758 uint8_t const T_CREATE = 2;
01760 uint8_t const T_WRITE = 4;
01761 // values for type_
01763 uint8_t const FAT_FILE_TYPE_CLOSED = 0;
01765 uint8_t const FAT_FILE_TYPE_NORMAL = 1;
01767 uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
01769 uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
01771 uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
01773 uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;
01774 
01782 static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
01783   return (year - 1980) << 9 | month << 5 | day;
01784 }
01790 static inline uint16_t FAT_YEAR(uint16_t fatDate) {
01791   return 1980 + (fatDate >> 9);
01792 }
01798 static inline uint8_t FAT_MONTH(uint16_t fatDate) {
01799   return (fatDate >> 5) & 0XF;
01800 }
01806 static inline uint8_t FAT_DAY(uint16_t fatDate) {
01807   return fatDate & 0X1F;
01808 }
01816 static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
01817   return hour << 11 | minute << 5 | second >> 1;
01818 }
01824 static inline uint8_t FAT_HOUR(uint16_t fatTime) {
01825   return fatTime >> 11;
01826 }
01832 static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
01833   return(fatTime >> 5) & 0X3F;
01834 }
01842 static inline uint8_t FAT_SECOND(uint16_t fatTime) {
01843   return 2*(fatTime & 0X1F);
01844 }
01846 uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
01848 uint16_t const FAT_DEFAULT_TIME = (1 << 11);
01849 //------------------------------------------------------------------------------
01854 class SdBaseFile {
01855  public:
01857   SdBaseFile() : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {}
01858   SdBaseFile(const char* path, uint8_t oflag);
01859   #if DESTRUCTOR_CLOSES_FILE
01860   ~SdBaseFile() {if(isOpen()) close();}
01861   #endif
01862 
01867   bool writeError;
01869   bool getWriteError() {return writeError;}
01871   void clearWriteError() {writeError = 0;}
01872   //----------------------------------------------------------------------------
01873   // helpers for stream classes
01877   void getpos(FatPos_t* pos);
01881   void setpos(FatPos_t* pos);
01882   //----------------------------------------------------------------------------
01884   uint32_t available() {return fileSize() - curPosition();}
01885   bool close();
01886   bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
01887   bool createContiguous(SdBaseFile* dirFile,
01888           const char* path, uint32_t size);
01890   uint32_t curCluster() const {return curCluster_;}
01892   uint32_t curPosition() const {return curPosition_;}
01894   static SdBaseFile* cwd() {return cwd_;}
01922   static void dateTimeCallback(
01923     void (*dateTime)(uint16_t* date, uint16_t* time)) {
01924     dateTime_ = dateTime;
01925   }
01927   static void dateTimeCallbackCancel() {dateTime_ = 0;}
01928   bool dirEntry(dir_t* dir);
01929   static void dirName(const dir_t& dir, char* name);
01930   bool exists(const char* name);
01931   int16_t fgets(char* str, int16_t num, char* delim = 0);
01933   uint32_t fileSize() const {return fileSize_;}
01935   uint32_t firstCluster() const {return firstCluster_;}
01936   bool getFilename(char* name);
01937   uint8_t lfn_checksum(const unsigned char *pFCBName);
01938   bool openParentReturnFile(SdBaseFile* dirFile, const char* path, uint8_t *dname, SdBaseFile *newParent, boolean bMakeDirs);
01939 
01940   
01942   bool isDir() const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
01944   bool isFile() const {return type_ == FAT_FILE_TYPE_NORMAL;}
01946   bool isOpen() const {return type_ != FAT_FILE_TYPE_CLOSED;}
01948   bool isSubDir() const {return type_ == FAT_FILE_TYPE_SUBDIR;}
01950   bool isRoot() const {
01951     return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32;
01952   }
01953   void ls(uint8_t flags = 0, uint8_t indent = 0);
01954   void ls(uint8_t flags = 0);
01955   bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
01956   // alias for backward compactability
01957   bool makeDir(SdBaseFile* dir, const char* path) {
01958     return mkdir(dir, path, false);
01959   }
01960   bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
01961   bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
01962   bool open(const char* path, uint8_t oflag = O_READ);
01963   bool openNext(SdBaseFile* dirFile, uint8_t oflag);
01964   bool openRoot(SdVolume* vol);
01965   int8_t readDir(dir_t& dir, char *longfilename) {return readDir(&dir, longfilename);}
01966   int peek();
01967   bool printCreateDateTime();
01968   static void printFatDate(uint16_t fatDate);
01969   static void printFatTime(uint16_t fatTime);
01970   bool printModifyDateTime();
01971   int printField(int16_t value, char term);
01972   int printField(uint16_t value, char term);
01973   int printField(int32_t value, char term);
01974   int printField(uint32_t value, char term);
01975   bool printName();
01976   int16_t read();
01977   int read(void* buf, size_t nbyte);
01978   int8_t readDir(dir_t* dir, char *longfilename);
01979 
01980   static bool remove(SdBaseFile* dirFile, const char* path);
01981   bool remove();
01983   void rewind() {seekSet(0);}
01984   bool rename(SdBaseFile* dirFile, const char* newPath);
01985   bool rmdir();
01986   // for backward compatibility
01987   bool rmDir() {return rmdir();}
01988   bool rmRfStar();
01993   bool seekCur(int32_t offset) {
01994     return seekSet(curPosition_ + offset);
01995   }
02000   bool seekEnd(int32_t offset = 0) {return seekSet(fileSize_ + offset);}
02001   bool seekSet(uint32_t pos);
02002   bool sync();
02003   bool timestamp(SdBaseFile* file);
02004   bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
02005           uint8_t hour, uint8_t minute, uint8_t second);
02011   uint8_t type() const {return type_;}
02012   bool truncate(uint32_t size);
02014   SdVolume* volume() const {return vol_;}
02015   int write(const void* buf, size_t nbyte);
02016 //------------------------------------------------------------------------------
02017  public:
02018   // allow SdFat to set cwd_
02019   friend class SdFat;
02020   // global pointer to cwd dir
02021   static SdBaseFile* cwd_;
02022   // data time callback function
02023   static void (*dateTime_)(uint16_t* date, uint16_t* time);
02024   // bits defined in flags_
02025   // should be 0X0F
02026   static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
02027   // sync of directory entry required
02028   static uint8_t const F_FILE_DIR_DIRTY = 0X80;
02029 
02030   // private data
02031   uint8_t   flags_;         // See above for definition of flags_ bits
02032   uint8_t   fstate_;        // error and eof indicator
02033   uint8_t   type_;          // type of file see above for values
02034   uint8_t   dirIndex_;      // index of directory entry in dirBlock
02035   SdVolume* vol_;           // volume where file is located
02036   uint32_t  curCluster_;    // cluster for current file position
02037   uint32_t  curPosition_;   // current file position in bytes from beginning
02038   uint32_t  dirBlock_;      // block for this files directory entry
02039   uint32_t  fileSize_;      // file size in bytes
02040   uint32_t  firstCluster_;  // first cluster of file
02041   char *pathend;
02042 
02043 
02044 
02046   bool openParent(SdBaseFile* dir);
02047   // private functions
02048   bool addCluster();
02049   cache_t* addDirCluster();
02050   dir_t* cacheDirEntry(uint8_t action);
02051   int8_t lsPrintNext(uint8_t flags, uint8_t indent);
02052   static bool make83Name(const char* str, uint8_t* name, const char** ptr);
02053   bool mkdir(SdBaseFile* parent, const uint8_t *dname);
02054   bool open(SdBaseFile* dirFile, const uint8_t *dname, uint8_t oflag, bool bDir);
02055   bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
02056   dir_t* readDirCache();
02057   dir_t* readDirCacheSpecial();
02058   dir_t *getLongFilename(dir_t *dir, char *longFilename, int8_t cVFATNeeded, uint32_t *pwIndexPos);
02059   bool findSpace(dir_t *dir, int8_t cVFATNeeded, int8_t *pcVFATFound, uint32_t *pwIndexPos);
02060   uint8_t lsRecursive(SdBaseFile *parent, uint8_t level, char *findFilename, SdBaseFile *pParentFound);
02061 
02062   bool setDirSize();
02063 //------------------------------------------------------------------------------
02064 // to be deleted
02065   static void printDirName(const dir_t& dir,
02066     uint8_t width, bool printSlash);
02067 //------------------------------------------------------------------------------
02068 // Deprecated functions  - suppress cpplint warnings with NOLINT comment
02069 #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
02070 
02071  public:
02078   bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) {  // NOLINT
02079     return contiguousRange(&bgnBlock, &endBlock);
02080   }
02089   bool createContiguous(SdBaseFile& dirFile,  // NOLINT
02090     const char* path, uint32_t size) {
02091     return createContiguous(&dirFile, path, size);
02092   }
02098   static void dateTimeCallback(
02099     void (*dateTime)(uint16_t& date, uint16_t& time)) {  // NOLINT
02100     oldDateTime_ = dateTime;
02101     dateTime_ = dateTime ? oldToNew : 0;
02102   }
02107   bool dirEntry(dir_t& dir) {return dirEntry(&dir);}  // NOLINT
02115   bool mkdir(SdBaseFile& dir, const char* path) {  // NOLINT
02116     return mkdir(&dir, path);
02117   }
02127   bool open(SdBaseFile& dirFile, // NOLINT
02128     const char* path, uint8_t oflag) {
02129     return open(&dirFile, path, oflag);
02130   }
02137   bool open(SdBaseFile& dirFile, const char* path) {  // NOLINT
02138     return open(dirFile, path, O_RDWR);
02139   }
02149   bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) {  // NOLINT
02150     return open(&dirFile, index, oflag);
02151   }
02156   bool openRoot(SdVolume& vol) {return openRoot(&vol);}  // NOLINT
02161   int8_t readDir(dir_t& dir) {return readDir(&dir);}  // NOLINT
02168   static bool remove(SdBaseFile& dirFile, const char* path) {  // NOLINT
02169     return remove(&dirFile, path);
02170   }
02171 //------------------------------------------------------------------------------
02172 // rest are private
02173  private:
02174   static void (*oldDateTime_)(uint16_t& date, uint16_t& time);  // NOLINT
02175   static void oldToNew(uint16_t* date, uint16_t* time) {
02176     uint16_t d;
02177     uint16_t t;
02178     oldDateTime_(d, t);
02179     *date = d;
02180     *time = t;
02181   }
02182 #elif !defined(DOXYGEN)  // ALLOW_DEPRECATED_FUNCTIONS
02183  public:
02184   bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock)  // NOLINT
02185     __attribute__((error("use contiguousRange(&bgnBlock, &endBlock)")));
02186   bool createContiguous(SdBaseFile& dirFile,  // NOLINT
02187     const char* path, uint32_t size)
02188     __attribute__((error("use createContiguous(&bgnBlock, &endBlock)")));
02189   static void dateTimeCallback(  // NOLINT
02190     void (*dateTime)(uint16_t& date, uint16_t& time))  // NOLINT
02191     __attribute__((error("use void dateTimeCallback("
02192      "void (*dateTime)(uint16_t* date, uint16_t* time))")));
02193   bool dirEntry(dir_t& dir)  // NOLINT
02194     __attribute__((error("use dirEntry(&dir)")));
02195   bool mkdir(SdBaseFile& dir, const char* path)  // NOLINT
02196     __attribute__((error("use mkdir(&dir, path)")));
02197   bool open(SdBaseFile& dirFile, // NOLINT
02198     const char* path, uint8_t oflag)
02199     __attribute__((error("use open(&dirFile, path, oflag)")));
02200   bool open(SdBaseFile& dirFile, const char* path)  // NOLINT
02201     __attribute__((error("use open(&dirFile, path, O_RDWR)")));
02202   bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) // NOLINT
02203     __attribute__((error("use open(&dirFile, index, oflag)")));
02204   bool openRoot(SdVolume& vol)  // NOLINT
02205     __attribute__((error("use openRoot(&vol)")));
02206   int8_t readDir(dir_t& dir)  // NOLINT
02207     __attribute__((error("use readDir(&dir)")));
02208   static bool remove(SdBaseFile& dirFile, const char* path)  // NOLINT
02209     __attribute__((error("use remove(&dirFile, path)")));
02210 #endif  // ALLOW_DEPRECATED_FUNCTIONS
02211 };
02212 //------------------------------------------------------------------------------
02217 class SdFile : public SdBaseFile {
02218  public:
02219   SdFile() {}
02220   SdFile(const char* name, uint8_t oflag);
02221 #if DESTRUCTOR_CLOSES_FILE
02222   ~SdFile() {}
02223 #endif  // DESTRUCTOR_CLOSES_FILE
02224 
02225   bool getWriteError() {return SdBaseFile::getWriteError();}
02227   void clearWriteError() {SdBaseFile::clearWriteError();}
02228 #ifdef COMPAT_PRE1
02229   void write(uint8_t b);
02230 #else
02231   size_t write(uint8_t b);
02232 #endif
02233   int write(const char* str);
02234   int write(const void* buf, size_t nbyte);
02235   void write_P(FSTRINGPARAM(str));
02236   void writeln_P(FSTRINGPARAM(str));
02237 };
02239 #define PgmPrint(x) SerialPrint_P(PSTR(x))
02240 
02241 #define PgmPrintln(x) SerialPrintln_P(PSTR(x))
02242 
02243 namespace SdFatUtil {
02244   int FreeRam();
02245   void SerialPrint_P(FSTRINGPARAM(str));
02246   void SerialPrintln_P(FSTRINGPARAM(str));
02247 }
02248 
02249 using namespace SdFatUtil;  // NOLINT
02250 
02251 //#include <SdStream.h>
02252 //#include <ArduinoStream.h>
02253 //------------------------------------------------------------------------------
02258 class SdFat {
02259  public:
02260   SdFat() {}
02261 #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
02262 
02274   bool init(uint8_t sckRateID = SPI_FULL_SPEED,
02275     uint8_t chipSelectPin = SD_CHIP_SELECT_PIN) {
02276     return begin(chipSelectPin, sckRateID);
02277   }
02278 #elif  !defined(DOXYGEN)  // ALLOW_DEPRECATED_FUNCTIONS
02279   bool init() __attribute__((error("use sd.begin()")));
02280   bool init(uint8_t sckRateID)
02281     __attribute__((error("use sd.begin(chipSelect, sckRate)")));
02282   bool init(uint8_t sckRateID, uint8_t chipSelectPin)
02283     __attribute__((error("use sd.begin(chipSelect, sckRate)")));
02284 #endif  // ALLOW_DEPRECATED_FUNCTIONS
02285 
02286   Sd2Card* card() {return &card_;}
02287   bool chdir(bool set_cwd = false);
02288   bool chdir(const char* path, bool set_cwd = false);
02289   void chvol();
02290   void errorHalt();
02291   void errorHalt_P(FSTRINGPARAM(msg));
02292   void errorHalt(char const *msg);
02293   void errorPrint();
02294   void errorPrint_P(FSTRINGPARAM(msg));
02295   void errorPrint(char const *msg);
02296   bool exists(const char* name);
02297   bool begin(uint8_t chipSelectPin = SD_CHIP_SELECT_PIN,
02298     uint8_t sckRateID = SPI_FULL_SPEED);
02299   void initErrorHalt();
02300   void initErrorHalt(char const *msg);
02301   void initErrorHalt_P(FSTRINGPARAM(msg));
02302   void initErrorPrint();
02303   void initErrorPrint(char const *msg);
02304   void initErrorPrint_P(FSTRINGPARAM(msg));
02305   void ls(uint8_t flags = 0);
02306   bool mkdir(const char* path, bool pFlag = true);
02307   bool remove(const char* path);
02308   bool rename(const char *oldPath, const char *newPath);
02309   bool rmdir(const char* path);
02310   bool truncate(const char* path, uint32_t length);
02312   SdVolume* vol() {return &vol_;}
02314   SdBaseFile* vwd() {return &vwd_;}
02315   //----------------------------------------------------------------------------
02316   // static functions for stdOut
02317 
02318  private:
02319   Sd2Card card_;
02320   SdVolume vol_;
02321   SdBaseFile vwd_;
02322 };
02323 #endif  // SdFat_h
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Defines