← ClaudeAtlas

embedded-firmware-misralisted

Write and review bare-metal or RTOS firmware in C for microcontrollers such as STM32, Cortex-M, AVR, ESP32 and RISC-V MCUs. Covers interrupt handler discipline, volatile and atomicity on state shared with an ISR, critical sections, ring buffers, memory-mapped register access, stack bounding, static analysis with cppcheck and clang-tidy, and the MISRA C:2012 rules that prevent real defects. Use when writing or reviewing .c or .h firmware, an IRQ handler, a peripheral driver, a HAL or register header, FreeRTOS or Zephyr task code, when a device works on the bench but fails intermittently in the field, or when a build needs static analysis or MISRA justification.
Premchand006/AI-assistant-skills · ★ 0 · AI & Automation · score 73
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