Debian Codegraph @

app/testdata/libraries/SD_1.1.1/SD/src/utility/Sd2Card.h

raw symbols SCIP
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 // include pins_arduino.h or variant.h depending on architecture, via Arduino.h
59 #include <Arduino.h>
60
61 /**
62 * SD Chip Select pin
63 *
64 * Warning if this pin is redefined the hardware SS will pin will be enabled
65 * as an output by init(). An avr processor will not function as an SPI
66 * master unless SS is set to output mode.
67 */
68 #ifndef SDCARD_SS_PIN
69 /** The default chip select pin for the SD card is SS. */
70 uint8_t const SD_CHIP_SELECT_PIN = SS;
71 #else
72 uint8_t const SD_CHIP_SELECT_PIN = SDCARD_SS_PIN;
73 #endif
74
75 // The following three pins must not be redefined for hardware SPI,
76 // so ensure that they are taken from pins_arduino.h or variant.h, depending on architecture.
77 #ifndef SDCARD_MOSI_PIN
78 /** SPI Master Out Slave In pin */
79 uint8_t const SPI_MOSI_PIN = MOSI;
80 /** SPI Master In Slave Out pin */
81 uint8_t const SPI_MISO_PIN = MISO;
82 /** SPI Clock pin */
83 uint8_t const SPI_SCK_PIN = SCK;
84 #else
85 uint8_t const SPI_MOSI_PIN = SDCARD_MOSI_PIN;
86 uint8_t const SPI_MISO_PIN = SDCARD_MISO_PIN;
87 uint8_t const SPI_SCK_PIN = SDCARD_SCK_PIN;
88 #endif
89
90 /** optimize loops for hardware SPI */
91 #ifndef USE_SPI_LIB
92 #define OPTIMIZE_HARDWARE_SPI
93 #endif
94
95 #else // SOFTWARE_SPI
96 // define software SPI pins so Mega can use unmodified GPS Shield
97 /** SPI chip select pin */
98 uint8_t const SD_CHIP_SELECT_PIN = 10;
99 /** SPI Master Out Slave In pin */
100 uint8_t const SPI_MOSI_PIN = 11;
101 /** SPI Master In Slave Out pin */
102 uint8_t const SPI_MISO_PIN = 12;
103 /** SPI Clock pin */
104 uint8_t const SPI_SCK_PIN = 13;
105 #endif // SOFTWARE_SPI
106 //------------------------------------------------------------------------------
107 /** Protect block zero from write if nonzero */
108 #define SD_PROTECT_BLOCK_ZERO 1
109 /** init timeout ms */
110 uint16_t const SD_INIT_TIMEOUT = 2000;
111 /** erase timeout ms */
112 uint16_t const SD_ERASE_TIMEOUT = 10000;
113 /** read timeout ms */
114 uint16_t const SD_READ_TIMEOUT = 300;
115 /** write time out ms */
116 uint16_t const SD_WRITE_TIMEOUT = 600;
117 //------------------------------------------------------------------------------
118 // SD card errors
119 /** timeout error for command CMD0 */
120 uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
121 /** CMD8 was not accepted - not a valid SD card*/
122 uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
123 /** card returned an error response for CMD17 (read block) */
124 uint8_t const SD_CARD_ERROR_CMD17 = 0X3;
125 /** card returned an error response for CMD24 (write block) */
126 uint8_t const SD_CARD_ERROR_CMD24 = 0X4;
127 /** WRITE_MULTIPLE_BLOCKS command failed */
128 uint8_t const SD_CARD_ERROR_CMD25 = 0X05;
129 /** card returned an error response for CMD58 (read OCR) */
130 uint8_t const SD_CARD_ERROR_CMD58 = 0X06;
131 /** SET_WR_BLK_ERASE_COUNT failed */
132 uint8_t const SD_CARD_ERROR_ACMD23 = 0X07;
133 /** card's ACMD41 initialization process timeout */
134 uint8_t const SD_CARD_ERROR_ACMD41 = 0X08;
135 /** card returned a bad CSR version field */
136 uint8_t const SD_CARD_ERROR_BAD_CSD = 0X09;
137 /** erase block group command failed */
138 uint8_t const SD_CARD_ERROR_ERASE = 0X0A;
139 /** card not capable of single block erase */
140 uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0X0B;
141 /** Erase sequence timed out */
142 uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0X0C;
143 /** card returned an error token instead of read data */
144 uint8_t const SD_CARD_ERROR_READ = 0X0D;
145 /** read CID or CSD failed */
146 uint8_t const SD_CARD_ERROR_READ_REG = 0X0E;
147 /** timeout while waiting for start of read data */
148 uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X0F;
149 /** card did not accept STOP_TRAN_TOKEN */
150 uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X10;
151 /** card returned an error token as a response to a write operation */
152 uint8_t const SD_CARD_ERROR_WRITE = 0X11;
153 /** attempt to write protected block zero */
154 uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X12;
155 /** card did not go ready for a multiple block write */
156 uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X13;
157 /** card returned an error to a CMD13 status check after a write */
158 uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
159 /** timeout occurred during write programming */
160 uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
161 /** incorrect rate selected */
162 uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
163 //------------------------------------------------------------------------------
164 // card types
165 /** Standard capacity V1 SD card */
166 uint8_t const SD_CARD_TYPE_SD1 = 1;
167 /** Standard capacity V2 SD card */
168 uint8_t const SD_CARD_TYPE_SD2 = 2;
169 /** High Capacity SD card */
170 uint8_t const SD_CARD_TYPE_SDHC = 3;
171 //------------------------------------------------------------------------------
172 /**
173 * \class Sd2Card
174 * \brief Raw access to SD and SDHC flash memory cards.
175 */
176 class Sd2Card {
177 public:
178 /** Construct an instance of Sd2Card. */
179 Sd2Card(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {}
180 uint32_t cardSize(void);
181 uint8_t erase(uint32_t firstBlock, uint32_t lastBlock);
182 uint8_t eraseSingleBlockEnable(void);
183 /**
184 * \return error code for last error. See Sd2Card.h for a list of error codes.
185 */
186 uint8_t errorCode(void) const {return errorCode_;}
187 /** \return error data for last error. */
188 uint8_t errorData(void) const {return status_;}
189 /**
190 * Initialize an SD flash memory card with default clock rate and chip
191 * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
192 */
193 uint8_t init(void) {
194 return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN);
195 }
196 /**
197 * Initialize an SD flash memory card with the selected SPI clock rate
198 * and the default SD chip select pin.
199 * See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
200 */
201 uint8_t init(uint8_t sckRateID) {
202 return init(sckRateID, SD_CHIP_SELECT_PIN);
203 }
204 uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
205 void partialBlockRead(uint8_t value);
206 /** Returns the current value, true or false, for partial block read. */
207 uint8_t partialBlockRead(void) const {return partialBlockRead_;}
208 uint8_t readBlock(uint32_t block, uint8_t* dst);
209 uint8_t readData(uint32_t block,
210 uint16_t offset, uint16_t count, uint8_t* dst);
211 /**
212 * Read a cards CID register. The CID contains card identification
213 * information such as Manufacturer ID, Product name, Product serial
214 * number and Manufacturing date. */
215 uint8_t readCID(cid_t* cid) {
216 return readRegister(CMD10, cid);
217 }
218 /**
219 * Read a cards CSD register. The CSD contains Card-Specific Data that
220 * provides information regarding access to the card's contents. */
221 uint8_t readCSD(csd_t* csd) {
222 return readRegister(CMD9, csd);
223 }
224 void readEnd(void);
225 uint8_t setSckRate(uint8_t sckRateID);
226 #ifdef USE_SPI_LIB
227 uint8_t setSpiClock(uint32_t clock);
228 #endif
229 /** Return the card type: SD V1, SD V2 or SDHC */
230 uint8_t type(void) const {return type_;}
231 uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src);
232 uint8_t writeData(const uint8_t* src);
233 uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount);
234 uint8_t writeStop(void);
235 private:
236 uint32_t block_;
237 uint8_t chipSelectPin_;
238 uint8_t errorCode_;
239 uint8_t inBlock_;
240 uint16_t offset_;
241 uint8_t partialBlockRead_;
242 uint8_t status_;
243 uint8_t type_;
244 // private functions
245 uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
246 cardCommand(CMD55, 0);
247 return cardCommand(cmd, arg);
248 }
249 uint8_t cardCommand(uint8_t cmd, uint32_t arg);
250 void error(uint8_t code) {errorCode_ = code;}
251 uint8_t readRegister(uint8_t cmd, void* buf);
252 uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
253 void chipSelectHigh(void);
254 void chipSelectLow(void);
255 void type(uint8_t value) {type_ = value;}
256 uint8_t waitNotBusy(uint16_t timeoutMillis);
257 uint8_t writeData(uint8_t token, const uint8_t* src);
258 uint8_t waitStartBlock(void);
259 };
260 #endif // Sd2Card_h