2011-11-05 09:19:57 -04:00
|
|
|
/* Arduino Sd2Card Library
|
|
|
|
* Copyright (C) 2009 by William Greiman
|
|
|
|
*
|
|
|
|
* This file is part of the Arduino Sd2Card Library
|
|
|
|
*
|
|
|
|
* This Library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This Library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with the Arduino Sd2Card Library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2011-12-26 03:28:51 -05:00
|
|
|
|
2015-01-15 18:52:10 -05:00
|
|
|
#include "Marlin.h"
|
2015-07-31 01:21:18 -04:00
|
|
|
#if ENABLED(SDSUPPORT)
|
2015-01-15 18:52:10 -05:00
|
|
|
|
2011-11-05 09:19:57 -04:00
|
|
|
#ifndef Sd2Card_h
|
|
|
|
#define Sd2Card_h
|
|
|
|
/**
|
|
|
|
* \file
|
2011-11-18 16:17:37 -05:00
|
|
|
* \brief Sd2Card class for V2 SD/SDHC cards
|
2011-11-05 09:19:57 -04:00
|
|
|
*/
|
2011-11-18 16:17:37 -05:00
|
|
|
#include "SdFatConfig.h"
|
2011-11-05 09:19:57 -04:00
|
|
|
#include "Sd2PinMap.h"
|
|
|
|
#include "SdInfo.h"
|
2011-11-18 16:17:37 -05:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6
|
2011-11-05 09:19:57 -04:00
|
|
|
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
|
|
|
|
uint8_t const SPI_FULL_SPEED = 0;
|
|
|
|
/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
|
|
|
|
uint8_t const SPI_HALF_SPEED = 1;
|
2011-11-18 16:17:37 -05:00
|
|
|
/** Set SCK rate to F_CPU/8. See Sd2Card::setSckRate(). */
|
2011-11-05 09:19:57 -04:00
|
|
|
uint8_t const SPI_QUARTER_SPEED = 2;
|
2011-11-18 16:17:37 -05:00
|
|
|
/** Set SCK rate to F_CPU/16. See Sd2Card::setSckRate(). */
|
|
|
|
uint8_t const SPI_EIGHTH_SPEED = 3;
|
|
|
|
/** Set SCK rate to F_CPU/32. See Sd2Card::setSckRate(). */
|
|
|
|
uint8_t const SPI_SIXTEENTH_SPEED = 4;
|
2011-11-05 09:19:57 -04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** init timeout ms */
|
|
|
|
uint16_t const SD_INIT_TIMEOUT = 2000;
|
|
|
|
/** erase timeout ms */
|
|
|
|
uint16_t const SD_ERASE_TIMEOUT = 10000;
|
|
|
|
/** read timeout ms */
|
|
|
|
uint16_t const SD_READ_TIMEOUT = 300;
|
|
|
|
/** write time out ms */
|
|
|
|
uint16_t const SD_WRITE_TIMEOUT = 600;
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// SD card errors
|
2011-11-18 16:17:37 -05:00
|
|
|
/** timeout error for command CMD0 (initialize card in SPI mode) */
|
2011-11-05 09:19:57 -04:00
|
|
|
uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
|
|
|
|
/** CMD8 was not accepted - not a valid SD card*/
|
|
|
|
uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
|
2011-11-18 16:17:37 -05:00
|
|
|
/** card returned an error response for CMD12 (write stop) */
|
|
|
|
uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card returned an error response for CMD17 (read block) */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
|
|
|
|
/** card returned an error response for CMD18 (read multiple block) */
|
|
|
|
uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card returned an error response for CMD24 (write block) */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** WRITE_MULTIPLE_BLOCKS command failed */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card returned an error response for CMD58 (read OCR) */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** SET_WR_BLK_ERASE_COUNT failed */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
|
|
|
|
/** ACMD41 initialization process timeout */
|
|
|
|
uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card returned a bad CSR version field */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** erase block group command failed */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_ERASE = 0XC;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card not capable of single block erase */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** Erase sequence timed out */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card returned an error token instead of read data */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_READ = 0XF;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** read CID or CSD failed */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** timeout while waiting for start of read data */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card did not accept STOP_TRAN_TOKEN */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card returned an error token as a response to a write operation */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_WRITE = 0X13;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** attempt to write protected block zero */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card did not go ready for a multiple block write */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** card returned an error to a CMD13 status check after a write */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** timeout occurred during write programming */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** incorrect rate selected */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
|
|
|
|
/** init() not called */
|
|
|
|
uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
|
2014-05-01 06:03:41 -04:00
|
|
|
/** crc check error */
|
|
|
|
uint8_t const SD_CARD_ERROR_CRC = 0X20;
|
2011-11-05 09:19:57 -04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// card types
|
|
|
|
/** Standard capacity V1 SD card */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_TYPE_SD1 = 1;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** Standard capacity V2 SD card */
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t const SD_CARD_TYPE_SD2 = 2;
|
2011-11-05 09:19:57 -04:00
|
|
|
/** High Capacity SD card */
|
|
|
|
uint8_t const SD_CARD_TYPE_SDHC = 3;
|
2011-11-18 16:17:37 -05:00
|
|
|
/**
|
|
|
|
* define SOFTWARE_SPI to use bit-bang SPI
|
|
|
|
*/
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
#if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
|
2015-10-03 02:08:58 -04:00
|
|
|
#define SOFTWARE_SPI
|
2011-11-18 16:17:37 -05:00
|
|
|
#elif USE_SOFTWARE_SPI
|
2015-10-03 02:08:58 -04:00
|
|
|
#define SOFTWARE_SPI
|
2011-11-18 16:17:37 -05:00
|
|
|
#endif // MEGA_SOFT_SPI
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// SPI pin definitions - do not edit here - change in SdFatConfig.h
|
|
|
|
//
|
2015-07-31 01:21:18 -04:00
|
|
|
#if DISABLED(SOFTWARE_SPI)
|
2015-10-03 02:08:58 -04:00
|
|
|
// hardware pin defs
|
|
|
|
/** The default chip select pin for the SD card is SS. */
|
|
|
|
uint8_t const SD_CHIP_SELECT_PIN = SS_PIN;
|
|
|
|
// The following three pins must not be redefined for hardware SPI.
|
|
|
|
/** SPI Master Out Slave In pin */
|
|
|
|
uint8_t const SPI_MOSI_PIN = MOSI_PIN;
|
|
|
|
/** SPI Master In Slave Out pin */
|
|
|
|
uint8_t const SPI_MISO_PIN = MISO_PIN;
|
|
|
|
/** SPI Clock pin */
|
|
|
|
uint8_t const SPI_SCK_PIN = SCK_PIN;
|
2011-11-18 16:17:37 -05:00
|
|
|
|
|
|
|
#else // SOFTWARE_SPI
|
|
|
|
|
2015-10-03 02:08:58 -04:00
|
|
|
/** SPI chip select pin */
|
|
|
|
uint8_t const SD_CHIP_SELECT_PIN = SOFT_SPI_CS_PIN;
|
|
|
|
/** SPI Master Out Slave In pin */
|
|
|
|
uint8_t const SPI_MOSI_PIN = SOFT_SPI_MOSI_PIN;
|
|
|
|
/** SPI Master In Slave Out pin */
|
|
|
|
uint8_t const SPI_MISO_PIN = SOFT_SPI_MISO_PIN;
|
|
|
|
/** SPI Clock pin */
|
|
|
|
uint8_t const SPI_SCK_PIN = SOFT_SPI_SCK_PIN;
|
2011-11-18 16:17:37 -05:00
|
|
|
#endif // SOFTWARE_SPI
|
2011-11-05 09:19:57 -04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* \class Sd2Card
|
|
|
|
* \brief Raw access to SD and SDHC flash memory cards.
|
|
|
|
*/
|
|
|
|
class Sd2Card {
|
|
|
|
public:
|
|
|
|
/** Construct an instance of Sd2Card. */
|
2011-11-18 16:17:37 -05:00
|
|
|
Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
|
|
|
|
uint32_t cardSize();
|
|
|
|
bool erase(uint32_t firstBlock, uint32_t lastBlock);
|
|
|
|
bool eraseSingleBlockEnable();
|
|
|
|
/**
|
|
|
|
* Set SD error code.
|
|
|
|
* \param[in] code value for error code.
|
|
|
|
*/
|
|
|
|
void error(uint8_t code) {errorCode_ = code;}
|
2011-11-05 09:19:57 -04:00
|
|
|
/**
|
|
|
|
* \return error code for last error. See Sd2Card.h for a list of error codes.
|
|
|
|
*/
|
2011-11-18 16:17:37 -05:00
|
|
|
int errorCode() const {return errorCode_;}
|
2011-11-05 09:19:57 -04:00
|
|
|
/** \return error data for last error. */
|
2011-11-18 16:17:37 -05:00
|
|
|
int errorData() const {return status_;}
|
2011-11-05 09:19:57 -04:00
|
|
|
/**
|
|
|
|
* Initialize an SD flash memory card with default clock rate and chip
|
|
|
|
* select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
|
2011-11-18 16:17:37 -05:00
|
|
|
*
|
|
|
|
* \return true for success or false for failure.
|
2011-11-05 09:19:57 -04:00
|
|
|
*/
|
2011-11-18 16:17:37 -05:00
|
|
|
bool init(uint8_t sckRateID = SPI_FULL_SPEED,
|
2015-10-03 02:08:58 -04:00
|
|
|
uint8_t chipSelectPin = SD_CHIP_SELECT_PIN);
|
2011-11-18 16:17:37 -05:00
|
|
|
bool readBlock(uint32_t block, uint8_t* dst);
|
2011-11-05 09:19:57 -04:00
|
|
|
/**
|
2011-11-18 16:17:37 -05:00
|
|
|
* Read a card's CID register. The CID contains card identification
|
2011-11-05 09:19:57 -04:00
|
|
|
* information such as Manufacturer ID, Product name, Product serial
|
2015-10-03 02:08:58 -04:00
|
|
|
* number and Manufacturing date.
|
2011-11-18 16:17:37 -05:00
|
|
|
*
|
|
|
|
* \param[out] cid pointer to area for returned data.
|
|
|
|
*
|
|
|
|
* \return true for success or false for failure.
|
|
|
|
*/
|
|
|
|
bool readCID(cid_t* cid) {
|
2011-11-05 09:19:57 -04:00
|
|
|
return readRegister(CMD10, cid);
|
|
|
|
}
|
|
|
|
/**
|
2011-11-18 16:17:37 -05:00
|
|
|
* Read a card's CSD register. The CSD contains Card-Specific Data that
|
|
|
|
* provides information regarding access to the card's contents.
|
|
|
|
*
|
|
|
|
* \param[out] csd pointer to area for returned data.
|
|
|
|
*
|
|
|
|
* \return true for success or false for failure.
|
|
|
|
*/
|
|
|
|
bool readCSD(csd_t* csd) {
|
2011-11-05 09:19:57 -04:00
|
|
|
return readRegister(CMD9, csd);
|
|
|
|
}
|
2015-10-03 02:08:58 -04:00
|
|
|
bool readData(uint8_t* dst);
|
2011-11-18 16:17:37 -05:00
|
|
|
bool readStart(uint32_t blockNumber);
|
|
|
|
bool readStop();
|
|
|
|
bool setSckRate(uint8_t sckRateID);
|
|
|
|
/** Return the card type: SD V1, SD V2 or SDHC
|
|
|
|
* \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
|
|
|
|
*/
|
|
|
|
int type() const {return type_;}
|
|
|
|
bool writeBlock(uint32_t blockNumber, const uint8_t* src);
|
|
|
|
bool writeData(const uint8_t* src);
|
|
|
|
bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
|
|
|
|
bool writeStop();
|
2011-11-05 09:19:57 -04:00
|
|
|
private:
|
2011-11-18 16:17:37 -05:00
|
|
|
//----------------------------------------------------------------------------
|
2011-11-05 09:19:57 -04:00
|
|
|
uint8_t chipSelectPin_;
|
|
|
|
uint8_t errorCode_;
|
2011-11-18 16:17:37 -05:00
|
|
|
uint8_t spiRate_;
|
2011-11-05 09:19:57 -04:00
|
|
|
uint8_t status_;
|
|
|
|
uint8_t type_;
|
|
|
|
// private functions
|
|
|
|
uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
|
|
|
|
cardCommand(CMD55, 0);
|
|
|
|
return cardCommand(cmd, arg);
|
|
|
|
}
|
|
|
|
uint8_t cardCommand(uint8_t cmd, uint32_t arg);
|
2011-11-18 16:17:37 -05:00
|
|
|
|
|
|
|
bool readData(uint8_t* dst, uint16_t count);
|
|
|
|
bool readRegister(uint8_t cmd, void* buf);
|
|
|
|
void chipSelectHigh();
|
|
|
|
void chipSelectLow();
|
2011-11-05 09:19:57 -04:00
|
|
|
void type(uint8_t value) {type_ = value;}
|
2011-11-18 16:17:37 -05:00
|
|
|
bool waitNotBusy(uint16_t timeoutMillis);
|
|
|
|
bool writeData(uint8_t token, const uint8_t* src);
|
2011-11-05 09:19:57 -04:00
|
|
|
};
|
|
|
|
#endif // Sd2Card_h
|
2015-01-15 18:52:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|