STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sx1262.h
Go to the documentation of this file.
1#ifndef SX1262_H
2#define SX1262_H
3
4#include <stdint.h>
5
6#include "radio.h"
7
8namespace radio_link {
9
10// How the driver talks to the radio chip. The rocket gets a real version
11// (SPI plus the BUSY pin). Tests get a pretend chip.
12class SpiBus {
13public:
14 // Wait until the chip's BUSY pin is low. Returns false if it never is.
15 virtual bool wait_busy() = 0;
16
17 // One SPI exchange: pull NSS low, send `len` bytes from `tx` while
18 // receiving `len` bytes into `rx`, then raise NSS. Both arrays must hold
19 // at least `len` bytes.
20 virtual void transfer(const uint8_t* tx, uint8_t* rx, uint16_t len) = 0;
21
22protected:
24 }
25};
26
27// SX1262 command numbers (datasheet Rev 1.2, section 11, pages 62-64).
28constexpr uint8_t kOpClearIrqStatus = 0x02;
29constexpr uint8_t kOpGetIrqStatus = 0x12;
30constexpr uint8_t kOpGetRxBufferStatus = 0x13;
31constexpr uint8_t kOpGetPacketStatus = 0x14;
32constexpr uint8_t kOpReadBuffer = 0x1E;
33constexpr uint8_t kOpSetRx = 0x82;
34
35// Event flags in the chip's IRQ register (datasheet Rev 1.2, table 13-29,
36// page 80).
37constexpr uint16_t kIrqRxDone = 1u << 1; // a packet was received
38constexpr uint16_t kIrqHeaderErr = 1u << 5; // LoRa header failed its check
39constexpr uint16_t kIrqCrcErr = 1u << 6; // the radio's own CRC was wrong
40
41// True when a reply's status byte is one a working chip sends (Table 13-76,
42// p.95); 0x00/0xFF (no chip, stuck MISO) and error statuses are false.
43bool sx1262_status_ok(uint8_t status);
44
45// Signal quality of the last LoRa packet, from GetPacketStatus (opcode 0x14,
46// datasheet Rev 1.2, Tables 13-79 and 13-80, page 96). Kept in the chip's own
47// units so nothing is rounded: RSSI in half-dB steps, SNR in quarter-dB steps.
49 int16_t rssi_x2; // average RSSI over the packet, dBm x 2
50 int8_t snr_x4; // SNR, dB x 4 (negative below the noise floor)
51 int16_t signal_rssi_x2; // RSSI of the LoRa signal after despreading
52};
53
54// Receives packets from an SX1262 over a SpiBus.
55//
56// The chip must be listening in continuous RX for receive() to see packets:
57// in single RX it returns to standby after the first packet and nothing here
58// would re-arm it. Call start_rx_continuous() once start-up is done and again
59// after every transmission (a send leaves the chip in standby).
60class Sx1262 {
61public:
62 explicit Sx1262(SpiBus& bus) : bus_(bus) {
63 }
64
65 // SetRx (opcode 0x82, datasheet Rev 1.2 page 62) with timeout 0xFFFFFF:
66 // continuous RX, the chip stays listening until told otherwise (page 70).
67 // Returns false if the chip never left BUSY.
69
70 // Same promise as radio_receive() in radio.h. ErrNotFound means the chip
71 // did not answer: BUSY never went low, or a reply's status byte is not
72 // one a working chip can send (a missing chip reads 0x00 or 0xFF).
73 RadioStatus receive(uint8_t* buf, uint8_t max_len, uint8_t& len);
74
75 // Packets the radio received but rejected (LoRa header or CRC error).
76 // receive() reports them as NoData; this count makes a bad antenna or
77 // too much range visible in telemetry. Needs LoRa CRC on at start-up.
78 // Signal quality of the packet receive() just returned (call it right
79 // after an Ok, before the next packet arrives). False if the chip did
80 // not answer; `out` is then left alone.
81 bool packet_status(PacketStatus& out);
82 uint32_t crc_errors() const {
83 return crc_errors_;
84 }
85
86private:
87 // Wait for BUSY to go low, then do one SPI exchange.
88 bool command(const uint8_t* tx, uint8_t* rx, uint16_t n);
89
90 // Reset the flags in `mask` in the chip's IRQ register.
91 bool clear_irq(uint16_t mask);
92
93 // Biggest SPI exchange: ReadBuffer = opcode + offset + NOP + up to 255
94 // packet bytes. Kept here, not on the caller's stack (~0.5 KB).
95 static constexpr unsigned kMaxExchange = 3 + 255;
96
98 uint32_t crc_errors_ = 0;
101};
102
103} // namespace radio_link
104
105#endif // SX1262_H
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474