Issue with two task with different priority

Hello,

I am implementing two tasks code as follow
/*******************************************************************************/
{
/
Two task created
/

xTaskCreate(vtask_1, “Task-1”, 500, NULL, 2, &xTaskHandle1);
xTaskCreate(vtask_2, “Task-2”, 500, NULL, 5, &xTaskHandle2);

/* Schedule the task */
vTaskStartScheduler();
}

/* Task_1 Handler*/
void (vtask_1)( void *params )
{

UBaseType_t p1,p2;
sprintf(usr_msg,"TASK 1 is runnung\r\n");
printmsg(usr_msg);

   sprintf(usr_msg,"TASK 1 priority is  : %ld\r\n",uxTaskPriorityGet(xTaskHandle1));
printmsg(usr_msg);

sprintf(usr_msg,"TASK 2 priority is  : %ld \r\n",uxTaskPriorityGet(xTaskHandle2));
printmsg(usr_msg);

while(1)
{
	if(prio)
	{
		prio = FALSE;
		 p1 = uxTaskPriorityGet(xTaskHandle1);
		 p2 = uxTaskPriorityGet(xTaskHandle2);

		 //switch prio
		 vTaskPrioritySet(xTaskHandle1, p2);
		 vTaskPrioritySet(xTaskHandle2, p1);
	}else
	{
		HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
		HAL_Delay(1000);
	}
}

}

/* Task_2 Handler*/
void (vtask_2)( void *params )
{
UBaseType_t p1,p2;
sprintf(usr_msg,“TASK 2 is runnung\r\n”);
printmsg(usr_msg);

sprintf(usr_msg,"TASK 1 priority is  : %ld\r\n",uxTaskPriorityGet(xTaskHandle1));
printmsg(usr_msg);

sprintf(usr_msg,"TASK 2 priority is  : %ld \r\n",uxTaskPriorityGet(xTaskHandle2));
printmsg(usr_msg);

while(1)
	{
		if(prio)
		{
			prio = FALSE;
			 p1 = uxTaskPriorityGet(xTaskHandle1);
			 p2 = uxTaskPriorityGet(xTaskHandle2);

			 //switch prio
			 vTaskPrioritySet(xTaskHandle1, p2);
			 vTaskPrioritySet(xTaskHandle2, p1);

		}else
		{
			HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
			HAL_Delay(100);
		}
}

}

I was under the impression that each time a task runs it runs the whole handler body. Meaning from the open brace of task_1 to the closing brace of the function body. However, i realized that after Task1 has switched in from Task2 or Task2 has switched in from Task1, for subsequent context switches, only the while loop or for loop gets executed.

Thus, subsequent button presses to switch priority between the 2 tasks will not print out the messages to Teraterm.

Only the 1st run context switching will run the whole handler body whereas subsequent switching won’t?

What is the reason?

Please refer the screenshot

Thanks
Error

When tasks switch in and out, you don’t want them to ‘restart’ at the beginning but the keep executing from where they were. That is why we put the loop in the task, to keep it running in that loop. Maybe you want the code that you think should be running each time within the loop.

one more small query like after calling vTaskStartScheduler() API, as per my understanding my higher priority tasks will run as in my case printmsg() of task2.

But I observe that when I start my program all printmsg() of task1 and task2 executes, so why task1 executes printmsg() function.

Inside while all is working fine for both case

Not knowing how printmsg works, it could easily be blocking while waiting for a message to finish going out, thus giving the other task time to run.

FreeRTOS runs the current highest priority ready task. When that task blocks for some reason, or if some other task of higher priority becomes ready, then FreeRTOS will switch to that task (at least if Preemption is turned on, otherwise it will wait for the current task to do something which indicates it is ready to be preempted before switching).

  1. Preemption is turned ON.

void printmsg(char msg)
{
HAL_USART_Transmit(&husart2, (uint8_t
)msg, strlen(msg), 1000);
}

Actually I have two tasks with priority with 2 and 5 respectively.
So as PREMPTOIN is turned on so my task with priority 5 should only run from time t = 0;
but I am observing that task with priority 2 is running till before it goes into a while loop.

So I am facing difficulty in understanding the issue.