|
192 | 192 | _RH_RF95_LOW_DATA_RATE_OPTIMIZE = const(0x01)
|
193 | 193 |
|
194 | 194 | # RH_RF95_REG_1E_MODEM_CONFIG2 0x1e
|
| 195 | +_RH_RF95_DETECTION_OPTIMIZE = const(0x31) |
| 196 | +_RH_RF95_DETECTION_THRESHOLD = const(0x37) |
195 | 197 | _RH_RF95_SPREADING_FACTOR = const(0xf0)
|
196 | 198 | _RH_RF95_SPREADING_FACTOR_64CPS = const(0x60)
|
197 | 199 | _RH_RF95_SPREADING_FACTOR_128CPS = const(0x70)
|
@@ -334,7 +336,8 @@ def __set__(self, obj, val):
|
334 | 336 | rx_done = _RegisterBits(_RH_RF95_REG_12_IRQ_FLAGS, offset=6, bits=1)
|
335 | 337 |
|
336 | 338 | 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): |
338 | 341 | self.high_power = high_power
|
339 | 342 | # Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
|
340 | 343 | # Set Default Baudrate to 5MHz to avoid problems
|
@@ -367,10 +370,37 @@ def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
|
367 | 370 | self._write_u8(_RH_RF95_REG_0F_FIFO_RX_BASE_ADDR, 0x00)
|
368 | 371 | # Set mode idle
|
369 | 372 | 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 | + ) |
371 | 403 | # 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? |
374 | 404 | self._write_u8(_RH_RF95_REG_26_MODEM_CONFIG3, 0x00) # Preamble lsb?
|
375 | 405 | # Set preamble length (default 8 bytes to match radiohead).
|
376 | 406 | self.preamble_length = preamble_length
|
|
0 commit comments