Skip to content

Fix setting of the Power amplifiers when power boost is enabled #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions adafruit_rfm69.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
_REG_FRF_LSB = const(0x09)
_REG_VERSION = const(0x10)
_REG_PA_LEVEL = const(0x11)
_REG_OCP = const(0x13)
_REG_RX_BW = const(0x19)
_REG_AFC_BW = const(0x1A)
_REG_RSSI_VALUE = const(0x24)
Expand All @@ -110,6 +111,8 @@
_TEST_PA1_BOOST = const(0x5D)
_TEST_PA2_NORMAL = const(0x70)
_TEST_PA2_BOOST = const(0x7C)
_OCP_NORMAL = const(0x1A)
_OCP_HIGH_POWER = const(0x0F)

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

# pylint: disable=too-many-statements
# pylint: disable=too-many-arguments
def __init__( # pylint: disable=invalid-name
self,
spi: SPI,
Expand Down Expand Up @@ -316,9 +320,6 @@ def __init__( # pylint: disable=invalid-name
self._write_u8(_REG_FIFO_THRESH, 0b10001111)
# Configure low beta off.
self._write_u8(_REG_TEST_DAGC, 0x30)
# Disable boost.
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
# Set the syncronization word.
self.sync_word = sync_word
self.preamble_length = preamble_length # Set the preamble length.
Expand Down Expand Up @@ -448,16 +449,17 @@ def reset(self) -> None:
self._reset.value = False
time.sleep(0.005) # 5 ms

def set_boost(self, setting: int) -> None:
"""Set preamp boost if needed."""
if self._tx_power >= 18:
self._write_u8(_REG_TEST_PA1, setting)
self._write_u8(_REG_TEST_PA2, setting)
def disable_boost(self) -> None:
"""Disable preamp boost."""
if self.high_power:
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
self._write_u8(_REG_OCP, _OCP_NORMAL)

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

def sleep(self) -> None:
Expand All @@ -469,7 +471,7 @@ def listen(self) -> None:
and retrieve packets as they're available.
"""
# Like RadioHead library, turn off high power boost if enabled.
self.set_boost(_TEST_PA1_NORMAL)
self.disable_boost()
# Enable payload ready interrupt for D0 line.
self.dio_0_mapping = 0b01
# Enter RX mode (will clear FIFO!).
Expand All @@ -480,8 +482,11 @@ def transmit(self) -> None:
entering transmit mode and more. For generating and transmitting a packet of data use
:py:func:`send` instead.
"""
# Like RadioHead library, turn on high power boost if enabled.
self.set_boost(_TEST_PA1_BOOST)
# Like RadioHead library, turn on high power boost if needed.
if self.high_power and (self._tx_power >= 18):
self._write_u8(_REG_TEST_PA1, _TEST_PA1_BOOST)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_BOOST)
self._write_u8(_REG_OCP, _OCP_HIGH_POWER)
# Enable packet sent interrupt for D0 line.
self.dio_0_mapping = 0b00
# Enter TX mode (will clear FIFO!).
Expand Down Expand Up @@ -653,10 +658,10 @@ def tx_power(self) -> int:
if not pa0 and pa1 and not pa2:
# -2 to 13 dBm range
return -18 + current_output_power
if not pa0 and pa1 and pa2 and not self.high_power:
if not pa0 and pa1 and pa2 and self.high_power and self._tx_power < 18:
# 2 to 17 dBm range
return -14 + current_output_power
if not pa0 and pa1 and pa2 and self.high_power:
if not pa0 and pa1 and pa2 and self.high_power and self._tx_power >= 18:
# 5 to 20 dBm range
return -11 + current_output_power
raise RuntimeError("Power amps state unknown!")
Expand Down