starting with library and measurment implementation
This commit is contained in:
+22
-2
@@ -3,20 +3,40 @@
|
||||
#![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());
|
||||
@@ -44,7 +64,7 @@ async fn main(_spawner: Spawner) -> ! {
|
||||
println!("received byte 0x{buf:x?}");
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(err) => {
|
||||
println!("{:?}", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
|
||||
use embassy_usb::msos::ConfigurationSubsetHeader;
|
||||
use lib_bms_protocol::BatteryState;
|
||||
|
||||
use ina219::{address::Address, calibration::{IntCalibration, MicroAmpere, MicroWatt, UnCalibrated}, configuration::Configuration, errors::{ConfigurationReadError, InitializationError}, measurements::{CurrentRegister, Measurements, PowerRegister}, SyncIna219};
|
||||
use thiserror::Error;
|
||||
|
||||
pub(crate) const INA219_DEVICE_ADDR: u8 = 0b100_1011;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub(crate) enum MeasurementError {
|
||||
#[error("ina219 sensor initialization failed")]
|
||||
Ina219InitError,
|
||||
#[error("ina219 configuration read error")]
|
||||
Ina219ConfigReadErr,
|
||||
#[error("ina219 measurement read error")]
|
||||
Ina219MeasurementReadError,
|
||||
}
|
||||
|
||||
impl<IE> From<ina219::errors::MeasurementError<IE>> for MeasurementError {
|
||||
fn from(value: ina219::errors::MeasurementError<IE>) -> Self {
|
||||
MeasurementError::Ina219MeasurementReadError
|
||||
}
|
||||
}
|
||||
|
||||
impl<IE> From<ConfigurationReadError<IE>> for MeasurementError {
|
||||
fn from(value: ConfigurationReadError<IE>) -> Self {
|
||||
MeasurementError::Ina219InitError
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, IE> From<InitializationError<I, IE>> for MeasurementError {
|
||||
fn from(value: InitializationError<I, IE>) -> Self {
|
||||
MeasurementError::Ina219InitError
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn read_ina219<T: embedded_hal::i2c::I2c>(i2c_dev: &mut T) -> Result<Option<BatteryState>, MeasurementError> {
|
||||
let mut dev: SyncIna219<&mut T, _> = SyncIna219::new_calibrated(i2c_dev, Address::from_byte(INA219_DEVICE_ADDR).unwrap(), IntCalibration::default())?;
|
||||
let config: Configuration = dev.configuration()?;
|
||||
|
||||
if let Some(ina_measurement) = dev.next_measurement()? {
|
||||
let ina_measurement: Measurements<MicroAmpere, MicroWatt> = ina_measurement;
|
||||
}
|
||||
let measurement = todo!();
|
||||
Ok(measurement)
|
||||
}
|
||||
Reference in New Issue
Block a user