Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

add some unsafe method to run code before RAM is initialized #17

Closed
japaric opened this issue Jun 14, 2017 · 4 comments
Closed

add some unsafe method to run code before RAM is initialized #17

japaric opened this issue Jun 14, 2017 · 4 comments

Comments

@japaric
Copy link
Member

japaric commented Jun 14, 2017

Some people have expressed that they would like to configure the CPU clock before RAM initialization to speed up the initialization process (specially when lots of RAM need to be initialized).

This has to be unsafe because all the static variables stored in .bss / .data would be uninitialized during the execution of this pre-init routine.

Possible user facing interface:

#[macro_use]
extern crate cortex_m_rt;

pre_init!(unsafe {
    // something
});

where pre_init! can only be used once.

@djmcgill
Copy link

How do you recommend doing this? I was thinking of either something like rust-lang/rfcs#1971 i.e. a default trait implementation and have the macro override it, or adding a new (weakly linked) function and then overriding that a la the r0 crate's init_array.

@japaric
Copy link
Member Author

japaric commented Aug 1, 2017

The latter, like we do for interrupt / exception handlers.

@jcsoo
Copy link
Contributor

jcsoo commented Apr 26, 2018

Another use case is disabling watchdogs before main() on some devices such as the K64 that have them enabled on reset. Unfortunately, leaving the watchdog enabled can leave the device in a reset loop that is difficult to recover from.

I would also like to enable instruction cache on devices that support it, and enter main() with interrupts disabled by default.

It sounds like the right way to do this is to add a weakly linked pre_init function that
is NOP by default but that can be overridden, and then call that within reset_handler().

Or would it be better to have a way to replace reset_handler?

@thejpster
Copy link
Contributor

I needed this on the AM5728 IPU1 as both Cortex-M4 cores (there are two) are released out of reset at the same time with the same reset vector. You write a little bit of code (making sure not to use the stack pointer) to put Core 1 to sleep and let Core 0 carry on. You can then take Core 1 out of sleep at a later time if you're running an SMP-capable RTOS.

Currently I fixed this by forking the crate.

bors bot added a commit that referenced this issue Aug 13, 2018
81: Add a __pre_init function to be called at the start of the reset handler r=korken89 a=yodaldevoid

I needed this for a project so I went ahead and rebased and tweaked @jcsoo 's changes from #71. I will admit, I got a little impatient waiting and that also played into why I did this. If either @jcsoo or @japaric have an issue with this PR, please let me know and I will remove it. I apologize for toes that I have stepped on.

Now onto the PR. This is possibly the simplest implementation that is possible. No nightly features were used in this implementation. Also, there is no hand-holding for the end user; if they want to mess with uninitialized statics, that is on them.

If you would like me to add more documentation, please let me know what you would like to see.

Fixes #17 

Co-authored-by: Jonathan Soo <[email protected]>
Co-authored-by: Gabriel Smith <[email protected]>
@bors bors bot closed this as completed in #81 Aug 13, 2018
rukai pushed a commit to rukai/cortex-m-rt that referenced this issue May 1, 2021
17: update copyright year r=japaric a=japaric

Testing branch protection

Co-authored-by: Jorge Aparicio <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants