Benjamin Walsh <benjamin.walsh@...>
I’m developing an app which requires fast sampling rate (~500 timesWhen we changed the new API to take ms instead of kernel ticks for
timeouts, we also decided the timeouts mean "wait for at least this
time" instead of "wait for at most this time".
The system is still tick-based though. So we convert ms to ticks
If you want to wait "at most" an amount of time, you have to ask for
one tick less. So if you know your tick rate is 100Hz, and you want to
wait at most 20ms, you have to ask for 10ms (that would give you two
Now, you say your sampling rate is 500Hz: however, the default tick rate
is 100Hz. You have to change CONFIG_SYS_CLOCK_TICKS_PER_SEC to 500.
However (again), since with a tick freq of 500Hz, if you wait for 2ms
you'll wait for "at least" 2ms, you might wait for 4ms. So what you
probably want is a CONFIG_SYS_CLOCK_TICKS_PER_SEC of 1000, and wait for
1ms, which will make you wait at most for 2ms.
I'm starting to wonder if we should have macros for this in the API,
e.g. AT_MOST()/AT_LEAST(), where you could do:
k_timer_start(&my_timer, AT_MOST(INTERVAL), 0);
This is all because the kernel is still tick-based. We would like to
move to a tickless kernel, where these would not be an issue anymore.
You cannot use K_FOREVER in this API: if you do not want periodic
repetition, you have to use 0.
I'm surprised this did not blow up. Actually, if you ran with
CONFIG_ASSERT=y, you would have hit the one at the top of
__ASSERT(timeout_in_ticks > 0, "");
k_timer_status_sync(&my_timer);You're getting these numbers because your tick rate is probably 100.
With 1000 you would probably get:
* INTERVAL = 1000 (one second)
Count 1000, delta = 1001
Count 2000, delta = 1001
Count 3000, delta = 1001
* INTERVAL = 100 (one hundred millisecs)
Count 1000, delta = 1010
Count 2000, delta = 1010
Count 3000, delta = 1010
* INTERVAL = 10 (ten millisecs)
Count 1000, delta = 1100
Count 2000, delta = 1100
Count 3000, delta = 1100
* INTERVAL = 1 (one millisec)
Count 1000, delta = 2000
Count 2000, delta = 2000
Count 3000, delta = 2000
Benjamin Walsh, SMTS
WR VxWorks Virtualization Profile
Zephyr kernel maintainer