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

+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