Skip to content

Commit

Permalink
Restructure cache classes to single class
Browse files Browse the repository at this point in the history
  • Loading branch information
greiman committed Dec 26, 2020
1 parent 1535ac2 commit f62283a
Show file tree
Hide file tree
Showing 20 changed files with 360 additions and 372 deletions.
12 changes: 6 additions & 6 deletions examples/RtcTimestampTest/RtcTimestampTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ bool setRtc() {
Serial.print(F("Input: "));
Serial.println(line);

y = strtol(line, &ptr, 0);
y = strtol(line, &ptr, 10);
if (*ptr++ != '-' || y < 2000 || y > 2099) return error("year");
m = strtol(ptr, &ptr, 0);
m = strtol(ptr, &ptr, 10);
if (*ptr++ != '-' || m < 1 || m > 12) return error("month");
d = strtol(ptr, &ptr, 0);
d = strtol(ptr, &ptr, 10);
if (d < 1 || d > 31) return error("day");
hh = strtol(ptr, &ptr, 0);
hh = strtol(ptr, &ptr, 10);
if (*ptr++ != ':' || hh > 23) return error("hour");
mm = strtol(ptr, &ptr, 0);
mm = strtol(ptr, &ptr, 10);
if (*ptr++ != ':' || mm > 59) return error("minute");
ss = strtol(ptr, &ptr, 0);
ss = strtol(ptr, &ptr, 10);
if (ss > 59) return error("second");

rtc.adjust(DateTime(y, m, d, hh, mm, ss));
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SdFat
version=2.0.2
version=2.0.3
license=MIT
author=Bill Greiman <fat16lib@sbcglobal.net>
maintainer=Bill Greiman <fat16lib@sbcglobal.net>
Expand Down
13 changes: 2 additions & 11 deletions src/ExFatLib/ExFatFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,24 +647,15 @@ int ExFatFile::read(void* buf, size_t count) {
ns = maxNs;
}
n = ns << m_vol->bytesPerSectorShift();
// Check for cache sector in read range.
if (sector <= m_vol->dataCacheSector()
&& m_vol->dataCacheSector() < (sector + ns)) {
// Flush cache if cache sector is in the range.
if (!m_vol->dataCacheSync()) {
DBG_FAIL_MACRO;
goto fail;
}
}
if (!m_vol->readSectors(sector, dst, ns)) {
if (!m_vol->cacheSafeRead(sector, dst, ns)) {
DBG_FAIL_MACRO;
goto fail;
}
#endif // USE_MULTI_SECTOR_IO
} else {
// read single sector
n = m_vol->bytesPerSector();
if (!m_vol->readSector(sector, dst)) {
if (!m_vol->cacheSafeRead(sector, dst)) {
DBG_FAIL_MACRO;
goto fail;
}
Expand Down
1 change: 0 additions & 1 deletion src/ExFatLib/ExFatFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ struct ExFatPos_t {
uint64_t position;
/** cluster for position */
uint32_t cluster;
ExFatPos_t() : position(0), cluster(0) {}
};
//------------------------------------------------------------------------------
/**
Expand Down
16 changes: 3 additions & 13 deletions src/ExFatLib/ExFatFileWrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,24 +747,14 @@ size_t ExFatFile::write(const void* buf, size_t nbyte) {
ns = maxNs;
}
n = ns << m_vol->bytesPerSectorShift();
// Check for cache sector in write range.
if (sector <= m_vol->dataCacheSector()
&& m_vol->dataCacheSector() < (sector + ns)) {
// Invalidate cache if cache sector is in the range.
m_vol->dataCacheInvalidate();
}
if (!m_vol->writeSectors(sector, src, ns)) {
DBG_FAIL_MACRO;
if (!m_vol->cacheSafeWrite(sector, src, ns)) {
DBG_FAIL_MACRO;
goto fail;
}
#endif // USE_MULTI_SECTOR_IO
} else {
// use single sector write command
n = m_vol->bytesPerSector();
if (m_vol->dataCacheSector() == sector) {
m_vol->dataCacheInvalidate();
}
if (!m_vol->writeSector(sector, src)) {
if (!m_vol->cacheSafeWrite(sector, src)) {
DBG_FAIL_MACRO;
goto fail;
}
Expand Down
45 changes: 0 additions & 45 deletions src/ExFatLib/ExFatPartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,6 @@
#include "ExFatVolume.h"
#include "../common/FsStructs.h"
//------------------------------------------------------------------------------
uint8_t* FsCache::get(uint32_t sector, uint8_t option) {
if (!m_blockDev) {
DBG_FAIL_MACRO;
goto fail;
}
if (m_sector != sector) {
if (!sync()) {
DBG_FAIL_MACRO;
goto fail;
}
if (!(option & CACHE_OPTION_NO_READ)) {
if (!m_blockDev->readSector(sector, m_cacheBuffer)) {
DBG_FAIL_MACRO;
goto fail;
}
}
m_status = 0;
m_sector = sector;
}
m_status |= option & CACHE_STATUS_MASK;
return m_cacheBuffer;

fail:
return nullptr;
}
//------------------------------------------------------------------------------
void FsCache::invalidate() {
m_status = 0;
m_sector = 0XFFFFFFFF;
}
//------------------------------------------------------------------------------
bool FsCache::sync() {
if (m_status & CACHE_STATUS_DIRTY) {
if (!m_blockDev->writeSector(m_sector, m_cacheBuffer)) {
DBG_FAIL_MACRO;
goto fail;
}
m_status &= ~CACHE_STATUS_DIRTY;
}
return true;

fail:
return false;
}
//==============================================================================
// return 0 if error, 1 if no space, else start cluster.
uint32_t ExFatPartition::bitmapFind(uint32_t cluster, uint32_t count) {
uint32_t start = cluster ? cluster - 2 : m_bitmapStart;
Expand Down
94 changes: 13 additions & 81 deletions src/ExFatLib/ExFatPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,86 +30,14 @@
*/
#include "../common/SysCall.h"
#include "../common/BlockDevice.h"
#include "../common/FsCache.h"
#include "ExFatConfig.h"
#include "ExFatTypes.h"
/** Type for exFAT partition */
const uint8_t FAT_TYPE_EXFAT = 64;

class ExFatFile;
//==============================================================================
/**
* \class FsCache
* \brief Sector cache.
*/
class FsCache {
public:
/** Cached sector is dirty */
static const uint8_t CACHE_STATUS_DIRTY = 1;
/** Cache sector status bits */
static const uint8_t CACHE_STATUS_MASK = CACHE_STATUS_DIRTY;
/** Sync existing sector but do not read new sector. */
static const uint8_t CACHE_OPTION_NO_READ = 2;
/** Cache sector for read. */
static const uint8_t CACHE_FOR_READ = 0;
/** Cache sector for write. */
static const uint8_t CACHE_FOR_WRITE = CACHE_STATUS_DIRTY;
/** Reserve cache sector for write - do not read from sector device. */
static const uint8_t CACHE_RESERVE_FOR_WRITE
= CACHE_STATUS_DIRTY | CACHE_OPTION_NO_READ;

FsCache() : m_blockDev(nullptr) {
invalidate();
}

/** \return Cache sector address. */
uint8_t* cacheBuffer() {
return m_cacheBuffer;
}
/** \return Clear the cache and returns a pointer to the cache. */
uint8_t* clear() {
if (isDirty() && !sync()) {
return nullptr;
}
invalidate();
return m_cacheBuffer;
}
/** Set current sector dirty. */
void dirty() {
m_status |= CACHE_STATUS_DIRTY;
}
/** Initialize the cache.
* \param[in] blockDev Block device for this partition.
*/
void init(BlockDevice* blockDev) {
m_blockDev = blockDev;
invalidate();
}
/** Invalidate current cache sector. */
void invalidate();
/** \return dirty status */
bool isDirty() {
return m_status & CACHE_STATUS_DIRTY;
}
/** \return Logical sector number for cached sector. */
uint32_t sector() {
return m_sector;
}
/** Fill cache with sector data.
* \param[in] sector Sector to read.
* \param[in] option mode for cached sector.
* \return Address of cached sector. */
uint8_t* get(uint32_t sector, uint8_t option);
/** Write current sector if dirty.
* \return true for success or false for failure.
*/
bool sync();

private:
uint8_t m_status;
BlockDevice* m_blockDev;
uint32_t m_sector;
uint8_t m_cacheBuffer[512];
};
//==============================================================================
/**
* \class ExFatPartition
Expand Down Expand Up @@ -233,20 +161,24 @@ class ExFatPartition {
bool syncDevice() {
return m_blockDev->syncDevice();
}
bool cacheSafeRead(uint32_t sector, uint8_t* dst) {
return m_dataCache.cacheSafeRead(sector, dst);
}
bool cacheSafeWrite(uint32_t sector, const uint8_t* src) {
return m_dataCache.cacheSafeWrite(sector, src);
}
bool cacheSafeRead(uint32_t sector, uint8_t* dst, size_t count) {
return m_dataCache.cacheSafeRead(sector, dst, count);
}
bool cacheSafeWrite(uint32_t sector, const uint8_t* src, size_t count) {
return m_dataCache.cacheSafeWrite(sector, src, count);
}
bool readSector(uint32_t sector, uint8_t* dst) {
return m_blockDev->readSector(sector, dst);
}
bool writeSector(uint32_t sector, const uint8_t* src) {
return m_blockDev->writeSector(sector, src);
}
#if USE_MULTI_SECTOR_IO
bool readSectors(uint32_t sector, uint8_t* dst, size_t count) {
return m_blockDev->readSectors(sector, dst, count);
}
bool writeSectors(uint32_t sector, const uint8_t* src, size_t count) {
return m_blockDev->writeSectors(sector, src, count);
}
#endif // USE_MULTI_SECTOR_IO
//----------------------------------------------------------------------------
static const uint8_t m_bytesPerSectorShift = 9;
static const uint16_t m_bytesPerSector = 512;
Expand Down
2 changes: 1 addition & 1 deletion src/FatLib/FatDbg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void FatPartition::dmpFat(print_t* pr, uint32_t start, uint32_t count) {
uint32_t sector = m_fatStartSector + start;
uint32_t cluster = nf*start;
for (uint32_t i = 0; i < count; i++) {
cache_t* pc = cacheFetchFat(sector + i, FatCache::CACHE_FOR_READ);
cache_t* pc = cacheFetchFat(sector + i, FsCache::CACHE_FOR_READ);
if (!pc) {
pr->println(F("cache read failed"));
return;
Expand Down
Loading

0 comments on commit f62283a

Please sign in to comment.