Frequency limit of binary semaphores?

marcodg wrote on Thursday, February 20, 2014:

I’ve run into this problem and not sure where to look next. This is a boiled down essence of a motor control.

I’m something of a FreeRTOS newbie but have been around a long time.
I have a timer tick interrupt. All it does is “give” the binary semaphore (ISR).

void TimerTick() { xSemaphoreGiveFromISR( sem ); }

void Foo()

sem = xSemaphoreCreateBinary();
n = 0;
while(1) {
xSemaphoreTake(sem); /* block until timer tick /
if (n++ & 1)
set IO pin high /
set IO pin low */

This works well for low frequencies. If I user a timer tick of 100Hz I get a beautiful 50Hz square wave. If I use a frequency of 1000Hz I get a 500Hz square wave most of the time. Occasionally it jitters and breaks and then restores itself. If I use, say 2kHz, it isn’t even close. Changing the task priority doesn’t matter and in fact is the only one of my tasks running. Since 1kHz is the breaking point is it related to the RTOS tick interval? I know the timer code works I can use much higher frequencies (e.g. sending audio data to a D/A) in other parts of my code. Any ideas?

rtel wrote on Thursday, February 20, 2014:

The maximum frequency you can achieve with this technique is somewhat dependent on the processor you are running on. It goes without saying it also depends on how fast the hardware is clocked.

There is a limit to how fast it will function simply because code is running - and a context switch needs to be performed. How long a context switch takes is also dependent on the hardware. You will find an indicative number here:

Are you using the xHigherPriorityTaskWoken parameter to the xSemaphoreGiveFromISR() function to ensure the interrupt returns directly to the task that calling xSemaphoreGiveFromISR() unblocked? It should not be related to the RTOS tick interval if you are, but will be if your not.


marcodg wrote on Thursday, February 20, 2014:

The processor is an NXP-1788 (Cortex M3) running at 120MHz. I am using the xHigherPriorityTaskWoken argument but I was not yielding. Thanks for the insight. I can now easily handle the frequencies required.