Line data Source code
1 : #include "LR_common.h"
2 :
3 : #include <string.h>
4 :
5 17 : LRxxxx::LRxxxx(Module* mod) : PhysicalLayer() {
6 17 : this->mod = mod;
7 17 : this->mod->spiConfig.stream = true;
8 17 : this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] = Module::BITS_16;
9 17 : this->mod->spiConfig.statusPos = 0;
10 17 : this->mod->spiConfig.parseStatusCb = SPIparseStatus;
11 17 : this->mod->spiConfig.checkStatusCb = SPIcheckStatus;
12 17 : }
13 :
14 0 : void LRxxxx::setPacketReceivedAction(void (*func)(void)) {
15 0 : this->setIrqAction(func);
16 0 : }
17 :
18 0 : void LRxxxx::clearPacketReceivedAction() {
19 0 : this->clearIrqAction();
20 0 : }
21 :
22 0 : void LRxxxx::setPacketSentAction(void (*func)(void)) {
23 0 : this->setIrqAction(func);
24 0 : }
25 :
26 0 : void LRxxxx::clearPacketSentAction() {
27 0 : this->clearIrqAction();
28 0 : }
29 :
30 4 : uint32_t LRxxxx::getIrqStatus() {
31 : // there is no dedicated "get IRQ" command, the IRQ bits are sent after the status bytes
32 4 : uint8_t buff[6] = { 0 };
33 4 : Module::BitWidth_t statusWidth = mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS];
34 4 : this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] = Module::BITS_0;
35 4 : mod->SPItransferStream(NULL, 0, false, NULL, buff, sizeof(buff), true);
36 4 : this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] = statusWidth;
37 4 : uint32_t irq = ((uint32_t)(buff[2]) << 24) | ((uint32_t)(buff[3]) << 16) | ((uint32_t)(buff[4]) << 8) | (uint32_t)buff[5];
38 4 : return(irq);
39 : }
40 :
41 3 : RadioLibTime_t LRxxxx::getToA(size_t len, ModemType_t modem) {
42 3 : DataRate_t dr = {};
43 3 : PacketConfig_t pc = {};
44 3 : switch(modem) {
45 0 : case ModemType_t::RADIOLIB_MODEM_LORA: {
46 0 : uint8_t cr = this->codingRate;
47 : // We assume same calculation for short and long interleaving, so map CR values 0-4 and 5-7 to the same values
48 0 : if (cr < 5) {
49 0 : cr = cr + 4;
50 0 : } else if (cr == 7) {
51 0 : cr = cr + 1;
52 : }
53 :
54 0 : dr.lora.spreadingFactor = this->spreadingFactor;
55 0 : dr.lora.bandwidth = this->bandwidthKhz;
56 0 : dr.lora.codingRate = cr;
57 :
58 0 : pc.lora.preambleLength = this->preambleLengthLoRa;
59 0 : pc.lora.implicitHeader = (this->headerType == RADIOLIB_LRXXXX_LORA_HEADER_IMPLICIT);
60 0 : pc.lora.crcEnabled = (this->crcTypeLoRa == RADIOLIB_LRXXXX_LORA_CRC_ENABLED);
61 0 : pc.lora.ldrOptimize = (bool)this->ldrOptimize;
62 0 : break;
63 : }
64 :
65 0 : case ModemType_t::RADIOLIB_MODEM_FSK: {
66 0 : dr.fsk.bitRate = (float)this->bitRate / 1000.0f;
67 0 : dr.fsk.freqDev = (float)this->frequencyDev;
68 0 : pc.fsk.preambleLength = this->preambleLengthGFSK;
69 0 : pc.fsk.syncWordLength = this->syncWordLength;
70 0 : pc.fsk.crcLength = this->crcLenGFSK;
71 0 : break;
72 : }
73 :
74 0 : case ModemType_t::RADIOLIB_MODEM_LRFHSS: {
75 0 : dr.lrFhss.bw = this->lrFhssBw;
76 0 : dr.lrFhss.cr = this->lrFhssCr;
77 0 : dr.lrFhss.narrowGrid = (this->lrFhssGrid == RADIOLIB_LRXXXX_LR_FHSS_GRID_STEP_NON_FCC) ? true : false;
78 :
79 0 : pc.lrFhss.hdrCount = this->lrFhssHdrCount;
80 0 : break;
81 : }
82 :
83 3 : default:
84 3 : return(RADIOLIB_ERR_WRONG_MODEM);
85 : }
86 :
87 0 : return(this->calculateTimeOnAir(modem, dr, pc, len));
88 : }
89 :
90 17 : RadioLibTime_t LRxxxx::calculateTimeOnAir(ModemType_t modem, DataRate_t dr, PacketConfig_t pc, size_t len) {
91 : // check active modem
92 17 : if (modem == ModemType_t::RADIOLIB_MODEM_LORA) {
93 6 : uint32_t symbolLength_us = ((uint32_t)(1000 * 10) << dr.lora.spreadingFactor) / (dr.lora.bandwidth * 10) ;
94 6 : uint8_t sfCoeff1_x4 = 17; // (4.25 * 4)
95 6 : uint8_t sfCoeff2 = 8;
96 6 : if(dr.lora.spreadingFactor == 5 || dr.lora.spreadingFactor == 6) {
97 0 : sfCoeff1_x4 = 25; // 6.25 * 4
98 0 : sfCoeff2 = 0;
99 : }
100 6 : uint8_t sfDivisor = 4*dr.lora.spreadingFactor;
101 6 : if(pc.lora.ldrOptimize) {
102 3 : sfDivisor = 4*(dr.lora.spreadingFactor - 2);
103 : }
104 6 : const int8_t bitsPerCrc = 16;
105 6 : const int8_t N_symbol_header = pc.lora.implicitHeader ? 0 : 20;
106 :
107 : // numerator of equation in section 6.1.4 of SX1268 datasheet v1.1 (might not actually be bitcount, but it has len * 8)
108 6 : int16_t bitCount = (int16_t) 8 * len + pc.lora.crcEnabled * bitsPerCrc - 4 * dr.lora.spreadingFactor + sfCoeff2 + N_symbol_header;
109 6 : if(bitCount < 0) {
110 0 : bitCount = 0;
111 : }
112 : // add (sfDivisor) - 1 to the numerator to give integer CEIL(...)
113 6 : uint16_t nPreCodedSymbols = (bitCount + (sfDivisor - 1)) / (sfDivisor);
114 :
115 : // preamble can be 65k, therefore nSymbol_x4 needs to be 32 bit
116 6 : uint32_t nSymbol_x4 = (pc.lora.preambleLength + 8) * 4 + sfCoeff1_x4 + nPreCodedSymbols * dr.lora.codingRate * 4;
117 :
118 : // get time-on-air in us
119 6 : return((symbolLength_us * nSymbol_x4) / 4);
120 :
121 11 : } else if(modem == ModemType_t::RADIOLIB_MODEM_FSK) {
122 4 : return((((float)(pc.fsk.crcLength * 8) + pc.fsk.syncWordLength + pc.fsk.preambleLength + (uint32_t)len * 8) / (dr.fsk.bitRate / 1000.0f)));
123 :
124 7 : } else if(modem == ModemType_t::RADIOLIB_MODEM_LRFHSS) {
125 : // calculate the number of bits based on coding rate
126 : uint16_t N_bits;
127 3 : switch(dr.lrFhss.cr) {
128 0 : case RADIOLIB_LRXXXX_LR_FHSS_CR_5_6:
129 0 : N_bits = ((len * 6) + 4) / 5; // this is from the official LR11xx driver, but why the extra +4?
130 0 : break;
131 0 : case RADIOLIB_LRXXXX_LR_FHSS_CR_2_3:
132 0 : N_bits = (len * 3) / 2;
133 0 : break;
134 0 : case RADIOLIB_LRXXXX_LR_FHSS_CR_1_2:
135 0 : N_bits = len * 2;
136 0 : break;
137 3 : case RADIOLIB_LRXXXX_LR_FHSS_CR_1_3:
138 3 : N_bits = len * 3;
139 3 : break;
140 0 : default:
141 0 : return(RADIOLIB_ERR_INVALID_CODING_RATE);
142 : }
143 :
144 : // calculate number of bits when accounting for unaligned last block
145 3 : uint16_t N_payBits = (N_bits / RADIOLIB_LRXXXX_LR_FHSS_FRAG_BITS) * RADIOLIB_LRXXXX_LR_FHSS_BLOCK_BITS;
146 3 : uint16_t N_lastBlockBits = N_bits % RADIOLIB_LRXXXX_LR_FHSS_FRAG_BITS;
147 3 : if(N_lastBlockBits) {
148 3 : N_payBits += N_lastBlockBits + 2;
149 : }
150 :
151 : // add header bits
152 3 : uint16_t N_totalBits = (RADIOLIB_LRXXXX_LR_FHSS_HEADER_BITS * pc.lrFhss.hdrCount) + N_payBits;
153 3 : return(((uint32_t)N_totalBits * 8 * 1000000UL) / RADIOLIB_LRXXXX_LR_FHSS_BIT_RATE);
154 :
155 : } else {
156 4 : return(RADIOLIB_ERR_WRONG_MODEM);
157 : }
158 :
159 : return(0);
160 : }
161 :
162 4 : RadioLibTime_t LRxxxx::calculateRxTimeout(RadioLibTime_t timeoutUs) {
163 : // the timeout value is given in units of 30.52 microseconds
164 : // the calling function should provide some extra width, as this number of units is truncated to integer
165 4 : RadioLibTime_t timeout = timeoutUs / 30.52;
166 4 : return(timeout);
167 : }
168 :
169 0 : int16_t LRxxxx::reset() {
170 : // run the reset sequence
171 0 : this->mod->hal->pinMode(this->mod->getRst(), this->mod->hal->GpioModeOutput);
172 0 : this->mod->hal->digitalWrite(this->mod->getRst(), this->mod->hal->GpioLevelLow);
173 0 : this->mod->hal->delay(10);
174 0 : this->mod->hal->digitalWrite(this->mod->getRst(), this->mod->hal->GpioLevelHigh);
175 :
176 : // the typical transition duration should be 273 ms
177 0 : this->mod->hal->delay(300);
178 :
179 : // wait for BUSY to go low
180 0 : RadioLibTime_t start = this->mod->hal->millis();
181 0 : while(this->mod->hal->digitalRead(this->mod->getGpio())) {
182 0 : this->mod->hal->yield();
183 0 : if(this->mod->hal->millis() - start >= 3000) {
184 : RADIOLIB_DEBUG_BASIC_PRINTLN("BUSY pin timeout after reset!");
185 0 : return(RADIOLIB_ERR_SPI_CMD_TIMEOUT);
186 : }
187 : }
188 :
189 0 : return(RADIOLIB_ERR_NONE);
190 : }
191 :
192 0 : int16_t LRxxxx::getStatus(uint8_t* stat1, uint8_t* stat2, uint32_t* irq) {
193 0 : uint8_t buff[6] = { 0 };
194 :
195 : // the status check command doesn't return status in the same place as other read commands
196 : // but only as the first byte (as with any other command), hence LRxxxx::SPIcommand can't be used
197 : // it also seems to ignore the actual command, and just sending in bunch of NOPs will work
198 0 : int16_t state = this->mod->SPItransferStream(NULL, 0, false, NULL, buff, sizeof(buff), true);
199 :
200 : // pass the replies
201 0 : if(stat1) { *stat1 = buff[0]; }
202 0 : if(stat2) { *stat2 = buff[1]; }
203 0 : if(irq) { *irq = ((uint32_t)(buff[2]) << 24) | ((uint32_t)(buff[3]) << 16) | ((uint32_t)(buff[4]) << 8) | (uint32_t)buff[5]; }
204 :
205 0 : return(state);
206 : }
207 :
208 0 : int16_t LRxxxx::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) {
209 : // check maximum size
210 0 : const uint8_t maxLen[4][4] = {
211 : { 189, 178, 167, 155, },
212 : { 151, 142, 133, 123, },
213 : { 112, 105, 99, 92, },
214 : { 74, 69, 65, 60, },
215 : };
216 0 : if((cr > RADIOLIB_LRXXXX_LR_FHSS_CR_1_3) || ((hdrCount - 1) > (int)sizeof(maxLen[0])) || (len > maxLen[cr][hdrCount - 1])) {
217 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
218 : }
219 :
220 : // build buffers
221 0 : size_t buffLen = 9 + len;
222 : #if RADIOLIB_STATIC_ONLY
223 : uint8_t dataBuff[9 + 190];
224 : #else
225 0 : uint8_t* dataBuff = new uint8_t[buffLen];
226 : #endif
227 :
228 : // set properties of the packet
229 0 : dataBuff[0] = hdrCount;
230 0 : dataBuff[1] = cr;
231 0 : dataBuff[2] = RADIOLIB_LRXXXX_LR_FHSS_MOD_TYPE_GMSK;
232 0 : dataBuff[3] = grid;
233 0 : dataBuff[4] = hop;
234 0 : dataBuff[5] = bw;
235 0 : dataBuff[6] = (uint8_t)((hopSeq >> 8) & 0x01);
236 0 : dataBuff[7] = (uint8_t)(hopSeq & 0xFF);
237 0 : dataBuff[8] = devOffset;
238 0 : memcpy(&dataBuff[9], payload, len);
239 :
240 0 : int16_t state = this->SPIcommand(cmd, true, dataBuff, buffLen);
241 : #if !RADIOLIB_STATIC_ONLY
242 0 : delete[] dataBuff;
243 : #endif
244 0 : return(state);
245 : }
246 :
247 3 : uint8_t LRxxxx::roundRampTime(uint32_t rampTimeUs) {
248 : uint8_t regVal;
249 :
250 : // Round up the ramp time to nearest discrete register value
251 3 : if(rampTimeUs <= 2) {
252 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_2U;
253 3 : } else if(rampTimeUs <= 4) {
254 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_4U;
255 3 : } else if(rampTimeUs <= 8) {
256 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_8U;
257 3 : } else if(rampTimeUs <= 16) {
258 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_16U;
259 3 : } else if(rampTimeUs <= 32) {
260 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_32U;
261 3 : } else if(rampTimeUs <= 48) {
262 3 : regVal = RADIOLIB_LRXXXX_PA_RAMP_48U;
263 0 : } else if(rampTimeUs <= 64) {
264 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_64U;
265 0 : } else if(rampTimeUs <= 80) {
266 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_80U;
267 0 : } else if(rampTimeUs <= 96) {
268 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_96U;
269 0 : } else if(rampTimeUs <= 112) {
270 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_112U;
271 0 : } else if(rampTimeUs <= 128) {
272 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_128U;
273 0 : } else if(rampTimeUs <= 144) {
274 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_144U;
275 0 : } else if(rampTimeUs <= 160) {
276 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_160U;
277 0 : } else if(rampTimeUs <= 176) {
278 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_176U;
279 0 : } else if(rampTimeUs <= 192) {
280 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_192U;
281 0 : } else if(rampTimeUs <= 208) {
282 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_208U;
283 0 : } else if(rampTimeUs <= 240) {
284 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_240U;
285 0 : } else if(rampTimeUs <= 272) {
286 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_272U;
287 : } else { // 304
288 0 : regVal = RADIOLIB_LRXXXX_PA_RAMP_304U;
289 : }
290 :
291 3 : return regVal;
292 : }
293 :
294 0 : int16_t LRxxxx::findRxBw(float rxBw, const uint8_t* lut, size_t lutSize, float rxBwMax, uint8_t* val) {
295 : // lookup tables to avoid comparing a whole bunch of floats
296 0 : const uint16_t rxBwAvg[] = {
297 : 53, 66, 85, 108, 134, 170, 211, 264,
298 : 341, 424, 529, 682, 847, 1058, 1364,
299 : 1695, 2116, 2729, 3390, 4233, 5159,
300 : 6111, 7179, 9401, 16665, 24440, 28710,
301 : };
302 :
303 : // iterate through the table and find whether the user-provided value
304 : // is lower than the pre-computed average of the adjacent bandwidth values
305 : // if it is, we consider that to be a match even though the actual value is not precise
306 0 : uint16_t rxBwInt = rxBw*10.0f;
307 0 : for(size_t i = 0; i < (lutSize - 1); i++) {
308 0 : if(rxBwInt < rxBwAvg[i]) {
309 0 : *val = lut[i];
310 0 : return(RADIOLIB_ERR_NONE);
311 : }
312 : }
313 :
314 : // if nothing matched up to here, match with the last value
315 0 : if(rxBwInt <= rxBwMax*10) {
316 0 : *val = lut[lutSize - 1];
317 0 : return(RADIOLIB_ERR_NONE);
318 : }
319 :
320 0 : return(RADIOLIB_ERR_INVALID_RX_BANDWIDTH);
321 : }
322 :
323 8 : int16_t LRxxxx::setU32(uint16_t cmd, uint32_t u32) {
324 : uint8_t buff[] = {
325 8 : (uint8_t)((u32 >> 24) & 0xFF), (uint8_t)((u32 >> 16) & 0xFF),
326 8 : (uint8_t)((u32 >> 8) & 0xFF), (uint8_t)(u32 & 0xFF),
327 8 : };
328 16 : return(this->SPIcommand(cmd, true, buff, sizeof(buff)));
329 : }
330 :
331 3059 : int16_t LRxxxx::SPIparseStatus(uint8_t in) {
332 3059 : if((in & 0b00001110) == RADIOLIB_LRXXXX_STAT_1_CMD_PERR) {
333 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
334 3059 : } else if((in & 0b00001110) == RADIOLIB_LRXXXX_STAT_1_CMD_FAIL) {
335 0 : return(RADIOLIB_ERR_SPI_CMD_FAILED);
336 3059 : } else if((in == 0x00) || (in == 0xFF)) {
337 3059 : return(RADIOLIB_ERR_CHIP_NOT_FOUND);
338 : }
339 0 : return(RADIOLIB_ERR_NONE);
340 : }
341 :
342 9 : int16_t LRxxxx::SPIcheckStatus(Module* mod) {
343 : // the status check command doesn't return status in the same place as other read commands,
344 : // but only as the first byte (as with any other command), hence LR11x0::SPIcommand can't be used
345 : // it also seems to ignore the actual command, and just sending in bunch of NOPs will work
346 9 : uint8_t buff[6] = { 0 };
347 9 : Module::BitWidth_t statusWidth = mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS];
348 9 : mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] = Module::BITS_0;
349 9 : int16_t state = mod->SPItransferStream(NULL, 0, false, NULL, buff, sizeof(buff), true);
350 9 : mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] = statusWidth;
351 9 : RADIOLIB_ASSERT(state);
352 0 : return(LRxxxx::SPIparseStatus(buff[0]));
353 : }
354 :
355 0 : int16_t LRxxxx::writeCommon(uint16_t cmd, uint32_t addrOffset, const uint32_t* data, size_t len, bool nonvolatile) {
356 : // build buffers - later we need to ensure endians are correct,
357 : // so there is probably no way to do this without copying buffers and iterating
358 0 : size_t buffLen = sizeof(uint32_t) + len*sizeof(uint32_t);
359 : #if RADIOLIB_STATIC_ONLY
360 : uint8_t dataBuff[sizeof(uint32_t) + RADIOLIB_LRXXXX_SPI_MAX_READ_WRITE_LEN];
361 : #else
362 0 : uint8_t* dataBuff = new uint8_t[buffLen];
363 : #endif
364 :
365 : // set the address or offset
366 0 : uint8_t* dataBuffPtr = reinterpret_cast<uint8_t*>(dataBuff);
367 0 : if(this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] >= Module::BITS_32) {
368 : // LR2021 has 24-bit address, whereas LR11x0 has 32-bit
369 0 : *(dataBuffPtr++) = (uint8_t)((addrOffset >> 24) & 0xFF);
370 : }
371 :
372 0 : *(dataBuffPtr++) = (uint8_t)((addrOffset >> 16) & 0xFF);
373 0 : *(dataBuffPtr++) = (uint8_t)((addrOffset >> 8) & 0xFF);
374 0 : *(dataBuffPtr++) = (uint8_t)(addrOffset & 0xFF);
375 :
376 : // convert endians
377 0 : for(size_t i = 0; i < len; i++) {
378 0 : uint32_t bin = 0;
379 0 : if(nonvolatile) {
380 0 : uint32_t* ptr = const_cast<uint32_t*>(data) + i;
381 0 : bin = RADIOLIB_NONVOLATILE_READ_DWORD(ptr);
382 : } else {
383 0 : bin = data[i];
384 : }
385 0 : *(dataBuffPtr++) = (uint8_t)((bin >> 24) & 0xFF);
386 0 : *(dataBuffPtr++) = (uint8_t)((bin >> 16) & 0xFF);
387 0 : *(dataBuffPtr++) = (uint8_t)((bin >> 8) & 0xFF);
388 0 : *(dataBuffPtr++) = (uint8_t)(bin & 0xFF);
389 : }
390 :
391 0 : int16_t state = this->mod->SPIwriteStream(cmd, dataBuff, buffLen, true, false);
392 : #if !RADIOLIB_STATIC_ONLY
393 0 : delete[] dataBuff;
394 : #endif
395 0 : return(state);
396 : }
397 :
398 149 : int16_t LRxxxx::SPIcommand(uint16_t cmd, bool write, uint8_t* data, size_t len, const uint8_t* out, size_t outLen) {
399 149 : int16_t state = RADIOLIB_ERR_UNKNOWN;
400 149 : if(!write) {
401 : // the SPI interface of LR11x0 requires two separate transactions for reading
402 : // send the 16-bit command
403 101 : state = this->mod->SPIwriteStream(cmd, out, outLen, true, false);
404 101 : RADIOLIB_ASSERT(state);
405 :
406 : // read the result without command
407 101 : this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] = Module::BITS_0;
408 101 : state = this->mod->SPIreadStream(RADIOLIB_LRXXXX_CMD_NOP, data, len, true, false);
409 101 : this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] = Module::BITS_16;
410 :
411 : } else {
412 : // write is just a single transaction
413 48 : state = this->mod->SPIwriteStream(cmd, data, len, true, true);
414 :
415 : }
416 :
417 149 : return(state);
418 : }
|