Skip to content

Commit 4696e07

Browse files
authored
Merge pull request #57 from jerryneedell/jerryn_updates
fix RSSI, implement updates for errata
2 parents 6590c91 + a375880 commit 4696e07

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

adafruit_rfm9x.py

+54-7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
# is a complex chip which requires exposing many attributes and state. Disable
127127
# the warning to work around the error.
128128
# pylint: disable=too-many-instance-attributes
129+
# pylint: disable=too-many-statements
129130

130131

131132
class RFM9x:
@@ -145,7 +146,7 @@ class RFM9x:
145146
is True for high power.
146147
- baudrate: Baud rate of the SPI connection, default is 10mhz but you might
147148
choose to lower to 1mhz if using long wires or a breadboard.
148-
149+
- agc: Boolean to Enable/Disable Automatic Gain Control - Default=False (AGC off)
149150
Remember this library makes a best effort at receiving packets with pure
150151
Python code. Trying to receive packets too quickly will result in lost data
151152
so limit yourself to simple scenarios of sending and receiving single
@@ -225,6 +226,18 @@ def __set__(self, obj, val):
225226

226227
dio0_mapping = _RegisterBits(_RH_RF95_REG_40_DIO_MAPPING1, offset=6, bits=2)
227228

229+
auto_agc = _RegisterBits(_RH_RF95_REG_26_MODEM_CONFIG3, offset=2, bits=1)
230+
231+
low_datarate_optimize = _RegisterBits(
232+
_RH_RF95_REG_26_MODEM_CONFIG3, offset=3, bits=1
233+
)
234+
235+
lna_boost_hf = _RegisterBits(_RH_RF95_REG_0C_LNA, offset=0, bits=2)
236+
237+
auto_ifon = _RegisterBits(_RH_RF95_DETECTION_OPTIMIZE, offset=7, bits=1)
238+
239+
detection_optimize = _RegisterBits(_RH_RF95_DETECTION_OPTIMIZE, offset=0, bits=3)
240+
228241
bw_bins = (7800, 10400, 15600, 20800, 31250, 41700, 62500, 125000, 250000)
229242

230243
def __init__(
@@ -236,7 +249,8 @@ def __init__(
236249
*,
237250
preamble_length=8,
238251
high_power=True,
239-
baudrate=5000000
252+
baudrate=5000000,
253+
agc=False
240254
):
241255
self.high_power = high_power
242256
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
@@ -281,14 +295,15 @@ def __init__(
281295
self.spreading_factor = 7
282296
# Default to disable CRC checking on incoming packets.
283297
self.enable_crc = False
284-
# Note no sync word is set for LoRa mode either!
285-
self._write_u8(_RH_RF95_REG_26_MODEM_CONFIG3, 0x00) # Preamble lsb?
298+
# set AGC - Default = True
299+
self.auto_agc = agc
300+
"""Automatic Gain Control state"""
286301
# Set transmit power to 13 dBm, a safe value any module supports.
287302
self.tx_power = 13
288303
# initialize last RSSI reading
289304
self.last_rssi = 0.0
290305
"""The RSSI of the last received packet. Stored when the packet was received.
291-
This instantaneous RSSI value may not be accurate once the
306+
The instantaneous RSSI value may not be accurate once the
292307
operating mode has been changed.
293308
"""
294309
# initialize timeouts and delays delays
@@ -492,7 +507,12 @@ def rssi(self):
492507
"""The received strength indicator (in dBm) of the last received message."""
493508
# Read RSSI register and convert to value using formula in datasheet.
494509
# Remember in LoRa mode the payload register changes function to RSSI!
495-
return self._read_u8(_RH_RF95_REG_1A_PKT_RSSI_VALUE) - 137
510+
raw_rssi = self._read_u8(_RH_RF95_REG_1A_PKT_RSSI_VALUE)
511+
if self.low_frequency_mode:
512+
raw_rssi -= 157
513+
else:
514+
raw_rssi -= 164
515+
return raw_rssi
496516

497517
@property
498518
def signal_bandwidth(self):
@@ -519,6 +539,28 @@ def signal_bandwidth(self, val):
519539
_RH_RF95_REG_1D_MODEM_CONFIG1,
520540
(self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0x0F) | (bw_id << 4),
521541
)
542+
if val >= 500000:
543+
# see Semtech SX1276 errata note 2.3
544+
self.auto_ifon = True
545+
# see Semtech SX1276 errata note 2.1
546+
if self.low_frequency_mode:
547+
self._write_u8(0x36, 0x02)
548+
self._write_u8(0x3A, 0x7F)
549+
else:
550+
self._write_u8(0x36, 0x02)
551+
self._write_u8(0x3A, 0x64)
552+
else:
553+
# see Semtech SX1276 errata note 2.3
554+
self.auto_ifon = False
555+
self._write_u8(0x36, 0x03)
556+
if val == 7800:
557+
self._write_u8(0x2F, 0x48)
558+
elif val >= 62500:
559+
# see Semtech SX1276 errata note 2.3
560+
self._write_u8(0x2F, 0x40)
561+
else:
562+
self._write_u8(0x2F, 0x44)
563+
self._write_u8(0x30, 0)
522564

523565
@property
524566
def coding_rate(self):
@@ -553,7 +595,12 @@ def spreading_factor(self):
553595
def spreading_factor(self, val):
554596
# Set spreading factor (set to 7 to match RadioHead Sf128).
555597
val = min(max(val, 6), 12)
556-
self._write_u8(_RH_RF95_DETECTION_OPTIMIZE, 0xC5 if val == 6 else 0xC3)
598+
599+
if val == 6:
600+
self.detection_optimize = 0x5
601+
else:
602+
self.detection_optimize = 0x3
603+
557604
self._write_u8(_RH_RF95_DETECTION_THRESHOLD, 0x0C if val == 6 else 0x0A)
558605
self._write_u8(
559606
_RH_RF95_REG_1E_MODEM_CONFIG2,

0 commit comments

Comments
 (0)