From 61d52503ab98a8ca8ffb2afda6bc540e799fabe4 Mon Sep 17 00:00:00 2001 From: Lars Nolden Date: Tue, 1 Sep 2026 14:53:23 +0200 Subject: [PATCH] firmware: LED heartbeat on GPIO35 (boot/alive/rx/tx/fault patterns), non-blocking --- firmware/main/CMakeLists.txt | 1 + firmware/main/board.h | 3 ++ firmware/main/led.c | 74 ++++++++++++++++++++++++++++++++++++ firmware/main/led.h | 20 ++++++++++ firmware/main/main.c | 4 +- firmware/main/repeater.c | 5 +++ 6 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 firmware/main/led.c create mode 100644 firmware/main/led.h diff --git a/firmware/main/CMakeLists.txt b/firmware/main/CMakeLists.txt index f6e9daf..c43a03a 100644 --- a/firmware/main/CMakeLists.txt +++ b/firmware/main/CMakeLists.txt @@ -5,5 +5,6 @@ idf_component_register( "wmbus.c" "repeater.c" "console.c" + "led.c" INCLUDE_DIRS "." ) diff --git a/firmware/main/board.h b/firmware/main/board.h index a448ce3..a0e8359 100644 --- a/firmware/main/board.h +++ b/firmware/main/board.h @@ -32,5 +32,8 @@ #define BOARD_TCXO_VOLTAGE_CODE 0x02 // MODE_TCXO_1_8V #define BOARD_TCXO_DELAY_STEPS 0xFF // x 15.625 us ~= 4 ms +// Status LED (white, per RNode 1.86 pin_led_rx/tx for this board) +#define BOARD_LED 35 + // Overcurrent protection value from RNode 1.86 ("OCP_TUNED 0x28") #define BOARD_SX1262_OCP 0x28 diff --git a/firmware/main/led.c b/firmware/main/led.c new file mode 100644 index 0000000..f185a0c --- /dev/null +++ b/firmware/main/led.c @@ -0,0 +1,74 @@ +#include "led.h" +#include "board.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "driver/gpio.h" +#include "esp_timer.h" + +static volatile bool s_boot_done; +static volatile int64_t s_rx_at; // us timestamps of last events +static volatile int64_t s_tx_at; +static volatile int64_t s_fault_at; + +static void set(bool on) +{ + gpio_set_level(BOARD_LED, on); +} + +// Pattern engine. 10 ms resolution is plenty for eyes and costs nothing. +static void led_task(void *arg) +{ + (void)arg; + for (;;) { + int64_t now = esp_timer_get_time(); + + if (!s_boot_done) { + set(true); // solid: powering up / radio init + vTaskDelay(pdMS_TO_TICKS(50)); + continue; + } + + if (now - s_fault_at < 5000000LL) { // fault: rapid flash, 5 s + set(true); vTaskDelay(pdMS_TO_TICKS(100)); + set(false); vTaskDelay(pdMS_TO_TICKS(100)); + continue; + } + + if (now - s_tx_at < 1000000LL) { // repeat: double blink + s_tx_at = 0; + set(true); vTaskDelay(pdMS_TO_TICKS(60)); + set(false); vTaskDelay(pdMS_TO_TICKS(80)); + set(true); vTaskDelay(pdMS_TO_TICKS(60)); + set(false); vTaskDelay(pdMS_TO_TICKS(200)); + continue; + } + + if (now - s_rx_at < 1000000LL) { // valid RX: single blink + s_rx_at = 0; + set(true); vTaskDelay(pdMS_TO_TICKS(40)); + set(false); vTaskDelay(pdMS_TO_TICKS(200)); + continue; + } + + // alive: short blip every 2 s + set(true); vTaskDelay(pdMS_TO_TICKS(30)); + set(false); vTaskDelay(pdMS_TO_TICKS(1970)); + } +} + +void led_init(void) +{ + gpio_config_t cfg = { + .pin_bit_mask = 1ULL << BOARD_LED, + .mode = GPIO_MODE_OUTPUT, + }; + gpio_config(&cfg); + set(true); + xTaskCreate(led_task, "led", 2048, NULL, 2, NULL); +} + +void led_boot_done(void) { s_boot_done = true; } +void led_notify_rx(void) { s_rx_at = esp_timer_get_time(); } +void led_notify_tx(void) { s_tx_at = esp_timer_get_time(); } +void led_notify_fault(void) { s_fault_at = esp_timer_get_time(); } diff --git a/firmware/main/led.h b/firmware/main/led.h new file mode 100644 index 0000000..911ab7e --- /dev/null +++ b/firmware/main/led.h @@ -0,0 +1,20 @@ +// Status LED: makes "powered" and "repeating" visible without any USB host. +// Exists because a standalone unit is otherwise a black box (docs/FUTURE_UI.md, +// and one full afternoon of RF-only guessing that a glance would have solved). +// +// Patterns (white LED, GPIO35): +// boot : solid ON until the radio initialises, then OFF +// alive : 30 ms blip every 2 s (radio in RX, nothing heard recently) +// rx valid : one 40 ms blink +// tx (repeat) : double blink +// fault : rapid 100 ms flashing for 5 s (recovery ladder engaged) +// +// The radio task only sets flags; all timing lives in a low-priority task so +// the LED can never perturb RX/TX timing. +#pragma once + +void led_init(void); // GPIO setup + pattern task; LED solid ON +void led_boot_done(void); // switch from solid-on to heartbeat patterns +void led_notify_rx(void); +void led_notify_tx(void); +void led_notify_fault(void); diff --git a/firmware/main/main.c b/firmware/main/main.c index 1b03a47..e026455 100644 --- a/firmware/main/main.c +++ b/firmware/main/main.c @@ -4,6 +4,7 @@ #include "esp_system.h" #include "esp_timer.h" #include "repeater.h" +#include "led.h" void console_start(void); @@ -12,6 +13,7 @@ void app_main(void) printf("{\"event\":\"boot\",\"fw\":\"wmbus-repeater\",\"version\":\"%s %s\"," "\"reset_reason\":%d}\n", __DATE__, __TIME__, (int)esp_reset_reason()); fflush(stdout); + led_init(); // solid ON = powered, initialising console_start(); - repeater_start(); + repeater_start(); // led_boot_done() fires once the radio is in RX } diff --git a/firmware/main/repeater.c b/firmware/main/repeater.c index 58f2536..94e69ad 100644 --- a/firmware/main/repeater.c +++ b/firmware/main/repeater.c @@ -2,6 +2,7 @@ #include "sx126x.h" #include "wmbus.h" #include "board.h" +#include "led.h" #include #include @@ -142,6 +143,7 @@ static bool radio_full_init(void) static bool recover(const char *reason, int level) { g_stats.recoveries++; + led_notify_fault(); bool ok = false; switch (level) { case 1: @@ -282,6 +284,7 @@ static const char *do_tx(const uint8_t *payload, int payload_len, } if (irq & SX126X_IRQ_TX_DONE) { g_stats.tx_success++; + led_notify_tx(); if (*tx_done_us > *tx_start_us) { g_stats.tx_airtime_us += (uint64_t)(*tx_done_us - *tx_start_us); } @@ -477,6 +480,7 @@ static void handle_rx(int64_t rxdone_ts) bool crc_ok = wmbus_check_crc(fmt, frame, total); if (crc_ok) g_stats.rx_valid++; else g_stats.rx_crc_fail++; + if (crc_ok) led_notify_rx(); uint32_t hash = wmbus_hash(frame, total); // RxDone fires after the fixed 255-byte capture; the frame actually // ended earlier. Reconstruct the real end-of-frame time. @@ -563,6 +567,7 @@ static void repeater_task(void *arg) recover_ladder("boot_init_failed"); } if (!sx126x_set_rx()) recover_ladder("boot_rx_failed"); + led_boot_done(); int64_t last_heartbeat = 0; for (;;) {