Drivers: timers

As with every peripheral, you need to init the subsystem before using it. Do that with timer_init().

timer_delay(ms)

If you need to just wait some time, call:

#include <drv/timer.h>

timer_delay(time);

with the number of milliseconds you want to wait. This call will automatically yield the CPU to other processes if you're using the kernel.

In addition, BeRTOS supports timers with callbacks. Define a Timer structure and call timer_setSoftint() to provide a callback that will be called when the timer expires.

Timer t;

void callback(void)
{
	kputs("timer expired\n");
	timer_add(&t);
}

int main()
{
	timer_init();

	// set the callback
	timer_setSoftint(&t, callback);
	// expire time: 1s
	timer_setDelay(&t, ms_to_ticks(1000));
	// start the timer
	timer_add(&t);
}

By default, BeRTOS timers are not recurrent, so you need to add them again when they expire (check callback() code). See also timer.h documentation.

 Download as PDF