Page 1 of 1

ESP32S2 store rtc time when reboot

Posted: Sat May 01, 2021 8:12 am
by huybk213
Dear everyone,

I have a question about ESP32 RTC clock.
As Espressif documents said "RTC timer: Allows keeping the system time during any resets and sleep mode" in documents
https://docs.espressif.com/projects/esp ... _time.html

Every time device reset (software reset), i readback rtc time, but it's seem rtc time not stored when device soft reset.

Could you please give me your suggestion, how to keep rtc timer when device reboot?

Thanks.

My code to read timer every device software reset.

Code: Select all

	struct timeval current_time;
	gettimeofday(&current_time, NULL);
	ESP_LOGW(TAG, "seconds : %ld\nmicro seconds : %ld",
    		current_time.tv_sec, current_time.tv_usec);

Re: ESP32S2 store rtc time when reboot

Posted: Tue May 04, 2021 12:44 am
by rfleming
Starting with the "examples/get-started/hellow_world" I added in

Code: Select all

        
        #include <time.h>
	#include <sys/time.h>
	...
        struct timespec tp;
	clock_gettime(CLOCK_REALTIME, &tp);
	int hours = tp.tv_sec / 3600 % 24;
	int minutes = tp.tv_sec / 60 % 60;
	int seconds = tp.tv_sec % 60;
And after each restart, the time value keeps increasing just fine. A hard restart will obviously reset this value.