# context switch from the void vApplicationTickHook(void) callback function

**URL:** <https://forums.freertos.org/t/context-switch-from-the-void-vapplicationtickhook-void-callback-function/3640>\
**Category:** Kernel\
**Created:** [October 27, 2015, 10:32am UTC](https://forums.freertos.org/t/context-switch-from-the-void-vapplicationtickhook-void-callback-function/3640 "2015-10-27T10:32:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lz1msz](https://avatars.discourse-cdn.com/v4/letter/l/e95f7d/32.png) [@lz1msz](https://forums.freertos.org/u/lz1msz)\
**Post date:** [October 27, 2015, 10:32am UTC](https://forums.freertos.org/t/context-switch-from-the-void-vapplicationtickhook-void-callback-function/3640/1 "2015-10-27T10:32:35Z")

</div>

**[lz1msz](https://sourceforge.net/u/lz1msz/)** wrote on [Tuesday, October 27, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/a27ca295/#dfe5):

We are in charge for development of project, based on FreeRTOS VER 8.2.0

Details:  
MCU: PIC24EP family /PIC24EP512GU810/  
Compiler: XC16 1.21 PRO mode, all optimization enabled  
FreeRTOS version: 8.2.0

We are using 5 tasks and several ISRs, but due to behavior issue, need to clarify our ISRs coding:

1. Is it RTOS save to switch the context in the callback function: vApplicationTickHook(void)  
Due to it is valuable to use it as a 1mS tick timer, we need to send message queue to task and wake-up the task. My question is: Is this context switch RTOS save code for the tick hook. I add the source code inside the vApplicationTickHook(void) function:

void vApplicationTickHook(void)  
{  
// Local variables:  
GOLMSG pMsg;  
GRAPHICSMESSAGE MyGraphicsMessage;  
BaseTypet xHigherPriorityTaskWoken;  
unsigned char PortYieldFlag;

```
PortYieldFlag = 0;

// Process outgoing messages to the graphics task:
TouchDetectPosition(); // Update the current coordinates
TouchGetMsg(&pMsg); // Decide if the new points generate touch message

// If there is touch message received, send it to the graphics task:
if(pMsg.type != EVENTINVALID) // Received is valid message
{
    MyGraphicsMessage.MessageCommand = GRAPHICSTASKTOUCSCREENMESSAGE; // Name the message command header, that this is touch screen message
    MyGraphicsMessage.MessageData.MyGolMessage = pMsg;
    
    xHigherPriorityTaskWoken = pdFALSE; // Initialize the variable, which store the condition if context switch is needed
    xQueueSendFromISR(GraphicsTaskQueue, &MyGraphicsMessage, &xHigherPriorityTaskWoken); // Send the message to the graphics task queue
    if(xHigherPriorityTaskWoken == pdTRUE) // Check if context switch is needed
    {
        //taskYIELD(); // Perform the context switch
        PortYieldFlag = 1;
    }
}

// Timestamp functions for all tasks:
MAINAPPLICATIONTASKSTimeStampTick(); // Rolling the software timers

// Process time for load screen duration:
if(1 == MAINAPPLICATIONTASKSLoadScreenTimerTick()) // Inside this function message queue is send to the desired task
{
    PortYieldFlag = 1;
}

// Application level task wake-up hook:
////APPLICATIONLEVELTASKTimeoutTick();
if(PortYieldFlag == 1)
{
    taskYIELD();
}

```

} // End of vApplicationTickHook()

1. Due to we use many ISRs with different priority, we changed the:#define configKERNEL\_INTERRUPT\_PRIORITY 0x07

(Also modified all needed values in the needed port files)

Currently all of our ISRs, which can cause context switch are priority level 7, all other ISRs are less priority level

Is this configuration RTOS SAVE?

Many thanks in advance

Regards  
Dimitar Belev

---

<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:** [October 27, 2015, 9:40pm UTC](https://forums.freertos.org/t/context-switch-from-the-void-vapplicationtickhook-void-callback-function/3640/2 "2015-10-27T21:40:49Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, October 27, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/a27ca295/#4fcb):

Looking at how the port is implemented on that part I would say no, it is not safe. In other ports it is safe, but also pointless, as the tick interrupt will switch to the highest priority task anyway.

It is ok to unblock a task (using [vTaskNotifyGiveFromISR()](http://www.freertos.org/vTaskNotifyGiveFromISR.html)), but don’t then perform a context switch. As mentioned above, if the unblocked task is the highest priority task it will get selected to run anyway.

Regards.

---

<div class="post-metadata">

**Author:** ![lz1msz](https://avatars.discourse-cdn.com/v4/letter/l/e95f7d/32.png) [@lz1msz](https://forums.freertos.org/u/lz1msz)\
**Post date:** [October 28, 2015, 9:21am UTC](https://forums.freertos.org/t/context-switch-from-the-void-vapplicationtickhook-void-callback-function/3640/3 "2015-10-28T09:21:05Z")

</div>

**[lz1msz](https://sourceforge.net/u/lz1msz/)** wrote on [Wednesday, October 28, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/a27ca295/#61ee):

Thank you!
