diff --git a/Cargo.lock b/Cargo.lock index 52fabd1b0..c31211b22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -776,6 +776,19 @@ dependencies = [ "riot-rs-boards", ] +[[package]] +name = "embassy-net-udp" +version = "0.1.0" +dependencies = [ + "embassy-executor", + "embassy-net", + "embassy-time", + "embedded-io-async", + "linkme", + "riot-rs", + "riot-rs-boards", +] + [[package]] name = "embassy-nrf" version = "0.1.0" diff --git a/examples/embassy-net-udp/Cargo.toml b/examples/embassy-net-udp/Cargo.toml new file mode 100644 index 000000000..8237595f7 --- /dev/null +++ b/examples/embassy-net-udp/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "embassy-net-udp" +version = "0.1.0" +authors.workspace = true +edition.workspace = true +publish = false + +[dependencies] +riot-rs = { path = "../../src/riot-rs", features = [ "time", "usb_ethernet"] } +riot-rs-boards = { path = "../../src/riot-rs-boards" } +embassy-executor = { workspace = true, default-features = false } +embassy-time = { workspace = true, default-features = false } +linkme.workspace = true +embassy-net = { workspace = true, features = ["udp", "nightly"] } +embedded-io-async = "0.6.0" diff --git a/examples/embassy-net-udp/README.md b/examples/embassy-net-udp/README.md new file mode 100644 index 000000000..fa882b67d --- /dev/null +++ b/examples/embassy-net-udp/README.md @@ -0,0 +1,15 @@ +# embassy-net-udp + +## About + +This application is testing basic +[embassy](https://github.com/embassy-rs/embassy) _networking_ usage with RIOT-rs. + +## How to run + +In this folder, run + + laze build -b nrf52840dk run + +With the device USB cable connected, a USB ethernet device should pop up. +RIOT-rs will reply to ping requests on 10.0.42.61. diff --git a/examples/embassy-net-udp/laze.yml b/examples/embassy-net-udp/laze.yml new file mode 100644 index 000000000..878cf1c6f --- /dev/null +++ b/examples/embassy-net-udp/laze.yml @@ -0,0 +1,4 @@ +apps: + - name: embassy-net-udp + selects: + - ?release diff --git a/examples/embassy-net-udp/src/main.rs b/examples/embassy-net-udp/src/main.rs new file mode 100644 index 000000000..7a541f82b --- /dev/null +++ b/examples/embassy-net-udp/src/main.rs @@ -0,0 +1,75 @@ +#![no_main] +#![no_std] +#![feature(type_alias_impl_trait)] +#![feature(used_with_arg)] + +use riot_rs as _; + +use riot_rs::embassy::TaskArgs; +use riot_rs::rt::debug::println; + +#[embassy_executor::task] +async fn udp_echo(args: TaskArgs) { + use embassy_net::udp::{UdpSocket, PacketMetadata}; + let stack = args.stack; + + let mut rx_meta = [PacketMetadata::EMPTY; 16]; + let mut rx_buffer = [0; 4096]; + let mut tx_meta = [PacketMetadata::EMPTY; 16]; + let mut tx_buffer = [0; 4096]; + let mut buf = [0; 4096]; + + loop { + let mut socket = UdpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer); + + println!("Listening on UDP:1234..."); + if let Err(e) = socket.bind(1234) { + println!("bind error: {:?}", e); + continue; + } + + loop { + let (n, remote_endpoint) = match socket.recv_from(&mut buf).await { + Ok((0, _)) => { + println!("read EOF"); + break; + } + Ok((n, remote_endpoint)) => (n, remote_endpoint), + Err(e) => { + println!("read error: {:?}", e); + break; + } + }; + + println!("Received datagram from {:?}", remote_endpoint); + + //println!("rxd {:02x}", &buf[..n]); + + match socket.send_to(&buf[..n], remote_endpoint).await { + Ok(()) => {} + Err(e) => { + println!("write error: {:?}", e); + break; + } + }; + } + } +} + +use linkme::distributed_slice; +use riot_rs::embassy::EMBASSY_TASKS; + +#[distributed_slice(EMBASSY_TASKS)] +fn __start_udp_echo(spawner: embassy_executor::Spawner, t: TaskArgs) { + spawner.spawn(udp_echo(t)).unwrap(); +} + +#[no_mangle] +fn riot_main() { + println!( + "Hello from riot_main()! Running on a {} board.", + riot_rs::buildinfo::BOARD + ); + + loop {} +} diff --git a/examples/laze.yml b/examples/laze.yml index 45160ceea..d819f1775 100644 --- a/examples/laze.yml +++ b/examples/laze.yml @@ -9,6 +9,7 @@ subdirs: - core-sizes - embassy - embassy-net-tcp + - embassy-net-udp # - embassy-gpio - hello-world - minimal