RadioLib
Universal wireless communication library for Arduino
Loading...
Searching...
No Matches
LR_common.h
1#if !defined(RADIOLIB_LR_COMMON_H)
2#define RADIOLIB_LR_COMMON_H
3
4#include "../../Module.h"
5#include "../../protocols/PhysicalLayer/PhysicalLayer.h"
6
7#define RADIOLIB_LRXXXX_CMD_NOP (0x0000)
8#define RADIOLIB_LRXXXX_SPI_MAX_READ_WRITE_LEN (128) // intentionally limited to the length supported by LR2021
9
10// RADIOLIB_LR11X0_CMD_GET_STATUS
11// RADIOLIB_LR2021_CMD_GET_STATUS MSB LSB DESCRIPTION
12#define RADIOLIB_LRXXXX_STAT_1_CMD_FAIL (0x00UL << 1) // 3 1 command status: last command could not be executed
13#define RADIOLIB_LRXXXX_STAT_1_CMD_PERR (0x01UL << 1) // 3 1 processing error
14#define RADIOLIB_LRXXXX_STAT_1_CMD_OK (0x02UL << 1) // 3 1 successfully processed
15#define RADIOLIB_LRXXXX_STAT_1_CMD_DAT (0x03UL << 1) // 3 1 successfully processed, data is being transmitted
16
17// RADIOLIB_LR11X0_CMD_LR_FHSS_BUILD_FRAME
18// RADIOLIB_LR2021_CMD_LR_FHSS_BUILD_FRAME
19#define RADIOLIB_LRXXXX_LR_FHSS_CR_5_6 (0x00UL << 0) // 7 0 LR FHSS coding rate: 5/6
20#define RADIOLIB_LRXXXX_LR_FHSS_CR_2_3 (0x01UL << 0) // 7 0 2/3
21#define RADIOLIB_LRXXXX_LR_FHSS_CR_1_2 (0x02UL << 0) // 7 0 1/2
22#define RADIOLIB_LRXXXX_LR_FHSS_CR_1_3 (0x03UL << 0) // 7 0 1/3
23#define RADIOLIB_LRXXXX_LR_FHSS_MOD_TYPE_GMSK (0x00UL << 0) // 7 0 LR FHSS modulation: GMSK
24#define RADIOLIB_LRXXXX_LR_FHSS_GRID_STEP_FCC (0x00UL << 0) // 7 0 LR FHSS step size: 25.390625 kHz (FCC)
25#define RADIOLIB_LRXXXX_LR_FHSS_GRID_STEP_NON_FCC (0x01UL << 0) // 7 0 3.90625 kHz (non-FCC)
26#define RADIOLIB_LRXXXX_LR_FHSS_HOPPING_DISABLED (0x00UL << 0) // 7 0 LR FHSS hopping: disabled
27#define RADIOLIB_LRXXXX_LR_FHSS_HOPPING_ENABLED (0x01UL << 0) // 7 0 enabled
28#define RADIOLIB_LRXXXX_LR_FHSS_HOPPING_TEST_NO_HOP (0x02UL << 0) // 7 0 test mode (packet encoded, no hopping)
29#define RADIOLIB_LRXXXX_LR_FHSS_HOPPING_TEST_PA_RAMP (0x03UL << 0) // 7 0 test mode (PA ramp up, no hopping)
30#define RADIOLIB_LRXXXX_LR_FHSS_BW_39_06 (0x00UL << 0) // 7 0 LR FHSS bandwidth: 39.06 kHz
31#define RADIOLIB_LRXXXX_LR_FHSS_BW_85_94 (0x01UL << 0) // 7 0 85.94 kHz
32#define RADIOLIB_LRXXXX_LR_FHSS_BW_136_72 (0x02UL << 0) // 7 0 136.72 kHz
33#define RADIOLIB_LRXXXX_LR_FHSS_BW_183_59 (0x03UL << 0) // 7 0 183.59 kHz
34#define RADIOLIB_LRXXXX_LR_FHSS_BW_335_94 (0x04UL << 0) // 7 0 335.94 kHz
35#define RADIOLIB_LRXXXX_LR_FHSS_BW_386_72 (0x05UL << 0) // 7 0 386.72 kHz
36#define RADIOLIB_LRXXXX_LR_FHSS_BW_722_66 (0x06UL << 0) // 7 0 722.66 kHz
37#define RADIOLIB_LRXXXX_LR_FHSS_BW_773_44 (0x07UL << 0) // 7 0 773.44 kHz
38#define RADIOLIB_LRXXXX_LR_FHSS_BW_1523_4 (0x08UL << 0) // 7 0 1523.4 kHz
39#define RADIOLIB_LRXXXX_LR_FHSS_BW_1574_2 (0x09UL << 0) // 7 0 1574.2 kHz
40#define RADIOLIB_LRXXXX_LR_FHSS_HEADER_BITS (114) // 7 0 LR FHSS packet bit widths: header
41#define RADIOLIB_LRXXXX_LR_FHSS_FRAG_BITS (48) // 7 0 payload fragment
42#define RADIOLIB_LRXXXX_LR_FHSS_BLOCK_PREAMBLE_BITS (2) // 7 0 block preamble
43#define RADIOLIB_LRXXXX_LR_FHSS_BLOCK_BITS (RADIOLIB_LRXXXX_LR_FHSS_FRAG_BITS + RADIOLIB_LRXXXX_LR_FHSS_BLOCK_PREAMBLE_BITS)
44
45// RADIOLIB_LR11X0_CMD_SET_TCXO_MODE
46// RADIOLIB_LR2021_CMD_SET_TCXO_MODE
47#define RADIOLIB_LRXXXX_TCXO_VOLTAGE_1_6 (0x00UL << 0) // 2 0 TCXO supply voltage: 1.6V
48#define RADIOLIB_LRXXXX_TCXO_VOLTAGE_1_7 (0x01UL << 0) // 2 0 1.7V
49#define RADIOLIB_LRXXXX_TCXO_VOLTAGE_1_8 (0x02UL << 0) // 2 0 1.8V
50#define RADIOLIB_LRXXXX_TCXO_VOLTAGE_2_2 (0x03UL << 0) // 2 0 2.2V
51#define RADIOLIB_LRXXXX_TCXO_VOLTAGE_2_4 (0x04UL << 0) // 2 0 2.4V
52#define RADIOLIB_LRXXXX_TCXO_VOLTAGE_2_7 (0x05UL << 0) // 2 0 2.7V
53#define RADIOLIB_LRXXXX_TCXO_VOLTAGE_3_0 (0x06UL << 0) // 2 0 3.0V
54#define RADIOLIB_LRXXXX_TCXO_VOLTAGE_3_3 (0x07UL << 0) // 2 0 3.3V
55
56// RADIOLIB_LR11X0_CMD_SET_TX_PARAMS
57// RADIOLIB_LR2021_CMD_SET_TX_PARAMS
58#define RADIOLIB_LRXXXX_PA_RAMP_2U (0x00UL << 0) // 7 0 PA ramp time: 2 us (LR2021 only)
59#define RADIOLIB_LRXXXX_PA_RAMP_4U (0x01UL << 0) // 7 0 4 us (LR2021 only)
60#define RADIOLIB_LRXXXX_PA_RAMP_8U (0x02UL << 0) // 7 0 8 us (LR2021 only)
61#define RADIOLIB_LRXXXX_PA_RAMP_16U (0x03UL << 0) // 7 0 16 us
62#define RADIOLIB_LRXXXX_PA_RAMP_32U (0x04UL << 0) // 7 0 32 us
63#define RADIOLIB_LRXXXX_PA_RAMP_48U (0x05UL << 0) // 7 0 48 us
64#define RADIOLIB_LRXXXX_PA_RAMP_64U (0x06UL << 0) // 7 0 64 us
65#define RADIOLIB_LRXXXX_PA_RAMP_80U (0x07UL << 0) // 7 0 80 us
66#define RADIOLIB_LRXXXX_PA_RAMP_96U (0x08UL << 0) // 7 0 96 us
67#define RADIOLIB_LRXXXX_PA_RAMP_112U (0x09UL << 0) // 7 0 112 us
68#define RADIOLIB_LRXXXX_PA_RAMP_128U (0x0AUL << 0) // 7 0 128 us
69#define RADIOLIB_LRXXXX_PA_RAMP_144U (0x0BUL << 0) // 7 0 144 us
70#define RADIOLIB_LRXXXX_PA_RAMP_160U (0x0CUL << 0) // 7 0 160 us
71#define RADIOLIB_LRXXXX_PA_RAMP_176U (0x0DUL << 0) // 7 0 176 us
72#define RADIOLIB_LRXXXX_PA_RAMP_192U (0x0EUL << 0) // 7 0 192 us
73#define RADIOLIB_LRXXXX_PA_RAMP_208U (0x0FUL << 0) // 7 0 208 us
74#define RADIOLIB_LRXXXX_PA_RAMP_240U (0x10UL << 0) // 7 0 240 us
75#define RADIOLIB_LRXXXX_PA_RAMP_272U (0x11UL << 0) // 7 0 272 us
76#define RADIOLIB_LRXXXX_PA_RAMP_304U (0x12UL << 0) // 7 0 304 us
77
78// RADIOLIB_LR11X0_CMD_SET_DIO_AS_RF_SWITCH
79// RADIOLIB_LR2021_CMD_SET_DIO_AS_RF_SWITCH
80#define RADIOLIB_LRXXXX_DIOx(X) ((X) | RFSWITCH_PIN_FLAG)
81#define RADIOLIB_LRXXXX_DIOx_VAL(X) ((X) & ~RFSWITCH_PIN_FLAG)
82
83// common configuration values
84#define RADIOLIB_LRXXXX_LR_FHSS_BIT_RATE (488.28215f) // 31 0 LR FHSS bit rate: 488.28215 bps
85#define RADIOLIB_LRXXXX_LR_FHSS_BIT_RATE_RAW (0x8001E848UL) // 31 0 488.28215 bps in raw
86#define RADIOLIB_LRXXXX_LORA_HEADER_EXPLICIT (0x00UL << 0) // 7 0 LoRa header mode: explicit
87#define RADIOLIB_LRXXXX_LORA_HEADER_IMPLICIT (0x01UL << 0) // 7 0 implicit
88#define RADIOLIB_LRXXXX_LORA_CRC_ENABLED (0x01UL << 0) // 7 0 CRC: enabled
89#define RADIOLIB_LRXXXX_LORA_CRC_DISABLED (0x00UL << 0) // 7 0 disabled
90
91class LRxxxx: public PhysicalLayer {
92 public:
93 explicit LRxxxx(Module* mod);
94
98 bool XTAL;
99
104 int16_t reset();
105
110 void setIrqAction(void (*func)(void));
111
115 void clearIrqAction();
116
121 void setPacketReceivedAction(void (*func)(void)) override;
122
126 void clearPacketReceivedAction() override;
127
132 void setPacketSentAction(void (*func)(void)) override;
133
137 void clearPacketSentAction() override;
138
143 uint32_t getIrqStatus();
144
153 RadioLibTime_t calculateTimeOnAir(ModemType_t modem, DataRate_t dr, PacketConfig_t pc, size_t len) override;
154
161
162 protected:
163 Module* mod;
164
165 float freqMHz = 0;
166 uint32_t rxTimeout = 0;
167
168 // cached LoRa parameters
169 uint8_t bandwidth = 0, spreadingFactor = 0, codingRate = 0, ldrOptimize = 0, crcTypeLoRa = 0, headerType = 0;
170 uint16_t preambleLengthLoRa = 0;
171 float bandwidthKhz = 0;
172 bool ldroAuto = true;
173 size_t implicitLen = 0;
174 bool invertIQEnabled = false;
175
176 // cached GFSK parameters
177 uint32_t bitRate = 0, frequencyDev = 0;
178 uint8_t preambleDetLength = 0, rxBandwidth = 0, pulseShape = 0, crcTypeGFSK = 0, crcLenGFSK = 0, syncWordLength = 0, addrComp = 0, whitening = 0, packetType = 0, node = 0;
179 uint16_t preambleLengthGFSK = 0;
180
181 // cached LR-FHSS parameters
182 uint8_t lrFhssCr = 0, lrFhssBw = 0, lrFhssHdrCount = 0, lrFhssGrid = 0;
183 uint16_t lrFhssHopSeq = 0;
184
185 // a lot of SPI commands have the same structure and arguments on both LR11xx as well as LR2021
186 // the only difference is the 16-bit command code - however, having everything in this base class
187 // will actually increase the binary size, because of the extra method calls that are needed
188 // for that reason, only the methods that are 100% the same are kept here
189 int16_t getStatus(uint8_t* stat1, uint8_t* stat2, uint32_t* irq);
190 int16_t lrFhssBuildFrame(uint16_t cmd, uint8_t hdrCount, uint8_t cr, uint8_t grid, uint8_t hop, uint8_t bw, uint16_t hopSeq, int8_t devOffset, const uint8_t* payload, size_t len);
191 uint8_t roundRampTime(uint32_t rampTimeUs);
192 int16_t findRxBw(float rxBw, const uint8_t* lut, size_t lutSize, float rxBwMax, uint8_t* val);
193 RadioLibTime_t getTimeOnAir(size_t len, ModemType_t modem);
194
195 // several commands just send unsigned 32-bit number
196 int16_t setU32(uint16_t cmd, uint32_t u32);
197
198 int16_t writeCommon(uint16_t cmd, uint32_t addrOffset, const uint32_t* data, size_t len, bool nonvolatile);
199 int16_t SPIcommand(uint16_t cmd, bool write, uint8_t* data, size_t len, const uint8_t* out = NULL, size_t outLen = 0);
200
201 static int16_t SPIparseStatus(uint8_t in);
202 static int16_t SPIcheckStatus(Module* mod);
203
204 private:
205};
206
207#endif
Definition LR_common.h:91
uint32_t getIrqStatus()
Reads the current IRQ status.
Definition LR_common.cpp:39
void setPacketReceivedAction(void(*func)(void)) override
Sets interrupt service routine to call when a packet is received.
Definition LR_common.cpp:23
RadioLibTime_t calculateRxTimeout(RadioLibTime_t timeoutUs) override
Calculate the timeout value for this specific module / series (in number of symbols or units of time)
Definition LR_common.cpp:171
RadioLibTime_t calculateTimeOnAir(ModemType_t modem, DataRate_t dr, PacketConfig_t pc, size_t len) override
Calculate the expected time-on-air for a given modem, data rate, packet configuration and payload siz...
Definition LR_common.cpp:99
void setPacketSentAction(void(*func)(void)) override
Sets interrupt service routine to call when a packet is sent.
Definition LR_common.cpp:31
void setIrqAction(void(*func)(void))
Sets interrupt service routine to call when IRQ1 activates.
Definition LR_common.cpp:15
void clearIrqAction()
Clears interrupt service routine to call when IRQ1 activates.
Definition LR_common.cpp:19
void clearPacketSentAction() override
Clears interrupt service routine to call when a packet is sent.
Definition LR_common.cpp:35
void clearPacketReceivedAction() override
Clears interrupt service routine to call when a packet is received.
Definition LR_common.cpp:27
int16_t reset()
Reset method. Will reset the chip to the default state using RST pin.
Definition LR_common.cpp:178
bool XTAL
Whether the module has an XTAL (true) or TCXO (false). Defaults to false.
Definition LR_common.h:98
Implements all common low-level methods to control the wireless module. Every module class contains o...
Definition Module.h:73
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition PhysicalLayer.h:257
unsigned long RadioLibTime_t
Type used for durations in RadioLib.
Definition TypeDef.h:658
Common data rate structure.
Definition PhysicalLayer.h:74
Common packet configuration structure.
Definition PhysicalLayer.h:119