From 747b1e1208448305461454327ea2190654210f7a Mon Sep 17 00:00:00 2001 From: R0ck Date: Sun, 1 Nov 2026 01:18:29 +0000 Subject: [PATCH] SimHal: interrupt on DIO1's rising edge, as RadioLib attaches it. --- variants/host/SimHal.h | 11 ++++++- variants/host/dio1_edge_test.cpp | 51 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 variants/host/dio1_edge_test.cpp diff --git a/variants/host/SimHal.h b/variants/host/SimHal.h index b5952f0..027efdc 100644 --- a/variants/host/SimHal.h +++ b/variants/host/SimHal.h @@ -70,6 +70,8 @@ class SimHal : public RadioLibHal { // timing arithmetic sees a plausible driver rather than an instant one. advanceUs(30 + len * 2); chip_.spiTransfer(out, len, in); + // A command that clears the flags drops the line, so the next one is an edge. + if (!chip_.irqAsserted()) dio1High_ = false; } // ---- the simulator's side ---- @@ -88,12 +90,18 @@ class SimHal : public RadioLibHal { // Deliver a pending interrupt to RadioLib, if the chip has raised one. Called // once per tick rather than from inside the chip, so an ISR never runs in the // middle of an SPI transaction the way it cannot on real hardware either. + // + // On the rising edge only, as RadioLib attaches DIO1: a line left high by a + // firmware that has not yet read the chip interrupts once, not every tick, and + // a second packet landing while it is still high raises nothing. void servicePendingIrq() { - if (isr_ && chip_.irqAsserted() && !inIsr_) { + const bool high = chip_.irqAsserted(); + if (isr_ && high && !dio1High_ && !inIsr_) { inIsr_ = true; isr_(); inIsr_ = false; } + dio1High_ = high; } static constexpr uint32_t kPinNss = 1; @@ -110,6 +118,7 @@ class SimHal : public RadioLibHal { VirtualSX1262 chip_; void (*isr_)(void) = nullptr; bool inIsr_ = false; + bool dio1High_ = false; uint64_t localUs_ = 0; }; diff --git a/variants/host/dio1_edge_test.cpp b/variants/host/dio1_edge_test.cpp new file mode 100644 index 0000000..8cdf9ef --- /dev/null +++ b/variants/host/dio1_edge_test.cpp @@ -0,0 +1,51 @@ +// DIO1 interrupts on its rising edge, as RadioLib attaches it. +// +// c++ -std=c++17 -I variants/host -I vendor/virtual-sx1262/src -I vendor/virtual-sx1262/include \ +// -I vendor/RadioLib/src \ +// variants/host/dio1_edge_test.cpp vendor/virtual-sx1262/src/*.cpp vendor/RadioLib/src/Hal.cpp \ +// -o /tmp/dio1_edge_test && /tmp/dio1_edge_test + +#include "SimHal.h" + +#include + +SimHal sim_hal; +static int interrupts = 0; +static void isr() { interrupts++; } +static int failures = 0; + +static void check(bool ok, const char* what) { + std::printf(" %-4s %s\n", ok ? "ok" : "FAIL", what); + if (!ok) failures++; +} + +static void spi(std::initializer_list cmd) { + uint8_t out[16] = {0}, in[16] = {0}; + size_t n = 0; + for (uint8_t b : cmd) out[n++] = b; + sim_hal.spiTransfer(out, n, in); +} + +int main() { + sim_hal.attachInterrupt(SimHal::kPinDio1, isr, 0); + spi({0x08, 0xFF, 0xFF, 0x00, 0x02, 0, 0, 0, 0}); // everything recorded, RxDone on DIO1 + spi({0x82, 0xFF, 0xFF, 0xFF}); // continuous receive + const uint8_t frame[] = {0x15, 0x00, 0x11, 0x22}; + uint32_t ms = 1; + sim_hal.chip().deliverFrame(frame, sizeof(frame)); + for (int i = 0; i < 5; i++) { + sim_hal.beginTick(ms++); + sim_hal.servicePendingIrq(); + } + check(interrupts == 1, "a line left high interrupts once"); + sim_hal.chip().deliverFrame(frame, sizeof(frame)); + sim_hal.beginTick(ms++); + sim_hal.servicePendingIrq(); + check(interrupts == 1, "a second packet while it is still high raises nothing"); + spi({0x02, 0xFF, 0xFF}); // ClearIrqStatus + sim_hal.chip().deliverFrame(frame, sizeof(frame)); + sim_hal.beginTick(ms++); + sim_hal.servicePendingIrq(); + check(interrupts == 2, "once cleared, the next packet interrupts again"); + return failures ? 1 : 0; +}