-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.rs
146 lines (122 loc) · 3.97 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use cyw43_pio::PioSpi;
use embassy_rp::{
gpio::{Level, Output},
peripherals::{DMA_CH0, PIN_23, PIN_25, PIO0},
pio::Pio,
};
use embassy_time::Duration;
use panic_persist as _;
use picoserve::routing::get;
use rand::Rng;
use static_cell::make_static;
embassy_rp::bind_interrupts!(struct Irqs {
PIO0_IRQ_0 => embassy_rp::pio::InterruptHandler<embassy_rp::peripherals::PIO0>;
USBCTRL_IRQ => embassy_rp::usb::InterruptHandler<embassy_rp::peripherals::USB>;
});
#[embassy_executor::task]
async fn logger_task(usb: embassy_rp::peripherals::USB) {
let driver = embassy_rp::usb::Driver::new(usb, Irqs);
embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver);
}
#[embassy_executor::task]
async fn wifi_task(
runner: cyw43::Runner<
'static,
Output<'static, PIN_23>,
PioSpi<'static, PIN_25, PIO0, 0, DMA_CH0>,
>,
) -> ! {
runner.run().await
}
#[embassy_executor::task]
async fn net_task(stack: &'static embassy_net::Stack<cyw43::NetDriver<'static>>) -> ! {
stack.run().await
}
type AppRouter = impl picoserve::routing::PathRouter;
const WEB_TASK_POOL_SIZE: usize = 8;
#[embassy_executor::task(pool_size = WEB_TASK_POOL_SIZE)]
async fn web_task(
id: usize,
stack: &'static embassy_net::Stack<cyw43::NetDriver<'static>>,
app: &'static picoserve::Router<AppRouter>,
config: &'static picoserve::Config<Duration>,
) -> ! {
let port = 80;
let mut tcp_rx_buffer = [0; 1024];
let mut tcp_tx_buffer = [0; 1024];
let mut http_buffer = [0; 2048];
picoserve::listen_and_serve(
id,
app,
config,
stack,
port,
&mut tcp_rx_buffer,
&mut tcp_tx_buffer,
&mut http_buffer,
)
.await
}
#[embassy_executor::main]
async fn main(spawner: embassy_executor::Spawner) {
let p = embassy_rp::init(Default::default());
spawner.must_spawn(logger_task(p.USB));
if let Some(panic_message) = panic_persist::get_panic_message_utf8() {
loop {
log::error!("{panic_message}");
embassy_time::Timer::after_secs(5).await;
}
}
let fw = include_bytes!("../../cyw43-firmware/43439A0.bin");
let clm = include_bytes!("../../cyw43-firmware/43439A0_clm.bin");
let pwr = Output::new(p.PIN_23, Level::Low);
let cs = Output::new(p.PIN_25, Level::High);
let mut pio = Pio::new(p.PIO0, Irqs);
let spi = cyw43_pio::PioSpi::new(
&mut pio.common,
pio.sm0,
pio.irq0,
cs,
p.PIN_24,
p.PIN_29,
p.DMA_CH0,
);
let state = make_static!(cyw43::State::new());
let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
spawner.must_spawn(wifi_task(runner));
control.init(clm).await;
let stack = &*make_static!(embassy_net::Stack::new(
net_device,
embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(192, 168, 0, 1), 24),
gateway: None,
dns_servers: Default::default(),
}),
make_static!(embassy_net::StackResources::<WEB_TASK_POOL_SIZE>::new()),
embassy_rp::clocks::RoscRng.gen(),
));
spawner.must_spawn(net_task(stack));
control
.start_ap_wpa2(
example_secrets::WIFI_SSID,
example_secrets::WIFI_PASSWORD,
8,
)
.await;
fn make_app() -> picoserve::Router<AppRouter> {
picoserve::Router::new().route("/", get(|| async move { "Hello World" }))
}
let app = make_static!(make_app());
let config = make_static!(picoserve::Config::new(picoserve::Timeouts {
start_read_request: Some(Duration::from_secs(5)),
read_request: Some(Duration::from_secs(1)),
write: Some(Duration::from_secs(1)),
})
.keep_connection_alive());
for id in 0..WEB_TASK_POOL_SIZE {
spawner.must_spawn(web_task(id, stack, app, config));
}
}