Quality RTOS & Embedded Software

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


Counting Semaphore Loses Count???

Posted by sfalias on March 11, 2015


I'm new to FreeRTOS though I have a lot of experience with other RTOSs.

I have a serial stream coming into a UART interrupt where the characters are stored in a large buffer. I'm trying to use a counting semaphore to count one particular character, lets call it FLAG, coming in on the UART. For each FLAG, I want to Give the semaphore to a task. In my task, I pend on a Take, then read from buffer until I get FLAG, then I pend for the next Take.

This works fine until I send a lot of messages. Eventually, my task hangs on Take. Doing this in a debugger, I can stop and look at my buffer. I can see the last Take stopped at the next to last FLAG, but I can also see the UART received more characters and another FLAG that are sitting in the buffer unread.

I tried to dig into FreeRTOS to see if I could find the actual count of the semaphore when a FLAG is in the buffer but the Take doesn't see it, but all I can find is a void * and don't know where the count is. I did put in a global counter on Give and Take and I find that Give is always 1 greater than Take when the hang occurs.

It appears the Give worked but the Take didn't see it. At this point I don't know what else to think other than this is a critical timing issue.

Anyone have any ideas on how I might find this problem?



Counting Semaphore Loses Count???

Posted by rtel on March 11, 2015

To ensure our answer is targetted, please let us know which FreeRTOS port are you using (chip and compiler).


Counting Semaphore Loses Count???

Posted by sfalias on March 11, 2015

Hi, sure. I didn't think it was important, but hey, if I knew what was going on I wouldn't have posted.


PIC32MX460, 470 and 575.



Counting Semaphore Loses Count???

Posted by rtel on March 11, 2015

What is the value of configMAXSYSCALLINTERRUPT_PRIORITY in FreeRTOSConfig.h?

Have you set the priority of the interrupt to ensure it is at or below the configMAXSYSCALLINTERRUPT_PRIORITY setting?

Which method are you using to implement the interrupt? With or without the assembly wrapper? The methods are described on http://www.freertos.org/portPIC32MIPS_MK4.html

Maybe not directly related to your question, but if it is always the same task that is being signaled from the interrupt then a task notification will be faster than a counting semaphore: http://www.freertos.org/RTOSTaskNotificationAsCounting_Semaphore.html


Counting Semaphore Loses Count???

Posted by sfalias on March 11, 2015

OK, I'm old school. Intuitively (to me), I always make UART ISRs higher priority to avoid dropping characters. But doing that put the UART above configMAXSYSCALLINTERRUPT_PRIORITY. I've lowered the UART priority to 3 (same as that macro) and am now testing. So far, 60K+ messages without a failure, 5x more than before. So it appears you've found my problem. And so quickly too!



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

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.


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


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