-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an example demonstrating UDP echo over USB
This example is a direct port of the embassy-net TCP echo example, but with UDP instead.
- Loading branch information
1 parent
055fcb1
commit 3f582a3
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apps: | ||
- name: embassy-net-udp | ||
selects: | ||
- ?release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters