diff options
| author | Siarhei B <[email protected]> | 2025-07-21 14:22:32 +0200 |
|---|---|---|
| committer | Siarhei B <[email protected]> | 2025-08-04 10:19:14 +0200 |
| commit | f9753f3d314ca00fb36103fa39b0911d3e3047ba (patch) | |
| tree | e41d14d422fb902587baaa2a8e1ae1516c22168b /examples/mspm0l1306/src/bin/i2c_async.rs | |
| parent | 7c640799d66f82f9936f6378cc5dd9856953662e (diff) | |
mspm0: Add I2C Controller examples for mspm0l1306, mspm0g3507 MCUs
- mspm0l1306 examples: add I2C blocking & async examples
- mspm0l1306 examples: add -O2 optimization due to Flash limitations
- mspm0g3507 examples: add I2C blocking & async examples
Diffstat (limited to 'examples/mspm0l1306/src/bin/i2c_async.rs')
| -rw-r--r-- | examples/mspm0l1306/src/bin/i2c_async.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/mspm0l1306/src/bin/i2c_async.rs b/examples/mspm0l1306/src/bin/i2c_async.rs new file mode 100644 index 000000000..34e2c64e7 --- /dev/null +++ b/examples/mspm0l1306/src/bin/i2c_async.rs | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | //! Example of using async I2C | ||
| 2 | //! | ||
| 3 | //! This uses the virtual COM port provided on the LP-MSPM0L1306 board. | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_mspm0::bind_interrupts; | ||
| 11 | use embassy_mspm0::i2c::{BusSpeed, ClockSel, Config, I2c, InterruptHandler}; | ||
| 12 | use embassy_mspm0::peripherals::I2C0; | ||
| 13 | use {defmt_rtt as _, panic_halt as _}; | ||
| 14 | |||
| 15 | const ADDRESS: u8 = 0x6a; | ||
| 16 | |||
| 17 | bind_interrupts!(struct Irqs { | ||
| 18 | I2C0 => InterruptHandler<I2C0>; | ||
| 19 | }); | ||
| 20 | |||
| 21 | #[embassy_executor::main] | ||
| 22 | async fn main(_spawner: Spawner) -> ! { | ||
| 23 | let p = embassy_mspm0::init(Default::default()); | ||
| 24 | |||
| 25 | let instance = p.I2C0; | ||
| 26 | let scl = p.PA1; | ||
| 27 | let sda = p.PA0; | ||
| 28 | |||
| 29 | let mut config = Config::default(); | ||
| 30 | config.clock_source = ClockSel::BusClk; | ||
| 31 | config.bus_speed = BusSpeed::FastMode; | ||
| 32 | let mut i2c = unwrap!(I2c::new_async(instance, scl, sda, Irqs, config)); | ||
| 33 | |||
| 34 | let mut to_read = [0u8; 1]; | ||
| 35 | let to_write: u8 = 0x0F; | ||
| 36 | |||
| 37 | match i2c.async_write_read(ADDRESS, &[to_write], &mut to_read).await { | ||
| 38 | Ok(()) => info!("Register {}: {}", to_write, to_read[0]), | ||
| 39 | Err(e) => error!("I2c Error: {:?}", e), | ||
| 40 | } | ||
| 41 | |||
| 42 | loop {} | ||
| 43 | } | ||
