1

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). It's a sensor application with some fairly intensive computation on the data gathered (hence the H7). Computation feedback is sent through CDC in the form of a char array, size 12. Nothing difficult. 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[].

my function looks like this:

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.

  • I tried sprintf with the same results
  • casting my float to a uint32_t and changing the arg type in my function to uint32_t, works. (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
  • 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

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. Crashes when executing the snprintf.

enter image description here

zytra
  • 87
  • 1
  • 11

3 Answers3

2

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

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

zytra
  • 87
  • 1
  • 11
1

Probably because float support is not included by default using newlib. See printf floats with newlib on STM32

HS2
  • 164
  • 1
  • 1
  • 4
  • Thanks, sadly that didn't work. I already had that option checked in the project properties but tried anyway. – zytra Jul 24 '20 at 15:46
  • also without that option you get a compiler warning telling you exactly that the float formatting needs to be enabled. – zytra Jul 24 '20 at 16:10
0

This problem happened to me too. I'm not sure yours is same with mine but maybe it can help yoou. I just fixed it and the only thing i did is, i included "stdio.h". Then my warning gone.