1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-13 13:01:55 +03:00
- optimize SPI usage 148% write speed (24kB/s -> 37kB/s) and 127% read speed (121kB/s -> 155kB/s) at 8MHz
 - add clock frequency as parameter for begin(csPin, frequency)
 - SD @80MHz write: 84kB/s read: 231kB/s

SPI add functions:
 - void write32(uint32_t data);
 - void write32(uint32_t data, bool msb);
 - void transferBytes(uint8_t * out, uint8_t * in, uint32_t size);

Adafruit_ILI9341:
 - code clean
This commit is contained in:
Markus Sattler
2015-05-10 17:34:16 +02:00
parent 539aa19305
commit 27f45a205a
5 changed files with 135 additions and 20 deletions

View File

@ -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

View File

@ -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.

View File

@ -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);