STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sx1262_setup.h
Go to the documentation of this file.
1#ifndef SX1262_SETUP_H
2#define SX1262_SETUP_H
3
4// SX1262 start-up and transmit, layered on Ryan's SpiBus / Sx1262 (receive).
5// Datasheet: Semtech DS_SX1261-2 Rev 1.2 (June 2019). Module: Seeed
6// Wio-SX1262 V1.1 (DC-DC, TCXO on DIO3, RF switch on DIO2 + RF_SW).
7//
8// Start-up order follows 14.2 / 14.4 (p.99-100): standby, regulator, TCXO,
9// calibrate, then packet type first, modulation, packet params. After it the
10// chip listens in continuous RX (Ryan's requirement, README): send() returns
11// it there after every packet.
12
13#include <stdint.h>
14
15#include "radio.h"
16#include "sx1262.h"
17
18namespace radio_link {
19
20// What the board does around the chip: the module's RF_SW pin and waiting.
22public:
23 // RF_SW (PB1): true while receiving, false while transmitting or idle.
24 // Meshtastic's Seeed XIAO + Wio-SX1262 variants drive this pin as RXEN;
25 // DIO2 drives the TX side (Wio datasheet p.5 note 1). Verify on bench.
26 virtual void set_rx_switch(bool receive) = 0;
27 virtual void delay_ms(uint32_t ms) = 0;
28
29protected:
31 }
32};
33
34// Link settings. Every value except the hardware ones (TCXO, regulator, PA
35// table) is a proposal for the lead (README "Decisions"); the ground station
36// must use the same frequency, SF, BW, CR, sync word and CRC.
37struct LoraConfig {
38 uint32_t freq_hz; // 915 MHz: in the Wio's 862-930 MHz band
39 uint8_t sf; // 7..12 (p.87)
40 uint8_t bw; // LORA_BW code (Table 13-48, p.87): 0x04 = 125 kHz
41 uint8_t cr; // 1..4 = 4/5..4/8 (Table 13-49)
42 uint16_t preamble; // symbols
43 int8_t power_dbm; // -9..+22 on the high-power PA (p.84)
44 uint16_t sync_word; // 0x1424 private network (reg 0x0740, p.65)
45};
46
47constexpr LoraConfig kDefaultLora = {915000000u, 9, 0x04, 1, 8, 22, 0x1424};
48
49// Steps of configure(); its return value names the first one that failed.
71
72enum class RxMode : uint8_t {
73 Continuous, // SetRx 0xFFFFFF: chip stays in RX until commanded otherwise
74 DutyCycle, // SetRxDutyCycle: chip sniffs then sleeps, loops autonomously
75};
76
78public:
79 Sx1262Setup(SpiBus& bus, RadioControl& control, const LoraConfig& cfg)
80 : bus_(bus), control_(control), cfg_(cfg) {
81 }
82
83 // Full start-up from power-on (chip out of reset, BUSY low). Leaves the
84 // chip in continuous RX with RF_SW set for receive.
86
87 // Ryan's radio_send() contract: Ok only after TxDone; ErrTimeout if
88 // neither TxDone nor the chip's own Timeout came in time; ErrNotFound
89 // if the chip stops answering. Always returns the chip to whichever RX
90 // mode was last set (continuous or duty cycle).
91 RadioStatus send(const uint8_t* bytes, uint8_t len);
92
93 // Switch to sniff mode (DS 13.1.7, p.71): listen rx_ms, sleep sleep_ms,
94 // repeat. Must be called from STDBY_RC or any RX mode. Returns false if
95 // the chip doesn't respond. send() returns here after every TX.
96 bool start_rx_duty_cycle(uint32_t rx_ms, uint32_t sleep_ms);
97
98 // Switch back to continuous RX (SetRx 0xFFFFFF). Stops the duty cycle.
100
101 // Re-arm after every RxDone in sniff mode: the chip returns to STDBY_RC
102 // after a packet (p.71) and must be restarted or it goes deaf. No-op in
103 // continuous mode (chip stays in RX on its own).
104 bool rearm_rx();
105
106 // OpError bits from the last configure() (Table 13-85, p.98).
107 uint16_t device_errors() const {
108 return device_errors_;
109 }
110 uint32_t tx_ok() const {
111 return tx_ok_;
112 }
113 uint32_t tx_timeouts() const {
114 return tx_timeouts_;
115 }
116
117private:
118 bool cmd(const uint8_t* tx, uint16_t n);
119 bool read_register(uint16_t addr, uint8_t& value);
120 bool write_register(uint16_t addr, const uint8_t* data, uint8_t n);
121 bool set_packet_params(uint8_t payload_len);
122 bool irq_status(uint16_t& irq);
123 bool back_to_rx();
124 bool send_duty_cycle();
125
130 uint32_t duty_rx_steps_ = 0; // rxPeriod in 15.625 µs steps
131 uint32_t duty_sleep_steps_ = 0; // sleepPeriod in 15.625 µs steps
132 uint16_t device_errors_ = 0;
133 uint32_t tx_ok_ = 0;
134 uint32_t tx_timeouts_ = 0;
135 uint8_t tx_[2 + 255]; // WriteBuffer: opcode + offset + payload
136 uint8_t rx_[2 + 255];
137};
138
139// LoRa time on air in microseconds, SF7..12 (6.1.4, p.41), explicit
140// header, CRC on, as configure() sets it.
141uint32_t lora_time_on_air_us(const LoraConfig& cfg, uint8_t payload_len);
142
143// SetRfFrequency word: freq x 2^25 / 32 MHz (p.83).
144uint32_t sx1262_frequency_word(uint32_t freq_hz);
145
146} // namespace radio_link
147
148#endif // SX1262_SETUP_H
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474