From fd6d1d61c2ea06cee1ab0bff007f89878ab26210 Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 24 Jun 2024 01:37:33 +0200 Subject: [PATCH] Compiling version, to be tested --- include/sta/utils/logger.hpp | 2 ++ include/sta/utils/logger.tpp | 47 +++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/include/sta/utils/logger.hpp b/include/sta/utils/logger.hpp index 6a7e747..86fe87a 100644 --- a/include/sta/utils/logger.hpp +++ b/include/sta/utils/logger.hpp @@ -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. * diff --git a/include/sta/utils/logger.tpp b/include/sta/utils/logger.tpp index d9ee4be..a81f14a 100644 --- a/include/sta/utils/logger.tpp +++ b/include/sta/utils/logger.tpp @@ -41,30 +41,24 @@ namespace sta void Logger::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 + bool Logger::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 T Logger::get(std::size_t i) {