Skip to content

Commit

Permalink
feat: add an example demonstrating UDP echo over USB
Browse files Browse the repository at this point in the history
This example is a direct port of the embassy-net TCP echo example, but
with UDP instead.
  • Loading branch information
ROMemories authored and kaspar030 committed Dec 5, 2023
1 parent 055fcb1 commit 3f582a3
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions examples/embassy-net-udp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 15 additions & 0 deletions examples/embassy-net-udp/README.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions examples/embassy-net-udp/laze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apps:
- name: embassy-net-udp
selects:
- ?release
75 changes: 75 additions & 0 deletions examples/embassy-net-udp/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
1 change: 1 addition & 0 deletions examples/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ subdirs:
- core-sizes
- embassy
- embassy-net-tcp
- embassy-net-udp
# - embassy-gpio
- hello-world
- minimal
Expand Down

0 comments on commit 3f582a3

Please sign in to comment.