Integrating FreeRTOS with C++

FreeRTOS has been using C language as its base programming language ever since it started. So, we decided to try using C++ with FreeRTOS. One of the advantages which C++ provides to FreeRTOS is the usage of class. By using class, users can supposedly create task with less syntax and coding lines.
First of all, a class.hpp header file is to be created which will contain the class information of the task creation class. This is to separate the class syntax from the main program so the main program will not get messy and the class syntax will be hidden from the user. The class header file is located at the /Source/portable/AEMB2 folder along with port.c, portmacro.h and other header files. Inside the header file itself, the class Task is defined with its constructor and destructor. The coding is as shown below.
//CLASS.h
#ifndef CLASS_H
#define CLASS_H
class Task
{
public:
Task( TaskFunction_t, const char* const, unsigned short );
~Task(){}
};
#endif

Inside the class.cc file, the Task constructor and other task members can be defined here. In this case, I am going to test the compilation of code with these header files so I just call the FreeRTOS task creation function xTaskCreate inside the Task constructor.

//CLASS.CC
#include "FreeRTOSConfig.h"
#include "FreeRTOS.h"
#include "task.h"
#include
#include "class.hpp"
Task::Task(TaskFunction_t Task_Function,const char* const Task_Name, unsigned short Stack_Depth)
{
xTaskCreate( Task_Function,
Task_Name,
Stack_Depth,
NULL,
1,
NULL
);
}

What this class does is that it will accept the function pointer of the task, a string for the task name, and stack value, then it will create the task using xTaskCreate function. The constructor will do all of this automatically when an object of this type is created.
Now that I have a header file and a class definition file, I need to call both of them inside the Makefile to make a class.o object file which will be linked together with other object files during code compilation. The following lines is added inside the Makefile.
class.o: ../../Source/portable/AEMB2/class.cc ../../Source/portable/AEMB2/class.hpp $(FREERTOS)
$(CXX) $(COPTS) $(OOPTS) $(FRTOSINC) -c ../../Source/portable/AEMB2/class.cc

The line of freertos.a is added with class.o
freertos.a: class.o list.o queue.o tasks.o port.o heap.o portasm.o #crt.o
$(AR) cr freertos.a *.o

After all of this are done, it is time to make some changes in the main program main.cc. The first change inside the main program is to include the header file.
#include "class.hpp"
Next thing is to use the class with task creation. Using the previous three tasks for this experiment, I implement the class Task calling the three tasks out inside the main body.
Task Task1(vTestTask1,"T0",1000);
Task Task2(vTestTask2,"T1",1000);
Task Task3(vTestTask3,"T2",1000);

The code compiles without any error and the simulation result show the correct output.

class

You may also like...

6 Responses

  1. Juergen says:

    Hello Er Wen Yang
    has C++ support developed in FreeRTOS? I see the great benefits of coding in classes, too. So I’d love to combine it with the RTOS. Is there a bigger community for help? Any tutorials to build up a (free) IDE (for STM microcontrollers) that supports C++ programming?
    Thanks a lot!
    Juergen

    • Shawn Tan says:

      We’ve only managed to get some primitive c++ support working. However, our work is focused on our processor. While it might work with other processors, it’s untested.

  2. Guru says:

    There is a IDE which exists already, Coocox CoIDE, you can use C++ and it has lot of microcontrollers support

    • shamna says:

      Is there any way to work FREE rtos with cpp extension in STM microcontroller

      • Shawn Tan says:

        It should not be a problem as the C++ extension is built on-top of C, without any assembly code.

  3. Some of you may be interested in my open-source RTOS for ARM Cortex-M microcontrollers – it’s written entirely in C++11, enabling features not even imaginable with simple C++ wrappers for RTOSes written in C. How about using a member function or a lambda as a thread (or software timer), passing any number of any arguments to the thread function (or to software timer), sending complex objects (which require copy or move construction/assignment – like std::unique_ptr) via message queues and so on?

    http://distortos.org/

Leave a Reply