Hi,
I am still a novice to Rust, and I've been struggling with the combination of Mutex + RefCell + Option in my STM32 project.
What I want in the code is described in the comments below, and the purpose of doing this, as indicated, is in order to use the GLOBAL_DATA in other functions without having to be passed an argument.
I think this might not be a recommended way in Rust, but is there even a way for that?
I will look forward to any suggestions!
Thanks.
#![no_main]
#![no_std]
use core::cell::RefCell;
use core::panic::PanicInfo;
use core::borrow::{Borrow, BorrowMut};
use core::ops::Deref;
use cortex_m_rt::entry;
use stm32l4xx_hal as hal;
use spin::Mutex;
pub struct Data {
value: u32,
}
static GLOBAL_DATA: Mutex<RefCell<Option<Data>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
unsafe {
let data = Data{value: 42};
GLOBAL_DATA.lock().replace(Some(data));
let guard = GLOBAL_DATA.lock();
let ref data_ref = guard.borrow().get_mut().unwrap();
// Here I would like to get an access to the data.value through GLOBAL_DATA.
// But, how?
// Is it even possible?
}
loop {
}
}
#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {}
}