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

Semaphore Active on Start of Scheduler

Posted by Ron Lisiecki on 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
global_ulTest1++;
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
vTaskSwitchContext();
}

}

// 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
#else
vPortCooperativeTickISR(); // Note: This is a call to an assembly routine in portext.asm
#endif


// 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

RE: Semaphore Active on Start of Scheduler

Posted by Richard on 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.

travfrog

RE: Semaphore Active on Start of Scheduler

Posted by Ron Lisiecki on January 15, 2013
travfrog:

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
vSemaphoreCreateBinary(xBinarySemaphoreTest1);

// 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.
vTaskStartScheduler();
}
else
{
// 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.

Thanks,

Ron

RE: Semaphore Active on Start of Scheduler

Posted by Richard on 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.

travfrog


[ 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