Skip to content

Commit 30e8723

Browse files
authored
Merge pull request #48 from jerryneedell/jerryn_power
Fix setting of the Power amplifiers when power boost is enabled
2 parents 864012e + 1e23cc8 commit 30e8723

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

adafruit_rfm69.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
_REG_FRF_LSB = const(0x09)
8787
_REG_VERSION = const(0x10)
8888
_REG_PA_LEVEL = const(0x11)
89+
_REG_OCP = const(0x13)
8990
_REG_RX_BW = const(0x19)
9091
_REG_AFC_BW = const(0x1A)
9192
_REG_RSSI_VALUE = const(0x24)
@@ -110,6 +111,8 @@
110111
_TEST_PA1_BOOST = const(0x5D)
111112
_TEST_PA2_NORMAL = const(0x70)
112113
_TEST_PA2_BOOST = const(0x7C)
114+
_OCP_NORMAL = const(0x1A)
115+
_OCP_HIGH_POWER = const(0x0F)
113116

114117
# The crystal oscillator frequency and frequency synthesizer step size.
115118
# See the datasheet for details of this calculation.
@@ -285,6 +288,7 @@ def __set__(self, obj: Optional["RFM69"], val: int) -> None:
285288
dio_0_mapping = _RegisterBits(_REG_DIO_MAPPING1, offset=6, bits=2)
286289

287290
# pylint: disable=too-many-statements
291+
# pylint: disable=too-many-arguments
288292
def __init__( # pylint: disable=invalid-name
289293
self,
290294
spi: SPI,
@@ -316,9 +320,6 @@ def __init__( # pylint: disable=invalid-name
316320
self._write_u8(_REG_FIFO_THRESH, 0b10001111)
317321
# Configure low beta off.
318322
self._write_u8(_REG_TEST_DAGC, 0x30)
319-
# Disable boost.
320-
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
321-
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
322323
# Set the syncronization word.
323324
self.sync_word = sync_word
324325
self.preamble_length = preamble_length # Set the preamble length.
@@ -448,16 +449,17 @@ def reset(self) -> None:
448449
self._reset.value = False
449450
time.sleep(0.005) # 5 ms
450451

451-
def set_boost(self, setting: int) -> None:
452-
"""Set preamp boost if needed."""
453-
if self._tx_power >= 18:
454-
self._write_u8(_REG_TEST_PA1, setting)
455-
self._write_u8(_REG_TEST_PA2, setting)
452+
def disable_boost(self) -> None:
453+
"""Disable preamp boost."""
454+
if self.high_power:
455+
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
456+
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
457+
self._write_u8(_REG_OCP, _OCP_NORMAL)
456458

457459
def idle(self) -> None:
458460
"""Enter idle standby mode (switching off high power amplifiers if necessary)."""
459461
# Like RadioHead library, turn off high power boost if enabled.
460-
self.set_boost(_TEST_PA1_NORMAL)
462+
self.disable_boost()
461463
self.operation_mode = STANDBY_MODE
462464

463465
def sleep(self) -> None:
@@ -469,7 +471,7 @@ def listen(self) -> None:
469471
and retrieve packets as they're available.
470472
"""
471473
# Like RadioHead library, turn off high power boost if enabled.
472-
self.set_boost(_TEST_PA1_NORMAL)
474+
self.disable_boost()
473475
# Enable payload ready interrupt for D0 line.
474476
self.dio_0_mapping = 0b01
475477
# Enter RX mode (will clear FIFO!).
@@ -480,8 +482,11 @@ def transmit(self) -> None:
480482
entering transmit mode and more. For generating and transmitting a packet of data use
481483
:py:func:`send` instead.
482484
"""
483-
# Like RadioHead library, turn on high power boost if enabled.
484-
self.set_boost(_TEST_PA1_BOOST)
485+
# Like RadioHead library, turn on high power boost if needed.
486+
if self.high_power and (self._tx_power >= 18):
487+
self._write_u8(_REG_TEST_PA1, _TEST_PA1_BOOST)
488+
self._write_u8(_REG_TEST_PA2, _TEST_PA2_BOOST)
489+
self._write_u8(_REG_OCP, _OCP_HIGH_POWER)
485490
# Enable packet sent interrupt for D0 line.
486491
self.dio_0_mapping = 0b00
487492
# Enter TX mode (will clear FIFO!).
@@ -653,10 +658,10 @@ def tx_power(self) -> int:
653658
if not pa0 and pa1 and not pa2:
654659
# -2 to 13 dBm range
655660
return -18 + current_output_power
656-
if not pa0 and pa1 and pa2 and not self.high_power:
661+
if not pa0 and pa1 and pa2 and self.high_power and self._tx_power < 18:
657662
# 2 to 17 dBm range
658663
return -14 + current_output_power
659-
if not pa0 and pa1 and pa2 and self.high_power:
664+
if not pa0 and pa1 and pa2 and self.high_power and self._tx_power >= 18:
660665
# 5 to 20 dBm range
661666
return -11 + current_output_power
662667
raise RuntimeError("Power amps state unknown!")

0 commit comments

Comments
 (0)