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