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.
Task( TaskFunction_t, const char* const, unsigned short );
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.
Task::Task(TaskFunction_t Task_Function,const char* const Task_Name, unsigned short Stack_Depth)
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.
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.
The code compiles without any error and the simulation result show the correct output.