Arduino IDE  0
Arduino IDE and compiler
Sd2Card Class Reference

Raw access to SD and SDHC flash memory cards. More...

#include <Sd2Card.h>

+ Collaboration diagram for Sd2Card:

Public Member Functions

 Sd2Card (void)
 
uint32_t cardSize (void)
 
uint8_t erase (uint32_t firstBlock, uint32_t lastBlock)
 
uint8_t eraseSingleBlockEnable (void)
 
uint8_t errorCode (void) const
 
uint8_t errorData (void) const
 
uint8_t init (void)
 
uint8_t init (uint8_t sckRateID)
 
uint8_t init (uint8_t sckRateID, uint8_t chipSelectPin)
 
void partialBlockRead (uint8_t value)
 
uint8_t partialBlockRead (void) const
 
uint8_t readBlock (uint32_t block, uint8_t *dst)
 
uint8_t readData (uint32_t block, uint16_t offset, uint16_t count, uint8_t *dst)
 
uint8_t readCID (cid_t *cid)
 
uint8_t readCSD (csd_t *csd)
 
void readEnd (void)
 
uint8_t setSckRate (uint8_t sckRateID)
 
uint8_t setSpiClock (uint32_t clock)
 
uint8_t type (void) const
 
uint8_t writeBlock (uint32_t blockNumber, const uint8_t *src)
 
uint8_t writeData (const uint8_t *src)
 
uint8_t writeStart (uint32_t blockNumber, uint32_t eraseCount)
 
uint8_t writeStop (void)
 
 Sd2Card (void)
 
uint32_t cardSize (void)
 
uint8_t erase (uint32_t firstBlock, uint32_t lastBlock)
 
uint8_t eraseSingleBlockEnable (void)
 
uint8_t errorCode (void) const
 
uint8_t errorData (void) const
 
uint8_t init (void)
 
uint8_t init (uint8_t sckRateID)
 
uint8_t init (uint8_t sckRateID, uint8_t chipSelectPin)
 
void partialBlockRead (uint8_t value)
 
uint8_t partialBlockRead (void) const
 
uint8_t readBlock (uint32_t block, uint8_t *dst)
 
uint8_t readData (uint32_t block, uint16_t offset, uint16_t count, uint8_t *dst)
 
uint8_t readCID (cid_t *cid)
 
uint8_t readCSD (csd_t *csd)
 
void readEnd (void)
 
uint8_t setSckRate (uint8_t sckRateID)
 
uint8_t setSpiClock (uint32_t clock)
 
uint8_t type (void) const
 
uint8_t writeBlock (uint32_t blockNumber, const uint8_t *src)
 
uint8_t writeData (const uint8_t *src)
 
uint8_t writeStart (uint32_t blockNumber, uint32_t eraseCount)
 
uint8_t writeStop (void)
 

Detailed Description

Raw access to SD and SDHC flash memory cards.

Definition at line 176 of file Sd2Card.h.

Constructor & Destructor Documentation

◆ Sd2Card() [1/2]

Sd2Card::Sd2Card ( void  )

Construct an instance of Sd2Card.

Definition at line 179 of file Sd2Card.h.

References cardSize(), erase(), and eraseSingleBlockEnable().

179 : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {}
+ Here is the call graph for this function:

◆ Sd2Card() [2/2]

Sd2Card::Sd2Card ( void  )

Construct an instance of Sd2Card.

Definition at line 179 of file Sd2Card.h.

References cardSize(), erase(), and eraseSingleBlockEnable().

179 : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {}
+ Here is the call graph for this function:

Member Function Documentation

◆ cardSize() [1/2]

uint32_t Sd2Card::cardSize ( void  )

Determine the size of an SD flash memory card.

Returns
The number of 512 byte data blocks in the card or zero if an error occurs.

Definition at line 145 of file Sd2Card.cpp.

References CSDV1::c_size_high, CSDV2::c_size_high, CSDV1::c_size_low, CSDV2::c_size_low, CSDV1::c_size_mid, CSDV2::c_size_mid, CSDV1::c_size_mult_high, CSDV1::c_size_mult_low, CSDV1::csd_ver, CSDV2::csd_ver, CSDV1::read_bl_len, readCSD(), SD_CARD_ERROR_BAD_CSD, csd_t::v1, and csd_t::v2.

Referenced by Sd2Card().

