Line data Source code
1 : #include "LR11x0.h"
2 :
3 : #include "../../utils/CRC.h"
4 : #include "../../utils/Cryptography.h"
5 : #include "LR_common.h"
6 :
7 : #include <string.h>
8 : #include <math.h>
9 :
10 : #if !RADIOLIB_EXCLUDE_LR11X0
11 :
12 0 : int16_t LR11x0::writeRegMem32(uint32_t addr, const uint32_t* data, size_t len) {
13 : // check maximum size
14 0 : if(len > (RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) {
15 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
16 : }
17 0 : return(this->writeCommon(RADIOLIB_LR11X0_CMD_WRITE_REG_MEM, addr, data, len, false));
18 : }
19 :
20 0 : int16_t LR11x0::readRegMem32(uint32_t addr, uint32_t* data, size_t len) {
21 : // check maximum size
22 0 : if(len >= (RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) {
23 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
24 : }
25 :
26 : // the request contains the address and length
27 : uint8_t reqBuff[5] = {
28 0 : (uint8_t)((addr >> 24) & 0xFF), (uint8_t)((addr >> 16) & 0xFF),
29 0 : (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF),
30 : (uint8_t)len,
31 0 : };
32 :
33 : // build buffers - later we need to ensure endians are correct,
34 : // so there is probably no way to do this without copying buffers and iterating
35 : #if RADIOLIB_STATIC_ONLY
36 : uint8_t rplBuff[RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN];
37 : #else
38 0 : uint8_t* rplBuff = new uint8_t[len*sizeof(uint32_t)];
39 : #endif
40 :
41 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_READ_REG_MEM, false, rplBuff, len*sizeof(uint32_t), reqBuff, sizeof(reqBuff));
42 :
43 : // convert endians
44 0 : if(data && (state == RADIOLIB_ERR_NONE)) {
45 0 : for(size_t i = 0; i < len; i++) {
46 0 : data[i] = ((uint32_t)rplBuff[2 + i*sizeof(uint32_t)] << 24) | ((uint32_t)rplBuff[3 + i*sizeof(uint32_t)] << 16) | ((uint32_t)rplBuff[4 + i*sizeof(uint32_t)] << 8) | (uint32_t)rplBuff[5 + i*sizeof(uint32_t)];
47 : }
48 : }
49 :
50 : #if !RADIOLIB_STATIC_ONLY
51 0 : delete[] rplBuff;
52 : #endif
53 :
54 0 : return(state);
55 : }
56 :
57 0 : int16_t LR11x0::writeBuffer8(const uint8_t* data, size_t len) {
58 : // check maximum size
59 0 : if(len > RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN) {
60 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
61 : }
62 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_WRITE_BUFFER, true, const_cast<uint8_t*>(data), len));
63 : }
64 :
65 0 : int16_t LR11x0::readBuffer8(uint8_t* data, size_t len, size_t offset) {
66 : // check maximum size
67 0 : if(len > RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN) {
68 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
69 : }
70 :
71 : // build buffers
72 0 : size_t reqLen = 2*sizeof(uint8_t) + len;
73 : #if RADIOLIB_STATIC_ONLY
74 : uint8_t reqBuff[sizeof(uint32_t) + RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN];
75 : #else
76 0 : uint8_t* reqBuff = new uint8_t[reqLen];
77 : #endif
78 :
79 : // set the offset and length
80 0 : reqBuff[0] = (uint8_t)offset;
81 0 : reqBuff[1] = (uint8_t)len;
82 :
83 : // send the request
84 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_READ_BUFFER, false, data, len, reqBuff, reqLen);
85 : #if !RADIOLIB_STATIC_ONLY
86 0 : delete[] reqBuff;
87 : #endif
88 0 : return(state);
89 : }
90 :
91 0 : int16_t LR11x0::clearRxBuffer(void) {
92 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_CLEAR_RX_BUFFER, true, NULL, 0));
93 : }
94 :
95 0 : int16_t LR11x0::writeRegMemMask32(uint32_t addr, uint32_t mask, uint32_t data) {
96 : uint8_t buff[12] = {
97 0 : (uint8_t)((addr >> 24) & 0xFF), (uint8_t)((addr >> 16) & 0xFF), (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF),
98 0 : (uint8_t)((mask >> 24) & 0xFF), (uint8_t)((mask >> 16) & 0xFF), (uint8_t)((mask >> 8) & 0xFF), (uint8_t)(mask & 0xFF),
99 0 : (uint8_t)((data >> 24) & 0xFF), (uint8_t)((data >> 16) & 0xFF), (uint8_t)((data >> 8) & 0xFF), (uint8_t)(data & 0xFF),
100 0 : };
101 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_WRITE_REG_MEM_MASK, true, buff, sizeof(buff)));
102 : }
103 :
104 0 : int16_t LR11x0::getVersion(uint8_t* hw, uint8_t* device, uint8_t* major, uint8_t* minor) {
105 0 : uint8_t buff[4] = { 0 };
106 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_VERSION, false, buff, sizeof(buff));
107 :
108 : // pass the replies
109 0 : if(hw) { *hw = buff[0]; }
110 0 : if(device) { *device = buff[1]; }
111 0 : if(major) { *major = buff[2]; }
112 0 : if(minor) { *minor = buff[3]; }
113 :
114 0 : return(state);
115 : }
116 :
117 0 : int16_t LR11x0::getErrors(uint16_t* err) {
118 0 : uint8_t buff[2] = { 0 };
119 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_ERRORS, false, buff, sizeof(buff));
120 :
121 : // pass the replies
122 0 : if(err) { *err = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1]; }
123 :
124 0 : return(state);
125 : }
126 :
127 0 : int16_t LR11x0::clearErrors(void) {
128 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_CLEAR_ERRORS, true, NULL, 0));
129 : }
130 :
131 0 : int16_t LR11x0::calibrate(uint8_t params) {
132 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_CALIBRATE, true, ¶ms, 1));
133 : }
134 :
135 0 : int16_t LR11x0::setRegMode(uint8_t mode) {
136 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_REG_MODE, true, &mode, 1));
137 : }
138 :
139 0 : int16_t LR11x0::calibrateImageRejection(float freqMin, float freqMax) {
140 : uint8_t buff[2] = {
141 0 : (uint8_t)floor((freqMin - 1.0f) / 4.0f),
142 0 : (uint8_t)ceil((freqMax + 1.0f) / 4.0f)
143 0 : };
144 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_CALIB_IMAGE, true, buff, sizeof(buff)));
145 : }
146 :
147 0 : int16_t LR11x0::setDioAsRfSwitch(uint8_t en, uint8_t stbyCfg, uint8_t rxCfg, uint8_t txCfg, uint8_t txHpCfg, uint8_t txHfCfg, uint8_t gnssCfg, uint8_t wifiCfg) {
148 0 : uint8_t buff[8] = { en, stbyCfg, rxCfg, txCfg, txHpCfg, txHfCfg, gnssCfg, wifiCfg };
149 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_DIO_AS_RF_SWITCH, true, buff, sizeof(buff)));
150 : }
151 :
152 3 : int16_t LR11x0::setDioIrqParams(uint32_t irq1, uint32_t irq2) {
153 : uint8_t buff[8] = {
154 3 : (uint8_t)((irq1 >> 24) & 0xFF), (uint8_t)((irq1 >> 16) & 0xFF), (uint8_t)((irq1 >> 8) & 0xFF), (uint8_t)(irq1 & 0xFF),
155 3 : (uint8_t)((irq2 >> 24) & 0xFF), (uint8_t)((irq2 >> 16) & 0xFF), (uint8_t)((irq2 >> 8) & 0xFF), (uint8_t)(irq2 & 0xFF),
156 3 : };
157 6 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_DIO_IRQ_PARAMS, true, buff, sizeof(buff)));
158 : }
159 :
160 0 : int16_t LR11x0::setDioIrqParams(uint32_t irq) {
161 0 : return(setDioIrqParams(irq, this->gnss ? 0 : irq));
162 : }
163 :
164 6 : int16_t LR11x0::clearIrqState(uint32_t irq) {
165 6 : return(this->setU32(RADIOLIB_LR11X0_CMD_CLEAR_IRQ, irq));
166 : }
167 :
168 0 : int16_t LR11x0::configLfClock(uint8_t setup) {
169 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_CONFIG_LF_CLOCK, true, &setup, 1));
170 : }
171 :
172 0 : int16_t LR11x0::setTcxoMode(uint8_t tune, uint32_t delay) {
173 : uint8_t buff[4] = {
174 0 : tune, (uint8_t)((delay >> 16) & 0xFF), (uint8_t)((delay >> 8) & 0xFF), (uint8_t)(delay & 0xFF),
175 0 : };
176 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_TCXO_MODE, true, buff, sizeof(buff)));
177 : }
178 :
179 0 : int16_t LR11x0::reboot(bool stay) {
180 0 : uint8_t buff[1] = { (uint8_t)(stay*3) };
181 0 : return(this->mod->SPIwriteStream(RADIOLIB_LR11X0_CMD_REBOOT, buff, sizeof(buff), true, false));
182 : }
183 :
184 0 : int16_t LR11x0::getVbat(float* vbat) {
185 0 : uint8_t buff[1] = { 0 };
186 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_VBAT, false, buff, sizeof(buff));
187 :
188 : // pass the replies
189 0 : if(vbat) { *vbat = (((float)buff[0]/51.0f) - 1.0f)*1.35f; }
190 :
191 0 : return(state);
192 : }
193 :
194 0 : int16_t LR11x0::getTemp(float* temp) {
195 0 : uint8_t buff[2] = { 0 };
196 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_TEMP, false, buff, sizeof(buff));
197 :
198 : // pass the replies
199 0 : if(temp) {
200 0 : uint16_t raw = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1];
201 0 : raw = raw & 0x07FF; //According LR1121 datasheet we need [0..10] bits
202 0 : *temp = 25.0f - (1000.0f/1.7f)*(((float)raw/2047.0f)*1.35f - 0.7295f); //According LR1121 datasheet 1.35
203 : }
204 :
205 0 : return(state);
206 : }
207 :
208 0 : int16_t LR11x0::setFs(void) {
209 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_FS, true, NULL, 0));
210 : }
211 :
212 3 : int16_t LR11x0::getRandomNumber(uint32_t* rnd) {
213 3 : uint8_t buff[4] = { 0 };
214 3 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_RANDOM_NUMBER, false, buff, sizeof(buff));
215 :
216 : // pass the replies
217 3 : if(rnd) { *rnd = ((uint32_t)(buff[0]) << 24) | ((uint32_t)(buff[1]) << 16) | ((uint32_t)(buff[2]) << 8) | (uint32_t)buff[3]; }
218 :
219 3 : return(state);
220 : }
221 :
222 0 : int16_t LR11x0::eraseInfoPage(void) {
223 : // only page 1 can be erased
224 0 : uint8_t buff[1] = { RADIOLIB_LR11X0_INFO_PAGE };
225 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_ERASE_INFO_PAGE, true, buff, sizeof(buff)));
226 : }
227 :
228 0 : int16_t LR11x0::writeInfoPage(uint16_t addr, const uint32_t* data, size_t len) {
229 : // check maximum size
230 0 : if(len > (RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) {
231 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
232 : }
233 :
234 : // build buffers - later we need to ensure endians are correct,
235 : // so there is probably no way to do this without copying buffers and iterating
236 0 : size_t buffLen = sizeof(uint8_t) + sizeof(uint16_t) + len*sizeof(uint32_t);
237 : #if RADIOLIB_STATIC_ONLY
238 : uint8_t dataBuff[sizeof(uint8_t) + sizeof(uint16_t) + RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN];
239 : #else
240 0 : uint8_t* dataBuff = new uint8_t[buffLen];
241 : #endif
242 :
243 : // set the address
244 0 : dataBuff[0] = RADIOLIB_LR11X0_INFO_PAGE;
245 0 : dataBuff[1] = (uint8_t)((addr >> 8) & 0xFF);
246 0 : dataBuff[2] = (uint8_t)(addr & 0xFF);
247 :
248 : // convert endians
249 0 : for(size_t i = 0; i < len; i++) {
250 0 : dataBuff[3 + i] = (uint8_t)((data[i] >> 24) & 0xFF);
251 0 : dataBuff[4 + i] = (uint8_t)((data[i] >> 16) & 0xFF);
252 0 : dataBuff[5 + i] = (uint8_t)((data[i] >> 8) & 0xFF);
253 0 : dataBuff[6 + i] = (uint8_t)(data[i] & 0xFF);
254 : }
255 :
256 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_WRITE_INFO_PAGE, true, dataBuff, buffLen);
257 : #if !RADIOLIB_STATIC_ONLY
258 0 : delete[] dataBuff;
259 : #endif
260 0 : return(state);
261 : }
262 :
263 0 : int16_t LR11x0::readInfoPage(uint16_t addr, uint32_t* data, size_t len) {
264 : // check maximum size
265 0 : if(len > (RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) {
266 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
267 : }
268 :
269 : // the request contains the address and length
270 0 : uint8_t reqBuff[4] = {
271 : RADIOLIB_LR11X0_INFO_PAGE,
272 0 : (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF),
273 : (uint8_t)len,
274 0 : };
275 :
276 : // build buffers - later we need to ensure endians are correct,
277 : // so there is probably no way to do this without copying buffers and iterating
278 : #if RADIOLIB_STATIC_ONLY
279 : uint8_t rplBuff[RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN];
280 : #else
281 0 : uint8_t* rplBuff = new uint8_t[len*sizeof(uint32_t)];
282 : #endif
283 :
284 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_READ_INFO_PAGE, false, rplBuff, len*sizeof(uint32_t), reqBuff, sizeof(reqBuff));
285 :
286 : // convert endians
287 0 : if(data && (state == RADIOLIB_ERR_NONE)) {
288 0 : for(size_t i = 0; i < len; i++) {
289 0 : data[i] = ((uint32_t)rplBuff[2 + i*sizeof(uint32_t)] << 24) | ((uint32_t)rplBuff[3 + i*sizeof(uint32_t)] << 16) | ((uint32_t)rplBuff[4 + i*sizeof(uint32_t)] << 8) | (uint32_t)rplBuff[5 + i*sizeof(uint32_t)];
290 : }
291 : }
292 :
293 : #if !RADIOLIB_STATIC_ONLY
294 0 : delete[] rplBuff;
295 : #endif
296 :
297 0 : return(state);
298 : }
299 :
300 0 : int16_t LR11x0::getChipEui(uint8_t* eui) {
301 0 : RADIOLIB_ASSERT_PTR(eui);
302 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_CHIP_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN));
303 : }
304 :
305 0 : int16_t LR11x0::getSemtechJoinEui(uint8_t* eui) {
306 0 : RADIOLIB_ASSERT_PTR(eui);
307 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_SEMTECH_JOIN_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN));
308 : }
309 :
310 0 : int16_t LR11x0::deriveRootKeysAndGetPin(uint8_t* pin) {
311 0 : RADIOLIB_ASSERT_PTR(pin);
312 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_DERIVE_ROOT_KEYS_AND_GET_PIN, false, pin, RADIOLIB_LR11X0_PIN_LEN));
313 : }
314 :
315 0 : int16_t LR11x0::enableSpiCrc(bool en) {
316 : // TODO implement this
317 : (void)en;
318 : // LR11X0 CRC is gen 0xA6 (0x65 but reflected), init 0xFF, input and result reflected
319 : /*RadioLibCRCInstance.size = 8;
320 : RadioLibCRCInstance.poly = 0xA6;
321 : RadioLibCRCInstance.init = 0xFF;
322 : RadioLibCRCInstance.out = 0x00;
323 : RadioLibCRCInstance.refIn = true;
324 : RadioLibCRCInstance.refOut = true;*/
325 0 : return(RADIOLIB_ERR_UNSUPPORTED);
326 : }
327 :
328 0 : int16_t LR11x0::driveDiosInSleepMode(bool en) {
329 0 : uint8_t buff[1] = { (uint8_t)en };
330 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_DRIVE_DIOS_IN_SLEEP_MODE, true, buff, sizeof(buff)));
331 : }
332 :
333 0 : int16_t LR11x0::resetStats(void) {
334 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_RESET_STATS, true, NULL, 0));
335 : }
336 :
337 0 : int16_t LR11x0::getStats(uint16_t* nbPktReceived, uint16_t* nbPktCrcError, uint16_t* data1, uint16_t* data2) {
338 0 : uint8_t buff[8] = { 0 };
339 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_STATS, false, buff, sizeof(buff));
340 :
341 : // pass the replies
342 0 : if(nbPktReceived) { *nbPktReceived = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1]; }
343 0 : if(nbPktCrcError) { *nbPktCrcError = ((uint16_t)(buff[2]) << 8) | (uint16_t)buff[3]; }
344 0 : if(data1) { *data1 = ((uint16_t)(buff[4]) << 8) | (uint16_t)buff[5]; }
345 0 : if(data2) { *data2 = ((uint16_t)(buff[6]) << 8) | (uint16_t)buff[7]; }
346 :
347 0 : return(state);
348 : }
349 :
350 69 : int16_t LR11x0::getPacketType(uint8_t* type) {
351 69 : uint8_t buff[1] = { 0 };
352 69 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_PACKET_TYPE, false, buff, sizeof(buff));
353 :
354 : // pass the replies
355 69 : if(type) { *type = buff[0]; }
356 :
357 69 : return(state);
358 : }
359 :
360 3 : int16_t LR11x0::getRxBufferStatus(uint8_t* len, uint8_t* startOffset) {
361 3 : uint8_t buff[2] = { 0 };
362 3 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_RX_BUFFER_STATUS, false, buff, sizeof(buff));
363 :
364 : // pass the replies
365 3 : if(len) { *len = buff[0]; }
366 3 : if(startOffset) { *startOffset = buff[1]; }
367 :
368 3 : return(state);
369 : }
370 :
371 0 : int16_t LR11x0::getPacketStatusLoRa(float* rssiPkt, float* snrPkt, float* signalRssiPkt) {
372 0 : uint8_t buff[3] = { 0 };
373 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_PACKET_STATUS, false, buff, sizeof(buff));
374 :
375 : // pass the replies
376 0 : if(rssiPkt) { *rssiPkt = (float)buff[0] / -2.0f; }
377 0 : if(snrPkt) { *snrPkt = (float)((int8_t)buff[1]) / 4.0f; }
378 0 : if(signalRssiPkt) { *signalRssiPkt = buff[2]; }
379 :
380 0 : return(state);
381 : }
382 :
383 0 : int16_t LR11x0::getPacketStatusGFSK(float* rssiSync, float* rssiAvg, uint8_t* rxLen, uint8_t* stat) {
384 0 : uint8_t buff[4] = { 0 };
385 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_PACKET_STATUS, false, buff, sizeof(buff));
386 :
387 : // pass the replies
388 0 : if(rssiSync) { *rssiSync = (float)buff[0] / -2.0f; }
389 0 : if(rssiAvg) { *rssiAvg = (float)buff[1] / -2.0f; }
390 0 : if(rxLen) { *rxLen = buff[2]; }
391 0 : if(stat) { *stat = buff[3]; }
392 :
393 0 : return(state);
394 : }
395 :
396 0 : int16_t LR11x0::getRssiInst(float* rssi) {
397 0 : uint8_t buff[1] = { 0 };
398 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_RSSI_INST, false, buff, sizeof(buff));
399 :
400 : // pass the replies
401 0 : if(rssi) { *rssi = (float)buff[0] / -2.0f; }
402 :
403 0 : return(state);
404 : }
405 :
406 0 : int16_t LR11x0::setGfskSyncWord(uint8_t* sync) {
407 0 : RADIOLIB_ASSERT_PTR(sync);
408 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_GFSK_SYNC_WORD, true, sync, RADIOLIB_LR11X0_GFSK_SYNC_WORD_LEN));
409 : }
410 :
411 0 : int16_t LR11x0::setLoRaPublicNetwork(bool pub) {
412 0 : uint8_t buff[1] = { (uint8_t)pub };
413 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_LORA_PUBLIC_NETWORK, true, buff, sizeof(buff)));
414 : }
415 :
416 3 : int16_t LR11x0::setRx(uint32_t timeout) {
417 : uint8_t buff[3] = {
418 3 : (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF),
419 3 : };
420 6 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_RX, true, buff, sizeof(buff)));
421 : }
422 :
423 0 : int16_t LR11x0::setTx(uint32_t timeout) {
424 : uint8_t buff[3] = {
425 0 : (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF),
426 0 : };
427 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_TX, true, buff, sizeof(buff)));
428 : }
429 :
430 0 : int16_t LR11x0::setRfFrequency(uint32_t rfFreq) {
431 0 : return(this->setU32(RADIOLIB_LR11X0_CMD_SET_RF_FREQUENCY, rfFreq));
432 : }
433 :
434 0 : int16_t LR11x0::autoTxRx(uint32_t delay, uint8_t intMode, uint32_t timeout) {
435 : uint8_t buff[7] = {
436 0 : (uint8_t)((delay >> 16) & 0xFF), (uint8_t)((delay >> 8) & 0xFF), (uint8_t)(delay & 0xFF), intMode,
437 0 : (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF),
438 0 : };
439 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_AUTO_TX_RX, true, buff, sizeof(buff)));
440 : }
441 :
442 0 : int16_t LR11x0::setCadParams(uint8_t symNum, uint8_t detPeak, uint8_t detMin, uint8_t cadExitMode, uint32_t timeout) {
443 : uint8_t buff[7] = {
444 : symNum, detPeak, detMin, cadExitMode,
445 0 : (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF),
446 0 : };
447 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_CAD_PARAMS, true, buff, sizeof(buff)));
448 : }
449 :
450 0 : int16_t LR11x0::setPacketType(uint8_t type) {
451 0 : uint8_t buff[1] = { type };
452 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_PACKET_TYPE, true, buff, sizeof(buff)));
453 : }
454 :
455 0 : int16_t LR11x0::setModulationParamsLoRa(uint8_t sf, uint8_t bw, uint8_t cr, uint8_t ldro) {
456 : // calculate symbol length and enable low data rate optimization, if auto-configuration is enabled
457 0 : if(this->ldroAuto) {
458 0 : float symbolLength = (float)(uint32_t(1) << this->spreadingFactor) / (float)this->bandwidthKhz;
459 0 : if(symbolLength >= 16.0f) {
460 0 : this->ldrOptimize = RADIOLIB_LR11X0_LORA_LDRO_ENABLED;
461 : } else {
462 0 : this->ldrOptimize = RADIOLIB_LR11X0_LORA_LDRO_DISABLED;
463 : }
464 : } else {
465 0 : this->ldrOptimize = ldro;
466 : }
467 :
468 0 : uint8_t buff[4] = { sf, bw, cr, this->ldrOptimize };
469 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_MODULATION_PARAMS, true, buff, sizeof(buff)));
470 : }
471 :
472 0 : int16_t LR11x0::setModulationParamsGFSK(uint32_t br, uint8_t sh, uint8_t rxBw, uint32_t freqDev) {
473 : uint8_t buff[10] = {
474 0 : (uint8_t)((br >> 24) & 0xFF), (uint8_t)((br >> 16) & 0xFF),
475 0 : (uint8_t)((br >> 8) & 0xFF), (uint8_t)(br & 0xFF), sh, rxBw,
476 0 : (uint8_t)((freqDev >> 24) & 0xFF), (uint8_t)((freqDev >> 16) & 0xFF),
477 0 : (uint8_t)((freqDev >> 8) & 0xFF), (uint8_t)(freqDev & 0xFF)
478 0 : };
479 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_MODULATION_PARAMS, true, buff, sizeof(buff)));
480 : }
481 :
482 0 : int16_t LR11x0::setModulationParamsLrFhss(uint32_t br, uint8_t sh) {
483 : uint8_t buff[5] = {
484 0 : (uint8_t)((br >> 24) & 0xFF), (uint8_t)((br >> 16) & 0xFF),
485 0 : (uint8_t)((br >> 8) & 0xFF), (uint8_t)(br & 0xFF), sh
486 0 : };
487 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_MODULATION_PARAMS, true, buff, sizeof(buff)));
488 : }
489 :
490 0 : int16_t LR11x0::setModulationParamsSigfox(uint32_t br, uint8_t sh) {
491 : // same as for LR-FHSS
492 0 : return(this->setModulationParamsLrFhss(br, sh));
493 : }
494 :
495 0 : int16_t LR11x0::setPacketParamsLoRa(uint16_t preambleLen, uint8_t hdrType, uint8_t payloadLen, uint8_t crcType, uint8_t invertIQ) {
496 : uint8_t buff[6] = {
497 0 : (uint8_t)((preambleLen >> 8) & 0xFF), (uint8_t)(preambleLen & 0xFF),
498 : hdrType, payloadLen, crcType, invertIQ
499 0 : };
500 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_PACKET_PARAMS, true, buff, sizeof(buff)));
501 : }
502 :
503 0 : int16_t LR11x0::setPacketParamsGFSK(uint16_t preambleLen, uint8_t preambleDetectorLen, uint8_t syncWordLen, uint8_t addrCmp, uint8_t packType, uint8_t payloadLen, uint8_t crcType, uint8_t whiten) {
504 : uint8_t buff[9] = {
505 0 : (uint8_t)((preambleLen >> 8) & 0xFF), (uint8_t)(preambleLen & 0xFF),
506 : preambleDetectorLen, syncWordLen, addrCmp, packType, payloadLen, crcType, whiten
507 0 : };
508 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_PACKET_PARAMS, true, buff, sizeof(buff)));
509 : }
510 :
511 0 : int16_t LR11x0::setPacketParamsSigfox(uint8_t payloadLen, uint16_t rampUpDelay, uint16_t rampDownDelay, uint16_t bitNum) {
512 : uint8_t buff[7] = {
513 0 : payloadLen, (uint8_t)((rampUpDelay >> 8) & 0xFF), (uint8_t)(rampUpDelay & 0xFF),
514 0 : (uint8_t)((rampDownDelay >> 8) & 0xFF), (uint8_t)(rampDownDelay & 0xFF),
515 0 : (uint8_t)((bitNum >> 8) & 0xFF), (uint8_t)(bitNum & 0xFF),
516 0 : };
517 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_PACKET_PARAMS, true, buff, sizeof(buff)));
518 : }
519 :
520 0 : int16_t LR11x0::setTxParams(int8_t pwr, uint8_t ramp) {
521 0 : uint8_t buff[2] = { (uint8_t)pwr, ramp };
522 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_TX_PARAMS, true, buff, sizeof(buff)));
523 : }
524 :
525 0 : int16_t LR11x0::setPacketAdrs(uint8_t node, uint8_t broadcast) {
526 0 : uint8_t buff[2] = { node, broadcast };
527 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_PACKET_ADRS, true, buff, sizeof(buff)));
528 : }
529 :
530 0 : int16_t LR11x0::setRxTxFallbackMode(uint8_t mode) {
531 0 : uint8_t buff[1] = { mode };
532 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_RX_TX_FALLBACK_MODE, true, buff, sizeof(buff)));
533 : }
534 :
535 0 : int16_t LR11x0::setRxDutyCycle(uint32_t rxPeriod, uint32_t sleepPeriod, uint8_t mode) {
536 : uint8_t buff[7] = {
537 0 : (uint8_t)((rxPeriod >> 16) & 0xFF), (uint8_t)((rxPeriod >> 8) & 0xFF), (uint8_t)(rxPeriod & 0xFF),
538 0 : (uint8_t)((sleepPeriod >> 16) & 0xFF), (uint8_t)((sleepPeriod >> 8) & 0xFF), (uint8_t)(sleepPeriod & 0xFF),
539 : mode
540 0 : };
541 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_RX_DUTY_CYCLE, true, buff, sizeof(buff)));
542 : }
543 :
544 3 : int16_t LR11x0::setPaConfig(uint8_t paSel, uint8_t regPaSupply, uint8_t paDutyCycle, uint8_t paHpSel) {
545 3 : uint8_t buff[4] = { paSel, regPaSupply, paDutyCycle, paHpSel };
546 6 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_PA_CONFIG, true, buff, sizeof(buff)));
547 : }
548 :
549 0 : int16_t LR11x0::stopTimeoutOnPreamble(bool stop) {
550 0 : uint8_t buff[1] = { (uint8_t)stop };
551 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_STOP_TIMEOUT_ON_PREAMBLE, true, buff, sizeof(buff)));
552 : }
553 :
554 0 : int16_t LR11x0::setCad(void) {
555 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_CAD, true, NULL, 0));
556 : }
557 :
558 3 : int16_t LR11x0::setTxCw(void) {
559 3 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_TX_CW, true, NULL, 0));
560 : }
561 :
562 0 : int16_t LR11x0::setTxInfinitePreamble(void) {
563 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_TX_INFINITE_PREAMBLE, true, NULL, 0));
564 : }
565 :
566 0 : int16_t LR11x0::setLoRaSynchTimeout(uint8_t symbolNum) {
567 0 : uint8_t buff[1] = { symbolNum };
568 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_LORA_SYNCH_TIMEOUT, true, buff, sizeof(buff)));
569 : }
570 :
571 0 : int16_t LR11x0::setRangingAddr(uint32_t addr, uint8_t checkLen) {
572 : uint8_t buff[5] = {
573 0 : (uint8_t)((addr >> 24) & 0xFF), (uint8_t)((addr >> 16) & 0xFF),
574 0 : (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF), checkLen
575 0 : };
576 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_RANGING_ADDR, true, buff, sizeof(buff)));
577 : }
578 :
579 0 : int16_t LR11x0::setRangingReqAddr(uint32_t addr) {
580 0 : return(this->setU32(RADIOLIB_LR11X0_CMD_SET_RANGING_REQ_ADDR, addr));
581 : }
582 :
583 0 : int16_t LR11x0::getRangingResult(uint8_t type, float* res) {
584 0 : uint8_t reqBuff[1] = { type };
585 0 : uint8_t rplBuff[4] = { 0 };
586 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_RANGING_RESULT, false, rplBuff, sizeof(rplBuff), reqBuff, sizeof(reqBuff));
587 0 : RADIOLIB_ASSERT(state);
588 :
589 0 : if(res) {
590 0 : if(type == RADIOLIB_LR11X0_RANGING_RESULT_DISTANCE) {
591 0 : uint32_t raw = ((uint32_t)(rplBuff[0]) << 24) | ((uint32_t)(rplBuff[1]) << 16) | ((uint32_t)(rplBuff[2]) << 8) | (uint32_t)rplBuff[3];
592 0 : *res = ((float)(raw*3e8))/((float)(4096*this->bandwidthKhz*1000));
593 : } else {
594 0 : *res = (float)rplBuff[3]/2.0f;
595 : }
596 : }
597 :
598 0 : return(state);
599 : }
600 :
601 0 : int16_t LR11x0::setRangingTxRxDelay(uint32_t delay) {
602 0 : return(this->setU32(RADIOLIB_LR11X0_CMD_SET_RANGING_TX_RX_DELAY, delay));
603 : }
604 :
605 0 : int16_t LR11x0::setGfskCrcParams(uint32_t init, uint32_t poly) {
606 : uint8_t buff[8] = {
607 0 : (uint8_t)((init >> 24) & 0xFF), (uint8_t)((init >> 16) & 0xFF),
608 0 : (uint8_t)((init >> 8) & 0xFF), (uint8_t)(init & 0xFF),
609 0 : (uint8_t)((poly >> 24) & 0xFF), (uint8_t)((poly >> 16) & 0xFF),
610 0 : (uint8_t)((poly >> 8) & 0xFF), (uint8_t)(poly & 0xFF)
611 0 : };
612 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_GFSK_CRC_PARAMS, true, buff, sizeof(buff)));
613 :
614 : }
615 :
616 0 : int16_t LR11x0::setGfskWhitParams(uint16_t seed) {
617 : uint8_t buff[2] = {
618 0 : (uint8_t)((seed >> 8) & 0xFF), (uint8_t)(seed & 0xFF)
619 0 : };
620 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_GFSK_WHIT_PARAMS, true, buff, sizeof(buff)));
621 : }
622 :
623 0 : int16_t LR11x0::setRangingParameter(uint8_t symbolNum) {
624 : // the first byte is reserved
625 0 : uint8_t buff[2] = { 0x00, symbolNum };
626 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_RANGING_PARAMETER, true, buff, sizeof(buff)));
627 : }
628 :
629 0 : int16_t LR11x0::setRssiCalibration(const int8_t* tune, int16_t gainOffset) {
630 : uint8_t buff[11] = {
631 0 : (uint8_t)((tune[0] & 0x0F) | (uint8_t)(tune[1] & 0x0F) << 4),
632 0 : (uint8_t)((tune[2] & 0x0F) | (uint8_t)(tune[3] & 0x0F) << 4),
633 0 : (uint8_t)((tune[4] & 0x0F) | (uint8_t)(tune[5] & 0x0F) << 4),
634 0 : (uint8_t)((tune[6] & 0x0F) | (uint8_t)(tune[7] & 0x0F) << 4),
635 0 : (uint8_t)((tune[8] & 0x0F) | (uint8_t)(tune[9] & 0x0F) << 4),
636 0 : (uint8_t)((tune[10] & 0x0F) | (uint8_t)(tune[11] & 0x0F) << 4),
637 0 : (uint8_t)((tune[12] & 0x0F) | (uint8_t)(tune[13] & 0x0F) << 4),
638 0 : (uint8_t)((tune[14] & 0x0F) | (uint8_t)(tune[15] & 0x0F) << 4),
639 0 : (uint8_t)((tune[16] & 0x0F) | (uint8_t)(tune[17] & 0x0F) << 4),
640 0 : (uint8_t)(((uint16_t)gainOffset >> 8) & 0xFF), (uint8_t)(gainOffset & 0xFF),
641 0 : };
642 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_RSSI_CALIBRATION, true, buff, sizeof(buff)));
643 : }
644 :
645 0 : int16_t LR11x0::setLoRaSyncWord(uint8_t sync) {
646 0 : uint8_t buff[1] = { sync };
647 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_LORA_SYNC_WORD, true, buff, sizeof(buff)));
648 : }
649 :
650 0 : int16_t LR11x0::lrFhssSetSyncWord(uint32_t sync) {
651 0 : return(this->setU32(RADIOLIB_LR11X0_CMD_LR_FHSS_SET_SYNC_WORD, sync));
652 : }
653 :
654 0 : int16_t LR11x0::configBleBeacon(uint8_t chan, const uint8_t* payload, size_t len) {
655 0 : return(this->bleBeaconCommon(RADIOLIB_LR11X0_CMD_CONFIG_BLE_BEACON, chan, payload, len));
656 : }
657 :
658 0 : int16_t LR11x0::getLoRaRxHeaderInfo(uint8_t* cr, bool* hasCRC) {
659 : // check if in explicit header mode
660 0 : if(this->headerType == RADIOLIB_LRXXXX_LORA_HEADER_IMPLICIT) {
661 0 : return(RADIOLIB_ERR_WRONG_MODEM);
662 : }
663 :
664 0 : uint8_t buff[1] = { 0 };
665 0 : int16_t state = this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_LORA_RX_HEADER_INFOS, false, buff, sizeof(buff));
666 :
667 : // pass the replies
668 0 : if(cr) { *cr = (buff[0] & 0x70) >> 4; }
669 0 : if(hasCRC) { *hasCRC = (buff[0] & RADIOLIB_LR11X0_LAST_HEADER_CRC_ENABLED) != 0; }
670 :
671 0 : return(state);
672 : }
673 :
674 0 : int16_t LR11x0::bleBeaconSend(uint8_t chan, const uint8_t* payload, size_t len) {
675 0 : return(this->bleBeaconCommon(RADIOLIB_LR11X0_CMD_BLE_BEACON_SEND, chan, payload, len));
676 : }
677 :
678 0 : int16_t LR11x0::bleBeaconCommon(uint16_t cmd, uint8_t chan, const uint8_t* payload, size_t len) {
679 : // check maximum size
680 : // TODO what is the actual maximum?
681 0 : if(len > RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN) {
682 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
683 : }
684 :
685 : // build buffers
686 : #if RADIOLIB_STATIC_ONLY
687 : uint8_t dataBuff[sizeof(uint8_t) + RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN];
688 : #else
689 0 : uint8_t* dataBuff = new uint8_t[sizeof(uint8_t) + len];
690 : #endif
691 :
692 : // set the channel
693 0 : dataBuff[0] = chan;
694 0 : memcpy(&dataBuff[1], payload, len);
695 :
696 0 : int16_t state = this->SPIcommand(cmd, true, dataBuff, sizeof(uint8_t) + len);
697 : #if !RADIOLIB_STATIC_ONLY
698 0 : delete[] dataBuff;
699 : #endif
700 0 : return(state);
701 : }
702 :
703 0 : int16_t LR11x0::bootEraseFlash(void) {
704 : // erasing flash takes about 2.5 seconds, temporarily tset SPI timeout to 3 seconds
705 0 : RadioLibTime_t timeout = this->mod->spiConfig.timeout;
706 0 : this->mod->spiConfig.timeout = 3000;
707 0 : int16_t state = this->mod->SPIwriteStream(RADIOLIB_LR11X0_CMD_BOOT_ERASE_FLASH, NULL, 0, false, false);
708 0 : this->mod->spiConfig.timeout = timeout;
709 0 : return(state);
710 : }
711 :
712 0 : int16_t LR11x0::bootWriteFlashEncrypted(uint32_t offset, const uint32_t* data, size_t len, bool nonvolatile) {
713 : // check maximum size
714 0 : if(len > (RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) {
715 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
716 : }
717 0 : return(this->writeCommon(RADIOLIB_LR11X0_CMD_BOOT_WRITE_FLASH_ENCRYPTED, offset, data, len, nonvolatile));
718 : }
719 :
720 0 : int16_t LR11x0::bootGetHash(uint8_t hash[RADIOLIB_LR11X0_HASH_LEN]) {
721 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_GET_HASH, false, hash, RADIOLIB_LR11X0_HASH_LEN));
722 : }
723 :
724 0 : int16_t LR11x0::bootReboot(bool stay) {
725 0 : uint8_t buff[1] = { (uint8_t)stay };
726 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_REBOOT, true, buff, sizeof(buff)));
727 : }
728 :
729 0 : int16_t LR11x0::bootGetPin(uint8_t* pin) {
730 0 : RADIOLIB_ASSERT_PTR(pin);
731 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_GET_PIN, false, pin, RADIOLIB_LR11X0_PIN_LEN));
732 : }
733 :
734 0 : int16_t LR11x0::bootGetChipEui(uint8_t* eui) {
735 0 : RADIOLIB_ASSERT_PTR(eui);
736 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_GET_CHIP_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN));
737 : }
738 :
739 0 : int16_t LR11x0::bootGetJoinEui(uint8_t* eui) {
740 0 : RADIOLIB_ASSERT_PTR(eui);
741 0 : return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_GET_JOIN_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN));
742 : }
743 :
744 : #endif
|