chore: initial commit
This commit is contained in:
+157
@@ -0,0 +1,157 @@
|
||||
#![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 picoserve::routing::get;
|
||||
use picoserve::{AppBuilder, AppRouter};
|
||||
|
||||
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");
|
||||
|
||||
struct AppProps;
|
||||
|
||||
impl AppBuilder for AppProps {
|
||||
type PathRouter = impl picoserve::routing::PathRouter;
|
||||
|
||||
fn build_app(self) -> picoserve::Router<Self::PathRouter> {
|
||||
picoserve::Router::new().route("/", get(|| async move { "Hello World" }))
|
||||
}
|
||||
}
|
||||
|
||||
#[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 net_config = embassy_net::Config::dhcpv4(Default::default());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
let app = mk_static!(AppRouter<AppProps>, AppProps.build_app());
|
||||
|
||||
let port = 80;
|
||||
let mut tcp_rx_buffer = [0; 1024];
|
||||
let mut tcp_tx_buffer = [0; 1024];
|
||||
let mut http_buffer = [0; 2048];
|
||||
let web_serve_config = picoserve::Config::const_default().keep_connection_alive();
|
||||
|
||||
picoserve::Server::new(app, &web_serve_config, &mut http_buffer)
|
||||
.listen_and_serve(0, net_stack, port, &mut tcp_rx_buffer, &mut tcp_tx_buffer)
|
||||
.await;
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#![no_std]
|
||||
Reference in New Issue
Block a user