Faults and recovery: interview questions
6 Faults and recovery questions embedded interviewers ask, each with a model answer. How does firmware find out why the MCU reset, and what are the pitfalls?…
How does firmware find out why the MCU reset, and what are the pitfalls?
It reads the reset-cause register at the start of main: RCC->CSR on an STM32, MCUSR on an AVR, RSTC->SR on a SAM. Each reset source sets its flag: power-on, brown-out, the reset pin, the independent and window watchdogs, a software reset, a low-power-mode reset.
The pitfalls:
- Several flags at once. Every internal reset also drives NRST low, so
PINRSTFis set besideIWDGRSTForSFTRSTF; a power-on reset on an STM32F4 also setsBORRSTF. Decode the most specific flag first and the pin last. - The flags accumulate until cleared (
RMVF, or writing 0 toMCUSR). Clear them after reading, or every later boot reports old causes too. - Something may read them first. A bootloader can clear them before the application runs; it then has to pass the value on (Optiboot leaves
MCUSRin r2). - Read them early. Code that runs before the read may itself cause a reset (a watchdog that is still running from the last boot) or overwrite where you meant to keep them.
What survives a reset, and how do you keep data across one safely?
RAM keeps its contents through every reset that does not remove power: a watchdog, software or pin reset, and a brown-out short enough that the supply stays above the RAM's retention voltage. Registers and peripherals are reset. The C startup code then overwrites .data and .bss, so data meant to survive goes in a section the startup code skips (a NOLOAD section such as .noinit) or at a fixed address outside the program's RAM.
At power-on that RAM holds random values, so every record carries a check: a magic number that says it is a record at all, and an inverted copy or a CRC that says it is complete and unchanged. A record that fails is rebuilt from defaults. Backup registers (in the RTC's backup domain) keep a few words even through a power-on reset as long as VBAT is supplied; flash or EEPROM keeps data through anything, at the cost of wear and write time.
How do you make writes to flash or EEPROM safe against a power failure part way through?
Never overwrite the only copy. Write a new record beside the old one, with a sequence number and a CRC, and write the CRC (or a "valid" marker) last, so that a record interrupted half way fails its check. At boot, scan for the valid record with the highest sequence number. When a sector fills, copy the latest valid record to the other sector before erasing this one, so there is always one intact copy. EEPROM emulation libraries (ST's for STM32, NVS on ESP32) implement this, and wear levelling with it.
Hardware helps: a brown-out detector set above the flash's minimum programming voltage stops a write being attempted at a voltage where it would be unreliable, and a supervisor or a hold-up capacitor can give the firmware time to finish a write when the supply fails.
What is a single event upset, and how is firmware made robust to one?
A bit that changes state in RAM, a register or a latch because a particle (a cosmic-ray neutron, an alpha particle from the package) deposited charge in a transistor. It is not a hardware fault: the cell works again afterwards, but its content is wrong. Rates rise with altitude, smaller process nodes and lower supply voltage.
Chips for safety work have ECC on RAM and flash, correcting single-bit errors and reporting double ones. Without ECC, firmware protects what matters: CRCs over configuration blocks checked periodically and restored from flash, critical variables stored with an inverted copy and compared on use, stack canaries, a CRC check of the flash image (IEC 60730 Class B requires one for household appliance controls), and RAM march tests at boot. Every detected error is logged, and leads to a safe state when it touches a safety function.
Why must a stuck I2C bus be handled by the master's firmware, and how?
Because no reset on the master's side reaches the slave. A slave that was in the middle of sending a byte when the master stopped clocking (it reset, browned out or was halted) holds SDA low and waits for clocks. The bus then never looks idle and no START can be made. The master detects it with timeouts on its I2C waits and by reading SDA as a GPIO, then clocks SCL as a GPIO up to nine times until the slave releases SDA, generates a STOP, and resets its I2C peripheral. If that fails the device has really failed, and only removing its power, through a load switch the board designer provided, will help.
What is fault injection testing for firmware, and how is it done?
Testing that the firmware's recovery code works, by causing the faults it is meant to handle instead of waiting for them. Supply dips and cuts from a programmable supply or a MOSFET switch, resets at random moments from a rig on NRST, RAM corruption written by a debugger or a simulator, devices that stop answering or hold their bus, sensor values out of range, stuck inputs, and blocked tasks to prove the watchdog bites. Each test states the expected reaction (a logged cause, a restored configuration, a recovered bus, a safe state) and checks it. Safety standards (IEC 61508, ISO 26262) expect fault injection as evidence that diagnostics work.