From 9dd2a8da9cb660cb75cde3aaea60eb6b0af1ca39 Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 3 Jun 2024 22:23:00 +0200 Subject: [PATCH] Successful reading and writing --- include/sta/utils/logger.hpp | 8 ++++---- include/sta/utils/logger.tpp | 28 +++++++++++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/include/sta/utils/logger.hpp b/include/sta/utils/logger.hpp index a8bcd14..9f1be34 100644 --- a/include/sta/utils/logger.hpp +++ b/include/sta/utils/logger.hpp @@ -13,11 +13,11 @@ namespace sta { public: /** - * @brief Construct a new Logger object + * @brief Constructs a logger object which manages reading and writing data to a segment of a flash chip. * * @param flash The flash chip to use for logging. - * @param startSector The index of the start sector (4096 bytes). - * @param endAddr The index of the end sector (4096 bytes). + * @param startSector The index of the start sector (1 LSB = 4096 bytes). + * @param endSector The index of the end sector (1 LSB = 4096 bytes). */ Logger(W25Qxx * flash, uint32_t startSector, uint32_t endSector); @@ -62,7 +62,7 @@ namespace sta * @param idx The index of the segement. * @return const T& The read data value. */ - const T& operator[](std::size_t idx); + T operator[](std::size_t idx); private: W25Qxx * flash_; uint32_t start_; diff --git a/include/sta/utils/logger.tpp b/include/sta/utils/logger.tpp index 8bc933c..7078895 100644 --- a/include/sta/utils/logger.tpp +++ b/include/sta/utils/logger.tpp @@ -23,11 +23,13 @@ namespace sta void Logger::write(T data) { uint8_t * bytes = (uint8_t*)(&data); - uint8_t length = sizeof(data); + uint8_t length = sizeof(T); // Bytes remaining until the page is full. uint8_t remaining = W25QXX_PAGE_SIZE - ptr_; + STA_DEBUG_PRINTF("Writing at address %d\n", address_+ptr_); + // If the written data exceeds the remaining bytes in the page. if (ptr_ + length >= W25QXX_PAGE_SIZE) { @@ -37,6 +39,7 @@ namespace sta bytes += remaining; length -= remaining; ptr_ = 0; + STA_DEBUG_PRINTLN("New PAGE!"); address_ += W25QXX_PAGE_SIZE; @@ -54,6 +57,7 @@ namespace sta } std::memcpy(buffer_ + ptr_, bytes, length); + ptr_ += length; } template @@ -62,9 +66,9 @@ namespace sta address_ = start_ * W25QXX_SECTOR_SIZE; ptr_ = 0; - for (uint32_t i = 0; i < end_ - start_; i++) + for (uint32_t i = start_; i < end_; i++) { - flash_->sectorErase((start_ + i) * W25QXX_SECTOR_SIZE); + flash_->sectorErase(i * W25QXX_SECTOR_SIZE); } } @@ -87,13 +91,23 @@ namespace sta } template - const T& Logger::operator[](std::size_t idx) + T Logger::operator[](std::size_t idx) { uint32_t address = start_ * W25QXX_SECTOR_SIZE + idx * sizeof(T); - uint8_t buffer[sizeof(T)]; - flash_->readData(address, buffer, sizeof(T)); - return *((T*)buffer); + // If the requested element is in the cache, read it from there. + if (address / W25QXX_PAGE_SIZE == address_ / W25QXX_PAGE_SIZE) + { + STA_DEBUG_PRINTLN("Reading data from buffer!"); + uint8_t * ptr = buffer_ + address % W25QXX_PAGE_SIZE; + return *reinterpret_cast(ptr); + } + else + { + uint8_t buffer[sizeof(T)]; + flash_->readData(address, buffer, sizeof(T)); + return *reinterpret_cast(buffer); + } } } // namespace sta