cancel
Showing results for 
Search instead for 
Did you mean: 

printf hard fault

luke514
Senior

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

 

 

 

23 REPLIES 23

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
Up vote any posts that you find helpful, it shows what's working..

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

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
Up vote any posts that you find helpful, it shows what's working..

Another thing, in all the other projects I have, printf works correctly.

Here that I imported the arm_math.h libraries etc. following this tutorial ... it does not work. Maybe is there some incompatibility problem?

I can send you pictures of the functions you mention, but I have no idea what to change/check here

luke514_1-1698949232993.png

 

luke514_2-1698949394893.png

 

 

 

the CubeIDE has many great things (...if they are working...) :

AScha3_0-1698949651392.png

 

If you feel a post has answered your question, please click "Accept as Solution".

Now I don't understand if the problem is in the code I wrote (main.c) or in some other file

Thanks @AScha.3 I didn't know about this function because I never needed it

 

Pavel A.
Evangelist III

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 ?

_sbrk() is in sysmem.c

luke514_0-1698962909151.png