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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Xtensa internal timers

use core::arch::asm;

#[cfg(feature = "ccompare0")]
#[inline]
pub fn get_ccompare0() -> u32 {
    let x: u32;
    unsafe { asm!("rsr.ccompare0 {0}", out(reg) x, options(nostack)) };
    x
}
#[cfg(feature = "ccompare1")]
#[inline]
pub fn get_ccompare1() -> u32 {
    let x: u32;
    unsafe { asm!("rsr.ccompare1 {0}", out(reg) x, options(nostack)) };
    x
}
#[cfg(feature = "ccompare2")]
#[inline]
pub fn get_ccompare2() -> u32 {
    let x: u32;
    unsafe { asm!("rsr.ccompare2 {0}", out(reg) x, options(nostack)) };
    x
}

#[cfg(feature = "ccompare3")]
#[inline]
pub fn get_ccompare3() -> u32 {
    let x: u32;
    unsafe { asm!("rsr.ccompare3 {0}", out(reg) x, options(nostack)) };
    x
}

#[cfg(feature = "ccompare0")]
#[inline]
pub fn set_ccompare0(val: u32) {
    unsafe {
        asm!("
        wsr.ccompare0 {0}
        isync
        ", in(reg) val, options(nostack))
    };
}
#[cfg(feature = "ccompare1")]
#[inline]
pub fn set_ccompare1(val: u32) {
    unsafe {
        asm!("
        wsr.ccompare1 {0}
        isync
        ", in(reg) val, options(nostack))
    };
}
#[cfg(feature = "ccompare2")]
#[inline]
pub fn set_ccompare2(val: u32) {
    unsafe {
        asm!("
        wsr.ccompare2 {0}
        isync
        ", in(reg) val, options(nostack))
    };
}
#[cfg(feature = "ccompare3")]
#[inline]
pub fn set_ccompare3(val: u32) {
    unsafe {
        asm!("
        wsr.ccompare3 {0}
        isync
        ", in(reg) val, options(nostack))
    };
}

/// Get the core cycle count
#[cfg(feature = "ccount")]
#[inline]
pub fn get_cycle_count() -> u32 {
    let x: u32;
    unsafe { asm!("rsr.ccount {0}", out(reg) x, options(nostack)) };
    x
}

/// cycle accurate delay using the cycle counter register
#[cfg(feature = "ccount")]
#[inline]
pub fn delay(clocks: u32) {
    let start = get_cycle_count();
    loop {
        if get_cycle_count().wrapping_sub(start) >= clocks {
            break;
        }
    }
}