bring selftest online, bring tz online, fix set_config/get_config race

This commit is contained in:
2025-09-24 22:29:58 +02:00
parent 5b009f50e5
commit e20b474dfd
13 changed files with 247 additions and 344 deletions

View File

@@ -1,5 +1,5 @@
use crate::hal::Box;
use crate::FatError::{FatError, FatResult};
use crate::fat_error::{FatError, FatResult};
use alloc::string::String;
use async_trait::async_trait;
use bq34z100::{Bq34z100g1, Bq34z100g1Driver, Flags};

View File

@@ -6,10 +6,11 @@ use chrono::{DateTime, Utc};
use serde::Serialize;
use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem;
use crate::FatError::{ContextExt, FatError, FatResult};
use crate::fat_error::{ContextExt, FatError, FatResult};
use alloc::string::ToString;
use alloc::sync::Arc;
use alloc::{format, string::String, vec::Vec};
use alloc::borrow::ToOwned;
use core::marker::PhantomData;
use core::net::{IpAddr, Ipv4Addr};
use core::str::FromStr;
@@ -317,7 +318,7 @@ impl Esp<'_> {
let stack = mk_static!(Stack, stack);
spawner
.spawn(connection(self.controller.clone(), ssid))
.spawn(connection(self.controller.clone(), ssid.to_owned()))
.ok();
spawner.spawn(net_task(runner)).ok();
spawner.spawn(run_dhcp(stack.clone(), gw_ip_addr_str)).ok();
@@ -328,13 +329,12 @@ impl Esp<'_> {
}
Timer::after(Duration::from_millis(500)).await;
}
println!(
"Connect to the AP `esp-wifi` and point your browser to http://{gw_ip_addr_str}:8080/"
);
println!("DHCP is enabled so there's no need to configure a static IP, just in case:");
while !stack.is_config_up() {
Timer::after(Duration::from_millis(100)).await
}
println!(
"Connect to the AP `${ssid}` and point your browser to http://{gw_ip_addr_str}/"
);
stack
.config_v4()
.inspect(|c| println!("ipv4 config: {c:?}"));
@@ -801,11 +801,6 @@ async fn connection(
controller: Arc<Mutex<CriticalSectionRawMutex, WifiController<'static>>>,
ssid: String,
) {
println!("start connection task");
println!(
"Device capabilities: {:?}",
controller.lock().await.capabilities()
);
let client_config = Configuration::AccessPoint(AccessPointConfiguration {
ssid: ssid.clone(),
..Default::default()
@@ -815,7 +810,6 @@ async fn connection(
.await
.set_configuration(&client_config)
.unwrap();
println!("Starting wifi");
controller.lock().await.start_async().await.unwrap();
println!("Wifi started!");
}

View File

@@ -3,7 +3,7 @@ use crate::hal::esp::Esp;
use crate::hal::rtc::{BackupHeader, RTCModuleInteraction};
use crate::hal::water::TankSensor;
use crate::hal::{BoardInteraction, FreePeripherals, Sensor};
use crate::FatError::{FatError, FatResult};
use crate::fat_error::{FatError, FatResult};
use crate::{
bail,
config::PlantControllerConfig,
@@ -100,7 +100,7 @@ impl<'a> BoardInteraction<'a> for Initial<'a> {
fn is_day(&self) -> bool {
false
}
fn light(&mut self, _enable: bool) -> Result<(), FatError> {
async fn light(&mut self, _enable: bool) -> Result<(), FatError> {
bail!("Please configure board revision")
}

View File

@@ -84,7 +84,7 @@ use crate::hal::battery::{print_battery_bq34z100, BQ34Z100G1};
use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem;
use crate::hal::water::TankSensor;
use crate::log::LOG_ACCESS;
use crate::FatError::FatError;
use crate::fat_error::FatError;
use embassy_sync::mutex::Mutex;
use embassy_sync::once_lock::OnceLock;
use esp_alloc as _;
@@ -140,7 +140,7 @@ pub trait BoardInteraction<'a> {
fn is_day(&self) -> bool;
//should be multsampled
fn light(&mut self, enable: bool) -> Result<(), FatError>;
async fn light(&mut self, enable: bool) -> Result<(), FatError>;
async fn pump(&mut self, plant: usize, enable: bool) -> Result<(), FatError>;
async fn pump_current(&mut self, plant: usize) -> Result<Current, FatError>;
async fn fault(&mut self, plant: usize, enable: bool) -> Result<(), FatError>;

View File

@@ -1,5 +1,5 @@
use crate::hal::Box;
use crate::FatError::FatResult;
use crate::fat_error::FatResult;
use async_trait::async_trait;
use bincode::config::Configuration;
use bincode::{config, Decode, Encode};

View File

@@ -3,17 +3,19 @@ use crate::hal::battery::BatteryInteraction;
use crate::hal::esp::Esp;
use crate::hal::rtc::RTCModuleInteraction;
use crate::hal::water::TankSensor;
use crate::hal::{BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER};
use crate::hal::{BoardInteraction, FreePeripherals, Sensor, I2C_DRIVER, PLANT_COUNT};
use alloc::boxed::Box;
use alloc::string::ToString;
use async_trait::async_trait;
use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_time::Timer;
use esp_hal::analog::adc::{Adc, AdcConfig, Attenuation};
use esp_hal::{twai, Blocking};
//use embedded_hal_bus::i2c::MutexDevice;
use crate::bail;
use crate::hal::v4_sensor::{SensorImpl, SensorInteraction};
use crate::FatError::{FatError, FatResult};
use crate::fat_error::{FatError, FatResult};
use esp_hal::gpio::{Flex, Input, InputConfig, Level, Output, OutputConfig, Pull};
use esp_hal::i2c::master::I2c;
use esp_hal::pcnt::Pcnt;
@@ -26,6 +28,7 @@ use ina219::SyncIna219;
use measurements::Resistance;
use measurements::{Current, Voltage};
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
use crate::log::{LogMessage, LOG_ACCESS};
const MPPT_CURRENT_SHUNT_OHMS: f64 = 0.05_f64;
const TWAI_BAUDRATE: twai::BaudRate = twai::BaudRate::B125K;
@@ -348,16 +351,14 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
}
fn is_day(&self) -> bool {
false
//self.charger.is_day()
self.charger.is_day()
}
fn light(&mut self, enable: bool) -> Result<(), FatError> {
bail!("not implemented");
async fn light(&mut self, enable: bool) -> Result<(), FatError> {
// unsafe { gpio_hold_dis(self.light.pin()) };
// self.light.set_state(enable.into())?;
self.light.set_level(enable.into());
// unsafe { gpio_hold_en(self.light.pin()) };
// anyhow::Ok(())
Ok(())
}
async fn pump(&mut self, plant: usize, enable: bool) -> FatResult<()> {
@@ -417,40 +418,40 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
}
async fn test(&mut self) -> Result<(), FatError> {
// self.general_fault(true);
// self.esp.delay.delay_ms(100);
// self.general_fault(false);
// self.esp.delay.delay_ms(500);
// self.light(true)?;
// self.esp.delay.delay_ms(500);
// self.light(false)?;
// self.esp.delay.delay_ms(500);
// for i in 0..PLANT_COUNT {
// self.fault(i, true)?;
// self.esp.delay.delay_ms(500);
// self.fault(i, false)?;
// self.esp.delay.delay_ms(500);
// }
// for i in 0..PLANT_COUNT {
// self.pump(i, true)?;
// self.esp.delay.delay_ms(100);
// self.pump(i, false)?;
// self.esp.delay.delay_ms(100);
// }
// for plant in 0..PLANT_COUNT {
// let a = self.measure_moisture_hz(plant, Sensor::A);
// let b = self.measure_moisture_hz(plant, Sensor::B);
// let aa = match a {
// OkStd(a) => a as u32,
// Err(_) => u32::MAX,
// };
// let bb = match b {
// OkStd(b) => b as u32,
// Err(_) => u32::MAX,
// };
// log(LogMessage::TestSensor, aa, bb, &plant.to_string(), "");
// }
// self.esp.delay.delay_ms(10);
self.general_fault(true).await;
Timer::after_millis(100).await;
self.general_fault(false).await;
Timer::after_millis(500).await;
self.light(true).await?;
Timer::after_millis(500).await;
self.light(false).await?;
Timer::after_millis(500).await;
for i in 0..PLANT_COUNT {
self.fault(i, true).await?;
Timer::after_millis(500).await;
self.fault(i, false).await?;
Timer::after_millis(500).await;
}
for i in 0..PLANT_COUNT {
self.pump(i, true).await?;
Timer::after_millis(100).await;
self.pump(i, false).await?;
Timer::after_millis(100).await;
}
for plant in 0..PLANT_COUNT {
let a = self.measure_moisture_hz(plant, Sensor::A).await;
let b = self.measure_moisture_hz(plant, Sensor::B).await;
let aa = match a {
Ok(a) => a as u32,
Err(_) => u32::MAX,
};
let bb = match b {
Ok(b) => b as u32,
Err(_) => u32::MAX,
};
LOG_ACCESS.lock().await.log(LogMessage::TestSensor, aa, bb, &plant.to_string(), "").await;
}
Timer::after_millis(10).await;
Ok(())
}

View File

@@ -1,7 +1,7 @@
use crate::hal::Box;
use crate::hal::Sensor;
use crate::log::{LogMessage, LOG_ACCESS};
use crate::FatError::FatResult;
use crate::fat_error::{FatError, FatResult};
use alloc::format;
use alloc::string::ToString;
use async_trait::async_trait;
@@ -121,8 +121,8 @@ impl SensorInteraction for SensorImpl {
let median = results[mid];
Ok(median)
}
SensorImpl::CanBus { .. } => {
todo!()
SensorImpl::CanBus { twai } => {
Err(FatError::String {error: "Not yet implemented".to_string()})
}
}
}

View File

@@ -1,6 +1,6 @@
use crate::bail;
use crate::hal::{ADC1, TANK_MULTI_SAMPLE};
use crate::FatError::FatError;
use crate::fat_error::FatError;
use embassy_time::Timer;
use esp_hal::analog::adc::{Adc, AdcConfig, AdcPin, Attenuation};
use esp_hal::delay::Delay;