-
Notifications
You must be signed in to change notification settings - Fork 194
/
get_time.rs
71 lines (57 loc) · 1.6 KB
/
get_time.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! time::now Test
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
#![no_std]
#![no_main]
use esp_hal::delay::Delay;
use hil_test as _;
struct Context {
delay: Delay,
}
fn time_moves_forward_during<F: FnOnce(Context)>(ctx: Context, f: F) {
let t1 = esp_hal::time::now();
f(ctx);
let t2 = esp_hal::time::now();
assert!(t2 > t1);
}
#[cfg(test)]
#[embedded_test::tests]
mod tests {
use super::*;
#[init]
fn init() -> Context {
let _ = esp_hal::init(esp_hal::Config::default());
let delay = Delay::new();
Context { delay }
}
#[test]
#[timeout(3)]
fn test_current_time(ctx: Context) {
let t1 = esp_hal::time::now();
ctx.delay.delay_millis(500);
let t2 = esp_hal::time::now();
assert!(t2 > t1);
assert!((t2 - t1).to_millis() >= 500u64);
}
#[cfg(systimer)]
#[test]
#[timeout(3)]
fn test_current_time_construct_systimer(ctx: Context) {
time_moves_forward_during(ctx, |_| {
// construct the timer in between calls to current_time
let _ = esp_hal::timer::systimer::SystemTimer::new(unsafe {
esp_hal::peripherals::SYSTIMER::steal()
});
})
}
#[cfg(esp32)]
#[test]
#[timeout(3)]
fn test_current_time_construct_timg0(ctx: Context) {
time_moves_forward_during(ctx, |_| {
// construct the timer in between calls to current_time
let _ = esp_hal::timer::timg::TimerGroup::new(unsafe {
esp_hal::peripherals::TIMG0::steal()
});
})
}
}