Repetier-Firmware 0.2
Repetier/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 
00026 #include "Sd2PinMap.h"
00027 #include "SdInfo.h"
00029 uint8_t const SPI_FULL_SPEED = 0;
00031 uint8_t const SPI_HALF_SPEED = 1;
00033 uint8_t const SPI_QUARTER_SPEED = 2;
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
00060 uint8_t const  SD_CHIP_SELECT_PIN = SS_PIN;
00061 // The following three pins must not be redefined for hardware SPI.
00063 uint8_t const  SPI_MOSI_PIN = MOSI_PIN;
00065 uint8_t const  SPI_MISO_PIN = MISO_PIN;
00067 uint8_t const  SPI_SCK_PIN = SCK_PIN;
00069 #define OPTIMIZE_HARDWARE_SPI
00070 
00071 #else  // SOFTWARE_SPI
00072 // define software SPI pins so Mega can use unmodified GPS Shield
00074 uint8_t const SD_CHIP_SELECT_PIN = 10;
00076 uint8_t const SPI_MOSI_PIN = 11;
00078 uint8_t const SPI_MISO_PIN = 12;
00080 uint8_t const SPI_SCK_PIN = 13;
00081 #endif  // SOFTWARE_SPI
00082 //------------------------------------------------------------------------------
00084 #define SD_PROTECT_BLOCK_ZERO 1
00085 
00086 uint16_t const SD_INIT_TIMEOUT = 2000;
00088 uint16_t const SD_ERASE_TIMEOUT = 10000;
00090 uint16_t const SD_READ_TIMEOUT = 300;
00092 uint16_t const SD_WRITE_TIMEOUT = 600;
00093 //------------------------------------------------------------------------------
00094 // SD card errors
00096 uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
00098 uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
00100 uint8_t const SD_CARD_ERROR_CMD17 = 0X3;
00102 uint8_t const SD_CARD_ERROR_CMD24 = 0X4;
00104 uint8_t const SD_CARD_ERROR_CMD25 = 0X05;
00106 uint8_t const SD_CARD_ERROR_CMD58 = 0X06;
00108 uint8_t const SD_CARD_ERROR_ACMD23 = 0X07;
00110 uint8_t const SD_CARD_ERROR_ACMD41 = 0X08;
00112 uint8_t const SD_CARD_ERROR_BAD_CSD = 0X09;
00114 uint8_t const SD_CARD_ERROR_ERASE = 0X0A;
00116 uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0X0B;
00118 uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0X0C;
00120 uint8_t const SD_CARD_ERROR_READ = 0X0D;
00122 uint8_t const SD_CARD_ERROR_READ_REG = 0X0E;
00124 uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X0F;
00126 uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X10;
00128 uint8_t const SD_CARD_ERROR_WRITE = 0X11;
00130 uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X12;
00132 uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X13;
00134 uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
00136 uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
00138 uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
00139 //------------------------------------------------------------------------------
00140 // card types
00142 uint8_t const SD_CARD_TYPE_SD1 = 1;
00144 uint8_t const SD_CARD_TYPE_SD2 = 2;
00146 uint8_t const SD_CARD_TYPE_SDHC = 3;
00147 //------------------------------------------------------------------------------
00152 class Sd2Card {
00153  public:
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);
00162   uint8_t errorCode(void) const {return errorCode_;}
00164   uint8_t errorData(void) const {return status_;}
00169   uint8_t init(void) {
00170     return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN);
00171   }
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);
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);
00191   uint8_t readCID(cid_t* cid) {
00192     return readRegister(CMD10, cid);
00193   }
00197   uint8_t readCSD(csd_t* csd) {
00198     return readRegister(CMD9, csd);
00199   }
00200   void readEnd(void);
00201   uint8_t setSckRate(uint8_t sckRateID);
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
 All Data Structures Files Functions Variables Typedefs Friends Defines