Library 39- Power consumption modes for STM32F4
For library 39, I made a low power modes library. This allows you to simple put STM32F4 into SLEEP, STOP or STANDBY mode. Point of this modes is simple. You want decrease current on device. If you are working on battery project, this will be very helpful.
You can set current down to about 2uA with proper low power mode.
I will tell you only the highlights of all 3 power down modes in this post. For detailed informations, check ST’s manual here.
Library
Features
- Enable SLEEP mode
- Enable STOP mode
- Enable STANDBY mode
- You can also test, if system was reset because of wakeup from Standby mode.
- Enable/disable wakeup pin, pin PA0
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx PWR
- TM
- TM DELAY
- defines.h
- TM DELAY
STM32F4 | Description |
---|---|
PA0 | If STM32F4 is in standby mode, rising edge on this pin will wake him up if it is activated. |
STM32F4 power consumption modes
Highlights below are from st’s manual from their website.
SLEEP mode
- Cortex-M4 core is stopped.
- Peripherals kept running.
- How to enter this mode?
- Look for example 1 below.
- How to exit this mode?
- Any peripheral interrupt acknowledged by the nested vectored interrupt controller (NVIC).
- Systick timer will also wake up MCU!
- After exit, MCU continues where it stopped.
STOP mode
- All clocks in 1.2V domain are stopped.
- PLL, HSI and HSE RC oscillators disabled.
- Internal SRAM and register contents are preserved.
- Voltage regulator in low-power mode.
- How to enter this mode?
- Look for example 2 below.
- How to exit this mode?
- Any EXTI Line (Internal or External) configured in Interrupt/Event mode.
- After exit, MCU continue there where it stopped. HSI is used for system core clock, but my library set it back to PLL as source.
STANDBY mode
- Cortex-M4 deepsleep mode.
- Voltage regulator disabled.
- 1.2V domain consequently powered off.
- PLL, HSI and HSE RC oscillators disabled.
- SRAM and register contents are lost except for the RTC registers, RTC backup registers, backup SRAM and Standby circuitry.
- How to enter this mode?
- Look for example 3 below.
- How to exit this mode?
- Wakeup pin rising edge.
- RTC alarm (Alarm A and B) and wakeup.
- Tamper and time-stamp event.
- External reset in NRST pin.
- IWDG reset.
- After exit, STM32F4xx reset.
VBAT mode
- Main digital supply is turned off.
- The circuit is supplied through VBAT pin which should be connected to an external supply voltage (a battery or any other source).
- RTC is running in this mode.
- How to enter this mode?
- If you disable main power and put supply voltage on VBAT pin, then MCU will be in VBAT mode.
- How to exit this mode?
- Put power supply on main Vdd pins.
Note
If you put your STM32F4 into low power mode, it will become unavailable for normal programming. You will have to program it under reset mode or wake it up before programming.
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/** * Go to sleep mode * MCU will be in sleep mode until next interrupt occured. * This means, that delay timer (Systick or any user selectable delay timer) can wakeup STM32F4 too. * If you want prevent this, you have to disable delay timer as well it. * This can be done with this function * * Parameters: * - uint8_t delay_timer: * 0: Leave delay timer as it is * > 0: Before enter sleep mode, disable delay timer, after wakeup, enable delay timer back * * No return */ extern void TM_LOWPOWER_SleepUntilInterrupt(uint8_t delay_timer); /** * Go to sleep mode * MCU will be in sleep mode until next event occured. * * No return */ extern void TM_LOWPOWER_SleepUntilEvent(void); /** * Go to STOP mode with "Wait For Interrupt" instruction * * No return */ extern void TM_LOWPOWER_StopUntilInterrupt(void); /** * Go to STOP mode with "Wait For Event" instruction * * No return */ extern void TM_LOWPOWER_StopUntilEvent(void); /** * Go to standby mode * * No return */ extern void TM_LOWPOWER_Standby(void); /** * Check if system was reset because of wakeup from standby mode * * Returns * 0: System was not reset because of wake up from standby mode * 1: System was reset because of wake up from standby mode */ extern uint8_t TM_LOWPOWER_StandbyReset(void); /** * Enable wakeup pin, fixed pin, PA0. * Rising edge of this pin will wakeup MCU from Standby mode. * * No return */ extern void TM_LOWPOWER_EnableWakeUpPin(void); /** * Disable wakeup pin functionality * * No return */ extern void TM_LOWPOWER_DisableWakeUpPin(void); |
Example 1
This example will put STM32F4 into sleep mode.
- RTC is configured with internal clock to generate wakeup interrupts every 10 seconds.
- After STM is wake, it will toggle green LED 20 times (10 times on, 10 times off) and MCU will go to SLEEP mode.
- Each time MCU will be waked up, RED led will be toggled.
- If you put Ammeter on IDD pins on STM32F4/29-Discovery or Nucleo board, you will see how current vary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
/** * Keil project for low power mode - Sleep mode * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_low_power.h" #include "tm_stm32f4_rtc.h" int main(void) { uint8_t i; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize LEDS */ TM_DISCO_LedInit(); /* Initialize RTC with internal clock */ TM_RTC_Init(TM_RTC_ClockSource_Internal); /* Set RTC to generate wakeup interrupt every 10 seconds */ TM_RTC_Interrupts(TM_RTC_Int_10s); /* Set time to 0 */ TM_DELAY_SetTime(0); while (1) { /* Toggle LEDs every 200ms */ if (TM_DELAY_Time() >= 200) { /* Reset time */ TM_DELAY_SetTime(0); /* Toggle leds */ TM_DISCO_LedToggle(LED_GREEN); /* Increase counter */ i++; /* After 20 toggles, put STM32F4 into sleep mode */ if (i == 20) { /* Reset counter */ i = 0; /* Sleep until interrupt occur */ /* Also disable systick with "1" as parameter */ /* Because systick makes interrupts and it will wakeup */ /* device back after some ticks. This is useless */ /* If you set parameter to "0", then this function will not */ /* affect to Systick timer */ TM_LOWPOWER_SleepUntilInterrupt(1); /* Toggle RED LED to indicate wakeup from sleep mode */ TM_DISCO_LedToggle(LED_RED); } } } } |
Example 2
- RTC is configured with internal clock to generate wakeup interrupts every 10 seconds
- After STM is wake, it will toggle green LED 20 times (10 times on, 10 times off) and MCU will go to STOP mode
- Each time MCU will be waked up, RED led will be toggled.
- If you put Ammeter on IDD pins on STM32F4/29-Discovery or Nucleo board, you will see how current vary.
- Main difference between SLEEP and STOP mode is that in sleep mode only processor sleep, peripherals work, in STOP mode everything is stopped, except EXTI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
/** * Keil project for low power mode - STOP mode * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_low_power.h" #include "tm_stm32f4_rtc.h" int main(void) { uint8_t i; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize LEDS */ TM_DISCO_LedInit(); /* Initialize RTC with internal clock */ TM_RTC_Init(TM_RTC_ClockSource_Internal); /* Set RTC to generate wakeup interrupt every 10 seconds */ TM_RTC_Interrupts(TM_RTC_Int_10s); /* Set time to 0 */ TM_DELAY_SetTime(0); while (1) { /* Toggle LEDs every 200ms */ if (TM_DELAY_Time() >= 200) { /* Reset time */ TM_DELAY_SetTime(0); /* Toggle leds */ TM_DISCO_LedToggle(LED_GREEN); /* Increase counter */ i++; /* After 20 toggles, put STM32F4 into STOP mode */ if (i == 20) { /* Reset counter */ i = 0; /* Stop STM32F4 */ /* If you stop device, then you can wake him up with interrupt/event on EXTI line */ /* Put it into STOP mode, allowing EXTI interrupts to wake him up */ /* RTC will wake him up after 10 seconds */ TM_LOWPOWER_StopUntilInterrupt(); /* Toggle RED LED to indicate wakeup from stop mode */ TM_DISCO_LedToggle(LED_RED); } } } } |
Example 3
- RTC is configured with internal clock to generate wakeup interrupts every 10 seconds
- After STM is wake, it will toggle green LED 20 times (10 times on, 10 times off) and MCU will go to STANDBY mode
- Each time MCU will be waked up, RED led will be toggled.
- If you put Ammeter on IDD pins on STM32F4/29-Discovery or Nucleo board, you will see how current vary.
- RED led should never be ON, because MCU will reset after wakeup
- RED led will toggle, when MCU will reset after wakeup from MCU
- If you reset system with “Reset button”, then RED led should not be toggled
- You want wake up device if you press the button (rising edge on PA0 pin) or waiting for RTC to make it every 10 seconds for you
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
/** * Keil project for low power mode - STANDBY mode * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_low_power.h" #include "tm_stm32f4_rtc.h" int main(void) { uint8_t i; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize LEDS */ TM_DISCO_LedInit(); /* Checks if reset was because of wakeup from standby */ if (TM_LOWPOWER_StandbyReset()) { for (i = 0; i < 10; i++) { /* Toggle LED red to indicate this */ TM_DISCO_LedToggle(LED_RED); /* Delay */ Delayms(100); } } /* Initialize RTC with internal clock */ TM_RTC_Init(TM_RTC_ClockSource_Internal); /* Set RTC to generate wakeup interrupt every 10 seconds */ TM_RTC_Interrupts(TM_RTC_Int_10s); /* Enable wakeup pin, PA0 */ TM_LOWPOWER_EnableWakeUpPin(); /* Set time to 0 */ TM_DELAY_SetTime(0); while (1) { /* Toggle LEDs every 200ms */ if (TM_DELAY_Time() >= 200) { /* Reset time */ TM_DELAY_SetTime(0); /* Toggle leds */ TM_DISCO_LedToggle(LED_GREEN); /* Increase counter */ i++; /* After 20 toggles, put STM32F4 into STANDBY mode */ if (i == 20) { /* Reset counter */ i = 0; /* Put STM32F4 into standby mode */ /* You can wake up MCU with rising edge on PA0 pin */ /* Or with some special interrupts, like RTC, etc */ TM_LOWPOWER_Standby(); /* Toggle RED LED to indicate wakeup from STANDBY mode */ /* This should never happen, because STM32F4 will reset after wakeup from STANDBY */ TM_DISCO_LedToggle(LED_RED); } } } } |
All examples are available separately on Github, download library below.
Allows you to put STM32F4 dvice into different low power modes.
Recent comments