Skip to content

Commit 1966b7f

Browse files
committed
soc: Add power state support for STM32G0
1 parent 2cb6420 commit 1966b7f

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

soc/arm/st_stm32/stm32g0/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
44
zephyr_sources(
55
soc.c
66
)
7+
8+
zephyr_sources_ifdef(CONFIG_PM
9+
power.c
10+
)

soc/arm/st_stm32/stm32g0/Kconfig.defconfig.series

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ source "soc/arm/st_stm32/stm32g0/Kconfig.defconfig.stm32g0*"
1111
config SOC_SERIES
1212
default "stm32g0"
1313

14+
if PM
15+
config PM_DEVICE
16+
default y
17+
18+
config STM32_LPTIM_TIMER
19+
default y
20+
endif # PM
21+
1422
endif # SOC_SERIES_STM32G0X

soc/arm/st_stm32/stm32g0/Kconfig.series

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Copyright (c) 2019 Philippe Retornaz <[email protected]>
44
# Copyright (c) 2019 STMicroelectronics
5+
# Copyright (c) 2021 G-Technologies Sdn. bhd.
56
# SPDX-License-Identifier: Apache-2.0
67

78
config SOC_SERIES_STM32G0X
@@ -13,5 +14,7 @@ config SOC_SERIES_STM32G0X
1314
select SOC_FAMILY_STM32
1415
select HAS_STM32CUBE
1516
select CPU_CORTEX_M_HAS_SYSTICK
17+
select HAS_SYS_POWER_STATE_SLEEP_1
18+
select HAS_SYS_POWER_STATE_SLEEP_2
1619
help
1720
Enable support for STM32G0 MCU series

soc/arm/st_stm32/stm32g0/power.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2021 STMicroelectronics.
3+
* Copyright (c) 2021 G-Technologies Sdn. Bhd.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
#include <zephyr.h>
8+
#include <pm/pm.h>
9+
#include <soc.h>
10+
#include <init.h>
11+
12+
#include <stm32g0xx_ll_utils.h>
13+
#include <stm32g0xx_ll_bus.h>
14+
#include <stm32g0xx_ll_cortex.h>
15+
#include <stm32g0xx_ll_pwr.h>
16+
#include <stm32g0xx_ll_rcc.h>
17+
#include <stm32g0xx_ll_system.h>
18+
#include <clock_control/clock_stm32_ll_common.h>
19+
20+
#include <logging/log.h>
21+
LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
22+
23+
/* Invoke Low Power/System Off specific Tasks */
24+
void pm_power_state_set(struct pm_state_info info)
25+
{
26+
if (info.state != PM_STATE_SUSPEND_TO_IDLE) {
27+
LOG_DBG("Unsupported power state %u", info.state);
28+
return;
29+
}
30+
31+
switch (info.substate_id) {
32+
case 1: /* this corresponds to the STOP0 mode: */
33+
/* enter STOP0 mode */
34+
LL_PWR_SetPowerMode(LL_PWR_MODE_STOP0);
35+
LL_LPM_EnableDeepSleep();
36+
/* enter SLEEP mode : WFE or WFI */
37+
k_cpu_idle();
38+
break;
39+
case 2: /* this corresponds to the STOP1 mode: */
40+
/* enter STOP1 mode */
41+
LL_PWR_SetPowerMode(LL_PWR_MODE_STOP1);
42+
LL_LPM_EnableDeepSleep();
43+
/* enter SLEEP mode : WFE or WFI */
44+
k_cpu_idle();
45+
break;
46+
default:
47+
LOG_DBG("Unsupported power state substate-id %u",
48+
info.substate_id);
49+
break;
50+
}
51+
}
52+
53+
/* Handle SOC specific activity after Low Power Mode Exit */
54+
void pm_power_state_exit_post_ops(struct pm_state_info info)
55+
{
56+
if (info.state != PM_STATE_SUSPEND_TO_IDLE) {
57+
LOG_DBG("Unsupported power substate-id %u", info.state);
58+
} else {
59+
switch (info.substate_id) {
60+
case 1: /* STOP0 */
61+
__fallthrough;
62+
case 2: /* STOP1 */
63+
LL_PWR_SetPowerMode(LL_PWR_MODE_STOP0);
64+
LL_LPM_EnableSleep();
65+
__fallthrough;
66+
default:
67+
LOG_DBG("Unsupported power substate-id %u",
68+
info.substate_id);
69+
break;
70+
}
71+
/* need to restore the clock */
72+
stm32_clock_control_init(NULL);
73+
}
74+
75+
/*
76+
* System is now in active mode.
77+
* Reenable interrupts which were disabled
78+
* when OS started idling code.
79+
*/
80+
irq_unlock(0);
81+
}
82+
83+
/* Initialize STM32 Power */
84+
static int stm32_power_init(const struct device *dev)
85+
{
86+
ARG_UNUSED(dev);
87+
88+
/* enable Power clock */
89+
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
90+
91+
#ifdef CONFIG_DEBUG
92+
/* Enable the Debug Module during all and any Low power mode */
93+
LL_DBGMCU_EnableDBGStopMode();
94+
#endif /* CONFIG_DEBUG */
95+
96+
return 0;
97+
}
98+
99+
SYS_INIT(stm32_power_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

0 commit comments

Comments
 (0)