Skip to content

Commit

Permalink
解决ktime部分函数计算时unsigned long溢出
Browse files Browse the repository at this point in the history
  • Loading branch information
ComerLater committed May 29, 2024
1 parent f179ce1 commit 3391a72
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ AlignConsecutiveBitFields:
AlignCompound: true
PadOperators: true
AlignConsecutiveDeclarations:
Enabled: false
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
Expand Down
14 changes: 7 additions & 7 deletions components/drivers/ktime/inc/ktime.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct rt_ktime_hrtimer
{
struct rt_object parent; /**< inherit from rt_object */
rt_list_t row;
void *parameter;
void *parameter;
unsigned long init_cnt;
unsigned long timeout_cnt;
rt_err_t error;
Expand Down Expand Up @@ -62,21 +62,21 @@ rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts);
*
* @return (resolution * RT_KTIME_RESMUL)
*/
unsigned long rt_ktime_cputimer_getres(void);
rt_uint64_t rt_ktime_cputimer_getres(void);

/**
* @brief Get cputimer frequency
*
* @return frequency
*/
unsigned long rt_ktime_cputimer_getfrq(void);
rt_uint64_t rt_ktime_cputimer_getfrq(void);

/**
* @brief Get cputimer the value of the cnt counter
*
* @return cnt
*/
unsigned long rt_ktime_cputimer_getcnt(void);
rt_uint64_t rt_ktime_cputimer_getcnt(void);

/**
* @brief Get cputimer the cnt value corresponding to 1 os tick
Expand All @@ -96,21 +96,21 @@ void rt_ktime_cputimer_init(void);
*
* @return (resolution * RT_KTIME_RESMUL)
*/
unsigned long rt_ktime_hrtimer_getres(void);
rt_uint64_t rt_ktime_hrtimer_getres(void);

/**
* @brief Get hrtimer frequency
*
* @return frequency
*/
unsigned long rt_ktime_hrtimer_getfrq(void);
rt_uint64_t rt_ktime_hrtimer_getfrq(void);

/**
* @brief Get hrtimer the value of the cnt counter
*
* @return cnt
*/
unsigned long rt_ktime_hrtimer_getcnt(void);
rt_uint64_t rt_ktime_hrtimer_getcnt(void);

/**
* @brief set hrtimer timeout, when timeout, the timer callback will call timeout
Expand Down
10 changes: 5 additions & 5 deletions components/drivers/ktime/src/aarch64/cputimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
#include "gtimer.h"
#include "ktime.h"

static volatile unsigned long _init_cnt = 0;
static volatile rt_uint64_t _init_cnt = 0;

unsigned long rt_ktime_cputimer_getres(void)
rt_uint64_t rt_ktime_cputimer_getres(void)
{
return ((1000UL * 1000 * 1000) * RT_KTIME_RESMUL) / rt_hw_get_gtimer_frq();
return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / rt_hw_get_gtimer_frq();
}

unsigned long rt_ktime_cputimer_getfrq(void)
rt_uint64_t rt_ktime_cputimer_getfrq(void)
{
return rt_hw_get_gtimer_frq();
}

unsigned long rt_ktime_cputimer_getcnt(void)
rt_uint64_t rt_ktime_cputimer_getcnt(void)
{
return rt_hw_get_cntpct_val() - _init_cnt;
}
Expand Down
18 changes: 9 additions & 9 deletions components/drivers/ktime/src/boottime.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@

#include "ktime.h"

#define __KTIME_MUL ((1000UL * 1000 * 1000) / RT_TICK_PER_SECOND)
#define __KTIME_MUL ((1000ULL * 1000 * 1000) / RT_TICK_PER_SECOND)

rt_weak rt_err_t rt_ktime_boottime_get_us(struct timeval *tv)
{
RT_ASSERT(tv != RT_NULL);

unsigned long ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;

tv->tv_sec = ns / (1000UL * 1000 * 1000);
tv->tv_usec = (ns % (1000UL * 1000 * 1000)) / 1000;
tv->tv_sec = ns / (1000ULL * 1000 * 1000);
tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000;

return RT_EOK;
}
Expand All @@ -28,9 +28,9 @@ rt_weak rt_err_t rt_ktime_boottime_get_s(time_t *t)
{
RT_ASSERT(t != RT_NULL);

unsigned long ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;

*t = ns / (1000UL * 1000 * 1000);
*t = ns / (1000ULL * 1000 * 1000);

return RT_EOK;
}
Expand All @@ -39,10 +39,10 @@ rt_weak rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts)
{
RT_ASSERT(ts != RT_NULL);

unsigned long ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;
rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL;

ts->tv_sec = ns / (1000UL * 1000 * 1000);
ts->tv_nsec = ns % (1000UL * 1000 * 1000);
ts->tv_sec = ns / (1000ULL * 1000 * 1000);
ts->tv_nsec = ns % (1000ULL * 1000 * 1000);

return RT_EOK;
}
8 changes: 4 additions & 4 deletions components/drivers/ktime/src/cputimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

#include "ktime.h"

rt_weak unsigned long rt_ktime_cputimer_getres(void)
rt_weak rt_uint64_t rt_ktime_cputimer_getres(void)
{
return ((1000UL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND;
return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND;
}

rt_weak unsigned long rt_ktime_cputimer_getfrq(void)
rt_weak rt_uint64_t rt_ktime_cputimer_getfrq(void)
{
return RT_TICK_PER_SECOND;
}

rt_weak unsigned long rt_ktime_cputimer_getcnt(void)
rt_weak rt_uint64_t rt_ktime_cputimer_getcnt(void)
{
return rt_tick_get();
}
Expand Down
10 changes: 5 additions & 5 deletions components/drivers/ktime/src/risc-v/virt64/cputimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@

#include "ktime.h"

static volatile unsigned long _init_cnt = 0;
static volatile rt_uint64_t _init_cnt = 0;

unsigned long rt_ktime_cputimer_getres(void)
rt_uint64_t rt_ktime_cputimer_getres(void)
{
return ((1000UL * 1000 * 1000) * RT_KTIME_RESMUL) / CPUTIME_TIMER_FREQ;
return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / CPUTIME_TIMER_FREQ;
}

unsigned long rt_ktime_cputimer_getfrq(void)
rt_uint64_t rt_ktime_cputimer_getfrq(void)
{
return CPUTIME_TIMER_FREQ;
}

unsigned long rt_ktime_cputimer_getcnt(void)
rt_uint64_t rt_ktime_cputimer_getcnt(void)
{
unsigned long time_elapsed;
__asm__ __volatile__("rdtime %0" : "=r"(time_elapsed));
Expand Down

0 comments on commit 3391a72

Please sign in to comment.