firmware: LED heartbeat on GPIO35 (boot/alive/rx/tx/fault patterns), non-blocking

This commit is contained in:
Lars Nolden
2026-09-01 14:53:23 +02:00
parent b221738d2a
commit 61d52503ab
6 changed files with 106 additions and 1 deletions
+1
View File
@@ -5,5 +5,6 @@ idf_component_register(
"wmbus.c" "wmbus.c"
"repeater.c" "repeater.c"
"console.c" "console.c"
"led.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
) )
+3
View File
@@ -32,5 +32,8 @@
#define BOARD_TCXO_VOLTAGE_CODE 0x02 // MODE_TCXO_1_8V #define BOARD_TCXO_VOLTAGE_CODE 0x02 // MODE_TCXO_1_8V
#define BOARD_TCXO_DELAY_STEPS 0xFF // x 15.625 us ~= 4 ms #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") // Overcurrent protection value from RNode 1.86 ("OCP_TUNED 0x28")
#define BOARD_SX1262_OCP 0x28 #define BOARD_SX1262_OCP 0x28
+74
View File
@@ -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(); }
+20
View File
@@ -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);
+3 -1
View File
@@ -4,6 +4,7 @@
#include "esp_system.h" #include "esp_system.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "repeater.h" #include "repeater.h"
#include "led.h"
void console_start(void); void console_start(void);
@@ -12,6 +13,7 @@ void app_main(void)
printf("{\"event\":\"boot\",\"fw\":\"wmbus-repeater\",\"version\":\"%s %s\"," printf("{\"event\":\"boot\",\"fw\":\"wmbus-repeater\",\"version\":\"%s %s\","
"\"reset_reason\":%d}\n", __DATE__, __TIME__, (int)esp_reset_reason()); "\"reset_reason\":%d}\n", __DATE__, __TIME__, (int)esp_reset_reason());
fflush(stdout); fflush(stdout);
led_init(); // solid ON = powered, initialising
console_start(); console_start();
repeater_start(); repeater_start(); // led_boot_done() fires once the radio is in RX
} }
+5
View File
@@ -2,6 +2,7 @@
#include "sx126x.h" #include "sx126x.h"
#include "wmbus.h" #include "wmbus.h"
#include "board.h" #include "board.h"
#include "led.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -142,6 +143,7 @@ static bool radio_full_init(void)
static bool recover(const char *reason, int level) static bool recover(const char *reason, int level)
{ {
g_stats.recoveries++; g_stats.recoveries++;
led_notify_fault();
bool ok = false; bool ok = false;
switch (level) { switch (level) {
case 1: case 1:
@@ -282,6 +284,7 @@ static const char *do_tx(const uint8_t *payload, int payload_len,
} }
if (irq & SX126X_IRQ_TX_DONE) { if (irq & SX126X_IRQ_TX_DONE) {
g_stats.tx_success++; g_stats.tx_success++;
led_notify_tx();
if (*tx_done_us > *tx_start_us) { if (*tx_done_us > *tx_start_us) {
g_stats.tx_airtime_us += (uint64_t)(*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); 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) g_stats.rx_valid++; else g_stats.rx_crc_fail++;
if (crc_ok) led_notify_rx();
uint32_t hash = wmbus_hash(frame, total); uint32_t hash = wmbus_hash(frame, total);
// RxDone fires after the fixed 255-byte capture; the frame actually // RxDone fires after the fixed 255-byte capture; the frame actually
// ended earlier. Reconstruct the real end-of-frame time. // ended earlier. Reconstruct the real end-of-frame time.
@@ -563,6 +567,7 @@ static void repeater_task(void *arg)
recover_ladder("boot_init_failed"); recover_ladder("boot_init_failed");
} }
if (!sx126x_set_rx()) recover_ladder("boot_rx_failed"); if (!sx126x_set_rx()) recover_ladder("boot_rx_failed");
led_boot_done();
int64_t last_heartbeat = 0; int64_t last_heartbeat = 0;
for (;;) { for (;;) {