From ea5128060ac5f9526bebc5489bf7f29b111062a9 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Fri, 22 Jun 2018 22:37:45 -0400 Subject: [PATCH 1/9] fix frequency initialiation --- adafruit_rfm69.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_rfm69.py b/adafruit_rfm69.py index 30e64e1..57a929f 100644 --- a/adafruit_rfm69.py +++ b/adafruit_rfm69.py @@ -340,7 +340,7 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4', # Set the preamble length. self.preamble_length = preamble_length # Set frequency. - self.frequency = frequency + self.frequency_mhz = frequency # Set encryption key. self.encryption_key = encryption_key # Set transmit power to 13 dBm, a safe value any module supports. From c6987cf25533ab35ec9e80e6fd6b70b161a39edc Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Sun, 8 Jul 2018 10:18:46 -0400 Subject: [PATCH 2/9] fix frequency setter - add timeout to send - set default baudrate to 5MH --- README.rst | 15 ++++++- adafruit_rfm69.py | 81 ++++++++++++++++++++---------------- examples/rfm69_simpletest.py | 2 +- 3 files changed, 60 insertions(+), 38 deletions(-) diff --git a/README.rst b/README.rst index f3ec83d..e59114d 100644 --- a/README.rst +++ b/README.rst @@ -39,8 +39,19 @@ This is easily achieved by downloading Usage Example ============= - See examples/rfm69_simpletest.py for a simple demo of the usage. +Note: the default baudrate for the SPI is 50000000 (5MHz). +The maximum setting is 10Mhz but +transmission errors have been observed expecially when using breakout boards. +For breakout boards or other configurations where the boards are separated, +it may be necessary to reduce the baudrate for reliable data transmission. +The baud rate may be specified as an keyword parameter when initializing the board. +To set it to 1000000 use : +.. code-block:: python# Initialze RFM radio +.. code-block:: python + # Initialze RFM radio + rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) + Contributing ============ @@ -94,4 +105,4 @@ Now, once you have the virtual environment activated: This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to -locally verify it will pass. \ No newline at end of file +locally verify it will pass. diff --git a/adafruit_rfm69.py b/adafruit_rfm69.py index 57a929f..6e6b1db 100644 --- a/adafruit_rfm69.py +++ b/adafruit_rfm69.py @@ -289,7 +289,7 @@ def __set__(self, obj, val): payload_ready = _RegisterBits(_REG_IRQ_FLAGS2, offset=2) def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4', - preamble_length=4, encryption_key=None, high_power=True, baudrate=10000000): + preamble_length=4, encryption_key=None, high_power=True, baudrate=5000000): self._tx_power = 13 self.high_power = high_power # Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz. @@ -673,10 +673,12 @@ def frequency_deviation(self, val): self._write_u8(_REG_FDEV_MSB, fdev >> 8) self._write_u8(_REG_FDEV_LSB, fdev & 0xFF) - def send(self, data): - """Send a string of data using the transmitter. You can only send 60 bytes at a time - (limited by chip's FIFO size and appended headers). Note this appends a 4 byte header to - be compatible with the RadioHead library. + def send(self, data, timeout=2.): + """Send a string of data using the transmitter. + You can only send 60 bytes at a time + (limited by chip's FIFO size and appended headers). + Note this appends a 4 byte header to be compatible with the RadioHead library. + The timeout is just to prevent a hang (arbitrarily set to 2 seconds) """ # Disable pylint warning to not use length as a check for zero. # This is a puzzling warning as the below code is clearly the most @@ -707,10 +709,17 @@ def send(self, data): # best that can be done right now without interrupts). while not self.packet_sent: pass + start = time.monotonic() + timed_out = False + while not timed_out and not self.packet_sent: + if (time.monotonic() - start) >= timeout: + timed_out = True # Go back to idle mode after transmit. self.idle() + if timed_out: + raise RuntimeError('Timeout during packet send') - def receive(self, timeout_s=0.5, keep_listening=True): + def receive(self, timeout=0.5, keep_listening=True): """Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of seconds for a packet to be received and decoded. If a packet is found the payload bytes are returned, otherwise None is returned (which indicates the timeout elapsed with no @@ -726,37 +735,39 @@ def receive(self, timeout_s=0.5, keep_listening=True): # enough, however it's the best that can be done from Python without # interrupt supports. start = time.monotonic() - while not self.payload_ready: - if (time.monotonic() - start) >= timeout_s: - return None # Exceeded timeout. + timed_out = False + while not timed_out and not self.payload_ready: + if (time.monotonic() - start) >= timeout: + timed_out = True # Payload ready is set, a packet is in the FIFO. packet = None # Enter idle mode to stop receiving other packets. self.idle() - # Read the data from the FIFO. - with self._device as device: - self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0 - # value (read). - device.write(self._BUFFER, end=1) - # Read the length of the FIFO. - device.readinto(self._BUFFER, end=1) - fifo_length = self._BUFFER[0] - # Handle if the received packet is too small to include the 4 byte - # RadioHead header--reject this packet and ignore it. - if fifo_length < 4: - # Invalid packet, ignore it. However finish reading the FIFO - # to clear the packet. - device.readinto(self._BUFFER, end=fifo_length) - packet = None - else: - # Read the 4 bytes of the RadioHead header. - device.readinto(self._BUFFER, end=4) - # Ignore validating any of the header bytes. - # Now read the remaining packet payload as the result. - fifo_length -= 4 - packet = bytearray(fifo_length) - device.readinto(packet) - # Listen again if necessary and return the result packet. - if keep_listening: - self.listen() + if not timed_out: + # Read the data from the FIFO. + with self._device as device: + self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0 + # value (read). + device.write(self._BUFFER, end=1) + # Read the length of the FIFO. + device.readinto(self._BUFFER, end=1) + fifo_length = self._BUFFER[0] + # Handle if the received packet is too small to include the 4 byte + # RadioHead header--reject this packet and ignore it. + if fifo_length < 4: + # Invalid packet, ignore it. However finish reading the FIFO + # to clear the packet. + device.readinto(self._BUFFER, end=fifo_length) + packet = None + else: + # Read the 4 bytes of the RadioHead header. + device.readinto(self._BUFFER, end=4) + # Ignore validating any of the header bytes. + # Now read the remaining packet payload as the result. + fifo_length -= 4 + packet = bytearray(fifo_length) + device.readinto(packet) + # Listen again if necessary and return the result packet. + if keep_listening: + self.listen() return packet diff --git a/examples/rfm69_simpletest.py b/examples/rfm69_simpletest.py index 8bfa4f8..98ff961 100644 --- a/examples/rfm69_simpletest.py +++ b/examples/rfm69_simpletest.py @@ -51,7 +51,7 @@ while True: packet = rfm69.receive() # Optionally change the receive timeout from its default of 0.5 seconds: - #packet = rfm69.receive(timeout_s=5.0) + #packet = rfm69.receive(timeout=5.0) # If no packet was received during the timeout then None is returned. if packet is None: print('Received nothing! Listening again...') From 816c44178d71e0f26b2d2550f93081affff608a9 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Sun, 8 Jul 2018 11:17:04 -0400 Subject: [PATCH 3/9] fix README.rst - indentation --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index e59114d..c05a3fa 100644 --- a/README.rst +++ b/README.rst @@ -50,7 +50,7 @@ To set it to 1000000 use : .. code-block:: python# Initialze RFM radio .. code-block:: python # Initialze RFM radio - rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) + rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) Contributing From 3b7fdf09e071a237e7720e635dc73efb9d37a586 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Sun, 8 Jul 2018 11:19:15 -0400 Subject: [PATCH 4/9] fix README.rst - indentation --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index c05a3fa..84c7dc9 100644 --- a/README.rst +++ b/README.rst @@ -49,8 +49,8 @@ The baud rate may be specified as an keyword parameter when initializing the boa To set it to 1000000 use : .. code-block:: python# Initialze RFM radio .. code-block:: python - # Initialze RFM radio - rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) +# Initialze RFM radio +rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) Contributing From 1e07f785d550cded5f02df208393b6a83fff87b1 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Sun, 8 Jul 2018 22:42:08 -0400 Subject: [PATCH 5/9] fix transmit timeout --- adafruit_rfm69.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/adafruit_rfm69.py b/adafruit_rfm69.py index 6e6b1db..65beff8 100644 --- a/adafruit_rfm69.py +++ b/adafruit_rfm69.py @@ -707,8 +707,6 @@ def send(self, data, timeout=2.): self.transmit() # Wait for packet sent interrupt with explicit polling (not ideal but # best that can be done right now without interrupts). - while not self.packet_sent: - pass start = time.monotonic() timed_out = False while not timed_out and not self.packet_sent: From 41e9926011a504cfd19adb1d19d2370156a57d8a Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 9 Jul 2018 10:37:31 -0400 Subject: [PATCH 6/9] fix typo in README.rst - default SPI baudrate --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 84c7dc9..0e25831 100644 --- a/README.rst +++ b/README.rst @@ -40,7 +40,7 @@ This is easily achieved by downloading Usage Example ============= See examples/rfm69_simpletest.py for a simple demo of the usage. -Note: the default baudrate for the SPI is 50000000 (5MHz). +Note: the default baudrate for the SPI is 5000000 (5MHz). The maximum setting is 10Mhz but transmission errors have been observed expecially when using breakout boards. For breakout boards or other configurations where the boards are separated, From f6d08044d964a003ecbf2e7dd64e7cccbd78006c Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 9 Jul 2018 21:48:20 -0400 Subject: [PATCH 7/9] modify .receive timeout - fix README typo --- README.rst | 1 - adafruit_rfm69.py | 55 ++++++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/README.rst b/README.rst index 0e25831..f59019d 100644 --- a/README.rst +++ b/README.rst @@ -47,7 +47,6 @@ For breakout boards or other configurations where the boards are separated, it may be necessary to reduce the baudrate for reliable data transmission. The baud rate may be specified as an keyword parameter when initializing the board. To set it to 1000000 use : -.. code-block:: python# Initialze RFM radio .. code-block:: python # Initialze RFM radio rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) diff --git a/adafruit_rfm69.py b/adafruit_rfm69.py index 65beff8..812eb27 100644 --- a/adafruit_rfm69.py +++ b/adafruit_rfm69.py @@ -741,31 +741,32 @@ def receive(self, timeout=0.5, keep_listening=True): packet = None # Enter idle mode to stop receiving other packets. self.idle() - if not timed_out: - # Read the data from the FIFO. - with self._device as device: - self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0 - # value (read). - device.write(self._BUFFER, end=1) - # Read the length of the FIFO. - device.readinto(self._BUFFER, end=1) - fifo_length = self._BUFFER[0] - # Handle if the received packet is too small to include the 4 byte - # RadioHead header--reject this packet and ignore it. - if fifo_length < 4: - # Invalid packet, ignore it. However finish reading the FIFO - # to clear the packet. - device.readinto(self._BUFFER, end=fifo_length) - packet = None - else: - # Read the 4 bytes of the RadioHead header. - device.readinto(self._BUFFER, end=4) - # Ignore validating any of the header bytes. - # Now read the remaining packet payload as the result. - fifo_length -= 4 - packet = bytearray(fifo_length) - device.readinto(packet) - # Listen again if necessary and return the result packet. - if keep_listening: - self.listen() + if timed_out: + return None + # Read the data from the FIFO. + with self._device as device: + self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0 + # value (read). + device.write(self._BUFFER, end=1) + # Read the length of the FIFO. + device.readinto(self._BUFFER, end=1) + fifo_length = self._BUFFER[0] + # Handle if the received packet is too small to include the 4 byte + # RadioHead header--reject this packet and ignore it. + if fifo_length < 4: + # Invalid packet, ignore it. However finish reading the FIFO + # to clear the packet. + device.readinto(self._BUFFER, end=fifo_length) + packet = None + else: + # Read the 4 bytes of the RadioHead header. + device.readinto(self._BUFFER, end=4) + # Ignore validating any of the header bytes. + # Now read the remaining packet payload as the result. + fifo_length -= 4 + packet = bytearray(fifo_length) + device.readinto(packet) + # Listen again if necessary and return the result packet. + if keep_listening: + self.listen() return packet From e1449ac5246ad3d1ff83fff38fcaa5e9cd688a54 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 9 Jul 2018 21:52:45 -0400 Subject: [PATCH 8/9] fix README typo again --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index f59019d..262ab01 100644 --- a/README.rst +++ b/README.rst @@ -47,6 +47,7 @@ For breakout boards or other configurations where the boards are separated, it may be necessary to reduce the baudrate for reliable data transmission. The baud rate may be specified as an keyword parameter when initializing the board. To set it to 1000000 use : + .. code-block:: python # Initialze RFM radio rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) From bb6d069c3fa5267bd9e29ba3e4d2e27f7c2497b9 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 9 Jul 2018 21:59:17 -0400 Subject: [PATCH 9/9] fix README typo again --- README.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 262ab01..66f2144 100644 --- a/README.rst +++ b/README.rst @@ -49,8 +49,9 @@ The baud rate may be specified as an keyword parameter when initializing the boa To set it to 1000000 use : .. code-block:: python -# Initialze RFM radio -rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) + + # Initialze RFM radio + rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000) Contributing