mirror of
https://github.com/esphome/esphome.git
synced 2026-11-17 18:46:08 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c2aa2c5995 | ||
|
|
3040ee9b3a | ||
|
|
719e5bf2fa | ||
|
|
4af3936ede | ||
|
|
24d843302e |
No files matched your search
@@ -158,7 +158,7 @@ recorder, dump saver and decoder, cover cycle driver), `online_image/`
|
||||
(host driver, Zephyr driver probe), `rtsp_server/` (protocol client, raw rate reader, measurement set,
|
||||
API image grab), `cse7766/` (API observer with ping), `zephyr_coredump/` (reader flow, decode with the
|
||||
Zephyr tools), `libretiny_prefs/` (rtc preference harness header), `libretiny_crash/` (BK72xx crash probe header,
|
||||
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
|
||||
capture decoder, RTL8720C ROM dump versus report matcher), `api_outgoing/` (wake cycle and Wi-Fi drop timing readers), `ld2450/` (zone number driver, loopback harness driver with number and binary sensor states and TX frame summarizer, frame stream harness with presence hold and link loss drivers, BT MAC driver), `api/` (raw plaintext native API client for user defined actions, store_yaml recovery client over Noise or plaintext), `http_request/` (update manifest server, update state driver, request test server, action case driver, concurrency, reboot, update check and OTA drivers, `ota_auth/` OTA request cases with basic auth), `modem/` (button and sensor driver over the plaintext API, also used for the BLE MIDI boards, AT commands for the UART passthrough), `dfrobot_sen0395/` (API driver and case runner), `homeassistant_light/` (direct bulb driver, held-report case driver), `usb_audio/` (URL player), `speaker_media_player/` (API player driver, pipeline task case runner), `web_server/` (SSE client, request mix, SSE and HTTPD case runners, per-session `/events` listing check with its config, `/events` head, multi-client, stall, Wi-Fi drop and churn modes with their config, head comparer and runner), `mqtt/` (`ha_discovery/`, `disable/`, `retained_burst/` and `reconnect/` runners; broker config with TLS, client-certificate and WebSocket listeners, test CA with server and client certificates, transport case runner, flash-and-drive runner, CLI TLS check without a board, MAC suffix runner with per-variant configs for two boards, `shutdown/` shutdown message runner and matcher), `udp/` (datagram recorder, `udp.write` payload checker and case driver), `entity_icons/` (icon and device class readback over the API, MQTT discovery, web_server and dump_config, with a comparer), `uart_sim/` (API driver and flash, capture and drive runner for the simulated UART sensors), `remote_base/` (loopback runner and API log decoder per sent frame), `xiaomi_ble/` (frame and header generator with the expected counts, injecting, advertising and receiving configs, counter and runner), `ble_terminate/` (proxy and peer configs, connect and disconnect driver, peer disconnect matcher, runner), `i2c_slave/` (D1 mini slave and BK7238 master configs), `wpa3_ap/` (PMF AP with join and leave logging, BK7238 and CB3S DUTs, runner and summary), `wisp_ap/` (WISP upstream with AP buttons, WISP and ALWAYS_ON DUTs, client, runner and per phase summary), `wifi_restart/` (WPA3 mixed WISP upstream, static IP DUTs with syslog, runner and summary), `ota_timing/` (OTA upload timer with deflate, raw and TCP modes, API build check, A/B runner and median summary, `inflate_bench/` on-device inflate benchmark with stream generator, configs and syslog runner), `gpio_hold/` (ISR pin hold self test with ESP32 and S3 configs), `ble_event_inject/` (GAP and GATTC event injector header and proxy package), `light_sleep_wake/` (LEDC wave and edge timer for light sleep wakeup on a C3, external wave generator and per-call edge detail probe for peripheral power-down on the P4), `manual_ip/` (station and AP manual IP configs, DNS probe and runners), `web_handlers/` (late handler, web_server, captive_portal and prometheus variants, web OTA runner), `bk72xx_flash/` (flash readback with retries, per partition diff of two dumps), `ble_server_security/` (secure GATT server and pairing client configs, driver, summary and runner), `prometheus/` (datetime metrics configs and driver), `zephyr_ble_server/` (nRF52 server, looping ESP32 client and UF2 flash and count runner), `ble_adv_triggers/` (advertiser with six raw frames, ESP32 and BK72xx receivers with eleven triggers, runner and checker), `sun/` (longitude sweep package, runner and checker), `sensor_filters/` (every filter shape with a fixed input sequence, per-board packages and runner), `led_driver_sniff/` (SPI slave capture header with selftest, config generator, window comparer and runner), `hub_input_sim/` (keypad, wiegand and rdm6300 simulation on a D1 mini, API log runner), `value_range/` (on_value_range sequence with a restart, runner), `ezo_fake_bus/` (fake EZO I2C bus and step config, runner), `twai_self/` (TWAI self-reception header, on_frame config and counter runner), `ota_automations/` (OTA automation package for both OTA platforms, four case runner, repeated web upload runner and per case summary), `output_button/` (pad edge timer header, button package and runner), `homeassistant_button/` (raw plaintext API client for action calls and wizard inputs, package and runner), `uart_button/` (UART0 loopback package, API driver with TX and RX counts, runner), `interval/` (plain and delayed interval package, timing summary and runner), `smp_udp_ota/` (SMP reset fault injector, OTA runner, per mode result check, MCUboot swap limit, running image check and slot blanking UF2, Thread device config), `ld2420_sim/` (fake LD2420 answering the hub's commands on a UART0 loopback, API state recorder, two-build comparer and runneLine truncated
|
||||
|
||||
## Templates in `$SKILL/templates`
|
||||
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
## Fake devices on an in-firmware I2C bus
|
||||
|
||||
When there is no chip and no wiring, subclass `i2c::I2CBus` in an include, implement `write_readv()` only, answer
|
||||
the registers the driver reads and check each write. Point the component at it from an `on_boot` lambda that runs
|
||||
before the component's `setup()`: `id(x)->set_i2c_bus(&fake::bus)` at priority 950, which is ahead of every I2C
|
||||
component (hubs set up at IO, 900, sensors at DATA, 600). The configs here that use 700 to 1000 work for their
|
||||
components; use 950 for a new one. Keep a real `i2c:` block in the config so validation passes. Bulk
|
||||
loops in a button lambda must call `App.feed_wdt()` and keep their per-iteration logs below INFO: 2000 DEBUG lines at
|
||||
115200 baud trip the task watchdog, which is a fault of the harness and not of the PR.
|
||||
When there is no chip and no wiring, subclass `i2c::I2CBus` in an include, implement `write_readv()` only, answer the
|
||||
registers the driver reads and check each write. Point the component at it from an `on_boot` lambda that runs before the
|
||||
component's `setup()`: `id(x)->set_i2c_bus(&fake::bus)` at a priority above the swapped component's own setup priority
|
||||
(a hub at BUS - 0.1 needs 999.95; 950 is ahead of hubs at IO, 900, and sensors at DATA, 600). Keep a real `i2c:` block
|
||||
in the config so validation passes. Bulk loops in a button lambda must call `App.feed_wdt()` and keep their
|
||||
per-iteration logs below INFO: 2000 DEBUG lines at 115200 baud trip the task watchdog, which is a fault of the harness
|
||||
and not of the PR. Sensor publishes log at VERBOSE, so log entity values from a lambda (`on_value` or an interval).
|
||||
|
||||
`templates/i2c_fake/` holds the fakes; each `*-common.yaml` or `*-test.yaml` is a classic ESP32 or ESP32-S3 config
|
||||
that includes its `.hpp` (add `esphome: name:` for the `-common` ones):
|
||||
@@ -151,3 +151,42 @@ Prefer this to glitching the wire. Holding SDA low by rerouting the pin (`esp_ro
|
||||
`SIG_GPIO_OUT_IDX` and back to `I2CEXT0_SDA_OUT_IDX`) wedges the ESP-IDF controller, which then needs
|
||||
`i2c_master_bus_reset()` after each glitch, and glitches timed onto writes left the VL53L0X answering 0x00 until a
|
||||
power cycle.
|
||||
|
||||
## WeiKai WK2168 on I2C (#22759)
|
||||
|
||||
`templates/i2c_fake/wkfake.hpp` + `wk-common.yaml`: answers 0x10 to 0x17 (`0 A1 A0 1 0 C1 C0 F`), keeps only A3..A0 of
|
||||
the register byte and builds the 6 bit address C1C0A3..A0, models the globals (a GRST write resets the channels in its
|
||||
low nibble and logs `!!!!! GRST WRITE`), per-channel SCR/LCR/FCR/baud/RFCNT/FSR and FIFOs (TX logged, RX injected with
|
||||
`inject_rx`, dropped when SCR is off), GPDIR/GPDAT with `set_input()`. It classifies each transaction W / P (1 byte
|
||||
pointer) / R / WR (repeated start); `fail_reads`, `fail_ptr` and `every_n` inject NACKs and the fake counts whether
|
||||
the next read is a retry of the same register. The driver polls RFCNT and FSR of every channel and GPDAT every loop:
|
||||
log only writes, changes and a `trace` window, never every read. `wk-map-check.cpp.example` is a host check with
|
||||
copies of the driver's `i2c_address()` (the same in both trees) and the PR's `i2c_register()`, comparing the address
|
||||
and register byte each tree sends for every register; paste the current bodies in before running it. On #22759 the
|
||||
base build's GPIO writes hit GRST and reset UART channel 0.
|
||||
|
||||
## BMI270 config upload and BMM150 aux (#22778)
|
||||
|
||||
`bmi270fake.hpp` + `bmi270-common.yaml` + `bmi270-motion-aux.yaml`: BMI270 register file with reset values; config
|
||||
upload port (INIT_ADDR word address, INIT_DATA into 8 KB) where INTERNAL_STATUS reads 0x01 only when every byte was
|
||||
written once and FNV-1a equals `expected_fnv` (default 0x95BB75B5, the current table, computed on the host from
|
||||
bmi270_config.h; the host harness takes an override in `BMI_EXPECTED_FNV`), else 0x02. Aux interface: AUX_DEV_ID selects
|
||||
the BMM150, manual mode turns AUX_WR_ADDR/AUX_RD_ADDR writes into BMM150 writes and 1/2/6/8 byte reads into AUX_X..,
|
||||
STATUS.aux_busy for two polls, auto mode mirrors the BMM150 data. Fault knobs: `bmm_mode` (absent, wrong chip id),
|
||||
`busy_stuck`, `stuck_from_aux_op` (busy stuck from the Nth aux transfer: 1 power-on, 2 soft reset, 3 chip id, 4 to 6
|
||||
trim, 7 normal mode), `nack_at` (main-bus transaction N). `norm_fnv` hashes register-level effects and leaves STATUS
|
||||
polls out (their count depends on timing). Heap is sampled at INIT_CTRL=0/1 and inside each INIT_DATA burst;
|
||||
`-DBMI_FAKE_WRAP_MALLOC` with `-Wl,--wrap=malloc` in `platformio_options: build_flags` counts malloc() during the upload
|
||||
through `alloc_count.hpp` (list it in `includes:` too; the link fails without the wrap, so a clean link proves it
|
||||
applied).
|
||||
- Host side: copy `bmi270-host-harness.cpp.example` (as a `.cpp`) and the fake into `tests/components/bmi270/` of both
|
||||
worktrees, plus a `benchmark.yaml` with `esphome: build_flags: [-DUSE_BMI270_BMM150, -DUSE_LOG_LISTENERS,
|
||||
-DESPHOME_LOG_MAX_LISTENERS=4]` (the unit test build does not use defines.h; this is how to get a component's
|
||||
optional code and log capture into it); the base also needs the PR's `__init__.py` manifest override.
|
||||
`bmi270-setters.py` turns each tree's generated main.cpp (`esphome compile --only-generate`) into the setter list
|
||||
the harness applies, so codegen changes (setters skipped at defaults) are tested as generated.
|
||||
`bmi270-host-run.sh.example TREE NAME SETTERS [32]` runs it (32 forces the Arduino burst branch);
|
||||
`bmi270-host-compare.py BASE.report PR.report` merges base's split INIT_ADDR writes, compares every config, fault and
|
||||
per-step scenario, maps the NACK sweep through the merged stream and lists every log difference.
|
||||
`bmm150-sweep.cpp.example` compares a decoder against a namespace-renamed copy of the base header bit for bit.
|
||||
- A planted one byte burst offset gave INTERNAL_STATUS 0x02 and "Config file upload failed" at both burst sizes.
|
||||
@@ -14,3 +14,39 @@ On the NiceMCU WB3S V1 board (BK7238), P26 and P24 are on the right-hand header.
|
||||
|
||||
Fake devices that need no real chip (in-firmware buses, the Pico 2 W target, the DS2482-800 simulator, NACK
|
||||
injection) are in [fake_i2c.md](fake_i2c.md).
|
||||
|
||||
## I2C wire A/B of a bus change (#22780, write_prefixed)
|
||||
|
||||
`scripts/i2c_wire_ab/`.
|
||||
- Host (`host/run.sh TREE OUT`, then `host/compare.py BASE PR`): copies a harness into the tree's
|
||||
`tests/components/i2c/` (removed again afterwards) and runs `script/cpp_unit_test.py i2c`. `extract.py` cuts the
|
||||
Arduino and ESP-IDF `write_readv`/`transfer_` bodies out of the tree and compiles them against the tree's real headers
|
||||
with stub `Wire.h` (esp8266 core 3.1.2 and arduino-pico 6.3.2 buffer rules, 128 and 256 bytes) and a stub
|
||||
`driver/i2c_master.h` (IDF 5.5.5 checks: at most SOC_I2C_CMD_REG_NUM jobs, NACK before STOP); the software bus is
|
||||
compiled whole with `#define InternalGPIOPin wab::FakePin` over a bit-level target. Every bus emits one token stream
|
||||
(`S 90+ 02+ ... P`, `Sr`, `rA0+`), with mallocs counted through macOS `malloc_logger` (ASan is dropped on macOS).
|
||||
Stub headers must live outside the test dir (a subfolder there is taken as a platform domain) and reach the build
|
||||
through a `benchmark.yaml` `esphome: build_flags: [-I...]`; `.h` files in the test dir are included into main.cpp, so
|
||||
generated bodies are `.inc`. Explicit-instantiation member robbers must be declared at global scope. The sources are
|
||||
stored as `.cpp.example`, `.h.example` and `.hpp` so the repo's C++ lints skip them; `run.sh` copies them back.
|
||||
- Device: `dev/snf.hpp` is a GPIO interrupt sniffer on the I2C pads (an ESP32-C2 with an ADS1015: SDA 4, SCL 5,
|
||||
`dev/c2common.yaml`) plus the same case sweep and a malloc counter; at 10 to 20 kHz on the C2 it still drops a bit in
|
||||
about 0.5 % of transactions (mostly at the chip's NACK), so capture three runs per build with
|
||||
`dev/flashrun.sh NAME YAML PORT SECONDS SUFFIX` (suffix empty, 2 and 3 write NAME-ser.txt, -ser2 and -ser3) and
|
||||
judge with `clean.py` (`--nacks` sorts each DIFF into a chip NACK or not) and `maj.py`, which also takes one or two
|
||||
captures. On the software bus it is exact. `dev/pdslave.hpp` with `snf.hpp` built with `-DSNF_PDSLAVE`
|
||||
(`pdcommon.yaml`) turn an ESP32-PICO-D4 into its own target at 0x48 on GPIO32/33 (ACKs every byte, so 300-byte
|
||||
writes reach the wire): exact on 6 of 6 captures at 25 kHz once one warm-up probe runs before the sweep (the ISR
|
||||
runs from flash; IRAM_ATTR on header inline functions fails to link with l32r literal errors). `mksw.py` turns a
|
||||
tree's LibreTiny-only software bus into a header so an ESP32 build can run it on real pins. The ADS1015 NACKs the
|
||||
third byte after the pointer, so long writes on that chip end there on every build.
|
||||
- `ard/swp.hpp` is the same sweep for ESP8266 (GPIO interrupt sniffer on GPIO4/5), RP2 (raw IO_IRQ_BANK0 handler) and
|
||||
BK72xx (no sniffer, so `ard/ardcmp.py --reads-by-data` compares reads by their data), with `ard/common.yaml` plus a
|
||||
board file (`d1.yaml`, `pico.yaml`, `bk.yaml`). Both sweeps share the decoder in `wiredecode.hpp` and count heap
|
||||
calls with `templates/i2c_fake/alloc_count.hpp`, which names the link flag for each platform. Each image checks
|
||||
itself with `ALLOC_PROBE 1`. `buildall.sh YAML...` builds the configs of either folder, each under the `name` of
|
||||
its substitutions (the file name without one), `x-pr.yaml` from PR_TREE and the rest from BASE_TREE.
|
||||
- Facts: on a D1 mini without a target, register writes up to 128 bytes total show `S 90- P`, longer ones fail with
|
||||
rc 6 and nothing on the wire; a write+read whose address is NACKed leaves no STOP (`S 90-`), dev too.
|
||||
arduino-pico: after a register write over 256 bytes, the next empty write (a probe) sends the stale buffer to the
|
||||
failed write's address.
|
||||
@@ -37,3 +37,7 @@ npm1300/*-cap.err
|
||||
npm1300/*-run.log
|
||||
rp2_sched_lock/*.txt
|
||||
rp2_sched_lock/*.log
|
||||
i2c_wire_ab/*/*-ser*.txt
|
||||
i2c_wire_ab/*/*-flash.log
|
||||
i2c_wire_ab/*/*-compile.log
|
||||
i2c_wire_ab/*/*-upload.log
|
||||
@@ -0,0 +1,50 @@
|
||||
"""ardcmp.py BASE_SER PR_SER [--reads-by-data]: per case rc and wire hash must match; alloc summary, rc counts and
|
||||
wire shapes. --reads-by-data compares read cases by the returned data instead (BK72xx has no sniffer, so its wire
|
||||
hash is that of an empty stream)."""
|
||||
|
||||
import argparse
|
||||
from collections import Counter
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
from snlog import first_alloc, is_read, load # noqa: E402
|
||||
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument("base")
|
||||
ap.add_argument("pr")
|
||||
ap.add_argument("--reads-by-data", action="store_true")
|
||||
args = ap.parse_args()
|
||||
b, p = load(args.base, "SW"), load(args.pr, "SW")
|
||||
print("cases", len(b), len(p))
|
||||
|
||||
|
||||
def key(label: str, case: dict) -> tuple:
|
||||
if args.reads_by_data and is_read(label):
|
||||
return case["rc"], case["rd"]
|
||||
return case["rc"], case["fnv"]
|
||||
|
||||
|
||||
diff = [k for k in b if k not in p or key(k, b[k]) != key(k, p[k])]
|
||||
print("rc/wire (or read data) differences:", len(diff))
|
||||
for k in diff[:20]:
|
||||
print(" ", k, "\n base", b[k], "\n pr ", p.get(k))
|
||||
for name, d in (("base", b), ("pr", p)):
|
||||
wr = [k for k in d if k.startswith("wreg")]
|
||||
allocs = {k: v["alloc"] for k, v in d.items()}
|
||||
first = {s: first_alloc(allocs, s) for s in ("wreg8", "wreg16")}
|
||||
print(
|
||||
f"{name}: write_register calls that allocated {sum(allocs[k] > 0 for k in wr)} of {len(wr)}; first "
|
||||
f"allocating len {first}; other allocs {[k for k in d if not k.startswith('wreg') and allocs[k]]}"
|
||||
)
|
||||
print(
|
||||
" rc counts:",
|
||||
dict(Counter(v["rc"] for v in d.values())),
|
||||
"| wire shapes:",
|
||||
dict(
|
||||
Counter(
|
||||
re.sub(r"[0-9A-F]{2}", "xx", v["wire"]) for v in d.values()
|
||||
).most_common(6)
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
bk72xx:
|
||||
board: xh-wb3s # set your board
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
id: dev
|
||||
i2c_id: bus_hw
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
# bkrun.sh NAME PORT: flash NAME's UF2 to a BK72xx board (up to 3 tries), then capture 60 s from a reset (log on
|
||||
# UART1) and print the sweep line count, the ALLOC_PROBE line and the SWDONE line. env: PY, LTPY (LibreTiny python)
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
|
||||
N=${1:?name}; P=${2:?port}; PY=${PY:-python3}
|
||||
LTPY=${LTPY:-$D/../pio/penv/.libretiny/bin/python}
|
||||
[ -x "$LTPY" ] || LTPY=~/.platformio/penv/.libretiny/bin/python
|
||||
for i in 1 2 3; do
|
||||
timeout 200 "$LTPY" "$S/flash_bk72xx.py" flash write -d "$P" ".esphome/build/$N/.pioenvs/$N/firmware.uf2" > "$N-flash.log" 2>&1 &&
|
||||
break
|
||||
echo "flash try $i failed: $(tail -1 "$N-flash.log")"
|
||||
[ "$i" = 3 ] && exit 1
|
||||
done
|
||||
$PY "$S/serial_capture.py" "$P" 60 "$N-ser.txt" --reset > /dev/null 2>&1
|
||||
echo "$N $(grep -a -c 'SW ' "$N-ser.txt") $(grep -a -o 'ALLOC_PROBE.*' "$N-ser.txt") $(grep -a -o 'SWDONE.*' "$N-ser.txt")"
|
||||
@@ -0,0 +1,47 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- ../../../templates/i2c_fake/alloc_count.hpp
|
||||
- ../wiredecode.hpp
|
||||
- swp.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- ${wrapflag}
|
||||
- ${defs}
|
||||
on_boot:
|
||||
- priority: -100
|
||||
then:
|
||||
- delay: ${start_delay}
|
||||
- lambda: |-
|
||||
swp::bus = id(bus_hw);
|
||||
swp::install();
|
||||
id(started) = true;
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
hardware_uart: ${loguart}
|
||||
|
||||
globals:
|
||||
- id: started
|
||||
type: bool
|
||||
initial_value: "false"
|
||||
- id: wedged
|
||||
type: bool
|
||||
initial_value: "false"
|
||||
|
||||
i2c:
|
||||
id: bus_hw
|
||||
sda: ${sda}
|
||||
scl: ${scl}
|
||||
frequency: ${freq}
|
||||
scan: true
|
||||
|
||||
interval:
|
||||
- interval: 20ms
|
||||
then:
|
||||
- lambda: |-
|
||||
if (!id(started)) return;
|
||||
if (!swp::step(id(dev)) && !id(wedged) && ${do_wedge}) {
|
||||
id(wedged) = true;
|
||||
swp::wedge(id(dev));
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
esp8266:
|
||||
board: d1_mini
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
id: dev
|
||||
i2c_id: bus_hw
|
||||
@@ -0,0 +1,17 @@
|
||||
rp2040:
|
||||
board: rpipico2w
|
||||
sensor:
|
||||
- platform: ina226
|
||||
id: dev
|
||||
i2c_id: bus_hw
|
||||
address: 0x40
|
||||
shunt_resistance: 0.1 ohm
|
||||
update_interval: 2s
|
||||
bus_voltage:
|
||||
name: vbus
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("rd", "READ vbus %.4f", x);
|
||||
shunt_voltage:
|
||||
name: vshunt
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("rd", "READ vshunt %.6f", x);
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
# picorun.sh NAME YAML PORT [OUTSUFFIX]: upload from TREE (reboots the Pico), wait for the port, capture 60 s and print
|
||||
# the sweep line count, the SWDONE line and the count of wedge cases. env: TREE, PY
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
|
||||
N=${1:?name}; Y=${2:?yaml}; P=${3:?port}; X=${4:-}; PY=${PY:-${TREE:?set TREE}/venv/bin/python}
|
||||
PIO_CORE=${PIO_CORE:-$D/../pio} bash "$S/pio_build.sh" upload "$N" "$Y" "$P" > /dev/null || exit 1
|
||||
$PY "$S/ports.py" --wait 2e8a > /dev/null 2>&1
|
||||
$PY "$S/serial_capture.py" "$P" 60 "$N-ser$X.txt" > /dev/null 2>&1
|
||||
echo "$N$X $(grep -a -c 'SW ' "$N-ser$X.txt") $(grep -a -o 'SWDONE.*' "$N-ser$X.txt")" \
|
||||
"$(grep -a -c 'WEDGE.*rc=6 then read rc=6' "$N-ser$X.txt")"
|
||||
@@ -0,0 +1,235 @@
|
||||
#pragma once
|
||||
// esphome#22780 sweep for the Arduino (ESP8266, RP2040) and software (BK72xx) buses: one "SW" line per I2CDevice call
|
||||
// with rc, mallocs during the call (alloc_count.hpp) and, where a sniffer exists (ESP8266, RP2), the decoded wire
|
||||
// stream and its hash.
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "alloc_count.hpp"
|
||||
#include "wiredecode.hpp"
|
||||
#ifdef USE_RP2
|
||||
#include <hardware/gpio.h>
|
||||
#include <hardware/irq.h>
|
||||
#include <hardware/structs/sio.h>
|
||||
#endif
|
||||
|
||||
#ifndef SWP_ADDR
|
||||
#define SWP_ADDR 0x48
|
||||
#endif
|
||||
#ifndef SWP_WREG
|
||||
#define SWP_WREG 0x02
|
||||
#endif
|
||||
#ifndef SWP_RREG
|
||||
#define SWP_RREG 0x01
|
||||
#endif
|
||||
#ifndef SWP_MAXLEN8
|
||||
#define SWP_MAXLEN8 300
|
||||
#endif
|
||||
#ifndef SWP_MAXLEN16
|
||||
#define SWP_MAXLEN16 300
|
||||
#endif
|
||||
#ifndef SWP_SDA
|
||||
#define SWP_SDA 4
|
||||
#endif
|
||||
#ifndef SWP_SCL
|
||||
#define SWP_SCL 5
|
||||
#endif
|
||||
|
||||
namespace swp {
|
||||
static const char *const TAG = "swp";
|
||||
|
||||
#if defined(USE_ESP8266) || defined(USE_RP2)
|
||||
#define SWP_SNIFF 1
|
||||
static constexpr uint32_t N = 12000;
|
||||
static uint8_t buf[N];
|
||||
static volatile uint32_t n = 0, ovf = 0;
|
||||
static volatile uint8_t last = 3;
|
||||
static volatile bool armed = false;
|
||||
static inline __attribute__((always_inline)) uint8_t levels() {
|
||||
#ifdef USE_ESP8266
|
||||
uint32_t v = GPI;
|
||||
#else
|
||||
uint32_t v = sio_hw->gpio_in;
|
||||
#endif
|
||||
return ((v >> SWP_SDA) & 1) | (((v >> SWP_SCL) & 1) << 1);
|
||||
}
|
||||
#ifdef USE_ESP8266
|
||||
static void IRAM_ATTR isr() {
|
||||
#else
|
||||
static void isr() {
|
||||
for (int p : {SWP_SDA, SWP_SCL}) {
|
||||
uint32_t ev = gpio_get_irq_event_mask(p);
|
||||
if (ev)
|
||||
gpio_acknowledge_irq(p, ev);
|
||||
}
|
||||
#endif
|
||||
uint8_t s = levels();
|
||||
if (!armed || s == last)
|
||||
return;
|
||||
last = s;
|
||||
if (n < N)
|
||||
buf[n++] = s;
|
||||
else
|
||||
ovf = ovf + 1;
|
||||
}
|
||||
static void install() {
|
||||
#ifdef USE_ESP8266
|
||||
attachInterrupt(SWP_SDA, isr, CHANGE);
|
||||
attachInterrupt(SWP_SCL, isr, CHANGE);
|
||||
#else
|
||||
gpio_add_raw_irq_handler_masked((1u << SWP_SDA) | (1u << SWP_SCL), isr);
|
||||
gpio_set_irq_enabled(SWP_SDA, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true);
|
||||
gpio_set_irq_enabled(SWP_SCL, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true);
|
||||
irq_set_enabled(IO_IRQ_BANK0, true);
|
||||
#endif
|
||||
ESP_LOGI(TAG, "sniffer on SDA %d SCL %d", SWP_SDA, SWP_SCL);
|
||||
}
|
||||
#else
|
||||
#define SWP_SNIFF 0
|
||||
static void install() {}
|
||||
#endif
|
||||
|
||||
static i2c::I2CBus *bus = nullptr;
|
||||
static uint8_t data[320];
|
||||
static uint8_t rdata[40];
|
||||
static uint32_t idx = 0, nalloc = 0;
|
||||
static bool done = false, probed = false;
|
||||
#if SWP_SNIFF
|
||||
static uint8_t start = 3; // levels when the sniffer was armed
|
||||
#endif
|
||||
|
||||
static void arm() {
|
||||
#if SWP_SNIFF
|
||||
n = 0;
|
||||
ovf = 0;
|
||||
start = levels();
|
||||
last = start;
|
||||
armed = true;
|
||||
#endif
|
||||
alloc_count::count = 0;
|
||||
alloc_count::window = true;
|
||||
}
|
||||
static void disarm() {
|
||||
alloc_count::window = false;
|
||||
#if SWP_SNIFF
|
||||
armed = false;
|
||||
#endif
|
||||
}
|
||||
static std::string sniffed(uint32_t &amb) {
|
||||
#if SWP_SNIFF
|
||||
return wiredecode::decode(buf, n, start, amb);
|
||||
#else
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool step(i2c::I2CDevice *dev) {
|
||||
if (done)
|
||||
return false;
|
||||
if (!probed) {
|
||||
// the counter must see a malloc inside the window
|
||||
alloc_count::count = 0;
|
||||
alloc_count::window = true;
|
||||
void *volatile p = malloc(100);
|
||||
alloc_count::window = false;
|
||||
free(p);
|
||||
ESP_LOGI(TAG, "ALLOC_PROBE %u", (unsigned) alloc_count::count);
|
||||
probed = true;
|
||||
}
|
||||
const uint32_t W8 = SWP_MAXLEN8 + 1, W16 = SWP_MAXLEN16 + 1;
|
||||
std::string label;
|
||||
uint32_t i = idx++;
|
||||
for (size_t k = 0; k < sizeof data; k++)
|
||||
data[k] = static_cast<uint8_t>(0x11 + i + k * 7);
|
||||
memset(rdata, 0, sizeof rdata);
|
||||
i2c::ErrorCode rc;
|
||||
if (i < W8) {
|
||||
label = "wreg8 len=" + std::to_string(i);
|
||||
arm();
|
||||
rc = dev->write_register(SWP_WREG, data, i);
|
||||
disarm();
|
||||
} else if (i < W8 + W16) {
|
||||
size_t len = i - W8;
|
||||
label = "wreg16 len=" + std::to_string(len);
|
||||
arm();
|
||||
rc = dev->write_register16((SWP_WREG << 8) | 0x03, data, len);
|
||||
disarm();
|
||||
} else if (i < W8 + W16 + 10) {
|
||||
static const size_t ns[] = {1, 2, 3, 4, 8};
|
||||
uint32_t j = i - W8 - W16;
|
||||
size_t k = ns[j % 5];
|
||||
bool sixteen = j >= 5;
|
||||
label = std::string(sixteen ? "rreg16" : "rreg8") + " n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = sixteen ? dev->read_register16(SWP_RREG << 8, rdata, k) : dev->read_register(SWP_RREG, rdata, k);
|
||||
disarm();
|
||||
} else if (i < W8 + W16 + 13) {
|
||||
size_t k = i - (W8 + W16 + 10) + 1;
|
||||
label = "read n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = dev->read(rdata, k);
|
||||
disarm();
|
||||
} else if (i < W8 + W16 + 16) {
|
||||
static const uint8_t p1[] = {SWP_RREG};
|
||||
size_t k = i - (W8 + W16 + 13) + 1;
|
||||
label = "write1+read n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = dev->write_read(p1, 1, rdata, k);
|
||||
disarm();
|
||||
} else if (i < W8 + W16 + 16 + 112) {
|
||||
uint8_t a = 8 + (i - (W8 + W16 + 16));
|
||||
label = "probe " + std::to_string(a);
|
||||
arm();
|
||||
rc = bus->write_readv(a, nullptr, 0, nullptr, 0);
|
||||
disarm();
|
||||
} else {
|
||||
done = true;
|
||||
ESP_LOGI(TAG, "SWDONE cases=%u alloc_calls=%u", (unsigned) idx - 1, (unsigned) nalloc);
|
||||
return false;
|
||||
}
|
||||
nalloc += alloc_count::count != 0;
|
||||
uint32_t amb = 0, events = 0, overflow = 0;
|
||||
std::string got = sniffed(amb);
|
||||
#if SWP_SNIFF
|
||||
events = n;
|
||||
overflow = ovf;
|
||||
#endif
|
||||
char rd[3 * 8 + 1] = "";
|
||||
for (int k = 0; k < 8 && label.rfind("wreg", 0) != 0 && label.rfind("probe", 0) != 0; k++)
|
||||
snprintf(rd + 3 * k, 4, "%02X ", rdata[k]);
|
||||
ESP_LOGI(TAG, "SW %s|rc=%d|alloc=%u|ev=%u|amb=%u|ovf=%u|fnv=%08X|%s|rd=%s", label.c_str(), (int) rc,
|
||||
(unsigned) alloc_count::count, (unsigned) events, (unsigned) amb, (unsigned) overflow,
|
||||
(unsigned) fnv1a_hash(got), got.substr(0, 90).c_str(), rd);
|
||||
App.feed_wdt();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Over-long register write and what the bus does afterwards (on arduino-pico this leaves Wire stuck, dev too).
|
||||
// Each step is sniffed; the probe step shows whether a stale buffer goes out.
|
||||
static void wedge_step(const char *what, const std::function<i2c::ErrorCode()> &fn) {
|
||||
uint32_t amb = 0;
|
||||
arm();
|
||||
auto rc = fn();
|
||||
disarm();
|
||||
std::string got = sniffed(amb);
|
||||
ESP_LOGI(TAG, "WEDGE %s rc=%d ev=%u wire=%s", what, (int) rc, (unsigned) got.size(), got.substr(0, 120).c_str());
|
||||
App.feed_wdt();
|
||||
}
|
||||
static void wedge(i2c::I2CDevice *dev) {
|
||||
static uint8_t r[2];
|
||||
wedge_step("read_register before", [dev] { return dev->read_register(SWP_RREG, r, 2); });
|
||||
wedge_step("plain read before", [dev] { return dev->read(r, 1); });
|
||||
wedge_step("write_register len=300", [dev] { return dev->write_register(SWP_WREG, data, 300); });
|
||||
wedge_step("read_register after", [dev] { return dev->read_register(SWP_RREG, r, 2); });
|
||||
wedge_step("plain read after", [dev] { return dev->read(r, 1); });
|
||||
wedge_step("write_register len=2 after", [dev] { return dev->write_register(SWP_WREG, data, 2); });
|
||||
wedge_step("probe 0x50 after", [] { return bus->write_readv(0x50, nullptr, 0, nullptr, 0); });
|
||||
wedge_step("plain read after probe", [dev] { return dev->read(r, 1); });
|
||||
wedge_step("write_register len=2 after probe", [dev] { return dev->write_register(SWP_WREG, data, 2); });
|
||||
}
|
||||
} // namespace swp
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
# buildall.sh YAML...: compile each config under the `name:` of its substitutions (the file name when it sets none),
|
||||
# from the tree named by its suffix (`x-pr.yaml` builds from PR_TREE, any other from BASE_TREE).
|
||||
# env: PR_TREE, BASE_TREE, PY, BUILD_LOCK (optional), PIO_CORE (default i2c_wire_ab/pio)
|
||||
# Stops at the first failed build and exits with its status.
|
||||
set -o pipefail
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$D")
|
||||
for y in "$@"; do
|
||||
b=$(basename "$y" .yaml)
|
||||
case ${b##*-} in pr) T=${PR_TREE:?set PR_TREE} ;; *) T=${BASE_TREE:?set BASE_TREE} ;; esac
|
||||
nm=$(awk '/^substitutions:/{s=1; next} /^[^ #]/{s=0} s && /^ name:/{gsub(/["\x27]/, "", $2); print $2; exit}' "$y")
|
||||
nm=${nm:-$b}
|
||||
TREE=$T PIO_CORE=${PIO_CORE:-$D/pio} bash "$S/pio_build.sh" compile "$nm" "$y" | sed "s/^/$nm /" || exit
|
||||
done
|
||||
@@ -0,0 +1,83 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- ${swfile}
|
||||
- ../../../templates/i2c_fake/alloc_count.hpp
|
||||
- ../wiredecode.hpp
|
||||
- snf.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- -Wl,--wrap=malloc
|
||||
- ${flags}
|
||||
on_boot:
|
||||
- priority: 700
|
||||
then:
|
||||
- lambda: |-
|
||||
gpio_reset_pin(GPIO_NUM_6); gpio_set_direction(GPIO_NUM_6, GPIO_MODE_OUTPUT); gpio_set_level(GPIO_NUM_6, 1);
|
||||
gpio_reset_pin(GPIO_NUM_7); gpio_set_direction(GPIO_NUM_7, GPIO_MODE_OUTPUT); gpio_set_level(GPIO_NUM_7, 0);
|
||||
#ifdef SNF_SW
|
||||
snf::bus = snf::make_sw();
|
||||
id(ads)->set_i2c_bus(snf::bus);
|
||||
#else
|
||||
snf::bus = id(bus_hw);
|
||||
#endif
|
||||
- priority: -100
|
||||
then:
|
||||
- delay: 6s
|
||||
- lambda: |-
|
||||
snf::install();
|
||||
id(started) = true;
|
||||
|
||||
esp32:
|
||||
board: esp32-c2-devkitm-1
|
||||
framework:
|
||||
type: esp-idf
|
||||
sdkconfig_options:
|
||||
CONFIG_XTAL_FREQ_26: y
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
baud_rate: 115200
|
||||
|
||||
globals:
|
||||
- id: started
|
||||
type: bool
|
||||
initial_value: "false"
|
||||
|
||||
i2c:
|
||||
id: bus_hw
|
||||
sda: ${hw_sda}
|
||||
scl: ${hw_scl}
|
||||
frequency: ${freq}
|
||||
scan: true
|
||||
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
id: ads
|
||||
i2c_id: bus_hw
|
||||
|
||||
sensor:
|
||||
- platform: ads1115
|
||||
ads1115_id: ads
|
||||
multiplexer: A2_GND
|
||||
gain: 6.144
|
||||
resolution: 12_BITS
|
||||
name: a2
|
||||
update_interval: 2s
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("rd", "READ a2 %.4f", x);
|
||||
- platform: ads1115
|
||||
ads1115_id: ads
|
||||
multiplexer: A3_GND
|
||||
gain: 6.144
|
||||
resolution: 12_BITS
|
||||
name: a3
|
||||
update_interval: 2s
|
||||
on_value:
|
||||
- lambda: ESP_LOGI("rd", "READ a3 %.4f", x);
|
||||
|
||||
interval:
|
||||
- interval: 20ms
|
||||
then:
|
||||
- lambda: |-
|
||||
if (id(started)) snf::step(id(ads));
|
||||
@@ -0,0 +1,69 @@
|
||||
"""clean.py [--nacks] NAME...: per build, cases with at least one capture (NAME-ser.txt, -ser2, -ser3) that is exactly
|
||||
the expected stream or the expected stream cut by the chip's NACK; also every rc seen per case.
|
||||
--nacks: per capture, count the DIFF cases that are a chip NACK and list the others."""
|
||||
|
||||
import argparse
|
||||
from collections import Counter, defaultdict
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
from snlog import CAPTURES, load, nack_cut, runs # noqa: E402
|
||||
|
||||
|
||||
def clean(case: dict) -> bool:
|
||||
if case["verdict"] == "OK":
|
||||
return True
|
||||
return nack_cut(case.get("got", []), case.get("exp", [])) is not None
|
||||
|
||||
|
||||
def nacks(name: str) -> None:
|
||||
for s in CAPTURES:
|
||||
path = pathlib.Path(f"{name}-ser{s}.txt")
|
||||
if not path.exists():
|
||||
continue
|
||||
c: Counter[str] = Counter()
|
||||
other = []
|
||||
for label, case in load(path).items():
|
||||
if case["verdict"] == "OK":
|
||||
continue
|
||||
got, exp = case.get("got", []), case.get("exp", [])
|
||||
if (j := nack_cut(got, exp)) is not None:
|
||||
c[f"chip NACK at token {j}"] += 1
|
||||
else:
|
||||
other.append((label, " ".join(got)[:80], " ".join(exp)[:80]))
|
||||
print(path, dict(c), "other:", len(other))
|
||||
for o in other[:10]:
|
||||
print(" ", o)
|
||||
|
||||
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument(
|
||||
"--nacks", action="store_true", help="classify the DIFF cases of each capture"
|
||||
)
|
||||
ap.add_argument("names", nargs="+")
|
||||
args = ap.parse_args()
|
||||
if args.nacks:
|
||||
for name in args.names:
|
||||
nacks(name)
|
||||
sys.exit()
|
||||
allrc = defaultdict(set)
|
||||
for name in args.names:
|
||||
caps = runs(name)
|
||||
bad = [k for k in caps[0] if not any(k in r and clean(r[k]) for r in caps)]
|
||||
for r in caps:
|
||||
for k, case in r.items():
|
||||
allrc[k].add(case["rc"])
|
||||
print(
|
||||
name,
|
||||
"captures",
|
||||
len(caps),
|
||||
"cases",
|
||||
len(caps[0]),
|
||||
"with no clean capture:",
|
||||
bad,
|
||||
)
|
||||
print(
|
||||
"cases whose rc differs between any captures/builds:",
|
||||
[k for k, s in allrc.items() if len(s) > 1],
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
# flashrun.sh NAME YAML PORT SECONDS [OUTSUFFIX]: upload from TREE, then capture from a reset into NAME-serOUTSUFFIX.txt
|
||||
# and print the sniffed line count and the SNDONE line. clean.py and maj.py read NAME-ser.txt, -ser2 and -ser3.
|
||||
# env: TREE, PY
|
||||
D=$(cd "$(dirname "$0")" && pwd); S=$(dirname "$(dirname "$D")"); cd "$D" || exit 1
|
||||
N=${1:?name}; Y=${2:?yaml}; P=${3:?port}; SEC=${4:?seconds}; X=${5:-}; PY=${PY:-${TREE:?set TREE}/venv/bin/python}
|
||||
pkill -f "[s]erial_capture.py $P "; sleep 1
|
||||
PIO_CORE=${PIO_CORE:-$D/../pio} bash "$S/pio_build.sh" upload "$N" "$Y" "$P" || exit 1
|
||||
$PY "$S/serial_capture.py" "$P" "$SEC" "$N-ser$X.txt" --reset > /dev/null 2>&1
|
||||
grep -a -c "SN " "$N-ser$X.txt"; grep -a "SNDONE" "$N-ser$X.txt"
|
||||
@@ -0,0 +1,57 @@
|
||||
"""maj.py NAME_BASE NAME_PR: per case majority of rc and wire hash (reads: rc and verdict, since the chip's registers
|
||||
change between runs) over NAME-ser.txt, -ser2 and -ser3 (1 to 3 captures), then base vs PR and the alloc summary."""
|
||||
|
||||
import argparse
|
||||
from collections import Counter
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
from snlog import first_alloc, is_read, runs # noqa: E402
|
||||
|
||||
|
||||
def majority(name: str) -> tuple[dict, int, int]:
|
||||
"""Case -> (majority key, votes, max alloc); also the count of unstable cases and of captures."""
|
||||
caps = runs(name)
|
||||
out, unstable = {}, 0
|
||||
for k in caps[0]:
|
||||
c = Counter(
|
||||
(r[k]["rc"], r[k]["verdict"] if is_read(k) else r[k]["fnv"])
|
||||
for r in caps
|
||||
if k in r
|
||||
)
|
||||
v, n = c.most_common(1)[0]
|
||||
unstable += len(c) > 1
|
||||
out[k] = (v, n, max(r[k]["alloc"] for r in caps if k in r))
|
||||
return out, unstable, len(caps)
|
||||
|
||||
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument("base")
|
||||
ap.add_argument("pr")
|
||||
args = ap.parse_args()
|
||||
b, ub, nb = majority(args.base)
|
||||
p, up, np_ = majority(args.pr)
|
||||
print(
|
||||
f"cases {len(b)} {len(p)}; captures base {nb}, pr {np_}; cases whose captures disagree: base {ub}, pr {up}"
|
||||
)
|
||||
d = [k for k in b if k not in p or b[k][0] != p[k][0]]
|
||||
print("base vs pr majority differences:", len(d))
|
||||
for k in d[:30]:
|
||||
print(" ", k, "base", b[k][0], "pr", p[k][0] if k in p else "missing")
|
||||
weak = [k for k in b if 2 * b[k][1] <= nb or (k in p and 2 * p[k][1] <= np_)]
|
||||
print("cases without a majority:", weak)
|
||||
for name, maj in (("base", b), ("pr", p)):
|
||||
wr = [k for k in maj if k.startswith("wreg")]
|
||||
allocs = {k: v[2] for k, v in maj.items()}
|
||||
print(
|
||||
name,
|
||||
"write_register calls that allocated:",
|
||||
sum(allocs[k] > 0 for k in wr),
|
||||
"of",
|
||||
len(wr),
|
||||
"| first allocating len:",
|
||||
{s: first_alloc(allocs, s) for s in ("wreg8", "wreg16")},
|
||||
"| other allocs:",
|
||||
[k for k in maj if not k.startswith("wreg") and allocs[k]],
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
"""mksw.py TREE OUT: the tree's software bus (header + source) as one header for an ESP32 build."""
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
t = Path(sys.argv[1]) / "esphome/components/i2c"
|
||||
h = (
|
||||
(t / "i2c_bus_software.h")
|
||||
.read_text()
|
||||
.replace("#pragma once", "")
|
||||
.replace('#include "i2c_bus.h"', '#include "esphome/components/i2c/i2c_bus.h"')
|
||||
)
|
||||
c = (
|
||||
(t / "i2c_bus_software.cpp")
|
||||
.read_text()
|
||||
.replace('#include "i2c_bus_software.h"', "")
|
||||
)
|
||||
Path(sys.argv[2]).write_text(
|
||||
"#pragma once\n#define USE_LIBRETINY\n" + h + "\n" + c + "\n#undef USE_LIBRETINY\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
@@ -0,0 +1,53 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- pdslave.hpp
|
||||
- ../../../templates/i2c_fake/alloc_count.hpp
|
||||
- ../wiredecode.hpp
|
||||
- snf.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- -Wl,--wrap=malloc
|
||||
- -DSNF_PDSLAVE
|
||||
on_boot:
|
||||
- priority: 700
|
||||
then:
|
||||
- lambda: |-
|
||||
pds::install();
|
||||
snf::bus = id(bus_hw);
|
||||
- priority: -100
|
||||
then:
|
||||
- delay: 4s
|
||||
- lambda: id(started) = true;
|
||||
|
||||
esp32:
|
||||
board: pico32
|
||||
framework:
|
||||
type: esp-idf
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
baud_rate: 115200
|
||||
|
||||
globals:
|
||||
- id: started
|
||||
type: bool
|
||||
initial_value: "false"
|
||||
|
||||
i2c:
|
||||
id: bus_hw
|
||||
sda: GPIO32
|
||||
scl: GPIO33
|
||||
frequency: ${freq}
|
||||
scan: false
|
||||
|
||||
ads1115:
|
||||
- address: 0x48
|
||||
id: ads
|
||||
i2c_id: bus_hw
|
||||
|
||||
interval:
|
||||
- interval: 20ms
|
||||
then:
|
||||
- lambda: |-
|
||||
if (id(started)) snf::step(id(ads));
|
||||
@@ -0,0 +1,187 @@
|
||||
#pragma once
|
||||
// Software I2C target at 0x48 on the classic ESP32 master's own pads (GPIO32 SDA, GPIO33 SCL), after the
|
||||
// `i2c_isr_fault/inasim.hpp` pattern: a GPIO ISR on core 0 spins through each transaction and pulls a line low by
|
||||
// switching the pad's output mux to GPIO output 0. ACKs every byte written to 0x48, answers reads with A0, A1, ...;
|
||||
// NACKs other addresses. Every START, byte (with the ACK bit seen on the wire) and STOP goes into a log.
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include "esphome/core/log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "soc/gpio_reg.h"
|
||||
#include "soc/gpio_sig_map.h"
|
||||
#include "esp_cpu.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
namespace pds {
|
||||
static const char *const TAG = "pds";
|
||||
constexpr int SDA = 32, SCL = 33;
|
||||
constexpr uint32_t SDA_BIT = 1u << (SDA - 32), SCL_BIT = 1u << (SCL - 32);
|
||||
constexpr uint32_t LOW_VAL = SIG_GPIO_OUT_IDX | GPIO_FUNC0_OEN_SEL;
|
||||
constexpr uint32_t CPU_MHZ = 240;
|
||||
inline volatile uint32_t ®(uint32_t a) { return *reinterpret_cast<volatile uint32_t *>(a); }
|
||||
inline uint32_t out_sel_addr(int pin) { return GPIO_FUNC0_OUT_SEL_CFG_REG + 4 * pin; }
|
||||
inline uint32_t in() { return reg(GPIO_IN1_REG); }
|
||||
inline uint32_t rel_sda = 0;
|
||||
|
||||
// log codes: 1 START, 2 RESTART, 3 STOP, 4 written byte / 6 read byte (next: value, ack), 5 idle abort
|
||||
constexpr size_t LOGN = 4096;
|
||||
inline DRAM_ATTR uint8_t lg[LOGN];
|
||||
inline volatile uint32_t ln = 0, lovf = 0;
|
||||
inline void put(uint8_t v) {
|
||||
if (ln < LOGN)
|
||||
lg[ln++] = v;
|
||||
else
|
||||
lovf = lovf + 1;
|
||||
}
|
||||
|
||||
inline void sda_low(bool low) { reg(out_sel_addr(SDA)) = low ? LOW_VAL : rel_sda; }
|
||||
|
||||
inline void run_transaction() {
|
||||
enum { ADDR, WDATA, RDATA, IGNORE } phase = ADDR;
|
||||
uint32_t prev = in(), last = esp_cpu_get_cycle_count();
|
||||
uint8_t byte = 0, cnt = 0, out = 0, rd = 0, pend_kind = 4, pend_val = 0;
|
||||
bool acked = false;
|
||||
put(1);
|
||||
reg(GPIO_OUT1_W1TC_REG) = SDA_BIT;
|
||||
reg(GPIO_ENABLE1_W1TS_REG) = SDA_BIT;
|
||||
for (;;) {
|
||||
uint32_t v = in();
|
||||
uint32_t now = esp_cpu_get_cycle_count();
|
||||
if (v == prev) {
|
||||
if (now - last > 20000u * CPU_MHZ) {
|
||||
put(5);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
last = now;
|
||||
uint32_t ch = v ^ prev;
|
||||
prev = v;
|
||||
if (ch & SCL_BIT) {
|
||||
if (v & SCL_BIT) {
|
||||
cnt++;
|
||||
if (cnt <= 8 && phase != RDATA)
|
||||
byte = (byte << 1) | ((v & SDA_BIT) ? 1 : 0);
|
||||
else if (cnt == 9) {
|
||||
// the ACK bit as it is on the wire
|
||||
bool ack = !(v & SDA_BIT);
|
||||
put(pend_kind);
|
||||
put(pend_val);
|
||||
put(ack ? 1 : 0);
|
||||
if (phase == RDATA && !ack)
|
||||
phase = IGNORE;
|
||||
}
|
||||
} else {
|
||||
if (cnt == 8) {
|
||||
pend_kind = phase == RDATA ? 6 : 4;
|
||||
pend_val = phase == RDATA ? out : byte;
|
||||
if (phase == ADDR) {
|
||||
if ((byte >> 1) == 0x48) {
|
||||
sda_low(true);
|
||||
acked = true;
|
||||
phase = (byte & 1) ? RDATA : WDATA;
|
||||
rd = 0;
|
||||
} else {
|
||||
phase = IGNORE;
|
||||
}
|
||||
} else if (phase == WDATA) {
|
||||
sda_low(true);
|
||||
acked = true;
|
||||
} else if (phase == RDATA) {
|
||||
sda_low(false); // master's ACK slot
|
||||
}
|
||||
} else if (cnt == 9) {
|
||||
if (acked) {
|
||||
sda_low(false);
|
||||
acked = false;
|
||||
}
|
||||
if (phase == RDATA) {
|
||||
out = 0xA0 + rd++;
|
||||
sda_low(!(out & 0x80));
|
||||
}
|
||||
cnt = 0;
|
||||
byte = 0;
|
||||
} else if (cnt >= 1 && cnt <= 7 && phase == RDATA) {
|
||||
sda_low(!(out & (0x80 >> cnt)));
|
||||
}
|
||||
}
|
||||
} else if ((ch & SDA_BIT) && (v & SCL_BIT)) {
|
||||
if (v & SDA_BIT) {
|
||||
put(3);
|
||||
break;
|
||||
}
|
||||
put(2);
|
||||
sda_low(false);
|
||||
phase = ADDR;
|
||||
cnt = 0;
|
||||
byte = 0;
|
||||
acked = false;
|
||||
}
|
||||
}
|
||||
sda_low(false);
|
||||
}
|
||||
|
||||
inline void slave_isr(void *) {
|
||||
reg(GPIO_STATUS1_W1TC_REG) = SDA_BIT;
|
||||
uint32_t v = in();
|
||||
if (!(v & SCL_BIT) || (v & SDA_BIT))
|
||||
return;
|
||||
run_transaction();
|
||||
reg(GPIO_STATUS1_W1TC_REG) = SDA_BIT;
|
||||
}
|
||||
|
||||
inline volatile bool installed = false;
|
||||
inline void install_task(void *) {
|
||||
rel_sda = reg(out_sel_addr(SDA));
|
||||
gpio_install_isr_service(0);
|
||||
gpio_set_intr_type((gpio_num_t) SDA, GPIO_INTR_NEGEDGE);
|
||||
esp_err_t e = gpio_isr_handler_add((gpio_num_t) SDA, slave_isr, nullptr);
|
||||
gpio_intr_enable((gpio_num_t) SDA);
|
||||
ESP_LOGI(TAG, "slave on core %d handler %d rel_sda 0x%x", xPortGetCoreID(), e, (unsigned) rel_sda);
|
||||
installed = true;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
inline void install() {
|
||||
xTaskCreatePinnedToCore(install_task, "pdsinst", 3072, nullptr, 5, nullptr, 0);
|
||||
while (!installed)
|
||||
vTaskDelay(1);
|
||||
}
|
||||
|
||||
inline std::string tokens() {
|
||||
std::string s;
|
||||
char t[8];
|
||||
for (uint32_t i = 0; i < ln; i++) {
|
||||
switch (lg[i]) {
|
||||
case 1:
|
||||
s += "S ";
|
||||
break;
|
||||
case 2:
|
||||
s += "Sr ";
|
||||
break;
|
||||
case 3:
|
||||
s += "P ";
|
||||
break;
|
||||
case 5:
|
||||
s += "IDLE ";
|
||||
break;
|
||||
case 4:
|
||||
case 6: {
|
||||
if (i + 2 >= ln) { // byte record cut off by a full log
|
||||
s += "? ";
|
||||
i = ln;
|
||||
break;
|
||||
}
|
||||
uint8_t b = lg[i + 1], a = lg[i + 2];
|
||||
snprintf(t, sizeof t, lg[i] == 6 ? "r%02X%c " : "%02X%c ", b, a ? '+' : '-');
|
||||
i += 2;
|
||||
s += t;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
s += "? ";
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
} // namespace pds
|
||||
@@ -0,0 +1,245 @@
|
||||
#pragma once
|
||||
// esphome#22780 device check: GPIO interrupt sniffer on the I2C pads, a sweep of I2CDevice call shapes, and a
|
||||
// malloc counter (alloc_count.hpp, -Wl,--wrap=malloc). One "SN" line per case: rc, mallocs, decoded wire stream
|
||||
// hash, match with the stream the call should produce.
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <driver/gpio.h>
|
||||
#include <soc/gpio_reg.h>
|
||||
#include <esp_attr.h>
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "alloc_count.hpp"
|
||||
#include "wiredecode.hpp"
|
||||
#ifdef SNF_PDSLAVE
|
||||
// the board is its own target (pdslave.hpp); its token stream replaces the pad sniffer's decode
|
||||
#include "pdslave.hpp"
|
||||
#endif
|
||||
|
||||
namespace snf {
|
||||
static const char *const TAG = "snf";
|
||||
#ifndef SNF_SDA
|
||||
#define SNF_SDA 4
|
||||
#endif
|
||||
#ifndef SNF_SCL
|
||||
#define SNF_SCL 5
|
||||
#endif
|
||||
static constexpr size_t N = 24000;
|
||||
static DRAM_ATTR uint8_t buf[N];
|
||||
static volatile DRAM_ATTR uint32_t n = 0;
|
||||
static volatile DRAM_ATTR uint32_t ovf = 0;
|
||||
static volatile DRAM_ATTR uint8_t last = 3;
|
||||
static volatile DRAM_ATTR bool armed = false;
|
||||
static uint8_t start = 3; // levels when the sniffer was armed
|
||||
|
||||
static void IRAM_ATTR isr(void *) {
|
||||
uint32_t v = REG_READ(GPIO_IN_REG);
|
||||
uint8_t s = ((v >> SNF_SDA) & 1) | (((v >> SNF_SCL) & 1) << 1);
|
||||
if (!armed || s == last)
|
||||
return;
|
||||
last = s;
|
||||
if (n < N)
|
||||
buf[n++] = s;
|
||||
else
|
||||
ovf = ovf + 1;
|
||||
}
|
||||
|
||||
static void install() {
|
||||
gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
|
||||
for (int p : {SNF_SDA, SNF_SCL}) {
|
||||
gpio_set_intr_type((gpio_num_t) p, GPIO_INTR_ANYEDGE);
|
||||
gpio_isr_handler_add((gpio_num_t) p, isr, nullptr);
|
||||
gpio_intr_enable((gpio_num_t) p);
|
||||
}
|
||||
ESP_LOGI(TAG, "sniffer on SDA %d SCL %d", SNF_SDA, SNF_SCL);
|
||||
}
|
||||
|
||||
static i2c::I2CBus *bus = nullptr; // the bus the device uses (hardware or the software copy)
|
||||
|
||||
static void wb(std::string &e, uint8_t b) {
|
||||
char t[8];
|
||||
snprintf(t, sizeof t, "%02X+ ", b);
|
||||
e += t;
|
||||
}
|
||||
static void rb(std::string &e, const uint8_t *r, size_t k) {
|
||||
char t[8];
|
||||
for (size_t i = 0; i < k; i++) {
|
||||
snprintf(t, sizeof t, "r%02X%c ", r[i], i + 1 < k ? '+' : '-');
|
||||
e += t;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t data[320];
|
||||
static uint8_t rdata[40];
|
||||
static uint32_t idx = 0, nok = 0, ndiff = 0, namb = 0, nalloc = 0;
|
||||
static bool done = false;
|
||||
|
||||
static void arm() {
|
||||
n = 0;
|
||||
ovf = 0;
|
||||
uint32_t v = REG_READ(GPIO_IN_REG);
|
||||
start = ((v >> SNF_SDA) & 1) | (((v >> SNF_SCL) & 1) << 1);
|
||||
last = start;
|
||||
alloc_count::count = 0;
|
||||
alloc_count::window = true;
|
||||
#ifdef SNF_PDSLAVE
|
||||
pds::ln = 0;
|
||||
pds::lovf = 0;
|
||||
#endif
|
||||
armed = true;
|
||||
}
|
||||
static void disarm() {
|
||||
armed = false;
|
||||
alloc_count::window = false;
|
||||
}
|
||||
|
||||
// Runs one case and returns false when the sweep is over
|
||||
static bool step(i2c::I2CDevice *dev) {
|
||||
if (done)
|
||||
return false;
|
||||
const uint8_t A = 0x48;
|
||||
std::string label, exp;
|
||||
#ifdef SNF_PDSLAVE
|
||||
if (idx == 0) {
|
||||
// warm the target ISR (it runs from flash) so the first case is not lost to a cache miss
|
||||
bus->write_readv(0x48, nullptr, 0, nullptr, 0);
|
||||
delay(5);
|
||||
}
|
||||
#endif
|
||||
uint32_t i = idx++;
|
||||
for (size_t k = 0; k < sizeof data; k++)
|
||||
data[k] = static_cast<uint8_t>(0x11 + i + k * 7);
|
||||
i2c::ErrorCode rc;
|
||||
static constexpr uint32_t W8 = 301, W16 = 301; // write_register lengths 0..300
|
||||
if (i < W8) {
|
||||
size_t len = i;
|
||||
label = "wreg8 len=" + std::to_string(len);
|
||||
arm();
|
||||
rc = dev->write_register(0x02, data, len);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, A << 1);
|
||||
wb(exp, 0x02);
|
||||
for (size_t k = 0; k < len; k++)
|
||||
wb(exp, data[k]);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16) {
|
||||
size_t len = i - W8;
|
||||
label = "wreg16 len=" + std::to_string(len);
|
||||
arm();
|
||||
rc = dev->write_register16(0x0203, data, len);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, A << 1);
|
||||
wb(exp, 0x02);
|
||||
wb(exp, 0x03);
|
||||
for (size_t k = 0; k < len; k++)
|
||||
wb(exp, data[k]);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16 + 10) {
|
||||
static const size_t ns[] = {1, 2, 3, 4, 8};
|
||||
uint32_t j = i - W8 - W16;
|
||||
size_t k = ns[j % 5];
|
||||
bool sixteen = j >= 5;
|
||||
label = std::string(sixteen ? "rreg16" : "rreg8") + " n=" + std::to_string(k);
|
||||
memset(rdata, 0, sizeof rdata);
|
||||
arm();
|
||||
rc = sixteen ? dev->read_register16(0x0100, rdata, k) : dev->read_register(0x01, rdata, k);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, A << 1);
|
||||
wb(exp, 0x01);
|
||||
if (sixteen)
|
||||
wb(exp, 0x00);
|
||||
exp += "Sr ";
|
||||
wb(exp, (A << 1) | 1);
|
||||
rb(exp, rdata, k);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16 + 13) {
|
||||
size_t k = i - (W8 + W16 + 10) + 1;
|
||||
label = "read n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = dev->read(rdata, k);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, (A << 1) | 1);
|
||||
rb(exp, rdata, k);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16 + 16) {
|
||||
static const uint8_t p1[] = {0x02};
|
||||
size_t k = i - (W8 + W16 + 13) + 1;
|
||||
label = "write1+read n=" + std::to_string(k);
|
||||
arm();
|
||||
rc = dev->write_read(p1, 1, rdata, k);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
wb(exp, A << 1);
|
||||
wb(exp, 0x02);
|
||||
exp += "Sr ";
|
||||
wb(exp, (A << 1) | 1);
|
||||
rb(exp, rdata, k);
|
||||
exp += "P ";
|
||||
} else if (i < W8 + W16 + 16 + 112) {
|
||||
uint8_t a = 8 + (i - (W8 + W16 + 16));
|
||||
label = "probe " + std::to_string(a);
|
||||
arm();
|
||||
rc = bus->write_readv(a, nullptr, 0, nullptr, 0);
|
||||
disarm();
|
||||
exp = "S ";
|
||||
char t[8];
|
||||
snprintf(t, sizeof t, "%02X%c ", a << 1, a == A ? '+' : '-');
|
||||
exp += t;
|
||||
exp += "P ";
|
||||
} else {
|
||||
done = true;
|
||||
ESP_LOGI(TAG, "SNDONE cases=%u ok=%u diff=%u amb=%u alloc_calls=%u", (unsigned) idx - 1, (unsigned) nok,
|
||||
(unsigned) ndiff, (unsigned) namb, (unsigned) nalloc);
|
||||
return false;
|
||||
}
|
||||
uint32_t amb = 0;
|
||||
#ifdef SNF_PDSLAVE
|
||||
delayMicroseconds(500);
|
||||
std::string got = pds::tokens();
|
||||
ovf = pds::lovf;
|
||||
n = pds::ln;
|
||||
#else
|
||||
std::string got = wiredecode::decode(buf, n, start, amb);
|
||||
#endif
|
||||
bool ok = got == exp && ovf == 0;
|
||||
nok += ok;
|
||||
ndiff += !ok;
|
||||
namb += amb;
|
||||
nalloc += alloc_count::count != 0;
|
||||
ESP_LOGI(TAG, "SN %s|rc=%d|alloc=%u|ev=%u|amb=%u|ovf=%u|fnv=%08X|%s", label.c_str(), (int) rc,
|
||||
(unsigned) alloc_count::count, (unsigned) n, (unsigned) amb, (unsigned) ovf, (unsigned) fnv1a_hash(got),
|
||||
ok ? "OK" : "DIFF");
|
||||
if (!ok) {
|
||||
ESP_LOGI(TAG, "SNGOT %s", got.substr(0, 200).c_str());
|
||||
ESP_LOGI(TAG, "SNEXP %s", exp.substr(0, 200).c_str());
|
||||
}
|
||||
App.feed_wdt();
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef SNF_SW
|
||||
static i2c::I2CBus *make_sw() {
|
||||
auto *sda = new esp32::ESP32InternalGPIOPin();
|
||||
sda->set_pin((gpio_num_t) SNF_SDA);
|
||||
sda->set_flags(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
|
||||
auto *scl = new esp32::ESP32InternalGPIOPin();
|
||||
scl->set_pin((gpio_num_t) SNF_SCL);
|
||||
scl->set_flags(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
|
||||
auto *b = new i2c::SoftwareI2CBus();
|
||||
b->set_sda_pin(sda);
|
||||
b->set_scl_pin(scl);
|
||||
b->set_frequency(SNF_SW_FREQ);
|
||||
b->set_scan(true);
|
||||
b->set_timeout(1000);
|
||||
b->setup();
|
||||
b->dump_config();
|
||||
return b;
|
||||
}
|
||||
#endif
|
||||
} // namespace snf
|
||||
@@ -0,0 +1,81 @@
|
||||
"""compare.py BASE PR: wire and return code per bus and case must match; summarize allocations and IDF job lists."""
|
||||
|
||||
from collections import Counter, defaultdict
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
from snlog import first_alloc # noqa: E402
|
||||
|
||||
|
||||
def load(p):
|
||||
rec, jobs = {}, {}
|
||||
with pathlib.Path(p).open(encoding="utf-8") as fh:
|
||||
for line in fh:
|
||||
line = line.rstrip("\n")
|
||||
if line.startswith("ALLOC_PROBE"):
|
||||
continue
|
||||
parts = line.split("|")
|
||||
if parts[0] == "J":
|
||||
jobs[(parts[1], parts[2])] = parts[3].strip()
|
||||
continue
|
||||
bus, case, rc, alloc, wire = parts
|
||||
rec[(bus, case)] = (rc, int(alloc.split("=")[1]), wire.strip())
|
||||
return rec, jobs
|
||||
|
||||
|
||||
b, bj = load(sys.argv[1])
|
||||
p, pj = load(sys.argv[2])
|
||||
assert b.keys() == p.keys(), "case lists differ"
|
||||
diff = [k for k in b if b[k][0] != p[k][0] or b[k][2] != p[k][2]]
|
||||
per_bus = Counter(k[0] for k in b)
|
||||
print("cases per bus:", dict(per_bus))
|
||||
print("wire/rc differences:", len(diff))
|
||||
for k in diff[:40]:
|
||||
print(
|
||||
" DIFF",
|
||||
k,
|
||||
"\n base",
|
||||
b[k][0],
|
||||
b[k][2][:160],
|
||||
"\n pr ",
|
||||
p[k][0],
|
||||
p[k][2][:160],
|
||||
)
|
||||
alloc = defaultdict(
|
||||
lambda: [0, 0, 0, 0]
|
||||
) # base cases with alloc, pr cases with alloc, base total, pr total
|
||||
for k in b:
|
||||
if not k[1].startswith(("wreg8", "wreg16")):
|
||||
continue
|
||||
a = alloc[k[0]]
|
||||
a[0] += b[k][1] > 0
|
||||
a[1] += p[k][1] > 0
|
||||
a[2] += b[k][1]
|
||||
a[3] += p[k][1]
|
||||
print("write_register/16 calls that allocated (base, pr):")
|
||||
for bus, a in alloc.items():
|
||||
print(
|
||||
f" {bus:8s} base {a[0]} calls ({a[2]} mallocs) pr {a[1]} calls ({a[3]} mallocs)"
|
||||
)
|
||||
# first length that allocates, per bus and shape
|
||||
for bus in per_bus:
|
||||
ab = {c: r[1] for (x, c), r in b.items() if x == bus}
|
||||
ap = {c: r[1] for (x, c), r in p.items() if x == bus}
|
||||
for shape in ("wreg8", "wreg16"):
|
||||
fb, fp = first_alloc(ab, shape), first_alloc(ap, shape)
|
||||
print(f" first allocating length {bus} {shape}: base {fb} pr {fp}")
|
||||
other = [
|
||||
k
|
||||
for k in b
|
||||
if not k[1].startswith(("wreg8", "wreg16", "wedge wreg", "absent wreg"))
|
||||
and (b[k][1] or p[k][1])
|
||||
]
|
||||
print("other calls with allocations:", other[:10])
|
||||
jd = Counter()
|
||||
for k in bj:
|
||||
if bj[k] != pj.get(k):
|
||||
jd[(bj[k], pj.get(k))] += 1
|
||||
print("IDF job list changes (base -> pr: count):")
|
||||
for (x, y), n in sorted(jd.items(), key=lambda t: -t[1])[:20]:
|
||||
print(f" {x!r} -> {y!r}: {n}")
|
||||
@@ -0,0 +1,33 @@
|
||||
"""extract.py TREE OUTDIR: copy the write_readv/transfer_ definitions of the Arduino and ESP-IDF buses out of TREE."""
|
||||
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
tree, out = Path(sys.argv[1]), Path(sys.argv[2])
|
||||
for cls, src, dst in (
|
||||
("ArduinoI2CBus", "i2c_bus_arduino.cpp", "wab_gen_arduino.inc"),
|
||||
("IDFI2CBus", "i2c_bus_esp_idf.cpp", "wab_gen_idf.inc"),
|
||||
):
|
||||
lines = (tree / "esphome/components/i2c" / src).read_text().splitlines()
|
||||
pat = re.compile(rf"^ErrorCode {cls}::(write_readv|transfer_)\(")
|
||||
starts = [i for i, line in enumerate(lines) if pat.match(line)]
|
||||
assert len(starts) == 1, (src, starts)
|
||||
s = starts[0]
|
||||
e = next(i for i in range(s, len(lines)) if lines[i] == "}")
|
||||
(out / dst).write_text("\n".join(lines[s : e + 1]) + "\n")
|
||||
print(dst, lines[s].split("(")[0], e - s + 1, "lines")
|
||||
|
||||
shutil.copy(
|
||||
tree / "esphome/components/i2c/i2c_bus_software.cpp", out / "wab_sw_impl.inc"
|
||||
)
|
||||
shutil.copy(
|
||||
tree / "esphome/components/i2c/i2c_bus_software.h", out / "i2c_bus_software.h"
|
||||
)
|
||||
h = out / "i2c_bus_software.h"
|
||||
h.write_text(
|
||||
h.read_text().replace(
|
||||
'#include "i2c_bus.h"', '#include "esphome/components/i2c/i2c_bus.h"'
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
# run.sh TREE OUTFILE: copy the harness into TREE's tests/components/i2c, generate the extracted bus bodies, run the
|
||||
# unit test and remove the harness again. env: PY (default TREE's venv python)
|
||||
T=$(cd "${1:?tree}" && pwd); O=$(cd "$(dirname "${2:?out}")" && pwd)/$(basename "$2")
|
||||
H=$(cd "$(dirname "$0")" && pwd); PY=${PY:-$T/venv/bin/python}
|
||||
D=$T/tests/components/i2c
|
||||
# The sources are stored as .cpp.example, .h.example and .hpp so the repo's C++ lints skip them; copy them back.
|
||||
FILES=(wab_common.h wab_ard8266.cpp wab_ardpico.cpp wab_idf.cpp wab_sw.cpp test_wab.cpp benchmark.yaml)
|
||||
G=$(mktemp -d)
|
||||
cleanup() { for f in "${FILES[@]}"; do rm -f "$D/$f"; done; rm -rf "$G"; }
|
||||
trap cleanup EXIT
|
||||
trap 'exit 130' INT TERM
|
||||
cp "$H/wab_common.hpp" "$D/wab_common.h"
|
||||
for f in "${FILES[@]:1:5}"; do cp "$H/$f.example" "$D/$f"; done
|
||||
mkdir -p "$G/gen" "$G/stubs/driver"
|
||||
for f in Arduino.h Wire.h driver/i2c_master.h; do cp "$H/stub/$f.example" "$G/stubs/$f"; done
|
||||
$PY "$H/extract.py" "$T" "$G/gen"
|
||||
printf "esphome:\n build_flags:\n - -I%s\n - -I%s\n - -I%s\n" "$D" "$G/stubs" "$G/gen" > "$D/benchmark.yaml"
|
||||
(cd "$T" && WAB_OUT=$O PYTHONPATH=$T $PY script/cpp_unit_test.py i2c) > "${O%.txt}-build.log" 2>&1
|
||||
rc=$?
|
||||
echo "rc=$rc"; tail -5 "${O%.txt}-build.log"
|
||||
exit $rc
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#define INPUT 0x01
|
||||
#define OUTPUT 0x03
|
||||
#define INPUT_PULLUP 0x05
|
||||
#define LOW 0
|
||||
#define HIGH 1
|
||||
inline void pinMode(uint8_t, uint8_t) {}
|
||||
inline void digitalWrite(uint8_t, uint8_t) {}
|
||||
inline int digitalRead(uint8_t) { return HIGH; }
|
||||
@@ -0,0 +1,207 @@
|
||||
#pragma once
|
||||
// TwoWire models of the cores ESPHome uses, with the cores' own buffer rules. Select with WAB_WIRE_ESP8266 or
|
||||
// WAB_WIRE_PICO before including. Bytes go on the wire through wab::wire() with ACKs from wab::slave().
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include "wab_common.h"
|
||||
|
||||
#if defined(WAB_WIRE_ESP8266)
|
||||
#define TwoWire TwoWire8266
|
||||
#define Wire Wire8266
|
||||
// esp8266 core 3.1.2-esphome.3 libraries/Wire: I2C_BUFFER_LENGTH 128
|
||||
class TwoWire {
|
||||
public:
|
||||
static constexpr size_t LEN = 128;
|
||||
void begin(int, int) {}
|
||||
void setClock(uint32_t) {}
|
||||
void setClockStretchLimit(uint32_t) {}
|
||||
void beginTransmission(uint8_t address) {
|
||||
transmitting = 1;
|
||||
txAddress = address;
|
||||
txBufferIndex = 0;
|
||||
txBufferLength = 0;
|
||||
}
|
||||
size_t write(uint8_t data) {
|
||||
if (transmitting) {
|
||||
if (txBufferLength >= LEN)
|
||||
return 0; // setWriteError()
|
||||
txBuffer[txBufferIndex] = data;
|
||||
++txBufferIndex;
|
||||
txBufferLength = txBufferIndex;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
size_t write(const uint8_t *data, size_t quantity) {
|
||||
if (transmitting) {
|
||||
for (size_t i = 0; i < quantity; ++i)
|
||||
if (!write(data[i]))
|
||||
return i;
|
||||
}
|
||||
return quantity;
|
||||
}
|
||||
uint8_t endTransmission(uint8_t sendStop) {
|
||||
// twi_writeTo: 0 ok, 2 address NACK, 3 data NACK; STOP when sendStop (also after a NACK)
|
||||
int8_t ret = 0;
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
w.start();
|
||||
uint8_t ab = txAddress << 1;
|
||||
bool ack = s.address(ab);
|
||||
w.wbyte(ab, ack);
|
||||
if (!ack) {
|
||||
ret = 2;
|
||||
} else {
|
||||
for (size_t i = 0; i < txBufferLength; i++) {
|
||||
bool a = s.data(txBuffer[i]);
|
||||
w.wbyte(txBuffer[i], a);
|
||||
if (!a) {
|
||||
ret = 3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sendStop || ret != 0)
|
||||
w.stop();
|
||||
txBufferIndex = 0;
|
||||
txBufferLength = 0;
|
||||
transmitting = 0;
|
||||
return ret;
|
||||
}
|
||||
size_t requestFrom(uint8_t address, size_t size, bool sendStop) {
|
||||
if (size > LEN)
|
||||
size = LEN;
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
w.start();
|
||||
uint8_t ab = (address << 1) | 1;
|
||||
bool ack = s.address(ab);
|
||||
w.wbyte(ab, ack);
|
||||
size_t read = 0;
|
||||
if (ack) {
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
rxBuffer[i] = s.next_read();
|
||||
w.rbyte(rxBuffer[i], i + 1 < size);
|
||||
}
|
||||
read = size;
|
||||
}
|
||||
if (sendStop || !ack)
|
||||
w.stop();
|
||||
rxBufferIndex = 0;
|
||||
rxBufferLength = read;
|
||||
return read;
|
||||
}
|
||||
int read() { return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex++] : -1; }
|
||||
void wab_reset() { transmitting = 0; txBufferLength = txBufferIndex = 0; }
|
||||
|
||||
uint8_t transmitting{0};
|
||||
uint8_t txAddress{0};
|
||||
size_t txBufferIndex{0}, txBufferLength{0}, rxBufferIndex{0}, rxBufferLength{0};
|
||||
uint8_t txBuffer[LEN]{}, rxBuffer[LEN]{};
|
||||
};
|
||||
extern TwoWire Wire;
|
||||
|
||||
#elif defined(WAB_WIRE_PICO)
|
||||
#define TwoWire TwoWirePico
|
||||
#define Wire WirePico
|
||||
#define Wire1 Wire1Pico
|
||||
// arduino-pico 6.3.2 libraries/Wire: WIRE_BUFFER_SIZE 256, one buffer for TX and RX
|
||||
#ifndef WIRE_BUFFER_SIZE
|
||||
#define WIRE_BUFFER_SIZE 256
|
||||
#endif
|
||||
class TwoWire {
|
||||
public:
|
||||
static constexpr size_t LEN = WIRE_BUFFER_SIZE;
|
||||
void setSDA(int) {}
|
||||
void setSCL(int) {}
|
||||
void begin() {}
|
||||
void setClock(uint32_t) {}
|
||||
void setTimeout(uint32_t) {}
|
||||
void beginTransmission(uint8_t addr) {
|
||||
if (!_running || _txBegun)
|
||||
return; // ERROR
|
||||
_addr = addr;
|
||||
_buffLen = 0;
|
||||
_buffOff = 0;
|
||||
_txBegun = true;
|
||||
}
|
||||
size_t write(uint8_t ucData) {
|
||||
if (!_running)
|
||||
return 0;
|
||||
if (!_txBegun || (_buffLen == int(LEN)))
|
||||
return 0;
|
||||
_buff[_buffLen++] = ucData;
|
||||
return 1;
|
||||
}
|
||||
size_t write(const uint8_t *data, size_t quantity) {
|
||||
for (size_t i = 0; i < quantity; ++i)
|
||||
if (!write(data[i]))
|
||||
return i;
|
||||
return quantity;
|
||||
}
|
||||
uint8_t endTransmission(bool stopBit) {
|
||||
if (!_running || !_txBegun)
|
||||
return 4;
|
||||
_txBegun = false;
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
if (!_buffLen) {
|
||||
// _probe(): its own START, address, STOP
|
||||
w.start();
|
||||
uint8_t ab = _addr << 1;
|
||||
bool ack = s.address(ab);
|
||||
w.wbyte(ab, ack);
|
||||
w.stop();
|
||||
return ack ? 0 : 2;
|
||||
}
|
||||
// i2c_write_blocking_until: abort (with STOP) on any NACK, returns PICO_ERROR_GENERIC
|
||||
w.start();
|
||||
uint8_t ab = _addr << 1;
|
||||
bool ok = s.address(ab);
|
||||
w.wbyte(ab, ok);
|
||||
for (int i = 0; ok && i < _buffLen; i++) {
|
||||
ok = s.data(_buff[i]);
|
||||
w.wbyte(_buff[i], ok);
|
||||
}
|
||||
if (stopBit || !ok)
|
||||
w.stop();
|
||||
_buffLen = 0;
|
||||
return ok ? 0 : 4;
|
||||
}
|
||||
size_t requestFrom(uint8_t address, size_t quantity, bool stopBit) {
|
||||
if (!_running || _txBegun || !quantity || (quantity > LEN))
|
||||
return 0;
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
w.start();
|
||||
uint8_t ab = (address << 1) | 1;
|
||||
bool ack = s.address(ab);
|
||||
w.wbyte(ab, ack);
|
||||
_buffOff = 0;
|
||||
if (!ack) {
|
||||
w.stop();
|
||||
_buffLen = 0;
|
||||
return 0;
|
||||
}
|
||||
for (size_t i = 0; i < quantity; i++) {
|
||||
_buff[i] = s.next_read();
|
||||
w.rbyte(_buff[i], i + 1 < quantity);
|
||||
}
|
||||
if (stopBit)
|
||||
w.stop();
|
||||
_buffLen = quantity;
|
||||
return quantity;
|
||||
}
|
||||
int read() { return _buffOff < _buffLen ? _buff[_buffOff++] : -1; }
|
||||
void wab_reset() { _txBegun = false; _buffLen = _buffOff = 0; }
|
||||
|
||||
bool _running{true};
|
||||
bool _txBegun{false};
|
||||
uint8_t _addr{0};
|
||||
int _buffLen{0}, _buffOff{0};
|
||||
uint8_t _buff[LEN]{};
|
||||
};
|
||||
extern TwoWire Wire;
|
||||
extern TwoWire Wire1;
|
||||
#else
|
||||
#error "select a Wire model"
|
||||
#endif
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
// Types of ESP-IDF 5.5.5 esp_driver_i2c used by i2c_bus_esp_idf, with an execute model that puts the jobs on the
|
||||
// wab wire and applies the driver's own argument checks (command count, NACK before STOP).
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
typedef int esp_err_t;
|
||||
#define ESP_OK 0
|
||||
#define ESP_FAIL -1
|
||||
#define ESP_ERR_INVALID_ARG 0x102
|
||||
#define ESP_ERR_INVALID_STATE 0x103
|
||||
#define ESP_ERR_INVALID_RESPONSE 0x108
|
||||
#define ESP_ERR_TIMEOUT 0x107
|
||||
inline const char *esp_err_to_name(esp_err_t) { return "err"; }
|
||||
typedef int i2c_port_t;
|
||||
typedef enum { I2C_MASTER_WRITE = 0, I2C_MASTER_READ } i2c_rw_t;
|
||||
typedef enum { I2C_ACK_VAL = 0, I2C_NACK_VAL = 1 } i2c_ack_value_t;
|
||||
typedef enum { I2C_MASTER_CMD_START, I2C_MASTER_CMD_WRITE, I2C_MASTER_CMD_READ, I2C_MASTER_CMD_STOP } i2c_master_command_t;
|
||||
typedef struct i2c_master_bus_t *i2c_master_bus_handle_t;
|
||||
typedef struct i2c_master_dev_t *i2c_master_dev_handle_t;
|
||||
typedef struct {
|
||||
i2c_master_command_t command;
|
||||
union {
|
||||
struct {
|
||||
bool ack_check;
|
||||
uint8_t *data;
|
||||
size_t total_bytes;
|
||||
} write;
|
||||
struct {
|
||||
i2c_ack_value_t ack_value;
|
||||
uint8_t *data;
|
||||
size_t total_bytes;
|
||||
} read;
|
||||
};
|
||||
} i2c_operation_job_t;
|
||||
#ifndef SOC_I2C_CMD_REG_NUM
|
||||
#define SOC_I2C_CMD_REG_NUM 8 // C2, C3, C6, S3, P4; 16 on ESP32 and S2
|
||||
#endif
|
||||
esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t dev, i2c_operation_job_t *ops, size_t n,
|
||||
int timeout_ms);
|
||||
@@ -0,0 +1,208 @@
|
||||
// Drives every I2CDevice call shape through each bus model and writes one line per call to $WAB_OUT.
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#ifndef __has_feature
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
|
||||
#define WAB_ASAN 1
|
||||
extern "C" int __sanitizer_install_malloc_and_free_hooks(void (*malloc_hook)(const volatile void *, size_t),
|
||||
void (*free_hook)(const volatile void *));
|
||||
#endif
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "wab_common.h"
|
||||
|
||||
esphome::i2c::I2CBus *wab_make_esp8266();
|
||||
esphome::i2c::I2CBus *wab_make_pico();
|
||||
esphome::i2c::I2CBus *wab_make_idf();
|
||||
esphome::i2c::I2CBus *wab_make_sw();
|
||||
void wab_reset_esp8266();
|
||||
void wab_reset_pico();
|
||||
void wab_reset_sw();
|
||||
|
||||
namespace wab {
|
||||
static volatile bool g_window = false;
|
||||
static volatile size_t g_allocs = 0;
|
||||
#ifdef WAB_ASAN
|
||||
static void on_malloc(const volatile void *, size_t) {
|
||||
if (g_window)
|
||||
g_allocs = g_allocs + 1;
|
||||
}
|
||||
static void on_free(const volatile void *) {}
|
||||
static bool install() {
|
||||
__sanitizer_install_malloc_and_free_hooks(on_malloc, on_free);
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
typedef void(malloc_logger_t)(uint32_t type, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t result,
|
||||
uint32_t num_hot_frames_to_skip);
|
||||
extern "C" malloc_logger_t *malloc_logger;
|
||||
static void on_log(uint32_t type, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uint32_t) {
|
||||
// MALLOC_LOG_TYPE_ALLOCATE = 2
|
||||
if (g_window && (type & 2))
|
||||
g_allocs = g_allocs + 1;
|
||||
}
|
||||
static bool install() {
|
||||
malloc_logger = on_log;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
static bool g_installed = install();
|
||||
void alloc_window(bool on) {
|
||||
if (on)
|
||||
g_allocs = 0;
|
||||
g_window = on;
|
||||
}
|
||||
size_t alloc_count() { return g_allocs; }
|
||||
} // namespace wab
|
||||
|
||||
namespace esphome::i2c::testing {
|
||||
|
||||
// Zephyr, host and TCA9548A keep the default write_prefixed(): this bus only has write_readv(), like them
|
||||
class DefaultPathBus : public I2CBus {
|
||||
public:
|
||||
ErrorCode write_readv(uint8_t address, const uint8_t *wb, size_t wc, uint8_t *rb, size_t rc) override {
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
if (wc != 0 || rc == 0) {
|
||||
w.start();
|
||||
bool ok = s.address(address << 1);
|
||||
w.wbyte(address << 1, ok);
|
||||
for (size_t i = 0; ok && i < wc; i++) {
|
||||
ok = s.data(wb[i]);
|
||||
w.wbyte(wb[i], ok);
|
||||
}
|
||||
if (!ok) {
|
||||
w.stop();
|
||||
return ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
if (rc == 0) {
|
||||
w.stop();
|
||||
return ERROR_OK;
|
||||
}
|
||||
}
|
||||
w.start();
|
||||
bool ok = s.address((address << 1) | 1);
|
||||
w.wbyte((address << 1) | 1, ok);
|
||||
for (size_t i = 0; ok && i < rc; i++) {
|
||||
rb[i] = s.next_read();
|
||||
w.rbyte(rb[i], i + 1 < rc);
|
||||
}
|
||||
w.stop();
|
||||
return ok ? ERROR_OK : ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
};
|
||||
|
||||
static std::vector<uint8_t> pattern(size_t len, uint8_t seed) {
|
||||
std::vector<uint8_t> v(len);
|
||||
for (size_t i = 0; i < len; i++)
|
||||
v[i] = static_cast<uint8_t>(seed + i * 7);
|
||||
return v;
|
||||
}
|
||||
|
||||
TEST(WireAB, Record) {
|
||||
const char *out = std::getenv("WAB_OUT");
|
||||
ASSERT_NE(out, nullptr);
|
||||
std::ofstream f(out);
|
||||
// Allocation counter sanity: a malloc inside the window must count
|
||||
wab::alloc_window(true);
|
||||
void *volatile probe = malloc(100);
|
||||
wab::alloc_window(false);
|
||||
free(probe);
|
||||
f << "ALLOC_PROBE " << wab::alloc_count() << "\n";
|
||||
ASSERT_EQ(wab::alloc_count(), 1u);
|
||||
|
||||
struct Bus {
|
||||
const char *name;
|
||||
I2CBus *bus;
|
||||
std::function<void()> reset;
|
||||
};
|
||||
std::vector<Bus> buses = {
|
||||
{"esp8266", wab_make_esp8266(), wab_reset_esp8266},
|
||||
{"pico", wab_make_pico(), wab_reset_pico},
|
||||
{"idf", wab_make_idf(), [] {}},
|
||||
{"sw", wab_make_sw(), wab_reset_sw},
|
||||
{"default", new DefaultPathBus(), [] {}},
|
||||
};
|
||||
for (auto &b : buses) {
|
||||
I2CDevice dev;
|
||||
dev.set_i2c_bus(b.bus);
|
||||
auto run = [&](const std::string &label, const std::function<ErrorCode()> &fn, bool reset = true) {
|
||||
if (reset)
|
||||
b.reset();
|
||||
wab::wire().clear();
|
||||
wab::jobs_note().clear();
|
||||
// the recorder must not allocate inside the window
|
||||
wab::wire().text.reserve(16384);
|
||||
wab::jobs_note().reserve(1024);
|
||||
wab::alloc_window(true);
|
||||
ErrorCode rc = fn();
|
||||
wab::alloc_window(false);
|
||||
f << b.name << "|" << label << "|rc=" << rc << "|alloc=" << wab::alloc_count() << "|" << wab::wire().text
|
||||
<< "\n";
|
||||
if (!wab::jobs_note().empty())
|
||||
f << "J|" << b.name << "|" << label << "|" << wab::jobs_note() << "\n";
|
||||
};
|
||||
auto &sl = wab::slave();
|
||||
sl.ack_limit = SIZE_MAX;
|
||||
dev.set_i2c_address(0x48);
|
||||
for (size_t len = 0; len <= 300; len++) {
|
||||
auto d = pattern(len, 0x11);
|
||||
run("wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
|
||||
run("wreg16 len=" + std::to_string(len), [&] { return dev.write_register16(0xBEEF, d.data(), d.size()); });
|
||||
}
|
||||
// nullptr data with len 0, as a caller with no payload may pass
|
||||
run("wreg8 null len=0", [&] { return dev.write_register(0x03, nullptr, 0); });
|
||||
run("wreg16 null len=0", [&] { return dev.write_register16(0x0304, nullptr, 0); });
|
||||
// slave NACKs after k data bytes (k counts the register byte)
|
||||
for (size_t k = 0; k <= 6; k++) {
|
||||
sl.ack_limit = k;
|
||||
auto d = pattern(4, 0x40);
|
||||
run("wreg8 len=4 acklimit=" + std::to_string(k), [&] { return dev.write_register(0x05, d.data(), d.size()); });
|
||||
run("wreg16 len=4 acklimit=" + std::to_string(k),
|
||||
[&] { return dev.write_register16(0x0506, d.data(), d.size()); });
|
||||
}
|
||||
sl.ack_limit = SIZE_MAX;
|
||||
// reads, write+read with repeated start, plain writes, probes
|
||||
for (size_t n : {1, 2, 3, 16, 32}) {
|
||||
std::vector<uint8_t> r(n);
|
||||
run("rreg8 n=" + std::to_string(n), [&] { return dev.read_register(0x01, r.data(), n); });
|
||||
run("rreg16 n=" + std::to_string(n), [&] { return dev.read_register16(0x0102, r.data(), n); });
|
||||
run("read n=" + std::to_string(n), [&] { return dev.read(r.data(), n); });
|
||||
auto w3 = pattern(3, 0x70);
|
||||
run("wr 3+" + std::to_string(n), [&] { return dev.write_read(w3.data(), 3, r.data(), n); });
|
||||
}
|
||||
for (size_t len : {0, 1, 5, 127, 128, 129, 255, 256, 257}) {
|
||||
auto d = pattern(len, 0x22);
|
||||
run("write len=" + std::to_string(len), [&] { return dev.write(d.data(), d.size()); });
|
||||
}
|
||||
// a normal transaction right after an over-long write, without resetting the Wire model in between
|
||||
for (size_t len : {200, 300}) {
|
||||
auto d = pattern(len, 0x33);
|
||||
run("wedge wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
|
||||
std::vector<uint8_t> r(2);
|
||||
run("wedge then rreg8 n=2", [&] { return dev.read_register(0x01, r.data(), 2); }, false);
|
||||
auto s = pattern(2, 0x44);
|
||||
run("wedge then wreg8 len=2", [&] { return dev.write_register(0x02, s.data(), 2); }, false);
|
||||
}
|
||||
// absent device
|
||||
dev.set_i2c_address(0x50);
|
||||
for (size_t len : {0, 1, 5}) {
|
||||
auto d = pattern(len, 0x55);
|
||||
run("absent wreg8 len=" + std::to_string(len), [&] { return dev.write_register(0x02, d.data(), d.size()); });
|
||||
run("absent wreg16 len=" + std::to_string(len),
|
||||
[&] { return dev.write_register16(0x0203, d.data(), d.size()); });
|
||||
}
|
||||
// scan probe over the whole range, as i2c_scan_() does
|
||||
for (uint8_t a = 8; a != 120; a++) {
|
||||
run("probe " + std::to_string(a), [&] { return b.bus->write_readv(a, nullptr, 0, nullptr, 0); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::i2c::testing
|
||||
@@ -0,0 +1,34 @@
|
||||
// Arduino bus body of the tree under test on the esp8266 core Wire model
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#define WAB_WIRE_ESP8266
|
||||
#define USE_ARDUINO
|
||||
#define USE_ESP8266
|
||||
#define ArduinoI2CBus ArduinoI2CBus8266
|
||||
#include "esphome/components/i2c/i2c_bus_arduino.h"
|
||||
TwoWire Wire;
|
||||
namespace esphome::i2c {
|
||||
ESPHOME_LOG_TAG(TAG, "i2c");
|
||||
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
|
||||
void ArduinoI2CBus::setup() {}
|
||||
void ArduinoI2CBus::dump_config() {}
|
||||
void ArduinoI2CBus::set_pins_and_clock_() {}
|
||||
#include "wab_gen_arduino.inc"
|
||||
} // namespace esphome::i2c
|
||||
#undef USE_ESP8266
|
||||
#undef USE_ARDUINO
|
||||
struct A8Init { using type = bool esphome::i2c::ArduinoI2CBus::*; friend type get(A8Init); };
|
||||
struct A8Wire { using type = TwoWire *esphome::i2c::ArduinoI2CBus::*; friend type get(A8Wire); };
|
||||
template struct WabRob<A8Init, &esphome::i2c::ArduinoI2CBus::initialized_>;
|
||||
template struct WabRob<A8Wire, &esphome::i2c::ArduinoI2CBus::wire_>;
|
||||
esphome::i2c::I2CBus *wab_make_esp8266() {
|
||||
auto *b = new esphome::i2c::ArduinoI2CBus();
|
||||
b->*get(A8Init()) = true;
|
||||
b->*get(A8Wire()) = &Wire;
|
||||
return b;
|
||||
}
|
||||
void wab_reset_esp8266() { Wire.wab_reset(); }
|
||||
@@ -0,0 +1,35 @@
|
||||
// Arduino bus body of the tree under test on the arduino-pico Wire model
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#define WAB_WIRE_PICO
|
||||
#define USE_ARDUINO
|
||||
#define USE_RP2
|
||||
#define ArduinoI2CBus ArduinoI2CBusPico
|
||||
#include "esphome/components/i2c/i2c_bus_arduino.h"
|
||||
TwoWire Wire;
|
||||
TwoWire Wire1;
|
||||
namespace esphome::i2c {
|
||||
ESPHOME_LOG_TAG(TAG, "i2c");
|
||||
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
|
||||
void ArduinoI2CBus::setup() {}
|
||||
void ArduinoI2CBus::dump_config() {}
|
||||
void ArduinoI2CBus::set_pins_and_clock_() {}
|
||||
#include "wab_gen_arduino.inc"
|
||||
} // namespace esphome::i2c
|
||||
#undef USE_RP2
|
||||
#undef USE_ARDUINO
|
||||
struct APInit { using type = bool esphome::i2c::ArduinoI2CBus::*; friend type get(APInit); };
|
||||
struct APWire { using type = TwoWire *esphome::i2c::ArduinoI2CBus::*; friend type get(APWire); };
|
||||
template struct WabRob<APInit, &esphome::i2c::ArduinoI2CBus::initialized_>;
|
||||
template struct WabRob<APWire, &esphome::i2c::ArduinoI2CBus::wire_>;
|
||||
esphome::i2c::I2CBus *wab_make_pico() {
|
||||
auto *b = new esphome::i2c::ArduinoI2CBus();
|
||||
b->*get(APInit()) = true;
|
||||
b->*get(APWire()) = &Wire;
|
||||
return b;
|
||||
}
|
||||
void wab_reset_pico() { Wire.wab_reset(); }
|
||||
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
// Wire-level A/B harness for esphome#22780: every bus model emits the same token stream.
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace wab {
|
||||
|
||||
// Token stream of the bus: "S", "Sr", "P", "AB+" (written byte, slave ACK), "AB-" (NACK), "rAB+"/"rAB-" (read byte,
|
||||
// master ACK/NACK), "ABORT".
|
||||
struct Wire {
|
||||
std::string text;
|
||||
bool open{false};
|
||||
void start() {
|
||||
text += open ? "Sr " : "S ";
|
||||
open = true;
|
||||
}
|
||||
void stop() {
|
||||
text += "P ";
|
||||
open = false;
|
||||
}
|
||||
void wbyte(uint8_t b, bool ack) {
|
||||
char t[8];
|
||||
snprintf(t, sizeof t, "%02X%c ", b, ack ? '+' : '-');
|
||||
text += t;
|
||||
}
|
||||
void rbyte(uint8_t b, bool ack) {
|
||||
char t[8];
|
||||
snprintf(t, sizeof t, "r%02X%c ", b, ack ? '+' : '-');
|
||||
text += t;
|
||||
}
|
||||
void note(const char *s) {
|
||||
text += s;
|
||||
text += ' ';
|
||||
}
|
||||
void clear() {
|
||||
text.clear();
|
||||
open = false;
|
||||
}
|
||||
};
|
||||
inline Wire &wire() {
|
||||
static Wire w;
|
||||
return w;
|
||||
}
|
||||
inline std::string &jobs_note() {
|
||||
static std::string s;
|
||||
return s;
|
||||
}
|
||||
|
||||
// Slave model: present addresses ACK their address and up to ack_limit data bytes after it.
|
||||
struct Slave {
|
||||
uint8_t present[4]{0x48, 0x1A, 0, 0};
|
||||
size_t ack_limit{SIZE_MAX};
|
||||
size_t written{0}; // data bytes written since the address byte
|
||||
size_t read_idx{0};
|
||||
bool is_present(uint8_t addr7) const {
|
||||
for (auto p : present)
|
||||
if (p != 0 && p == addr7)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
// Called for the address byte; returns ACK
|
||||
bool address(uint8_t byte) {
|
||||
this->written = 0;
|
||||
this->read_idx = 0;
|
||||
return this->is_present(byte >> 1);
|
||||
}
|
||||
bool data(uint8_t) { return this->written++ < this->ack_limit; }
|
||||
uint8_t next_read() { return static_cast<uint8_t>(0xA0 + this->read_idx++); }
|
||||
};
|
||||
inline Slave &slave() {
|
||||
static Slave s;
|
||||
return s;
|
||||
}
|
||||
|
||||
// Allocation counter (window opened around one bus call)
|
||||
void alloc_window(bool on);
|
||||
size_t alloc_count();
|
||||
|
||||
} // namespace wab
|
||||
|
||||
// Member access to private fields of final classes (explicit instantiation ignores access checks)
|
||||
template<typename Tag, typename Tag::type M> struct WabRob {
|
||||
friend typename Tag::type get(Tag) { return M; }
|
||||
};
|
||||
@@ -0,0 +1,88 @@
|
||||
// ESP-IDF bus body of the tree under test on the IDF 5.5.5 job model
|
||||
#include <string>
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "wab_common.h"
|
||||
#define USE_ESP32
|
||||
#include "esphome/components/i2c/i2c_bus_esp_idf.h"
|
||||
namespace esphome::i2c {
|
||||
ESPHOME_LOG_TAG(TAG, "i2c.idf");
|
||||
static constexpr size_t I2C_MAX_LOG_BYTES = 32;
|
||||
void IDFI2CBus::setup() {}
|
||||
void IDFI2CBus::dump_config() {}
|
||||
#include "wab_gen_idf.inc"
|
||||
} // namespace esphome::i2c
|
||||
#undef USE_ESP32
|
||||
|
||||
esp_err_t i2c_master_execute_defined_operations(i2c_master_dev_handle_t, i2c_operation_job_t *ops, size_t n, int) {
|
||||
auto &jn = wab::jobs_note();
|
||||
jn.clear();
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char t[16];
|
||||
switch (ops[i].command) {
|
||||
case I2C_MASTER_CMD_START: jn += "S "; break;
|
||||
case I2C_MASTER_CMD_STOP: jn += "P "; break;
|
||||
case I2C_MASTER_CMD_WRITE: snprintf(t, sizeof t, "W%zu ", ops[i].write.total_bytes); jn += t; break;
|
||||
case I2C_MASTER_CMD_READ: snprintf(t, sizeof t, "R%zu%s ", ops[i].read.total_bytes,
|
||||
ops[i].read.ack_value == I2C_NACK_VAL ? "n" : "a"); jn += t; break;
|
||||
}
|
||||
}
|
||||
auto &w = wab::wire();
|
||||
auto &s = wab::slave();
|
||||
if (n > SOC_I2C_CMD_REG_NUM) {
|
||||
w.note("INVALID_ARG_TOO_MANY_JOBS");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
for (size_t i = 0; i + 1 < n; i++) {
|
||||
if (ops[i].command == I2C_MASTER_CMD_READ && ops[i + 1].command == I2C_MASTER_CMD_STOP &&
|
||||
ops[i].read.ack_value != I2C_NACK_VAL) {
|
||||
w.note("INVALID_ARG_ACK_BEFORE_STOP");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
bool expect_addr = false;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
auto &op = ops[i];
|
||||
switch (op.command) {
|
||||
case I2C_MASTER_CMD_START:
|
||||
w.start();
|
||||
expect_addr = true;
|
||||
break;
|
||||
case I2C_MASTER_CMD_STOP:
|
||||
w.stop();
|
||||
break;
|
||||
case I2C_MASTER_CMD_WRITE:
|
||||
for (size_t k = 0; k < op.write.total_bytes; k++) {
|
||||
uint8_t b = op.write.data[k];
|
||||
bool ack = expect_addr ? s.address(b) : s.data(b);
|
||||
expect_addr = false;
|
||||
w.wbyte(b, ack);
|
||||
if (!ack && op.write.ack_check) {
|
||||
w.note("ABORT");
|
||||
w.stop();
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case I2C_MASTER_CMD_READ:
|
||||
for (size_t k = 0; k < op.read.total_bytes; k++) {
|
||||
op.read.data[k] = s.next_read();
|
||||
w.rbyte(op.read.data[k], op.read.ack_value == I2C_ACK_VAL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
struct IdfInit { using type = bool esphome::i2c::IDFI2CBus::*; friend type get(IdfInit); };
|
||||
template struct WabRob<IdfInit, &esphome::i2c::IDFI2CBus::initialized_>;
|
||||
esphome::i2c::I2CBus *wab_make_idf() {
|
||||
auto *b = new esphome::i2c::IDFI2CBus();
|
||||
b->*get(IdfInit()) = true;
|
||||
return b;
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
// The software bus of the tree under test, whole, on fake open-drain pins with a bit-level slave
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/gpio.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "wab_common.h"
|
||||
|
||||
namespace wab {
|
||||
struct BitSlave {
|
||||
bool m_sda_low{false}, m_scl_low{false}, s_sda_low{false};
|
||||
bool sda{true}, scl{true};
|
||||
enum { IDLE, ADDR, WDATA, RDATA, IGNORE } state{IDLE};
|
||||
enum { BITS, ACK_OUT, ACK_IN } phase{BITS};
|
||||
int bits{0};
|
||||
uint8_t shift{0}, cur{0};
|
||||
bool last_ack{false}, is_read{false}, master_ack{false};
|
||||
void drive_bit() { this->s_sda_low = !(this->cur & (0x80 >> this->bits)); }
|
||||
void update() {
|
||||
auto &w = wire();
|
||||
auto &sl = slave();
|
||||
for (int guard = 0; guard < 4; guard++) {
|
||||
bool nsda = !(this->m_sda_low || this->s_sda_low), nscl = !this->m_scl_low;
|
||||
if (nsda == this->sda && nscl == this->scl)
|
||||
return;
|
||||
bool sda_fell = this->sda && !nsda, sda_rose = !this->sda && nsda;
|
||||
bool scl_rose = !this->scl && nscl, scl_fell = this->scl && !nscl;
|
||||
this->sda = nsda;
|
||||
this->scl = nscl;
|
||||
if (this->scl && !scl_rose && sda_fell) { // START
|
||||
w.start();
|
||||
this->state = ADDR;
|
||||
this->phase = BITS;
|
||||
this->bits = 0;
|
||||
this->shift = 0;
|
||||
this->s_sda_low = false;
|
||||
continue;
|
||||
}
|
||||
if (this->scl && !scl_rose && sda_rose) { // STOP
|
||||
if (w.open)
|
||||
w.stop();
|
||||
this->state = IDLE;
|
||||
this->s_sda_low = false;
|
||||
continue;
|
||||
}
|
||||
if (scl_rose) {
|
||||
if ((this->state == ADDR || this->state == WDATA) && this->phase == BITS) {
|
||||
this->shift = (this->shift << 1) | (this->sda ? 1 : 0);
|
||||
this->bits++;
|
||||
} else if (this->state == RDATA && this->phase == ACK_IN) {
|
||||
this->master_ack = !this->sda;
|
||||
w.rbyte(this->cur, this->master_ack);
|
||||
}
|
||||
} else if (scl_fell) {
|
||||
if ((this->state == ADDR || this->state == WDATA) && this->phase == BITS && this->bits == 8) {
|
||||
if (this->state == ADDR) {
|
||||
this->last_ack = sl.address(this->shift);
|
||||
this->is_read = this->shift & 1;
|
||||
} else {
|
||||
this->last_ack = sl.data(this->shift);
|
||||
}
|
||||
w.wbyte(this->shift, this->last_ack);
|
||||
this->s_sda_low = this->last_ack;
|
||||
this->phase = ACK_OUT;
|
||||
} else if (this->phase == ACK_OUT) {
|
||||
this->s_sda_low = false;
|
||||
this->bits = 0;
|
||||
this->shift = 0;
|
||||
this->phase = BITS;
|
||||
if (!this->last_ack) {
|
||||
this->state = IGNORE;
|
||||
} else if (this->state == ADDR && this->is_read) {
|
||||
this->state = RDATA;
|
||||
this->cur = sl.next_read();
|
||||
this->drive_bit();
|
||||
} else {
|
||||
this->state = WDATA;
|
||||
}
|
||||
} else if (this->state == RDATA && this->phase == BITS) {
|
||||
this->bits++;
|
||||
if (this->bits == 8) {
|
||||
this->s_sda_low = false;
|
||||
this->phase = ACK_IN;
|
||||
} else {
|
||||
this->drive_bit();
|
||||
}
|
||||
} else if (this->state == RDATA && this->phase == ACK_IN) {
|
||||
if (this->master_ack) {
|
||||
this->cur = sl.next_read();
|
||||
this->bits = 0;
|
||||
this->phase = BITS;
|
||||
this->drive_bit();
|
||||
} else {
|
||||
this->state = IGNORE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
inline BitSlave &bitslave() {
|
||||
static BitSlave b;
|
||||
return b;
|
||||
}
|
||||
|
||||
class FakePin : public esphome::GPIOPin {
|
||||
public:
|
||||
explicit FakePin(bool is_sda) : is_sda_(is_sda) {}
|
||||
void setup() override {}
|
||||
void pin_mode(esphome::gpio::Flags flags) override {
|
||||
this->flags_ = flags;
|
||||
this->apply_();
|
||||
}
|
||||
esphome::gpio::Flags get_flags() const override { return this->flags_; }
|
||||
bool digital_read() override { return this->is_sda_ ? bitslave().sda : bitslave().scl; }
|
||||
void digital_write(bool value) override {
|
||||
this->value_ = value;
|
||||
this->apply_();
|
||||
}
|
||||
size_t dump_summary(char *buffer, size_t len) const override { return 0; }
|
||||
|
||||
protected:
|
||||
void apply_() {
|
||||
bool low = (this->flags_ & esphome::gpio::FLAG_OUTPUT) && !this->value_;
|
||||
(this->is_sda_ ? bitslave().m_sda_low : bitslave().m_scl_low) = low;
|
||||
bitslave().update();
|
||||
}
|
||||
bool is_sda_;
|
||||
bool value_{true};
|
||||
esphome::gpio::Flags flags_{esphome::gpio::FLAG_INPUT};
|
||||
};
|
||||
} // namespace wab
|
||||
|
||||
#define USE_LIBRETINY
|
||||
#define InternalGPIOPin wab::FakePin
|
||||
#include "wab_sw_impl.inc"
|
||||
#undef InternalGPIOPin
|
||||
#undef USE_LIBRETINY
|
||||
|
||||
esphome::i2c::I2CBus *wab_make_sw() {
|
||||
static wab::FakePin sda(true), scl(false);
|
||||
auto *b = new esphome::i2c::SoftwareI2CBus();
|
||||
b->set_sda_pin(&sda);
|
||||
b->set_scl_pin(&scl);
|
||||
b->set_frequency(1000000);
|
||||
b->set_scan(false);
|
||||
b->setup();
|
||||
return b;
|
||||
}
|
||||
void wab_reset_sw() {
|
||||
auto &b = wab::bitslave();
|
||||
b.state = wab::BitSlave::IDLE;
|
||||
b.s_sda_low = false;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
"""Readers for the sweep lines of dev/snf.hpp (SN) and ard/swp.hpp (SW), shared by the comparers."""
|
||||
|
||||
from collections.abc import Mapping
|
||||
import pathlib
|
||||
import re
|
||||
|
||||
ANSI = re.compile(r"\x1b\[[0-9;]*m")
|
||||
FIELDS = r"\|rc=(-?\d+)\|alloc=(\d+)\|ev=\d+\|amb=\d+\|ovf=\d+\|fnv=([0-9A-F]+)\|(.*)$"
|
||||
CAPTURES = ("", "2", "3")
|
||||
|
||||
|
||||
def load(path: str | pathlib.Path, tag: str = "SN") -> dict[str, dict]:
|
||||
"""Case label -> rc, alloc, fnv and the rest of the line.
|
||||
|
||||
SN lines get `verdict` (OK or DIFF) and, on a DIFF, the `got` and `exp` token lists of the SNGOT and SNEXP lines
|
||||
that follow. SW lines get `wire` (the decoded stream) and `rd` (read data).
|
||||
"""
|
||||
line_re = re.compile(rf"{tag} (.+?){FIELDS}")
|
||||
cases: dict[str, dict] = {}
|
||||
cur: dict | None = None
|
||||
text = pathlib.Path(path).read_text(encoding="utf-8", errors="replace")
|
||||
for line in text.splitlines():
|
||||
line = ANSI.sub("", line).rstrip()
|
||||
if m := line_re.search(line):
|
||||
label, rc, alloc, fnv, rest = m.groups()
|
||||
cur = cases[label] = {"rc": int(rc), "alloc": int(alloc), "fnv": fnv}
|
||||
if tag == "SW":
|
||||
wire, _, rd = rest.partition("|rd=")
|
||||
cur.update(wire=wire.strip(), rd=rd.strip())
|
||||
else:
|
||||
cur["verdict"] = rest
|
||||
elif cur is not None and (m := re.search(r"SN(GOT|EXP) (.*)$", line)):
|
||||
cur["got" if m.group(1) == "GOT" else "exp"] = m.group(2).split()
|
||||
return cases
|
||||
|
||||
|
||||
def is_read(label: str) -> bool:
|
||||
"""Reads return chip data, so their wire hash changes between runs."""
|
||||
return label.startswith(("rreg", "read", "write1+read"))
|
||||
|
||||
|
||||
def nack_cut(got: list[str], exp: list[str]) -> int | None:
|
||||
"""Index of the token the chip NACKed when `got` is `exp` cut there (then STOP), else None.
|
||||
|
||||
Only address and write tokens count as a chip NACK; a read token ("r..") NACKed early stays a difference.
|
||||
"""
|
||||
j = next(
|
||||
(j for j, t in enumerate(got) if t.endswith("-") and not t.startswith("r")),
|
||||
None,
|
||||
)
|
||||
if (
|
||||
j is not None
|
||||
and got[:j] == exp[:j]
|
||||
and got[j][:-1] == exp[j][:-1]
|
||||
and got[j + 1 :] == ["P"]
|
||||
):
|
||||
return j
|
||||
return None
|
||||
|
||||
|
||||
def runs(name: str) -> list[dict[str, dict]]:
|
||||
"""The captures NAME-ser.txt, NAME-ser2.txt and NAME-ser3.txt that exist (at least one)."""
|
||||
found = [pathlib.Path(f"{name}-ser{s}.txt") for s in CAPTURES]
|
||||
loaded = [load(p) for p in found if p.exists()]
|
||||
if not loaded:
|
||||
raise SystemExit(f"no capture {name}-ser.txt")
|
||||
return loaded
|
||||
|
||||
|
||||
def first_alloc(allocs: Mapping[str, int], shape: str, limit: int = 400) -> int | None:
|
||||
"""First length of `shape` (wreg8, wreg16) whose call allocated; allocs maps case label -> malloc count."""
|
||||
return next(
|
||||
(n for n in range(limit) if allocs.get(f"{shape} len={n}", 0) > 0), None
|
||||
)
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
// Wire decoder shared by dev/snf.hpp and ard/swp.hpp. Each sample is a level state: SDA in bit 0, SCL in bit 1.
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
namespace wiredecode {
|
||||
// Decodes `count` level states that follow `start` into tokens (`S`, `Sr`, `P`, `90+`, `rA0-`); adds to amb the
|
||||
// samples where both lines changed at once.
|
||||
inline std::string decode(const uint8_t *buf, uint32_t count, uint8_t start, uint32_t &amb) {
|
||||
std::string out;
|
||||
uint8_t prev = start;
|
||||
bool open = false, first = false, reading = false;
|
||||
int bits = -1; // -1: no frame; 0..8 bit index in the current 9-bit unit
|
||||
uint16_t sh = 0;
|
||||
char t[8];
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
uint8_t cur = buf[i];
|
||||
bool psda = prev & 1, pscl = prev & 2, sda = cur & 1, scl = cur & 2;
|
||||
if ((psda != sda) && (pscl != scl))
|
||||
amb++;
|
||||
if (pscl && scl && psda && !sda) {
|
||||
out += open ? "Sr " : "S ";
|
||||
open = true;
|
||||
bits = 0;
|
||||
sh = 0;
|
||||
first = true;
|
||||
} else if (pscl && scl && !psda && sda) {
|
||||
out += "P ";
|
||||
open = false;
|
||||
bits = -1;
|
||||
} else if (!pscl && scl && bits >= 0) {
|
||||
if (bits < 8) {
|
||||
sh = (sh << 1) | (sda ? 1 : 0);
|
||||
bits++;
|
||||
} else {
|
||||
uint8_t b = sh & 0xFF;
|
||||
if (first)
|
||||
reading = b & 1;
|
||||
snprintf(t, sizeof t, (!first && reading) ? "r%02X%c " : "%02X%c ", b, sda ? '-' : '+');
|
||||
out += t;
|
||||
first = false;
|
||||
bits = 0;
|
||||
sh = 0;
|
||||
}
|
||||
}
|
||||
prev = cur;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace wiredecode
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
// Heap allocation counter for test harnesses: counts while alloc_count::window is set. Link flag per platform:
|
||||
// ESP32 and ESP8266 -Wl,--wrap=malloc (on ESP8266 the wrapper must be in IRAM: in flash it crash-loops with
|
||||
// IllegalInst at its first instruction). arduino-pico already wraps malloc and _malloc_r (a second wrap is a duplicate
|
||||
// symbol), so use -Wl,--wrap=__malloc_lock (malloc and free each count once). LibreTiny already wraps malloc into
|
||||
// pvPortMalloc, so use -Wl,--wrap=pvPortMalloc.
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace alloc_count {
|
||||
inline volatile bool window = false;
|
||||
inline volatile uint32_t count = 0;
|
||||
inline void note() {
|
||||
if (window)
|
||||
count = count + 1;
|
||||
}
|
||||
} // namespace alloc_count
|
||||
|
||||
#if defined(USE_RP2)
|
||||
extern "C" void __real___malloc_lock(struct _reent *r);
|
||||
extern "C" void __wrap___malloc_lock(struct _reent *r) {
|
||||
alloc_count::note();
|
||||
__real___malloc_lock(r);
|
||||
}
|
||||
#elif defined(USE_LIBRETINY)
|
||||
extern "C" void *__real_pvPortMalloc(size_t n);
|
||||
extern "C" void *__wrap_pvPortMalloc(size_t n) {
|
||||
alloc_count::note();
|
||||
return __real_pvPortMalloc(n);
|
||||
}
|
||||
#elif defined(USE_ESP8266)
|
||||
extern "C" void *__real_malloc(size_t n);
|
||||
extern "C" void *IRAM_ATTR __wrap_malloc(size_t n) {
|
||||
if (alloc_count::window)
|
||||
alloc_count::count = alloc_count::count + 1;
|
||||
return __real_malloc(n);
|
||||
}
|
||||
#else
|
||||
extern "C" void *__real_malloc(size_t n);
|
||||
extern "C" void *__wrap_malloc(size_t n) {
|
||||
alloc_count::note();
|
||||
return __real_malloc(n);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,69 @@
|
||||
# Fake BMI270 on an in-firmware I2C bus (bmi270fake.hpp). The real i2c: bus keeps codegen happy; the on_boot
|
||||
# at 700 runs after the bus and before the bmi270 setup (DATA) and swaps the device onto the fake.
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- alloc_count.hpp
|
||||
- bmi270fake.hpp
|
||||
platformio_options:
|
||||
build_flags:
|
||||
- -Wl,--wrap=malloc
|
||||
- -DBMI_FAKE_WRAP_MALLOC
|
||||
on_boot:
|
||||
- priority: 700
|
||||
then:
|
||||
- lambda: |-
|
||||
bmi270fake::bus.init_bmm(${fake_aux_addr});
|
||||
id(imu)->set_i2c_bus(&bmi270fake::bus);
|
||||
- priority: -100
|
||||
then:
|
||||
- lambda: |-
|
||||
ESP_LOGI("bmi270fake", "BUILD %s imu_failed=%d", "${name}", id(imu)->is_failed());
|
||||
bmi270fake::bus.report();
|
||||
|
||||
logger:
|
||||
level: DEBUG
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
|
||||
api:
|
||||
reboot_timeout: 0s
|
||||
|
||||
i2c:
|
||||
id: i2c_bus
|
||||
sda: ${sda}
|
||||
scl: ${scl}
|
||||
scan: false
|
||||
|
||||
sensor:
|
||||
- platform: bmi270
|
||||
bmi270_id: imu
|
||||
name: "BMI Temperature"
|
||||
- platform: motion
|
||||
type: acceleration_x
|
||||
name: "BMI Accel X"
|
||||
accuracy_decimals: 4
|
||||
- platform: motion
|
||||
type: acceleration_y
|
||||
name: "BMI Accel Y"
|
||||
accuracy_decimals: 4
|
||||
- platform: motion
|
||||
type: acceleration_z
|
||||
name: "BMI Accel Z"
|
||||
accuracy_decimals: 4
|
||||
- platform: motion
|
||||
type: gyroscope_x
|
||||
name: "BMI Gyro X"
|
||||
- platform: motion
|
||||
type: gyroscope_y
|
||||
name: "BMI Gyro Y"
|
||||
- platform: motion
|
||||
type: gyroscope_z
|
||||
name: "BMI Gyro Z"
|
||||
|
||||
interval:
|
||||
- interval: 60s
|
||||
then:
|
||||
- lambda: bmi270fake::bus.report();
|
||||
@@ -0,0 +1,136 @@
|
||||
"""bmi270-host-compare.py BASE.report PR.report: compare every scenario of the harness between base and PR.
|
||||
|
||||
Streams: base's split INIT_ADDR writes (W 5b xx / W 5c yy) are merged before comparing. NACK scenarios are matched by
|
||||
the normalized transaction they hit. LOG lines are compared separately and every difference is printed."""
|
||||
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
ANSI = re.compile(r"\x1b\[[0-9;]*m")
|
||||
|
||||
|
||||
def parse(path):
|
||||
"""Scenario name -> head, LOG, TX and other lines; also the NACK_TOTAL count."""
|
||||
scen, cur, nack_total = {}, None, 0
|
||||
with pathlib.Path(path).open(encoding="utf-8", errors="replace") as fh:
|
||||
for line in fh:
|
||||
line = ANSI.sub("", line.rstrip("\n"))
|
||||
if line.startswith("SCENARIO "):
|
||||
cur = scen.setdefault(
|
||||
line.split()[1], {"head": "", "log": [], "tx": [], "other": []}
|
||||
)
|
||||
elif line.startswith("NACK_TOTAL"):
|
||||
nack_total = int(line.split()[1])
|
||||
elif line.startswith(" failed="):
|
||||
cur["head"] = re.sub(r" setup_tx=\d+", "", line.strip())
|
||||
elif line.startswith(" LOG "):
|
||||
cur["log"].append(line[6:])
|
||||
elif line.startswith(" TX "):
|
||||
cur["tx"].append(line[5:])
|
||||
else:
|
||||
cur["other"].append(
|
||||
re.sub(r" (aux_w|aux_r|status_reads)=\d+", "", line.strip())
|
||||
)
|
||||
return scen, nack_total
|
||||
|
||||
|
||||
def merge(tx):
|
||||
"""Returns the merged list and, per original index (0 based), the merged index."""
|
||||
out, idx, i = [], [], 0
|
||||
while i < len(tx):
|
||||
a = tx[i]
|
||||
if (
|
||||
a.startswith("W 5b ")
|
||||
and len(a.split()) == 3
|
||||
and i + 1 < len(tx)
|
||||
and tx[i + 1].startswith("W 5c ")
|
||||
and len(tx[i + 1].split()) == 3
|
||||
):
|
||||
out.append(a + " " + tx[i + 1].split()[2])
|
||||
idx += [len(out) - 1, len(out) - 1]
|
||||
i += 2
|
||||
continue
|
||||
out.append(a)
|
||||
idx.append(len(out) - 1)
|
||||
i += 1
|
||||
return out, idx
|
||||
|
||||
|
||||
(b, nack_total), (p, _) = parse(sys.argv[1]), parse(sys.argv[2])
|
||||
problems = 0
|
||||
logdiffs = {}
|
||||
for name in sorted(n for n in p if not n.startswith(("nack_", "step_"))):
|
||||
sb, sp = b[name], p[name]
|
||||
same_head = sb["head"] == sp["head"]
|
||||
same_other = sb["other"] == sp["other"]
|
||||
line = f"{name:20s} head={'same' if same_head else 'DIFF'} regs/values/upload={'same' if same_other else 'DIFF'}"
|
||||
if sp["tx"]:
|
||||
mb, _ = merge(sb["tx"])
|
||||
line += f" stream base={len(sb['tx'])}->{len(mb)} pr={len(sp['tx'])} {'same' if mb == sp['tx'] else 'DIFF'}"
|
||||
if mb != sp["tx"]:
|
||||
problems += 1
|
||||
if not (same_head and same_other):
|
||||
problems += 1
|
||||
line += f"\n base: {sb['head']} {sb['other']}\n pr: {sp['head']} {sp['other']}"
|
||||
print(line)
|
||||
if sb["log"] != sp["log"]:
|
||||
key = (
|
||||
tuple(x for x in sb["log"] if x not in sp["log"]),
|
||||
tuple(x for x in sp["log"] if x not in sb["log"]),
|
||||
)
|
||||
logdiffs.setdefault(key, []).append(name)
|
||||
|
||||
# NACK sweep, matched through the merged aux stream; indexes count main-bus transactions only (the fake numbers those)
|
||||
main_b = [t for t in b["cfg_aux"]["tx"] if not t.startswith("A ")]
|
||||
main_p = [t for t in p["cfg_aux"]["tx"] if not t.startswith("A ")]
|
||||
mmb, midx = merge(main_b)
|
||||
assert mmb == main_p, "main streams differ"
|
||||
nack_outcomes = {}
|
||||
for i in range(nack_total):
|
||||
k = midx[i]
|
||||
sb, sp = b[f"nack_{i + 1}"], p[f"nack_{k + 1}"]
|
||||
hb = (sb["head"], tuple(sb["other"]))
|
||||
hp = (sp["head"], tuple(sp["other"]))
|
||||
if hb != hp:
|
||||
problems += 1
|
||||
print(
|
||||
f"NACK base tx {i + 1} / pr tx {k + 1} ({main_p[k][:20]}): DIFF\n base {hb}\n pr {hp}"
|
||||
)
|
||||
wb = [x for x in sb["log"] if x[:1] in "EW"]
|
||||
wp = [x for x in sp["log"] if x[:1] in "EW"]
|
||||
nack_outcomes.setdefault((sp["head"].split(" status")[0], tuple(wp)), []).append(
|
||||
k + 1
|
||||
)
|
||||
if wb != wp:
|
||||
key = (tuple(x for x in wb if x not in wp), tuple(x for x in wp if x not in wb))
|
||||
logdiffs.setdefault(key, []).append(f"nack_pr{k + 1}")
|
||||
print(
|
||||
f"NACK scenarios checked: {nack_total} base transactions -> {len(main_p)} pr transactions"
|
||||
)
|
||||
print("PR NACK outcome classes (head, E/W log lines): pr transaction numbers")
|
||||
for (h, w), ks in sorted(nack_outcomes.items(), key=lambda x: x[1][0]):
|
||||
ks = sorted(set(ks))
|
||||
print(f" {h} | {' / '.join(w)} | tx {ks[0]}..{ks[-1]} ({len(ks)})")
|
||||
print("BMM150 step failures (failed/mag_ready, values, W lines): base vs PR")
|
||||
for name in sorted(n for n in p if n.startswith("step_")):
|
||||
sb, sp = b[name], p[name]
|
||||
vb = [x for x in sb["other"] if x.startswith(("REGS", "VALUES"))]
|
||||
vp = [x for x in sp["other"] if x.startswith(("REGS", "VALUES"))]
|
||||
if sb["head"] != sp["head"] or vb != vp:
|
||||
problems += 1
|
||||
print(
|
||||
f" {name}: DIFF head/regs/values\n base {sb['head']} {vb}\n pr {sp['head']} {vp}"
|
||||
)
|
||||
print(
|
||||
f" {name:32s} {sp['head'].split(' status')[0]} | mag {[x for x in vp if x.startswith('VALUES')][0].split(' t=')[1] if vp else '-'}"
|
||||
)
|
||||
print(f" base W: {[x for x in sb['log'] if x[:1] in 'EW']}")
|
||||
print(f" pr W: {[x for x in sp['log'] if x[:1] in 'EW']}")
|
||||
print("LOG differences (only in base | only in PR): scenarios")
|
||||
for (ob, op), names in logdiffs.items():
|
||||
names = sorted(set(names))
|
||||
print(
|
||||
f" base-only {list(ob)} | pr-only {list(op)} : {len(names)} e.g. {names[:4]}"
|
||||
)
|
||||
print("RESULT", "SAME" if problems == 0 else f"DIFFERENT ({problems})")
|
||||
@@ -0,0 +1,203 @@
|
||||
#ifdef USE_HOST
|
||||
// Host harness for esphome#22778. Runs BMI270Component::setup() and update_data() against bmi270fake::Bus for every
|
||||
// scenario and writes one report to $BMI_OUT: per scenario the outcome (failed, error reason, warning, magnetometer
|
||||
// ready), the log lines, the final registers, decoded values and the raw transaction stream.
|
||||
// $BMI_EXPECTED_FNV (hex, optional) overrides the fake's expected config table hash.
|
||||
// Scenarios: configs from $BMI_SETTERS (lines "name setter value", parsed from each tree's generated main.cpp),
|
||||
// aux faults, and a NACK at every main-bus transaction of the aux config.
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#ifndef USE_BMI270_BMM150
|
||||
#error "build with -DUSE_BMI270_BMM150 (benchmark.yaml build_flags)"
|
||||
#endif
|
||||
#ifndef USE_LOG_LISTENERS
|
||||
#error "build with -DUSE_LOG_LISTENERS"
|
||||
#endif
|
||||
#define protected public // read magnetometer_ready_ and call update_data(); harness only
|
||||
#include "esphome/components/bmi270/bmi270.h"
|
||||
#undef protected
|
||||
#include "esphome/components/bmi270/bmi270_config.h"
|
||||
#include "esphome/components/logger/logger.h"
|
||||
#include "bmi270fake.hpp"
|
||||
|
||||
namespace esphome::bmi270::testing {
|
||||
|
||||
static std::vector<std::string> g_lines;
|
||||
|
||||
struct Setter {
|
||||
std::string fn;
|
||||
int value;
|
||||
};
|
||||
using SetterMap = std::map<std::string, std::vector<Setter>>;
|
||||
|
||||
static SetterMap load_setters() {
|
||||
SetterMap m;
|
||||
std::ifstream f(std::getenv("BMI_SETTERS"));
|
||||
std::string name, fn;
|
||||
int v;
|
||||
while (f >> name >> fn >> v)
|
||||
m[name].push_back({fn, v});
|
||||
return m;
|
||||
}
|
||||
|
||||
static void apply(BMI270Component &imu, const std::vector<Setter> &setters) {
|
||||
for (auto &s : setters) {
|
||||
if (s.fn == "set_accel_range")
|
||||
imu.set_accel_range((BMI270AccelRange) s.value);
|
||||
else if (s.fn == "set_accel_odr")
|
||||
imu.set_accel_odr((BMI270AccelODR) s.value);
|
||||
else if (s.fn == "set_gyro_range")
|
||||
imu.set_gyro_range((BMI270GyroRange) s.value);
|
||||
else if (s.fn == "set_gyro_odr")
|
||||
imu.set_gyro_odr((BMI270GyroODR) s.value);
|
||||
else if (s.fn == "set_aux_device")
|
||||
imu.set_aux_device((BMI270AuxDevice) s.value);
|
||||
else if (s.fn == "set_aux_device_address")
|
||||
imu.set_aux_device_address(s.value);
|
||||
else if (s.fn != "none")
|
||||
ADD_FAILURE() << "unknown setter " << s.fn;
|
||||
}
|
||||
}
|
||||
|
||||
struct Result {
|
||||
std::string text;
|
||||
uint32_t tx;
|
||||
std::vector<std::string> txlog;
|
||||
};
|
||||
|
||||
// aux_addr: the address the fake BMM150 answers on
|
||||
static Result run(const std::string &label, const std::vector<Setter> &setters, uint8_t aux_addr, uint8_t bmm_mode,
|
||||
bool busy_stuck, uint32_t nack_at, bool full, uint8_t stuck_from = 0) {
|
||||
auto bus = std::make_unique<bmi270fake::Bus>();
|
||||
if (const char *fnv = std::getenv("BMI_EXPECTED_FNV"))
|
||||
bus->expected_fnv = std::strtoul(fnv, nullptr, 16);
|
||||
bus->init_bmm(aux_addr, bmm_mode);
|
||||
bus->busy_stuck = busy_stuck;
|
||||
bus->nack_at = nack_at;
|
||||
bus->stuck_from_aux_op = stuck_from;
|
||||
BMI270Component imu;
|
||||
imu.set_i2c_bus(bus.get());
|
||||
imu.set_i2c_address(0x68);
|
||||
apply(imu, setters);
|
||||
float temp = NAN;
|
||||
bmm150::BMM150Data mag{NAN, NAN, NAN};
|
||||
imu.add_temperature_listener([&temp](float t) { temp = t; });
|
||||
imu.add_magnetometer_listener([&mag](const bmm150::BMM150Data &m) { mag = m; });
|
||||
g_lines.clear();
|
||||
imu.setup();
|
||||
const uint32_t setup_tx = bus->transactions;
|
||||
motion::MotionData d{};
|
||||
bool sampled = imu.update_data(d);
|
||||
std::ostringstream o;
|
||||
char fr[240];
|
||||
bus->final_regs(fr, sizeof(fr));
|
||||
o << "SCENARIO " << label << "\n";
|
||||
o << " failed=" << imu.is_failed() << " warning=" << imu.status_has_warning()
|
||||
<< " error=" << imu.status_has_error() << " mag_ready=" << imu.magnetometer_ready_ << " setup_tx=" << setup_tx
|
||||
<< " status=0x" << std::hex << (int) bus->regs[0x21] << std::dec << "\n";
|
||||
for (auto &l : g_lines)
|
||||
o << " LOG " << l << "\n";
|
||||
if (full || !imu.is_failed()) {
|
||||
o << " REGS " << fr << "\n";
|
||||
char vals[300];
|
||||
snprintf(vals, sizeof(vals),
|
||||
" VALUES sampled=%d ax=%.6f ay=%.6f az=%.6f gx=%.5f gy=%.5f gz=%.5f t=%.4f mx=%.6f my=%.6f mz=%.6f\n",
|
||||
sampled, d.acceleration[0], d.acceleration[1], d.acceleration[2], d.angular_rate[0], d.angular_rate[1],
|
||||
d.angular_rate[2], temp, mag.x, mag.y, mag.z);
|
||||
o << vals;
|
||||
o << " UPLOAD mem_fnv=" << std::hex << bus->mem_fnv << std::dec << " rewrites=" << bus->rewrites
|
||||
<< " perr=" << bus->protocol_errors << " data_writes=" << bus->data_writes << " burst=" << bus->burst_min << ".."
|
||||
<< bus->burst_max << " aux_w=" << bus->aux_writes << " aux_r=" << bus->aux_reads
|
||||
<< " status_reads=" << bus->status_reads << " norm=" << std::hex << bus->norm_fnv << std::dec << "\n";
|
||||
}
|
||||
if (full) {
|
||||
for (auto &l : bus->log)
|
||||
o << " TX " << l << "\n";
|
||||
std::string memp = std::string(std::getenv("BMI_OUT")) + "." + label + ".mem";
|
||||
FILE *f = std::fopen(memp.c_str(), "wb");
|
||||
std::fwrite(bus->mem, 1, sizeof(bus->mem), f);
|
||||
std::fclose(f);
|
||||
}
|
||||
return {o.str(), setup_tx, bus->log};
|
||||
}
|
||||
|
||||
TEST(BMI270Harness22778, AllScenarios) {
|
||||
ASSERT_NE(std::getenv("BMI_OUT"), nullptr);
|
||||
logger::global_logger->add_log_callback(nullptr, [](void *, uint8_t level, const char *tag, const char *msg,
|
||||
size_t len) {
|
||||
std::string s(msg, len);
|
||||
// drop the colour/header prefix up to the tag's closing bracket
|
||||
auto p = s.find("]: ");
|
||||
g_lines.push_back(std::string(1, "NEWICDVV"[level & 7]) + " " + tag + ": " +
|
||||
(p == std::string::npos ? s : s.substr(p + 3)));
|
||||
});
|
||||
SetterMap setters = load_setters();
|
||||
ASSERT_FALSE(setters.empty());
|
||||
std::ofstream out(std::string(std::getenv("BMI_OUT")) + ".report");
|
||||
uint32_t aux_tx = 0;
|
||||
std::vector<std::string> aux_log;
|
||||
for (auto &[name, list] : setters) {
|
||||
uint8_t aux_addr = 0x10;
|
||||
for (auto &s : list)
|
||||
if (s.fn == "set_aux_device_address")
|
||||
aux_addr = s.value;
|
||||
auto r = run("cfg_" + name, list, aux_addr, 0, false, 0, true);
|
||||
out << r.text;
|
||||
if (name == "aux") {
|
||||
aux_tx = r.tx;
|
||||
aux_log = r.txlog;
|
||||
}
|
||||
}
|
||||
ASSERT_NE(aux_tx, 0u);
|
||||
const auto &aux = setters["aux"];
|
||||
out << run("aux_absent", aux, 0x10, 1, false, 0, false).text;
|
||||
out << run("aux_wrong_id", aux, 0x10, 2, false, 0, false).text;
|
||||
out << run("aux_busy_stuck", aux, 0x10, 0, true, 0, false).text;
|
||||
out << run("aux_wrong_address", aux, 0x11, 0, false, 0, false).text;
|
||||
// One failure per BMM150 setup step: a NACK on the main-bus write of that step, or the aux busy bit stuck from
|
||||
// the step's aux transfer on (1 power-on, 2 soft reset, 3 chip ID read, 4-6 trim reads, 7 normal mode)
|
||||
auto main_index = [&](const std::string &prefix) -> uint32_t {
|
||||
uint32_t n = 0;
|
||||
for (auto &l : aux_log) {
|
||||
if (l.rfind("A ", 0) == 0)
|
||||
continue;
|
||||
n++;
|
||||
if (l.rfind(prefix, 0) == 0 && n > 60) // after the config upload
|
||||
return n;
|
||||
}
|
||||
ADD_FAILURE() << "no transaction " << prefix;
|
||||
return 0;
|
||||
};
|
||||
const std::pair<const char *, const char *> nack_steps[] = {
|
||||
{"step_aux_if_conf_nack", "W 6b 20"}, {"step_aux_manual_nack", "W 4c 8c"},
|
||||
{"step_aux_dev_id_nack", "W 4b 20"}, {"step_power_on_data_nack", "W 4f 01"},
|
||||
{"step_soft_reset_data_nack", "W 4f 83"}, {"step_chip_id_rd_addr_nack", "W 4d 40"},
|
||||
{"step_trim_rd_addr_nack", "W 4d 5d"}, {"step_normal_mode_data_nack", "W 4f 38"},
|
||||
{"step_auto_burst_conf_nack", "W 4c 4f"}, {"step_auto_burst_rd_addr_nack", "W 4d 42"},
|
||||
{"step_auto_burst_pwr_ctrl_nack", "W 7d 0f"},
|
||||
};
|
||||
for (auto &[label, prefix] : nack_steps)
|
||||
out << run(label, aux, 0x10, 0, false, main_index(prefix), false).text;
|
||||
// the soft reset writes the same AUX_WR_ADDR as power-on; the second "W 4e 4b" is the soft reset one
|
||||
const char *stuck_labels[] = {"step_power_on_busy", "step_soft_reset_busy", "step_chip_id_read_busy",
|
||||
"step_trim1_busy", "step_trim2_busy", "step_trim3_busy",
|
||||
"step_normal_mode_busy"};
|
||||
for (uint8_t op = 1; op <= 7; op++)
|
||||
out << run(stuck_labels[op - 1], aux, 0x10, 0, false, 0, false, op).text;
|
||||
out << run("step_chip_id_wrong", aux, 0x10, 2, false, 0, false).text;
|
||||
for (uint32_t n = 1; n <= aux_tx; n++)
|
||||
out << run("nack_" + std::to_string(n), aux, 0x10, 0, false, n, false).text;
|
||||
out << "NACK_TOTAL " << aux_tx << "\n";
|
||||
}
|
||||
|
||||
} // namespace esphome::bmi270::testing
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
# bmi270-host-run.sh TREE OUTNAME SETTERS [32]: host harness run; with 32, forces the 32 byte burst branch (perl, in
|
||||
# place; the original is saved first and put back on exit, even after an interrupted run). Writes OUT_DIR/OUTNAME and OUT_DIR/OUTNAME.log.
|
||||
# env: OUT_DIR (default ./host), PY (default TREE's venv python), PIO_CORE (default ./pio)
|
||||
T=$(cd "${1:?tree}" && pwd); N=${2:?outname}; ST=$(cd "$(dirname "${3:?setters}")" && pwd)/$(basename "$3")
|
||||
F=$T/esphome/components/bmi270/bmi270.cpp
|
||||
O=$(mkdir -p "${OUT_DIR:-host}" && cd "${OUT_DIR:-host}" && pwd); PY=${PY:-$T/venv/bin/python}
|
||||
PC=$(mkdir -p "${PIO_CORE:-pio}" && cd "${PIO_CORE:-pio}" && pwd)
|
||||
if [ "$4" = 32 ]; then
|
||||
cp "$F" "$O/$N.bmi270.cpp.orig"
|
||||
trap 'cp "$O/$N.bmi270.cpp.orig" "$F"' EXIT
|
||||
perl -pi -e 's{^#if defined\(USE_ARDUINO\) && !defined\(USE_ESP32\)$}{#if 1 // harness: force 32}' "$F"
|
||||
grep -q '^#if 1 // harness' "$F" || { echo no-sed; exit 1; }
|
||||
fi
|
||||
(cd "$T" && PLATFORMIO_CORE_DIR=$PC PYTHONPATH=$T BMI_OUT=$O/$N BMI_SETTERS=$ST \
|
||||
timeout 2400 $PY script/cpp_unit_test.py bmi270 > "$O/$N.log" 2>&1); rc=$?; echo "$N rc=$rc"
|
||||
grep -a -E '^\[ (PASSED|FAILED) |BMM150_SWEEP' "$O/$N.log"
|
||||
exit $rc
|
||||
@@ -0,0 +1,20 @@
|
||||
motion:
|
||||
- platform: bmi270
|
||||
id: imu
|
||||
i2c_id: i2c_bus
|
||||
update_interval: 5s
|
||||
aux_device: BMM150
|
||||
|
||||
sensor:
|
||||
- platform: bmi270
|
||||
bmi270_id: imu
|
||||
type: magnetic_field_x
|
||||
name: "BMI Mag X"
|
||||
- platform: bmi270
|
||||
bmi270_id: imu
|
||||
type: magnetic_field_y
|
||||
name: "BMI Mag Y"
|
||||
- platform: bmi270
|
||||
bmi270_id: imu
|
||||
type: magnetic_field_z
|
||||
name: "BMI Mag Z"
|
||||
@@ -0,0 +1,26 @@
|
||||
"""setters.py TREE OUT NAME=main.cpp...: write 'name setter value' lines for the harness from generated main.cpp."""
|
||||
|
||||
import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
tree, out = sys.argv[1], sys.argv[2]
|
||||
h = pathlib.Path(f"{tree}/esphome/components/bmi270/bmi270.h").read_text(
|
||||
encoding="utf-8"
|
||||
)
|
||||
enums = {
|
||||
m[0]: int(m[1], 0)
|
||||
for m in re.findall(r"(BMI270_[A-Z0-9_]+) = (0x[0-9A-Fa-f]+|\d+)", h)
|
||||
}
|
||||
with pathlib.Path(out).open("w", encoding="utf-8") as f:
|
||||
for arg in sys.argv[3:]:
|
||||
name, path = arg.split("=")
|
||||
calls = re.findall(
|
||||
r"imu->(set_(?:accel|gyro|aux)\w*)\((?:bmi270::)?(\w+)\);",
|
||||
pathlib.Path(path).read_text(encoding="utf-8"),
|
||||
)
|
||||
if not calls:
|
||||
f.write(f"{name} none 0\n")
|
||||
f.writelines(
|
||||
f"{name} {fn} {enums[v] if v in enums else int(v, 0)}\n" for fn, v in calls
|
||||
)
|
||||
@@ -0,0 +1,364 @@
|
||||
#pragma once
|
||||
// Fake BMI270 (+ BMM150 behind its aux interface) on an in-firmware I2C bus (esphome#22778).
|
||||
// BMI270: register file with reset values, soft reset, config upload port (INIT_ADDR_0/1 word address, INIT_DATA
|
||||
// streaming into 8 KB), INTERNAL_STATUS 0x01 only when INIT_CTRL=1 arrives with every config byte written once and
|
||||
// the memory's FNV-1a equal to `expected_fnv` (default: the current table, computed on the host from
|
||||
// bmi270_config.h), 0x02 otherwise.
|
||||
// Aux interface: AUX_DEV_ID selects the BMM150 (addr << 1); in manual mode (AUX_IF_CONF bit 7) a write to
|
||||
// AUX_WR_ADDR writes AUX_WR_DATA to the BMM150 and a write to AUX_RD_ADDR reads man_rd_burst (1/2/6/8) bytes into
|
||||
// AUX_X_LSB..; either sets STATUS.aux_busy for `busy_reads` STATUS reads. In auto mode with aux enabled the AUX data
|
||||
// registers mirror the BMM150 from AUX_RD_ADDR. Modes: bmm_mode 0 ok, 1 absent (aux transfers do nothing),
|
||||
// 2 wrong chip id; busy_stuck keeps aux_busy set (stuck_from_aux_op: from the Nth aux transfer); nack_at fails main-bus
|
||||
// transaction N (1 based). `norm_fnv` hashes the register-level effect of the stream (split or merged writes hash the
|
||||
// same). Heap is sampled at INIT_CTRL=0/1 and inside every INIT_DATA burst; -DBMI_FAKE_WRAP_MALLOC with the link flag
|
||||
// from alloc_count.hpp counts malloc() during the upload.
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "esphome/core/log.h"
|
||||
#ifdef USE_HOST
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#endif
|
||||
#ifdef USE_ESP8266
|
||||
#include <Esp.h>
|
||||
#elif defined(USE_ESP32)
|
||||
#include <esp_heap_caps.h>
|
||||
#endif
|
||||
|
||||
#ifdef BMI_FAKE_WRAP_MALLOC
|
||||
#include "alloc_count.hpp"
|
||||
#endif
|
||||
|
||||
namespace bmi270fake {
|
||||
static const char *const TAG = "bmi270fake";
|
||||
static constexpr uint8_t ADDR = 0x68;
|
||||
static constexpr size_t MEM = 8192;
|
||||
static constexpr uint8_t R_CHIP_ID = 0x00, R_STATUS = 0x03, R_AUX_X = 0x04, R_DATA_8 = 0x0C, R_INTERNAL_STATUS = 0x21,
|
||||
R_TEMP_0 = 0x22, R_AUX_DEV_ID = 0x4B, R_AUX_IF_CONF = 0x4C, R_AUX_RD_ADDR = 0x4D,
|
||||
R_AUX_WR_ADDR = 0x4E, R_AUX_WR_DATA = 0x4F, R_INIT_CTRL = 0x59, R_INIT_ADDR_0 = 0x5B,
|
||||
R_INIT_ADDR_1 = 0x5C, R_INIT_DATA = 0x5E, R_IF_CONF = 0x6B, R_PWR_CONF = 0x7C,
|
||||
R_PWR_CTRL = 0x7D, R_CMD = 0x7E;
|
||||
|
||||
inline uint32_t heap_free() {
|
||||
#ifdef USE_ESP8266
|
||||
return ESP.getFreeHeap();
|
||||
#elif defined(USE_ESP32)
|
||||
return heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
inline uint32_t heap_block() {
|
||||
#ifdef USE_ESP8266
|
||||
return ESP.getMaxFreeBlockSize();
|
||||
#elif defined(USE_ESP32)
|
||||
return heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct Bus final : public esphome::i2c::I2CBus {
|
||||
uint8_t regs[128]{};
|
||||
uint8_t mem[MEM]{};
|
||||
uint8_t written[MEM / 8]{};
|
||||
uint8_t bmm[128]{}; // BMM150 registers 0x00..0x7F
|
||||
uint8_t bmm_addr{0x10};
|
||||
uint8_t bmm_mode{0};
|
||||
bool busy_stuck{false};
|
||||
uint8_t stuck_from_aux_op{0}; // aux busy never clears from this aux transfer on (1 based), 0 = off
|
||||
uint32_t aux_ops{0};
|
||||
uint8_t busy_reads{2}, busy_left{0};
|
||||
uint32_t nack_at{0};
|
||||
uint32_t expected_fnv{0x95BB75B5}; // FNV-1a of the 8192 byte table in bmi270_config.h
|
||||
uint32_t mem_fnv{0}, norm_fnv{2166136261u};
|
||||
uint32_t transactions{0}, reads{0}, writes{0}, data_writes{0}, data_bytes{0}, addr_writes{0}, rewrites{0};
|
||||
uint32_t protocol_errors{0}, loads_ok{0}, loads_bad{0}, resets{0}, aux_writes{0}, aux_reads{0}, status_reads{0};
|
||||
uint32_t burst_min{UINT32_MAX}, burst_max{0};
|
||||
uint32_t heap_start{0}, block_start{0}, heap_end{0}, block_end{0}, heap_min_in_burst{UINT32_MAX};
|
||||
uint32_t bursts_heap_below_start{0};
|
||||
// accel x/y/z, gyro x/y/z (LE int16 at 0x0C..0x17), temperature raw at 0x22
|
||||
int16_t sample[6]{4096, -8192, 12288, 1638, -3277, 819};
|
||||
int16_t temp_raw{1024};
|
||||
// BMM150 raw data 0x42..0x49 and trim 0x5D..0x74 (datasheet-like values)
|
||||
uint8_t mag_raw[8]{0x10, 0x12, 0x20, 0xF0, 0x30, 0x08, 0x40, 0x1A};
|
||||
// x1=-2 y1=3 z4=-10 x2=26 y2=27 z2=760 z1=24786 xyz1=7101 z3=64 xy2=-3 xy1=29
|
||||
uint8_t trim[24]{0xFE, 0x03, 0x00, 0x00, 0x00, 0xF6, 0xFF, 0x1A, 0x1B, 0x00, 0x00, 0xF8,
|
||||
0x02, 0xD2, 0x60, 0xBD, 0x1B, 0x40, 0x00, 0xFD, 0x1D, 0x00, 0x00, 0x00};
|
||||
#ifdef USE_HOST
|
||||
std::vector<std::string> log; // raw transaction stream, plus "A" lines for BMM150 side effects
|
||||
#endif
|
||||
|
||||
Bus() {
|
||||
this->reset_();
|
||||
this->bmm_reset_(true);
|
||||
}
|
||||
|
||||
// Puts the BMM150 on `addr` with fault mode `mode` and powers it off
|
||||
void init_bmm(uint8_t addr, uint8_t mode = 0) {
|
||||
this->bmm_addr = addr;
|
||||
this->bmm_mode = mode;
|
||||
this->bmm_reset_(true);
|
||||
}
|
||||
|
||||
void reset_() {
|
||||
memset(this->regs, 0, sizeof(this->regs));
|
||||
this->regs[R_CHIP_ID] = 0x24;
|
||||
this->regs[R_PWR_CONF] = 0x03;
|
||||
this->regs[R_AUX_DEV_ID] = 0x20;
|
||||
this->regs[R_AUX_IF_CONF] = 0x83;
|
||||
this->regs[R_AUX_RD_ADDR] = 0x42;
|
||||
this->regs[R_AUX_WR_ADDR] = 0x4C;
|
||||
this->regs[R_AUX_WR_DATA] = 0x02;
|
||||
memset(this->mem, 0, sizeof(this->mem));
|
||||
memset(this->written, 0, sizeof(this->written));
|
||||
this->busy_left = 0;
|
||||
}
|
||||
void bmm_reset_(bool power_off) {
|
||||
uint8_t pwr = this->bmm[0x4B] & 0x01;
|
||||
memset(this->bmm, 0, sizeof(this->bmm));
|
||||
this->bmm[0x40] = this->bmm_mode == 2 ? 0x33 : 0x32;
|
||||
this->bmm[0x4B] = power_off ? 0 : pwr;
|
||||
this->bmm[0x4C] = 0x06; // sleep mode
|
||||
memcpy(&this->bmm[0x42], this->mag_raw, 8);
|
||||
memcpy(&this->bmm[0x5D], this->trim, sizeof(this->trim));
|
||||
}
|
||||
uint8_t bmm_read_(uint8_t r) {
|
||||
if (r == 0x40 && !(this->bmm[0x4B] & 0x01))
|
||||
return 0x00; // chip id reads 0 in suspend
|
||||
if (r >= 0x42 && r <= 0x49)
|
||||
return this->mag_raw[r - 0x42];
|
||||
return this->bmm[r & 0x7F];
|
||||
}
|
||||
void mix_(uint32_t v) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
this->norm_fnv ^= (v >> (8 * i)) & 0xFF;
|
||||
this->norm_fnv *= 16777619u;
|
||||
}
|
||||
}
|
||||
static uint32_t fnv(const uint8_t *d, size_t n) {
|
||||
uint32_t h = 2166136261u;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
h ^= d[i];
|
||||
h *= 16777619u;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
uint32_t covered_() const {
|
||||
uint32_t n = 0;
|
||||
for (uint8_t b : this->written)
|
||||
n += __builtin_popcount(b);
|
||||
return n;
|
||||
}
|
||||
void note_(const char *fmt, unsigned a, unsigned b) {
|
||||
#ifdef USE_HOST
|
||||
char s[48];
|
||||
snprintf(s, sizeof(s), fmt, a, b);
|
||||
this->log.push_back(s);
|
||||
#endif
|
||||
}
|
||||
bool aux_present_() const {
|
||||
return this->bmm_mode != 1 && (this->regs[R_IF_CONF] & 0x20) && this->regs[R_AUX_DEV_ID] == (this->bmm_addr << 1);
|
||||
}
|
||||
void aux_trigger_() {
|
||||
this->aux_ops++;
|
||||
if (this->stuck_from_aux_op && this->aux_ops >= this->stuck_from_aux_op)
|
||||
this->busy_stuck = true;
|
||||
this->busy_left = this->busy_reads;
|
||||
}
|
||||
void fill_data_() {
|
||||
for (int i = 0; i < 6; i++) {
|
||||
this->regs[R_DATA_8 + 2 * i] = this->sample[i] & 0xFF;
|
||||
this->regs[R_DATA_8 + 2 * i + 1] = (uint16_t) this->sample[i] >> 8;
|
||||
}
|
||||
this->regs[R_TEMP_0] = this->temp_raw & 0xFF;
|
||||
this->regs[R_TEMP_0 + 1] = (uint16_t) this->temp_raw >> 8;
|
||||
// auto mode: the BMI270 keeps AUX_X.. filled from the BMM150
|
||||
if (!(this->regs[R_AUX_IF_CONF] & 0x80) && (this->regs[R_PWR_CTRL] & 0x01) && this->aux_present_()) {
|
||||
for (int i = 0; i < 8; i++)
|
||||
this->regs[R_AUX_X + i] = this->bmm_read_(this->regs[R_AUX_RD_ADDR] + i);
|
||||
}
|
||||
}
|
||||
|
||||
void write_reg_(uint8_t reg, uint8_t v) {
|
||||
this->mix_(0x01000000u | (reg << 8) | v);
|
||||
if (reg == R_CMD && v == 0xB6) {
|
||||
this->resets++;
|
||||
this->reset_();
|
||||
return;
|
||||
}
|
||||
if (reg == R_INIT_ADDR_0 || reg == R_INIT_ADDR_1)
|
||||
this->addr_writes++;
|
||||
if (reg == R_INIT_CTRL) {
|
||||
if (v == 0x00) {
|
||||
this->heap_start = heap_free();
|
||||
this->block_start = heap_block();
|
||||
this->heap_min_in_burst = UINT32_MAX;
|
||||
this->bursts_heap_below_start = 0;
|
||||
this->regs[R_INTERNAL_STATUS] = 0x00;
|
||||
#ifdef BMI_FAKE_WRAP_MALLOC
|
||||
alloc_count::count = 0;
|
||||
alloc_count::window = true;
|
||||
#endif
|
||||
} else if (v == 0x01) {
|
||||
#ifdef BMI_FAKE_WRAP_MALLOC
|
||||
alloc_count::window = false;
|
||||
#endif
|
||||
this->heap_end = heap_free();
|
||||
this->block_end = heap_block();
|
||||
this->mem_fnv = fnv(this->mem, MEM);
|
||||
bool ok = this->mem_fnv == this->expected_fnv && this->covered_() == MEM && this->rewrites == 0;
|
||||
this->regs[R_INTERNAL_STATUS] = ok ? 0x01 : 0x02;
|
||||
(ok ? this->loads_ok : this->loads_bad)++;
|
||||
}
|
||||
}
|
||||
this->regs[reg & 0x7F] = v;
|
||||
bool manual = this->regs[R_AUX_IF_CONF] & 0x80;
|
||||
if (reg == R_AUX_WR_ADDR && manual) {
|
||||
this->aux_writes++;
|
||||
this->aux_trigger_();
|
||||
if (this->aux_present_()) {
|
||||
uint8_t d = this->regs[R_AUX_WR_DATA];
|
||||
this->note_("A W %02x %02x", v, d);
|
||||
this->mix_(0x04000000u | (v << 8) | d);
|
||||
if (v == 0x4B && (d & 0x82) == 0x82)
|
||||
this->bmm_reset_(false);
|
||||
else
|
||||
this->bmm[v & 0x7F] = d;
|
||||
}
|
||||
} else if (reg == R_AUX_RD_ADDR && manual) {
|
||||
this->aux_reads++;
|
||||
this->aux_trigger_();
|
||||
if (this->aux_present_()) {
|
||||
static const uint8_t LEN[4] = {1, 2, 6, 8};
|
||||
uint8_t n = LEN[(this->regs[R_AUX_IF_CONF] >> 2) & 3];
|
||||
this->note_("A R %02x n%u", v, n);
|
||||
this->mix_(0x05000000u | (v << 8) | n);
|
||||
for (uint8_t i = 0; i < n; i++)
|
||||
this->regs[R_AUX_X + i] = this->bmm_read_(v + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init_data_(const uint8_t *d, size_t n) {
|
||||
this->data_writes++;
|
||||
this->data_bytes += n;
|
||||
if (n < this->burst_min)
|
||||
this->burst_min = n;
|
||||
if (n > this->burst_max)
|
||||
this->burst_max = n;
|
||||
uint32_t h = heap_free();
|
||||
if (h < this->heap_min_in_burst)
|
||||
this->heap_min_in_burst = h;
|
||||
if (h < this->heap_start)
|
||||
this->bursts_heap_below_start++;
|
||||
if ((this->regs[R_PWR_CONF] & 0x01) || this->regs[R_INIT_CTRL] != 0x00) {
|
||||
this->protocol_errors++;
|
||||
return;
|
||||
}
|
||||
size_t off = 2 * ((this->regs[R_INIT_ADDR_0] & 0x0F) | ((size_t) this->regs[R_INIT_ADDR_1] << 4));
|
||||
for (size_t i = 0; i < n; i++, off++) {
|
||||
if (off >= MEM) {
|
||||
this->protocol_errors++;
|
||||
return;
|
||||
}
|
||||
this->mix_(0x02000000u | (off << 8) | d[i]);
|
||||
if (this->written[off / 8] & (1 << (off % 8)))
|
||||
this->rewrites++;
|
||||
this->written[off / 8] |= 1 << (off % 8);
|
||||
this->mem[off] = d[i];
|
||||
}
|
||||
}
|
||||
|
||||
esphome::i2c::ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out,
|
||||
size_t out_size) override {
|
||||
if (address != ADDR)
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
this->transactions++;
|
||||
#ifdef USE_HOST
|
||||
{
|
||||
std::string s = size > 1 ? "W" : "R";
|
||||
char b[8];
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
snprintf(b, sizeof(b), " %02x", data[i]);
|
||||
s += b;
|
||||
}
|
||||
if (out_size) {
|
||||
snprintf(b, sizeof(b), " r%u", (unsigned) out_size);
|
||||
s += b;
|
||||
}
|
||||
if (this->transactions == this->nack_at)
|
||||
s += " NACK";
|
||||
this->log.push_back(s);
|
||||
}
|
||||
#endif
|
||||
if (this->transactions == this->nack_at)
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
if (size == 0) {
|
||||
this->protocol_errors++;
|
||||
return esphome::i2c::ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
uint8_t reg = data[0] & 0x7F;
|
||||
if (size > 1) {
|
||||
this->writes++;
|
||||
if (reg == R_INIT_DATA) {
|
||||
this->init_data_(data + 1, size - 1);
|
||||
} else {
|
||||
for (size_t i = 1; i < size; i++)
|
||||
this->write_reg_((reg + i - 1) & 0x7F, data[i]);
|
||||
}
|
||||
}
|
||||
if (out_size) {
|
||||
this->reads++;
|
||||
// STATUS polls are timing dependent (busy wait), so they stay out of the hash
|
||||
if (reg != R_STATUS)
|
||||
this->mix_(0x03000000u | (reg << 8) | (out_size & 0xFF));
|
||||
if (reg == R_STATUS) {
|
||||
this->status_reads++;
|
||||
this->regs[R_STATUS] = (this->busy_stuck || this->busy_left) ? 0x04 : 0x00;
|
||||
if (this->busy_left)
|
||||
this->busy_left--;
|
||||
}
|
||||
this->fill_data_();
|
||||
for (size_t i = 0; i < out_size; i++)
|
||||
out[i] = this->regs[(reg + i) & 0x7F];
|
||||
}
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
|
||||
// Final configuration registers, for comparing builds
|
||||
void final_regs(char *buf, size_t n) const {
|
||||
snprintf(buf, n,
|
||||
"ACC_CONF=%02X ACC_RANGE=%02X GYR_CONF=%02X GYR_RANGE=%02X PWR_CONF=%02X PWR_CTRL=%02X IF_CONF=%02X "
|
||||
"AUX_DEV_ID=%02X AUX_IF_CONF=%02X AUX_RD_ADDR=%02X BMM_PWR=%02X BMM_OPMODE=%02X",
|
||||
this->regs[0x40], this->regs[0x41], this->regs[0x42], this->regs[0x43], this->regs[R_PWR_CONF],
|
||||
this->regs[R_PWR_CTRL], this->regs[R_IF_CONF], this->regs[R_AUX_DEV_ID], this->regs[R_AUX_IF_CONF],
|
||||
this->regs[R_AUX_RD_ADDR], this->bmm[0x4B], this->bmm[0x4C]);
|
||||
}
|
||||
|
||||
void report() const {
|
||||
char fr[220];
|
||||
this->final_regs(fr, sizeof(fr));
|
||||
ESP_LOGI(TAG,
|
||||
"UPLOAD status=0x%02X ok=%u bad=%u mem_fnv=%08X expected=%08X covered=%u rewrites=%u perr=%u resets=%u",
|
||||
this->regs[R_INTERNAL_STATUS], this->loads_ok, this->loads_bad, this->mem_fnv, this->expected_fnv,
|
||||
this->covered_(), this->rewrites, this->protocol_errors, this->resets);
|
||||
ESP_LOGI(TAG,
|
||||
"STREAM tx=%u writes=%u reads=%u data_writes=%u data_bytes=%u burst=%u..%u addr_reg_writes=%u aux_w=%u "
|
||||
"aux_r=%u norm=%08X",
|
||||
this->transactions, this->writes, this->reads, this->data_writes, this->data_bytes,
|
||||
this->burst_min == UINT32_MAX ? 0 : this->burst_min, this->burst_max, this->addr_writes, this->aux_writes,
|
||||
this->aux_reads, this->norm_fnv);
|
||||
ESP_LOGI(TAG, "REGS %s", fr);
|
||||
ESP_LOGI(TAG, "HEAP start=%u/%u end=%u/%u min_in_burst=%u bursts_below_start=%u", this->heap_start,
|
||||
this->block_start, this->heap_end, this->block_end, this->heap_min_in_burst,
|
||||
this->bursts_heap_below_start);
|
||||
#ifdef BMI_FAKE_WRAP_MALLOC
|
||||
ESP_LOGI(TAG, "MALLOC during upload=%u", (unsigned) alloc_count::count);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
inline Bus bus;
|
||||
} // namespace bmi270fake
|
||||
@@ -0,0 +1,98 @@
|
||||
#ifdef USE_HOST
|
||||
// esphome#22778: the PR's bmm150_parse_trim/bmm150_convert against the merge base's copy (bmm150_aux_base.h),
|
||||
// bit for bit, over exhaustive per-field sweeps with edge trims and random raw/trim pairs.
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include "esphome/components/bmi270/bmm150_aux.h"
|
||||
#include "bmm150_aux_base.h"
|
||||
|
||||
namespace esphome::bmi270::testing {
|
||||
|
||||
static uint32_t bits(float f) {
|
||||
uint32_t u;
|
||||
std::memcpy(&u, &f, 4);
|
||||
return u;
|
||||
}
|
||||
|
||||
struct Counts {
|
||||
uint64_t checked{0}, mismatched{0}, nan{0}, finite{0};
|
||||
};
|
||||
|
||||
static void check(const uint8_t *trim_regs, const uint8_t *raw, Counts &c) {
|
||||
const auto tp = bmm150::bmm150_parse_trim(trim_regs);
|
||||
const auto tb = bmm150_base::bmm150_parse_trim(trim_regs);
|
||||
static_assert(sizeof(tp) == sizeof(tb));
|
||||
const auto p = bmm150::bmm150_convert(tp, raw);
|
||||
const auto b = bmm150_base::bmm150_convert(tb, raw);
|
||||
c.checked++;
|
||||
bool same = std::memcmp(&tp, &tb, sizeof(tp)) == 0 && bits(p.x) == bits(b.x) && bits(p.y) == bits(b.y) &&
|
||||
bits(p.z) == bits(b.z);
|
||||
if (!same) {
|
||||
if (c.mismatched < 5)
|
||||
ADD_FAILURE() << "mismatch raw " << std::hex << (int) raw[0] << " " << (int) raw[1] << " " << (int) raw[2]
|
||||
<< " " << (int) raw[3] << " " << (int) raw[4] << " " << (int) raw[5] << " " << (int) raw[6]
|
||||
<< " " << (int) raw[7];
|
||||
c.mismatched++;
|
||||
}
|
||||
(std::isnan(p.x) || std::isnan(p.y) || std::isnan(p.z) ? c.nan : c.finite)++;
|
||||
}
|
||||
|
||||
TEST(BMM150Sweep22778, BaseAndPrDecodeIdentically) {
|
||||
std::vector<std::array<uint8_t, 24>> trims;
|
||||
// datasheet-like (same as the fake), all zero, all 0xFF, all 0x80, all 0x7F, alternating, and single zeroed words
|
||||
std::array<uint8_t, 24> ds{0xFE, 0x03, 0, 0, 0, 0xF6, 0xFF, 0x1A, 0x1B, 0, 0, 0xF8, 0x02, 0xD2, 0x60, 0xBD, 0x1B,
|
||||
0x40, 0, 0xFD, 0x1D, 0, 0, 0};
|
||||
trims.push_back(ds);
|
||||
for (uint8_t fill : {0x00, 0xFF, 0x80, 0x7F, 0x01}) {
|
||||
std::array<uint8_t, 24> t;
|
||||
t.fill(fill);
|
||||
trims.push_back(t);
|
||||
}
|
||||
for (int off : {11, 13, 15}) { // z2 = 0, z1 = 0, xyz1 = 0 give NaN paths
|
||||
auto t = ds;
|
||||
t[off] = t[off + 1] = 0;
|
||||
trims.push_back(t);
|
||||
}
|
||||
{
|
||||
auto t = ds;
|
||||
t[16] = 0xFF; // xyz1 with bit 15 set (masked)
|
||||
trims.push_back(t);
|
||||
}
|
||||
const uint8_t backgrounds[3][8] = {{0x10, 0x12, 0x20, 0xF0, 0x30, 0x08, 0x40, 0x1A},
|
||||
{0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0xFC, 0xFF},
|
||||
{0xF8, 0x7F, 0x07, 0x00, 0x01, 0xC0, 0x00, 0x00}};
|
||||
Counts c;
|
||||
for (auto &t : trims)
|
||||
for (auto &bg : backgrounds)
|
||||
for (int field = 0; field < 4; field++)
|
||||
for (uint32_t w = 0; w <= 0xFFFF; w++) {
|
||||
uint8_t raw[8];
|
||||
std::memcpy(raw, bg, 8);
|
||||
raw[2 * field] = w & 0xFF;
|
||||
raw[2 * field + 1] = w >> 8;
|
||||
check(t.data(), raw, c);
|
||||
}
|
||||
const uint64_t exhaustive = c.checked;
|
||||
std::mt19937 rng(22778);
|
||||
for (int i = 0; i < 3000000; i++) {
|
||||
uint8_t t[24], raw[8];
|
||||
for (auto &b : t)
|
||||
b = rng();
|
||||
for (auto &b : raw)
|
||||
b = rng();
|
||||
check(t, raw, c);
|
||||
}
|
||||
printf("BMM150_SWEEP exhaustive=%llu random=%llu mismatched=%llu nan_results=%llu finite_results=%llu\n",
|
||||
(unsigned long long) exhaustive, (unsigned long long) (c.checked - exhaustive),
|
||||
(unsigned long long) c.mismatched, (unsigned long long) c.nan, (unsigned long long) c.finite);
|
||||
EXPECT_EQ(c.mismatched, 0u);
|
||||
}
|
||||
|
||||
} // namespace esphome::bmi270::testing
|
||||
#endif
|
||||
@@ -0,0 +1,168 @@
|
||||
esphome:
|
||||
name: ${name}
|
||||
includes:
|
||||
- wkfake.hpp
|
||||
on_boot:
|
||||
- priority: 999.95 # before the weikai hub setup (BUS - 0.1)
|
||||
then:
|
||||
- lambda: |-
|
||||
wkfake::bus.trace = 1000;
|
||||
id(wk)->set_i2c_bus(&wkfake::bus);
|
||||
- priority: -100
|
||||
then:
|
||||
- lambda: wkfake::bus.trace = 0;
|
||||
- delay: 4s
|
||||
- script.execute: drive
|
||||
|
||||
logger:
|
||||
level: VERBOSE
|
||||
logs:
|
||||
component: DEBUG
|
||||
scheduler: DEBUG
|
||||
app: DEBUG
|
||||
switch: DEBUG
|
||||
binary_sensor: DEBUG
|
||||
gpio.binary_sensor: DEBUG
|
||||
gpio.switch: DEBUG
|
||||
esp32: DEBUG
|
||||
|
||||
i2c:
|
||||
id: realbus
|
||||
sda: GPIO21
|
||||
scl: GPIO22
|
||||
scan: false
|
||||
|
||||
wk2168_i2c:
|
||||
- id: wk
|
||||
i2c_id: realbus
|
||||
address: 0x10
|
||||
uart:
|
||||
- id: wk_uart0
|
||||
channel: 0
|
||||
baud_rate: 115200
|
||||
- id: wk_uart1
|
||||
channel: 1
|
||||
baud_rate: 9600
|
||||
|
||||
switch:
|
||||
- platform: gpio
|
||||
id: wk_out0
|
||||
name: "WK pin 0"
|
||||
restore_mode: ALWAYS_OFF
|
||||
pin:
|
||||
wk2168_i2c: wk
|
||||
number: 0
|
||||
mode:
|
||||
output: true
|
||||
on_turn_on:
|
||||
- logger.log: {format: "SWITCH pin0 ON", level: INFO}
|
||||
on_turn_off:
|
||||
- logger.log: {format: "SWITCH pin0 OFF", level: INFO}
|
||||
|
||||
binary_sensor:
|
||||
- platform: gpio
|
||||
id: wk_in1
|
||||
name: "WK pin 1"
|
||||
trigger_on_initial_state: true
|
||||
pin:
|
||||
wk2168_i2c: wk
|
||||
number: 1
|
||||
mode:
|
||||
input: true
|
||||
on_state:
|
||||
- lambda: |-
|
||||
ESP_LOGI("wktest", "BINARY pin1 = %s", x ? "ON" : "OFF");
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
id: in_high
|
||||
name: "Fake input pin1 high"
|
||||
on_press:
|
||||
- lambda: wkfake::bus.set_input(1, true);
|
||||
- platform: template
|
||||
id: in_low
|
||||
name: "Fake input pin1 low"
|
||||
on_press:
|
||||
- lambda: wkfake::bus.set_input(1, false);
|
||||
|
||||
interval:
|
||||
- interval: 100ms
|
||||
then:
|
||||
- lambda: |-
|
||||
uart::UARTComponent *u[2] = {id(wk_uart0), id(wk_uart1)};
|
||||
for (int c = 0; c < 2; c++) {
|
||||
size_t n = u[c]->available();
|
||||
if (!n) continue;
|
||||
char buf[64];
|
||||
if (n > sizeof(buf) - 1) n = sizeof(buf) - 1;
|
||||
u[c]->read_array((uint8_t *) buf, n);
|
||||
for (size_t i = 0; i < n; i++) if (buf[i] < 0x20) buf[i] = '.';
|
||||
buf[n] = 0;
|
||||
ESP_LOGI("wktest", "UART ch%d got %u bytes '%s'", c, (unsigned) n, buf);
|
||||
}
|
||||
|
||||
script:
|
||||
- id: drive
|
||||
then:
|
||||
- logger.log: {format: "STEP 1 state after setup", level: INFO}
|
||||
- lambda: wkfake::bus.dump();
|
||||
- logger.log: {format: "STEP 2 trace one poll pass", level: INFO}
|
||||
- lambda: wkfake::bus.trace = 14;
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 3 GPIO output toggles", level: INFO}
|
||||
- switch.turn_on: wk_out0
|
||||
- delay: 1s
|
||||
- switch.turn_off: wk_out0
|
||||
- delay: 1s
|
||||
- switch.turn_on: wk_out0
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 4 GPIO input changes", level: INFO}
|
||||
- button.press: in_high
|
||||
- delay: 1s
|
||||
- button.press: in_low
|
||||
- delay: 1s
|
||||
- button.press: in_high
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 5 UART TX", level: INFO}
|
||||
- lambda: |-
|
||||
id(wk_uart0)->write_str("PING0");
|
||||
id(wk_uart1)->write_str("PING1");
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 6 UART RX inject", level: INFO}
|
||||
- lambda: |-
|
||||
wkfake::bus.inject_rx(0, "hello0");
|
||||
wkfake::bus.inject_rx(1, "hello1");
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 7 three single read NACKs", level: INFO}
|
||||
- lambda: wkfake::bus.fail_reads = 1;
|
||||
- delay: 1s
|
||||
- lambda: wkfake::bus.fail_reads = 1;
|
||||
- delay: 1s
|
||||
- lambda: wkfake::bus.fail_reads = 1;
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 8 single NACK on the register pointer write", level: INFO}
|
||||
- lambda: wkfake::bus.fail_ptr = 1;
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 9 double read NACK", level: INFO}
|
||||
- lambda: wkfake::bus.fail_reads = 2;
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 10 every 37th read NACKed for 5 s", level: INFO}
|
||||
- lambda: |-
|
||||
wkfake::bus.every_n = 37;
|
||||
ESP_LOGI("wktest", "EVERYN start injected=%u retries=%u", wkfake::bus.injected, wkfake::bus.retries);
|
||||
- delay: 5s
|
||||
- lambda: |-
|
||||
wkfake::bus.every_n = 0;
|
||||
ESP_LOGI("wktest", "EVERYN end injected=%u retries=%u no_retry=%u", wkfake::bus.injected, wkfake::bus.retries, wkfake::bus.no_retry);
|
||||
- delay: 500ms
|
||||
- logger.log: {format: "STEP 11 GPIO and UART again after faults", level: INFO}
|
||||
- switch.turn_off: wk_out0
|
||||
- button.press: in_low
|
||||
- delay: 1s
|
||||
- lambda: |-
|
||||
wkfake::bus.inject_rx(0, "again0");
|
||||
id(wk_uart1)->write_str("PONG1");
|
||||
- delay: 1s
|
||||
- logger.log: {format: "STEP 12 final", level: INFO}
|
||||
- lambda: wkfake::bus.dump();
|
||||
- logger.log: {format: "DRIVE DONE", level: INFO}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
enum RegType { REG = 0, FIFO = 1 };
|
||||
// i2c_address() is the same in both trees; only the PR adds i2c_register() (the base sends register_ unchanged).
|
||||
inline uint8_t i2c_address(uint8_t base_address, uint8_t channel, RegType fifo) {
|
||||
return base_address | channel << 1 | fifo << 0;
|
||||
}
|
||||
namespace pr {
|
||||
inline uint8_t i2c_register(uint8_t reg, uint8_t &channel) {
|
||||
if (reg > 0x0F) {
|
||||
channel = (reg >> 4) & 0x03;
|
||||
}
|
||||
return reg & 0x0F;
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int same = 0, diff = 0;
|
||||
const uint8_t B = 0x10;
|
||||
for (int ch = 0; ch < 4; ch++)
|
||||
for (int reg = 0; reg < 0x40; reg++) {
|
||||
uint8_t ba = i2c_address(B, ch, REG), br = reg;
|
||||
uint8_t c = ch;
|
||||
uint8_t pr_reg = pr::i2c_register(reg, c);
|
||||
uint8_t pa = i2c_address(B, c, REG);
|
||||
bool eq = ba == pa && br == pr_reg;
|
||||
if (reg < 0x10) { eq ? same++ : diff++; if (!eq) printf("MISMATCH ch%d reg %02X\n", ch, reg); }
|
||||
else if (reg == 0x10 || reg == 0x11 || reg == 0x21 || reg == 0x31)
|
||||
printf("global %02X ch%d: base @%02X r=%02X -> PR @%02X r=%02X\n", reg, ch, ba, br, pa, pr_reg);
|
||||
}
|
||||
printf("regs below 0x10, 4 channels: %d same, %d different\n", same, diff);
|
||||
for (int ch = 0; ch < 4; ch++) printf("FIFO ch%d: @%02X in both\n", ch, i2c_address(B, ch, FIFO));
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
#pragma once
|
||||
// Fake WeiKai WK2168 on an in-firmware I2C bus (esphome#22759, 2026-11-15).
|
||||
// Device address 0 A1 A0 1 0 C1 C0 F (base 0x10 when A1 A0 = 00): eight addresses, F=1 is the FIFO of channel C1C0.
|
||||
// The register byte carries only A3..A0; the 6 bit register address is C1C0A3A2A1A0 (datasheet 12.4). Global registers
|
||||
// are 0x00 GENA, 0x01 GRST, 0x02 GMUT, 0x10 GIER, 0x11 GIFR, 0x21 GPDIR, 0x31 GPDAT; A3..A0 >= 3 is a sub-UART register
|
||||
// of channel C1C0. A register byte with bits above A3..A0 is truncated like the chip does and counted in hi_bits.
|
||||
// A write to GRST soft-resets the channels in its low nibble (registers and FIFOs to defaults) and is logged loudly.
|
||||
// Every transaction is classified: W (register write), P (1 byte write: register pointer), R (read only, uses the
|
||||
// pointer of that address), WR (write then read in one transaction, repeated start). trace > 0 logs the next `trace`
|
||||
// transactions; writes, faults and changed read values are always logged; the rest is counted in cnt[][][].
|
||||
// Fault injection: fail_reads NACKs the next read phases (R or WR), fail_ptr the next P transfers, every_n NACKs every
|
||||
// Nth read phase once. After a NACK, the next read phase on the same address and register counts as a retry.
|
||||
#include <cstring>
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace wkfake {
|
||||
static const char *const TAG = "wkfake";
|
||||
using esphome::i2c::ErrorCode;
|
||||
|
||||
enum Kind : uint8_t { K_W = 0, K_P = 1, K_R = 2, K_WR = 3 };
|
||||
static const char *const KIND[] = {"W ", "P ", "R ", "WR"};
|
||||
|
||||
struct Fifo {
|
||||
uint8_t d[256];
|
||||
uint16_t head{0}, n{0};
|
||||
void clear() { head = n = 0; }
|
||||
bool push(uint8_t b) {
|
||||
if (n == 256)
|
||||
return false;
|
||||
d[(head + n++) & 0xFF] = b;
|
||||
return true;
|
||||
}
|
||||
uint8_t pop() {
|
||||
if (!n)
|
||||
return 0;
|
||||
uint8_t b = d[head];
|
||||
head = (head + 1) & 0xFF;
|
||||
n--;
|
||||
return b;
|
||||
}
|
||||
};
|
||||
|
||||
struct Bus final : public esphome::i2c::I2CBus {
|
||||
uint8_t base{0x10};
|
||||
uint8_t glob[64]{};
|
||||
uint8_t pg0[4][16]{}, pg1[4][16]{}, spage[4]{};
|
||||
uint8_t ptr[8]{};
|
||||
uint8_t gpio_in{0};
|
||||
Fifo rx[4];
|
||||
// counters
|
||||
uint32_t cnt[8][64][4]{};
|
||||
uint32_t n_trans{0}, grst_writes{0}, hi_bits{0}, reserved{0}, tx_bytes[4]{}, tx_dropped{0}, rx_dropped{0};
|
||||
uint32_t injected{0}, retries{0}, no_retry{0}, gpdir_writes{0}, gpdat_writes{0}, gpdat_reads{0};
|
||||
uint8_t last_read[8][64];
|
||||
// controls
|
||||
uint32_t trace{0}, fail_reads{0}, fail_ptr{0}, every_n{0}, read_phases{0};
|
||||
bool await_retry{false};
|
||||
uint8_t await_addr{0}, await_reg{0};
|
||||
|
||||
Bus() {
|
||||
memset(this->last_read, 0xEE, sizeof(this->last_read));
|
||||
for (int c = 0; c < 4; c++)
|
||||
this->reset_channel_(c);
|
||||
}
|
||||
void reset_channel_(int c) {
|
||||
memset(this->pg0[c], 0, 16);
|
||||
memset(this->pg1[c], 0, 16);
|
||||
this->spage[c] = 0;
|
||||
this->rx[c].clear();
|
||||
}
|
||||
static const char *gname(uint8_t a6) {
|
||||
switch (a6) {
|
||||
case 0x00:
|
||||
return "GENA";
|
||||
case 0x01:
|
||||
return "GRST";
|
||||
case 0x02:
|
||||
return "GMUT";
|
||||
case 0x10:
|
||||
return "GIER";
|
||||
case 0x11:
|
||||
return "GIFR";
|
||||
case 0x21:
|
||||
return "GPDIR";
|
||||
case 0x31:
|
||||
return "GPDAT";
|
||||
default:
|
||||
return "RSVD";
|
||||
}
|
||||
}
|
||||
static const char *cname(uint8_t r, bool p1) {
|
||||
static const char *const P0[] = {"", "", "", "SPAGE", "SCR", "LCR", "FCR", "SIER",
|
||||
"SIFR", "TFCNT", "RFCNT", "FSR", "LSR", "FDAT", "R0E", "R0F"};
|
||||
static const char *const P1[] = {"", "", "", "SPAGE", "BRH", "BRL", "BRD", "RFI",
|
||||
"TFI", "R19", "R1A", "R1B", "R1C", "R1D", "R1E", "R1F"};
|
||||
return p1 ? P1[r & 15] : P0[r & 15];
|
||||
}
|
||||
void name_(uint8_t addr, uint8_t regbyte, char *out, size_t len) {
|
||||
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
|
||||
if (r < 3)
|
||||
snprintf(out, len, "%s(%02X)", gname((c << 4) | r), (c << 4) | r);
|
||||
else
|
||||
snprintf(out, len, "ch%u.%s", c, cname(r, r != 3 && this->spage[c]));
|
||||
}
|
||||
uint8_t gpdat_value() const {
|
||||
uint8_t dir = this->glob[0x21];
|
||||
return (this->glob[0x31] & dir) | (this->gpio_in & ~dir);
|
||||
}
|
||||
void show_gpio(const char *why) {
|
||||
ESP_LOGI(TAG, "GPIO %s: GPDIR=%02X GPDAT latch=%02X inputs=%02X pins=%02X", why, this->glob[0x21], this->glob[0x31],
|
||||
this->gpio_in, this->gpdat_value());
|
||||
}
|
||||
|
||||
void reg_write_(uint8_t addr, uint8_t regbyte, uint8_t v) {
|
||||
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
|
||||
if (r < 3) {
|
||||
uint8_t a6 = (c << 4) | r;
|
||||
switch (a6) {
|
||||
case 0x01:
|
||||
this->grst_writes++;
|
||||
ESP_LOGW(TAG, "!!!!! GRST WRITE %02X via @%02X reg byte %02X: soft reset of channels mask %X !!!!!", v, addr,
|
||||
regbyte, v & 0x0F);
|
||||
for (int i = 0; i < 4; i++)
|
||||
if (v & (1 << i))
|
||||
this->reset_channel_(i);
|
||||
return; // self clearing
|
||||
case 0x21:
|
||||
this->gpdir_writes++;
|
||||
this->glob[a6] = v;
|
||||
this->show_gpio("dir write");
|
||||
return;
|
||||
case 0x31:
|
||||
this->gpdat_writes++;
|
||||
this->glob[a6] = v;
|
||||
this->show_gpio("data write");
|
||||
return;
|
||||
case 0x00:
|
||||
case 0x02:
|
||||
case 0x10:
|
||||
case 0x11:
|
||||
this->glob[a6] = v;
|
||||
return;
|
||||
default:
|
||||
this->reserved++;
|
||||
ESP_LOGW(TAG, "write to reserved global %02X", a6);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (r == 3) {
|
||||
this->spage[c] = v & 1;
|
||||
return;
|
||||
}
|
||||
if (this->spage[c]) {
|
||||
this->pg1[c][r] = v;
|
||||
return;
|
||||
}
|
||||
if (r == 6) { // FCR: reset bits clear the FIFOs and do not stay set
|
||||
if (v & 1)
|
||||
this->rx[c].clear();
|
||||
v &= 0x0C;
|
||||
}
|
||||
if (r == 0x0D) {
|
||||
this->tx_bytes[c]++;
|
||||
return;
|
||||
}
|
||||
if (r >= 9 && r <= 0x0C)
|
||||
return; // read only
|
||||
this->pg0[c][r] = v;
|
||||
}
|
||||
|
||||
uint8_t reg_read_(uint8_t addr, uint8_t regbyte) {
|
||||
uint8_t c = (addr >> 1) & 3, r = regbyte & 0x0F;
|
||||
if (r < 3) {
|
||||
uint8_t a6 = (c << 4) | r;
|
||||
if (a6 == 0x01)
|
||||
return 0;
|
||||
if (a6 == 0x31) {
|
||||
this->gpdat_reads++;
|
||||
return this->gpdat_value();
|
||||
}
|
||||
return this->glob[a6];
|
||||
}
|
||||
if (r == 3)
|
||||
return this->spage[c];
|
||||
if (this->spage[c])
|
||||
return this->pg1[c][r];
|
||||
switch (r) {
|
||||
case 0x09:
|
||||
return 0; // TFCNT: the fake sends at once
|
||||
case 0x0A:
|
||||
return (uint8_t) this->rx[c].n; // RFCNT (256 reads 0)
|
||||
case 0x0B:
|
||||
return this->rx[c].n ? (1 << 3) : 0; // FSR: RFDAT
|
||||
case 0x0C:
|
||||
return 0; // LSR
|
||||
case 0x0D:
|
||||
return this->rx[c].pop(); // FDAT
|
||||
default:
|
||||
return this->pg0[c][r];
|
||||
}
|
||||
}
|
||||
|
||||
bool channel_on_(int c, uint8_t bit) const { return (this->glob[0] & (1 << c)) && (this->pg0[c][4] & bit); }
|
||||
|
||||
void inject_rx(int c, const char *s) {
|
||||
if (!this->channel_on_(c, 1)) {
|
||||
this->rx_dropped += strlen(s);
|
||||
ESP_LOGW(TAG, "RX inject ch%d dropped %u bytes: receiver disabled (GENA=%02X SCR=%02X)", c, (unsigned) strlen(s),
|
||||
this->glob[0], this->pg0[c][4]);
|
||||
return;
|
||||
}
|
||||
for (const char *p = s; *p; p++)
|
||||
this->rx[c].push((uint8_t) *p);
|
||||
ESP_LOGI(TAG, "RX inject ch%d '%s' fifo=%u", c, s, this->rx[c].n);
|
||||
}
|
||||
|
||||
void set_input(uint8_t pin, bool v) {
|
||||
if (v)
|
||||
this->gpio_in |= 1 << pin;
|
||||
else
|
||||
this->gpio_in &= ~(1 << pin);
|
||||
this->show_gpio("input change");
|
||||
}
|
||||
|
||||
ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out, size_t out_size) override {
|
||||
if ((address & 0xF8) != this->base)
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
if (size == 0 && out_size == 0)
|
||||
return esphome::i2c::ERROR_OK;
|
||||
this->n_trans++;
|
||||
const uint8_t a = address & 7, c = (a >> 1) & 3;
|
||||
if (a & 1) { // FIFO
|
||||
if (size) {
|
||||
char txt[48];
|
||||
size_t i = 0;
|
||||
for (; i < size && i < sizeof(txt) - 1; i++)
|
||||
txt[i] = data[i] >= 0x20 && data[i] < 0x7F ? (char) data[i] : '.';
|
||||
txt[i] = 0;
|
||||
if (this->channel_on_(c, 2)) {
|
||||
this->tx_bytes[c] += size;
|
||||
ESP_LOGI(TAG, "#%u @%02X FIFO TX ch%u %u bytes '%s'", this->n_trans, address, c, (unsigned) size, txt);
|
||||
} else {
|
||||
this->tx_dropped += size;
|
||||
ESP_LOGW(TAG, "#%u @%02X FIFO TX ch%u %u bytes '%s' DROPPED: transmitter disabled (GENA=%02X SCR=%02X)",
|
||||
this->n_trans, address, c, (unsigned) size, txt, this->glob[0], this->pg0[c][4]);
|
||||
}
|
||||
}
|
||||
if (out_size) {
|
||||
for (size_t i = 0; i < out_size; i++)
|
||||
out[i] = this->rx[c].pop();
|
||||
ESP_LOGI(TAG, "#%u @%02X FIFO RX ch%u %u bytes, %u left", this->n_trans, address, c, (unsigned) out_size,
|
||||
this->rx[c].n);
|
||||
}
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
Kind k;
|
||||
uint8_t regbyte;
|
||||
if (out_size == 0)
|
||||
k = size == 1 ? K_P : K_W, regbyte = data[0];
|
||||
else if (size == 0)
|
||||
k = K_R, regbyte = this->ptr[a];
|
||||
else
|
||||
k = K_WR, regbyte = data[0];
|
||||
if (regbyte & 0xF0 && (++this->hi_bits <= 3 || k == K_W || k == K_P)) {
|
||||
ESP_LOGW(TAG, "#%u @%02X %s reg byte %02X has bits above A3..A0; the chip sees register %X", this->n_trans,
|
||||
address, KIND[k], regbyte, regbyte & 0x0F);
|
||||
}
|
||||
this->cnt[a][regbyte & 0x3F][k]++;
|
||||
char nm_buf[24];
|
||||
// the register name is only built for the paths that log
|
||||
auto nm = [&]() {
|
||||
this->name_(address, regbyte, nm_buf, sizeof(nm_buf));
|
||||
return nm_buf;
|
||||
};
|
||||
if (k == K_P) {
|
||||
if (this->fail_ptr) {
|
||||
this->fail_ptr--;
|
||||
this->injected++;
|
||||
this->arm_retry_(address, regbyte);
|
||||
ESP_LOGW(TAG, "#%u @%02X P r=%02X %s INJECTED NACK", this->n_trans, address, regbyte, nm());
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
this->ptr[a] = regbyte;
|
||||
if (this->trace) {
|
||||
this->trace--;
|
||||
ESP_LOGI(TAG, "#%u @%02X P r=%02X %s", this->n_trans, address, regbyte, nm());
|
||||
}
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
if (k == K_W) {
|
||||
this->ptr[a] = regbyte;
|
||||
for (size_t i = 1; i < size; i++)
|
||||
this->reg_write_(address, regbyte, data[i]);
|
||||
if (this->trace)
|
||||
this->trace--;
|
||||
ESP_LOGI(TAG, "#%u @%02X W r=%02X %s = %02X", this->n_trans, address, regbyte, nm(), data[1]);
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
// read phase (R or WR)
|
||||
this->read_phases++;
|
||||
if (this->await_retry) {
|
||||
this->await_retry = false;
|
||||
if (address == this->await_addr && (regbyte == this->await_reg || k == K_R)) {
|
||||
this->retries++;
|
||||
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s RETRY after injected NACK", this->n_trans, address, KIND[k], regbyte,
|
||||
nm());
|
||||
} else {
|
||||
this->no_retry++;
|
||||
ESP_LOGW(TAG, "#%u @%02X %s r=%02X %s: no retry of the NACKed read", this->n_trans, address, KIND[k], regbyte,
|
||||
nm());
|
||||
}
|
||||
}
|
||||
if (k == K_WR)
|
||||
this->ptr[a] = regbyte;
|
||||
bool fail = false;
|
||||
if (this->fail_reads) {
|
||||
this->fail_reads--;
|
||||
fail = true;
|
||||
} else if (this->every_n && this->read_phases % this->every_n == 0) {
|
||||
fail = true;
|
||||
}
|
||||
if (fail) {
|
||||
this->injected++;
|
||||
this->arm_retry_(address, regbyte);
|
||||
memset(out, 0, out_size);
|
||||
ESP_LOGW(TAG, "#%u @%02X %s r=%02X %s INJECTED NACK", this->n_trans, address, KIND[k], regbyte, nm());
|
||||
return esphome::i2c::ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
for (size_t i = 0; i < out_size; i++)
|
||||
out[i] = this->reg_read_(address, regbyte);
|
||||
uint8_t &last = this->last_read[a][regbyte & 0x3F];
|
||||
bool changed = last != out[0];
|
||||
last = out[0];
|
||||
if (this->trace) {
|
||||
this->trace--;
|
||||
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s -> %02X", this->n_trans, address, KIND[k], regbyte, nm(), out[0]);
|
||||
} else if (changed) {
|
||||
ESP_LOGI(TAG, "#%u @%02X %s r=%02X %s -> %02X (changed)", this->n_trans, address, KIND[k], regbyte, nm(), out[0]);
|
||||
}
|
||||
return esphome::i2c::ERROR_OK;
|
||||
}
|
||||
void arm_retry_(uint8_t addr, uint8_t reg) {
|
||||
this->await_retry = true;
|
||||
this->await_addr = addr;
|
||||
this->await_reg = reg;
|
||||
}
|
||||
|
||||
void dump() {
|
||||
ESP_LOGI(TAG,
|
||||
"STATS trans=%u grst_writes=%u hi_bits=%u reserved=%u gpdir_w=%u gpdat_w=%u gpdat_r=%u tx=%u/%u "
|
||||
"tx_dropped=%u rx_dropped=%u injected=%u retries=%u no_retry=%u",
|
||||
this->n_trans, this->grst_writes, this->hi_bits, this->reserved, this->gpdir_writes, this->gpdat_writes,
|
||||
this->gpdat_reads, this->tx_bytes[0], this->tx_bytes[1], this->tx_dropped, this->rx_dropped,
|
||||
this->injected, this->retries, this->no_retry);
|
||||
this->show_gpio("now");
|
||||
for (int c = 0; c < 2; c++)
|
||||
ESP_LOGI(TAG, "ch%d SCR=%02X LCR=%02X FCR=%02X BR=%02X%02X.%X SPAGE=%u", c, this->pg0[c][4], this->pg0[c][5],
|
||||
this->pg0[c][6], this->pg1[c][4], this->pg1[c][5], this->pg1[c][6], this->spage[c]);
|
||||
char nm[24];
|
||||
for (int a = 0; a < 8; a++)
|
||||
for (int r = 0; r < 64; r++) {
|
||||
uint32_t *k = this->cnt[a][r];
|
||||
if (k[0] | k[1] | k[2] | k[3]) {
|
||||
this->name_(this->base | a, r, nm, sizeof(nm));
|
||||
ESP_LOGI(TAG, "MAP @%02X r=%02X %-12s W=%u P=%u R=%u WR=%u", this->base | a, r, nm, k[0], k[1], k[2], k[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static Bus bus;
|
||||
} // namespace wkfake
|
||||
Reference in new issue
Block a user