From b9848fe79a0bbdace58228826519a6b37a074b15 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 4 Aug 2026 21:32:58 +0200 Subject: [PATCH] feat(radio): DIO1-less operation + pin diagnostics, to salvage bench-board bench-board died 29. 7. 2026 to a breadboard short that put GND on D1 (= DIO1) and 5 V on D0. Signature: `sent 0` with tx_timeout climbing WHILE A WITNESS HEARD THE FRAMES, plus `recv 0` at strong signal -- the radio works, the interrupt line does not. setPacketReceivedAction() is the only thing that ever sets STATE_INT_READY, so a dead line means nothing is ever picked up. LORA_POLL_IRQ reads completion from the chip's IRQ status register over SPI instead. The chip latches those events regardless of whether they are mapped out to a DIO pin, so this bypasses the whole line and therefore works no matter WHICH end of it broke. Rewiring DIO1 to a spare GPIO only helps if the broken end is the MCU's input pad -- and that is the less likely end, because grounding an input cannot damage the input, it stresses whatever drives the net. Polling is strictly stronger: if it does not revive the node, no wire will either. Cost is one extra SPI read per Dispatcher pass, next to the isReceivingPacket() read already in that path. getIrqFlags() returns the RAW chip register (SX126x bits, not RadioLib's generic RADIOLIB_IRQ_* indices), so the mask comes from a new virtual irqDoneMask() that only the chip-specific wrapper can fill in. PIN_DIAG answers "which end?" without a multimeter: - boot-time pull-up/pull-down sweep of every header pin, placed BEFORE radio_init() because that halt()s when the radio does not answer -- so it is the only diagnostic that still runs with the module unplugged, which is also the only state where D1's result means anything (attached, the SX1262 drives DIO1 low and a healthy board reads 0 too). The internal pull is the ohmmeter. It cannot see a lifted pad -- the resistor is on the die -- hence the optional PIN_DIAG_LOOP_A/B jumper loopback, which drives from outside and reads while pulling AGAINST the driver. - `dio1` has the radio assert DIO1 via a 1-byte transmit and compares the IRQ status register against the actual pin level. Where they disagree is where the break is. All of it is behind #ifdef; a build without the flags is unchanged. Co-Authored-By: Claude Opus 5 (1M context) --- examples/simple_repeater/MyMesh.cpp | 6 ++ examples/simple_repeater/main.cpp | 108 +++++++++++++++++++++ src/helpers/radiolib/CustomSX1262Wrapper.h | 10 +- src/helpers/radiolib/RadioLibWrappers.cpp | 93 ++++++++++++++++++ src/helpers/radiolib/RadioLibWrappers.h | 18 ++++ 5 files changed, 234 insertions(+), 1 deletion(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index d80ffd98..0132ee8d 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1644,6 +1644,12 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply // verdict, which `set tx` swallows. See main.cpp. extern bool txPwrHandleCommand(const char* command, char* reply); if (txPwrHandleCommand(command, reply)) return; +#ifdef PIN_DIAG + // CUSTOM (TeTeHacko): `dio1` — is the DIO1 IRQ line alive, and if not, which + // END of it is broken? Salvage diagnostics, see main.cpp. + extern bool pinDiagHandleCommand(const char* command, char* reply); + if (pinDiagHandleCommand(command, reply)) return; +#endif #if defined(BLE_PIN_CODE) && defined(NRF52_PLATFORM) // CUSTOM (TeTeHacko): `ble on|off|ble` — implemented in main.cpp (BLE console). // Available from serial, the BLE console and remotely via MC (admin remote CLI). diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 080253eb..c87a0709 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -532,6 +532,43 @@ bool txPwrHandleCommand(const char* command, char* reply) { return true; } +#ifdef PIN_DIAG +// CUSTOM (TeTeHacko): `dio1` -- salvage diagnostic for a node whose radio +// transmits (a witness hears the frames) but never reports completion, i.e. the +// classic dead-DIO1 signature: `sent 0`, tx_timeout climbing, `recv 0`. +// +// The board itself becomes the instrument. probeDio1() has the SX1262 generate a +// TX_DONE and then compares two things that a working line keeps in step: the +// chip's IRQ status register (over SPI, so it is true regardless of the pin) and +// the level on the pin. Where they disagree is where the break is. +bool pinDiagHandleCommand(const char* command, char* reply) { + if (memcmp(command, "dio1", 4) != 0 || command[4] != 0) return false; + + uint32_t irq = 0; + int lvl = -1, probe = -1; + int st = radio_driver.probeDio1(&irq, &lvl, &probe); + + if (st == -1) { strcpy(reply, "ERR - busy transmitting, try again"); return true; } + if (st == -3) { strcpy(reply, "ERR - this radio exposes no IRQ mask"); return true; } + if (st == -2) { + sprintf(reply, "dio1: NO TX_DONE within 2s (irq=0x%04lx) - the fault is NOT the DIO1 line " + "(SPI/BUSY/PA); the chip never finished the transmit", + (unsigned long) irq); + return true; + } + if (st != 0) { sprintf(reply, "ERR - startTransmit %d", st); return true; } + + const char* verdict; + if (lvl == 1) verdict = "line OK - DIO1 rose with the IRQ, look elsewhere"; + else if (probe == 1) verdict = "MCU input pad DEAD (probe wire sees the signal) -> rewiring FIXES it"; + else if (probe == 0) verdict = "net dead at the SX1262 end -> rewiring will NOT help"; + else verdict = "DIO1 stayed LOW - fit a probe wire (PIN_DIAG_PROBE) to tell the ends apart"; + sprintf(reply, "dio1: irq=0x%04lx TX_DONE set, pin=%d probe=%d - %s", + (unsigned long) irq, lvl, probe, verdict); + return true; +} +#endif + void halt() { while (1) ; } @@ -581,6 +618,77 @@ void setup() { } #endif +#if defined(PIN_DIAG) && defined(XIAO_NRF52) + // CUSTOM (TeTeHacko): header GPIO integrity sweep, for salvaging a board whose + // pins were abused (bench-board took GND on D1 and 5 V on D0 in a breadboard). + // + // Deliberately placed BEFORE radio_init(), because that halt()s when the radio + // does not answer -- so this is the only diagnostic that still runs with the + // XIAO UNPLUGGED from the Wio-SX1262 module. Unplugged is also the only state + // in which the D1 result means anything: attached, the SX1262 drives DIO1 low + // and a perfectly healthy board reads 0 too. + // + // The internal pull resistor IS the ohmmeter (~13 kOhm): a floating pin follows + // it, a pin shorted to a rail does not. Run the same build on a known-good + // board as the paired control. + // + // What the pull test CANNOT see: a lifted pad or broken bond. The resistor sits + // on the die, so an open pad still reads 1/0 exactly like a healthy one while + // the outside world is disconnected. Only the loopback below catches that. + { + static const uint8_t diag_pins[] = { D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10 }; + static const char* diag_use[] = { "-", "DIO1", "RESET", "BUSY", "NSS", "RXEN", + "SCL", "SDA", "SCK", "MISO", "MOSI" }; + Serial.println("PIN_DIAG: unplug the Wio module first, otherwise the radio drives these pins"); + Serial.println("PIN_DIAG: pin use pu pd verdict"); + for (uint8_t i = 0; i < sizeof(diag_pins); i++) { + uint8_t p = diag_pins[i]; + pinMode(p, INPUT_PULLUP); delay(2); int pu = digitalRead(p); + pinMode(p, INPUT_PULLDOWN); delay(2); int pd = digitalRead(p); + pinMode(p, INPUT); + const char* v = (pu == 1 && pd == 0) ? "follows pulls (pad healthy OR open)" + : (pu == 0 && pd == 0) ? "STUCK LOW <- shorted to GND?" + : (pu == 1 && pd == 1) ? "STUCK HIGH <- shorted to VDD?" + : "inverted?!"; + Serial.printf("PIN_DIAG: D%-2d %-5s %d %d %s\n", (int) i, diag_use[i], pu, pd, v); + } + + #if defined(PIN_DIAG_LOOP_A) && defined(PIN_DIAG_LOOP_B) + // Loopback over ONE jumper wire between two pins -- the only test that can + // see a lifted pad, because it drives from outside the die. Each read pulls + // AGAINST the driver, so a pass proves the driver actually wins the pin. + // + // DANGER: this DRIVES pins. Never enable it with the Wio module attached. + // Driving DIO1 against the SX1262's own output is the very fight that is + // suspected of having killed this board. + { + const uint8_t pair[2] = { PIN_DIAG_LOOP_A, PIN_DIAG_LOOP_B }; + int ok = 0; + for (int dir = 0; dir < 2; dir++) { + uint8_t drv = pair[dir], rd = pair[1 - dir]; + pinMode(drv, OUTPUT); + for (int lvl = 0; lvl < 2; lvl++) { + digitalWrite(drv, lvl); + pinMode(rd, lvl ? INPUT_PULLDOWN : INPUT_PULLUP); // pull against it + delay(2); + int got = digitalRead(rd); + if (got == lvl) ok++; + Serial.printf("PIN_DIAG: loop pin%d=%d -> pin%d reads %d %s\n", + (int) drv, lvl, (int) rd, got, got == lvl ? "ok" : "FAIL"); + } + pinMode(drv, INPUT); + } + Serial.printf("PIN_DIAG: loopback %d/4 - 4 means both pads drive AND sense through the wire\n", ok); + } + #endif + + // The sweep took the I2C pins away from the TWI peripheral that board.begin() + // configured. Nothing here depends on I2C (no RTC, no sensors) and the clock + // falls back to VolatileRTCClock, but say so rather than leave it silent. + Serial.println("PIN_DIAG: done (I2C pin config was clobbered by the sweep)"); + } +#endif + #ifdef DISPLAY_CLASS if (display.begin()) { display.startFrame(); diff --git a/src/helpers/radiolib/CustomSX1262Wrapper.h b/src/helpers/radiolib/CustomSX1262Wrapper.h index cc7bb223..034edbb4 100644 --- a/src/helpers/radiolib/CustomSX1262Wrapper.h +++ b/src/helpers/radiolib/CustomSX1262Wrapper.h @@ -20,9 +20,17 @@ public: updatePreamble(sf); } - bool isReceivingPacket() override { + bool isReceivingPacket() override { return ((CustomSX1262 *)_radio)->isReceiving(); } +#if defined(LORA_POLL_IRQ) || defined(PIN_DIAG) + // CUSTOM (TeTeHacko): SX126x native status bits. TIMEOUT is in the mask because + // an RX window that expires has to end the wait too -- with the DIO1 line dead + // nothing else would ever release it. + uint32_t irqDoneMask() const override { + return RADIOLIB_SX126X_IRQ_TX_DONE | RADIOLIB_SX126X_IRQ_RX_DONE | RADIOLIB_SX126X_IRQ_TIMEOUT; + } +#endif float getCurrentRSSI() override { return ((CustomSX1262 *)_radio)->getRSSI(false); } diff --git a/src/helpers/radiolib/RadioLibWrappers.cpp b/src/helpers/radiolib/RadioLibWrappers.cpp index 49560bf9..cc15cd0c 100644 --- a/src/helpers/radiolib/RadioLibWrappers.cpp +++ b/src/helpers/radiolib/RadioLibWrappers.cpp @@ -89,7 +89,38 @@ void RadioLibWrapper::resetAGC() { _floor_sample_sum = 0; } +#ifdef LORA_POLL_IRQ +// CUSTOM (TeTeHacko): salvage path for a node whose DIO1 interrupt line is dead. +// +// setPacketReceivedAction() above is the ONLY thing that ever sets +// STATE_INT_READY, so a broken DIO1 line means RxDone/TxDone never reach the +// MCU: TX times out and retries while the frames actually radiate (a witness +// node hears them), and received packets are never picked up. That is the exact +// signature that killed bench-board -- see nrf52-radio-fault-diagnosis. +// +// The chip latches those events in its IRQ status register regardless of whether +// they are mapped out to a DIO pin, so reading it over SPI bypasses the whole +// line -- and therefore works no matter WHICH end of it is broken (dead nRF52 +// input, dead SX1262 output, or a cut trace). Rewiring DIO1 to a spare GPIO only +// helps in the first of those three cases, so this is the strictly stronger fix: +// if polling does not revive the node, no wire can either. +// +// Cost: one extra SPI read per Dispatcher pass (Dispatcher.cpp:72 -> loop()), +// alongside the isReceivingPacket() read that already happens in the same path, +// and completion is noticed on the next pass instead of instantly. +void RadioLibWrapper::pollIrq() { + if (state != STATE_RX && state != STATE_TX_WAIT) return; // nothing pending + uint32_t mask = irqDoneMask(); + if (mask == 0) return; // radio has no mask + if (_radio->getIrqFlags() & mask) setFlag(); +} +#endif + void RadioLibWrapper::loop() { +#ifdef LORA_POLL_IRQ + pollIrq(); +#endif + if (state == STATE_RX && _num_floor_samples < NUM_NOISE_FLOOR_SAMPLES) { if (!isReceivingPacket()) { int rssi = getCurrentRSSI(); @@ -184,6 +215,68 @@ void RadioLibWrapper::onSendFinished() { state = STATE_IDLE; } +#ifdef PIN_DIAG +// CUSTOM (TeTeHacko): make the radio itself assert DIO1, then report the chip's +// IRQ status register (read over SPI, independent of the pin) NEXT TO the actual +// level on the pin. That pair is what a multimeter would otherwise have to tell +// us, and it says which END of a dead DIO1 line is broken: +// +// irq has TX_DONE, pin HIGH -> the line is fine, look elsewhere +// irq has TX_DONE, pin LOW -> line dead. If a probe wire is fitted from the +// DIO1 net to PIN_DIAG_PROBE and THAT reads HIGH, +// the SX1262 still drives and it is the nRF52's +// input pad that died -- the one case a rewire +// to a spare GPIO actually fixes. +// no TX_DONE at all -> the chip never finished the transmit; the +// fault is not the IRQ line (SPI/BUSY/PA). +// +// A 1-byte transmit is used as the stimulus because TX_DONE is unconditional -- +// it needs no peer, no antenna match and no RX activity. It DOES put a frame on +// the air, so run it on the bench band. +int RadioLibWrapper::probeDio1(uint32_t* irq_out, int* dio1_level, int* probe_level) { + if (state == STATE_TX_WAIT) return -1; // mid-transmit, don't clobber it + + uint32_t mask = irqDoneMask(); + if (mask == 0) return -3; // radio has no chip mask -> can't tell + + uint8_t dummy = 0; + _radio->standby(); + _board->onBeforeTransmit(); + int err = _radio->startTransmit(&dummy, 1); + if (err != RADIOLIB_ERR_NONE) { + _board->onAfterTransmit(); + idle(); + return err; + } + + uint32_t irq = 0; + unsigned long deadline = millis() + 2000; + while (millis() < deadline) { + irq = _radio->getIrqFlags(); + if (irq & mask) break; + } + + // Levels have to be sampled while the IRQ is still asserted: DIO1 stays high + // until the status register is cleared, and finishTransmit() clears it. + // Runtime compare, not #if: P_LORA_DIO_1 is usually a variant constant like D1 + // (a static const, invisible to the preprocessor, which would silently read 0). + uint32_t dio1_pin = (uint32_t) P_LORA_DIO_1; + *dio1_level = (dio1_pin == RADIOLIB_NC) ? -1 : digitalRead(dio1_pin); +#ifdef PIN_DIAG_PROBE + pinMode(PIN_DIAG_PROBE, INPUT); + *probe_level = digitalRead(PIN_DIAG_PROBE); +#else + *probe_level = -1; +#endif + *irq_out = irq; + + _radio->finishTransmit(); + _board->onAfterTransmit(); + state = STATE_IDLE; // Dispatcher will startReceive() again + return (irq & mask) ? 0 : -2; +} +#endif + bool RadioLibWrapper::isChannelActive() { return _threshold == 0 ? false // interference check is disabled diff --git a/src/helpers/radiolib/RadioLibWrappers.h b/src/helpers/radiolib/RadioLibWrappers.h index db4fb0ad..f6eebd6a 100644 --- a/src/helpers/radiolib/RadioLibWrappers.h +++ b/src/helpers/radiolib/RadioLibWrappers.h @@ -17,6 +17,9 @@ protected: void idle(); void startRecv(); +#ifdef LORA_POLL_IRQ + void pollIrq(); // CUSTOM (TeTeHacko): DIO1-less completion detection, see .cpp +#endif float packetScoreInt(float snr, int sf, int packet_len); virtual bool isReceivingPacket() =0; virtual void doResetAGC(); @@ -77,6 +80,21 @@ public: virtual void setRxBoostedGainMode(bool) { } virtual bool getRxBoostedGainMode() const { return false; } + +#if defined(LORA_POLL_IRQ) || defined(PIN_DIAG) + // CUSTOM (TeTeHacko): chip-native IRQ status bits that mean "the operation the + // driver is waiting for has finished" -- i.e. exactly what the DIO1 interrupt + // would have told us. getIrqFlags() returns the RAW chip register (SX126x + // returns its own bitmask, not RadioLib's generic RADIOLIB_IRQ_* indices), so + // the mask has to come from the chip-specific wrapper. Returning 0 disables + // both the polling fallback and the DIO1 probe for radios that don't override. + virtual uint32_t irqDoneMask() const { return 0; } +#endif + +#ifdef PIN_DIAG + // CUSTOM (TeTeHacko): salvage diagnostics -- see `dio1` in simple_repeater. + int probeDio1(uint32_t* irq_out, int* dio1_level, int* probe_level); +#endif }; /**