diff options
Diffstat (limited to 'examples/mspm0g3507/src/bin/i2c_async.rs')
| -rw-r--r-- | examples/mspm0g3507/src/bin/i2c_async.rs | 26 |
1 files changed, 18 insertions, 8 deletions
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 @@ | |||
| 1 | //! The example uses FIFO and interrupts, wrapped in async API. | 1 | //! The example uses FIFO and interrupts, wrapped in async API. |
| 2 | //! | 2 | //! |
| 3 | //! This uses the virtual COM port provided on the LP-MSPM0G3507 board. | 3 | //! This example controls AD5171 digital potentiometer via I2C with the LP-MSPM0G3507 board. |
| 4 | 4 | ||
| 5 | #![no_std] | 5 | #![no_std] |
| 6 | #![no_main] | 6 | #![no_main] |
| @@ -10,6 +10,7 @@ use embassy_executor::Spawner; | |||
| 10 | use embassy_mspm0::bind_interrupts; | 10 | use embassy_mspm0::bind_interrupts; |
| 11 | use embassy_mspm0::i2c::{Config, I2c, InterruptHandler}; | 11 | use embassy_mspm0::i2c::{Config, I2c, InterruptHandler}; |
| 12 | use embassy_mspm0::peripherals::I2C1; | 12 | use embassy_mspm0::peripherals::I2C1; |
| 13 | use embassy_time::Timer; | ||
| 13 | use {defmt_rtt as _, panic_halt as _}; | 14 | use {defmt_rtt as _, panic_halt as _}; |
| 14 | 15 | ||
| 15 | const ADDRESS: u8 = 0x6a; | 16 | const ADDRESS: u8 = 0x6a; |
| @@ -28,13 +29,22 @@ async fn main(_spawner: Spawner) -> ! { | |||
| 28 | 29 | ||
| 29 | let mut i2c = unwrap!(I2c::new_async(instance, scl, sda, Irqs, Config::default())); | 30 | let mut i2c = unwrap!(I2c::new_async(instance, scl, sda, Irqs, Config::default())); |
| 30 | 31 | ||
| 31 | let mut to_read = [1u8; 17]; | 32 | let mut pot_value: u8 = 0; |
| 32 | let to_write = [0u8; 17]; | ||
| 33 | 33 | ||
| 34 | match i2c.async_write_read(ADDRESS, &to_write, &mut to_read).await { | 34 | loop { |
| 35 | Ok(()) => info!("Register {}: {}", to_write, to_read[0]), | 35 | let to_write = [0u8, pot_value]; |
| 36 | Err(e) => error!("I2c Error: {:?}", e), | 36 | |
| 37 | } | 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 | } | ||
| 38 | 41 | ||
| 39 | loop {} | 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 | } | ||
| 40 | } | 50 | } |
