TI Hercules microcontrollers are "functional safety" devices for automotive, industrial and medical use.
When you create a new firmware project for this family, you use the TI HALCoGen tool to configure the peripherals and create the source.
This generated souce code is in C.
If you want to develop in C++, because <enter your random reason here>, here's a low impact approach.
It will take care that your source files stay HALCoGen compatible and you can do round trip modifications without loosing anything.
At the same time, it will allow you to use your own and external C++ objects.
This works for any C project where you want to start using C++ - if the toolset supports that.
Project Generation with HALCoGen
You don't have to do anything specific. Just use TI's instructions and generate a project according to your needs.
You enable and configure drivers as if you are doing a plain C project.
Create a C to C++ Bridge
Here's the trick: To step from the HALCoGen generated C main(), we want to jump to C++.
For that, generate a folder in your project called cpp (this is optional. I do that to split C and C++ and create a clear boundary)
Add that Folder to your include path.
In that folder, create a class that will serve as the bridge
I called it SemTechSx1276 because I want to port the SemTech MBED libraries without refactoring from C++ to C.
Header:
/* * SemTechSx1276.h * * Created on: 9 nov. 2019 * Author: jancu */ #ifndef CPP_SEMTECHSX1276_H_ #define CPP_SEMTECHSX1276_H_ class SemTechSx1276 { public: SemTechSx1276(); virtual ~SemTechSx1276(); static void main(); }; #endif /* CPP_SEMTECHSX1276_H_ */
Source:
/* * SemTechSx1276.cpp * * Created on: 9 nov. 2019 * Author: jancu */ #include <cpp/SemTechSx1276.h> // start Hercules HAL includes #include "HL_gio.h" #include "HL_mibspi.h" #include "HL_sci.h" // end Hercules HAL Includes extern "C" void SemTechSx1276Bridge() { SemTechSx1276::main(); } SemTechSx1276::SemTechSx1276() { } SemTechSx1276::~SemTechSx1276() { } void SemTechSx1276::main() { // C++ business starts here. This is your new main() // start Hercules HAL init gioInit(); mibspiInit(); sciInit(); // end Hercules HAL init } }
What is critical here, is that you put extern "C" in front of the bridge function.
extern "C" void SemTechSx1276Bridge() { SemTechSx1276::main(); }
That will take care that the compiler will not mess with the function name and that the linker will be able to link it with your main() function in the HALCoGen generated C source.
Use the C to C++ Bridge
In your HALCoGen generated main file, you first declare this bridge function near the start of the file. This will avoid warnings that you're trying to use an undeclared function.
Take care to put it between /* USER CODE BEGIN (1) */ and /* USER CODE END */. This is to keep the HALCoGen round-trip functionality intact.
extern void SemTechSx1276Bridge();
The extern clause isn't strictly neccessary. Comment below if you think I should not have added that.
Then, in the HALCoGen generated main() function, call the bridge.
This will take care that you immediately switch to the C++ realm.
/* USER CODE BEGIN (2) */ /* USER CODE END */ int main(void) { /* USER CODE BEGIN (3) */ SemTechSx1276Bridge(); /* USER CODE END */ return 0; }
This little post may help you to use C++ in your Hercules projects.
Top Comments