aboutsummaryrefslogtreecommitdiff
path: root/examples/mspm0l1306/src
diff options
context:
space:
mode:
authorIooon <[email protected]>2025-10-01 11:00:44 +0200
committercrispaudio <[email protected]>2025-10-10 07:01:23 +0200
commit7797cc0effa069b78be29ff19b81068b17f98ac2 (patch)
tree0ea39389870a804bd62bf6b34fbcee50b478a38f /examples/mspm0l1306/src
parente62ec23a389801f7a7c22db0727ae43814138fe1 (diff)
mspm0-i2c-target: add mspm0l1306 example and set target address explicitly in examples
Diffstat (limited to 'examples/mspm0l1306/src')
-rw-r--r--examples/mspm0l1306/src/bin/i2c_target.rs61
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
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_mspm0::i2c_target::{Command, I2cTarget, ReadStatus};
11use embassy_mspm0::peripherals::I2C0;
12use embassy_mspm0::{bind_interrupts, i2c};
13use {defmt_rtt as _, panic_halt as _};
14
15bind_interrupts!(struct Irqs {
16 I2C0 => i2c::InterruptHandler<I2C0>;
17});
18
19#[embassy_executor::main]
20async 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}