简体   繁体   English

STM32 RTOS (H743) 使用带有浮点格式的 sprintf 或 snprintf 时崩溃

[英]STM32 RTOS (H743) Crashing when using sprintf or snprintf with float formatting

I have read a few older threads on this issue, and quite frankly the discussion flew over my head a bit.我已经阅读了一些关于这个问题的旧帖子,坦率地说,讨论让我有点不知所措。 So I'm hoping for some help that I will hopefully be able to follow.所以我希望能得到一些帮助,我希望能够遵循。

I am programming an STM32 with RTOS (two threads needed).我正在用 RTOS 编程一个 STM32(需要两个线程)。 It's a sensor application with some fairly intensive computation on the data gathered (hence the H7).这是一个传感器应用程序,对收集的数据进行了相当密集的计算(因此是 H7)。 Computation feedback is sent through CDC in the form of a char array, size 12. Nothing difficult.计算反馈通过 CDC 以 char 数组的形式发送,大小为 12。没什么难的。 The computation feedback is a float.计算反馈是一个浮点数。 And this where I am having problems.这就是我遇到问题的地方。

Prior to sending the data I need to convert the float to a char[].在发送数据之前,我需要将浮点数转换为 char[]。

my function looks like this:我的 function 看起来像这样:

void ASCII_transmitFloat(float value) {
    uint8_t buffer[DEF_ASCII_TX_BUF];
    snprintf((char *)buffer, sizeof(buffer), "%11.9f\n", value);
    CDC_Transmit_FS(buffer, sizeof(buffer));
}

I am not getting an error, just a crash on the snprintf.我没有收到错误,只是 snprintf 崩溃了。

  • I tried sprintf with the same results我尝试了 sprintf,结果相同
  • casting my float to a uint32_t and changing the arg type in my function to uint32_t, works.将我的浮点数转换为 uint32_t 并将 function 中的 arg 类型更改为 uint32_t,可以。 (I'm losing precision so this is not a solution, but tried anyway) (我失去了精度,所以这不是一个解决方案,但无论如何都试过了)
  • I have the same version of the function for integers, and these work fine as well对于整数,我有相同版本的 function,这些也可以正常工作
  • oh and I kinda of mickey-moused between uint8_t and char, it's probably as bad as it is ugly, but I haven't found a better way yet哦,我有点像米老鼠一样在 uint8_t 和 char 之间徘徊,它可能既丑又丑,但我还没有找到更好的方法

anyhow thanks for any help you can provide无论如何感谢您提供的任何帮助

cheers干杯

edit:编辑:

Editing in response to the first response.编辑以响应第一个响应。 I had the "use float with printf" option selected in the project properties (MCU settings) - see the screenshot below (not sure if this check box does the same as adding the flag manually) I tried adding the line -u _printf_float in the linked as suggested in your link, but I have the same results.我在项目属性(MCU 设置)中选择了“使用 float 和 printf”选项 - 请参阅下面的屏幕截图(不确定此复选框是否与手动添加标志相同)我尝试在按照您的链接中的建议链接,但我有相同的结果。 Crashes when executing the snprintf.执行 snprintf 时崩溃。

在此处输入图像描述

Probably because float support is not included by default using newlib.可能是因为使用 newlib 默认不包含浮点支持。 See printf floats with newlib on STM32请参阅printf 在 STM32 上使用 newlib 浮动

Here is how I fixed the problem.这是我解决问题的方法。 The problem is known, and ST hasn't fixed the issue since it was first brought up here a year ago, see: https://community.st.com/s/question/0D50X0000BB1eL7SQJ/bug-cubemx-freertos-projects-corrupt-memory and here's the recommended fix: http://www.nadler.com/embedded/newlibAndFreeRTOS.html这个问题是已知的,自从一年前首次在这里提出以来,ST 一直没有解决这个问题,请参阅: https://community.st.com/s/question/0D50X0000BB1eL7SQJ/bug-cubemx-freertos-projects-内存损坏,这是推荐的修复方法: http://www.nadler.com/embedded/newlibAndFreeRTOS.html

A bit over my head so I chose to go the route of using a lighter version of the printf function: https://github.com/mpaland/printf A bit over my head so I chose to go the route of using a lighter version of the printf function: https://github.com/mpaland/printf

I hope it helps someone else with the issue.我希望它可以帮助其他人解决这个问题。

Cheers干杯

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

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