diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 9c79847e2366a73f9e04498e5cffd9bf390dfe3f..1b6fc17ebf49cffbb014fab2ca4553c49698b5df 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -107,6 +107,9 @@ if RT_USING_CPUTIME depends on ARCH_RISCV64 help Some RISCV64 MCU Use rdtime instructions read CPU time. + config CPUTIME_TIMER_FREQ + int "CPUTIME timer freq" + default 0 endif config RT_USING_I2C diff --git a/components/drivers/cputime/cputime.c b/components/drivers/cputime/cputime.c index 5fe0b07cb1311fafd0ff1bf935a7d50a233eed3d..22b56e6db25534ebb34bf197b86965a66e65c6c2 100644 --- a/components/drivers/cputime/cputime.c +++ b/components/drivers/cputime/cputime.c @@ -20,7 +20,7 @@ static const struct rt_clock_cputime_ops *_cputime_ops = RT_NULL; * * @return the number of nanosecond per tick */ -float clock_cpu_getres(void) +double clock_cpu_getres(void) { if (_cputime_ops) return _cputime_ops->cputime_getres(); @@ -53,7 +53,7 @@ uint64_t clock_cpu_gettime(void) */ uint64_t clock_cpu_microsecond(uint64_t cpu_tick) { - float unit = clock_cpu_getres(); + double unit = clock_cpu_getres(); return (uint64_t)((cpu_tick * unit) / 1000); } @@ -68,7 +68,7 @@ uint64_t clock_cpu_microsecond(uint64_t cpu_tick) */ uint64_t clock_cpu_millisecond(uint64_t cpu_tick) { - float unit = clock_cpu_getres(); + double unit = clock_cpu_getres(); return (uint64_t)((cpu_tick * unit) / (1000 * 1000)); } diff --git a/components/drivers/cputime/cputime_cortexm.c b/components/drivers/cputime/cputime_cortexm.c index d877064893b247a0f7049b4df93e20f52ee24669..247e4fcf2df561abfa676178b8fb14c72841dbc4 100644 --- a/components/drivers/cputime/cputime_cortexm.c +++ b/components/drivers/cputime/cputime_cortexm.c @@ -19,9 +19,9 @@ #endif /* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */ -static float cortexm_cputime_getres(void) +static double cortexm_cputime_getres(void) { - float ret = 1000 * 1000 * 1000; + double ret = 1000UL * 1000 * 1000; ret = ret / SystemCoreClock; return ret; diff --git a/components/drivers/cputime/cputime_riscv.c b/components/drivers/cputime/cputime_riscv.c index 7e327fc2d42516233f2455a75b497715fc816de5..a486c0a5f577ec91dcaa2754b65d9a08f632d3f4 100644 --- a/components/drivers/cputime/cputime_riscv.c +++ b/components/drivers/cputime/cputime_riscv.c @@ -4,16 +4,13 @@ #include - -#define TIMER_FREQ (24000000) - /* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */ -static float riscv_cputime_getres(void) +static double riscv_cputime_getres(void) { - float ret = 1000 * 1000 * 1000; + double ret = 1000UL * 1000 * 1000; - ret = ret / TIMER_FREQ; + ret = ret / CPUTIME_TIMER_FREQ; return ret; } @@ -27,9 +24,10 @@ static uint64_t riscv_cputime_gettime(void) } const static struct rt_clock_cputime_ops _riscv_ops = - { - riscv_cputime_getres, - riscv_cputime_gettime}; +{ + riscv_cputime_getres, + riscv_cputime_gettime +}; int riscv_cputime_init(void) { diff --git a/components/drivers/include/drivers/cputime.h b/components/drivers/include/drivers/cputime.h index bcc339ca355f37af3b156c6057f30e184357b287..8fce901c6a1fee38c8121109ef3bb8d450326cfd 100644 --- a/components/drivers/include/drivers/cputime.h +++ b/components/drivers/include/drivers/cputime.h @@ -15,11 +15,11 @@ struct rt_clock_cputime_ops { - float (*cputime_getres) (void); + double (*cputime_getres)(void); uint64_t (*cputime_gettime)(void); }; -float clock_cpu_getres(void); +double clock_cpu_getres(void); uint64_t clock_cpu_gettime(void); uint64_t clock_cpu_microsecond(uint64_t cpu_tick); diff --git a/components/libc/compilers/common/ctime.c b/components/libc/compilers/common/ctime.c index c89f122ad385ace0327c33ec2b9f3caa9db20ed7..17c0ee969ea290e40a912393c9ebe5f22204c6c7 100644 --- a/components/libc/compilers/common/ctime.c +++ b/components/libc/compilers/common/ctime.c @@ -538,7 +538,7 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) uint64_t cpu_tick, cpu_tick_old; cpu_tick_old = clock_cpu_gettime(); rt_tick_t tick; - float unit = clock_cpu_getres(); + double unit = clock_cpu_getres(); cpu_tick = (rqtp->tv_sec * NANOSECOND_PER_SECOND + ((uint64_t)rqtp->tv_nsec * NANOSECOND_PER_SECOND) / NANOSECOND_PER_SECOND) / unit; tick = (unit * cpu_tick) / (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND); @@ -549,8 +549,8 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) if (rmtp) { uint64_t rmtp_cpu_tick = cpu_tick_old + cpu_tick - clock_cpu_gettime(); - rmtp->tv_sec = ((int)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND; - rmtp->tv_nsec = ((int)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND; + rmtp->tv_sec = ((time_t)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND; + rmtp->tv_nsec = ((long)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND; } rt_set_errno(EINTR); return -1; @@ -674,7 +674,7 @@ int clock_gettime(clockid_t clockid, struct timespec *tp) { case CLOCK_REALTIME: { - int tick; + rt_tick_t tick; rt_base_t level; level = rt_hw_interrupt_disable(); @@ -686,16 +686,17 @@ int clock_gettime(clockid_t clockid, struct timespec *tp) break; #ifdef RT_USING_CPUTIME + case CLOCK_MONOTONIC: case CLOCK_CPUTIME_ID: { - float unit = 0; - long long cpu_tick; + double unit = 0; + uint64_t cpu_tick; unit = clock_cpu_getres(); cpu_tick = clock_cpu_gettime(); - tp->tv_sec = ((long long)(cpu_tick * unit)) / NANOSECOND_PER_SECOND; - tp->tv_nsec = ((long long)(cpu_tick * unit)) % NANOSECOND_PER_SECOND; + tp->tv_sec = ((uint64_t)(cpu_tick * unit)) / NANOSECOND_PER_SECOND; + tp->tv_nsec = ((uint64_t)(cpu_tick * unit)) % NANOSECOND_PER_SECOND; } break; #endif @@ -738,6 +739,7 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, s tick = rqtp->tv_sec * RT_TICK_PER_SECOND + ((uint64_t)(rqtp->tv_nsec) * RT_TICK_PER_SECOND) / NANOSECOND_PER_SECOND; } rt_thread_delay(tick); + if (rt_get_errno() == -RT_EINTR) { if (rmtp) @@ -760,7 +762,7 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, s uint64_t cpu_tick, cpu_tick_old; cpu_tick_old = clock_cpu_gettime(); rt_tick_t tick; - float unit = clock_cpu_getres(); + double unit = clock_cpu_getres(); cpu_tick = (rqtp->tv_sec * NANOSECOND_PER_SECOND + rqtp->tv_nsec * (NANOSECOND_PER_SECOND / NANOSECOND_PER_SECOND)) / unit; if ((flags & TIMER_ABSTIME) == TIMER_ABSTIME) @@ -773,8 +775,8 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, s if (rmtp) { uint64_t rmtp_cpu_tick = cpu_tick_old + cpu_tick - clock_cpu_gettime(); - rmtp->tv_sec = ((int)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND; - rmtp->tv_nsec = ((int)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND; + rmtp->tv_sec = ((time_t)(rmtp_cpu_tick * unit)) / NANOSECOND_PER_SECOND; + rmtp->tv_nsec = ((long)(rmtp_cpu_tick * unit)) % NANOSECOND_PER_SECOND; } rt_set_errno(EINTR); return -1; @@ -817,7 +819,7 @@ int clock_settime(clockid_t clockid, const struct timespec *tp) tick = rt_tick_get(); /* get tick */ /* update timevalue */ _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK; - _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1; + _timevalue.tv_sec = second - tick / RT_TICK_PER_SECOND - 1; rt_hw_interrupt_enable(level); /* update for RTC device */