2017-10-15 13:26:58 -04:00
|
|
|
"""Test for nrf24l01 module. Portable between MicroPython targets."""
|
2014-10-02 14:36:56 -04:00
|
|
|
|
2020-06-18 05:19:14 -04:00
|
|
|
import usys
|
2017-10-15 13:26:58 -04:00
|
|
|
import ustruct as struct
|
2017-10-03 04:59:28 -04:00
|
|
|
import utime
|
|
|
|
from machine import Pin, SPI
|
2014-10-02 14:36:56 -04:00
|
|
|
from nrf24l01 import NRF24L01
|
2017-10-15 13:26:58 -04:00
|
|
|
from micropython import const
|
|
|
|
|
|
|
|
# Slave pause between receiving data and checking for further packets.
|
|
|
|
_RX_POLL_DELAY = const(15)
|
|
|
|
# Slave pauses an additional _SLAVE_SEND_DELAY ms after receiving data and before
|
|
|
|
# transmitting to allow the (remote) master time to get into receive mode. The
|
|
|
|
# master may be a slow device. Value tested with Pyboard, ESP32 and ESP8266.
|
|
|
|
_SLAVE_SEND_DELAY = const(10)
|
|
|
|
|
2020-06-18 05:19:14 -04:00
|
|
|
if usys.platform == "pyboard":
|
2020-02-26 23:36:53 -05:00
|
|
|
cfg = {"spi": 2, "miso": "Y7", "mosi": "Y8", "sck": "Y6", "csn": "Y5", "ce": "Y4"}
|
2020-06-18 05:19:14 -04:00
|
|
|
elif usys.platform == "esp8266": # Hardware SPI
|
2020-02-26 23:36:53 -05:00
|
|
|
cfg = {"spi": 1, "miso": 12, "mosi": 13, "sck": 14, "csn": 4, "ce": 5}
|
2020-06-18 05:19:14 -04:00
|
|
|
elif usys.platform == "esp32": # Software SPI
|
2020-02-26 23:36:53 -05:00
|
|
|
cfg = {"spi": -1, "miso": 32, "mosi": 33, "sck": 25, "csn": 26, "ce": 27}
|
2017-10-15 13:26:58 -04:00
|
|
|
else:
|
2020-06-18 05:19:14 -04:00
|
|
|
raise ValueError("Unsupported platform {}".format(usys.platform))
|
2014-10-02 14:36:56 -04:00
|
|
|
|
2020-02-11 01:31:16 -05:00
|
|
|
# Addresses are in little-endian format. They correspond to big-endian
|
|
|
|
# 0xf0f0f0f0e1, 0xf0f0f0f0d2
|
2020-02-26 23:36:53 -05:00
|
|
|
pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0")
|
|
|
|
|
2014-10-02 14:36:56 -04:00
|
|
|
|
|
|
|
def master():
|
2020-02-26 23:36:53 -05:00
|
|
|
csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
|
|
|
|
ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
|
|
|
|
if cfg["spi"] == -1:
|
|
|
|
spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"]))
|
2017-10-15 13:26:58 -04:00
|
|
|
nrf = NRF24L01(spi, csn, ce, payload_size=8)
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8)
|
2014-10-02 14:36:56 -04:00
|
|
|
|
|
|
|
nrf.open_tx_pipe(pipes[0])
|
|
|
|
nrf.open_rx_pipe(1, pipes[1])
|
|
|
|
nrf.start_listening()
|
|
|
|
|
|
|
|
num_needed = 16
|
|
|
|
num_successes = 0
|
|
|
|
num_failures = 0
|
|
|
|
led_state = 0
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("NRF24L01 master mode, sending %d packets..." % num_needed)
|
2014-10-02 14:36:56 -04:00
|
|
|
|
|
|
|
while num_successes < num_needed and num_failures < num_needed:
|
|
|
|
# stop listening and send packet
|
|
|
|
nrf.stop_listening()
|
2017-10-03 04:59:28 -04:00
|
|
|
millis = utime.ticks_ms()
|
2020-02-26 23:36:53 -05:00
|
|
|
led_state = max(1, (led_state << 1) & 0x0F)
|
|
|
|
print("sending:", millis, led_state)
|
2014-10-02 14:36:56 -04:00
|
|
|
try:
|
2020-02-26 23:36:53 -05:00
|
|
|
nrf.send(struct.pack("ii", millis, led_state))
|
2014-10-02 14:36:56 -04:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# start listening again
|
|
|
|
nrf.start_listening()
|
|
|
|
|
|
|
|
# wait for response, with 250ms timeout
|
2017-10-03 04:59:28 -04:00
|
|
|
start_time = utime.ticks_ms()
|
2014-10-02 14:36:56 -04:00
|
|
|
timeout = False
|
|
|
|
while not nrf.any() and not timeout:
|
2017-10-03 04:59:28 -04:00
|
|
|
if utime.ticks_diff(utime.ticks_ms(), start_time) > 250:
|
2014-10-02 14:36:56 -04:00
|
|
|
timeout = True
|
|
|
|
|
|
|
|
if timeout:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("failed, response timed out")
|
2014-10-02 14:36:56 -04:00
|
|
|
num_failures += 1
|
|
|
|
|
|
|
|
else:
|
|
|
|
# recv packet
|
2020-02-26 23:36:53 -05:00
|
|
|
(got_millis,) = struct.unpack("i", nrf.recv())
|
2014-10-02 14:36:56 -04:00
|
|
|
|
|
|
|
# print response and round-trip delay
|
2020-02-26 23:36:53 -05:00
|
|
|
print(
|
|
|
|
"got response:",
|
|
|
|
got_millis,
|
|
|
|
"(delay",
|
|
|
|
utime.ticks_diff(utime.ticks_ms(), got_millis),
|
|
|
|
"ms)",
|
|
|
|
)
|
2014-10-02 14:36:56 -04:00
|
|
|
num_successes += 1
|
|
|
|
|
|
|
|
# delay then loop
|
2017-10-03 04:59:28 -04:00
|
|
|
utime.sleep_ms(250)
|
2014-10-02 14:36:56 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("master finished sending; successes=%d, failures=%d" % (num_successes, num_failures))
|
|
|
|
|
2014-10-02 14:36:56 -04:00
|
|
|
|
|
|
|
def slave():
|
2020-02-26 23:36:53 -05:00
|
|
|
csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
|
|
|
|
ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
|
|
|
|
if cfg["spi"] == -1:
|
|
|
|
spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"]))
|
2017-10-15 13:26:58 -04:00
|
|
|
nrf = NRF24L01(spi, csn, ce, payload_size=8)
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8)
|
2014-10-02 14:36:56 -04:00
|
|
|
|
|
|
|
nrf.open_tx_pipe(pipes[1])
|
|
|
|
nrf.open_rx_pipe(1, pipes[0])
|
|
|
|
nrf.start_listening()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)")
|
2014-10-02 14:36:56 -04:00
|
|
|
|
|
|
|
while True:
|
|
|
|
if nrf.any():
|
|
|
|
while nrf.any():
|
|
|
|
buf = nrf.recv()
|
2020-02-26 23:36:53 -05:00
|
|
|
millis, led_state = struct.unpack("ii", buf)
|
|
|
|
print("received:", millis, led_state)
|
2017-10-03 04:59:28 -04:00
|
|
|
for led in leds:
|
|
|
|
if led_state & 1:
|
|
|
|
led.on()
|
2014-10-02 14:36:56 -04:00
|
|
|
else:
|
2017-10-03 04:59:28 -04:00
|
|
|
led.off()
|
|
|
|
led_state >>= 1
|
2017-10-15 13:26:58 -04:00
|
|
|
utime.sleep_ms(_RX_POLL_DELAY)
|
2014-10-02 14:36:56 -04:00
|
|
|
|
2017-10-15 13:26:58 -04:00
|
|
|
# Give master time to get into receive mode.
|
|
|
|
utime.sleep_ms(_SLAVE_SEND_DELAY)
|
2014-10-02 14:36:56 -04:00
|
|
|
nrf.stop_listening()
|
|
|
|
try:
|
2020-02-26 23:36:53 -05:00
|
|
|
nrf.send(struct.pack("i", millis))
|
2014-10-02 14:36:56 -04:00
|
|
|
except OSError:
|
|
|
|
pass
|
2020-02-26 23:36:53 -05:00
|
|
|
print("sent response")
|
2014-10-02 14:36:56 -04:00
|
|
|
nrf.start_listening()
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2017-10-03 04:59:28 -04:00
|
|
|
try:
|
|
|
|
import pyb
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2017-10-03 04:59:28 -04:00
|
|
|
leds = [pyb.LED(i + 1) for i in range(4)]
|
|
|
|
except:
|
|
|
|
leds = []
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
print("NRF24L01 test module loaded")
|
|
|
|
print("NRF24L01 pinout for test:")
|
|
|
|
print(" CE on", cfg["ce"])
|
|
|
|
print(" CSN on", cfg["csn"])
|
|
|
|
print(" SCK on", cfg["sck"])
|
|
|
|
print(" MISO on", cfg["miso"])
|
|
|
|
print(" MOSI on", cfg["mosi"])
|
|
|
|
print("run nrf24l01test.slave() on slave, then nrf24l01test.master() on master")
|