Embedded · Simulation
Simulated two-ECU CAN network on Zephyr
A sensor ECU (electronic control unit) and a controller ECU run the same C++17 Zephyr firmware as simulated nodes on a Linux virtual CAN bus. No board was involved.
A Python gateway injects bus faults between them, and a 23-test pytest suite checks each reaction against frames captured on the bus and the ECUs' own counters. All 23 tests pass, and pass again with the sensor rewritten in Rust.
The problem
CAN's 15-bit CRC protects bits on the wire. It cannot see a task resending a stale buffer, a frame lost in a queue, a hung thread or a node gone bus-off (disconnected after too many errors).
Firmware has to catch each of those in bounded time and fall back to a defined state. I wanted to measure that without a bench of boards, so both ECUs run as Linux processes (Zephyr's native_sim target) on a virtual CAN bus, in CI as well.
What I built
One C++17 codebase builds both ECUs, with the role chosen by Kconfig. The sensor publishes data every 10 ms and the controller answers with a command every 20 ms. One DBC file defines the messages, and my generators emit the C++ and Rust codecs.
Protected frames carry a 4-bit rolling counter and a CRC-8 that also covers the identifier, so a misrouted frame fails the check. I picked the polynomial by measurement: on the 80-bit codeword, 0x2F detects every triple-bit error and 0x1D misses 362 of 82,160.
A 5 ms supervisor tracks a timeout per message and drives the node state machine. It feeds the watchdog only while every thread has checked in within 300 ms. Bus-off recovery backs off from 100 ms to 1.6 s, then latches FAULT after five failures. A link-time guard makes heap allocation after start-up a fatal error.
A Python gateway between the bus segments drops, delays, duplicates, reorders, corrupts and floods frames. Firmware test hooks freeze a counter, break a CRC, force bus-off or stall a thread.
I then ported the sensor ECU to Rust: a no_std, allocation-free core that takes frames and time as arguments, under a thin SocketCAN adapter.
Results
All timings come from the simulated bus on a desktop OS.
The pytest suite passes 23 of 23. With the sensor's heartbeat suppressed, the SAFE_STATE command came 374.8 ms after the last heartbeat. That is the 350 ms timeout plus at most one 5 ms supervisor tick and one 20 ms command period. A stalled transmit thread ended in a watchdog reset 1,301.6 ms after its last frame.
The controller rejected all 22 frames the gateway corrupted. A flood sweep up to 4,000 unknown frames per second (3,999.4 achieved) dropped nothing.
The firmware application's line coverage under the suite is 90.5%, and the libraries reach 100% under 77 GoogleTests. The Rust sensor passes the same 23 tests against the C++ controller.
Limits and next steps
The bus is simulated. A vcan interface has no bit rate, arbitration or error frames, so error-passive and bus-off come from a modeled controller and the flood tests only the receive path. Jitter figures describe the host's scheduler.
CI builds the firmware for an STM32 Nucleo-F446RE in both roles. I have not run it on a board, so the bxCAN and hardware watchdog paths are untested. The Rust port covers only the sensor.
The next steps would be a run on Nucleo boards over a real bus and a Rust port of the controller.
Ask a question about this project
An AI assistant answers from this site's text. It can be wrong; this page is the source.