esp_wifi/
tasks.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::{
    compat::timer_compat::TIMERS,
    preempt::arch_specific::task_create,
    timer::{systimer_count, yield_task},
};

/// Initializes the `main` and `timer` tasks for the Wi-Fi driver.
pub(crate) fn init_tasks() {
    // allocate the main task
    crate::preempt::allocate_main_task();

    // schedule the timer task
    task_create(timer_task, core::ptr::null_mut(), 8192);
}

/// Entry point for the timer task responsible for handling scheduled timer
/// events.
pub(crate) extern "C" fn timer_task(_param: *mut esp_wifi_sys::c_types::c_void) {
    loop {
        let current_timestamp = systimer_count();
        let to_run = critical_section::with(|cs| unsafe {
            let mut timers = TIMERS.borrow_ref_mut(cs);
            let to_run = timers.find_next_due(current_timestamp);

            if let Some(to_run) = to_run {
                to_run.active = to_run.periodic;

                if to_run.periodic {
                    to_run.started = current_timestamp;
                }

                Some(to_run.callback)
            } else {
                None
            }
        });

        // run the due timer callback NOT in an interrupt free context
        if let Some(to_run) = to_run {
            trace!("trigger timer....");
            to_run.call();
            trace!("timer callback called");
        } else {
            yield_task();
        }
    }
}