Skip to content

Commit 934fe8b

Browse files
committed
Merge branch 'master' into w-ockham-cad
2 parents 1f2e4f8 + 55d49c5 commit 934fe8b

File tree

9 files changed

+123
-60
lines changed

9 files changed

+123
-60
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: [sandeepmistry]

API.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Initialize the library with the specified frequency.
1515
```arduino
1616
LoRa.begin(frequency);
1717
```
18-
* `frequency` - frequency in Hz (`433E6`, `866E6`, `915E6`)
18+
* `frequency` - frequency in Hz (`433E6`, `868E6`, `915E6`)
1919

2020
Returns `1` on success, `0` on failure.
2121

@@ -194,7 +194,7 @@ The `onReceive` callback will be called when a packet is received.
194194
int rssi = LoRa.packetRssi();
195195
```
196196

197-
Returns the RSSI of the received packet.
197+
Returns the averaged RSSI of the last received packet (dBm).
198198

199199
### Packet SNR
200200

@@ -204,6 +204,14 @@ float snr = LoRa.packetSnr();
204204

205205
Returns the estimated SNR of the received packet in dB.
206206

207+
## RSSI
208+
209+
```arduino
210+
int rssi = LoRa.rssi();
211+
```
212+
213+
Returns the current RSSI of the radio (dBm). RSSI can be read at any time (during packet reception or not)
214+
207215
### Packet Frequency Error
208216

209217
```arduino
@@ -306,7 +314,7 @@ Change the frequency of the radio.
306314
```arduino
307315
LoRa.setFrequency(frequency);
308316
```
309-
* `frequency` - frequency in Hz (`433E6`, `866E6`, `915E6`)
317+
* `frequency` - frequency in Hz (`433E6`, `868E6`, `915E6`)
310318

311319
### Spreading Factor
312320

@@ -329,7 +337,7 @@ LoRa.setSignalBandwidth(signalBandwidth);
329337

330338
* `signalBandwidth` - signal bandwidth in Hz, defaults to `125E3`.
331339

332-
Supported values are `7.8E3`, `10.4E3`, `15.6E3`, `20.8E3`, `31.25E3`, `41.7E3`, `62.5E3`, `125E3`, and `250E3`.
340+
Supported values are `7.8E3`, `10.4E3`, `15.6E3`, `20.8E3`, `31.25E3`, `41.7E3`, `62.5E3`, `125E3`, `250E3`, and `500E3`.
333341

334342
### Coding Rate
335343

