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.
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.
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.
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?
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.