Cant get a simple SPI mutex to work..

jon_newcomb wrote on Tuesday, April 27, 2010:

I’m having one of those days where I’m missing the blinding obvious…
Using FreeRTOS V5.3.0, IAR 5.41.2,  JLink, AT91SAM7S256

We have lots of comms on a singe SPI port. I thought it sensible to convert my (old and working I hope) binary 
semaphore to a (new and improved) mutex.
I have a global declaration…
static xSemaphoreHandle semaphore_spi_guard;

Simple… change the line where it is initialised…

vSemaphoreCreateBinary (semaphore_spi_guard


semaphore_spi_guard = xSemaphoreCreateMutex(


As described in

It compiles… 

#define configUSE_MUTEXES 1  in FreeRTOSConfig.h

is all present and correct.

The help says…
“Mutexes created using this macro can be accessed using the xSemaphoreTake() and xSemaphoreGive() macros”
Great… I need not alter any of my code because this is the way I give and take my (old) binary SPI semaphore.

Reality was different…
I was disappointed to find every give or take of the mutex fails… time to start investigating…
I put breakpoints in a few different locations…
This is when started to really scratch my head and get confused…


xQueueHandle xQueueCreateMutex( void


The following values are end up as follows:

pxNewQueue->pcWriteTo = NULL;
  pxNewQueue->pcReadFrom = NULL;

… OK, so unlike a queue, we don’t set these to the ‘head’ of a queue… So far so good.

Then every time we come to give or take the mutex I arrive at xQueueGenericSend or xQueueGenericReceive.
The very first few lines perform a check that always fails - and I cant work out why it does the check and how it can pass…

This check is…

   if (!pxQueue || !pxQueue->pcHead) // extended check for valid Queue
      return errQUEUE_UNDEF;


Well of course it fails with


I get back to my give / take mutex having don’t nothing and returning a fail…
Please help… I’m very confused!

Jon Newcomb

rtel wrote on Tuesday, April 27, 2010:

I don’t think there has ever been an error code errQUEUE_UNDEF, so can only assume that you are not using an official version of FreeRTOS, or that somebody at your end has added this code.  Try comparing your queue.c to one from the official release - although your version is not up to date you can still download all the previous versions from SourceForge.


jon_newcomb wrote on Wednesday, April 28, 2010:

That is indeed correct, and as I suspected, obvious. Thankyou.