# Task notifications

**URL:** <https://forums.freertos.org/t/task-notifications/8434>\
**Category:** Kernel\
**Created:** [December 16, 2019, 8:08pm UTC](https://forums.freertos.org/t/task-notifications/8434 "2019-12-16T20:08:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pnadathur](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pnadathur](https://forums.freertos.org/u/pnadathur)\
**Post date:** [December 16, 2019, 8:08pm UTC](https://forums.freertos.org/t/task-notifications/8434/1 "2019-12-16T20:08:31Z")

</div>

This is a follow up question to [RTOS basics](https://forums.freertos.org/t/rtos-basics/8424)

I am trying to use a new task to relieve the extent of processing in an ISR from another task.  
REPMTask (priority 3)

```auto
    vTaskNotifyGiveFromISR(PRINTERTaskHandle, &printerTaskWoken);

    portYIELD_FROM_ISR(printerTaskWoken);
    ulTaskNotifyTake(pdTRUE, 0);

PrinterTask (priority 2)
void PrinterTask(void * pvParameters)
{
    for(;;){
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
        PRINTHEAD_CountAndPrint();
        xTaskNotifyGive(REPMTaskHandle);
    }

}

```

When I use this combination of priorities and API, the printer task does not print. Other tasks work OK until the ISR tries to switch to printer task. And then nothing else executes.

I still have holes in how I understand this, I appreciate some added pointers.

Thanks,  
Priya

---

<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:** [December 16, 2019, 9:11pm UTC](https://forums.freertos.org/t/task-notifications/8434/2 "2019-12-16T21:11:36Z")

</div>

So if I understand correctly, everything runs as expected until the interrupt that sends a notification to the printer task runs - after which nothing appears to run.

As before, you can run under the control of the debugger, then when the error occurs, pause the debugger to see what is executing. Maybe you are just in an an assert()?

Also, once you print out the message you send a notification to another ask - REPMTaskHandle - what does that task do? Can you show the code for that too please.

You can use three back ticks “```” before and after a code block to have the code formatted as code in the post. I will edit the post above to do that so you can see what I mean.

---

<div class="post-metadata">

**Author:** ![pnadathur](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pnadathur](https://forums.freertos.org/u/pnadathur)\
**Post date:** [December 16, 2019, 9:28pm UTC](https://forums.freertos.org/t/task-notifications/8434/3 "2019-12-16T21:28:45Z")

</div>

Richard,  
Thank you for your reply. Seems like the printer task never gets the notification from the ISR. I stepped into  
`vTaskNotifyGiveFromISR(PRINTERTaskHandle, &printerTaskWoken);`

and it skips this conditional check:  
`if( eOriginalNotifyState == eWaitingNotification )`  
It also did not step into  
`configASSERT( xTaskToNotify );`

As far as what the REPM task do after returning from printer task:

````auto

    portYIELD_FROM_ISR(printerTaskWoken);
    ulTaskNotifyTake(pdTRUE, 0);

  // PRINTHEAD_CountAndPrint();

    REPM_CTRL_CountAndStop();
    if(g_bShortLongTicketDetectionEnabled)
    	{
    	if(g_bShortLongTicketMeasurementStart)
    		RecordTicketLength (TRUE);
    	else
    		RecordTicketLength (FALSE);

    	}```

Thanks,
Priya

			{
````

---

<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:** [December 16, 2019, 10:16pm UTC](https://forums.freertos.org/t/task-notifications/8434/4 "2019-12-16T22:16:47Z")

</div>

What is eOriginalNotifyState set to before the notification is given?

I don’t really udnerstand the REPM task structure - the task should be in an infinite loop as it must not be allowed to run off the end of its implementing function. Also, what is the portYIELD\_FROM\_ISR() call at the top - that should only be called from an interrupt service routine. Finally, what is the call to ulTaskNotifyTake() for? Is that where it waits for the notification - because the block time in your call is 0.

Did you read the book I linked to in your previous thread?

---

<div class="post-metadata">

**Author:** ![pnadathur](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pnadathur](https://forums.freertos.org/u/pnadathur)\
**Post date:** [December 17, 2019, 2:08pm UTC](https://forums.freertos.org/t/task-notifications/8434/5 "2019-12-17T14:08:09Z")

</div>

Richard,  
I am switching tasks from an ISR in the REPM task in order to reduce the extent of processing happening in it.

vTaskNotifyGiveFromISR(PRINTERTaskHandle, &printerTaskWoken);  
portYIELD\_FROM\_ISR(printerTaskWoken);

The structure of the printing task has been posted earlier. When this task is ready to give back control to the REPM task,is the best place to use

ulTaskNotifyTake(pdTRUE, 0);  
the main loop of the REPM\_task?

Initial value of eOriginnalNotifyState is eNotWaitingNotification and then changes to eNotified.

---

<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:** [December 17, 2019, 3:09pm UTC](https://forums.freertos.org/t/task-notifications/8434/6 "2019-12-17T15:09:23Z")

</div>

Trying to piece this together. Is this what you have?

````auto
void vMyISR( void )
{
    // Unblock the printer task implemented by PrinterTask().
    vTaskNotifyGiveFromISR(PRINTERTaskHandle, &printerTaskWoken);

    portYIELD_FROM_ISR(printerTaskWoken);
    ulTaskNotifyTake(pdTRUE, 0); //!! WHAT IS THIS DOING?
}

void PrinterTask(void * pvParameters)
{
    for(;;){
        // Wait for interrupt to notify this task.
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
        
        // Perform operation.
        PRINTHEAD_CountAndPrint();
        
        // Unblock the REPMTask.
        xTaskNotifyGive(REPMTaskHandle);
    }
}

void REPMTask( void * pvParameters )
{
    REPM_CTRL_CountAndStop();
    if(g_bShortLongTicketDetectionEnabled)
        {
        if(g_bShortLongTicketMeasurementStart)
            RecordTicketLength (TRUE);
        else
            RecordTicketLength (FALSE);

        }```
        
    //!! IF THIS IS A TASK THEN MUST NOT DROP OFF
    // THE END OF THE FUNCTION (as documented in
    // the book and website). IF THIS IS JUST A
    // FUNCTION AND NOT A TASK THEN THE LINE:
    // xTaskNotifyGive(REPMTaskHandle); ABOVE WOULD
    // SEEM WRONG.
}

````

---

<div class="post-metadata">

**Author:** ![pnadathur](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pnadathur](https://forums.freertos.org/u/pnadathur)\
**Post date:** [December 17, 2019, 3:21pm UTC](https://forums.freertos.org/t/task-notifications/8434/7 "2019-12-17T15:21:12Z")

</div>

```auto
void RepmTask(void * pvParameters)
{

	for( ;; )
	{
      
        }
void My_ISR(void){
     //CALL THE RELEVENT PRINT ROUTINE
    vTaskNotifyGiveFromISR(PRINTERTaskHandle, &printerTaskWoken);

    portYIELD_FROM_ISR(printerTaskWoken);
    ulTaskNotifyTake(pdTRUE, 0);

  

    	}
}
}

```

My\_ISR happens from REPM\_Task. I switch control to printer task. Printer task returns control to REPM\_task. While I can put all of the ISR processing after the task switch in the printer task, the execution needs to return to whatever happens after the ISR. Or it needs to return to the ISR itself to wrap up the ISR.

Currently the code you have put in the REPM main loop is still inside the ISR, it is possible to move it to the printer task.

I need to understand the right set of APIs to use, where/how to use them. Inside an ISR, what is the best blocking time to use to wait for a return notification?

Thanks,  
Priya

---

<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:** [December 17, 2019, 4:55pm UTC](https://forums.freertos.org/t/task-notifications/8434/8 "2019-12-17T16:55:39Z")

</div>

The code you posted has one function defined inside another function. I’m surprised that even compiles. Are you sure your code is compiling?

In any case, ISRs (Interrupt Service Routines) are asynchronous - they occur when an interrupt is accepted by the processor, at which point the execution flow jumps from whatever was executing to the interrupt service routine which then runs to completion before execution returns to whatever was executing previously. This is a function of the hardware - there is nothing in software that can effect it. Interrupts to not execute in tasks.

I may be mistaken, but from reading your posts it doesn’t seem that you understand how the processor works. I would recommend writing some basic examples with just a main() function that sets up an interrupt and an interrupt handler to learn some of the basics of embedded programming before attempting to use the RTOS - the processor vendor and compiler vendor probably have lots of examples already. Then, once you have the basics, read the book that is freely available for you before you attempt to use the RTOS. The book also contains lots of examples.

---

<div class="post-metadata">

**Author:** ![pnadathur](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@pnadathur](https://forums.freertos.org/u/pnadathur)\
**Post date:** [December 17, 2019, 5:03pm UTC](https://forums.freertos.org/t/task-notifications/8434/9 "2019-12-17T17:03:33Z")

</div>

This is code in production. Thank you for your reply.

---

<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:** [December 18, 2019, 2:40am UTC](https://forums.freertos.org/t/task-notifications/8434/10 "2019-12-18T02:40:51Z")

</div>

The code as presented, with the ISR function defined within the task function is NOT valid C, but maybe some extension you are using.

Ths ISR including the statement ulTaskNotifyTake(pdTRUE, 0); breaks the API rules of FREERTOS (that ISRs are not allowed to call functions that don’t end in FromISR.

If this is the actual code you have in production, it is broken.
