85 virtual bool begin()
override;
159 return lastTimestamp_ms_;
168 return lastDataPoint_;
176 return launchWriteAddress_;
184 return nextWriteAddress_;
192 lastTimestamp_ms_ = 0;
201 return this->postLaunchMode_;
206 uint16_t timestampInterval_ms_;
209 uint32_t lastTimestamp_ms_;
212 uint32_t launchTimestamp_ms_;
221 uint32_t nextWriteAddress_;
224 uint32_t launchWriteAddress_;
226 bool postLaunchMode_;
236 bool writeToFlash(
const uint8_t* data,
size_t length);
245 bool readFromFlash(uint32_t& readAddress, uint8_t* buffer,
size_t length);
249 size_t bufferIndex_ = 0;
250 uint32_t bufferFlushes_ = 0;
268 return bufferFlushes_;
276 return isChipFullDueToPostLaunchProtection_;
284 return rebootedInPostLaunchMode_;
296 void setPostLaunchStateForTest(uint32_t nextWriteAddress_in,
297 uint32_t launchWriteAddress_in,
298 bool postLaunchMode_in) {
300 nextWriteAddress_ = nextWriteAddress_in;
301 launchWriteAddress_ = launchWriteAddress_in;
302 postLaunchMode_ = postLaunchMode_in;
303 isChipFullDueToPostLaunchProtection_ =
false;
313 uint32_t normalizeDataAddress(uint32_t address)
const;
321 bool isProtectedLaunchSector(uint32_t sectorNumber)
const;
326 enum class SectorEraseResult {
328 kProtectedSectorLatched,
340 SectorEraseResult eraseSectorForWriteAndLatchOnProtection(uint32_t sectorNumber);
347 bool shouldStopForPostLaunchWindow();
365 int addDataToBuffer(
const uint8_t* data,
size_t length);
375 int addRecordToBuffer(Record_t * record) {
376 return addDataToBuffer(
reinterpret_cast<const uint8_t*
>(record), 5);
384 int addRecordToBuffer(TimestampRecord_t * record) {
385 return addDataToBuffer(
reinterpret_cast<const uint8_t*
>(record), 5);
390 bool isChipFullDueToPostLaunchProtection_;
394 bool rebootedInPostLaunchMode_ =
false;
398 uint32_t preparedSectorNumber_ = std::numeric_limits<uint32_t>::max();
constexpr uint32_t kDataStartAddress
constexpr uint32_t kMetadataStartAddress
constexpr uint8_t kPostLaunchFlagTrue
constexpr uint32_t kPostLaunchFlagAddress
constexpr uint8_t kPostLaunchFlagFalse
constexpr uint32_t kLaunchStartAddressAddress
constexpr uint8_t kEmptyPageValue
Timestamped float measurement container.
DataSaverSPI(uint16_t timestampInterval_ms, Adafruit_SPIFlash *flash)
Construct a new DataSaverSPI object.
void resetTimestamp()
Resets the timestamp to 0. Can be used to start a new flight during runtime. Useful for testing.
size_t getBufferIndex() const
Returns the current buffer index Useful for testing.
static constexpr size_t kBufferSize_bytes
uint32_t getLaunchWriteAddress() const
Returns the launch-protected address computed during launch detection.
bool getIsChipFullDueToPostLaunchProtection() const
Returns whether writes have been stopped by post-launch protection.
bool quickGetPostLaunchMode()
Returns whether the flash chip is in post-launch mode without updating the post-launch mode flag or r...
uint32_t getNextWriteAddress() const
Returns the next flash address where a full page will be written.
int saveDataPoint(const DataPoint &dataPoint, uint8_t name) override
Saves a DataPoint to SPI flash.
void clearInternalState()
Reset in-memory pointers without erasing flash contents.
uint32_t getLastTimestamp() const
Returns the last timestamp that was actually written to flash.
bool isPostLaunchMode()
Returns whether the metadata from the flash chip indicates that it contains post-launch data that has...
void clearPostLaunchMode()
Clears the post-launch mode flag on the flash chip **WARNING: This will allow the data to be overwrit...
DataPoint getLastDataPoint() const
Returns the last DataPoint that was written (not necessarily including timestamp, just the data chunk...
void eraseAllData()
Erase the entire flash chip to start fresh.
int saveTimestamp(uint32_t timestamp_ms)
Persist a bare timestamp entry to flash.
uint32_t getBufferFlushes() const
Returns the current buffer flush count Useful for testing.
bool getRebootedInPostLaunchMode() const
Returns whether startup detected existing post-launch mode metadata.
void dumpData(Stream &serial, bool ignoreEmptyPages)
Stream all recorded data to a serial connection.
virtual bool begin() override
Initialize the flash chip and metadata.
void launchDetected(uint32_t launchTimestamp_ms)
Call this when launch is detected to set the post-launch flag and prevent any data from being overwri...
Abstract interface for persisting timestamped data points.