Search code examples
printfstm32rtos

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). 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


Solution

  • 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