@page page_device_clock_hrtimer Clock HRTimer
The clock hrtimer layer provides high-resolution timeout scheduling on top of clock_time. It keeps a sorted timeout list, programs the next deadline, and runs callbacks when the deadline expires.
void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer,
const char *name,
rt_uint8_t flag,
void (*timeout)(void *parameter),
void *parameter);
rt_err_t rt_clock_hrtimer_start(rt_clock_hrtimer_t timer, unsigned long cnt);
rt_err_t rt_clock_hrtimer_stop(rt_clock_hrtimer_t timer);
rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg);
rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer);
void rt_clock_hrtimer_delay_init(struct rt_clock_hrtimer *timer);
void rt_clock_hrtimer_delay_detach(struct rt_clock_hrtimer *timer);
rt_err_t rt_clock_hrtimer_sleep(struct rt_clock_hrtimer *timer, unsigned long cnt);
rt_err_t rt_clock_hrtimer_ndelay(struct rt_clock_hrtimer *timer, unsigned long ns);
rt_err_t rt_clock_hrtimer_udelay(struct rt_clock_hrtimer *timer, unsigned long us);
rt_err_t rt_clock_hrtimer_mdelay(struct rt_clock_hrtimer *timer, unsigned long ms);
Flags reuse RT_TIMERFLAG* definitions (one-shot/periodic/hard timer). cnt
is a counter delta based on the default clock source.
void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer,
const char *name,
rt_uint8_t flag,
void (*timeout)(void *parameter),
void *parameter);
timer: the hrtimer object to initialize.name: timer name (truncated to RT_NAME_MAX-1).flag: RT_TIMERFLAG* (one-shot/periodic/hard timer).timeout: callback function on expiry.parameter: user parameter passed to the callback.rt_clock_hrtimer_start() to arm it.rt_err_t rt_clock_hrtimer_start(rt_clock_hrtimer_t timer, unsigned long cnt);
cnt counter ticks.cnt: relative delay in clock source counter units.cnt.cnt must be less than half of the maximum counter range to avoid wrap
ambiguity.rt_err_t rt_clock_hrtimer_stop(rt_clock_hrtimer_t timer);
rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg);
delay_cnt into *(unsigned long *)arg.delay_cnt from *(unsigned long *)arg.rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer);
rt_clock_hrtimer_sleep() waiters with
an error code.void rt_clock_hrtimer_delay_init(struct rt_clock_hrtimer *timer);
void rt_clock_hrtimer_delay_detach(struct rt_clock_hrtimer *timer);
rt_clock_hrtimer_delay_init().rt_err_t rt_clock_hrtimer_sleep(struct rt_clock_hrtimer *timer, unsigned long cnt);
timer must be initialized (typically by delay_init).cnt is zero.rt_err_t rt_clock_hrtimer_ndelay(struct rt_clock_hrtimer *timer, unsigned long ns);
rt_err_t rt_clock_hrtimer_udelay(struct rt_clock_hrtimer *timer, unsigned long us);
rt_err_t rt_clock_hrtimer_mdelay(struct rt_clock_hrtimer *timer, unsigned long ms);
rt_clock_hrtimer_sleep().#include <drivers/clock_time.h>
static struct rt_clock_hrtimer demo_timer;
static void demo_timeout(void *parameter)
{
RT_UNUSED(parameter);
rt_kprintf("hrtimer timeout\n");
}
static void demo_hrtimer_start(void)
{
rt_uint64_t ns = 5ULL * 1000 * 1000; /* 5 ms */
unsigned long cnt = (unsigned long)rt_clock_time_ns_to_counter(ns);
rt_clock_hrtimer_init(&demo_timer, "demo", RT_TIMER_FLAG_ONE_SHOT,
demo_timeout, RT_NULL);
rt_clock_hrtimer_start(&demo_timer, cnt);
}
static void demo_hrtimer_sleep(void)
{
struct rt_clock_hrtimer timer;
rt_clock_hrtimer_delay_init(&timer);
rt_clock_hrtimer_mdelay(&timer, 10);
rt_clock_hrtimer_delay_detach(&timer);
}