From 928f29863e162914ce888c920e5a4db88902c068 Mon Sep 17 00:00:00 2001 From: Brennen Bearnes Date: Thu, 16 Aug 2018 11:43:56 -0600 Subject: [PATCH 1/5] fixes for cpython & pi: hard reset before i2c init, tighten struct.unpack() It's necessary to hook up the reset pin and toggle it before initializing the i2c device, at least with some hardware (including the Raspberry Pi). struct.unpack() is stricter about inputs on CPython than under CircuitPython. --- adafruit_si4713.py | 18 +++++++++++++++--- examples/simpletest.py | 21 +++++++++++++-------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/adafruit_si4713.py b/adafruit_si4713.py index 6514753..12007e8 100644 --- a/adafruit_si4713.py +++ b/adafruit_si4713.py @@ -120,11 +120,24 @@ class SI4713: def __init__(self, i2c, *, address=_SI4710_ADDR1, reset=None, timeout_s=0.1): self._timeout_s = timeout_s - self._device = i2c_device.I2CDevice(i2c, address) + # Configure reset line if it was provided. self._reset = reset + if self._reset is not None: self._reset.switch_to_output(value=True) + + # Toggle reset line low to reset the chip and then wait a bit for + # startup - this is necessary before initializing as an i2c device + # on at least the Raspberry Pi, and potentially elsewhere: + self._reset.value = True + time.sleep(0.01) + self._reset.value = False + time.sleep(0.01) + self._reset.value = True + time.sleep(0.25) + + self._device = i2c_device.I2CDevice(i2c, address) self.reset() # Check product ID. if self._get_product_number() != 13: @@ -382,8 +395,7 @@ def input_level(self): """ # Perform ASQ request, then parse out 8 bit _signed_ input level value. self._asq_status() - return struct.unpack('bbbbb', self._BUFFER)[4] - + return struct.unpack('bbbbb', self._BUFFER[0:5])[4] @property def audio_signal_status(self): """Retrieve the ASQ or audio signal quality status value from the chip. diff --git a/examples/simpletest.py b/examples/simpletest.py index 685dba4..112813b 100644 --- a/examples/simpletest.py +++ b/examples/simpletest.py @@ -12,19 +12,24 @@ # mentions you can only specify 50khz steps! FREQUENCY_KHZ = 102300 # 102.300mhz - # Initialize I2C bus. i2c = busio.I2C(board.SCL, board.SDA) # Initialize SI4713. -si4713 = adafruit_si4713.SI4713(i2c) +# si4713 = adafruit_si4713.SI4713(i2c) + # Alternatively you can specify the I2C address of the device if it changed: -#si4713 = adafruit_si4713.SI4713(i2c, address=0x11) -# Also if you hooked up the reset line you can specify that too. Make sure -# to pass in a DigitalInOut instance: -#import digitalio -#reset = digitalio.DigitalInOut(board.D5) -#si4713 = adafruit_si4713.SI4713(i2c, reset=reset) +# si4713 = adafruit_si4713.SI4713(i2c, address=0x11) + +# If you hooked up the reset line you should specify that too. Make sure +# to pass in a DigitalInOut instance. You will need the reset pin with the +# Raspberry Pi, and probably other devices: +import digitalio +reset = digitalio.DigitalInOut(board.D5) + +print('initializing si4713 instance') +si4713 = adafruit_si4713.SI4713(i2c, reset=reset, timeout_s=0.5) +print('done') # Measure the noise level for the transmit frequency (this assumes automatic # antenna capacitance setting, but see below to adjust to a specific value). From f72818e407ac43f10a49b0ebccf049d39ca419c8 Mon Sep 17 00:00:00 2001 From: Brennen Bearnes Date: Thu, 16 Aug 2018 11:49:42 -0600 Subject: [PATCH 2/5] move digitalio import to top of simpletest.py, per pylint --- examples/simpletest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simpletest.py b/examples/simpletest.py index 112813b..1424de1 100644 --- a/examples/simpletest.py +++ b/examples/simpletest.py @@ -4,6 +4,7 @@ import board import busio +import digitalio import adafruit_si4713 @@ -24,7 +25,6 @@ # If you hooked up the reset line you should specify that too. Make sure # to pass in a DigitalInOut instance. You will need the reset pin with the # Raspberry Pi, and probably other devices: -import digitalio reset = digitalio.DigitalInOut(board.D5) print('initializing si4713 instance') From 44a67a5bd29e5691c16395b086c61c0af0019bc6 Mon Sep 17 00:00:00 2001 From: Brennen Bearnes Date: Thu, 16 Aug 2018 12:06:04 -0600 Subject: [PATCH 3/5] rename simpletest.py -> si4713_simpletest.py; tweak whitespace per pylint --- adafruit_si4713.py | 2 +- examples/{simpletest.py => si4713_simpletest.py} | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) rename examples/{simpletest.py => si4713_simpletest.py} (99%) diff --git a/adafruit_si4713.py b/adafruit_si4713.py index 12007e8..204b581 100644 --- a/adafruit_si4713.py +++ b/adafruit_si4713.py @@ -127,7 +127,7 @@ def __init__(self, i2c, *, address=_SI4710_ADDR1, reset=None, timeout_s=0.1): if self._reset is not None: self._reset.switch_to_output(value=True) - # Toggle reset line low to reset the chip and then wait a bit for + # Toggle reset line low to reset the chip and then wait a bit for # startup - this is necessary before initializing as an i2c device # on at least the Raspberry Pi, and potentially elsewhere: self._reset.value = True diff --git a/examples/simpletest.py b/examples/si4713_simpletest.py similarity index 99% rename from examples/simpletest.py rename to examples/si4713_simpletest.py index 1424de1..a0acda6 100644 --- a/examples/simpletest.py +++ b/examples/si4713_simpletest.py @@ -8,7 +8,6 @@ import adafruit_si4713 - # Specify the FM frequency to transmit on in kilohertz. As the datasheet # mentions you can only specify 50khz steps! FREQUENCY_KHZ = 102300 # 102.300mhz From 7a37317bebedba5e356e75dcb388ba6514451e14 Mon Sep 17 00:00:00 2001 From: Brennen Bearnes Date: Thu, 16 Aug 2018 12:23:38 -0600 Subject: [PATCH 4/5] update simpletest reference in examples.rst --- docs/examples.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index 1af3138..b789f62 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -3,6 +3,6 @@ Simple test Ensure your device works with this simple test. -.. literalinclude:: ../examples/simpletest.py - :caption: examples/simpletest.py +.. literalinclude:: ../examples/si4713_simpletest.py + :caption: examples/si4713_simpletest.py :linenos: From cfde317feacf2c4c220b9430f3791ad1092a4db9 Mon Sep 17 00:00:00 2001 From: Brennen Bearnes Date: Thu, 16 Aug 2018 20:34:18 -0600 Subject: [PATCH 5/5] simple test: reset -> si_reset --- examples/si4713_simpletest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/si4713_simpletest.py b/examples/si4713_simpletest.py index a0acda6..d4adf50 100644 --- a/examples/si4713_simpletest.py +++ b/examples/si4713_simpletest.py @@ -24,10 +24,10 @@ # If you hooked up the reset line you should specify that too. Make sure # to pass in a DigitalInOut instance. You will need the reset pin with the # Raspberry Pi, and probably other devices: -reset = digitalio.DigitalInOut(board.D5) +si_reset = digitalio.DigitalInOut(board.D5) print('initializing si4713 instance') -si4713 = adafruit_si4713.SI4713(i2c, reset=reset, timeout_s=0.5) +si4713 = adafruit_si4713.SI4713(i2c, reset=si_reset, timeout_s=0.5) print('done') # Measure the noise level for the transmit frequency (this assumes automatic