Skip to content

Commit ccaa5f5

Browse files
peterhinchdpgeorge
authored andcommitted
drivers/nrf24l01: Make driver and test run on pyboard, ESP8266, ESP32.
1 parent 31550a5 commit ccaa5f5

2 files changed

Lines changed: 46 additions & 16 deletions

File tree

drivers/nrf24l01/nrf24l01.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ def __init__(self, spi, cs, ce, channel=46, payload_size=16):
6262

6363
# init the SPI bus and pins
6464
self.init_spi(4000000)
65-
cs.init(cs.OUT, value=1)
66-
ce.init(ce.OUT, value=0)
6765

6866
# reset everything
69-
self.ce(0)
70-
self.cs(1)
67+
ce.init(ce.OUT, value=0)
68+
cs.init(cs.OUT, value=1)
69+
7170
self.payload_size = payload_size
7271
self.pipe0_read_addr = None
7372
utime.sleep_ms(5)
@@ -215,7 +214,7 @@ def recv(self):
215214

216215
# blocking wait for tx complete
217216
def send(self, buf, timeout=500):
218-
send_nonblock = self.send_start(buf)
217+
self.send_start(buf)
219218
start = utime.ticks_ms()
220219
result = None
221220
while result is None and utime.ticks_diff(utime.ticks_ms(), start) < timeout:

drivers/nrf24l01/nrf24l01test.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
"""Test for nrf24l01 module."""
1+
"""Test for nrf24l01 module. Portable between MicroPython targets."""
22

3-
import struct
3+
import sys
4+
import ustruct as struct
45
import utime
56
from machine import Pin, SPI
67
from nrf24l01 import NRF24L01
8+
from micropython import const
9+
10+
# Slave pause between receiving data and checking for further packets.
11+
_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)
16+
17+
if sys.platform == 'pyboard':
18+
cfg = {'spi': 2, 'miso': 'Y7', 'mosi': 'Y8', 'sck': 'Y6', 'csn': 'Y5', 'ce': 'Y4'}
19+
elif sys.platform == 'esp8266': # Hardware SPI
20+
cfg = {'spi': 1, 'miso': 12, 'mosi': 13, 'sck': 14, 'csn': 4, 'ce': 5}
21+
elif sys.platform == 'esp32': # Software SPI
22+
cfg = {'spi': -1, 'miso': 32, 'mosi': 33, 'sck': 25, 'csn': 26, 'ce': 27}
23+
else:
24+
raise ValueError('Unsupported platform {}'.format(sys.platform))
725

826
pipes = (b'\xf0\xf0\xf0\xf0\xe1', b'\xf0\xf0\xf0\xf0\xd2')
927

1028
def master():
11-
nrf = NRF24L01(SPI(2), Pin('Y5'), Pin('Y4'), payload_size=8)
29+
csn = Pin(cfg['csn'], mode=Pin.OUT, value=1)
30+
ce = Pin(cfg['ce'], mode=Pin.OUT, value=0)
31+
if cfg['spi'] == -1:
32+
spi = SPI(-1, sck=Pin(cfg['sck']), mosi=Pin(cfg['mosi']), miso=Pin(cfg['miso']))
33+
nrf = NRF24L01(spi, csn, ce, payload_size=8)
34+
else:
35+
nrf = NRF24L01(SPI(cfg['spi']), csn, ce, payload_size=8)
1236

1337
nrf.open_tx_pipe(pipes[0])
1438
nrf.open_rx_pipe(1, pipes[1])
@@ -60,7 +84,13 @@ def master():
6084
print('master finished sending; successes=%d, failures=%d' % (num_successes, num_failures))
6185

6286
def slave():
63-
nrf = NRF24L01(SPI(2), Pin('Y5'), Pin('Y4'), payload_size=8)
87+
csn = Pin(cfg['csn'], mode=Pin.OUT, value=1)
88+
ce = Pin(cfg['ce'], mode=Pin.OUT, value=0)
89+
if cfg['spi'] == -1:
90+
spi = SPI(-1, sck=Pin(cfg['sck']), mosi=Pin(cfg['mosi']), miso=Pin(cfg['miso']))
91+
nrf = NRF24L01(spi, csn, ce, payload_size=8)
92+
else:
93+
nrf = NRF24L01(SPI(cfg['spi']), csn, ce, payload_size=8)
6494

6595
nrf.open_tx_pipe(pipes[1])
6696
nrf.open_rx_pipe(1, pipes[0])
@@ -69,7 +99,6 @@ def slave():
6999
print('NRF24L01 slave mode, waiting for packets... (ctrl-C to stop)')
70100

71101
while True:
72-
machine.idle()
73102
if nrf.any():
74103
while nrf.any():
75104
buf = nrf.recv()
@@ -81,8 +110,10 @@ def slave():
81110
else:
82111
led.off()
83112
led_state >>= 1
84-
utime.sleep_ms(15)
113+
utime.sleep_ms(_RX_POLL_DELAY)
85114

115+
# Give master time to get into receive mode.
116+
utime.sleep_ms(_SLAVE_SEND_DELAY)
86117
nrf.stop_listening()
87118
try:
88119
nrf.send(struct.pack('i', millis))
@@ -99,9 +130,9 @@ def slave():
99130

100131
print('NRF24L01 test module loaded')
101132
print('NRF24L01 pinout for test:')
102-
print(' CE on Y4')
103-
print(' CSN on Y5')
104-
print(' SCK on Y6')
105-
print(' MISO on Y7')
106-
print(' MOSI on Y8')
133+
print(' CE on', cfg['ce'])
134+
print(' CSN on', cfg['csn'])
135+
print(' SCK on', cfg['sck'])
136+
print(' MISO on', cfg['miso'])
137+
print(' MOSI on', cfg['mosi'])
107138
print('run nrf24l01test.slave() on slave, then nrf24l01test.master() on master')

0 commit comments

Comments
 (0)