Skip to content

Commit

Permalink
lora-sx126x: Change to class-level memoryview for _cmd buf.
Browse files Browse the repository at this point in the history
Currently, the LoRa SX126x driver dynamically creates at least one,
sometimes two, memoryview objects with each call to `_cmd`.  This commit
simply provides the class with a long-lived memoryview object for `_cmd` to
easily slice as necessary.

Unlike the SX127x chips, Semtech unfortunately designed the SX126x modems
to be more command-centric (as opposed to directly setting registers).
Given the amount `_cmd` is called during normal device operation, even a
minor improvement here should have a decent impact.

Basic TX and RX tests pass on hardware.

Signed-off-by: Max Holliday <[email protected]>
  • Loading branch information
maholli authored and dpgeorge committed Jul 23, 2024
1 parent fbf7e12 commit 60d1370
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions micropython/lora/lora-sx126x/lora/sx126x.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __init__(
dio3_tcxo_start_time_us if dio3_tcxo_millivolts else 0
)

self._buf = bytearray(9) # shared buffer for commands
self._buf_view = memoryview(bytearray(9)) # shared buffer for commands

# These settings are kept in the object (as can't read them back from the modem)
self._output_power = 14
Expand Down Expand Up @@ -704,11 +704,11 @@ def _cmd(self, fmt, *write_args, n_read=0, write_buf=None, read_buf=None):
# have happened well before _cmd() is called again.
self._wait_not_busy(self._busy_timeout)

# Pack write_args into _buf and wrap a memoryview of the correct length around it
# Pack write_args into slice of _buf_view memoryview of correct length
wrlen = struct.calcsize(fmt)
assert n_read + wrlen <= len(self._buf) # if this fails, make _buf bigger!
struct.pack_into(fmt, self._buf, 0, *write_args)
buf = memoryview(self._buf)[: (wrlen + n_read)]
assert n_read + wrlen <= len(self._buf_view) # if this fails, make _buf bigger!
struct.pack_into(fmt, self._buf_view, 0, *write_args)
buf = self._buf_view[: (wrlen + n_read)]

if _DEBUG:
print(">>> {}".format(buf[:wrlen].hex()))
Expand All @@ -723,7 +723,7 @@ def _cmd(self, fmt, *write_args, n_read=0, write_buf=None, read_buf=None):
self._cs(1)

if n_read > 0:
res = memoryview(buf)[wrlen : (wrlen + n_read)] # noqa: E203
res = self._buf_view[wrlen : (wrlen + n_read)] # noqa: E203
if _DEBUG:
print("<<< {}".format(res.hex()))
return res
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.2")
metadata(version="0.1.3")
require("lora")
package("lora")

0 comments on commit 60d1370

Please sign in to comment.