Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Binary Semaphore - Question

Posted by iammrt on December 10, 2009
Dear Community,

I'm about to check out the freeRTOS, but im stuck at testing a simple binary semaphore behavior.

This is my code:
----------------------

INCLUDE_vTaskSuspend = 1
is set

[...]

int main(void){

[some initial code]

statT1 = xTaskCreate( myTask, ( signed char * )"myTask", 500, NULL, 2, NULL );
statT2 = xTaskCreate( mySecTask, ( signed char * )"mySecTask", 500, NULL, 1, NULL );

vSemaphoreCreateBinary( ledSyncSem );

[other code]
}

void myTask(void * pvParameters){

while(1){
xSemaphoreTake( ledSyncSem, portMAX_DELAY);
LED_OFF();
xSemaphoreGive(ledSyncSem);
firstTaskCnt++;
vTaskDelay(1000);
}
}


void mySecTask(void * pvParameters){
while(1){
xSemaphoreTake( ledSyncSem, portMAX_DELAY);
LED_ON();
xSemaphoreGive(ledSyncSem);
secondTaskCnt++;
vTaskDelay(1000);
}
}

I expect to have a the active Task wait (blocking) on the semaphore until (wait forever condition) it can "take" it. When it took it, the task will toggle on or off (depending on Task) the LEDs. After this happend, it "gives" the semaphore back, and the semaphore is free for the other task. Now the active Task will go to TaskDelay(1000) and the 2nd task will have time to perform his toggle...
So i expect the LED to toggle on/off. But this don't happen. Its ON all the time. If I invert Task Priority its OFF all the time.

It seems i miss understand how to use those semaphore functions. I checked if the semaphore has been allocated correctly, seems to be fine.

Would you mind to correct me?

RE: Binary Semaphore - Question

Posted by Stephen Osborn on December 10, 2009
You understand the semaphores correctly, what you are missing is the timing interaction of your two tasks.

Here is your timing sequence.

myTask does:

Take, LED_OFF, Give, Pause

at the pause mySecTask is allowed to run and the semaphore is available so meSecTask does

Take, LED_ON, Give, Pause

So in a very short time (not 1 second) you have had the following sequence

Take, LED_OFF, Give, Take, LED_ON, Give

Now both tasks are paused for 1 second, and the cycles repeats.

If you put an oscilloscope on the LED you will see a short OFF pulse followed by a long ON pulse

To correct your problem you need to put the Give after the Pause. Do not release the semaphore until your are completely finished with it. In this case you are not finished with the semaphore until the LED blink time is complete.

Good Luck. And BTW FreeRTOS is awsome.


RE: Binary Semaphore - Question

Posted by Richard Damon on December 10, 2009
Even putting the Give after the Wait won't always work, for this sort of thing you would need two flags, as here is what would happen (assuming myTask has higher priority:

MyTask starts, Takes semaphore, LED off, wait
MySecTask starts, Tries to take semaphore but blocks
MyTask wait ends, MyTask gives semaphore, but is still highest task so keeps running (so MySecTask doesn't actually take semaphore yet), MyTask than loops and takes semaphore again and loop continues.

RE: Binary Semaphore - Question

Posted by iammrt on December 10, 2009
Well I tried the "wait" after the LED toggle and before the SemGive Funtion. I just added a counting loop to get a delay. This works. The LED is toggling. Is there a "Wait(x ticks)" like os function, which wont block/suspend the calling task? Its ease to implement, I know, but if there is a system function doing this, i want to use it. Can't find something in the API reference.

I tried out (with wait loop before semGive) to set both Tasks to the same priority. When i do this only "mySecTask" is running, firstTaskCnt++ never happens.

Whats wrong about this? I expect 2 Tasks on same prio => Tasks will switch each tick. So they shall have a 50:50 share of CPU time, aren't they?
Maybe I need to set-up a special define to enable this feature?

RE: Binary Semaphore - Question

Posted by iammrt on December 11, 2009
Oh btw. I tried this morning to use vTaskSuspend(xHandle) or (NULL), it don't have effect, the Suspended Task is still running. Im pretty sure I missed something somewhere in config or so. What ever INCLUDE_vTaskSuspend is set to 1.

RE: Binary Semaphore - Question

Posted by iammrt on December 11, 2009
Just ignore Post #5 please. To early in the morning - the issue was something bout not started timers. sorry bout that.

By the way: thanks for this nice support forum. All questions got answered very quick, and the answers are high quality! :)


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists