embedded-firmware-misralisted
Install: claude install-skill Premchand006/AI-assistant-skills
# Firmware that survives the field
The bugs that matter here are the ones that depend on when an interrupt lands.
They pass every test on the bench, then fail once a week in a product that has
already shipped. Most of them come from three mistakes: state shared with an
interrupt that is not `volatile`, a read-modify-write on shared state with no
critical section, and a handler that blocks.
```bash
python3 scripts/check_isr_safety.py src/ --fail-on error
./scripts/run_static.sh --misra src/ # adds cppcheck and clang-tidy
```
The checker finds interrupt handlers, works out which globals they share with
the rest of the program, and reports the unsafe shares.
## volatile is not atomicity
`volatile` stops the compiler caching a variable in a register. That is all it
does, and it is not enough for anything that reads then writes:
```c
volatile uint32_t count;
count = 0; /* safe: a single store */
x = count; /* safe: a single load */
count++; /* broken: load, add, store - an interrupt between them loses an update */
```
Three ways out, in order of preference:
```c
/* 1. Make the ISR the only writer. A single-producer single-consumer ring
buffer with a power-of-two mask needs no critical section at all. */
/* ISR: buf[head] = byte; head = (head + 1u) & MASK; */
/* main: while (tail != head) { use(buf[tail]); tail = (tail + 1u) & MASK; } */
/* 2. C11 atomics, where the toolchain has them. */
atomic_fetch_add(&count, 1);
/* 3. The shortest