]> granicus.if.org Git - esp-idf/commitdiff
esp_clk.h: make public, add getters for RTC time, CPU/APB freq
authorIvan Grokhotkov <ivan@espressif.com>
Fri, 22 Sep 2017 15:04:16 +0000 (23:04 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Wed, 18 Oct 2017 06:19:14 +0000 (14:19 +0800)
components/esp32/clk.c
components/esp32/cpu_start.c
components/esp32/esp_clk_internal.h [new file with mode: 0644]
components/esp32/include/esp_clk.h
components/newlib/time.c

index 319b31fbcd829ab7fa5e44436f95463a2e7eca58..6e01cc2ea1dcfb03f085c53a8d9aaac9b9eb4c28 100644 (file)
 #include <stdint.h>
 #include <sys/cdefs.h>
 #include <sys/time.h>
+#include <sys/param.h>
 #include "sdkconfig.h"
 #include "esp_attr.h"
 #include "esp_log.h"
 #include "esp_clk.h"
+#include "esp_clk_internal.h"
 #include "rom/ets_sys.h"
 #include "rom/uart.h"
 #include "rom/rtc.h"
 
 static void select_rtc_slow_clk(rtc_slow_freq_t slow_clk);
 
+// g_ticks_us defined in ROMs for PRO and APP CPU
+extern uint32_t g_ticks_per_us_pro;
+extern uint32_t g_ticks_per_us_app;
+
 static const char* TAG = "clk";
-/*
- * 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_clk_init(void)
 {
     rtc_config_t cfg = RTC_CONFIG_DEFAULT();
@@ -90,10 +91,19 @@ void esp_clk_init(void)
     XTHAL_SET_CCOUNT( XTHAL_GET_CCOUNT() * freq_after / freq_before );
 }
 
+int IRAM_ATTR esp_clk_cpu_freq(void)
+{
+    return g_ticks_per_us_pro * 1000000;
+}
+
+int IRAM_ATTR esp_clk_apb_freq(void)
+{
+    return MIN(g_ticks_per_us_pro, 80) * 1000000;
+}
+
 void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us)
 {
-    extern uint32_t g_ticks_per_us_pro;  // g_ticks_us defined in ROM for PRO CPU
-    extern uint32_t g_ticks_per_us_app;  // same defined for APP CPU
+    /* Update scale factors used by ets_delay_us */
     g_ticks_per_us_pro = ticks_per_us;
     g_ticks_per_us_app = ticks_per_us;
 }
index 3aefdce0a421166e447915b7973f62401a92ba4d..ccf8a1d4bc553f62dc40c2e4ae7c4112efa650b7 100644 (file)
@@ -64,7 +64,7 @@
 #include "esp_app_trace.h"
 #include "esp_efuse.h"
 #include "esp_spiram.h"
-#include "esp_clk.h"
+#include "esp_clk_internal.h"
 #include "esp_timer.h"
 #include "trax.h"
 
diff --git a/components/esp32/esp_clk_internal.h b/components/esp32/esp_clk_internal.h
new file mode 100644 (file)
index 0000000..d1fd434
--- /dev/null
@@ -0,0 +1,39 @@
+// 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
+
+/**
+ * @file esp_clk_internal.h
+ *
+ * Private clock-related functions
+ */
+
+/**
+ * @brief Initialize clock-related settings
+ *
+ * Called from cpu_start.c, not intended to be called from other places.
+ * This function configures the CPU clock, RTC slow and fast clocks, and
+ * performs RTC slow clock calibration.
+ */
+void esp_clk_init(void);
+
+
+/**
+ * @brief Disables clock of some peripherals
+ *
+ * Called from cpu_start.c, not intended to be called from other places.
+ * This function disables clock of useless peripherals when cpu starts.
+ */
+void esp_perip_clk_init(void);
index 5c6f5cb8710a00cf59af1b655ebbf5e8709bfcfd..6526aa92724fbdc98001dfab8739b9fa3b9a51a0 100644 (file)
  * @file esp_clk.h
  *
  * This file contains declarations of clock related functions.
- * These functions are used in ESP-IDF components, but should not be considered
- * to be part of public API.
  */
 
-/**
- * @brief Initialize clock-related settings
- *
- * Called from cpu_start.c, not intended to be called from other places.
- * This function configures the CPU clock, RTC slow and fast clocks, and
- * performs RTC slow clock calibration.
- */
-void esp_clk_init(void);
-
-
 /**
  * @brief Get the calibration value of RTC slow clock
  *
@@ -42,7 +30,6 @@ void esp_clk_init(void);
  */
 uint32_t esp_clk_slowclk_cal_get();
 
-
 /**
  * @brief Update the calibration value of RTC slow clock
  *
@@ -55,10 +42,34 @@ uint32_t esp_clk_slowclk_cal_get();
 void esp_clk_slowclk_cal_set(uint32_t value);
 
 /**
- * @brief Disables clock of some peripherals
+ * @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.
  *
- * Called from cpu_start.c, not intended to be called from other places.
- * This function disables clock of useless peripherals when cpu starts.
+ * @return CPU clock frequency, in Hz
  */
-void esp_perip_clk_init(void);
+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();
index 0357f6412eefe56670d07a4a108ad12482283429..edb9a59fe0e0f4664b27560ecb054695e0348325 100644 (file)
@@ -145,6 +145,15 @@ void esp_set_time_from_rtc()
 #endif // WITH_FRC1 && WITH_RTC
 }
 
+uint64_t esp_clk_rtc_time(void)
+{
+#ifdef WITH_RTC
+    return get_rtc_time_us();
+#else
+    return 0;
+#endif
+}
+
 clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
 {
     clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000);