Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce Application trait #41

Merged
merged 7 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

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

13 changes: 13 additions & 0 deletions examples/application/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "application"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
publish = false

[dependencies]
riot-rs = { path = "../../src/riot-rs" }
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
5 changes: 5 additions & 0 deletions examples/application/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# application

## About

This folder contains a minimal `Application` example.
4 changes: 4 additions & 0 deletions examples/application/laze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apps:
- name: application
selects:
- ?release
34 changes: 34 additions & 0 deletions examples/application/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![no_main]
#![no_std]
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

use riot_rs::embassy::{arch, Application, ApplicationInitError, Drivers, InitializationArgs};

use riot_rs::rt::debug::println;

struct MyApplication {
state: u32, // some state
}

impl Application for MyApplication {
fn initialize(
_peripherals: &mut arch::OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
println!("MyApplication::initialize()");
Ok(&Self { state: 0 })
}

fn start(&self, _spawner: embassy_executor::Spawner, _drivers: Drivers) {
println!("MyApplication::start()");
// ...
}
}

riot_rs::embassy::riot_initialize!(MyApplication);

#[no_mangle]
fn riot_main() {
riot_rs::rt::debug::exit(Ok(()))
}
26 changes: 17 additions & 9 deletions examples/embassy-net-tcp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

use riot_rs as _;
use riot_rs::embassy::{arch, Application, ApplicationInitError, Drivers, InitializationArgs};

use riot_rs::embassy::TaskArgs;
use riot_rs::rt::debug::println;

use embedded_io_async::Write;

#[embassy_executor::task]
async fn tcp_echo(args: TaskArgs) {
async fn tcp_echo(drivers: Drivers) {
use embassy_net::tcp::TcpSocket;
let stack = args.stack;
let stack = drivers.stack.get().unwrap();
kaspar030 marked this conversation as resolved.
Show resolved Hide resolved

let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
Expand Down Expand Up @@ -57,14 +56,23 @@ async fn tcp_echo(args: TaskArgs) {
}
}

use linkme::distributed_slice;
use riot_rs::embassy::EMBASSY_TASKS;
struct TcpEcho {}

#[distributed_slice(EMBASSY_TASKS)]
fn __start_tcp_echo(spawner: embassy_executor::Spawner, t: TaskArgs) {
spawner.spawn(tcp_echo(t)).unwrap();
impl Application for TcpEcho {
fn initialize(
_peripherals: &mut arch::OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
Ok(&Self {})
}

fn start(&self, spawner: embassy_executor::Spawner, drivers: Drivers) {
spawner.spawn(tcp_echo(drivers)).unwrap();
}
}

riot_rs::embassy::riot_initialize!(TcpEcho);

#[no_mangle]
fn riot_main() {
println!(
Expand Down
36 changes: 25 additions & 11 deletions examples/embassy-net-udp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

use riot_rs as _;
use riot_rs::embassy::{arch, Application, ApplicationInitError, Drivers, InitializationArgs};

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;
async fn udp_echo(drivers: Drivers) {
use embassy_net::udp::{PacketMetadata, UdpSocket};
let stack = drivers.stack.get().unwrap();

let mut rx_meta = [PacketMetadata::EMPTY; 16];
let mut rx_buffer = [0; 4096];
Expand All @@ -20,7 +19,13 @@ async fn udp_echo(args: TaskArgs) {
let mut buf = [0; 4096];

loop {
let mut socket = UdpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer);
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) {
Expand Down Expand Up @@ -56,14 +61,23 @@ async fn udp_echo(args: TaskArgs) {
}
}

use linkme::distributed_slice;
use riot_rs::embassy::EMBASSY_TASKS;
struct UdpEcho {}

#[distributed_slice(EMBASSY_TASKS)]
fn __start_udp_echo(spawner: embassy_executor::Spawner, t: TaskArgs) {
spawner.spawn(udp_echo(t)).unwrap();
impl Application for UdpEcho {
fn initialize(
_peripherals: &mut arch::OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
Ok(&Self {})
}

fn start(&self, spawner: embassy_executor::Spawner, drivers: Drivers) {
spawner.spawn(udp_echo(drivers)).unwrap();
}
}

riot_rs::embassy::riot_initialize!(UdpEcho);

#[no_mangle]
fn riot_main() {
println!(
Expand Down
1 change: 1 addition & 0 deletions examples/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defaults:
- riot-rs

subdirs:
- application
- benchmark
- bottles
- core-sizes
Expand Down
14 changes: 13 additions & 1 deletion src/riot-rs-embassy/src/assign_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
/// Using the `assign_resources!` macro to define the peripherals to extract will generate another
/// macro, `split_resources!`, that allows to obtain the requested peripherals where needed (see
/// the original documentation for details).
/// The `assign_resources!` macro expects a `peripherals` module to be in scope, where the
/// peripheral types should come from.
///
/// `split_resources!` should be provided with an instance of `OptionalPeripherals` to extract
/// peripherals from.
/// When attempting to extract a peripheral from `OptionalPeripherals` that has already been
/// extracted, `split_resources!` will return from the function where it has been called in with
/// [`Err(AssigningResourcesError::ObtainingPeripheral)?`].
// Based on https://github.com/adamgreig/assign-resources/tree/94ad10e2729afdf0fd5a77cd12e68409a982f58a
// under MIT license
#[macro_export]
Expand Down Expand Up @@ -57,10 +63,16 @@ macro_rules! assign_resources {
($p:ident) => {
$resources {
$($group_name: $group_struct {
$($resource_name: $p.$resource_field.take().ok_or(1)?),*
$($resource_name: $p.$resource_field
.take()
.ok_or($crate::assign_resources::AssigningResourcesError::ObtainingPeripheral)?),*
}),*
}
}
);
}
}

pub enum AssigningResourcesError {
ObtainingPeripheral,
}
Loading