|
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,67 @@ float common_hal_mcu_processor_get_voltage(void) {
|
44 | 46 | }
|
45 | 47 |
|
46 | 48 | uint32_t common_hal_mcu_processor_get_frequency(void) {
|
| 49 | + #if CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY |
| 50 | + esp_pm_config_t pm; |
| 51 | + CHECK_ESP_RESULT(esp_pm_get_configuration(&pm)); |
| 52 | + return pm.min_freq_mhz * 1000000; |
| 53 | + #else |
47 | 54 | return CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * 1000000;
|
| 55 | + #endif |
| 56 | +} |
| 57 | + |
| 58 | +#if CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY // Don't need a NotImplementedError here if this is false, as that is handled in shared-bindings |
| 59 | +// If the requested frequency is not supported by the hardware, return the next lower supported frequency |
| 60 | +static uint32_t get_valid_cpu_frequency(uint32_t requested_freq_mhz) { |
| 61 | + |
| 62 | + #if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6) |
| 63 | + uint32_t valid_cpu_frequencies[] = {20, 40, 80, 160}; |
| 64 | + #elif defined(CONFIG_IDF_TARGET_ESP32C2) |
| 65 | + uint32_t valid_cpu_frequencies[] = {20, 40, 80, 120}; |
| 66 | + #elif defined(CONFIG_IDF_TARGET_ESP32H2) |
| 67 | + uint32_t valid_cpu_frequencies[] = {32, 48, 64, 96}; |
| 68 | + #else |
| 69 | + uint32_t valid_cpu_frequencies[] = {20, 40, 80, 160, 240}; |
| 70 | + #endif |
| 71 | + |
| 72 | + if (requested_freq_mhz < valid_cpu_frequencies[0]) { |
| 73 | + // Don't round to the lowest valid frequency automatically here because the lowest valid frequency |
| 74 | + // can break UART/USB connection on some boards and it's very easy to trigger this case accidentally |
| 75 | + // (e.g. accidentally setting the frequency to 16000000 instead of 160000000, |
| 76 | + // or setting the frequency to 160 instead of 160000000). So trigger an exception instead. |
| 77 | + mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid %q"), MP_QSTR_frequency); |
| 78 | + } |
| 79 | + |
| 80 | + const size_t num_valid_frequencies = MP_ARRAY_SIZE(valid_cpu_frequencies); |
| 81 | + |
| 82 | + for (size_t i = 1; i < num_valid_frequencies; i++) { |
| 83 | + if (requested_freq_mhz < valid_cpu_frequencies[i]) { |
| 84 | + return valid_cpu_frequencies[i - 1]; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return valid_cpu_frequencies[num_valid_frequencies - 1]; |
48 | 89 | }
|
49 | 90 |
|
| 91 | +void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency) { |
| 92 | + // Without this check, everything would compile without errors, but silently fail at runtime if |
| 93 | + // CONFIG_PM_ENABLE is ever accidentally disabled |
| 94 | + #if !defined(CONFIG_PM_ENABLE) |
| 95 | + #error "common_hal_mcu_processor_set_frequency needs CONFIG_PM_ENABLE to be defined." |
| 96 | + #endif |
| 97 | + |
| 98 | + frequency /= 1000000; |
| 99 | + |
| 100 | + frequency = get_valid_cpu_frequency(frequency); |
| 101 | + |
| 102 | + esp_pm_config_t pm; |
| 103 | + pm.max_freq_mhz = frequency; |
| 104 | + pm.min_freq_mhz = frequency; |
| 105 | + pm.light_sleep_enable = false; |
| 106 | + CHECK_ESP_RESULT(esp_pm_configure(&pm)); |
| 107 | +} |
| 108 | +#endif |
| 109 | + |
50 | 110 | static uint8_t swap_nibbles(uint8_t v) {
|
51 | 111 | return ((v << 4) | (v >> 4)) & 0xff;
|
52 | 112 | }
|
|
0 commit comments