Browse Source

Merge pull request #7 from FrankBoesing/master

Fix writes/reads >=64KB
main
Paul Stoffregen 7 years ago
parent
commit
1f3077c475
2 changed files with 7 additions and 7 deletions
  1. +2
    -2
      utility/SdFat.h
  2. +5
    -5
      utility/SdFile.cpp

+ 2
- 2
utility/SdFat.h View File

uint8_t b; uint8_t b;
return read(&b, 1) == 1 ? b : -1; return read(&b, 1) == 1 ? b : -1;
} }
int16_t read(void* buf, uint16_t nbyte);
int32_t read(void* buf, size_t nbyte);
int8_t readDir(dir_t* dir); int8_t readDir(dir_t* dir);
static uint8_t remove(SdFile* dirFile, const char* fileName); static uint8_t remove(SdFile* dirFile, const char* fileName);
uint8_t remove(void); uint8_t remove(void);
/** \return SdVolume that contains this file. */ /** \return SdVolume that contains this file. */
SdVolume* volume(void) const {return vol_;} SdVolume* volume(void) const {return vol_;}
size_t write(uint8_t b); size_t write(uint8_t b);
size_t write(const void* buf, uint16_t nbyte);
size_t write(const void* buf, size_t nbyte);
size_t write(const char* str); size_t write(const char* str);
void write_P(PGM_P str); void write_P(PGM_P str);
void writeln_P(PGM_P str); void writeln_P(PGM_P str);

+ 5
- 5
utility/SdFile.cpp View File

* read() called before a file has been opened, corrupt file system * read() called before a file has been opened, corrupt file system
* or an I/O error occurred. * or an I/O error occurred.
*/ */
int16_t SdFile::read(void* buf, uint16_t nbyte) {
int32_t SdFile::read(void* buf, size_t nbyte) {
uint8_t* dst = reinterpret_cast<uint8_t*>(buf); uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
// error if not open or write only // error if not open or write only
if (nbyte > (fileSize_ - curPosition_)) nbyte = fileSize_ - curPosition_; if (nbyte > (fileSize_ - curPosition_)) nbyte = fileSize_ - curPosition_;
// amount left to read // amount left to read
uint16_t toRead = nbyte;
uint32_t toRead = nbyte;
while (toRead > 0) { while (toRead > 0) {
uint32_t block; // raw device block number uint32_t block; // raw device block number
uint16_t offset = curPosition_ & 0X1FF; // offset in block uint16_t offset = curPosition_ & 0X1FF; // offset in block
} }
block = vol_->clusterStartBlock(curCluster_) + blockOfCluster; block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
} }
uint16_t n = toRead;
int32_t n = toRead;
// amount to be read from current block // amount to be read from current block
if (n > (512 - offset)) n = 512 - offset; if (n > (512 - offset)) n = 512 - offset;
* for a read-only file, device is full, a corrupt file system or an I/O error. * for a read-only file, device is full, a corrupt file system or an I/O error.
* *
*/ */
size_t SdFile::write(const void* buf, uint16_t nbyte) {
size_t SdFile::write(const void* buf, size_t nbyte) {
// convert void* to uint8_t* - must be before goto statements // convert void* to uint8_t* - must be before goto statements
const uint8_t* src = reinterpret_cast<const uint8_t*>(buf); const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
// number of bytes left to write - must be before goto statements // number of bytes left to write - must be before goto statements
uint16_t nToWrite = nbyte;
size_t nToWrite = nbyte;
// error if not a normal file or is read-only // error if not a normal file or is read-only
if (!isFile() || !(flags_ & O_WRITE)) goto writeErrorReturn; if (!isFile() || !(flags_ & O_WRITE)) goto writeErrorReturn;

Loading…
Cancel
Save