drivers/nrf24l01: Make driver and test run on pyboard, ESP8266, ESP32.
This commit is contained in:
parent
31550a52e4
commit
ccaa5f5b0b
@ -62,12 +62,11 @@ class NRF24L01:
|
||||
|
||||
# init the SPI bus and pins
|
||||
self.init_spi(4000000)
|
||||
cs.init(cs.OUT, value=1)
|
||||
ce.init(ce.OUT, value=0)
|
||||
|
||||
# reset everything
|
||||
self.ce(0)
|
||||
self.cs(1)
|
||||
ce.init(ce.OUT, value=0)
|
||||
cs.init(cs.OUT, value=1)
|
||||
|
||||
self.payload_size = payload_size
|
||||
self.pipe0_read_addr = None
|
||||
utime.sleep_ms(5)
|
||||
@ -215,7 +214,7 @@ class NRF24L01:
|
||||
|
||||
# blocking wait for tx complete
|
||||
def send(self, buf, timeout=500):
|
||||
send_nonblock = self.send_start(buf)
|
||||
self.send_start(buf)
|
||||
start = utime.ticks_ms()
|
||||
result = None
|
||||
while result is None and utime.ticks_diff(utime.ticks_ms(), start) < timeout:
|
||||
|
@ -1,14 +1,38 @@
|
||||
"""Test for nrf24l01 module."""
|
||||
"""Test for nrf24l01 module. Portable between MicroPython targets."""
|
||||
|
||||
import struct
|
||||
import sys
|
||||
import ustruct as struct
|
||||
import utime
|
||||
from machine import Pin, SPI
|
||||
from nrf24l01 import NRF24L01
|
||||
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)
|
||||
|
||||
if sys.platform == 'pyboard':
|
||||
cfg = {'spi': 2, 'miso': 'Y7', 'mosi': 'Y8', 'sck': 'Y6', 'csn': 'Y5', 'ce': 'Y4'}
|
||||
elif sys.platform == 'esp8266': # Hardware SPI
|
||||
cfg = {'spi': 1, 'miso': 12, 'mosi': 13, 'sck': 14, 'csn': 4, 'ce': 5}
|
||||
elif sys.platform == 'esp32': # Software SPI
|
||||
cfg = {'spi': -1, 'miso': 32, 'mosi': 33, 'sck': 25, 'csn': 26, 'ce': 27}
|
||||
else:
|
||||
raise ValueError('Unsupported platform {}'.format(sys.platform))
|
||||
|
||||
pipes = (b'\xf0\xf0\xf0\xf0\xe1', b'\xf0\xf0\xf0\xf0\xd2')
|
||||
|
||||
def master():
|
||||
nrf = NRF24L01(SPI(2), Pin('Y5'), Pin('Y4'), payload_size=8)
|
||||
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']))
|
||||
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_rx_pipe(1, pipes[1])
|
||||
@ -60,7 +84,13 @@ def master():
|
||||
print('master finished sending; successes=%d, failures=%d' % (num_successes, num_failures))
|
||||
|
||||
def slave():
|
||||
nrf = NRF24L01(SPI(2), Pin('Y5'), Pin('Y4'), payload_size=8)
|
||||
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']))
|
||||
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_rx_pipe(1, pipes[0])
|
||||
@ -69,7 +99,6 @@ def slave():
|
||||
print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)')
|
||||
|
||||
while True:
|
||||
machine.idle()
|
||||
if nrf.any():
|
||||
while nrf.any():
|
||||
buf = nrf.recv()
|
||||
@ -81,8 +110,10 @@ def slave():
|
||||
else:
|
||||
led.off()
|
||||
led_state >>= 1
|
||||
utime.sleep_ms(15)
|
||||
utime.sleep_ms(_RX_POLL_DELAY)
|
||||
|
||||
# Give master time to get into receive mode.
|
||||
utime.sleep_ms(_SLAVE_SEND_DELAY)
|
||||
nrf.stop_listening()
|
||||
try:
|
||||
nrf.send(struct.pack('i', millis))
|
||||
@ -99,9 +130,9 @@ except:
|
||||
|
||||
print('NRF24L01 test module loaded')
|
||||
print('NRF24L01 pinout for test:')
|
||||
print(' CE on Y4')
|
||||
print(' CSN on Y5')
|
||||
print(' SCK on Y6')
|
||||
print(' MISO on Y7')
|
||||
print(' MOSI on Y8')
|
||||
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')
|
||||
|
Loading…
x
Reference in New Issue
Block a user