Browse Source

added method `_get_filter_bandwidth_hertz()`

Fabian Peter Hammerle 3 years ago
parent
commit
c8605d8007
2 changed files with 63 additions and 0 deletions
  1. 22 0
      cc1101/__init__.py
  2. 41 0
      tests/test_config.py

+ 22 - 0
cc1101/__init__.py

@@ -170,6 +170,28 @@ class CC1101:
     def _reset(self) -> None:
         self._command_strobe(StrobeAddress.SRES)
 
+    @classmethod
+    def _filter_bandwidth_floating_point_to_real(
+        cls, mantissa: int, exponent: int
+    ) -> float:
+        """
+        See "13 Receiver Channel Filter Bandwidth"
+        """
+        return cls._CRYSTAL_OSCILLATOR_FREQUENCY_HERTZ / (
+            8 * (4 + mantissa) * (2 ** exponent)
+        )
+
+    def _get_filter_bandwidth_hertz(self) -> float:
+        """
+        See "13 Receiver Channel Filter Bandwidth"
+
+        MDMCFG4.CHANBW_E & MDMCFG4.CHANBW_M
+        """
+        mdmcfg4 = self._read_single_byte(ConfigurationRegisterAddress.MDMCFG4)
+        return self._filter_bandwidth_floating_point_to_real(
+            exponent=mdmcfg4 >> 6, mantissa=(mdmcfg4 >> 4) & 0b11
+        )
+
     def _get_symbol_rate_exponent(self) -> int:
         """
         MDMCFG4.DRATE_E

+ 41 - 0
tests/test_config.py

@@ -48,6 +48,47 @@ def test__hertz_to_frequency_control_word(control_word, hertz):
     assert cc1101.CC1101._hertz_to_frequency_control_word(hertz) == control_word
 
 
+_FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS = [
+    # > The default values give 203 kHz channel filter bandwidth,
+    # > assuming a 26.0 MHz crystal.
+    (0, 2, 203e3),
+    # "Table 26: Channel Filter Bandwidths [kHz] (assuming a 26 MHz crystal)"
+    (0, 0, 812e3),
+    (0, 1, 406e3),
+    (0, 2, 203e3),
+    (1, 0, 650e3),
+    (1, 1, 325e3),
+    (3, 0, 464e3),
+    (3, 1, 232e3),
+    (3, 2, 116e3),
+    (3, 3, 58e3),
+]
+
+
+@pytest.mark.parametrize(
+    ("mantissa", "exponent", "real"), _FILTER_BANDWIDTH_MANTISSA_EXPONENT_REAL_PARAMS
+)
+def test__filter_bandwidth_floating_point_to_real(mantissa, exponent, real):
+    assert cc1101.CC1101._filter_bandwidth_floating_point_to_real(
+        mantissa=mantissa, exponent=exponent
+    ) == pytest.approx(real, rel=1e-3)
+
+
+@pytest.mark.parametrize(
+    ("mdmcfg4", "real"),
+    [
+        (0b10001100, 203e3),
+        (0b10001010, 203e3),
+        (0b10001110, 203e3),
+        (0b11111100, 58e3),
+        (0b01011100, 325e3),
+    ],
+)
+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)
+
+
 _SYMBOL_RATE_MANTISSA_EXPONENT_REAL_PARAMS = [
     # > The default values give a data rate of 115.051 kBaud
     # > (closest setting to 115.2 kBaud), assuming a 26.0 MHz crystal.