Energy consumption is an important characteristic that needs to be monitored carefully when developing an IoT device. Energy consumption can be the difference between a device lasting for several years on a single battery or contributing to an ever-increasing electrical bill. As developers create connected devices, they will undoubtedly use an RTOS to manage their complex timing requirements, but how can they manage their RTOSs energy consumption? In this post, we will examine how to enable and configure Amazon FreeRTOSs tickless mode and how we can customize it for our own application.
The Typical System Tick
In order to take a few measurements, I loaded the latest version of Amazon FreeRTOS onto a ST Microelectronics IoT Discovery Node development board and instrumented the tick interrupt so that when the interrupt fires it sets a GPIO line high at the start of the tick interrupt and then brings it low at the end of the interrupt. Using a Saleae Logic probe, I found that a 1 millisecond tick pattern was generated as expected which can be seen below:
If they were to zoom out and look at the pattern that is generated over a 20 second period, they would find that the system tick runs every 1 millisecond even though the demonstration application is idle for approximately 86% of the time. This pattern can be seen in the logic analyzer trace below:
Now this is what we expect for a typical application that doesn’t care about low power savings mode, but if we are developing an IoT application, we do care about low power and want to spend as much time sleeping as possible. We don’t want our system to wake-up every 1 millisecond only to discover that there is no work to be done.
Amazon FreeRTOS contains an interesting feature called Tickless mode that allows the processor to go to sleep and stretch out the system tick until either a task comes due or an event such as an interrupt wakes up the system. This feature allows the processor to stay asleep as long as possible and conserve a significant amount of energy. Let’s look at how we can enable this feature.
Enabling Tickless Mode in Amazon FreeRTOS
All configuration for the RTOS in Amazon FreeRTOS can be adjusted in the FreeRTOSConfig.h file. Within the configuration, there is a #define for configUSETICKLESS_IDLE which by default is set to 0. Changing the 0 to a 1 will enable tickless mode.
When tickless mode is enabled, there are two functions that are expected to be defined. The first, is PreSleepProcessing which allows a developer to add custom code that they want executed immediately before the RTOS puts the processor to sleep. A few ideas on what a developer should be doing here include:
- Setting the desired sleep mode setting (Stop, deep sleep, etc)
- Disabling power hungry peripherals
- Slowing down peripheral clocks
- Enabling the processors wake-up features.
The second function is PostSleepProcessing. As you can imagine, a developer would place code in this function that is executed immediately after the processor wakes up. In many cases, this is going to be code that undoes what was done in PreSleepProcessing but it could also be code that checks what woke the system up and then decides how awake the processor should become before going back to sleep. The possibilities are quite limitless and application dependent.
When defining the PreSleepProcessing and PostSleepProcessing functions, it’s important to define there are two things to take into consideration. First, you need to define the functions as follows:
Scroll or drag the corner of the box to expand as needed. ↑
The ulExpectedIdleTime is a parameter that lets the developer know how long the RTOS expects the system to be idle. This allows a developer to decide how deep into sleep to go along with what they want to do based on the expected sleep time.
Testing Tickless Mode
I took a quick trace of the system tick with the tickless mode enabled and as you can see below, there are times over the 20 seconds interval where there is no need to run the 1 millisecond tick and you get larger gaps in between. Now the image below was taken at start-up so you can see during the first 8 seconds after start-up the demonstration application is quite busy establishing a secure connection to Amazon Web Services and then the system occasionally wakes up to transmit and receive MQTT data.
Using tickless mode in Amazon FreeRTOS is a great way to manage and decrease the energy consumption that your system is using. Instead of waking the system up every millisecond, the system instead can wake-up only when it has work that needs to be done. Developers do need to be careful to make sure that they set their sleep mode setting properly and in a state that will allow the processor to still wake-up. They also need to ensure that there aren’t any other pieces of framework code that could wake their system up unexpectedly. Beyond that, developers have the complete flexibility to do whatever it is that they need to in order to minimize their energy consumption.
Jacob Beningo is an embedded software consultant, advisor and educator who currently works with clients in more than a dozen countries to dramatically transform their software, systems and processes. Feel free to contact him at email@example.com, at his website www.beningo.com, and sign-up for his monthly Embedded Bytes Newsletter.