#![no_std] #![no_main] #![feature(type_alias_impl_trait)] teleprobe_meta::target!(b"w5100s-evb-pico"); teleprobe_meta::timeout!(120); use defmt::{assert, *}; use embassy_executor::Spawner; use embassy_futures::join::join; use embassy_net::tcp::TcpSocket; use embassy_net::{Ipv4Address, Stack, StackResources}; use embassy_net_wiznet::chip::W5100S; use embassy_net_wiznet::*; use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Input, Level, Output, Pull}; use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0}; use embassy_rp::spi::{Async, Config as SpiConfig, Spi}; use embassy_time::{with_timeout, Delay, Duration, Timer}; use embedded_hal_bus::spi::ExclusiveDevice; use rand::RngCore; use static_cell::make_static; use {defmt_rtt as _, panic_probe as _}; #[embassy_executor::task] async fn ethernet_task( runner: Runner< 'static, W5100S, ExclusiveDevice, Output<'static, PIN_17>, Delay>, Input<'static, PIN_21>, Output<'static, PIN_20>, >, ) -> ! { runner.run().await } #[embassy_executor::task] async fn net_task(stack: &'static Stack>) -> ! { stack.run().await } #[embassy_executor::main] async fn main(spawner: Spawner) { let p = embassy_rp::init(Default::default()); let mut rng = RoscRng; let mut spi_cfg = SpiConfig::default(); spi_cfg.frequency = 50_000_000; let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18); let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); let cs = Output::new(p.PIN_17, Level::High); let w5500_int = Input::new(p.PIN_21, Pull::Up); let w5500_reset = Output::new(p.PIN_20, Level::High); let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; let state = make_static!(State::<8, 8>::new()); let (device, runner) = embassy_net_wiznet::new( mac_addr, state, ExclusiveDevice::new(spi, cs, Delay), w5500_int, w5500_reset, ) .await; unwrap!(spawner.spawn(ethernet_task(runner))); // Generate random seed let seed = rng.next_u64(); // Init network stack let stack = &*make_static!(Stack::new( device, embassy_net::Config::dhcpv4(Default::default()), make_static!(StackResources::<2>::new()), seed )); // Launch network task unwrap!(spawner.spawn(net_task(&stack))); info!("Waiting for DHCP up..."); while stack.config_v4().is_none() { Timer::after(Duration::from_millis(100)).await; } info!("IP addressing up!"); let down = test_download(stack).await; let up = test_upload(stack).await; let updown = test_upload_download(stack).await; assert!(down > TEST_EXPECTED_DOWNLOAD_KBPS); assert!(up > TEST_EXPECTED_UPLOAD_KBPS); assert!(updown > TEST_EXPECTED_UPLOAD_DOWNLOAD_KBPS); info!("Test OK"); cortex_m::asm::bkpt(); } const TEST_DURATION: usize = 10; const TEST_EXPECTED_DOWNLOAD_KBPS: usize = 500; const TEST_EXPECTED_UPLOAD_KBPS: usize = 500; const TEST_EXPECTED_UPLOAD_DOWNLOAD_KBPS: usize = 300; const RX_BUFFER_SIZE: usize = 4096; const TX_BUFFER_SIZE: usize = 4096; const SERVER_ADDRESS: Ipv4Address = Ipv4Address::new(192, 168, 2, 2); const DOWNLOAD_PORT: u16 = 4321; const UPLOAD_PORT: u16 = 4322; const UPLOAD_DOWNLOAD_PORT: u16 = 4323; async fn test_download(stack: &'static Stack>) -> usize { info!("Testing download..."); let mut rx_buffer = [0; RX_BUFFER_SIZE]; let mut tx_buffer = [0; TX_BUFFER_SIZE]; let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); socket.set_timeout(Some(Duration::from_secs(10))); info!("connecting to {:?}:{}...", SERVER_ADDRESS, DOWNLOAD_PORT); if let Err(e) = socket.connect((SERVER_ADDRESS, DOWNLOAD_PORT)).await { error!("connect error: {:?}", e); return 0; } info!("connected, testing..."); let mut rx_buf = [0; 4096]; let mut total: usize = 0; with_timeout(Duration::from_secs(TEST_DURATION as _), async { loop { match socket.read(&mut rx_buf).await { Ok(0) => { error!("read EOF"); return 0; } Ok(n) => total += n, Err(e) => { error!("read error: {:?}", e); return 0; } } } }) .await .ok(); let kbps = (total + 512) / 1024 / TEST_DURATION; info!("download: {} kB/s", kbps); kbps } async fn test_upload(stack: &'static Stack>) -> usize { info!("Testing upload..."); let mut rx_buffer = [0; RX_BUFFER_SIZE]; let mut tx_buffer = [0; TX_BUFFER_SIZE]; let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); socket.set_timeout(Some(Duration::from_secs(10))); info!("connecting to {:?}:{}...", SERVER_ADDRESS, UPLOAD_PORT); if let Err(e) = socket.connect((SERVER_ADDRESS, UPLOAD_PORT)).await { error!("connect error: {:?}", e); return 0; } info!("connected, testing..."); let buf = [0; 4096]; let mut total: usize = 0; with_timeout(Duration::from_secs(TEST_DURATION as _), async { loop { match socket.write(&buf).await { Ok(0) => { error!("write zero?!??!?!"); return 0; } Ok(n) => total += n, Err(e) => { error!("write error: {:?}", e); return 0; } } } }) .await .ok(); let kbps = (total + 512) / 1024 / TEST_DURATION; info!("upload: {} kB/s", kbps); kbps } async fn test_upload_download(stack: &'static Stack>) -> usize { info!("Testing upload+download..."); let mut rx_buffer = [0; RX_BUFFER_SIZE]; let mut tx_buffer = [0; TX_BUFFER_SIZE]; let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); socket.set_timeout(Some(Duration::from_secs(10))); info!("connecting to {:?}:{}...", SERVER_ADDRESS, UPLOAD_DOWNLOAD_PORT); if let Err(e) = socket.connect((SERVER_ADDRESS, UPLOAD_DOWNLOAD_PORT)).await { error!("connect error: {:?}", e); return 0; } info!("connected, testing..."); let (mut reader, mut writer) = socket.split(); let tx_buf = [0; 4096]; let mut rx_buf = [0; 4096]; let mut total: usize = 0; let tx_fut = async { loop { match writer.write(&tx_buf).await { Ok(0) => { error!("write zero?!??!?!"); return 0; } Ok(_) => {} Err(e) => { error!("write error: {:?}", e); return 0; } } } }; let rx_fut = async { loop { match reader.read(&mut rx_buf).await { Ok(0) => { error!("read EOF"); return 0; } Ok(n) => total += n, Err(e) => { error!("read error: {:?}", e); return 0; } } } }; with_timeout(Duration::from_secs(TEST_DURATION as _), join(tx_fut, rx_fut)) .await .ok(); let kbps = (total + 512) / 1024 / TEST_DURATION; info!("upload+download: {} kB/s", kbps); kbps }