diff --git a/Cargo.lock b/Cargo.lock index 81f6882..15b43b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,7 +65,6 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "ch32-hal" version = "0.1.0" -source = "git+https://github.com/ch32-rs/ch32-hal#2b8e1c864ba5545ee65b1c77dcb17c86a471b70c" dependencies = [ "ch32-metapac 0.1.0", "critical-section", diff --git a/Cargo.toml b/Cargo.toml index ce4fc45..619d9b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] -ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal", features = [ +#ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal", features = [ +ch32-hal = { path = "/home/judge/dev/github.com/ju6ge/ch32-hal", features = [ "ch32v203c8t6", "memory-x", "embassy", diff --git a/src/main.rs b/src/main.rs index 3b2a650..00727b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,13 @@ #![feature(type_alias_impl_trait)] #![feature(impl_trait_in_assoc_type)] -mod i2c_slave; - +use ch32_hal::i2c::Duty; +use ch32_hal::time::Hertz; use embassy_executor::Spawner; -use embassy_time::Timer; +use embassy_time::{Duration, Timer}; use hal::bind_interrupts; use hal::gpio::{Level, Output}; +use hal::i2c::{Config, I2c, SlaveAddress, SlaveConfig}; use hal::peripherals::USBD; use hal::usbd::InterruptHandler; use {ch32_hal as hal, panic_halt as _}; @@ -23,11 +24,41 @@ async fn main(_spawner: Spawner) -> ! { 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) + .into_slave(SlaveConfig { + address: SlaveAddress::SevenBit(0x55), + general_call: false, + }); + + let mut blinks: u8 = 8; loop { Timer::after_millis(1000).await; - //class.write_packet(b"hello world from embassy main\r\n").await; - - led.toggle(); + if blinks > 0 { + led.toggle(); + if led.is_set_low() { + blinks -= 1; + } + } else { + if let Ok(command) = i2c_slave.listen_blocking() { + blinks = 1; + match command { + 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]); + }, + ch32_hal::i2c::SlaveCommand::WriteCommand => { + let mut buf: [u8; 1] = [0x0]; + i2c_slave.blocking_read_timeout(&mut buf); + blinks = buf[0]; + }, + } + } else { + blinks = 2; + } + } } }