]> granicus.if.org Git - esp-idf/commitdiff
esp32: Fix not the right time spent in a deep sleep
authorKonstantin Kondrashov <konstantin@espressif.com>
Mon, 4 Jun 2018 07:39:18 +0000 (12:39 +0500)
committerKonstantin Kondrashov <konstantin@espressif.com>
Wed, 6 Jun 2018 13:59:30 +0000 (18:59 +0500)
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

.gitlab-ci.yml
components/esp32/sleep_modes.c
components/esp32/test/test_sleep.c
components/newlib/platform_include/esp_newlib.h
components/newlib/time.c
tools/unit-test-app/components/unity/include/unity_config.h

index 17feb07fbe3abc17645ea7aca109ea88cd53fe2a..62a06ea4470d86a5796f06c810d2440e15a764a6 100644 (file)
@@ -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:
index 65d0f056833511ad69ae2243a6df5c80e9c6d492..9cad8cde3e4cdd5731caf2f2fe3a6f1359686671 100644 (file)
@@ -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);
index 201c2312e359097df26b7ff35b2a31e63b685279..fafff847182effe5358bcf4c34fe83c6bd8a77f7 100644 (file)
@@ -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);
index 192844393b05557c6340b53316ea18475bcd625d..31adf7d84737cca575e9ded0d98695765d51c7b3 100644 (file)
@@ -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__
index 37b78f3de58f972795b87b04e72cfe8fb385b7f4..c710162553eb93ea0332f23a15ea59b55e7023f2 100644 (file)
@@ -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
+}
index 875bd0fc8b511bfed873821f34b0e55e5e5d1123..bb672ccc959eba51fbf08559473a6bc3ac7ebcc7 100644 (file)
@@ -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;
 };