diff --git a/API.md b/API.md index 2d86815..91dba2c 100644 --- a/API.md +++ b/API.md @@ -363,6 +363,17 @@ LoRa.enableInvertIQ(); LoRa.disableInvertIQ(); ``` +### LNA Gain + +Set LNA Gain for better RX sensitivity, by default AGC (Automatic Gain Control) is used and LNA gain is not used. + +```arduino +LoRa.setGain(gain); +``` + + * `gain` - LNA gain + +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. ## Other functions diff --git a/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino b/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino index e227e23..ed554d9 100644 --- a/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino +++ b/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino @@ -1,43 +1,45 @@ -#include -#include - -#ifdef ARDUINO_SAMD_MKRWAN1300 -#error "This example is not compatible with the Arduino MKR WAN 1300 board!" -#endif - -void setup() { - Serial.begin(9600); - while (!Serial); - - Serial.println("LoRa Receiver Callback"); - - if (!LoRa.begin(915E6)) { - Serial.println("Starting LoRa failed!"); - while (1); - } - - // register the receive callback - LoRa.onReceive(onReceive); - - // put the radio into receive mode - LoRa.receive(); -} - -void loop() { - // do nothing -} - -void onReceive(int packetSize) { - // received a packet - Serial.print("Received packet '"); - - // read packet - for (int i = 0; i < packetSize; i++) { - Serial.print((char)LoRa.read()); - } - - // print RSSI of packet - Serial.print("' with RSSI "); - Serial.println(LoRa.packetRssi()); -} - +#include +#include + +#ifdef ARDUINO_SAMD_MKRWAN1300 +#error "This example is not compatible with the Arduino MKR WAN 1300 board!" +#endif + +void setup() { + Serial.begin(9600); + while (!Serial); + + Serial.println("LoRa Receiver Callback"); + + if (!LoRa.begin(915E6)) { + Serial.println("Starting LoRa failed!"); + while (1); + } + + // Uncomment the next line to disable the default AGC and set LNA gain, values between 1 - 6 are supported + // LoRa.setGain(6); + + // register the receive callback + LoRa.onReceive(onReceive); + + // put the radio into receive mode + LoRa.receive(); +} + +void loop() { + // do nothing +} + +void onReceive(int packetSize) { + // received a packet + Serial.print("Received packet '"); + + // read packet + for (int i = 0; i < packetSize; i++) { + Serial.print((char)LoRa.read()); + } + + // print RSSI of packet + Serial.print("' with RSSI "); + Serial.println(LoRa.packetRssi()); +} diff --git a/keywords.txt b/keywords.txt index 63e0e9a..986d066 100644 --- a/keywords.txt +++ b/keywords.txt @@ -47,6 +47,7 @@ enableCrc KEYWORD2 disableCrc KEYWORD2 enableInvertIQ KEYWORD2 disableInvertIQ KEYWORD2 +setGain KEYWORD2 random KEYWORD2 setPins KEYWORD2 diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 4396518..280aaa4 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -607,6 +607,32 @@ void LoRaClass::setOCP(uint8_t mA) writeRegister(REG_OCP, 0x20 | (0x1F & ocpTrim)); } +void LoRaClass::setGain(uint8_t gain) +{ + // check allowed range + if (gain > 6) { + gain = 6; + } + + // set to standby + idle(); + + // set gain + if (gain == 0) { + // if gain = 0, enable AGC + writeRegister(REG_MODEM_CONFIG_3, 0x04); + } else { + // disable AGC + writeRegister(REG_MODEM_CONFIG_3, 0x00); + + // clear Gain and set LNA boost + writeRegister(REG_LNA, 0x03); + + // set gain + writeRegister(REG_LNA, readRegister(REG_LNA) | (gain << 5)); + } +} + byte LoRaClass::random() { return readRegister(REG_RSSI_WIDEBAND); diff --git a/src/LoRa.h b/src/LoRa.h index c1671c1..6f1ccb0 100644 --- a/src/LoRa.h +++ b/src/LoRa.h @@ -77,6 +77,8 @@ public: void disableInvertIQ(); void setOCP(uint8_t mA); // Over Current Protection control + + void setGain(uint8_t gain); // Set LNA gain // deprecated void crc() { enableCrc(); }