Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion variants/host/SimHal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----
Expand All @@ -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;
Expand All @@ -110,6 +118,7 @@ class SimHal : public RadioLibHal {
VirtualSX1262 chip_;
void (*isr_)(void) = nullptr;
bool inIsr_ = false;
bool dio1High_ = false;
uint64_t localUs_ = 0;
};

Expand Down
51 changes: 51 additions & 0 deletions variants/host/dio1_edge_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>

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<uint8_t> 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;
}
Loading