# Advice on Mutex

**URL:** <https://forums.freertos.org/t/advice-on-mutex/2087>\
**Category:** Kernel\
**Created:** [October 3, 2014, 6:35am UTC](https://forums.freertos.org/t/advice-on-mutex/2087 "2014-10-03T06:35:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 3, 2014, 6:35am UTC](https://forums.freertos.org/t/advice-on-mutex/2087/1 "2014-10-03T06:35:24Z")

</div>

**[celephicus1](https://sourceforge.net/u/celephicus1/)** wrote on [Friday, October 03, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/5d4f1923/#a6f9):

This is a generic question about mutexes. I am using an I2C library that starts a transmission with an API call, and signals completion with a callback called from an ISR.

Now, I would use mutex to allow several tasks to share the use of the I2C library, with the calling task taking the mutex before starting the transmission, and the callback giving the mutex. But the mutex give/take functions cannot be called from ISR context. I am using a semaphore but I am getting hard faults that I can’t track down.

Here is the code:

static SemaphoreHandle\_t l\_i2c\_mutex;  
static void i2cInitMutex(void) {  
l\_i2c\_mutex = xSemaphoreCreateBinary();  
xSemaphoreGive(l\_i2c\_mutex);  
}  
static void i2cAcquireI2cMutex(void) { // Called before starting I2C transmission.  
xSemaphoreTake(l\_i2c\_mutex, portMAX\_DELAY);  
}  
static void i2cReleaseI2cMutexFromIsr(void) { // Called by ISR when transmission complete.  
xSemaphoreGiveFromISR(l\_i2c\_mutex, NULL);  
}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 3, 2014, 6:50am UTC](https://forums.freertos.org/t/advice-on-mutex/2087/2 "2014-10-03T06:50:02Z")

</div>

**[tjohansen](https://sourceforge.net/u/tjohansen/)** wrote on [Friday, October 03, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/5d4f1923/#dedb):

Hi,

I think you problem is that you has NULL as param in the xSemaphoreGiveFromISR(l\_i2c\_mutex, NULL) function…

In the ISR yo need to supply the pxHigherPriorityTaskWoken parameter

See [http://www.freertos.org/a00124.html](http://www.freertos.org/a00124.html)

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 3, 2014, 7:49am UTC](https://forums.freertos.org/t/advice-on-mutex/2087/3 "2014-10-03T07:49:55Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, October 03, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/5d4f1923/#dedb/d5d3):

> I think you problem is that you has NULL as param in the  
> xSemaphoreGiveFromISR(l\_i2c\_mutex, NULL) function…
> 
> In the ISR yo need to supply the pxHigherPriorityTaskWoken parameter

That should not cause it to hard fault, but it will mean that a switch  
to a task that was waiting for the mutex being returned will not occur  
until the next tick interrupt - or until the task that is currently  
running yields (because it blocked or manually called task Yield).

From the original post:

> But the mutex  
> give/take functions cannot be called from ISR context.

The give function can be called, but the take cannot (who would be the  
mutex owner if an ISR took a mutex?). There are some test  
tasks/interrupts that do just that:  
[http://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Demo/Common/Minimal/IntSemTest.c](http://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Demo/Common/Minimal/IntSemTest.c)

How this works depends slightly on your FreeRTOS version. Which version  
are you using?

However, the code you posted is using a binary semaphore, not a mutex.

You also have not stated which chip you are using. Depending on the  
chip the most common cause of such an issue is an incorrect priority  
assignment.

Please post your FreeRTOS version and the chip you are using.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 9, 2014, 3:04am UTC](https://forums.freertos.org/t/advice-on-mutex/2087/4 "2014-10-09T03:04:01Z")

</div>

**[celephicus1](https://sourceforge.net/u/celephicus1/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/5d4f1923/#dedb/d5d3/72c8):

Sorry it took so long. FreeRTOS V8.0.1 on LPC1549 using LPCExpresso IDE.

I have now confirmed that my use of mutexes is not the problem, but I would appresciate an inswer to a related question.

I have seen code where a mutex lock is created with a call to xSemaphoreCreateMutex(), but the lock is given/taken with calls to xSemaphoreGiveFrom ISR() & xSemaphoreTake(). Is this legal?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 9, 2014, 8:10am UTC](https://forums.freertos.org/t/advice-on-mutex/2087/5 "2014-10-09T08:10:38Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/5d4f1923/#dedb/d5d3/72c8/94e6):

How that works depends on the version of FreeRTOS you are using - and  
version 8.1.2 made some improvements in that area to make it easier to  
integrate with third party software - but yes it is legal.
