diff --git a/libraries/SD/src/SD.cpp b/libraries/SD/src/SD.cpp index 65d32741c..d426dbe8e 100644 --- a/libraries/SD/src/SD.cpp +++ b/libraries/SD/src/SD.cpp @@ -326,13 +326,7 @@ boolean callback_rmdir(SdFile& parentDir, char *filePathComponent, return true; } - - -/* Implementation of class used to create `SDCard` object. */ - - - -boolean SDClass::begin(uint8_t csPin) { +boolean SDClass::begin(uint8_t csPin, uint32_t sckRateID) { /* Performs the initialisation required by the sdfatlib library. @@ -340,13 +334,11 @@ boolean SDClass::begin(uint8_t csPin) { Return true if initialization succeeds, false otherwise. */ - return card.init(SPI_HALF_SPEED, csPin) && + return card.init(sckRateID, csPin) && volume.init(card) && root.openRoot(volume); } - - // this little helper is used to traverse paths SdFile SDClass::getParentDir(const char *filepath, int *index) { // get parent directory diff --git a/libraries/SD/src/SD.h b/libraries/SD/src/SD.h index 7435cf577..e229cf8ad 100644 --- a/libraries/SD/src/SD.h +++ b/libraries/SD/src/SD.h @@ -65,8 +65,8 @@ private: public: // This needs to be called to set up the connection to the SD card // before other methods are used. - boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN); - + boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, uint32_t sckRateID = SPI_FULL_SPEED); + // Open the specified file/directory with the supplied mode (e.g. read or // write, etc). Returns a File object for interacting with the file. // Note that currently only one file can be open at a time. diff --git a/libraries/SD/src/utility/Sd2Card.cpp b/libraries/SD/src/utility/Sd2Card.cpp index 98a02ba42..2d7618d65 100644 --- a/libraries/SD/src/utility/Sd2Card.cpp +++ b/libraries/SD/src/utility/Sd2Card.cpp @@ -33,9 +33,13 @@ static void spiSend(uint8_t b) { SPDR = b; while (!(SPSR & (1 << SPIF))) ; +#else +#ifdef ESP8266 + SPI.write(b); #else SPI.transfer(b); #endif +#endif } /** Receive a byte from the card */ static uint8_t spiRec(void) { @@ -116,8 +120,14 @@ uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) { // send command spiSend(cmd | 0x40); +#ifdef ESP8266 + // send argument + SPI.write32(arg, true); +#else // send argument for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s); +#endif + // send CRC uint8_t crc = 0xFF; @@ -424,7 +434,14 @@ uint8_t Sd2Card::readData(uint32_t block, dst[n] = SPDR; #else // OPTIMIZE_HARDWARE_SPI +#ifdef ESP8266 + // skip data before offset + SPI.transferBytes(NULL, NULL, offset_); + // transfer data + SPI.transferBytes(NULL, dst, count); + +#else // skip data before offset for (;offset_ < offset; offset_++) { spiRec(); @@ -433,6 +450,7 @@ uint8_t Sd2Card::readData(uint32_t block, for (uint16_t i = 0; i < count; i++) { dst[i] = spiRec(); } +#endif #endif // OPTIMIZE_HARDWARE_SPI offset_ += count; @@ -463,7 +481,11 @@ void Sd2Card::readEnd(void) { while (!(SPSR & (1 << SPIF))) ; #else // OPTIMIZE_HARDWARE_SPI +#ifdef ESP8266 + SPI.transferBytes(NULL, NULL, (514-offset_)); +#else while (offset_++ < 514) spiRec(); +#endif #endif // OPTIMIZE_HARDWARE_SPI chipSelectHigh(); inBlock_ = 0; @@ -479,7 +501,11 @@ uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) { } if (!waitStartBlock()) goto fail; // transfer data +#ifdef ESP8266 + SPI.transferBytes(NULL, dst, 16); +#else for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec(); +#endif spiRec(); // get first crc byte spiRec(); // get second crc byte chipSelectHigh(); @@ -646,13 +672,21 @@ uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) { #else // OPTIMIZE_HARDWARE_SPI spiSend(token); +#ifdef ESP8266 + // send argument + SPI.writeBytes((uint8_t *)src, 512); +#else for (uint16_t i = 0; i < 512; i++) { spiSend(src[i]); } +#endif #endif // OPTIMIZE_HARDWARE_SPI +#ifdef ESP8266 + SPI.write16(0xFFFF, true); +#else spiSend(0xff); // dummy crc spiSend(0xff); // dummy crc - +#endif status_ = spiRec(); if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) { error(SD_CARD_ERROR_WRITE); diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp index 6b3d29784..be4627740 100644 --- a/libraries/SPI/SPI.cpp +++ b/libraries/SPI/SPI.cpp @@ -216,8 +216,16 @@ void SPIClass::setClockDivider(uint32_t clockDiv) { SPI1CLK = clockDiv; } +inline void SPIClass::setDataBits(uint16_t bits) { + const uint32_t mask = ~((SPIMMOSI << SPILMOSI) | (SPIMMISO << SPILMISO)); + bits--; + SPI1U1 = ((SPI1U1 & mask) | ((bits << SPILMOSI) | (bits << SPILMISO))); +} + uint8_t SPIClass::transfer(uint8_t data) { while(SPI1CMD & SPIBUSY) {} + // reset to 8Bit mode + setDataBits(8); SPI1W0 = data; SPI1CMD |= SPIBUSY; while(SPI1CMD & SPIBUSY) {} @@ -248,19 +256,21 @@ uint16_t SPIClass::transfer16(uint16_t data) { void SPIClass::write(uint8_t data) { while(SPI1CMD & SPIBUSY) {} + // reset to 8Bit mode + setDataBits(8); SPI1W0 = data; SPI1CMD |= SPIBUSY; while(SPI1CMD & SPIBUSY) {} } void SPIClass::write16(uint16_t data) { - write16(data, (SPI1C & (SPICWBO | SPICRBO))); + write16(data, !(SPI1C & (SPICWBO | SPICRBO))); } void SPIClass::write16(uint16_t data, bool msb) { while(SPI1CMD & SPIBUSY) {} // Set to 16Bits transfer - SPI1U1 = (SPI1U1 & ~((SPIMMOSI << SPILMOSI))) | ((16 - 1) << SPILMOSI); + setDataBits(16); if(msb) { // MSBFIRST Byte first SPI1W0 = (data >> 8) | (data << 8); @@ -271,8 +281,31 @@ void SPIClass::write16(uint16_t data, bool msb) { SPI1CMD |= SPIBUSY; } while(SPI1CMD & SPIBUSY) {} - // reset to 8Bit mode - SPI1U1 = (SPI1U1 & ~((SPIMMOSI << SPILMOSI))) | ((8 - 1) << SPILMOSI); +} + +void SPIClass::write32(uint32_t data) { + write32(data, !(SPI1C & (SPICWBO | SPICRBO))); +} + +void SPIClass::write32(uint32_t data, bool msb) { + while(SPI1CMD & SPIBUSY) {} + // Set to 32Bits transfer + setDataBits(32); + if(msb) { + union { + uint32_t l; + uint8_t b[4]; + } data_; + data_.l = data; + // MSBFIRST Byte first + SPI1W0 = (data_.b[3] | (data_.b[2] << 8) | (data_.b[1] << 16) | (data_.b[0] << 24)); + SPI1CMD |= SPIBUSY; + } else { + // LSBFIRST Byte first + SPI1W0 = data; + SPI1CMD |= SPIBUSY; + } + while(SPI1CMD & SPIBUSY) {} } void SPIClass::writeBytes(uint8_t * data, uint32_t size) { @@ -291,7 +324,7 @@ void SPIClass::writeBytes(uint8_t * data, uint32_t size) { void SPIClass::writeBytes_(uint8_t * data, uint8_t size) { while(SPI1CMD & SPIBUSY) {} // Set Bits to transfer - SPI1U1 = (SPI1U1 & ~((SPIMMOSI << SPILMOSI))) | ((size * 8 - 1) << SPILMOSI); + setDataBits(size * 8); volatile uint32_t * fifoPtr = &SPI1W0; uint32_t * dataPtr = (uint32_t*) data; @@ -305,8 +338,6 @@ void SPIClass::writeBytes_(uint8_t * data, uint8_t size) { SPI1CMD |= SPIBUSY; while(SPI1CMD & SPIBUSY) {} - // reset to 8Bit mode - SPI1U1 = (SPI1U1 & ~((SPIMMOSI << SPILMOSI))) | ((8 - 1) << SPILMOSI); } void SPIClass::writePattern(uint8_t * data, uint8_t size, uint32_t repeat) { @@ -344,3 +375,56 @@ void SPIClass::writePattern_(uint8_t * data, uint8_t size, uint8_t repeat) { writeBytes(&buffer[0], bytes); } + +void SPIClass::transferBytes(uint8_t * out, uint8_t * in, uint32_t size) { + while(size) { + if(size > 64) { + transferBytes_(out, in, 64); + size -= 64; + if(out) out += 64; + if(in) in += 64; + } else { + transferBytes_(out, in, size); + size = 0; + } + } +} + +void SPIClass::transferBytes_(uint8_t * out, uint8_t * in, uint8_t size) { + while(SPI1CMD & SPIBUSY) {} + // Set in/out Bits to transfer + + setDataBits(size * 8); + + volatile uint32_t * fifoPtr = &SPI1W0; + uint8_t dataSize = ((size + 3) / 4); + + if(out) { + uint32_t * dataPtr = (uint32_t*) out; + while(dataSize--) { + *fifoPtr = *dataPtr; + dataPtr++; + fifoPtr++; + } + } else { + // no out data only read fill with dummy data! + while(dataSize--) { + *fifoPtr = 0xFFFFFFFF; + fifoPtr++; + } + } + + SPI1CMD |= SPIBUSY; + while(SPI1CMD & SPIBUSY) {} + + if(in) { + volatile uint8_t * fifoPtr8 = (volatile uint8_t *) &SPI1W0; + dataSize = size; + while(dataSize--) { + *in = *fifoPtr8; + in++; + fifoPtr8++; + } + } +} + diff --git a/libraries/SPI/SPI.h b/libraries/SPI/SPI.h index 1030eeaa9..e67b5b0d5 100644 --- a/libraries/SPI/SPI.h +++ b/libraries/SPI/SPI.h @@ -75,13 +75,18 @@ public: void write(uint8_t data); void write16(uint16_t data); void write16(uint16_t data, bool msb); + void write32(uint32_t data); + void write32(uint32_t data, bool msb); void writeBytes(uint8_t * data, uint32_t size); void writePattern(uint8_t * data, uint8_t size, uint32_t repeat); + void transferBytes(uint8_t * out, uint8_t * in, uint32_t size); void endTransaction(void); private: bool useHwCs; void writeBytes_(uint8_t * data, uint8_t size); void writePattern_(uint8_t * data, uint8_t size, uint8_t repeat); + void transferBytes_(uint8_t * out, uint8_t * in, uint8_t size); + inline void setDataBits(uint16_t bits); }; extern SPIClass SPI;