shared flash test

This commit is contained in:
2025-11-02 02:30:21 +01:00
parent 0519ca3efe
commit 8cd9e08e93
6 changed files with 117 additions and 28 deletions

View File

@@ -4,11 +4,13 @@ pub mod esp;
mod initial_hal;
mod little_fs2storage_adapter;
pub(crate) mod rtc;
mod shared_flash;
mod v3_hal;
mod v3_shift_register;
mod v4_hal;
pub(crate) mod v4_sensor;
mod water;
use crate::alloc::string::ToString;
use crate::hal::rtc::{DS3231Module, RTCModuleInteraction};
use esp_hal::peripherals::Peripherals;
@@ -79,7 +81,7 @@ use crate::hal::water::TankSensor;
use crate::log::LOG_ACCESS;
use embassy_sync::mutex::Mutex;
use embassy_sync::once_lock::OnceLock;
use embedded_storage::nor_flash::ReadNorFlash;
use embedded_storage::ReadStorage;
use esp_alloc as _;
use esp_backtrace as _;
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState};
@@ -101,6 +103,7 @@ use littlefs2::object_safe::DynStorage;
use log::{error, info, warn};
use portable_atomic::AtomicBool;
use serde::Serialize;
use shared_flash::MutexFlashStorage;
pub static TIME_ACCESS: OnceLock<Mutex<CriticalSectionRawMutex, Rtc>> = OnceLock::new();
@@ -306,7 +309,15 @@ impl PlantHal {
[u8; esp_bootloader_esp_idf::partitions::PARTITION_TABLE_MAX_LEN],
[0u8; esp_bootloader_esp_idf::partitions::PARTITION_TABLE_MAX_LEN]
);
let flash_storage = mk_static!(FlashStorage, FlashStorage::new(peripherals.FLASH));
let bullshit = MutexFlashStorage {
inner: Arc::new(CriticalSectionMutex::new(RefCell::new(FlashStorage::new(
peripherals.FLASH,
)))),
};
let flash_storage = mk_static!(MutexFlashStorage, bullshit.clone());
let flash_storage_2 = mk_static!(MutexFlashStorage, bullshit.clone());
let flash_storage_3 = mk_static!(MutexFlashStorage, bullshit.clone());
let pt =
esp_bootloader_esp_idf::partitions::read_partition_table(flash_storage, tablebuffer)?;
@@ -320,8 +331,8 @@ impl PlantHal {
);
let ota_data = mk_static!(
FlashRegion<FlashStorage>,
ota_data.as_embedded_storage(flash_storage)
FlashRegion<MutexFlashStorage>,
ota_data.as_embedded_storage(flash_storage_2)
);
let state_0 = ota_state(AppPartitionSubType::Ota0, ota_data);
@@ -353,7 +364,7 @@ impl PlantHal {
let ota_target = mk_static!(PartitionEntry, ota_target);
let ota_target = mk_static!(
FlashRegion<FlashStorage>,
FlashRegion<MutexFlashStorage>,
ota_target.as_embedded_storage(flash_storage)
);
@@ -365,8 +376,8 @@ impl PlantHal {
let data_partition = mk_static!(PartitionEntry, data_partition);
let data = mk_static!(
FlashRegion<FlashStorage>,
data_partition.as_embedded_storage(flash_storage)
FlashRegion<MutexFlashStorage>,
data_partition.as_embedded_storage(flash_storage_3)
);
let lfs2filesystem = mk_static!(LittleFs2Filesystem, LittleFs2Filesystem { storage: data });
let alloc = mk_static!(Allocation<LittleFs2Filesystem>, lfs2Filesystem::allocate());
@@ -584,15 +595,18 @@ impl PlantHal {
}
}
fn ota_state(slot: AppPartitionSubType, ota_data: &mut FlashRegion<FlashStorage>) -> OtaImageState {
fn ota_state(
slot: AppPartitionSubType,
ota_data: &mut FlashRegion<MutexFlashStorage>,
) -> OtaImageState {
// Read and log OTA states for both slots before constructing Ota
// Each OTA select entry is 32 bytes: [seq:4][label:20][state:4][crc:4]
// Offsets within the OTA data partition: slot0 @ 0x0000, slot1 @ 0x1000
let mut slot_buf = [0u8; 32];
if slot == AppPartitionSubType::Ota0 {
let _ = ota_data.read(0x0000, &mut slot_buf);
let _ = ReadStorage::read(ota_data, 0x0000, &mut slot_buf);
} else {
let _ = ota_data.read(0x1000, &mut slot_buf);
let _ = ReadStorage::read(ota_data, 0x1000, &mut slot_buf);
}
let raw_state = u32::from_le_bytes(slot_buf[24..28].try_into().unwrap_or([0xff; 4]));
@@ -600,7 +614,7 @@ fn ota_state(slot: AppPartitionSubType, ota_data: &mut FlashRegion<FlashStorage>
}
fn get_current_slot_and_fix_ota_data(
ota: &mut Ota<FlashStorage>,
ota: &mut Ota<MutexFlashStorage>,
state0: OtaImageState,
state1: OtaImageState,
) -> Result<AppPartitionSubType, FatError> {