Public Access
M3 step 3: the Radio Service, receive only
One task owns the SX1262 and does all its SPI behind the card's bus lock; the main loop posts requests and DIO1 only wakes the task. It listens while a client asks (App, Capture, Console) and sleeps otherwise, with a ring of the last 32 packets (9.8 KB, freed when idle). No transmit path. The Cap's antenna switch (expander P0) is set on the main loop, which owns the I2C bus. `lora probe` now runs on the radio task and checks the DIO1 interrupt with a receive timeout; `lora status`, `lora rx on|off`, `lora preset`, and `lora custom` for other LoRa settings. Measured: DIO1 works (timeout after 105 ms); four 1.7 MB uploads and Gemini pages to the card while listening, no radio or card errors; task stack peak 2.0 KB. Twenty minutes on LongFast and LoRaWAN: no packets, and a noise floor of -83 to -94 dBm at the desk. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
#pragma once
|
||||
|
||||
#include <Print.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "loratap.h"
|
||||
#include "meshtastic_presets.h"
|
||||
#include "service.h"
|
||||
|
||||
class SX1262;
|
||||
class Module;
|
||||
class ArduinoHal;
|
||||
|
||||
namespace roro {
|
||||
|
||||
// One received packet, as the Radio Service keeps it.
|
||||
struct RadioPacket {
|
||||
uint32_t seq = 0; // 1, 2, 3… since boot
|
||||
uint32_t ms = 0; // millis() when it was read (the Clock is read on the main loop, not here)
|
||||
lora::RxInfo rx;
|
||||
float frequencyError = 0; // Hz
|
||||
bool crcOk = true;
|
||||
uint8_t len = 0;
|
||||
uint8_t data[255];
|
||||
};
|
||||
|
||||
// The LoRa radio on the Cap (CONTEXT.md: Radio Service; docs/milestones/M3.md). One task owns the
|
||||
// SX1262 and does all its SPI traffic, behind the bus lock the SD card uses (Q93); the main loop
|
||||
// only posts requests, and DIO1's interrupt only wakes the task. **Receive only (Q94): there is
|
||||
// no way to transmit.** The radio listens while someone asks it to (Q100) and sleeps otherwise.
|
||||
class RadioService : public Service {
|
||||
public:
|
||||
enum Client : uint8_t { App = 1, Capture = 2, Console = 4 };
|
||||
static constexpr size_t kRing = 32; // Q103
|
||||
static constexpr uint32_t kNoiseEveryMs = 500; // instantaneous RSSI while listening
|
||||
|
||||
struct Config {
|
||||
uint32_t frequencyHz;
|
||||
float bandwidthKHz;
|
||||
uint8_t spreadingFactor, codingRate, syncWord;
|
||||
uint16_t preamble;
|
||||
const char* name; // the preset's, or "Custom"
|
||||
};
|
||||
struct Counters {
|
||||
uint32_t packets, crcErrors, radioErrors, restarts;
|
||||
// Seen in the IRQ status at each noise sample (every 500 ms). Preamble detections include
|
||||
// false alarms in noise (M3: more on an empty frequency than on LongFast); headers don't.
|
||||
uint32_t preambles, headers, headerErrors;
|
||||
};
|
||||
|
||||
RadioService();
|
||||
const char* name() const override { return "radio"; }
|
||||
uint32_t tickIntervalMs() const override { return 100; }
|
||||
void start() override;
|
||||
void tick(uint32_t nowMs) override;
|
||||
|
||||
bool present() const { return present_; }
|
||||
bool listening() const { return listening_; }
|
||||
void listen(Client client, bool on);
|
||||
Config config() const;
|
||||
void setPreset(const meshtastic::Preset& preset);
|
||||
void setConfig(const Config& config); // custom settings (Q95), named "Custom"
|
||||
|
||||
uint32_t received() const { return seq_; } // seq of the newest packet
|
||||
bool packet(uint32_t seq, RadioPacket& out) const; // false once it has left the ring
|
||||
float noiseFloor() const { return noise_; }
|
||||
Counters counters() const {
|
||||
return {packets_, crcErrors_, radioErrors_, restarts_, preambles_, headers_, headerErrors_};
|
||||
}
|
||||
|
||||
// Debug aids (Q102).
|
||||
void printStatus(Print& out) const;
|
||||
void setEcho(bool echo);
|
||||
void probe(Print& out); // `lora probe`: runs on the radio task
|
||||
|
||||
private:
|
||||
enum Notify : uint32_t { kIrq = 1, kRequest = 2 };
|
||||
static void taskEntry(void* self);
|
||||
static void onDio1();
|
||||
void run();
|
||||
bool beginRadio(Print* report);
|
||||
void applyConfig();
|
||||
void startListening();
|
||||
void stopListening();
|
||||
int16_t receive();
|
||||
void readPacket();
|
||||
void sampleNoise();
|
||||
void runProbe();
|
||||
void wake(Notify why);
|
||||
|
||||
std::unique_ptr<ArduinoHal> hal_;
|
||||
std::unique_ptr<Module> module_;
|
||||
std::unique_ptr<SX1262> radio_;
|
||||
TaskHandle_t task_ = nullptr;
|
||||
mutable SemaphoreHandle_t lock_; // the ring and the config
|
||||
std::unique_ptr<RadioPacket[]> ring_;
|
||||
Config config_;
|
||||
bool present_ = false, expander_ = false;
|
||||
float tcxo_ = 1.8f;
|
||||
std::atomic<uint8_t> clients_{0};
|
||||
std::atomic<bool> listening_{false}, configChanged_{false}, probeWanted_{false}, echo_{false};
|
||||
std::atomic<uint32_t> seq_{0}, packets_{0}, crcErrors_{0}, radioErrors_{0}, restarts_{0};
|
||||
std::atomic<uint32_t> preambles_{0}, headers_{0}, headerErrors_{0};
|
||||
std::atomic<float> noise_{0};
|
||||
uint32_t lastNoiseMs_ = 0, echoed_ = 0;
|
||||
Print* probeOut_ = nullptr;
|
||||
static RadioService* instance_;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
Reference in New Issue
Block a user