cancel
Showing results for 
Search instead for 
Did you mean: 

FreeRTOS tasks..

hitsumen
Associate II
Posted on April 23, 2016 at 23:16

Good day,

If anyone tried to use FreeRTOS I have one question, why my task is not finished, and another task is started? Here is my code and log.

code:

void vDebug1(void *pvParameters)

{

vTaskDelay( 100 / portTICK_RATE_MS );

printf(''\nstart debug task1!'');

for(;;)

{

printf(''\nDebug task1'');

vTaskDelay( 250 / portTICK_RATE_MS );

}

}

void vDebug2(void *pvParameters)

{

vTaskDelay( 200 / portTICK_RATE_MS );

printf(''\nstart debug task2!'');

for(;;)

{

printf(''\nDebug task2'');

vTaskDelay( 250 / portTICK_RATE_MS );

}

}

void vLedBlinkBlue(void *pvParameters)

{

vTaskDelay( 300 / portTICK_RATE_MS );

printf(''\nvLedBlinkBlue task!'');

for(;;)

{

STM_EVAL_LEDToggle(LED3);

vTaskDelay( 1000 / portTICK_RATE_MS );

printf(''\nSTM_EVAL_LEDToggle LED1!'');

}

}

void vLedBlinkRed(void *pvParameters)

{

vTaskDelay( 400 / portTICK_RATE_MS );

printf(''\nvLedBlinkRed task!'');

for(;;)

{

STM_EVAL_LEDToggle(LED4);

vTaskDelay( 500 / portTICK_RATE_MS );

printf(''\nSTM_EVAL_LEDToggle LED2!'');

}

}

void vLedBlinkGreen(void *pvParameters)

{

vTaskDelay( 500 / portTICK_RATE_MS );

printf(''\nvLedBlinkGreen task!'');

for(;;)

{

STM_EVAL_LEDToggle(LED5);

vTaskDelay( 125 / portTICK_RATE_MS );

printf(''\nSTM_EVAL_LEDToggle LED3!'');

}

}

void vLedBlinkOrange(void *pvParameters)

{

vTaskDelay( 600 / portTICK_RATE_MS );

printf(''\nvLedBlinkOrange task!'');

for(;;)

{

STM_EVAL_LEDToggle(LED6);

vTaskDelay( 250 / portTICK_RATE_MS );

printf(''\nSTM_EVAL_LEDToggle LED4!'');

}

}

Debug log: 

Debug task1

STM_EVAL_LEDToggle LED4!

Debug task2

STM_EVAL_LEDToggle LED3!

STM_EVAL_LEDToggle LED1!

STM_EVAL_LEDToggle LED3!

Debug task1

STM_EVAL_LEDToggle LED4!

STM_EVAL_LEDToggle LED2!

Debug task2

STM_EVAL_LEDToggle LED3!

STM_EVAL_LEDToggle LED3!

Debug task1

STM_EVAL_LEDToggle LED4!

Debug task2

STM_EVAL_LEDToggle LED3!

STM_EVAL_LEDToggle L

Debug task1

ED3!

STM_EVAL_LEDToggle LED4!

STM_EVAL_LEDToggle LED2!

Debug task2

STM_EVAL_LEDToggle LED3!

Debug task1

STM_EVAL_LEDToggle LED3!

STM_EVAL_LEDToggle LED4!

Debug task2

STM_EVAL_LEDToggle LED3!
5 REPLIES 5
mark239955_stm1
Associate II
Posted on April 24, 2016 at 03:09

FreeRTOS does (by default) pre-emptive multi-tasking.

The highest-priority active task always runs.  If there are multiple active tasks of the same (highest) priority then they all take turns running, with each task running for a finite amount of time before the RTOS pauses the current task and restarts the next task from wherever it was when it was paused.

This is exactly what you're seeing with the interleaved outputs of your tasks.

hitsumen
Associate II
Posted on April 24, 2016 at 06:43

Is it possible somehow to fix that? 

I want that my task should be finished.

re.wolff9
Senior
Posted on April 24, 2016 at 08:17

That would not be called ''fix'': that is the way it is supposed to work. 

To program what you want, you can start a task and then wait for the first task to finish before starting the next task. 

Or you use a semaphore. Signal the semaphore at the end of the first task wait for it in the beginning of the second task. 

But easiest: Just combine the code of the two tasks into one. People use tasks for things that need to run in parallel. In the simplest case, you might have a blinking led and need to handle bytes from a serial port. So you create a task for the blinking led which is very simple (toggle led, wait, repeat), while the led code would be complicated (keep time, when time to toggle the led has elapsed toggle the led. But then when your serial input requests an action that is longer, the led will stop blinking. Complicated code. This is simplified by using tasks). 

hitsumen
Associate II
Posted on April 24, 2016 at 12:14

wolff.roger, turner.mark.003

Thank you for your answers.

Its only an example, I wanted to play with MicroSD card, like writing 2 files at once.

So i could not understand why this is happening. I will try to use semaphore. Will write about results. 

Walid FTITI_O
Senior II
Posted on April 25, 2016 at 12:33

Hi 

kovaliov.nikolaj

,

You can manage that by using

osSignalWait()

osSignalWait() is called to wait for one or more signal flags to become signaled (set) for the current running thread.

Refer to the ''FreeRTOS_signal example in the

http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1897/PF261909

in this path :

STM32Cube_FW_F7_V1.3.0\Projects\STM32756G_EVAL\Applications\FreeRTOS\FreeRTOS_Signal

-Hannibal-