Keep "astyled" elements in SD-related files

This commit is contained in:
Scott Lahteine
2015-10-02 23:15:24 -07:00
parent 2bffd12ebd
commit 4c99327329
14 changed files with 597 additions and 599 deletions

View File

@@ -112,10 +112,9 @@ static uint8_t spiRec() {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/** Soft SPI read data */ /** Soft SPI read data */
static void spiRead(uint8_t* buf, uint16_t nbyte) { static void spiRead(uint8_t* buf, uint16_t nbyte) {
for (uint16_t i = 0; i < nbyte; i++) { for (uint16_t i = 0; i < nbyte; i++)
buf[i] = spiRec(); buf[i] = spiRec();
} }
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/** Soft SPI send byte */ /** Soft SPI send byte */
static void spiSend(uint8_t data) { static void spiSend(uint8_t data) {
@@ -144,10 +143,9 @@ static void spiSend(uint8_t data) {
/** Soft SPI send block */ /** Soft SPI send block */
void spiSendBlock(uint8_t token, const uint8_t* buf) { void spiSendBlock(uint8_t token, const uint8_t* buf) {
spiSend(token); spiSend(token);
for (uint16_t i = 0; i < 512; i++) { for (uint16_t i = 0; i < 512; i++)
spiSend(buf[i]); spiSend(buf[i]);
} }
}
#endif // SOFTWARE_SPI #endif // SOFTWARE_SPI
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// send command and return error code. Return zero for OK // send command and return error code. Return zero for OK
@@ -257,7 +255,6 @@ bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
} }
chipSelectHigh(); chipSelectHigh();
return true; return true;
fail: fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
@@ -322,7 +319,8 @@ bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
// check SD version // check SD version
if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) { if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
type(SD_CARD_TYPE_SD1); type(SD_CARD_TYPE_SD1);
} else { }
else {
// only need last byte of r7 response // only need last byte of r7 response
for (uint8_t i = 0; i < 4; i++) status_ = spiRec(); for (uint8_t i = 0; i < 4; i++) status_ = spiRec();
if (status_ != 0XAA) { if (status_ != 0XAA) {
@@ -384,8 +382,7 @@ bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
if (retryCnt > 0) goto retry; if (retryCnt > 0) goto retry;
goto fail; goto fail;
} }
if (!readData(dst, 512)) if (!readData(dst, 512)) {
{
if (retryCnt > 0) goto retry; if (retryCnt > 0) goto retry;
goto fail; goto fail;
} }
@@ -488,8 +485,7 @@ bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
uint16_t calcCrc = CRC_CCITT(dst, count); uint16_t calcCrc = CRC_CCITT(dst, count);
uint16_t recvCrc = spiRec() << 8; uint16_t recvCrc = spiRec() << 8;
recvCrc |= spiRec(); recvCrc |= spiRec();
if (calcCrc != recvCrc) if (calcCrc != recvCrc) {
{
error(SD_CARD_ERROR_CRC); error(SD_CARD_ERROR_CRC);
goto fail; goto fail;
} }
@@ -501,7 +497,6 @@ bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
#endif #endif
chipSelectHigh(); chipSelectHigh();
return true; return true;
fail: fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
@@ -515,7 +510,6 @@ bool Sd2Card::readRegister(uint8_t cmd, void* buf) {
goto fail; goto fail;
} }
return readData(dst, 16); return readData(dst, 16);
fail: fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
@@ -539,7 +533,6 @@ bool Sd2Card::readStart(uint32_t blockNumber) {
} }
chipSelectHigh(); chipSelectHigh();
return true; return true;
fail: fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
@@ -558,7 +551,6 @@ bool Sd2Card::readStop() {
} }
chipSelectHigh(); chipSelectHigh();
return true; return true;
fail: fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
@@ -592,7 +584,6 @@ bool Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
if (((uint16_t)millis() - t0) >= timeoutMillis) goto fail; if (((uint16_t)millis() - t0) >= timeoutMillis) goto fail;
} }
return true; return true;
fail: fail:
return false; return false;
} }
@@ -626,7 +617,6 @@ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
} }
chipSelectHigh(); chipSelectHigh();
return true; return true;
fail: fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
@@ -644,7 +634,6 @@ bool Sd2Card::writeData(const uint8_t* src) {
if (!writeData(WRITE_MULTIPLE_TOKEN, src)) goto fail; if (!writeData(WRITE_MULTIPLE_TOKEN, src)) goto fail;
chipSelectHigh(); chipSelectHigh();
return true; return true;
fail: fail:
error(SD_CARD_ERROR_WRITE_MULTIPLE); error(SD_CARD_ERROR_WRITE_MULTIPLE);
chipSelectHigh(); chipSelectHigh();
@@ -664,7 +653,6 @@ bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
goto fail; goto fail;
} }
return true; return true;
fail: fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
@@ -695,7 +683,6 @@ bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
} }
chipSelectHigh(); chipSelectHigh();
return true; return true;
fail: fail:
chipSelectHigh(); chipSelectHigh();
return false; return false;
@@ -713,7 +700,6 @@ bool Sd2Card::writeStop() {
if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
chipSelectHigh(); chipSelectHigh();
return true; return true;
fail: fail:
error(SD_CARD_ERROR_STOP_TRAN); error(SD_CARD_ERROR_STOP_TRAN);
chipSelectHigh(); chipSelectHigh();

View File

@@ -68,7 +68,6 @@ bool SdBaseFile::addDirCluster() {
// Increase directory file size by cluster size // Increase directory file size by cluster size
fileSize_ += 512UL << vol_->clusterSizeShift_; fileSize_ += 512UL << vol_->clusterSizeShift_;
return true; return true;
fail: fail:
return false; return false;
} }
@@ -78,7 +77,6 @@ bool SdBaseFile::addDirCluster() {
dir_t* SdBaseFile::cacheDirEntry(uint8_t action) { dir_t* SdBaseFile::cacheDirEntry(uint8_t action) {
if (!vol_->cacheRawBlock(dirBlock_, action)) goto fail; if (!vol_->cacheRawBlock(dirBlock_, action)) goto fail;
return vol_->cache()->dir + dirIndex_; return vol_->cache()->dir + dirIndex_;
fail: fail:
return 0; return 0;
} }
@@ -167,7 +165,6 @@ bool SdBaseFile::createContiguous(SdBaseFile* dirFile,
flags_ |= F_FILE_DIR_DIRTY; flags_ |= F_FILE_DIR_DIRTY;
return sync(); return sync();
fail: fail:
return false; return false;
} }
@@ -191,7 +188,6 @@ bool SdBaseFile::dirEntry(dir_t* dir) {
// copy to caller's struct // copy to caller's struct
memcpy(dir, p, sizeof(dir_t)); memcpy(dir, p, sizeof(dir_t));
return true; return true;
fail: fail:
return false; return false;
} }
@@ -258,7 +254,8 @@ int16_t SdBaseFile::fgets(char* str, int16_t num, char* delim) {
str[n++] = ch; str[n++] = ch;
if (!delim) { if (!delim) {
if (ch == '\n') break; if (ch == '\n') break;
} else { }
else {
if (strchr(delim, ch)) break; if (strchr(delim, ch)) break;
} }
} }
@@ -392,7 +389,8 @@ bool SdBaseFile::make83Name(const char* str, uint8_t* name, const char** ptr) {
if (n == 10) goto fail; // only one dot allowed if (n == 10) goto fail; // only one dot allowed
n = 10; // max index for full 8.3 name n = 10; // max index for full 8.3 name
i = 8; // place for extension i = 8; // place for extension
} else { }
else {
// illegal FAT characters // illegal FAT characters
PGM_P p = PSTR("|<>^+=?/[];,*\"\\"); PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
uint8_t b; uint8_t b;
@@ -406,7 +404,6 @@ bool SdBaseFile::make83Name(const char* str, uint8_t* name, const char** ptr) {
*ptr = str; *ptr = str;
// must have a file name, extension is optional // must have a file name, extension is optional
return name[0] != ' '; return name[0] != ' ';
fail: fail:
return false; return false;
} }
@@ -454,7 +451,6 @@ bool SdBaseFile::mkdir(SdBaseFile* parent, const char* path, bool pFlag) {
sub = parent != &dir1 ? &dir1 : &dir2; sub = parent != &dir1 ? &dir1 : &dir2;
} }
return mkdir(parent, dname); return mkdir(parent, dname);
fail: fail:
return false; return false;
} }
@@ -503,7 +499,8 @@ bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) {
if (parent->isRoot()) { if (parent->isRoot()) {
d.firstClusterLow = 0; d.firstClusterLow = 0;
d.firstClusterHigh = 0; d.firstClusterHigh = 0;
} else { }
else {
d.firstClusterLow = parent->firstCluster_ & 0XFFFF; d.firstClusterLow = parent->firstCluster_ & 0XFFFF;
d.firstClusterHigh = parent->firstCluster_ >> 16; d.firstClusterHigh = parent->firstCluster_ >> 16;
} }
@@ -512,7 +509,6 @@ bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) {
// write first block // write first block
return vol_->cacheFlush(); return vol_->cacheFlush();
fail: fail:
return false; return false;
} }
@@ -609,7 +605,6 @@ bool SdBaseFile::open(SdBaseFile* dirFile, const char* path, uint8_t oflag) {
sub = parent != &dir1 ? &dir1 : &dir2; sub = parent != &dir1 ? &dir1 : &dir2;
} }
return open(parent, dname, oflag); return open(parent, dname, oflag);
fail: fail:
return false; return false;
} }
@@ -641,7 +636,8 @@ bool SdBaseFile::open(SdBaseFile* dirFile,
} }
// done if no entries follow // done if no entries follow
if (p->name[0] == DIR_NAME_FREE) break; if (p->name[0] == DIR_NAME_FREE) break;
} else if (!memcmp(dname, p->name, 11)) { }
else if (!memcmp(dname, p->name, 11)) {
fileFound = true; fileFound = true;
break; break;
} }
@@ -649,14 +645,16 @@ bool SdBaseFile::open(SdBaseFile* dirFile,
if (fileFound) { if (fileFound) {
// don't open existing file if O_EXCL // don't open existing file if O_EXCL
if (oflag & O_EXCL) goto fail; if (oflag & O_EXCL) goto fail;
} else { }
else {
// don't create unless O_CREAT and O_WRITE // don't create unless O_CREAT and O_WRITE
if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) goto fail; if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) goto fail;
if (emptyFound) { if (emptyFound) {
index = dirIndex_; index = dirIndex_;
p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE); p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
if (!p) goto fail; if (!p) goto fail;
} else { }
else {
if (dirFile->type_ == FAT_FILE_TYPE_ROOT_FIXED) goto fail; if (dirFile->type_ == FAT_FILE_TYPE_ROOT_FIXED) goto fail;
// add and zero cluster for dirFile - first cluster is in cache for write // add and zero cluster for dirFile - first cluster is in cache for write
@@ -674,7 +672,8 @@ bool SdBaseFile::open(SdBaseFile* dirFile,
if (dateTime_) { if (dateTime_) {
// call user date/time function // call user date/time function
dateTime_(&p->creationDate, &p->creationTime); dateTime_(&p->creationDate, &p->creationTime);
} else { }
else {
// use default date/time // use default date/time
p->creationDate = FAT_DEFAULT_DATE; p->creationDate = FAT_DEFAULT_DATE;
p->creationTime = FAT_DEFAULT_TIME; p->creationTime = FAT_DEFAULT_TIME;
@@ -688,7 +687,6 @@ bool SdBaseFile::open(SdBaseFile* dirFile,
} }
// open entry in cache // open entry in cache
return openCachedEntry(index, oflag); return openCachedEntry(index, oflag);
fail: fail:
return false; return false;
} }
@@ -731,7 +729,6 @@ bool SdBaseFile::open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag) {
} }
// open cached entry // open cached entry
return openCachedEntry(index & 0XF, oflag); return openCachedEntry(index & 0XF, oflag);
fail: fail:
return false; return false;
} }
@@ -757,10 +754,12 @@ bool SdBaseFile::openCachedEntry(uint8_t dirIndex, uint8_t oflag) {
if (DIR_IS_FILE(p)) { if (DIR_IS_FILE(p)) {
fileSize_ = p->fileSize; fileSize_ = p->fileSize;
type_ = FAT_FILE_TYPE_NORMAL; type_ = FAT_FILE_TYPE_NORMAL;
} else if (DIR_IS_SUBDIR(p)) { }
else if (DIR_IS_SUBDIR(p)) {
if (!vol_->chainSize(firstCluster_, &fileSize_)) goto fail; if (!vol_->chainSize(firstCluster_, &fileSize_)) goto fail;
type_ = FAT_FILE_TYPE_SUBDIR; type_ = FAT_FILE_TYPE_SUBDIR;
} else { }
else {
goto fail; goto fail;
} }
// save open flags for read/write // save open flags for read/write
@@ -771,7 +770,6 @@ bool SdBaseFile::openCachedEntry(uint8_t dirIndex, uint8_t oflag) {
curPosition_ = 0; curPosition_ = 0;
if ((oflag & O_TRUNC) && !truncate(0)) return false; if ((oflag & O_TRUNC) && !truncate(0)) return false;
return oflag & O_AT_END ? seekEnd(0) : true; return oflag & O_AT_END ? seekEnd(0) : true;
fail: fail:
type_ = FAT_FILE_TYPE_CLOSED; type_ = FAT_FILE_TYPE_CLOSED;
return false; return false;
@@ -818,7 +816,6 @@ bool SdBaseFile::openNext(SdBaseFile* dirFile, uint8_t oflag) {
return openCachedEntry(index, oflag); return openCachedEntry(index, oflag);
} }
} }
fail: fail:
return false; return false;
} }
@@ -862,8 +859,9 @@ bool SdBaseFile::openParent(SdBaseFile* dir) {
// '..' is pointer to first cluster of parent. open '../..' to find parent // '..' is pointer to first cluster of parent. open '../..' to find parent
if (p->firstClusterHigh == 0 && p->firstClusterLow == 0) { if (p->firstClusterHigh == 0 && p->firstClusterLow == 0) {
if (!file.openRoot(dir->volume())) goto fail; if (!file.openRoot(dir->volume())) goto fail;
} else { }
if (!file.openCachedEntry(1, O_READ)) goto fail; else if (!file.openCachedEntry(1, O_READ)) {
goto fail;
} }
// search for parent in '../..' // search for parent in '../..'
do { do {
@@ -873,7 +871,6 @@ bool SdBaseFile::openParent(SdBaseFile* dir) {
} while (c != cluster); } while (c != cluster);
// open parent // open parent
return open(&file, file.curPosition() / 32 - 1, O_READ); return open(&file, file.curPosition() / 32 - 1, O_READ);
fail: fail:
return false; return false;
} }
@@ -895,11 +892,13 @@ bool SdBaseFile::openRoot(SdVolume* vol) {
type_ = FAT_FILE_TYPE_ROOT_FIXED; type_ = FAT_FILE_TYPE_ROOT_FIXED;
firstCluster_ = 0; firstCluster_ = 0;
fileSize_ = 32 * vol->rootDirEntryCount(); fileSize_ = 32 * vol->rootDirEntryCount();
} else if (vol->fatType() == 32) { }
else if (vol->fatType() == 32) {
type_ = FAT_FILE_TYPE_ROOT32; type_ = FAT_FILE_TYPE_ROOT32;
firstCluster_ = vol->rootDirStart(); firstCluster_ = vol->rootDirStart();
if (!vol->chainSize(firstCluster_, &fileSize_)) goto fail; if (!vol->chainSize(firstCluster_, &fileSize_)) goto fail;
} else { }
else {
// volume is not initialized, invalid, or FAT12 without support // volume is not initialized, invalid, or FAT12 without support
return false; return false;
} }
@@ -915,7 +914,6 @@ bool SdBaseFile::openRoot(SdVolume* vol) {
dirBlock_ = 0; dirBlock_ = 0;
dirIndex_ = 0; dirIndex_ = 0;
return true; return true;
fail: fail:
return false; return false;
} }
@@ -1060,14 +1058,16 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
offset = curPosition_ & 0X1FF; // offset in block offset = curPosition_ & 0X1FF; // offset in block
if (type_ == FAT_FILE_TYPE_ROOT_FIXED) { if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
block = vol_->rootDirStart() + (curPosition_ >> 9); block = vol_->rootDirStart() + (curPosition_ >> 9);
} else { }
else {
uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_); uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
if (offset == 0 && blockOfCluster == 0) { if (offset == 0 && blockOfCluster == 0) {
// start of new cluster // start of new cluster
if (curPosition_ == 0) { if (curPosition_ == 0) {
// use first cluster in file // use first cluster in file
curCluster_ = firstCluster_; curCluster_ = firstCluster_;
} else { }
else {
// get next cluster from FAT // get next cluster from FAT
if (!vol_->fatGet(curCluster_, &curCluster_)) goto fail; if (!vol_->fatGet(curCluster_, &curCluster_)) goto fail;
} }
@@ -1082,7 +1082,8 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
// no buffering needed if n == 512 // no buffering needed if n == 512
if (n == 512 && block != vol_->cacheBlockNumber()) { if (n == 512 && block != vol_->cacheBlockNumber()) {
if (!vol_->readBlock(block, dst)) goto fail; if (!vol_->readBlock(block, dst)) goto fail;
} else { }
else {
// read block to cache and copy data to caller // read block to cache and copy data to caller
if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail; if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail;
uint8_t* src = vol_->cache()->data + offset; uint8_t* src = vol_->cache()->data + offset;
@@ -1093,7 +1094,6 @@ int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
toRead -= n; toRead -= n;
} }
return nbyte; return nbyte;
fail: fail:
return -1; return -1;
} }
@@ -1166,7 +1166,6 @@ dir_t* SdBaseFile::readDirCache() {
// return pointer to entry // return pointer to entry
return vol_->cache()->dir + i; return vol_->cache()->dir + i;
fail: fail:
return 0; return 0;
} }
@@ -1202,7 +1201,6 @@ bool SdBaseFile::remove() {
// write entry to SD // write entry to SD
return vol_->cacheFlush(); return vol_->cacheFlush();
return true; return true;
fail: fail:
return false; return false;
} }
@@ -1228,7 +1226,6 @@ bool SdBaseFile::remove(SdBaseFile* dirFile, const char* path) {
SdBaseFile file; SdBaseFile file;
if (!file.open(dirFile, path, O_WRITE)) goto fail; if (!file.open(dirFile, path, O_WRITE)) goto fail;
return file.remove(); return file.remove();
fail: fail:
// can't set iostate - static function // can't set iostate - static function
return false; return false;
@@ -1272,7 +1269,8 @@ bool SdBaseFile::rename(SdBaseFile* dirFile, const char* newPath) {
if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) { if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) {
goto restore; goto restore;
} }
} else { }
else {
// don't create missing path prefix components // don't create missing path prefix components
if (!file.mkdir(dirFile, newPath, false)) { if (!file.mkdir(dirFile, newPath, false)) {
goto restore; goto restore;
@@ -1358,7 +1356,6 @@ bool SdBaseFile::rmdir() {
type_ = FAT_FILE_TYPE_NORMAL; type_ = FAT_FILE_TYPE_NORMAL;
flags_ |= O_WRITE; flags_ |= O_WRITE;
return remove(); return remove();
fail: fail:
return false; return false;
} }
@@ -1402,7 +1399,8 @@ bool SdBaseFile::rmRfStar() {
if (f.isSubDir()) { if (f.isSubDir()) {
// recursively delete // recursively delete
if (!f.rmRfStar()) goto fail; if (!f.rmRfStar()) goto fail;
} else { }
else {
// ignore read-only // ignore read-only
f.flags_ |= O_WRITE; f.flags_ |= O_WRITE;
if (!f.remove()) goto fail; if (!f.remove()) goto fail;
@@ -1417,7 +1415,6 @@ bool SdBaseFile::rmRfStar() {
if (!rmdir()) goto fail; if (!rmdir()) goto fail;
} }
return true; return true;
fail: fail:
return false; return false;
} }
@@ -1465,7 +1462,8 @@ bool SdBaseFile::seekSet(uint32_t pos) {
if (nNew < nCur || curPosition_ == 0) { if (nNew < nCur || curPosition_ == 0) {
// must follow chain from first cluster // must follow chain from first cluster
curCluster_ = firstCluster_; curCluster_ = firstCluster_;
} else { }
else {
// advance from curPosition // advance from curPosition
nNew -= nCur; nNew -= nCur;
} }
@@ -1637,7 +1635,6 @@ bool SdBaseFile::timestamp(uint8_t flags, uint16_t year, uint8_t month,
d->lastWriteTime = dirTime; d->lastWriteTime = dirTime;
} }
return vol_->cacheFlush(); return vol_->cacheFlush();
fail: fail:
return false; return false;
} }
@@ -1674,7 +1671,8 @@ bool SdBaseFile::truncate(uint32_t length) {
// free all clusters // free all clusters
if (!vol_->freeChain(firstCluster_)) goto fail; if (!vol_->freeChain(firstCluster_)) goto fail;
firstCluster_ = 0; firstCluster_ = 0;
} else { }
else {
uint32_t toFree; uint32_t toFree;
if (!vol_->fatGet(curCluster_, &toFree)) goto fail; if (!vol_->fatGet(curCluster_, &toFree)) goto fail;
@@ -1739,16 +1737,19 @@ int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
if (firstCluster_ == 0) { if (firstCluster_ == 0) {
// allocate first cluster of file // allocate first cluster of file
if (!addCluster()) goto fail; if (!addCluster()) goto fail;
} else { }
else {
curCluster_ = firstCluster_; curCluster_ = firstCluster_;
} }
} else { }
else {
uint32_t next; uint32_t next;
if (!vol_->fatGet(curCluster_, &next)) goto fail; if (!vol_->fatGet(curCluster_, &next)) goto fail;
if (vol_->isEOC(next)) { if (vol_->isEOC(next)) {
// add cluster if at end of chain // add cluster if at end of chain
if (!addCluster()) goto fail; if (!addCluster()) goto fail;
} else { }
else {
curCluster_ = next; curCluster_ = next;
} }
} }
@@ -1768,13 +1769,15 @@ int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
vol_->cacheSetBlockNumber(0XFFFFFFFF, false); vol_->cacheSetBlockNumber(0XFFFFFFFF, false);
} }
if (!vol_->writeBlock(block, src)) goto fail; if (!vol_->writeBlock(block, src)) goto fail;
} else { }
else {
if (blockOffset == 0 && curPosition_ >= fileSize_) { if (blockOffset == 0 && curPosition_ >= fileSize_) {
// start of new block don't need to read into cache // start of new block don't need to read into cache
if (!vol_->cacheFlush()) goto fail; if (!vol_->cacheFlush()) goto fail;
// set cache dirty and SD address of block // set cache dirty and SD address of block
vol_->cacheSetBlockNumber(block, true); vol_->cacheSetBlockNumber(block, true);
} else { }
else {
// rewrite part of block // rewrite part of block
if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail; if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail;
} }
@@ -1789,7 +1792,8 @@ int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
// update fileSize and insure sync will update dir entry // update fileSize and insure sync will update dir entry
fileSize_ = curPosition_; fileSize_ = curPosition_;
flags_ |= F_FILE_DIR_DIRTY; flags_ |= F_FILE_DIR_DIRTY;
} else if (dateTime_ && nbyte) { }
else if (dateTime_ && nbyte) {
// insure sync will update modified date and time // insure sync will update modified date and time
flags_ |= F_FILE_DIR_DIRTY; flags_ |= F_FILE_DIR_DIRTY;
} }

