fix: TCXO XOSC_START_ERR handling, always apply config, register DIO1 ISR callback; TX proof of life confirmed by SDR

This commit is contained in:
Lars Nolden
2026-08-31 22:43:55 +02:00
parent 3b8547fb8a
commit f7c8614a3b
9 changed files with 283 additions and 14 deletions
+4
View File
@@ -88,6 +88,10 @@ static void console_task(void *arg)
repeater_request_radio_reset();
ack("radio_reset", true, "queued");
}
else if (strcmp(line, "probe") == 0) {
repeater_request_probe();
ack("probe", true, "queued");
}
else if (strcmp(line, "test_tx") == 0) {
repeater_request_test_tx();
ack("test_tx", true, "queued");
+95 -6
View File
@@ -9,6 +9,7 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#include "driver/gpio.h"
#include "esp_task_wdt.h"
#include "esp_system.h"
@@ -31,6 +32,7 @@ static TaskHandle_t s_task;
static volatile int64_t s_irq_ts;
static volatile bool s_want_radio_reset;
static volatile bool s_want_test_tx;
static volatile bool s_want_probe;
static uint8_t s_test_access_nr;
static uint32_t s_seq;
@@ -62,9 +64,13 @@ static void irq_cb(int64_t ts)
static bool radio_full_init(void)
{
sx126x_diag_t diag;
bool ok = sx126x_init(&diag);
bool cfg_ok = ok && sx126x_config_wmbus_c1(868950000u, RX_PAYLOAD_LEN)
&& sx126x_set_tx_power(g_cfg.tx_power_dbm);
bool init_ok = sx126x_init(&diag);
// Apply the wM-Bus configuration even when init reports soft failures:
// an unconfigured radio in RX is worse than a partially degraded one,
// and the recovery ladder judges the combined result anyway.
bool cfg_ok = sx126x_config_wmbus_c1(868950000u, RX_PAYLOAD_LEN)
& sx126x_set_tx_power(g_cfg.tx_power_dbm);
cfg_ok = init_ok && cfg_ok;
LOGJ("\"event\":\"radio_init\",\"spi\":%s,\"reset\":%s,\"busy_low\":%s,"
"\"tcxo\":%s,\"device_errors\":%u,\"chip_mode\":%u,"
"\"fem_power\":true,\"fem_mode\":\"rx\",\"frequency_hz\":868950000,"
@@ -181,12 +187,33 @@ static const char *do_tx(const uint8_t *payload, int payload_len,
}
// airtime: 48 bit preamble + 16 bit sync + payload -> ~80us/byte + 640
uint32_t airtime_ms = (payload_len * US_PER_BYTE + 640) / 1000;
uint16_t irq = wait_irq(airtime_ms + 200);
*tx_done_us = s_irq_ts;
// Instrumented wait: poll IRQ flags, DIO1 level and chip mode while
// also honoring the ISR notification. Gives ground truth about the
// TX sequence even when the interrupt path is broken.
int64_t deadline = esp_timer_get_time() + (airtime_ms + 200) * 1000LL;
uint16_t irq = 0;
bool notify_seen = false, dio1_seen = false;
uint8_t max_mode = 0;
while (esp_timer_get_time() < deadline) {
if (ulTaskNotifyTake(pdTRUE, 0) > 0) notify_seen = true;
if (gpio_get_level(BOARD_LORA_DIO1)) dio1_seen = true;
uint8_t st = sx126x_get_status();
uint8_t mode = (st >> 4) & 0x7;
if (mode > max_mode) max_mode = mode;
irq = sx126x_get_irq();
if (irq) break;
vTaskDelay(pdMS_TO_TICKS(1));
}
*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());
if (irq & SX126X_IRQ_TX_DONE) {
g_stats.tx_success++;
return "ok";
return notify_seen ? "ok" : "ok_polled";
}
if (irq & SX126X_IRQ_TIMEOUT) {
g_stats.tx_timeout++;
@@ -231,6 +258,62 @@ static void run_test_tx(void)
sx126x_restore_rx_payload_len();
if (!sx126x_set_rx()) recover_ladder("test_tx_rx_restore");
}
// ---------------------------------------------------------------- probe
static void hexdump_json(const char *key, const uint8_t *d, int n)
{
printf("\"%s\":\"", key);
print_hex(d, n);
printf("\",");
}
// Scripted low-level diagnostic with raw SPI response dumps.
static void run_probe(void)
{
uint8_t r[16];
printf("{\"event\":\"probe\",");
sx126x_standby();
sx126x_raw(0xC0, NULL, 0, r, 2); // GetStatus
hexdump_json("status0", r, 2);
// SetDioIrqParams: all IRQs, all on DIO1
uint8_t di[8] = {0x03, 0xFF, 0x03, 0xFF, 0, 0, 0, 0};
sx126x_raw(0x08, di, 8, r, 9);
hexdump_json("setirq_resp", r, 9);
sx126x_raw(0xC0, NULL, 0, r, 2); // status after SetDioIrqParams
hexdump_json("status_after_setirq", r, 2);
sx126x_raw(0x02, (uint8_t[]){0x03, 0xFF}, 2, NULL, 0); // ClearIrq
sx126x_raw(0x12, NULL, 0, r, 4); // GetIrqStatus raw
hexdump_json("irq_cleared", r, 4);
// small TX: 8 byte payload
uint8_t pkt[8] = {0x54, 0xCD, 0xAA, 0x55, 0xDE, 0xAD, 0xBE, 0xEF};
sx126x_start_tx(pkt, 8);
printf("\"txpoll\":[");
for (int i = 0; i < 12; i++) {
vTaskDelay(pdMS_TO_TICKS(2));
uint8_t s2[2], q[4];
sx126x_raw(0xC0, NULL, 0, s2, 2);
sx126x_raw(0x12, NULL, 0, q, 4);
printf("\"%02X-%02X%02X%02X%02X\"%s", s2[1], q[0], q[1], q[2], q[3],
i < 11 ? "," : "");
}
printf("],");
fem_set_tx(false);
sx126x_raw(0x17, NULL, 0, r, 4); // GetDeviceErrors
hexdump_json("dev_errors", r, 4);
sx126x_clear_irq(SX126X_IRQ_ALL);
sx126x_restore_rx_payload_len();
sx126x_set_rx();
printf("\"done\":true}\n");
fflush(stdout);
}
// ------------------------------------------------------------------ RX path
@@ -333,6 +416,7 @@ static void repeater_task(void *arg)
{
(void)arg;
s_task = xTaskGetCurrentTaskHandle();
sx126x_set_irq_callback(irq_cb);
esp_task_wdt_add(NULL);
if (!radio_full_init()) {
@@ -354,6 +438,10 @@ static void repeater_task(void *arg)
s_want_test_tx = false;
run_test_tx();
}
if (s_want_probe) {
s_want_probe = false;
run_probe();
}
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) > 0) {
uint16_t irq = sx126x_get_irq();
@@ -388,6 +476,7 @@ void repeater_start(void)
void repeater_request_radio_reset(void) { s_want_radio_reset = true; }
void repeater_request_test_tx(void) { s_want_test_tx = true; }
void repeater_request_probe(void) { s_want_probe = true; }
// ------------------------------------------------------------------ status
+1
View File
@@ -36,6 +36,7 @@ extern repeater_stats_t g_stats;
void repeater_start(void);
void repeater_request_radio_reset(void);
void repeater_request_test_tx(void);
void repeater_request_probe(void);
void repeater_print_status(void);
void repeater_print_stats(void);
void repeater_print_radio_status(void);
+34 -8
View File
@@ -231,10 +231,22 @@ bool sx126x_init(sx126x_diag_t *diag)
read_regs(REG_SYNCWORD_0, verify, 2);
diag->spi_ok = (verify[0] == 0x54 && verify[1] == 0x3D);
// TCXO on DIO3 (1.8 V, ~4 ms), then recalibrate everything because
// enabling the TCXO invalidates the RC-based calibration.
// TCXO on DIO3 (1.8 V, ~4 ms). The first calibrate after enabling the
// TCXO latches a benign XOSC_START_ERR (the XOSC block briefly probes
// the crystal before DIO3 powers the TCXO). Correct sequence: enable,
// clear stale errors, force STDBY_XOSC (actually starts the TCXO),
// recalibrate, then judge the remaining error flags.
uint8_t tcxo[4] = {BOARD_TCXO_VOLTAGE_CODE, 0x00, 0x00, BOARD_TCXO_DELAY_STEPS};
cmd(OP_SET_DIO3_TCXO_CTRL, tcxo, 4);
cmd(OP_CLEAR_DEVICE_ERRORS, (uint8_t[]){0, 0}, 2);
uint8_t stdby_xosc = 0x01;
cmd(OP_SET_STANDBY, &stdby_xosc, 1);
vTaskDelay(pdMS_TO_TICKS(10));
busy_wait();
bool xosc_running = (((sx126x_get_status() >> 4) & 0x7) == 0x3);
cmd(OP_CLEAR_DEVICE_ERRORS, (uint8_t[]){0, 0}, 2);
uint8_t calib = 0x7F;
cmd(OP_CALIBRATE, &calib, 1);
vTaskDelay(pdMS_TO_TICKS(10));
@@ -242,9 +254,9 @@ bool sx126x_init(sx126x_diag_t *diag)
uint16_t errs = sx126x_get_device_errors();
diag->device_errors = errs;
// XOSC_START_ERR (bit 5) would indicate TCXO trouble
diag->tcxo_ok = ((errs & 0x20) == 0);
diag->tcxo_ok = xosc_running && ((errs & 0x20) == 0);
if (errs) cmd(OP_CLEAR_DEVICE_ERRORS, (uint8_t[]){0, 0}, 2);
cmd(OP_SET_STANDBY, &(uint8_t){0x00}, 1);
// DC-DC regulator (Heltec module has the inductor; RNode uses default LDO,
// but SX1262 modules from Heltec are DCDC-capable. Use DCDC for lower
@@ -312,11 +324,11 @@ bool sx126x_config_wmbus_c1(uint32_t freq_hz, uint8_t rx_payload_len)
ok &= cmd(OP_SET_BUFFER_BASE, (uint8_t[]){0x00, 0x00}, 2);
// IRQs on DIO1
uint16_t irqs = SX126X_IRQ_TX_DONE | SX126X_IRQ_RX_DONE | SX126X_IRQ_TIMEOUT;
// IRQs: enable ALL in the latch mask (diagnosis of missing TxDone/RxDone;
// flags only latch when enabled here), route all to DIO1.
uint8_t di[8] = {
irqs >> 8, irqs & 0xFF, // IRQ mask
irqs >> 8, irqs & 0xFF, // DIO1
0x03, 0xFF, // IRQ mask: all
0x02, 0x03, // DIO1: TxDone | RxDone | Timeout
0x00, 0x00, // DIO2 (RF switch)
0x00, 0x00, // DIO3 (TCXO)
};
@@ -445,6 +457,20 @@ uint16_t sx126x_get_device_errors(void)
if (!cmd_read(OP_GET_DEVICE_ERRORS, e, 2)) return 0xFFFF;
return ((uint16_t)e[0] << 8) | e[1];
}
bool sx126x_raw(uint8_t op, const uint8_t *args, size_t n_tx,
uint8_t *rx, size_t rx_len)
{
uint8_t tx[32] = {0}, rxb[32] = {0};
size_t total = 1 + n_tx;
if (rx_len > total) total = rx_len;
if (total > sizeof(tx)) return false;
tx[0] = op;
if (n_tx) memcpy(tx + 1, args, n_tx);
if (!xfer(tx, rxb, total)) return false;
if (rx && rx_len) memcpy(rx, rxb, rx_len);
return true;
}
uint8_t sx126x_get_status(void)
{
+4
View File
@@ -49,6 +49,10 @@ int sx126x_read_rx(uint8_t *out, size_t maxlen, int8_t *rssi_dbm);
int8_t sx126x_get_rssi_inst(void);
uint16_t sx126x_get_device_errors(void);
uint8_t sx126x_get_status(void);
// Raw command exchange for diagnostics: sends op + n_tx arg bytes, captures
// the full-duplex MISO bytes into rx (same length). Returns false on error.
bool sx126x_raw(uint8_t op, const uint8_t *args, size_t n_tx,
uint8_t *rx, size_t rx_len);
// FEM control (exposed for diagnostics)
void fem_set_tx(bool tx);