Skip to content

Commit 8e8ed8a

Browse files
committed
test the nrf52840 HAL using defmt-test
this commit 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
1 parent 6fc5061 commit 8e8ed8a

File tree

12 files changed

+390
-6
lines changed

12 files changed

+390
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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"
9+
harness = false
10+
11+
[[test]]
12+
name = "gpio-output-push-pull"
13+
harness = false
14+
15+
[[test]]
16+
name = "gpio-output-open-drain"
17+
harness = false
18+
19+
[[test]]
20+
name = "gpio-pull"
21+
harness = false
22+
23+
[[test]]
24+
name = "serial"
25+
harness = false
26+
27+
[dev-dependencies]
28+
defmt = "0.1.3"
29+
defmt-rtt = "0.1.0"
30+
defmt-test = "0.1.1"
31+
nrf52840-hal = { path = "../nrf52840-hal" }
32+
panic-probe = { version = "0.1.0", features = ["print-defmt"] }
33+
34+
[features]
35+
# enable all defmt logging levels
36+
default = ["defmt-trace"]
37+
38+
# do not modify these features
39+
defmt-default = []
40+
defmt-trace = []
41+
defmt-debug = []
42+
defmt-info = []
43+
defmt-warn = []
44+
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: 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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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, Output, Pin, PushPull};
14+
15+
struct State {
16+
input_pin: Pin<Input<Floating>>,
17+
output_pin: Pin<Output<PushPull>>,
18+
}
19+
20+
#[defmt_test::tests]
21+
mod tests {
22+
use defmt::{assert, unwrap};
23+
use nrf52840_hal::{
24+
gpio::{p0, Level},
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.p0_29.into_push_pull_output(Level::High).degrade();
38+
39+
State {
40+
input_pin,
41+
output_pin,
42+
}
43+
}
44+
45+
#[test]
46+
fn set_low_is_low(state: &mut State) {
47+
state.output_pin.set_low().unwrap();
48+
assert!(state.input_pin.is_low().unwrap());
49+
}
50+
51+
#[test]
52+
fn set_high_is_high(state: &mut State) {
53+
state.output_pin.set_high().unwrap();
54+
assert!(state.input_pin.is_high().unwrap());
55+
}
56+
}

0 commit comments

Comments
 (0)