feat(esp32): SignalRouter — model the GPIO Matrix as first-class
Replaces the per-peripheral ad-hoc `_ledc_gpio_map` cache with a
proper signal-routing abstraction that mirrors the ESP32 SoC's
IO_MUX + GPIO Matrix exactly. Same idea as real silicon: signal
sources (LEDC channels, RMT, MCPWM, ...) → 40-entry routing table
→ GPIO pins.
Motivation (from user bug report in
velxio.dev/project/5218f9e3-136d-43b3-bba1-6cebde21e1a4): two
ESP32 servos on a solar-tracker visibly oscillated between two
positions instead of moving smoothly when the user changed LDR
sliders. Commit 77bf897 patched it (per-channel gpio memo +
broadcast guard) but the user requested a proper hardware-fidel
architecture, not patches.
Backend:
* `app/services/signal_router.py` — SignalRouter class. Forward
index (gpio → signal_id) + reverse index (signal_id → set of
gpios). `replace_snapshot()` returns the diff for the polling-
fallback path; future C plugin hook becomes a push without
touching this code.
* `app/services/esp32_signals.py` — Signal id constants from
ESP32 TRM (LEDC HS 72-79, LS 80-87) + `ledc_signal_for_channel()`
helper.
* `app/services/esp32_worker.py` — `_ledc_gpio_map` is gone;
`_refresh_ledc_gpio_map` replaced by `_refresh_signal_routing`
which emits `gpio_routing {gpio, signal_id}` events on diff.
The 0x5000 LEDC callback and the LEDC poll thread now emit
`ledc_duty {channel, duty_pct}` (canonical, no gpio) alongside
the legacy `ledc_update {channel, duty, gpio}` for back-compat
during rollout.
Frontend:
* `simulation/SignalRouter.ts` — 1-to-1 TS mirror of the Python
class. Same forward + reverse index; same `pinsForSignal` /
`updateRouting` / `clearRouting` API.
* `simulation/esp32-signals.ts` — Signal id constants, mirror
of the Python module.
* `simulation/Esp32Bridge.ts` — new `onLedcDuty`, `onGpioRouting`,
`onGpioRoutingClear` callbacks; handlers for the new event types.
* `store/useSimulatorStore.ts` — `makeLedcDutyHandler` looks up
pins via `router.pinsForSignal(ledcSignalForChannel(channel))`
and dispatches per pin. `makeGpioRoutingHandler` /
`makeGpioRoutingClearHandler` keep the mirror in sync. Per-board
`signalRouterMap` parallels `pinManagerMap` in lifecycle.
`makeLedcUpdateHandler` (and its memo workaround from 77bf897)
stays wired for back-compat during rollout; removed in a
follow-up commit once prod is verified stable on the new path.
Tests:
* `test/backend/unit/test_signal_router.py` (20 tests) covers
update/clear semantics, idempotency, multi-pin routing,
snapshot diff, channel↔signal-id helpers, and the multi-servo
regression scenario.
* `frontend/src/__tests__/SignalRouter.test.ts` (17 tests) is the
mirror — same scenarios on the TS side.
* `frontend/src/__tests__/esp32-multi-servo-gpio-matrix.test.ts`
(6 tests) drives the end-to-end SignalRouter handler pipeline,
asserts that two servos on GPIO 13/12 via LEDC channels 0/1
move independently (no mirroring), that re-routing carries
cleanly, and — critically — that `PinManager.broadcastPwm` is
never called.
Totals: +700 LOC, 1876 frontend tests pass (was 1853), 278 backend
unit tests pass (was 259).
Docs: ESP32_EMULATION.md §9.2 rewritten with the new architecture
diagram + a runbook for adding future peripherals through the
SignalRouter.
The C plugin hook in qemu-lcgamboa that would push gpio_out_sel
writes synchronously (eliminating the polling race window entirely)
is the next step — kept as a follow-up because the polling-fallback
path here already resolves the routing before each duty event
fires, so the bug is fixed end-to-end. The plugin work removes the
race condition fundamentally.
2026-05-17 10:00:53 +07:00
|
|
|
"""ESP32 GPIO Matrix output signal source IDs.
|
|
|
|
|
|
|
|
|
|
Lifted from the ESP32 Technical Reference Manual (Espressif ESP32 TRM
|
|
|
|
|
section 4.11, "IO_MUX and GPIO Matrix"). Each output GPIO has a
|
|
|
|
|
configuration register `GPIO_FUNCx_OUT_SEL_CFG_REG[x]` whose low 9
|
|
|
|
|
bits (`FUNCx_OUT_SEL`) select one of 256 internal peripheral signals
|
|
|
|
|
to drive the pin. The constants below name the signals that velxio
|
|
|
|
|
actually emulates today; add more here when a new peripheral wants
|
|
|
|
|
to participate in the SignalRouter.
|
|
|
|
|
|
|
|
|
|
The signal ID range mirrors the QEMU plugin's interpretation of
|
|
|
|
|
`gpio_out_sel`; the existing worker code at
|
|
|
|
|
`esp32_worker.py:_refresh_ledc_gpio_map` already reads these values
|
|
|
|
|
out of the matrix via `qemu_picsimlab_get_internals(2)`.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── LEDC (PWM peripheral) ─────────────────────────────────────────────────
|
fix(esp32): LEDC signal IDs are 71-86 per ESP32 TRM, not 72-87
User report: on the solar-tracker project (5218f9e3) only one servo
moved and the log showed `ch=0 duty=X% gpio=12` (wrong — servoPan was
attached to GPIO 13) and `ch=1 ... gpio=-1` (servoTilt's channel
never resolved).
Root cause traced through the GPIO Matrix dump: the firmware does
exactly what the Arduino-ESP32 Servo library says — `ledcAttachPin(
13, 0)` writes signal 71 (LEDC_HS_SIG_OUT0) into `gpio_out_sel[13]`,
and `ledcAttachPin(12, 1)` writes signal 72 (LEDC_HS_SIG_OUT1) into
`gpio_out_sel[12]`. Per the ESP32 Technical Reference Manual section
4.11, Table 4-3:
71 .. 78 → LEDC HS channels 0..7
79 .. 86 → LEDC LS channels 0..7
The legacy worker code at esp32_worker.py:426 used the off-by-one
range `72 <= signal <= 87` with `ledc_ch = signal - 72`. The mistake
masked itself for single-servo projects because the 0x5000 duty
callback's channel index was internally consistent with the bogus
math, so the duty STILL reached the correctly-routed pin (just
labelled wrong). The new SignalRouter unit tests caught the
discrepancy the moment two servos drove distinct channels: signal
71 (HS_CH0, gpio 13) was REJECTED by the off-by-one filter and
signal 72 (HS_CH1, gpio 12) was misclassified as channel 0.
When I ported the legacy range into `esp32_signals.SIG_LEDC_HS_CH0_OUT_IDX`
the bug came along for the ride. Fix both modules:
* `backend/app/services/esp32_signals.py`: HS 71-78, LS 79-86.
* `frontend/src/simulation/esp32-signals.ts`: mirror.
* tests updated; 20 backend + 23 frontend pass.
After deploy the user's two servos will resolve to their declared
pins:
ch=0 duty=X% gpio=13 (servoPan, was wrongly emitting gpio=12)
ch=1 duty=X% gpio=12 (servoTilt, was wrongly emitting gpio=-1)
This is also why the multi-servo blink "patch" in commit 77bf897
appeared to help: with both pins ALIASED to the same channel via
the off-by-one, the broadcast fallback was the only thing producing
ANY movement on the second servo at all.
2026-05-17 10:42:52 +07:00
|
|
|
# Per ESP32 Technical Reference Manual section 4.11, Table 4-3 (GPIO
|
|
|
|
|
# Matrix output signals):
|
|
|
|
|
# 71-78 → LEDC_HS_SIG_OUT[0..7] (high-speed channels 0-7)
|
|
|
|
|
# 79-86 → LEDC_LS_SIG_OUT[0..7] (low-speed channels 0-7)
|
|
|
|
|
#
|
|
|
|
|
# The legacy worker code at esp32_worker.py:426 used the off-by-one
|
|
|
|
|
# range 72-87; that masked itself because the channel index encoded in
|
|
|
|
|
# the 0x5000 duty callback (0..15) was internally consistent with the
|
|
|
|
|
# bogus signal-id math, so single-servo demos still appeared to work.
|
|
|
|
|
# Multi-servo projects (e.g. solar-tracker, project 5218f9e3) exposed
|
|
|
|
|
# the bug — `ledcAttachPin(13, 0)` actually writes signal 71 to
|
|
|
|
|
# gpio_out_sel[13], which the off-by-one scan REJECTED, so channel 0
|
|
|
|
|
# resolved to GPIO 12 (the next servo's pin, whose signal 72 WAS in
|
|
|
|
|
# range and was misinterpreted as channel 0).
|
|
|
|
|
SIG_LEDC_HS_CH0_OUT_IDX = 71 # add N for HS channel N (0..7)
|
|
|
|
|
SIG_LEDC_HS_CH_LAST = 78
|
|
|
|
|
SIG_LEDC_LS_CH0_OUT_IDX = 79 # add N for LS channel N (0..7)
|
|
|
|
|
SIG_LEDC_LS_CH_LAST = 86
|
feat(esp32): SignalRouter — model the GPIO Matrix as first-class
Replaces the per-peripheral ad-hoc `_ledc_gpio_map` cache with a
proper signal-routing abstraction that mirrors the ESP32 SoC's
IO_MUX + GPIO Matrix exactly. Same idea as real silicon: signal
sources (LEDC channels, RMT, MCPWM, ...) → 40-entry routing table
→ GPIO pins.
Motivation (from user bug report in
velxio.dev/project/5218f9e3-136d-43b3-bba1-6cebde21e1a4): two
ESP32 servos on a solar-tracker visibly oscillated between two
positions instead of moving smoothly when the user changed LDR
sliders. Commit 77bf897 patched it (per-channel gpio memo +
broadcast guard) but the user requested a proper hardware-fidel
architecture, not patches.
Backend:
* `app/services/signal_router.py` — SignalRouter class. Forward
index (gpio → signal_id) + reverse index (signal_id → set of
gpios). `replace_snapshot()` returns the diff for the polling-
fallback path; future C plugin hook becomes a push without
touching this code.
* `app/services/esp32_signals.py` — Signal id constants from
ESP32 TRM (LEDC HS 72-79, LS 80-87) + `ledc_signal_for_channel()`
helper.
* `app/services/esp32_worker.py` — `_ledc_gpio_map` is gone;
`_refresh_ledc_gpio_map` replaced by `_refresh_signal_routing`
which emits `gpio_routing {gpio, signal_id}` events on diff.
The 0x5000 LEDC callback and the LEDC poll thread now emit
`ledc_duty {channel, duty_pct}` (canonical, no gpio) alongside
the legacy `ledc_update {channel, duty, gpio}` for back-compat
during rollout.
Frontend:
* `simulation/SignalRouter.ts` — 1-to-1 TS mirror of the Python
class. Same forward + reverse index; same `pinsForSignal` /
`updateRouting` / `clearRouting` API.
* `simulation/esp32-signals.ts` — Signal id constants, mirror
of the Python module.
* `simulation/Esp32Bridge.ts` — new `onLedcDuty`, `onGpioRouting`,
`onGpioRoutingClear` callbacks; handlers for the new event types.
* `store/useSimulatorStore.ts` — `makeLedcDutyHandler` looks up
pins via `router.pinsForSignal(ledcSignalForChannel(channel))`
and dispatches per pin. `makeGpioRoutingHandler` /
`makeGpioRoutingClearHandler` keep the mirror in sync. Per-board
`signalRouterMap` parallels `pinManagerMap` in lifecycle.
`makeLedcUpdateHandler` (and its memo workaround from 77bf897)
stays wired for back-compat during rollout; removed in a
follow-up commit once prod is verified stable on the new path.
Tests:
* `test/backend/unit/test_signal_router.py` (20 tests) covers
update/clear semantics, idempotency, multi-pin routing,
snapshot diff, channel↔signal-id helpers, and the multi-servo
regression scenario.
* `frontend/src/__tests__/SignalRouter.test.ts` (17 tests) is the
mirror — same scenarios on the TS side.
* `frontend/src/__tests__/esp32-multi-servo-gpio-matrix.test.ts`
(6 tests) drives the end-to-end SignalRouter handler pipeline,
asserts that two servos on GPIO 13/12 via LEDC channels 0/1
move independently (no mirroring), that re-routing carries
cleanly, and — critically — that `PinManager.broadcastPwm` is
never called.
Totals: +700 LOC, 1876 frontend tests pass (was 1853), 278 backend
unit tests pass (was 259).
Docs: ESP32_EMULATION.md §9.2 rewritten with the new architecture
diagram + a runbook for adding future peripherals through the
SignalRouter.
The C plugin hook in qemu-lcgamboa that would push gpio_out_sel
writes synchronously (eliminating the polling race window entirely)
is the next step — kept as a follow-up because the polling-fallback
path here already resolves the routing before each duty event
fires, so the bug is fixed end-to-end. The plugin work removes the
race condition fundamentally.
2026-05-17 10:00:53 +07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def ledc_signal_for_channel(channel: int) -> int:
|
|
|
|
|
"""Map a velxio-style unified LEDC channel index (0..15) to its
|
|
|
|
|
GPIO Matrix signal source id.
|
|
|
|
|
|
|
|
|
|
The ESP32 LEDC hardware has two channel groups: 8 high-speed (HS)
|
|
|
|
|
and 8 low-speed (LS). velxio unifies them into a single 0..15
|
|
|
|
|
space where ch 0-7 = HS, ch 8-15 = LS (matches the encoding the
|
|
|
|
|
QEMU plugin emits on the 0x5000 duty callback).
|
|
|
|
|
"""
|
|
|
|
|
if not 0 <= channel < 16:
|
|
|
|
|
raise ValueError(f"ledc channel out of range: {channel}")
|
|
|
|
|
if channel < 8:
|
|
|
|
|
return SIG_LEDC_HS_CH0_OUT_IDX + channel
|
|
|
|
|
return SIG_LEDC_LS_CH0_OUT_IDX + (channel - 8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def channel_for_ledc_signal(signal_id: int) -> int | None:
|
|
|
|
|
"""Inverse of :func:`ledc_signal_for_channel`. Returns None when
|
|
|
|
|
the signal id is not an LEDC channel."""
|
|
|
|
|
if SIG_LEDC_HS_CH0_OUT_IDX <= signal_id <= SIG_LEDC_HS_CH_LAST:
|
|
|
|
|
return signal_id - SIG_LEDC_HS_CH0_OUT_IDX
|
|
|
|
|
if SIG_LEDC_LS_CH0_OUT_IDX <= signal_id <= SIG_LEDC_LS_CH_LAST:
|
|
|
|
|
return 8 + (signal_id - SIG_LEDC_LS_CH0_OUT_IDX)
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── Sentinel for "GPIO not routed to any peripheral" ──────────────────────
|
|
|
|
|
# When `gpio_out_sel[N]` carries this value the pin is driven by
|
|
|
|
|
# normal GPIO output (the value in the GPIO_OUT_REG bit N), not a
|
|
|
|
|
# peripheral signal.
|
|
|
|
|
SIG_GPIO_DIRECT_OUT_IDX = 256
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
"SIG_LEDC_HS_CH0_OUT_IDX",
|
|
|
|
|
"SIG_LEDC_HS_CH_LAST",
|
|
|
|
|
"SIG_LEDC_LS_CH0_OUT_IDX",
|
|
|
|
|
"SIG_LEDC_LS_CH_LAST",
|
|
|
|
|
"SIG_GPIO_DIRECT_OUT_IDX",
|
|
|
|
|
"ledc_signal_for_channel",
|
|
|
|
|
"channel_for_ledc_signal",
|
|
|
|
|
]
|