Reputation: 139
I'd like to develop an embedded system where physical button presses trigger interrupts. I need to expose this data externally; however to my best understand, interrupts do not generate a return nor do they take parameters as they can be called anywhere.
Therefore, I'd like to set a global mutable variable when an interrupt is trigger so that the button presses may be detected at a later time. I realize that a global mutable variable is almost always avoidable, though I wonder if this specific instance is considered acceptable.
This question is within the scope of Rust, which I'd like to develop for.
Upvotes: 0
Views: 844
Reputation: 5501
So first I should recommend that you use RTIC. It really simplifies using interrupts. It has become many a Rust developers go-to library when dealing with interrupts.
There is also another library called cmim created by James Munns.
This is actually a nice alternative to use if you don't want to use RTIC.
It's also nicer to use than the static mut
variable route.
If you want to go the static mutable variable route you'll have to do the mutex dance:
static GPIO: Mutex<RefCell<Option<GPIOC>>> =
Mutex::new( RefCell::new( None ) );
#[interrupt]
unsafe fn EXTI4( ) {
free( | cs | {
if let Some( ref mut gpio ) = GPIO.borrow( cs ).borrow_mut( ).deref_mut( ) {
// Do something useful with gpio...
}
} );
}
Upvotes: 2
Reputation: 1660
In this case, a mutable global of some kind would be needed, but there are rust-safe ways of doing so. Some of the embedded rust libraries have a function that borrows a global as mutable for a limited duration, which is the main way of manipulating low level registers
Upvotes: 1