Hard fault when calling sprintf with floating point numbers from .cpp file

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Hard fault when calling sprintf with floating point numbers from .cpp file

1,605 Views
danielhurowitz
Contributor I

Hi,

I have been having trouble using sprintf for floats. 

I read all of the posts I could find on the topic and failed to resolve the problem.

I am using KDS 3.2 with processor expert with a K26F processor.

The current state of my program is as follows:

when I call 

sprintf((char*)s,"Event;\t  %f\r\n ",1.5);

from main.c, It works fine.

The exact call from a .cpp file results in a hard fault. 

I have several other uses of sprintf with integers, without any problems. 

Please note that I have already tried the following (this is the current state of my project):

1. Enlarging the heap and stack.

2. In the cross ARM C++ Linker settings, I checked

"use float with nano printf (-u _printf_float)" and "use float with nano scanf (-u _scanf_float)"

3. In Cross ARM C Compiler Preprocessor definitions I defined "PRINTF_FLOAT_ENABLE=1"  and  "SCANF_FLOAT_ENABLE=1".

4. Called  the following after startup:

void vPortEnableVFP(void) {

/* The FPU enable bits are in the CPACR. */

__asm volatile (

" ldr.w r0, =0xE000ED88 \n" /* CAPCR, 0xE000ED88 */

" ldr r1, [r0] \n" /* read CAPR */

" orr r1, r1, #(0xf<<20) \n" /* Enable CP10 and CP11 coprocessors */

" str r1,[r0] \n" /* store to new value back */

: /* no output */

: /* no input */

: "r0","r1" /* clobber */

);

}

Do you have any further suggestions?

Thanks!

Labels (1)
0 Kudos
1 Reply

1,099 Views
BlackNight
NXP Employee
NXP Employee

It might be related to your heap/stack allocation (see FreeRTOS, malloc() and SP check with GNU Tools | MCU on Eclipse ) as depending on your library usage (newlib or newlib-nano) it will use malloc(), and if the heap is misplaced.

I quickly tried to reproduce what you say, but it worked for me. Attached is a project for the FRDM-K64F, just in case you spot the difference to your setup.

I hope this helps,

Erich

0 Kudos