Skip to content

lora: Multiple small fixes (including syncword, sx126x minimum config) #804

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 5 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 18 additions & 14 deletions micropython/lora/lora-sx126x/lora/sx126x.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ def __init__(
# 0x02 is 40us, default value appears undocumented but this is the SX1276 default
self._ramp_val = 0x02

# Configure the SX126x at least once after reset
self._configured = False

if reset:
# If the caller supplies a reset pin argument, reset the radio
reset.init(Pin.OUT, value=0)
Expand Down Expand Up @@ -383,24 +386,24 @@ def configure(self, lora_cfg):
# see
# https://www.thethingsnetwork.org/forum/t/should-private-lorawan-networks-use-a-different-sync-word/34496/15
syncword = 0x0404 + ((syncword & 0x0F) << 4) + ((syncword & 0xF0) << 8)
self._cmd(">BBH", _CMD_WRITE_REGISTER, _REG_LSYNCRH, syncword)
self._cmd(">BHH", _CMD_WRITE_REGISTER, _REG_LSYNCRH, syncword)

if "output_power" in lora_cfg:
if not self._configured or any(
key in lora_cfg for key in ("output_power", "pa_ramp_us", "tx_ant")
):
pa_config_args, self._output_power = self._get_pa_tx_params(
lora_cfg["output_power"], lora_cfg.get("tx_ant", None)
lora_cfg.get("output_power", self._output_power), lora_cfg.get("tx_ant", None)
)
self._cmd("BBBBB", _CMD_SET_PA_CONFIG, *pa_config_args)

if "pa_ramp_us" in lora_cfg:
self._ramp_val = self._get_pa_ramp_val(
lora_cfg, [10, 20, 40, 80, 200, 800, 1700, 3400]
)
if "pa_ramp_us" in lora_cfg:
self._ramp_val = self._get_pa_ramp_val(
lora_cfg, [10, 20, 40, 80, 200, 800, 1700, 3400]
)

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

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

self._check_error()
self._configured = True

def _invert_workaround(self, enable):
# Apply workaround for DS 15.4 Optimizing the Inverted IQ Operation
Expand All @@ -465,7 +469,7 @@ def calibrate(self):
# See DS 13.1.12 Calibrate Function

# calibParam 0xFE means to calibrate all blocks.
self._cmd("<BB", _CMD_CALIBRATE, 0xFE)
self._cmd("BB", _CMD_CALIBRATE, 0xFE)

time.sleep_us(_CALIBRATE_TYPICAL_TIME_US)

Expand Down Expand Up @@ -541,7 +545,7 @@ def start_recv(self, timeout_ms=None, continuous=False, rx_length=0xFF):
else:
timeout = 0 # Single receive mode, no timeout

self._cmd(">BBH", _CMD_SET_RX, timeout >> 16, timeout)
self._cmd(">BBH", _CMD_SET_RX, timeout >> 16, timeout) # 24 bits

return self._dio1

Expand Down Expand Up @@ -725,10 +729,10 @@ def _cmd(self, fmt, *write_args, n_read=0, write_buf=None, read_buf=None):
return res

def _reg_read(self, addr):
return self._cmd("BBBB", _CMD_READ_REGISTER, addr >> 8, addr & 0xFF, n_read=1)[0]
return self._cmd(">BHB", _CMD_READ_REGISTER, addr, 0, n_read=1)[0]

def _reg_write(self, addr, val):
return self._cmd("BBBB", _CMD_WRITE_REGISTER, addr >> 8, addr & 0xFF, val & 0xFF)
return self._cmd(">BHB", _CMD_WRITE_REGISTER, addr, val & 0xFF)


class _SX1262(_SX126x):
Expand Down
2 changes: 1 addition & 1 deletion micropython/lora/lora-sx126x/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="0.1.1")
metadata(version="0.1.2")
require("lora")
package("lora")
3 changes: 3 additions & 0 deletions micropython/lora/lora-sx127x/lora/sx127x.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ def configure(self, lora_cfg):

self._reg_update(_REG_MODEM_CONFIG3, update_mask, modem_config3)

if "syncword" in lora_cfg:
self._reg_write(_REG_SYNC_WORD, lora_cfg["syncword"])

def _reg_write(self, reg, value):
self._cs(0)
if isinstance(value, int):
Expand Down
2 changes: 1 addition & 1 deletion micropython/lora/lora-sx127x/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="0.1.0")
metadata(version="0.1.1")
require("lora")
package("lora")
2 changes: 1 addition & 1 deletion micropython/lora/lora-sync/lora/sync_modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def send(self, packet, tx_at_ms=None):

tx = True
while tx is True:
tx = self.poll_send()
self._sync_wait(will_irq)
tx = self.poll_send()
return tx

def recv(self, timeout_ms=None, rx_length=0xFF, rx_packet=None):
Expand Down
2 changes: 1 addition & 1 deletion micropython/lora/lora-sync/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="0.1.0")
metadata(version="0.1.1")
require("lora")
package("lora")
7 changes: 4 additions & 3 deletions micropython/lora/lora/lora/modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def __init__(self, ant_sw):
self._ant_sw = ant_sw
self._irq_callback = None

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