[136] | 1 | /* Arduino Sd2Card Library
|
---|
| 2 | * Copyright (C) 2009 by William Greiman
|
---|
| 3 | *
|
---|
| 4 | * This file is part of the Arduino Sd2Card 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 Sd2Card Library. If not, see
|
---|
| 18 | * <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #ifndef Sd2Card_h
|
---|
| 21 | #define Sd2Card_h
|
---|
| 22 | /**
|
---|
| 23 | * \file
|
---|
| 24 | * Sd2Card class
|
---|
| 25 | */
|
---|
| 26 | #include "Sd2PinMap.h"
|
---|
| 27 | #include "SdInfo.h"
|
---|
| 28 | /** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
|
---|
| 29 | uint8_t const SPI_FULL_SPEED = 0;
|
---|
| 30 | /** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
|
---|
| 31 | uint8_t const SPI_HALF_SPEED = 1;
|
---|
| 32 | /** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
|
---|
| 33 | uint8_t const SPI_QUARTER_SPEED = 2;
|
---|
| 34 | /**
|
---|
| 35 | * USE_SPI_LIB: if set, use the SPI library bundled with Arduino IDE, otherwise
|
---|
| 36 | * run with a standalone driver for AVR.
|
---|
| 37 | */
|
---|
| 38 | #define USE_SPI_LIB
|
---|
| 39 | /**
|
---|
| 40 | * Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
|
---|
| 41 | * Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
|
---|
| 42 | *
|
---|
| 43 | * MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
|
---|
| 44 | * on Mega Arduinos. Software SPI works well with GPS Shield V1.1
|
---|
| 45 | * but many SD cards will fail with GPS Shield V1.0.
|
---|
| 46 | */
|
---|
| 47 | #define MEGA_SOFT_SPI 0
|
---|
| 48 | //------------------------------------------------------------------------------
|
---|
| 49 | #if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
|
---|
| 50 | #define SOFTWARE_SPI
|
---|
| 51 | #endif // MEGA_SOFT_SPI
|
---|
| 52 | //------------------------------------------------------------------------------
|
---|
| 53 | // SPI pin definitions
|
---|
| 54 | //
|
---|
| 55 | #ifndef SOFTWARE_SPI
|
---|
| 56 | // hardware pin defs
|
---|
| 57 | /**
|
---|
| 58 | * SD Chip Select pin
|
---|
| 59 | *
|
---|
| 60 | * Warning if this pin is redefined the hardware SS will pin will be enabled
|
---|
| 61 | * as an output by init(). An avr processor will not function as an SPI
|
---|
| 62 | * master unless SS is set to output mode.
|
---|
| 63 | */
|
---|
| 64 | /** The default chip select pin for the SD card is SS. */
|
---|
| 65 | uint8_t const SD_CHIP_SELECT_PIN = SS_PIN;
|
---|
| 66 | // The following three pins must not be redefined for hardware SPI.
|
---|
| 67 | /** SPI Master Out Slave In pin */
|
---|
| 68 | uint8_t const SPI_MOSI_PIN = MOSI_PIN;
|
---|
| 69 | /** SPI Master In Slave Out pin */
|
---|
| 70 | uint8_t const SPI_MISO_PIN = MISO_PIN;
|
---|
| 71 | /** SPI Clock pin */
|
---|
| 72 | uint8_t const SPI_SCK_PIN = SCK_PIN;
|
---|
| 73 | /** optimize loops for hardware SPI */
|
---|
| 74 | #ifndef USE_SPI_LIB
|
---|
| 75 | #define OPTIMIZE_HARDWARE_SPI
|
---|
| 76 | #endif
|
---|
| 77 |
|
---|
| 78 | #else // SOFTWARE_SPI
|
---|
| 79 | // define software SPI pins so Mega can use unmodified GPS Shield
|
---|
| 80 | /** SPI chip select pin */
|
---|
| 81 | uint8_t const SD_CHIP_SELECT_PIN = 10;
|
---|
| 82 | /** SPI Master Out Slave In pin */
|
---|
| 83 | uint8_t const SPI_MOSI_PIN = 11;
|
---|
| 84 | /** SPI Master In Slave Out pin */
|
---|
| 85 | uint8_t const SPI_MISO_PIN = 12;
|
---|
| 86 | /** SPI Clock pin */
|
---|
| 87 | uint8_t const SPI_SCK_PIN = 13;
|
---|
| 88 | #endif // SOFTWARE_SPI
|
---|
| 89 | //------------------------------------------------------------------------------
|
---|
| 90 | /** Protect block zero from write if nonzero */
|
---|
| 91 | #define SD_PROTECT_BLOCK_ZERO 1
|
---|
| 92 | /** init timeout ms */
|
---|
| 93 | uint16_t const SD_INIT_TIMEOUT = 2000;
|
---|
| 94 | /** erase timeout ms */
|
---|
| 95 | uint16_t const SD_ERASE_TIMEOUT = 10000;
|
---|
| 96 | /** read timeout ms */
|
---|
| 97 | uint16_t const SD_READ_TIMEOUT = 300;
|
---|
| 98 | /** write time out ms */
|
---|
| 99 | uint16_t const SD_WRITE_TIMEOUT = 600;
|
---|
| 100 | //------------------------------------------------------------------------------
|
---|
| 101 | // SD card errors
|
---|
| 102 | /** timeout error for command CMD0 */
|
---|
| 103 | uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
|
---|
| 104 | /** CMD8 was not accepted - not a valid SD card*/
|
---|
| 105 | uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
|
---|
| 106 | /** card returned an error response for CMD17 (read block) */
|
---|
| 107 | uint8_t const SD_CARD_ERROR_CMD17 = 0X3;
|
---|
| 108 | /** card returned an error response for CMD24 (write block) */
|
---|
| 109 | uint8_t const SD_CARD_ERROR_CMD24 = 0X4;
|
---|
| 110 | /** WRITE_MULTIPLE_BLOCKS command failed */
|
---|
| 111 | uint8_t const SD_CARD_ERROR_CMD25 = 0X05;
|
---|
| 112 | /** card returned an error response for CMD58 (read OCR) */
|
---|
| 113 | uint8_t const SD_CARD_ERROR_CMD58 = 0X06;
|
---|
| 114 | /** SET_WR_BLK_ERASE_COUNT failed */
|
---|
| 115 | uint8_t const SD_CARD_ERROR_ACMD23 = 0X07;
|
---|
| 116 | /** card's ACMD41 initialization process timeout */
|
---|
| 117 | uint8_t const SD_CARD_ERROR_ACMD41 = 0X08;
|
---|
| 118 | /** card returned a bad CSR version field */
|
---|
| 119 | uint8_t const SD_CARD_ERROR_BAD_CSD = 0X09;
|
---|
| 120 | /** erase block group command failed */
|
---|
| 121 | uint8_t const SD_CARD_ERROR_ERASE = 0X0A;
|
---|
| 122 | /** card not capable of single block erase */
|
---|
| 123 | uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0X0B;
|
---|
| 124 | /** Erase sequence timed out */
|
---|
| 125 | uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0X0C;
|
---|
| 126 | /** card returned an error token instead of read data */
|
---|
| 127 | uint8_t const SD_CARD_ERROR_READ = 0X0D;
|
---|
| 128 | /** read CID or CSD failed */
|
---|
| 129 | uint8_t const SD_CARD_ERROR_READ_REG = 0X0E;
|
---|
| 130 | /** timeout while waiting for start of read data */
|
---|
| 131 | uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X0F;
|
---|
| 132 | /** card did not accept STOP_TRAN_TOKEN */
|
---|
| 133 | uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X10;
|
---|
| 134 | /** card returned an error token as a response to a write operation */
|
---|
| 135 | uint8_t const SD_CARD_ERROR_WRITE = 0X11;
|
---|
| 136 | /** attempt to write protected block zero */
|
---|
| 137 | uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X12;
|
---|
| 138 | /** card did not go ready for a multiple block write */
|
---|
| 139 | uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X13;
|
---|
| 140 | /** card returned an error to a CMD13 status check after a write */
|
---|
| 141 | uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
|
---|
| 142 | /** timeout occurred during write programming */
|
---|
| 143 | uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
|
---|
| 144 | /** incorrect rate selected */
|
---|
| 145 | uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
|
---|
| 146 | //------------------------------------------------------------------------------
|
---|
| 147 | // card types
|
---|
| 148 | /** Standard capacity V1 SD card */
|
---|
| 149 | uint8_t const SD_CARD_TYPE_SD1 = 1;
|
---|
| 150 | /** Standard capacity V2 SD card */
|
---|
| 151 | uint8_t const SD_CARD_TYPE_SD2 = 2;
|
---|
| 152 | /** High Capacity SD card */
|
---|
| 153 | uint8_t const SD_CARD_TYPE_SDHC = 3;
|
---|
| 154 | //------------------------------------------------------------------------------
|
---|
| 155 | /**
|
---|
| 156 | * \class Sd2Card
|
---|
| 157 | * \brief Raw access to SD and SDHC flash memory cards.
|
---|
| 158 | */
|
---|
| 159 | class Sd2Card {
|
---|
| 160 | public:
|
---|
| 161 | /** Construct an instance of Sd2Card. */
|
---|
| 162 | Sd2Card(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {}
|
---|
| 163 | uint32_t cardSize(void);
|
---|
| 164 | uint8_t erase(uint32_t firstBlock, uint32_t lastBlock);
|
---|
| 165 | uint8_t eraseSingleBlockEnable(void);
|
---|
| 166 | /**
|
---|
| 167 | * \return error code for last error. See Sd2Card.h for a list of error codes.
|
---|
| 168 | */
|
---|
| 169 | uint8_t errorCode(void) const {return errorCode_;}
|
---|
| 170 | /** \return error data for last error. */
|
---|
| 171 | uint8_t errorData(void) const {return status_;}
|
---|
| 172 | /**
|
---|
| 173 | * Initialize an SD flash memory card with default clock rate and chip
|
---|
| 174 | * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
|
---|
| 175 | */
|
---|
| 176 | uint8_t init(void) {
|
---|
| 177 | return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN);
|
---|
| 178 | }
|
---|
| 179 | /**
|
---|
| 180 | * Initialize an SD flash memory card with the selected SPI clock rate
|
---|
| 181 | * and the default SD chip select pin.
|
---|
| 182 | * See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
|
---|
| 183 | */
|
---|
| 184 | uint8_t init(uint8_t sckRateID) {
|
---|
| 185 | return init(sckRateID, SD_CHIP_SELECT_PIN);
|
---|
| 186 | }
|
---|
| 187 | uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
|
---|
| 188 | void partialBlockRead(uint8_t value);
|
---|
| 189 | /** Returns the current value, true or false, for partial block read. */
|
---|
| 190 | uint8_t partialBlockRead(void) const {return partialBlockRead_;}
|
---|
| 191 | uint8_t readBlock(uint32_t block, uint8_t* dst);
|
---|
| 192 | uint8_t readData(uint32_t block,
|
---|
| 193 | uint16_t offset, uint16_t count, uint8_t* dst);
|
---|
| 194 | /**
|
---|
| 195 | * Read a cards CID register. The CID contains card identification
|
---|
| 196 | * information such as Manufacturer ID, Product name, Product serial
|
---|
| 197 | * number and Manufacturing date. */
|
---|
| 198 | uint8_t readCID(cid_t* cid) {
|
---|
| 199 | return readRegister(CMD10, cid);
|
---|
| 200 | }
|
---|
| 201 | /**
|
---|
| 202 | * Read a cards CSD register. The CSD contains Card-Specific Data that
|
---|
| 203 | * provides information regarding access to the card's contents. */
|
---|
| 204 | uint8_t readCSD(csd_t* csd) {
|
---|
| 205 | return readRegister(CMD9, csd);
|
---|
| 206 | }
|
---|
| 207 | void readEnd(void);
|
---|
| 208 | uint8_t setSckRate(uint8_t sckRateID);
|
---|
| 209 | /** Return the card type: SD V1, SD V2 or SDHC */
|
---|
| 210 | uint8_t type(void) const {return type_;}
|
---|
| 211 | uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src);
|
---|
| 212 | uint8_t writeData(const uint8_t* src);
|
---|
| 213 | uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount);
|
---|
| 214 | uint8_t writeStop(void);
|
---|
| 215 | private:
|
---|
| 216 | uint32_t block_;
|
---|
| 217 | uint8_t chipSelectPin_;
|
---|
| 218 | uint8_t errorCode_;
|
---|
| 219 | uint8_t inBlock_;
|
---|
| 220 | uint16_t offset_;
|
---|
| 221 | uint8_t partialBlockRead_;
|
---|
| 222 | uint8_t status_;
|
---|
| 223 | uint8_t type_;
|
---|
| 224 | // private functions
|
---|
| 225 | uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
|
---|
| 226 | cardCommand(CMD55, 0);
|
---|
| 227 | return cardCommand(cmd, arg);
|
---|
| 228 | }
|
---|
| 229 | uint8_t cardCommand(uint8_t cmd, uint32_t arg);
|
---|
| 230 | void error(uint8_t code) {errorCode_ = code;}
|
---|
| 231 | uint8_t readRegister(uint8_t cmd, void* buf);
|
---|
| 232 | uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
|
---|
| 233 | void chipSelectHigh(void);
|
---|
| 234 | void chipSelectLow(void);
|
---|
| 235 | void type(uint8_t value) {type_ = value;}
|
---|
| 236 | uint8_t waitNotBusy(uint16_t timeoutMillis);
|
---|
| 237 | uint8_t writeData(uint8_t token, const uint8_t* src);
|
---|
| 238 | uint8_t waitStartBlock(void);
|
---|
| 239 | };
|
---|
| 240 | #endif // Sd2Card_h
|
---|