Skip to main content

FreeRTOS + newlib (gcc toolchain) on the STM32F103 (Cortex-M3)

Here are some of the steps to get FreeRTOS and newlib (gcc) to work correctly on the STM32F10x, a Cortex-M3 ARM processor.

Configure the priority bits

Per the FreeRTOS page about the Cortex M3 and M4, the priority bits should be assigned to be preempt priority by calling NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ); If you or any start up related code you are using isn't doing this you may end up with odd behavior or bus faults.

Map FreeRTOS tasking functions to CMSIS vector handlers

Per this post you'll want to map the FreeRTOS handlers for vPortSVCHandler, xPortPendSVHandler and xPortSysTickHandler to the CMSIS handlers. A readelf of the elf file of my example application was showing the default interrupt handler was being used for those interrupts, which meant FreeRTOS wasn't handling the interrupts it needed to function correctly. The CMSIS default handlers are weakly bound so having other definitions with the same names will override these, causing the FreeRTOS handlers to be called instead of the default vector handler.

You can do this by adding these lines to your FreeRTOSConfig.h file:

/** Defines to map FreeRTOS tasking functions to CMSIS standard vector table entries */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler
Neglecting to perform this mapping was causing me to see odd exceptions such as the usb lp handler being called:


Newlib reentrancy handling

If you are building newlib with reentrancy support, support for calling library functions at the same time from multiple threads, then you'll have to take some care to configure newlib properly at run-time. There isn't much information about this topic, it may be that newlib isn't often used with FreeRTOS on smaller embedded systems.

During a bringup of FreeRTOS and newlib I kept seeing imprecise bus faults at start up. Debugging narrowed the fault down to a call to malloc() which was calling _malloc_r(). A few hours of stepping through the source code revealed that _REENT (also defined as impure_ptr), a struct _reent*, appeared to be invalid. Some searching brought up this thread. There also appears to be a global reent structure called _impure_data that is assigned to _global_impure_ptr. Nothing appears to be using this pointer or assigning it to _REENT though, which is why the imprecise bus fault happened. The above thread shows how to integrate the configuration of impure_ptr with thread switching. This ensures that there is a struct _reent for each thread and that impure_ptr is set as the thread is activated to run.

As a temporary work around I added this call at the start of main():
_REENT_INIT_PTR(_REENT);

I've posted on the FreeRTOS support forums and upon Richard's request posted a feature request for the changes to be made to the FreeRTOS code to support newlib natively. If FreeRTOS gets newlib support it should be a matter of setting the appropriate config flag in FreeRTOSConfig.h and you'll be able to avoid dozens of hours tracking down this issue and patching FreeRTOS to fix it.

Comments

Popular posts from this blog

Debugging an imprecise bus access fault on a Cortex-M3

This information may apply to other cortex series processors but is written from practical experience with the Cortex-M3. Imprecise bus access faults are ambiguous, as noted by the term "imprecise". Compared to precise bus errors, imprecise errors are much trickier to debug and especially so without a deep understanding of arm processors and assembly language. Imprecise and precise flags are found in the BusFault status register, a byte in the CFSR (Configurable Fault Status Register). BusFault status register bits The definition for imprecise and precise bits is: [2] IMPRECISERR Imprecise data bus error: 0 = no imprecise data bus error 1 = a data bus error has occurred, but the return address in the stack frame is not related to the instruction that caused the error. When the processor sets this bit to 1, it does not write a fault address to the BFAR. This is an asynchronous fault. Therefore, if it is detected when the priority of the current pr

Graco Swing By Me - Battery to AC wall adapter modification

If you have one of these Graco battery powered swings you are probably familiar with the cost of C batteries! The swing takes four of them and they only last a handful of days. I'm not sure if the newer models support being plugged into the wall but ours didn't. If you are a little familiar with electronics and soldering, here is a rough guide on how you can modify yours to plug in! I wasn't sure how exactly to disassemble the swing side where the batteries were. I was able to open up the clamshell a bit but throughout this mod I was unable to determine how to fully separate the pieces. I suspect that there is some kind of a slip plate on the moving arm portion. The two parts of the plastic are assembled and the moving arm portion with the slip plate is slid onto the shaft. Because of the tension in that slip plate it doesn't want to back away, and because of the mechanicals that portion of the assembly doesn't appear accessible in order to free it. I was

Memory efficient queuing of variable length elements

In embedded environments memory can be a critical driver of the design of data structures and containers. Computing resources have been expanding steadily each year but there are still a wide range of systems with far less than a megabyte of memory. On systems with tens of kilobytes of memory, structures are often designed to be compact to maximize data density. Rather than splurging on memory aligned elements that would be faster for the processor to access, a developer will typically use types with minimal sizes based on the known range of values that the element is intending to hold. Fixed sized buffers At my day job a fixed size pool of messages was implemented to hold message data. While this achieved one design goal of using statically allocated buffers, avoiding dynamic allocations that might fail at runtime, it isn't efficient if there is a wide range of message sizes. It isn't efficient because each message uses a message buffer. With small message sizes the buff