Skip to content

Commit 315f4cf

Browse files
committed
Implement bad CRC triggers warning and skip packet if desired.
1 parent 8eb678a commit 315f4cf

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

adafruit_rfm9x.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
import digitalio
3434
from micropython import const
3535

36+
try:
37+
from warnings import warn
38+
except ImportError:
39+
def warn(msg, cat=None, stacklevel=1):
40+
print("%s: %s" % ("Warning" if cat is None else cat.__name__, msg))
41+
3642
import adafruit_bus_device.spi_device as spidev
3743

3844

@@ -335,6 +341,8 @@ def __set__(self, obj, val):
335341

336342
rx_done = _RegisterBits(_RH_RF95_REG_12_IRQ_FLAGS, offset=6, bits=1)
337343

344+
crc_error = _RegisterBits(_RH_RF95_REG_12_IRQ_FLAGS, offset=5, bits=1)
345+
338346
def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
339347
high_power=True, baudrate=5000000, signal_bandwidth=125000,
340348
coding_rate=5, spreading_factor=7, enable_crc=False):
@@ -403,7 +411,10 @@ def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
403411
# Optionally enable CRC checking on incoming packets.
404412
if enable_crc:
405413
config = self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) | 0x04
406-
self._write_u8(_RH_RF95_REG_1E_MODEM_CONFIG2, config)
414+
else:
415+
config = self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0xfb
416+
self._write_u8(_RH_RF95_REG_1E_MODEM_CONFIG2, config)
417+
self.enable_crc = enable_crc
407418
# Note no sync word is set for LoRa mode either!
408419
self._write_u8(_RH_RF95_REG_26_MODEM_CONFIG3, 0x00) # Preamble lsb?
409420
# Set preamble length (default 8 bytes to match radiohead).
@@ -651,25 +662,28 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False,
651662
# Payload ready is set, a packet is in the FIFO.
652663
packet = None
653664
if not timed_out:
654-
# Grab the length of the received packet and check it has at least 5
655-
# bytes to indicate the 4 byte header and at least 1 byte of user data.
656-
length = self._read_u8(_RH_RF95_REG_13_RX_NB_BYTES)
657-
if length < 5:
658-
packet = None
665+
if self.enable_crc and self.crc_error:
666+
warn("CRC error, packet ignored")
659667
else:
660-
# Have a good packet, grab it from the FIFO.
661-
# Reset the fifo read ptr to the beginning of the packet.
662-
current_addr = self._read_u8(_RH_RF95_REG_10_FIFO_RX_CURRENT_ADDR)
663-
self._write_u8(_RH_RF95_REG_0D_FIFO_ADDR_PTR, current_addr)
664-
packet = bytearray(length)
665-
# Read the packet.
666-
self._read_into(_RH_RF95_REG_00_FIFO, packet)
667-
if (rx_filter != _RH_BROADCAST_ADDRESS and packet[0] != _RH_BROADCAST_ADDRESS
668-
and packet[0] != rx_filter):
668+
# Grab the length of the received packet and check it has at least 5
669+
# bytes to indicate the 4 byte header and at least 1 byte of user data.
670+
length = self._read_u8(_RH_RF95_REG_13_RX_NB_BYTES)
671+
if length < 5:
669672
packet = None
670-
elif not with_header: # skip the header if not wanted
671-
packet = packet[4:]
672-
# Listen again if necessary and return the result packet.
673+
else:
674+
# Have a good packet, grab it from the FIFO.
675+
# Reset the fifo read ptr to the beginning of the packet.
676+
current_addr = self._read_u8(_RH_RF95_REG_10_FIFO_RX_CURRENT_ADDR)
677+
self._write_u8(_RH_RF95_REG_0D_FIFO_ADDR_PTR, current_addr)
678+
packet = bytearray(length)
679+
# Read the packet.
680+
self._read_into(_RH_RF95_REG_00_FIFO, packet)
681+
if (rx_filter != _RH_BROADCAST_ADDRESS and packet[0] != _RH_BROADCAST_ADDRESS
682+
and packet[0] != rx_filter):
683+
packet = None
684+
elif not with_header: # skip the header if not wanted
685+
packet = packet[4:]
686+
# Listen again if necessary and return the result packet.
673687
if keep_listening:
674688
self.listen()
675689
else:

0 commit comments

Comments
 (0)