Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

Advice on Mutex

Posted 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 Mutex

Posted 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 Mutex

Posted 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 Mutex

Posted 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 Mutex

Posted 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.


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner