summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxieyangrun <[email protected]>2023-01-09 15:21:38 +0800
committerguo <[email protected]>2023-01-10 10:03:35 +0800
commit21472e3ff081df18e1876ceb6706c42607b34261 (patch)
tree0ea438920867efa725396a321e750ea6f51555c1
parent93dd8c6c6e97bd2cc9865f148e786326cf197fe5 (diff)
fix cputime and ctime.
-rwxr-xr-xcomponents/drivers/Kconfig3
-rw-r--r--components/drivers/cputime/cputime.c6
-rw-r--r--components/drivers/cputime/cputime_cortexm.c4
-rw-r--r--components/drivers/cputime/cputime_riscv.c16
-rw-r--r--components/drivers/include/drivers/cputime.h4
-rw-r--r--components/libc/compilers/common/ctime.c26
6 files changed, 31 insertions, 28 deletions
diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig
index 9c79847e23..1b6fc17ebf 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 5fe0b07cb1..22b56e6db2 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 d877064893..247e4fcf2d 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 7e327fc2d4..a486c0a5f5 100644
--- a/components/drivers/cputime/cputime_riscv.c
+++ b/components/drivers/cputime/cputime_riscv.c
@@ -4,16 +4,13 @@
#include <board.h>
-
-#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 bcc339ca35..8fce901c6a 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 c89f122ad3..17c0ee969e 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 */