Browse Source

added method `get_sync_word()`

Fabian Peter Hammerle 3 years ago
parent
commit
413e5431bb
3 changed files with 37 additions and 3 deletions
  1. 3 0
      CHANGELOG.md
  2. 22 3
      cc1101/__init__.py
  3. 12 0
      tests/test_config.py

+ 3 - 0
CHANGELOG.md

@@ -5,6 +5,9 @@ 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
+- method `.get_sync_word()`
+- sync word in string representation
 
 ## [2.0.0] - 2020-12-03
 ### Changed

+ 22 - 3
cc1101/__init__.py

@@ -271,7 +271,8 @@ class CC1101:
         """
         MDMCFG2.MANCHESTER_EN
 
-        Enable manchester encoding & decoding.
+        Enable manchester encoding & decoding for the entire packet,
+        including the preamble and synchronization word.
         """
         mdmcfg2 = self._read_single_byte(ConfigurationRegisterAddress.MDMCFG2)
         mdmcfg2 |= 0b1000
@@ -405,6 +406,7 @@ class CC1101:
         )
 
     def __str__(self) -> str:
+        sync_mode = self.get_sync_mode()
         attrs = (
             "marcstate={}".format(
                 self.get_main_radio_control_state_machine_state().name.lower()
@@ -414,7 +416,10 @@ 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),
+            "sync_mode={}".format(sync_mode.name),
+            "sync_word=0x{:02x}{:02x}".format(*self.get_sync_word())
+            if sync_mode != SyncMode.NO_PREAMBLE_AND_SYNC_WORD
+            else None,
             "packet_length{}{}B".format(
                 "≤"
                 if self.get_packet_length_mode() == PacketLengthMode.VARIABLE
@@ -422,7 +427,7 @@ class CC1101:
                 self.get_packet_length_bytes(),
             ),
         )
-        return "CC1101({})".format(", ".join(attrs))
+        return "CC1101({})".format(", ".join(filter(None, attrs)))
 
     def get_configuration_register_values(
         self,
@@ -440,6 +445,20 @@ class CC1101:
             for i, v in enumerate(values)
         }
 
+    def get_sync_word(self) -> bytes:
+        """
+        SYNC1 & SYNC0
+
+        See "15.2 Packet Format"
+
+        The first byte's most significant bit is transmitted first.
+        """
+        return bytes(
+            self._read_burst(
+                start_register=ConfigurationRegisterAddress.SYNC1, length=2
+            )
+        )
+
     def get_packet_length_bytes(self) -> int:
         """
         PKTLEN

+ 12 - 0
tests/test_config.py

@@ -24,6 +24,17 @@ import cc1101.options
 
 # pylint: disable=protected-access
 
+
+@pytest.mark.parametrize(
+    ("xfer_return_value", "sync_word"),
+    [([64, 211, 145], b"\xd3\x91"), ([64, 0, 0], b"\0\0")],
+)
+def test_get_sync_word(transceiver, xfer_return_value, sync_word):
+    transceiver._spi.xfer.return_value = xfer_return_value
+    assert transceiver.get_sync_word() == sync_word
+    transceiver._spi.xfer.assert_called_once_with([0x04 | 0xC0, 0, 0])
+
+
 _FREQUENCY_CONTROL_WORD_HERTZ_PARAMS = [
     ([0x10, 0xA7, 0x62], 433000000),
     ([0x10, 0xAB, 0x85], 433420000),
@@ -87,6 +98,7 @@ def test__filter_bandwidth_floating_point_to_real(mantissa, exponent, real):
 def test__get_filter_bandwidth_hertz(transceiver, mdmcfg4, real):
     transceiver._spi.xfer.return_value = [15, mdmcfg4]
     assert transceiver._get_filter_bandwidth_hertz() == pytest.approx(real, rel=1e-3)
+    transceiver._spi.xfer.assert_called_once_with([0x10 | 0x80, 0])
 
 
 _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [