mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/driver-w25qxxx.git
synced 2025-06-10 18:45:59 +00:00
Updated address restoration
This commit is contained in:
parent
cee54f1d8c
commit
80fed4bc44
@ -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 <typename T>
|
||||
void Logger<T>::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 <typename T>
|
||||
@ -139,25 +139,28 @@ namespace sta
|
||||
template <typename T>
|
||||
void Logger<T>::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 <typename T>
|
||||
uint32_t Logger<T>::capacity()
|
||||
{
|
||||
return ((end_ - start_) * W25QXX_SECTOR_SIZE) / (sizeof(T)+1);
|
||||
return ((end_ - start_) * W25QXX_SECTOR_SIZE - 1) / (sizeof(T)+1) + 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Logger<T>::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<T*>(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<T*>(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
|
||||
|
Loading…
x
Reference in New Issue
Block a user