2017-12-28 15:06:45 +08:00
|
|
|
use core::i64;
|
2017-01-02 01:23:27 +08:00
|
|
|
use csr;
|
2016-09-30 04:36:04 +08:00
|
|
|
|
2017-12-28 15:06:45 +08:00
|
|
|
const INIT: u64 = i64::MAX as u64;
|
2016-12-16 21:28:25 +08:00
|
|
|
const FREQ: u64 = csr::CONFIG_CLOCK_FREQUENCY as u64;
|
2016-09-30 04:36:04 +08:00
|
|
|
|
|
|
|
pub fn init() {
|
|
|
|
unsafe {
|
2016-10-01 15:56:34 +08:00
|
|
|
csr::timer0::en_write(0);
|
|
|
|
csr::timer0::load_write(INIT);
|
|
|
|
csr::timer0::reload_write(INIT);
|
|
|
|
csr::timer0::en_write(1);
|
2016-09-30 04:36:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 02:28:29 +08:00
|
|
|
pub fn get_us() -> u64 {
|
|
|
|
unsafe {
|
|
|
|
csr::timer0::update_value_write(1);
|
|
|
|
(INIT - csr::timer0::value_read()) / (FREQ / 1_000_000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 04:36:04 +08:00
|
|
|
pub fn get_ms() -> u64 {
|
|
|
|
unsafe {
|
2016-10-01 15:56:34 +08:00
|
|
|
csr::timer0::update_value_write(1);
|
|
|
|
(INIT - csr::timer0::value_read()) / (FREQ / 1_000)
|
2016-09-30 04:36:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn spin_us(interval: u64) {
|
|
|
|
unsafe {
|
2016-10-01 15:56:34 +08:00
|
|
|
csr::timer0::update_value_write(1);
|
|
|
|
let threshold = csr::timer0::value_read() - interval * (FREQ / 1_000_000);
|
|
|
|
while csr::timer0::value_read() > threshold {
|
|
|
|
csr::timer0::update_value_write(1)
|
2016-09-30 04:36:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|