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

View File

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

View File

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

View File

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