element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Blog Hercules Safety Microcontroller: Use C++ with HALCoGen C Projects
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 9 Nov 2019 4:35 PM Date Created
  • Views 2239 views
  • Likes 3 likes
  • Comments 4 comments
  • industrial
  • halcogen
  • automotive
  • cpp
  • c++
  • hercules
Related
Recommended

Hercules Safety Microcontroller: Use C++ with HALCoGen C Projects

Jan Cumps
Jan Cumps
9 Nov 2019

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.

image

 

 

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.

 

Related blog
Use C++ with HALCoGen C Projects

Port an MBED design to non-MBED - Phase 1: compile without errors

Port an MBED design to non-MBED - Phase 2a: DigitalOut Class Constructor
Port an MBED design to non-MBED - Phase 2b: DigitalOut Class and Blinky example
Port an MBED design to non-MBED - Phase 3: InterruptIn Class and example
Port an MBED design to non-MBED - Phase 4: Timeout Class and Blinky example
Port an MBED design to non-MBED - Phase 5: OO Callbacks Simplified
  • Sign in to reply

Top Comments

  • Jan Cumps
    Jan Cumps over 5 years ago in reply to clem57 +1
    All of the modern ones do. You could create a project with a .c and a .cpp file. Then put a clasd declaration in the .cpp file. Then watch the build log.
  • Jan Cumps
    Jan Cumps over 5 years ago

    Just a warning: If you have Class instances declared as global variables, their constructors will be calld before the main() function.

    This is normal, but for HALCoGen developers that are used to have the first line of custom code executed in the main() function, it could be a surprise.

     

    If your classes use HALCoGen API calls (gio, spi, i2c, uart, ...) in their constructor and they are created as global variables (Arrduino / MBED style) ,

    and you call gioInit, spiInit, i2cInit, sciInit, ... at the start of main (as usual), this will create issues.

     

    In a C program this is normally not an issue, because declaring a global variable in C does nor invoke a code call.

     

    Advice is to not perform any hardware manipulations in the constructors.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 5 years ago in reply to clem57

    All of the modern ones do. You could create a project with a .c and a .cpp file. Then put a clasd declaration in the .cpp file. Then watch the build log.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 5 years ago in reply to clem57

    Oh BTW, how do you test a tool set for C++ support?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 5 years ago

    I guess the following is true then:

    This works for any C project where you want to start using C++ - if the toolset supports that.

    Nice work Jan Cumps. You have come up with some real help.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube