get more functions online

This commit is contained in:
2025-09-23 20:51:59 +02:00
parent b594a02870
commit d010c5d12a
6 changed files with 569 additions and 453 deletions

View File

@@ -4,8 +4,8 @@ mod initial_hal;
mod little_fs2storage_adapter;
pub(crate) mod rtc;
mod v4_hal;
mod v4_sensor;
mod water;
//mod water;
use crate::alloc::string::ToString;
use crate::hal::rtc::{DS3231Module, RTCModuleInteraction};
@@ -38,8 +38,8 @@ use esp_hal::peripherals::GPIO5;
use esp_hal::peripherals::GPIO6;
use esp_hal::peripherals::GPIO7;
use esp_hal::peripherals::GPIO8;
use esp_hal::peripherals::TWAI0;
//use crate::hal::water::TankSensor;
use crate::{
bail,
config::{BatteryBoardVersion, BoardVersion, PlantControllerConfig},
@@ -53,6 +53,7 @@ use alloc::boxed::Box;
use alloc::format;
use alloc::sync::Arc;
use async_trait::async_trait;
use bq34z100::Bq34z100g1Driver;
use chrono::{DateTime, FixedOffset, Utc};
use core::cell::RefCell;
use ds323x::ic::DS3231;
@@ -75,6 +76,7 @@ use esp_hal::clock::CpuClock;
use esp_hal::gpio::{Input, InputConfig, Pull};
use measurements::{Current, Voltage};
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;
@@ -84,6 +86,7 @@ use embassy_sync::once_lock::OnceLock;
use esp_alloc as _;
use esp_backtrace as _;
use esp_bootloader_esp_idf::ota::Slot;
use esp_bootloader_esp_idf::partitions::DataPartitionSubType::Fat;
use esp_hal::delay::Delay;
use esp_hal::i2c::master::{BusTimeout, Config, I2c};
use esp_hal::rng::Rng;
@@ -91,6 +94,7 @@ use esp_hal::rtc_cntl::{Rtc, SocResetReason};
use esp_hal::system::reset_reason;
use esp_hal::time::Rate;
use esp_hal::timer::timg::TimerGroup;
use esp_hal::twai::Twai;
use esp_hal::Blocking;
use esp_storage::FlashStorage;
use esp_wifi::{init, EspWifiController};
@@ -196,6 +200,7 @@ pub struct FreePeripherals<'a> {
pub gpio28: GPIO28<'a>,
pub gpio29: GPIO29<'a>,
pub gpio30: GPIO30<'a>,
pub twai: TWAI0<'a>,
// pub pcnt0: PCNT0,
// pub pcnt1: PCNT1,
// pub adc1: ADC1,
@@ -214,22 +219,6 @@ macro_rules! mk_static {
const GW_IP_ADDR_ENV: Option<&'static str> = option_env!("GATEWAY_IP");
impl PlantHal {
//fn create_i2c() -> Mutex<CriticalSectionRawMutex, ()> {
// let peripherals = unsafe { Peripherals::new() };
//
// let config = I2cConfig::new()
// .scl_enable_pullup(true)
// .sda_enable_pullup(true)
// .baudrate(100_u32.kHz().into())
// .timeout(APBTickType::from(Duration::from_millis(100)));
//
// let i2c = peripherals.i2c0;
// let scl = peripherals.pins.gpio19.downgrade();
// let sda = peripherals.pins.gpio20.downgrade();
//
// Mutex::new(I2cDriver::new(i2c, sda, scl, &config).unwrap())
//}
pub async fn create(
spawner: Spawner,
) -> Result<Mutex<CriticalSectionRawMutex, HAL<'static>>, FatError> {
@@ -240,10 +229,9 @@ impl PlantHal {
esp_alloc::heap_allocator!(#[link_section = ".dram2_uninit"] size: 64000);
let rtc: Rtc = Rtc::new(peripherals.LPWR);
match (TIME_ACCESS.init(rtc)) {
Result::Ok(_) => {}
Err(_) => {}
}
TIME_ACCESS.init(rtc).map_err(|_| FatError::String {
error: "Init error rct".to_string(),
})?;
let systimer = SystemTimer::new(peripherals.SYSTIMER);
@@ -300,6 +288,7 @@ impl PlantHal {
gpio28: peripherals.GPIO28,
gpio29: peripherals.GPIO29,
gpio30: peripherals.GPIO30,
twai: peripherals.TWAI0,
};
let tablebuffer = mk_static!(
@@ -500,34 +489,36 @@ impl PlantHal {
let battery_interaction: Box<dyn BatteryInteraction + Send> =
match config.hardware.battery {
BatteryBoardVersion::Disabled => Box::new(NoBatteryMonitor {}),
// BatteryBoardVersion::BQ34Z100G1 => {
// let mut battery_driver = Bq34z100g1Driver {
// i2c: MutexDevice::new(&I2C_DRIVER),
// delay: Delay::new(0),
// flash_block_data: [0; 32],
// };
// let status = print_battery_bq34z100(&mut battery_driver);
// match status {
// Ok(_) => {}
// Err(err) => {
// log(
// LogMessage::BatteryCommunicationError,
// 0u32,
// 0,
// "",
// &format!("{err:?})"),
// );
// }
// }
// Box::new(BQ34Z100G1 { battery_driver })
// }
BatteryBoardVersion::BQ34Z100G1 => {
let battery_device = I2cDevice::new(I2C_DRIVER.get().await);
let mut battery_driver = Bq34z100g1Driver {
i2c: battery_device,
delay: Delay::new(),
flash_block_data: [0; 32],
};
let status = print_battery_bq34z100(&mut battery_driver);
match status {
Ok(_) => {}
Err(err) => {
LOG_ACCESS
.lock()
.await
.log(
LogMessage::BatteryCommunicationError,
0u32,
0,
"",
&format!("{err:?})"),
)
.await;
}
}
Box::new(BQ34Z100G1 { battery_driver })
}
BatteryBoardVersion::WchI2cSlave => {
// TODO use correct implementation once availible
Box::new(NoBatteryMonitor {})
}
_ => {
todo!()
}
};
let board_hal: Box<dyn BoardInteraction + Send> = match config.hardware.board {
@@ -542,7 +533,7 @@ impl PlantHal {
.await?
}
_ => {
todo!()
bail!("Unknown board version");
}
};