First project for STM32 with HAL in C ++

I would like to create a project for STM32 with HAL in C ++. Unfortunately, CubeMX only supports C projects. However, HAL C ++ support.

I created a project in CubeMX, and I tried to import it into any of the IDE above, but without any success, because it is imported as a C project. So this does not seem like a good solution.

In addition, I tried to create a C ++ project in the mentioned IDE and add files from CubeMX to it. This seems more logical, but I cannot do it right, because my configuration is still wrong.

Can someone explain to me how can I set up new C ++ projects in Eclipse or Atollic TrueSTUDIO to use HAL?

+7
source share
4 answers

Recent versions of CubeMX System Workbench 4 STM32 support, which is an Eclipse-based IDE pre-configured for development for STM32. He has the ability to convert the project to C ++ (right-click on the project in the project explorer, then select "Convert to C ++").

Please note that I have no direct experience using this method. We use STM HAL and build our applications in C ++, so this is definitely possible, but our IDE is Keil, therefore YMMV.

+6
source

How to convert Atollic Eclipse C generated STM32CubeMX project to C ++ (3 steps):

  • Copy main.c to main.cpp and exclude main.c from the assembly. Result: not working yet. Although main.cpp may appear in the list of Eclipse project files, it does not even start compiling (it does not detect explicit syntax errors) and there are linker errors for the missing main ().
  • Then add org.eclipse.cdt.core. cc nature for the .project file using a text editor, for example ... <natures> <nature>org.eclipse.cdt.core.cnature</nature> <nature>org.eclipse.cdt.core.ccnature</nature> See http://www.openstm32.org/forumthread1244 . This can be done when Eclipse works with an open project. Result: main.cpp compiles, but cannot find the include files because the include paths are incorrect. You might think that cpp nature would be a better name. Change: Search for Eclipse help for "C ++ nature" for tips on how to add C ++ nature using the File / New / Other / Convert to C / C ++ Make Project menu.
  • Then change the project settings to duplicate the corresponding C settings to C ++ settings - as shown here. enter image description here The project must be closed and reopened for the changes to take effect. Result: assemblies, links, works and works correctly. main.cpp can make HAL c code calls, and HAL callbacks can call functions in .cpp files. The extern "C" modifier may be needed in .cpp files if 1 side of .cpp calls a function on the .c side (modify function prototypes by waiting for defined extern "C" 2, side of the defined extern "C" side calls the callback defined in. cpp (change the definition of the function on the .cpp side, previously expecting the defined extern "C" .

Some notes:

  • If I ever regenerate the code using CubeMX (for example, to change the configuration of the clock or outputs), main.c will be updated, and main.cpp will not, so you need to combine the changes / differences from the .c file into a .cpp file.
  • To make sure that the C & C ++ settings are equivalent: after the build, look at the console window and compare the command line call for gcc and g ++ to make sure that the corresponding parameters (paths, -D, etc.) are identical.
  • You can (but risky) edit the XML file Eclipse.cproject and replicate the C settings to the C ++ side (specific details are omitted here - but close the project in Eclipse before editing the .cproject file).
  • Regarding the objection to "bloating" CubeMX / HAL: CubeMX generates code and designs very quickly. If the HAL API is slow - for example, access via the GPIO port - just replace the API call with a new user-defined function that contains a simpler subset of what this bloated API does. I do this often in time-critical code.
  • I am using CubeMX (4.14.0) and Atollic (v6.0.0 Lite). I did not see a project option for converting to C ++ in Atollic Eclipse, although I saw this option in other Eclipse environments, such as Xilinx Eclipse.

Opportunities for improvement: Perhaps there is a CubeMX template that can be edited to make these changes more automatic, but I do not know enough about CubeMX templates to make this hack more elegant.

I also use CubeMX with IAR EWARM and go through a similar (but slightly simpler) process to convert to a .cpp project.

+5
source

First of all, many thanks to Jim Fred for describing how to convert the Atollic Eclipse C project to C ++.

I followed the steps, but it did not work. Perhaps because I was not so deep in programming, finally failed.
I will list the steps that I have taken, maybe someone with a lot of Knowledge can help me, or can give some advice on what to do and where to look for additional information about the Topic (Compilation; Linking).

  1. The problem was that I had "unknown type" compiler errors for uint8_t and similar variables.
    The solution to this problem was simply to include "stdint-gcc.h".

  2. then the following compiler error occurred:

../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h:186lla: error: #error "First select the target device STM32L4xx used in your application (in the file stm32l4xx.h)"

target selection error

  1. Looking through the project settings, I noticed that where Hal preprocessor directives for GNU C are installed, I added them to GNU C ++ entries.

added macros to C ++ records

compilation again caused the same error:

../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h:186lla: error: #error "First select the target device STM32L4xx used in your application (in the file stm32l4xx.h)"

target selection error

  • perhaps because the C compiler and that C ++ compiler compile the same code?

  • What bothers me is that the c compiler first compiles the files, and after that the files are compiled by the C ++ compiler. Do I need to change the binding settings to bind C and C ++ together?

  • can this work when all the code is compiled in C ++ directly?

I apologize that my English is not the best. Some help would be very good :) Thanks.

0
source

Depending on which STM32 chip it supports, it may be supported by mBed. The mBed online compiler allows you to export to various IDEs. The new standalone mode (yotta) uses cmake, which can also be exported to some IDEs. Unfortunately, yotta only supports a couple of chips at the moment.

-1
source

Source: https://habr.com/ru/post/1242605/


All Articles