74 lines
2.5 KiB
Rust
74 lines
2.5 KiB
Rust
#![no_std]
|
|
#![no_main]
|
|
#![feature(type_alias_impl_trait)]
|
|
#![feature(impl_trait_in_assoc_type)]
|
|
|
|
mod measurement;
|
|
|
|
use ch32_hal::i2c::mode::OperatingMode;
|
|
use ch32_hal::mode::Mode;
|
|
use ch32_hal::println;
|
|
use ch32_hal::time::Hertz;
|
|
use embassy_executor::Spawner;
|
|
use embassy_sync::blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex};
|
|
use embassy_sync::lazy_lock::LazyLock;
|
|
use embassy_sync::mutex::Mutex;
|
|
use embassy_sync::rwlock::RwLock;
|
|
//use embassy_time::{Duration, Timer};
|
|
use hal::bind_interrupts;
|
|
use hal::i2c::{Config, I2c, SlaveAddress, SlaveConfig};
|
|
use {ch32_hal as hal, panic_halt as _};
|
|
|
|
bind_interrupts!(struct Irqs {
|
|
I2C1_EV => ch32_hal::i2c::EventInterruptHandler<ch32_hal::peripherals::I2C1>;
|
|
I2C1_ER => ch32_hal::i2c::ErrorInterruptHandler<ch32_hal::peripherals::I2C1>;
|
|
});
|
|
|
|
const MEASUREMENT_SAMPLES_CAPACITY: usize = 1024;
|
|
static MEASUREMENTS: LazyLock<
|
|
embassy_sync::mutex::Mutex<
|
|
CriticalSectionRawMutex,
|
|
heapless::Deque<lib_bms_protocol::BatteryState, MEASUREMENT_SAMPLES_CAPACITY>,
|
|
>,
|
|
> = LazyLock::new(|| Mutex::new(heapless::Deque::new()));
|
|
|
|
async fn measurement_task<'m, M: Mode, O: OperatingMode>(
|
|
i2c_dev: I2c<'m, ch32_hal::peripherals::I2C2, M, O>,
|
|
) {
|
|
}
|
|
|
|
#[embassy_executor::main(entry = "qingke_rt::entry")]
|
|
async fn main(_spawner: Spawner) -> ! {
|
|
let p = hal::init(Default::default());
|
|
|
|
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,
|
|
});
|
|
|
|
loop {
|
|
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::ReadCommand => {
|
|
// send empty response
|
|
let _ = i2c_slave.blocking_write_timeout(&[0x05, 0x01]);
|
|
}
|
|
ch32_hal::i2c::SlaveCommand::WriteCommand => {
|
|
let mut buf: [u8; 3] = [0x0, 0x0, 0x0];
|
|
let _ = i2c_slave.blocking_read_timeout(&mut buf);
|
|
println!("received byte 0x{buf:x?}");
|
|
}
|
|
}
|
|
}
|
|
Err(err) => {
|
|
println!("{:?}", err)
|
|
}
|
|
}
|
|
}
|
|
}
|