From f16f8e43f46a69591701ee7e7bc4fb34fc1bfb79 Mon Sep 17 00:00:00 2001
From: jgromes <jgromes@users.noreply.github.com>
Date: Thu, 18 Feb 2021 18:48:01 +0100
Subject: [PATCH] [RFM9x] Fixed frequency setting not saved (#253)

---
 src/modules/RFM9x/RFM95.cpp | 8 ++++++--
 src/modules/RFM9x/RFM96.cpp | 8 ++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/modules/RFM9x/RFM95.cpp b/src/modules/RFM9x/RFM95.cpp
index 3e7d1ad1..69538731 100644
--- a/src/modules/RFM9x/RFM95.cpp
+++ b/src/modules/RFM9x/RFM95.cpp
@@ -43,8 +43,12 @@ int16_t RFM95::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncW
 int16_t RFM95::setFrequency(float freq) {
   RADIOLIB_CHECK_RANGE(freq, 862.0, 1020.0, ERR_INVALID_FREQUENCY);
 
-  // set frequency
-  return(SX127x::setFrequencyRaw(freq));
+  // set frequency and if successful, save the new setting
+  int16_t state = SX127x::setFrequencyRaw(freq);
+  if(state == ERR_NONE) {
+    SX127x::_freq = freq;
+  }
+  return(state);
 }
 
 #endif
diff --git a/src/modules/RFM9x/RFM96.cpp b/src/modules/RFM9x/RFM96.cpp
index 1403b3dc..ff937eb8 100644
--- a/src/modules/RFM9x/RFM96.cpp
+++ b/src/modules/RFM9x/RFM96.cpp
@@ -44,8 +44,12 @@ int16_t RFM96::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncW
 int16_t RFM96::setFrequency(float freq) {
   RADIOLIB_CHECK_RANGE(freq, 410.0, 525.0, ERR_INVALID_FREQUENCY);
 
-  // set frequency
-  return(SX127x::setFrequencyRaw(freq));
+  // set frequency and if successful, save the new setting
+  int16_t state = SX127x::setFrequencyRaw(freq);
+  if(state == ERR_NONE) {
+    SX127x::_freq = freq;
+  }
+  return(state);
 }
 
 #endif