mirror of
https://github.com/italicsjenga/rp-hal-boards.git
synced 2024-12-27 14:21:31 +11:00
990d964a93
* Implement peripheral support for i2c and an advanced example for the pico board. * Simplify i2c peripheral bootstrap and add a "free" function to allow switching modes. * Set dependency to futures & nostd_async to specific version/revision. * move enum & struct to the start of the file * Add a bit of documentation to the pico_i2c_controller_peripheral demo. * Migrate to pico_i2c_controller_peripheral to embassy & simplify the peripheral support nostd_async is broken since last stable roll out. The pico_i2c_controller_peripheral is being migrated to use embassy's executor. The Controller Async API is now aligned with embassy's traits to facilitate integration. The peripheral no longer require async to run and now appears as an event iterator. Embassy's support relies on unstable features (generic_associated_types and type_alias_impl_traits) and is therefore gated behind the `embassy-traits` feature flag. * make futures & embassy optional for the pico board too * Pin embassy to a specific rev. * Impl embassy_traits::i2c::WriteIter & enable unlimited transfer size on i2c * Applies comment suggestion from @9names for the advanced i2c example. Co-authored-by: 9names <60134748+9names@users.noreply.github.com> * use `I2C block` instead of `IP`. * Fix formatting (unnecessary space at end of line) * Enhance explanation for why `rd_req()` is not cleared in `Iterator::next`'s implementation. Co-authored-by: 9names <60134748+9names@users.noreply.github.com>
96 lines
3.1 KiB
Rust
96 lines
3.1 KiB
Rust
//! I2C Peripheral demo
|
|
//!
|
|
//! This module implements a state machine serving the I2C requests from the controller in this
|
|
//! demo. In a real-life application the state machine may not need to be validated as thoroughly
|
|
//! demonstrated here.
|
|
|
|
use core::ops::Deref;
|
|
use rp2040_hal::i2c::peripheral::I2CEvent;
|
|
use rp2040_hal::i2c::peripheral::I2CPeripheralEventIterator;
|
|
use rp2040_hal::pac::i2c0::RegisterBlock as I2CBlock;
|
|
|
|
pub async fn run_demo<Block, Pins>(
|
|
i2c: &mut I2CPeripheralEventIterator<Block, Pins>,
|
|
) -> Result<(), rp2040_hal::i2c::Error>
|
|
where
|
|
Block: Deref<Target = I2CBlock>,
|
|
{
|
|
let mut expected_value = 0..;
|
|
let mut output = 128;
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
enum Stage {
|
|
Idle0,
|
|
FirstRead,
|
|
Idle1,
|
|
FirstWrite,
|
|
SecondRead,
|
|
Idle2,
|
|
SecondWrite,
|
|
Done,
|
|
}
|
|
let mut stage = Stage::Idle0;
|
|
|
|
while stage != Stage::Done {
|
|
let ev = futures::future::poll_fn(|cx| {
|
|
cx.waker().wake_by_ref();
|
|
i2c.next()
|
|
.map(core::task::Poll::Ready)
|
|
.unwrap_or(core::task::Poll::Pending)
|
|
})
|
|
.await;
|
|
match ev {
|
|
I2CEvent::Start => {
|
|
stage = match stage {
|
|
Stage::Idle0 => Stage::FirstRead,
|
|
Stage::Idle1 => Stage::FirstWrite,
|
|
Stage::Idle2 => Stage::SecondWrite,
|
|
_ => panic!("Unexpected {:?} while in {:?}", ev, stage),
|
|
}
|
|
}
|
|
I2CEvent::TransferRead => {
|
|
if stage != Stage::FirstRead && stage != Stage::SecondRead {
|
|
panic!("Unexpected {:?} while in {:?}", ev, stage);
|
|
}
|
|
|
|
i2c.write(&[output, output + 1, output + 2, output + 3]);
|
|
output += 4;
|
|
}
|
|
I2CEvent::TransferWrite => {
|
|
if stage != Stage::FirstWrite && stage != Stage::SecondWrite {
|
|
panic!("Unexpected {:?} while in {:?}", ev, stage);
|
|
}
|
|
|
|
let mut buf = [0; 16];
|
|
loop {
|
|
let read = i2c.read(&mut buf);
|
|
if read == 0 {
|
|
break;
|
|
}
|
|
|
|
buf.iter()
|
|
.take(read)
|
|
.cloned()
|
|
.zip(&mut expected_value)
|
|
.for_each(|(a, b)| assert_eq!(a, b));
|
|
}
|
|
}
|
|
I2CEvent::Stop => {
|
|
stage = match stage {
|
|
Stage::FirstRead => Stage::Idle1,
|
|
Stage::SecondRead => Stage::Idle2,
|
|
Stage::SecondWrite => Stage::Done,
|
|
_ => panic!("Unexpected {:?} while in {:?}", ev, stage),
|
|
}
|
|
}
|
|
I2CEvent::Restart => {
|
|
stage = match stage {
|
|
Stage::FirstWrite => Stage::SecondRead,
|
|
_ => panic!("Unexpected {:?} while in {:?}", ev, stage),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|