mirror of
https://github.com/embassy-rs/embassy.git
synced 2025-09-27 04:10:25 +00:00
examples/rp: add examples using ICMP sockets and the ping utility
This commit is contained in:
parent
74c1fd64d2
commit
38b5f8bd0a
@ -12,7 +12,7 @@ embassy-executor = { version = "0.7.0", path = "../../embassy-executor", feature
|
||||
embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
|
||||
embassy-rp = { version = "0.3.0", path = "../../embassy-rp", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp2040"] }
|
||||
embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] }
|
||||
embassy-net = { version = "0.6.0", path = "../../embassy-net", features = ["defmt", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns", "proto-ipv4", "proto-ipv6", "multicast"] }
|
||||
embassy-net = { version = "0.6.0", path = "../../embassy-net", features = ["defmt", "icmp", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns", "proto-ipv4", "proto-ipv6", "multicast"] }
|
||||
embassy-net-wiznet = { version = "0.2.0", path = "../../embassy-net-wiznet", features = ["defmt"] }
|
||||
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
|
||||
embassy-usb-logger = { version = "0.4.0", path = "../../embassy-usb-logger" }
|
||||
|
143
examples/rp/src/bin/ethernet_w5500_icmp.rs
Normal file
143
examples/rp/src/bin/ethernet_w5500_icmp.rs
Normal file
@ -0,0 +1,143 @@
|
||||
//! This example implements an echo (ping) with an ICMP Socket and using defmt to report the results.
|
||||
//!
|
||||
//! Although there is a better way to execute pings using the child module ping of the icmp module,
|
||||
//! this example allows for other icmp messages like `Destination unreachable` to be sent aswell.
|
||||
//!
|
||||
//! Example written for the [`WIZnet W5500-EVB-Pico`](https://docs.wiznet.io/Product/iEthernet/W5500/w5500-evb-pico) board.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::yield_now;
|
||||
use embassy_net::icmp::{ChecksumCapabilities, IcmpEndpoint, IcmpSocket, Icmpv4Packet, Icmpv4Repr, PacketMetadata};
|
||||
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::SPI0;
|
||||
use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
|
||||
use embassy_time::{Delay, Instant, Timer};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use rand::RngCore;
|
||||
use static_cell::StaticCell;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
type ExclusiveSpiDevice = ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static>, Delay>;
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn ethernet_task(runner: Runner<'static, W5500, ExclusiveSpiDevice, 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 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];
|
||||
static STATE: StaticCell<State<8, 8>> = 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();
|
||||
unwrap!(spawner.spawn(ethernet_task(runner)));
|
||||
|
||||
// Generate random seed
|
||||
let seed = rng.next_u64();
|
||||
|
||||
// Init network stack
|
||||
static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
|
||||
let (stack, runner) = embassy_net::new(
|
||||
device,
|
||||
embassy_net::Config::dhcpv4(Default::default()),
|
||||
RESOURCES.init(StackResources::new()),
|
||||
seed,
|
||||
);
|
||||
|
||||
// Launch network task
|
||||
unwrap!(spawner.spawn(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);
|
||||
|
||||
// Then we can use it!
|
||||
let mut rx_buffer = [0; 256];
|
||||
let mut tx_buffer = [0; 256];
|
||||
let mut rx_meta = [PacketMetadata::EMPTY];
|
||||
let mut tx_meta = [PacketMetadata::EMPTY];
|
||||
|
||||
// Identifier used for the ICMP socket
|
||||
let ident = 42;
|
||||
|
||||
// Create and bind the socket
|
||||
let mut socket = IcmpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer);
|
||||
socket.bind(IcmpEndpoint::Ident(ident)).unwrap();
|
||||
|
||||
// Create the repr for the packet
|
||||
let icmp_repr = Icmpv4Repr::EchoRequest {
|
||||
ident,
|
||||
seq_no: 0,
|
||||
data: b"Hello, icmp!",
|
||||
};
|
||||
|
||||
// Send the packet and store the starting instant to mesure latency later
|
||||
let start = socket
|
||||
.send_to_with(
|
||||
icmp_repr.buffer_len(),
|
||||
cfg.gateway.unwrap(),
|
||||
|buf| {
|
||||
// Create and populate the packet buffer allocated by `send_to_with`
|
||||
let mut icmp_packet = Icmpv4Packet::new_unchecked(buf);
|
||||
icmp_repr.emit(&mut icmp_packet, &ChecksumCapabilities::default());
|
||||
Instant::now() // Return the instant where the packet was sent
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Recieve and log the data of the reply
|
||||
socket
|
||||
.recv_with(|(buf, addr)| {
|
||||
let packet = Icmpv4Packet::new_checked(buf).unwrap();
|
||||
info!("Recieved {:?} from {} in {}ms", packet.data(), addr, start.elapsed().as_millis());
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
loop {
|
||||
Timer::after_secs(10).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;
|
||||
}
|
||||
}
|
135
examples/rp/src/bin/ethernet_w5500_icmp_ping.rs
Normal file
135
examples/rp/src/bin/ethernet_w5500_icmp_ping.rs
Normal file
@ -0,0 +1,135 @@
|
||||
//! This example implements a LAN ping scan with the ping utilities in the icmp module of embassy-net.
|
||||
//!
|
||||
//! Example written for the [`WIZnet W5500-EVB-Pico`](https://docs.wiznet.io/Product/iEthernet/W5500/w5500-evb-pico) board.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use core::net::Ipv4Addr;
|
||||
use core::ops::Not;
|
||||
use core::str::FromStr;
|
||||
|
||||
use defmt::*;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::yield_now;
|
||||
use embassy_net::icmp::ping::{PingManager, PingParams};
|
||||
use embassy_net::icmp::PacketMetadata;
|
||||
use embassy_net::{Ipv4Cidr, 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::SPI0;
|
||||
use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
|
||||
use embassy_time::{Delay, Duration};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use rand::RngCore;
|
||||
use static_cell::StaticCell;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
type ExclusiveSpiDevice = ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static>, Delay>;
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn ethernet_task(runner: Runner<'static, W5500, ExclusiveSpiDevice, 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 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];
|
||||
static STATE: StaticCell<State<8, 8>> = 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();
|
||||
unwrap!(spawner.spawn(ethernet_task(runner)));
|
||||
|
||||
// Generate random seed
|
||||
let seed = rng.next_u64();
|
||||
|
||||
// Init network stack
|
||||
static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
|
||||
let (stack, runner) = embassy_net::new(
|
||||
device,
|
||||
embassy_net::Config::dhcpv4(Default::default()),
|
||||
RESOURCES.init(StackResources::new()),
|
||||
seed,
|
||||
);
|
||||
|
||||
// Launch network task
|
||||
unwrap!(spawner.spawn(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 gateway = cfg.gateway.unwrap();
|
||||
let mask = cfg.address.netmask();
|
||||
let lower_bound = (gateway.to_bits() & mask.to_bits()) + 1;
|
||||
let upper_bound = gateway.to_bits() | mask.to_bits().not();
|
||||
let addr_range = lower_bound..=upper_bound;
|
||||
|
||||
// Then we can use it!
|
||||
let mut rx_buffer = [0; 256];
|
||||
let mut tx_buffer = [0; 256];
|
||||
let mut rx_meta = [PacketMetadata::EMPTY];
|
||||
let mut tx_meta = [PacketMetadata::EMPTY];
|
||||
|
||||
// Create the ping manager instance
|
||||
let mut ping_manager = PingManager::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer);
|
||||
let addr = "192.168.8.1"; // Address to ping to
|
||||
// Create the PingParams with the target address
|
||||
let mut ping_params = PingParams::new(Ipv4Addr::from_str(addr).unwrap());
|
||||
// (optional) Set custom properties of the ping
|
||||
ping_params.set_payload(b"Hello, Ping!"); // custom payload
|
||||
ping_params.set_count(1); // ping 1 times per ping call
|
||||
ping_params.set_timeout(Duration::from_millis(500)); // wait .5 seconds instead of 4
|
||||
|
||||
info!("Online hosts in {}:", Ipv4Cidr::from_netmask(gateway, mask).unwrap());
|
||||
let mut total_online_hosts = 0u32;
|
||||
for addr in addr_range {
|
||||
let ip_addr = Ipv4Addr::from_bits(addr);
|
||||
// Set the target address in the ping params
|
||||
ping_params.set_target(ip_addr);
|
||||
// Execute the ping with the given parameters and wait for the reply
|
||||
match ping_manager.ping(&ping_params).await {
|
||||
Ok(time) => {
|
||||
info!("{} is online\n- latency: {}ms\n", ip_addr, time.as_millis());
|
||||
total_online_hosts += 1;
|
||||
},
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
info!("Ping scan complete, total online hosts: {}", total_online_hosts);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user