cancel
Showing results for 
Search instead for 
Did you mean: 

How to get printf() float / double support working on F7

Michael B
Associate III

I've been fighting with this too long and can't figure it out. I've added uart communication for use with printf() by implementing the following in syscalls.c:

__attribute__((weak)) int _write(int file, char *ptr, int len)
{
   HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, (uint8_t *)ptr, len, 0xFFFF);
   return (status == HAL_OK ? len : 0);
}

That works fine with printf. But printing floats or doubles causes it to hang when it tries to output the number. I have enabled -u _printf_float in the MCU Settings of STM32CubeIDE (also tried adding it manually to the Linker flags).

double test = 1.2345;
printf("test: %f\r\n", test);
arm-none-eabi-g++ -o "ProjectTest.elf" @"objects.list"  -l:libtouchgfx-float-abi-hard.a -mcpu=cortex-m7 -T"B:\gitrepo\ProjectTest\STM32F746NGHX_FLASH.ld" --specs=nosys.specs -Wl,-Map="ProjectTest.map" -Wl,--gc-sections -static -L../Middlewares/ST/TouchGFX/touchgfx/lib/core/cortex_m7/gcc --specs=nano.specs -mfpu=fpv5-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -Wl,--start-group -lc -lm -lstdc++ -lsupc++ -Wl,--end-group

I don't really know how to debug this further, and I've read many relevant posts but nothing I've tried has worked. I really need it to, as the debugger in STM32CubeIDE is a pile of junk and the watcher shows incorrect values for variables.

Also note I'm using FreeRTOS here, not sure if it's an issue with stack alignment or how to debug that.

Help a brutha out?

11 REPLIES 11

Ah. Using C11 generics. Very cool =)

 But what is suffix 'd' on floating constants? No, it does not mean double. Is it a gcc extension? I googled but found only "decimal floating" which has suffixes like df or dd, but not a single d.

-- pa

The suffixes "d" and "f" are a way to bypass GCC optimization, to force data to be stored in this format. For example, GCC can save the constant 1.0 as uint8_t - it is more convenient for it. Or expand it to double precision - if the constant is used in a mathematical operation with data in double precision.

There are five suffixes for integers, three for floating point, and six for fractional numbers. All of them apply only to constants, and to some extent are syntactic sugar. When printing variables, suffixes do not matter.