Home | Projects | Notes > Real-Time Operating Systems (RTOS) > Exercise: FreeRTOS Hook Functions
Write a program to send MCU to go to sleep mode when the idle task is scheduled to run on the CPU, and measure the current.
Hint: Begin with the project 03_LED_Task_Delay
, enable the configuration item configUSE_IDLE_HOOK
defined in the FreeRTOSConfig.h
and implement the vApplicationIdleHook()
in the application.
Low power mode:
WFI (Wait For Interrupt) makes the processor suspended execution (Clock is stopped) until one of the following events take place:
An IRQ interrupt
An FIQ interrupt
A Debug Entry request made to the processor
xxxxxxxxxx
451/* Project/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c */
2
3/**
4* @brief Enters Sleep mode.
5*
6* @note In Sleep mode, all I/O pins keep the same state as in Run mode.
7*
8* @note In Sleep mode, the systick is stopped to avoid exit from this mode with
9* systick interrupt when used as time base for Timeout
10*
11* @param Regulator Specifies the regulator state in SLEEP mode.
12* This parameter can be one of the following values:
13* @arg PWR_MAINREGULATOR_ON: SLEEP mode with regulator ON
14* @arg PWR_LOWPOWERREGULATOR_ON: SLEEP mode with low power regulator ON
15* @note This parameter is not used for the STM32F4 family and is kept as parameter
16* just to maintain compatibility with the lower power families.
17* @param SLEEPEntry Specifies if SLEEP mode in entered with WFI or WFE instruction.
18* This parameter can be one of the following values:
19* @arg PWR_SLEEPENTRY_WFI: enter SLEEP mode with WFI instruction
20* @arg PWR_SLEEPENTRY_WFE: enter SLEEP mode with WFE instruction
21* @retval None
22*/
23void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
24{
25/* Check the parameters */
26assert_param(IS_PWR_REGULATOR(Regulator));
27assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));
28
29/* Clear SLEEPDEEP bit of Cortex System Control Register */
30CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
31
32/* Select SLEEP mode entry -------------------------------------------------*/
33if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
34{
35/* Request Wait For Interrupt */
36__WFI();
37}
38else
39{
40/* Request Wait For Event */
41__SEV();
42__WFE();
43__WFE();
44}
45}
In the case of 03_LED_Task_Delay
project where the processor was never sent to the low-power mode while running the idle task, the current was measured at around 60 mA.
But in this project (08_Idle_Hook
) where the processor was sent to the low-power mode by using the Idle Hook function, the current was measured at around 20 mA. (Less power consumption!)
How to measure the current of STM32F407G-Discovery board? See the section 6.8 of "Discovery kit with STM32F407G MCU" manual.
Nayak, K. (2022). Mastering RTOS: Hands on FreeRTOS and STM32Fx with Debugging [Video file]. Retrieved from https://www.udemy.com/course/mastering-rtos-hands-on-with-freertos-arduino-and-stm32fx/