Skip to content

Commit f7ba9ce

Browse files
ycsinnashif
authored andcommitted
soc: arm: stm32g0: Add PM support
Low power modes for the STM32G0 series. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent c235144 commit f7ba9ce

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
# Copyright (c) 2021 G-Technologies Sdn. Bhd.
12
# SPDX-License-Identifier: Apache-2.0
23

34
zephyr_include_directories(${ZEPHYR_BASE}/drivers)
45
zephyr_sources(
56
soc.c
67
)
8+
9+
zephyr_sources_ifdef(CONFIG_PM
10+
power.c
11+
)

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

Lines changed: 9 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
if SOC_SERIES_STM32G0X
@@ -11,4 +12,12 @@ source "soc/arm/st_stm32/stm32g0/Kconfig.defconfig.stm32g0*"
1112
config SOC_SERIES
1213
default "stm32g0"
1314

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

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_system.h>
17+
#include <clock_control/clock_stm32_ll_common.h>
18+
19+
#include <logging/log.h>
20+
LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
21+
22+
/* Invoke Low Power/System Off specific Tasks */
23+
void pm_power_state_set(struct pm_state_info info)
24+
{
25+
if (info.state != PM_STATE_SUSPEND_TO_IDLE) {
26+
LOG_DBG("Unsupported power state %u", info.state);
27+
return;
28+
}
29+
30+
switch (info.substate_id) {
31+
case 1: /* this corresponds to the STOP0 mode: */
32+
/* enter STOP0 mode */
33+
LL_PWR_SetPowerMode(LL_PWR_MODE_STOP0);
34+
LL_LPM_EnableDeepSleep();
35+
/* enter SLEEP mode : WFE or WFI */
36+
k_cpu_idle();
37+
break;
38+
case 2: /* this corresponds to the STOP1 mode: */
39+
/* enter STOP1 mode */
40+
LL_PWR_SetPowerMode(LL_PWR_MODE_STOP1);
41+
LL_LPM_EnableDeepSleep();
42+
/* enter SLEEP mode : WFE or WFI */
43+
k_cpu_idle();
44+
break;
45+
default:
46+
LOG_DBG("Unsupported power state substate-id %u",
47+
info.substate_id);
48+
break;
49+
}
50+
}
51+
52+
/* Handle SOC specific activity after Low Power Mode Exit */
53+
void pm_power_state_exit_post_ops(struct pm_state_info info)
54+
{
55+
if (info.state != PM_STATE_SUSPEND_TO_IDLE) {
56+
LOG_DBG("Unsupported power substate %u", info.state);
57+
} else {
58+
switch (info.substate_id) {
59+
case 1: /* STOP0 */
60+
__fallthrough;
61+
case 2: /* STOP1 */
62+
LL_LPM_DisableSleepOnExit();
63+
/* Clear SLEEPDEEP bit */
64+
LL_LPM_EnableSleep();
65+
break;
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)