rart_rs/no_std/
timer.rs

1use crate::futures::time::DelayState;
2use crate::no_std::arc::Arc;
3use crate::common::blocking_mutex::BlockingMutex;
4use crate::common::result::Expect;
5use crate::RARTError;
6
7pub fn timer_init() {
8    // TODO Explain why this is safe
9    unsafe {
10        rtos_timer_init();
11    }
12}
13
14pub fn timer_new_delay(state: Arc<BlockingMutex<DelayState>>, timeout: u32) -> Result<(), RARTError> {
15    let state_ptr = Arc::into_raw(state) as *const ();
16
17    // TODO Explain why this is safe
18    unsafe {
19        rtos_timer_reschedule(rtos_timer_timeout, state_ptr, timeout);
20    }
21
22    Ok(())
23}
24
25#[no_mangle]
26pub extern "C" fn rtos_timer_timeout(state: *const ()) {
27    // TODO Explain why this is safe
28    let state = unsafe { Arc::from_raw(state as *const BlockingMutex<DelayState>) };
29    let mut state = state.lock().rart_expect("Cannot lock at rtos timer timeout");
30
31    if let DelayState::Waiting(waker) = &*state {
32        waker.wake_by_ref();
33    }
34    *state = DelayState::Completed;
35}
36
37extern "C" {
38    pub fn rtos_timer_init();
39    pub fn rtos_timer_reschedule(callback: unsafe extern "C" fn(*const ()), state: *const (), timeout: u32);
40}