145  {
146  csd_t csd;
147  if (!readCSD(&csd)) return 0;
148  if (csd.v1.csd_ver == 0) {
149  uint8_t read_bl_len = csd.v1.read_bl_len;
150  uint16_t c_size = (csd.v1.c_size_high << 10)
151  | (csd.v1.c_size_mid << 2) | csd.v1.c_size_low;
152  uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1)
153  | csd.v1.c_size_mult_low;
154  return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7);
155  } else if (csd.v2.csd_ver == 1) {
156  uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16)
157  | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low;
158  return (c_size + 1) << 10;
159  } else {
160  error(SD_CARD_ERROR_BAD_CSD);
161  return 0;
162  }
163 }
unsigned c_size_high
Definition: SdInfo.h:126
unsigned c_size_mult_high
Definition: SdInfo.h:139
uint8_t c_size_mid
Definition: SdInfo.h:196
uint8_t readCSD(csd_t *csd)
Definition: Sd2Card.h:221
uint8_t c_size_mid
Definition: SdInfo.h:133
csd2_t v2
Definition: SdInfo.h:230
unsigned c_size_high
Definition: SdInfo.h:194
csd1_t v1
Definition: SdInfo.h:229
unsigned csd_ver
Definition: SdInfo.h:113
Definition: SdInfo.h:228
unsigned c_size_mult_low
Definition: SdInfo.h:145
unsigned c_size_low
Definition: SdInfo.h:137
unsigned csd_ver
Definition: SdInfo.h:174
uint8_t const SD_CARD_ERROR_BAD_CSD
Definition: Sd2Card.h:136
unsigned read_bl_len
Definition: SdInfo.h:123
uint8_t c_size_low
Definition: SdInfo.h:198
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cardSize() [2/2]

uint32_t Sd2Card::cardSize ( void  )

◆ erase() [1/2]

uint8_t Sd2Card::erase ( uint32_t  firstBlock,
uint32_t  lastBlock 
)

Erase a range of blocks.

Parameters
[in]firstBlockThe address of the first block in the range.
[in]lastBlockThe address of the last block in the range.
Note
This function requests the SD card to do a flash erase for a range of blocks. The data on the card after an erase operation is either 0 or 1, depends on the card vendor. The card must support single block erase.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 200 of file Sd2Card.cpp.

References CMD32, CMD33, CMD38, eraseSingleBlockEnable(), SD_CARD_ERROR_ERASE, SD_CARD_ERROR_ERASE_SINGLE_BLOCK, SD_CARD_ERROR_ERASE_TIMEOUT, SD_CARD_TYPE_SDHC, and SD_ERASE_TIMEOUT.

Referenced by Sd2Card().

200  {
201  if (!eraseSingleBlockEnable()) {
203  goto fail;
204  }
205  if (type_ != SD_CARD_TYPE_SDHC) {
206  firstBlock <<= 9;
207  lastBlock <<= 9;
208  }
209  if (cardCommand(CMD32, firstBlock)
210  || cardCommand(CMD33, lastBlock)
211  || cardCommand(CMD38, 0)) {
212  error(SD_CARD_ERROR_ERASE);
213  goto fail;
214  }
215  if (!waitNotBusy(SD_ERASE_TIMEOUT)) {
217  goto fail;
218  }
219  chipSelectHigh();
220  return true;
221 
222  fail:
223  chipSelectHigh();
224  return false;
225 }
uint8_t eraseSingleBlockEnable(void)
Definition: Sd2Card.cpp:232
uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT
Definition: Sd2Card.h:142
uint8_t const CMD38
Definition: SdInfo.h:57
uint8_t const CMD32
Definition: SdInfo.h:52
uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK
Definition: Sd2Card.h:140
uint8_t const CMD33
Definition: SdInfo.h:55
uint16_t const SD_ERASE_TIMEOUT
Definition: Sd2Card.h:112
uint8_t const SD_CARD_ERROR_ERASE
Definition: Sd2Card.h:138
uint8_t const SD_CARD_TYPE_SDHC
Definition: Sd2Card.h:170
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ erase() [2/2]

uint8_t Sd2Card::erase ( uint32_t  firstBlock,
uint32_t  lastBlock 
)

◆ eraseSingleBlockEnable() [1/2]

uint8_t Sd2Card::eraseSingleBlockEnable ( void  )

Determine if card supports single block erase.

Returns
The value one, true, is returned if single block erase is supported. The value zero, false, is returned if single block erase is not supported.

Definition at line 232 of file Sd2Card.cpp.

References CSDV1::erase_blk_en, readCSD(), and csd_t::v1.

Referenced by erase(), and Sd2Card().

