2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 * Based on https://github.com/ExpressLRS/ExpressLRS
23 * Thanks to AlessandroAU, original creator of the ExpressLRS project.
28 #include "common/time.h"
30 #define REG_LR_FIRMWARE_VERSION_MSB 0x0153 //The address of the register holding the firmware version MSB
31 #define SX1280_REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB 0x0954
32 #define SX1280_REG_LR_ESTIMATED_FREQUENCY_ERROR_MASK 0x0FFFFF
34 #define SX1280_XTAL_FREQ 52000000
35 #define SX1280_FREQ_STEP (SX1280_XTAL_FREQ / 262144.0)
38 SX1280_RF_IDLE
= 0x00, // The radio is idle
39 SX1280_RF_RX_RUNNING
, // The radio is in reception state
40 SX1280_RF_TX_RUNNING
, // The radio is in transmission state
41 SX1280_RF_CAD
, // The radio is doing channel activity detection
45 * \brief Represents the operating mode the radio is actually running
48 SX1280_MODE_SLEEP
= 0x00, // The radio is in sleep mode
49 SX1280_MODE_CALIBRATION
, // The radio is in calibration mode
50 SX1280_MODE_STDBY_RC
, // The radio is in standby mode with RC oscillator
51 SX1280_MODE_STDBY_XOSC
, // The radio is in standby mode with XOSC oscillator
52 SX1280_MODE_FS
, // The radio is in frequency synthesis mode
53 SX1280_MODE_RX
, // The radio is in receive mode
54 SX1280_MODE_TX
, // The radio is in transmit mode
55 SX1280_MODE_CAD
// The radio is in channel activity detection mode
56 } sx1280OperatingModes_e
;
59 * \brief Declares the oscillator in use while in standby mode
61 * Using the STDBY_RC standby mode allow to reduce the energy consumption
62 * STDBY_XOSC should be used for time critical applications
65 SX1280_STDBY_RC
= 0x00,
66 SX1280_STDBY_XOSC
= 0x01,
67 } sx1280StandbyModes_e
;
70 * \brief Declares the power regulation used to power the device
72 * This command allows the user to specify if DC-DC or LDO is used for power regulation.
73 * Using only LDO implies that the Rx or Tx current is doubled
76 SX1280_USE_LDO
= 0x00, // Use LDO (default value)
77 SX1280_USE_DCDC
= 0x01, // Use DCDC
78 } sx1280RegulatorModes_e
;
81 * \brief Represents the possible packet type (i.e. modem) used
84 SX1280_PACKET_TYPE_GFSK
= 0x00,
85 SX1280_PACKET_TYPE_LORA
,
86 SX1280_PACKET_TYPE_RANGING
,
87 SX1280_PACKET_TYPE_FLRC
,
88 SX1280_PACKET_TYPE_BLE
,
89 SX1280_PACKET_TYPE_NONE
= 0x0F,
90 } sx1280PacketTypes_e
;
93 SX1280_LORA_IQ_NORMAL
= 0x40,
94 SX1280_LORA_IQ_INVERTED
= 0x00,
95 } sx1280LoraIqModes_e
;
98 SX1280_RADIO_CRC_OFF
= 0x00, // No CRC in use
99 SX1280_RADIO_CRC_1_BYTES
= 0x10,
100 SX1280_RADIO_CRC_2_BYTES
= 0x20,
101 SX1280_RADIO_CRC_3_BYTES
= 0x30,
105 * \brief Represents the ramping time for power amplifier
108 SX1280_RADIO_RAMP_02_US
= 0x00,
109 SX1280_RADIO_RAMP_04_US
= 0x20,
110 SX1280_RADIO_RAMP_06_US
= 0x40,
111 SX1280_RADIO_RAMP_08_US
= 0x60,
112 SX1280_RADIO_RAMP_10_US
= 0x80,
113 SX1280_RADIO_RAMP_12_US
= 0xA0,
114 SX1280_RADIO_RAMP_16_US
= 0xC0,
115 SX1280_RADIO_RAMP_20_US
= 0xE0,
119 * \brief Represents the number of symbols to be used for channel activity detection operation
122 SX1280_LORA_CAD_01_SYMBOL
= 0x00,
123 SX1280_LORA_CAD_02_SYMBOLS
= 0x20,
124 SX1280_LORA_CAD_04_SYMBOLS
= 0x40,
125 SX1280_LORA_CAD_08_SYMBOLS
= 0x60,
126 SX1280_LORA_CAD_16_SYMBOLS
= 0x80,
127 } sx1280LoraCadSymbols_e
;
130 * \brief Represents the possible spreading factor values in LORA packet types
133 SX1280_LORA_SF5
= 0x50,
134 SX1280_LORA_SF6
= 0x60,
135 SX1280_LORA_SF7
= 0x70,
136 SX1280_LORA_SF8
= 0x80,
137 SX1280_LORA_SF9
= 0x90,
138 SX1280_LORA_SF10
= 0xA0,
139 SX1280_LORA_SF11
= 0xB0,
140 SX1280_LORA_SF12
= 0xC0,
141 } sx1280LoraSpreadingFactors_e
;
144 * \brief Represents the bandwidth values for LORA packet type
147 SX1280_LORA_BW_0200
= 0x34,
148 SX1280_LORA_BW_0400
= 0x26,
149 SX1280_LORA_BW_0800
= 0x18,
150 SX1280_LORA_BW_1600
= 0x0A,
151 } sx1280LoraBandwidths_e
;
154 * \brief Represents the coding rate values for LORA packet type
157 SX1280_LORA_CR_4_5
= 0x01,
158 SX1280_LORA_CR_4_6
= 0x02,
159 SX1280_LORA_CR_4_7
= 0x03,
160 SX1280_LORA_CR_4_8
= 0x04,
161 SX1280_LORA_CR_LI_4_5
= 0x05,
162 SX1280_LORA_CR_LI_4_6
= 0x06,
163 SX1280_LORA_CR_LI_4_7
= 0x07,
164 } sx1280LoraCodingRates_e
;
167 SX1280_LORA_PACKET_VARIABLE_LENGTH
= 0x00, // The packet is on variable size, header included
168 SX1280_LORA_PACKET_FIXED_LENGTH
= 0x80, // The packet is known on both sides, no header included in the packet
169 SX1280_LORA_PACKET_EXPLICIT
= SX1280_LORA_PACKET_VARIABLE_LENGTH
,
170 SX1280_LORA_PACKET_IMPLICIT
= SX1280_LORA_PACKET_FIXED_LENGTH
,
171 } sx1280LoraPacketLengthsModes_e
;
174 SX1280_LORA_CRC_ON
= 0x20, // CRC activated
175 SX1280_LORA_CRC_OFF
= 0x00, // CRC not used
176 } sx1280LoraCrcModes_e
;
179 SX1280_RADIO_GET_STATUS
= 0xC0,
180 SX1280_RADIO_WRITE_REGISTER
= 0x18,
181 SX1280_RADIO_READ_REGISTER
= 0x19,
182 SX1280_RADIO_WRITE_BUFFER
= 0x1A,
183 SX1280_RADIO_READ_BUFFER
= 0x1B,
184 SX1280_RADIO_SET_SLEEP
= 0x84,
185 SX1280_RADIO_SET_STANDBY
= 0x80,
186 SX1280_RADIO_SET_FS
= 0xC1,
187 SX1280_RADIO_SET_TX
= 0x83,
188 SX1280_RADIO_SET_RX
= 0x82,
189 SX1280_RADIO_SET_RXDUTYCYCLE
= 0x94,
190 SX1280_RADIO_SET_CAD
= 0xC5,
191 SX1280_RADIO_SET_TXCONTINUOUSWAVE
= 0xD1,
192 SX1280_RADIO_SET_TXCONTINUOUSPREAMBLE
= 0xD2,
193 SX1280_RADIO_SET_PACKETTYPE
= 0x8A,
194 SX1280_RADIO_GET_PACKETTYPE
= 0x03,
195 SX1280_RADIO_SET_RFFREQUENCY
= 0x86,
196 SX1280_RADIO_SET_TXPARAMS
= 0x8E,
197 SX1280_RADIO_SET_CADPARAMS
= 0x88,
198 SX1280_RADIO_SET_BUFFERBASEADDRESS
= 0x8F,
199 SX1280_RADIO_SET_MODULATIONPARAMS
= 0x8B,
200 SX1280_RADIO_SET_PACKETPARAMS
= 0x8C,
201 SX1280_RADIO_GET_RXBUFFERSTATUS
= 0x17,
202 SX1280_RADIO_GET_PACKETSTATUS
= 0x1D,
203 SX1280_RADIO_GET_RSSIINST
= 0x1F,
204 SX1280_RADIO_SET_DIOIRQPARAMS
= 0x8D,
205 SX1280_RADIO_GET_IRQSTATUS
= 0x15,
206 SX1280_RADIO_CLR_IRQSTATUS
= 0x97,
207 SX1280_RADIO_CALIBRATE
= 0x89,
208 SX1280_RADIO_SET_REGULATORMODE
= 0x96,
209 SX1280_RADIO_SET_SAVECONTEXT
= 0xD5,
210 SX1280_RADIO_SET_AUTOTX
= 0x98,
211 SX1280_RADIO_SET_AUTOFS
= 0x9E,
212 SX1280_RADIO_SET_LONGPREAMBLE
= 0x9B,
213 SX1280_RADIO_SET_UARTSPEED
= 0x9D,
214 SX1280_RADIO_SET_RANGING_ROLE
= 0xA3,
218 SX1280_IRQ_RADIO_NONE
= 0x0000,
219 SX1280_IRQ_TX_DONE
= 0x0001,
220 SX1280_IRQ_RX_DONE
= 0x0002,
221 SX1280_IRQ_SYNCWORD_VALID
= 0x0004,
222 SX1280_IRQ_SYNCWORD_ERROR
= 0x0008,
223 SX1280_IRQ_HEADER_VALID
= 0x0010,
224 SX1280_IRQ_HEADER_ERROR
= 0x0020,
225 SX1280_IRQ_CRC_ERROR
= 0x0040,
226 SX1280_IRQ_RANGING_SLAVE_RESPONSE_DONE
= 0x0080,
227 SX1280_IRQ_RANGING_SLAVE_REQUEST_DISCARDED
= 0x0100,
228 SX1280_IRQ_RANGING_MASTER_RESULT_VALID
= 0x0200,
229 SX1280_IRQ_RANGING_MASTER_TIMEOUT
= 0x0400,
230 SX1280_IRQ_RANGING_SLAVE_REQUEST_VALID
= 0x0800,
231 SX1280_IRQ_CAD_DONE
= 0x1000,
232 SX1280_IRQ_CAD_DETECTED
= 0x2000,
233 SX1280_IRQ_RX_TX_TIMEOUT
= 0x4000,
234 SX1280_IRQ_PREAMBLE_DETECTED
= 0x8000,
235 SX1280_IRQ_RADIO_ALL
= 0xFFFF,
239 SX1280_RADIO_DIO1
= 0x02,
240 SX1280_RADIO_DIO2
= 0x04,
241 SX1280_RADIO_DIO3
= 0x08,
245 SX1280_RADIO_TICK_SIZE_0015_US
= 0x00,
246 SX1280_RADIO_TICK_SIZE_0062_US
= 0x01,
247 SX1280_RADIO_TICK_SIZE_1000_US
= 0x02,
248 SX1280_RADIO_TICK_SIZE_4000_US
= 0x03,
251 bool sx1280Init(IO_t resetPin
, IO_t busyPin
);
252 void sx1280ISR(void);
253 bool sx1280HandleFromTick(void);
254 bool sx1280IsBusy(void);
255 void sx1280WriteCommand(const uint8_t address
, const uint8_t data
);
256 void sx1280WriteCommandBurst(const uint8_t address
, const uint8_t *data
, const uint8_t length
);
257 void sx1280ReadCommandBurst(const uint8_t address
, uint8_t *data
, const uint8_t length
);
258 void sx1280WriteRegisterBurst(const uint16_t address
, const uint8_t *buffer
, const uint8_t size
);
259 void sx1280WriteRegister(const uint16_t address
, const uint8_t value
);
260 void sx1280ReadRegisterBurst(const uint16_t address
, uint8_t *buffer
, const uint8_t size
);
261 uint8_t sx1280ReadRegister(const uint16_t address
);
262 void sx1280WriteBuffer(const uint8_t offset
, const uint8_t *buffer
, const uint8_t size
);
263 void sx1280ReadBuffer(const uint8_t offset
, uint8_t *buffer
, const uint8_t size
);
265 uint8_t sx1280GetStatus(void);
266 void sx1280ConfigLoraDefaults(void);
267 void sx1280Config(const sx1280LoraBandwidths_e bw
, const sx1280LoraSpreadingFactors_e sf
, const sx1280LoraCodingRates_e cr
, const uint32_t freq
, const uint8_t preambleLength
, const bool iqInverted
);
268 void sx1280SetOutputPower(const int8_t power
);
269 void sx1280SetPacketParams(const uint8_t preambleLength
, const sx1280LoraPacketLengthsModes_e headerType
, const uint8_t payloadLength
, const sx1280LoraCrcModes_e crc
, const sx1280LoraIqModes_e invertIQ
);
270 void sx1280SetMode(const sx1280OperatingModes_e opMode
);
271 void sx1280ConfigLoraModParams(const sx1280LoraBandwidths_e bw
, const sx1280LoraSpreadingFactors_e sf
, const sx1280LoraCodingRates_e cr
);
272 void sx1280SetFrequencyReg(const uint32_t freqReg
);
273 void sx1280AdjustFrequency(int32_t offset
, const uint32_t freq
);
274 void sx1280SetFifoAddr(const uint8_t txBaseAddr
, const uint8_t rxBaseAddr
);
275 void sx1280SetDioIrqParams(const uint16_t irqMask
, const uint16_t dio1Mask
, const uint16_t dio2Mask
, const uint16_t dio3Mask
);
276 void sx1280ClearIrqStatus(const uint16_t irqMask
);
277 void sx1280GetIrqReason(void);
279 void sx1280HandleFromTock();
281 void sx1280TransmitData(const uint8_t *data
, const uint8_t length
);
282 void sx1280ReceiveData(uint8_t *data
, const uint8_t length
);
283 void sx1280StartReceiving(void);
285 void sx1280GetLastPacketStats(int8_t *rssi
, int8_t *snr
);