Page 1 of 1

Using sprintf / sbrk_r

Posted: Fri Jan 04, 2013 11:02 pm
by Lupin
At the moment i am stuck trying to get sprintf to work with ChibiOS (STM32F4 / gcc-arm-embedded compiler).

At first i got linker errors because of missing sbrk_r function. Then i included the syscalls.c file that comes along with ChibiOS.

The code compiles, works for a few seconds and then the OS stops working.

I guess it's because i run out of memory. I created a new thread that calls sprintf about 100 times per second and the result is written to my display.


I noticed that chCoreAlloc is used in _sbrk_r implementation. As far as i found out the "incr" argument can also be negative to free memory - afaik chCoreAlloc can't free memory, so that might be the reason why i run out of memory after a while.

Any ideas what is going wrong and how to fix that?

Re: Using sprintf / sbrk_r

Posted: Fri Jan 04, 2013 11:11 pm
by Lupin
I just modified chprintf to chsprintf and it works good. But still i would like to know how one could fix the issue with sbrk_r?

Re: Using sprintf / sbrk_r

Posted: Fri Jan 04, 2013 11:24 pm
by Giovanni
Hi,

Not sure, is sbrk_r() really invoked with a negative parameter? if so then it cannot be implemented using chCoreAlloc() because chCoreAlloc() cab also be called by other allocators and the application, the library is not the only user.
The only solution would be creating a dedicated heap for the library and implement a sbrk() working inside that space.

Giovanni