Need more detail of the uxPriority parameter in xCreateTask function

suxilong1 wrote on Wednesday, October 28, 2015:

Hi All
** i am new in learning FreeRTOS。
my project is running FreeRTOS in ESP8266(wifi SOC )
environment:VS2013+VisualGDB 5.0
at first i creat a new project。add two task in the main() ,and be aware of their different uxPriority

static const char *pcTextForTask1 = "Task 1 is running\r\n";
static const char *pcTextForTask2 = "Task 2 is running\r\n";
xTaskCreate(LEDBlinkTask, (signed char *)"Blink1", 256, (void *)pcTextForTask1, 1, NULL);
xTaskCreate(LEDBlinkTask, (signed char *)"Blink2", 256, (void *)pcTextForTask2, 2, NULL);

** and there is the LEDBlinkTask function:**

 static void RAMFUNC LEDBlinkTask(void *pvParameters)
	char *pchar;
	pchar = (char *)pvParameters;
	for (int tick = 0;; tick++)
		vTaskDelay(s_LEDPeriod / portTICK_RATE_MS);
		gpio_output_conf(0, BIT2, BIT2, 0);
		vTaskDelay(s_LEDPeriod / portTICK_RATE_MS);
		gpio_output_conf(BIT2, 0, BIT2, 0);

**when s_LEDPeriod = 300 adn then running the project, i could seen that it prinft “Task 2 is running” and “Task 1 is running”

but when i decrease the s_LEDPeriod as lower than 10 , it was only printf “Task 2 is running”,there is no more “Task 1 is running”

does anybody give me a hand , tell me what is the matter uxPriority with vTaskdelay


richard_damon wrote on Wednesday, October 28, 2015:

What is portTICK_RATE_MS set to? (You may need to trace back other defines)
If s_LEDPeriod is set below it this would be expected as the delay becomes 0 ticks, or ‘no delay’.

rtel wrote on Wednesday, October 28, 2015:

A few minor details before getting to the main point of your post:

s_LEDPeriod / portTICK_RATE_MS

The recommended way of doing this now is to use the code “pdMS_TO_TICKS( s_LEDPeriod )” - although both will still work.

xTaskCreate( …, (signed char *)“Blink1”, …);

be careful as to where the string passed in as the task parameter is allocated. It will probably change with the compiler optimisation. If it is allocated in const memory then there will be problem, but if it is allocated on the stack of the function that creates the tasks then there could be a problem as you will end up with one task referencing the stack of another function - maybe the main() function or maybe another task - and that memory is likely to change.

The easiest thing to do is declare the strings as file scope constants, such as:

static const char *pcString1 = “Blink1”;

and then pass in the pcString1 variable as the task parameter.

…now onto your question, in addition to Richard Damon’s reply:

How is printf() implemented? Where is it sending its output?

printf() will often take a long time to execute, so may not be able to execute in time, and it is rarely re-entrant, which can cause corruption if two tasks try using it at the same time. For example, if the output goes to a UART and one task starts to print “hello”, but half way through there is a switch to the other task, and that task prints “world”, then the output may look something like “helworldlo” (a context switch occurred between the two ‘l’ characters.

You can protect printf from this type of problem using a mutex, or scheduler locking, etc.