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
99 int16_t reset();
100
105 void setPacketReceivedAction(void (*func)(void)) override;
106
110 void clearPacketReceivedAction() override;
111
116 void setPacketSentAction(void (*func)(void)) override;
117
121 void clearPacketSentAction() override;
122
127 uint32_t getIrqStatus();
128
137 RadioLibTime_t calculateTimeOnAir(ModemType_t modem, DataRate_t dr, PacketConfig_t pc, size_t len) override;
138
145
146 protected:
147 Module* mod;
148
149 float freqMHz = 0;
150 uint32_t rxTimeout = 0;
151 uint32_t tcxoDelay = 0;
152
153 // cached LoRa parameters
154 uint8_t bandwidth = 0, spreadingFactor = 0, codingRate = 0, ldrOptimize = 0, crcTypeLoRa = 0, headerType = 0;
155 uint16_t preambleLengthLoRa = 0;
156 float bandwidthKhz = 0;
157 bool ldroAuto = true;
158 size_t implicitLen = 0;
159 bool invertIQEnabled = false;
160
161 // cached GFSK parameters
162 uint32_t bitRate = 0, frequencyDev = 0;
163 uint8_t preambleDetLength = 0, rxBandwidth = 0, pulseShape = 0, crcTypeGFSK = 0, crcLenGFSK = 0, syncWordLength = 0, addrComp = 0, whitening = 0, packetType = 0, node = 0;
164 uint16_t preambleLengthGFSK = 0;
165
166 // cached LR-FHSS parameters
167 uint8_t lrFhssCr = 0, lrFhssBw = 0, lrFhssHdrCount = 0, lrFhssGrid = 0;
168 uint16_t lrFhssHopSeq = 0;
169
170 // a lot of SPI commands have the same structure and arguments on both LR11xx as well as LR2021
171 // the only difference is the 16-bit command code - however, having everything in this base class
172 // will actually increase the binary size, because of the extra method calls that are needed
173 // for that reason, only the methods that are 100% the same are kept here
174 int16_t getStatus(uint8_t* stat1, uint8_t* stat2, uint32_t* irq);
175 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);
176 uint8_t roundRampTime(uint32_t rampTimeUs);
177 int16_t findRxBw(float rxBw, const uint8_t* lut, size_t lutSize, float rxBwMax, uint8_t* val);
178 RadioLibTime_t getToA(size_t len, ModemType_t modem);
179
180 // several commands just send unsigned 32-bit number
181 int16_t setU32(uint16_t cmd, uint32_t u32);
182
183 int16_t writeCommon(uint16_t cmd, uint32_t addrOffset, const uint32_t* data, size_t len, bool nonvolatile);
184 int16_t SPIcommand(uint16_t cmd, bool write, uint8_t* data, size_t len, const uint8_t* out = NULL, size_t outLen = 0);
185
186 static int16_t SPIparseStatus(uint8_t in);
187 static int16_t SPIcheckStatus(Module* mod);
188
189 private:
190};
191
192#endif
Definition LR_common.h:91
uint32_t getIrqStatus()
Reads the current IRQ status.
Definition LR_common.cpp:30
void setPacketReceivedAction(void(*func)(void)) override
Sets interrupt service routine to call when a packet is received.
Definition LR_common.cpp:14
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:162
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:90
void setPacketSentAction(void(*func)(void)) override
Sets interrupt service routine to call when a packet is sent.
Definition LR_common.cpp:22
void clearPacketSentAction() override
Clears interrupt service routine to call when a packet is sent.
Definition LR_common.cpp:26
void clearPacketReceivedAction() override
Clears interrupt service routine to call when a packet is received.
Definition LR_common.cpp:18
int16_t reset()
Reset method. Will reset the chip to the default state using RST pin.
Definition LR_common.cpp:169
Implements all common low-level methods to control the wireless module. Every module class contains o...
Definition Module.h:69
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition PhysicalLayer.h:390
unsigned long RadioLibTime_t
Type used for durations in RadioLib.
Definition TypeDef.h:679
Common data rate structure.
Definition PhysicalLayer.h:74
Common packet configuration structure.
Definition PhysicalLayer.h:119