Line data Source code
1 : #include "SX126x.h"
2 :
3 : // this file contains implementation of all commands
4 : // supported by the SX126x SPI interface
5 : // in most cases, the names of methods match those in the datasheet
6 : // however, sometimes slight changes had to be made in order to
7 : // better fit the RadioLib API
8 :
9 : #if !RADIOLIB_EXCLUDE_SX126X
10 :
11 0 : int16_t SX126x::resetAGC() {
12 : // warm sleep to power down the analog frontend
13 0 : int16_t state = sleep(true);
14 0 : RADIOLIB_ASSERT(state);
15 :
16 : // wake to RC standby
17 0 : state = standby(RADIOLIB_SX126X_STANDBY_RC, true);
18 0 : RADIOLIB_ASSERT(state);
19 :
20 : // recalibrate all blocks
21 0 : state = calibrate(RADIOLIB_SX126X_CALIBRATE_ALL);
22 0 : RADIOLIB_ASSERT(state);
23 :
24 : // re-calibrate image rejection for the operating frequency
25 0 : state = calibrateImage(this->freqMHz);
26 0 : RADIOLIB_ASSERT(state);
27 :
28 : // re-apply DIO2 RF switch if it was configured
29 0 : if(this->dio2RfSwitch) {
30 0 : state = setDio2AsRfSwitch(true);
31 0 : RADIOLIB_ASSERT(state);
32 : }
33 :
34 : // re-apply RX boosted gain if it was configured
35 0 : if(this->rxBoostedGainMode) {
36 0 : state = setRxBoostedGainMode(true);
37 0 : RADIOLIB_ASSERT(state);
38 : }
39 :
40 0 : return(RADIOLIB_ERR_NONE);
41 : }
42 :
43 0 : int16_t SX126x::calibrate(uint8_t params) {
44 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE, ¶ms, 1, true, false));
45 : }
46 :
47 3 : int16_t SX126x::sleep() {
48 3 : return(SX126x::sleep(true));
49 : }
50 :
51 3 : int16_t SX126x::sleep(bool retainConfig) {
52 : // set RF switch (if present)
53 3 : this->mod->setRfSwitchState(Module::MODE_IDLE);
54 :
55 3 : uint8_t sleepMode = RADIOLIB_SX126X_SLEEP_START_WARM | RADIOLIB_SX126X_SLEEP_RTC_OFF;
56 3 : if(!retainConfig) {
57 0 : sleepMode = RADIOLIB_SX126X_SLEEP_START_COLD | RADIOLIB_SX126X_SLEEP_RTC_OFF;
58 : }
59 3 : int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_SLEEP, &sleepMode, 1, false, false);
60 :
61 : // wait for SX126x to safely enter sleep mode
62 3 : this->mod->hal->delay(1);
63 :
64 3 : return(state);
65 : }
66 :
67 24 : int16_t SX126x::standby() {
68 24 : return(SX126x::standby(this->standbyXOSC ? RADIOLIB_SX126X_STANDBY_XOSC : RADIOLIB_SX126X_STANDBY_RC, true));
69 : }
70 :
71 3 : int16_t SX126x::standby(uint8_t mode) {
72 3 : return(SX126x::standby(mode, true));
73 : }
74 :
75 27 : int16_t SX126x::standby(uint8_t mode, bool wakeup) {
76 : // set RF switch (if present)
77 27 : this->mod->setRfSwitchState(Module::MODE_IDLE);
78 :
79 27 : if(wakeup) {
80 : // send a NOP command - this pulls the NSS low to exit the sleep mode,
81 : // while preventing interference with possible other SPI transactions
82 : // see https://github.com/jgromes/RadioLib/discussions/1364
83 27 : (void)this->mod->SPIwriteStream((uint16_t)RADIOLIB_SX126X_CMD_NOP, NULL, 0, false, false);
84 : }
85 :
86 27 : const uint8_t data[] = { mode };
87 54 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_STANDBY, data, 1));
88 : }
89 :
90 0 : int16_t SX126x::setFs() {
91 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_FS, NULL, 0));
92 : }
93 :
94 0 : int16_t SX126x::setTx(uint32_t timeout) {
95 0 : const uint8_t data[] = { (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF)} ;
96 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_TX, data, 3));
97 : }
98 :
99 6 : int16_t SX126x::setRx(uint32_t timeout) {
100 6 : const uint8_t data[] = { (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF) };
101 12 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_RX, data, 3, true, false));
102 : }
103 :
104 0 : int16_t SX126x::setCad(uint8_t symbolNum, uint8_t detPeak, uint8_t detMin, uint8_t exitMode, RadioLibTime_t timeout) {
105 : // default CAD parameters are selected according to recommendations on Semtech DS.SX1261-2.W.APP rev. 1.1, page 92.
106 :
107 : // build the packet with default configuration
108 : uint8_t data[7];
109 0 : data[0] = RADIOLIB_SX126X_CAD_ON_4_SYMB;
110 0 : data[1] = this->spreadingFactor + 13;
111 0 : data[2] = RADIOLIB_SX126X_CAD_PARAM_DET_MIN;
112 0 : data[3] = RADIOLIB_SX126X_CAD_GOTO_STDBY;
113 0 : uint32_t timeout_raw = (float)timeout / 15.625f;
114 0 : data[4] = (uint8_t)((timeout_raw >> 16) & 0xFF);
115 0 : data[5] = (uint8_t)((timeout_raw >> 8) & 0xFF);
116 0 : data[6] = (uint8_t)(timeout_raw & 0xFF);
117 :
118 : // set user-provided values
119 0 : if(symbolNum != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) {
120 0 : data[0] = symbolNum;
121 : }
122 :
123 0 : if(detPeak != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) {
124 0 : data[1] = detPeak;
125 : }
126 :
127 0 : if(detMin != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) {
128 0 : data[2] = detMin;
129 : }
130 :
131 0 : if(exitMode != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) {
132 0 : data[3] = exitMode;
133 : }
134 :
135 : // configure parameters
136 0 : int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_CAD_PARAMS, data, 7);
137 0 : RADIOLIB_ASSERT(state);
138 :
139 : // start CAD
140 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_CAD, NULL, 0));
141 : }
142 :
143 0 : int16_t SX126x::setPaConfig(uint8_t paDutyCycle, uint8_t deviceSel, uint8_t hpMax, uint8_t paLut) {
144 0 : const uint8_t data[] = { paDutyCycle, hpMax, deviceSel, paLut };
145 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_PA_CONFIG, data, 4));
146 : }
147 :
148 0 : int16_t SX126x::writeRegister(uint16_t addr, const uint8_t* data, uint8_t numBytes) {
149 0 : this->mod->SPIwriteRegisterBurst(addr, data, numBytes);
150 0 : return(RADIOLIB_ERR_NONE);
151 : }
152 :
153 27 : int16_t SX126x::readRegister(uint16_t addr, uint8_t* data, uint8_t numBytes) {
154 : // send the command
155 27 : this->mod->SPIreadRegisterBurst(addr, numBytes, data);
156 :
157 : // check the status
158 27 : int16_t state = this->mod->SPIcheckStream();
159 27 : return(state);
160 : }
161 :
162 0 : int16_t SX126x::writeBuffer(const uint8_t* data, uint8_t numBytes, uint8_t offset) {
163 0 : const uint8_t cmd[] = { RADIOLIB_SX126X_CMD_WRITE_BUFFER, offset };
164 0 : return(this->mod->SPIwriteStream(cmd, 2, data, numBytes));
165 : }
166 :
167 0 : int16_t SX126x::readBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset) {
168 0 : const uint8_t cmd[] = { RADIOLIB_SX126X_CMD_READ_BUFFER, offset };
169 0 : return(this->mod->SPIreadStream(cmd, 2, data, numBytes));
170 : }
171 :
172 3 : int16_t SX126x::setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask) {
173 3 : const uint8_t data[8] = {(uint8_t)((irqMask >> 8) & 0xFF), (uint8_t)(irqMask & 0xFF),
174 3 : (uint8_t)((dio1Mask >> 8) & 0xFF), (uint8_t)(dio1Mask & 0xFF),
175 3 : (uint8_t)((dio2Mask >> 8) & 0xFF), (uint8_t)(dio2Mask & 0xFF),
176 3 : (uint8_t)((dio3Mask >> 8) & 0xFF), (uint8_t)(dio3Mask & 0xFF)};
177 6 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_DIO_IRQ_PARAMS, data, 8));
178 : }
179 :
180 6 : int16_t SX126x::clearIrqStatus(uint16_t clearIrqParams) {
181 6 : const uint8_t data[] = { (uint8_t)((clearIrqParams >> 8) & 0xFF), (uint8_t)(clearIrqParams & 0xFF) };
182 12 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CLEAR_IRQ_STATUS, data, 2));
183 : }
184 :
185 0 : int16_t SX126x::setRfFrequency(uint32_t frf) {
186 0 : const uint8_t data[] = { (uint8_t)((frf >> 24) & 0xFF), (uint8_t)((frf >> 16) & 0xFF), (uint8_t)((frf >> 8) & 0xFF), (uint8_t)(frf & 0xFF) };
187 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_RF_FREQUENCY, data, 4));
188 : }
189 :
190 0 : int16_t SX126x::calibrateImage(const uint8_t* data) {
191 0 : int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE_IMAGE, data, 2);
192 :
193 : // if something failed, show the device errors
194 : #if RADIOLIB_DEBUG_BASIC
195 : if(state != RADIOLIB_ERR_NONE) {
196 : // unless mode is forced to standby, device errors will be 0
197 : standby();
198 : uint16_t errors = getDeviceErrors();
199 : RADIOLIB_DEBUG_BASIC_PRINTLN("Calibration failed, device errors: 0x%X", errors);
200 : }
201 : #endif
202 0 : return(state);
203 : }
204 :
205 57 : uint8_t SX126x::getPacketType() {
206 57 : uint8_t data = 0xFF;
207 57 : this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_PACKET_TYPE, &data, 1);
208 57 : return(data);
209 : }
210 :
211 0 : int16_t SX126x::setTxParams(uint8_t pwr, uint8_t rampTime) {
212 0 : const uint8_t data[] = { pwr, rampTime };
213 0 : int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_TX_PARAMS, data, 2);
214 0 : if(state == RADIOLIB_ERR_NONE) {
215 0 : this->pwr = pwr;
216 : }
217 0 : return(state);
218 : }
219 :
220 0 : int16_t SX126x::setModulationParams(uint8_t sf, uint8_t bw, uint8_t cr, uint8_t ldro) {
221 : // calculate symbol length and enable low data rate optimization, if auto-configuration is enabled
222 0 : if(this->ldroAuto) {
223 0 : float symbolLength = (float)(uint32_t(1) << this->spreadingFactor) / (float)this->bandwidthKhz;
224 0 : if(symbolLength >= 16.0f) {
225 0 : this->ldrOptimize = RADIOLIB_SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_ON;
226 : } else {
227 0 : this->ldrOptimize = RADIOLIB_SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_OFF;
228 : }
229 : } else {
230 0 : this->ldrOptimize = ldro;
231 : }
232 : // 500/9/8 - 0x09 0x04 0x03 0x00 - SF9, BW125, 4/8
233 : // 500/11/8 - 0x0B 0x04 0x03 0x00 - SF11 BW125, 4/7
234 0 : const uint8_t data[4] = {sf, bw, cr, this->ldrOptimize};
235 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_MODULATION_PARAMS, data, 4));
236 : }
237 :
238 0 : int16_t SX126x::setModulationParamsFSK(uint32_t br, uint8_t sh, uint8_t rxBw, uint32_t freqDev) {
239 0 : const uint8_t data[8] = {(uint8_t)((br >> 16) & 0xFF), (uint8_t)((br >> 8) & 0xFF), (uint8_t)(br & 0xFF),
240 : sh, rxBw,
241 0 : (uint8_t)((freqDev >> 16) & 0xFF), (uint8_t)((freqDev >> 8) & 0xFF), (uint8_t)(freqDev & 0xFF)};
242 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_MODULATION_PARAMS, data, 8));
243 : }
244 :
245 0 : int16_t SX126x::setModulationParamsBPSK(uint32_t br, uint8_t sh) {
246 0 : const uint8_t data[] = {(uint8_t)((br >> 16) & 0xFF), (uint8_t)((br >> 8) & 0xFF), (uint8_t)(br & 0xFF), sh};
247 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_MODULATION_PARAMS, data, sizeof(data)));
248 : }
249 :
250 0 : int16_t SX126x::setPacketParams(uint16_t preambleLen, uint8_t crcType, uint8_t payloadLen, uint8_t hdrType, uint8_t invertIQ) {
251 0 : int16_t state = fixInvertedIQ(invertIQ);
252 0 : RADIOLIB_ASSERT(state);
253 0 : const uint8_t data[6] = {(uint8_t)((preambleLen >> 8) & 0xFF), (uint8_t)(preambleLen & 0xFF), hdrType, payloadLen, crcType, invertIQ};
254 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_PACKET_PARAMS, data, 6));
255 : }
256 :
257 0 : int16_t SX126x::setPacketParamsFSK(uint16_t preambleLen, uint8_t preambleDetectorLen, uint8_t crcType, uint8_t syncWordLen, uint8_t addrCmp, uint8_t whiten, uint8_t packType, uint8_t payloadLen) {
258 0 : const uint8_t data[9] = {(uint8_t)((preambleLen >> 8) & 0xFF), (uint8_t)(preambleLen & 0xFF),
259 : preambleDetectorLen, syncWordLen, addrCmp,
260 0 : packType, payloadLen, crcType, whiten};
261 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_PACKET_PARAMS, data, 9));
262 : }
263 :
264 0 : int16_t SX126x::setPacketParamsBPSK(uint8_t payloadLen, uint16_t rampUpDelay, uint16_t rampDownDelay, uint16_t payloadLenBits) {
265 : const uint8_t data[] = { payloadLen,
266 0 : (uint8_t)((rampUpDelay >> 8) & 0xFF), (uint8_t)(rampUpDelay & 0xFF),
267 0 : (uint8_t)((rampDownDelay >> 8) & 0xFF), (uint8_t)(rampDownDelay & 0xFF),
268 0 : (uint8_t)((payloadLenBits >> 8) & 0xFF), (uint8_t)(payloadLenBits & 0xFF)
269 0 : };
270 :
271 : // this one is a bit different, it seems to be split into command transaction and then a register write
272 0 : int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_PACKET_PARAMS, data, sizeof(uint8_t));
273 0 : RADIOLIB_ASSERT(state);
274 0 : return(this->writeRegister(RADIOLIB_SX126X_REG_BPSK_PACKET_PARAMS, &data[1], sizeof(data) - sizeof(uint8_t)));
275 : }
276 :
277 0 : int16_t SX126x::setBufferBaseAddress(uint8_t txBaseAddress, uint8_t rxBaseAddress) {
278 0 : const uint8_t data[2] = {txBaseAddress, rxBaseAddress};
279 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_BUFFER_BASE_ADDRESS, data, 2));
280 : }
281 :
282 0 : int16_t SX126x::setRegulatorMode(uint8_t mode) {
283 0 : const uint8_t data[1] = {mode};
284 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_REGULATOR_MODE, data, 1));
285 : }
286 :
287 0 : uint8_t SX126x::getStatus() {
288 0 : uint8_t data = 0;
289 0 : this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_STATUS, &data, 0);
290 0 : return(data);
291 : }
292 :
293 3 : uint32_t SX126x::getPacketStatus() {
294 3 : uint8_t data[3] = {0, 0, 0};
295 3 : this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_PACKET_STATUS, data, 3);
296 3 : return((((uint32_t)data[0]) << 16) | (((uint32_t)data[1]) << 8) | (uint32_t)data[2]);
297 : }
298 :
299 0 : uint16_t SX126x::getDeviceErrors() {
300 0 : uint8_t data[2] = {0, 0};
301 0 : this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_DEVICE_ERRORS, data, 2);
302 0 : uint16_t opError = (((uint16_t)data[0] & 0xFF) << 8) | ((uint16_t)data[1]);
303 0 : return(opError);
304 : }
305 :
306 0 : int16_t SX126x::clearDeviceErrors() {
307 0 : const uint8_t data[2] = {RADIOLIB_SX126X_CMD_NOP, RADIOLIB_SX126X_CMD_NOP};
308 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CLEAR_DEVICE_ERRORS, data, 2));
309 : }
310 :
311 : #endif
|