2025-03-14 17:42:07 +00:00
|
|
|
//! Raspberry Pi Pico W meat thermometer
|
2025-03-13 13:28:48 +00:00
|
|
|
|
|
|
|
|
#![no_std]
|
|
|
|
|
#![no_main]
|
2025-03-14 17:42:07 +00:00
|
|
|
// required for impl in AppProps code for picoserve
|
|
|
|
|
#![feature(impl_trait_in_assoc_type)]
|
2025-03-13 13:28:48 +00:00
|
|
|
|
|
|
|
|
use cyw43::JoinOptions;
|
|
|
|
|
use cyw43_pio::{PioSpi, DEFAULT_CLOCK_DIVIDER};
|
|
|
|
|
use embassy_executor::Spawner;
|
2025-03-14 17:42:07 +00:00
|
|
|
use embassy_net::{Config, StackResources, Ipv4Cidr, Ipv4Address};
|
2025-03-13 13:28:48 +00:00
|
|
|
use embassy_rp::bind_interrupts;
|
|
|
|
|
use embassy_rp::clocks::RoscRng;
|
|
|
|
|
use embassy_rp::gpio::{Level, Output};
|
|
|
|
|
use embassy_rp::peripherals::{DMA_CH0, PIO0};
|
|
|
|
|
use embassy_rp::peripherals::USB;
|
|
|
|
|
use embassy_rp::pio::{InterruptHandler, Pio};
|
|
|
|
|
use embassy_rp::usb::{Driver, InterruptHandler as USBInterruptHandler};
|
|
|
|
|
use embassy_time::{Duration, Timer};
|
2025-03-14 17:42:07 +00:00
|
|
|
use heapless::Vec;
|
|
|
|
|
use picoserve::{
|
|
|
|
|
make_static,
|
|
|
|
|
routing::{get_service, PathRouter},
|
|
|
|
|
AppBuilder, AppRouter
|
|
|
|
|
};
|
|
|
|
|
use picoserve::response::File;
|
2025-03-13 13:28:48 +00:00
|
|
|
use rand::RngCore;
|
|
|
|
|
use static_cell::StaticCell;
|
|
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
|
|
|
|
|
const WIFI_NETWORK: &str = "bendybogalow";
|
|
|
|
|
const WIFI_PASSWORD: &str = "parsnipcabbageonion";
|
|
|
|
|
|
|
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
|
PIO0_IRQ_0 => InterruptHandler<PIO0>;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bind_interrupts!(struct UsbIrqs {
|
|
|
|
|
USBCTRL_IRQ => USBInterruptHandler<USB>;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
|
async fn logger_task(driver: Driver<'static, USB>) {
|
|
|
|
|
embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
|
async fn cyw43_task(runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>) -> ! {
|
|
|
|
|
runner.run().await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
|
async fn net_task(mut runner: embassy_net::Runner<'static, cyw43::NetDriver<'static>>) -> ! {
|
|
|
|
|
runner.run().await
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 17:42:07 +00:00
|
|
|
|
|
|
|
|
// picoserve HTTP code kicked off using: https://github.com/sammhicks/picoserve/blob/main/examples/embassy/hello_world/src/main.rs
|
|
|
|
|
struct AppProps;
|
|
|
|
|
|
|
|
|
|
impl AppBuilder for AppProps {
|
|
|
|
|
type PathRouter = impl PathRouter;
|
|
|
|
|
|
|
|
|
|
fn build_app(self) -> picoserve::Router<Self::PathRouter> {
|
|
|
|
|
picoserve::Router::new()
|
|
|
|
|
.route(
|
|
|
|
|
"/",
|
|
|
|
|
get_service(File::html(include_str!("html/index.html")))
|
|
|
|
|
)
|
|
|
|
|
.route(
|
|
|
|
|
"/main.css",
|
|
|
|
|
get_service(File::css(include_str!("html/main.css")))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2 is plenty of a little IoT thermometer, right?
|
|
|
|
|
const WEB_TASK_POOL_SIZE: usize = 2;
|
|
|
|
|
|
|
|
|
|
#[embassy_executor::task(pool_size = WEB_TASK_POOL_SIZE)]
|
|
|
|
|
async fn web_task(
|
|
|
|
|
id: usize,
|
|
|
|
|
stack: embassy_net::Stack<'static>,
|
|
|
|
|
app: &'static AppRouter<AppProps>,
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 13:28:48 +00:00
|
|
|
#[embassy_executor::main]
|
|
|
|
|
async fn main(spawner: Spawner) {
|
|
|
|
|
let p = embassy_rp::init(Default::default());
|
|
|
|
|
let driver = Driver::new(p.USB, UsbIrqs);
|
|
|
|
|
spawner.spawn(logger_task(driver)).unwrap();
|
|
|
|
|
let mut rng = RoscRng;
|
|
|
|
|
|
|
|
|
|
// TODO: this was required to make log entries before the loop actually reach the TTY - so I am
|
|
|
|
|
// guessing there is some setup happening in the background and wonder if there is a better way
|
|
|
|
|
// to wait for that than a sleep...
|
|
|
|
|
Timer::after_secs(1).await;
|
|
|
|
|
|
|
|
|
|
log::info!("main: entry");
|
|
|
|
|
|
|
|
|
|
let fw = include_bytes!("../cyw43-firmware/43439A0.bin");
|
|
|
|
|
let clm = include_bytes!("../cyw43-firmware/43439A0_clm.bin");
|
|
|
|
|
|
|
|
|
|
// To make flashing faster for development, you may want to flash the firmwares independently
|
|
|
|
|
// at hardcoded addresses, instead of baking them into the program with `include_bytes!`:
|
|
|
|
|
// probe-rs download ../../cyw43-firmware/43439A0.bin --binary-format bin --chip RP2040 --base-address 0x10100000
|
|
|
|
|
// probe-rs download ../../cyw43-firmware/43439A0_clm.bin --binary-format bin --chip RP2040 --base-address 0x10140000
|
|
|
|
|
//let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 230321) };
|
|
|
|
|
//let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
|
|
|
|
|
|
|
|
|
|
log::info!("main: init IO");
|
|
|
|
|
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 = PioSpi::new(
|
|
|
|
|
&mut pio.common,
|
|
|
|
|
pio.sm0,
|
|
|
|
|
DEFAULT_CLOCK_DIVIDER,
|
|
|
|
|
pio.irq0,
|
|
|
|
|
cs,
|
|
|
|
|
p.PIN_24,
|
|
|
|
|
p.PIN_29,
|
|
|
|
|
p.DMA_CH0,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
log::info!("main: init wifi");
|
|
|
|
|
static STATE: StaticCell<cyw43::State> = StaticCell::new();
|
|
|
|
|
let state = STATE.init(cyw43::State::new());
|
|
|
|
|
let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
|
|
|
|
|
defmt::unwrap!(spawner.spawn(cyw43_task(runner)));
|
|
|
|
|
|
|
|
|
|
log::info!("main: init clm");
|
|
|
|
|
control.init(clm).await;
|
|
|
|
|
control
|
|
|
|
|
.set_power_management(cyw43::PowerManagementMode::PowerSave)
|
|
|
|
|
.await;
|
|
|
|
|
|
2025-03-14 17:42:07 +00:00
|
|
|
//let config = Config::dhcpv4(Default::default());
|
|
|
|
|
let config = embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
|
|
|
|
|
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 3, 14), 24),
|
|
|
|
|
dns_servers: Vec::new(),
|
|
|
|
|
gateway: Some(Ipv4Address::new(192, 168, 3, 1)),
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-13 13:28:48 +00:00
|
|
|
// Generate random seed
|
|
|
|
|
let seed = rng.next_u64();
|
|
|
|
|
|
|
|
|
|
// Init network stack
|
|
|
|
|
static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
|
|
|
|
|
let (stack, runner) = embassy_net::new(net_device, config, RESOURCES.init(StackResources::new()), seed);
|
|
|
|
|
|
|
|
|
|
defmt::unwrap!(spawner.spawn(net_task(runner)));
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
match control
|
|
|
|
|
.join(WIFI_NETWORK, JoinOptions::new(WIFI_PASSWORD.as_bytes()))
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => break,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("join failed with status={}", err.status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for DHCP, not necessary when using static IP
|
2025-03-14 17:42:07 +00:00
|
|
|
/*log::info!("waiting for DHCP...");
|
2025-03-13 13:28:48 +00:00
|
|
|
while !stack.is_config_up() {
|
|
|
|
|
Timer::after_millis(100).await;
|
|
|
|
|
}
|
2025-03-14 17:42:07 +00:00
|
|
|
log::info!("DHCP is now up!");*/
|
2025-03-13 13:28:48 +00:00
|
|
|
|
|
|
|
|
// And now we can use it!
|
2025-03-14 17:42:07 +00:00
|
|
|
let app = make_static!(AppRouter<AppProps>, AppProps.build_app());
|
|
|
|
|
|
|
|
|
|
let config = make_static!(
|
|
|
|
|
picoserve::Config<Duration>,
|
|
|
|
|
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()
|
|
|
|
|
);
|
2025-03-13 13:28:48 +00:00
|
|
|
|
2025-03-14 17:42:07 +00:00
|
|
|
for id in 0..WEB_TASK_POOL_SIZE {
|
|
|
|
|
spawner.must_spawn(web_task(id, stack, app, config));
|
|
|
|
|
}
|
|
|
|
|
/*
|
2025-03-13 13:28:48 +00:00
|
|
|
let mut rx_buffer = [0; 4096];
|
|
|
|
|
let mut tx_buffer = [0; 4096];
|
|
|
|
|
let mut buf = [0; 4096];
|
|
|
|
|
|
2025-03-14 17:42:07 +00:00
|
|
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
|
|
|
|
|
2025-03-13 13:28:48 +00:00
|
|
|
//let delay = Duration::from_secs(1);
|
|
|
|
|
log::info!("main: pre-loop");
|
|
|
|
|
loop {
|
|
|
|
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
|
|
|
|
socket.set_timeout(Some(Duration::from_secs(10)));
|
|
|
|
|
|
|
|
|
|
control.gpio_set(0, false).await;
|
2025-03-14 17:42:07 +00:00
|
|
|
log::info!("Listening on TCP:00...");
|
2025-03-13 13:28:48 +00:00
|
|
|
if let Err(e) = socket.accept(1234).await {
|
|
|
|
|
log::warn!("accept error: {:?}", e);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log::info!("Received connection from {:?}", socket.remote_endpoint());
|
|
|
|
|
|
|
|
|
|
control.gpio_set(0, true).await;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
let n = match socket.read(&mut buf).await {
|
|
|
|
|
Ok(0) => {
|
|
|
|
|
log::warn!("read EOF");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Ok(n) => n,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::warn!("read error: {:?}", e);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
log::info!("rxd {}", from_utf8(&buf[..n]).unwrap());
|
|
|
|
|
|
|
|
|
|
match socket.write_all(&buf[..n]).await {
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::warn!("write error: {:?}", e);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log::info!("LED off!");
|
|
|
|
|
control.gpio_set(0, false).await;
|
|
|
|
|
}
|
2025-03-14 17:42:07 +00:00
|
|
|
*/
|
2025-03-13 13:28:48 +00:00
|
|
|
}
|