Compiling version, to be tested

This commit is contained in:
dario 2024-06-24 01:37:33 +02:00
parent e8f0bcdf38
commit fd6d1d61c2
2 changed files with 32 additions and 17 deletions

View File

@ -64,6 +64,8 @@ namespace sta
*/
size_t capacity();
bool isValid(std::size_t i);
/**
* @brief Get the ith element stored in the flash storage.
*

View File

@ -41,30 +41,24 @@ namespace sta
void Logger<T>::findLast()
{
uint32_t left = start_;
uint32_t right = end_+1;
uint32_t right = capacity()-1;
uint32_t middle;
uint8_t * buffer = new uint8_t[256];
while (left <= right)
{
middle = (left + right) / 2;
flash_->readData(middle * W25QXX_SECTOR_SIZE, buffer, W25QXX_PAGE_SIZE);
if (middle == 0 && searchCriterion(buffer))
{
break;
}
if (searchCriterion(buffer))
// Check if we have to look in the upper half of the address space or the lower half.
if (!isValid(middle))
{
// If we reached the first sector, we are finished.
if (middle == 0)
{
break;
}
flash_->readData((middle-1) * W25QXX_SECTOR_SIZE, buffer, W25QXX_PAGE_SIZE);
if (!searchCriterion(buffer))
if (isValid(middle-1))
{
break;
}
@ -78,9 +72,8 @@ namespace sta
}
middle = (left + right) / 2;
delete[] buffer;
address_ = middle * W25QXX_SECTOR_SIZE;
address_ = ((middle * (sizeof(T)+1)) / W25QXX_PAGE_SIZE) * W25QXX_PAGE_SIZE;
ptr_ = 0;
}
@ -105,6 +98,7 @@ namespace sta
// Set the first byte to 0x42 to mark it as a valid data point.
buffer[0] = 0x42;
uint8_t * buff_ptr = buffer;
uint32_t length = sizeof(T)+1;
// If the written data exceeds the remaining bytes in the page.
@ -119,16 +113,16 @@ namespace sta
flash_->sectorErase(address_);
}
std::memcpy(buffer_ + ptr_, buffer, remaining);
std::memcpy(buffer_ + ptr_, buff_ptr, remaining);
flash_->pageProgram(address_, buffer_, W25QXX_PAGE_SIZE);
bytes += remaining;
buff_ptr += remaining;
length -= remaining;
ptr_ = 0;
address_ += W25QXX_PAGE_SIZE;
}
std::memcpy(buffer_ + ptr_, buffer, length);
std::memcpy(buffer_ + ptr_, buff_ptr, length);
ptr_ += length;
return true;
@ -186,6 +180,25 @@ namespace sta
return (end_ - start_) * W25QXX_SECTOR_SIZE / (sizeof(T)+1);
}
template <typename T>
bool Logger<T>::isValid(std::size_t i)
{
uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * (sizeof(T)+1);
// If the requested element is in the cache, read it from there.
if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE)
{
uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE;
return (bool)*ptr;
}
else
{
uint8_t buffer[sizeof(T)+1];
flash_->readData(address, buffer, sizeof(T)+1);
return (bool)*buffer;
}
}
template <typename T>
T Logger<T>::get(std::size_t i)
{