View on Github

Timer / counter

Timer / counter driver support for hardware abstraction layer.

Minimalist code

In timer mode, with callback handler :

void timer100ms_fn(void)
{
    puts("time out");
}

// init timer
rt_dev_t timer = timer_getFreeDevice();
timer_setPeriodMs(timer, 100);
timer_setHandler(timer, timer100ms_fn);
timer_enable(timer);                       // timer100ms_fn function will be called every 100ms

In counter mode :

// init timer
rt_dev_t timer = timer_getFreeDevice();
timer_setCounter(timer, 1);     // no divider
timer_enable(timer);            // start to count

uint16_t value;
value = timer_value(timer);     // get count
timer_clearValue(timer);        // clear count

API

Device assignation

timer_getFreeDevice

rt_dev_t timer_getFreeDevice();

Gives a free timer device number

timer_open

int timer_open(rt_dev_t device);

Opens a timer

timer_close

int timer_close(rt_dev_t device);

Closes a timer

timer_isOpened

bool timer_isOpened(rt_dev_t device)

Timer sdk state

Device enable/disable

timer_enable

int timer_enable(rt_dev_t device);

Enables the specified timer device

timer_disable

int timer_disable(rt_dev_t device);

Disables the specified timer device

timer_isEnabled

bool timer_isEnabled(rt_dev_t device);

Timer sdk enabled state

Timer mode

timer_setPeriodMs

int timer_setPeriodMs(rt_dev_t device, uint32_t periodMs);

Sets the period in ms of the timer module to work in timer mode

timer_periodMs

uint32_t timer_periodMs(rt_dev_t device);

Returns the current period in ms

timer_setPeriodUs

int timer_setPeriodUs(rt_dev_t device, uint32_t periodUs);

Sets the period in us of the timer module to work in timer mode

timer_periodMs

uint32_t timer_periodUs(rt_dev_t device);

Returns the current period in us

timer_setPeriod

int timer_setPeriodUs(rt_dev_t device, uint32_t period);

Sets the period in low level units. If the period is greatter than the PR register size, the pre divider is adapted

timer_period

uint32_t timer_period(rt_dev_t device);

Returns the current period in low level units

Counter mode

timer_setCounter

int timer_setCounter(rt_dev_t device, uint16_t diviser);

Returns the current value of timer

timer_clearValue

int timer_clearValue(rt_dev_t device);

Reset the value of counter

timer_setValue

int timer_setValue(rt_dev_t device, uint16_t value);

Sets the current value of timer

timer_value

uint16_t timer_value(rt_dev_t device);

Returns the current value of timer

Development status

TODO

Supported architectures

Header file : timer.h