# Task Notification not working as expected

**URL:** <https://forums.freertos.org/t/task-notification-not-working-as-expected/15857>\
**Category:** Kernel\
**Created:** [September 17, 2022, 9:48pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857 "2022-09-17T21:48:54Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 17, 2022, 9:48pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/1 "2022-09-17T21:48:54Z")

</div>

I’m using Task Notifications to speed up SPI transfers in my system which previously used queues. Surprisingly there was no difference. I have a task (Task1) using SPI bus to update a DAC, this needs to happen as fast as possible, it sends data to the SPI device driver then calls ulNotifyTake(). The SPI device driver initiates the SPI transfer and then the SPI device driver ISR calls vTaskNotifyGiveFromISR() when the SPI transfer is complete. I’m watching the SPI transfers on an oscilloscope and it takes about 10us. In Task1 I set a GPIO bit High just before starting the SPI transfer and then set the GPIO bit Low after ulNotifyTake() returns. Monitoring the GPIO pulse shows it is 20ms which happens to be the length of one tick. I changed the tick rate so the tics are 10ms and then the GPIO pulse is10ms. So I’m puzzled by this because I expected the GPIO pulse to be a little longer than the 10us the SPI transfer takes, because the only tasks are Task1 and the Idle task? Can anyone explain this behavior to me?  
Thanks, John

---

<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:** [September 17, 2022, 10:36pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/2 "2022-09-17T22:36:36Z")

</div>

A couple of thoughts:

1. Are you using the pxHigherPriorityTaskWoken parameter to [vTaskNotifyGiveFromISR()](https://www.freertos.org/vTaskNotifyGiveFromISR.html). See the example source code on the linked page. If not the context switch won’t occur until the next tick interrupt.
2. Is the task you are notifying the highest priority task in the system at that time? If not, it won’t run until it is.

Using a trace tool such as Percepio’s Tracealyzer or Segger’s System view would show you what the MCU was executing.

---

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 19, 2022, 3:20pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/3 "2022-09-19T15:20:50Z")

</div>

Thanks for your response. Yes I’m using the pxHigherPriorityTaskWoken parameter, and Yes the task I’m notifying is the highest priority task in the system. It appears to me that although I’m calling vTakNotifyGiveFromISR() the context switch doesn’t occur until the next tick, so I’m assuming the Idle task is running (since it is the only other task) until the next tick. I thought that calling vTaskNotifyGiveFromISR() would force a context switch after the ISR exits? With this behavior the fastest I can update the DACs is the tick rate which is too slow.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [September 19, 2022, 3:30pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/4 "2022-09-19T15:30:54Z")

</div>

It’s exactly as you expect and as documented.  
I guess you’ve enabled [configUSE\_PREEMPTION](https://www.freertos.org/a00110.html#configUSE_PREEMPTION), right ?  
BTW Which MCU/FreeRTOS port and version do you use ?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 19, 2022, 3:35pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/5 "2022-09-19T15:35:04Z")

</div>

Also, please share your ISR code and FreeRTOSConfig.h.

---

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 19, 2022, 4:10pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/6 "2022-09-19T16:10:33Z")

</div>

Yes, configUSE\_PREEMPTION = 1, and I’m using Xilinx/AMD ZYNQ with the FreeRTOS port included in the Xilinx Vitus tools which I beleive is version 10.3.1.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [September 19, 2022, 4:16pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/7 "2022-09-19T16:16:42Z")

</div>

Silly question but is `*pxHigherPriorityTaskWoken` set to `pdTRUE` (1) by `vTaskNotifyGiveFromISR` as expected ?

---

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 19, 2022, 4:22pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/8 "2022-09-19T16:22:51Z")

</div>

Yes, when I set a breakpoint in the ISR pxHigherPriorityTaskWoken is set to true after vTaskNotiryGiveFromISR() returns.

---

<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:** [September 19, 2022, 4:46pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/9 "2022-09-19T16:46:22Z")

</div>

Does your ISR end with the test of the flag and a call to the yield function? (Or a call to the end ISR macro that does that)

THAT is what causes the context switch.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 19, 2022, 4:49pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/10 "2022-09-19T16:49:44Z")

