Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
DataSaverSPI Class Reference

SPI flash implementation of IDataSaver with timestamp compression. More...

#include <DataSaverSPI.h>

Inheritance diagram for DataSaverSPI:
Inheritance graph
Collaboration diagram for DataSaverSPI:
Collaboration graph

Public Member Functions

 DataSaverSPI (uint16_t timestampInterval_ms, Adafruit_SPIFlash *flash)
 Construct a new DataSaverSPI object.
int saveDataPoint (const DataPoint &dataPoint, uint8_t name) override
 Saves a DataPoint to SPI flash.
int saveTimestamp (uint32_t timestamp_ms)
 Persist a bare timestamp entry to flash.
virtual bool begin () override
 Initialize the flash chip and metadata.
bool isPostLaunchMode ()
 Returns whether the metadata from the flash chip indicates that it contains post-launch data that hasn't been dumped yet. Call clearPostLaunchMode() to clear this flag. once you are confident that you have the data you want dumped to a more permanent storage or another machine.
void clearPostLaunchMode ()
 Clears the post-launch mode flag on the flash chip **WARNING: This will allow the data to be overwritten Only call this after you have dumped the data to a more permanent storage or another machine.
void launchDetected (uint32_t launchTimestamp_ms)
 Call this when launch is detected to set the post-launch flag and prevent any data from being overwritten until the flag is cleared. All data written from this point on is "post-launch" data which is sacred and should not be overwritten until it has been dumped.
void dumpData (Stream &serial, bool ignoreEmptyPages)
 Stream all recorded data to a serial connection.
void clearInternalState ()
 Reset in-memory pointers without erasing flash contents.
void eraseAllData ()
 Erase the entire flash chip to start fresh.
uint32_t getLastTimestamp () const
 Returns the last timestamp that was actually written to flash.
DataPoint getLastDataPoint () const
 Returns the last DataPoint that was written (not necessarily including timestamp, just the data chunk).
uint32_t getLaunchWriteAddress () const
 Returns the launch-protected address computed during launch detection.
uint32_t getNextWriteAddress () const
 Returns the next flash address where a full page will be written.
void resetTimestamp ()
 Resets the timestamp to 0. Can be used to start a new flight during runtime. Useful for testing.
bool quickGetPostLaunchMode ()
 Returns whether the flash chip is in post-launch mode without updating the post-launch mode flag or reading from flash.
size_t getBufferIndex () const
 Returns the current buffer index Useful for testing.
uint32_t getBufferFlushes () const
 Returns the current buffer flush count Useful for testing.
bool getIsChipFullDueToPostLaunchProtection () const
 Returns whether writes have been stopped by post-launch protection.
bool getRebootedInPostLaunchMode () const
 Returns whether startup detected existing post-launch mode metadata.
Public Member Functions inherited from IDataSaver
virtual ~IDataSaver ()=default
virtual int saveDataPoint (float data, uint32_t timestamp_ms, uint8_t name) final
 Convenience overload to construct and save a data point.

Static Public Attributes

static constexpr size_t kBufferSize_bytes = 256

Detailed Description

SPI flash implementation of IDataSaver with timestamp compression.

Note
When to use: onboard non-volatile logging where SD cards are impractical and data may need to survive power loss or long recovery.

Definition at line 40 of file DataSaverSPI.h.

Constructor & Destructor Documentation

◆ DataSaverSPI()

DataSaverSPI::DataSaverSPI ( uint16_t timestampInterval_ms,
Adafruit_SPIFlash * flash )

Construct a new DataSaverSPI object.

Parameters
timestampInterval_msInterval in ms at which timestamps are stored
flashPointer to an initialized Adafruit_SPIFlash object
Note
timestampInterval_ms must be < 65535 (about 1 minute)

Definition at line 7 of file DataSaverSPI.cpp.

Member Function Documentation

◆ begin()

bool DataSaverSPI::begin ( )
overridevirtual

Initialize the flash chip and metadata.

Note
When to use: call during setup before any saveDataPoint usage.
Returns
true when flash is initialized and writable for logging.
false when initialization fails or chip is already in post-launch mode.

Reimplemented from IDataSaver.

Definition at line 172 of file DataSaverSPI.cpp.

◆ clearInternalState()

void DataSaverSPI::clearInternalState ( )

Reset in-memory pointers without erasing flash contents.

Note
When to use: restart logging logic while preserving prior data on the chip.

Definition at line 293 of file DataSaverSPI.cpp.

◆ clearPostLaunchMode()

void DataSaverSPI::clearPostLaunchMode ( )
virtual

Clears the post-launch mode flag on the flash chip **WARNING: This will allow the data to be overwritten Only call this after you have dumped the data to a more permanent storage or another machine.

Reimplemented from IDataSaver.

Definition at line 197 of file DataSaverSPI.cpp.

◆ dumpData()

void DataSaverSPI::dumpData ( Stream & serial,
bool ignoreEmptyPages )

Stream all recorded data to a serial connection.

Parameters
serialOutput stream.
ignoreEmptyPagesSkip pages that appear unwritten.
Note
When to use: post-flight data retrieval before erasing or redeploying the flash chip.

Definition at line 206 of file DataSaverSPI.cpp.

◆ eraseAllData()

void DataSaverSPI::eraseAllData ( )

Erase the entire flash chip to start fresh.

Note
When to use: Never needs to be used in normal operation. After about 1 hour of runtime, the entire chip is overwritten anyways. Can be used in testing to reset the chip to a known state and then test which bytes are written

Definition at line 306 of file DataSaverSPI.cpp.

