Skip to content

Commit 04e3fcb

Browse files
bors[bot]japaric
andauthored
Merge #291
291: test the nrf52840 HAL using defmt-test r=jonas-schievink a=japaric this PR adds tests that exercise the GPIO API 3 connections are required: - P0.03 <-> GND - P0.04 <-> VDD - P0.28 <-> P0.29 the first two are used to test the Input API against reference voltages the short-circuit connection is used to test the Input and Output API using the input API that was previously tested I tried testing the Uarte API but ran into #289. It seems to me that we can't test TX/RX loopback or overrun errors with the existing `read` API Co-authored-by: Jorge Aparicio <[email protected]>
2 parents aae1794 + 88f74c3 commit 04e3fcb

File tree

13 files changed

+442
-6
lines changed

13 files changed

+442
-6
lines changed

.cargo/config

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
[alias]
22
xtask = "run -p xtask --"
3-
4-
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
5-
runner = 'arm-none-eabi-gdb'
6-
rustflags = [
7-
"-C", "link-arg=-Tlink.x",
8-
]

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ members = [
66
"nrf52832-hal",
77
"nrf52833-hal",
88
"nrf52840-hal",
9+
"nrf52840-hal-tests",
910
"nrf9160-hal",
1011
"examples/*",
1112
]
13+
exclude = ["examples/.cargo"]
1214

1315
[profile.dev]
1416
incremental = false

examples/.cargo/config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2+
runner = 'arm-none-eabi-gdb'
3+
rustflags = [
4+
"-C", "link-arg=-Tlink.x",
5+
]

nrf52840-hal-tests/.cargo/config.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[target.thumbv7em-none-eabihf]
2+
runner = "probe-run --chip nRF52840_xxAA --probe 1366:1015"
3+
rustflags = [
4+
"-C", "link-arg=--nmagic",
5+
"-C", "link-arg=-Tlink.x",
6+
"-C", "link-arg=-Tdefmt.x",
7+
"-C", "linker=flip-link",
8+
]
9+
10+
[build]
11+
target = "thumbv7em-none-eabihf"

nrf52840-hal-tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

nrf52840-hal-tests/Cargo.toml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[package]
2+
edition = "2018"
3+
name = "nrf52840-hal-tests"
4+
publish = false
5+
version = "0.0.0"
6+
7+
[[test]]
8+
name = "gpio-input-floating"
9+
harness = false
10+
11+
[[test]]
12+
name = "gpio-input-pulled"
13+
harness = false
14+
15+
[[test]]
16+
name = "gpio-output-push-pull"
17+
harness = false
18+
19+
[[test]]
20+
name = "gpio-output-open-drain"
21+
harness = false
22+
23+
[[test]]
24+
name = "serial"
25+
harness = false
26+
27+
[dev-dependencies]
28+
cortex-m = "0.7.0"
29+
defmt = "0.2.0"
30+
defmt-rtt = "0.2.0"
31+
defmt-test = "0.2.0"
32+
nrf52840-hal = { path = "../nrf52840-hal" }
33+
panic-probe = { version = "0.2.0", features = ["print-defmt"] }
34+
35+
[features]
36+
# enable all defmt logging levels
37+
default = ["defmt-trace"]
38+
39+
# do not modify these features
40+
defmt-default = []
41+
defmt-trace = []
42+
defmt-debug = []
43+
defmt-info = []
44+
defmt-warn = []
45+
defmt-error = []

