diff --git a/src/main.rs b/src/main.rs index 40df2c6..55d111b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,17 +3,21 @@ #![feature(type_alias_impl_trait)] #![feature(impl_trait_in_assoc_type)] -use ch32_hal::i2c::Duty; +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 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; I2C1_EV => ch32_hal::i2c::EventInterruptHandler; @@ -23,43 +27,35 @@ 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 mut led = Output::new(p.PB2, Level::Low, Default::default()); - - let mut config = Config::default(); - config.duty = Duty::Duty16_9; - let mut i2c_slave = I2c::new_blocking(p.I2C1, p.PB6, p.PB7, Hertz::khz(400), config) + let config = Config::default(); + let mut i2c_slave = I2c::new_blocking(p.I2C1, p.PB6, p.PB7, Hertz::khz(100), config) .into_slave(SlaveConfig { address: SlaveAddress::SevenBit(0x55), general_call: false, }); - let mut blinks: u8 = 8; loop { - Timer::after_millis(1000).await; + Timer::after_millis(10).await; - if blinks > 0 { - led.toggle(); - if led.is_set_low() { - blinks -= 1; - } - } else { - if let Ok(command) = i2c_slave.listen_blocking() { - blinks = 1; + match i2c_slave.listen_blocking() { + Ok(command) => { match command { - ch32_hal::i2c::SlaveCommand::GeneralCall => { /* will not be triggered because we disabled it */ }, + ch32_hal::i2c::SlaveCommand::GeneralCall => { /* will not be triggered because we disabled it */ + } ch32_hal::i2c::SlaveCommand::ReadCommand => { // send empty response - i2c_slave.blocking_write_timeout(&[0x01]); - }, + let _ = i2c_slave.blocking_write_timeout(&[0x01]); + } ch32_hal::i2c::SlaveCommand::WriteCommand => { let mut buf: [u8; 1] = [0x0]; - i2c_slave.blocking_read_timeout(&mut buf); - blinks = buf[0]; - }, + let _ = i2c_slave.blocking_read_timeout(&mut buf); + } } - } else { - blinks = 2; + }, + Err(err) => { + println!("{:?}", err) } } }