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

refactor: drop initialization args #83

Merged
merged 2 commits into from
Feb 19, 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
3 changes: 1 addition & 2 deletions examples/application/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

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

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

Expand All @@ -14,7 +14,6 @@ struct MyApplication {
impl Application for MyApplication {
fn initialize(
_peripherals: &mut arch::OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
println!("MyApplication::initialize()");
Ok(&Self { state: 0 })
Expand Down
4 changes: 1 addition & 3 deletions examples/embassy-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ mod routes;
use riot_rs as _;

use riot_rs::embassy::{
arch::OptionalPeripherals, Application, ApplicationInitError, Drivers, InitializationArgs,
NetworkStack,
arch::OptionalPeripherals, Application, ApplicationInitError, Drivers, NetworkStack,
};
use riot_rs::rt::debug::println;

Expand Down Expand Up @@ -95,7 +94,6 @@ struct WebServer {
impl Application for WebServer {
fn initialize(
peripherals: &mut OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
#[cfg(feature = "button-readings")]
let button_inputs = {
Expand Down
3 changes: 1 addition & 2 deletions examples/embassy-net-tcp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

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

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

Expand Down Expand Up @@ -61,7 +61,6 @@ struct TcpEcho {}
impl Application for TcpEcho {
fn initialize(
_peripherals: &mut arch::OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
Ok(&Self {})
}
Expand Down
3 changes: 1 addition & 2 deletions examples/embassy-net-udp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

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

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

Expand Down Expand Up @@ -66,7 +66,6 @@ struct UdpEcho {}
impl Application for UdpEcho {
fn initialize(
_peripherals: &mut arch::OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
Ok(&Self {})
}
Expand Down
28 changes: 6 additions & 22 deletions src/riot-rs-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ mod network;
#[cfg(feature = "wifi_cyw43")]
mod wifi;

#[cfg(feature = "net")]
use core::cell::OnceCell;

// re-exports
pub use linkme::{self, distributed_slice};
pub use static_cell::make_static;

use embassy_executor::Spawner;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};

use crate::define_peripherals::DefinePeripheralsError;
use embassy_executor::Spawner;

#[cfg(feature = "usb")]
use usb::ethernet::NetworkDevice;
Expand All @@ -46,17 +45,8 @@ pub use network::NetworkStack;
#[cfg(feature = "threading")]
pub mod blocker;

pub type Task = fn(
&mut arch::OptionalPeripherals,
InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError>;

// Allows us to pass extra initialization arguments in the future
#[derive(Copy, Clone)]
#[non_exhaustive]
pub struct InitializationArgs {
pub peripherals: &'static Mutex<CriticalSectionRawMutex, arch::OptionalPeripherals>,
}
pub type Task =
fn(&mut arch::OptionalPeripherals) -> Result<&dyn Application, ApplicationInitError>;

#[derive(Copy, Clone)]
pub struct Drivers {
Expand Down Expand Up @@ -202,13 +192,9 @@ async fn init_task(mut peripherals: arch::OptionalPeripherals) {
wifi::cyw43::join(control).await;
};

let init_args = InitializationArgs {
peripherals: make_static!(Mutex::new(peripherals)),
};

for task in EMBASSY_TASKS {
// TODO: should all tasks be initialized before starting the first one?
match task(&mut *init_args.peripherals.lock().await, init_args) {
match task(&mut peripherals) {
Ok(initialized_application) => initialized_application.start(spawner, drivers),
Err(err) => panic!("Error while initializing an application: {err:?}"),
}
Expand All @@ -232,7 +218,6 @@ pub trait Application {
/// The [`assign_resources!`] macro can be leveraged to extract the required peripherals.
fn initialize(
peripherals: &mut arch::OptionalPeripherals,
init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError>
where
Self: Sized;
Expand Down Expand Up @@ -271,9 +256,8 @@ macro_rules! riot_initialize {
#[linkme(crate = $crate::linkme)]
fn __init_application(
peripherals: &mut $crate::arch::OptionalPeripherals,
init_args: $crate::InitializationArgs,
) -> Result<&dyn $crate::Application, $crate::ApplicationInitError> {
<$prog_type as Application>::initialize(peripherals, init_args)
<$prog_type as Application>::initialize(peripherals)
}
};
}
Loading