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

Problem Running Tasks From Within C++ Object

Posted by A S on January 21, 2013
NXP17xx port:

Have simplified problem as follows: Create and run two simple tasks with for loops and vTaskDelays from a file called main.cpp. When I try to create and execute those same tasks from within an object, the program goes off into the weeds. Note I'm able to to create the tasks and within certain conditions they will run. If appears that if I make any API call from within a task or the scheduler runs, that that is when the program fails.

For example task A has priority 2, task B has priority 3. If I have a while loop within Task B that updates a counter, all works fine. If I add a vTaskDelay() within that loop, the program fails when this function is entered.

If I create Task A and B both with the same priority, the programs fails. I can put a breakpoint in the tasks and one task will start, my guess is that when the scheduler goes to start the other task, the program fails.

Before I go doing into this much further to find out where I screwed up things, wondering if there's something fundamentally wrong with creating tasks from within an object.

Thanks.






RE: Problem Running Tasks From Within C++ Object

Posted by Richard Damon on January 21, 2013
I am not sure what you mean by "creating a task from within an object". One important thing to remember is that task functions should be declared "extern C" and their signature must be void taskfunc(void*); and thus can NOT be a normal member function.

When I built a c++ wrapper for FreeRTOS, I had a static member function that made a base class that had a private static member function, that would cast the void* pointer to the type of the class, and call a virtual member function on that pointer. The constructor for that class would create a task using its this pointer for the void* parameter, and the rest of the parameters for task creation were needed to be passed to the constructor which forwarded them to the create task function and stored the task handle in a member variable.

RE: Problem Running Tasks From Within C++ Object

Posted by A S on January 29, 2013
Richard,

Thanks for the response. I got sidetracked onto another issue and just got back to this. I just tried building with the C++ wrapper you referenced (see below), I get build errors regarding the static_cast keyword.

static_cast(parm)->task();

The compiler expects a '<' and '<'' characters enclosing "parm". I'm also not familiar with using '(' and ')' with the static_cast keyword. What am I doing wrong?

Also, could you provide a short stub using the class with a function per your description above?

Thanks, Andy ...

class TaskBase {
public:
xTaskHandle handle;

~TaskBase() {
#if INCLUDE_vTaskDelete
vTaskDelete(handle);
#endif
return;
}
};

class Task : public TaskBase {
public:
Task(char const*name, void (*taskfun)(void *), unsigned portBASE_TYPE priority,
unsigned portSHORT stackDepth=configMINIMAL_STACK_SIZE) {
xTaskCreate(taskfun, (signed char*)name, stackDepth, this, priority, &handle);
}

};

class TaskClass : public TaskBase {
public:
TaskClass(char const*name, unsigned portBASE_TYPE priority,
unsigned portSHORT stackDepth=configMINIMAL_STACK_SIZE) {
xTaskCreate(&taskfun, (signed char*)name, stackDepth, this, priority, &handle);
}
virtual void task() = 0;
static void taskfun(void* parm) {
static_cast(parm)->task();
#if INCLUDE_vTaskDelete
xTaskDelete(handle);
#else
while(1)
vTaskDelay(10000);
#endif
}
};


RE: Problem Running Tasks From Within C++ Object

Posted by Joshua Napoli on January 29, 2013
The static_cast looks like a typo. Try "static_cast(param)->task()"

RE: Problem Running Tasks From Within C++ Object

Posted by Richard Damon on January 30, 2013
Yes, that is what that line should look like. I bet that something thought the contents of the angle brackets was an HTML tag and removed it.

For Task, you just create a "Task" object, pass it the parameters needed, including a task function, and the task will be created and run, where I find it useful is to create the Task object as a global, so the task is created without needing to add the lines to the main function. This allows different version to be created by just adding needed modules into the project, without needing to change the main file.

For TaskClass you derive your class you want to run as a task from TaskClass, and define in it member function task as

virtual void task();

Each object of that class will cause a task to be created, and run on that object. Again, the object can be created as a global and get created automatically.


RE: Problem Running Tasks From Within C++ Object

Posted by A S on January 30, 2013
Thanks to all for the support.


[ 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