147 lines
4.7 KiB
Rust
147 lines
4.7 KiB
Rust
#![no_std]
|
|
#![no_main]
|
|
#![deny(
|
|
clippy::mem_forget,
|
|
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
|
|
holding buffers for the duration of a data transfer."
|
|
)]
|
|
#![deny(clippy::large_stack_frames)]
|
|
#![feature(impl_trait_in_assoc_type)]
|
|
|
|
use defmt::{error, info};
|
|
use embassy_executor::Spawner;
|
|
use embassy_net::Runner;
|
|
use embassy_time::{Duration, Timer};
|
|
use esp_backtrace as _;
|
|
use esp_hal::clock::CpuClock;
|
|
use esp_hal::timer::timg::TimerGroup;
|
|
use esp_println as _;
|
|
use esp_radio::wifi::sta::StationConfig;
|
|
use esp_radio::wifi::{ControllerConfig, Interface, WifiController};
|
|
|
|
use cover_theif::{http_api::{self, HttpApiProps, HttpApiService}, clock};
|
|
|
|
extern crate alloc;
|
|
|
|
// This creates a default app-descriptor required by the esp-idf bootloader.
|
|
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
|
|
esp_bootloader_esp_idf::esp_app_desc!();
|
|
|
|
// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
|
|
macro_rules! mk_static {
|
|
($t:ty,$val:expr) => {{
|
|
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
|
|
#[deny(unused_attributes)]
|
|
let x = STATIC_CELL.uninit().write($val);
|
|
x
|
|
}};
|
|
}
|
|
|
|
const SSID: &str = env!("SSID");
|
|
const PASSWORD: &str = env!("PASSWORD");
|
|
|
|
#[allow(
|
|
clippy::large_stack_frames,
|
|
reason = "it's not unusual to allocate larger buffers etc. in main"
|
|
)]
|
|
#[esp_rtos::main]
|
|
async fn main(spawner: Spawner) -> ! {
|
|
// generator version: 1.3.0
|
|
// generator parameters: --chip esp32c6 -o alloc -o unstable-hal -o esp-backtrace -o embassy -o defmt
|
|
|
|
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
|
let peripherals = esp_hal::init(config);
|
|
|
|
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 65536);
|
|
|
|
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
|
let sw_interrupt =
|
|
esp_hal::interrupt::software::SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
|
|
esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0);
|
|
|
|
info!("Embassy initialized!");
|
|
|
|
let wifi_config = esp_radio::wifi::Config::Station(
|
|
StationConfig::default()
|
|
.with_ssid(SSID)
|
|
.with_password(PASSWORD.into()),
|
|
);
|
|
|
|
let (wifi_controller, wifi_interfaces) = esp_radio::wifi::new(
|
|
peripherals.WIFI,
|
|
ControllerConfig::default().with_initial_config(wifi_config),
|
|
)
|
|
.expect("Failed to initialize Wi-Fi controller");
|
|
|
|
let mut dhcp_config = embassy_net::DhcpConfig::default();
|
|
dhcp_config.hostname = Some("cover-theif".try_into().unwrap());
|
|
let net_config = embassy_net::Config::dhcpv4(dhcp_config);
|
|
|
|
let rng = esp_hal::rng::Rng::new();
|
|
let seed = (rng.random() as u64) << 32 | rng.random() as u64;
|
|
|
|
let (net_stack, net_runner) = embassy_net::new(
|
|
wifi_interfaces.station,
|
|
net_config,
|
|
mk_static!(
|
|
embassy_net::StackResources<3>,
|
|
embassy_net::StackResources::<3>::new()
|
|
),
|
|
seed,
|
|
);
|
|
|
|
spawner.spawn(connection(wifi_controller).unwrap());
|
|
spawner.spawn(net_task(net_runner).unwrap());
|
|
|
|
net_stack.wait_config_up().await;
|
|
|
|
if let Some(config) = net_stack.config_v4() {
|
|
info!("Got IP: {}", config.address);
|
|
}
|
|
|
|
spawner.spawn(clock::ntp_upkeep_service(net_stack).unwrap());
|
|
|
|
spawner.spawn(http_api_serve_task(HttpApiService::new(net_stack, Default::default(), Default::default())).unwrap());
|
|
|
|
loop {
|
|
info!("Hello world");
|
|
Timer::after(Duration::from_secs(1)).await;
|
|
}
|
|
|
|
// for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.1.0/examples
|
|
}
|
|
|
|
#[embassy_executor::task]
|
|
async fn connection(mut controller: WifiController<'static>) {
|
|
info!("start connection task");
|
|
|
|
loop {
|
|
info!("About to connect...");
|
|
|
|
match controller.connect_async().await {
|
|
Ok(info) => {
|
|
info!("Wifi connected to {:?}", info);
|
|
|
|
// wait until we're no longer connected
|
|
let info = controller.wait_for_disconnect_async().await.ok();
|
|
info!("Disconnected: {:?}", info);
|
|
}
|
|
Err(e) => {
|
|
error!("Failed to connect to wifi: {:?}", e);
|
|
}
|
|
}
|
|
|
|
Timer::after(Duration::from_millis(5000)).await
|
|
}
|
|
}
|
|
|
|
#[embassy_executor::task]
|
|
async fn net_task(mut runner: Runner<'static, Interface<'static>>) {
|
|
runner.run().await
|
|
}
|
|
|
|
#[embassy_executor::task]
|
|
async fn http_api_serve_task(mut service: HttpApiService<'static, HttpApiProps>) {
|
|
service.serve().await;
|
|
}
|