</div>

In other words, do you have the following line at the end of your ISR:

```nohighlight
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );

```

---

<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:** [September 19, 2022, 4:50pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/11 "2022-09-19T16:50:33Z")

</div>

…as shown in the code at the end of [https://www.freertos.org/RTOS-Xilinx-Zynq.html#ConfigAndUsage](https://www.freertos.org/RTOS-Xilinx-Zynq.html#ConfigAndUsage)

---

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 19, 2022, 5:48pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/12 "2022-09-19T17:48:02Z")

</div>

Yes, I call portYIELD\_FROM\_ISR( xHigherPriorityTaskWoken ) at the end of the ISR.

---

<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:** [September 19, 2022, 5:51pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/13 "2022-09-19T17:51:21Z")

</div>

As per Gaurav’s request - please post the minimal ISR code that demonstrates the issue, and the FreeRTOSConfig.h. We may need to take a trace.

---

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 19, 2022, 6:05pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/14 "2022-09-19T18:05:06Z")

</div>

I think I’ve found the problem, trying to verify it now. I’ll keep you posted. All your help is very much appreciated.  
John

---

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 19, 2022, 9:37pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/15 "2022-09-19T21:37:15Z")

</div>

Found the problem (my fault) and got it fixed, everything is working now as expected. Thanks again to everyone who responded.  
John

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 20, 2022, 5:01am UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/16 "2022-09-20T05:01:10Z")

</div>

Glad that you found the issue. It will be useful for others too if you can post your solution too.

---

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 20, 2022, 4:09pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/17 "2022-09-20T16:09:50Z")

</div>

Well it’s complicated, but long story short my FreeRTOS tasks are all C++ classes, my hardware drivers are all C++ classes. So a hardware driver that has an ISR has a static ISR function that’s not part of the class that gets passed the this pointer to the class from the interrupt handler. This static ISR uses the this pointer to call the ISR that is a class member function. Unfortunately the FreeRTOS macro portYIELD\_FROM\_ISR() can’t be used inside the class ISR member function, this macro also uses the FreeRTOS macro port\_END\_SWITCHING\_ISR which accesses the FreeRTOS global variable ulPortYieldRequired which is set depending on the value of xHigherPriorityTaskWoken, but ulPortYieldRequired is not in the namespace of the class ISR. So the problem I had was the way I was calling portYIELD\_FROM\_ISR(). This call has to be made in the static ISR function not the class member ISR function. Once I resolved this problem everything works beautifully. All the questions raised here by everyone made me realize that portYIELD\_FROM\_ISR was not getting passed the xHigherPriorityTaskWoken from the class ISR so the task switch was never occurring until the next tick. Again, many thanks to all for leading me down the path to a solution.  
John

---

<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:** [September 20, 2022, 4:36pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/18 "2022-09-20T16:36:00Z")

</div>

I use something similar, and let me make a few comments.

Port\_END\_SWITCHI\_ISR does NOT us a global, but the parameter passed to it, which should not refer to a global, but a local variable specific to that ISR. Sharing a variable between ISRs can cause loss of the need to context switch, as you clear that variable on entry to the ISR.  
Second, I make the class member ISR function return the value of it xHigherPriorityTaskWoken flag from inside the isr, and the main ISR function directly called by the interrupt is basically something like:

```auto
void UART0_IRQHandler(void) { portEND_SWITCHI_ISR(uart0.isr()); }

```

UART0\_IRQHandler might need a decorator to mark it as an ISR, depending on the processor.

---

<div class="post-metadata">

**Author:** ![john\_c\_kennedy](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@john\_c\_kennedy](https://forums.freertos.org/u/john_c_kennedy)\
**Post date:** [September 20, 2022, 5:59pm UTC](https://forums.freertos.org/t/task-notification-not-working-as-expected/15857/19 "2022-09-20T17:59:51Z")

</div>

Yep, that’s basically what I’m doing. ulPortYieldRequired is defined in port.c and declared extern in portEND\_SWITCHING\_ISR. So yes the xHigherPiorityTaskWoken variable is local to each ISR but it is used to update ulPortYieldRequired. So everyone that uses the macro has access to it, that makes it global to me.