232  {
233  csd_t csd;
234  return readCSD(&csd) ? csd.v1.erase_blk_en : 0;
235 }
unsigned erase_blk_en
Definition: SdInfo.h:144
uint8_t readCSD(csd_t *csd)
Definition: Sd2Card.h:221
csd1_t v1
Definition: SdInfo.h:229
Definition: SdInfo.h:228
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eraseSingleBlockEnable() [2/2]

uint8_t Sd2Card::eraseSingleBlockEnable ( void  )

◆ errorCode() [1/2]

uint8_t Sd2Card::errorCode ( void  ) const
Returns
error code for last error. See Sd2Card.h for a list of error codes.

Definition at line 186 of file Sd2Card.h.

186 {return errorCode_;}

◆ errorCode() [2/2]

uint8_t Sd2Card::errorCode ( void  ) const
Returns
error code for last error. See Sd2Card.h for a list of error codes.

Definition at line 186 of file Sd2Card.h.

186 {return errorCode_;}

◆ errorData() [1/2]

uint8_t Sd2Card::errorData ( void  ) const
Returns
error data for last error.

Definition at line 188 of file Sd2Card.h.

188 {return status_;}

◆ errorData() [2/2]

uint8_t Sd2Card::errorData ( void  ) const
Returns
error data for last error.

Definition at line 188 of file Sd2Card.h.

188 {return status_;}

◆ init() [1/6]

uint8_t Sd2Card::init ( void  )

Initialize an SD flash memory card with default clock rate and chip select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).

Definition at line 193 of file Sd2Card.h.

References SD_CHIP_SELECT_PIN, and SPI_FULL_SPEED.

Referenced by SDLib::SDClass::begin(), and init().

193  {
195  }
uint8_t const SD_CHIP_SELECT_PIN
Definition: Sd2Card.h:70
uint8_t const SPI_FULL_SPEED
Definition: Sd2Card.h:29
uint8_t init(void)
Definition: Sd2Card.h:193
+ Here is the caller graph for this function:

◆ init() [2/6]

uint8_t Sd2Card::init ( void  )

Initialize an SD flash memory card with default clock rate and chip select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).

Definition at line 193 of file Sd2Card.h.

References init(), SD_CHIP_SELECT_PIN, and SPI_FULL_SPEED.

193  {
195  }
uint8_t const SD_CHIP_SELECT_PIN
Definition: Sd2Card.h:70
uint8_t const SPI_FULL_SPEED
Definition: Sd2Card.h:29
uint8_t init(void)
Definition: Sd2Card.h:193
+ Here is the call graph for this function:

◆ init() [3/6]

uint8_t Sd2Card::init ( uint8_t  sckRateID)

Initialize an SD flash memory card with the selected SPI clock rate and the default SD chip select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).

Definition at line 201 of file Sd2Card.h.

References init(), partialBlockRead(), and SD_CHIP_SELECT_PIN.

201  {
202  return init(sckRateID, SD_CHIP_SELECT_PIN);
203  }
uint8_t const SD_CHIP_SELECT_PIN
Definition: Sd2Card.h:70
uint8_t init(void)
Definition: Sd2Card.h:193
+ Here is the call graph for this function:

◆ init() [4/6]

uint8_t Sd2Card::init ( uint8_t  sckRateID)

Initialize an SD flash memory card with the selected SPI clock rate and the default SD chip select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).

Definition at line 201 of file Sd2Card.h.

References init(), partialBlockRead(), and SD_CHIP_SELECT_PIN.

201  {
202  return init(sckRateID, SD_CHIP_SELECT_PIN);
203  }
uint8_t const SD_CHIP_SELECT_PIN
Definition: Sd2Card.h:70
uint8_t init(void)
Definition: Sd2Card.h:193
+ Here is the call graph for this function:

◆ init() [5/6]

uint8_t Sd2Card::init ( uint8_t  sckRateID,
uint8_t  chipSelectPin 
)

Initialize an SD flash memory card.

Parameters
[in]sckRateIDSPI clock rate selector. See setSckRate().
[in]chipSelectPinSD chip select pin number.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure. The reason for failure can be determined by calling errorCode() and errorData().

Definition at line 247 of file Sd2Card.cpp.

References SDCARD_SPI, SPI_MISO_PIN, SPI_MOSI_PIN, and SPI_SCK_PIN.

