|
33 | 33 | import digitalio
|
34 | 34 | from micropython import const
|
35 | 35 |
|
| 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 | + |
36 | 42 | import adafruit_bus_device.spi_device as spidev
|
37 | 43 |
|
38 | 44 |
|
@@ -335,6 +341,8 @@ def __set__(self, obj, val):
|
335 | 341 |
|
336 | 342 | rx_done = _RegisterBits(_RH_RF95_REG_12_IRQ_FLAGS, offset=6, bits=1)
|
337 | 343 |
|
| 344 | + crc_error = _RegisterBits(_RH_RF95_REG_12_IRQ_FLAGS, offset=5, bits=1) |
| 345 | + |
338 | 346 | def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
|
339 | 347 | high_power=True, baudrate=5000000, signal_bandwidth=125000,
|
340 | 348 | coding_rate=5, spreading_factor=7, enable_crc=False):
|
@@ -403,7 +411,10 @@ def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
|
403 | 411 | # Optionally enable CRC checking on incoming packets.
|
404 | 412 | if enable_crc:
|
405 | 413 | 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 |
407 | 418 | # Note no sync word is set for LoRa mode either!
|
408 | 419 | self._write_u8(_RH_RF95_REG_26_MODEM_CONFIG3, 0x00) # Preamble lsb?
|
409 | 420 | # 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,
|
651 | 662 | # Payload ready is set, a packet is in the FIFO.
|
652 | 663 | packet = None
|
653 | 664 | 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") |
659 | 667 | 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: |
669 | 672 | 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. |
673 | 687 | if keep_listening:
|
674 | 688 | self.listen()
|
675 | 689 | else:
|
|
0 commit comments