stm32f1_hal/common/
os.rs

1cfg_if::cfg_if! {
2    if #[cfg(feature = "std")] {
3        pub use std::sync::Arc;
4    } else {
5        pub use alloc::vec::Vec;
6        pub use alloc::boxed::Box;
7        pub use alloc::sync::Arc;
8    }
9}
10
11#[inline(always)]
12pub fn yield_cpu() {
13    // TODO yield
14}
15
16pub trait Timeout {
17    fn start(&mut self) -> impl TimeoutInstance;
18}
19
20pub trait TimeoutInstance {
21    fn timeout(&mut self) -> bool;
22    fn restart(&mut self);
23    fn interval(&self);
24}
25
26// Retry ----------------------------------
27
28pub struct RetryTimes {
29    retry_times: usize,
30}
31impl RetryTimes {
32    pub fn new(retry_times: usize) -> Self {
33        Self { retry_times }
34    }
35}
36impl Timeout for RetryTimes {
37    #[inline]
38    fn start(&mut self) -> impl TimeoutInstance {
39        RetryTimesInstance {
40            count: 0,
41            retry_times: self.retry_times,
42        }
43    }
44}
45
46pub struct RetryTimesInstance {
47    count: usize,
48    retry_times: usize,
49}
50impl TimeoutInstance for RetryTimesInstance {
51    #[inline]
52    fn timeout(&mut self) -> bool {
53        if self.count <= self.retry_times {
54            self.count = self.count.wrapping_add(1);
55            false
56        } else {
57            true
58        }
59    }
60
61    #[inline(always)]
62    fn restart(&mut self) {
63        self.count = 0;
64    }
65
66    #[inline(always)]
67    fn interval(&self) {}
68}
69
70// Always ----------------------------------
71
72#[derive(Default)]
73pub struct AlwaysTimeout {}
74impl Timeout for AlwaysTimeout {
75    #[inline]
76    fn start(&mut self) -> impl TimeoutInstance {
77        AlwaysTimeoutInstance {}
78    }
79}
80
81pub struct AlwaysTimeoutInstance {}
82impl TimeoutInstance for AlwaysTimeoutInstance {
83    #[inline(always)]
84    fn timeout(&mut self) -> bool {
85        true
86    }
87
88    #[inline(always)]
89    fn restart(&mut self) {}
90
91    #[inline(always)]
92    fn interval(&self) {}
93}