247  {
248  errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0;
249  chipSelectPin_ = chipSelectPin;
250  // 16-bit init start time allows over a minute
251  uint16_t t0 = (uint16_t)millis();
252  uint32_t arg;
253 
254  // set pin modes
255  pinMode(chipSelectPin_, OUTPUT);
256  digitalWrite(chipSelectPin_, HIGH);
257 #ifndef USE_SPI_LIB
258  pinMode(SPI_MISO_PIN, INPUT);
259  pinMode(SPI_MOSI_PIN, OUTPUT);
260  pinMode(SPI_SCK_PIN, OUTPUT);
261 #endif
262 
263 #ifndef SOFTWARE_SPI
264 #ifndef USE_SPI_LIB
265  // SS must be in output mode even it is not chip select
266  pinMode(SS_PIN, OUTPUT);
267  digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
268  // Enable SPI, Master, clock rate f_osc/128
269  SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
270  // clear double speed
271  SPSR &= ~(1 << SPI2X);
272 #else // USE_SPI_LIB
273  SDCARD_SPI.begin();
274  settings = SPISettings(250000, MSBFIRST, SPI_MODE0);
275 #endif // USE_SPI_LIB
276 #endif // SOFTWARE_SPI
277 
278  // must supply min of 74 clock cycles with CS high.
279 #ifdef USE_SPI_LIB
280  SDCARD_SPI.beginTransaction(settings);
281 #endif
282  for (uint8_t i = 0; i < 10; i++) spiSend(0XFF);
283 #ifdef USE_SPI_LIB
284  SDCARD_SPI.endTransaction();
285 #endif
286 
287  chipSelectLow();
288 
289  // command to go idle in SPI mode
290  while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
291  if (((uint16_t)(millis() - t0)) > SD_INIT_TIMEOUT) {
292  error(SD_CARD_ERROR_CMD0);
293  goto fail;
294  }
295  }
296  // check SD version
297  if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
299  } else {
300  // only need last byte of r7 response
301  for (uint8_t i = 0; i < 4; i++) status_ = spiRec();
302  if (status_ != 0XAA) {
303  error(SD_CARD_ERROR_CMD8);
304  goto fail;
305  }
307  }
308  // initialize card and send host supports SDHC if SD2
309  arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0;
310 
311  while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
312  // check for timeout
313  if (((uint16_t)(millis() - t0)) > SD_INIT_TIMEOUT) {
314  error(SD_CARD_ERROR_ACMD41);
315  goto fail;
316  }
317  }
318  // if SD2 read OCR register to check for SDHC card
319  if (type() == SD_CARD_TYPE_SD2) {
320  if (cardCommand(CMD58, 0)) {
321  error(SD_CARD_ERROR_CMD58);
322  goto fail;
323  }
324  if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC);
325  // discard rest of ocr - contains allowed voltage range
326  for (uint8_t i = 0; i < 3; i++) spiRec();
327  }
328  chipSelectHigh();
329 
330 #ifndef SOFTWARE_SPI
331  return setSckRate(sckRateID);
332 #else // SOFTWARE_SPI
333  return true;
334 #endif // SOFTWARE_SPI
335 
336  fail:
337  chipSelectHigh();
338  return false;
339 }
uint8_t const SD_CARD_ERROR_ACMD41
Definition: Sd2Card.h:134
#define SDCARD_SPI
Definition: Sd2Card.cpp:28
uint8_t const CMD58
Definition: SdInfo.h:61
uint16_t const SD_INIT_TIMEOUT
Definition: Sd2Card.h:110
uint8_t const SD_CARD_ERROR_CMD0
Definition: Sd2Card.h:120
uint8_t const SPI_SCK_PIN
Definition: Sd2Card.h:83
uint8_t type(void) const
Definition: Sd2Card.h:230
uint8_t const SD_CARD_TYPE_SD2
Definition: Sd2Card.h:168
uint8_t const SPI_MISO_PIN
Definition: Sd2Card.h:81
uint8_t const SD_CARD_ERROR_CMD58
Definition: Sd2Card.h:130
uint8_t const R1_READY_STATE
Definition: SdInfo.h:70
uint8_t const SD_CARD_TYPE_SD1
Definition: Sd2Card.h:166
uint8_t const SD_CARD_ERROR_CMD8
Definition: Sd2Card.h:122
uint8_t const CMD0
Definition: SdInfo.h:36
uint8_t const SPI_MOSI_PIN
Definition: Sd2Card.h:79
uint8_t const ACMD41
Definition: SdInfo.h:67
uint8_t setSckRate(uint8_t sckRateID)
Definition: Sd2Card.cpp:505
uint8_t const CMD8
Definition: SdInfo.h:38
uint8_t const R1_ILLEGAL_COMMAND
Definition: SdInfo.h:74
uint8_t const SD_CARD_TYPE_SDHC
Definition: Sd2Card.h:170
uint8_t const R1_IDLE_STATE
Definition: SdInfo.h:72

