Skip to content

Commit 2003212

Browse files
committed
i2c: implement embedded-hal-async with tests
1 parent cfe2372 commit 2003212

File tree

13 files changed

+1260
-25
lines changed

13 files changed

+1260
-25
lines changed

on-target-tests/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ harness = false
3232
name = "i2c_loopback"
3333
harness = false
3434

35+
[[test]]
36+
name = "i2c_loopback_async"
37+
harness = false
38+
3539
[dependencies]
3640
cortex-m = "0.7"
3741
cortex-m-rt = "0.7"
3842
embedded_hal_0_2 = { package = "embedded-hal", version = "0.2.5", features = [
3943
"unproven",
4044
] }
4145
embedded-hal = "1.0.0"
46+
embedded-hal-async = "1.0.0"
4247

4348
defmt = "0.3"
4449
defmt-rtt = "0.4"
@@ -59,4 +64,10 @@ heapless = { version = "0.8.0", features = [
5964
"portable-atomic-critical-section",
6065
"defmt-03",
6166
] }
67+
# - `wfe`: we may want to signal between cores with sev
68+
# - `wfe` implies `cortex-m`
69+
nostd_async = { version = "0.6.1", features = ["wfe"] }
70+
futures = { version = "0.3.30", default-features = false, features = [
71+
"async-await",
72+
] }
6273
itertools = { version = "0.12.0", default-features = false }
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! This test needs a connection between:
2+
//!
3+
//! | from GPIO (pico Pin) | to GPIO (pico Pin) |
4+
//! | -------------------- | ------------------ |
5+
//! | 0 (1) | 2 (4) |
6+
//! | 1 (2) | 3 (5) |
7+
8+
#![no_std]
9+
#![no_main]
10+
#![cfg(test)]
11+
12+
use defmt_rtt as _; // defmt transport
13+
use defmt_test as _;
14+
use panic_probe as _;
15+
use rp2040_hal as hal; // memory layout // panic handler
16+
17+
use hal::{async_utils::AsyncPeripheral, pac::interrupt};
18+
19+
/// The linker will place this boot block at the start of our program image. We
20+
/// need this to help the ROM bootloader get our code up and running.
21+
/// Note: This boot block is not necessary when using a rp-hal based BSP
22+
/// as the BSPs already perform this step.
23+
#[link_section = ".boot2"]
24+
#[used]
25+
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;
26+
27+
/// External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust
28+
/// if your board has a different frequency
29+
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
30+
31+
pub mod i2c_tests;
32+
33+
#[interrupt]
34+
unsafe fn I2C0_IRQ() {
35+
i2c_tests::Controller::on_interrupt();
36+
}
37+
38+
#[interrupt]
39+
unsafe fn I2C1_IRQ() {
40+
i2c_tests::Target::on_interrupt();
41+
}
42+
43+
#[defmt_test::tests]
44+
mod tests {
45+
use crate::i2c_tests::{
46+
non_blocking::{self, run_test, State},
47+
ADDR_10BIT, ADDR_7BIT,
48+
};
49+
50+
#[init]
51+
fn setup() -> State {
52+
non_blocking::setup(super::XTAL_FREQ_HZ, ADDR_7BIT)
53+
}
54+
55+
#[test]
56+
fn embedded_hal(state: &mut State) {
57+
run_test(non_blocking::embedded_hal(state, ADDR_7BIT, 2..=2));
58+
run_test(non_blocking::embedded_hal(state, ADDR_10BIT, 2..=7));
59+
}
60+
61+
#[test]
62+
fn transations_iter(state: &mut State) {
63+
run_test(non_blocking::transaction(state, ADDR_7BIT, 7..=9));
64+
run_test(non_blocking::transaction(state, ADDR_10BIT, 7..=14));
65+
}
66+
67+
// Sad paths:
68+
// invalid tx buf on write
69+
// invalid rx buf on read
70+
//
71+
// invalid (rx/tx) buf in transactions
72+
//
73+
// Peripheral Nack
74+
//
75+
// Arbritration conflict
76+
}

on-target-tests/tests/i2c_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rp2040_hal::{
1111
};
1212

1313
pub mod blocking;
14+
pub mod non_blocking;
1415

1516
pub const ADDR_7BIT: u8 = 0x2c;
1617
pub const ADDR_10BIT: u16 = 0x12c;

0 commit comments

Comments
 (0)