Prusa3d Marlin fork
Sd2Card.h
Go to the documentation of this file.
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 
21 #include "Marlin.h"
22 #ifdef SDSUPPORT
23 
24 #ifndef Sd2Card_h
25 #define Sd2Card_h
30 #include "SdFatConfig.h"
31 #include "SdInfo.h"
32 //------------------------------------------------------------------------------
33 // SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6
35 uint8_t const SPI_FULL_SPEED = 0;
37 uint8_t const SPI_HALF_SPEED = 1;
39 uint8_t const SPI_QUARTER_SPEED = 2;
41 uint8_t const SPI_EIGHTH_SPEED = 3;
43 uint8_t const SPI_SIXTEENTH_SPEED = 4;
44 //------------------------------------------------------------------------------
46 uint16_t const SD_INIT_TIMEOUT = 2000;
48 uint16_t const SD_ERASE_TIMEOUT = 10000;
50 uint16_t const SD_READ_TIMEOUT = 300;
52 uint16_t const SD_WRITE_TIMEOUT = 600;
53 //------------------------------------------------------------------------------
54 // SD card errors
56 uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
58 uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
60 uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
62 uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
64 uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
66 uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
68 uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
70 uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
72 uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
74 uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
76 uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
78 uint8_t const SD_CARD_ERROR_ERASE = 0XC;
82 uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
84 uint8_t const SD_CARD_ERROR_READ = 0XF;
86 uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
88 uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
90 uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
92 uint8_t const SD_CARD_ERROR_WRITE = 0X13;
94 uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used
96 uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
98 uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
100 uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
102 uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
104 uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
106 uint8_t const SD_CARD_ERROR_CRC = 0X20;
108 uint8_t const SD_CARD_ERROR_FF_TIMEOUT = 0X21;
109 
111 uint8_t const SD_CARD_ERROR_CMD48 = 0x80;
113 uint8_t const SD_CARD_ERROR_CMD49 = 0x81;
114 
115 //------------------------------------------------------------------------------
116 // card types
118 uint8_t const SD_CARD_TYPE_SD1 = 1;
120 uint8_t const SD_CARD_TYPE_SD2 = 2;
122 uint8_t const SD_CARD_TYPE_SDHC = 3;
126 //------------------------------------------------------------------------------
127 #if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
128 #define SOFTWARE_SPI
129 #elif USE_SOFTWARE_SPI
130 #define SOFTWARE_SPI
131 #endif // MEGA_SOFT_SPI
132 //------------------------------------------------------------------------------
133 // SPI pin definitions - do not edit here - change in SdFatConfig.h
134 //
135 #ifdef SOFTWARE_SPI
142 #endif // SOFTWARE_SPI
143 //------------------------------------------------------------------------------
148 class Sd2Card {
149  public:
151  Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0), flash_air_compatible_(false) {}
152  uint32_t cardSize();
153  bool erase(uint32_t firstBlock, uint32_t lastBlock);
154  bool eraseSingleBlockEnable();
159  void error(uint8_t code) {errorCode_ = code;}
163  uint8_t errorCode() const {return errorCode_;}
165  uint8_t errorData() const {return status_;}
172  bool init(uint8_t sckRateID = SPI_FULL_SPEED);
173  bool readBlock(uint32_t block, uint8_t* dst);
183  bool readCID(cid_t* cid) {
184  return readRegister(CMD10, cid);
185  }
194  bool readCSD(csd_t* csd) {
195  return readRegister(CMD9, csd);
196  }
197  bool readData(uint8_t *dst);
198  bool readStart(uint32_t blockNumber);
199  bool readStop();
200  bool setSckRate(uint8_t sckRateID);
204  int type() const {return type_;}
205  bool writeBlock(uint32_t blockNumber, const uint8_t* src);
206  bool writeData(const uint8_t* src);
207  bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
208  bool writeStop();
209 
210  // Toshiba FlashAir support
211  uint8_t readExtMemory(uint8_t mio, uint8_t func, uint32_t addr, uint16_t count, uint8_t* dst);
212 
213  void setFlashAirCompatible(bool flashAirCompatible) { flash_air_compatible_ = flashAirCompatible; }
214  bool getFlashAirCompatible() const { return flash_air_compatible_; }
215 
216  private:
217  //----------------------------------------------------------------------------
218  uint8_t errorCode_;
219  uint8_t spiRate_;
220  uint8_t status_;
221  uint8_t type_;
222  bool flash_air_compatible_;
223  // private functions
224  uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
225  cardCommand(CMD55, 0);
226  return cardCommand(cmd, arg);
227  }
228  uint8_t cardCommand(uint8_t cmd, uint32_t arg);
229 
230  bool readData(uint8_t* dst, uint16_t count);
231  bool readRegister(uint8_t cmd, void* buf);
232  void chipSelectHigh();
233  void chipSelectLow();
234  void type(uint8_t value) {type_ = value;}
235  bool waitNotBusy(uint16_t timeoutMillis);
236  bool writeData(uint8_t token, const uint8_t* src);
237 
238 
239  // Toshiba FlashAir support
240  uint8_t waitStartBlock(void);
241  uint8_t readExt(uint32_t arg, uint8_t* dst, uint16_t count);
242 };
243 #endif // Sd2Card_h
244 
245 
246 #endif
uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK
Definition: Sd2Card.h:80
uint16_t const SD_READ_TIMEOUT
Definition: Sd2Card.h:50
uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING
Definition: Sd2Card.h:98
uint8_t const SD_CARD_ERROR_BAD_CSD
Definition: Sd2Card.h:76
uint8_t const SD_CARD_ERROR_CMD58
Definition: Sd2Card.h:70
uint8_t const SD_CARD_ERROR_CMD18
Definition: Sd2Card.h:64
uint8_t const SD_CARD_TYPE_SD2
Definition: Sd2Card.h:120
uint8_t const SD_CARD_ERROR_CMD8
Definition: Sd2Card.h:58
uint8_t const SD_CARD_ERROR_CMD17
Definition: Sd2Card.h:62
uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE
Definition: Sd2Card.h:96
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO
Definition: Sd2Card.h:94
uint8_t const SD_CARD_ERROR_CMD0
Definition: Sd2Card.h:56
uint8_t const SD_CARD_TYPE_SDHC
Definition: Sd2Card.h:122
uint8_t const SD_CARD_ERROR_CMD49
Definition: Sd2Card.h:113
uint8_t const SD_CARD_ERROR_CMD25
Definition: Sd2Card.h:68
uint8_t const SD_CARD_ERROR_READ_TIMEOUT
Definition: Sd2Card.h:88
uint8_t const SD_CARD_ERROR_ACMD41
Definition: Sd2Card.h:74
uint8_t const SD_CARD_ERROR_FF_TIMEOUT
Definition: Sd2Card.h:108
uint8_t const SPI_FULL_SPEED
Definition: Sd2Card.h:35
uint8_t const SD_CARD_ERROR_ERASE
Definition: Sd2Card.h:78
uint8_t const SPI_SIXTEENTH_SPEED
Definition: Sd2Card.h:43
uint8_t const SPI_SCK_PIN
Definition: Sd2Card.h:141
uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT
Definition: Sd2Card.h:82
uint8_t const SD_CARD_ERROR_STOP_TRAN
Definition: Sd2Card.h:90
uint8_t const SD_CARD_ERROR_WRITE
Definition: Sd2Card.h:92
uint16_t const SD_WRITE_TIMEOUT
Definition: Sd2Card.h:52
uint8_t const SD_CARD_ERROR_SCK_RATE
Definition: Sd2Card.h:102
uint8_t const SD_CARD_ERROR_ACMD23
Definition: Sd2Card.h:72
uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED
Definition: Sd2Card.h:104
uint16_t const SD_INIT_TIMEOUT
Definition: Sd2Card.h:46
uint8_t const SD_CARD_ERROR_CMD48
Definition: Sd2Card.h:111
uint8_t const SPI_MOSI_PIN
Definition: Sd2Card.h:137
uint8_t const SPI_HALF_SPEED
Definition: Sd2Card.h:37
uint8_t const SPI_EIGHTH_SPEED
Definition: Sd2Card.h:41
uint8_t const SPI_QUARTER_SPEED
Definition: Sd2Card.h:39
uint8_t const SD_CARD_ERROR_READ
Definition: Sd2Card.h:84
uint8_t const SD_CARD_ERROR_CMD24
Definition: Sd2Card.h:66
uint8_t const SPI_MISO_PIN
Definition: Sd2Card.h:139
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT
Definition: Sd2Card.h:100
uint8_t const SD_CARD_ERROR_CMD12
Definition: Sd2Card.h:60
uint8_t const SD_CARD_ERROR_READ_REG
Definition: Sd2Card.h:86
uint8_t const SD_CARD_TYPE_SD1
Definition: Sd2Card.h:118
uint8_t const SD_CARD_ERROR_CRC
Definition: Sd2Card.h:106
uint16_t const SD_ERASE_TIMEOUT
Definition: Sd2Card.h:48
configuration definitions
uint8_t const SOFT_SPI_MOSI_PIN
Definition: SdFatConfig.h:100
uint8_t const SOFT_SPI_SCK_PIN
Definition: SdFatConfig.h:104
uint8_t const SOFT_SPI_MISO_PIN
Definition: SdFatConfig.h:102
Raw access to SD and SDHC flash memory cards.
Definition: Sd2Card.h:148
bool writeStart(uint32_t blockNumber, uint32_t eraseCount)
Definition: Sd2Card.cpp:708
Sd2Card()
Definition: Sd2Card.h:151
bool readCSD(csd_t *csd)
Definition: Sd2Card.h:194
uint32_t cardSize()
Definition: Sd2Card.cpp:187
bool init(uint8_t sckRateID=SPI_FULL_SPEED)
Definition: Sd2Card.cpp:286
bool erase(uint32_t firstBlock, uint32_t lastBlock)
Definition: Sd2Card.cpp:231
bool readStart(uint32_t blockNumber)
Definition: Sd2Card.cpp:558
bool readBlock(uint32_t block, uint8_t *dst)
Definition: Sd2Card.cpp:393
bool readData(uint8_t *dst)
Definition: Sd2Card.cpp:438
bool writeStop()
Definition: Sd2Card.cpp:733
uint8_t readExtMemory(uint8_t mio, uint8_t func, uint32_t addr, uint16_t count, uint8_t *dst)
Definition: Sd2Card.cpp:818
int type() const
Definition: Sd2Card.h:204
uint8_t errorCode() const
Definition: Sd2Card.h:163
uint8_t errorData() const
Definition: Sd2Card.h:165
bool readCID(cid_t *cid)
Definition: Sd2Card.h:183
bool eraseSingleBlockEnable()
Definition: Sd2Card.cpp:271
bool writeBlock(uint32_t blockNumber, const uint8_t *src)
Definition: Sd2Card.cpp:632
bool setSckRate(uint8_t sckRateID)
Definition: Sd2Card.cpp:603
bool readStop()
Definition: Sd2Card.cpp:577
void error(uint8_t code)
Definition: Sd2Card.h:159
bool writeData(const uint8_t *src)
Definition: Sd2Card.cpp:664
Definition: SdInfo.h:100
Definition: SdInfo.h:280