startup and ota state detection working, now wifi_ap next

This commit is contained in:
2025-09-13 20:56:11 +02:00
parent 4160202cdc
commit be3c4a5095
9 changed files with 294 additions and 225 deletions

View File

@@ -5,7 +5,7 @@ mod rtc;
//mod water;
use crate::alloc::string::ToString;
use crate::hal::rtc::{RTCModuleInteraction};
use crate::hal::rtc::RTCModuleInteraction;
//use crate::hal::water::TankSensor;
use crate::{
config::{BatteryBoardVersion, BoardVersion, PlantControllerConfig},
@@ -17,20 +17,24 @@ use crate::{
};
use alloc::boxed::Box;
use alloc::format;
use core::marker::PhantomData;
use anyhow::{Ok, Result};
use async_trait::async_trait;
use core::marker::PhantomData;
use embassy_executor::Spawner;
//use battery::BQ34Z100G1;
use bq34z100::Bq34z100g1Driver;
use ds323x::{DateTimeAccess, Ds323x};
use eeprom24x::{Eeprom24x, SlaveAddr, Storage};
use embassy_sync::blocking_mutex::raw::{CriticalSectionRawMutex};
//use bq34z100::Bq34z100g1Driver;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::mutex::Mutex;
use embassy_sync::lazy_lock::LazyLock;
use esp_bootloader_esp_idf::partitions::DataPartitionSubType;
use esp_hal::clock::CpuClock;
use esp_hal::gpio::{Input, InputConfig, Pull};
use esp_hal::timer::systimer::SystemTimer;
use esp_println::println;
use measurements::{Current, Voltage};
use esp_alloc as _;
use esp_backtrace as _;
//Only support for 8 right now!
pub const PLANT_COUNT: usize = 8;
@@ -38,24 +42,34 @@ const TANK_MULTI_SAMPLE: usize = 11;
//pub static I2C_DRIVER: LazyLock<Mutex<CriticalSectionRawMutex,I2cDriver<'static>>> = LazyLock::new(PlantHal::create_i2c);
fn deep_sleep(duration_in_ms: u64) -> ! {
// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}
fn deep_sleep(_duration_in_ms: u64) -> ! {
//unsafe {
// //if we don't do this here, we might just revert newly flashed firmware
// mark_app_valid();
// //allow early wakeup by pressing the boot button
// if duration_in_ms == 0 {
// esp_restart();
// } else {
// //configure gpio 1 to wakeup on low, reused boot button for this
// esp_sleep_enable_ext1_wakeup(
// 0b10u64,
// esp_sleep_ext1_wakeup_mode_t_ESP_EXT1_WAKEUP_ANY_LOW,
// );
// esp_deep_sleep(duration_in_ms);
// }
loop {
todo!()
}
// //if we don't do this here, we might just revert newly flashed firmware
// mark_app_valid();
// //allow early wakeup by pressing the boot button
// if duration_in_ms == 0 {
// esp_restart();
// } else {
// //configure gpio 1 to wakeup on low, reused boot button for this
// esp_sleep_enable_ext1_wakeup(
// 0b10u64,
// esp_sleep_ext1_wakeup_mode_t_ESP_EXT1_WAKEUP_ANY_LOW,
// );
// esp_deep_sleep(duration_in_ms);
// }
loop {
todo!()
}
//};
}
@@ -95,7 +109,6 @@ pub trait BoardInteraction<'a> {
async fn get_mptt_current(&mut self) -> anyhow::Result<Current>;
}
impl dyn BoardInteraction<'_> {
//the counter is just some arbitrary number that increases whenever some progress was made, try to keep the updates < 10 per second for ux reasons
async fn _progress(&mut self, counter: u32) {
@@ -163,17 +176,19 @@ impl PlantHal {
// Mutex::new(I2cDriver::new(i2c, sda, scl, &config).unwrap())
// }
pub fn create() -> Result<Mutex<CriticalSectionRawMutex, HAL<'static>>> {
pub fn create(spawner: Spawner) -> Result<Mutex<CriticalSectionRawMutex, HAL<'static>>> {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(size: 64 * 1024);
let timer0 = SystemTimer::new(peripherals.SYSTIMER);
esp_hal_embassy::init(timer0.alarm0);
let systimer = SystemTimer::new(peripherals.SYSTIMER);
esp_hal_embassy::init(systimer.alarm0);
let boot_button = Input::new(
peripherals.GPIO9,
InputConfig::default().with_pull(Pull::None),
);
// let mut boot_button = PinDriver::input(peripherals.pins.gpio9.downgrade())?;
// boot_button.set_pull(Pull::Floating)?;
//
// let free_pins = FreePeripherals {
// can: peripherals.can,
@@ -210,13 +225,46 @@ impl PlantHal {
// gpio30: peripherals.pins.gpio30,
// };
//
let mut storage = esp_storage::FlashStorage::new();
let mut buffer = [0u8; esp_bootloader_esp_idf::partitions::PARTITION_TABLE_MAX_LEN];
let pt =
esp_bootloader_esp_idf::partitions::read_partition_table(&mut storage, &mut buffer)?;
// List all partitions - this is just FYI
for i in 0..pt.len() {
println!("{:?}", pt.get_partition(i));
}
// Find the OTA-data partition and show the currently active partition
let ota_part = pt
.find_partition(esp_bootloader_esp_idf::partitions::PartitionType::Data(
DataPartitionSubType::Ota,
))?
.unwrap();
let mut ota_part = ota_part.as_embedded_storage(&mut storage);
println!("Found ota data");
let mut ota = esp_bootloader_esp_idf::ota::Ota::new(&mut ota_part)?;
let current = ota.current_slot()?;
println!(
"current image state {:?} (only relevant if the bootloader was built with auto-rollback support)",
ota.current_ota_state()
);
println!("current {:?} - next {:?}", current, current.next());
let ota_state = ota.current_ota_state()?;
let mut esp = Esp {
mqtt_client: None,
boot_button,
mqtt_client: None,
storage,
slot: current.number(),
next_slot: current.next().number(),
ota_state,
dummy: PhantomData::default(),
wall_clock_offset : 0
// wifi_driver,
// boot_button
};
wall_clock_offset: 0, // wifi_driver,
// boot_button
};
//init,reset rtc memory depending on cause
let mut init_rtc_store: bool = false;