# Delay between portEND\_SWITCHING\_ISR & Task Resume

**URL:** <https://forums.freertos.org/t/delay-between-portend-switching-isr-task-resume/14420>\
**Category:** Kernel\
**Created:** [February 9, 2022, 10:24am UTC](https://forums.freertos.org/t/delay-between-portend-switching-isr-task-resume/14420 "2022-02-09T10:24:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdrienC](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@AdrienC](https://forums.freertos.org/u/AdrienC)\
**Post date:** [February 9, 2022, 10:24am UTC](https://forums.freertos.org/t/delay-between-portend-switching-isr-task-resume/14420/1 "2022-02-09T10:24:31Z")

</div>

Hi,  
I try to implement a usDelay function to wait for some hundred of microseconds with semaphore usage.  
For it :

```auto
void TIM3_IRQHandler()
{
    portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
    xSemaphoreHandle sem = NULL;
    if (TIM_GetITStatus (TIM3, TIM_IT_CC1))
    {
        TIM_ClearITPendingBit (TIM3, TIM_IT_CC1);
        TIM_ITConfig (TIM3, TIM_IT_CC1, DISABLE);
        xSemaphoreGiveFromISR (sem, &xHigherPriorityTaskWoken);
        portEND_SWITCHING_ISR (xHigherPriorityTaskWoken);
    }
}

```

From my task i will call this function :

```auto
void delay_us (unsigned short delay)
{
    TIM_ClearITPendingBit (TIM3, TIM_IT_CC1);
    TIM_SetCompare1 (TIM3, TIM3->CNT + delay);
    TIM_ITConfig (TIM3, TIM_IT_CC1, ENABLE);
    xSemaphoreTake (sem, portMAX_DELAY);
}

```

Here the code in the task :

```auto
t1 = TIM3->CNT;
delay_us1 (100);
t2 = TIM3->CNT;
duration = t2-t1;

```

For this example i get a 200us duration ?!?!?!

My timer settings are correct (SYSCLK set to 720000000 & Timer prescaler set to 71).  
I find a huge 100us variable delay between the ISR END Function & my task resume. I don’t understand why, any answers ?  
Please tell me if you need other informations  
Thank’s

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [February 9, 2022, 2:50pm UTC](https://forums.freertos.org/t/delay-between-portend-switching-isr-task-resume/14420/2 "2022-02-09T14:50:35Z")

</div>

What else is your system doing? Does it have any critical sections that will delay the timer interrupt? Which chip are you using? not that it would make much difference to the jitter - you would be better off using a [direct to task notification rather than a semaphore](https://www.freertos.org/2020/09/decrease-ram-footprint-and-accelerate-execution-with-freertos-notifications.html).

---

<div class="post-metadata">

**Author:** ![AdrienC](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@AdrienC](https://forums.freertos.org/u/AdrienC)\
**Post date:** [February 9, 2022, 2:59pm UTC](https://forums.freertos.org/t/delay-between-portend-switching-isr-task-resume/14420/3 "2022-02-09T14:59:41Z")

</div>

I work on a STM32F103ZDT6. I have around 15 Tasks working together without any critical sections.  
I did some tests using only one test task and for a delay of 100us i achieve a real delay of 120us.  
I currently work with FreeRTOS 7.0 & Tasks Notifications are not available under this version. But i think i will need to upgrade my firmware …

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [February 9, 2022, 5:27pm UTC](https://forums.freertos.org/t/delay-between-portend-switching-isr-task-resume/14420/4 "2022-02-09T17:27:34Z")

</div>

> [@AdrienC](#):
>
> ` xSemaphoreHandle sem = NULL;`

Why is the ISR using a different variable for the semaphore handle, and setting it to NULL?

There should be some ‘global’ semaphore handle that has been defined and initialized, (or as Richard Barry suggests, use direct-to-task notification).

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [February 9, 2022, 6:59pm UTC](https://forums.freertos.org/t/delay-between-portend-switching-isr-task-resume/14420/5 "2022-02-09T18:59:04Z")

</div>

Version 7 is very old. The newer the version the more assert points there are to help you identify issues the first time you run the code.

Is the 120us period consistent? What period do you get if you setup the timer interrupt from main() then sit in a loop (in main(), so without making any FreeRTOS API calls)?
