From 812b3bb9ac506ee94d12588fa45434a58848cd5e Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Sat, 31 Oct 2026 19:51:11 +0100 Subject: [PATCH] radiolib: RADIO_DIAG -- radiostat/radiokick for a receiver that went deaf Co-Authored-By: Claude Opus 5.5 --- examples/simple_sensor/SensorMesh.cpp | 4 ++ src/helpers/radiolib/CustomSX1262.h | 7 +++ src/helpers/radiolib/CustomSX1262Wrapper.h | 23 ++++++++ src/helpers/radiolib/RadioLibWrappers.cpp | 61 ++++++++++++++++++++++ src/helpers/radiolib/RadioLibWrappers.h | 11 ++++ 5 files changed, 106 insertions(+) diff --git a/examples/simple_sensor/SensorMesh.cpp b/examples/simple_sensor/SensorMesh.cpp index b1860668..6a799427 100644 --- a/examples/simple_sensor/SensorMesh.cpp +++ b/examples/simple_sensor/SensorMesh.cpp @@ -515,6 +515,10 @@ void SensorMesh::handleCommand(ClientInfo* from, uint32_t sender_timestamp, char board.setGpio(val); } sprintf(reply, "%x", board.getGpio()); +#ifdef RADIO_DIAG + // CUSTOM (TeTeHacko): `radiostat` / `radiokick` -- see RadioLibWrappers.cpp. + } else if (radio_driver.diagHandleCommand(command, reply)) { +#endif } else{ _cli.handleCommand(sender_timestamp, command, reply); // common CLI commands } diff --git a/src/helpers/radiolib/CustomSX1262.h b/src/helpers/radiolib/CustomSX1262.h index b4ee6c97..e5c88aed 100644 --- a/src/helpers/radiolib/CustomSX1262.h +++ b/src/helpers/radiolib/CustomSX1262.h @@ -162,4 +162,11 @@ class CustomSX1262 : public SX1262 { readRegister(RADIOLIB_SX126X_REG_RX_GAIN, &rxGain, 1); return (rxGain == RADIOLIB_SX126X_RX_GAIN_BOOSTED); } + +#ifdef RADIO_DIAG + // CUSTOM (TeTeHacko): public views of SX126x commands RadioLib keeps + // protected, for `radiostat` (RadioLibWrappers.cpp). Reads only. + uint8_t diagStatus() { return getStatus(); } + uint16_t diagDeviceErrors() { return getDeviceErrors(); } +#endif }; \ No newline at end of file diff --git a/src/helpers/radiolib/CustomSX1262Wrapper.h b/src/helpers/radiolib/CustomSX1262Wrapper.h index ac6f9a7c..0ca96b3d 100644 --- a/src/helpers/radiolib/CustomSX1262Wrapper.h +++ b/src/helpers/radiolib/CustomSX1262Wrapper.h @@ -62,4 +62,27 @@ public: } void doResetAGC() override { sx126xResetAGC((SX126x *)_radio, getRxBoostedGainMode()); } + +#ifdef RADIO_DIAG +protected: + // CUSTOM (TeTeHacko): `radiostat`, chip side. mode: 2 STBY_RC, 3 STBY_XOSC, + // 4 FS, 5 RX, 6 TX; cmd: 2 data available, 3 timeout, 4 processing error, + // 5 exec failure. Pin levels are read back, so -1 = not wired. + int diagChip(char* out, int sz) override { + CustomSX1262* r = (CustomSX1262 *)_radio; + uint8_t s = r->diagStatus(); + int dio1 = -1, rxen = -1; + #ifdef P_LORA_DIO_1 + if ((uint32_t) P_LORA_DIO_1 != (uint32_t) RADIOLIB_NC) dio1 = digitalRead(P_LORA_DIO_1); + #endif + #ifdef SX126X_RXEN + if ((uint32_t) SX126X_RXEN != (uint32_t) RADIOLIB_NC) rxen = digitalRead(SX126X_RXEN); + #endif + return snprintf(out, sz, " mode=%u cmd=%u irq=0x%04lx deverr=0x%04x rssi=%d dio1=%d rxen=%d boost=%d", + (unsigned) ((s >> 4) & 7), (unsigned) ((s >> 1) & 7), + (unsigned long) r->getIrqFlags(), (unsigned) r->diagDeviceErrors(), + (int) r->getRSSI(false), dio1, rxen, (int) r->getRxBoostedGainMode()); + } +public: +#endif }; diff --git a/src/helpers/radiolib/RadioLibWrappers.cpp b/src/helpers/radiolib/RadioLibWrappers.cpp index 63ce8a62..f9370aa9 100644 --- a/src/helpers/radiolib/RadioLibWrappers.cpp +++ b/src/helpers/radiolib/RadioLibWrappers.cpp @@ -12,6 +12,10 @@ #define SAMPLING_THRESHOLD 14 static volatile uint8_t state = STATE_IDLE; +#ifdef RADIO_DIAG +static volatile uint32_t diag_isr_count = 0; // DIO1 edges that reached the MCU +static volatile uint32_t diag_isr_ms = 0; // millis() of the last one +#endif // this function is called when a complete packet // is transmitted by the module @@ -22,6 +26,10 @@ static void setFlag(void) { // we sent a packet, set the flag state |= STATE_INT_READY; +#ifdef RADIO_DIAG + diag_isr_count++; + diag_isr_ms = millis(); +#endif } void RadioLibWrapper::begin() { @@ -183,6 +191,9 @@ int RadioLibWrapper::recvRaw(uint8_t* bytes, int sz) { } else { // Serial.print(" readData() -> "); Serial.println(len); n_recv++; +#ifdef RADIO_DIAG + _diag_last_rx_ms = millis(); +#endif } } #if defined(USE_LR2021) @@ -380,3 +391,53 @@ PacketMillis RadioLibWrapper::calcMaxPacketMillis(uint8_t sf, float bw, uint8_t return PacketMillis {(preamble_us + 999) / 1000, (payload_us + 999) / 1000}; } + +#ifdef RADIO_DIAG +// CUSTOM (TeTeHacko): a receiver that silently stops receiving (31. 10. 2026, +// bench-sensor: recv frozen for minutes, noise_floor -120, last_rssi 0, a reboot +// revived it) can be stuck in several different places, and each one is a +// different bug: +// +// chip mode != RX -> the chip left RX (reset/brownout, or a +// command path that put it in standby) +// while `st` still says RX +// mode RX, RX_DONE in irq, DIO1=1 -> the packet is there and the line is up, +// but the edge never reached the MCU +// (isr count frozen): lost interrupt +// mode RX, irq clean, RSSI ~floor -> analog front end / AGC stuck +// +// `radiostat` reads all of it without touching the radio state. `radiokick` +// then applies ONE recovery step at a time, from the gentlest; the first step +// that brings recv back names the layer that was broken. +bool RadioLibWrapper::diagHandleCommand(const char* command, char* reply) { + if (strcmp(command, "radiostat") == 0) { + uint32_t now = millis(); + int n = snprintf(reply, 160, "st=%u isr=%lu/%lus rx=%lu/%lus err=%lu", + (unsigned) state, (unsigned long) diag_isr_count, + (unsigned long) ((now - diag_isr_ms) / 1000), + (unsigned long) n_recv, (unsigned long) ((now - _diag_last_rx_ms) / 1000), + (unsigned long) n_recv_errors); + if (n > 0 && n < 160) diagChip(reply + n, 160 - n); + return true; + } + if (memcmp(command, "radiokick ", 10) == 0) { + const char* what = command + 10; + if (strcmp(what, "rx") == 0) { // re-arm RX only (no standby, no clear) + state = STATE_IDLE; + } else if (strcmp(what, "clrirq") == 0) { // drop latched IRQs: DIO1 falls, next RX_DONE is a new edge + _radio->clearIrqFlags(0xFFFFFFFF); + } else if (strcmp(what, "standby") == 0) { // standby + startReceive + idle(); + } else if (strcmp(what, "agc") == 0) { // warm sleep + calibrate, the agc.reset path + doResetAGC(); + state = STATE_IDLE; + } else { + strcpy(reply, "usage: radiokick rx|clrirq|standby|agc"); + return true; + } + sprintf(reply, "OK - %s", what); + return true; + } + return false; +} +#endif diff --git a/src/helpers/radiolib/RadioLibWrappers.h b/src/helpers/radiolib/RadioLibWrappers.h index 33462e3f..573bf0df 100644 --- a/src/helpers/radiolib/RadioLibWrappers.h +++ b/src/helpers/radiolib/RadioLibWrappers.h @@ -110,6 +110,17 @@ public: // CUSTOM (TeTeHacko): salvage diagnostics -- see `dio1` in simple_repeater. int probeDio1(uint32_t* irq_out, int* dio1_level, int* probe_level); #endif + +#ifdef RADIO_DIAG + // CUSTOM (TeTeHacko): `radiostat` / `radiokick` -- what state is a receiver + // that has gone deaf actually in? See RadioLibWrappers.cpp. + bool diagHandleCommand(const char* command, char* reply); +protected: + uint32_t _diag_last_rx_ms = 0; // millis() of the last packet read without error + // Chip-side half of the snapshot: mode, IRQ, errors, pins. Default = none. + virtual int diagChip(char* out, int sz) { return snprintf(out, sz, " chip=n/a"); } +public: +#endif }; /**