Skip to main content
Zanon.Luciano
Associate III
March 19, 2020
Question

Calling rand() on Cortex-M3 produces hard fault due to malloc() error (SOLVED).

  • March 19, 2020
  • 14 replies
  • 3250 views

Very strange behavior, the instruction:

uint32_t xid;

xid = rand();

cause hard fault due to unaligned address.

I am using Atollic TrueSTUDIO Version: 9.3.0 

This topic has been closed for replies.

14 replies

Zanon.Luciano
Associate III
March 20, 2020

The problem is solved !!

As mentioned, the problem was that in my environment the stack is located in a ram area at a lower address than the heap.

Normally the heap is growing and the stack is decreasing so the check that is done is to avoid the collision between the heap and the stack, this check is done in the "_sbrk" routine in syscalls.c.

It was enough to modify the _sbrk routine (which is available at the user level) and test the area following the heap instead of the stack pointer to solve the problem !!

Luciano

Zanon.Luciano
Associate III
March 20, 2020

As always, in the end the problem was a simple thing, but as always at the beginning it seemed a big problem ...

Piranha
Principal III
March 21, 2020

You could be interested in reading this article by @Dave Nadler​:

http://www.nadler.com/embedded/newlibAndFreeRTOS.html

Ultimately the best option for devices and operating systems without MMU is to not use any heap memory at all, because it can easily lead to heap fragmentation and failure.

Zanon.Luciano
Associate III
March 21, 2020

thanks Piranha I will read it carefully!:smiling_face_with_smiling_eyes: