You have probably noticed there is more power in the STM32 processors than simple tasks can use. In this case you probably want to run some more “tasks”.
FreeRTOS has just the stuff you need – tasks, mutexes/semaphores, queues.
Unfortunately I ran into trouble with newlib and FreeRTOS not playing nicely as they should. Memory allocation (malloc) didn’t work as expected and caused the CPU to Bus-Fault. Then I realized there is another allocator in the FreeRTOS, and why does one need 2 allocators on embeded system ?
First of all, you need to be sure that you compiled newlib with something like (MALLOC_PROVIDED being the important here)
make CFLAGS_FOR_TARGET="-msoft-float -DMALLOC_PROVIDED" CCASFLAGS="-msoft-float -DMALLOC_PROVIDED"
By doing this you tell newlib that you are going to provide malloc and friends (that includes: free, realloc, calloc)
Next thing you need is a “glue” that tells newlib to use the allocator of FreeRTOS. I suggest that you use heap2.c allocator. (allocator heap1.c is too simple for this use. Heap3.c uses external library, which could have been malloc from newlib, if it were working right :) )
The glue can be downloaded here: alloc.c.
It implements functions like _malloc_r, _free_r, etc. (R standing for reentrant?) All of these functions are straight-forward, except realloc, that needs to copy some memory. I have not done very extensive testing on this. The state is now “Works-For-ME”, so far I have tested sprintf function while testing gcc to support soft-float. The sprintf probably uses only malloc and free.
If you have any suggestions, patches, please comment.
I have created project that runs on Olimex’s stm32-103stk using FreeRTOS. The project includes:
- Makefile (to help putting everything together – not my work)
- SPI Master driver (using DMA transfers, and locking support)
- Simple menu for the LCD.
- Nokia 3310 LCD driver (again not my work, but heavily modified to support my SPI driver)
If anyone is interested just leave a comment, I will put together some releasable version :)
Update: I have created git repository with my projects HERE.
Some notes about newlib:
Don’t use [s|vs|…]printf if you are dealing with integers only. Use *iprintf versions instead, these don’t include printing of floating point, reduce size of final code, and should be faster – they use static buffer instead of allocating one.