◆ init() [6/6]

uint8_t Sd2Card::init ( uint8_t  sckRateID,
uint8_t  chipSelectPin 
)

◆ partialBlockRead() [1/4]

void Sd2Card::partialBlockRead ( uint8_t  value)

Enable or disable partial block reads.

Enabling partial block reads improves performance by allowing a block to be read over the SPI bus as several sub-blocks. Errors may occur if the time between reads is too long since the SD card may timeout. The SPI SS line will be held low until the entire block is read or readEnd() is called.

Use this for applications like the Adafruit Wave Shield.

Parameters
[in]valueThe value TRUE (non-zero) or FALSE (zero).)

Definition at line 354 of file Sd2Card.cpp.

References readEnd().

354  {
355  readEnd();
356  partialBlockRead_ = value;
357 }
void readEnd(void)
Definition: Sd2Card.cpp:451
+ Here is the call graph for this function:

◆ partialBlockRead() [2/4]

void Sd2Card::partialBlockRead ( uint8_t  value)

◆ partialBlockRead() [3/4]

uint8_t Sd2Card::partialBlockRead ( void  ) const

Returns the current value, true or false, for partial block read.

Definition at line 207 of file Sd2Card.h.

References readBlock(), and readData().

207 {return partialBlockRead_;}
+ Here is the call graph for this function:

◆ partialBlockRead() [4/4]

uint8_t Sd2Card::partialBlockRead ( void  ) const

Returns the current value, true or false, for partial block read.

Definition at line 207 of file Sd2Card.h.

References readBlock(), and readData().

Referenced by init().

207 {return partialBlockRead_;}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readBlock() [1/2]

uint8_t Sd2Card::readBlock ( uint32_t  block,
uint8_t *  dst 
)

◆ readBlock() [2/2]

uint8_t Sd2Card::readBlock ( uint32_t  block,
uint8_t *  dst 
)

Read a 512 byte block from an SD card device.

Parameters
[in]blockLogical block to be read.
[out]dstPointer to the location that will receive the data.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 368 of file Sd2Card.cpp.

References readData().

Referenced by SdVolume::init(), and partialBlockRead().

368  {
369  return readData(block, 0, 512, dst);
370 }
uint8_t readData(uint32_t block, uint16_t offset, uint16_t count, uint8_t *dst)
Definition: Sd2Card.cpp:382
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readCID() [1/2]

uint8_t Sd2Card::readCID ( cid_t cid)

Read a cards CID register. The CID contains card identification information such as Manufacturer ID, Product name, Product serial number and Manufacturing date.

Definition at line 215 of file Sd2Card.h.

References CMD10.

215  {
216  return readRegister(CMD10, cid);
217  }
uint8_t const CMD10
Definition: SdInfo.h:42

◆ readCID() [2/2]

uint8_t Sd2Card::readCID ( cid_t cid)

Read a cards CID register. The CID contains card identification information such as Manufacturer ID, Product name, Product serial number and Manufacturing date.

Definition at line 215 of file Sd2Card.h.

References CMD10.

215  {
216  return readRegister(CMD10, cid);
217  }
uint8_t const CMD10
Definition: SdInfo.h:42

◆ readCSD() [1/2]

uint8_t Sd2Card::readCSD ( csd_t csd)

Read a cards CSD register. The CSD contains Card-Specific Data that provides information regarding access to the card's contents.

Definition at line 221 of file Sd2Card.h.

References CMD9, readEnd(), setSckRate(), and setSpiClock().

221  {
222  return readRegister(CMD9, csd);
223  }
uint8_t const CMD9
Definition: SdInfo.h:40
+ Here is the call graph for this function:

◆ readCSD() [2/2]

uint8_t Sd2Card::readCSD ( csd_t csd)

Read a cards CSD register. The CSD contains Card-Specific Data that provides information regarding access to the card's contents.

Definition at line 221 of file Sd2Card.h.

References CMD9, readEnd(), setSckRate(), and setSpiClock().

Referenced by cardSize(), and eraseSingleBlockEnable().

221  {
222  return readRegister(CMD9, csd);
223  }
uint8_t const CMD9
Definition: SdInfo.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readData() [1/2]

uint8_t Sd2Card::readData ( uint32_t  block,
uint16_t  offset,
uint16_t  count,
uint8_t *  dst 
)

Read part of a 512 byte block from an SD card.

Parameters
[in]blockLogical block to be read.
[in]offsetNumber of bytes to skip at start of block
[out]dstPointer to the location that will receive the data.
[in]countNumber of bytes to read
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 382 of file Sd2Card.cpp.

