Skip to content

Commit c6987cf

Browse files
committed
fix frequency setter - add timeout to send - set default baudrate to 5MH
1 parent ea51280 commit c6987cf

File tree

3 files changed

+60
-38
lines changed

3 files changed

+60
-38
lines changed

README.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,19 @@ This is easily achieved by downloading
3939

4040
Usage Example
4141
=============
42-
4342
See examples/rfm69_simpletest.py for a simple demo of the usage.
43+
Note: the default baudrate for the SPI is 50000000 (5MHz).
44+
The maximum setting is 10Mhz but
45+
transmission errors have been observed expecially when using breakout boards.
46+
For breakout boards or other configurations where the boards are separated,
47+
it may be necessary to reduce the baudrate for reliable data transmission.
48+
The baud rate may be specified as an keyword parameter when initializing the board.
49+
To set it to 1000000 use :
50+
.. code-block:: python# Initialze RFM radio
51+
.. code-block:: python
52+
# Initialze RFM radio
53+
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000)
54+
4455
4556
Contributing
4657
============
@@ -94,4 +105,4 @@ Now, once you have the virtual environment activated:
94105
95106
This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to
96107
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
97-
locally verify it will pass.
108+
locally verify it will pass.

adafruit_rfm69.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def __set__(self, obj, val):
289289
payload_ready = _RegisterBits(_REG_IRQ_FLAGS2, offset=2)
290290

291291
def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
292-
preamble_length=4, encryption_key=None, high_power=True, baudrate=10000000):
292+
preamble_length=4, encryption_key=None, high_power=True, baudrate=5000000):
293293
self._tx_power = 13
294294
self.high_power = high_power
295295
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
@@ -673,10 +673,12 @@ def frequency_deviation(self, val):
673673
self._write_u8(_REG_FDEV_MSB, fdev >> 8)
674674
self._write_u8(_REG_FDEV_LSB, fdev & 0xFF)
675675

676-
def send(self, data):
677-
"""Send a string of data using the transmitter. You can only send 60 bytes at a time
678-
(limited by chip's FIFO size and appended headers). Note this appends a 4 byte header to
679-
be compatible with the RadioHead library.
676+
def send(self, data, timeout=2.):
677+
"""Send a string of data using the transmitter.
678+
You can only send 60 bytes at a time
679+
(limited by chip's FIFO size and appended headers).
680+
Note this appends a 4 byte header to be compatible with the RadioHead library.
681+
The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
680682
"""
681683
# Disable pylint warning to not use length as a check for zero.
682684
# This is a puzzling warning as the below code is clearly the most
@@ -707,10 +709,17 @@ def send(self, data):
707709
# best that can be done right now without interrupts).
708710
while not self.packet_sent:
709711
pass
712+
start = time.monotonic()
713+
timed_out = False
714+
while not timed_out and not self.packet_sent:
715+
if (time.monotonic() - start) >= timeout:
716+
timed_out = True
710717
# Go back to idle mode after transmit.
711718
self.idle()
719+
if timed_out:
720+
raise RuntimeError('Timeout during packet send')
712721

713-
def receive(self, timeout_s=0.5, keep_listening=True):
722+
def receive(self, timeout=0.5, keep_listening=True):
714723
"""Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
715724
seconds for a packet to be received and decoded. If a packet is found the payload bytes
716725
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):
726735
# enough, however it's the best that can be done from Python without
727736
# interrupt supports.
728737
start = time.monotonic()
729-
while not self.payload_ready:
730-
if (time.monotonic() - start) >= timeout_s:
731-
return None # Exceeded timeout.
738+
timed_out = False
739+
while not timed_out and not self.payload_ready:
740+
if (time.monotonic() - start) >= timeout:
741+
timed_out = True
732742
# Payload ready is set, a packet is in the FIFO.
733743
packet = None
734744
# Enter idle mode to stop receiving other packets.
735745
self.idle()
736-
# Read the data from the FIFO.
737-
with self._device as device:
738-
self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0
739-
# value (read).
740-
device.write(self._BUFFER, end=1)
741-
# Read the length of the FIFO.
742-
device.readinto(self._BUFFER, end=1)
743-
fifo_length = self._BUFFER[0]
744-
# Handle if the received packet is too small to include the 4 byte
745-
# RadioHead header--reject this packet and ignore it.
746-
if fifo_length < 4:
747-
# Invalid packet, ignore it. However finish reading the FIFO
748-
# to clear the packet.
749-
device.readinto(self._BUFFER, end=fifo_length)
750-
packet = None
751-
else:
752-
# Read the 4 bytes of the RadioHead header.
753-
device.readinto(self._BUFFER, end=4)
754-
# Ignore validating any of the header bytes.
755-
# Now read the remaining packet payload as the result.
756-
fifo_length -= 4
757-
packet = bytearray(fifo_length)
758-
device.readinto(packet)
759-
# Listen again if necessary and return the result packet.
760-
if keep_listening:
761-
self.listen()
746+
if not timed_out:
747+
# Read the data from the FIFO.
748+
with self._device as device:
749+
self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0
750+
# value (read).
751+
device.write(self._BUFFER, end=1)
752+
# Read the length of the FIFO.
753+
device.readinto(self._BUFFER, end=1)
754+
fifo_length = self._BUFFER[0]
755+
# Handle if the received packet is too small to include the 4 byte
756+
# RadioHead header--reject this packet and ignore it.
757+
if fifo_length < 4:
758+
# Invalid packet, ignore it. However finish reading the FIFO
759+
# to clear the packet.
760+
device.readinto(self._BUFFER, end=fifo_length)
761+
packet = None
762+
else:
763+
# Read the 4 bytes of the RadioHead header.
764+
device.readinto(self._BUFFER, end=4)
765+
# Ignore validating any of the header bytes.
766+
# Now read the remaining packet payload as the result.
767+
fifo_length -= 4
768+
packet = bytearray(fifo_length)
769+
device.readinto(packet)
770+
# Listen again if necessary and return the result packet.
771+
if keep_listening:
772+
self.listen()
762773
return packet

examples/rfm69_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
while True:
5252
packet = rfm69.receive()
5353
# Optionally change the receive timeout from its default of 0.5 seconds:
54-
#packet = rfm69.receive(timeout_s=5.0)
54+
#packet = rfm69.receive(timeout=5.0)
5555
# If no packet was received during the timeout then None is returned.
5656
if packet is None:
5757
print('Received nothing! Listening again...')

0 commit comments

Comments
 (0)