minimal i2c slave example
This commit is contained in:
+4
-13
@@ -3,23 +3,16 @@
|
|||||||
#![feature(type_alias_impl_trait)]
|
#![feature(type_alias_impl_trait)]
|
||||||
#![feature(impl_trait_in_assoc_type)]
|
#![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::println;
|
||||||
use ch32_hal::time::Hertz;
|
use ch32_hal::time::Hertz;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_time::{Duration, Timer};
|
//use embassy_time::{Duration, Timer};
|
||||||
use hal::bind_interrupts;
|
use hal::bind_interrupts;
|
||||||
use hal::gpio::{Level, Output};
|
|
||||||
use hal::i2c::{Config, I2c, SlaveAddress, SlaveConfig};
|
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 _};
|
use {ch32_hal as hal, panic_halt as _};
|
||||||
|
|
||||||
|
|
||||||
bind_interrupts!(struct Irqs {
|
bind_interrupts!(struct Irqs {
|
||||||
USB_LP_CAN1_RX0 => InterruptHandler<USBD>;
|
|
||||||
I2C1_EV => ch32_hal::i2c::EventInterruptHandler<ch32_hal::peripherals::I2C1>;
|
I2C1_EV => ch32_hal::i2c::EventInterruptHandler<ch32_hal::peripherals::I2C1>;
|
||||||
I2C1_ER => ch32_hal::i2c::ErrorInterruptHandler<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")]
|
#[embassy_executor::main(entry = "qingke_rt::entry")]
|
||||||
async fn main(_spawner: Spawner) -> ! {
|
async fn main(_spawner: Spawner) -> ! {
|
||||||
let p = hal::init(Default::default());
|
let p = hal::init(Default::default());
|
||||||
SDIPrint::enable();
|
|
||||||
|
|
||||||
let config = Config::default();
|
let config = Config::default();
|
||||||
let mut i2c_slave = I2c::new_blocking(p.I2C1, p.PB6, p.PB7, Hertz::khz(100), config)
|
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 {
|
loop {
|
||||||
Timer::after_millis(10).await;
|
|
||||||
|
|
||||||
match i2c_slave.listen_blocking() {
|
match i2c_slave.listen_blocking() {
|
||||||
Ok(command) => {
|
Ok(command) => {
|
||||||
match command {
|
match command {
|
||||||
@@ -46,11 +36,12 @@ async fn main(_spawner: Spawner) -> ! {
|
|||||||
}
|
}
|
||||||
ch32_hal::i2c::SlaveCommand::ReadCommand => {
|
ch32_hal::i2c::SlaveCommand::ReadCommand => {
|
||||||
// send empty response
|
// send empty response
|
||||||
let _ = i2c_slave.blocking_write_timeout(&[0x01]);
|
let _ = i2c_slave.blocking_write_timeout(&[0x05, 0x01]);
|
||||||
}
|
}
|
||||||
ch32_hal::i2c::SlaveCommand::WriteCommand => {
|
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);
|
let _ = i2c_slave.blocking_read_timeout(&mut buf);
|
||||||
|
println!("received byte 0x{buf:x?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user