From 98615ea08e2c778ef3ea9dabe643ab0eca913f43 Mon Sep 17 00:00:00 2001 From: decaday Date: Thu, 7 Nov 2024 15:56:37 +0800 Subject: [PATCH] example: add blocking i2c example --- examples/py32f030/src/bin/i2c.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 examples/py32f030/src/bin/i2c.rs diff --git a/examples/py32f030/src/bin/i2c.rs b/examples/py32f030/src/bin/i2c.rs new file mode 100644 index 0000000..4d710dc --- /dev/null +++ b/examples/py32f030/src/bin/i2c.rs @@ -0,0 +1,28 @@ +#![no_std] +#![no_main] +#![feature(impl_trait_in_assoc_type)] + +use defmt::*; +use embassy_executor::Spawner; +use py32_hal::i2c::{Error, I2c}; +use py32_hal::time::Hertz; +use {defmt_rtt as _, panic_probe as _}; + +const ADDRESS: u8 = 0x5F; +const WHOAMI: u8 = 0x0F; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + info!("Hello world!"); + let p = py32_hal::init(Default::default()); + + let mut i2c = I2c::new_blocking(p.I2C1, p.PF1, p.PF0, Hertz(100_000), Default::default()); + + let mut data = [0u8; 1]; + + match i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data) { + Ok(()) => info!("Whoami: {}", data[0]), + Err(Error::Timeout) => error!("Operation timed out"), + Err(e) => error!("I2c Error: {:?}", e), + } +}