From e78959ed67f8c89f2adc78f5e4580c62d3c66081 Mon Sep 17 00:00:00 2001 From: Siarhei B Date: Mon, 4 Aug 2025 13:10:39 +0200 Subject: mspm0-I2C: replace examples for mspm0l1306 & mspm0g3507 with AD5171 --- examples/mspm0g3507/src/bin/i2c.rs | 26 ++++++++++++++++++-------- examples/mspm0g3507/src/bin/i2c_async.rs | 26 ++++++++++++++++++-------- examples/mspm0l1306/src/bin/i2c.rs | 28 +++++++++++++++++++--------- examples/mspm0l1306/src/bin/i2c_async.rs | 26 ++++++++++++++++++-------- 4 files changed, 73 insertions(+), 33 deletions(-) (limited to 'examples') diff --git a/examples/mspm0g3507/src/bin/i2c.rs b/examples/mspm0g3507/src/bin/i2c.rs index 2e3bf2ca1..8d1ed1726 100644 --- a/examples/mspm0g3507/src/bin/i2c.rs +++ b/examples/mspm0g3507/src/bin/i2c.rs @@ -1,7 +1,7 @@ //! This example uses FIFO with polling, and the maximum FIFO size is 8. //! Refer to async example to handle larger packets. //! -//! This uses the virtual COM port provided on the LP-MSPM0G3507 board. +//! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0G3507 board. #![no_std] #![no_main] @@ -9,6 +9,7 @@ use defmt::*; use embassy_executor::Spawner; use embassy_mspm0::i2c::{Config, I2c}; +use embassy_time::Timer; use {defmt_rtt as _, panic_halt as _}; const ADDRESS: u8 = 0x6a; @@ -23,13 +24,22 @@ async fn main(_spawner: Spawner) -> ! { let mut i2c = unwrap!(I2c::new_blocking(instance, scl, sda, Config::default())); - let mut to_read = [0u8; 1]; - let to_write: u8 = 0x0F; + let mut pot_value: u8 = 0; - match i2c.blocking_write_read(ADDRESS, &[to_write], &mut to_read) { - Ok(()) => info!("Register {}: {}", to_write, to_read[0]), - Err(e) => error!("I2c Error: {:?}", e), - } + loop { + let to_write = [0u8, pot_value]; + + match i2c.blocking_write(ADDRESS, &to_write) { + Ok(()) => info!("New potentioemter value: {}", pot_value), + Err(e) => error!("I2c Error: {:?}", e), + } - loop {} + pot_value += 1; + // if reached 64th position (max) + // start over from lowest value + if pot_value == 64 { + pot_value = 0; + } + Timer::after_millis(500).await; + } } diff --git a/examples/mspm0g3507/src/bin/i2c_async.rs b/examples/mspm0g3507/src/bin/i2c_async.rs index 294550605..d486e2a03 100644 --- a/examples/mspm0g3507/src/bin/i2c_async.rs +++ b/examples/mspm0g3507/src/bin/i2c_async.rs @@ -1,6 +1,6 @@ //! The example uses FIFO and interrupts, wrapped in async API. //! -//! This uses the virtual COM port provided on the LP-MSPM0G3507 board. +//! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0G3507 board. #![no_std] #![no_main] @@ -10,6 +10,7 @@ use embassy_executor::Spawner; use embassy_mspm0::bind_interrupts; use embassy_mspm0::i2c::{Config, I2c, InterruptHandler}; use embassy_mspm0::peripherals::I2C1; +use embassy_time::Timer; use {defmt_rtt as _, panic_halt as _}; const ADDRESS: u8 = 0x6a; @@ -28,13 +29,22 @@ async fn main(_spawner: Spawner) -> ! { let mut i2c = unwrap!(I2c::new_async(instance, scl, sda, Irqs, Config::default())); - let mut to_read = [1u8; 17]; - let to_write = [0u8; 17]; + let mut pot_value: u8 = 0; - match i2c.async_write_read(ADDRESS, &to_write, &mut to_read).await { - Ok(()) => info!("Register {}: {}", to_write, to_read[0]), - Err(e) => error!("I2c Error: {:?}", e), - } + loop { + let to_write = [0u8, pot_value]; + + match i2c.async_write(ADDRESS, &to_write).await { + Ok(()) => info!("New potentioemter value: {}", pot_value), + Err(e) => error!("I2c Error: {:?}", e), + } - loop {} + pot_value += 1; + // if reached 64th position (max) + // start over from lowest value + if pot_value == 64 { + pot_value = 0; + } + Timer::after_millis(500).await; + } } diff --git a/examples/mspm0l1306/src/bin/i2c.rs b/examples/mspm0l1306/src/bin/i2c.rs index 51327dff5..e8801c485 100644 --- a/examples/mspm0l1306/src/bin/i2c.rs +++ b/examples/mspm0l1306/src/bin/i2c.rs @@ -1,7 +1,7 @@ //! This example uses FIFO with polling, and the maximum FIFO size is 8. //! Refer to async example to handle larger packets. //! -//! This uses the virtual COM port provided on the LP-MSPM0L1306 board. +//! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0L1306 board. #![no_std] #![no_main] @@ -9,9 +9,10 @@ use defmt::*; use embassy_executor::Spawner; use embassy_mspm0::i2c::{Config, I2c}; +use embassy_time::Timer; use {defmt_rtt as _, panic_halt as _}; -const ADDRESS: u8 = 0x6a; +const ADDRESS: u8 = 0x2c; #[embassy_executor::main] async fn main(_spawner: Spawner) -> ! { @@ -23,13 +24,22 @@ async fn main(_spawner: Spawner) -> ! { let mut i2c = unwrap!(I2c::new_blocking(instance, scl, sda, Config::default())); - let mut to_read = [0u8; 1]; - let to_write: u8 = 0x0F; + let mut pot_value: u8 = 0; - match i2c.blocking_write_read(ADDRESS, &[to_write], &mut to_read) { - Ok(()) => info!("Register {}: {}", to_write, to_read[0]), - Err(e) => error!("I2c Error: {:?}", e), - } + loop { + let to_write = [0u8, pot_value]; + + match i2c.blocking_write(ADDRESS, &to_write) { + Ok(()) => info!("New potentioemter value: {}", pot_value), + Err(e) => error!("I2c Error: {:?}", e), + } - loop {} + pot_value += 1; + // if reached 64th position (max) + // start over from lowest value + if pot_value == 64 { + pot_value = 0; + } + Timer::after_millis(500).await; + } } diff --git a/examples/mspm0l1306/src/bin/i2c_async.rs b/examples/mspm0l1306/src/bin/i2c_async.rs index 74826bcc4..c4a6938ff 100644 --- a/examples/mspm0l1306/src/bin/i2c_async.rs +++ b/examples/mspm0l1306/src/bin/i2c_async.rs @@ -1,6 +1,6 @@ //! The example uses FIFO and interrupts, wrapped in async API. //! -//! This uses the virtual COM port provided on the LP-MSPM0L1306 board. +//! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0L1306 board. #![no_std] #![no_main] @@ -10,6 +10,7 @@ use embassy_executor::Spawner; use embassy_mspm0::bind_interrupts; use embassy_mspm0::i2c::{Config, I2c, InterruptHandler}; use embassy_mspm0::peripherals::I2C0; +use embassy_time::Timer; use {defmt_rtt as _, panic_halt as _}; const ADDRESS: u8 = 0x6a; @@ -28,13 +29,22 @@ async fn main(_spawner: Spawner) -> ! { let mut i2c = unwrap!(I2c::new_async(instance, scl, sda, Irqs, Config::default())); - let mut to_read = [1u8; 17]; - let to_write = [0u8; 17]; + let mut pot_value: u8 = 0; - match i2c.async_write_read(ADDRESS, &to_write, &mut to_read).await { - Ok(()) => info!("Register {}: {}", to_write, to_read[0]), - Err(e) => error!("I2c Error: {:?}", e), - } + loop { + let to_write = [0u8, pot_value]; + + match i2c.async_write(ADDRESS, &to_write).await { + Ok(()) => info!("New potentioemter value: {}", pot_value), + Err(e) => error!("I2c Error: {:?}", e), + } - loop {} + pot_value += 1; + // if reached 64th position (max) + // start over from lowest value + if pot_value == 64 { + pot_value = 0; + } + Timer::after_millis(500).await; + } } -- cgit