knaxuslearn · build · ship

Roadmaps / STM32 / Phase 3

Phase 3 of 4 · Study weeks 9-12

Interfaces, watchdogs and faults

This phase makes the firmware survive bad input and bad code, and leave evidence when it does fail.

Topics every week

  1. Week 98 h
    • UART with DMA and idle-line detection, ring buffers, framing, the hardware CRC unit, a parser with no hardware calls

    Hand inThe parser plus host unit tests (Unity or CppUTest), with the hardware CRC checked against a known vector: polynomial 0x04C11DB7, initial value 0xFFFFFFFF, input and output reflected, final XOR applied in software, so the CRC-32 of 123456789 is 0xCBF43926; the bootloader and host tool use the same settings

  2. Week 108 h
    • Protocol design: lengths, timeouts, back-pressure, resynchronisation
    • fuzzing
    • FDCAN in internal loopback

    Hand inA one-page protocol spec and a fuzz log

  3. Week 118 h
    • Watchdogs: independent against window, who feeds it and when, timer break input, pull-down safe state, reset reasons, brown-out reset level

    Hand inA capture of the stuck-loop reset and the output staying low

  4. Week 128 h
    • Fault anatomy: stacked frame, EXC_RETURN, fault status registers, precise against imprecise bus faults, a record in a .noinit section of main SRAM that survives reset (not CCM SRAM, which an option byte can erase at reset, and check RM0440 for which SRAM has a parity option before using SRAM2), decoding with addr2line

    Hand inCheckpoint 3

Practice projects

  1. Parser fuzz on the laptop: 1 million random frames through the parser compiled for the host with a sanitizer. Done when there are no findings and every rejected frame increments a counter.
  2. Watchdog policy: the watchdog is fed only when the loop, the command task and the telemetry task have each checked in. Done when stopping any one of the three causes a reset within 20 ms of the stall (worst-case timeout: the 10 ms nominal divided by one minus the LSI tolerance, plus the time since the last feed, plus reset time).
  3. Fault gallery: each of the four seeded faults produces a record and a decoded report. Done when a script prints the right fault class and source line for each.

GitHub repos to study

RepoWhat to readWeek
ThrowTheSwitch/UnityThe assertion macros and the test runner generation9
cpputest/cpputestAn alternative host test framework; compare setup cost9
STMicroelectronics/STM32CubeG4Examples/IWDG, Examples/WWDG, Examples/CRC, Examples/UART9, 11
memfault/interrupt_posts: the HardFault debugging and watchdog best-practice posts11, 12

Milestone checkpoint

Add the protocol, FDCAN telemetry, the watchdog policy and the fault recorder. Done when the Checkpoint 3 row passes. Evidence: protocol spec, fuzz log with counts, four decoded fault records, the safe-state capture, ADR-4 (watchdog and safe-state strategy). The independent watchdog runs from a 32 kHz RC oscillator: the datasheet's headline figure is ±5%, and its characteristics table gives a wider range over temperature, so take the worst case from your revision's table. Set the timeout at 10 ms nominal, compute the worst-case timeout from the table, and show in the ADR that it plus the reset time stays under 20 ms.

Check yourself

Answer each question in your head first, then open it to check.

  1. Which errors does a CRC-32 find that a sum of bytes does not?
    Every burst of up to 32 bits and every single-bit error; a byte sum misses reordered bytes and compensating changes.
  2. Why does the parser resynchronise on a start byte plus a length plus a CRC?
    The start byte is only a hint; the length and CRC prove a frame, and a failure restarts the search one byte later.
  3. Why feed the watchdog from one place only, after all checks?
    A watchdog fed from a timer interrupt proves only that the timer works. One feed point after all checks proves the checks passed.
  4. What do CFSR and the stacked PC tell you after a HardFault?
    The fault class (bus, usage, memory) and the stacked program counter, which is the faulting instruction only for precise faults. Check the BFAR or MMFAR valid bits and the stacked link register.
  5. Why is "the watchdog reset the board" not a safe state by itself?
    After reset the pins are inputs; unless the output is pulled to a safe level, it floats.