lagavulin18 wrote on Saturday, January 18, 2014:
Ok, I think I understand the runTask() issue and the resulting lost of link to the objects t1 and t2.
In the meantime I have revised my code, oriented on your example:
http://www.freertos.org/FreeRTOS_Support_Forum_Archive/July_2010/freertos_Is_it_possible_create_freertos_task_in_c_3778071.html
But how can I declare both C++ tasks from the runTask() function as global or static to fix my missing link issue? Or in other words how can I access both tasks from the FreeRTOS main()?
FreeRTOSTask.h
#ifndef FREERTOSTASK_H_
#define FREERTOSTASK_H_
#include <stdio.h>
#include <stdlib.h>
#include <LPC1768Function.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <FreeRTOS.h>
#include <task.h>
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
// BaseTask ---------------------------------------------------------
class FreeRTOSBaseTask {
public:
xTaskHandle xth;
};
// TaskClass ---------------------------------------------------------
class FreeRTOSTask : public FreeRTOSBaseTask {
public:
FreeRTOSTask(char const* name,
unsigned portBASE_TYPE priority,
unsigned portSHORT stackDepth);
virtual void task() = 0;
static void tskfunc(void* parm);
};
// Task 1 ---------------------------------------------------------
class Task1 : public FreeRTOSTask {
public:
Task1(char const* name,
unsigned portBASE_TYPE priority,
unsigned portSHORT stackDepth);
void task();
};
// Task 2 ---------------------------------------------------------
class Task2 : public FreeRTOSTask {
public:
Task2(char const* name,
unsigned portBASE_TYPE priority,
unsigned portSHORT stackDepth);
void task();
};
#endif
// ------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
void runTask();
#ifdef __cplusplus
}
#endif
// -------------------------------------------------------------------
#endif /* FREERTOSTASK_H_ */
FreeRTOSTask.cc
#include <FreeRTOSTask.h>
// -------------------------------------------------------------------
FreeRTOSTask::FreeRTOSTask(char const* name,
unsigned portBASE_TYPE priority,
unsigned portSHORT stackDepth) {
xTaskCreate(&tskfunc, (signed char*) name, stackDepth, this, priority, &xth);
}
void FreeRTOSTask::tskfunc(void* param) {
static_cast<FreeRTOSTask *>(param)->task();
}
// Task-1---------------------------------------------------------------
Task1::Task1(char const* name,
unsigned portBASE_TYPE priority,
unsigned portSHORT stackDepth) :
FreeRTOSTask(name, priority, stackDepth) {
}
void Task1::task() {
volatile unsigned long ul;
for( ;; )
{
for( ul = 0; ul < 0xffffff; ul++)
{
UARTPuts_(LPC_UART2, "\r\nFreeRTOS Task1 is running\r\n");
LED_On(7);
LED_On(5);
LED_On(3);
LED_On(1);
}
}
}
// Task-2 -------------------------------------------------------------
Task2::Task2(char const* name,
unsigned portBASE_TYPE priority,
unsigned portSHORT stackDepth) :
FreeRTOSTask(name, priority, stackDepth) {
}
void Task2::task() {
volatile unsigned long ul;
for( ;; )
{
for( ul = 0; ul < 0xffffff; ul++)
{
UARTPuts_(LPC_UART2, "\r\nTask2 running\r\n");
LED_Off(7);
LED_Off(5);
LED_Off(3);
LED_Off(1);
}
}
}
// ----------------------------------------------------------------
void runTask(){
Task1 *tsk1;
tsk1 = new Task1("Task1",1,1000);
tsk1->task();
Task2 *tsk2;
tsk2 = new Task2("Task2",1,1000);
tsk2->task();
}
FreeRTOS main()
/*-----------------------------------------------------------*/
int main( void )
{
prvSetupHardware();
runTask();
/* Start the scheduler. */
vTaskStartScheduler();
// Will only get here if there was insufficient memory to create the idle
// task. The idle task is created within vTaskStartScheduler(). */
for( ;; );
}
/*-----------------------------------------------------------*/