References CMD17, SD_CARD_ERROR_CMD17, SD_CARD_TYPE_SDHC, and type().

Referenced by SdVolume::init(), partialBlockRead(), and readBlock().

383  {
384  if (count == 0) return true;
385  if ((count + offset) > 512) {
386  goto fail;
387  }
388  if (!inBlock_ || block != block_ || offset < offset_) {
389  block_ = block;
390  // use address if not SDHC card
391  if (type()!= SD_CARD_TYPE_SDHC) block <<= 9;
392  if (cardCommand(CMD17, block)) {
393  error(SD_CARD_ERROR_CMD17);
394  goto fail;
395  }
396  if (!waitStartBlock()) {
397  goto fail;
398  }
399  offset_ = 0;
400  inBlock_ = 1;
401  }
402 
403 #ifdef OPTIMIZE_HARDWARE_SPI
404  // start first spi transfer
405  SPDR = 0XFF;
406 
407  // skip data before offset
408  for (;offset_ < offset; offset_++) {
409  while (!(SPSR & (1 << SPIF)))
410  ;
411  SPDR = 0XFF;
412  }
413  // transfer data
414  n = count - 1;
415  for (uint16_t i = 0; i < n; i++) {
416  while (!(SPSR & (1 << SPIF)))
417  ;
418  dst[i] = SPDR;
419  SPDR = 0XFF;
420  }
421  // wait for last byte
422  while (!(SPSR & (1 << SPIF)))
423  ;
424  dst[n] = SPDR;
425 
426 #else // OPTIMIZE_HARDWARE_SPI
427 
428  // skip data before offset
429  for (;offset_ < offset; offset_++) {
430  spiRec();
431  }
432  // transfer data
433  for (uint16_t i = 0; i < count; i++) {
434  dst[i] = spiRec();
435  }
436 #endif // OPTIMIZE_HARDWARE_SPI
437 
438  offset_ += count;
439  if (!partialBlockRead_ || offset_ >= 512) {
440  // read rest of data, checksum and set chip select high
441  readEnd();
442  }
443  return true;
444 
445  fail:
446  chipSelectHigh();
447  return false;
448 }
uint8_t type(void) const
Definition: Sd2Card.h:230
void readEnd(void)
Definition: Sd2Card.cpp:451
uint8_t const CMD17
Definition: SdInfo.h:46
uint8_t const SD_CARD_ERROR_CMD17
Definition: Sd2Card.h:124
uint8_t const SD_CARD_TYPE_SDHC
Definition: Sd2Card.h:170
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readData() [2/2]

uint8_t Sd2Card::readData ( uint32_t  block,
uint16_t  offset,
uint16_t  count,
uint8_t *  dst 
)

◆ readEnd() [1/2]

void Sd2Card::readEnd ( void  )

◆ readEnd() [2/2]

void Sd2Card::readEnd ( void  )

Skip remaining data in a block when in partial block read mode.

Definition at line 451 of file Sd2Card.cpp.

Referenced by partialBlockRead(), and readCSD().

451  {
452  if (inBlock_) {
453  // skip data and crc
454 #ifdef OPTIMIZE_HARDWARE_SPI
455  // optimize skip for hardware
456  SPDR = 0XFF;
457  while (offset_++ < 513) {
458  while (!(SPSR & (1 << SPIF)))
459  ;
460  SPDR = 0XFF;
461  }
462  // wait for last crc byte
463  while (!(SPSR & (1 << SPIF)))
464  ;
465 #else // OPTIMIZE_HARDWARE_SPI
466  while (offset_++ < 514) spiRec();
467 #endif // OPTIMIZE_HARDWARE_SPI
468  chipSelectHigh();
469  inBlock_ = 0;
470  }
471 }
+ Here is the caller graph for this function:

◆ setSckRate() [1/2]

uint8_t Sd2Card::setSckRate ( uint8_t  sckRateID)

Set the SPI clock rate.

Parameters
[in]sckRateIDA value in the range [0, 6].

The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum SPI rate is F_CPU/2 for sckRateID = 0 and the minimum rate is F_CPU/128 for scsRateID = 6.

Returns
The value one, true, is returned for success and the value zero, false, is returned for an invalid value of sckRateID.

Definition at line 505 of file Sd2Card.cpp.

References SD_CARD_ERROR_SCK_RATE.

Referenced by readCSD().

505  {
506  if (sckRateID > 6) {
507  error(SD_CARD_ERROR_SCK_RATE);
508  return false;
509  }
510 #ifndef USE_SPI_LIB
511  // see avr processor datasheet for SPI register bit definitions
512  if ((sckRateID & 1) || sckRateID == 6) {
513  SPSR &= ~(1 << SPI2X);
514  } else {
515  SPSR |= (1 << SPI2X);
516  }
517  SPCR &= ~((1 <<SPR1) | (1 << SPR0));
518  SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
519  | (sckRateID & 2 ? (1 << SPR0) : 0);
520 #else // USE_SPI_LIB
521  switch (sckRateID) {
522  case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break;
523  case 1: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break;
524  case 2: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break;
525  case 3: settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); break;
526  case 4: settings = SPISettings(500000, MSBFIRST, SPI_MODE0); break;
527  case 5: settings = SPISettings(250000, MSBFIRST, SPI_MODE0); break;
528  default: settings = SPISettings(125000, MSBFIRST, SPI_MODE0);
529  }
530 #endif // USE_SPI_LIB
531  return true;
532 }
uint8_t const SD_CARD_ERROR_SCK_RATE
Definition: Sd2Card.h:162
+ Here is the caller graph for this function:

◆ setSckRate() [2/2]

uint8_t Sd2Card::setSckRate ( uint8_t  sckRateID)

◆ setSpiClock() [1/2]

uint8_t Sd2Card::setSpiClock ( uint32_t  clock)

◆ setSpiClock() [2/2]

uint8_t Sd2Card::setSpiClock ( uint32_t  clock)

Definition at line 536 of file Sd2Card.cpp.

Referenced by SDLib::SDClass::begin(), and readCSD().

537 {
538  settings = SPISettings(clock, MSBFIRST, SPI_MODE0);
539  return true;
540 }
+ Here is the caller graph for this function:

◆ type() [1/2]

uint8_t Sd2Card::type ( void  ) const

Return the card type: SD V1, SD V2 or SDHC

Definition at line 230 of file Sd2Card.h.

References CMD55, type(), writeBlock(), writeData(), writeStart(), and writeStop().

230 {return type_;}
+ Here is the call graph for this function:

◆ type() [2/2]

uint8_t Sd2Card::type ( void  ) const

Return the card type: SD V1, SD V2 or SDHC

Definition at line 230 of file Sd2Card.h.

References CMD55, writeBlock(), writeData(), writeStart(), and writeStop().

Referenced by readData(), type(), writeBlock(), and writeStart().

230 {return type_;}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeBlock() [1/2]

uint8_t Sd2Card::writeBlock ( uint32_t  blockNumber,
const uint8_t *  src 
)

◆ writeBlock() [2/2]

uint8_t Sd2Card::writeBlock ( uint32_t  blockNumber,
const uint8_t *  src 
)

Writes a 512 byte block to an SD card.

Parameters
[in]blockNumberLogical block to be written.
[in]srcPointer to the location of the data to be written.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 581 of file Sd2Card.cpp.

References CMD13, CMD24, DATA_START_BLOCK, SD_CARD_ERROR_CMD24, SD_CARD_ERROR_WRITE_BLOCK_ZERO, SD_CARD_ERROR_WRITE_TIMEOUT, SD_CARD_TYPE_SDHC, SD_WRITE_TIMEOUT, type(), and writeData().

Referenced by SdVolume::init(), and type().

581  {
582 #if SD_PROTECT_BLOCK_ZERO
583  // don't allow write to first block
584  if (blockNumber == 0) {
586  goto fail;
587  }
588 #endif // SD_PROTECT_BLOCK_ZERO
589 
590  // use address if not SDHC card
591  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
592  if (cardCommand(CMD24, blockNumber)) {
593  error(SD_CARD_ERROR_CMD24);
594  goto fail;
595  }
596  if (!writeData(DATA_START_BLOCK, src)) goto fail;
597 
598  // wait for flash programming to complete
599  if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
601  goto fail;
602  }
603  // response is r2 so get and check two bytes for nonzero
604  if (cardCommand(CMD13, 0) || spiRec()) {
606  goto fail;
607  }
608  chipSelectHigh();
609  return true;
610 
611  fail:
612  chipSelectHigh();
613  return false;
614 }
uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING
Definition: Sd2Card.h:158
uint16_t const SD_WRITE_TIMEOUT
Definition: Sd2Card.h:116
uint8_t writeData(const uint8_t *src)
Definition: Sd2Card.cpp:617
uint8_t type(void) const
Definition: Sd2Card.h:230
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO
Definition: Sd2Card.h:154
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT
Definition: Sd2Card.h:160
uint8_t const DATA_START_BLOCK
Definition: SdInfo.h:76
uint8_t const CMD13
Definition: SdInfo.h:44
uint8_t const SD_CARD_ERROR_CMD24
Definition: Sd2Card.h:126
uint8_t const SD_CARD_TYPE_SDHC
Definition: Sd2Card.h:170
uint8_t const CMD24
Definition: SdInfo.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeData() [1/2]

uint8_t Sd2Card::writeData ( const uint8_t *  src)

Write one data block in a multiple block write sequence

Definition at line 617 of file Sd2Card.cpp.

References SD_CARD_ERROR_WRITE_MULTIPLE, SD_WRITE_TIMEOUT, and WRITE_MULTIPLE_TOKEN.

Referenced by type(), and writeBlock().

617  {
618  // wait for previous write to finish
619  if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
621  chipSelectHigh();
622  return false;
623  }
624  return writeData(WRITE_MULTIPLE_TOKEN, src);
625 }
uint16_t const SD_WRITE_TIMEOUT
Definition: Sd2Card.h:116
uint8_t writeData(const uint8_t *src)
Definition: Sd2Card.cpp:617
uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE
Definition: Sd2Card.h:156
uint8_t const WRITE_MULTIPLE_TOKEN
Definition: SdInfo.h:80
+ Here is the caller graph for this function:

◆ writeData() [2/2]

uint8_t Sd2Card::writeData ( const uint8_t *  src)

◆ writeStart() [1/2]

uint8_t Sd2Card::writeStart ( uint32_t  blockNumber,
uint32_t  eraseCount 
)

◆ writeStart() [2/2]

uint8_t Sd2Card::writeStart ( uint32_t  blockNumber,
uint32_t  eraseCount 
)

Start a write multiple blocks sequence.

Parameters
[in]blockNumberAddress of first block in sequence.
[in]eraseCountThe number of blocks to be pre-erased.
Note
This function is used with writeData() and writeStop() for optimized multiple block writes.
Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 677 of file Sd2Card.cpp.

References ACMD23, CMD25, SD_CARD_ERROR_ACMD23, SD_CARD_ERROR_CMD25, SD_CARD_ERROR_WRITE_BLOCK_ZERO, SD_CARD_TYPE_SDHC, and type().

Referenced by type().

677  {
678 #if SD_PROTECT_BLOCK_ZERO
679  // don't allow write to first block
680  if (blockNumber == 0) {
682  goto fail;
683  }
684 #endif // SD_PROTECT_BLOCK_ZERO
685  // send pre-erase count
686  if (cardAcmd(ACMD23, eraseCount)) {
687  error(SD_CARD_ERROR_ACMD23);
688  goto fail;
689  }
690  // use address if not SDHC card
691  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
692  if (cardCommand(CMD25, blockNumber)) {
693  error(SD_CARD_ERROR_CMD25);
694  goto fail;
695  }
696  return true;
697 
698  fail:
699  chipSelectHigh();
700  return false;
701 }
uint8_t const ACMD23
Definition: SdInfo.h:64
uint8_t type(void) const
Definition: Sd2Card.h:230
uint8_t const SD_CARD_ERROR_ACMD23
Definition: Sd2Card.h:132
uint8_t const CMD25
Definition: SdInfo.h:50
uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO
Definition: Sd2Card.h:154
uint8_t const SD_CARD_ERROR_CMD25
Definition: Sd2Card.h:128
uint8_t const SD_CARD_TYPE_SDHC
Definition: Sd2Card.h:170
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeStop() [1/2]

uint8_t Sd2Card::writeStop ( void  )

End a write multiple blocks sequence.

Returns
The value one, true, is returned for success and the value zero, false, is returned for failure.

Definition at line 708 of file Sd2Card.cpp.

References SD_WRITE_TIMEOUT.

Referenced by type().

708  {
709  if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
710  spiSend(STOP_TRAN_TOKEN);
711  if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
712  chipSelectHigh();
713  return true;
714 
715  fail:
717  chipSelectHigh();
718  return false;
719 }
uint8_t const STOP_TRAN_TOKEN
Definition: SdInfo.h:78
uint16_t const SD_WRITE_TIMEOUT
Definition: Sd2Card.h:116
uint8_t const SD_CARD_ERROR_STOP_TRAN
Definition: Sd2Card.h:150
+ Here is the caller graph for this function:

◆ writeStop() [2/2]

uint8_t Sd2Card::writeStop ( void  )

The documentation for this class was generated from the following files: