Deniz Gursoy Electrical Engineering at UPenn · Hardware Design, Robotics, and Embedded Systems
All projects

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.

Dates
Oct 2026
Context
Personal project
Role
Solo
Status
Finished
23 / 23system tests pass, with either the C++ or the Rust sensor
374.8 msfrom last heartbeat to safe-state command: 350 ms timeout, one local run
0 droppedby the controller's receive queue in a flood of up to 3,999.4 unknown frames per second
90.5%line coverage of the firmware application under the system suite

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.

Heartbeat loss on the bus, drawn from the demo's own capture
Timeline of bus frames: SENSOR_HEARTBEAT stops for 701 ms, and the command mode steps to SAFE_STATE 364 ms after the last heartbeat, then to NORMAL 523 ms after it returns.
Stalled transmit thread and watchdog reset, C++ and Rust sensor
Two stacked timelines, C++ sensor above and Rust sensor below: a stalled transmit thread silences SENSOR_DATA for 1,312 ms and 1,323 ms, through the watchdog reset and the reboot.
Transmit periods under the system suite, 10 ms and 20 ms nominal
Three log-scale histograms of transmit periods: SENSOR_DATA from the C++ sensor (std 0.068 ms) and the Rust sensor (std 0.045 ms), and CONTROLLER_COMMAND (std 0.073 ms).
Echo round trip, host to ECU and back, from kernel timestamps
Overlaid histograms of 200 echo round trips per ECU, spread between 0 and 1.2 ms: sensor median 0.55 ms, controller median 0.58 ms.

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.