# ulTaskNotifyTake timeout accuracy

**URL:** <https://forums.freertos.org/t/ultasknotifytake-timeout-accuracy/9629>\
**Category:** Kernel\
**Created:** [April 24, 2020, 5:41pm UTC](https://forums.freertos.org/t/ultasknotifytake-timeout-accuracy/9629 "2020-04-24T17:41:54Z")\
**Posts on this page:** 1\
**Showing post:** 37

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [May 7, 2020, 6:02am UTC](https://forums.freertos.org/t/ultasknotifytake-timeout-accuracy/9629/37 "2020-05-07T06:02:14Z")

</div>

Here’s what seems to be happening. There is _another_ edge case not handled properly in `vPortSuppressTicksAndSleep()`. If something other than systick wakes the CPU, and if the time remaining until the next tick is very small, then a very tiny value is loaded into the reload register [here](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/bac101c98869c7716700d1549f064e02a892cd78/portable/GCC/ARM_CM4F/port.c#L656). Then the systick expires while `vTaskStepTick()` executes [here](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/bac101c98869c7716700d1549f064e02a892cd78/portable/GCC/ARM_CM4F/port.c#L664), which pends the systick ISR **and starts another very small systick period**. While this second period is underway, we finally load the standard value into the reload register [here](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/bac101c98869c7716700d1549f064e02a892cd78/portable/GCC/ARM_CM4F/port.c#L665). Then we unmask interrupts, at which point the pending systick ISR executes, and then very soon thereafter another systick interrupt occurs from the second very small systick period ending.

What’s interesting about this edge case is that there is a small window of reload values that can cause the error. Only values larger than e.g. 2 and smaller than e.g. 10 would ever cause the error. But in this case the ITM usage, or `traceINCREASE_TICK_COUNT()` usage, in `vTaskStepTick()` would expand the error window a bit because those things delay our loading the correct value into the reload register.

For a quick experiment and fix, can you swap the last two statements:

Current Code:

```auto
portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
vTaskStepTick( ulCompleteTickPeriods );
portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;

```

Proposed (testing only):

```auto
portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
vTaskStepTick( ulCompleteTickPeriods );

```

---

_[View the full topic](https://forums.freertos.org/t/ultasknotifytake-timeout-accuracy/9629)._
