Skip to main content
davidregan9
Associate III
August 12, 2019
Question

Cannot use printf with floats on a Cube generated System Workbench project for STM32F429i discovery board.

  • August 12, 2019
  • 14 replies
  • 4890 views

Used cube to generate a project with default settings for STM32F429I disco board plus CMSIS V2 RTOS and USART 2 half duplex transmit only . Generated the code for System Workbench.

I retargeted _write() and provided __io_putchar(). I added -u _printf_float to the linker flags.

In the StartDefaultTask I added

 float fVal = 345.678;

 printf("Hello World\r\n");

 printf("fVal %f\r\n",fVal);

Output

Hello World

fVal . !

Issues

With -specs = nosys.specs -specs= nano.specs -u _printf_float - printf causes a hard fault.

With specs = nosys.specs -specs -u _printf_float - 1st printf OK 2nd printf produces incorrect output

This topic has been closed for replies.

14 replies

Dave Nadler
Senior III
August 13, 2019

See: http://www.nadler.com/embedded/newlibAndFreeRTOS.html

If you're using an RTOS, probably your printf with %f is failing because printf internally tries to malloc a buffer and fails.

Let us know if that's the issue please!

Michael98006
Associate III
August 13, 2019

When I had issues with snprintf() under FreeRTOS, I increased heap and stack allocation as well as task stack size (by a factor of 10x from the original size), and the issue was resolved.

Dave Nadler
Senior III
August 14, 2019

@davidregan9​ - Was this any help? Did you get your issue fixed? Please let us know,

Thanks, Best Regards, Dave

davidregan9
Associate III
August 14, 2019

Hi Dave

I am still having issues.

I tested the return values of the printf which was a positive value (albeit wrong).

No errno was set.

I have been trying to isolate the issue. Without RTOS it works

davidregan9
Associate III
August 14, 2019

BTW Increasing heap/stack made no difference.

Dave Nadler
Senior III
August 14, 2019

@davidregan9​ - Can you confirm you are using:

  • FreeRTOS?
  • newlib and gcc?

If yes to both, your problem will definitely be solved using code here: http://www.nadler.com/embedded/newlibAndFreeRTOS.html

Please let us know, Thanks, Best Regards, Dave

davidregan9
Associate III
August 14, 2019

@Dave Nadler​ I am using FreeRTOS 10.2.1; GCC ARM Embedded Toolchain 8 2019-q3-update and newlib (I assume since I removed -specs=nano.specs)

Dave Nadler
Senior III
August 14, 2019

@davidregan9​ You are using newlib (or newlib-nano).

Use the code/instructions on my web page and it will solve your problem.

If the page isn't clear PLEASE let me know how to improve it.

Thanks, Best Regards, Dave

davidregan9
Associate III
August 14, 2019

@Dave Nadler​ I will try it out and let you know. Thank you

davidregan9
Associate III
August 20, 2019

@Dave Nadler​ 

Hello Dave. I implemented your solution with heap_useNewlib.c and it worked.

If I'm understanding it correctly ANY kind of dynamic memory assignment is now coming out of the RAM between end and _estack.

That's all printf family type stuff and all RTOS tasks/objects.

Have you had any experience of running out of heap in a reasonable sized project. On our last project we had an external sdram and directed all RTOS activity to that.