Просмотр исходного кода

esp32: Fix not the right time spent in a deep sleep

Before entering the deep sleep, the RTC and FRC counters are synchronized. Updating the boot_time.
Added a unit test for this case.
Fixed warnings for MULTIPLE_STAGES

Closes https://github.com/espressif/esp-idf/issues/1840
Konstantin Kondrashov 7 лет назад
Родитель
Сommit
7e2a3da643

+ 25 - 0
.gitlab-ci.yml

@@ -809,6 +809,24 @@ UT_001_24:
     - ESP32_IDF
     - UT_T1_1
 
+UT_001_25:
+  <<: *unit_test_template
+  tags:
+    - ESP32_IDF
+    - UT_T1_1
+    
+UT_001_26:
+  <<: *unit_test_template
+  tags:
+    - ESP32_IDF
+    - UT_T1_1
+
+UT_001_27:
+  <<: *unit_test_template
+  tags:
+    - ESP32_IDF
+    - UT_T1_1
+
 UT_002_01:
   <<: *unit_test_template
   tags:
@@ -901,6 +919,13 @@ UT_004_08:
     - UT_T1_1
     - psram
 
+UT_004_09:
+  <<: *unit_test_template
+  tags:
+    - ESP32_IDF
+    - UT_T1_1
+    - psram
+    
 UT_005_01:
   <<: *unit_test_template
   tags:

+ 1 - 1
components/esp32/sleep_modes.c

@@ -203,7 +203,7 @@ void IRAM_ATTR esp_deep_sleep_start()
 {
     // record current RTC time
     s_config.rtc_ticks_at_sleep_start = rtc_time_get();
-
+    esp_sync_counters_rtc_and_frc();
     // Configure wake stub
     if (esp_get_deep_sleep_wake_stub() == NULL) {
         esp_set_deep_sleep_wake_stub(esp_wake_deep_sleep);

+ 34 - 0
components/esp32/test/test_sleep.c

@@ -14,6 +14,8 @@
 #include "soc/rtc.h"            // for wakeup trigger defines
 #include "soc/rtc_cntl_reg.h"   // for read rtc registers directly (cause)
 #include "soc/soc.h"            // for direct register read macros
+#include "rom/rtc.h"
+#include "esp_newlib.h"
 
 #define ESP_EXT0_WAKEUP_LEVEL_LOW 0
 #define ESP_EXT0_WAKEUP_LEVEL_HIGH 1
@@ -338,3 +340,35 @@ TEST_CASE("disable source trigger behavior", "[deepsleep]")
     TEST_ASSERT(err_code == ESP_ERR_INVALID_STATE);
 }
 
+static RTC_DATA_ATTR struct timeval start;
+static void trigger_deepsleep(void)
+{
+    printf("Trigger deep sleep. Waiting 30 sec ...\n");
+
+    // Simulate the dispersion of the calibration coefficients at start-up.
+    // Corrupt the calibration factor.
+    esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() - 1000000);
+    esp_set_time_from_rtc();
+
+    // Delay for time error accumulation.
+    vTaskDelay(30000/portTICK_RATE_MS);
+
+    // Save start time. Deep sleep.
+    gettimeofday(&start, NULL);
+    esp_sleep_enable_timer_wakeup(1000);
+    esp_deep_sleep_start();
+}
+
+static void check_time_deepsleep(void)
+{
+    struct timeval stop;
+    RESET_REASON reason = rtc_get_reset_reason(0);
+    TEST_ASSERT(reason == DEEPSLEEP_RESET);
+    gettimeofday(&stop, NULL);
+    // Time dt_ms must in any case be positive.
+    int dt_ms = (stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_usec - start.tv_usec) / 1000;
+    printf("delta time = %d \n", dt_ms);
+    TEST_ASSERT_MESSAGE(dt_ms > 0, "Time in deep sleep is negative");
+}
+
+TEST_CASE_MULTIPLE_STAGES("check a time after wakeup from deep sleep", "[deepsleep][reset=DEEPSLEEP_RESET]", trigger_deepsleep, check_time_deepsleep);

+ 5 - 0
components/newlib/platform_include/esp_newlib.h

@@ -38,4 +38,9 @@ void esp_setup_syscall_table();
  */
 void esp_set_time_from_rtc();
 
+/*
+ * Sync counters RTC and FRC. Update boot_time.
+ */
+void esp_sync_counters_rtc_and_frc();
+
 #endif //__ESP_NEWLIB_H__

+ 8 - 0
components/newlib/time.c

@@ -371,3 +371,11 @@ uint64_t system_get_rtc_time(void)
 #endif
 }
 
+void esp_sync_counters_rtc_and_frc()
+{
+#if defined( WITH_FRC ) && defined( WITH_RTC )
+    adjtime_corr_stop();
+    int64_t s_microseconds_offset_cur = get_rtc_time_us() - esp_timer_get_time();
+    set_boot_time(get_adjusted_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur));
+#endif
+}

+ 2 - 2
tools/unit-test-app/components/unity/include/unity_config.h

@@ -49,7 +49,7 @@
 
 #define UNITY_TEST_FN_SET(...)  \
     static test_func UNITY_TEST_UID(test_functions)[] = {__VA_ARGS__}; \
-    static char* UNITY_TEST_UID(test_fn_name)[] = FN_NAME_SET(PP_NARG(__VA_ARGS__), __VA_ARGS__)
+    static const char* UNITY_TEST_UID(test_fn_name)[] = FN_NAME_SET(PP_NARG(__VA_ARGS__), __VA_ARGS__)
 
 
 typedef void (* test_func)(void);
@@ -62,7 +62,7 @@ struct test_desc_t
     const char* file;
     int line;
     uint8_t test_fn_count;
-    char ** test_fn_name;
+    const char ** test_fn_name;
     struct test_desc_t* next;
 };