diff options
| author | skkeye <[email protected]> | 2025-02-09 01:07:25 -0500 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2025-02-13 10:27:30 +0100 |
| commit | 38b5f8bd0afd9669e638b8d83210427d2d372a96 (patch) | |
| tree | 442db6d61d6d1e57a01923d517e316c544a30c41 /examples | |
| parent | 74c1fd64d218c3ac5d1ab7a2c1155a87e4169150 (diff) | |
examples/rp: add examples using ICMP sockets and the ping utility
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/rp/src/bin/ethernet_w5500_icmp.rs | 143 | ||||
| -rw-r--r-- | examples/rp/src/bin/ethernet_w5500_icmp_ping.rs | 135 |
3 files changed, 279 insertions, 1 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index 50cfdca1e..c7823a711 100644 --- a/examples/rp/Cargo.toml +++ b/examples/rp/Cargo.toml | |||
| @@ -12,7 +12,7 @@ embassy-executor = { version = "0.7.0", path = "../../embassy-executor", feature | |||
| 12 | embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 12 | embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } |
| 13 | embassy-rp = { version = "0.3.0", path = "../../embassy-rp", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp2040"] } | 13 | embassy-rp = { version = "0.3.0", path = "../../embassy-rp", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp2040"] } |
| 14 | embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] } | 14 | embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] } |
| 15 | embassy-net = { version = "0.6.0", path = "../../embassy-net", features = ["defmt", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns", "proto-ipv4", "proto-ipv6", "multicast"] } | 15 | embassy-net = { version = "0.6.0", path = "../../embassy-net", features = ["defmt", "icmp", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns", "proto-ipv4", "proto-ipv6", "multicast"] } |
| 16 | embassy-net-wiznet = { version = "0.2.0", path = "../../embassy-net-wiznet", features = ["defmt"] } | 16 | embassy-net-wiznet = { version = "0.2.0", path = "../../embassy-net-wiznet", features = ["defmt"] } |
| 17 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 17 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 18 | embassy-usb-logger = { version = "0.4.0", path = "../../embassy-usb-logger" } | 18 | embassy-usb-logger = { version = "0.4.0", path = "../../embassy-usb-logger" } |
diff --git a/examples/rp/src/bin/ethernet_w5500_icmp.rs b/examples/rp/src/bin/ethernet_w5500_icmp.rs new file mode 100644 index 000000000..a07cdf88d --- /dev/null +++ b/examples/rp/src/bin/ethernet_w5500_icmp.rs | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | //! This example implements an echo (ping) with an ICMP Socket and using defmt to report the results. | ||
| 2 | //! | ||
| 3 | //! Although there is a better way to execute pings using the child module ping of the icmp module, | ||
| 4 | //! this example allows for other icmp messages like `Destination unreachable` to be sent aswell. | ||
| 5 | //! | ||
| 6 | //! Example written for the [`WIZnet W5500-EVB-Pico`](https://docs.wiznet.io/Product/iEthernet/W5500/w5500-evb-pico) board. | ||
| 7 | |||
| 8 | #![no_std] | ||
| 9 | #![no_main] | ||
| 10 | |||
| 11 | use defmt::*; | ||
| 12 | use embassy_executor::Spawner; | ||
| 13 | use embassy_futures::yield_now; | ||
| 14 | use embassy_net::icmp::{ChecksumCapabilities, IcmpEndpoint, IcmpSocket, Icmpv4Packet, Icmpv4Repr, PacketMetadata}; | ||
| 15 | use embassy_net::{Stack, StackResources}; | ||
| 16 | use embassy_net_wiznet::chip::W5500; | ||
| 17 | use embassy_net_wiznet::*; | ||
| 18 | use embassy_rp::clocks::RoscRng; | ||
| 19 | use embassy_rp::gpio::{Input, Level, Output, Pull}; | ||
| 20 | use embassy_rp::peripherals::SPI0; | ||
| 21 | use embassy_rp::spi::{Async, Config as SpiConfig, Spi}; | ||
| 22 | use embassy_time::{Delay, Instant, Timer}; | ||
| 23 | use embedded_hal_bus::spi::ExclusiveDevice; | ||
| 24 | use rand::RngCore; | ||
| 25 | use static_cell::StaticCell; | ||
| 26 | use {defmt_rtt as _, panic_probe as _}; | ||
| 27 | |||
| 28 | type ExclusiveSpiDevice = ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static>, Delay>; | ||
| 29 | |||
| 30 | #[embassy_executor::task] | ||
| 31 | async fn ethernet_task(runner: Runner<'static, W5500, ExclusiveSpiDevice, Input<'static>, Output<'static>>) -> ! { | ||
| 32 | runner.run().await | ||
| 33 | } | ||
| 34 | |||
| 35 | #[embassy_executor::task] | ||
| 36 | async fn net_task(mut runner: embassy_net::Runner<'static, Device<'static>>) -> ! { | ||
| 37 | runner.run().await | ||
| 38 | } | ||
| 39 | |||
| 40 | #[embassy_executor::main] | ||
| 41 | async fn main(spawner: Spawner) { | ||
| 42 | let p = embassy_rp::init(Default::default()); | ||
| 43 | let mut rng = RoscRng; | ||
| 44 | |||
| 45 | let mut spi_cfg = SpiConfig::default(); | ||
| 46 | spi_cfg.frequency = 50_000_000; | ||
| 47 | let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18); | ||
| 48 | let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); | ||
| 49 | let cs = Output::new(p.PIN_17, Level::High); | ||
| 50 | let w5500_int = Input::new(p.PIN_21, Pull::Up); | ||
| 51 | let w5500_reset = Output::new(p.PIN_20, Level::High); | ||
| 52 | |||
| 53 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; | ||
| 54 | static STATE: StaticCell<State<8, 8>> = StaticCell::new(); | ||
| 55 | let state = STATE.init(State::<8, 8>::new()); | ||
| 56 | let (device, runner) = embassy_net_wiznet::new( | ||
| 57 | mac_addr, | ||
| 58 | state, | ||
| 59 | ExclusiveDevice::new(spi, cs, Delay), | ||
| 60 | w5500_int, | ||
| 61 | w5500_reset, | ||
| 62 | ) | ||
| 63 | .await | ||
| 64 | .unwrap(); | ||
| 65 | unwrap!(spawner.spawn(ethernet_task(runner))); | ||
| 66 | |||
| 67 | // Generate random seed | ||
| 68 | let seed = rng.next_u64(); | ||
| 69 | |||
| 70 | // Init network stack | ||
| 71 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); | ||
| 72 | let (stack, runner) = embassy_net::new( | ||
| 73 | device, | ||
| 74 | embassy_net::Config::dhcpv4(Default::default()), | ||
| 75 | RESOURCES.init(StackResources::new()), | ||
| 76 | seed, | ||
| 77 | ); | ||
| 78 | |||
| 79 | // Launch network task | ||
| 80 | unwrap!(spawner.spawn(net_task(runner))); | ||
| 81 | |||
| 82 | info!("Waiting for DHCP..."); | ||
| 83 | let cfg = wait_for_config(stack).await; | ||
| 84 | let local_addr = cfg.address.address(); | ||
| 85 | info!("IP address: {:?}", local_addr); | ||
| 86 | |||
| 87 | // Then we can use it! | ||
| 88 | let mut rx_buffer = [0; 256]; | ||
| 89 | let mut tx_buffer = [0; 256]; | ||
| 90 | let mut rx_meta = [PacketMetadata::EMPTY]; | ||
| 91 | let mut tx_meta = [PacketMetadata::EMPTY]; | ||
| 92 | |||
| 93 | // Identifier used for the ICMP socket | ||
| 94 | let ident = 42; | ||
| 95 | |||
| 96 | // Create and bind the socket | ||
| 97 | let mut socket = IcmpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer); | ||
| 98 | socket.bind(IcmpEndpoint::Ident(ident)).unwrap(); | ||
| 99 | |||
| 100 | // Create the repr for the packet | ||
| 101 | let icmp_repr = Icmpv4Repr::EchoRequest { | ||
| 102 | ident, | ||
| 103 | seq_no: 0, | ||
| 104 | data: b"Hello, icmp!", | ||
| 105 | }; | ||
| 106 | |||
| 107 | // Send the packet and store the starting instant to mesure latency later | ||
| 108 | let start = socket | ||
| 109 | .send_to_with( | ||
| 110 | icmp_repr.buffer_len(), | ||
| 111 | cfg.gateway.unwrap(), | ||
| 112 | |buf| { | ||
| 113 | // Create and populate the packet buffer allocated by `send_to_with` | ||
| 114 | let mut icmp_packet = Icmpv4Packet::new_unchecked(buf); | ||
| 115 | icmp_repr.emit(&mut icmp_packet, &ChecksumCapabilities::default()); | ||
| 116 | Instant::now() // Return the instant where the packet was sent | ||
| 117 | }, | ||
| 118 | ) | ||
| 119 | .await | ||
| 120 | .unwrap(); | ||
| 121 | |||
| 122 | // Recieve and log the data of the reply | ||
| 123 | socket | ||
| 124 | .recv_with(|(buf, addr)| { | ||
| 125 | let packet = Icmpv4Packet::new_checked(buf).unwrap(); | ||
| 126 | info!("Recieved {:?} from {} in {}ms", packet.data(), addr, start.elapsed().as_millis()); | ||
| 127 | }) | ||
| 128 | .await | ||
| 129 | .unwrap(); | ||
| 130 | |||
| 131 | loop { | ||
| 132 | Timer::after_secs(10).await; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 { | ||
| 137 | loop { | ||
| 138 | if let Some(config) = stack.config_v4() { | ||
| 139 | return config.clone(); | ||
| 140 | } | ||
| 141 | yield_now().await; | ||
| 142 | } | ||
| 143 | } | ||
diff --git a/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs b/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs new file mode 100644 index 000000000..0d83e3831 --- /dev/null +++ b/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | //! This example implements a LAN ping scan with the ping utilities in the icmp module of embassy-net. | ||
| 2 | //! | ||
| 3 | //! Example written for the [`WIZnet W5500-EVB-Pico`](https://docs.wiznet.io/Product/iEthernet/W5500/w5500-evb-pico) board. | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use core::net::Ipv4Addr; | ||
| 9 | use core::ops::Not; | ||
| 10 | use core::str::FromStr; | ||
| 11 | |||
| 12 | use defmt::*; | ||
| 13 | use embassy_executor::Spawner; | ||
| 14 | use embassy_futures::yield_now; | ||
| 15 | use embassy_net::icmp::ping::{PingManager, PingParams}; | ||
| 16 | use embassy_net::icmp::PacketMetadata; | ||
| 17 | use embassy_net::{Ipv4Cidr, Stack, StackResources}; | ||
| 18 | use embassy_net_wiznet::chip::W5500; | ||
| 19 | use embassy_net_wiznet::*; | ||
| 20 | use embassy_rp::clocks::RoscRng; | ||
| 21 | use embassy_rp::gpio::{Input, Level, Output, Pull}; | ||
| 22 | use embassy_rp::peripherals::SPI0; | ||
| 23 | use embassy_rp::spi::{Async, Config as SpiConfig, Spi}; | ||
| 24 | use embassy_time::{Delay, Duration}; | ||
| 25 | use embedded_hal_bus::spi::ExclusiveDevice; | ||
| 26 | use rand::RngCore; | ||
| 27 | use static_cell::StaticCell; | ||
| 28 | use {defmt_rtt as _, panic_probe as _}; | ||
| 29 | |||
| 30 | type ExclusiveSpiDevice = ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static>, Delay>; | ||
| 31 | |||
| 32 | #[embassy_executor::task] | ||
| 33 | async fn ethernet_task(runner: Runner<'static, W5500, ExclusiveSpiDevice, Input<'static>, Output<'static>>) -> ! { | ||
| 34 | runner.run().await | ||
| 35 | } | ||
| 36 | |||
| 37 | #[embassy_executor::task] | ||
| 38 | async fn net_task(mut runner: embassy_net::Runner<'static, Device<'static>>) -> ! { | ||
| 39 | runner.run().await | ||
| 40 | } | ||
| 41 | |||
| 42 | #[embassy_executor::main] | ||
| 43 | async fn main(spawner: Spawner) { | ||
| 44 | let p = embassy_rp::init(Default::default()); | ||
| 45 | let mut rng = RoscRng; | ||
| 46 | |||
| 47 | let mut spi_cfg = SpiConfig::default(); | ||
| 48 | spi_cfg.frequency = 50_000_000; | ||
| 49 | let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18); | ||
| 50 | let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg); | ||
| 51 | let cs = Output::new(p.PIN_17, Level::High); | ||
| 52 | let w5500_int = Input::new(p.PIN_21, Pull::Up); | ||
| 53 | let w5500_reset = Output::new(p.PIN_20, Level::High); | ||
| 54 | |||
| 55 | let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00]; | ||
| 56 | static STATE: StaticCell<State<8, 8>> = StaticCell::new(); | ||
| 57 | let state = STATE.init(State::<8, 8>::new()); | ||
| 58 | let (device, runner) = embassy_net_wiznet::new( | ||
| 59 | mac_addr, | ||
| 60 | state, | ||
| 61 | ExclusiveDevice::new(spi, cs, Delay), | ||
| 62 | w5500_int, | ||
| 63 | w5500_reset, | ||
| 64 | ) | ||
| 65 | .await | ||
| 66 | .unwrap(); | ||
| 67 | unwrap!(spawner.spawn(ethernet_task(runner))); | ||
| 68 | |||
| 69 | // Generate random seed | ||
| 70 | let seed = rng.next_u64(); | ||
| 71 | |||
| 72 | // Init network stack | ||
| 73 | static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new(); | ||
| 74 | let (stack, runner) = embassy_net::new( | ||
| 75 | device, | ||
| 76 | embassy_net::Config::dhcpv4(Default::default()), | ||
| 77 | RESOURCES.init(StackResources::new()), | ||
| 78 | seed, | ||
| 79 | ); | ||
| 80 | |||
| 81 | // Launch network task | ||
| 82 | unwrap!(spawner.spawn(net_task(runner))); | ||
| 83 | |||
| 84 | info!("Waiting for DHCP..."); | ||
| 85 | let cfg = wait_for_config(stack).await; | ||
| 86 | let local_addr = cfg.address.address(); | ||
| 87 | info!("IP address: {:?}", local_addr); | ||
| 88 | let gateway = cfg.gateway.unwrap(); | ||
| 89 | let mask = cfg.address.netmask(); | ||
| 90 | let lower_bound = (gateway.to_bits() & mask.to_bits()) + 1; | ||
| 91 | let upper_bound = gateway.to_bits() | mask.to_bits().not(); | ||
| 92 | let addr_range = lower_bound..=upper_bound; | ||
| 93 | |||
| 94 | // Then we can use it! | ||
| 95 | let mut rx_buffer = [0; 256]; | ||
| 96 | let mut tx_buffer = [0; 256]; | ||
| 97 | let mut rx_meta = [PacketMetadata::EMPTY]; | ||
| 98 | let mut tx_meta = [PacketMetadata::EMPTY]; | ||
| 99 | |||
| 100 | // Create the ping manager instance | ||
| 101 | let mut ping_manager = PingManager::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer); | ||
| 102 | let addr = "192.168.8.1"; // Address to ping to | ||
| 103 | // Create the PingParams with the target address | ||
| 104 | let mut ping_params = PingParams::new(Ipv4Addr::from_str(addr).unwrap()); | ||
| 105 | // (optional) Set custom properties of the ping | ||
| 106 | ping_params.set_payload(b"Hello, Ping!"); // custom payload | ||
| 107 | ping_params.set_count(1); // ping 1 times per ping call | ||
| 108 | ping_params.set_timeout(Duration::from_millis(500)); // wait .5 seconds instead of 4 | ||
| 109 | |||
| 110 | info!("Online hosts in {}:", Ipv4Cidr::from_netmask(gateway, mask).unwrap()); | ||
| 111 | let mut total_online_hosts = 0u32; | ||
| 112 | for addr in addr_range { | ||
| 113 | let ip_addr = Ipv4Addr::from_bits(addr); | ||
| 114 | // Set the target address in the ping params | ||
| 115 | ping_params.set_target(ip_addr); | ||
| 116 | // Execute the ping with the given parameters and wait for the reply | ||
| 117 | match ping_manager.ping(&ping_params).await { | ||
| 118 | Ok(time) => { | ||
| 119 | info!("{} is online\n- latency: {}ms\n", ip_addr, time.as_millis()); | ||
| 120 | total_online_hosts += 1; | ||
| 121 | }, | ||
| 122 | _ => continue, | ||
| 123 | } | ||
| 124 | } | ||
| 125 | info!("Ping scan complete, total online hosts: {}", total_online_hosts); | ||
| 126 | } | ||
| 127 | |||
| 128 | async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 { | ||
| 129 | loop { | ||
| 130 | if let Some(config) = stack.config_v4() { | ||
| 131 | return config.clone(); | ||
| 132 | } | ||
| 133 | yield_now().await; | ||
| 134 | } | ||
| 135 | } | ||
