Hi,
my plan is to "rescue" the system time across a reboot (crash, firmware update, etc.). I know I could rely on NTP, but I'd like to have this feature as a fall back solution anyways.
My understanding is that there is an RTC running and that the RTC memory survives a reboot. In theory. However I noticed (looking at the log time stamps) that after the reboot, the system time is awfully wrong. It jumped several minutes or hours into the future. After the NTP event it is corrected.
Now the question: What causes this behavior and how do I keep the time accurate across a reboot?
System time corrupted across reboots
System time corrupted across reboots
ESP32 / ESP-IDF 5.1.4
Re: System time corrupted across reboots
I think it depends on what kind of reset is performed and whether the clocks were stopped. For instance, if you toggle the EN/CHIP_PU line, that's a power cycle of the chip.
I think the only real way to recover the time locally would be to include an I2C RTC with a battery backup.
I think the only real way to recover the time locally would be to include an I2C RTC with a battery backup.
Re: System time corrupted across reboots
https://docs.espressif.com/projects/esp ... ght=across
In menuconfig I chose the option "RTC and high-resolution timer (default)", so time should persist. But it doesn't. I wonder if others have more experience. This is my second week with the ESP32.
In menuconfig I chose the option "RTC and high-resolution timer (default)", so time should persist. But it doesn't. I wonder if others have more experience. This is my second week with the ESP32.
ESP32 / ESP-IDF 5.1.4
Re: System time corrupted across reboots
I tried calling esp_sync_counters_rtc_and_frc() just before esp_restart(), but that didn't help.
ESP32 / ESP-IDF 5.1.4
Re: System time corrupted across reboots
I found the very old thread
https://www.esp32.com/viewtopic.php?t=7544
indicating the same problem.
There's the recommendation to change CONFIG_NEWLIB_TIME_SYSCALL to only rtc as the Timer for gettimeofday. But that does not help either.
I wonder why the behavior differs from the documentation here. Is it a bug? Or is time simply not defined before NTP? Then why not initialize it to zero like after poweron?
Maybe Espressif can clear this up.
https://www.esp32.com/viewtopic.php?t=7544
indicating the same problem.
There's the recommendation to change CONFIG_NEWLIB_TIME_SYSCALL to only rtc as the Timer for gettimeofday. But that does not help either.
I wonder why the behavior differs from the documentation here. Is it a bug? Or is time simply not defined before NTP? Then why not initialize it to zero like after poweron?
Maybe Espressif can clear this up.
ESP32 / ESP-IDF 5.1.4
Re: System time corrupted across reboots
Hi cruvus,
I tried but couldn't reproduce the problem based on your description. Could you please open an issue in ESP-IDF Github project, attaching the code to reproduce this behavior?
I tried but couldn't reproduce the problem based on your description. Could you please open an issue in ESP-IDF Github project, attaching the code to reproduce this behavior?
Re: System time corrupted across reboots
I am willing to provide a minimum code example, but I didn't have success in doing so yet...
It might be another mechanism in my application which causes this, but I have no idea which one, despite commenting out almost everything one by one.
I am redirecting the log into a (noinit) buffer which I access via the http server. This is how I can easily see what the system time was just before and after esp_restart(). Restart/OTA is also done using the http server.
A side effect of this is that I do not need any serial connection and the board rarely is hardware-reset. At the moment esp_rtc_get_time_us() is ~3 days.
I get the impression the phenomenon gets worse the more time passes since poweron (== hardware reset). I also noticed the value of esp_rtc_get_time_us() jumping. Whether it gets changed just after esp_restart() or just before the user code runs I don't know.
It might be another mechanism in my application which causes this, but I have no idea which one, despite commenting out almost everything one by one.
I am redirecting the log into a (noinit) buffer which I access via the http server. This is how I can easily see what the system time was just before and after esp_restart(). Restart/OTA is also done using the http server.
A side effect of this is that I do not need any serial connection and the board rarely is hardware-reset. At the moment esp_rtc_get_time_us() is ~3 days.
I get the impression the phenomenon gets worse the more time passes since poweron (== hardware reset). I also noticed the value of esp_rtc_get_time_us() jumping. Whether it gets changed just after esp_restart() or just before the user code runs I don't know.
ESP32 / ESP-IDF 5.1.4
Re: System time corrupted across reboots
That is another topic about the same problem:
viewtopic.php?t=19504
It seems that on "freshly powered on" (<1 day) devices the issue is indeed not very obvious.
Edit: I'll try to whip up a very simple example which automatically reboots once per hour or so. The main problem is, my serial connection is not stable enough to leave this running for like 3 days. Remember that in order to demonstrate the issue, a hardware reboot must not happen during the test.
viewtopic.php?t=19504
It seems that on "freshly powered on" (<1 day) devices the issue is indeed not very obvious.
Edit: I'll try to whip up a very simple example which automatically reboots once per hour or so. The main problem is, my serial connection is not stable enough to leave this running for like 3 days. Remember that in order to demonstrate the issue, a hardware reboot must not happen during the test.
ESP32 / ESP-IDF 5.1.4
Re: System time corrupted across reboots
Example code. Run it and leave it for 1-3 days. It will (soft-)restart automatically every hour. Use PuTTY or a similar terminal and do not hardware reset the chip during the test. The problem will only manifest after several hours or days. Typical output see below.
- #include "esp_log.h"
- #include "esp_sntp.h"
- #include "esp_wifi.h"
- #include "nvs_flash.h"
- #include "esp32/rtc.h"
- static const char *TAG = "main";
- void time_sync_notification_cb(struct timeval *tv) {
- ESP_LOGI(TAG, "time_sync_notification_cb");
- }
- void log_rtc(void) {
- int u = esp_rtc_get_time_us() / 1000000;
- ESP_LOGI(TAG, "rtc=%d seconds (%d days %02d:%02d:%02d)", u, u / (24*3600), (u % (24*3600)) / 3600, (u % 3600) / 60, u % 60);
- }
- void app_main(void) {
- log_rtc();
- ESP_LOGI(TAG, "start");
- ESP_ERROR_CHECK(nvs_flash_init());
- ESP_ERROR_CHECK(esp_netif_init());
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- esp_netif_create_default_wifi_sta();
- wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
- wifi_config_t conf_sta = {
- .sta.ssid = "xxxxx",
- };
- esp_wifi_set_config(WIFI_IF_STA, &conf_sta);
- ESP_ERROR_CHECK(esp_wifi_start());
- esp_wifi_connect();
- sntp_setoperatingmode(SNTP_OPMODE_POLL);
- sntp_setservername(0, "pool.ntp.org");
- sntp_set_time_sync_notification_cb(time_sync_notification_cb);
- sntp_init();
- // Wait an hour
- for (int i = 0; i < 60; i++) {
- ESP_LOGI(TAG, "%d", i);
- vTaskDelay(60000 / portTICK_PERIOD_MS);
- }
- ESP_LOGI(TAG, "end");
- log_rtc();
- esp_restart();
- // esp_abort(); // TODO: Try the other "restart methods" and see if there is a difference
- // esp_deep_sleep(1000000);
- }
Here you can see the log just before and after a reboot. Look at the time stamp. Restarting takes maybe ~1 second, but instead time and rtc jumped backwards more than half a minute.I (08:07:52.302) main: 58
I (08:08:52.302) main: 59
I (08:09:52.302) main: end
I (08:09:52.303) main: rtc=32399 seconds (0 days 08:59:59)
[...]
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
[...]
I (08:09:15.296) main: rtc=32362 seconds (0 days 08:59:22)
I (08:09:15.301) main: start
ESP32 / ESP-IDF 5.1.4
-
- Posts: 1
- Joined: Thu Oct 27, 2022 6:18 pm
Re: System time corrupted across reboots
Hi there! Any updates on that? I'm facing the same problem with NTC, after some resets it jumps to the future...
Who is online
Users browsing this forum: jialin.yang and 125 guests