Bladeren bron

Eliminate never-used partial block read

main
PaulStoffregen 8 jaren geleden
bovenliggende
commit
48264458ef
4 gewijzigde bestanden met toevoegingen van 34 en 111 verwijderingen
  1. +28
    -97
      utility/Sd2Card.cpp
  2. +3
    -7
      utility/Sd2Card.h
  3. +1
    -4
      utility/SdFat.h
  4. +2
    -3
      utility/SdFile.cpp

+ 28
- 97
utility/Sd2Card.cpp Bestand weergeven

@@ -170,10 +170,8 @@ static uint8_t spiRec(void) {
//------------------------------------------------------------------------------
// send command and return error code. Return zero for OK
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
// end read if in partialBlockRead mode
readEnd();
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg)
{
// select card
chipSelectLow();
@@ -231,7 +229,7 @@ void Sd2Card::chipSelectLow(void) {
* can be determined by calling errorCode() and errorData().
*/
uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
errorCode_ = inBlock_ = type_ = 0;
errorCode_ = type_ = 0;
chipSelectPin_ = chipSelectPin;
// 16-bit init start time allows over a minute
uint16_t t0 = (uint16_t)millis();
@@ -329,90 +327,49 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst) {
return readData(block, 0, 512, dst);
}
//------------------------------------------------------------------------------
/**
* Read part of a 512 byte block from an SD card.
*
* \param[in] block Logical block to be read.
* \param[in] offset Number of bytes to skip at start of block
* \param[out] dst Pointer to the location that will receive the data.
* \param[in] count Number of bytes to read
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
uint8_t Sd2Card::readData(uint32_t block,
uint16_t offset, uint16_t count, uint8_t* dst) {
#if !defined(USE_TEENSY3_SPI) && defined(OPTIMIZE_HARDWARE_SPI)
uint16_t n;
#endif
if (count == 0) return true;
if ((count + offset) > 512) {
uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst)
{
uint16_t offset=0;
uint16_t count=512;
// use address if not SDHC card
if (type() != SD_CARD_TYPE_SDHC) block <<= 9;
if (cardCommand(CMD17, block)) {
error(SD_CARD_ERROR_CMD17);
goto fail;
}
if (!inBlock_ || block != block_ || offset < offset_) {
block_ = block;
// use address if not SDHC card
if (type()!= SD_CARD_TYPE_SDHC) block <<= 9;
if (cardCommand(CMD17, block)) {
error(SD_CARD_ERROR_CMD17);
goto fail;
}
if (!waitStartBlock()) {
goto fail;
}
offset_ = 0;
inBlock_ = 1;
if (!waitStartBlock()) {
goto fail;
}
#if defined(USE_TEENSY3_SPI)
// skip data before offset
//for (;offset_ < offset; offset_++) {
//spiRec();
//}
spiRecIgnore(offset);
spiRec(dst, count);
spiRecIgnore(2);
#elif defined(OPTIMIZE_HARDWARE_SPI)
// start first spi transfer
SPDR = 0XFF;
// skip data before offset
for (;offset_ < offset; offset_++) {
while (!(SPSR & (1 << SPIF)));
SPDR = 0XFF;
}
// transfer data
n = count - 1;
for (uint16_t i = 0; i < n; i++) {
for (uint16_t i = 0; i < 511; i++) {
while (!(SPSR & (1 << SPIF)));
dst[i] = SPDR;
SPDR = 0XFF;
}
// wait for last byte
while (!(SPSR & (1 << SPIF)));
dst[n] = SPDR;
#else // OPTIMIZE_HARDWARE_SPI
// skip data before offset
for (;offset_ < offset; offset_++) {
spiRec();
}
dst[511] = SPDR;
// skip CRC bytes
SPDR = 0XFF;
while (!(SPSR & (1 << SPIF)));
SPDR = 0XFF;
while (!(SPSR & (1 << SPIF)));
#else // SPI library
// transfer data
for (uint16_t i = 0; i < count; i++) {
for (uint16_t i = 0; i < 512; i++) {
dst[i] = spiRec();
}
#endif // OPTIMIZE_HARDWARE_SPI
offset_ += count;
if (offset_ >= 512) {
// read rest of data, checksum and set chip select high
readEnd();
}
spiRec();
spiRec();
#endif
chipSelectHigh();
return true;
fail:
@@ -420,32 +377,6 @@ uint8_t Sd2Card::readData(uint32_t block,
return false;
}
//------------------------------------------------------------------------------
/** Skip remaining data in a block when in partial block read mode. */
void Sd2Card::readEnd(void) {
if (inBlock_) {
// skip data and crc
#if defined(USE_TEENSY3_SPI)
if (offset_ < 514) {
spiRecIgnore(514 - offset_);
offset_ = 514;
}
#elif defined(OPTIMIZE_HARDWARE_SPI)
// optimize skip for hardware
SPDR = 0XFF;
while (offset_++ < 513) {
while (!(SPSR & (1 << SPIF)));
SPDR = 0XFF;
}
// wait for last crc byte
while (!(SPSR & (1 << SPIF)));
#else // OPTIMIZE_HARDWARE_SPI
while (offset_++ < 514) spiRec();
#endif // OPTIMIZE_HARDWARE_SPI
chipSelectHigh();
inBlock_ = 0;
}
}
//------------------------------------------------------------------------------
/** read CID or CSR register */
uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
uint8_t* dst = reinterpret_cast<uint8_t*>(buf);

+ 3
- 7
utility/Sd2Card.h Bestand weergeven

@@ -127,7 +127,7 @@ uint8_t const SD_CARD_TYPE_SDHC = 3;
class Sd2Card {
public:
/** Construct an instance of Sd2Card. */
Sd2Card(void) : errorCode_(0), inBlock_(0), type_(0) {}
Sd2Card(void) : errorCode_(0), type_(0) {}
/**
* \return error code for last error. See Sd2Card.h for a list of error codes.
*/
@@ -152,8 +152,6 @@ class Sd2Card {
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
/** Returns the current value, true or false, for partial block read. */
uint8_t readBlock(uint32_t block, uint8_t* dst);
uint8_t readData(uint32_t block,
uint16_t offset, uint16_t count, uint8_t* dst);
/**
* Read a cards CID register. The CID contains card identification
* information such as Manufacturer ID, Product name, Product serial
@@ -172,11 +170,8 @@ class Sd2Card {
uint8_t type(void) const {return type_;}
uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src);
private:
uint32_t block_;
uint8_t chipSelectPin_;
uint8_t errorCode_;
uint8_t inBlock_;
uint16_t offset_;
uint8_t status_;
uint8_t type_;
// private functions
@@ -194,6 +189,7 @@ class Sd2Card {
uint8_t waitNotBusy(uint16_t timeoutMillis);
uint8_t writeData(uint8_t token, const uint8_t* src);
uint8_t waitStartBlock(void);
void readEnd(void);
uint8_t readData(uint32_t block,
uint16_t offset, uint16_t count, uint8_t* dst);
};
#endif // Sd2Card_h

+ 1
- 4
utility/SdFat.h Bestand weergeven

@@ -535,10 +535,7 @@ class SdVolume {
return cluster >= (fatType_ == 16 ? FAT16EOC_MIN : FAT32EOC_MIN);
}
uint8_t readBlock(uint32_t block, uint8_t* dst) {
return sdCard_->readBlock(block, dst);}
uint8_t readData(uint32_t block, uint16_t offset,
uint16_t count, uint8_t* dst) {
return sdCard_->readData(block, offset, count, dst);
return sdCard_->readBlock(block, dst);
}
uint8_t writeBlock(uint32_t block, const uint8_t* dst) {
return sdCard_->writeBlock(block, dst);

+ 2
- 3
utility/SdFile.cpp Bestand weergeven

@@ -693,9 +693,8 @@ int16_t SdFile::read(void* buf, uint16_t nbyte) {
if (n > (512 - offset)) n = 512 - offset;
// no buffering needed if n == 512 or user requests no buffering
if ((unbufferedRead() || n == 512) &&
block != SdVolume::cacheBlockNumber_) {
if (!vol_->readData(block, offset, n, dst)) return -1;
if ((unbufferedRead() || n == 512) && block != SdVolume::cacheBlockNumber_) {
if (!vol_->readBlock(block, dst)) return -1;
dst += n;
} else {
// read block to cache and copy data to caller

Laden…
Annuleren
Opslaan