littlefs2 impl stuff

This commit is contained in:
2025-09-17 01:36:53 +02:00
parent 8b938e7687
commit 4c54edbcea
4 changed files with 124 additions and 58 deletions

View File

@@ -1,3 +1,4 @@
mod LittleFS2StorageAdapter;
pub(crate) mod battery;
pub mod esp;
mod initial_hal;
@@ -22,7 +23,7 @@ use crate::{
use alloc::boxed::Box;
use alloc::format;
use alloc::sync::Arc;
use anyhow::{Ok, Result};
use anyhow::{bail, Ok, Result};
use async_trait::async_trait;
use embassy_executor::Spawner;
//use battery::BQ34Z100G1;
@@ -32,11 +33,13 @@ use esp_bootloader_esp_idf::partitions::{
AppPartitionSubType, DataPartitionSubType, FlashRegion, PartitionEntry,
};
use esp_hal::clock::CpuClock;
use esp_hal::gpio::{Input, InputConfig, Io, Pull};
use esp_hal::gpio::{Input, InputConfig, Pull};
use esp_println::println;
use measurements::{Current, Voltage};
use crate::hal::LittleFS2StorageAdapter::LittleFs2Filesystem;
use embassy_sync::mutex::Mutex;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use esp_alloc as _;
use esp_backtrace as _;
use esp_bootloader_esp_idf::ota::Slot;
@@ -44,7 +47,8 @@ use esp_hal::rng::Rng;
use esp_hal::timer::timg::TimerGroup;
use esp_storage::FlashStorage;
use esp_wifi::{init, EspWifiController};
use littlefs2::fs::Filesystem;
use littlefs2::fs::{Allocation, Filesystem as lfs2Filesystem};
use littlefs2::object_safe::DynStorage;
//Only support for 8 right now!
pub const PLANT_COUNT: usize = 8;
@@ -275,6 +279,7 @@ impl PlantHal {
};
let ota_next = mk_static!(PartitionEntry, ota_partition);
let storage_ota = mk_static!(FlashStorage, FlashStorage::new());
let ota_next = mk_static!(
FlashRegion<FlashStorage>,
ota_next.as_embedded_storage(storage_ota)
@@ -285,12 +290,34 @@ impl PlantHal {
DataPartitionSubType::LittleFs,
))?
.unwrap();
let data_partition = mk_static!(PartitionEntry, data_partition);
let mut data = data_partition.as_embedded_storage(storage_ota);
let mut alloc = Filesystem::allocate();
let mut fs = Filesystem::mount(&mut alloc, &mut data).unwrap();
let storage_data = mk_static!(FlashStorage, FlashStorage::new());
let mut data = mk_static!(
FlashRegion<FlashStorage>,
data_partition.as_embedded_storage(storage_data)
);
let lfs2filesystem = mk_static!(LittleFs2Filesystem, LittleFs2Filesystem { storage: data });
let alloc = mk_static!(Allocation<LittleFs2Filesystem>, lfs2Filesystem::allocate());
if lfs2filesystem.is_mountable() {
log::info!("Littlefs2 filesystem is mountable");
} else {
match lfs2filesystem.format() {
Result::Ok(..) => {
log::info!("Littlefs2 filesystem is formatted");
}
Err(err) => {
bail!("Littlefs2 filesystem could not be formatted: {:?}", err);
}
}
}
let fs = Arc::new(Mutex::new(
lfs2Filesystem::mount(alloc, lfs2filesystem).unwrap(),
));
let mut esp = Esp {
fs,
rng,
controller: Arc::new(Mutex::new(controller)),
interfaces: Some(interfaces),