diff --git a/components/app_trace/app_trace.c b/components/app_trace/app_trace.c index c46f71b5a..1e792dd86 100644 --- a/components/app_trace/app_trace.c +++ b/components/app_trace/app_trace.c @@ -203,8 +203,6 @@ const static char *TAG = "esp_apptrace"; #define ESP_APPTRACE_LOGV( format, ... ) ESP_APPTRACE_LOG_LEV(V, ESP_LOG_VERBOSE, format, ##__VA_ARGS__) #define ESP_APPTRACE_LOGO( format, ... ) ESP_APPTRACE_LOG_LEV(E, ESP_LOG_NONE, format, ##__VA_ARGS__) -#define ESP_APPTRACE_CPUTICKS2US(_t_) ((_t_)/(XT_CLOCK_FREQ/1000000)) - // TODO: move these (and same definitions in trax.c to dport_reg.h) #define TRACEMEM_MUX_PROBLK0_APPBLK1 0 #define TRACEMEM_MUX_BLK0_ONLY 1 diff --git a/components/app_trace/app_trace_util.c b/components/app_trace/app_trace_util.c index 2af3cd4d9..da0a8d58a 100644 --- a/components/app_trace/app_trace_util.c +++ b/components/app_trace/app_trace_util.c @@ -15,23 +15,24 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_app_trace_util.h" +#include "esp_clk.h" /////////////////////////////////////////////////////////////////////////////// ///////////////////////////////// TIMEOUT ///////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// -// TODO: get actual clock from PLL config -#define ESP_APPTRACE_CPUTICKS2US(_t_) ((_t_)/(XT_CLOCK_FREQ/1000000)) -#define ESP_APPTRACE_US2CPUTICKS(_t_) ((_t_)*(XT_CLOCK_FREQ/1000000)) +#define ESP_APPTRACE_CPUTICKS2US(_t_, _cpu_freq_) ((_t_)/(_cpu_freq_/1000000)) +#define ESP_APPTRACE_US2CPUTICKS(_t_, _cpu_freq_) ((_t_)*(_cpu_freq_/1000000)) esp_err_t esp_apptrace_tmo_check(esp_apptrace_tmo_t *tmo) { + int cpu_freq = esp_clk_cpu_freq(); if (tmo->tmo != ESP_APPTRACE_TMO_INFINITE) { unsigned cur = portGET_RUN_TIME_COUNTER_VALUE(); if (tmo->start <= cur) { - tmo->elapsed = ESP_APPTRACE_CPUTICKS2US(cur - tmo->start); + tmo->elapsed = ESP_APPTRACE_CPUTICKS2US(cur - tmo->start, cpu_freq); } else { - tmo->elapsed = ESP_APPTRACE_CPUTICKS2US(0xFFFFFFFF - tmo->start + cur); + tmo->elapsed = ESP_APPTRACE_CPUTICKS2US(0xFFFFFFFF - tmo->start + cur, cpu_freq); } if (tmo->elapsed >= tmo->tmo) { return ESP_ERR_TIMEOUT; diff --git a/components/app_trace/test/test_trace.c b/components/app_trace/test/test_trace.c index e92b6ffc6..6846ea50a 100644 --- a/components/app_trace/test/test_trace.c +++ b/components/app_trace/test/test_trace.c @@ -87,8 +87,6 @@ static void esp_apptrace_test_timer_init(int timer_group, int timer_idx, uint32_ #define ESP_APPTRACE_TEST_WRITE_FROM_ISR(_b_, _s_) esp_apptrace_write(ESP_APPTRACE_DEST_TRAX, _b_, _s_, 0UL) #define ESP_APPTRACE_TEST_WRITE_NOWAIT(_b_, _s_) esp_apptrace_write(ESP_APPTRACE_DEST_TRAX, _b_, _s_, 0) -#define ESP_APPTRACE_TEST_CPUTICKS2US(_t_) ((_t_)/(XT_CLOCK_FREQ/1000000)) - typedef struct { uint8_t *buf; uint32_t buf_sz; diff --git a/components/esp32/core_dump.c b/components/esp32/core_dump.c index 12f63a442..56ce08b64 100644 --- a/components/esp32/core_dump.c +++ b/components/esp32/core_dump.c @@ -23,6 +23,7 @@ #include "esp_panic.h" #include "esp_partition.h" +#include "esp_clk.h" #if CONFIG_ESP32_ENABLE_COREDUMP #define LOG_LOCAL_LEVEL CONFIG_ESP32_CORE_DUMP_LOG_LEVEL @@ -522,10 +523,11 @@ void esp_core_dump_to_uart(XtExcFrame *frame) PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_U0TXD); ESP_COREDUMP_LOGI("Press Enter to print core dump to UART..."); - tm_end = xthal_get_ccount() / (XT_CLOCK_FREQ / 1000) + CONFIG_ESP32_CORE_DUMP_UART_DELAY; + const int cpu_ticks_per_ms = esp_clk_cpu_freq() / 1000; + tm_end = xthal_get_ccount() / cpu_ticks_per_ms + CONFIG_ESP32_CORE_DUMP_UART_DELAY; ch = esp_core_dump_uart_get_char(); while (!(ch == '\n' || ch == '\r')) { - tm_cur = xthal_get_ccount() / (XT_CLOCK_FREQ / 1000); + tm_cur = xthal_get_ccount() / cpu_ticks_per_ms; if (tm_cur >= tm_end) break; ch = esp_core_dump_uart_get_char(); diff --git a/components/esp32/hw_random.c b/components/esp32/hw_random.c index 4f63ecd98..78ad542fc 100644 --- a/components/esp32/hw_random.c +++ b/components/esp32/hw_random.c @@ -17,6 +17,7 @@ #include #include #include "esp_attr.h" +#include "esp_clk.h" #include "soc/wdev_reg.h" #include "freertos/FreeRTOSConfig.h" #include "xtensa/core-macros.h" @@ -35,13 +36,21 @@ uint32_t IRAM_ATTR esp_random(void) * WDEV_RND_REG reads while waiting. */ + /* This code does not run in a critical section, so CPU frequency switch may + * happens while this code runs (this will not happen in the current + * implementation, but possible in the future). However if that happens, + * the number of cycles spent on frequency switching will certainly be more + * than the number of cycles we need to wait here. + */ + uint32_t cpu_to_apb_freq_ratio = esp_clk_cpu_freq() / esp_clk_apb_freq(); + static uint32_t last_ccount = 0; uint32_t ccount; uint32_t result = 0; do { ccount = XTHAL_GET_CCOUNT(); result ^= REG_READ(WDEV_RND_REG); - } while (ccount - last_ccount < XT_CLOCK_FREQ / APB_CLK_FREQ * 16); + } while (ccount - last_ccount < cpu_to_apb_freq_ratio * 16); last_ccount = ccount; return result ^ REG_READ(WDEV_RND_REG); } diff --git a/components/freertos/include/freertos/FreeRTOSConfig.h b/components/freertos/include/freertos/FreeRTOSConfig.h index 3ccd3fda6..f830a4520 100644 --- a/components/freertos/include/freertos/FreeRTOSConfig.h +++ b/components/freertos/include/freertos/FreeRTOSConfig.h @@ -95,8 +95,20 @@ #define configNUM_THREAD_LOCAL_STORAGE_POINTERS CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS #define configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS 1 -/* TODO: config freq by menuconfig */ -#define XT_CLOCK_FREQ (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * 1000000) +#ifndef __ASSEMBLER__ + +/** + * This function is defined to provide a deprecation warning whenever + * XT_CLOCK_FREQ macro is used. + * Update the code to use esp_clk_cpu_freq function instead. + * @return current CPU clock frequency, in Hz + */ +int xt_clock_freq(void) __attribute__((deprecated)); + +#define XT_CLOCK_FREQ (xt_clock_freq()) + +#endif // __ASSEMBLER__ + /* Required for configuration-dependent settings */ #include "xtensa_config.h" diff --git a/components/freertos/xtensa_init.c b/components/freertos/xtensa_init.c index ded8df109..624f242b1 100644 --- a/components/freertos/xtensa_init.c +++ b/components/freertos/xtensa_init.c @@ -34,31 +34,21 @@ that are implemented in C. #endif #include "xtensa_rtos.h" +#include "esp_clk.h" #ifdef XT_RTOS_TIMER_INT unsigned _xt_tick_divisor = 0; /* cached number of cycles per tick */ -/* -Compute and initialize at run-time the tick divisor (the number of -processor clock cycles in an RTOS tick, used to set the tick timer). -Called when the processor clock frequency is not known at compile-time. -*/ void _xt_tick_divisor_init(void) { -#ifdef XT_CLOCK_FREQ + _xt_tick_divisor = esp_clk_cpu_freq() / XT_TICK_PER_SEC; +} - _xt_tick_divisor = (XT_CLOCK_FREQ / XT_TICK_PER_SEC); - -#else - - #ifdef XT_BOARD - _xt_tick_divisor = xtbsp_clock_freq_hz() / XT_TICK_PER_SEC; - #else - #error "No way to obtain processor clock frequency" - #endif /* XT_BOARD */ - -#endif /* XT_CLOCK_FREQ */ +/* Deprecated, to be removed */ +int xt_clock_freq(void) +{ + return esp_clk_cpu_freq(); } #endif /* XT_RTOS_TIMER_INT */ diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index afb8f407b..2302bf1d4 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -49,7 +49,7 @@ static spi_flash_counters_t s_flash_stats; #define COUNTER_STOP(counter) \ do{ \ s_flash_stats.counter.count++; \ - s_flash_stats.counter.time += (xthal_get_ccount() - ts_begin) / (XT_CLOCK_FREQ / 1000000); \ + s_flash_stats.counter.time += (xthal_get_ccount() - ts_begin) / (esp_clk_cpu_freq() / 1000000); \ } while(0) #define COUNTER_ADD_BYTES(counter, size) \ diff --git a/tools/unit-test-app/components/unity/unity_platform.c b/tools/unit-test-app/components/unity/unity_platform.c index 8e73fd525..2eeb66048 100644 --- a/tools/unit-test-app/components/unity/unity_platform.c +++ b/tools/unit-test-app/components/unity/unity_platform.c @@ -8,6 +8,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" +#include "esp_clk.h" #include "soc/cpu.h" #include "esp_heap_caps.h" #include "test_utils.h" @@ -150,7 +151,7 @@ static void unity_run_single_test_by_index_parse(const char* filter, int index_m unity_run_single_test_by_index(test_index - 1); uint32_t end; RSR(CCOUNT, end); - uint32_t ms = (end - start) / (XT_CLOCK_FREQ / 1000); + uint32_t ms = (end - start) / (esp_clk_cpu_freq() / 1000); printf("Test ran in %dms\n", ms); } }