@@ -384,6 +392,17 @@ LoRa.enableInvertIQ();
384392
385393
LoRa.disableInvertIQ();
386394
```
395+
### LNA Gain
396+
397+
Set LNA Gain for better RX sensitivity, by default AGC (Automatic Gain Control) is used and LNA gain is not used.
398+
399+
```arduino
400+
LoRa.setGain(gain);
401+
```
402+
403+
* `gain` - LNA gain
404+
405+
Supported values are between `0` and `6`. If gain is 0, AGC will be enabled and LNA gain will not be used. Else if gain is from 1 to 6, AGC will be disabled and LNA gain will be used.
387406

388407
## Other functions
389408

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ An [Arduino](https://arduino.cc/) library for sending and receiving data using [
66

77
## Compatible Hardware
88

9-
* [Semtech SX1276/77/78/79](http://www.semtech.com/apps/product.php?pn=SX1276) based boards including:
10-
* [Dragino Lora Shield](http://www.dragino.com/products/module/item/102-lora-shield.html)
11-
* [HopeRF](http://www.hoperf.com/rf_transceiver/lora/) [RFM95W](http://www.hoperf.com/rf_transceiver/lora/RFM95W.html), [RFM96W](http://www.hoperf.com/rf_transceiver/lora/RFM96W.html), and [RFM98W](http://www.hoperf.com/rf_transceiver/lora/RFM98W.html)
9+
* [Semtech SX1276/77/78/79](https://www.semtech.com/apps/product.php?pn=SX1276) based boards including:
10+
* [Dragino Lora Shield](https://www.dragino.com/products/lora/item/102-lora-shield.html)
11+
* [HopeRF](https://www.hoperf.com/modules/lora/index.html) [RFM95W](https://www.hoperf.com/modules/lora/RFM95.html), [RFM96W](https://www.hoperf.com/modules/lora/RFM96.html), and [RFM98W](https://www.hoperf.com/modules/lora/RFM98.html)
1212
* [Modtronix](http://modtronix.com/) [inAir4](http://modtronix.com/inair4.html), [inAir9](http://modtronix.com/inair9.html), and [inAir9B](http://modtronix.com/inair9b.html)
1313
* [Arduino MKR WAN 1300](https://store.arduino.cc/usa/mkr-wan-1300)
1414
* **NOTE:** Requires firmware v1.1.6 or later on the on-board Murata module. Please use the [MKRWANFWUpdate_standalone example](https://github.com/arduino-libraries/MKRWAN/blob/master/examples/MKRWANFWUpdate_standalone/MKRWANFWUpdate_standalone.ino) from latest [MKRWAN library](https://github.com/arduino-libraries/MKRWAN) release to update the firmware.
Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1-
#include <SPI.h>
2-
#include <LoRa.h>
3-
4-
#ifdef ARDUINO_SAMD_MKRWAN1300
5-
#error "This example is not compatible with the Arduino MKR WAN 1300 board!"
6-
#endif
7-
8-
void setup() {
9-
Serial.begin(9600);
10-
while (!Serial);
11-
12-
Serial.println("LoRa Receiver Callback");
13-
14-
if (!LoRa.begin(915E6)) {
15-
Serial.println("Starting LoRa failed!");
16-
while (1);
17-
}
18-
19-
// register the receive callback
20-
LoRa.onReceive(onReceive);
21-
22-
// put the radio into receive mode
23-
LoRa.receive();
24-
}
25-
26-
void loop() {
27-
// do nothing
28-
}
29-
30-
void onReceive(int packetSize) {
31-
// received a packet
32-
Serial.print("Received packet '");
33-
34-
// read packet
35-
for (int i = 0; i < packetSize; i++) {
36-
Serial.print((char)LoRa.read());
37-
}
38-
39-
// print RSSI of packet
40-
Serial.print("' with RSSI ");
41-
Serial.println(LoRa.packetRssi());
42-
}
43-
1+
#include <SPI.h>
2+
#include <LoRa.h>
3+
4+
#ifdef ARDUINO_SAMD_MKRWAN1300
5+
#error "This example is not compatible with the Arduino MKR WAN 1300 board!"
6+
#endif
7+
8+
void setup() {
9+
Serial.begin(9600);
10+
while (!Serial);
11+
12+
Serial.println("LoRa Receiver Callback");
13+
14+
if (!LoRa.begin(915E6)) {
15+
Serial.println("Starting LoRa failed!");
16+
while (1);
17+
}
18+
19+
// Uncomment the next line to disable the default AGC and set LNA gain, values between 1 - 6 are supported
20+
// LoRa.setGain(6);
21+
22+
// register the receive callback
23+
LoRa.onReceive(onReceive);
24+
25+
// put the radio into receive mode
26+
LoRa.receive();
27+
}
28+
29+
void loop() {
30+
// do nothing
31+
}
32+
33+
void onReceive(int packetSize) {
34+
// received a packet
35+
Serial.print("Received packet '");
36+
37+
// read packet
38+
for (int i = 0; i < packetSize; i++) {
39+
Serial.print((char)LoRa.read());
40+
}
41+
42+
// print RSSI of packet
43+
Serial.print("' with RSSI ");
44+
Serial.println(LoRa.packetRssi());
45+
}

examples/LoRaSenderNonBlockingCallback/LoRaSenderNonBlockingCallback.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void setup() {
1818
}
1919

2020
void loop() {
21-
if (runEvery(1000)) { // repeat every 1000 millis
21+
if (runEvery(5000)) { // repeat every 5000 millis
2222

2323
Serial.print("Sending packet non-blocking: ");
2424
Serial.println(counter);

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ packetRssi KEYWORD2
2323
packetSnr KEYWORD2
2424
packetFrequencyError KEYWORD2
2525

26+
rssi KEYWORD2
27+
2628
write KEYWORD2
2729

2830
available KEYWORD2
@@ -49,6 +51,7 @@ enableCrc KEYWORD2
4951
disableCrc KEYWORD2
5052
enableInvertIQ KEYWORD2
5153
disableInvertIQ KEYWORD2
54+
setGain KEYWORD2
5255

5356
random KEYWORD2
5457
setPins KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=LoRa
2-
version=0.7.0
2+
version=0.8.0
33
author=Sandeep Mistry <[email protected]>
44
maintainer=Sandeep Mistry <[email protected]>
55
sentence=An Arduino library for sending and receiving data using LoRa radios.

src/LoRa.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define REG_RX_NB_BYTES 0x13
2121
#define REG_PKT_SNR_VALUE 0x19
2222
#define REG_PKT_RSSI_VALUE 0x1a
23+
#define REG_RSSI_VALUE 0x1b
2324
#define REG_MODEM_CONFIG_1 0x1d
2425
#define REG_MODEM_CONFIG_2 0x1e
2526
#define REG_PREAMBLE_MSB 0x20
@@ -58,6 +59,10 @@
5859
#define IRQ_CAD_DONE_MASK 0x04
5960
#define IRQ_CAD_DETECTED_MASK 0x01
6061

62+
#define RF_MID_BAND_THRESHOLD 525E6
63+
#define RSSI_OFFSET_HF_PORT 157
64+
#define RSSI_OFFSET_LF_PORT 164
65+
6166
#define MAX_PKT_LENGTH 255
6267

6368
#if (ESP8266 || ESP32)
@@ -262,7 +267,7 @@ int LoRaClass::parsePacket(int size)
262267

263268
int LoRaClass::packetRssi()
264269
{
265-
return (readRegister(REG_PKT_RSSI_VALUE) - (_frequency < 868E6 ? 164 : 157));
270+
return (readRegister(REG_PKT_RSSI_VALUE) - (_frequency < RF_MID_BAND_THRESHOLD ? RSSI_OFFSET_LF_PORT : RSSI_OFFSET_HF_PORT));
266271
}
267272

268273
float LoRaClass::packetSnr()
@@ -273,14 +278,14 @@ float LoRaClass::packetSnr()
273278
long LoRaClass::packetFrequencyError()
274279
{
275280
int32_t freqError = 0;
276-
freqError = static_cast<int32_t>(readRegister(REG_FREQ_ERROR_MSB) & B111);
281+
freqError = static_cast<int32_t>(readRegister(REG_FREQ_ERROR_MSB) & 0b111);
277282
freqError <<= 8L;
278283
freqError += static_cast<int32_t>(readRegister(REG_FREQ_ERROR_MID));
279284
freqError <<= 8L;
280285
freqError += static_cast<int32_t>(readRegister(REG_FREQ_ERROR_LSB));
281286

282-
if (readRegister(REG_FREQ_ERROR_MSB) & B1000) { // Sign bit is on
283-
freqError -= 524288; // B1000'0000'0000'0000'0000
287+
if (readRegister(REG_FREQ_ERROR_MSB) & 0b1000) { // Sign bit is on
288+
freqError -= 524288; // 0b1000'0000'0000'0000'0000
284289
}
285290

286291
const float fXtal = 32E6; // FXOSC: crystal oscillator (XTAL) frequency (2.5. Chip Specification, p. 14)
@@ -289,6 +294,11 @@ long LoRaClass::packetFrequencyError()
289294
return static_cast<long>(fError);
290295
}
291296

297+
int LoRaClass::rssi()
298+
{
299+
return (readRegister(REG_RSSI_VALUE) - (_frequency < RF_MID_BAND_THRESHOLD ? RSSI_OFFSET_LF_PORT : RSSI_OFFSET_HF_PORT));
300+
}
301+
292302
size_t LoRaClass::write(uint8_t byte)
293303
{
294304
return write(&byte, sizeof(byte));
@@ -635,6 +645,32 @@ void LoRaClass::setOCP(uint8_t mA)
635645
writeRegister(REG_OCP, 0x20 | (0x1F & ocpTrim));
636646
}
637647

648+
void LoRaClass::setGain(uint8_t gain)
649+
{
650+
// check allowed range
651+
if (gain > 6) {
652+
gain = 6;
653+
}
654+
655+
// set to standby
656+
idle();
657+
658+
// set gain
659+
if (gain == 0) {
660+
// if gain = 0, enable AGC
661+
writeRegister(REG_MODEM_CONFIG_3, 0x04);
662+
} else {
663+
// disable AGC
664+
writeRegister(REG_MODEM_CONFIG_3, 0x00);
665+
666+
// clear Gain and set LNA boost
667+
writeRegister(REG_LNA, 0x03);
668+
669+
// set gain
670+
writeRegister(REG_LNA, readRegister(REG_LNA) | (gain << 5));
671+
}
672+
}
673+
638674
byte LoRaClass::random()
639675
{
640676
return readRegister(REG_RSSI_WIDEBAND);
@@ -707,10 +743,6 @@ void LoRaClass::handleDio0Rise()
707743
if (_onReceive) {
708744
_onReceive(packetLength);
709745
}
710-
711-
// reset FIFO address
712-
writeRegister(REG_FIFO_ADDR_PTR, 0);
713-
714746
} else if ((irqFlags & IRQ_TX_DONE_MASK) != 0) {
715747
if (_onTxDone) {
716748
_onTxDone();

src/LoRa.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class LoRaClass : public Stream {
4545
float packetSnr();
4646
long packetFrequencyError();
4747

48+
int rssi();
49+
4850
// from Print
4951
virtual size_t write(uint8_t byte);
5052
virtual size_t write(const uint8_t *buffer, size_t size);
@@ -79,6 +81,8 @@ class LoRaClass : public Stream {
7981
void disableInvertIQ();
8082

8183
void setOCP(uint8_t mA); // Over Current Protection control
84+
85+
void setGain(uint8_t gain); // Set LNA gain
8286

8387
// deprecated
8488
void crc() { enableCrc(); }

0 commit comments

Comments
 (0)