esp-hal release wip
This commit is contained in:
@@ -27,14 +27,8 @@ use esp_hal::peripherals::GPIO2;
|
||||
use esp_hal::peripherals::GPIO21;
|
||||
use esp_hal::peripherals::GPIO22;
|
||||
use esp_hal::peripherals::GPIO23;
|
||||
use esp_hal::peripherals::GPIO24;
|
||||
use esp_hal::peripherals::GPIO25;
|
||||
use esp_hal::peripherals::GPIO26;
|
||||
use esp_hal::peripherals::GPIO27;
|
||||
use esp_hal::peripherals::GPIO28;
|
||||
use esp_hal::peripherals::GPIO29;
|
||||
use esp_hal::peripherals::GPIO3;
|
||||
use esp_hal::peripherals::GPIO30;
|
||||
use esp_hal::peripherals::GPIO4;
|
||||
use esp_hal::peripherals::GPIO5;
|
||||
use esp_hal::peripherals::GPIO6;
|
||||
@@ -89,9 +83,9 @@ use embedded_storage::nor_flash::ReadNorFlash;
|
||||
use esp_alloc as _;
|
||||
use esp_backtrace as _;
|
||||
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState};
|
||||
use esp_bootloader_esp_idf::ota::{Slot as ota_slot, Slot};
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::i2c::master::{BusTimeout, Config, I2c};
|
||||
use esp_hal::interrupt::software::SoftwareInterruptControl;
|
||||
use esp_hal::pcnt::unit::Unit;
|
||||
use esp_hal::pcnt::Pcnt;
|
||||
use esp_hal::rng::Rng;
|
||||
@@ -100,8 +94,8 @@ use esp_hal::system::reset_reason;
|
||||
use esp_hal::time::Rate;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_hal::Blocking;
|
||||
use esp_radio::{init, Controller};
|
||||
use esp_storage::FlashStorage;
|
||||
use esp_wifi::{init, EspWifiController};
|
||||
use littlefs2::fs::{Allocation, Filesystem as lfs2Filesystem};
|
||||
use littlefs2::object_safe::DynStorage;
|
||||
use log::{error, info, warn};
|
||||
@@ -221,13 +215,7 @@ pub struct FreePeripherals<'a> {
|
||||
pub gpio21: GPIO21<'a>,
|
||||
pub gpio22: GPIO22<'a>,
|
||||
pub gpio23: GPIO23<'a>,
|
||||
pub gpio24: GPIO24<'a>,
|
||||
pub gpio25: GPIO25<'a>,
|
||||
pub gpio26: GPIO26<'a>,
|
||||
pub gpio27: GPIO27<'a>,
|
||||
pub gpio28: GPIO28<'a>,
|
||||
pub gpio29: GPIO29<'a>,
|
||||
pub gpio30: GPIO30<'a>,
|
||||
pub twai: TWAI0<'a>,
|
||||
pub pcnt0: Unit<'a, 0>,
|
||||
pub pcnt1: Unit<'a, 1>,
|
||||
@@ -258,7 +246,9 @@ impl PlantHal {
|
||||
error: "Init error rct".to_string(),
|
||||
})?;
|
||||
|
||||
let systimer = SystemTimer::new(peripherals.SYSTIMER);
|
||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
||||
let sw_int = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
|
||||
esp_rtos::start(timg0.timer0, sw_int.software_interrupt0);
|
||||
|
||||
let boot_button = Input::new(
|
||||
peripherals.GPIO9,
|
||||
@@ -268,21 +258,15 @@ impl PlantHal {
|
||||
// Reserve GPIO1 for deep sleep wake (configured just before entering sleep)
|
||||
let wake_gpio1 = peripherals.GPIO1;
|
||||
|
||||
let rng = Rng::new(peripherals.RNG);
|
||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
||||
let rng = Rng::new();
|
||||
let esp_wifi_ctrl = &*mk_static!(
|
||||
EspWifiController<'static>,
|
||||
init(timg0.timer0, rng).expect("Could not init wifi controller")
|
||||
Controller<'static>,
|
||||
init().expect("Could not init wifi controller")
|
||||
);
|
||||
|
||||
let (controller, interfaces) =
|
||||
esp_wifi::wifi::new(esp_wifi_ctrl, peripherals.WIFI).expect("Could not init wifi");
|
||||
|
||||
use esp_hal::timer::systimer::SystemTimer;
|
||||
esp_hal_embassy::init(systimer.alarm0);
|
||||
|
||||
//let mut adc1 = Adc::new(peripherals.ADC1, adc1_config);
|
||||
//
|
||||
esp_radio::wifi::new(esp_wifi_ctrl, peripherals.WIFI, Default::default())
|
||||
.expect("Could not init wifi");
|
||||
|
||||
let pcnt_module = Pcnt::new(peripherals.PCNT);
|
||||
|
||||
@@ -311,13 +295,7 @@ impl PlantHal {
|
||||
gpio21: peripherals.GPIO21,
|
||||
gpio22: peripherals.GPIO22,
|
||||
gpio23: peripherals.GPIO23,
|
||||
gpio24: peripherals.GPIO24,
|
||||
gpio25: peripherals.GPIO25,
|
||||
gpio26: peripherals.GPIO26,
|
||||
gpio27: peripherals.GPIO27,
|
||||
gpio28: peripherals.GPIO28,
|
||||
gpio29: peripherals.GPIO29,
|
||||
gpio30: peripherals.GPIO30,
|
||||
twai: peripherals.TWAI0,
|
||||
pcnt0: pcnt_module.unit0,
|
||||
pcnt1: pcnt_module.unit1,
|
||||
@@ -328,9 +306,10 @@ impl PlantHal {
|
||||
[u8; esp_bootloader_esp_idf::partitions::PARTITION_TABLE_MAX_LEN],
|
||||
[0u8; esp_bootloader_esp_idf::partitions::PARTITION_TABLE_MAX_LEN]
|
||||
);
|
||||
let storage_ota = mk_static!(FlashStorage, FlashStorage::new());
|
||||
let flash_storage = mk_static!(FlashStorage, FlashStorage::new(peripherals.FLASH));
|
||||
|
||||
let pt =
|
||||
esp_bootloader_esp_idf::partitions::read_partition_table(storage_ota, tablebuffer)?;
|
||||
esp_bootloader_esp_idf::partitions::read_partition_table(flash_storage, tablebuffer)?;
|
||||
|
||||
let ota_data = mk_static!(
|
||||
PartitionEntry,
|
||||
@@ -342,14 +321,14 @@ impl PlantHal {
|
||||
|
||||
let ota_data = mk_static!(
|
||||
FlashRegion<FlashStorage>,
|
||||
ota_data.as_embedded_storage(storage_ota)
|
||||
ota_data.as_embedded_storage(flash_storage)
|
||||
);
|
||||
|
||||
let state_0 = ota_state(ota_slot::Slot0, ota_data);
|
||||
let state_1 = ota_state(ota_slot::Slot1, ota_data);
|
||||
let mut ota = Ota::new(ota_data)?;
|
||||
let state_0 = ota_state(AppPartitionSubType::Ota0, ota_data);
|
||||
let state_1 = ota_state(AppPartitionSubType::Ota1, ota_data);
|
||||
let mut ota = Ota::new(ota_data, 2)?;
|
||||
let running = get_current_slot_and_fix_ota_data(&mut ota, state_0, state_1)?;
|
||||
let target = running.next();
|
||||
let target = next_partition(running)?;
|
||||
|
||||
info!("Currently running OTA slot: {running:?}");
|
||||
info!("Slot0 state: {state_0:?}");
|
||||
@@ -357,26 +336,25 @@ impl PlantHal {
|
||||
|
||||
//obtain current_state and next_state here!
|
||||
let ota_target = match target {
|
||||
Slot::None => {
|
||||
panic!("No OTA slot active?");
|
||||
}
|
||||
Slot::Slot0 => pt
|
||||
AppPartitionSubType::Ota0 => pt
|
||||
.find_partition(esp_bootloader_esp_idf::partitions::PartitionType::App(
|
||||
AppPartitionSubType::Ota0,
|
||||
))?
|
||||
.context("Partition table invalid no ota0")?,
|
||||
Slot::Slot1 => pt
|
||||
AppPartitionSubType::Ota1 => pt
|
||||
.find_partition(esp_bootloader_esp_idf::partitions::PartitionType::App(
|
||||
AppPartitionSubType::Ota1,
|
||||
))?
|
||||
.context("Partition table invalid no ota1")?,
|
||||
_ => {
|
||||
bail!("Invalid target partition");
|
||||
}
|
||||
};
|
||||
|
||||
let ota_target = mk_static!(PartitionEntry, ota_target);
|
||||
let storage_ota = mk_static!(FlashStorage, FlashStorage::new());
|
||||
let ota_target = mk_static!(
|
||||
FlashRegion<FlashStorage>,
|
||||
ota_target.as_embedded_storage(storage_ota)
|
||||
ota_target.as_embedded_storage(flash_storage)
|
||||
);
|
||||
|
||||
let data_partition = pt
|
||||
@@ -386,10 +364,9 @@ impl PlantHal {
|
||||
.expect("Data partition with littlefs not found");
|
||||
let data_partition = mk_static!(PartitionEntry, data_partition);
|
||||
|
||||
let storage_data = mk_static!(FlashStorage, FlashStorage::new());
|
||||
let data = mk_static!(
|
||||
FlashRegion<FlashStorage>,
|
||||
data_partition.as_embedded_storage(storage_data)
|
||||
data_partition.as_embedded_storage(flash_storage)
|
||||
);
|
||||
let lfs2filesystem = mk_static!(LittleFs2Filesystem, LittleFs2Filesystem { storage: data });
|
||||
let alloc = mk_static!(Allocation<LittleFs2Filesystem>, lfs2Filesystem::allocate());
|
||||
@@ -607,15 +584,12 @@ impl PlantHal {
|
||||
}
|
||||
}
|
||||
|
||||
fn ota_state(slot: ota_slot, ota_data: &mut FlashRegion<FlashStorage>) -> OtaImageState {
|
||||
fn ota_state(slot: AppPartitionSubType, ota_data: &mut FlashRegion<FlashStorage>) -> 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
|
||||
if slot == ota_slot::None {
|
||||
return OtaImageState::Undefined;
|
||||
}
|
||||
let mut slot_buf = [0u8; 32];
|
||||
if slot == ota_slot::Slot0 {
|
||||
if slot == AppPartitionSubType::Ota0 {
|
||||
let _ = ota_data.read(0x0000, &mut slot_buf);
|
||||
} else {
|
||||
let _ = ota_data.read(0x1000, &mut slot_buf);
|
||||
@@ -629,7 +603,7 @@ fn get_current_slot_and_fix_ota_data(
|
||||
ota: &mut Ota<FlashStorage>,
|
||||
state0: OtaImageState,
|
||||
state1: OtaImageState,
|
||||
) -> Result<ota_slot, FatError> {
|
||||
) -> Result<AppPartitionSubType, FatError> {
|
||||
let state = ota.current_ota_state().unwrap_or_default();
|
||||
let swap = match state {
|
||||
OtaImageState::Invalid => true,
|
||||
@@ -640,11 +614,11 @@ fn get_current_slot_and_fix_ota_data(
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
let current = ota.current_slot()?;
|
||||
let current = ota.current_app_partition()?;
|
||||
if swap {
|
||||
let other = match current {
|
||||
ota_slot::Slot0 => state1,
|
||||
ota_slot::Slot1 => state0,
|
||||
AppPartitionSubType::Ota0 => state1,
|
||||
AppPartitionSubType::Ota1 => state0,
|
||||
_ => OtaImageState::Invalid,
|
||||
};
|
||||
|
||||
@@ -668,13 +642,26 @@ fn get_current_slot_and_fix_ota_data(
|
||||
_ => {}
|
||||
}
|
||||
info!("Current slot has state {state:?} other state has {other:?} swapping");
|
||||
ota.set_current_slot(current.next())?;
|
||||
let next = next_partition(current)?;
|
||||
ota.set_current_app_partition(next)?;
|
||||
|
||||
//we actually booted other slot, than partition table assumes
|
||||
return Ok(ota.current_slot()?);
|
||||
return Ok(ota.current_app_partition()?);
|
||||
};
|
||||
Ok(current)
|
||||
}
|
||||
|
||||
pub fn next_partition(current: AppPartitionSubType) -> FatResult<AppPartitionSubType> {
|
||||
let next = match current {
|
||||
AppPartitionSubType::Ota0 => AppPartitionSubType::Ota1,
|
||||
AppPartitionSubType::Ota1 => AppPartitionSubType::Ota0,
|
||||
_ => {
|
||||
bail!("Current slot is not ota0 or ota1");
|
||||
}
|
||||
};
|
||||
Ok(next)
|
||||
}
|
||||
|
||||
pub async fn esp_time() -> DateTime<Utc> {
|
||||
let guard = TIME_ACCESS.get().await.lock().await;
|
||||
DateTime::from_timestamp_micros(guard.current_time_us() as i64).unwrap()
|
||||
|
||||
Reference in New Issue
Block a user