aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authori509VCB <[email protected]>2025-08-14 20:36:40 +0000
committerGitHub <[email protected]>2025-08-14 20:36:40 +0000
commit32f142d58587bb219b6835e1507758b7eb6f28b8 (patch)
treea75ab3442f368bdb2020ceee2392a8a04bbb4a6d /examples
parent5bd4722b6018524748dad6343a79adf1a55f1aa9 (diff)
parentf84eb9a7eb2df85b2d0dc7a934f2568640ef6161 (diff)
Merge pull request #4435 from bespsm/mspm0-i2c
MSPM0: Add I2C Controller (blocking & async) + examples for mspm0l1306, mspm0g3507 (tested MCUs)
Diffstat (limited to 'examples')
-rw-r--r--examples/mspm0g3507/src/bin/i2c.rs45
-rw-r--r--examples/mspm0g3507/src/bin/i2c_async.rs50
-rw-r--r--examples/mspm0l1306/Cargo.toml4
-rw-r--r--examples/mspm0l1306/src/bin/i2c.rs45
-rw-r--r--examples/mspm0l1306/src/bin/i2c_async.rs50
5 files changed, 194 insertions, 0 deletions
diff --git a/examples/mspm0g3507/src/bin/i2c.rs b/examples/mspm0g3507/src/bin/i2c.rs
new file mode 100644
index 000000000..8d1ed1726
--- /dev/null
+++ b/examples/mspm0g3507/src/bin/i2c.rs
@@ -0,0 +1,45 @@
1//! This example uses FIFO with polling, and the maximum FIFO size is 8.
2//! Refer to async example to handle larger packets.
3//!
4//! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0G3507 board.
5
6#![no_std]
7#![no_main]
8
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_mspm0::i2c::{Config, I2c};
12use embassy_time::Timer;
13use {defmt_rtt as _, panic_halt as _};
14
15const ADDRESS: u8 = 0x6a;
16
17#[embassy_executor::main]
18async fn main(_spawner: Spawner) -> ! {
19 let p = embassy_mspm0::init(Default::default());
20
21 let instance = p.I2C1;
22 let scl = p.PB2;
23 let sda = p.PB3;
24
25 let mut i2c = unwrap!(I2c::new_blocking(instance, scl, sda, Config::default()));
26
27 let mut pot_value: u8 = 0;
28
29 loop {
30 let to_write = [0u8, pot_value];
31
32 match i2c.blocking_write(ADDRESS, &to_write) {
33 Ok(()) => info!("New potentioemter value: {}", pot_value),
34 Err(e) => error!("I2c Error: {:?}", e),
35 }
36
37 pot_value += 1;
38 // if reached 64th position (max)
39 // start over from lowest value
40 if pot_value == 64 {
41 pot_value = 0;
42 }
43 Timer::after_millis(500).await;
44 }
45}
diff --git a/examples/mspm0g3507/src/bin/i2c_async.rs b/examples/mspm0g3507/src/bin/i2c_async.rs
new file mode 100644
index 000000000..d486e2a03
--- /dev/null
+++ b/examples/mspm0g3507/src/bin/i2c_async.rs
@@ -0,0 +1,50 @@
1//! The example uses FIFO and interrupts, wrapped in async API.
2//!
3//! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0G3507 board.
4
5#![no_std]
6#![no_main]
7
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_mspm0::bind_interrupts;
11use embassy_mspm0::i2c::{Config, I2c, InterruptHandler};
12use embassy_mspm0::peripherals::I2C1;
13use embassy_time::Timer;
14use {defmt_rtt as _, panic_halt as _};
15
16const ADDRESS: u8 = 0x6a;
17
18bind_interrupts!(struct Irqs {
19 I2C1 => InterruptHandler<I2C1>;
20});
21
22#[embassy_executor::main]
23async fn main(_spawner: Spawner) -> ! {
24 let p = embassy_mspm0::init(Default::default());
25
26 let instance = p.I2C1;
27 let scl = p.PB2;
28 let sda = p.PB3;
29
30 let mut i2c = unwrap!(I2c::new_async(instance, scl, sda, Irqs, Config::default()));
31
32 let mut pot_value: u8 = 0;
33
34 loop {
35 let to_write = [0u8, pot_value];
36
37 match i2c.async_write(ADDRESS, &to_write).await {
38 Ok(()) => info!("New potentioemter value: {}", pot_value),
39 Err(e) => error!("I2c Error: {:?}", e),
40 }
41
42 pot_value += 1;
43 // if reached 64th position (max)
44 // start over from lowest value
45 if pot_value == 64 {
46 pot_value = 0;
47 }
48 Timer::after_millis(500).await;
49 }
50}
diff --git a/examples/mspm0l1306/Cargo.toml b/examples/mspm0l1306/Cargo.toml
index 16562f3ee..724ca58a0 100644
--- a/examples/mspm0l1306/Cargo.toml
+++ b/examples/mspm0l1306/Cargo.toml
@@ -19,3 +19,7 @@ panic-semihosting = "0.6.0"
19 19
20[profile.release] 20[profile.release]
21debug = 2 21debug = 2
22
23[profile.dev]
24debug = 2
25opt-level = 2
diff --git a/examples/mspm0l1306/src/bin/i2c.rs b/examples/mspm0l1306/src/bin/i2c.rs
new file mode 100644
index 000000000..e8801c485
--- /dev/null
+++ b/examples/mspm0l1306/src/bin/i2c.rs
@@ -0,0 +1,45 @@
1//! This example uses FIFO with polling, and the maximum FIFO size is 8.
2//! Refer to async example to handle larger packets.
3//!
4//! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0L1306 board.
5
6#![no_std]
7#![no_main]
8
9use defmt::*;
10use embassy_executor::Spawner;
11use embassy_mspm0::i2c::{Config, I2c};
12use embassy_time::Timer;
13use {defmt_rtt as _, panic_halt as _};
14
15const ADDRESS: u8 = 0x2c;
16
17#[embassy_executor::main]
18async fn main(_spawner: Spawner) -> ! {
19 let p = embassy_mspm0::init(Default::default());
20
21 let instance = p.I2C0;
22 let scl = p.PA1;
23 let sda = p.PA0;
24
25 let mut i2c = unwrap!(I2c::new_blocking(instance, scl, sda, Config::default()));
26
27 let mut pot_value: u8 = 0;
28
29 loop {
30 let to_write = [0u8, pot_value];
31
32 match i2c.blocking_write(ADDRESS, &to_write) {
33 Ok(()) => info!("New potentioemter value: {}", pot_value),
34 Err(e) => error!("I2c Error: {:?}", e),
35 }
36
37 pot_value += 1;
38 // if reached 64th position (max)
39 // start over from lowest value
40 if pot_value == 64 {
41 pot_value = 0;
42 }
43 Timer::after_millis(500).await;
44 }
45}
diff --git a/examples/mspm0l1306/src/bin/i2c_async.rs b/examples/mspm0l1306/src/bin/i2c_async.rs
new file mode 100644
index 000000000..c4a6938ff
--- /dev/null
+++ b/examples/mspm0l1306/src/bin/i2c_async.rs
@@ -0,0 +1,50 @@
1//! The example uses FIFO and interrupts, wrapped in async API.
2//!
3//! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0L1306 board.
4
5#![no_std]
6#![no_main]
7
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_mspm0::bind_interrupts;
11use embassy_mspm0::i2c::{Config, I2c, InterruptHandler};
12use embassy_mspm0::peripherals::I2C0;
13use embassy_time::Timer;
14use {defmt_rtt as _, panic_halt as _};
15
16const ADDRESS: u8 = 0x6a;
17
18bind_interrupts!(struct Irqs {
19 I2C0 => InterruptHandler<I2C0>;
20});
21
22#[embassy_executor::main]
23async fn main(_spawner: Spawner) -> ! {
24 let p = embassy_mspm0::init(Default::default());
25
26 let instance = p.I2C0;
27 let scl = p.PA1;
28 let sda = p.PA0;
29
30 let mut i2c = unwrap!(I2c::new_async(instance, scl, sda, Irqs, Config::default()));
31
32 let mut pot_value: u8 = 0;
33
34 loop {
35 let to_write = [0u8, pot_value];
36
37 match i2c.async_write(ADDRESS, &to_write).await {
38 Ok(()) => info!("New potentioemter value: {}", pot_value),
39 Err(e) => error!("I2c Error: {:?}", e),
40 }
41
42 pot_value += 1;
43 // if reached 64th position (max)
44 // start over from lowest value
45 if pot_value == 64 {
46 pot_value = 0;
47 }
48 Timer::after_millis(500).await;
49 }
50}