Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npl/riot: Improve timer glue code #753

Merged
merged 2 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions porting/npl/riot/include/nimble/nimble_npl_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct ble_npl_eventq {

struct ble_npl_callout {
xtimer_t timer;
ble_npl_time_t target_ticks;
uint64_t target_us;
struct ble_npl_event e;
event_queue_t *q;
};
Expand Down Expand Up @@ -100,7 +100,8 @@ ble_npl_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo)
} else if (tmo == BLE_NPL_TIME_FOREVER) {
return (struct ble_npl_event *)event_wait(&evq->q);
} else {
return (struct ble_npl_event *)event_wait_timeout(&evq->q, (tmo * 1000));
return (struct ble_npl_event *)event_wait_timeout64(&evq->q,
tmo * US_PER_MS);
}
}

Expand Down Expand Up @@ -215,7 +216,7 @@ ble_npl_callout_is_active(struct ble_npl_callout *c)
static inline ble_npl_time_t
ble_npl_callout_get_ticks(struct ble_npl_callout *co)
{
return co->target_ticks;
return (ble_npl_time_t)(co->target_us / US_PER_MS);
}

static inline void
Expand All @@ -224,10 +225,10 @@ ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
co->e.arg = arg;
}

static inline uint32_t
static inline ble_npl_time_t
ble_npl_time_get(void)
{
return xtimer_now_usec() / 1000;
return (ble_npl_time_t)(xtimer_now_usec64() / US_PER_MS);
}

static inline ble_npl_error_t
Expand Down Expand Up @@ -259,7 +260,7 @@ ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
static inline void
ble_npl_time_delay(ble_npl_time_t ticks)
{
xtimer_usleep(ticks * 1000);
xtimer_usleep64(ticks * US_PER_MS);
}

static inline uint32_t
Expand Down
20 changes: 12 additions & 8 deletions porting/npl/riot/src/npl_os_riot.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
struct timespec abs;
uint64_t time;

time = xtimer_now_usec64() + ble_npl_time_ticks_to_ms32(timeout) * 1000;
abs.tv_sec = (time_t)(time / 1000000);
abs.tv_nsec = (long)((time % 1000000) * 1000);
time = xtimer_now_usec64() +
(ble_npl_time_ticks_to_ms32(timeout) * US_PER_MS);
abs.tv_sec = (time_t)(time / US_PER_SEC);
abs.tv_nsec = (long)((time % US_PER_SEC) * NS_PER_US);

rc = sem_timedwait(&sem->sem, &abs);

Expand All @@ -61,16 +62,19 @@ ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq,
ble_npl_error_t
ble_npl_callout_reset(struct ble_npl_callout *c, ble_npl_time_t ticks)
{
uint32_t now = xtimer_now_usec();
c->target_ticks = (ble_npl_time_t)((now / 1000) + ticks);
xtimer_set(&c->timer, ((ticks * 1000) - ((xtimer_now_usec() - now) / 1000)));
/* Use critical section to ensure matching target_us and xtimer value. */
uint32_t crit_state = ble_npl_hw_enter_critical();
uint64_t now = xtimer_now_usec64();
c->target_us = now + ticks * US_PER_MS;
xtimer_set64(&c->timer, ticks * US_PER_MS);
ble_npl_hw_exit_critical(crit_state);
return BLE_NPL_OK;
}

uint32_t
ble_npl_callout_remaining_ticks(struct ble_npl_callout *co,
ble_npl_time_t time)
{
uint32_t now = xtimer_now_usec();
return ((uint32_t)co->target_ticks) - (now / 1000);
uint64_t now = xtimer_now_usec64();
return (uint32_t)((co->target_us - now) / US_PER_MS);
}