From 0ea3478fb5e4fcdcd86e439186794d126ed2eca4 Mon Sep 17 00:00:00 2001 From: Riceman2000 Date: Fri, 12 Sep 2025 12:47:47 -0400 Subject: [PATCH 1/6] Fix typo in PIO SPI examples --- examples/rp/src/bin/pio_spi.rs | 6 +++--- examples/rp/src/bin/pio_spi_async.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/rp/src/bin/pio_spi.rs b/examples/rp/src/bin/pio_spi.rs index 4218327ec..c45aeac7d 100644 --- a/examples/rp/src/bin/pio_spi.rs +++ b/examples/rp/src/bin/pio_spi.rs @@ -27,9 +27,9 @@ async fn main(_spawner: Spawner) { // These pins are routed to different hardware SPI peripherals, but we can // use them together regardless - let mosi = p.PIN_6; // SPI0 SCLK - let miso = p.PIN_7; // SPI0 MOSI - let clk = p.PIN_8; // SPI1 MISO + let mosi = p.PIN_6; + let miso = p.PIN_7; + let clk = p.PIN_8; let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs); diff --git a/examples/rp/src/bin/pio_spi_async.rs b/examples/rp/src/bin/pio_spi_async.rs index 18b57d26e..e7d9b0ecc 100644 --- a/examples/rp/src/bin/pio_spi_async.rs +++ b/examples/rp/src/bin/pio_spi_async.rs @@ -27,9 +27,9 @@ async fn main(_spawner: Spawner) { // These pins are routed to different hardware SPI peripherals, but we can // use them together regardless - let mosi = p.PIN_6; // SPI0 SCLK - let miso = p.PIN_7; // SPI0 MOSI - let clk = p.PIN_8; // SPI1 MISO + let mosi = p.PIN_6; + let miso = p.PIN_7; + let clk = p.PIN_8; let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs); From f829ddd3b236b146701951004b41525de4633c9a Mon Sep 17 00:00:00 2001 From: Riceman2000 Date: Fri, 12 Sep 2025 12:47:55 -0400 Subject: [PATCH 2/6] Example first draft --- .../rp/src/bin/ethernet_w55rp20_tcp_server.rs | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs diff --git a/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs b/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs new file mode 100644 index 000000000..6f4ba4a70 --- /dev/null +++ b/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs @@ -0,0 +1,145 @@ +//! This example implements a TCP client that attempts to connect to a host on port 1234 and send it some data once per second. +//! +//! Example written for the [`WIZnet W55RP20-EVB-Pico`](https://docs.wiznet.io/Product/ioNIC/W55RP20/w55rp20-evb-pico) board. +//! Note: the W55RP20 is a single package that contains both a RP2040 and the Wiznet W5500 ethernet +//! controller + +#![no_std] +#![no_main] + +use core::str::FromStr; + +use defmt::*; +use embassy_executor::Spawner; +use embassy_futures::yield_now; +use embassy_net::{Stack, StackResources}; +use embassy_net_wiznet::chip::W5500; +use embassy_net_wiznet::*; +use embassy_rp::clocks::RoscRng; +use embassy_rp::gpio::{Input, Level, Output, Pull}; +use embassy_rp::peripherals::PIO0; +use embassy_rp::pio_programs::spi::Spi; +use embassy_rp::spi::{Async, Config as SpiConfig}; +use embassy_rp::{bind_interrupts, pio}; +use embassy_time::{Delay, Duration, Timer}; +use embedded_hal_bus::spi::ExclusiveDevice; +use embedded_io_async::Write; +use static_cell::StaticCell; +use {defmt_rtt as _, panic_probe as _}; + +bind_interrupts!(struct Irqs { + PIO0_IRQ_0 => pio::InterruptHandler; +}); + +#[embassy_executor::task] +async fn ethernet_task( + runner: Runner< + 'static, + W5500, + ExclusiveDevice, Output<'static>, Delay>, + Input<'static>, + Output<'static>, + >, +) -> ! { + runner.run().await +} + +#[embassy_executor::task] +async fn net_task(mut runner: embassy_net::Runner<'static, Device<'static>>) -> ! { + runner.run().await +} + +#[embassy_executor::main] +async fn main(spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + let mut rng = RoscRng; + let mut led = Output::new(p.PIN_19, Level::Low); + + // The W55RP20 uses a PIO unit for SPI communication, once the SPI bus has been formed using a + // PIO statemachine everything else is generally unchanged from the other examples that use the W5500 + let mosi = p.PIN_23; + let miso = p.PIN_22; + let clk = p.PIN_21; + + let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs); + + // Construct an SPI driver backed by a PIO state machine + let mut spi_cfg = SpiConfig::default(); + spi_cfg.frequency = 50_000_000; + let spi = Spi::new(&mut common, sm0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); + + // Further control pins + let cs = Output::new(p.PIN_20, Level::High); + let w5500_int = Input::new(p.PIN_24, Pull::Up); + let w5500_reset = Output::new(p.PIN_25, Level::High); + + let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; + static STATE: StaticCell> = StaticCell::new(); + let state = STATE.init(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(unwrap!(ethernet_task(runner))); + + // Generate random seed + let seed = rng.next_u64(); + + // Init network stack + static RESOURCES: StaticCell> = StaticCell::new(); + let (stack, runner) = embassy_net::new( + device, + embassy_net::Config::dhcpv4(Default::default()), + RESOURCES.init(StackResources::new()), + seed, + ); + + // Launch network task + spawner.spawn(unwrap!(net_task(runner))); + + info!("Waiting for DHCP..."); + let cfg = wait_for_config(stack).await; + let local_addr = cfg.address.address(); + info!("IP address: {:?}", local_addr); + + let mut rx_buffer = [0; 4096]; + let mut tx_buffer = [0; 4096]; + loop { + let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); + socket.set_timeout(Some(Duration::from_secs(10))); + + led.set_low(); + info!("Connecting..."); + let host_addr = embassy_net::Ipv4Address::from_str("192.168.1.110").unwrap(); + if let Err(e) = socket.connect((host_addr, 1234)).await { + warn!("connect error: {:?}", e); + continue; + } + info!("Connected to {:?}", socket.remote_endpoint()); + led.set_high(); + + let msg = b"Hello world!\n"; + loop { + if let Err(e) = socket.write_all(msg).await { + warn!("write error: {:?}", e); + break; + } + info!("txd: {}", core::str::from_utf8(msg).unwrap()); + Timer::after_secs(1).await; + } + } +} + +async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 { + loop { + if let Some(config) = stack.config_v4() { + return config.clone(); + } + yield_now().await; + } +} From 139ee907755bfa7817001c3ebc4a38eaf31cf243 Mon Sep 17 00:00:00 2001 From: riceman2000 Date: Fri, 12 Sep 2025 17:17:24 -0400 Subject: [PATCH 3/6] Updated example --- .../rp/src/bin/ethernet_w55rp20_tcp_server.rs | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs b/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs index 6f4ba4a70..17dc40aff 100644 --- a/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs +++ b/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs @@ -1,4 +1,5 @@ -//! This example implements a TCP client that attempts to connect to a host on port 1234 and send it some data once per second. +//! This example implements a TCP echo server on port 1234 and using DHCP. +//! Send it some data, you should see it echoed back and printed in the console. //! //! Example written for the [`WIZnet W55RP20-EVB-Pico`](https://docs.wiznet.io/Product/ioNIC/W55RP20/w55rp20-evb-pico) board. //! Note: the W55RP20 is a single package that contains both a RP2040 and the Wiznet W5500 ethernet @@ -65,7 +66,8 @@ async fn main(spawner: Spawner) { // Construct an SPI driver backed by a PIO state machine let mut spi_cfg = SpiConfig::default(); - spi_cfg.frequency = 50_000_000; + spi_cfg.frequency = 10_000_000; // The PIO SPI program is much less stable than the actual SPI + // peripheral, use higher speeds at your peril let spi = Spi::new(&mut common, sm0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); // Further control pins @@ -109,28 +111,38 @@ async fn main(spawner: Spawner) { let mut rx_buffer = [0; 4096]; let mut tx_buffer = [0; 4096]; + let mut buf = [0; 4096]; loop { let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); socket.set_timeout(Some(Duration::from_secs(10))); led.set_low(); - info!("Connecting..."); - let host_addr = embassy_net::Ipv4Address::from_str("192.168.1.110").unwrap(); - if let Err(e) = socket.connect((host_addr, 1234)).await { - warn!("connect error: {:?}", e); + info!("Listening on TCP:1234..."); + if let Err(e) = socket.accept(1234).await { + warn!("accept error: {:?}", e); continue; } - info!("Connected to {:?}", socket.remote_endpoint()); + info!("Received connection from {:?}", socket.remote_endpoint()); led.set_high(); - let msg = b"Hello world!\n"; loop { - if let Err(e) = socket.write_all(msg).await { + let n = match socket.read(&mut buf).await { + Ok(0) => { + warn!("read EOF"); + break; + } + Ok(n) => n, + Err(e) => { + warn!("{:?}", e); + break; + } + }; + info!("rxd {}", core::str::from_utf8(&buf[..n]).unwrap()); + + if let Err(e) = socket.write_all(&buf[..n]).await { warn!("write error: {:?}", e); break; } - info!("txd: {}", core::str::from_utf8(msg).unwrap()); - Timer::after_secs(1).await; } } } From 6beb7e35a6bf2ae0e72a389b2dac6bde08e5dcd2 Mon Sep 17 00:00:00 2001 From: riceman2000 Date: Fri, 12 Sep 2025 22:52:53 -0400 Subject: [PATCH 4/6] Remove unused imports --- examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs b/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs index 17dc40aff..0d69b66c4 100644 --- a/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs +++ b/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs @@ -8,8 +8,6 @@ #![no_std] #![no_main] -use core::str::FromStr; - use defmt::*; use embassy_executor::Spawner; use embassy_futures::yield_now; @@ -22,7 +20,7 @@ use embassy_rp::peripherals::PIO0; use embassy_rp::pio_programs::spi::Spi; use embassy_rp::spi::{Async, Config as SpiConfig}; use embassy_rp::{bind_interrupts, pio}; -use embassy_time::{Delay, Duration, Timer}; +use embassy_time::{Delay, Duration}; use embedded_hal_bus::spi::ExclusiveDevice; use embedded_io_async::Write; use static_cell::StaticCell; From daae1fe5c9357ae97b897defae3d149eeafcc49f Mon Sep 17 00:00:00 2001 From: riceman2000 Date: Sun, 14 Sep 2025 11:30:22 -0400 Subject: [PATCH 5/6] Up SPI freq --- examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs b/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs index 0d69b66c4..f51df2df9 100644 --- a/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs +++ b/examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs @@ -64,7 +64,7 @@ async fn main(spawner: Spawner) { // Construct an SPI driver backed by a PIO state machine let mut spi_cfg = SpiConfig::default(); - spi_cfg.frequency = 10_000_000; // The PIO SPI program is much less stable than the actual SPI + spi_cfg.frequency = 12_500_000; // The PIO SPI program is much less stable than the actual SPI // peripheral, use higher speeds at your peril let spi = Spi::new(&mut common, sm0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); From 1c080559fd20eb250e76278ff92d23432b5e0ce8 Mon Sep 17 00:00:00 2001 From: riceman2000 Date: Sun, 14 Sep 2025 14:14:59 -0400 Subject: [PATCH 6/6] Fix removed comments --- examples/rp/src/bin/pio_spi.rs | 6 +++--- examples/rp/src/bin/pio_spi_async.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/rp/src/bin/pio_spi.rs b/examples/rp/src/bin/pio_spi.rs index c45aeac7d..4218327ec 100644 --- a/examples/rp/src/bin/pio_spi.rs +++ b/examples/rp/src/bin/pio_spi.rs @@ -27,9 +27,9 @@ async fn main(_spawner: Spawner) { // These pins are routed to different hardware SPI peripherals, but we can // use them together regardless - let mosi = p.PIN_6; - let miso = p.PIN_7; - let clk = p.PIN_8; + let mosi = p.PIN_6; // SPI0 SCLK + let miso = p.PIN_7; // SPI0 MOSI + let clk = p.PIN_8; // SPI1 MISO let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs); diff --git a/examples/rp/src/bin/pio_spi_async.rs b/examples/rp/src/bin/pio_spi_async.rs index e7d9b0ecc..18b57d26e 100644 --- a/examples/rp/src/bin/pio_spi_async.rs +++ b/examples/rp/src/bin/pio_spi_async.rs @@ -27,9 +27,9 @@ async fn main(_spawner: Spawner) { // These pins are routed to different hardware SPI peripherals, but we can // use them together regardless - let mosi = p.PIN_6; - let miso = p.PIN_7; - let clk = p.PIN_8; + let mosi = p.PIN_6; // SPI0 SCLK + let miso = p.PIN_7; // SPI0 MOSI + let clk = p.PIN_8; // SPI1 MISO let pio::Pio { mut common, sm0, .. } = pio::Pio::new(p.PIO0, Irqs);