简体   繁体   English

带有 HAL 的 C++ STM32 的第一个项目

[英]First project for STM32 with HAL in C++

I would like to create a project for STM32 with HAL in C++.我想用 C++ 用 HAL 为 STM32 创建一个项目。 Unfortunately, CubeMX supports only projects in C. However, HAL support C++.不幸的是,CubeMX 仅支持 C 中的项目。但是,HAL 支持 C++。

I have created a project in CubeMX and I was trying to import it to any of the IDEs above but without any success because it is imported as a C project.我在 CubeMX 中创建了一个项目,我试图将它导入到上面的任何 IDE,但没有成功,因为它是作为 C 项目导入的。 So it doesn't look like a good solution.所以这看起来不是一个好的解决方案。

Also, I have tried creating a C++ project in the mentioned IDE and adding to it files from CubeMX.此外,我尝试在提到的 IDE 中创建一个 C++ 项目,并从 CubeMX 添加文件。 It seems to be more logic but I can't do it in the right way because my configuration is still wrong.这似乎更符合逻辑,但我无法以正确的方式做到这一点,因为我的配置仍然是错误的。

Could anyone explain me how should I configure new C++ projects in Eclipse or Atollic TrueSTUDIO in order to use HAL?谁能解释一下我应该如何在 Eclipse 或 Atollic TrueSTUDIO 中配置新的 C++ 项目以使用 HAL?

Recent versions of CubeMX support System Workbench 4 STM32, which is an Eclipse-based IDE pre-configured to develop for STM32 targets. CubeMX 的最新版本支持 System Workbench 4 STM32,这是一个基于 Eclipse 的 IDE,已预先配置为针对 STM32 目标进行开发。 It has the option to convert a project to C++ (Right-click on the project in the project explorer, then select "Convert to C++").它具有将项目转换为 C++ 的选项(在项目资源管理器中右键单击该项目,然后选择“转换为 C++”)。

Note that I have no direct experience using this method.请注意,我没有使用这种方法的直接经验。 We use the STM HAL and build our C++ applications on top of that, so this is definitely possible, but our IDE is Keil, so YMMV.我们使用 STM HAL 并在此基础上构建我们的 C++ 应用程序,所以这绝对是可能的,但我们的 IDE 是 Keil,所以 YMMV。

How to convert a STM32CubeMX-generated Atollic Eclipse C project to C++ (3 steps):如何将 STM32CubeMX 生成的 Atollic Eclipse C 项目转换为 C++(3 个步骤):

  • Copy main.c to main.cpp and 'exclude' main.c from build.将 main.c 复制到 main.cpp 并从构建中“排除”main.c。 Result: Doesn't quite work yet.结果:还没有完全奏效。 Although main.cpp might appear in the Eclipse project file list, it doesn't even begin to compile (won't catch obvious syntax errors) and there are linker errors for a missing main().尽管 main.cpp 可能出现在 Eclipse 项目文件列表中,但它甚至没有开始编译(不会捕获明显的语法错误)并且存在缺少 main() 的链接器错误。
  • Next, Add org.eclipse.cdt.core.接下来,添加 org.eclipse.cdt.core。 cc nature to the .project file with a text editor, like this... <natures> <nature>org.eclipse.cdt.core.cnature</nature> <nature>org.eclipse.cdt.core.ccnature</nature> See http://www.openstm32.org/forumthread1244 .立方厘米性质.project文件用文本编辑器,像这样... <natures> <nature>org.eclipse.cdt.core.cnature</nature> <nature>org.eclipse.cdt.core.ccnature</nature>http://www.openstm32.org/forumthread1244 This can be done while Eclipse is running with the project open.这可以在 Eclipse 运行且项目打开时完成。 Result: main.cpp compiles but it can't find include files because the include paths are wrong.结果:main.cpp 编译但找不到包含文件,因为包含路径错误。 You'd think cpp nature would have been a better name.你会认为cpp nature会是一个更好的名字。 Edit: search Eclipse help for "C++ nature" for tips on how to add C++ nature using menu File / New / Other / Convert to C/C++ Make Project.编辑:在 Eclipse 帮助中搜索“C++ 自然”,获取有关如何使用菜单文件/新建/其他/转换为 C/C++ 制作项目添加 C++ 自然的提示。
  • Next, Modify project settings to duplicate pertinent C settings over to C++ settings - as shown here.接下来,修改项目设置以将相关的 C 设置复制到 C++ 设置 - 如此处所示。 在此处输入图片说明 The project needs to be closed and re-opened for this change to take effect.需要关闭并重新打开项目才能使此更改生效。 Result: builds, links, runs and runs correctly.结果:构建、链接、运行和正确运行。 main.cpp can make calls to HAL c code and HAL callbacks can call functions in .cpp files. main.cpp 可以调用 HAL c 代码,HAL 回调可以调用 .cpp 文件中的函数。 An extern "C" modifier may be needed in .cpp files if 1 the .cpp side is calling a function on the .c side (modify function prototypes by pre-pending defined extern "C" 2 .c side side is calling a callback defined in a .cpp (modify the function definition on the .cpp side by pre-pending defined extern "C" .如果1 .cpp 端正在调用 .c 端的函数(通过预先defined extern "C"修改函数原型来修改函数原型) 2 .c 端正在调用回调,则 .cpp 文件中可能需要一个 extern“C”修饰符在 .cpp 中定义(通过预先defined extern "C"修改 .cpp 端的函数定义。

Some notes:一些注意事项:

  • If I ever re-generate code with CubeMX (eg, to change a clock or pin-configuration), main.c will get updated but main.cpp will not - so it's necessary to merge changes/diffs from the .c file over to the .cpp file.如果我使用 CubeMX 重新生成代码(例如,更改时钟或引脚配置),main.c 将得到更新,但 main.cpp 不会 - 因此有必要将 .c 文件中的更改/差异合并到.cpp 文件。
  • How to be certain the C & C++ settings are equivalent: After a build, look at the Console window and compare the command-line invocation for gcc and g++ to make sure pertinent parameters (paths, -D etc) are identical.如何确定 C 和 C++ 设置等效:构建后,查看控制台窗口并比较 gcc 和 g++ 的命令行调用,以确保相关参数(路径、-D 等)相同。
  • It's possible (but risky) to edit the Eclipse .cproject XML file and replicate the C settings over to the C++ side (specific details omitted here - but close project in Eclipse before editing the .cproject file).可以(但有风险)编辑 Eclipse .cproject XML 文件并将 C 设置复制到 C++ 端(此处省略了特定细节 - 但在编辑 .cproject 文件之前关闭 Eclipse 中的项目)。
  • Regarding objections to CubeMX/HAL 'bloat': CubeMX generates code and projects very quickly.关于对 CubeMX/HAL“膨胀”的反对意见:CubeMX 生成代码和项目的速度非常快。 If a HAL API is slow - eg, GPIO port-pin access - then simply replace the API call with a new user-function containing a simpler subset of what that bloated API does.如果 HAL API 很慢——例如,GPIO 端口引脚访问——那么只需用一个新的用户函数替换 API 调用,该函数包含那个臃肿的 API 所做的更简单的子集。 I do this frequently in time-critical code.我经常在时间关键的代码中这样做。
  • I'm using CubeMX (4.14.0) and Atollic (v6.0.0 Lite).我正在使用 CubeMX (4.14.0) 和 Atollic (v6.0.0 Lite)。 I didn't see the project option to convert to C++ in the Atollic Eclipse although I have seen that option in other Eclipse environments eg, Xilinx Eclipse.我没有在 Atollic Eclipse 中看到转换为 C++ 的项目选项,尽管我在其他 Eclipse 环境(例如 Xilinx Eclipse)中看到了该选项。

Room for improvement: There might be a CubeMX template that could be edited to make these changes more automatic but I don't know enough about CubeMX templates to make this hack more elegant.改进空间:可能有一个 CubeMX 模板可以编辑,使这些更改更加自动化,但我对 CubeMX 模板了解得不够多,无法使这个 hack 更加优雅。

I use CubeMX with IAR EWARM too and go through a similar (but slightly easier) process to convert to a .cpp project.我也将 CubeMX 与 IAR EWARM 一起使用,并通过类似(但稍微简单一些)的过程转换为 .cpp 项目。

there is very simple way to build a cpp project in stm32cubeidestm32cubeide构建 cpp 项目的方法非常简单

  1. after making new stm32 project check the c++ box制作新的 stm32 项目后,选中 c++ 框
  2. do the initialize做初始化
  3. compile the code in main folder编译主文件夹中的代码
  4. press right click and select source folder按右键单击并选择源文件夹
  5. put your cpp file in it把你的cpp文件放在里面
  6. for call this cpp functions in main.c in cpp file make a simple function like main2()为了在 cpp 文件中的main.c中调用这个 cpp 函数,创建一个像main2()这样的简单函数
  7. that you call it from main.c now you can use cpp function through main2() function你从main.c调用它现在你可以通过main2()函数使用 cpp 函数

Depending on which STM32 chip it is, it might be supported by mBed.根据它是哪种 STM32 芯片,mBed 可能支持它。 The online mBed compiler allows exporting to a variety of IDEs.在线 mBed 编译器允许导出到各种 IDE。 The new offline one (yotta) uses cmake which can export to some IDEs too.新的离线 (yotta) 使用 cmake,它也可以导出到一些 IDE。 Unfortunately yotta only supports a couple of chips at the moment.不幸的是,yotta 目前只支持几个芯片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM