esp-hal release wip
This commit is contained in:
@@ -22,8 +22,8 @@ use embassy_sync::once_lock::OnceLock;
|
||||
use embassy_time::{Duration, Timer, WithTimeout};
|
||||
use embedded_storage::nor_flash::{check_erase, NorFlash, ReadNorFlash};
|
||||
use esp_bootloader_esp_idf::ota::OtaImageState::Valid;
|
||||
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState, Slot};
|
||||
use esp_bootloader_esp_idf::partitions::FlashRegion;
|
||||
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState};
|
||||
use esp_bootloader_esp_idf::partitions::{AppPartitionSubType, FlashRegion};
|
||||
use esp_hal::gpio::{Input, RtcPinWithResistors};
|
||||
use esp_hal::rng::Rng;
|
||||
use esp_hal::rtc_cntl::{
|
||||
@@ -32,11 +32,11 @@ use esp_hal::rtc_cntl::{
|
||||
};
|
||||
use esp_hal::system::software_reset;
|
||||
use esp_println::println;
|
||||
use esp_storage::FlashStorage;
|
||||
use esp_wifi::wifi::{
|
||||
AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration,
|
||||
ScanConfig, ScanTypeConfig, WifiController, WifiDevice, WifiState,
|
||||
use esp_radio::wifi::{
|
||||
AccessPointConfig, AccessPointInfo, AuthMethod, ClientConfig, ModeConfig, ScanConfig,
|
||||
ScanTypeConfig, WifiController, WifiDevice, WifiStaState,
|
||||
};
|
||||
use esp_storage::FlashStorage;
|
||||
use littlefs2::fs::Filesystem;
|
||||
use littlefs2_core::{FileType, PathBuf, SeekFrom};
|
||||
use log::{info, warn};
|
||||
@@ -49,13 +49,13 @@ use smoltcp::socket::udp::PacketMetadata;
|
||||
use smoltcp::wire::DnsQueryType;
|
||||
use sntpc::{get_time, NtpContext, NtpTimestampGenerator};
|
||||
|
||||
#[esp_hal::ram(rtc_fast, persistent)]
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
|
||||
#[esp_hal::ram(rtc_fast, persistent)]
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut CONSECUTIVE_WATERING_PLANT: [u32; PLANT_COUNT] = [0; PLANT_COUNT];
|
||||
#[esp_hal::ram(rtc_fast, persistent)]
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut LOW_VOLTAGE_DETECTED: i8 = 0;
|
||||
#[esp_hal::ram(rtc_fast, persistent)]
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut RESTART_TO_CONF: i8 = 0;
|
||||
|
||||
const CONFIG_FILE: &str = "config.json";
|
||||
@@ -127,9 +127,9 @@ pub struct Esp<'a> {
|
||||
// RTC-capable GPIO used as external wake source (store the raw peripheral)
|
||||
pub wake_gpio1: esp_hal::peripherals::GPIO1<'static>,
|
||||
|
||||
pub ota: Ota<'static, FlashStorage>,
|
||||
pub ota_target: &'static mut FlashRegion<'static, FlashStorage>,
|
||||
pub current: Slot,
|
||||
pub ota: Ota<'static, FlashStorage<'static>>,
|
||||
pub ota_target: &'static mut FlashRegion<'static, FlashStorage<'static>>,
|
||||
pub current: AppPartitionSubType,
|
||||
pub slot0_state: OtaImageState,
|
||||
pub slot1_state: OtaImageState,
|
||||
}
|
||||
@@ -236,13 +236,19 @@ impl Esp<'_> {
|
||||
}
|
||||
|
||||
pub(crate) async fn finalize_ota(&mut self) -> Result<(), FatError> {
|
||||
let current = self.ota.current_slot()?;
|
||||
if self.ota.current_ota_state()? != OtaImageState::Valid {
|
||||
let current = self.ota.current_app_partition()?;
|
||||
if self.ota.current_ota_state()? != Valid {
|
||||
info!("Validating current slot {current:?} as it was able to ota");
|
||||
self.ota.set_current_ota_state(Valid)?;
|
||||
}
|
||||
|
||||
self.ota.set_current_slot(current.next())?;
|
||||
let next = match current {
|
||||
AppPartitionSubType::Ota0 => AppPartitionSubType::Ota1,
|
||||
AppPartitionSubType::Ota1 => AppPartitionSubType::Ota0,
|
||||
_ => {
|
||||
bail!("Invalid current slot {current:?} for ota");
|
||||
}
|
||||
};
|
||||
self.ota.set_current_app_partition(next)?;
|
||||
info!("switched slot");
|
||||
self.ota.set_current_ota_state(OtaImageState::New)?;
|
||||
info!("switched state for new partition");
|
||||
@@ -319,16 +325,10 @@ impl Esp<'_> {
|
||||
info!("start wifi scan");
|
||||
let mut lock = self.controller.try_lock()?;
|
||||
info!("start wifi scan lock");
|
||||
let scan_config = ScanConfig {
|
||||
ssid: None,
|
||||
bssid: None,
|
||||
channel: None,
|
||||
show_hidden: false,
|
||||
scan_type: ScanTypeConfig::Active {
|
||||
min: Default::default(),
|
||||
max: Default::default(),
|
||||
},
|
||||
};
|
||||
let scan_config = ScanConfig::default().with_scan_type(ScanTypeConfig::Active {
|
||||
min: Default::default(),
|
||||
max: Default::default(),
|
||||
});
|
||||
let rv = lock.scan_with_config_async(scan_config).await?;
|
||||
info!("end wifi scan lock");
|
||||
Ok(rv)
|
||||
@@ -378,14 +378,12 @@ impl Esp<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn wifi_ap(&mut self) -> FatResult<Stack<'static>> {
|
||||
pub(crate) async fn wifi_ap(&mut self, spawner: Spawner) -> FatResult<Stack<'static>> {
|
||||
let ssid = match self.load_config().await {
|
||||
Ok(config) => config.network.ap_ssid.as_str().to_string(),
|
||||
Err(_) => "PlantCtrl Emergency Mode".to_string(),
|
||||
};
|
||||
|
||||
let spawner = Spawner::for_current_executor().await;
|
||||
|
||||
let device = self.interface_ap.take().unwrap();
|
||||
let gw_ip_addr_str = "192.168.71.1";
|
||||
let gw_ip_addr = Ipv4Addr::from_str(gw_ip_addr_str).expect("failed to parse gateway ip");
|
||||
@@ -408,15 +406,9 @@ impl Esp<'_> {
|
||||
);
|
||||
let stack = mk_static!(Stack, stack);
|
||||
|
||||
let client_config = Configuration::AccessPoint(AccessPointConfiguration {
|
||||
ssid: ssid.clone(),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
self.controller
|
||||
.lock()
|
||||
.await
|
||||
.set_configuration(&client_config)?;
|
||||
let client_config =
|
||||
ModeConfig::AccessPoint(AccessPointConfig::default().with_ssid(ssid.clone()));
|
||||
self.controller.lock().await.set_config(&client_config)?;
|
||||
|
||||
println!("start new");
|
||||
self.controller.lock().await.start()?;
|
||||
@@ -445,8 +437,9 @@ impl Esp<'_> {
|
||||
pub(crate) async fn wifi(
|
||||
&mut self,
|
||||
network_config: &NetworkConfig,
|
||||
spawner: Spawner,
|
||||
) -> FatResult<Stack<'static>> {
|
||||
esp_wifi::wifi_set_log_verbose();
|
||||
esp_radio::wifi_set_log_verbose();
|
||||
let ssid = network_config.ssid.clone();
|
||||
match &ssid {
|
||||
Some(ssid) => {
|
||||
@@ -466,8 +459,6 @@ impl Esp<'_> {
|
||||
};
|
||||
let max_wait = network_config.max_wait;
|
||||
|
||||
let spawner = Spawner::for_current_executor().await;
|
||||
|
||||
let device = self.interface_sta.take().unwrap();
|
||||
let config = embassy_net::Config::dhcpv4(DhcpConfig::default());
|
||||
|
||||
@@ -482,17 +473,15 @@ impl Esp<'_> {
|
||||
);
|
||||
let stack = mk_static!(Stack, stack);
|
||||
|
||||
let client_config = Configuration::Client(ClientConfiguration {
|
||||
ssid,
|
||||
bssid: None,
|
||||
auth_method: AuthMethod::WPA2Personal, //FIXME read from config, fill via scan
|
||||
password,
|
||||
channel: None,
|
||||
});
|
||||
let client_config = ClientConfig::default()
|
||||
.with_ssid(ssid)
|
||||
.with_auth_method(AuthMethod::Wpa2Personal)
|
||||
.with_password(password);
|
||||
|
||||
self.controller
|
||||
.lock()
|
||||
.await
|
||||
.set_configuration(&client_config)?;
|
||||
.set_config(&ModeConfig::Client(client_config))?;
|
||||
spawner.spawn(net_task(runner)).ok();
|
||||
self.controller.lock().await.start_async().await?;
|
||||
|
||||
@@ -501,8 +490,8 @@ impl Esp<'_> {
|
||||
guard.current_time_us()
|
||||
} + max_wait as u64 * 1000;
|
||||
loop {
|
||||
let state = esp_wifi::wifi::sta_state();
|
||||
if state == WifiState::StaStarted {
|
||||
let state = esp_radio::wifi::sta_state();
|
||||
if state == WifiStaState::Started {
|
||||
self.controller.lock().await.connect()?;
|
||||
break;
|
||||
}
|
||||
@@ -520,8 +509,8 @@ impl Esp<'_> {
|
||||
guard.current_time_us()
|
||||
} + max_wait as u64 * 1000;
|
||||
loop {
|
||||
let state = esp_wifi::wifi::sta_state();
|
||||
if state == WifiState::StaConnected {
|
||||
let state = esp_radio::wifi::sta_state();
|
||||
if state == WifiStaState::Connected {
|
||||
break;
|
||||
}
|
||||
if {
|
||||
@@ -699,6 +688,7 @@ impl Esp<'_> {
|
||||
&mut self,
|
||||
network_config: &'static NetworkConfig,
|
||||
stack: Stack<'static>,
|
||||
spawner: Spawner,
|
||||
) -> FatResult<()> {
|
||||
let base_topic = network_config
|
||||
.base_topic
|
||||
@@ -751,7 +741,6 @@ impl Esp<'_> {
|
||||
let keep_alive = Duration::from_secs(60 * 60 * 2).as_secs() as u16;
|
||||
let (receiver, task) = builder.build(keep_alive);
|
||||
|
||||
let spawner = Spawner::for_current_executor().await;
|
||||
spawner.spawn(mqtt_incoming_task(
|
||||
receiver,
|
||||
round_trip_topic.clone(),
|
||||
|
||||
Reference in New Issue
Block a user