Internal RTC Drifting -8 Minutes in 15 Hours.
Posted: Tue May 21, 2019 6:53 am
Hi all,
I'm using this below mentioned code :
int rtc_set_time(rtc_time_date_t ip)
{
time_t now;
struct tm t;
struct timeval val = {0,0};
if(ip.year<=2019&& ip.year>1970)
t.tm_year=ip.year-1900;
else
return 0;
if(ip.min<60 && ip.min>=0)
t.tm_min=ip.min;
else
return 0;
if(ip.mon>0 && ip.mon<13)
t.tm_mon=ip.mon-1;
else
return 0;
if(ip.hour>=0 && ip.hour<=23)
t.tm_hour=ip.hour;
else
return 0;
if(ip.sec>=0 && ip.sec<=60)
t.tm_sec=ip.sec;
else
return 0;
if(ip.mday>0 && ip.mday<=31)
t.tm_mday=ip.mday;
else
return 0;
t.tm_isdst=0;
now=mktime(&t);
val.tv_sec=now;
settimeofday(&val,NULL);
return 1;
}
void rtc_get_time(rtc_time_date_t *ip)
{
struct timeval val = {0,0};
time_t now;
struct tm t;
gettimeofday(&val,NULL);
now=val.tv_sec;
time(&now);
localtime_r(&now,&t);
(*ip).year=t.tm_year+1900;
(*ip).mon=t.tm_mon+1;
(*ip).mday=t.tm_mday;
(*ip).hour=t.tm_hour;
(*ip).min=t.tm_min;
(*ip).sec=t.tm_sec;
}
main()
{
rtc_time_date_t ip;
int check;
struct timeval val;
ip.year=(unsigned int)2019;
ip.mon=(unsigned int)5;
ip.mday=(unsigned int)20;
ip.hour=(unsigned int)17;
ip.min=(unsigned int)52;
ip.sec=(unsigned int)30;
check=rtc_set_time(ip);
if(check!=1)
{
printf("Set_time Failed \n");
return 0;
}
else
printf("Set_time Success\n");
vTaskDelay(50/portTICK_PERIOD_MS);
while(1)
{
rtc_get_time(&ip);
ESP_LOGI(TAG1,"\nYear:%d Month:%d Day:%d\nHour:%d,Min:%d,Sec:%d\n",
ip.year,ip.mon,ip.mday,ip.hour,ip.min,ip.sec);
ESP_LOGI(TAG1,"RTC Time %llu\n",rtc_time_get());
// printf("RTC Time %llu\n",rtc_time_slowclk_to_us(1,10));
vTaskDelay(5000/portTICK_PERIOD_MS);
}
}
where "rtc_time_date_t" is a structure variable that converts raw data into data required by "struct tm"
My CPU Frequency is 160 Mhz.
RTC clock source is internal 150 KHz clock.
1024 Number of cycles for RTC_SLOW_CLK calibration.
Timers used for gettimeofday function (RTC).
Main Xtal Frequency is autodetect.
My dirft after 15 hrs is -8 Minutes .
I dont want my device to sleep and fetch data . I want it to readily fetch the data.
Thanks,
Dhananjay Sutariya
I'm using this below mentioned code :
int rtc_set_time(rtc_time_date_t ip)
{
time_t now;
struct tm t;
struct timeval val = {0,0};
if(ip.year<=2019&& ip.year>1970)
t.tm_year=ip.year-1900;
else
return 0;
if(ip.min<60 && ip.min>=0)
t.tm_min=ip.min;
else
return 0;
if(ip.mon>0 && ip.mon<13)
t.tm_mon=ip.mon-1;
else
return 0;
if(ip.hour>=0 && ip.hour<=23)
t.tm_hour=ip.hour;
else
return 0;
if(ip.sec>=0 && ip.sec<=60)
t.tm_sec=ip.sec;
else
return 0;
if(ip.mday>0 && ip.mday<=31)
t.tm_mday=ip.mday;
else
return 0;
t.tm_isdst=0;
now=mktime(&t);
val.tv_sec=now;
settimeofday(&val,NULL);
return 1;
}
void rtc_get_time(rtc_time_date_t *ip)
{
struct timeval val = {0,0};
time_t now;
struct tm t;
gettimeofday(&val,NULL);
now=val.tv_sec;
time(&now);
localtime_r(&now,&t);
(*ip).year=t.tm_year+1900;
(*ip).mon=t.tm_mon+1;
(*ip).mday=t.tm_mday;
(*ip).hour=t.tm_hour;
(*ip).min=t.tm_min;
(*ip).sec=t.tm_sec;
}
main()
{
rtc_time_date_t ip;
int check;
struct timeval val;
ip.year=(unsigned int)2019;
ip.mon=(unsigned int)5;
ip.mday=(unsigned int)20;
ip.hour=(unsigned int)17;
ip.min=(unsigned int)52;
ip.sec=(unsigned int)30;
check=rtc_set_time(ip);
if(check!=1)
{
printf("Set_time Failed \n");
return 0;
}
else
printf("Set_time Success\n");
vTaskDelay(50/portTICK_PERIOD_MS);
while(1)
{
rtc_get_time(&ip);
ESP_LOGI(TAG1,"\nYear:%d Month:%d Day:%d\nHour:%d,Min:%d,Sec:%d\n",
ip.year,ip.mon,ip.mday,ip.hour,ip.min,ip.sec);
ESP_LOGI(TAG1,"RTC Time %llu\n",rtc_time_get());
// printf("RTC Time %llu\n",rtc_time_slowclk_to_us(1,10));
vTaskDelay(5000/portTICK_PERIOD_MS);
}
}
where "rtc_time_date_t" is a structure variable that converts raw data into data required by "struct tm"
My CPU Frequency is 160 Mhz.
RTC clock source is internal 150 KHz clock.
1024 Number of cycles for RTC_SLOW_CLK calibration.
Timers used for gettimeofday function (RTC).
Main Xtal Frequency is autodetect.
My dirft after 15 hrs is -8 Minutes .
I dont want my device to sleep and fetch data . I want it to readily fetch the data.
Thanks,
Dhananjay Sutariya