minimal i2c slave example

This commit is contained in:
2025-12-29 14:29:33 +01:00
parent 9e95573553
commit d2636108b2
+4 -13
View File
@@ -3,23 +3,16 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
use ch32_hal::i2c::{self, Duty};
use ch32_hal::pac::{self, I2C1};
use ch32_hal::println;
use ch32_hal::time::Hertz;
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
//use embassy_time::{Duration, Timer};
use hal::bind_interrupts;
use hal::gpio::{Level, Output};
use hal::i2c::{Config, I2c, SlaveAddress, SlaveConfig};
use hal::debug::SDIPrint;
use hal::peripherals::USBD;
use hal::usbd::InterruptHandler;
use {ch32_hal as hal, panic_halt as _};
bind_interrupts!(struct Irqs {
USB_LP_CAN1_RX0 => InterruptHandler<USBD>;
I2C1_EV => ch32_hal::i2c::EventInterruptHandler<ch32_hal::peripherals::I2C1>;
I2C1_ER => ch32_hal::i2c::ErrorInterruptHandler<ch32_hal::peripherals::I2C1>;
});
@@ -27,7 +20,6 @@ bind_interrupts!(struct Irqs {
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(_spawner: Spawner) -> ! {
let p = hal::init(Default::default());
SDIPrint::enable();
let config = Config::default();
let mut i2c_slave = I2c::new_blocking(p.I2C1, p.PB6, p.PB7, Hertz::khz(100), config)
@@ -37,8 +29,6 @@ async fn main(_spawner: Spawner) -> ! {
});
loop {
Timer::after_millis(10).await;
match i2c_slave.listen_blocking() {
Ok(command) => {
match command {
@@ -46,11 +36,12 @@ async fn main(_spawner: Spawner) -> ! {
}
ch32_hal::i2c::SlaveCommand::ReadCommand => {
// send empty response
let _ = i2c_slave.blocking_write_timeout(&[0x01]);
let _ = i2c_slave.blocking_write_timeout(&[0x05, 0x01]);
}
ch32_hal::i2c::SlaveCommand::WriteCommand => {
let mut buf: [u8; 1] = [0x0];
let mut buf: [u8; 3] = [0x0, 0x0, 0x0];
let _ = i2c_slave.blocking_read_timeout(&mut buf);
println!("received byte 0x{buf:x?}");
}
}
},