Entering and exiting the critical section using taskENTER_CRITICAL() and taskEXIT_CRITICAL() should work inside the task function. Make sure to NOT call any FreeRTOS API while inside the critical section.
It seems that you are using printf inside the critical section. While it will work, it is not recommended as printf may take longer to execute. taskENTER_CRITICAL() and taskEXIT_CRITICAL() implement critical section by disabling interrupts and therefore, are recommended for very short and deterministic critical sections. Consider using mutex to serialize printf calls.
While it seems what you suggested was for when the printf messages are withing the task but enveloped between task*Critical(), what about the following scenario?
void CalledFunction()
{
taskENTERCritical();
printf("In Called Function\r\n");
taskEXITCritical();
//Something else
return;
}
static void Task1(void * pvParameters){
{
for(;;){
//Something here
CalledFunction();
//Something there
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
It is still the same thing. What you are doing is calling printf in a critical section implemented using taskENTER_CRITICAL / taskEXIT_CRITICAL which is not a good idea as @karahulx already explained. Why do you want to use critical section - probably to ensure that the output from multiple tasks is not mangled, right? You should use mutex in this case -