2021-09-13 17:05:18 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Linaro Limited
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 11:11:04 +02:00
|
|
|
#include <zephyr/pm/pm.h>
|
2021-09-13 17:05:18 +02:00
|
|
|
#include <soc.h>
|
2022-05-06 11:11:04 +02:00
|
|
|
#include <zephyr/init.h>
|
2021-09-13 17:05:18 +02:00
|
|
|
|
|
|
|
#include <stm32u5xx_ll_utils.h>
|
|
|
|
#include <stm32u5xx_ll_bus.h>
|
|
|
|
#include <stm32u5xx_ll_cortex.h>
|
|
|
|
#include <stm32u5xx_ll_pwr.h>
|
|
|
|
#include <stm32u5xx_ll_rcc.h>
|
|
|
|
#include <stm32u5xx_ll_system.h>
|
|
|
|
#include <clock_control/clock_stm32_ll_common.h>
|
|
|
|
|
2022-05-06 11:11:04 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2021-09-13 17:05:18 +02:00
|
|
|
LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
|
|
|
|
|
|
|
|
/* select MSI as wake-up system clock if configured, HSI otherwise */
|
|
|
|
#if STM32_SYSCLK_SRC_MSI
|
|
|
|
#define RCC_STOP_WAKEUPCLOCK_SELECTED LL_RCC_STOP_WAKEUPCLOCK_MSI
|
|
|
|
#else
|
|
|
|
#define RCC_STOP_WAKEUPCLOCK_SELECTED LL_RCC_STOP_WAKEUPCLOCK_HSI
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void set_mode_stop(uint8_t substate_id)
|
|
|
|
{
|
|
|
|
/* ensure the proper wake-up system clock */
|
|
|
|
LL_RCC_SetClkAfterWakeFromStop(RCC_STOP_WAKEUPCLOCK_SELECTED);
|
|
|
|
|
|
|
|
switch (substate_id) {
|
|
|
|
case 1: /* enter STOP0 mode */
|
|
|
|
LL_PWR_SetPowerMode(LL_PWR_STOP0_MODE);
|
|
|
|
break;
|
|
|
|
case 2: /* enter STOP1 mode */
|
|
|
|
LL_PWR_SetPowerMode(LL_PWR_STOP1_MODE);
|
|
|
|
break;
|
|
|
|
case 3: /* enter STOP2 mode */
|
|
|
|
LL_PWR_SetPowerMode(LL_PWR_STOP2_MODE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_DBG("Unsupported power state substate-id %u", substate_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_mode_standby(uint8_t substate_id)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(substate_id);
|
|
|
|
/* Select standby mode */
|
|
|
|
LL_PWR_SetPowerMode(LL_PWR_STANDBY_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_mode_shutdown(uint8_t substate_id)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(substate_id);
|
|
|
|
/* Select shutdown mode */
|
|
|
|
LL_PWR_SetPowerMode(LL_PWR_SHUTDOWN_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invoke Low Power/System Off specific Tasks */
|
2022-02-04 17:19:52 -08:00
|
|
|
__weak void pm_state_set(enum pm_state state, uint8_t substate_id)
|
2021-09-13 17:05:18 +02:00
|
|
|
{
|
2022-01-21 18:28:10 +01:00
|
|
|
switch (state) {
|
2021-09-13 17:05:18 +02:00
|
|
|
case PM_STATE_SUSPEND_TO_IDLE:
|
2022-01-21 18:28:10 +01:00
|
|
|
set_mode_stop(substate_id);
|
2021-09-13 17:05:18 +02:00
|
|
|
break;
|
|
|
|
case PM_STATE_STANDBY:
|
|
|
|
/* To be tested */
|
2022-01-21 18:28:10 +01:00
|
|
|
set_mode_standby(substate_id);
|
2021-09-13 17:05:18 +02:00
|
|
|
break;
|
|
|
|
case PM_STATE_SOFT_OFF:
|
2022-01-21 18:28:10 +01:00
|
|
|
set_mode_shutdown(substate_id);
|
2021-09-13 17:05:18 +02:00
|
|
|
break;
|
2021-11-26 17:10:47 +01:00
|
|
|
default:
|
2022-01-21 18:28:10 +01:00
|
|
|
LOG_DBG("Unsupported power state %u", state);
|
2021-09-13 17:05:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set SLEEPDEEP bit of Cortex System Control Register */
|
|
|
|
LL_LPM_EnableDeepSleep();
|
|
|
|
|
|
|
|
/* Select mode entry : WFE or WFI and enter the CPU selected mode */
|
|
|
|
k_cpu_idle();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle SOC specific activity after Low Power Mode Exit */
|
2022-02-05 22:24:29 -08:00
|
|
|
__weak void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id)
|
2021-09-13 17:05:18 +02:00
|
|
|
{
|
2022-01-21 18:28:10 +01:00
|
|
|
switch (state) {
|
2021-09-13 17:05:18 +02:00
|
|
|
case PM_STATE_SUSPEND_TO_IDLE:
|
2022-01-21 18:28:10 +01:00
|
|
|
if (substate_id <= 3) {
|
2021-09-13 17:05:18 +02:00
|
|
|
LL_LPM_DisableSleepOnExit();
|
|
|
|
LL_LPM_EnableSleep();
|
|
|
|
} else {
|
|
|
|
LOG_DBG("Unsupported power substate-id %u",
|
2022-01-21 18:28:10 +01:00
|
|
|
substate_id);
|
2021-09-13 17:05:18 +02:00
|
|
|
}
|
|
|
|
case PM_STATE_STANDBY:
|
|
|
|
/* To be tested */
|
|
|
|
LL_LPM_EnableSleep();
|
|
|
|
case PM_STATE_SOFT_OFF:
|
|
|
|
/* We should not get there */
|
|
|
|
__fallthrough;
|
|
|
|
case PM_STATE_ACTIVE:
|
|
|
|
__fallthrough;
|
|
|
|
case PM_STATE_SUSPEND_TO_RAM:
|
|
|
|
__fallthrough;
|
|
|
|
case PM_STATE_SUSPEND_TO_DISK:
|
|
|
|
__fallthrough;
|
|
|
|
default:
|
2022-01-21 18:28:10 +01:00
|
|
|
LOG_DBG("Unsupported power state %u", state);
|
2021-09-13 17:05:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* need to restore the clock */
|
|
|
|
stm32_clock_control_init(NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* System is now in active mode.
|
|
|
|
* Reenable interrupts which were disabled
|
|
|
|
* when OS started idling code.
|
|
|
|
*/
|
|
|
|
irq_unlock(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize STM32 Power */
|
init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:
- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices
They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:
```c
struct init_entry {
int (*init)(const struct device *dev);
/* only set by DEVICE_*, otherwise NULL */
const struct device *dev;
}
```
As a result, we end up with such weird/ugly pattern:
```c
static int my_init(const struct device *dev)
{
/* always NULL! add ARG_UNUSED to avoid compiler warning */
ARG_UNUSED(dev);
...
}
```
This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:
```c
static int my_init(void)
{
...
}
```
This is achieved using a union:
```c
union init_function {
/* for SYS_INIT, used when init_entry.dev == NULL */
int (*sys)(void);
/* for DEVICE*, used when init_entry.dev != NULL */
int (*dev)(const struct device *dev);
};
struct init_entry {
/* stores init function (either for SYS_INIT or DEVICE*)
union init_function init_fn;
/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
* to know which union entry to call.
*/
const struct device *dev;
}
```
This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.
**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
init: convert SYS_INIT functions to the new signature
Conversion scripted using scripts/utils/migrate_sys_init.py.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
manifest: update projects for SYS_INIT changes
Update modules with updated SYS_INIT calls:
- hal_ti
- lvgl
- sof
- TraceRecorderSource
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: devicetree: devices: adjust test
Adjust test according to the recently introduced SYS_INIT
infrastructure.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: kernel: threads: adjust SYS_INIT call
Adjust to the new signature: int (*init_fn)(void);
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 09:33:44 +02:00
|
|
|
static int stm32_power_init(void)
|
2021-09-13 17:05:18 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/* enable Power clock */
|
|
|
|
LL_AHB3_GRP1_EnableClock(LL_AHB3_GRP1_PERIPH_PWR);
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
/* Enable the Debug Module during all and any Low power mode */
|
|
|
|
LL_DBGMCU_EnableDBGStopMode();
|
|
|
|
#endif /* CONFIG_DEBUG */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:36:34 +01:00
|
|
|
SYS_INIT(stm32_power_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
|