Skip to content

Commit

Permalink
i2c: implement embedded-hal-async with tests
Browse files Browse the repository at this point in the history
Also adds "-Tdefmt.x" to .cargo/config.
This required for i2c async examples & on-target-tests.
Note this does not affect downstream dependents.
  • Loading branch information
ithinuel committed Feb 8, 2024
1 parent cfe2372 commit abc7fb8
Show file tree
Hide file tree
Showing 15 changed files with 1,264 additions and 29 deletions.
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ target = "thumbv6m-none-eabi"
rustflags = [
"-C", "link-arg=--nmagic",
"-C", "link-arg=-Tlink.x",
"-C", "link-arg=-Tdefmt.x",
"-C", "inline-threshold=5",
"-C", "no-vectorize-loops",
]
Expand Down
11 changes: 11 additions & 0 deletions on-target-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ harness = false
name = "i2c_loopback"
harness = false

[[test]]
name = "i2c_loopback_async"
harness = false

[dependencies]
cortex-m = "0.7"
cortex-m-rt = "0.7"
embedded_hal_0_2 = { package = "embedded-hal", version = "0.2.5", features = [
"unproven",
] }
embedded-hal = "1.0.0"
embedded-hal-async = "1.0.0"

defmt = "0.3"
defmt-rtt = "0.4"
Expand All @@ -59,4 +64,10 @@ heapless = { version = "0.8.0", features = [
"portable-atomic-critical-section",
"defmt-03",
] }
# - `wfe`: we may want to signal between cores with sev
# - `wfe` implies `cortex-m`
nostd_async = { version = "0.6.1", features = ["wfe"] }
futures = { version = "0.3.30", default-features = false, features = [
"async-await",
] }
itertools = { version = "0.12.0", default-features = false }
3 changes: 0 additions & 3 deletions on-target-tests/build.rs

This file was deleted.

76 changes: 76 additions & 0 deletions on-target-tests/tests/i2c_loopback_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! This test needs a connection between:
//!
//! | from GPIO (pico Pin) | to GPIO (pico Pin) |
//! | -------------------- | ------------------ |
//! | 0 (1) | 2 (4) |
//! | 1 (2) | 3 (5) |

#![no_std]
#![no_main]
#![cfg(test)]

use defmt_rtt as _; // defmt transport
use defmt_test as _;
use panic_probe as _;
use rp2040_hal as hal; // memory layout // panic handler

use hal::{async_utils::AsyncPeripheral, pac::interrupt};

/// The linker will place this boot block at the start of our program image. We
/// need this to help the ROM bootloader get our code up and running.
/// Note: This boot block is not necessary when using a rp-hal based BSP
/// as the BSPs already perform this step.
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;

/// External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust
/// if your board has a different frequency
const XTAL_FREQ_HZ: u32 = 12_000_000u32;

pub mod i2c_tests;

#[interrupt]
unsafe fn I2C0_IRQ() {
i2c_tests::Controller::on_interrupt();
}

#[interrupt]
unsafe fn I2C1_IRQ() {
i2c_tests::Target::on_interrupt();
}

#[defmt_test::tests]
mod tests {
use crate::i2c_tests::{
non_blocking::{self, run_test, State},
ADDR_10BIT, ADDR_7BIT,
};

#[init]
fn setup() -> State {
non_blocking::setup(super::XTAL_FREQ_HZ, ADDR_7BIT)
}

#[test]
fn embedded_hal(state: &mut State) {
run_test(non_blocking::embedded_hal(state, ADDR_7BIT, 2..=2));
run_test(non_blocking::embedded_hal(state, ADDR_10BIT, 2..=7));
}

#[test]
fn transations_iter(state: &mut State) {
run_test(non_blocking::transaction(state, ADDR_7BIT, 7..=9));
run_test(non_blocking::transaction(state, ADDR_10BIT, 7..=14));
}

// Sad paths:
// invalid tx buf on write
// invalid rx buf on read
//
// invalid (rx/tx) buf in transactions
//
// Peripheral Nack
//
// Arbritration conflict
}
1 change: 1 addition & 0 deletions on-target-tests/tests/i2c_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rp2040_hal::{
};

pub mod blocking;
pub mod non_blocking;

pub const ADDR_7BIT: u8 = 0x2c;
pub const ADDR_10BIT: u16 = 0x12c;
Expand Down
Loading

0 comments on commit abc7fb8

Please sign in to comment.