Skip to content

Commit 88de80b

Browse files
lkoudelkaMaureenHelm
authored andcommitted
drivers: clock_control: stm32: adding config_regulator_voltage for L0
STM32 MCU shall set voltage regulator level with respect to set clock frequency to reach optimal power consumption. Voltage regulator is set prior to clock setting based on configuration from dts/overlay file. Config_regulator_voltage is set as weak in clock_stm32_ll_common - config_regulator_voltage can be extended to other STM32 families without need to rewrite heavily family clock driver, default one can be still used. Signed-off-by: Lubos Koudelka <[email protected]>
1 parent 048b755 commit 88de80b

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

drivers/clock_control/clock_stm32_ll_common.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ int stm32_clock_control_init(const struct device *dev)
733733

734734
/* Some clocks would be activated by default */
735735
config_enable_default_clocks();
736+
config_regulator_voltage(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC);
736737

737738
#if defined(FLASH_ACR_LATENCY)
738739
uint32_t old_flash_freq;
@@ -838,6 +839,7 @@ void HAL_RCC_CSSCallback(void)
838839
}
839840
#endif
840841

842+
void __weak config_regulator_voltage(uint32_t hclk_freq) {}
841843
/**
842844
* @brief RCC device, note that priority is intentionally set to 1 so
843845
* that the device init runs just after SOC init

drivers/clock_control/clock_stm32_ll_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void config_pll2(void);
4949
void config_plli2s(void);
5050
#endif
5151
void config_enable_default_clocks(void);
52+
void config_regulator_voltage(uint32_t hclk_freq);
5253

5354
/* functions exported to the soc power.c */
5455
int stm32_clock_control_init(const struct device *dev);

drivers/clock_control/clock_stm32l0_l1.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <soc.h>
1010
#include <stm32_ll_bus.h>
1111
#include <stm32_ll_rcc.h>
12+
#include <stm32_ll_pwr.h>
1213
#include <stm32_ll_utils.h>
1314
#include <zephyr/drivers/clock_control.h>
1415
#include <zephyr/sys/util.h>
@@ -81,6 +82,22 @@ uint32_t get_pllout_frequency(void)
8182

8283
#endif /* defined(STM32_PLL_ENABLED) */
8384

85+
/**
86+
* @brief Set up voltage regulator voltage
87+
*/
88+
void config_regulator_voltage(uint32_t hclk_freq)
89+
{
90+
if (hclk_freq <= MHZ(4.2)) {
91+
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE3);
92+
} else if (hclk_freq <= MHZ(16)) {
93+
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE2);
94+
} else {
95+
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
96+
}
97+
while (LL_PWR_IsActiveFlag_VOS() == 1) {
98+
}
99+
}
100+
84101
/**
85102
* @brief Activate default clocks
86103
*/
@@ -92,4 +109,5 @@ void config_enable_default_clocks(void)
92109
/* Enable System Configuration Controller clock. */
93110
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
94111
#endif
112+
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
95113
}

soc/st/stm32/stm32l0x/soc.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <zephyr/linker/linker-defs.h>
1515
#include <string.h>
1616
#include <stm32_ll_bus.h>
17-
#include <stm32_ll_pwr.h>
18-
#include <stm32_ll_bus.h>
1917

2018
#include <cmsis_core.h>
2119

@@ -33,13 +31,6 @@ static int stm32l0_init(void)
3331
/* At reset, system core clock is set to 2.1 MHz from MSI */
3432
SystemCoreClock = 2097152;
3533

36-
/* Default Voltage scaling range selection (range2)
37-
* doesn't allow to configure Max frequency
38-
* switch to range1 to match any frequency
39-
*/
40-
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
41-
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
42-
4334
/* On STM32L0, there are some hardfault when enabling DBGMCU bit:
4435
* Sleep, Stop or Standby.
4536
* See https://github.com/zephyrproject-rtos/zephyr/issues/#37119

0 commit comments

Comments
 (0)