Skip to content

Simulation Notes

Tanmay Patil edited this page Sep 15, 2026 · 1 revision

Simulation notes

Why a software model at all

No canvas-style circuit simulator (Cirkit Designer, Wokwi, Tinkercad, SimulIDE) emulates the ESP32's TWAI peripheral — Cirkit's own support table lists TWAI/CAN as "not yet supported". The only path that executes the real firmware is Espressif's QEMU fork, which emulates TWAI and bridges to a Linux SocketCAN interface (vcan) — worth doing if you want unmodified-firmware runs, heavier to set up.

So sim/ takes the other honest route: model the bus itself, properly.

What is real in there

can_sim.py builds standard-format frames down to the stuffed bitstream — SOF, 11-bit ID, RTR/IDE/r0, DLC, data, CRC-15 (poly 0x4599), ACK slot, EOF, IFS — and resolves arbitration bitwise with dominant-wins semantics. The ACK slot is driven by every attached node regardless of address filtering, TEC climbs +8 per missing ACK and decays on success, and nodes pass through error-active → error-passive → bus-off → recovery.

The node logic is a line-for-line port of can_node.ino's sendFrame/onFrame — same payload layout, same filter check, same CMD→ACK exchange.

Driving it

  • Buttons on the schematic → POST /press?node=N&btn=B → real frame queued.
  • force collision → POST /clash — both nodes queue before the bus wakes, so arbitration is genuine; watch losers in the event stream.
  • unplug node 254 → POST /plug?node=254&on=0 — then press anything on 253 and watch TEC climb in its badge until bus-off, then auto-recovery.
  • The scope strip under the bus draws the last frame's real stuffed bitstream as a CAN_H/CAN_L differential waveform.
  • python3 can_sim.py (no --serve) runs a scripted headless demo including arbitration and bus-off tests.

Known simplifications

REC, error frames, receive-side CRC checking, identical-ID arbitration into the data field, and the electrical layer are not modeled — see Protocol for the full list. They are choices, not bugs; the events that reach the browser are the same ones the hardware produces.

Endpoints (for poking it yourself)

Method Path Effect
GET / the animated schematic
GET /events SSE stream of every bus event
POST /press?node=253&btn=0 button press
POST /plug?node=254&on=0 detach/reattach a node
POST /clash simultaneous TX on both nodes

Clone this wiki locally