cancel
Showing results for 
Search instead for 
Did you mean: 

sprintf, FreeRTOS, Hard Fault and Stack aligment, need explanation

hexel2
Associate II

Hello!

I encounter random HardFault when calling sprintf, sscanf from thread context in FreeRTOS. MCU is F446. When called before KernelStart, it works well. Meanwhile, it was working ok, before I`ve rearanged modules and added new structures. I didn`t even used new module yet. Behavior also depends on optimization options.

After surfing over internets, I found this could happen if unaligned stack access is performed. After this point I didn`t understand actual techniques to make it happen. What directives should I put and where? I`m using IAR 8.3

If code insights are needed, I`l put it, but entire project is quite large.

Also, rtos config is included, maybe it has some issues

Hope for you help!

4 REPLIES 4
KnarfB
Principal III
hexel2
Associate II

Frustrating. I wish I find this article before project start. But still, I cannot find exact parts in my FreeRTOS modules, which I should remove in accordance with that guide, i. e. sbrk is not found by full text search not in project sources, nor repository files. My Heap_4.c file have no routine named sbrk at all. Yes, in fact I`ve used RubeMX for initial code generation, firmware version is FW_F4 V1.21.0, MX is 4.25.1. Is that firmware supposed to be buggy? Or had this version already fixed?

KnarfB
Principal III

Oh, I'm not using IAR but STM32CubeIDE. The link might not be relevant to you, not sure.

KnarfB

Normally people look at what the Hard Fault is actually complaining about, and work backward from there. The MCU provides a lot of detail about what it has a problem with.

LDRD/STRD may fault on an F4 (CM4) when the address isn't on a 32-bit boundary, not sure that's inherently an sprintf() related thing, but watch for pointers to doubles, and byte aligned structures from files/datagrams.

For RTOS you need to be sure things library functions are using are thread safe. Some things like strtok() inherently are not. If sprintf() is using static buffers, these may present an issue.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..