Skip to main content
luke514
Associate III
November 2, 2023
Question

printf hard fault

  • November 2, 2023
  • 4 replies
  • 18126 views

Whenever the compiler executes

printf("frequency %f: %f \n", ((float32_t)(i * SAMPLING_RATE) / FFT_BUFFER_SIZE), output_fft_mag);

it goes to HardFault.

I attach main.c and syscall.c

All the code that we need to analyze is inside the function void get_sineval_FFT2()

luke514_0-1698947305139.png

Here's the problem:

luke514_0-1698949948629.png

EDIT: The problem is only there when I try to printf floats ... whereas with normal strings or ints everything goes

 

 

 

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    November 2, 2023

    Look at what's actually faulting. The MCU instructions and registers.

    Do regular printf() or putchar() work, or do they fault too?

    Keil, but the mechanics are the same..  https://github.com/cturvey/RandomNinjaChef/blob/main/KeilHardFault.c

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    luke514
    luke514Author
    Associate III
    November 2, 2023

    Thank you for your reply!

    With breakpoints, the compiler is faulting exatcly when printf is executed, it doesn't matter what its argument is ... I tried as well to write printf("Hello \n"); and HardFault occours.

    Does that answer your comment? Because it is not very clear to me when you say "verify using instructions and registers" and what should I do with the code in the link you sent me

    Tesla DeLorean
    Guru
    November 2, 2023

    I mean at the MCU level not at the C function level. Something with finer granularity, and not the broad side of a barn..

    Do you enable the FPU in your SystemInit() code?

    Has it linked the appropriate C library. 32-bit ARM code will fault a CM4F processor.

    The call to printf() or the code within the function?

    Does the CubeIDE provide some sort of Hard Fault Analyzer function or window?

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Pavel A.
    Super User
    November 2, 2023

    The last thing on the call stack is malloc_r (malloc) so check the pool (size, address....). Where _sbrk() is implemented? Do you have sysmem.c ?

    luke514
    luke514Author
    Associate III
    November 2, 2023

    _sbrk() is in sysmem.c

    luke514_0-1698962909151.png

     

    Associate
    November 2, 2023

    May I know which compiler toolchain version you are using? I am asking because I have recently experienced the similar error and it was because I was using compiler toolchain version 12.3... Compiler version 11 and 12 provided by ARM have known issues with how they were compiled and people are using them by performing a manual patch to fix the error. I won't go into details.

     

    Anyways; to fix your problem try using compiler version 10.3 instead. This will most likely solve your issue.

    AScha.3
    Super User
    November 3, 2023

    btw on my (old , from 2017) MX linux system new toolchains dont work, (IDE 1.13.1) ,

    so i use 9-2020-q2 ; no problems so far.

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    luke514
    luke514Author
    Associate III
    November 3, 2023

    @AuthorityThis toolchain version issue are you sure it applies here? Because, as I said, printf works in all my other projects and has never given me any problems with this version .. so maybe there is a technical problem in this project either in the code or incompatibility with the DSP libraries I have installed

    Pavel A.
    Super User
    November 3, 2023

    >The problem appears only when I try to printf floats

    Printing floats in the newlib requires something more than integers - maybe, extra allocation. So, not surprising. You can look for alternative 3rd party printf that is more eco-friendly. This or others.

    luke514
    luke514Author
    Associate III
    November 3, 2023

    I finally solved it by declaring "double" variables to be used inside print .. and not "floats". However, the code doesn't print what it is supposed to print, and I hope it is not due to this little change

    Pavel A.
    Super User
    November 3, 2023

    This is very strange. Floats passed to printf  must be automatically converted to doubles. Something else goes on there. The float 'coprocessor' not enabled by startup?