-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Closed
Labels
area: KernelbugThe issue is a bug, or the PR is fixing a bugThe issue is a bug, or the PR is fixing a bugpriority: mediumMedium impact/importance bugMedium impact/importance bug
Description
I did some tests for a project that required precise timing and I noticed that k_sleep sleeps one tick too long when using a relative timeout, and one tick too short when using an absolute timeout.
Test code:
targetTicks = k_uptime_ticks() + 2;
while (k_uptime_ticks() < targetTicks);
// we are at the beginning of a tick period now
GPIO_PinOutToggle(PIN_DEBUG);
targetTicks += 4;
retVal = k_sleep(K_TICKS(4)); // case 1
//retVal = k_sleep(K_TIMEOUT_ABS_TICKS(targetTicks)); // case 2
GPIO_PinOutToggle(PIN_DEBUG);
endTicks = k_uptime_ticks();Observed behavior:
k_sleep(K_TICKS(4));
endTicks == targetTicks + 1
retVal == 0
k_sleep(K_TIMEOUT_ABS_TICKS(targetTicks));
endTicks == targetTicks - 1
retVal == -1717986927Looking at the debug pin with a scope confirms this and I see a 520us pulse in case 1, and a 320us pulse in case 2.
The problem seems to come from the following line in z_add_timeout():
k_ticks_t ticks = timeout.ticks + 1;My setup is a efm32gg_stk3701a board (Cortex-M4) with 10kHz ticks, 50MHz clock, no time slicing, 64-bit timeouts enabled.
Metadata
Metadata
Assignees
Labels
area: KernelbugThe issue is a bug, or the PR is fixing a bugThe issue is a bug, or the PR is fixing a bugpriority: mediumMedium impact/importance bugMedium impact/importance bug