Browse Source

fix cputime and ctime.

xieyangrun 2 years ago
parent
commit
21472e3ff0

+ 3 - 0
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

+ 3 - 3
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));
 }

+ 2 - 2
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;

+ 7 - 9
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)
 {

+ 2 - 2
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);

+ 14 - 12
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 */