From 80fed4bc441642aa7e8b0dcedfecfce33981e496 Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 24 Jun 2024 14:08:51 +0200 Subject: [PATCH] Updated address restoration --- include/sta/utils/logger.tpp | 56 ++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/include/sta/utils/logger.tpp b/include/sta/utils/logger.tpp index 93ad67f..d2dd4cd 100644 --- a/include/sta/utils/logger.tpp +++ b/include/sta/utils/logger.tpp @@ -13,7 +13,7 @@ namespace sta : flash_{flash}, start_{startSec}, end_{endSec}, - address_{start_ * W25QXX_SECTOR_SIZE}, + address_{end_ * W25QXX_SECTOR_SIZE}, buffer_{0x00, }, flushed_{false}, ptr_ {0} @@ -42,7 +42,7 @@ namespace sta template void Logger::findLast() { - uint32_t left = start_; + uint32_t left = 0; uint32_t right = capacity()-1; uint32_t middle; @@ -75,8 +75,8 @@ namespace sta middle = (left + right) / 2; - ptr_ = (middle * sizeof(T)+1) % W25QXX_PAGE_SIZE; - address_ = (middle * sizeof(T)+1) - ptr_; + ptr_ = (middle * (sizeof(T)+1)) % W25QXX_PAGE_SIZE; + address_ = (middle * (sizeof(T)+1)) - ptr_; } template @@ -139,25 +139,28 @@ namespace sta template void Logger::clear() { - uint32_t left = start_; - uint32_t right = end_+1; + uint32_t left = 0; + uint32_t right = capacity()-1; uint32_t middle; // Erase all sectors the binary search would check when trying to find the last written page. while (left <= right) { 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; if (middle == 0) break; - - flash_->sectorErase((middle-1) * W25QXX_SECTOR_SIZE, true); + + flash_->sectorErase((sector-1) * W25QXX_SECTOR_SIZE, true); } 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; ptr_ = 0; @@ -179,25 +182,25 @@ namespace sta template uint32_t Logger::capacity() { - return ((end_ - start_) * W25QXX_SECTOR_SIZE) / (sizeof(T)+1); + return ((end_ - start_) * W25QXX_SECTOR_SIZE - 1) / (sizeof(T)+1) + 1; } template bool Logger::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 (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE) - { - uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE; - return *ptr == STA_LOGGER_VALID_DATA_BYTE; + { + return buffer_[address % W25QXX_PAGE_SIZE] == STA_LOGGER_VALID_DATA_BYTE; } else { - uint8_t buffer[sizeof(T)+1]; - flash_->readData(address, buffer, sizeof(T)+1); - return buffer[0] == STA_LOGGER_VALID_DATA_BYTE; + uint8_t valid; + flash_->readData(address, &valid, 1); + + 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 (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE) - { - uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE + 1; - return *reinterpret_cast(ptr); + { + T data; + std::memcpy(&data, buffer_ + address % W25QXX_PAGE_SIZE + 1, sizeof(T)); + return data; } else { - uint8_t buffer[sizeof(T)+1]; - flash_->readData(address, buffer, sizeof(T)+1); - return *reinterpret_cast(buffer+1); + uint8_t buffer[sizeof(T)]; + flash_->readData(address+1, buffer, sizeof(T)); + + T data; + std::memcpy(&data, buffer, sizeof(T)); + + return data; } } } // namespace sta