diff --git a/adafruit_rfm69.py b/adafruit_rfm69.py index f32ecae..23df7f5 100644 --- a/adafruit_rfm69.py +++ b/adafruit_rfm69.py @@ -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) @@ -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. @@ -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, @@ -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. @@ -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: @@ -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!). @@ -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!). @@ -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!")