From 5ab769516d406c30928d2306490bf9b1636a6960 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 13 Sep 2016 18:10:58 +0800 Subject: [PATCH 1/3] components/esp32: add CPU frequency selection in menuconfig Note that with WiFi stack enabled, system_init will reset frequency to 240MHz. To make this setting useful, esp32-wifi-libs submodule needs to be updated. --- components/esp32/Kconfig | 20 ++++++ components/esp32/cpu_freq.c | 68 +++++++++++++++++++ components/esp32/cpu_start.c | 3 + components/esp32/include/soc/cpu.h | 10 +++ .../include/freertos/FreeRTOSConfig.h | 2 +- 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 components/esp32/cpu_freq.c diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index 36c6f7af8e..0ad32756e8 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -1,5 +1,25 @@ menu "ESP32-specific config" +choice ESP32_DEFAULT_CPU_FREQ_MHZ + prompt "CPU frequency" + default ESP32_DEFAULT_CPU_FREQ_240 + help + CPU frequency to be set on application startup. + +config ESP32_DEFAULT_CPU_FREQ_80 + bool "80 MHz" +config ESP32_DEFAULT_CPU_FREQ_160 + bool "160 MHz" +config ESP32_DEFAULT_CPU_FREQ_240 + bool "240 MHz" +endchoice + +config ESP32_DEFAULT_CPU_FREQ_MHZ + int + default 80 if ESP32_DEFAULT_CPU_FREQ_80 + default 160 if ESP32_DEFAULT_CPU_FREQ_160 + default 240 if ESP32_DEFAULT_CPU_FREQ_240 + config WIFI_ENABLED bool "Enable low-level WiFi stack" default "y" diff --git a/components/esp32/cpu_freq.c b/components/esp32/cpu_freq.c new file mode 100644 index 0000000000..c4bca44f0e --- /dev/null +++ b/components/esp32/cpu_freq.c @@ -0,0 +1,68 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include "rom/ets_sys.h" +#include "sdkconfig.h" + +typedef enum{ + XTAL_40M = 40, + XTAL_26M = 26, + XTAL_24M = 24, + XTAL_AUTO = 0 +} xtal_freq_t; + +typedef enum{ + CPU_80M = 1, + CPU_160M = 2, + CPU_240M = 3, +} cpu_freq_t; + +extern void phy_get_romfunc_addr(); + +// TODO: these functions need to be moved from librtc to ESP-IDF +extern void rtc_init_lite(); +extern void rtc_set_cpu_freq(xtal_freq_t xtal_freq, cpu_freq_t cpu_freq); + +/* + * This function is not exposed as an API at this point, + * because FreeRTOS doesn't yet support dynamic changing of + * CPU frequency. Also we need to implement hooks for + * components which want to be notified of CPU frequency + * changes. + */ +void esp_set_cpu_freq() +{ + uint32_t freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ; + phy_get_romfunc_addr(); + rtc_init_lite(); + cpu_freq_t freq = CPU_80M; + switch(freq_mhz) { + case 240: + freq = CPU_240M; + break; + case 160: + freq = CPU_160M; + break; + default: + freq_mhz = 80; + /* no break */ + case 80: + freq = CPU_80M; + break; + } + rtc_set_cpu_freq(XTAL_AUTO, freq); + ets_update_cpu_frequency(freq_mhz); +} + diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index d895e908eb..aebad03d41 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -22,6 +22,7 @@ #include "soc/dport_reg.h" #include "soc/io_mux_reg.h" +#include "soc/cpu.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -201,6 +202,8 @@ static void do_global_ctors(void) { extern esp_err_t app_main(void *ctx); void user_start_cpu0(void) { + esp_set_cpu_freq(); // set CPU frequency configured in menuconfig + uart_div_modify(0, (80000000 << 4) / 115200); ets_setup_syscalls(); do_global_ctors(); esp_ipc_init(); diff --git a/components/esp32/include/soc/cpu.h b/components/esp32/include/soc/cpu.h index fdcf62190e..283c0a2c6a 100644 --- a/components/esp32/include/soc/cpu.h +++ b/components/esp32/include/soc/cpu.h @@ -33,4 +33,14 @@ static inline bool cpu_in_interrupt_context(void) return (ps & PS_UM) == 0; } + +/* + * @brief Set CPU frequency to the value defined in menuconfig + * + * Called from cpu_start.c, not intended to be called from other places. + * This is a temporary function which will be replaced once dynamic + * CPU frequency changing is implemented. + */ +void esp_set_cpu_freq(); + #endif diff --git a/components/freertos/include/freertos/FreeRTOSConfig.h b/components/freertos/include/freertos/FreeRTOSConfig.h index 95bf1e103a..e43b9cfeb3 100644 --- a/components/freertos/include/freertos/FreeRTOSConfig.h +++ b/components/freertos/include/freertos/FreeRTOSConfig.h @@ -100,7 +100,7 @@ #define configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS 1 /* TODO: config freq by menuconfig */ -#define XT_CLOCK_FREQ 80000000 +#define XT_CLOCK_FREQ (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * 1000000) /* Required for configuration-dependent settings */ #include "xtensa_config.h" From 066f3358a7157613e7a3504ff656fc13f3eaea15 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 13 Sep 2016 20:53:25 +0800 Subject: [PATCH 2/3] components/esp32: use APB_CLK_FREQ instead of a number --- components/esp32/cpu_start.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index aebad03d41..de0fdb5863 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -203,7 +203,7 @@ extern esp_err_t app_main(void *ctx); void user_start_cpu0(void) { esp_set_cpu_freq(); // set CPU frequency configured in menuconfig - uart_div_modify(0, (80000000 << 4) / 115200); + uart_div_modify(0, (APB_CLK_FREQ << 4) / 115200); ets_setup_syscalls(); do_global_ctors(); esp_ipc_init(); From b3e671e7258dac81a30769222b4c8fbca0f7aa4a Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Wed, 14 Sep 2016 17:51:51 +0800 Subject: [PATCH 3/3] esp32/lib: update to 3372298f remove freq change in system_init --- components/esp32/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp32/lib b/components/esp32/lib index 1303c92c10..3372298fe2 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit 1303c92c1056b7f59b95360b58e70a21cb4a93e1 +Subproject commit 3372298fe2ff517858bdc8c64f76540b9ac7d7d5