This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Sprintf and haldfault error

I am working on some project with TM4C123 microcontroller. I have problem with sprintf function, when i use this function, my program is going to hard fault. I am using sprintf with floating numbers. I am using FPU unit. And for this function i already gave a heap memory, and it still doesnt look like works. 

In the below, u can see how i used sprintf function.

	sprintf(WiData, "$TLM,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f*%c%c", Accel.x, Accel.y, Accel.z, Gyro.x, Gyro.y, Gyro.z, Compass.x, Compass.y, Compass.z, Angle.x, Angle.y, Angle.z,0x0A,0x0D);

And this is how many heap memory i gave

; <h> Heap Configuration
;   <o>  Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>

Heap_Size       EQU     0x00000200

                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit

I dont know what i am missing?

Thanks

  • Hi,

    Sorry, do not understand what is this:  2f*%c%c     at the end of format parameter?

    Try first just one or two variables, the extend. Also, the end of the line is CR-LF, seems you reverted...

    Petrei

  • Bilici said:
    sprintf(WiData,

     Assuming string terminator pointed by Petrei is Ok, Has WiData space enough to contain a so large string?

     This has nothing to do with Heap if WiData is on static space or on stack but no declaration is in your post to analyze.

     Please post missing details.

  • printf uses stack, (and large amount of stack, judging your example), so heap size doesn't actually matter. This assumes you are using a library with a printf implementation supporting float).

    And the FPU is of no particular help for printing numbers.

    I suggest incrasing the stack size, and splitting up your large sprintf call into multiple smaller calls.

  • Thanks for answers.I will try to add important part at code.

    First i increase heap size because i read it sprintf function uses malloc function, but it didnt solve my problem. I also increased stack size and reduce function variables as u can see below. problem still goes on.

    char WiData[255];
    void main() { ... while(1) { .... SendTelemetry(Telemetry, Accel); .... } ... } void SendTelemetry(char WiData[255], Type_F_Vector Accel) { sprintf(WiData, "$TLM,%.2f", Accel.x); }

    typedef struct {
    	float x;
    	float y;
    	float z;
    } Type_F_Vector;

    I used this code with another CortexM4F microprocessor and everything looks work on that microprocessor. 

    Thanks

    Bilici

  • Hi,

    First, the picture you posted shows an error at address 0x47704000 - which peripheral has your "other microprocessor" at that address? I searched TM4C123 and found none, so it is normal to see such thing.

    Second, as @fm states, printf/sprintf does not uses heap (I think at least as long as you don't use semi-hosting).

    For usual printf/sprintf functions provided by the IDE tools, stack should be set starting at minimum 2KB. What is yours?

    Petrei

  • Thanks for all help and information, i've finally found my problem. It was because of stack memory. At the beginning of project, when i tried to start my program in debug mode, i saw there was a problem to get main function and i just changed the main function name as __main, and i assume that this function got all variables together and made my stack memory full(maybe i misunderstood but program started to work fine now).