esp-hosted: wait for esp firmware init.

This commit is contained in:
Dario Nieuwenhuis 2023-06-21 19:05:20 +02:00
parent 082f1ab494
commit 764b43e82c
4 changed files with 37 additions and 3 deletions

View file

@ -29,6 +29,9 @@ impl<'a> Control<'a> {
}
pub async fn init(&mut self) {
debug!("wait for init event...");
self.shared.init_wait().await;
debug!("set wifi mode");
self.set_wifi_mode(WifiMode::Sta as _).await;
let mac_addr = self.get_mac_addr().await;

View file

@ -23,6 +23,7 @@ pub struct Shared(RefCell<SharedInner>);
struct SharedInner {
ioctl: IoctlState,
is_init: bool,
control_waker: WakerRegistration,
runner_waker: WakerRegistration,
}
@ -31,6 +32,7 @@ impl Shared {
pub fn new() -> Self {
Self(RefCell::new(SharedInner {
ioctl: IoctlState::Done { resp_len: 0 },
is_init: false,
control_waker: WakerRegistration::new(),
runner_waker: WakerRegistration::new(),
}))
@ -97,4 +99,25 @@ impl Shared {
warn!("IOCTL Response but no pending Ioctl");
}
}
// // // // // // // // // // // // // // // // // // // //
pub fn init_done(&self) {
let mut this = self.0.borrow_mut();
this.is_init = true;
this.control_waker.wake();
}
pub async fn init_wait(&self) {
poll_fn(|cx| {
let mut this = self.0.borrow_mut();
if this.is_init {
Poll::Ready(())
} else {
this.control_waker.register(cx.waker());
Poll::Pending
}
})
.await
}
}

View file

@ -11,6 +11,7 @@ use ioctl::Shared;
use proto::CtrlMsg;
use crate::ioctl::PendingIoctl;
use crate::proto::CtrlMsgPayload;
mod proto;
@ -308,6 +309,16 @@ where
};
debug!("event: {:?}", &event);
let Some(payload) = &event.payload else {
warn!("event without payload?");
return
};
match payload {
CtrlMsgPayload::EventEspInit(_) => self.shared.init_done(),
_ => {}
}
}
}

View file

@ -69,9 +69,6 @@ async fn main(spawner: Spawner) {
unwrap!(spawner.spawn(wifi_task(runner)));
// TODO: wait for ESP_INIT event instead of hardcoding delay.
Timer::after(Duration::from_secs(3)).await;
control.init().await;
control.join(env!("WIFI_NETWORK"), env!("WIFI_PASSWORD")).await;