Skip to content

Commit f2ebfe5

Browse files
committed
esp: make cpu frequency settable
1 parent ed5591c commit f2ebfe5

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

locale/circuitpython.pot

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,11 +1029,27 @@ msgstr ""
10291029
msgid "Format not supported"
10301030
msgstr ""
10311031

1032+
#: ports/espressif/common-hal/microcontroller/Processor.c
1033+
msgid "Frequency must be 20, 40, 80 or 120MHz"
1034+
msgstr ""
1035+
1036+
#: ports/espressif/common-hal/microcontroller/Processor.c
1037+
msgid "Frequency must be 20, 40, 80 or 160MHz"
1038+
msgstr ""
1039+
1040+
#: ports/espressif/common-hal/microcontroller/Processor.c
1041+
msgid "Frequency must be 20, 40, 80, 160 or 240MHz"
1042+
msgstr ""
1043+
10321044
#: ports/mimxrt10xx/common-hal/microcontroller/Processor.c
10331045
msgid ""
10341046
"Frequency must be 24, 150, 396, 450, 528, 600, 720, 816, 912, 960 or 1008 Mhz"
10351047
msgstr ""
10361048

1049+
#: ports/espressif/common-hal/microcontroller/Processor.c
1050+
msgid "Frequency must be 32, 48, 64 or 96MHz"
1051+
msgstr ""
1052+
10371053
#: shared-bindings/bitbangio/I2C.c shared-bindings/bitbangio/SPI.c
10381054
#: shared-bindings/busio/I2C.c shared-bindings/busio/SPI.c
10391055
msgid "Function requires lock"

ports/espressif/common-hal/microcontroller/Processor.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
#include "py/runtime.h"
1212

13+
#include "bindings/espidf/__init__.h"
1314
#include "common-hal/microcontroller/Processor.h"
1415
#include "shared-bindings/microcontroller/Processor.h"
1516
#include "shared-bindings/microcontroller/ResetReason.h"
1617

1718
#include "esp_sleep.h"
1819
#include "esp_system.h"
20+
#include "esp_pm.h"
1921

2022
#include "soc/efuse_reg.h"
2123

@@ -44,9 +46,51 @@ float common_hal_mcu_processor_get_voltage(void) {
4446
}
4547

4648
uint32_t common_hal_mcu_processor_get_frequency(void) {
47-
return CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * 1000000;
49+
esp_pm_config_t pm;
50+
CHECK_ESP_RESULT(esp_pm_get_configuration(&pm));
51+
return pm.min_freq_mhz * 1000000;
4852
}
4953

54+
#if defined(CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY) // Don't need a NotImplementedError here if this is false, as that is handled in shared-bindings
55+
static void validate_cpu_frequency(uint32_t freq_mhz) {
56+
#if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6)
57+
if (freq_mhz != 20 && freq_mhz != 40 && freq_mhz != 80 && freq_mhz != 160) {
58+
mp_raise_ValueError(MP_ERROR_TEXT("Frequency must be 20, 40, 80 or 160MHz"));
59+
}
60+
#elif defined(CONFIG_IDF_TARGET_ESP32C2)
61+
if (freq_mhz != 20 && freq_mhz != 40 && freq_mhz != 80 && freq_mhz != 120) {
62+
mp_raise_ValueError(MP_ERROR_TEXT("Frequency must be 20, 40, 80 or 120MHz"));
63+
}
64+
#elif defined(CONFIG_IDF_TARGET_ESP32H2)
65+
if (freq_mhz != 32 && freq_mhz != 48 && freq_mhz != 64 && freq_mhz != 96) {
66+
mp_raise_ValueError(MP_ERROR_TEXT("Frequency must be 32, 48, 64 or 96MHz"));
67+
}
68+
#else
69+
if (freq_mhz != 20 && freq_mhz != 40 && freq_mhz != 80 && freq_mhz != 160 && freq_mhz != 240) {
70+
mp_raise_ValueError(MP_ERROR_TEXT("Frequency must be 20, 40, 80, 160 or 240MHz"));
71+
}
72+
#endif
73+
}
74+
75+
void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency) {
76+
// Without this check, everything would compile without error, but silently fail at runtime if
77+
// CONFIG_PM_ENABLE is ever accidentally disabled
78+
#if !defined(CONFIG_PM_ENABLE)
79+
#error "common_hal_mcu_processor_set_frequency needs CONFIG_PM_ENABLE to be defined."
80+
#endif
81+
82+
frequency /= 1000000;
83+
84+
validate_cpu_frequency(frequency);
85+
86+
esp_pm_config_t pm;
87+
pm.max_freq_mhz = frequency;
88+
pm.min_freq_mhz = frequency;
89+
pm.light_sleep_enable = false;
90+
CHECK_ESP_RESULT(esp_pm_configure(&pm));
91+
}
92+
#endif
93+
5094
static uint8_t swap_nibbles(uint8_t v) {
5195
return ((v << 4) | (v >> 4)) & 0xff;
5296
}

ports/espressif/esp-idf-config/sdkconfig.defaults

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ CONFIG_GPTIMER_ISR_IRAM_SAFE=y
3434
CONFIG_ESP_MAIN_TASK_STACK_SIZE=16384
3535
# CONFIG_ESP_TASK_WDT_INIT is not set
3636
# CONFIG_ESP_DEBUG_OCDAWARE is not set
37+
38+
CONFIG_PM_ENABLE=y # required for CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY
3739
# end of ESP System Settings
3840

3941

ports/espressif/mpconfigport.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,5 @@ CIRCUITPY_MESSAGE_COMPRESSION_LEVEL ?= 1
228228

229229
CIRCUITPY_AUDIOMP3 ?= 1
230230
CIRCUITPY_AUDIOMP3_USE_PORT_ALLOCATOR ?= 1
231+
232+
CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY ?= 1

0 commit comments

Comments
 (0)