mirror of
https://github.com/italicsjenga/rp-hal-boards.git
synced 2024-12-25 21:41: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>
48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
//! I2C Controller demo
|
|
//!
|
|
//! This module implements a demonstration of an I2C controller sending read & write requests to a
|
|
//! peripheral.
|
|
//! This demo takes advandage of rust's async.await support to run read & write operation while
|
|
//! serving those request from an independant context.
|
|
|
|
use super::ADDRESS;
|
|
use core::ops::Deref;
|
|
use rp2040_hal::i2c::I2C;
|
|
use rp2040_hal::pac::i2c0::RegisterBlock as I2CBlock;
|
|
|
|
use embassy_traits::i2c::I2c;
|
|
|
|
/// Controller demo
|
|
pub async fn run_demo<Block, Pins>(i2c: &mut I2C<Block, Pins>) -> Result<(), rp2040_hal::i2c::Error>
|
|
where
|
|
Block: Deref<Target = I2CBlock>,
|
|
{
|
|
let mut tx_filler = 0;
|
|
let mut tx = [0u8; 24];
|
|
let mut rx = [0u8; 24];
|
|
|
|
i2c.read(ADDRESS, &mut rx).await?;
|
|
rx.iter()
|
|
.cloned()
|
|
.zip(0x80..)
|
|
.for_each(|(a, b)| assert_eq!(a, b));
|
|
|
|
tx.iter_mut().for_each(|b| {
|
|
*b = tx_filler;
|
|
tx_filler += 1;
|
|
});
|
|
|
|
i2c.write_read(ADDRESS, &tx, &mut rx).await?;
|
|
rx.iter()
|
|
.cloned()
|
|
.zip(0x80 + 24..) // follows the inital read
|
|
.for_each(|(a, b)| assert_eq!(a, b));
|
|
|
|
tx.iter_mut().for_each(|b| {
|
|
*b = tx_filler;
|
|
tx_filler += 1;
|
|
});
|
|
i2c.write(ADDRESS, &tx).await?;
|
|
Ok(())
|
|
}
|