In this lesson, you will learn how to preserve system integrity by periodically saving your program’s vital state to non-volatile memory (NVM), allowing you to recover after a transient fault.
The Logic of Checkpointing
In safety-critical systems, a failure often leaves your variables or state machines in an inconsistent or undefined state. While a watchdog timer (Lesson 18) can force a system reset, the system will often restart from the very beginning, potentially losing hours of progress or violating safety constraints. Checkpointing solves this by capturing a "snapshot" of critical data—such as motor positions, sensor logs, or state machine variables—at reliable intervals.
Implementing the Snapshot
To implement a checkpoint, you must serialize your critical data structures into NVM (like EEPROM or Flash). Crucially, you must ensure this operation is atomic to prevent saving a corrupted state during an interrupted power cycle.
typedef struct {
uint32_t state_id;
float motor_pos;
uint16_t checksum;
} SystemState;
void save_checkpoint(SystemState *data) {
data->checksum = calculate_crc(data); // Validate data integrity
nvm_write(CHECKPOINT_ADDR, data, sizeof(SystemState));
}
The Rollback Mechanism
When the system initializes after a fault or power-up, it performs a self-test. If the current state is deemed invalid, the system enters the Rollback phase. It reads the NVM snapshot, verifies the checksum to ensure the data wasn't corrupted while the system was crashing, and—if valid—restores the saved values into RAM. This brings the system back to the most recent "known good" state rather than a cold start.
Key Considerations
- Frequency: Taking snapshots too often creates wear on Flash memory and increases latency. Taking them too rarely increases the amount of "lost work" during a rollback.
- Double Buffering: Always use two storage slots (Ping-Pong buffering) for your checkpoints. If the system crashes mid-write, you still have the previous, valid checkpoint in the other slot to fall back on.
Key Takeaway: Software checkpointing provides a recovery safety net by periodically persisting critical system state to non-volatile memory, ensuring that a system can resume operations from a known good state rather than suffering a total loss of data upon failure.