1 /* Arduino Sd2Card Library
2 * Copyright (C) 2009 by William Greiman
4 * This file is part of the Arduino Sd2Card Library
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.
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.
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/>.
26 //------------------------------------------------------------------------------
28 static int8_t mosiPin_, misoPin_, clockPin_;
29 static volatile uint8_t *mosiport, *clkport, *misoport;
30 static uint8_t mosipinmask, clkpinmask, misopinmask;
32 //------------------------------------------------------------------------------
33 /** nop to tune soft SPI timing */
34 #define nop asm volatile ("nop\n\t")
37 // functions for hardware SPI
38 /** Send a byte to the card */
39 static void spiSend(uint8_t data) {
40 if (clockPin_ == -1) {
42 while (!(SPSR & (1 << SPIF)));
45 // Fast SPI bitbang swiped from LPD8806 library
46 for (uint8_t i = 0; i < 8; i++) {
47 *clkport &= ~clkpinmask;
49 *mosiport |= mosipinmask;
51 *mosiport &= ~mosipinmask;
52 *clkport |= clkpinmask;
56 *clkport &= ~clkpinmask;
62 /** Receive a byte from the card */
63 static uint8_t spiRec(void) {
64 if (clockPin_ == -1) {
69 // no interrupts during byte receive - about 8 us
71 // output pin high - like sending 0XFF
72 *mosiport |= mosipinmask;
74 for (uint8_t i = 0; i < 8; i++) {
75 *clkport |= clkpinmask;
78 //if (fastDigitalRead(SPI_MISO_PIN)) data |= 1;
79 if ((*misoport) & misopinmask) data |= 1;
81 *clkport &= ~clkpinmask;
83 // adjust so SCK is nice
96 //------------------------------------------------------------------------------
97 /** Soft SPI receive */
98 uint8_t spiRec(void) {
100 // no interrupts during byte receive - about 8 us
102 // output pin high - like sending 0XFF
103 fastDigitalWrite(SPI_MOSI_PIN, HIGH);
105 for (uint8_t i = 0; i < 8; i++) {
106 fastDigitalWrite(SPI_SCK_PIN, HIGH);
108 // adjust so SCK is nice
114 if (fastDigitalRead(SPI_MISO_PIN)) data |= 1;
116 fastDigitalWrite(SPI_SCK_PIN, LOW);
122 //------------------------------------------------------------------------------
124 void spiSend(uint8_t data) {
125 // no interrupts during byte send - about 8 us
127 for (uint8_t i = 0; i < 8; i++) {
128 fastDigitalWrite(SPI_SCK_PIN, LOW);
130 fastDigitalWrite(SPI_MOSI_PIN, data & 0X80);
134 fastDigitalWrite(SPI_SCK_PIN, HIGH);
136 // hold SCK high for a few ns
142 fastDigitalWrite(SPI_SCK_PIN, LOW);
146 #endif // SOFTWARE_SPI
147 //------------------------------------------------------------------------------
148 // send command and return error code. Return zero for OK
149 uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
150 // end read if in partialBlockRead mode
156 // wait up to 300 ms if busy
163 for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s);
167 if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0
168 if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA
172 for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++);
175 //------------------------------------------------------------------------------
177 * Determine the size of an SD flash memory card.
179 * \return The number of 512 byte data blocks in the card
180 * or zero if an error occurs.
182 uint32_t Sd2Card::cardSize(void) {
184 if (!readCSD(&csd)) return 0;
185 if (csd.v1.csd_ver == 0) {
186 uint8_t read_bl_len = csd.v1.read_bl_len;
187 uint16_t c_size = (csd.v1.c_size_high << 10)
188 | (csd.v1.c_size_mid << 2) | csd.v1.c_size_low;
189 uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1)
190 | csd.v1.c_size_mult_low;
191 return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7);
192 } else if (csd.v2.csd_ver == 1) {
193 uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16)
194 | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low;
195 return (c_size + 1) << 10;
197 error(SD_CARD_ERROR_BAD_CSD);
201 //------------------------------------------------------------------------------
202 void Sd2Card::chipSelectHigh(void) {
203 digitalWrite(chipSelectPin_, HIGH);
205 //------------------------------------------------------------------------------
206 void Sd2Card::chipSelectLow(void) {
207 digitalWrite(chipSelectPin_, LOW);
209 //------------------------------------------------------------------------------
210 /** Erase a range of blocks.
212 * \param[in] firstBlock The address of the first block in the range.
213 * \param[in] lastBlock The address of the last block in the range.
215 * \note This function requests the SD card to do a flash erase for a
216 * range of blocks. The data on the card after an erase operation is
217 * either 0 or 1, depends on the card vendor. The card must support
218 * single block erase.
220 * \return The value one, true, is returned for success and
221 * the value zero, false, is returned for failure.
223 uint8_t Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
224 if (!eraseSingleBlockEnable()) {
225 error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK);
228 if (type_ != SD_CARD_TYPE_SDHC) {
232 if (cardCommand(CMD32, firstBlock)
233 || cardCommand(CMD33, lastBlock)
234 || cardCommand(CMD38, 0)) {
235 error(SD_CARD_ERROR_ERASE);
238 if (!waitNotBusy(SD_ERASE_TIMEOUT)) {
239 error(SD_CARD_ERROR_ERASE_TIMEOUT);
249 //------------------------------------------------------------------------------
250 /** Determine if card supports single block erase.
252 * \return The value one, true, is returned if single block erase is supported.
253 * The value zero, false, is returned if single block erase is not supported.
255 uint8_t Sd2Card::eraseSingleBlockEnable(void) {
257 return readCSD(&csd) ? csd.v1.erase_blk_en : 0;
259 //------------------------------------------------------------------------------
261 * Initialize an SD flash memory card.
263 * \param[in] sckRateID SPI clock rate selector. See setSckRate().
264 * \param[in] chipSelectPin SD chip select pin number.
266 * \return The value one, true, is returned for success and
267 * the value zero, false, is returned for failure. The reason for failure
268 * can be determined by calling errorCode() and errorData().
270 uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin, int8_t mosiPin, int8_t misoPin, int8_t clockPin) {
272 writeCRC_ = errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0;
273 chipSelectPin_ = chipSelectPin;
276 clockPin_ = clockPin;
277 // 16-bit init start time allows over a minute
278 uint16_t t0 = (uint16_t)millis();
282 pinMode(chipSelectPin_, OUTPUT);
285 if (clockPin != -1) {
286 // use slow bitbang mode
287 pinMode(misoPin_, INPUT);
288 pinMode(mosiPin_, OUTPUT);
289 pinMode(clockPin_, OUTPUT);
290 clkport = portOutputRegister(digitalPinToPort(clockPin_));
291 clkpinmask = digitalPinToBitMask(clockPin_);
292 mosiport = portOutputRegister(digitalPinToPort(mosiPin_));
293 mosipinmask = digitalPinToBitMask(mosiPin_);
294 misoport = portInputRegister(digitalPinToPort(misoPin_));
295 misopinmask = digitalPinToBitMask(misoPin_);
297 pinMode(SPI_MISO_PIN, INPUT);
298 pinMode(SPI_MOSI_PIN, OUTPUT);
299 pinMode(SPI_SCK_PIN, OUTPUT);
302 // SS must be in output mode even it is not chip select
303 pinMode(SS_PIN, OUTPUT);
304 digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
305 // Enable SPI, Master, clock rate f_osc/128
306 SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
307 // clear double speed
308 SPSR &= ~(1 << SPI2X);
309 #endif // SOFTWARE_SPI
312 // must supply min of 74 clock cycles with CS high.
313 for (uint8_t i = 0; i < 10; i++) spiSend(0XFF);
317 // command to go idle in SPI mode
318 while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
319 if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
320 error(SD_CARD_ERROR_CMD0);
325 if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
326 type(SD_CARD_TYPE_SD1);
328 // only need last byte of r7 response
329 for (uint8_t i = 0; i < 4; i++) status_ = spiRec();
330 if (status_ != 0XAA) {
331 error(SD_CARD_ERROR_CMD8);
334 type(SD_CARD_TYPE_SD2);
336 // initialize card and send host supports SDHC if SD2
337 arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0;
339 while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
341 if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
342 error(SD_CARD_ERROR_ACMD41);
346 // if SD2 read OCR register to check for SDHC card
347 if (type() == SD_CARD_TYPE_SD2) {
348 if (cardCommand(CMD58, 0)) {
349 error(SD_CARD_ERROR_CMD58);
352 if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC);
353 // discard rest of ocr - contains allowed voltage range
354 for (uint8_t i = 0; i < 3; i++) spiRec();
360 return setSckRate(sckRateID);
363 #else // SOFTWARE_SPI
365 #endif // SOFTWARE_SPI
371 //------------------------------------------------------------------------------
373 * Enable or disable partial block reads.
375 * Enabling partial block reads improves performance by allowing a block
376 * to be read over the SPI bus as several sub-blocks. Errors may occur
377 * if the time between reads is too long since the SD card may timeout.
378 * The SPI SS line will be held low until the entire block is read or
379 * readEnd() is called.
381 * Use this for applications like the Adafruit Wave Shield.
383 * \param[in] value The value TRUE (non-zero) or FALSE (zero).)
385 void Sd2Card::partialBlockRead(uint8_t value) {
387 partialBlockRead_ = value;
389 //------------------------------------------------------------------------------
391 * Read a 512 byte block from an SD card device.
393 * \param[in] block Logical block to be read.
394 * \param[out] dst Pointer to the location that will receive the data.
396 * \return The value one, true, is returned for success and
397 * the value zero, false, is returned for failure.
399 uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst) {
400 return readData(block, 0, 512, dst);
402 //------------------------------------------------------------------------------
404 * Read part of a 512 byte block from an SD card.
406 * \param[in] block Logical block to be read.
407 * \param[in] offset Number of bytes to skip at start of block
408 * \param[out] dst Pointer to the location that will receive the data.
409 * \param[in] count Number of bytes to read
410 * \return The value one, true, is returned for success and
411 * the value zero, false, is returned for failure.
413 uint8_t Sd2Card::readData(uint32_t block,
414 uint16_t offset, uint16_t count, uint8_t* dst) {
416 if (count == 0) return true;
417 if ((count + offset) > 512) {
420 if (!inBlock_ || block != block_ || offset < offset_) {
422 // use address if not SDHC card
423 if (type()!= SD_CARD_TYPE_SDHC) block <<= 9;
424 if (cardCommand(CMD17, block)) {
425 error(SD_CARD_ERROR_CMD17);
428 if (!waitStartBlock()) {
435 #ifdef OPTIMIZE_HARDWARE_SPI
436 // start first spi transfer
439 // skip data before offset
440 for (;offset_ < offset; offset_++) {
441 while (!(SPSR & (1 << SPIF)));
446 for (uint16_t i = 0; i < n; i++) {
447 while (!(SPSR & (1 << SPIF)));
451 // wait for last byte
452 while (!(SPSR & (1 << SPIF)));
455 #else // OPTIMIZE_HARDWARE_SPI
457 // skip data before offset
458 for (;offset_ < offset; offset_++) {
462 for (uint16_t i = 0; i < count; i++) {
465 #endif // OPTIMIZE_HARDWARE_SPI
468 if (!partialBlockRead_ || offset_ >= 512) {
469 // read rest of data, checksum and set chip select high
478 //------------------------------------------------------------------------------
479 /** Skip remaining data in a block when in partial block read mode. */
480 void Sd2Card::readEnd(void) {
483 #ifdef OPTIMIZE_HARDWARE_SPI
484 // optimize skip for hardware
486 while (offset_++ < 513) {
487 while (!(SPSR & (1 << SPIF)));
490 // Wait for last CRC byte
491 while (!(SPSR & (1 << SPIF)));
492 #else // OPTIMIZE_HARDWARE_SPI
493 while (offset_++ < 514) spiRec();
494 #endif // OPTIMIZE_HARDWARE_SPI
499 //------------------------------------------------------------------------------
500 /** read CID or CSR register */
501 uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
502 uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
503 if (cardCommand(cmd, 0)) {
504 error(SD_CARD_ERROR_READ_REG);
507 if (!waitStartBlock()) goto fail;
509 for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec();
510 spiRec(); // get first crc byte
511 spiRec(); // get second crc byte
519 //------------------------------------------------------------------------------
521 * Set the SPI clock rate.
523 * \param[in] sckRateID A value in the range [0, 6].
525 * The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum
526 * SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128
527 * for \a scsRateID = 6.
529 * \return The value one, true, is returned for success and the value zero,
530 * false, is returned for an invalid value of \a sckRateID.
532 uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
534 error(SD_CARD_ERROR_SCK_RATE);
537 // see avr processor datasheet for SPI register bit definitions
538 if ((sckRateID & 1) || sckRateID == 6) {
539 SPSR &= ~(1 << SPI2X);
541 SPSR |= (1 << SPI2X);
543 SPCR &= ~((1 <<SPR1) | (1 << SPR0));
544 SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
545 | (sckRateID & 2 ? (1 << SPR0) : 0);
548 //------------------------------------------------------------------------------
549 // wait for card to go not busy
550 uint8_t Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
551 uint16_t t0 = millis();
553 if (spiRec() == 0XFF) return true;
555 while (((uint16_t)millis() - t0) < timeoutMillis);
558 //------------------------------------------------------------------------------
559 /** Wait for start block token */
560 uint8_t Sd2Card::waitStartBlock(void) {
561 uint16_t t0 = millis();
562 while ((status_ = spiRec()) == 0XFF) {
563 if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
564 error(SD_CARD_ERROR_READ_TIMEOUT);
568 if (status_ != DATA_START_BLOCK) {
569 error(SD_CARD_ERROR_READ);
578 //------------------------------------------------------------------------------
580 * Writes a 512 byte block to an SD card.
582 * \param[in] blockNumber Logical block to be written.
583 * \param[in] src Pointer to the location of the data to be written.
584 * \return The value one, true, is returned for success and
585 * the value zero, false, is returned for failure.
587 uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
588 #if SD_PROTECT_BLOCK_ZERO
589 // don't allow write to first block
590 if (blockNumber == 0) {
591 error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
594 #endif // SD_PROTECT_BLOCK_ZERO
596 // use address if not SDHC card
597 if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
598 if (cardCommand(CMD24, blockNumber)) {
599 error(SD_CARD_ERROR_CMD24);
602 if (!writeData(DATA_START_BLOCK, src)) goto fail;
604 // wait for flash programming to complete
605 if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
606 error(SD_CARD_ERROR_WRITE_TIMEOUT);
609 // response is r2 so get and check two bytes for nonzero
610 if (cardCommand(CMD13, 0) || spiRec()) {
611 error(SD_CARD_ERROR_WRITE_PROGRAMMING);
621 //------------------------------------------------------------------------------
622 /** Write one data block in a multiple block write sequence */
623 uint8_t Sd2Card::writeData(const uint8_t* src) {
624 // wait for previous write to finish
625 if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
626 error(SD_CARD_ERROR_WRITE_MULTIPLE);
630 return writeData(WRITE_MULTIPLE_TOKEN, src);
632 //------------------------------------------------------------------------------
633 // send one block of data for write block or write multiple blocks
634 uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
636 // CRC16 checksum is supposed to be ignored in SPI mode (unless
637 // explicitly enabled) and a dummy value is normally written.
638 // A few funny cards (e.g. Eye-Fi X2) expect a valid CRC anyway.
639 // Call setCRC(true) to enable CRC16 checksum on block writes.
640 // This has a noticeable impact on write speed. :(
644 // CRC16 code via Scott Dattalo www.dattalo.com
645 for(crc=i=0; i<512; i++) {
646 x = ((crc >> 8) ^ src[i]) & 0xff;
648 crc = (crc << 8) ^ (x << 12) ^ (x << 5) ^ x;
651 crc = 0xffff; // Dummy CRC value
654 #ifdef OPTIMIZE_HARDWARE_SPI
656 // send data - optimized loop
659 // send two byte per iteration
660 for (uint16_t i = 0; i < 512; i += 2) {
661 while (!(SPSR & (1 << SPIF)));
663 while (!(SPSR & (1 << SPIF)));
667 // wait for last data byte
668 while (!(SPSR & (1 << SPIF)));
670 #else // OPTIMIZE_HARDWARE_SPI
672 for (uint16_t i = 0; i < 512; i++) {
675 #endif // OPTIMIZE_HARDWARE_SPI
677 spiSend(crc >> 8); // Might be dummy value, that's OK
681 if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
682 error(SD_CARD_ERROR_WRITE);
688 //------------------------------------------------------------------------------
689 /** Start a write multiple blocks sequence.
691 * \param[in] blockNumber Address of first block in sequence.
692 * \param[in] eraseCount The number of blocks to be pre-erased.
694 * \note This function is used with writeData() and writeStop()
695 * for optimized multiple block writes.
697 * \return The value one, true, is returned for success and
698 * the value zero, false, is returned for failure.
700 uint8_t Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
701 #if SD_PROTECT_BLOCK_ZERO
702 // don't allow write to first block
703 if (blockNumber == 0) {
704 error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
707 #endif // SD_PROTECT_BLOCK_ZERO
708 // send pre-erase count
709 if (cardAcmd(ACMD23, eraseCount)) {
710 error(SD_CARD_ERROR_ACMD23);
713 // use address if not SDHC card
714 if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
715 if (cardCommand(CMD25, blockNumber)) {
716 error(SD_CARD_ERROR_CMD25);
725 //------------------------------------------------------------------------------
726 /** End a write multiple blocks sequence.
728 * \return The value one, true, is returned for success and
729 * the value zero, false, is returned for failure.
731 uint8_t Sd2Card::writeStop(void) {
732 if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
733 spiSend(STOP_TRAN_TOKEN);
734 if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
739 error(SD_CARD_ERROR_STOP_TRAN);
744 void Sd2Card::enableCRC(uint8_t mode) {