user3583807
user3583807

Reputation: 800

Stm32 printf float variable

I want to log out from stm32f405 via usart. In my syscall.c file i realize function to print via usart:

int _write(int file, char *ptr, int len)
{
    int todo;
    for (todo = 0; todo < len; todo++)
    {
    usart_send_char( *ptr++ );
    }
    return len;
}

Function usart_send_char( *ptr++ ); work as expected. But when i call:

printf("%s, %d, %3.2f\r\n", "asd", 777, 13.2 );

I get: asd, 777, 0.00 The float variable not printed correctly.

Makefile:

PROCESSOR = -mcpu=cortex-m4 -mthumb -mfloat-abi=softfp -mfpu=fpv4-sp-d16
CFLAGS += $(PROCESSOR) $(INCLUDES) $(STFLAGS) -Wall -fno-strict-aliasing $(C_PROFILE)
LDFLAGS = $(PROCESSOR) -Wl,-Map=$(PROG).map,--cref,--gc-sections

Used compilator:

Sourcery CodeBench Lite 2014.05-28

Where i am mistaken?

Upvotes: 12

Views: 39812

Answers (4)

noodle7
noodle7

Reputation: 71

The STM32cubeIDE has a checkbox for this capability but, apparently, it does the same thing as has been suggested: -u _printf_float. AND there's a separate one for _scanf_float. Right click on the project name and select "Properties" (at the bottom of the list).

Properties dialog box

Upvotes: 3

Waged
Waged

Reputation: 420

Snapshot of where you addd the command -u _printf_float in STM project settings

To enable the floats in sprintf, add -u _printf_float to your project as this picture indicate.

Upvotes: 7

Frank Hunleth
Frank Hunleth

Reputation: 770

I haven't used Sourcery Codebench gcc for STM32F4, but with the GCC ARM Embedded toolchain, floating point support in printf isn't enabled by default. To enable, add -u _printf_float to your LDFLAGS.

Upvotes: 25

Lui
Lui

Reputation: 159

  1. I am not sure, but I think the mfloat-abi-flag must be set for the CFLAGS.
  2. STM32F4 has a FPU, so why do you use mfloat-abi=soft? Can't you use mfloat-abi=hard?
  3. If you don't need to calculate floatingpointvalue very often, you could do it in this way.

Code example:

 int i = 132;
 printf("Result is: %d.%d", i/10, i%10);

Upvotes: 7

Related Questions