2
votes

I configured Atmel's ARM Cortex M0's UART for printing strings and integers at the console using std C function printf() in Atmel studio 7.

Case I

I am trying to make printf() type of function print floating point values and for that I followed following suggestions to do so:

arm-none-eabi-gcc : Printing float number using printf

and later on I edited/added the linker flags following texts separately at different time :

-lc -u _printf_float
-lc -lrdimon -u _printf_float 

Case II

Though I could not understand everything they said but I followed some of the suggestions to edit makefile from this forum too.

Printf/Sprintf for float point variables not working

and added following text inside the makefile

ldflags-gnu-y += -lc -u _printf_float 

Makefile Path (Atmel Studio 7, using ASF) : ../src/ASF/sam0/utils/make/Makefile.sam.in

Now in the main.c I used printf() for printing floating point number as :

float a = 345.65412;
char buffr[20];         
/* --- Print Float using printf only --- */
printf("Float Number 1 : %f\r\n", a);   
/* --- Print Float using sprintf ---*/
sprintf(buffr, "Float Number ( Using Sprintf) : %3.3f\r\n", a); 
printf(buffr);

Output on a UART console app.:

Case I:

Float Number 1 : 2.000000
Float Number ( Using Sprintf) : -0.000

Case II:

Float Number 1 : 
Float Number ( Using Sprintf) :

Does anyone know configuring linker to make printf(), sprintf() or vprintf() work for printing floating point number on the console for ARM Cortex M0 (SAM B 11) in Atmel Studio 7 ?

1
Some other links I followed which I could not include on the question : Printf with floating point support in armgcc Using floats with sprintf, gcc-arm-none-eabi, nRF51822mrUnfused

1 Answers

1
votes

You buffer is too small for your data

char buffr[20];

have to be at least (considering 0.0 as format output)

char buffr[38];

The small buffer causes a "write out of bounds" of the array. It means you have a Undefined Behavior and a variable is polluted.

sprintf is different than printf. It copy all chars to the output buffer and you must grant space for text and for all chars that will be added by format specifiers.

In your specific case:

  • Float Number ( Using Sprintf) : --> takes 32 chars
  • %3.3f --> takes 7 chars due to the real output of 345.654
  • \r\n --> takes 2 chars
  • a final byte is needed for the null terminator '\0'

That menas your buffer have to be:

char buffr[42];