◆ getBufferFlushes()

uint32_t DataSaverSPI::getBufferFlushes ( ) const
inline

Returns the current buffer flush count Useful for testing.

Returns
Number of successful page flushes.

Definition at line 267 of file DataSaverSPI.h.

◆ getBufferIndex()

size_t DataSaverSPI::getBufferIndex ( ) const
inline

Returns the current buffer index Useful for testing.

Returns
Number of bytes currently buffered.

Definition at line 258 of file DataSaverSPI.h.

◆ getIsChipFullDueToPostLaunchProtection()

bool DataSaverSPI::getIsChipFullDueToPostLaunchProtection ( ) const
inline

Returns whether writes have been stopped by post-launch protection.

Returns
true if chip-full post-launch protection latch is set; otherwise false.

Definition at line 275 of file DataSaverSPI.h.

◆ getLastDataPoint()

DataPoint DataSaverSPI::getLastDataPoint ( ) const
inline

Returns the last DataPoint that was written (not necessarily including timestamp, just the data chunk).

Returns
Copy of the last cached data point.

Definition at line 167 of file DataSaverSPI.h.

◆ getLastTimestamp()

uint32_t DataSaverSPI::getLastTimestamp ( ) const
inline

Returns the last timestamp that was actually written to flash.

Returns
Last persisted timestamp in milliseconds.

Definition at line 158 of file DataSaverSPI.h.

◆ getLaunchWriteAddress()

uint32_t DataSaverSPI::getLaunchWriteAddress ( ) const
inline

Returns the launch-protected address computed during launch detection.

Returns
Address in flash used as post-launch protection boundary.

Definition at line 175 of file DataSaverSPI.h.

◆ getNextWriteAddress()

uint32_t DataSaverSPI::getNextWriteAddress ( ) const
inline

Returns the next flash address where a full page will be written.

Returns
Next write address in flash.

Definition at line 183 of file DataSaverSPI.h.

◆ getRebootedInPostLaunchMode()

bool DataSaverSPI::getRebootedInPostLaunchMode ( ) const
inline

Returns whether startup detected existing post-launch mode metadata.

Returns
true if rebooted in post-launch mode; otherwise false.

Definition at line 283 of file DataSaverSPI.h.

◆ isPostLaunchMode()

bool DataSaverSPI::isPostLaunchMode ( )

Returns whether the metadata from the flash chip indicates that it contains post-launch data that hasn't been dumped yet. Call clearPostLaunchMode() to clear this flag. once you are confident that you have the data you want dumped to a more permanent storage or another machine.

Will also update the post-launch mode flag.

Returns
true if post-launch mode is active
false if post-launch mode is not active

Definition at line 190 of file DataSaverSPI.cpp.

◆ launchDetected()

void DataSaverSPI::launchDetected ( uint32_t launchTimestamp_ms)
virtual

Call this when launch is detected to set the post-launch flag and prevent any data from being overwritten until the flag is cleared. All data written from this point on is "post-launch" data which is sacred and should not be overwritten until it has been dumped.

Sets the post-launch mode flag to true on the flash chip. Records the address at which the launch was detected and ensures that it's not overwritten

Data is saved in a circular fashion but once the address where the launch detected is reached this will stop saving data entirely. Also keeps 1 minute of data from before the launch was detected (b/c launch can be detected late and we have extra room)

The rocket may not be recovered for several hours, this prevents the cool launch data from being overwitten with boring laying-on-the-ground data.

Parameters
launchTimestamp_msTimestamp at which launch was detected.

Reimplemented from IDataSaver.

Definition at line 317 of file DataSaverSPI.cpp.

◆ quickGetPostLaunchMode()

bool DataSaverSPI::quickGetPostLaunchMode ( )
inline

Returns whether the flash chip is in post-launch mode without updating the post-launch mode flag or reading from flash.

Returns
true when in post-launch mode; otherwise false.

Definition at line 200 of file DataSaverSPI.h.

◆ resetTimestamp()

void DataSaverSPI::resetTimestamp ( )
inline

Resets the timestamp to 0. Can be used to start a new flight during runtime. Useful for testing.

Definition at line 191 of file DataSaverSPI.h.

◆ saveDataPoint()

int DataSaverSPI::saveDataPoint ( const DataPoint & dataPoint,
uint8_t name )
overridevirtual

Saves a DataPoint to SPI flash.

If the new data point’s timestamp differs from the last written timestamp by more than timestampInterval_ms_, the new timestamp is also written to flash. Otherwise, the timestamp is omitted (to save space).

Parameters
dataPointThe data point to save
nameAn 8-bit “identifier” for the data point (could be a sensor ID)
Returns
int 0 on success, 1 when writes are blocked by post-launch state, and -1 on write/buffer error.

Implements IDataSaver.

Definition at line 21 of file DataSaverSPI.cpp.

◆ saveTimestamp()

int DataSaverSPI::saveTimestamp ( uint32_t timestamp_ms)

Persist a bare timestamp entry to flash.

Parameters
timestamp_msTimestamp in milliseconds to record.
Note
When to use: emitted internally when gaps exceed timestampInterval_ms_, or explicitly in tests.
Returns
int 0 on success, 1 when writes are blocked by post-launch state, and -1 on non-post-launch write/buffer error.

Definition at line 47 of file DataSaverSPI.cpp.

Member Data Documentation

◆ kBufferSize_bytes

size_t DataSaverSPI::kBufferSize_bytes = 256
staticconstexpr

Definition at line 43 of file DataSaverSPI.h.


The documentation for this class was generated from the following files: