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
Posted on April 05, 2017 at 08:06

No, task 1 is wait for a sem1 semaphore (

xSemaphoreTake(Sem1, portMAX_DELAY)

) that's mean task 1 is never run until task 2 give a sem1 

semaphore (

xSemaphoreGive(Sem1)

), except first time 

task 1 give 

a sem1 semaphore for itself before enter infinite loop.

sorry for my poor english.

Posted on April 05, 2017 at 08:42

I've created :

static void StartDefaultTask(void const * argument)

{

/* USER CODE BEGIN StartDefaultTask */

/* Infinite loop */

xSemaphoreGive(Sem1);

for (;;)

{//thread1 endless loop begin

if(xSemaphoreTake(Sem1, portMAX_DELAY) == pdTRUE)

{

printf('Start Default Task! \n');

printf('Task 1, DefaultTask running! \n \n');

vTaskDelay(100);

/* Wake the second task. */

xSemaphoreGive(Sem2);

}//end if xSemaphoreTake Sem1

}//thread1 endless loop end

/* USER CODE END StartDefaultTask */

}

static void StartSecondTask(void const * argument)

{

for (;;)

{//thread2 endless loop begin

/* Wait for Sem2 */

if(xSemaphoreTake(Sem2, portMAX_DELAY) == pdTRUE)

{

//second task

printf('Start Second Task! \n');

printf('Task 2, SecondTask running! \n \n');

/* Resume Thread 1 */

vTaskDelay(200);

/* Wake the first task. */

xSemaphoreGive(Sem1);

}//end xSemaphoreTake Sem2

}//thread2 endless loop end

}

What should I make on Task1, so it will finish the job first before

giving semaphore to task 2 ? because the time slice for Task 1 is far

too less compare to the process it must finish ?

antonius
Senior
Posted on April 05, 2017 at 11:49

Is it relevant with :

http://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html

 

?

I tried putting the real task, and it seems that it never looped anymore and stack in one task.

Posted on April 05, 2017 at 11:24

Nothing. time slice is least significant in this case because when the task 2 reach xSemaphoreTake(Sem2, portMAX_DELAY) while task 1 is processing it's job, task 2 will enter blocking state. Context switch can occur 

while task 1 is processing it's job, but

 task 2 is in blocking state and scheduler will not bring the blocking task to running state, so in  the same priority only task 1 is ready to run, scheduler will resume task 1 to running state.

after task 1 finish it's job and reach 

xSemaphoreGive(Sem2);, then task 2 will back to ready state and task 1 continues to next round and reach 

xSemaphoreTake(Sem1, portMAX_DELAY). Now is 

task 1

 turn to be blocked and task 2 turn to run.

sorry for my poor english.

Posted on April 05, 2017 at 13:17

Yes, you can use a binary semaphore in place of a mutex, however, semaphores are quite heavy in comparison to mutexes. 

My rule of thumb:

  1. If you need speed and code size, use a signal
  2. If you need thread safe locking mechanisms, semaphore
  3. If you just need locking but not in ISRs, mutex
Posted on April 05, 2017 at 14:28

I tried to call function on Task 1, and Task 2, will it make the synchronization fail ?

Posted on April 05, 2017 at 16:09

Is it possible that I have less heap size ?

&sharpdefine configTOTAL_HEAP_SIZE ((size_t)8192)

Posted on April 05, 2017 at 16:12

No, it will not. you can call function on both task.

Posted on April 05, 2017 at 16:15

I've tried calling a function, Task 1 will do the function but stop and

never goes to Task 2, do you want to see the function ?

Posted on April 05, 2017 at 16:25

void test_fatfs()

{

FATFS SDFatFs; /* File system object for SD card logical drive */

FIL MyFile; /* File object */

const char wtext[] = 'Hello ! This is from STM32F103VET6 with RTOS';

FRESULT res; /* FatFs function common result code */

uint32_t byteswritten, bytesread; /* File write/read counts */

char rtext[256]; /* File read buffer */

//TEST SDIO begin

if(retSD == 0)

{

printf('inside if retSD \n');

HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6,

GPIO_PIN_RESET);

HAL_Delay(1000);

/*♯♯-2- Register the file system object to the

FatFs module ♯♯♯*/

if(f_mount(&SDFatFs, (TCHAR const*)SD_Path,

0) != FR_OK)

{

/* FatFs Initialization Error : set

the red LED on */

printf('FMOUNT FAIL! \n');

while(1);

}

else

{

printf('inside else on f_mount

\n');

printf('f_mount OK! \n');

res = f_open(&MyFile, 'hello.txt',

FA_CREATE_ALWAYS | FA_WRITE);

printf('after res command! \n');

/*♯♯-4- Create &

Open a new text file object with write access♯*/

//if(f_open(&MyFile, 'Hello.txt', FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)

if (res != FR_OK)

{

/* 'Hello.txt'

file Open for write Error : set the red LED on */

printf('f_open FAIL! \n');

while(1);

}

else

{

printf('f_open OK! \n');

/♯♯-5- Write data to the text file ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯/

res =

f_write(&MyFile, wtext, sizeof(wtext), (void*)&byteswritten);

if((byteswritten == 0) || (res != FR_OK))

{

/* 'Hello.txt' file Write or EOF Error : set the red LED on */

while(1);

}

else

{

/*♯♯-6- Successful open/write : set the blue LED on */

HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);

f_close(&MyFile);

}

}

}

}

}

*

*void reading_file_test()

{

UINT bw,size;

FRESULT fr; /* FatFs return code */

char line[82]; /* Line buffer */

FIL MyFile; /* File object */

FATFS SDFatFsRead;

/TESTING READING FILE BEGIN/

/* Open a text file */

f_mount(&SDFatFsRead, (TCHAR const*)SD_Path, 0);

fr = f_open(&MyFile, 'test.txt', FA_READ);

printf('Reading file \n \n');

/* Read all lines and display it */

while (f_gets(line, sizeof line, &MyFile))

printf(line);

/* Close the file */

printf('\n \n Finished reading file.. \n \n');

f_close(&MyFile);

/TESTING READING FILE END/

}

static void StartDefaultTask(void const * argument)

{

/====variable declaration=====/

uint32_t count = 0;

(void) argument;

/====variable declaration=====

/* init code for FATFS */

MX_FATFS_Init();

/* init code for USB_DEVICE */

MX_USB_DEVICE_Init();

/* USER CODE BEGIN StartDefaultTask */

/* Infinite loop */

xSemaphoreGive(Sem1);

for (;;)

{//thread1 endless loop begin

if(xSemaphoreTake(Sem1, portMAX_DELAY) == pdTRUE)

{

printf('Start Default Task! \n');

printf('Task 1, DefaultTask running!

==================================\n \n');

*test_fatfs(); * //task thread_2 1

==> *Stop here and never continue to Task2, why ?? *

vTaskDelay(100);

/* Wake the second task. */

xSemaphoreGive(Sem2);

}//end if xSemaphoreTake Sem1

}//thread1 endless loop end

/*

for(;;)

{

osDelay(1);

}

*/

/* USER CODE END StartDefaultTask */

}

static void StartSecondTask(void const * argument)

{

for (;;)

{//thread2 endless loop begin

/* Wait for Sem2 */

if(xSemaphoreTake(Sem2, portMAX_DELAY) == pdTRUE)

{

//second task

printf('Start Second Task! \n');

printf('Task 2, SecondTask

running!============================== \n \n');

reading_file_test(); //task on thread 2

vTaskDelay(200);

/* Wake the first task. */

xSemaphoreGive(Sem1);

}//end xSemaphoreTake Sem2

}//thread2 endless loop end

}