utility
Embed:
(wiki syntax)
Show/hide line numbers
Sd2Card.h
Go to the documentation of this file.
00001 /* Arduino Sd2Card Library 00002 * Copyright (C) 2009 by William Greiman 00003 * 00004 * This file is part of the Arduino Sd2Card 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 Sd2Card Library. If not, see 00018 * <http://www.gnu.org/licenses/>. 00019 */ 00020 #ifndef Sd2Card_h 00021 #define Sd2Card_h 00022 /** 00023 * \file 00024 * Sd2Card class 00025 */ 00026 #include "Sd2PinMap.h" 00027 #include "SdInfo.h" 00028 /** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */ 00029 uint8_t const SPI_FULL_SPEED = 0; 00030 /** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */ 00031 uint8_t const SPI_HALF_SPEED = 1; 00032 /** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */ 00033 uint8_t const SPI_QUARTER_SPEED = 2; 00034 /** 00035 * Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos. 00036 * Pins used are SS 10, MOSI 11, MISO 12, and SCK 13. 00037 * 00038 * MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used 00039 * on Mega Arduinos. Software SPI works well with GPS Shield V1.1 00040 * but many SD cards will fail with GPS Shield V1.0. 00041 */ 00042 #define MEGA_SOFT_SPI 0 00043 //------------------------------------------------------------------------------ 00044 #if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__)) 00045 #define SOFTWARE_SPI 00046 #endif // MEGA_SOFT_SPI 00047 //------------------------------------------------------------------------------ 00048 // SPI pin definitions 00049 // 00050 #ifndef SOFTWARE_SPI 00051 // hardware pin defs 00052 /** 00053 * SD Chip Select pin 00054 * 00055 * Warning if this pin is redefined the hardware SS will pin will be enabled 00056 * as an output by init(). An avr processor will not function as an SPI 00057 * master unless SS is set to output mode. 00058 */ 00059 /** The default chip select pin for the SD card is SS. */ 00060 uint8_t const SD_CHIP_SELECT_PIN = SS_PIN; 00061 // The following three pins must not be redefined for hardware SPI. 00062 /** SPI Master Out Slave In pin */ 00063 uint8_t const SPI_MOSI_PIN = MOSI_PIN; 00064 /** SPI Master In Slave Out pin */ 00065 uint8_t const SPI_MISO_PIN = MISO_PIN; 00066 /** SPI Clock pin */ 00067 uint8_t const SPI_SCK_PIN = SCK_PIN; 00068 /** optimize loops for hardware SPI */ 00069 #define OPTIMIZE_HARDWARE_SPI 00070 00071 #else // SOFTWARE_SPI 00072 // define software SPI pins so Mega can use unmodified GPS Shield 00073 /** SPI chip select pin */ 00074 uint8_t const SD_CHIP_SELECT_PIN = 10; 00075 /** SPI Master Out Slave In pin */ 00076 uint8_t const SPI_MOSI_PIN = 11; 00077 /** SPI Master In Slave Out pin */ 00078 uint8_t const SPI_MISO_PIN = 12; 00079 /** SPI Clock pin */ 00080 uint8_t const SPI_SCK_PIN = 13; 00081 #endif // SOFTWARE_SPI 00082 //------------------------------------------------------------------------------ 00083 /** Protect block zero from write if nonzero */ 00084 #define SD_PROTECT_BLOCK_ZERO 1 00085 /** init timeout ms */ 00086 uint16_t const SD_INIT_TIMEOUT = 2000; 00087 /** erase timeout ms */ 00088 uint16_t const SD_ERASE_TIMEOUT = 10000; 00089 /** read timeout ms */ 00090 uint16_t const SD_READ_TIMEOUT = 300; 00091 /** write time out ms */ 00092 uint16_t const SD_WRITE_TIMEOUT = 600; 00093 //------------------------------------------------------------------------------ 00094 // SD card errors 00095 /** timeout error for command CMD0 */ 00096 uint8_t const SD_CARD_ERROR_CMD0 = 0X1; 00097 /** CMD8 was not accepted - not a valid SD card*/ 00098 uint8_t const SD_CARD_ERROR_CMD8 = 0X2; 00099 /** card returned an error response for CMD17 (read block) */ 00100 uint8_t const SD_CARD_ERROR_CMD17 = 0X3; 00101 /** card returned an error response for CMD24 (write block) */ 00102 uint8_t const SD_CARD_ERROR_CMD24 = 0X4; 00103 /** WRITE_MULTIPLE_BLOCKS command failed */ 00104 uint8_t const SD_CARD_ERROR_CMD25 = 0X05; 00105 /** card returned an error response for CMD58 (read OCR) */ 00106 uint8_t const SD_CARD_ERROR_CMD58 = 0X06; 00107 /** SET_WR_BLK_ERASE_COUNT failed */ 00108 uint8_t const SD_CARD_ERROR_ACMD23 = 0X07; 00109 /** card's ACMD41 initialization process timeout */ 00110 uint8_t const SD_CARD_ERROR_ACMD41 = 0X08; 00111 /** card returned a bad CSR version field */ 00112 uint8_t const SD_CARD_ERROR_BAD_CSD = 0X09; 00113 /** erase block group command failed */ 00114 uint8_t const SD_CARD_ERROR_ERASE = 0X0A; 00115 /** card not capable of single block erase */ 00116 uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0X0B; 00117 /** Erase sequence timed out */ 00118 uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0X0C; 00119 /** card returned an error token instead of read data */ 00120 uint8_t const SD_CARD_ERROR_READ = 0X0D; 00121 /** read CID or CSD failed */ 00122 uint8_t const SD_CARD_ERROR_READ_REG = 0X0E; 00123 /** timeout while waiting for start of read data */ 00124 uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X0F; 00125 /** card did not accept STOP_TRAN_TOKEN */ 00126 uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X10; 00127 /** card returned an error token as a response to a write operation */ 00128 uint8_t const SD_CARD_ERROR_WRITE = 0X11; 00129 /** attempt to write protected block zero */ 00130 uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X12; 00131 /** card did not go ready for a multiple block write */ 00132 uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X13; 00133 /** card returned an error to a CMD13 status check after a write */ 00134 uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14; 00135 /** timeout occurred during write programming */ 00136 uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15; 00137 /** incorrect rate selected */ 00138 uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16; 00139 //------------------------------------------------------------------------------ 00140 // card types 00141 /** Standard capacity V1 SD card */ 00142 uint8_t const SD_CARD_TYPE_SD1 = 1; 00143 /** Standard capacity V2 SD card */ 00144 uint8_t const SD_CARD_TYPE_SD2 = 2; 00145 /** High Capacity SD card */ 00146 uint8_t const SD_CARD_TYPE_SDHC = 3; 00147 //------------------------------------------------------------------------------ 00148 /** 00149 * \class Sd2Card 00150 * \brief Raw access to SD and SDHC flash memory cards. 00151 */ 00152 class Sd2Card { 00153 public: 00154 /** Construct an instance of Sd2Card. */ 00155 Sd2Card(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {} 00156 uint32_t cardSize(void); 00157 uint8_t erase(uint32_t firstBlock, uint32_t lastBlock); 00158 uint8_t eraseSingleBlockEnable(void); 00159 /** 00160 * \return error code for last error. See Sd2Card.h for a list of error codes. 00161 */ 00162 uint8_t errorCode (void) const {return errorCode_;} 00163 /** \return error data for last error. */ 00164 uint8_t errorData (void) const {return status_;} 00165 /** 00166 * Initialize an SD flash memory card with default clock rate and chip 00167 * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin). 00168 */ 00169 uint8_t init(void) { 00170 return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN); 00171 } 00172 /** 00173 * Initialize an SD flash memory card with the selected SPI clock rate 00174 * and the default SD chip select pin. 00175 * See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin). 00176 */ 00177 uint8_t init(uint8_t sckRateID) { 00178 return init(sckRateID, SD_CHIP_SELECT_PIN); 00179 } 00180 uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin); 00181 void partialBlockRead(uint8_t value); 00182 /** Returns the current value, true or false, for partial block read. */ 00183 uint8_t partialBlockRead(void) const {return partialBlockRead_;} 00184 uint8_t readBlock(uint32_t block, uint8_t* dst); 00185 uint8_t readData(uint32_t block, 00186 uint16_t offset, uint16_t count, uint8_t* dst); 00187 /** 00188 * Read a cards CID register. The CID contains card identification 00189 * information such as Manufacturer ID, Product name, Product serial 00190 * number and Manufacturing date. */ 00191 uint8_t readCID(cid_t* cid) { 00192 return readRegister(CMD10, cid); 00193 } 00194 /** 00195 * Read a cards CSD register. The CSD contains Card-Specific Data that 00196 * provides information regarding access to the card's contents. */ 00197 uint8_t readCSD(csd_t* csd) { 00198 return readRegister(CMD9, csd); 00199 } 00200 void readEnd(void); 00201 uint8_t setSckRate(uint8_t sckRateID); 00202 /** Return the card type: SD V1, SD V2 or SDHC */ 00203 uint8_t type(void) const {return type_;} 00204 uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src); 00205 uint8_t writeData(const uint8_t* src); 00206 uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount); 00207 uint8_t writeStop(void); 00208 private: 00209 uint32_t block_; 00210 uint8_t chipSelectPin_; 00211 uint8_t errorCode_; 00212 uint8_t inBlock_; 00213 uint16_t offset_; 00214 uint8_t partialBlockRead_; 00215 uint8_t status_; 00216 uint8_t type_; 00217 // private functions 00218 uint8_t cardAcmd(uint8_t cmd, uint32_t arg) { 00219 cardCommand(CMD55, 0); 00220 return cardCommand(cmd, arg); 00221 } 00222 uint8_t cardCommand(uint8_t cmd, uint32_t arg); 00223 void error(uint8_t code) {errorCode_ = code;} 00224 uint8_t readRegister(uint8_t cmd, void* buf); 00225 uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount); 00226 void chipSelectHigh(void); 00227 void chipSelectLow(void); 00228 void type(uint8_t value) {type_ = value;} 00229 uint8_t waitNotBusy(uint16_t timeoutMillis); 00230 uint8_t writeData(uint8_t token, const uint8_t* src); 00231 uint8_t waitStartBlock(void); 00232 }; 00233 #endif // Sd2Card_h
Generated on Thu Jul 14 2022 02:07:54 by
1.7.2