FreeRTOS Support Archive
The FreeRTOS support forum is used to obtain active support directly from Real
Time Engineers Ltd. In return for using our top quality software and services for
free, we request you play fair and do your bit to help others too! Sign up
to receive notifications of new support topics then help where you can.
This is a read only archive of threads posted to the FreeRTOS support forum.
The archive is updated every week, so will not always contain the very latest posts.
Use these archive pages to search previous posts. Use the Live FreeRTOS Forum
link to reply to a post, or start a new support thread.
[FreeRTOS Home] [Live FreeRTOS Forum] [FAQ] [Archive Top] [December 2009 Threads] Binary Semaphore - QuestionPosted 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 - QuestionPosted 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 - QuestionPosted 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 - QuestionPosted 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 - QuestionPosted 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 - QuestionPosted 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! :)
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.
|