Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




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 ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists