Skip to content

Commit d1aaec7

Browse files
authored
nrf24l01: Improve test to add RP2 support, fix ESP32.
Use explicit pin numbers to instantiate the SPI interface on RP2. On ESP32 use SoftSPI(...) rather than SPI(-1, ...). Update terminology to initiator/responder. Tested with two Pico boards.
1 parent 4556023 commit d1aaec7

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

micropython/drivers/radio/nrf24l01/nrf24l01test.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,29 @@
33
import usys
44
import ustruct as struct
55
import utime
6-
from machine import Pin, SPI
6+
from machine import Pin, SPI, SoftSPI
77
from nrf24l01 import NRF24L01
88
from micropython import const
99

10-
# Slave pause between receiving data and checking for further packets.
10+
# Responder pause between receiving data and checking for further packets.
1111
_RX_POLL_DELAY = const(15)
12-
# Slave pauses an additional _SLAVE_SEND_DELAY ms after receiving data and before
13-
# transmitting to allow the (remote) master time to get into receive mode. The
14-
# master may be a slow device. Value tested with Pyboard, ESP32 and ESP8266.
15-
_SLAVE_SEND_DELAY = const(10)
12+
# Responder pauses an additional _RESPONER_SEND_DELAY ms after receiving data and before
13+
# transmitting to allow the (remote) initiator time to get into receive mode. The
14+
# initiator may be a slow device. Value tested with Pyboard, ESP32 and ESP8266.
15+
_RESPONDER_SEND_DELAY = const(10)
1616

1717
if usys.platform == "pyboard":
18-
cfg = {"spi": 2, "miso": "Y7", "mosi": "Y8", "sck": "Y6", "csn": "Y5", "ce": "Y4"}
18+
spi = SPI(2) # miso : Y7, mosi : Y8, sck : Y6
19+
cfg = {"spi": spi, "csn": "Y5", "ce": "Y4"}
1920
elif usys.platform == "esp8266": # Hardware SPI
20-
cfg = {"spi": 1, "miso": 12, "mosi": 13, "sck": 14, "csn": 4, "ce": 5}
21+
spi = SPI(1) # miso : 12, mosi : 13, sck : 14
22+
cfg = {"spi": spi, "csn": 4, "ce": 5}
2123
elif usys.platform == "esp32": # Software SPI
22-
cfg = {"spi": -1, "miso": 32, "mosi": 33, "sck": 25, "csn": 26, "ce": 27}
24+
spi = SoftSPI(sck=Pin(25), mosi=Pin(33), miso=Pin(32))
25+
cfg = {"spi": spi, "csn": 26, "ce": 27}
26+
elif usys.platform == "rp2": # Hardware SPI with explicit pin definitions
27+
spi = SPI(0, sck=Pin(2), mosi=Pin(3), miso=Pin(4))
28+
cfg = {"spi": spi, "csn": 5, "ce": 6}
2329
else:
2430
raise ValueError("Unsupported platform {}".format(usys.platform))
2531

@@ -28,14 +34,11 @@
2834
pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0")
2935

3036

31-
def master():
37+
def initiator():
3238
csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
3339
ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
34-
if cfg["spi"] == -1:
35-
spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"]))
36-
nrf = NRF24L01(spi, csn, ce, payload_size=8)
37-
else:
38-
nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8)
40+
spi = cfg["spi"]
41+
nrf = NRF24L01(spi, csn, ce, payload_size=8)
3942

4043
nrf.open_tx_pipe(pipes[0])
4144
nrf.open_rx_pipe(1, pipes[1])
@@ -46,7 +49,7 @@ def master():
4649
num_failures = 0
4750
led_state = 0
4851

49-
print("NRF24L01 master mode, sending %d packets..." % num_needed)
52+
print("NRF24L01 initiator mode, sending %d packets..." % num_needed)
5053

5154
while num_successes < num_needed and num_failures < num_needed:
5255
# stop listening and send packet
@@ -90,23 +93,20 @@ def master():
9093
# delay then loop
9194
utime.sleep_ms(250)
9295

93-
print("master finished sending; successes=%d, failures=%d" % (num_successes, num_failures))
96+
print("initiator finished sending; successes=%d, failures=%d" % (num_successes, num_failures))
9497

9598

96-
def slave():
99+
def responder():
97100
csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
98101
ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
99-
if cfg["spi"] == -1:
100-
spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"]))
101-
nrf = NRF24L01(spi, csn, ce, payload_size=8)
102-
else:
103-
nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8)
102+
spi = cfg["spi"]
103+
nrf = NRF24L01(spi, csn, ce, payload_size=8)
104104

105105
nrf.open_tx_pipe(pipes[1])
106106
nrf.open_rx_pipe(1, pipes[0])
107107
nrf.start_listening()
108108

109-
print("NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)")
109+
print("NRF24L01 responder mode, waiting for packets... (ctrl-C to stop)")
110110

111111
while True:
112112
if nrf.any():
@@ -122,8 +122,8 @@ def slave():
122122
led_state >>= 1
123123
utime.sleep_ms(_RX_POLL_DELAY)
124124

125-
# Give master time to get into receive mode.
126-
utime.sleep_ms(_SLAVE_SEND_DELAY)
125+
# Give initiator time to get into receive mode.
126+
utime.sleep_ms(_RESPONDER_SEND_DELAY)
127127
nrf.stop_listening()
128128
try:
129129
nrf.send(struct.pack("i", millis))
@@ -144,7 +144,5 @@ def slave():
144144
print("NRF24L01 pinout for test:")
145145
print(" CE on", cfg["ce"])
146146
print(" CSN on", cfg["csn"])
147-
print(" SCK on", cfg["sck"])
148-
print(" MISO on", cfg["miso"])
149-
print(" MOSI on", cfg["mosi"])
150-
print("run nrf24l01test.slave() on slave, then nrf24l01test.master() on master")
147+
print(" SPI on", cfg["spi"])
148+
print("run nrf24l01test.responder() on responder, then nrf24l01test.initiator() on initiator")

0 commit comments

Comments
 (0)