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

switching from mutex to binary semaphore does not work

Posted by williamjsell on August 12, 2015

I made an error as I was taking a mutex from one task and giving it back in another task. This is not the correct way to signal tasks, so I replaced the mutex with a binary sempaphore:

for(;;)
{
    //wait forever for a message.  This updates the global variable "device"
    xQueueReceive(calibrationQueue, &calDev, portMAX_DELAY);
    //wait forever for the resource
    xSemaphoreTake(calibrationSmphr, portMAX_DELAY);
    //now update the public device
    memcpy(&calibrateDevice, calDev, sizeof(_calibration_type_t));
    //call the calibrate task
    xEventGroupSetBits(xCalibrationGroup, calibrateDevice.calType);
}

The problem is the xSempahoreTake never returns on the first call to the function, so my system never gets started. Basically this task queues requests and waits for the semaphore to be available, before setting another event. When the calibration event is complete it gives back the semaphore. What am I doing wrong? I checked the semaphore has been properly allocated from CreateBinarySemaphore. When I substiture the mutex rather than binary semaphore, everything works.


switching from mutex to binary semaphore does not work

Posted by rtel on August 12, 2015

Which function are you using to create the semaphore? If you are using xSemaphoreCreateMutex() (which is the correct function to use in newer versions of FreeRTOS), then the semaphore is created empty. Whereas if you create a mutex, then the mutrex is created full. Hence the difference in the behaviour you observed.

If you want the semaphore to be available the first time you call the function, then give the semaphore immediately after you have created it. Then, inside the function where it is used, the first 'take' operation will pass.

See the comments in the source code example at the bottom of the following page: http://www.freertos.org/xSemaphoreCreateBinary.html

Regards.


switching from mutex to binary semaphore does not work

Posted by williamjsell on August 12, 2015

Thanks, I missed the part about giving before using the semaphore...!

Tx


[ 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