Resource management

thomashh wrote on Monday, July 08, 2013:


I have a question about mutexes and semaphores.
Is it possible to use a mutex (or semaphore) to control the access to a peripheral within one task?
I want to implement a block data transfer. Therefore I want to generate a mutex which is taken before the data is written to the peripheral. If the peripherals FIFO is empty I want an interrupt service routine to give the mutex back so that the next data can be sent.
I have already tried to implement this behavior but without success.
The xSemaphoreTake(SBP_ClientTxSem, 0x00) function does not block before the mutex was given by the ISR. This produces data loss.
Can anybody help me?


davedoors wrote on Monday, July 08, 2013:

That is the normal way of doing it, but if it is only used by a single task use a binary semaphore instead of a mutex.

In you post the second parameter to xSemaphroeTake() is zero, so the block time is 0 and the function will return right away. If you want to wait for a finite time then set the second parameter to the number of ticks you want to block and check the return value to see if the semaphore was taken (true) or if the call timed out (false).

thomashh wrote on Tuesday, July 09, 2013:

Hi davedoors,

thank you for your quick reply.
Unfortunately I misinterpreted the sentence “A block time of zero can be used to poll the semaphore.”

It works perfectly.

Thank you