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

A task doesn't wait for a semaphore to ...

Posted by https://www.google.com/accounts on September 11, 2012
A task that supposes to be waiting for a binary semaphore to be given starts running before that happens, why? Regards the next code snippet, task_Led2() runs as soon as the systems starts, however, what one expects is that this task should wait until task_led1() gives it the semaphore.



void task_Led1(void *p)
{
uint32_t i=0;

while(1)
{
led1_Toggle();
vTaskDelay(500);

if(++i == 5){
i=0;
xSemaphoreGive(bsmphUno);
}
}
}

void task_Led2(void *p)
{
uint32_t i=0;

for(;;)
{
xSemaphoreTake(bsmphUno,portMAX_DELAY); // doesn't wait for the semaphore to be given the first time, it goes to the for() immediatly

for(i=0;i<5;i++){
led2_Toggle();
vTaskDelay(100);
}
}
}

int main(void)
{
...
xTaskCreate(task_Led1,"",configMINIMAL_STACK_SIZE*1,NULL,1,NULL);
xTaskCreate(task_Led2,"",configMINIMAL_STACK_SIZE*1,NULL,1,NULL);

vSemaphoreCreateBinary(bsmphUno);

vTaskStartScheduler();

for(;;);

}


Any clues? Thank you!!


RE: A task doesn't wait for a semaphore to ...

Posted by MEdwards on September 11, 2012
As this is a binary semaphore, it is already available when it is created. Add a call to xSemaphoreTake( bsmphUno, 0 ) after the semaphore is created, probably best to do that inside the task when the task starts running, and it will run how you think it should.

RE: A task doesn't wait for a semaphore to ...

Posted by https://www.google.com/accounts on September 11, 2012
Hi Edwards,

It works!! I added the line you pointed me out:


...
xTaskCreate(task_Led1,"",configMINIMAL_STACK_SIZE*1,NULL,1,NULL);
xTaskCreate(task_Led2,"",configMINIMAL_STACK_SIZE*1,NULL,1,NULL);
vSemaphoreCreateBinary(bsmphUno);

xSemaphoreTake(bsmphUno,0);

vTaskStartScheduler();
...


I'm worried because this feature is not officially documented, but into a comment block inside a function in an example. I own both Barry's books, "A practical guided" and "Reference manual", from 2009. In "A practical guide"' page 76, inside vHandlerTask() function, there is an embedded comment code that says:

"/* Use the semaphore to wait for an event. The semaphore was created before the scheduler was started so before this task ran for the first time. ... */"


Hope the behaviour for the other API's functions are in fact well documented.

Thank you!!


[ 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