nobody wrote on Monday, December 19, 2005:
A while back I posted thread on semaphores and a problem I had whith xSemaphoreTake returning before the waiting time had expired. Barry posted a reply to that but since then I hadn’t had much time to have a look at it, but a week ago I got some time again started to reinvestigare the problem. Here are my findings, please comment on any wrong assumptions/conclusions I’ve come up with.
The setup:
2 tasks, one running at prio 4, the other at 2. he high prio task is used for MP3-playback and basically only releases the CPU for 1 mS every 10-12 mS.
The two tasks share a common resource, an SPI bus which is used to communicate with a memory card and a display.
What happens??
Well, the SPI driver is called from two different tasks… At the entry of the read and write functions the driver tries to aquire a semaphore. As it turns out the low prio task sometimes misses to aquire the semaphore, even though only 2-5 mS of it’s allowed waiting time has passed.
The reason I firgure is this… (In the following T# notation is used, # indicates the priority of the task).
1. T4 aquire the semaphore. Starts to read some data. During the read the processor is released, so that lower prio tasks can be run.
2. T2 runs, and tries to aquire the semaphore… I has to wait.
3. T4, read is complete and the semaphore is released… Now FreeRTOS detects a task wating for the semaphore and releases it, T2 enters ready state.
4. T4 maintains the processor. Feeding data to a DAC.
5. T4 goes to sleep, antoher task T2B becomes active.
6. T4 resumes with stage 1.
Now T2 once again runs during the read or during the sleep, just to detect that the semaphore is taken by T4.
Frankly I don’t know how to get around this, since if I understand FreeRTOS a task waiting for a semaphore is not guaranteed to receive it. I susspose I could write a different way of handling semaphores… But it would be nice to have some input on how you would solve such a problem??
// Q