Skip to content

Commit b712103

Browse files
projectgusdpgeorge
authored andcommitted
lora-sx126x: Fix invalid default configuration after reset.
According to the docs, only freq_khz was needed for working output. However: - Without output_power setting, no output from SX1262 antenna (theory: output routed to the SX1261 antenna). - SF,BW,etc. settings were different from the SX127x power on defaults, so modems with an identical configuration were unable to communicate. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 4cc6706 commit b712103

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

micropython/lora/lora-sx126x/lora/sx126x.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ def __init__(
163163
# 0x02 is 40us, default value appears undocumented but this is the SX1276 default
164164
self._ramp_val = 0x02
165165

166+
# Configure the SX126x at least once after reset
167+
self._configured = False
168+
166169
if reset:
167170
# If the caller supplies a reset pin argument, reset the radio
168171
reset.init(Pin.OUT, value=0)
@@ -385,22 +388,22 @@ def configure(self, lora_cfg):
385388
syncword = 0x0404 + ((syncword & 0x0F) << 4) + ((syncword & 0xF0) << 8)
386389
self._cmd(">BBH", _CMD_WRITE_REGISTER, _REG_LSYNCRH, syncword)
387390

388-
if "output_power" in lora_cfg:
391+
if not self._configured or any(
392+
key in lora_cfg for key in ("output_power", "pa_ramp_us", "tx_ant")
393+
):
389394
pa_config_args, self._output_power = self._get_pa_tx_params(
390-
lora_cfg["output_power"], lora_cfg.get("tx_ant", None)
395+
lora_cfg.get("output_power", self._output_power), lora_cfg.get("tx_ant", None)
391396
)
392397
self._cmd("BBBBB", _CMD_SET_PA_CONFIG, *pa_config_args)
393398

394-
if "pa_ramp_us" in lora_cfg:
395-
self._ramp_val = self._get_pa_ramp_val(
396-
lora_cfg, [10, 20, 40, 80, 200, 800, 1700, 3400]
397-
)
399+
if "pa_ramp_us" in lora_cfg:
400+
self._ramp_val = self._get_pa_ramp_val(
401+
lora_cfg, [10, 20, 40, 80, 200, 800, 1700, 3400]
402+
)
398403

399-
if "output_power" in lora_cfg or "pa_ramp_us" in lora_cfg:
400-
# Only send the SetTxParams command if power level or PA ramp time have changed
401404
self._cmd("BBB", _CMD_SET_TX_PARAMS, self._output_power, self._ramp_val)
402405

403-
if any(key in lora_cfg for key in ("sf", "bw", "coding_rate")):
406+
if not self._configured or any(key in lora_cfg for key in ("sf", "bw", "coding_rate")):
404407
if "sf" in lora_cfg:
405408
self._sf = lora_cfg["sf"]
406409
if self._sf < _CFG_SF_MIN or self._sf > _CFG_SF_MAX:
@@ -441,6 +444,7 @@ def configure(self, lora_cfg):
441444
self._reg_write(_REG_RX_GAIN, 0x96 if lora_cfg["rx_boost"] else 0x94)
442445

443446
self._check_error()
447+
self._configured = True
444448

445449
def _invert_workaround(self, enable):
446450
# Apply workaround for DS 15.4 Optimizing the Inverted IQ Operation

micropython/lora/lora/lora/modem.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ def __init__(self, ant_sw):
3737
self._ant_sw = ant_sw
3838
self._irq_callback = None
3939

40-
# Common configuration settings that need to be tracked by all modem drivers
41-
# (Note that subclasses may set these to other values in their constructors, to match
42-
# the power-on-reset configuration of a particular modem.)
40+
# Common configuration settings that need to be tracked by all modem drivers.
4341
#
42+
# Where modem hardware sets different values after reset, the driver should
43+
# set them back to these defaults (if not provided by the user), so that
44+
# behaviour remains consistent between different modems using the same driver.
4445
self._rf_freq_hz = 0 # Needs to be set via configure()
4546
self._sf = 7 # Spreading factor
4647
self._bw_hz = 125000 # Reset value

0 commit comments

Comments
 (0)