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

Thread stack corruption when using a function that contanis a semaphore?

Posted by benval on October 17, 2014

Hi! I'm getting started with FreeRTOS and wrote a simple program that starts three threads that toggle an LED on a ARM Cortex-M4F. Each thread also outputs their ID over UART, I'm using tiny printf for that.

To prevent the threads from writing over each other, I guard the printf with a semaphore.

static void my_task(void *args) {
    TickType_t xNextWakeTime = xTaskGetTickCount();

    int id = (int) args;

    while (1) {
	xSemaphoreTake(printf_semaphore, portMAX_DELAY);
	printf("Hello from task %d!\n", id);
	xSemaphoreGive(printf_semaphore);

	/* Place this task in the blocked state until it is time to run again.
	The block time is specified in ticks, the constant used converts ticks
	to ms.  While in the Blocked state this task will not consume any CPU
	time. */
	vTaskDelayUntil( &xNextWakeTime, (id + 1) * 200 / portTICK_PERIOD_MS);

	switch (id) {
	case 0:
	    RED_LED_TOGGLE;
	    break;
	case 1:
	    GREEN_LED_TOGGLE;
	    break;
	case 2:
	    BLUE_LED_TOGGLE;
	    break;
	}
    }
}


int main(void)
{
	printf_semaphore = xSemaphoreCreateBinary();
	xSemaphoreGive(printf_semaphore);

    puts("Hello World!\n");

	xTaskCreate(my_task, "task 1", configMINIMAL_STACK_SIZE, (void*) 0, tskIDLE_PRIORITY + 1, NULL);
	xTaskCreate(my_task, "task 2", configMINIMAL_STACK_SIZE, (void*) 1, tskIDLE_PRIORITY + 1, NULL);
	xTaskCreate(my_task, "task 3", configMINIMAL_STACK_SIZE, (void*) 2, tskIDLE_PRIORITY + 1, NULL);
	vTaskStartScheduler();
}

This works as expected.

2014-10-17 15:57:09,334 - INFO # Hello World!
2014-10-17 15:57:09,336 - INFO # Hello from task 2!
2014-10-17 15:57:09,338 - INFO # Hello from task 0!
2014-10-17 15:57:09,339 - INFO # Hello from task 1!
2014-10-17 15:57:09,536 - INFO # Hello from task 0!
2014-10-17 15:57:09,736 - INFO # Hello from task 1!
2014-10-17 15:57:09,738 - INFO # Hello from task 0!
2014-10-17 15:57:09,935 - INFO # Hello from task 2!
2014-10-17 15:57:09,937 - INFO # Hello from task 0!
2014-10-17 15:57:10,136 - INFO # Hello from task 1!
2014-10-17 15:57:10,138 - INFO # Hello from task 0!
2014-10-17 15:57:10,336 - INFO # Hello from task 0!
2014-10-17 15:57:10,535 - INFO # Hello from task 2!
2014-10-17 15:57:10,537 - INFO # Hello from task 1!
2014-10-17 15:57:10,538 - INFO # Hello from task 0!
2014-10-17 15:57:10,736 - INFO # Hello from task 0!
2014-10-17 15:57:10,936 - INFO # Hello from task 1!
2014-10-17 15:57:10,938 - INFO # Hello from task 0!
2014-10-17 15:57:11,135 - INFO # Hello from task 2!
2014-10-17 15:57:11,137 - INFO # Hello from task 0!
2014-10-17 15:57:11,336 - INFO # Hello from task 1!
2014-10-17 15:57:11,338 - INFO # Hello from task 0!

Now instead of putting a semaphore around each printf, I would rather just add a wrapper around it.

void printf(char *fmt, ...) {
    xSemaphoreTake(printf_semaphore, portMAX_DELAY);

    va_list arg;
    va_start(arg, fmt);
    tfp_printf(fmt, arg);
    va_end(arg);

    xSemaphoreGive(printf_semaphore);
}

This however corrupts the threads' local variable:

2014-10-17 16:03:29,602 - INFO # Hello World!
2014-10-17 16:03:29,604 - INFO # Hello from task 2060!
2014-10-17 16:03:29,605 - INFO # Hello from task 892!
2014-10-17 16:03:29,607 - INFO # Hello from task 1476!
2014-10-17 16:03:29,803 - INFO # Hello from task 892!
2014-10-17 16:03:30,003 - INFO # Hello from task 1476!
2014-10-17 16:03:30,005 - INFO # Hello from task 892!
2014-10-17 16:03:30,202 - INFO # Hello from task 2060!
2014-10-17 16:03:30,204 - INFO # Hello from task 892!
2014-10-17 16:03:30,403 - INFO # Hello from task 1476!
2014-10-17 16:03:30,405 - INFO # Hello from task 892!
2014-10-17 16:03:30,603 - INFO # Hello from task 892!
2014-10-17 16:03:30,802 - INFO # Hello from task 2060!
2014-10-17 16:03:30,804 - INFO # Hello from task 1476!
2014-10-17 16:03:30,806 - INFO # Hello from task 892!
2014-10-17 16:03:31,003 - INFO # Hello from task 892!
2014-10-17 16:03:31,203 - INFO # Hello from task 1476!
2014-10-17 16:03:31,205 - INFO # Hello from task 892!
2014-10-17 16:03:31,402 - INFO # Hello from task 2060!

What is going on here?


Thread stack corruption when using a function that contanis a semaphore?

Posted by davedoors on October 17, 2014

First check all the normal things like ensuring you have stack overflow protection on, and configASSERT defined. http://www.freertos.org/Stacks-and-stack-overflow-checking.html http://www.freertos.org/a00110.html#configASSERT

Try changing your printf function so xSemaphoreTake is called after the arg variable is declared.


Thread stack corruption when using a function that contanis a semaphore?

Posted by heinbali01 on October 19, 2014

Hi Benjamin,

I haven't used tfp_printf() before, but when I look it up it says:

void tfp_printf(char *fmt, ...);

and not:

void tfp_printf(char *fmt, va_list param);

The compiler won't complain because the ellipsis (...) has no type-checking

Hein


[ 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