diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 7b9d88180f..d6419a47d9 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -40,7 +40,7 @@ #include "soc/rtc.h" #include "soc/rtc_cntl_reg.h" #include "soc/soc_memory_layout.h" -#include "esp_clk.h" +#include "esp32c3/clk.h" #include "esp_coexist_internal.h" #include "esp32c3/rom/rom_layout.h" #include "esp_timer.h" diff --git a/components/bt/controller/esp32s3/bt.c b/components/bt/controller/esp32s3/bt.c index 71a0aacba8..9d066f21df 100644 --- a/components/bt/controller/esp32s3/bt.c +++ b/components/bt/controller/esp32s3/bt.c @@ -41,7 +41,7 @@ #include "soc/rtc.h" #include "soc/rtc_cntl_reg.h" #include "soc/soc_memory_layout.h" -#include "esp_clk.h" +#include "esp32c3/clk.h" #include "esp_coexist_internal.h" #if CONFIG_BT_ENABLED diff --git a/components/esp32/CMakeLists.txt b/components/esp32/CMakeLists.txt index 4c11e7cef7..f5e1fe2cb4 100644 --- a/components/esp32/CMakeLists.txt +++ b/components/esp32/CMakeLists.txt @@ -13,7 +13,6 @@ else() # Regular app build set(srcs "cache_sram_mmu.c" - "clk.c" "dport_access.c" "esp_himem.c" "spiram.c" diff --git a/components/esp32/clk.c b/components/esp32/clk.c deleted file mode 100644 index b0c9caa530..0000000000 --- a/components/esp32/clk.c +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2015-2017 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 - -#include "esp_attr.h" -#include "soc/rtc.h" -#include "esp32/clk.h" - -#define MHZ (1000000) - -// g_ticks_us defined in ROMs for PRO and APP CPU -extern uint32_t g_ticks_per_us_pro; -#ifndef CONFIG_FREERTOS_UNICORE -extern uint32_t g_ticks_per_us_app; -#endif - -int IRAM_ATTR esp_clk_cpu_freq(void) -{ - return g_ticks_per_us_pro * MHZ; -} - -int IRAM_ATTR esp_clk_apb_freq(void) -{ - return MIN(g_ticks_per_us_pro, 80) * MHZ; -} - -int IRAM_ATTR esp_clk_xtal_freq(void) -{ - return rtc_clk_xtal_freq_get() * MHZ; -} - -void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us) -{ - /* Update scale factors used by esp_rom_delay_us */ - g_ticks_per_us_pro = ticks_per_us; -#ifndef CONFIG_FREERTOS_UNICORE - g_ticks_per_us_app = ticks_per_us; -#endif -} diff --git a/components/esp32c3/CMakeLists.txt b/components/esp32c3/CMakeLists.txt index 3ae3e3d880..fb12a45134 100644 --- a/components/esp32c3/CMakeLists.txt +++ b/components/esp32c3/CMakeLists.txt @@ -11,8 +11,7 @@ if(BOOTLOADER_BUILD) else() # Regular app build - set(srcs "clk.c" - "dport_access.c" + set(srcs "dport_access.c" "esp_hmac.c" "esp_ds.c" "esp_crypto_lock.c" diff --git a/components/esp32c3/include/esp32c3/clk.h b/components/esp32c3/include/esp32c3/clk.h deleted file mode 100644 index 1d4e462bcc..0000000000 --- a/components/esp32c3/include/esp32c3/clk.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2015-2020 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. - -#pragma once -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @file esp_clk.h - * - * This file contains declarations of clock related functions. - */ - -/** - * @brief Get the calibration value of RTC slow clock - * - * The value is in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * - * @return the calibration value obtained using rtc_clk_cal, at startup time - */ -uint32_t esp_clk_slowclk_cal_get(void); - -/** - * @brief Update the calibration value of RTC slow clock - * - * The value has to be in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * This value is used by timekeeping functions (such as gettimeofday) to - * calculate current time based on RTC counter value. - * @param value calibration value obtained using rtc_clk_cal - */ -void esp_clk_slowclk_cal_set(uint32_t value); - -/** - * @brief Return current CPU clock frequency - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return CPU clock frequency, in Hz - */ -int esp_clk_cpu_freq(void); - -/** - * @brief Return current APB clock frequency - * - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return APB clock frequency, in Hz - */ -int esp_clk_apb_freq(void); - - -/** - * @brief Read value of RTC counter, converting it to microseconds - * @attention The value returned by this function may change abruptly when - * calibration value of RTC counter is updated via esp_clk_slowclk_cal_set - * function. This should not happen unless application calls esp_clk_slowclk_cal_set. - * In ESP-IDF, esp_clk_slowclk_cal_set is only called in startup code. - * - * @return Value or RTC counter, expressed in microseconds - */ -uint64_t esp_clk_rtc_time(void); - -#ifdef __cplusplus -} -#endif diff --git a/components/esp32c3/include/esp_clk.h b/components/esp32c3/include/esp_clk.h deleted file mode 100644 index 78f6678aac..0000000000 --- a/components/esp32c3/include/esp_clk.h +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2015-2020 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. - -#pragma once - -#include - -/** - * @file esp_clk.h - * - * This file contains declarations of clock related functions. - */ - -/** - * @brief Get the calibration value of RTC slow clock - * - * The value is in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * - * @return the calibration value obtained using rtc_clk_cal, at startup time - */ -uint32_t esp_clk_slowclk_cal_get(void); - -/** - * @brief Update the calibration value of RTC slow clock - * - * The value has to be in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * This value is used by timekeeping functions (such as gettimeofday) to - * calculate current time based on RTC counter value. - * @param value calibration value obtained using rtc_clk_cal - */ -void esp_clk_slowclk_cal_set(uint32_t value); - -/** - * @brief Return current CPU clock frequency - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return CPU clock frequency, in Hz - */ -int esp_clk_cpu_freq(void); - -/** - * @brief Return current APB clock frequency - * - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return APB clock frequency, in Hz - */ -int esp_clk_apb_freq(void); - - -/** - * @brief Read value of RTC counter, converting it to microseconds - * @attention The value returned by this function may change abruptly when - * calibration value of RTC counter is updated via esp_clk_slowclk_cal_set - * function. This should not happen unless application calls esp_clk_slowclk_cal_set. - * In ESP-IDF, esp_clk_slowclk_cal_set is only called in startup code. - * - * @return Value or RTC counter, expressed in microseconds - */ -uint64_t esp_clk_rtc_time(void); diff --git a/components/esp32s2/CMakeLists.txt b/components/esp32s2/CMakeLists.txt index 4a0c47dfda..6cf09ac6f5 100644 --- a/components/esp32s2/CMakeLists.txt +++ b/components/esp32s2/CMakeLists.txt @@ -12,7 +12,6 @@ else() # Regular app build set(srcs "memprot.c" - "clk.c" "dport_access.c" "spiram.c" "spiram_psram.c" diff --git a/components/esp32s2/include/esp32s2/clk.h b/components/esp32s2/include/esp32s2/clk.h deleted file mode 100644 index 8a0952fadc..0000000000 --- a/components/esp32s2/include/esp32s2/clk.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2015-2017 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. - -#pragma once -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @file esp_clk.h - * - * This file contains declarations of clock related functions. - */ - -/** - * @brief Get the calibration value of RTC slow clock - * - * The value is in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * - * @return the calibration value obtained using rtc_clk_cal, at startup time - */ -uint32_t esp_clk_slowclk_cal_get(void); - -/** - * @brief Update the calibration value of RTC slow clock - * - * The value has to be in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * This value is used by timekeeping functions (such as gettimeofday) to - * calculate current time based on RTC counter value. - * @param value calibration value obtained using rtc_clk_cal - */ -void esp_clk_slowclk_cal_set(uint32_t value); - -/** - * @brief Return current CPU clock frequency - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return CPU clock frequency, in Hz - */ -int esp_clk_cpu_freq(void); - -/** - * @brief Return current APB clock frequency - * - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return APB clock frequency, in Hz - */ -int esp_clk_apb_freq(void); - - -/** - * @brief Read value of RTC counter, converting it to microseconds - * @attention The value returned by this function may change abruptly when - * calibration value of RTC counter is updated via esp_clk_slowclk_cal_set - * function. This should not happen unless application calls esp_clk_slowclk_cal_set. - * In ESP-IDF, esp_clk_slowclk_cal_set is only called in startup code. - * - * @return Value or RTC counter, expressed in microseconds - */ -uint64_t esp_clk_rtc_time(void); - -#ifdef __cplusplus -} -#endif diff --git a/components/esp32s2/include/esp_clk.h b/components/esp32s2/include/esp_clk.h deleted file mode 100644 index 430d1c1b7a..0000000000 --- a/components/esp32s2/include/esp_clk.h +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2015-2017 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. - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @file esp_clk.h - * - * This file contains declarations of clock related functions. - */ - -/** - * @brief Get the calibration value of RTC slow clock - * - * The value is in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * - * @return the calibration value obtained using rtc_clk_cal, at startup time - */ -uint32_t esp_clk_slowclk_cal_get(void); - -/** - * @brief Update the calibration value of RTC slow clock - * - * The value has to be in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * This value is used by timekeeping functions (such as gettimeofday) to - * calculate current time based on RTC counter value. - * @param value calibration value obtained using rtc_clk_cal - */ -void esp_clk_slowclk_cal_set(uint32_t value); - -/** - * @brief Return current CPU clock frequency - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return CPU clock frequency, in Hz - */ -int esp_clk_cpu_freq(void); - -/** - * @brief Return current APB clock frequency - * - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return APB clock frequency, in Hz - */ -int esp_clk_apb_freq(void); - - -/** - * @brief Read value of RTC counter, converting it to microseconds - * @attention The value returned by this function may change abruptly when - * calibration value of RTC counter is updated via esp_clk_slowclk_cal_set - * function. This should not happen unless application calls esp_clk_slowclk_cal_set. - * In ESP-IDF, esp_clk_slowclk_cal_set is only called in startup code. - * - * @return Value or RTC counter, expressed in microseconds - */ -uint64_t esp_clk_rtc_time(void); - -#ifdef __cplusplus -} -#endif diff --git a/components/esp32s3/CMakeLists.txt b/components/esp32s3/CMakeLists.txt index 8bb1c515ef..dde455bbf9 100644 --- a/components/esp32s3/CMakeLists.txt +++ b/components/esp32s3/CMakeLists.txt @@ -12,8 +12,7 @@ if(BOOTLOADER_BUILD) else() # Regular app build - set(srcs "clk.c" - "dport_access.c" + set(srcs "dport_access.c" "esp_crypto_lock.c" "memprot.c" "spiram.c" diff --git a/components/esp32s3/include/esp32s3/clk.h b/components/esp32s3/include/esp32s3/clk.h deleted file mode 100644 index 1d4e462bcc..0000000000 --- a/components/esp32s3/include/esp32s3/clk.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2015-2020 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. - -#pragma once -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @file esp_clk.h - * - * This file contains declarations of clock related functions. - */ - -/** - * @brief Get the calibration value of RTC slow clock - * - * The value is in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * - * @return the calibration value obtained using rtc_clk_cal, at startup time - */ -uint32_t esp_clk_slowclk_cal_get(void); - -/** - * @brief Update the calibration value of RTC slow clock - * - * The value has to be in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * This value is used by timekeeping functions (such as gettimeofday) to - * calculate current time based on RTC counter value. - * @param value calibration value obtained using rtc_clk_cal - */ -void esp_clk_slowclk_cal_set(uint32_t value); - -/** - * @brief Return current CPU clock frequency - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return CPU clock frequency, in Hz - */ -int esp_clk_cpu_freq(void); - -/** - * @brief Return current APB clock frequency - * - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return APB clock frequency, in Hz - */ -int esp_clk_apb_freq(void); - - -/** - * @brief Read value of RTC counter, converting it to microseconds - * @attention The value returned by this function may change abruptly when - * calibration value of RTC counter is updated via esp_clk_slowclk_cal_set - * function. This should not happen unless application calls esp_clk_slowclk_cal_set. - * In ESP-IDF, esp_clk_slowclk_cal_set is only called in startup code. - * - * @return Value or RTC counter, expressed in microseconds - */ -uint64_t esp_clk_rtc_time(void); - -#ifdef __cplusplus -} -#endif diff --git a/components/esp32s3/include/esp_clk.h b/components/esp32s3/include/esp_clk.h deleted file mode 100644 index 78f6678aac..0000000000 --- a/components/esp32s3/include/esp_clk.h +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2015-2020 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. - -#pragma once - -#include - -/** - * @file esp_clk.h - * - * This file contains declarations of clock related functions. - */ - -/** - * @brief Get the calibration value of RTC slow clock - * - * The value is in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * - * @return the calibration value obtained using rtc_clk_cal, at startup time - */ -uint32_t esp_clk_slowclk_cal_get(void); - -/** - * @brief Update the calibration value of RTC slow clock - * - * The value has to be in the same format as returned by rtc_clk_cal (microseconds, - * in Q13.19 fixed-point format). - * This value is used by timekeeping functions (such as gettimeofday) to - * calculate current time based on RTC counter value. - * @param value calibration value obtained using rtc_clk_cal - */ -void esp_clk_slowclk_cal_set(uint32_t value); - -/** - * @brief Return current CPU clock frequency - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return CPU clock frequency, in Hz - */ -int esp_clk_cpu_freq(void); - -/** - * @brief Return current APB clock frequency - * - * When frequency switching is performed, this frequency may change. - * However it is guaranteed that the frequency never changes with a critical - * section. - * - * @return APB clock frequency, in Hz - */ -int esp_clk_apb_freq(void); - - -/** - * @brief Read value of RTC counter, converting it to microseconds - * @attention The value returned by this function may change abruptly when - * calibration value of RTC counter is updated via esp_clk_slowclk_cal_set - * function. This should not happen unless application calls esp_clk_slowclk_cal_set. - * In ESP-IDF, esp_clk_slowclk_cal_set is only called in startup code. - * - * @return Value or RTC counter, expressed in microseconds - */ -uint64_t esp_clk_rtc_time(void); diff --git a/components/esp_hw_support/CMakeLists.txt b/components/esp_hw_support/CMakeLists.txt index 6ef6036c6d..29a65a9303 100644 --- a/components/esp_hw_support/CMakeLists.txt +++ b/components/esp_hw_support/CMakeLists.txt @@ -7,11 +7,11 @@ endif() set(srcs "compare_set.c" "cpu_util.c") if(NOT BOOTLOADER_BUILD) - list(APPEND srcs "clk_ctrl_os.c" "mac_addr.c" "hw_random.c") + list(APPEND srcs "esp_clk.c" "clk_ctrl_os.c" "mac_addr.c" "hw_random.c") endif() idf_component_register(SRCS ${srcs} - INCLUDE_DIRS include + INCLUDE_DIRS include include/soc REQUIRES ${requires} PRIV_REQUIRES efuse LDFRAGMENTS linker.lf) diff --git a/components/esp_hw_support/component.mk b/components/esp_hw_support/component.mk index 07566e52b8..b86f339418 100644 --- a/components/esp_hw_support/component.mk +++ b/components/esp_hw_support/component.mk @@ -1,5 +1,5 @@ COMPONENT_SRCDIRS := . port/$(IDF_TARGET) -COMPONENT_ADD_INCLUDEDIRS := . include port/$(IDF_TARGET)/private_include +COMPONENT_ADD_INCLUDEDIRS := . include include/soc port/$(IDF_TARGET)/private_include COMPONENT_ADD_LDFRAGMENTS := linker.lf port/$(IDF_TARGET)/rtc_clk.o: CFLAGS += -fno-jump-tables -fno-tree-switch-conversion diff --git a/components/esp_hw_support/esp_clk.c b/components/esp_hw_support/esp_clk.c new file mode 100644 index 0000000000..4342c95d05 --- /dev/null +++ b/components/esp_hw_support/esp_clk.c @@ -0,0 +1,139 @@ +// Copyright 2015-2017 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 + +#include "esp_attr.h" +#include "soc/rtc.h" + +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/rtc.h" +#include "esp32/clk.h" +#include "esp32/rtc.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/rtc.h" +#include "esp32s2/clk.h" +#include "esp32s2/rtc.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/rtc.h" +#include "esp32s3/clk.h" +#include "esp32s3/rtc.h" +#include "esp32s3/rom/ets_sys.h" +#elif CONFIG_IDF_TARGET_ESP32C3 +#include "esp32c3/rom/rtc.h" +#include "esp32c3/clk.h" +#include "esp32c3/rtc.h" +#endif + +#define MHZ (1000000) + +// g_ticks_us defined in ROMs for PRO and APP CPU +extern uint32_t g_ticks_per_us_pro; +#if CONFIG_IDF_TARGET_ESP32 +#ifndef CONFIG_FREERTOS_UNICORE +extern uint32_t g_ticks_per_us_app; +#endif +#endif + +static _lock_t s_esp_rtc_time_lock; +static RTC_DATA_ATTR uint64_t s_esp_rtc_time_us = 0, s_rtc_last_ticks = 0; + +int IRAM_ATTR esp_clk_cpu_freq(void) +{ +#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 + return ets_get_cpu_frequency() * MHZ; +#else + return g_ticks_per_us_pro * MHZ; +#endif +} + +int IRAM_ATTR esp_clk_apb_freq(void) +{ +#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 + return MIN(ets_get_cpu_frequency(), 81) * MHZ; +#else + return MIN(g_ticks_per_us_pro, 80) * MHZ; +#endif +} + +int IRAM_ATTR esp_clk_xtal_freq(void) +{ + return rtc_clk_xtal_freq_get() * MHZ; +} + +#ifndef CONFIG_IDF_TARGET_ESP32C3 +void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us) +{ + /* Update scale factors used by esp_rom_delay_us */ + g_ticks_per_us_pro = ticks_per_us; +#if CONFIG_IDF_TARGET_ESP32 +#ifndef CONFIG_FREERTOS_UNICORE + g_ticks_per_us_app = ticks_per_us; +#endif +#endif +} +#endif + +uint64_t esp_rtc_get_time_us(void) +{ + _lock_acquire(&s_esp_rtc_time_lock); + const uint32_t cal = esp_clk_slowclk_cal_get(); + const uint64_t rtc_this_ticks = rtc_time_get(); + const uint64_t ticks = rtc_this_ticks - s_rtc_last_ticks; + /* RTC counter result is up to 2^48, calibration factor is up to 2^24, + * for a 32kHz clock. We need to calculate (assuming no overflow): + * (ticks * cal) >> RTC_CLK_CAL_FRACT + * + * An overflow in the (ticks * cal) multiplication would cause time to + * wrap around after approximately 13 days, which is probably not enough + * for some applications. + * Therefore multiplication is split into two terms, for the lower 32-bit + * and the upper 16-bit parts of "ticks", i.e.: + * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT + */ + const uint64_t ticks_low = ticks & UINT32_MAX; + const uint64_t ticks_high = ticks >> 32; + const uint64_t delta_time_us = ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) + + ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT)); + s_esp_rtc_time_us += delta_time_us; + s_rtc_last_ticks = rtc_this_ticks; + _lock_release(&s_esp_rtc_time_lock); + return s_esp_rtc_time_us; +} + +void esp_clk_slowclk_cal_set(uint32_t new_cal) +{ +#if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) + /* To force monotonic time values even when clock calibration value changes, + * we adjust esp_rtc_time + */ + esp_rtc_get_time_us(); +#endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER + REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal); +} + +uint32_t esp_clk_slowclk_cal_get(void) +{ + return REG_READ(RTC_SLOW_CLK_CAL_REG); +} + +uint64_t esp_clk_rtc_time(void) +{ +#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER + return esp_rtc_get_time_us(); +#else + return 0; +#endif +} diff --git a/components/esp32/include/esp_clk.h b/components/esp_hw_support/include/esp_clk.h similarity index 100% rename from components/esp32/include/esp_clk.h rename to components/esp_hw_support/include/esp_clk.h diff --git a/components/esp32/include/esp32/clk.h b/components/esp_hw_support/include/esp_private/esp_clk.h similarity index 99% rename from components/esp32/include/esp32/clk.h rename to components/esp_hw_support/include/esp_private/esp_clk.h index dde169a16e..14326898bd 100644 --- a/components/esp32/include/esp32/clk.h +++ b/components/esp_hw_support/include/esp_private/esp_clk.h @@ -20,7 +20,7 @@ extern "C" { #endif /** - * @file esp32/clk.h + * @file esp_clk.h * * This file contains declarations of clock related functions. */ diff --git a/components/esp32s3/clk.c b/components/esp_hw_support/include/soc/esp32/clk.h similarity index 53% rename from components/esp32s3/clk.c rename to components/esp_hw_support/include/soc/esp32/clk.h index 08b3c5d6e8..31902ebad0 100644 --- a/components/esp32s3/clk.c +++ b/components/esp_hw_support/include/soc/esp32/clk.h @@ -1,4 +1,4 @@ -// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2017 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. @@ -12,27 +12,5 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include - -#include "esp_attr.h" -#include "esp32s3/clk.h" -#include "esp32s3/rom/ets_sys.h" -#include "soc/rtc.h" - -#define MHZ (1000000) - -int IRAM_ATTR esp_clk_cpu_freq(void) -{ - return ets_get_cpu_frequency() * MHZ; -} - -int IRAM_ATTR esp_clk_apb_freq(void) -{ - return MIN(ets_get_cpu_frequency(), 80) * MHZ; -} - -int IRAM_ATTR esp_clk_xtal_freq(void) -{ - return rtc_clk_xtal_freq_get() * MHZ; -} +#pragma once +#include "esp_private/esp_clk.h" diff --git a/components/esp32c3/clk.c b/components/esp_hw_support/include/soc/esp32c3/clk.h similarity index 53% rename from components/esp32c3/clk.c rename to components/esp_hw_support/include/soc/esp32c3/clk.h index 978331a28b..31902ebad0 100644 --- a/components/esp32c3/clk.c +++ b/components/esp_hw_support/include/soc/esp32c3/clk.h @@ -1,4 +1,4 @@ -// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2017 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. @@ -12,27 +12,5 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include - -#include "esp_attr.h" -#include "soc/rtc.h" -#include "esp32c3/clk.h" -#include "esp32c3/rom/ets_sys.h" - -#define MHZ (1000000) - -int IRAM_ATTR esp_clk_cpu_freq(void) -{ - return ets_get_cpu_frequency() * MHZ; -} - -int IRAM_ATTR esp_clk_apb_freq(void) -{ - return MIN(80, ets_get_cpu_frequency()) * MHZ; -} - -int IRAM_ATTR esp_clk_xtal_freq(void) -{ - return rtc_clk_xtal_freq_get() * MHZ; -} +#pragma once +#include "esp_private/esp_clk.h" diff --git a/components/esp32s2/clk.c b/components/esp_hw_support/include/soc/esp32s2/clk.h similarity index 50% rename from components/esp32s2/clk.c rename to components/esp_hw_support/include/soc/esp32s2/clk.h index 59a61ed585..31902ebad0 100644 --- a/components/esp32s2/clk.c +++ b/components/esp_hw_support/include/soc/esp32s2/clk.h @@ -12,35 +12,5 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include - -#include "esp_attr.h" -#include "soc/rtc.h" -#include "esp32s2/clk.h" - -#define MHZ (1000000) - -// g_ticks_us defined in ROMs -extern uint32_t g_ticks_per_us_pro; - -int IRAM_ATTR esp_clk_cpu_freq(void) -{ - return g_ticks_per_us_pro * MHZ; -} - -int IRAM_ATTR esp_clk_apb_freq(void) -{ - return MIN(g_ticks_per_us_pro, 80) * MHZ; -} - -int IRAM_ATTR esp_clk_xtal_freq(void) -{ - return rtc_clk_xtal_freq_get() * MHZ; -} - -void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us) -{ - /* Update scale factors used by esp_rom_delay_us */ - g_ticks_per_us_pro = ticks_per_us; -} +#pragma once +#include "esp_private/esp_clk.h" diff --git a/components/esp_hw_support/include/soc/esp32s3/clk.h b/components/esp_hw_support/include/soc/esp32s3/clk.h new file mode 100644 index 0000000000..31902ebad0 --- /dev/null +++ b/components/esp_hw_support/include/soc/esp32s3/clk.h @@ -0,0 +1,16 @@ +// Copyright 2015-2017 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. + +#pragma once +#include "esp_private/esp_clk.h" diff --git a/components/esp_pm/linker.lf b/components/esp_pm/linker.lf index f1fe2f4baa..2d9ea8d3ed 100644 --- a/components/esp_pm/linker.lf +++ b/components/esp_pm/linker.lf @@ -12,6 +12,9 @@ archive: libesp_hw_support.a entries: if PM_SLP_IRAM_OPT = y: rtc_init:rtc_vddsdio_get_config (noflash) + esp_clk:esp_clk_slowclk_cal_set (noflash) + esp_clk:esp_clk_slowclk_cal_get (noflash) + esp_clk:esp_rtc_get_time_us (noflash) [mapping:esp_system_pm] archive: libesp_system.a @@ -47,9 +50,6 @@ entries: if PM_SLP_IRAM_OPT = y: esp_time_impl:esp_time_impl_set_boot_time (noflash) esp_time_impl:esp_time_impl_get_boot_time (noflash) - esp_time_impl:esp_clk_slowclk_cal_get (noflash) - esp_time_impl:esp_rtc_get_time_us (noflash) - esp_time_impl:esp_clk_slowclk_cal_set (noflash) esp_time_impl:esp_set_time_from_rtc (noflash) [mapping:driver_pm] diff --git a/components/fatfs/test_fatfs_host/Makefile.files b/components/fatfs/test_fatfs_host/Makefile.files index 94c2d3c9fc..2c16bd8ddb 100644 --- a/components/fatfs/test_fatfs_host/Makefile.files +++ b/components/fatfs/test_fatfs_host/Makefile.files @@ -25,6 +25,7 @@ INCLUDE_DIRS := \ $(addprefix ../../../components/, \ esp_rom/include \ esp_hw_support/include \ + esp_hw_support/include/soc \ esp_system/include \ xtensa/include \ xtensa/esp32/include \ diff --git a/components/newlib/port/esp_time_impl.c b/components/newlib/port/esp_time_impl.c index 5317156b63..db8bc529d7 100644 --- a/components/newlib/port/esp_time_impl.c +++ b/components/newlib/port/esp_time_impl.c @@ -60,9 +60,6 @@ static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE static _lock_t s_boot_time_lock; -static _lock_t s_esp_rtc_time_lock; -static RTC_DATA_ATTR uint64_t s_esp_rtc_time_us = 0, s_rtc_last_ticks = 0; - #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER ) uint64_t esp_time_impl_get_time_since_boot(void) { @@ -106,15 +103,6 @@ void esp_time_impl_set_boot_time(uint64_t time_us) _lock_release(&s_boot_time_lock); } -uint64_t esp_clk_rtc_time(void) -{ -#ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER - return esp_rtc_get_time_us(); -#else - return 0; -#endif -} - uint64_t esp_time_impl_get_boot_time(void) { uint64_t result; @@ -128,49 +116,6 @@ uint64_t esp_time_impl_get_boot_time(void) return result; } -uint32_t esp_clk_slowclk_cal_get(void) -{ - return REG_READ(RTC_SLOW_CLK_CAL_REG); -} - -uint64_t esp_rtc_get_time_us(void) -{ - _lock_acquire(&s_esp_rtc_time_lock); - const uint32_t cal = esp_clk_slowclk_cal_get(); - const uint64_t rtc_this_ticks = rtc_time_get(); - const uint64_t ticks = rtc_this_ticks - s_rtc_last_ticks; - /* RTC counter result is up to 2^48, calibration factor is up to 2^24, - * for a 32kHz clock. We need to calculate (assuming no overflow): - * (ticks * cal) >> RTC_CLK_CAL_FRACT - * - * An overflow in the (ticks * cal) multiplication would cause time to - * wrap around after approximately 13 days, which is probably not enough - * for some applications. - * Therefore multiplication is split into two terms, for the lower 32-bit - * and the upper 16-bit parts of "ticks", i.e.: - * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT - */ - const uint64_t ticks_low = ticks & UINT32_MAX; - const uint64_t ticks_high = ticks >> 32; - const uint64_t delta_time_us = ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) + - ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT)); - s_esp_rtc_time_us += delta_time_us; - s_rtc_last_ticks = rtc_this_ticks; - _lock_release(&s_esp_rtc_time_lock); - return s_esp_rtc_time_us; -} - -void esp_clk_slowclk_cal_set(uint32_t new_cal) -{ -#if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) - /* To force monotonic time values even when clock calibration value changes, - * we adjust esp_rtc_time - */ - esp_rtc_get_time_us(); -#endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER - REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal); -} - void esp_set_time_from_rtc(void) { #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER ) diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index efafd28463..f7b6f1b9bd 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -40,6 +40,7 @@ #include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/cache.h" #include "esp32s3/clk.h" +#include "esp32s3/clk.h" #elif CONFIG_IDF_TARGET_ESP32C3 #include "esp32c3/rom/cache.h" #include "esp32c3/rom/spi_flash.h" diff --git a/components/spi_flash/sim/Makefile.files b/components/spi_flash/sim/Makefile.files index 55ce3e7157..57cefe0ebe 100644 --- a/components/spi_flash/sim/Makefile.files +++ b/components/spi_flash/sim/Makefile.files @@ -27,6 +27,7 @@ INCLUDE_DIRS := \ esp_rom/include \ esp_common/include \ esp_hw_support/include \ + esp_hw_support/include/soc \ esp_system/include \ xtensa/include \ xtensa/esp32/include \ diff --git a/components/spi_flash/sim/stubs/Makefile.files b/components/spi_flash/sim/stubs/Makefile.files index e18ac1e8c9..dfa0f2a64b 100644 --- a/components/spi_flash/sim/stubs/Makefile.files +++ b/components/spi_flash/sim/stubs/Makefile.files @@ -21,6 +21,7 @@ INCLUDE_DIRS := \ $(addprefix ../../../../components/, \ esp_common/include \ esp_hw_support/include \ + esp_hw_support/include/soc \ esp_system/include \ soc/esp32/include \ soc/include \ diff --git a/components/spiffs/test_spiffs_host/Makefile.files b/components/spiffs/test_spiffs_host/Makefile.files index c79130bdcc..822b5243b1 100644 --- a/components/spiffs/test_spiffs_host/Makefile.files +++ b/components/spiffs/test_spiffs_host/Makefile.files @@ -26,6 +26,7 @@ INCLUDE_DIRS := \ esp_rom/include \ esp_common/include \ esp_hw_support/include \ + esp_hw_support/include/soc \ esp_system/include \ xtensa/include \ xtensa/esp32/include \ diff --git a/components/wear_levelling/test_wl_host/Makefile.files b/components/wear_levelling/test_wl_host/Makefile.files index 2f503dbf3b..1cd58a8cc2 100644 --- a/components/wear_levelling/test_wl_host/Makefile.files +++ b/components/wear_levelling/test_wl_host/Makefile.files @@ -26,6 +26,7 @@ INCLUDE_DIRS := \ esp_system/include \ esp_common/include \ esp_hw_support/include \ + esp_hw_support/include/soc \ xtensa/include \ xtensa/esp32/include \ soc/esp32/include \