kopia lustrzana https://github.com/micropython/micropython-lib
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.pull/507/head
rodzic
4556023a0c
commit
d1aaec7174
|
@ -3,23 +3,29 @@
|
||||||
import usys
|
import usys
|
||||||
import ustruct as struct
|
import ustruct as struct
|
||||||
import utime
|
import utime
|
||||||
from machine import Pin, SPI
|
from machine import Pin, SPI, SoftSPI
|
||||||
from nrf24l01 import NRF24L01
|
from nrf24l01 import NRF24L01
|
||||||
from micropython import const
|
from micropython import const
|
||||||
|
|
||||||
# Slave pause between receiving data and checking for further packets.
|
# Responder pause between receiving data and checking for further packets.
|
||||||
_RX_POLL_DELAY = const(15)
|
_RX_POLL_DELAY = const(15)
|
||||||
# Slave pauses an additional _SLAVE_SEND_DELAY ms after receiving data and before
|
# Responder pauses an additional _RESPONER_SEND_DELAY ms after receiving data and before
|
||||||
# transmitting to allow the (remote) master time to get into receive mode. The
|
# transmitting to allow the (remote) initiator time to get into receive mode. The
|
||||||
# master may be a slow device. Value tested with Pyboard, ESP32 and ESP8266.
|
# initiator may be a slow device. Value tested with Pyboard, ESP32 and ESP8266.
|
||||||
_SLAVE_SEND_DELAY = const(10)
|
_RESPONDER_SEND_DELAY = const(10)
|
||||||
|
|
||||||
if usys.platform == "pyboard":
|
if usys.platform == "pyboard":
|
||||||
cfg = {"spi": 2, "miso": "Y7", "mosi": "Y8", "sck": "Y6", "csn": "Y5", "ce": "Y4"}
|
spi = SPI(2) # miso : Y7, mosi : Y8, sck : Y6
|
||||||
|
cfg = {"spi": spi, "csn": "Y5", "ce": "Y4"}
|
||||||
elif usys.platform == "esp8266": # Hardware SPI
|
elif usys.platform == "esp8266": # Hardware SPI
|
||||||
cfg = {"spi": 1, "miso": 12, "mosi": 13, "sck": 14, "csn": 4, "ce": 5}
|
spi = SPI(1) # miso : 12, mosi : 13, sck : 14
|
||||||
|
cfg = {"spi": spi, "csn": 4, "ce": 5}
|
||||||
elif usys.platform == "esp32": # Software SPI
|
elif usys.platform == "esp32": # Software SPI
|
||||||
cfg = {"spi": -1, "miso": 32, "mosi": 33, "sck": 25, "csn": 26, "ce": 27}
|
spi = SoftSPI(sck=Pin(25), mosi=Pin(33), miso=Pin(32))
|
||||||
|
cfg = {"spi": spi, "csn": 26, "ce": 27}
|
||||||
|
elif usys.platform == "rp2": # Hardware SPI with explicit pin definitions
|
||||||
|
spi = SPI(0, sck=Pin(2), mosi=Pin(3), miso=Pin(4))
|
||||||
|
cfg = {"spi": spi, "csn": 5, "ce": 6}
|
||||||
else:
|
else:
|
||||||
raise ValueError("Unsupported platform {}".format(usys.platform))
|
raise ValueError("Unsupported platform {}".format(usys.platform))
|
||||||
|
|
||||||
|
@ -28,14 +34,11 @@ else:
|
||||||
pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0")
|
pipes = (b"\xe1\xf0\xf0\xf0\xf0", b"\xd2\xf0\xf0\xf0\xf0")
|
||||||
|
|
||||||
|
|
||||||
def master():
|
def initiator():
|
||||||
csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
|
csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
|
||||||
ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
|
ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
|
||||||
if cfg["spi"] == -1:
|
spi = cfg["spi"]
|
||||||
spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"]))
|
|
||||||
nrf = NRF24L01(spi, csn, ce, payload_size=8)
|
nrf = NRF24L01(spi, csn, ce, payload_size=8)
|
||||||
else:
|
|
||||||
nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8)
|
|
||||||
|
|
||||||
nrf.open_tx_pipe(pipes[0])
|
nrf.open_tx_pipe(pipes[0])
|
||||||
nrf.open_rx_pipe(1, pipes[1])
|
nrf.open_rx_pipe(1, pipes[1])
|
||||||
|
@ -46,7 +49,7 @@ def master():
|
||||||
num_failures = 0
|
num_failures = 0
|
||||||
led_state = 0
|
led_state = 0
|
||||||
|
|
||||||
print("NRF24L01 master mode, sending %d packets..." % num_needed)
|
print("NRF24L01 initiator mode, sending %d packets..." % num_needed)
|
||||||
|
|
||||||
while num_successes < num_needed and num_failures < num_needed:
|
while num_successes < num_needed and num_failures < num_needed:
|
||||||
# stop listening and send packet
|
# stop listening and send packet
|
||||||
|
@ -90,23 +93,20 @@ def master():
|
||||||
# delay then loop
|
# delay then loop
|
||||||
utime.sleep_ms(250)
|
utime.sleep_ms(250)
|
||||||
|
|
||||||
print("master finished sending; successes=%d, failures=%d" % (num_successes, num_failures))
|
print("initiator finished sending; successes=%d, failures=%d" % (num_successes, num_failures))
|
||||||
|
|
||||||
|
|
||||||
def slave():
|
def responder():
|
||||||
csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
|
csn = Pin(cfg["csn"], mode=Pin.OUT, value=1)
|
||||||
ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
|
ce = Pin(cfg["ce"], mode=Pin.OUT, value=0)
|
||||||
if cfg["spi"] == -1:
|
spi = cfg["spi"]
|
||||||
spi = SPI(-1, sck=Pin(cfg["sck"]), mosi=Pin(cfg["mosi"]), miso=Pin(cfg["miso"]))
|
|
||||||
nrf = NRF24L01(spi, csn, ce, payload_size=8)
|
nrf = NRF24L01(spi, csn, ce, payload_size=8)
|
||||||
else:
|
|
||||||
nrf = NRF24L01(SPI(cfg["spi"]), csn, ce, payload_size=8)
|
|
||||||
|
|
||||||
nrf.open_tx_pipe(pipes[1])
|
nrf.open_tx_pipe(pipes[1])
|
||||||
nrf.open_rx_pipe(1, pipes[0])
|
nrf.open_rx_pipe(1, pipes[0])
|
||||||
nrf.start_listening()
|
nrf.start_listening()
|
||||||
|
|
||||||
print("NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)")
|
print("NRF24L01 responder mode, waiting for packets... (ctrl-C to stop)")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if nrf.any():
|
if nrf.any():
|
||||||
|
@ -122,8 +122,8 @@ def slave():
|
||||||
led_state >>= 1
|
led_state >>= 1
|
||||||
utime.sleep_ms(_RX_POLL_DELAY)
|
utime.sleep_ms(_RX_POLL_DELAY)
|
||||||
|
|
||||||
# Give master time to get into receive mode.
|
# Give initiator time to get into receive mode.
|
||||||
utime.sleep_ms(_SLAVE_SEND_DELAY)
|
utime.sleep_ms(_RESPONDER_SEND_DELAY)
|
||||||
nrf.stop_listening()
|
nrf.stop_listening()
|
||||||
try:
|
try:
|
||||||
nrf.send(struct.pack("i", millis))
|
nrf.send(struct.pack("i", millis))
|
||||||
|
@ -144,7 +144,5 @@ print("NRF24L01 test module loaded")
|
||||||
print("NRF24L01 pinout for test:")
|
print("NRF24L01 pinout for test:")
|
||||||
print(" CE on", cfg["ce"])
|
print(" CE on", cfg["ce"])
|
||||||
print(" CSN on", cfg["csn"])
|
print(" CSN on", cfg["csn"])
|
||||||
print(" SCK on", cfg["sck"])
|
print(" SPI on", cfg["spi"])
|
||||||
print(" MISO on", cfg["miso"])
|
print("run nrf24l01test.responder() on responder, then nrf24l01test.initiator() on initiator")
|
||||||
print(" MOSI on", cfg["mosi"])
|
|
||||||
print("run nrf24l01test.slave() on slave, then nrf24l01test.master() on master")
|
|
||||||
|
|
Ładowanie…
Reference in New Issue