Skip to content

Commit 341f78f

Browse files
committed
[DeviceDrivers] Add more API.
1. Use float type for clock_cpu_getres() return value; 2. Add clock_cpu_microsecond/millisecond APIs.
1 parent 872975b commit 341f78f

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

components/drivers/Kconfig

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ config RT_USING_DEVICE_IPC
77
config RT_USING_SERIAL
88
bool "Using serial device drivers"
99
select RT_USING_DEVICE_IPC
10-
select RT_USING_DEVICE
10+
select RT_USING_DEVICE
1111
default y
1212

1313
config RT_USING_CAN
@@ -24,13 +24,17 @@ config RT_USING_CPUTIME
2424
help
2525
When enable this option, the BSP should provide a rt_clock_cputime_ops
2626
for CPU time by:
27-
clock_cpu_setops(const struct rt_clock_cputime_ops *ops);
27+
const static struct rt_clock_cputime_ops _ops = {...};
28+
clock_cpu_setops(&_ops);
2829

29-
Then developer can use high resolution clock counter with:
30+
Then user can use high resolution clock counter with:
3031

31-
ts = clock_cpu_gettime();
32-
/* The unit of clock_cpu_gettime() can be returned by */
33-
unit = clock_cpu_getres(); /* number for nanosecond */
32+
ts1 = clock_cpu_gettime();
33+
ts2 = clock_cpu_gettime();
34+
35+
/* and get the ms of delta tick with API: */
36+
ms_tick = clock_cpu_millisecond(t2 - t1);
37+
us_tick = clock_cpu_microsecond(t2 - t1);
3438

3539
if RT_USING_CPUTIME
3640
config RT_USING_CPUTIME_CORTEXM

components/drivers/cputime/cputime.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ static const struct rt_clock_cputime_ops *_cputime_ops = RT_NULL;
3030
/**
3131
* The clock_cpu_getres() function shall return the resolution of CPU time, the
3232
* number of nanosecond per tick.
33+
*
34+
* @return the number of nanosecond per tick
3335
*/
34-
uint32_t clock_cpu_getres(void)
36+
float clock_cpu_getres(void)
3537
{
3638
if (_cputime_ops)
3739
return _cputime_ops->cputime_getres();
@@ -42,6 +44,8 @@ uint32_t clock_cpu_getres(void)
4244

4345
/**
4446
* The clock_cpu_gettime() function shall return the current value of cpu time tick.
47+
*
48+
* @return the cpu tick
4549
*/
4650
uint32_t clock_cpu_gettime(void)
4751
{
@@ -52,6 +56,36 @@ uint32_t clock_cpu_gettime(void)
5256
return 0;
5357
}
5458

59+
/**
60+
* The clock_cpu_microsecond() fucntion shall return the microsecond according to
61+
* cpu_tick parameter.
62+
*
63+
* @param cpu_tick the cpu tick
64+
*
65+
* @return the microsecond
66+
*/
67+
uint32_t clock_cpu_microsecond(uint32_t cpu_tick)
68+
{
69+
float unit = clock_cpu_getres();
70+
71+
return (cpu_tick * unit) / 1000;
72+
}
73+
74+
/**
75+
* The clock_cpu_microsecond() fucntion shall return the millisecond according to
76+
* cpu_tick parameter.
77+
*
78+
* @param cpu_tick the cpu tick
79+
*
80+
* @return the millisecond
81+
*/
82+
uint32_t clock_cpu_millisecond(uint32_t cpu_tick)
83+
{
84+
float unit = clock_cpu_getres();
85+
86+
return (cpu_tick * unit) / (1000 * 1000);
87+
}
88+
5589
/**
5690
* The clock_cpu_seops() function shall set the ops of cpu time.
5791
*

components/drivers/cputime/cputime_cortexm.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030

3131
/* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */
3232

33-
static uint32_t cortexm_cputime_getres(void)
33+
static float cortexm_cputime_getres(void)
3434
{
35-
return (1000 * 1000 * 1000)/SystemCoreClock;
35+
float ret = 1000 * 1000 * 1000;
36+
37+
ret = ret / SystemCoreClock;
38+
return ret;
3639
}
3740

3841
static uint32_t cortexm_cputime_gettime(void)

components/drivers/include/drivers/cputime.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727

2828
struct rt_clock_cputime_ops
2929
{
30-
uint32_t (*cputime_getres) (void);
30+
float (*cputime_getres) (void);
3131
uint32_t (*cputime_gettime)(void);
3232
};
3333

34-
uint32_t clock_cpu_getres(void);
34+
float clock_cpu_getres(void);
3535
uint32_t clock_cpu_gettime(void);
3636

37+
uint32_t clock_cpu_microsecond(uint32_t cpu_tick);
38+
uint32_t clock_cpu_millisecond(uint32_t cpu_tick);
39+
3740
int clock_cpu_setops(const struct rt_clock_cputime_ops *ops);
3841

3942
#endif

0 commit comments

Comments
 (0)