Skip to content

Commit

Permalink
drivers/nrf24l01: Make driver and test run on pyboard, ESP8266, ESP32.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhinch authored and dpgeorge committed Nov 20, 2017
1 parent 31550a5 commit ccaa5f5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
9 changes: 4 additions & 5 deletions drivers/nrf24l01/nrf24l01.py
Expand Up @@ -62,12 +62,11 @@ def __init__(self, spi, cs, ce, channel=46, payload_size=16):

# 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)
Expand Down Expand Up @@ -215,7 +214,7 @@ def recv(self):

# 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:
Expand Down
53 changes: 42 additions & 11 deletions drivers/nrf24l01/nrf24l01test.py
@@ -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])
Expand Down Expand Up @@ -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])
Expand All @@ -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()
Expand All @@ -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))
Expand All @@ -99,9 +130,9 @@ def slave():

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')

0 comments on commit ccaa5f5

Please sign in to comment.