Skip to content

Commit 3faf095

Browse files
committed
soc: stm32f7: add power management support
Adds low-power mode support for STM32F7 series Implements basic sleep and stop modes integrated with the power management subsystem, similar to the STM32F4 implementation. Added Mode: STOP low-power mode, exposed as Zephyr PM state "suspend-to-idle". Tested on: NUCLEO-F722ZE board. Signed-off-by: Roy Jamil <roy.jamil@ac6.fr>
1 parent f955d54 commit 3faf095

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

dts/arm/st/f7/stm32f7.dtsi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
chosen {
2929
zephyr,entropy = &rng;
3030
zephyr,flash-controller = &flash;
31+
zephyr,cortex-m-idle-timer = &rtc;
3132
};
3233

3334
cpus {
@@ -38,6 +39,7 @@
3839
device_type = "cpu";
3940
compatible = "arm,cortex-m7";
4041
reg = <0>;
42+
cpu-power-states = <&stop>;
4143
#address-cells = <1>;
4244
#size-cells = <1>;
4345

@@ -46,6 +48,14 @@
4648
reg = <0xe000ed90 0x40>;
4749
};
4850
};
51+
power-states {
52+
stop: stop {
53+
compatible = "zephyr,power-state";
54+
power-state-name = "suspend-to-idle";
55+
min-residency-us = <400>;
56+
exit-latency-us = <300>;
57+
};
58+
};
4959
};
5060

5161
quadspi_memory: memory-placeholder@90000000 {

soc/st/stm32/stm32f7x/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ zephyr_sources(
88
zephyr_include_directories(.)
99

1010
set(SOC_LINKER_SCRIPT ${ZEPHYR_BASE}/include/zephyr/arch/arm/cortex_m/scripts/linker.ld CACHE INTERNAL "")
11+
12+
zephyr_sources_ifdef(CONFIG_PM
13+
power.c
14+
)

soc/st/stm32/stm32f7x/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ config SOC_SERIES_STM32F7X
1414
select HAS_STM32_FLASH_PREFETCH
1515
select CPU_HAS_ARM_MPU
1616
select HAS_SWO
17+
select HAS_PM
1718
select SOC_EARLY_INIT_HOOK

soc/st/stm32/stm32f7x/power.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2025 Ac6
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <clock_control/clock_stm32_ll_common.h>
8+
#include <soc.h>
9+
10+
#include <stm32f7xx_ll_bus.h>
11+
#include <stm32f7xx_ll_cortex.h>
12+
#include <stm32f7xx_ll_pwr.h>
13+
#include <stm32f7xx.h>
14+
15+
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
16+
#include <zephyr/drivers/counter.h>
17+
#include <zephyr/drivers/interrupt_controller/gic.h>
18+
#include <zephyr/kernel.h>
19+
#include <zephyr/logging/log.h>
20+
#include <zephyr/pm/pm.h>
21+
#include <zephyr/init.h>
22+
23+
LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
24+
25+
BUILD_ASSERT(DT_SAME_NODE(DT_CHOSEN(zephyr_cortex_m_idle_timer), DT_NODELABEL(rtc)),
26+
"STM32Fx series needs RTC as an additional IDLE timer for power management");
27+
28+
void pm_state_set(enum pm_state state, uint8_t substate_id)
29+
{
30+
ARG_UNUSED(substate_id);
31+
32+
switch (state) {
33+
case PM_STATE_SUSPEND_TO_IDLE:
34+
/* Prepare STOP mode with low-power regulator */
35+
LL_LPM_DisableEventOnPend();
36+
LL_PWR_ClearFlag_WU1();
37+
LL_PWR_ClearFlag_WU2();
38+
LL_PWR_ClearFlag_WU3();
39+
LL_PWR_ClearFlag_WU4();
40+
LL_PWR_ClearFlag_WU5();
41+
LL_PWR_ClearFlag_WU6();
42+
43+
/* Use low-power regulator in STOP to reduce consumption */
44+
LL_PWR_SetPowerMode(LL_PWR_MODE_STOP_LPREGU);
45+
LL_LPM_EnableDeepSleep();
46+
47+
k_cpu_idle();
48+
49+
break;
50+
default:
51+
LOG_DBG("Unsupported power state %u", state);
52+
break;
53+
}
54+
}
55+
56+
void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id)
57+
{
58+
ARG_UNUSED(substate_id);
59+
60+
switch (state) {
61+
case PM_STATE_SUSPEND_TO_IDLE:
62+
/* Back to normal sleep and restore clocks after STOP */
63+
LL_LPM_DisableSleepOnExit();
64+
LL_LPM_EnableSleep();
65+
66+
/* Restore the clock setup after STOP mode */
67+
stm32_clock_control_init(NULL);
68+
break;
69+
70+
default:
71+
LOG_DBG("Unsupported power substate-id %u", state);
72+
break;
73+
}
74+
75+
/* System is now active, re-enable interrupts disabled on entry */
76+
irq_unlock(0);
77+
}

soc/st/stm32/stm32f7x/soc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ void soc_early_init_hook(void)
3737
/* Update CMSIS SystemCoreClock variable (HCLK) */
3838
/* At reset, system core clock is set to 16 MHz from HSI */
3939
SystemCoreClock = 16000000;
40+
41+
#if defined(CONFIG_PM)
42+
/* Ensure PWR peripheral clock is enabled on APB1 */
43+
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
44+
#endif
4045
}

0 commit comments

Comments
 (0)