Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
ccaa5f5b
Commit
ccaa5f5b
authored
7 years ago
by
Peter Hinch
Committed by
Damien George
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
drivers/nrf24l01: Make driver and test run on pyboard, ESP8266, ESP32.
parent
31550a52
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
drivers/nrf24l01/nrf24l01.py
+4
-5
4 additions, 5 deletions
drivers/nrf24l01/nrf24l01.py
drivers/nrf24l01/nrf24l01test.py
+42
-11
42 additions, 11 deletions
drivers/nrf24l01/nrf24l01test.py
with
46 additions
and
16 deletions
drivers/nrf24l01/nrf24l01.py
+
4
−
5
View file @
ccaa5f5b
...
@@ -62,12 +62,11 @@ class NRF24L01:
...
@@ -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:
...
@@ -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
:
...
...
This diff is collapsed.
Click to expand it.
drivers/nrf24l01/nrf24l01test.py
+
42
−
11
View file @
ccaa5f5b
"""
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():
...
@@ -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():
...
@@ -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():
...
@@ -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:
...
@@ -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
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment