diff options
Diffstat (limited to 'examples/mspm0l1306/src/bin/i2c_target.rs')
| -rw-r--r-- | examples/mspm0l1306/src/bin/i2c_target.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/examples/mspm0l1306/src/bin/i2c_target.rs b/examples/mspm0l1306/src/bin/i2c_target.rs new file mode 100644 index 000000000..38d309e6b --- /dev/null +++ b/examples/mspm0l1306/src/bin/i2c_target.rs | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | //! Example of using async I2C target | ||
| 2 | //! | ||
| 3 | //! This uses the virtual COM port provided on the LP-MSPM0L1306 board. | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_mspm0::i2c_target::{Command, I2cTarget, ReadStatus}; | ||
| 11 | use embassy_mspm0::peripherals::I2C0; | ||
| 12 | use embassy_mspm0::{bind_interrupts, i2c}; | ||
| 13 | use {defmt_rtt as _, panic_halt as _}; | ||
| 14 | |||
| 15 | bind_interrupts!(struct Irqs { | ||
| 16 | I2C0 => i2c::InterruptHandler<I2C0>; | ||
| 17 | }); | ||
| 18 | |||
| 19 | #[embassy_executor::main] | ||
| 20 | async fn main(_spawner: Spawner) -> ! { | ||
| 21 | let p = embassy_mspm0::init(Default::default()); | ||
| 22 | |||
| 23 | let instance = p.I2C0; | ||
| 24 | let scl = p.PA1; | ||
| 25 | let sda = p.PA0; | ||
| 26 | |||
| 27 | let mut config = i2c::Config::default(); | ||
| 28 | config.target_addr = 0x48; | ||
| 29 | config.general_call = true; | ||
| 30 | let mut i2c = I2cTarget::new(instance, scl, sda, Irqs, config).unwrap(); | ||
| 31 | |||
| 32 | let mut read = [0u8; 8]; | ||
| 33 | let data = [8u8; 2]; | ||
| 34 | let data_wr = [9u8; 2]; | ||
| 35 | |||
| 36 | loop { | ||
| 37 | match i2c.listen(&mut read).await { | ||
| 38 | Ok(Command::GeneralCall(_)) => info!("General call received"), | ||
| 39 | Ok(Command::Read) => { | ||
| 40 | info!("Read command received"); | ||
| 41 | match i2c.respond_to_read(&data).await.unwrap() { | ||
| 42 | ReadStatus::Done => info!("Finished reading"), | ||
| 43 | ReadStatus::NeedMoreBytes => { | ||
| 44 | info!("Read needs more bytes - will reset"); | ||
| 45 | i2c.reset().unwrap(); | ||
| 46 | } | ||
| 47 | ReadStatus::LeftoverBytes(_) => { | ||
| 48 | info!("Leftover bytes received"); | ||
| 49 | i2c.flush_tx_fifo(); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | Ok(Command::Write(_)) => info!("Write command received"), | ||
| 54 | Ok(Command::WriteRead(_)) => { | ||
| 55 | info!("Write-Read command received"); | ||
| 56 | i2c.respond_and_fill(&data_wr, 0xFE).await.unwrap(); | ||
| 57 | } | ||
| 58 | Err(e) => info!("Got error {}", e), | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
