From e8f0bcdf385a41fd8a5fdc065e040a84a92936ca Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 24 Jun 2024 01:25:18 +0200 Subject: [PATCH] Updated logger to add a flag byte --- include/sta/utils/logger.tpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/include/sta/utils/logger.tpp b/include/sta/utils/logger.tpp index 6d8bbb5..d9ee4be 100644 --- a/include/sta/utils/logger.tpp +++ b/include/sta/utils/logger.tpp @@ -100,8 +100,12 @@ namespace sta } // Convert the data to a byte array. - uint8_t * bytes = reinterpret_cast(data); - uint32_t length = sizeof(T); + uint8_t buffer[sizeof(T)+1]; + std::memcpy(buffer+1, data, sizeof(T)); + + // Set the first byte to 0x42 to mark it as a valid data point. + buffer[0] = 0x42; + uint32_t length = sizeof(T)+1; // If the written data exceeds the remaining bytes in the page. while (ptr_ + length >= W25QXX_PAGE_SIZE) @@ -115,7 +119,7 @@ namespace sta flash_->sectorErase(address_); } - std::memcpy(buffer_ + ptr_, bytes, remaining); + std::memcpy(buffer_ + ptr_, buffer, remaining); flash_->pageProgram(address_, buffer_, W25QXX_PAGE_SIZE); bytes += remaining; @@ -124,7 +128,7 @@ namespace sta address_ += W25QXX_PAGE_SIZE; } - std::memcpy(buffer_ + ptr_, bytes, length); + std::memcpy(buffer_ + ptr_, buffer, length); ptr_ += length; return true; @@ -167,37 +171,37 @@ namespace sta template size_t Logger::count() { - return ((address_ + ptr_) - start_ * W25QXX_SECTOR_SIZE) / sizeof(T); + return ((address_ + ptr_) - start_ * W25QXX_SECTOR_SIZE) / (sizeof(T)+1); } template size_t Logger::remaining() { - return (end_ * W25QXX_SECTOR_SIZE - (address_ + ptr_)) / sizeof(T); + return (end_ * W25QXX_SECTOR_SIZE - (address_ + ptr_)) / (sizeof(T)+1); } template size_t Logger::capacity() { - return (end_ - start_) * W25QXX_SECTOR_SIZE / sizeof(T); + return (end_ - start_) * W25QXX_SECTOR_SIZE / (sizeof(T)+1); } template T Logger::get(std::size_t i) { - uint32_t address = start_ * W25QXX_SECTOR_SIZE + i * sizeof(T); + 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; + uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE + 1; return *reinterpret_cast(ptr); } else { - uint8_t buffer[sizeof(T)]; - flash_->readData(address, buffer, sizeof(T)); - return *reinterpret_cast(buffer); + uint8_t buffer[sizeof(T)+1]; + flash_->readData(address, buffer, sizeof(T)+1); + return *reinterpret_cast(buffer+1); } } } // namespace sta