|
10 | 10 |
|
11 | 11 | #include "py/runtime.h"
|
12 | 12 |
|
| 13 | +#include "bindings/espidf/__init__.h" |
13 | 14 | #include "common-hal/microcontroller/Processor.h"
|
14 | 15 | #include "shared-bindings/microcontroller/Processor.h"
|
15 | 16 | #include "shared-bindings/microcontroller/ResetReason.h"
|
16 | 17 |
|
17 | 18 | #include "esp_sleep.h"
|
18 | 19 | #include "esp_system.h"
|
| 20 | +#include "esp_pm.h" |
19 | 21 |
|
20 | 22 | #include "soc/efuse_reg.h"
|
21 | 23 |
|
@@ -44,9 +46,51 @@ float common_hal_mcu_processor_get_voltage(void) {
|
44 | 46 | }
|
45 | 47 |
|
46 | 48 | 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; |
48 | 52 | }
|
49 | 53 |
|
| 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 | + |
50 | 94 | static uint8_t swap_nibbles(uint8_t v) {
|
51 | 95 | return ((v << 4) | (v >> 4)) & 0xff;
|
52 | 96 | }
|
|
0 commit comments