View File

@@ -55,13 +55,11 @@ int16_t SdFile::write(const void* buf, uint16_t nbyte) {
* Use writeError to check for errors. * Use writeError to check for errors.
*/ */
#if ARDUINO >= 100 #if ARDUINO >= 100
size_t SdFile::write(uint8_t b) size_t SdFile::write(uint8_t b) {
{
return SdBaseFile::write(&b, 1); return SdBaseFile::write(&b, 1);
} }
#else #else
void SdFile::write(uint8_t b) void SdFile::write(uint8_t b) {
{
SdBaseFile::write(&b, 1); SdBaseFile::write(&b, 1);
} }
#endif #endif

View File

@@ -50,7 +50,8 @@ bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
// don't save new start location // don't save new start location
setStart = false; setStart = false;
} else { }
else {
// start at likely place for free cluster // start at likely place for free cluster
bgnCluster = allocSearchStart_; bgnCluster = allocSearchStart_;
@@ -75,7 +76,8 @@ bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
if (f != 0) { if (f != 0) {
// cluster in use try next cluster as bgnCluster // cluster in use try next cluster as bgnCluster
bgnCluster = endCluster + 1; bgnCluster = endCluster + 1;
} else if ((endCluster - bgnCluster + 1) == count) { }
else if ((endCluster - bgnCluster + 1) == count) {
// done - found space // done - found space
break; break;
} }
@@ -99,7 +101,6 @@ bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
if (setStart) allocSearchStart_ = bgnCluster + 1; if (setStart) allocSearchStart_ = bgnCluster + 1;
return true; return true;
fail: fail:
return false; return false;
} }
@@ -119,7 +120,6 @@ bool SdVolume::cacheFlush() {
cacheDirty_ = 0; cacheDirty_ = 0;
} }
return true; return true;
fail: fail:
return false; return false;
} }
@@ -132,7 +132,6 @@ bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
} }
if (dirty) cacheDirty_ = true; if (dirty) cacheDirty_ = true;
return true; return true;
fail: fail:
return false; return false;
} }
@@ -146,7 +145,6 @@ bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
} while (!isEOC(cluster)); } while (!isEOC(cluster));
*size = s; *size = s;
return true; return true;
fail: fail:
return false; return false;
} }
@@ -173,9 +171,11 @@ bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
} }
if (fatType_ == 16) { if (fatType_ == 16) {
lba = fatStartBlock_ + (cluster >> 8); lba = fatStartBlock_ + (cluster >> 8);
} else if (fatType_ == 32) { }
else if (fatType_ == 32) {
lba = fatStartBlock_ + (cluster >> 7); lba = fatStartBlock_ + (cluster >> 7);
} else { }
else {
goto fail; goto fail;
} }
if (lba != cacheBlockNumber_) { if (lba != cacheBlockNumber_) {
@@ -183,11 +183,11 @@ bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
} }
if (fatType_ == 16) { if (fatType_ == 16) {
*value = cacheBuffer_.fat16[cluster & 0XFF]; *value = cacheBuffer_.fat16[cluster & 0XFF];
} else { }
else {
*value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK; *value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK;
} }
return true; return true;
fail: fail:
return false; return false;
} }
@@ -231,22 +231,24 @@ bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
} }
if (fatType_ == 16) { if (fatType_ == 16) {
lba = fatStartBlock_ + (cluster >> 8); lba = fatStartBlock_ + (cluster >> 8);
} else if (fatType_ == 32) { }
else if (fatType_ == 32) {
lba = fatStartBlock_ + (cluster >> 7); lba = fatStartBlock_ + (cluster >> 7);
} else { }
else {
goto fail; goto fail;
} }
if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail; if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
// store entry // store entry
if (fatType_ == 16) { if (fatType_ == 16) {
cacheBuffer_.fat16[cluster & 0XFF] = value; cacheBuffer_.fat16[cluster & 0XFF] = value;
} else { }
else {
cacheBuffer_.fat32[cluster & 0X7F] = value; cacheBuffer_.fat32[cluster & 0X7F] = value;
} }
// mirror second FAT // mirror second FAT
if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_; if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
return true; return true;
fail: fail:
return false; return false;
} }
@@ -268,7 +270,6 @@ bool SdVolume::freeChain(uint32_t cluster) {
} while (!isEOC(cluster)); } while (!isEOC(cluster));
return true; return true;
fail: fail:
return false; return false;
} }
@@ -284,9 +285,11 @@ int32_t SdVolume::freeClusterCount() {
if (fatType_ == 16) { if (fatType_ == 16) {
n = 256; n = 256;
} else if (fatType_ == 32) { }
else if (fatType_ == 32) {
n = 128; n = 128;
} else { }
else {
// put FAT12 here // put FAT12 here
return -1; return -1;
} }
@@ -298,7 +301,8 @@ int32_t SdVolume::freeClusterCount() {
for (uint16_t i = 0; i < n; i++) { for (uint16_t i = 0; i < n; i++) {
if (cacheBuffer_.fat16[i] == 0) free++; if (cacheBuffer_.fat16[i] == 0) free++;
} }
} else { }
else {
for (uint16_t i = 0; i < n; i++) { for (uint16_t i = 0; i < n; i++) {
if (cacheBuffer_.fat32[i] == 0) free++; if (cacheBuffer_.fat32[i] == 0) free++;
} }
@@ -381,6 +385,7 @@ bool SdVolume::init(Sd2Card* dev, uint8_t part) {
// total blocks for FAT16 or FAT32 // total blocks for FAT16 or FAT32
totalBlocks = fbs->totalSectors16 ? totalBlocks = fbs->totalSectors16 ?
fbs->totalSectors16 : fbs->totalSectors32; fbs->totalSectors16 : fbs->totalSectors32;
// total data blocks // total data blocks
clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock); clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
@@ -391,14 +396,15 @@ bool SdVolume::init(Sd2Card* dev, uint8_t part) {
if (clusterCount_ < 4085) { if (clusterCount_ < 4085) {
fatType_ = 12; fatType_ = 12;
if (!FAT12_SUPPORT) goto fail; if (!FAT12_SUPPORT) goto fail;
} else if (clusterCount_ < 65525) { }
else if (clusterCount_ < 65525) {
fatType_ = 16; fatType_ = 16;
} else { }
else {
rootDirStart_ = fbs->fat32RootCluster; rootDirStart_ = fbs->fat32RootCluster;
fatType_ = 32; fatType_ = 32;
} }
return true; return true;
fail: fail:
return false; return false;
} }

View File

@@ -154,11 +154,14 @@ class SdVolume {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
bool allocContiguous(uint32_t count, uint32_t* curCluster); bool allocContiguous(uint32_t count, uint32_t* curCluster);
uint8_t blockOfCluster(uint32_t position) const { uint8_t blockOfCluster(uint32_t position) const {
return (position >> 9) & (blocksPerCluster_ - 1);} return (position >> 9) & (blocksPerCluster_ - 1);
}
uint32_t clusterStartBlock(uint32_t cluster) const { uint32_t clusterStartBlock(uint32_t cluster) const {
return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);} return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);
}
uint32_t blockNumber(uint32_t cluster, uint32_t position) const { uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
return clusterStartBlock(cluster) + blockOfCluster(position);} return clusterStartBlock(cluster) + blockOfCluster(position);
}
cache_t* cache() {return &cacheBuffer_;} cache_t* cache() {return &cacheBuffer_;}
uint32_t cacheBlockNumber() {return cacheBlockNumber_;} uint32_t cacheBlockNumber() {return cacheBlockNumber_;}
#if USE_MULTIPLE_CARDS #if USE_MULTIPLE_CARDS
@@ -187,7 +190,8 @@ class SdVolume {
return cluster >= FAT32EOC_MIN; return cluster >= FAT32EOC_MIN;
} }
bool readBlock(uint32_t block, uint8_t* dst) { bool readBlock(uint32_t block, uint8_t* dst) {
return sdCard_->readBlock(block, dst);} return sdCard_->readBlock(block, dst);
}
bool writeBlock(uint32_t block, const uint8_t* dst) { bool writeBlock(uint32_t block, const uint8_t* dst) {
return sdCard_->writeBlock(block, dst); return sdCard_->writeBlock(block, dst);
} }