Expand description
We use the mutex-traits crate to provide mutex functionality.
You need to select an appropriate mutex implementation based on your needs.
And you can implement your own mutex by implementing the RawMutex trait from the mutex-traits crate.
use stm32f1_hal::mutex;
cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
use mutex::StdRawMutex;
type Mutex<T> = mutex::Mutex<StdRawMutex, T>;
} else {
use mutex::FakeRawMutex;
type Mutex<T> = mutex::Mutex<FakeRawMutex, T>;
}
}
let mutex = Mutex::<u32>::new(0);
let mut guard = mutex.try_lock().unwrap();
assert_eq!(*guard, 0);
*guard = 4;
drop(guard);
mutex
.try_with_lock(|data| {
assert_eq!(*data, 4);
*data = 5;
})
.unwrap();