From 341f78fed7bc2ea53f454cfb0966e43b45cb1786 Mon Sep 17 00:00:00 2001 From: BernardXiong Date: Sun, 31 Dec 2017 14:44:04 +0800 Subject: [PATCH] [DeviceDrivers] Add more API. 1. Use float type for clock_cpu_getres() return value; 2. Add clock_cpu_microsecond/millisecond APIs. --- components/drivers/Kconfig | 16 +++++---- components/drivers/cputime/cputime.c | 36 +++++++++++++++++++- components/drivers/cputime/cputime_cortexm.c | 7 ++-- components/drivers/include/drivers/cputime.h | 7 ++-- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 4e47b21408..dcd23715b0 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -7,7 +7,7 @@ config RT_USING_DEVICE_IPC config RT_USING_SERIAL bool "Using serial device drivers" select RT_USING_DEVICE_IPC - select RT_USING_DEVICE + select RT_USING_DEVICE default y config RT_USING_CAN @@ -24,13 +24,17 @@ config RT_USING_CPUTIME help When enable this option, the BSP should provide a rt_clock_cputime_ops for CPU time by: - clock_cpu_setops(const struct rt_clock_cputime_ops *ops); + const static struct rt_clock_cputime_ops _ops = {...}; + clock_cpu_setops(&_ops); - Then developer can use high resolution clock counter with: + Then user can use high resolution clock counter with: - ts = clock_cpu_gettime(); - /* The unit of clock_cpu_gettime() can be returned by */ - unit = clock_cpu_getres(); /* number for nanosecond */ + ts1 = clock_cpu_gettime(); + ts2 = clock_cpu_gettime(); + + /* and get the ms of delta tick with API: */ + ms_tick = clock_cpu_millisecond(t2 - t1); + us_tick = clock_cpu_microsecond(t2 - t1); if RT_USING_CPUTIME config RT_USING_CPUTIME_CORTEXM diff --git a/components/drivers/cputime/cputime.c b/components/drivers/cputime/cputime.c index a607aeefa1..20f41b6dac 100644 --- a/components/drivers/cputime/cputime.c +++ b/components/drivers/cputime/cputime.c @@ -30,8 +30,10 @@ static const struct rt_clock_cputime_ops *_cputime_ops = RT_NULL; /** * The clock_cpu_getres() function shall return the resolution of CPU time, the * number of nanosecond per tick. + * + * @return the number of nanosecond per tick */ -uint32_t clock_cpu_getres(void) +float clock_cpu_getres(void) { if (_cputime_ops) return _cputime_ops->cputime_getres(); @@ -42,6 +44,8 @@ uint32_t clock_cpu_getres(void) /** * The clock_cpu_gettime() function shall return the current value of cpu time tick. + * + * @return the cpu tick */ uint32_t clock_cpu_gettime(void) { @@ -52,6 +56,36 @@ uint32_t clock_cpu_gettime(void) return 0; } +/** + * The clock_cpu_microsecond() fucntion shall return the microsecond according to + * cpu_tick parameter. + * + * @param cpu_tick the cpu tick + * + * @return the microsecond + */ +uint32_t clock_cpu_microsecond(uint32_t cpu_tick) +{ + float unit = clock_cpu_getres(); + + return (cpu_tick * unit) / 1000; +} + +/** + * The clock_cpu_microsecond() fucntion shall return the millisecond according to + * cpu_tick parameter. + * + * @param cpu_tick the cpu tick + * + * @return the millisecond + */ +uint32_t clock_cpu_millisecond(uint32_t cpu_tick) +{ + float unit = clock_cpu_getres(); + + return (cpu_tick * unit) / (1000 * 1000); +} + /** * The clock_cpu_seops() function shall set the ops of cpu time. * diff --git a/components/drivers/cputime/cputime_cortexm.c b/components/drivers/cputime/cputime_cortexm.c index 7db27b4b98..f576df785f 100644 --- a/components/drivers/cputime/cputime_cortexm.c +++ b/components/drivers/cputime/cputime_cortexm.c @@ -30,9 +30,12 @@ /* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */ -static uint32_t cortexm_cputime_getres(void) +static float cortexm_cputime_getres(void) { - return (1000 * 1000 * 1000)/SystemCoreClock; + float ret = 1000 * 1000 * 1000; + + ret = ret / SystemCoreClock; + return ret; } static uint32_t cortexm_cputime_gettime(void) diff --git a/components/drivers/include/drivers/cputime.h b/components/drivers/include/drivers/cputime.h index 54037146bb..c3dd243f11 100644 --- a/components/drivers/include/drivers/cputime.h +++ b/components/drivers/include/drivers/cputime.h @@ -27,13 +27,16 @@ struct rt_clock_cputime_ops { - uint32_t (*cputime_getres) (void); + float (*cputime_getres) (void); uint32_t (*cputime_gettime)(void); }; -uint32_t clock_cpu_getres(void); +float clock_cpu_getres(void); uint32_t clock_cpu_gettime(void); +uint32_t clock_cpu_microsecond(uint32_t cpu_tick); +uint32_t clock_cpu_millisecond(uint32_t cpu_tick); + int clock_cpu_setops(const struct rt_clock_cputime_ops *ops); #endif -- GitLab