From da0cd455c93f31fe10910c65979190feffb4c822 Mon Sep 17 00:00:00 2001 From: Robert <3704942+JasonRJ@users.noreply.github.com> Date: Wed, 23 Feb 2022 23:58:56 -0500 Subject: [PATCH] Add method to support SX128x Manual Gain Mode. --- src/modules/SX128x/SX128x.cpp | 33 +++++++++++++++++++++++++++++++++ src/modules/SX128x/SX128x.h | 9 +++++++++ 2 files changed, 42 insertions(+) diff --git a/src/modules/SX128x/SX128x.cpp b/src/modules/SX128x/SX128x.cpp index 9316eae2..a9241716 100644 --- a/src/modules/SX128x/SX128x.cpp +++ b/src/modules/SX128x/SX128x.cpp @@ -1035,6 +1035,39 @@ int16_t SX128x::setHighSensitivityMode(bool hsm) { return(0); } +int16_t SX128x::setGainControl(uint8_t gain) { + // update registers + uint8_t ManualGainSetting = 0; + int16_t state = readRegister(RADIOLIB_SX128X_REG_MANUAL_GAIN_CONTROL_ENABLE_2, &ManualGainSetting, 1); + RADIOLIB_ASSERT(state); + uint8_t LNAGainValue = 0; + state = readRegister(RADIOLIB_SX128X_REG_MANUAL_GAIN_SETTING, &LNAGainValue, 1); + RADIOLIB_ASSERT(state); + uint8_t LNAGainControl = 0; + state = readRegister(RADIOLIB_SX128X_REG_MANUAL_GAIN_CONTROL_ENABLE_1, &LNAGainControl, 1); + RADIOLIB_ASSERT(state); + if (gain > 0 && gain < 14) { + // Set manual gain + ManualGainSetting &= ~0x01; // Set bit 0 to 0 (Enable Manual Gain Control) + LNAGainValue &= 0xF0; // Bits 0, 1, 2 and 3 to 0 + LNAGainValue |= gain; // Set bits 0, 1, 2 and 3 to Manual Gain Setting (1-13) + LNAGainControl |= 0x80; // Set bit 7 to 1 (Enable Manual Gain Control) + } else { + // Set automatic gain if 0 or out of range + ManualGainSetting |= 0x01; // Set bit 0 to 1 (Enable Automatic Gain Control) + LNAGainValue &= 0xF0; // Bits 0, 1, 2 and 3 to 0 + LNAGainValue |= 0x0A; // Set bits 0, 1, 2 and 3 to Manual Gain Setting (1-13) + LNAGainControl = 0x4D; // Set bit 7 to 0 (Enable Automatic Gain Control) + } + state = writeRegister(RADIOLIB_SX128X_REG_MANUAL_GAIN_CONTROL_ENABLE_2, &ManualGainSetting, 1); + RADIOLIB_ASSERT(state); + state = writeRegister(RADIOLIB_SX128X_REG_MANUAL_GAIN_SETTING, &LNAGainValue, 1); + RADIOLIB_ASSERT(state); + state = writeRegister(RADIOLIB_SX128X_REG_MANUAL_GAIN_CONTROL_ENABLE_1, &LNAGainControl, 1); + RADIOLIB_ASSERT(state); + return(0); +} + float SX128x::getRSSI() { // get packet status uint8_t packetStatus[5]; diff --git a/src/modules/SX128x/SX128x.h b/src/modules/SX128x/SX128x.h index 5c18d6d1..2537fe43 100644 --- a/src/modules/SX128x/SX128x.h +++ b/src/modules/SX128x/SX128x.h @@ -722,6 +722,15 @@ class SX128x: public PhysicalLayer { */ int16_t setHighSensitivityMode(bool hsm = false); + /*! + \brief Enables or disables receiver manual gain control. + + \param Gain 0 automatic gain; 1 minimum gain to 13 maximum gain. + + \returns \ref status_codes + */ + int16_t setGainControl(uint8_t gain = 0); + /*! \brief Gets RSSI (Recorded Signal Strength Indicator) of the last received packet.