1. Introduction
Currently, more and more embedded product development uses RTOS as a software platform, and the demand for low power consumption during development is also increasing. This document discusses how to handle the low power characteristics of the STM32 microcontroller in FREERTOS. The general idea of low power design is as follows:
1. Enter low power mode when the Idle task is running;
2. Wake up the MCU through interrupts or external events under appropriate conditions.
From the second point, it can be seen that every time a timer generates an interrupt in the OS system, it will also wake up the MCU from low power mode, and frequent entry into sleep and wake-up will prevent the MCU from entering deep sleep, which is also unreasonable for low power design.
A low power design mode is provided in FreeRTOS—Tickless Idle Mode, which allows the MCU to stay in low power mode for a longer time.
2. Tickless Idle Mode Principle and Implementation
2.1 The design concept of Tickless Idle Mode is to make the MCU enter low power mode as much as possible when it is idle.
Let’s look at a specific example:
The above figure is a task scheduling diagram, with the horizontal axis as the timeline. T1, T2, T3, T4 are the time slice benchmarks of RTOS, with four tasks: TaskA, B, C, D.
Task A: Periodic task
Task B: Periodic task
Task C: Burst task
Task D: Periodic task
From the figure, it can be seen that there are four idle periods between the scheduling of the four tasks (at this time, the RTOS will schedule the Idle task to run, and the software design goal should be to keep the MCU in low power mode as much as possible during the Idle task).
Idle1: During the Idle task running, a system clock tick will be generated, which will wake up the MCU. After waking up, the MCU will enter low power mode again, and this wake-up is meaningless. It is expected to keep the MCU in low power mode during Idle1, so the system timer interrupt should be adjusted to avoid triggering the system clock interrupt at T1, and the interrupt trigger point should be set to when Task B arrives;
Idle2: Task C wakes up the MCU before the system tick arrives (external event), and the MCU can remain in low power mode during Idle2;
Idle3: Similar to Idle2, but Idle3 time is very short. If this time is very short, then the significance of entering low power mode is not great. Therefore, a strategy should be added in the software when entering low power mode;
Idle4: Similar to Idle1.
2.2 From the above scenarios, the issues that software design needs to address include:
a. Reasonably enter low power mode (avoid unnecessary switching between low power mode and running mode for the MCU);
The RTOS system clock is derived from a certain periodic timer in the hardware (most Cortex-M series cores use ysTick);
The RTOS task scheduler can anticipate the trigger time of the next periodic task (or timer task). As mentioned above, adjusting the system clock timer interrupt trigger time can avoid unnecessary time interrupts in the RTOS, allowing it to stay longer in low power mode. At this time, the RTOS clock is no longer periodic but dynamic (no interrupts will be generated at the original clock benchmark, i.e., Tickless);
b. When the MCU is awakened, provide compensation to the system clock in some way. The MCU may be awakened by two situations: dynamically adjusted system clock interrupts or burst external events. In either case, the time the MCU spends in low power mode can be calculated using some timer running in low power mode, and software compensation to the system time can be performed after the MCU wakes up;
c. When implementing the software, it is necessary to handle issues based on the specific application scenarios and the MCU’s low power characteristics. Especially for the MCU’s low power characteristics, different MCUs have different peripherals (mainly timers) available in different low power modes, and the RTOS’s system clock can be adjusted accordingly.
2.3 Implementation of Tickless Idle Mode
Taking the STM32F407 series MCU as an example, the first thing to clarify is the low power modes of the MCU. F407 has three low power modes: Sleep, Stop, Standby, and under the RTOS platform, SRAM and register data should not be lost. In addition, a timer is needed to provide the system clock for RTOS, and we choose to implement it under Sleep mode.
3. Conclusion
The STM32 family has different series, especially the L series designed for low power applications, which can have more implementation methods when designing RTOS low power characteristics (for example, in a certain mode, the core stops running, at which point an external timer or RTC can replace Systick as the system timer).
Taking STM32F4 as an example, there are relevant low power application examples in the STM32CubeF4 firmware library. After decompressing the firmware package, it is located at…\stm32cubef4\STM32Cube_FW_F4_V1.10.0\Projects\STM324x9I_EVAL\Applications\FreeRTOS\FreeRTOS_LowPower, interested readers can read it themselves.
============================
Previous topics link:
1. How to run code in RAM under KEIL environment
2. USART baud rate auto-recognition problem of M0
3. Video resource library that STM32 enthusiasts must know
4. RTOS training notice in October 2016
5. Several common problems in FREERTOS applications based on STM32