cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX generated _sbrk() syscall does not work with freeRTOS enabled. Is there a recommended _sbrk that work well with freeRTOS? And if so, what is it and could you include it into CubeMX that is enabled only if freeRTOS is activated.

Brian Khuu
Associate III

I found others have similar issue, but they are trying to work around it instead of putting in a bug report: https://community.st.com/s/question/0D50X0000AAIEWbSQP/stm32cubemx-generated-sbrk-in-syscallsc-not-working-with-freertos

I was trying to do a basic cubemx generated project with freeRTOS and libc standard lib enabled. While blinking led went without issue... as soon as I called `putchar()` or `printf()`, _sbrk() said there was a stack collision with the heap.

Inspecting the position of the stack pointer relative to the heap pointer... the stack pointer is behind the heap pointer. But the stackpointer can be behind the libc heap, as the stackpointer would be pointing to the freeRTOS's task stack pointer instead for each task.

My stopgap solution is to ignore the check (at my own risk) for now for a practice project.

caddr_t _sbrk(int incr)
{
	extern char end asm("end");
	static char *heap_end;
	char *prev_heap_end;
 
	if (heap_end == 0)
		heap_end = &end;
 
	prev_heap_end = heap_end;
	heap_end += incr;
 
	return (caddr_t) prev_heap_end;
}

I tried to modify the check to use `extern char end asm("_estack")` to check between the heap pointer and the roof of the memory. But was unable to, since I'm still learning about how to get linker symbols to be accessible like how you did with the `end` linker symbol.

This is based on my understanding of how freertos memory works according to http://amichalec.net/2014/05/24/water-level-logger-external-ram/ (Specifically this diagram http://amichalec.net/wp-content/uploads/2014/05/malloc-custom.png)

0690X000009YZHGQA4.png

So again, whats your recommended approach, and could you get the generated freeRTOS to the point that it is ready to use with default settings with malloc?

However you can also consider allowing the designer to disable standard lib completely and supply printf that does not need malloc. I was giving it a shot as I would like to try and give retargeting of the uart using your recommended approach a shot.

----------

edit: Here is another example of how other people approached checking heap range with freeRTOS enabled https://github.com/Links2004/freertos/blob/master/FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/standalone_v4_1/src/_sbrk.c

3 REPLIES 3
Brian Khuu
Associate III

One other I would like to also suggest, is if you could have a "Partial Assert" mode, so we can use your `assert_failed(uint8_t *file, uint32_t line)` asserts, but with HAL asserts disabled (for space savings, if we want to just focus on testing the main code). Of course this should be removed in production mode too, and included only in debug firmware.

@Brian Khuu​ - You might find this helpful (to be updated in next couple weeks for current STM tools)...

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

Hope that helps,

Best Regards, Dave

@Brian Khuu​ - I just updated the document and included the set of required fixes for ST32F..

Any feedback will be greatly appreciated (especially if I wasn't clear)!

Thanks, Best Regards, Dave

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