Enable floating point support sprintf

How do I enable floating point support for the sprintf function? For printf it worked after enabling PRINTF_FLOAT_ENABLE in platform/utilities/src/print_scan.c.

What toolchain are you using exactly?

Setting PRINTF_FLOAT_ENABLE in platform/utilities/src/print_scan.h enables floating point support for the debug printf implemenations provided by NXP. Those are only available through debug_printf or PRINTF. This “mini library” only provides printf and scanf, but not sprintf. If you use regular printf, then printf is taken from the libc library provided by the Linaro bare metal toolchain. The same is true when you use sprintf.

Now newlib provided by the bare metal toolchain comes in two flavors: newlib and newlib-nano.

The newlib-nano flavor has the option to enable floating point support in printf and scanf by using --specs=nano.specs -u _printf_float -u _scanf_float. You can add those options to the linker flags by extending the CMAKE_EXE_LINKER_FLAGS variable of CMakeLists.txt.

The newlib flavor has support for float on all IO functions, including sprintf. To use the full newlib flavor you need to remove the --specs=nano.specs from the linker flags (by removing that from CMAKE_EXE_LINKER_FLAGS).

Note however this doubles the size of the firmware which does not fit into tightly couppled memory (TCM) anymore. A simple hello world still fits into on-chip SRAM. You can change the location of the firmware by using -T MCIMX7D_M4_ocram.ld in CMakeLists.txt.

See also the readme file provided by the Linaro toolchain (in share/doc/gcc-arm-none-eabi/readme.txt).

Note: It is unclear to me why NXP included a separate set of C library functions… It might be a better idea to just use one set of I/O functions.

Thank you so much! We already switched to the ocram linker file so I will check if it makes sense to switch to the other library. Thanks again for the detailed answer!