improved wifi setup

This commit is contained in:
Djeeberjr 2025-10-27 15:23:06 +01:00
parent d63e9e964d
commit 9852534dc6
2 changed files with 35 additions and 18 deletions

View File

@ -3,11 +3,10 @@ use embassy_net::Stack;
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use esp_hal::Blocking; use esp_hal::Blocking;
use esp_hal::delay::Delay; use esp_hal::delay::Delay;
use esp_hal::gpio::{AnyPin}; use esp_hal::gpio::AnyPin;
use esp_hal::i2c::master::Config; use esp_hal::i2c::master::Config;
use esp_hal::peripherals::{ use esp_hal::peripherals::{
GPIO1, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, I2C0, RMT, SPI2, GPIO1, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, I2C0, RMT, SPI2, UART1,
UART1,
}; };
use esp_hal::rmt::Rmt; use esp_hal::rmt::Rmt;
use esp_hal::spi::master::{Config as Spi_config, Spi}; use esp_hal::spi::master::{Config as Spi_config, Spi};
@ -73,6 +72,9 @@ pub enum HardwareInitError {
#[error("Failed to setuo LED")] #[error("Failed to setuo LED")]
Led(#[from] esp_hal::rmt::Error), Led(#[from] esp_hal::rmt::Error),
#[error("Failed to setup wifi")]
Wifi(#[from] wifi::WifiError),
} }
pub struct AppHardware { pub struct AppHardware {
@ -103,7 +105,7 @@ impl AppHardware {
let network_seed = (rng.random() as u64) << 32 | rng.random() as u64; let network_seed = (rng.random() as u64) << 32 | rng.random() as u64;
wifi::set_antenna_mode(peripherals.GPIO3, peripherals.GPIO14).await; wifi::set_antenna_mode(peripherals.GPIO3, peripherals.GPIO14).await;
let interfaces = wifi::setup_wifi(peripherals.WIFI, spawner); let interfaces = wifi::setup_wifi(peripherals.WIFI, spawner)?;
let network_stack = network::setup_network(network_seed, interfaces.ap, spawner); let network_stack = network::setup_network(network_seed, interfaces.ap, spawner);
Timer::after(Duration::from_millis(1)).await; Timer::after(Duration::from_millis(1)).await;

View File

@ -8,8 +8,7 @@ use esp_radio::wifi::{
}; };
use log::debug; use log::debug;
use static_cell::StaticCell; use static_cell::StaticCell;
use thiserror::Error;
static ESP_WIFI_CTRL: StaticCell<Controller<'static>> = StaticCell::new();
pub async fn set_antenna_mode(gpio3: GPIO3<'static>, gpio14: GPIO14<'static>) { pub async fn set_antenna_mode(gpio3: GPIO3<'static>, gpio14: GPIO14<'static>) {
let mut rf_switch = Output::new(gpio3, esp_hal::gpio::Level::Low, OutputConfig::default()); let mut rf_switch = Output::new(gpio3, esp_hal::gpio::Level::Low, OutputConfig::default());
@ -23,29 +22,45 @@ pub async fn set_antenna_mode(gpio3: GPIO3<'static>, gpio14: GPIO14<'static>) {
antenna_mode.set_low(); antenna_mode.set_low();
} }
pub fn setup_wifi<'d: 'static>(wifi: WIFI<'static>, spawner: Spawner) -> Interfaces<'d> { #[derive(Error, Debug)]
let esp_wifi_ctrl = ESP_WIFI_CTRL.init(esp_radio::init().unwrap()); pub enum WifiError {
#[error("Failed to init radio")]
Init(#[from] esp_radio::InitializationError),
#[error("Failed to init wifi")]
Wifi(#[from] esp_radio::wifi::WifiError),
#[error("Failed to spawn wifi task")]
Spawn(#[from] embassy_executor::SpawnError),
}
pub fn setup_wifi<'d: 'static>(
wifi: WIFI<'static>,
spawner: Spawner,
) -> Result<Interfaces<'d>, WifiError> {
static ESP_WIFI_CTRL: StaticCell<Controller<'static>> = StaticCell::new();
let esp_wifi_ctrl = ESP_WIFI_CTRL.init(esp_radio::init()?);
let config = esp_radio::wifi::Config::default(); let config = esp_radio::wifi::Config::default();
let (controller, interfaces) = esp_radio::wifi::new(esp_wifi_ctrl, wifi, config).unwrap(); let (controller, interfaces) = esp_radio::wifi::new(esp_wifi_ctrl, wifi, config)?;
spawner.must_spawn(connection(controller)); spawner.spawn(connection(controller))?;
interfaces Ok(interfaces)
} }
#[embassy_executor::task] #[embassy_executor::task]
async fn connection(mut controller: WifiController<'static>) { async fn connection(mut controller: WifiController<'static>) {
debug!("start connection task"); debug!("start connection task");
debug!("Device capabilities: {:?}", controller.capabilities()); debug!("Device capabilities: {:?}", controller.capabilities());
loop { loop {
match esp_radio::wifi::ap_state() { if esp_radio::wifi::ap_state() == WifiApState::Started {
WifiApState::Started => { // wait until we're no longer connected
// wait until we're no longer connected controller.wait_for_event(WifiEvent::ApStop).await;
controller.wait_for_event(WifiEvent::ApStop).await; Timer::after(Duration::from_millis(5000)).await
Timer::after(Duration::from_millis(5000)).await
}
_ => {}
} }
if !matches!(controller.is_started(), Ok(true)) { if !matches!(controller.is_started(), Ok(true)) {
let client_config = ModeConfig::AccessPoint( let client_config = ModeConfig::AccessPoint(
AccessPointConfig::default() AccessPointConfig::default()