Added mutex and more doxygen

This commit is contained in:
dario 2024-06-03 22:49:04 +02:00
parent 9dd2a8da9c
commit 007ed46363
3 changed files with 20 additions and 13 deletions

View File

@ -2,6 +2,8 @@
#define STA_SENSORS_W25Q128_HPP #define STA_SENSORS_W25Q128_HPP
#include <sta/bus/spi/device.hpp> #include <sta/bus/spi/device.hpp>
#include <sta/mutex.hpp>
#include <sta/drivers/w25qxx_defs.hpp> #include <sta/drivers/w25qxx_defs.hpp>
#include <functional> #include <functional>
@ -60,12 +62,14 @@ namespace sta
using DelayUsFunc = void (*)(uint32_t); using DelayUsFunc = void (*)(uint32_t);
/** /**
* @brief Construct a new W25Qxx object * @brief Driver class for the W25QXX flash storage series.
* *
* @param device * @param device A SPI device handle from sta-core.
* @param addrMode * @param delay A microsecond delay function.
* @param addrMode Choose between 24 Bit and 32 Bit addressing.
* @param mutex A mutex for thread safety if the flash chip is used by multiple threads. Defaults to a always free mutex.
*/ */
W25Qxx(SPIDevice * device, DelayUsFunc delay, AddressMode addrMode = AddressMode::_24BIT); W25Qxx(SPIDevice * device, DelayUsFunc delay, AddressMode addrMode = AddressMode::_24BIT, Mutex * mutex = Mutex::ALWAYS_FREE);
/** /**
* @brief Initialize the flash chip. * @brief Initialize the flash chip.
@ -318,6 +322,7 @@ namespace sta
private: private:
SPIDevice * device_; SPIDevice * device_;
DelayUsFunc delay_; DelayUsFunc delay_;
Mutex * mutex_;
ChipState state_; ChipState state_;
AddressMode addrMode_; AddressMode addrMode_;
}; };

View File

@ -33,6 +33,12 @@ namespace sta
// If the written data exceeds the remaining bytes in the page. // If the written data exceeds the remaining bytes in the page.
if (ptr_ + length >= W25QXX_PAGE_SIZE) if (ptr_ + length >= W25QXX_PAGE_SIZE)
{ {
// If the segment is full, stop writing data to it.
if (address_ / W25QXX_SECTOR_SIZE == end_)
{
return;
}
std::memcpy(buffer_ + ptr_, bytes, remaining); std::memcpy(buffer_ + ptr_, bytes, remaining);
flash_->pageProgram(address_, buffer_, W25QXX_PAGE_SIZE); flash_->pageProgram(address_, buffer_, W25QXX_PAGE_SIZE);
@ -43,15 +49,9 @@ namespace sta
address_ += W25QXX_PAGE_SIZE; address_ += W25QXX_PAGE_SIZE;
// If a new sector was started, erase the new sector. // If a new sector has to be started, erase the new sector.
if (address_ % W25QXX_SECTOR_SIZE == 0) 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_); flash_->sectorErase(address_);
} }
} }
@ -81,7 +81,7 @@ namespace sta
template <typename T> template <typename T>
size_t Logger<T>::remaining() size_t Logger<T>::remaining()
{ {
return (end_ * W25QXX_SECTOR_SIZE - address_) / sizeof(T); return (end_ * W25QXX_SECTOR_SIZE - address_) / sizeof(T);
} }
template <typename T> template <typename T>

View File

@ -9,13 +9,15 @@
namespace sta namespace sta
{ {
W25Qxx::W25Qxx(SPIDevice * device, DelayUsFunc delay, AddressMode addrMode /* = AddressMode::_24BIT */) W25Qxx::W25Qxx(SPIDevice * device, DelayUsFunc delay, AddressMode addrMode /* = AddressMode::_24BIT */, Mutex * mutex /* = Mutex::ALWAYS_FREE */)
: device_{device}, : device_{device},
delay_{delay}, delay_{delay},
mutex_{mutex},
state_{ChipState::POWERED_DOWN}, state_{ChipState::POWERED_DOWN},
addrMode_{addrMode} addrMode_{addrMode}
{ {
STA_ASSERT(device != nullptr); STA_ASSERT(device != nullptr);
STA_ASSERT(mutex != nullptr);
} }
uint8_t W25Qxx::init() uint8_t W25Qxx::init()