Updated address restoration

This commit is contained in:
dario 2024-06-24 14:08:51 +02:00
parent cee54f1d8c
commit 80fed4bc44

View File

@ -13,7 +13,7 @@ namespace sta
: flash_{flash}, : flash_{flash},
start_{startSec}, start_{startSec},
end_{endSec}, end_{endSec},
address_{start_ * W25QXX_SECTOR_SIZE}, address_{end_ * W25QXX_SECTOR_SIZE},
buffer_{0x00, }, buffer_{0x00, },
flushed_{false}, flushed_{false},
ptr_ {0} ptr_ {0}
@ -42,7 +42,7 @@ namespace sta
template <typename T> template <typename T>
void Logger<T>::findLast() void Logger<T>::findLast()
{ {
uint32_t left = start_; uint32_t left = 0;
uint32_t right = capacity()-1; uint32_t right = capacity()-1;
uint32_t middle; uint32_t middle;
@ -75,8 +75,8 @@ namespace sta
middle = (left + right) / 2; middle = (left + right) / 2;
ptr_ = (middle * sizeof(T)+1) % W25QXX_PAGE_SIZE; ptr_ = (middle * (sizeof(T)+1)) % W25QXX_PAGE_SIZE;
address_ = (middle * sizeof(T)+1) - ptr_; address_ = (middle * (sizeof(T)+1)) - ptr_;
} }
template <typename T> template <typename T>
@ -139,25 +139,28 @@ namespace sta
template <typename T> template <typename T>
void Logger<T>::clear() void Logger<T>::clear()
{ {
uint32_t left = start_; uint32_t left = 0;
uint32_t right = end_+1; uint32_t right = capacity()-1;
uint32_t middle; uint32_t middle;
// Erase all sectors the binary search would check when trying to find the last written page. // Erase all sectors the binary search would check when trying to find the last written page.
while (left <= right) while (left <= right)
{ {
middle = (left + right) / 2; middle = (left + right) / 2;
flash_->sectorErase(middle * W25QXX_SECTOR_SIZE, true); uint32_t sector = ((middle * (sizeof(T)+1)) / W25QXX_SECTOR_SIZE);
flash_->sectorErase(sector * W25QXX_SECTOR_SIZE, true);
right = middle; right = middle;
if (middle == 0) if (middle == 0)
break; break;
flash_->sectorErase((middle-1) * W25QXX_SECTOR_SIZE, true); flash_->sectorErase((sector-1) * W25QXX_SECTOR_SIZE, true);
} }
middle = (left + right) / 2; middle = (left + right) / 2;
flash_->sectorErase(middle * W25QXX_SECTOR_SIZE, true); uint32_t sector = ((middle * (sizeof(T)+1)) / W25QXX_SECTOR_SIZE);
flash_->sectorErase(sector * W25QXX_SECTOR_SIZE, true);
address_ = start_ * W25QXX_SECTOR_SIZE; address_ = start_ * W25QXX_SECTOR_SIZE;
ptr_ = 0; ptr_ = 0;
@ -179,25 +182,25 @@ namespace sta
template <typename T> template <typename T>
uint32_t Logger<T>::capacity() uint32_t Logger<T>::capacity()
{ {
return ((end_ - start_) * W25QXX_SECTOR_SIZE) / (sizeof(T)+1); return ((end_ - start_) * W25QXX_SECTOR_SIZE - 1) / (sizeof(T)+1) + 1;
} }
template <typename T> template <typename T>
bool Logger<T>::isValid(uint32_t i) bool Logger<T>::isValid(uint32_t i)
{ {
uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * (sizeof(T)+1); uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * (sizeof(T)+1);
// If the requested element is in the cache, read it from there. // If the requested element is in the cache, read it from there.
if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE) if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE)
{ {
uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE; return buffer_[address % W25QXX_PAGE_SIZE] == STA_LOGGER_VALID_DATA_BYTE;
return *ptr == STA_LOGGER_VALID_DATA_BYTE;
} }
else else
{ {
uint8_t buffer[sizeof(T)+1]; uint8_t valid;
flash_->readData(address, buffer, sizeof(T)+1); flash_->readData(address, &valid, 1);
return buffer[0] == STA_LOGGER_VALID_DATA_BYTE;
return valid == STA_LOGGER_VALID_DATA_BYTE;
} }
} }
@ -208,15 +211,20 @@ namespace sta
// If the requested element is in the cache, read it from there. // If the requested element is in the cache, read it from there.
if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE) if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE)
{ {
uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE + 1; T data;
return *reinterpret_cast<T*>(ptr); std::memcpy(&data, buffer_ + address % W25QXX_PAGE_SIZE + 1, sizeof(T));
return data;
} }
else else
{ {
uint8_t buffer[sizeof(T)+1]; uint8_t buffer[sizeof(T)];
flash_->readData(address, buffer, sizeof(T)+1); flash_->readData(address+1, buffer, sizeof(T));
return *reinterpret_cast<T*>(buffer+1);
T data;
std::memcpy(&data, buffer, sizeof(T));
return data;
} }
} }
} // namespace sta } // namespace sta