Skip to content

Commit 0dc8cb3

Browse files
committed
use SPI directly for 1-byte commands
1 parent a4a7af0 commit 0dc8cb3

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

circuitpython_nrf24l01/rf24.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,13 @@ def ce_pin(self) -> bool:
167167
def ce_pin(self, val: bool):
168168
self._ce_pin.value = val
169169

170-
def _reg_read(self, reg: int, command: bool = False) -> int:
170+
def _reg_read(self, reg: int) -> int:
171171
self._out[0] = reg
172-
len = int(not command) + 1
173172
with self._spi as spi:
174173
# time.sleep(0.000005)
175-
spi.write_readinto(self._out, self._in, out_end=len, in_end=len)
176-
# if command:
177-
# if reg != 0xFF:
178-
# print("SPI command", ("%02X" % reg))
179-
# else:
180-
# print(
181-
# "SPI read", len, "byte from", ("%02X" % reg), ("%02X" % self._in[1])
182-
# )
183-
return self._in[not command]
174+
spi.write_readinto(self._out, self._in, out_end=2, in_end=2)
175+
# print("SPI read", len, "byte from", ("%02X" % reg), ("%02X" % self._in[1]))
176+
return self._in[1]
184177

185178
def _reg_read_bytes(self, reg: int, buf_len: int = 5) -> bytearray:
186179
self._out[0] = reg
@@ -379,7 +372,9 @@ def irq_df(self) -> bool:
379372

380373
def update(self) -> Literal[True]:
381374
"""This function gets an updated status byte over SPI."""
382-
self._reg_read(0xFF, command=True)
375+
self._out[0] = 0xFF
376+
with self._spi as spi:
377+
spi.write_readinto(self._out, self._in, in_end=1, out_end=1)
383378
return True
384379

385380
def clear_status_flags(
@@ -807,7 +802,9 @@ def resend(self, send_only: bool = False):
807802
if not send_only and (self._in[0] >> 1) < 6:
808803
self.flush_rx()
809804
self.clear_status_flags()
810-
# self._reg_read(0xE3, command=True)
805+
# self._out = 0xE3 # REUSE_TX command
806+
# with self._spi as spi:
807+
# spi.write_readinto(self._out, self._in, in_end=1, out_end=1)
811808
up_cnt = 0
812809
self._ce_pin.value = True
813810
while not self._in[0] & 0x30:
@@ -846,11 +843,15 @@ def write(
846843

847844
def flush_rx(self):
848845
"""Flush all 3 levels of the RX FIFO."""
849-
self._reg_read(0xE2, command=True)
846+
self._out = 0xE2
847+
with self._spi as spi:
848+
spi.write_readinto(self._out, self._in, in_end=1, out_end=1)
850849

851850
def flush_tx(self):
852851
"""Flush all 3 levels of the TX FIFO."""
853-
self._reg_read(0xE1, command=True)
852+
self._out = 0xE1
853+
with self._spi as spi:
854+
spi.write_readinto(self._out, self._in, in_end=1, out_end=1)
854855

855856
def fifo(self, about_tx: bool = False, check_empty: Optional[bool] = None):
856857
"""This provides the status of the TX/RX FIFO buffers. (read-only)"""

0 commit comments

Comments
 (0)