Browse Source

support for fixed packet length mode via new method `set_packet_length_mode(PacketLengthMode.FIXED)`

Fabian Peter Hammerle 3 years ago
parent
commit
22b47e56e8
3 changed files with 42 additions and 2 deletions
  1. 7 0
      CHANGELOG.md
  2. 25 2
      cc1101/__init__.py
  3. 10 0
      cc1101/options.py

+ 7 - 0
CHANGELOG.md

@@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Added
+- support for fixed packet length mode
+  via new method `set_packet_length_mode(PacketLengthMode.FIXED)`
+- new enum `options.PacketLengthMode`
+- new method `get_packet_length_mode()`
+- added configured packet length to `CC1101`'s string representation
+  (`≤n` indicates variable length mode, `=n` fixed length mode)
 
 ## [1.1.0] - 2020-12-01
 ### Added

+ 25 - 2
cc1101/__init__.py

@@ -29,7 +29,7 @@ from cc1101.addresses import (
     StatusRegisterAddress,
     FIFORegisterAddress,
 )
-from cc1101.options import SyncMode, ModulationFormat
+from cc1101.options import PacketLengthMode, SyncMode, ModulationFormat
 
 
 _LOGGER = logging.getLogger(__name__)
@@ -385,6 +385,12 @@ class CC1101:
             "symbol_rate={:.2f}kBaud".format(self.get_symbol_rate_baud() / 1000),
             "modulation_format={}".format(self.get_modulation_format().name),
             "sync_mode={}".format(self.get_sync_mode().name),
+            "packet_length{}{}".format(
+                "≤"
+                if self.get_packet_length_mode() == PacketLengthMode.VARIABLE
+                else "=",
+                self._get_packet_length(),
+            ),
         )
         return "CC1101({})".format(", ".join(attrs))
 
@@ -392,6 +398,10 @@ class CC1101:
         """
         packet length in fixed packet length mode,
         maximum packet length in variable packet length mode.
+
+        > In variable packet length mode, [...]
+        > any packet received with a length byte
+        > with a value greater than PKTLEN will be discarded.
         """
         return self._read_single_byte(ConfigurationRegisterAddress.PKTLEN)
 
@@ -456,6 +466,18 @@ class CC1101:
             start_register=ConfigurationRegisterAddress.PKTCTRL0, values=[pktctrl0]
         )
 
+    def get_packet_length_mode(self) -> PacketLengthMode:
+        pktctrl0 = self._read_single_byte(ConfigurationRegisterAddress.PKTCTRL0)
+        return PacketLengthMode(pktctrl0 & 0b11)
+
+    def set_packet_length_mode(self, mode: PacketLengthMode) -> None:
+        pktctrl0 = self._read_single_byte(ConfigurationRegisterAddress.PKTCTRL0)
+        pktctrl0 &= 0b11111100
+        pktctrl0 |= mode
+        self._write_burst(
+            start_register=ConfigurationRegisterAddress.PKTCTRL0, values=[pktctrl0]
+        )
+
     def _flush_tx_fifo_buffer(self) -> None:
         # > Only issue SFTX in IDLE or TXFIFO_UNDERFLOW states.
         _LOGGER.debug("flushing tx fifo buffer")
@@ -463,7 +485,8 @@ class CC1101:
 
     def transmit(self, payload: bytes) -> None:
         """
-        > [...], the packet length is configured by the first byte [...].
+        > In variable packet length mode [.set/get_packet_length_mode()],
+        > the packet length is configured by the first byte [...].
         > The packet length is defined as the payload data,
         > excluding the length byte and the optional CRC.
         from "15.2 Packet Format"

+ 10 - 0
cc1101/options.py

@@ -18,6 +18,16 @@
 import enum
 
 
+class PacketLengthMode(enum.IntEnum):
+    """
+    PKTCTRL0.LENGTH_CONFIG
+    """
+
+    FIXED = 0b00
+    VARIABLE = 0b01
+    # INFINITE = 0b10
+
+
 class ModulationFormat(enum.IntEnum):
     """
     MDMCFG2.MOD_FORMAT