Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions radio/src/drivers/frftl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,18 @@ static PhysicalPageState getPhysicalPageState(FrFTL* ftl,
static void setPhysicalPageState(FrFTL* ftl, uint16_t physicalPageNo,
PhysicalPageState state)
{
if (state == ERASE_REQUIRED) {
if (ftl->callbacks->isFlashErased(physicalPageNo * PAGE_SIZE)) {
state = ERASED;
}
}
uint32_t idx = physicalPageNo >> 4;
uint32_t mask = 0x3 << ((physicalPageNo & 0xf) * 2);
ftl->physicalPageState[idx] &= ~mask;
ftl->physicalPageState[idx] |=
((state & 0x3) << ((physicalPageNo & 0xf) * 2));
}


static const uint16_t crc16_ccitt_start = 0xFFFF;

static inline uint16_t crc16_x25_ccitt(const void* buf, uint32_t len) {
Expand All @@ -188,12 +192,10 @@ static void resolveUnknownState(FrFTL* ftl, uint16_t count)
uint16_t idx = ftl->writeFrontier;
bool earlyEnd = false;

const FrFTLOps* cb = ftl->callbacks;
for (uint16_t i = 0; i < ftl->physicalPageCount; i++) {
if (getPhysicalPageState(ftl, idx) == UNKNOWN) {
PhysicalPageState state =
cb->isFlashErased(idx * PAGE_SIZE) ? ERASED : ERASE_REQUIRED;
setPhysicalPageState(ftl, idx, state);
// Will detect automatically whether a erase is really required
setPhysicalPageState(ftl, idx, ERASE_REQUIRED);
count--;
if (count == 0) {
earlyEnd = true;
Expand Down Expand Up @@ -549,10 +551,10 @@ static bool quickErase(FrFTL* ftl, uint32_t addr)
{

const FrFTLOps* cb = ftl->callbacks;
uint16_t ppn = addr / PAGE_SIZE;
if ((addr & BLOCK_MASK) == 0) {
// Block aligned

uint16_t ppn = addr / PAGE_SIZE;
uint8_t count = 0;
bool hasUsed = false;

Expand All @@ -578,7 +580,11 @@ static bool quickErase(FrFTL* ftl, uint32_t addr)
return ret;
}
}
return cb->flashErase(addr);
if (cb->flashErase(addr)) {
setPhysicalPageState(ftl, ppn, ERASED);
return true;
}
return false;
}

static bool programPage(FrFTL* ftl, PageBuffer* buffer, bool doErase)
Expand Down Expand Up @@ -691,11 +697,17 @@ static bool lockTTPages(FrFTL* ftl, uint16_t logicalPageNo)
}
ttBuffer =
loadPhysicalPageInBuffer(ftl, ttPageNo, ttPageInfo.physicalPageNo);
if (!ttBuffer) {
return false;
}
ttBuffer->lock = LOCKED;
ttBuffer->pMode = RELOCATE_ERASE_PROGRAM;
if (ttPageNo > 0) {
// TT page not MTT page, need to lock MTT page as well
ttBuffer = loadPhysicalPageInBuffer(ftl, 0, ftl->mttPhysicalPageNo);
if (!ttBuffer) {
return false;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
ttBuffer->lock = LOCKED;
ttBuffer->pMode = RELOCATE_ERASE_PROGRAM;
}
Expand Down Expand Up @@ -777,8 +789,9 @@ bool ftlSync(FrFTL* ftl)
bool ftlWrite(FrFTL* ftl, uint32_t startSectorNo, uint32_t noOfSectors,
const uint8_t* buf)
{
resolveUnknownState(ftl, ftl->ttPageCount);
if (startSectorNo + noOfSectors > ftl->usableSectorCount) {
resolveUnknownState(ftl, ftl->ttPageCount > 16 ? ftl->ttPageCount : 16);
if (startSectorNo > ftl->usableSectorCount ||
noOfSectors > ftl->usableSectorCount - startSectorNo) {
return false;
}

Expand All @@ -798,7 +811,9 @@ bool ftlWrite(FrFTL* ftl, uint32_t startSectorNo, uint32_t noOfSectors,

// Read page info
PageInfo pageInfo;
readPageInfo(ftl, &pageInfo, logicalPageNo);
if (!readPageInfo(ftl, &pageInfo, logicalPageNo)) {
return false;
}
PageBuffer* dataBuffer;

// Allocate new physical page for uninitialized logical page
Expand Down Expand Up @@ -867,7 +882,6 @@ bool ftlWrite(FrFTL* ftl, uint32_t startSectorNo, uint32_t noOfSectors,

bool ftlRead(FrFTL* ftl, uint32_t sectorNo, uint8_t* buffer)
{
// doGC(ftl, ftl->ttPageCount, 1);
if (sectorNo >= ftl->usableSectorCount) {
return false;
}
Expand All @@ -877,7 +891,9 @@ bool ftlRead(FrFTL* ftl, uint32_t sectorNo, uint8_t* buffer)

// Read page info
PageInfo pageInfo;
readPageInfo(ftl, &pageInfo, logicalPageNo);
if (!readPageInfo(ftl, &pageInfo, logicalPageNo)) {
return false;
}

// Check if sector written before
uint8_t sectMask = 1 << pageSectorNo;
Expand All @@ -893,8 +909,9 @@ bool ftlRead(FrFTL* ftl, uint32_t sectorNo, uint8_t* buffer)

bool ftlTrim(FrFTL* ftl, uint32_t startSectorNo, uint32_t noOfSectors)
{
resolveUnknownState(ftl, ftl->ttPageCount);
if (startSectorNo + noOfSectors > ftl->usableSectorCount) {
resolveUnknownState(ftl, ftl->ttPageCount > 16 ? ftl->ttPageCount : 16);
if (startSectorNo > ftl->usableSectorCount ||
noOfSectors > ftl->usableSectorCount - startSectorNo) {
return false;
}

Expand All @@ -914,7 +931,9 @@ bool ftlTrim(FrFTL* ftl, uint32_t startSectorNo, uint32_t noOfSectors)

// Read page info
PageInfo pageInfo;
readPageInfo(ftl, &pageInfo, logicalPageNo);
if (!readPageInfo(ftl, &pageInfo, logicalPageNo)) {
return false;
}

// Check if physical page in used
if (pageInfo.physicalPageNo != 0xffff) {
Expand Down Expand Up @@ -1184,6 +1203,7 @@ bool ftlInit(FrFTL* ftl, const FrFTLOps* cb, uint16_t flashSizeInMB)
memset(ftl->physicalPageState, 0, stateSize * sizeof(uint32_t));
createFTL(ftl);
}

return true;
}

Expand Down
Loading