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 #define REG_LR_FIRMWARE_VERSION_MSB 0x0153 //The address of the register holding the firmware version MSB
29 #define SX1280_REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB 0x0954
30 #define SX1280_REG_LR_ESTIMATED_FREQUENCY_ERROR_MASK 0x0FFFFF
32 #define SX1280_XTAL_FREQ 52000000
33 #define SX1280_FREQ_STEP (SX1280_XTAL_FREQ / 262144.0)
36 SX1280_RF_IDLE
= 0x00, // The radio is idle
37 SX1280_RF_RX_RUNNING
, // The radio is in reception state
38 SX1280_RF_TX_RUNNING
, // The radio is in transmission state
39 SX1280_RF_CAD
, // The radio is doing channel activity detection
43 * \brief Represents the operating mode the radio is actually running
46 SX1280_MODE_SLEEP
= 0x00, // The radio is in sleep mode
47 SX1280_MODE_CALIBRATION
, // The radio is in calibration mode
48 SX1280_MODE_STDBY_RC
, // The radio is in standby mode with RC oscillator
49 SX1280_MODE_STDBY_XOSC
, // The radio is in standby mode with XOSC oscillator
50 SX1280_MODE_FS
, // The radio is in frequency synthesis mode
51 SX1280_MODE_RX
, // The radio is in receive mode
52 SX1280_MODE_TX
, // The radio is in transmit mode
53 SX1280_MODE_CAD
// The radio is in channel activity detection mode
54 } sx1280OperatingModes_e
;
57 * \brief Declares the oscillator in use while in standby mode
59 * Using the STDBY_RC standby mode allow to reduce the energy consumption
60 * STDBY_XOSC should be used for time critical applications
63 SX1280_STDBY_RC
= 0x00,
64 SX1280_STDBY_XOSC
= 0x01,
65 } sx1280StandbyModes_e
;
68 * \brief Declares the power regulation used to power the device
70 * This command allows the user to specify if DC-DC or LDO is used for power regulation.
71 * Using only LDO implies that the Rx or Tx current is doubled
74 SX1280_USE_LDO
= 0x00, // Use LDO (default value)
75 SX1280_USE_DCDC
= 0x01, // Use DCDC
76 } sx1280RegulatorModes_e
;
79 * \brief Represents the possible packet type (i.e. modem) used
82 SX1280_PACKET_TYPE_GFSK
= 0x00,
83 SX1280_PACKET_TYPE_LORA
,
84 SX1280_PACKET_TYPE_RANGING
,
85 SX1280_PACKET_TYPE_FLRC
,
86 SX1280_PACKET_TYPE_BLE
,
87 SX1280_PACKET_TYPE_NONE
= 0x0F,
88 } sx1280PacketTypes_e
;
91 SX1280_LORA_IQ_NORMAL
= 0x40,
92 SX1280_LORA_IQ_INVERTED
= 0x00,
93 } sx1280LoraIqModes_e
;
96 SX1280_RADIO_CRC_OFF
= 0x00, // No CRC in use
97 SX1280_RADIO_CRC_1_BYTES
= 0x10,
98 SX1280_RADIO_CRC_2_BYTES
= 0x20,
99 SX1280_RADIO_CRC_3_BYTES
= 0x30,
103 * \brief Represents the ramping time for power amplifier
106 SX1280_RADIO_RAMP_02_US
= 0x00,
107 SX1280_RADIO_RAMP_04_US
= 0x20,
108 SX1280_RADIO_RAMP_06_US
= 0x40,
109 SX1280_RADIO_RAMP_08_US
= 0x60,
110 SX1280_RADIO_RAMP_10_US
= 0x80,
111 SX1280_RADIO_RAMP_12_US
= 0xA0,
112 SX1280_RADIO_RAMP_16_US
= 0xC0,
113 SX1280_RADIO_RAMP_20_US
= 0xE0,
117 * \brief Represents the number of symbols to be used for channel activity detection operation
120 SX1280_LORA_CAD_01_SYMBOL
= 0x00,
121 SX1280_LORA_CAD_02_SYMBOLS
= 0x20,
122 SX1280_LORA_CAD_04_SYMBOLS
= 0x40,
123 SX1280_LORA_CAD_08_SYMBOLS
= 0x60,
124 SX1280_LORA_CAD_16_SYMBOLS
= 0x80,
125 } sx1280LoraCadSymbols_e
;
128 * \brief Represents the possible spreading factor values in LORA packet types
131 SX1280_LORA_SF5
= 0x50,
132 SX1280_LORA_SF6
= 0x60,
133 SX1280_LORA_SF7
= 0x70,
134 SX1280_LORA_SF8
= 0x80,
135 SX1280_LORA_SF9
= 0x90,
136 SX1280_LORA_SF10
= 0xA0,
137 SX1280_LORA_SF11
= 0xB0,
138 SX1280_LORA_SF12
= 0xC0,
139 } sx1280LoraSpreadingFactors_e
;
142 * \brief Represents the bandwidth values for LORA packet type
145 SX1280_LORA_BW_0200
= 0x34,
146 SX1280_LORA_BW_0400
= 0x26,
147 SX1280_LORA_BW_0800
= 0x18,
148 SX1280_LORA_BW_1600
= 0x0A,
149 } sx1280LoraBandwidths_e
;
152 * \brief Represents the coding rate values for LORA packet type
155 SX1280_LORA_CR_4_5
= 0x01,
156 SX1280_LORA_CR_4_6
= 0x02,
157 SX1280_LORA_CR_4_7
= 0x03,
158 SX1280_LORA_CR_4_8
= 0x04,
159 SX1280_LORA_CR_LI_4_5
= 0x05,
160 SX1280_LORA_CR_LI_4_6
= 0x06,
161 SX1280_LORA_CR_LI_4_7
= 0x07,
162 } sx1280LoraCodingRates_e
;
165 SX1280_LORA_PACKET_VARIABLE_LENGTH
= 0x00, // The packet is on variable size, header included
166 SX1280_LORA_PACKET_FIXED_LENGTH
= 0x80, // The packet is known on both sides, no header included in the packet
167 SX1280_LORA_PACKET_EXPLICIT
= SX1280_LORA_PACKET_VARIABLE_LENGTH
,
168 SX1280_LORA_PACKET_IMPLICIT
= SX1280_LORA_PACKET_FIXED_LENGTH
,
169 } sx1280LoraPacketLengthsModes_e
;
172 SX1280_LORA_CRC_ON
= 0x20, // CRC activated
173 SX1280_LORA_CRC_OFF
= 0x00, // CRC not used
174 } sx1280LoraCrcModes_e
;
177 SX1280_RADIO_GET_STATUS
= 0xC0,
178 SX1280_RADIO_WRITE_REGISTER
= 0x18,
179 SX1280_RADIO_READ_REGISTER
= 0x19,
180 SX1280_RADIO_WRITE_BUFFER
= 0x1A,
181 SX1280_RADIO_READ_BUFFER
= 0x1B,
182 SX1280_RADIO_SET_SLEEP
= 0x84,
183 SX1280_RADIO_SET_STANDBY
= 0x80,
184 SX1280_RADIO_SET_FS
= 0xC1,
185 SX1280_RADIO_SET_TX
= 0x83,
186 SX1280_RADIO_SET_RX
= 0x82,
187 SX1280_RADIO_SET_RXDUTYCYCLE
= 0x94,
188 SX1280_RADIO_SET_CAD
= 0xC5,
189 SX1280_RADIO_SET_TXCONTINUOUSWAVE
= 0xD1,
190 SX1280_RADIO_SET_TXCONTINUOUSPREAMBLE
= 0xD2,
191 SX1280_RADIO_SET_PACKETTYPE
= 0x8A,
192 SX1280_RADIO_GET_PACKETTYPE
= 0x03,
193 SX1280_RADIO_SET_RFFREQUENCY
= 0x86,
194 SX1280_RADIO_SET_TXPARAMS
= 0x8E,
195 SX1280_RADIO_SET_CADPARAMS
= 0x88,
196 SX1280_RADIO_SET_BUFFERBASEADDRESS
= 0x8F,
197 SX1280_RADIO_SET_MODULATIONPARAMS
= 0x8B,
198 SX1280_RADIO_SET_PACKETPARAMS
= 0x8C,
199 SX1280_RADIO_GET_RXBUFFERSTATUS
= 0x17,
200 SX1280_RADIO_GET_PACKETSTATUS
= 0x1D,
201 SX1280_RADIO_GET_RSSIINST
= 0x1F,
202 SX1280_RADIO_SET_DIOIRQPARAMS
= 0x8D,
203 SX1280_RADIO_GET_IRQSTATUS
= 0x15,
204 SX1280_RADIO_CLR_IRQSTATUS
= 0x97,
205 SX1280_RADIO_CALIBRATE
= 0x89,
206 SX1280_RADIO_SET_REGULATORMODE
= 0x96,
207 SX1280_RADIO_SET_SAVECONTEXT
= 0xD5,
208 SX1280_RADIO_SET_AUTOTX
= 0x98,
209 SX1280_RADIO_SET_AUTOFS
= 0x9E,
210 SX1280_RADIO_SET_LONGPREAMBLE
= 0x9B,
211 SX1280_RADIO_SET_UARTSPEED
= 0x9D,
212 SX1280_RADIO_SET_RANGING_ROLE
= 0xA3,
216 SX1280_IRQ_RADIO_NONE
= 0x0000,
217 SX1280_IRQ_TX_DONE
= 0x0001,
218 SX1280_IRQ_RX_DONE
= 0x0002,
219 SX1280_IRQ_SYNCWORD_VALID
= 0x0004,
220 SX1280_IRQ_SYNCWORD_ERROR
= 0x0008,
221 SX1280_IRQ_HEADER_VALID
= 0x0010,
222 SX1280_IRQ_HEADER_ERROR
= 0x0020,
223 SX1280_IRQ_CRC_ERROR
= 0x0040,
224 SX1280_IRQ_RANGING_SLAVE_RESPONSE_DONE
= 0x0080,
225 SX1280_IRQ_RANGING_SLAVE_REQUEST_DISCARDED
= 0x0100,
226 SX1280_IRQ_RANGING_MASTER_RESULT_VALID
= 0x0200,
227 SX1280_IRQ_RANGING_MASTER_TIMEOUT
= 0x0400,
228 SX1280_IRQ_RANGING_SLAVE_REQUEST_VALID
= 0x0800,
229 SX1280_IRQ_CAD_DONE
= 0x1000,
230 SX1280_IRQ_CAD_DETECTED
= 0x2000,
231 SX1280_IRQ_RX_TX_TIMEOUT
= 0x4000,
232 SX1280_IRQ_PREAMBLE_DETECTED
= 0x8000,
233 SX1280_IRQ_RADIO_ALL
= 0xFFFF,
237 SX1280_RADIO_DIO1
= 0x02,
238 SX1280_RADIO_DIO2
= 0x04,
239 SX1280_RADIO_DIO3
= 0x08,
243 SX1280_RADIO_TICK_SIZE_0015_US
= 0x00,
244 SX1280_RADIO_TICK_SIZE_0062_US
= 0x01,
245 SX1280_RADIO_TICK_SIZE_1000_US
= 0x02,
246 SX1280_RADIO_TICK_SIZE_4000_US
= 0x03,
249 bool sx1280Init(IO_t resetPin
, IO_t busyPin
);
250 uint8_t sx1280ISR(uint32_t *timeStamp
);
251 bool sx1280IsBusy(void);
252 void sx1280WriteCommand(const uint8_t address
, const uint8_t data
);
253 void sx1280WriteCommandBurst(const uint8_t address
, const uint8_t *data
, const uint8_t length
);
254 void sx1280ReadCommandBurst(const uint8_t address
, uint8_t *data
, const uint8_t length
);
255 void sx1280WriteRegisterBurst(const uint16_t address
, const uint8_t *buffer
, const uint8_t size
);
256 void sx1280WriteRegister(const uint16_t address
, const uint8_t value
);
257 void sx1280ReadRegisterBurst(const uint16_t address
, uint8_t *buffer
, const uint8_t size
);
258 uint8_t sx1280ReadRegister(const uint16_t address
);
259 void sx1280WriteBuffer(const uint8_t offset
, const uint8_t *buffer
, const uint8_t size
);
260 void sx1280ReadBuffer(const uint8_t offset
, uint8_t *buffer
, const uint8_t size
);
262 uint8_t sx1280GetStatus(void);
263 void sx1280ConfigLoraDefaults(void);
264 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
);
265 void sx1280SetOutputPower(const int8_t power
);
266 void sx1280SetPacketParams(const uint8_t preambleLength
, const sx1280LoraPacketLengthsModes_e headerType
, const uint8_t payloadLength
, const sx1280LoraCrcModes_e crc
, const sx1280LoraIqModes_e invertIQ
);
267 void sx1280SetMode(const sx1280OperatingModes_e opMode
);
268 void sx1280ConfigLoraModParams(const sx1280LoraBandwidths_e bw
, const sx1280LoraSpreadingFactors_e sf
, const sx1280LoraCodingRates_e cr
);
269 void sx1280SetFrequencyHZ(const uint32_t reqFreq
);
270 void sx1280SetFrequencyReg(const uint32_t freq
);
271 void sx1280AdjustFrequency(int32_t offset
, const uint32_t freq
);
272 void sx1280SetFIFOaddr(const uint8_t txBaseAddr
, const uint8_t rxBaseAddr
);
273 void sx1280SetDioIrqParams(const uint16_t irqMask
, const uint16_t dio1Mask
, const uint16_t dio2Mask
, const uint16_t dio3Mask
);
274 uint16_t sx1280GetIrqStatus(void);
275 void sx1280ClearIrqStatus(const uint16_t irqMask
);
276 uint8_t sx1280GetIrqReason(void);
278 void sx1280TransmitData(const uint8_t *data
, const uint8_t length
);
279 void sx1280ReceiveData(uint8_t *data
, const uint8_t length
);
280 void sx1280StartReceiving(void);
282 void sx1280GetLastPacketStats(int8_t *rssi
, int8_t *snr
);