Checking in initial baseline for project based on a combo

of Rust templates and content from the embassy project
and including my initial merging of the wifi_tcp_server,
usb_serial, and blinky sample code.
This commit is contained in:
Yvan 2025-03-13 13:28:48 +00:00
parent 47320978b3
commit dd75b23619
15 changed files with 2159 additions and 2 deletions

186
src/main.rs Normal file
View file

@ -0,0 +1,186 @@
//! This example test the RP Pico W on board LED.
//!
//! It does not work with the RP Pico board. See blinky.rs.
#![no_std]
#![no_main]
use core::str::from_utf8;
use cyw43::JoinOptions;
use cyw43_pio::{PioSpi, DEFAULT_CLOCK_DIVIDER};
use embassy_executor::Spawner;
use embassy_net::{Config, StackResources};
use embassy_net::tcp::TcpSocket;
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};
use embedded_io_async::Write;
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
}
#[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;
let config = Config::dhcpv4(Default::default());
// 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
log::info!("waiting for DHCP...");
while !stack.is_config_up() {
Timer::after_millis(100).await;
}
log::info!("DHCP is now up!");
// And now we can use it!
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut buf = [0; 4096];
//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;
log::info!("Listening on TCP:1234...");
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;
}
}