Skip to main content
Associate III
Nov 2, 2023 5:49 PM UTC
Question

printf hard fault

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.

    23 replies

    Tesla DeLorean
    Guru
    Nov 2, 2023 6:05 PM UTC

    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 Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    luke514Author
    Associate III
    Nov 2, 2023 6:10 PM UTC

    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
    Nov 2, 2023 6:17 PM UTC

    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 Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Pavel A.
    Nov 2, 2023 9:53 PM UTC

    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 ?

    luke514Author
    Associate III
    Nov 2, 2023 10:08 PM UTC

    _sbrk() is in sysmem.c

    luke514_0-1698962909151.png

     

    Associate
    Nov 2, 2023 11:28 PM UTC

    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
    Nov 3, 2023 8:58 AM UTC

    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 on " Best Answer ".
    luke514Author
    Associate III
    Nov 3, 2023 10:08 AM UTC

    @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.
    Nov 3, 2023 12:58 PM UTC

    >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.

    luke514Author
    Associate III
    Nov 3, 2023 5:31 PM UTC

    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.
    Nov 3, 2023 8:45 PM UTC

    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?