-
Notifications
You must be signed in to change notification settings - Fork 2.1k
xtimer with any value clock frequency #5237
Description
The xtimer module needs some patching for the (rare) case where frequency of the timer peripheral are not a natural one (1 usec) or a power of two of this frequency.
The real case is for example the fixed 80 Mhz frequency of cc3200 [1] timer module.
Below what works for this chip, at least with my preliminary tests, hoping this may help to find a rigorous patch that works for every system.
MACRO for scaling
Redefined and moved the macros XTIMER_USEC_TO_TICKS and XTIMER_TICKS_TO_USEC
into a specific header (boards/cc3200-launchxl/include/periph_conf.h:
#define XTIMER_SHIFT 1
#define XTIMER_USEC_TO_TICKS_FACTOR 80
#define XTIMER_USEC_TO_TICKS(value) ( value*XTIMER_USEC_TO_TICKS_FACTOR )
#define XTIMER_TICKS_TO_USEC(value) ( value/XTIMER_USEC_TO_TICKS_FACTOR )
Syncronize wrapping of natural and hw counter
Aligned the wrap of natural counter (1 usec based) with the wrapping of the system timer (1/80 usec based)
#ifdef XTIMER_SHIFT
timer->target = (target - XTIMER_OVERHEAD)%(0xffffffff/XTIMER_USEC_TO_TICKS_FACTOR);
#else
timer->target = target - XTIMER_OVERHEAD; //!!! to improve timer precision
//timer->target = target; //!!! this is RIOT original
#endif
The only place where I did this is into xtimer_core.c/_xtimer_set_absolute function. I think some reviewing by the timer task force has to be done to confirm that this is correct.
As another minor consideration, not related to this issue, please see the //!!! comments for a suggested patch to improve the precision of timers as it comes out for my measures.