Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT




Loading

first task is replaced by second task

Posted by vincent on August 27, 2012
Hi:

I create two task with xTaskCreate(channel_task ...... ) and void channel_task(void *pvparam);

the two task share same task function, but with different input parameters. I found that the first
task 's input parameters is replayed by second task input parameters after I create second task.

how to solve it ?


vincent

RE: first task is replaced by second task

Posted by Dave on August 27, 2012
What is the parameter? A constant, or a pointer? If it is a pointer, are you passing a pointer to the same structure into both tasks?

Please post the code that creates the tasks, and the code that uses the parameter in the task.

RE: first task is replaced by second task

Posted by vincent on August 27, 2012
hi ,
the parameter is pointer
if (strcmp(tmp, "on") == 0)
{
struct relay_outlet_param outletparam;

outletparam.outletidx = channel;
outletparam.outletsts = OUTLET_ON;

xTaskCreate(outlet_task,"OUTLETON", configMINIMAL_STACK_SIZE*2,&outletparam, RS485_TASK_PRIO, NULL);

sprintf(consolemsg, "\r\n%4X", ERROR_SUCCESS);
}
else if (strcmp(tmp, "off") == 0)
{
struct relay_outlet_param outletparam;

outletparam.outletidx = channel;
outletparam.outletsts = OUTLET_OFF;

xTaskCreate(outlet_task,"OUTLETOFF", configMINIMAL_STACK_SIZE*2,&outletparam, RS485_TASK_PRIO, NULL);
sprintf(consolemsg, "\r\n%4X", ERROR_SUCCESS);
}
else if (strcmp(tmp, "reboot") == 0)
{
struct relay_outlet_param outletparam;

outletparam.outletidx = channel;
outletparam.outletsts = OUTLET_REBOOT;

xTaskCreate(outlet_task,"OUTLETRBT", configMINIMAL_STACK_SIZE*2,&outletparam, RS485_TASK_PRIO, NULL);
sprintf(consolemsg, "\r\n%4X", ERROR_SUCCESS);
}

RE: first task is replaced by second task

Posted by Dave on August 27, 2012
if (strcmp(tmp, "on") == 0)
{
struct relay_outlet_param outletparam;


In the code above you are declaring a structure on the stack with block scope.

xTaskCreate(outlet_task,"OUTLETON", configMINIMAL_STACK_SIZE*2,&outletparam, RS485_TASK_PRIO, NULL);


In the code above you are passing into a task a pointer to a structure that only has block scope.

}
else if (strcmp(tmp, "off") == 0)


When you leave the block the structure becomes invalid, so when the task runs it is accessing something on a stack frame that no longer exists. The memory might not be corrupted for a while, how long depends on when the stack frame reaches that level again. If you call the function again it will create a new stack frame and probably allocate the structure in the same stack memory again so it might be that both tasks are accessing the same area of invalid memory.

You need to allocate the structures from global memory. It can be malloced from the heap, or be two separate structures defined with file or global (instead of block) scope.

RE: first task is replaced by second task

Posted by vincent on August 27, 2012
i guess the xTaskCreate function copy the parameter in own task stack...i will have a try

RE: first task is replaced by second task

Posted by Richard Damon on August 28, 2012
xTaskCreate does copy the parameter to the tasks stack. The parameter is of course the void pointer value. FreeRTOS does not (and can not) copy the structure the pointer points to, for the very good reason that it doesn't know what it points to, if in fact it actually points to anything. If you pass pointers to data when creating tasks, that data needs to live until the task no longer needs it.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS