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
//! Xtensa internal timers

#[inline]
pub fn get_ccompare0() -> u32 {
    let x: u32;
    unsafe { llvm_asm!("rsr.ccompare0 $0" : "=r"(x) ::: "volatile" ) };
    x
}
#[inline]
pub fn get_ccompare1() -> u32 {
    let x: u32;
    unsafe { llvm_asm!("rsr.ccompare1 $0" : "=r"(x) ::: "volatile" ) };
    x
}
#[inline]
pub fn get_ccompare2() -> u32 {
    let x: u32;
    unsafe { llvm_asm!("rsr.ccompare2 $0" : "=r"(x) ::: "volatile" ) };
    x
}

#[inline]
pub fn set_ccompare0(val: u32) {
    unsafe {
        llvm_asm!("
        wsr.ccompare0 $0
        isync
        " :: "r"(val):::: "volatile" )
    };
}
#[inline]
pub fn set_ccompare1(val: u32) {
    unsafe {
        llvm_asm!("
        wsr.ccompare1 $0
        isync
        " :: "r"(val):::: "volatile" )
    };
}
#[inline]
pub fn set_ccompare2(val: u32) {
    unsafe {
        llvm_asm!("
        wsr.ccompare2 $0
        isync
        " :: "r"(val):::: "volatile" )
    };
}

/// Get the core cycle count
#[inline]
pub fn get_cycle_count() -> u32 {
    let x: u32;
    unsafe { llvm_asm!("rsr.ccount $0" : "=r"(x) ::: "volatile" ) };
    x
}

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