From c6fa87b3d5f03eadf23c6db9dc366e60b4c98c18 Mon Sep 17 00:00:00 2001 From: liaoao Date: Thu, 7 Mar 2024 16:07:25 +0800 Subject: [PATCH] riscv_mtimer: modify riscv_mtimer_current to reduce precision lost Signed-off-by: liaoao --- arch/risc-v/src/common/riscv_mtimer.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/risc-v/src/common/riscv_mtimer.c b/arch/risc-v/src/common/riscv_mtimer.c index b8e3f4600f780..1b4df41ce7c78 100644 --- a/arch/risc-v/src/common/riscv_mtimer.c +++ b/arch/risc-v/src/common/riscv_mtimer.c @@ -319,10 +319,11 @@ static int riscv_mtimer_current(struct oneshot_lowerhalf_s *lower, struct riscv_mtimer_lowerhalf_s *priv = (struct riscv_mtimer_lowerhalf_s *)lower; uint64_t mtime = riscv_mtimer_get_mtime(priv); - uint64_t nsec = mtime / (priv->freq / USEC_PER_SEC) * NSEC_PER_USEC; + uint64_t left; - ts->tv_sec = nsec / NSEC_PER_SEC; - ts->tv_nsec = nsec % NSEC_PER_SEC; + ts->tv_sec = mtime / priv->freq; + left = mtime - ts->tv_sec * priv->freq; + ts->tv_nsec = NSEC_PER_SEC * left / priv->freq; return 0; }