cancel
Showing results for 
Search instead for 
Did you mean: 

FreeRTOS Mutex example ?

antonius
Senior
Posted on April 03, 2017 at 00:53

Everyone,

Does anyone know where I can find a simple example for FreeRTOS on Mutex ?

I want to do two task but the first task never finishes and jump already to the second one because of the systick,

how can I make the first one finish and do the second one with Mutex ?

Thanks for reading and advicing

#multitasking #rtos #stm32f1xx #mutex

Note: this post was migrated and contained many threaded conversations, some content may be missing.
21 REPLIES 21
Barry.Richard
Associate III
Posted on April 03, 2017 at 06:17

Try the

http://www.freertos.org/a00113.html

 or better still the

http://www.freertos.org/Documentation/RTOS_book.html

.
Posted on April 03, 2017 at 11:14

Thanks for the link, I appreciate that, can you tell me which directory containing an example for semaphore in FreeRTOS9 directory ? thanks

Amel NASRI
ST Employee
Posted on April 03, 2017 at 16:55

Hello

H.Rick

‌,

In all STM32Cube packages, you will find an example called ''FreeRTOS_Mutexes''.

Just search it in the package you are using.

You can getmore details about mutexes in the

http://www.st.com/content/ccc/resource/technical/document/user_manual/2d/60/ff/15/8c/c9/43/77/DM00105pdf/files/DM00105pdf/jcr:content/translations/en.DM00105pdf

''Developing Applications on STM32Cube with RTOS'', sub-section ''Mutexes example''.

If you are using an STM32F4, the example ''

FreeRTOS_Mutexes'' is available as application example for boards marked with ''x'' in this table (extracted from

http://www.st.com/content/ccc/resource/technical/document/application_note/ca/34/db/d3/71/50/44/9b/DM002135pdf/files/DM002135pdf/jcr:content/translations/en.DM002135pdf

:(

0690X00000606gwQAA.png

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

dechawat
Associate II
Posted on April 03, 2017 at 21:12

I think use semaphore instead of mutex is easier.

SemaphoreHandle_t Sem1, Sem2;

int main(void) {

      /* Create semaphore. */

      Sem1 = xSemaphoreCreateBinary();

      Sem2 = xSemaphoreCreateBinary();

      .... Create task and run scheduler.

}

void Task1(void *param) {

      /* Give once for first time. */

      xSemaphoreGive(Sem1);

      while(1) {

            

/* Wait for Sem1 */

            if(xSemaphoreTake(Sem1, portMAX_DELAY) == pdTURE) {

                  /* Your first task code */

                  /* Wake

the

second task. */

                  xSemaphoreGive(Sem2);

            }

      }

}

void Task2(void *param) {

      while(1) {

            /* Wait for Sem2 */

            if(xSemaphoreTake(Sem2, portMAX_DELAY) == pdTURE) {

 /* Your second task code */

                  /* Wake

the

 first task. */

                  xSemaphoreGive(Sem1);

            }

      }

}
Posted on April 04, 2017 at 00:23

Thank you very much for the advice.

Posted on April 04, 2017 at 08:58

That's good idea to try, thanks

Posted on April 05, 2017 at 05:51

What should I add on task2 if task 2 time is much longer than task 1 ? and task 2 time is much longer than default systick ?

It seemed that it's doing task 1 but failed on task 2 then stop forever ?

thanks

Posted on April 05, 2017 at 06:04

Did you read the chapter on mutexes in the book I posted a link to in my first reply?  In particular read the part about using mutexes from tight loops.

Posted on April 05, 2017 at 07:20

This chapter : 7.3 Mutexes (and Binary Semaphores) ?

Which part exactly ? May be I missed it ?