Binary Semaphore runs on task initialising

phil4321 wrote on Tuesday, March 31, 2009:


I have created a binary semaphore in my module initialization code:

vSemaphoreCreateBinary( xBinarySemaphoreRxMsg );

It initializes ok,  I then create a new task to run:

xTaskCreate( vRX_MsgHandlerTask, "RX", MSG_TASK_STACK_SIZE, NULL, xTask_Priority, NULL );

All ok, here is the basic task code:

void  vRX_MsgHandlerTask( void *pvParameters )
    unsigned portSHORT i;
    //unsigned portSHORT usFrameIndex;

    ulMsgCounter = 0;
    for( ;; )
        //blocking for RX event to happen
        xSemaphoreTake( xBinarySemaphoreRxMsg , portMAX_DELAY );

        TRACE_sprintf("Processing the Data\r\n");

Now when the vRX_MsgHandlerTask starts for the first time the TRACE_sprintf("Processing the Data\r\n"); is called and outputs to the console. I am pretty sure there has been no call to xSemaphoreGive( xBinarySemaphoreRxMsg  ); it is as if the binary semaphore thinks it can take the semaphore, the code runs as expected after this has happened.

Is this a school boy error, hope you can help.


adarkar9 wrote on Tuesday, March 31, 2009:

The semaphore starts out available to be taken (check out the definition of “vSemaphoreCreateBinary” in “semphr.h”).  You must take it first outside of your task’s loop if you don’t want it to be available when your loop first runs.

phil4321 wrote on Tuesday, March 31, 2009:

Thanks for the quick response that fixed the problem. :slight_smile: