STM32CubeMx: How to add your own “USER CODE BEGIN / END” section?

前言

看到STM32CubeMX中没有考虑所有用户代码区标记。
如果自己在官方规定的用户代码区外添加代码,当使用CubeMX重新生成代码时,会将自己手写的代码清掉。
我自己想的方法是, 使用CubeMX前,备份工程,使用CubeMX后,用BC4合并代码到新工程。
不过,一直再想,ST官方做的软件不可能这么寒蝉吧? 是不是有官方的方法可以添加自定义的用户代码区呢?
今天查资料,看到这方面的确切资料。官方居然真不支持用户自定义的代码区,真寒蝉啊! 这么大的公司作出的软件…

笔记

STM32CubeMx: How to add your own “USER CODE BEGIN / END” section?



I have a project where I need to add a few lines to one of the generated C files. However, the place where I want to add the change does not have a "USER CODE BEGIN / END" section. So whenever, I regenerate code, the changes are overwritten. I tried adding my own user code section as shown below but even that got overwritten. It seems, CubeMX is looks for a predefined set of USER CODE blocks, and overwrites everything else.

/* USER CODE BEGIN 8 */

/* USER CODE END 8 */
I would like to be able to define my own user code blocks so that I can write custom code in places where CubeMX has not already provided a user code block.
Adding custom user code sections is not supported by CubeMX.

See this support post:

https://community.st.com/s/question/0D50X0000ALxNlmSQF/is-it-possible-to-add-custom-user-code-sections

Is it possible to add custom USER CODE sections?

Is it possible to add custom USER CODE sections?
We are using the ST MCSDK, it is very helpful. In MCSDK/MCLib/Any/Src there is a function called STM_FaultProcessing. It is auto generated by Cube. We have one line we want to add to it that turns an LED on alerting the user to a problem.

 

This works, but if we then auto generate code, it gets removed.

 

We attempted to put our own user code brackets around the line to protect it:

官方管理员这么说的。

 (Community Member)

a year ago
USER CODE sections can't be user created. Workaround maybe needed with side effects to take care.

 

For example, find the master source directory which files will be copied at generation, and edit it.

Next library upgrade, pouf! In the meantime, you can edit the code without any user code as long as you track what you changes.

 

Otherwise, copy your project to safe haven, and compare the new generated code vs your reference for update. Windiff mastering required.

总结

其实如果官方不支持的东西,大家想的替代方法都差不错。

STM32CubeMx: How to add your own “USER CODE BEGIN / END” section?STM32CubeMx: How to add your own “USER CODE BEGIN / END” section? LostSpeed 发布了446 篇原创文章 · 获赞 126 · 访问量 176万+ 他的留言板 关注
上一篇:默认参数函数,引用


下一篇:500以内所有的奇数偶数