Skip to content

Commit 3f33b9a

Browse files
committed
Expose controls for spreading factor, coding rate, signal bandwidth.
1 parent 7205d16 commit 3f33b9a

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

README.rst

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,26 @@ This is easily achieved by downloading
2929
Usage Example
3030
=============
3131

32-
See examples/rfm9x_simpletest.py for a demo of the usage.
32+
Initialization of the RFM radio requires specifying a frequency appropriate to
33+
your radio hardware (i.e. 868-915 or 433 MHz) and specifying the pins used in your
34+
wiring from the controller board to the radio module.
35+
36+
This example code matches the wiring used in the
37+
`LoRa and LoRaWAN Radio for Raspberry Pi <https://learn.adafruit.com/lora-and-lorawan-radio-for-raspberry-pi/>`_
38+
project:
39+
40+
.. code-block:: python
41+
import digitalio
42+
import board
43+
import busio
44+
import adafruit_rfm9x
45+
46+
RADIO_FREQ_MHZ = 915.0
47+
CS = digitalio.DigitalInOut(board.CE1)
48+
RESET = digitalio.DigitalInOut(board.D25)
49+
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
50+
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
51+
3352
Note: the default baudrate for the SPI is 50000000 (5MHz). The maximum setting is 10Mhz but
3453
transmission errors have been observed expecially when using breakout boards.
3554
For breakout boards or other configurations where the boards are separated, it may be necessary to reduce
@@ -39,9 +58,21 @@ To set it to 1000000 use :
3958

4059
.. code-block:: python
4160
42-
# Initialze RFM radio
61+
# Initialze RFM radio with a more conservative baudrate
4362
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ, baudrate=1000000)
4463
64+
Optional controls exist to alter the signal bandwidth, coding rate, and spreading factor
65+
settings used by the radio to achieve better performance in different environments.
66+
By default, settings compatible with RadioHead Bw125Cr45Sf128 mode are used, matching
67+
the following example:
68+
69+
.. code-block:: python
70+
71+
# Initialze RFM radio with conservative baudrate and default modem config
72+
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ, baudrate=1000000,
73+
signal_bandwidth=125000, coding_rate=5, spreading_factor=7)
74+
75+
See examples/rfm9x_simpletest.py for an expanded demo of the usage.
4576

4677

4778
Contributing

adafruit_rfm9x.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@
192192
_RH_RF95_LOW_DATA_RATE_OPTIMIZE = const(0x01)
193193

194194
# RH_RF95_REG_1E_MODEM_CONFIG2 0x1e
195+
_RH_RF95_DETECTION_OPTIMIZE = const(0x31)
196+
_RH_RF95_DETECTION_THRESHOLD = const(0x37)
195197
_RH_RF95_SPREADING_FACTOR = const(0xf0)
196198
_RH_RF95_SPREADING_FACTOR_64CPS = const(0x60)
197199
_RH_RF95_SPREADING_FACTOR_128CPS = const(0x70)
@@ -334,7 +336,8 @@ def __set__(self, obj, val):
334336
rx_done = _RegisterBits(_RH_RF95_REG_12_IRQ_FLAGS, offset=6, bits=1)
335337

336338
def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
337-
high_power=True, baudrate=5000000):
339+
high_power=True, baudrate=5000000, signal_bandwidth=125000,
340+
coding_rate=5, spreading_factor=7):
338341
self.high_power = high_power
339342
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
340343
# Set Default Baudrate to 5MHz to avoid problems
@@ -367,10 +370,37 @@ def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
367370
self._write_u8(_RH_RF95_REG_0F_FIFO_RX_BASE_ADDR, 0x00)
368371
# Set mode idle
369372
self.idle()
370-
# Set modem config to RadioHead compatible Bw125Cr45Sf128 mode.
373+
# Defaults set modem config to RadioHead compatible Bw125Cr45Sf128 mode.
374+
# Set signal bandwidth (set to 125000 to match RadioHead Bw125).
375+
bins = (7800, 10400, 15600, 20800, 31250, 41700, 62500, 125000, 250000)
376+
for bw, cutoff in enumerate(bins):
377+
if bandwidth <= cutoff:
378+
break
379+
else:
380+
bw = 9
381+
self._write_u8(
382+
_RH_RF95_REG_1D_MODEM_CONFIG1,
383+
(self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0x0f) | (bw << 4)
384+
)
385+
# Set coding rate (set to 5 to match RadioHead Cr45).
386+
denominator = min(max(coding_rate, 5), 8)
387+
cr = denominator - 4
388+
self._write_u8(
389+
_RH_RF95_REG_1D_MODEM_CONFIG1,
390+
(self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0xf1) | (cr << 1)
391+
)
392+
# Set spreading factor (set to 7 to match RadioHead Sf128).
393+
sf = min(max(spreading_factor, 6), 12)
394+
self._write_u8(_RH_RF95_DETECTION_OPTIMIZE, 0xc5 if sf == 6 else 0xc3)
395+
self._write_u8(_RH_RF95_DETECTION_THRESHOLD, 0x0c if sf == 6 else 0x0a)
396+
self._write_u8(
397+
_RH_RF95_REG_1E_MODEM_CONFIG2,
398+
(
399+
(self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0x0f) |
400+
((sf << 4) & 0xf0)
401+
)
402+
)
371403
# Note no sync word is set for LoRa mode either!
372-
self._write_u8(_RH_RF95_REG_1D_MODEM_CONFIG1, 0x72) # Fei msb?
373-
self._write_u8(_RH_RF95_REG_1E_MODEM_CONFIG2, 0x74) # Fei lsb?
374404
self._write_u8(_RH_RF95_REG_26_MODEM_CONFIG3, 0x00) # Preamble lsb?
375405
# Set preamble length (default 8 bytes to match radiohead).
376406
self.preamble_length = preamble_length

0 commit comments

Comments
 (0)