|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Google LLC |
| 3 | + * Copyright (c) 2025 Tomas Jurena |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <clock_control/clock_stm32_ll_common.h> |
| 9 | +#include <soc.h> |
| 10 | + |
| 11 | +#include <stm32c0xx_ll_cortex.h> |
| 12 | +#include <stm32c0xx_ll_pwr.h> |
| 13 | +#include <stm32c0xx.h> |
| 14 | + |
| 15 | +#include <zephyr/kernel.h> |
| 16 | +#include <zephyr/logging/log.h> |
| 17 | + |
| 18 | +LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL); |
| 19 | + |
| 20 | +BUILD_ASSERT(DT_SAME_NODE(DT_CHOSEN(zephyr_cortex_m_idle_timer), DT_NODELABEL(rtc)), |
| 21 | + "STM32C0x series needs RTC as an additional IDLE timer for power management"); |
| 22 | + |
| 23 | +void pm_state_set(enum pm_state state, uint8_t substate_id) |
| 24 | +{ |
| 25 | + ARG_UNUSED(substate_id); |
| 26 | + |
| 27 | + switch (state) { |
| 28 | + case PM_STATE_SUSPEND_TO_IDLE: |
| 29 | + LL_LPM_DisableEventOnPend(); |
| 30 | + LL_PWR_ClearFlag_WU(); |
| 31 | + |
| 32 | + LL_PWR_SetPowerMode(LL_PWR_MODE_STOP0); |
| 33 | + LL_LPM_EnableDeepSleep(); |
| 34 | + |
| 35 | + k_cpu_idle(); |
| 36 | + |
| 37 | + break; |
| 38 | + default: |
| 39 | + LOG_WRN("Unsupported power state %u", state); |
| 40 | + break; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id) |
| 45 | +{ |
| 46 | + ARG_UNUSED(substate_id); |
| 47 | + |
| 48 | + switch (state) { |
| 49 | + case PM_STATE_SUSPEND_TO_IDLE: |
| 50 | + LL_LPM_DisableSleepOnExit(); |
| 51 | + LL_LPM_EnableSleep(); |
| 52 | + |
| 53 | + /* Restore the clock setup. */ |
| 54 | + stm32_clock_control_init(NULL); |
| 55 | + break; |
| 56 | + default: |
| 57 | + LOG_WRN("Unsupported power substate-id %u", state); |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + /* |
| 62 | + * System is now in active mode. Reenable interrupts which were |
| 63 | + * disabled when OS started idling code. |
| 64 | + */ |
| 65 | + irq_unlock(0); |
| 66 | +} |
0 commit comments