radiolib: RADIO_DIAG -- radiostat/radiokick for a receiver that went deaf

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-10-31 19:51:11 +01:00
1 parent 439d40ac04
commit 65ff32a10a
5 files changed
+106

No files matched your search

+4
View File
@@ -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
}
+7
View File
@@ -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
};
@@ -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
};
+61
View File
@@ -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
+11
View File
@@ -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
};
/**