New static mutable storage for the entry function
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- rust
- Domain
- embedded-iot
Research direction
Start by reviewing the existing #[entry] macro behavior and the proposed entry_with_context syntax, then compare the RTIC local-resource example described in the issue. Trace how the current static mut expansion reaches the entry function and define the desired context API and expansion; done means the design is agreed and its safety and compatibility implications are documented.
Written by the indexing model from the issue text.
Description
We carefully ensure the #[entry] function cannot be called other than after a reset (and after the start-up code has initialised everything).
Currently we use this knowledge to 'safely' replace static mut FOO: T = T::new() with code that uses an unsafe block to create &mut T reference to the static, which is passed in as an argument.
That is:
#[cortex_m_rt::entry]
fn main() -> ! {
static mut FOO: u32 = 123;
loop {}
}
becomes:
#[doc(hidden)]
#[export_name = "main"]
pub unsafe extern "C" fn __cortex_m_rt_main_trampoline() {
#[allow(static_mut_refs)]
__cortex_m_rt_main({
static mut FOO: u32 = 123;
unsafe { &mut FOO }
})
}
fn __cortex_m_rt_main(#[allow(non_snake_case)] FOO: &'static mut u32) -> ! {
loop {}
}
It is widely accepted that this kind of sleight of hand is not good. However, RTIC shows us a syntax that could work:
#[task(local = [state: u32 = 0])]
async fn foo(c: foo::Context) {
let old_state: u32 = *c.local.state;
*c.local.state += 1;
}
So, what if we wrote a new macro, called entry_with_context:
#[cortex_m_rt::entry_with_context(context = [state: u32 = 0])]
fn main(c: main::Context) -> ! {
let old_state: u32 = *c.state;
*c.state += 1;
loop {}
}
The advantage here is that is clear some 'magic' is happening to create a static resource and pass it to the main function.
It would expand to something like:
mod main {
pub(crate) struct Context {
state: &mut u32
}
}
#[doc(hidden)]
#[export_name = "main"]
pub unsafe extern "C" fn __cortex_m_rt_main_trampoline() {
static STATE: RacyCell<u32> = RacyCell::new(123);
__cortex_m_rt_main(main::Context {
state: unsafe { STATE.get_mut_ref() },
})
}
fn main(c: main::Context) -> ! {
let old_state: u32 = *c.state;
*c.state += 1;
loop {}
}
We don't even need to hide the actual fn main() function anymore, because you cannot call it without creating the context object.
- Dominant language
- Rust
- Stars
- 1k
- Forks
- 201
- Avg merge
- 1h 8m
- Merged PRs (30d)
- 2
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from rust-embedded/cortex-m
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
rust-embedded/cortex-m#651 · 5 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
rust-embedded/cortex-m#637 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
rust-embedded/cortex-m#511 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
rust-embedded/cortex-m#695 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
rust-embedded/cortex-m#694 · 1 comment ·
All issues in rust-embedded/cortex-m
Similar issues
-
proxy: SQL-over-HTTP turns empty arrays into [""] or [false], or HTTP 500 for int and float arraysOpen
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
neondatabase/neon#12965 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
goauthentik/authentik#26867 ·
Maintainers usually reply within 1 day
-
api bug macos maintainer-needed p2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
area::tern difficulty::easy good first issue kind::cleanup triage::ready
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
eclipse-zenoh/zenoh#2842 · 2 comments ·
Maintainers usually reply within 1 day