Browse Source

support chip version 0x04

https://github.com/fphammerle/python-cc1101/issues/15
Fabian Peter Hammerle 3 years ago
parent
commit
067b4ff01d
3 changed files with 36 additions and 6 deletions
  1. 2 0
      CHANGELOG.md
  2. 10 4
      cc1101/__init__.py
  3. 24 2
      tests/test_spi.py

+ 2 - 0
CHANGELOG.md

@@ -5,6 +5,8 @@ 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 chip version `0x04`
 
 ## [2.3.0] - 2020-12-11
 ### Added

+ 10 - 4
cc1101/__init__.py

@@ -131,7 +131,12 @@ class CC1101:
 
     # 29.3 Status Register Details
     _SUPPORTED_PARTNUM = 0
-    _SUPPORTED_VERSION = 0x14
+    # > The two versions of the chip will behave the same.
+    # https://e2e.ti.com/support/wireless-connectivity/sub-1-ghz/f/156/p/428028/1529544#1529544
+    _SUPPORTED_VERSIONS = [
+        0x04,  #  https://github.com/fphammerle/python-cc1101/issues/15
+        0x14,
+    ]
 
     _CRYSTAL_OSCILLATOR_FREQUENCY_HERTZ = 26e6
     # see "21 Frequency Programming"
@@ -474,10 +479,11 @@ class CC1101:
                 )
             )
         version = self._read_status_register(StatusRegisterAddress.VERSION)
-        if version != self._SUPPORTED_VERSION:
+        if version not in self._SUPPORTED_VERSIONS:
             raise ValueError(
-                "unexpected chip version number {} (expected: {})".format(
-                    version, self._SUPPORTED_VERSION
+                "unsupported chip version 0x{:02x} (expected one of [{}])".format(
+                    version,
+                    ", ".join("0x{:02x}".format(v) for v in self._SUPPORTED_VERSIONS),
                 )
             )
 

+ 24 - 2
tests/test_spi.py

@@ -15,6 +15,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
+import re
 import unittest.mock
 
 import pytest
@@ -59,7 +60,8 @@ def test__reset(transceiver):
     transceiver._spi.xfer.assert_called_once_with([0x30 | 0x00])
 
 
-def test___enter__(transceiver):
+@pytest.mark.parametrize("chip_version", [0x14, 0x04])
+def test___enter__(transceiver, chip_version):
     with unittest.mock.patch.object(
         transceiver, "_read_status_register"
     ) as read_status_register_mock, unittest.mock.patch.object(
@@ -79,7 +81,7 @@ def test___enter__(transceiver):
     ):
         read_status_register_mock.side_effect = lambda r: {
             cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
-            cc1101.addresses.StatusRegisterAddress.VERSION: 0x14,
+            cc1101.addresses.StatusRegisterAddress.VERSION: chip_version,
         }[r]
         with transceiver as transceiver_context:
             assert transceiver == transceiver_context
@@ -94,6 +96,26 @@ def test___enter__(transceiver):
             write_burst_mock.assert_called_once_with(0x18, [0b010100])
 
 
+def test___enter___unsupported_chip_version(transceiver):
+    with unittest.mock.patch.object(
+        transceiver, "_read_status_register"
+    ) as read_status_register_mock, unittest.mock.patch.object(transceiver, "_reset"):
+        read_status_register_mock.side_effect = lambda r: {
+            cc1101.addresses.StatusRegisterAddress.PARTNUM: 0,
+            cc1101.addresses.StatusRegisterAddress.VERSION: 0x15,
+        }[r]
+        with pytest.raises(
+            ValueError,
+            match=r"^{}$".format(
+                re.escape(
+                    "unsupported chip version 0x15 (expected one of [0x04, 0x14])"
+                )
+            ),
+        ):
+            with transceiver:
+                pass
+
+
 @pytest.mark.parametrize("bus", [0, 1])
 @pytest.mark.parametrize("chip_select", [0, 2])
 def test___enter__permission_error(transceiver, bus, chip_select):