Over a million developers have joined DZone.

Optimized FreeRTOS: Stack Check and SysTick for ARM Cortex Cores

DZone's Guide to

Optimized FreeRTOS: Stack Check and SysTick for ARM Cortex Cores

· Performance Zone ·
Free Resource

Built by operators for operators, the Sensu monitoring event pipeline empowers businesses to automate their monitoring workflows and gain deep visibility into their multi-cloud environments. Get started for free today.

The ARM Cortex specification includes ‘SysTick’ (System Tick Timer), a dedicated system timer that is intended to be used as a time base for an RTOS. While, technically, it would be possible to use any periodic interrupt timer, I’m also using the SysTick for my FreeRTOS ARM ports. And because Processor Expert includes a nice timer interface, I’m using the TimerUnit_LDD:

TimerUnit LDD for SysTick

TimerUnit LDD for SysTick

While this is great for flexibility, it has its price in efficiency. That TimerUnit_LDD adds overhead. So I want to get rid of the TimerUnit_LDD and use a more efficient way.

Direct SysTick Implementation

Instead of using the TimerUnit_LDD, I want to directly use the SysTick. As SysTick is part of the ARM core, the implementation is pretty much the same for every ARM core, so why not use it directly?

So I decided to introduce an extra property:

SysTick Boolean Property

SysTick Boolean Property

If SysTick is set to "yes" (default), it directly uses the SysTick of the ARM core, and there's no need to configure a TimerUnit_LDD:

No TimerUnit_LDD needed

No TimerUnit_LDD Needed

Inside the port, it directly accesses the SysTick hardware and is not using the TimerUnit_LDD interface.

The positive side effect is:

  1. No need to worry about aligning the RTOS frequency with the timer frequency
  2. No need to select a free timer: it is always using the SysTick
  3. Better runtime performance (no need for extra push/pop in interrupt)
  4. And best of all: it saves 200 bytes of FLASH and 16 bytes of RAM (ARM Cortex M0+) :-):-)

:idea: If you run into an error message about #include “RTOSTICKLDD1.h” in Events.c, Events.h or Processor Expert: It seems that in some cases Processor Expert does not remove the include, as that file is not needed and not generated anymore. In that case, you can simply remove it by hand from the source file.

Stack Overflow Checking

FreeRTOS has a feature to check for a stack overflow at the time of a context switch which is incredibly helpful. Still, sometimes I want to disable this for performance. So far I was able to do this by disabling the stack overflow event:

Disabling Stack Overflow Hook

Disabling Stack Overflow Hook

Now it is possible to select "none" directly in the properties:

No Stack Overflow Checking

No Stack Overflow Checking

And in an ARM Cortex-M0+ application with GNU GCC, this saves 52 bytes of FLASH. :-)


The above changes are not very big, but making use of FreeRTOS especially on ARM Cortex better and easier, especially with Processor Expert. What I have additionally in my mind, but not done yet is to SysTick as the performance counter: now a dedicated timer is used, and for low power tickless idle mode to use a different timer than SysTick. So there is still room for improvement. :-)

Happy Ticking :-)

Download our guide to mitigating alert fatigue, with real-world tips on automating remediation and triage from an IT veteran.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}