//! 硬延时函数

use crate::{
    processor::cpu_wfe,
    time::{
        NSEC_PER_MSEC, NSEC_PER_USEC,
        timekeeping::{ktime_get, ktime_get_cycles},
    },
};

/// 硬延时tick次
pub fn delay(cycles: u64) {
    let start = ktime_get_cycles();
    while (ktime_get_cycles() - start) < cycles {
        cpu_wfe();
    }

    while (ktime_get_cycles() - start) < cycles {
        cpu_wfe();
    }
}

/// 硬延时ns纳秒
pub fn ndelay(nsec: u64) {
    let start = ktime_get();

    while (ktime_get() - start) < nsec {
        cpu_wfe();
    }

    while (ktime_get() - start) < nsec {
        cpu_wfe();
    }
}

/// 硬延时us微秒
pub fn udelay(usec: u64) {
    ndelay(usec * NSEC_PER_USEC);
}

/// 硬延时ms毫秒
pub fn mdelay(msec: u64) {
    ndelay(msec * NSEC_PER_MSEC);
}

/// 硬延时s秒
pub fn sdelay(sec: u64) {
    mdelay(sec * 1000);
}
