Skip to content

Commit

Permalink
qemu增加了hrt_absolute_time和rt_hw_us_delay函数
Browse files Browse the repository at this point in the history
  • Loading branch information
ComerLater committed May 8, 2024
1 parent 75f5a07 commit 834c2bc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 24 deletions.
10 changes: 10 additions & 0 deletions bsps/sitl/qemu/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Available style options are described in https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
# An easy way to create the .clang-format file is:
#
# clang-format -style=llvm -dump-config > .clang-format
#
---
Language: Cpp
DisableFormat: true
---
43 changes: 41 additions & 2 deletions bsps/sitl/qemu/drivers/drv_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,46 @@ void timer_clear_pending(int timer)
}
}

uint64_t hrt_absolute_time()
{
const uint32_t SystemCoreClock = 1000000ULL;

uint64_t us = rt_tick_get_millisecond() * 1000UL;

uint32_t reload = TIMER_LOAD(TIMER_HW_BASE);
uint32_t tvalue = TIMER_VALUE(TIMER_HW_BASE);

us += 1000000ULL * (reload - tvalue) / SystemCoreClock;

return us;
}

void rt_hw_us_delay(rt_uint32_t us)
{
rt_thread_mdelay(us / 1000);
}
rt_uint32_t ticks;
rt_uint32_t told, tnow, tcnt = 0;

rt_uint32_t reload = TIMER_LOAD(TIMER_HW_BASE);
ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
told = TIMER_VALUE(TIMER_HW_BASE);
while (1)
{
tnow = TIMER_VALUE(TIMER_HW_BASE);
if (tnow != told)
{
if (tnow < told)
{
tcnt += told - tnow;
}
else
{
tcnt += reload - tnow + told;
}
told = tnow;
if (tcnt >= ticks)
{
break;
}
}
}
}
23 changes: 13 additions & 10 deletions pkgs/hrtimer/V3/hrt_using_ostick.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static rt_timer_t _timer = NULL;
* Fetch a never-wrapping absolute time value in microseconds from
* some arbitrary epoch shortly after system start.
*/
hrt_abstime hrt_absolute_time(void) {
RT_WEAK hrt_abstime hrt_absolute_time(void) {
uint64_t tick = rt_tick_get();
return 1000000ULL * tick / RT_TICK_PER_SECOND;
}
Expand All @@ -29,27 +29,30 @@ static void hrt_tim_timeout(void *param) {
hrt_tim_isr(1);
}

int hrt_tim_control(int cmd, void *arg) {
int hrt_tim_expiry(uint32_t timeout_us) {
if (!_timer) {
return -1;
}

rt_err_t ret = 0;

if (cmd == RT_TIMER_CTRL_SET_TIME) {
if (_timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) {
ret += rt_timer_stop(_timer);
}
ret += rt_timer_control(_timer, cmd, arg);
ret += rt_timer_start(_timer);
} else {
ret += rt_timer_control(_timer, cmd, arg);

if (_timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) {
ret += rt_timer_stop(_timer);
}

rt_int32_t timeout_tick = rt_tick_from_millisecond(timeout_us / 1000);

ret += rt_timer_control(_timer, RT_TIMER_CTRL_SET_TIME, &timeout_tick);
ret += rt_timer_start(_timer);


return ret;
}

int hrt_tim_init(void) {
_timer = rt_timer_create("hrtimer", hrt_tim_timeout, NULL, 1, RT_TIMER_FLAG_ONE_SHOT);
return 0;
}

INIT_SUBSYS_EXPORT(hrt_tim_init);
24 changes: 20 additions & 4 deletions pkgs/hrtimer/V3/hrt_using_systick.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,30 @@ static void hrt_tim_timeout(void *param) {
hrt_tim_isr(1);
}

int hrt_tim_control(int cmd, void *arg) {
if (_timer) {
return rt_timer_control(_timer, cmd, arg);
int hrt_tim_expiry(uint32_t timeout_us) {
if (!_timer) {
return -1;
}
return -1;

rt_err_t ret = 0;


if (_timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) {
ret += rt_timer_stop(_timer);
}

rt_int32_t timeout_tick = rt_tick_from_millisecond(timeout_us / 1000);

ret += rt_timer_control(_timer, RT_TIMER_CTRL_SET_TIME, &timeout_tick);
ret += rt_timer_start(_timer);


return ret;
}

int hrt_tim_init(void) {
_timer = rt_timer_create("hrtimer", hrt_tim_timeout, NULL, 1, RT_TIMER_FLAG_ONE_SHOT);
return 0;
}

INIT_SUBSYS_EXPORT(hrt_tim_init);
2 changes: 1 addition & 1 deletion pkgs/hrtimer/V3/hrtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ static void hrt_call_reschedule() {
latency_baseline = deadline;

// Remove the existing expiry and update with the new expiry
hrt_tim_control(RT_TIMER_CTRL_SET_TIME, deadline - now);
hrt_tim_expiry(deadline - now);
}

static void hrt_latency_update(void) {
Expand Down
9 changes: 2 additions & 7 deletions pkgs/hrtimer/V3/hrtimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,9 @@ __EXPORT extern void hrt_call_init(struct hrt_call *entry);
__EXPORT extern void hrt_call_delay(struct hrt_call *entry, hrt_abstime delay);

/*
* Initialise the HRT.
* update with the new expiry
*/
__EXPORT extern void hrt_init(void);

/*
* Initialise the HRT ioctl (user mode access to HRT).
*/
__EXPORT extern void hrt_ioctl_init(void);
__EXPORT extern int hrt_tim_expiry(uint32_t timeout_us);

#if defined(ENABLE_LOCKSTEP_SCHEDULER)

Expand Down

0 comments on commit 834c2bc

Please sign in to comment.