kopia lustrzana https://github.com/micropython/micropython
drivers/nrf24l01: Make driver and test run on pyboard, ESP8266, ESP32.
rodzic
31550a52e4
commit
ccaa5f5b0b
|
@ -62,12 +62,11 @@ class NRF24L01:
|
||||||
|
|
||||||
# init the SPI bus and pins
|
# init the SPI bus and pins
|
||||||
self.init_spi(4000000)
|
self.init_spi(4000000)
|
||||||
cs.init(cs.OUT, value=1)
|
|
||||||
ce.init(ce.OUT, value=0)
|
|
||||||
|
|
||||||
# reset everything
|
# reset everything
|
||||||
self.ce(0)
|
ce.init(ce.OUT, value=0)
|
||||||
self.cs(1)
|
cs.init(cs.OUT, value=1)
|
||||||
|
|
||||||
self.payload_size = payload_size
|
self.payload_size = payload_size
|
||||||
self.pipe0_read_addr = None
|
self.pipe0_read_addr = None
|
||||||
utime.sleep_ms(5)
|
utime.sleep_ms(5)
|
||||||
|
@ -215,7 +214,7 @@ class NRF24L01:
|
||||||
|
|
||||||
# blocking wait for tx complete
|
# blocking wait for tx complete
|
||||||
def send(self, buf, timeout=500):
|
def send(self, buf, timeout=500):
|
||||||
send_nonblock = self.send_start(buf)
|
self.send_start(buf)
|
||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
result = None
|
result = None
|
||||||
while result is None and utime.ticks_diff(utime.ticks_ms(), start) < timeout:
|
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
|
import utime
|
||||||
from machine import Pin, SPI
|
from machine import Pin, SPI
|
||||||
from nrf24l01 import NRF24L01
|
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')
|
pipes = (b'\xf0\xf0\xf0\xf0\xe1', b'\xf0\xf0\xf0\xf0\xd2')
|
||||||
|
|
||||||
def master():
|
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_tx_pipe(pipes[0])
|
||||||
nrf.open_rx_pipe(1, pipes[1])
|
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))
|
print('master finished sending; successes=%d, failures=%d' % (num_successes, num_failures))
|
||||||
|
|
||||||
def slave():
|
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_tx_pipe(pipes[1])
|
||||||
nrf.open_rx_pipe(1, pipes[0])
|
nrf.open_rx_pipe(1, pipes[0])
|
||||||
|
@ -69,7 +99,6 @@ def slave():
|
||||||
print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)')
|
print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)')
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
machine.idle()
|
|
||||||
if nrf.any():
|
if nrf.any():
|
||||||
while nrf.any():
|
while nrf.any():
|
||||||
buf = nrf.recv()
|
buf = nrf.recv()
|
||||||
|
@ -81,8 +110,10 @@ def slave():
|
||||||
else:
|
else:
|
||||||
led.off()
|
led.off()
|
||||||
led_state >>= 1
|
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()
|
nrf.stop_listening()
|
||||||
try:
|
try:
|
||||||
nrf.send(struct.pack('i', millis))
|
nrf.send(struct.pack('i', millis))
|
||||||
|
@ -99,9 +130,9 @@ except:
|
||||||
|
|
||||||
print('NRF24L01 test module loaded')
|
print('NRF24L01 test module loaded')
|
||||||
print('NRF24L01 pinout for test:')
|
print('NRF24L01 pinout for test:')
|
||||||
print(' CE on Y4')
|
print(' CE on', cfg['ce'])
|
||||||
print(' CSN on Y5')
|
print(' CSN on', cfg['csn'])
|
||||||
print(' SCK on Y6')
|
print(' SCK on', cfg['sck'])
|
||||||
print(' MISO on Y7')
|
print(' MISO on', cfg['miso'])
|
||||||
print(' MOSI on Y8')
|
print(' MOSI on', cfg['mosi'])
|
||||||
print('run nrf24l01test.slave() on slave, then nrf24l01test.master() on master')
|
print('run nrf24l01test.slave() on slave, then nrf24l01test.master() on master')
|
||||||
|
|
Ładowanie…
Reference in New Issue