nrf52840-hal-tests/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# nRF52840 HAL tests
2+
3+
Run `cargo test -p nrf52840-hal-tests` to test the HAL on a nRF52840.
4+
5+
The crate assumes that you'll test the HAL on a nRF52840 Development Kit.
6+
If you wish to use a different development board you'll need to update the flags passed to `probe-run` in `.cargo/config.toml`.
7+
8+
The following wiring is required:
9+
10+
- P0.03 <-> GND
11+
- P0.04 <-> VDD
12+
- P0.28 <-> P0.29
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Required connections:
2+
//
3+
// - P0.03 <-> GND
4+
// - P0.04 <-> VDD
5+
6+
#![deny(warnings)]
7+
#![no_std]
8+
#![no_main]
9+
10+
use defmt_rtt as _;
11+
use nrf52840_hal as _;
12+
use panic_probe as _;
13+
14+
use nrf52840_hal::gpio::{Floating, Input, Pin};
15+
16+
struct State {
17+
input_ground: Pin<Input<Floating>>,
18+
input_vdd: Pin<Input<Floating>>,
19+
}
20+
21+
#[defmt_test::tests]
22+
mod tests {
23+
use defmt::{assert, unwrap};
24+
use nrf52840_hal::{gpio::p0, pac, prelude::*};
25+
26+
use super::State;
27+
28+
#[init]
29+
fn init() -> State {
30+
let p = unwrap!(pac::Peripherals::take());
31+
let port0 = p0::Parts::new(p.P0);
32+
33+
let input_ground = port0.p0_03.into_floating_input().degrade();
34+
let input_vdd = port0.p0_04.into_floating_input().degrade();
35+
36+
State {
37+
input_ground,
38+
input_vdd,
39+
}
40+
}
41+
42+
#[test]
43+
fn ground_is_low(state: &mut State) {
44+
assert!(state.input_ground.is_low().unwrap());
45+
}
46+
47+
#[test]
48+
fn vdd_is_high(state: &mut State) {
49+
assert!(state.input_vdd.is_high().unwrap());
50+
}
51+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Required connections:
2+
//
3+
// - P0.28 <-> P0.29
4+
5+
#![deny(warnings)]
6+
#![no_std]
7+
#![no_main]
8+
9+
use defmt_rtt as _;
10+
use nrf52840_hal as _;
11+
use panic_probe as _;
12+
13+
use nrf52840_hal::gpio::{Floating, Input, Pin};
14+
15+
struct State {
16+
input_pin: Pin<Input<Floating>>,
17+
puller_pin: Option<Pin<Input<Floating>>>,
18+
}
19+
20+
#[defmt_test::tests]
21+
mod tests {
22+
use cortex_m::asm;
23+
use defmt::{assert, unwrap};
24+
use nrf52840_hal::{gpio::p0, pac, prelude::*};
25+
26+
use super::State;
27+
28+
#[init]
29+
fn init() -> State {
30+
let p = unwrap!(pac::Peripherals::take());
31+
let port0 = p0::Parts::new(p.P0);
32+
33+
let input_pin = port0.p0_28.into_floating_input().degrade();
34+
let puller_pin = Some(port0.p0_29.into_floating_input().degrade());
35+
36+
State {
37+
input_pin,
38+
puller_pin,
39+
}
40+
}
41+
42+
#[test]
43+
fn pulldown_is_low(state: &mut State) {
44+
let puller_pin = unwrap!(state.puller_pin.take());
45+
46+
let pulldown_pin = puller_pin.into_pulldown_input();
47+
// GPIO re-configuration is not instantaneous so a delay is needed
48+
asm::delay(100);
49+
assert!(pulldown_pin.is_low().unwrap());
50+
51+
state.puller_pin = Some(pulldown_pin.into_floating_input());
52+
}
53+
54+
#[test]
55+
fn pulldown_drives_low(state: &mut State) {
56+
let puller_pin = unwrap!(state.puller_pin.take());
57+
58+
let pulldown_pin = puller_pin.into_pulldown_input();
59+
assert!(state.input_pin.is_low().unwrap());
60+
61+
state.puller_pin = Some(pulldown_pin.into_floating_input());
62+
}
63+
64+
#[test]
65+
fn pullup_is_high(state: &mut State) {
66+
let puller_pin = unwrap!(state.puller_pin.take());
67+
68+
let pullup_pin = puller_pin.into_pullup_input();
69+
// GPIO re-configuration is not instantaneous so a delay is needed
70+
asm::delay(100);
71+
assert!(pullup_pin.is_high().unwrap());
72+
73+
state.puller_pin = Some(pullup_pin.into_floating_input());
74+
}
75+
76+
#[test]
77+
fn pullup_drives_high(state: &mut State) {
78+
let puller_pin = unwrap!(state.puller_pin.take());
79+
80+
let pullup_pin = puller_pin.into_pullup_input();
81+
// GPIO re-configuration is not instantaneous so a delay is needed
82+
asm::delay(100);
83+
assert!(state.input_pin.is_high().unwrap());
84+
85+
state.puller_pin = Some(pullup_pin.into_floating_input());
86+
}
87+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Required connections:
2+
//
3+
// - P0.28 <-> P0.29
4+
5+
#![deny(warnings)]
6+
#![no_std]
7+
#![no_main]
8+
9+
use defmt_rtt as _;
10+
use nrf52840_hal as _;
11+
use panic_probe as _;
12+
13+
use nrf52840_hal::gpio::{Floating, Input, OpenDrain, Output, Pin};
14+
15+
struct State {
16+
input_pin: Pin<Input<Floating>>,
17+
output_pin: Pin<Output<OpenDrain>>,
18+
}
19+
20+
#[defmt_test::tests]
21+
mod tests {
22+
use defmt::{assert, unwrap};
23+
use nrf52840_hal::{
24+
gpio::{p0, Level, OpenDrainConfig},
25+
pac,
26+
prelude::*,
27+
};
28+
29+
use super::State;
30+
31+
#[init]
32+
fn init() -> State {
33+
let p = unwrap!(pac::Peripherals::take());
34+
let port0 = p0::Parts::new(p.P0);
35+
36+
let input_pin = port0.p0_28.into_floating_input().degrade();
37+
let output_pin = port0
38+
.p0_29
39+
.into_open_drain_output(OpenDrainConfig::Standard0Disconnect1, Level::High)
40+
.degrade();
41+
42+
State {
43+
input_pin,
44+
output_pin,
45+
}
46+
}
47+
48+
#[test]
49+
fn set_low_is_low(state: &mut State) {
50+
state.output_pin.set_low().unwrap();
51+
assert!(state.input_pin.is_low().unwrap());
52+
}
53+
54+
// with the current API we cannot test this w/o an _external_ pull-up
55+
/*
56+
#[test]
57+
fn set_high_is_high(state: &mut State) {
58+
state.output_pin.set_high().unwrap();
59+
assert!(state.input_pin.is_high().unwrap());
60+
}
61+
*/
62+
}

0 commit comments

Comments
 (0)