mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/driver-w25qxxx.git
synced 2025-08-01 20:21:53 +00:00
Added simple logger
This commit is contained in:
parent
0de96367af
commit
8f6eb72ab4
73
include/sta/utils/logger.hpp
Normal file
73
include/sta/utils/logger.hpp
Normal file
@ -0,0 +1,73 @@
|
||||
#ifndef STA_UTILS_LOGGER_HPP
|
||||
#define STA_UTILS_LOGGER_HPP
|
||||
|
||||
#include <sta/drivers/w25qxx.hpp>
|
||||
#include <sta/debug/assert.hpp>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace sta
|
||||
{
|
||||
template <typename T>
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new Logger object
|
||||
*
|
||||
* @param flash
|
||||
* @param startAddr
|
||||
* @param endAddr
|
||||
*/
|
||||
Logger(W25Qxx * flash, uint32_t startSector, uint32_t endSector);
|
||||
|
||||
/**
|
||||
* @brief Write a new data point to the flash chip.
|
||||
* @note If the total capacity of this logger was exceeded, it restarts at the first sector, overwriting its data.
|
||||
*
|
||||
* @param data The data to write to the flash chip.
|
||||
*/
|
||||
void write(T data);
|
||||
|
||||
/**
|
||||
* @brief Clear the flash memory used by the logger.
|
||||
*
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* @brief Get the number of data points currently written to the memory segment.
|
||||
*
|
||||
* @return size_t The number of data points.
|
||||
*/
|
||||
size_t occupied();
|
||||
|
||||
/**
|
||||
* @brief Get the number of data points that can be written to the logger before an overflow occurs.
|
||||
*
|
||||
* @return size_t The number of data points.
|
||||
*/
|
||||
size_t remaining();
|
||||
|
||||
/**
|
||||
* @brief Get the total number of data points that fit into the logger.
|
||||
*
|
||||
* @return size_t The number of data points.
|
||||
*/
|
||||
size_t length();
|
||||
|
||||
const T& operator[](std::size_t idx);
|
||||
private:
|
||||
W25Qxx * flash_;
|
||||
uint32_t start_;
|
||||
uint32_t end_;
|
||||
uint32_t address_;
|
||||
|
||||
uint8_t buffer_[W25QXX_PAGE_SIZE];
|
||||
uint8_t ptr_;
|
||||
};
|
||||
} // namespace sta
|
||||
|
||||
#include <sta/utils/logger.tpp>
|
||||
|
||||
#endif // STA_UTILS_LOGGER_HPP
|
100
include/sta/utils/logger.tpp
Normal file
100
include/sta/utils/logger.tpp
Normal file
@ -0,0 +1,100 @@
|
||||
#ifndef STA_UTILS_LOGGER_TPP
|
||||
#define STA_UTILS_LOGGER_TPP
|
||||
|
||||
#include <sta/debug/debug.hpp>
|
||||
#include <sta/drivers/w25qxx.hpp>
|
||||
|
||||
namespace sta
|
||||
{
|
||||
template <typename T>
|
||||
Logger<T>::Logger(W25Qxx * flash, uint32_t startSec, uint32_t endSec)
|
||||
: flash_{flash},
|
||||
start_{startSec},
|
||||
end_{endSec},
|
||||
address_{start_ * W25QXX_SECTOR_SIZE},
|
||||
buffer_{0x00, },
|
||||
ptr_ {0}
|
||||
{
|
||||
STA_ASSERT(flash != nullptr);
|
||||
STA_ASSERT(endAddr > startAddr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Logger<T>::write(T data)
|
||||
{
|
||||
uint8_t * bytes = (uint8_t*)(&data);
|
||||
uint8_t length = sizeof(data);
|
||||
|
||||
// Bytes remaining until the page is full.
|
||||
uint8_t remaining = W25QXX_PAGE_SIZE - ptr_;
|
||||
|
||||
// If the written data exceeds the remaining bytes in the page.
|
||||
if (ptr_ + length >= W25QXX_PAGE_SIZE)
|
||||
{
|
||||
std::memcpy(buffer_ + ptr_, bytes, remaining);
|
||||
flash_->pageProgram(address_, buffer_, W25QXX_PAGE_SIZE);
|
||||
|
||||
bytes += remaining;
|
||||
length -= remaining;
|
||||
ptr_ = 0;
|
||||
|
||||
address_ += W25QXX_PAGE_SIZE;
|
||||
|
||||
// If a new sector was started, erase the new sector.
|
||||
if (address_ % W25QXX_SECTOR_SIZE == 0)
|
||||
{
|
||||
// If the size of the logger segment was exceeded, restart at the first sector.
|
||||
if (address_ / W25QXX_SECTOR_SIZE == end_)
|
||||
{
|
||||
address_ = start_ * W25QXX_SECTOR_SIZE;
|
||||
}
|
||||
|
||||
flash_->sectorErase(address_);
|
||||
}
|
||||
}
|
||||
|
||||
std::memcpy(buffer_ + ptr_, bytes, length);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Logger<T>::clear()
|
||||
{
|
||||
address_ = start_ * W25QXX_SECTOR_SIZE;
|
||||
ptr_ = 0;
|
||||
|
||||
for (uint32_t i = 0; i < end_ - start_; i++)
|
||||
{
|
||||
flash_->sectorErase((start_ + i) * W25QXX_SECTOR_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
size_t Logger<T>::occupied()
|
||||
{
|
||||
return (address_ - start_ * W25QXX_SECTOR_SIZE) / sizeof(T);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
size_t Logger<T>::remaining()
|
||||
{
|
||||
return (end_ * W25QXX_SECTOR_SIZE - address_) / sizeof(T);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
size_t Logger<T>::length()
|
||||
{
|
||||
return (end_ - start_) * W25QXX_SECTOR_SIZE / sizeof(T);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T& Logger<T>::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);
|
||||
}
|
||||
} // namespace sta
|
||||
|
||||
#endif // STA_UTILS_LOGGER_TPP
|
Loading…
x
Reference in New Issue
Block a user