Thu, 07 Jul 2016 12:23:34 +0200
added missing sanguino files
0 | 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 | |
26 | /** | |
27 | * \file | |
28 | * \brief Sd2Card class for V2 SD/SDHC cards | |
29 | */ | |
30 | #include "SdFatConfig.h" | |
31 | #include "Sd2PinMap.h" | |
32 | #include "SdInfo.h" | |
33 | //------------------------------------------------------------------------------ | |
34 | // SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6 | |
35 | /** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */ | |
36 | uint8_t const SPI_FULL_SPEED = 0; | |
37 | /** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */ | |
38 | uint8_t const SPI_HALF_SPEED = 1; | |
39 | /** Set SCK rate to F_CPU/8. See Sd2Card::setSckRate(). */ | |
40 | uint8_t const SPI_QUARTER_SPEED = 2; | |
41 | /** Set SCK rate to F_CPU/16. See Sd2Card::setSckRate(). */ | |
42 | uint8_t const SPI_EIGHTH_SPEED = 3; | |
43 | /** Set SCK rate to F_CPU/32. See Sd2Card::setSckRate(). */ | |
44 | uint8_t const SPI_SIXTEENTH_SPEED = 4; | |
45 | //------------------------------------------------------------------------------ | |
46 | /** init timeout ms */ | |
47 | uint16_t const SD_INIT_TIMEOUT = 2000; | |
48 | /** erase timeout ms */ | |
49 | uint16_t const SD_ERASE_TIMEOUT = 10000; | |
50 | /** read timeout ms */ | |
51 | uint16_t const SD_READ_TIMEOUT = 300; | |
52 | /** write time out ms */ | |
53 | uint16_t const SD_WRITE_TIMEOUT = 600; | |
54 | //------------------------------------------------------------------------------ | |
55 | // SD card errors | |
56 | /** timeout error for command CMD0 (initialize card in SPI mode) */ | |
57 | uint8_t const SD_CARD_ERROR_CMD0 = 0X1; | |
58 | /** CMD8 was not accepted - not a valid SD card*/ | |
59 | uint8_t const SD_CARD_ERROR_CMD8 = 0X2; | |
60 | /** card returned an error response for CMD12 (write stop) */ | |
61 | uint8_t const SD_CARD_ERROR_CMD12 = 0X3; | |
62 | /** card returned an error response for CMD17 (read block) */ | |
63 | uint8_t const SD_CARD_ERROR_CMD17 = 0X4; | |
64 | /** card returned an error response for CMD18 (read multiple block) */ | |
65 | uint8_t const SD_CARD_ERROR_CMD18 = 0X5; | |
66 | /** card returned an error response for CMD24 (write block) */ | |
67 | uint8_t const SD_CARD_ERROR_CMD24 = 0X6; | |
68 | /** WRITE_MULTIPLE_BLOCKS command failed */ | |
69 | uint8_t const SD_CARD_ERROR_CMD25 = 0X7; | |
70 | /** card returned an error response for CMD58 (read OCR) */ | |
71 | uint8_t const SD_CARD_ERROR_CMD58 = 0X8; | |
72 | /** SET_WR_BLK_ERASE_COUNT failed */ | |
73 | uint8_t const SD_CARD_ERROR_ACMD23 = 0X9; | |
74 | /** ACMD41 initialization process timeout */ | |
75 | uint8_t const SD_CARD_ERROR_ACMD41 = 0XA; | |
76 | /** card returned a bad CSR version field */ | |
77 | uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB; | |
78 | /** erase block group command failed */ | |
79 | uint8_t const SD_CARD_ERROR_ERASE = 0XC; | |
80 | /** card not capable of single block erase */ | |
81 | uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD; | |
82 | /** Erase sequence timed out */ | |
83 | uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE; | |
84 | /** card returned an error token instead of read data */ | |
85 | uint8_t const SD_CARD_ERROR_READ = 0XF; | |
86 | /** read CID or CSD failed */ | |
87 | uint8_t const SD_CARD_ERROR_READ_REG = 0X10; | |
88 | /** timeout while waiting for start of read data */ | |
89 | uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11; | |
90 | /** card did not accept STOP_TRAN_TOKEN */ | |
91 | uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12; | |
92 | /** card returned an error token as a response to a write operation */ | |
93 | uint8_t const SD_CARD_ERROR_WRITE = 0X13; | |
94 | /** attempt to write protected block zero */ | |
95 | uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used | |
96 | /** card did not go ready for a multiple block write */ | |
97 | uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15; | |
98 | /** card returned an error to a CMD13 status check after a write */ | |
99 | uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16; | |
100 | /** timeout occurred during write programming */ | |
101 | uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17; | |
102 | /** incorrect rate selected */ | |
103 | uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18; | |
104 | /** init() not called */ | |
105 | uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19; | |
106 | //------------------------------------------------------------------------------ | |
107 | // card types | |
108 | /** Standard capacity V1 SD card */ | |
109 | uint8_t const SD_CARD_TYPE_SD1 = 1; | |
110 | /** Standard capacity V2 SD card */ | |
111 | uint8_t const SD_CARD_TYPE_SD2 = 2; | |
112 | /** High Capacity SD card */ | |
113 | uint8_t const SD_CARD_TYPE_SDHC = 3; | |
114 | /** | |
115 | * define SOFTWARE_SPI to use bit-bang SPI | |
116 | */ | |
117 | //------------------------------------------------------------------------------ | |
118 | #if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__)) | |
119 | #define SOFTWARE_SPI | |
120 | #elif USE_SOFTWARE_SPI | |
121 | #define SOFTWARE_SPI | |
122 | #endif // MEGA_SOFT_SPI | |
123 | //------------------------------------------------------------------------------ | |
124 | // SPI pin definitions - do not edit here - change in SdFatConfig.h | |
125 | // | |
126 | #ifndef SOFTWARE_SPI | |
127 | // hardware pin defs | |
128 | /** The default chip select pin for the SD card is SS. */ | |
129 | uint8_t const SD_CHIP_SELECT_PIN = SS_PIN; | |
130 | // The following three pins must not be redefined for hardware SPI. | |
131 | /** SPI Master Out Slave In pin */ | |
132 | uint8_t const SPI_MOSI_PIN = MOSI_PIN; | |
133 | /** SPI Master In Slave Out pin */ | |
134 | uint8_t const SPI_MISO_PIN = MISO_PIN; | |
135 | /** SPI Clock pin */ | |
136 | uint8_t const SPI_SCK_PIN = SCK_PIN; | |
137 | ||
138 | #else // SOFTWARE_SPI | |
139 | ||
140 | /** SPI chip select pin */ | |
141 | uint8_t const SD_CHIP_SELECT_PIN = SOFT_SPI_CS_PIN; | |
142 | /** SPI Master Out Slave In pin */ | |
143 | uint8_t const SPI_MOSI_PIN = SOFT_SPI_MOSI_PIN; | |
144 | /** SPI Master In Slave Out pin */ | |
145 | uint8_t const SPI_MISO_PIN = SOFT_SPI_MISO_PIN; | |
146 | /** SPI Clock pin */ | |
147 | uint8_t const SPI_SCK_PIN = SOFT_SPI_SCK_PIN; | |
148 | #endif // SOFTWARE_SPI | |
149 | //------------------------------------------------------------------------------ | |
150 | /** | |
151 | * \class Sd2Card | |
152 | * \brief Raw access to SD and SDHC flash memory cards. | |
153 | */ | |
154 | class Sd2Card { | |
155 | public: | |
156 | /** Construct an instance of Sd2Card. */ | |
157 | Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {} | |
158 | uint32_t cardSize(); | |
159 | bool erase(uint32_t firstBlock, uint32_t lastBlock); | |
160 | bool eraseSingleBlockEnable(); | |
161 | /** | |
162 | * Set SD error code. | |
163 | * \param[in] code value for error code. | |
164 | */ | |
165 | void error(uint8_t code) {errorCode_ = code;} | |
166 | /** | |
167 | * \return error code for last error. See Sd2Card.h for a list of error codes. | |
168 | */ | |
169 | int errorCode() const {return errorCode_;} | |
170 | /** \return error data for last error. */ | |
171 | int errorData() 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 | * \return true for success or false for failure. | |
177 | */ | |
178 | bool init(uint8_t sckRateID = SPI_FULL_SPEED, | |
179 | uint8_t chipSelectPin = SD_CHIP_SELECT_PIN); | |
180 | bool readBlock(uint32_t block, uint8_t* dst); | |
181 | /** | |
182 | * Read a card's CID register. The CID contains card identification | |
183 | * information such as Manufacturer ID, Product name, Product serial | |
184 | * number and Manufacturing date. | |
185 | * | |
186 | * \param[out] cid pointer to area for returned data. | |
187 | * | |
188 | * \return true for success or false for failure. | |
189 | */ | |
190 | bool readCID(cid_t* cid) { | |
191 | return readRegister(CMD10, cid); | |
192 | } | |
193 | /** | |
194 | * Read a card's CSD register. The CSD contains Card-Specific Data that | |
195 | * provides information regarding access to the card's contents. | |
196 | * | |
197 | * \param[out] csd pointer to area for returned data. | |
198 | * | |
199 | * \return true for success or false for failure. | |
200 | */ | |
201 | bool readCSD(csd_t* csd) { | |
202 | return readRegister(CMD9, csd); | |
203 | } | |
204 | bool readData(uint8_t *dst); | |
205 | bool readStart(uint32_t blockNumber); | |
206 | bool readStop(); | |
207 | bool setSckRate(uint8_t sckRateID); | |
208 | /** Return the card type: SD V1, SD V2 or SDHC | |
209 | * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC. | |
210 | */ | |
211 | int type() const {return type_;} | |
212 | bool writeBlock(uint32_t blockNumber, const uint8_t* src); | |
213 | bool writeData(const uint8_t* src); | |
214 | bool writeStart(uint32_t blockNumber, uint32_t eraseCount); | |
215 | bool writeStop(); | |
216 | private: | |
217 | //---------------------------------------------------------------------------- | |
218 | uint8_t chipSelectPin_; | |
219 | uint8_t errorCode_; | |
220 | uint8_t spiRate_; | |
221 | uint8_t status_; | |
222 | uint8_t type_; | |
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 | #endif // Sd2Card_h | |
239 | ||
240 | ||
241 | #endif |