Skip to content

Peripherals as scoped parameters to main #403

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

Open
bergus opened this issue Mar 2, 2019 · 5 comments
Open

Peripherals as scoped parameters to main #403

bergus opened this issue Mar 2, 2019 · 5 comments

Comments

@bergus
Copy link
Contributor

bergus commented Mar 2, 2019

Hello!
When using cortex-m-rtfm, I found it incredibly useful and intuitive that the peripherals from the respective device crate are passed into the init function. It's like taking argc and argv parameters in a main function.
Would it be possible to have the same for #[entry]-attributed functions? The current Peripheral API, introduced in the [RFC] Peripherals as scoped singletons is comparably clumsy. I dislike that global initialisation flag and the panicky unwrapping of the Option, and even in case the optimiser is able to eliminate them (I haven't checked) it would be cleaner code if one could just omit the static mut DEVICE_PERIPHERALS: bool from the device crates. I am hoping for something like

#[entry]
fn main(peripherals: stm32f30x::Peripherals) -> ! {
    // initialization
    loop {
        // application logic
    }
}

I'm not sure whether this is feasible at all (especially regarding the dependency hierarchy between cortext-m-rt and the device crates), where the right place to discuss this is (svd2rust? embedded-wg?) and what needs to be done to make this happen (should this be a RFC?). Please provide some guidance :-)

@korken89
Copy link
Contributor

IMO this is a much cleaner entry point for peripherals. If you'd like to, please create an RFC even if it has been a lot of time since this issue was started. :)

@korken89
Copy link
Contributor

This also gives the idea of a starting "world context" that can be moved, split, and passed around.
Stealing a bit of RTFM syntax would yield:

#[entry(device = my_pac_crate)]
fn main(core: cortex_m::Peripherals, device: my_pac_crate::Peripherals) -> ! {
    // ...
}

or alternatively:

#[entry(device = my_pac_crate)]
fn main(cx: entry::Context) -> ! {
    let core = cx.core;
    let device = cx.device;

    // ...
}

This would also give users the choice to put these in globals if they want, and not be forced to it as they are today,

@perlindgren
Copy link

IMHO, we have two levels of abstraction, cortex-m/cortex-m-rt, which provides the low level HW interaction, and we have RTFM as the lowest level user facing API. So there is no need to extend the level of abstraction provided by cortex-m/cortex-m-rt.

RTFM is designed NOT to stand in your way, and does not by itself imply any overhead (rather the opposite, as it provides zero-cost safe resource sharing).

If you want to build/use other abstractions to concurrent programming (generators/async-await etc.) there is nothing stopping you from do that on-top of RTFM. It just adds the clear advantage of keeping the abstractions sound by construction.

Of course all of these crates are moving targets as we progress, and we (The RTFM Team) are very happy to discuss suggestions/improvements facilitating embedded programming in Rust.

@therealprof
Copy link
Contributor

@perlindgren Would you mind keeping RTFM diversion and promotions out of the cortex-m-rt context, please?

@thejpster
Copy link
Contributor

@therealprof, to be fair the OP is talking about how much they liked cortex-m-rtfm, but let's try and stay on-topic.

@adamgreig adamgreig transferred this issue from rust-embedded/cortex-m-rt Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants