diff options
| author | riceman2000 <[email protected]> | 2025-09-12 17:17:24 -0400 |
|---|---|---|
| committer | riceman2000 <[email protected]> | 2025-09-12 17:17:24 -0400 |
| commit | 139ee907755bfa7817001c3ebc4a38eaf31cf243 (patch) | |
| tree | 0d73645acddf576383cc3bc2f614f4dfbb401768 /examples/rp | |
| parent | f829ddd3b236b146701951004b41525de4633c9a (diff) | |
Updated example
Diffstat (limited to 'examples/rp')
| -rw-r--r-- | examples/rp/src/bin/ethernet_w55rp20_tcp_server.rs | 34 |
1 files 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 @@ | |||
| 1 | //! This example implements a TCP client that attempts to connect to a host on port 1234 and send it some data once per second. | 1 | //! This example implements a TCP echo server on port 1234 and using DHCP. |
| 2 | //! Send it some data, you should see it echoed back and printed in the console. | ||
| 2 | //! | 3 | //! |
| 3 | //! Example written for the [`WIZnet W55RP20-EVB-Pico`](https://docs.wiznet.io/Product/ioNIC/W55RP20/w55rp20-evb-pico) board. | 4 | //! Example written for the [`WIZnet W55RP20-EVB-Pico`](https://docs.wiznet.io/Product/ioNIC/W55RP20/w55rp20-evb-pico) board. |
| 4 | //! Note: the W55RP20 is a single package that contains both a RP2040 and the Wiznet W5500 ethernet | 5 | //! 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) { | |||
| 65 | 66 | ||
| 66 | // Construct an SPI driver backed by a PIO state machine | 67 | // Construct an SPI driver backed by a PIO state machine |
| 67 | let mut spi_cfg = SpiConfig::default(); | 68 | let mut spi_cfg = SpiConfig::default(); |
| 68 | spi_cfg.frequency = 50_000_000; | 69 | spi_cfg.frequency = 10_000_000; // The PIO SPI program is much less stable than the actual SPI |
| 70 | // peripheral, use higher speeds at your peril | ||
| 69 | let spi = Spi::new(&mut common, sm0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); | 71 | let spi = Spi::new(&mut common, sm0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); |
| 70 | 72 | ||
| 71 | // Further control pins | 73 | // Further control pins |
| @@ -109,28 +111,38 @@ async fn main(spawner: Spawner) { | |||
| 109 | 111 | ||
| 110 | let mut rx_buffer = [0; 4096]; | 112 | let mut rx_buffer = [0; 4096]; |
| 111 | let mut tx_buffer = [0; 4096]; | 113 | let mut tx_buffer = [0; 4096]; |
| 114 | let mut buf = [0; 4096]; | ||
| 112 | loop { | 115 | loop { |
| 113 | let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | 116 | let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); |
| 114 | socket.set_timeout(Some(Duration::from_secs(10))); | 117 | socket.set_timeout(Some(Duration::from_secs(10))); |
| 115 | 118 | ||
| 116 | led.set_low(); | 119 | led.set_low(); |
| 117 | info!("Connecting..."); | 120 | info!("Listening on TCP:1234..."); |
| 118 | let host_addr = embassy_net::Ipv4Address::from_str("192.168.1.110").unwrap(); | 121 | if let Err(e) = socket.accept(1234).await { |
| 119 | if let Err(e) = socket.connect((host_addr, 1234)).await { | 122 | warn!("accept error: {:?}", e); |
| 120 | warn!("connect error: {:?}", e); | ||
| 121 | continue; | 123 | continue; |
| 122 | } | 124 | } |
| 123 | info!("Connected to {:?}", socket.remote_endpoint()); | 125 | info!("Received connection from {:?}", socket.remote_endpoint()); |
| 124 | led.set_high(); | 126 | led.set_high(); |
| 125 | 127 | ||
| 126 | let msg = b"Hello world!\n"; | ||
| 127 | loop { | 128 | loop { |
| 128 | if let Err(e) = socket.write_all(msg).await { | 129 | let n = match socket.read(&mut buf).await { |
| 130 | Ok(0) => { | ||
| 131 | warn!("read EOF"); | ||
| 132 | break; | ||
| 133 | } | ||
| 134 | Ok(n) => n, | ||
| 135 | Err(e) => { | ||
| 136 | warn!("{:?}", e); | ||
| 137 | break; | ||
| 138 | } | ||
| 139 | }; | ||
| 140 | info!("rxd {}", core::str::from_utf8(&buf[..n]).unwrap()); | ||
| 141 | |||
| 142 | if let Err(e) = socket.write_all(&buf[..n]).await { | ||
| 129 | warn!("write error: {:?}", e); | 143 | warn!("write error: {:?}", e); |
| 130 | break; | 144 | break; |
| 131 | } | 145 | } |
| 132 | info!("txd: {}", core::str::from_utf8(msg).unwrap()); | ||
| 133 | Timer::after_secs(1).await; | ||
| 134 | } | 146 | } |
| 135 | } | 147 | } |
| 136 | } | 148 | } |
