Project

General

Profile

Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / SD / utility / Sd2Card.h @ 58d82c77

History | View | Annotate | Download (9.06 KB)

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