FreeRTOS Support Archive
The FreeRTOS support forum is used to obtain active support directly from Real
Time Engineers Ltd. In return for using our top quality software and services for
free, we request you play fair and do your bit to help others too! Sign up
to receive notifications of new support topics then help where you can.
This is a read only archive of threads posted to the FreeRTOS support forum.
The archive is updated every week, so will not always contain the very latest posts.
Use these archive pages to search previous posts. Use the Live FreeRTOS Forum
link to reply to a post, or start a new support thread.
[FreeRTOS Home] [Live FreeRTOS Forum] [FAQ] [Archive Top] [October 2014 Threads] Advice on MutexPosted by celephicus1 on October 3, 2014 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 SemaphoreHandlet li2cmutex;
static void i2cInitMutex(void) {
li2cmutex = xSemaphoreCreateBinary();
xSemaphoreGive(li2cmutex);
}
static void i2cAcquireI2cMutex(void) { // Called before starting I2C transmission.
xSemaphoreTake(li2cmutex, portMAXDELAY);
}
static void i2cReleaseI2cMutexFromIsr(void) { // Called by ISR when transmission complete.
xSemaphoreGiveFromISR(li2cmutex, NULL);
}
Advice on MutexPosted by tjohansen on October 3, 2014 Hi,
I think you problem is that you has NULL as param in the xSemaphoreGiveFromISR(li2cmutex, NULL) function...
In the ISR yo need to supply the pxHigherPriorityTaskWoken parameter
See http://www.freertos.org/a00124.html
Advice on MutexPosted by rtel on October 3, 2014
I think you problem is that you has NULL as param in the
xSemaphoreGiveFromISR(li2cmutex, 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
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.
Advice on MutexPosted by celephicus1 on October 9, 2014 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?
Advice on MutexPosted by rtel on October 9, 2014 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.
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.
|