From c835c621722933e9a36e23afb1562c5d839fe9a3 Mon Sep 17 00:00:00 2001 From: Lars Nolden Date: Tue, 1 Sep 2026 11:23:28 +0200 Subject: [PATCH] firmware: debug/test build profiles with real behavioural differences --- .gitignore | 4 ++++ docs/TESTING.md | 23 ++++++++++++++++++++++ firmware/main/Kconfig.projbuild | 27 ++++++++++++++++++++++++++ firmware/main/repeater.c | 32 +++++++++++++++++++++++++------ firmware/sdkconfig.defaults.debug | 9 +++++++++ firmware/sdkconfig.defaults.test | 7 +++++++ hil/run.sh | 10 +++++++--- tools/build.sh | 15 +++++++++++++++ tools/flash.sh | 19 ++++++++++++++---- 9 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 firmware/main/Kconfig.projbuild create mode 100644 firmware/sdkconfig.defaults.debug create mode 100644 firmware/sdkconfig.defaults.test create mode 100755 tools/build.sh diff --git a/.gitignore b/.gitignore index 6e6cccf..e8faec1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,9 @@ captures/ logs/ result* firmware/build/ +firmware/build-*/ firmware/sdkconfig +firmware/sdkconfig.test +firmware/sdkconfig.debug +firmware/sdkconfig.old __pycache__/ diff --git a/docs/TESTING.md b/docs/TESTING.md index 7a324a9..4ab7945 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -102,3 +102,26 @@ inject sleep -> heartbeat chip_mode=0, config_intact=false -> radio_recovery level 1 -> failed (correctly refuses) -> radio_recovery level 2 -> rx_restored ``` + +## Build profiles + +``` +./tools/build.sh test # default; the configuration all HIL numbers use +./tools/build.sh debug # verbose diagnostics, assertions, per-TX dumps +./tools/flash.sh [test|debug] +``` + +Differences are behavioural, not cosmetic: + +| | test | debug | +|---|---|---| +| `verbose` at boot | off | on | +| `tx_diag` after every TX | only on failure | always | +| raw frame hex in events | yes (HIL needs it) | yes | +| optimisation / log level | size, warn | debug, info | +| task watchdog | 15 s | 30 s | + +`hil/run.sh` always builds and flashes the `test` profile, so reported +timing is never perturbed by debug serial traffic. Build directories are +separate (`build-test`, `build-debug`) so profiles cannot silently reuse a +stale sdkconfig. diff --git a/firmware/main/Kconfig.projbuild b/firmware/main/Kconfig.projbuild new file mode 100644 index 0000000..caddf8d --- /dev/null +++ b/firmware/main/Kconfig.projbuild @@ -0,0 +1,27 @@ +menu "wM-Bus repeater" + +config WMBUS_VERBOSE_DEFAULT + bool "Verbose diagnostics enabled at boot" + default n + help + Emit dropped-frame reasons, suppressed duplicates, rx_ready timing + and unexpected-IRQ events by default. Can always be toggled at + runtime with "set verbose 0|1". + +config WMBUS_TX_DIAG_ALWAYS + bool "Always emit tx_diag after every transmission" + default n + help + Logs polled IRQ state, DIO1 level, chip mode and device errors for + every TX. Useful when bringing up a new board revision; adds one + serial line per repeat, which perturbs timing measurements. + +config WMBUS_LOG_RAW_FRAMES + bool "Include raw frame hex in wmbus_rx / wmbus_tx events" + default y + help + Required by the HIL correlator to compare bytes against the SDR. + Disable only for a quiet production-style build; doing so makes + hil/correlate.py unable to match frames. + +endmenu diff --git a/firmware/main/repeater.c b/firmware/main/repeater.c index 2fc0969..5646d29 100644 --- a/firmware/main/repeater.c +++ b/firmware/main/repeater.c @@ -22,7 +22,11 @@ repeater_cfg_t g_cfg = { .repeat_delay_ms = 50, .tx_power_dbm = -9, .repeat_enabled = true, +#ifdef CONFIG_WMBUS_VERBOSE_DEFAULT + .verbose = true, +#else .verbose = false, +#endif .whitelist = {0}, .whitelist_len = 0, }; @@ -219,10 +223,18 @@ static const char *do_tx(const uint8_t *payload, int payload_len, *tx_done_us = notify_seen ? s_irq_ts : esp_timer_get_time(); fem_set_tx(false); sx126x_clear_irq(SX126X_IRQ_ALL); - LOGJ("\"event\":\"tx_diag\",\"irq\":%u,\"notify\":%s,\"dio1_high_seen\":%s," - "\"max_chip_mode\":%u,\"device_errors\":%u", - irq, notify_seen ? "true" : "false", dio1_seen ? "true" : "false", - max_mode, sx126x_get_device_errors()); +#ifdef CONFIG_WMBUS_TX_DIAG_ALWAYS + const bool want_diag = true; +#else + // otherwise only when something went wrong, so timing stays clean + const bool want_diag = g_cfg.verbose || !(irq & SX126X_IRQ_TX_DONE); +#endif + if (want_diag) { + LOGJ("\"event\":\"tx_diag\",\"irq\":%u,\"notify\":%s," + "\"dio1_high_seen\":%s,\"max_chip_mode\":%u,\"device_errors\":%u", + irq, notify_seen ? "true" : "false", dio1_seen ? "true" : "false", + max_mode, sx126x_get_device_errors()); + } if (irq & SX126X_IRQ_TX_DONE) { g_stats.tx_success++; return notify_seen ? "ok" : "ok_polled"; @@ -247,11 +259,15 @@ static void log_tx(const char *result, uint32_t hash, int len, (long long)(tx_start - rx_ts), len, hash, (int)g_cfg.tx_power_dbm, (long long)(tx_done > tx_start ? tx_done - tx_start : 0), result); +#ifdef CONFIG_WMBUS_LOG_RAW_FRAMES if (payload && payload_len > 0) { printf(",\"tx_payload\":\""); print_hex(payload, payload_len); printf("\""); } +#else + (void)payload; (void)payload_len; +#endif printf("}\n"); fflush(stdout); } @@ -421,12 +437,16 @@ static void handle_rx(int64_t rxdone_ts) printf("{\"seq\":%" PRIu32 ",\"event\":\"wmbus_rx\",\"timestamp_us\":%lld," "\"rxdone_us\":%lld,\"frequency_hz\":868950000,\"format\":\"%c\"," "\"length\":%d,\"rssi_dbm\":%d,\"crc_ok\":%s," - "\"hash\":\"%08" PRIx32 "\",\"raw\":\"", + "\"hash\":\"%08" PRIx32 "\"", s_seq++, (long long)frame_end_ts, (long long)rxdone_ts, fmt == WMBUS_FRAME_A ? 'A' : 'B', total, rssi, crc_ok ? "true" : "false", hash); +#ifdef CONFIG_WMBUS_LOG_RAW_FRAMES + printf(",\"raw\":\""); print_hex(frame, total); - printf("\"}\n"); + printf("\""); +#endif + printf("}\n"); fflush(stdout); if (!g_cfg.repeat_enabled || !crc_ok) return; diff --git a/firmware/sdkconfig.defaults.debug b/firmware/sdkconfig.defaults.debug new file mode 100644 index 0000000..f451ca8 --- /dev/null +++ b/firmware/sdkconfig.defaults.debug @@ -0,0 +1,9 @@ +# Debug profile: verbose diagnostics, assertions, per-TX register dumps. +# Extra serial traffic perturbs timing -- do not use for timing measurements. +CONFIG_WMBUS_VERBOSE_DEFAULT=y +CONFIG_WMBUS_TX_DIAG_ALWAYS=y +CONFIG_WMBUS_LOG_RAW_FRAMES=y +CONFIG_LOG_DEFAULT_LEVEL_INFO=y +CONFIG_COMPILER_OPTIMIZATION_DEBUG=y +CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y +CONFIG_ESP_TASK_WDT_TIMEOUT_S=30 diff --git a/firmware/sdkconfig.defaults.test b/firmware/sdkconfig.defaults.test new file mode 100644 index 0000000..a75fa7d --- /dev/null +++ b/firmware/sdkconfig.defaults.test @@ -0,0 +1,7 @@ +# Release/test profile: the configuration all HIL measurements are taken +# with. Structured JSON only, no per-TX register dumps, size-optimised. +CONFIG_WMBUS_VERBOSE_DEFAULT=n +CONFIG_WMBUS_TX_DIAG_ALWAYS=n +CONFIG_WMBUS_LOG_RAW_FRAMES=y +CONFIG_LOG_DEFAULT_LEVEL_WARN=y +CONFIG_COMPILER_OPTIMIZATION_SIZE=y diff --git a/hil/run.sh b/hil/run.sh index c954299..59b71a2 100755 --- a/hil/run.sh +++ b/hil/run.sh @@ -64,12 +64,16 @@ grep -q "SDR_HEALTH=ok" "$RUN_DIR/sdr_health.txt" || { # --- 4-6. build + flash + reset -------------------------------------------- if [ "$FLASH" = 1 ]; then - (cd firmware && idf.py build) > "$RUN_DIR/build.log" 2>&1 - if ! (cd firmware && idf.py -p "$DUT_PORT" flash) >> "$RUN_DIR/build.log" 2>&1; then + # always the "test" profile: the configuration every reported number + # is measured with (see firmware/sdkconfig.defaults.test) + ./tools/build.sh test > "$RUN_DIR/build.log" 2>&1 + if ! (cd firmware && idf.py -B build-test -p "$DUT_PORT" flash) \ + >> "$RUN_DIR/build.log" 2>&1; then # the port re-enumerates when entering download mode; rediscover once sleep 2 eval "$(./tools/discover_devices.sh)" - (cd firmware && idf.py -p "$DUT_PORT" flash) >> "$RUN_DIR/build.log" 2>&1 + (cd firmware && idf.py -B build-test -p "$DUT_PORT" flash) \ + >> "$RUN_DIR/build.log" 2>&1 fi sleep 2 eval "$(./tools/discover_devices.sh)" diff --git a/tools/build.sh b/tools/build.sh new file mode 100755 index 0000000..a2d9da7 --- /dev/null +++ b/tools/build.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Build a named firmware profile. +# ./tools/build.sh test (default) - configuration all HIL numbers use +# ./tools/build.sh debug - verbose, assertions, per-TX dumps +# Build dirs are kept separate so switching profiles cannot silently reuse +# a stale sdkconfig. +set -euo pipefail +cd "$(dirname "$0")/../firmware" +PROFILE=${1:-test} +case "$PROFILE" in + test|debug) ;; + *) echo "unknown profile: $PROFILE (use test|debug)" >&2; exit 2 ;; +esac +export SDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.defaults.$PROFILE" +exec idf.py -B "build-$PROFILE" -D SDKCONFIG="sdkconfig.$PROFILE" "${@:2}" build diff --git a/tools/flash.sh b/tools/flash.sh index 082da33..846d8cd 100755 --- a/tools/flash.sh +++ b/tools/flash.sh @@ -1,11 +1,22 @@ #!/usr/bin/env bash -# Build + flash the firmware to the discovered DUT. Refuses ambiguous devices. +# Build + flash a firmware profile to the discovered DUT. +# ./tools/flash.sh [test|debug] +# Refuses ambiguous serial devices (see tools/discover_devices.sh). set -euo pipefail cd "$(dirname "$0")/.." +PROFILE=${1:-test} eval "$(./tools/discover_devices.sh)" -echo "Flashing DUT: $DUT_PORT" >&2 +echo "Flashing profile '$PROFILE' to: $DUT_PORT" >&2 + +./tools/build.sh "$PROFILE" cd firmware -idf.py build -idf.py -p "$DUT_PORT" flash +if ! idf.py -B "build-$PROFILE" -p "$DUT_PORT" flash; then + # entering download mode re-enumerates the USB device; rediscover once + sleep 2 + cd .. + eval "$(./tools/discover_devices.sh)" + cd firmware + idf.py -B "build-$PROFILE" -p "$DUT_PORT" flash +fi