rust mutex issue

This commit is contained in:
2024-10-23 18:17:18 +02:00
parent 171bd19458
commit 7957cf4003
8 changed files with 261 additions and 376 deletions

View File

@@ -1,10 +1,12 @@
use bq34z100::{Bq34Z100Error, Bq34z100g1, Bq34z100g1Driver};
use chrono_tz::Europe::Berlin;
use ds323x::Ds323x;
use embedded_hal_bus::i2c::MutexDevice;
use embedded_svc::wifi::{
AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration,
};
use esp_idf_hal::adc::attenuation;
use esp_idf_hal::adc::{attenuation, Resolution};
use esp_idf_hal::adc::oneshot::config::AdcChannelConfig;
use esp_idf_hal::adc::oneshot::{AdcChannelDriver, AdcDriver};
use esp_idf_hal::i2c::{APBTickType, I2cConfig, I2cDriver, I2cError};
@@ -24,6 +26,7 @@ use anyhow::anyhow;
use anyhow::{bail, Ok, Result};
use std::ffi::CString;
use std::fs::File;
use std::ops::Deref;
use std::path::Path;
use chrono::{DateTime, Utc};
@@ -219,7 +222,8 @@ pub struct PlantCtrlBoard<'a> {
pub wifi_driver: EspWifi<'a>,
one_wire_bus: OneWire<PinDriver<'a, Gpio18, esp_idf_hal::gpio::InputOutput>>,
mqtt_client: Option<EspMqttClient<'a>>,
battery_driver: Option<Bq34z100g1Driver<I2cDriver<'a>, Delay>>,
battery_driver: Option<Bq34z100g1Driver<MutexDevice<'a, I2cDriver<'a>>, Delay>>,
rtc: Option<Ds323x<ds323x::interface::I2cInterface<MutexDevice<'a, I2cDriver<'a>>>, ds323x::ic::DS3231>>
}
impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
@@ -1013,7 +1017,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
}
fn print_battery(
battery_driver: &mut Bq34z100g1Driver<I2cDriver, Delay>,
battery_driver: &mut Bq34z100g1Driver<MutexDevice<'_, I2cDriver<'_>>, Delay>,
) -> Result<(), Bq34Z100Error<I2cError>> {
println!("Try communicating with battery");
let fwversion = battery_driver.fw_version().unwrap_or_else(|e| {
@@ -1084,20 +1088,24 @@ impl CreatePlantHal<'_> for PlantHal {
let sda = peripherals.pins.gpio20.downgrade();
let driver = I2cDriver::new(i2c, sda, scl, &config).unwrap();
let i2c_port = driver.port();
let mut timeout: i32 = 0;
esp!(unsafe { esp_idf_sys::i2c_get_timeout(i2c_port, &mut timeout) }).unwrap();
println!("init i2c timeout is {}", timeout);
//esp!(unsafe { esp_idf_sys::i2c_set_timeout(i2c_port, 22)}).unwrap();
let i2c_mutex = Arc::new(Mutex::new(driver));
let mut battery_driver: Bq34z100g1Driver<I2cDriver, Delay> = Bq34z100g1Driver {
i2c: driver,
let i2c_battery_device = MutexDevice::new(&i2c_mutex);
let mut battery_driver: Bq34z100g1Driver<MutexDevice<I2cDriver>, Delay> = Bq34z100g1Driver {
i2c: i2c_battery_device,
delay: Delay::new(0),
flash_block_data: [0; 32],
};
let i2c_rtc_device = MutexDevice::new(&i2c_mutex);
let rtc = Ds323x::new_ds3231(i2c_rtc_device);
let mut clock = PinDriver::input_output(peripherals.pins.gpio15.downgrade())?;
clock.set_pull(Pull::Floating).unwrap();
let mut latch = PinDriver::input_output(peripherals.pins.gpio3.downgrade())?;
@@ -1139,6 +1147,8 @@ impl CreatePlantHal<'_> for PlantHal {
ResetReason::Brownout => true,
ResetReason::TaskWatchdog => true,
ResetReason::DeepSleep => false,
ResetReason::USBPeripheral => true,
ResetReason::JTAG => true,
};
if reset_store {
println!("Clear and reinit RTC store");
@@ -1212,7 +1222,7 @@ impl CreatePlantHal<'_> for PlantHal {
let adc_config = AdcChannelConfig {
attenuation: attenuation::DB_11,
resolution: esp_idf_hal::adc::config::Resolution::Resolution12Bit,
resolution: Resolution::Resolution12Bit,
calibration: true,
};
let tank_driver = AdcDriver::new(peripherals.adc1)?;
@@ -1265,8 +1275,8 @@ impl CreatePlantHal<'_> for PlantHal {
signal_counter: counter_unit1,
wifi_driver,
mqtt_client: None,
//battery_driver: None,
battery_driver: Some(battery_driver),
rtc: Some(rtc)
});
let _ = rv.lock().is_ok_and(|mut board| {