Semaphore Active on Start of Scheduler

rslisiecki wrote on Monday, January 14, 2013:

I am seeing a semaphore active just after the start of the scheduler but before the tick interrupts start.  My tick ISR “gives” the semaphore and I have a simple task that blocks on the semaphore.  How can the semaphore be active before the first tick of the tick ISR?  I am monitoring the status of the scheduler, task and tick ISR using an oscilloscope and some port lines.  I am using an MSP430F169 processor with code modified from your “MSP430X_MSP430F5438_CCS” demo project.  Everything seems to be working with installed FreeRTOS at this time except for this anomaly.  I have no stack or heap overflow issues.

Here’s what the tic ISR looks like.  global_ulTest1 is set to 0 in main() before the scheduler is started.  There are no other interrupts active in this system.

#pragma vector=configTICK_VECTOR
interrupt void vTickISREntry( void )
  //extern void vPortTickISR( void );
  extern void vPortPreemptiveTickISR( void );
  //extern void vPortCooperativeTickISR( void );

  static int nCount;

  static portBASE_TYPE xHigherPriorityTaskWoken;

  xHigherPriorityTaskWoken = pdFALSE;

  // Use a hardware counter to tell when to signal vTask1 using the semaphore
  if (global_ulTest1 >= 512) // once a second
    global_ulTest1 = 0;

    // Give the semaphore to the handler, in this case, vTask1
    xSemaphoreGiveFromISR(xBinarySemaphoreTest1, &xHigherPriorityTaskWoken);

    if (xHigherPriorityTaskWoken == pdTRUE)
      // Perform a required context swtich in the MSP430 context


  // Make sure that all of the clock oscillators and the CPU are turned-on
  // They could be off if the designer put the processor into a sleep
  // state for the idle task.
  //__bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );

  // The following calls to FreeRTOS allow it to update its tick counter
  // and manage tasks that are blocked.
  // Note: This project uses configUSE_PREEMPTION = 1
#if configUSE_PREEMPTION == 1
vPortPreemptiveTickISR(); // Note: This is a call to an assembly routine in portext.asm
vPortCooperativeTickISR(); // Note: This is a call to an assembly routine in portext.asm

  // Toggle the red LED (X30) on the iBox board in order to watch the tail of this ISR.
  for (nCount = 0; nCount < 30; nCount++)
    P5OUT = P5OUT & 0XFD;
    P5OUT = P5OUT | 0X02;

  // Clear Timer_A’s interrupt flag to allow another tick
  TACTL =              // Timer_A Control Register
    (TASSEL1    * 0) | // Timer_A clock source select: 00=TACLK
    (TASSEL0    * 1) | // 01=ACLK 10=SMCLK 11=INCLK
    (ID1        * 0) | // Input divider: 00=/1 01=/2 10=/4 11=/8
    (ID0        * 0) | //
    (MC1        * 0) | // Mode control: 00=stop mode (timer halted) 01=up mode
    (MC0        * 1) | // 10=continuous mode 11=up/down mode
    (TACLR      * 0) | // Timer_A clear: 1=reset TAR and divider
    (TAIE       * 1) | // Timer_A interrupt enable: 0=disabled
    (TAIFG      * 0);  // Timer_A interrupt flag: 0=no interrupt pending

} // end of interrupt void vTickISREntry( void )

// End of port.c

travfrog wrote on Monday, January 14, 2013:

Hi Ron,

When you create the semaphore, do you take it immediately? when created, the RTOS appears to give it and if you don’t take it, the next attempt to take it will work - and your interrupt did not give it.


rslisiecki wrote on Tuesday, January 15, 2013:


I do not take the semaphore immediately.  It is initially given by the tick ISR one second after the scheduler is started and at one second intervals thereafter.  I have only one task waiting to take the semaphore.

However, I have fixed this problem.  I performed a “take” of the semaphore just before I created the tasks and started the scheduler.  When I do this, I do not see the semaphore active in my task immediately after the scheduler starts but see it one second afterward just like it is supposed to do.

Here’s my new code in main() that works just fine.

// Create a binary semaphore between the ISR for TIMERA1 (the tick counter) and vTask1

// Create the queue used by the tasks
xTestQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( xQueueMessage ) );

// If the queue or semaphore could not be created then don’t create any tasks that might
// attempt to use the queue or the semaphore.

if ((xTestQueue != NULL) && (xBinarySemaphoreTest1 != NULL))
  // Clear the semaphore before starting the scheduler
  xSemaphoreTake(xBinarySemaphoreTest1, 0);

  // Create Task1
  xTaskCreate(vTask1, (signed char *)“Task_1”, 100, NULL, 1, NULL);

  // Create Task2
  xTaskCreate(vTask2, (signed char *)“Task_2”, 100, NULL, 1, NULL);

  // Start the scheduler.
  // Just wait here if there was a problem creating the test queue and semaphore
  while (TRUE)

Therefore, I’m pretty sure that FreeRTOS has a small bug associated with the startup of a semaphore.  I believe that the expectation of any programmer would be that on creation of a semaphore, the semaphore should be inactive until a “give” function is executed.  This currently doesn’t seem to be the case.  The semaphore is active right when its created.  If it wasn’t then why did my “take” function just before the task creations and scheduler make my system work?

Let me know if you think differently.



travfrog wrote on Tuesday, January 15, 2013:

Hi Ron, agreed.

The RTOS gives the semaphore upon creation (I believe this means it puts the semaphore into the 1-deep Q thereby making it available). I have several tasks that create and use semaphores and each one of them takes the semaphore during task creation.