|
|
@@ -37,6 +37,8 @@ static const unsigned short rtc_ydays[2][13] = {
|
|
|
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
|
|
|
};
|
|
|
|
|
|
+static u32 rtc_residual;
|
|
|
+
|
|
|
#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
|
|
|
|
|
|
static int RTC_WaitNotBusy(void)
|
|
|
@@ -450,13 +452,20 @@ uint32_t rtc_tm_to_time(struct rtc_time *tm)
|
|
|
static void rtc_update_time(unsigned int time)
|
|
|
{
|
|
|
int timeout = 100000;
|
|
|
+ u32 cnth;
|
|
|
+
|
|
|
+ if (RTC->RTCSTA & RTC_STA_OSC32K_SEL) {
|
|
|
+ cnth = time;
|
|
|
+ } else {
|
|
|
+ cnth = (u64)time * 46875 / 65536;
|
|
|
+ rtc_residual = ((u64)time * 46875) & (65536 - 1);
|
|
|
+ }
|
|
|
|
|
|
- RTC_WriteReg(RTC_CNT, &RTC->CNT_L, 0);
|
|
|
- RTC_WriteReg(RTC_CNT, &RTC->CNT_H, time);
|
|
|
+ RTC_WriteReg(RTC_CNT, &RTC->CNT_H, cnth);
|
|
|
|
|
|
//wait rtc_busy;
|
|
|
RTC_WaitNotBusy();
|
|
|
- while (RTC->CNT_H != time) {
|
|
|
+ while(RTC->CNT_H != cnth) {
|
|
|
if (timeout-- == 0)
|
|
|
break;
|
|
|
taskYIELD();
|
|
|
@@ -476,9 +485,10 @@ static int rtc_read_time(struct rtc_time *tm)
|
|
|
|
|
|
/* we don't report wday/yday/isdst ... */
|
|
|
RTC_WaitNotBusy();
|
|
|
-
|
|
|
- time = RTC->CNT_H;
|
|
|
-
|
|
|
+ if (RTC->RTCSTA & RTC_STA_OSC32K_SEL)
|
|
|
+ time = RTC->CNT_H;
|
|
|
+ else
|
|
|
+ time = ((u64)RTC->CNT_H * 65536 + rtc_residual) / 46875;
|
|
|
rtc_time_to_tm(time, tm);
|
|
|
|
|
|
return 0;
|
|
|
@@ -520,6 +530,11 @@ int rtc_time_init(void)
|
|
|
|
|
|
int iGetLocalTime(SystemTime_t *tm)
|
|
|
{
|
|
|
+ if (tm == NULL) {
|
|
|
+ printf("%s invalid tm.\n", __func__);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
rtc_read_time(tm);
|
|
|
tm->tm_year += 1900;
|
|
|
tm->tm_mon += 1;
|
|
|
@@ -527,18 +542,28 @@ int iGetLocalTime(SystemTime_t *tm)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-void vSetLocalTime(SystemTime_t *tm)
|
|
|
+int iSetLocalTime(SystemTime_t *tm)
|
|
|
{
|
|
|
+ if (tm == NULL) {
|
|
|
+ printf("%s invalid tm.\n", __func__);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
tm->tm_year -= 1900;
|
|
|
tm->tm_mon -= 1;
|
|
|
rtc_set_time(tm);
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
uint32_t CpuGetLocalTime(void)
|
|
|
{
|
|
|
RTC_WaitNotBusy();
|
|
|
|
|
|
- return RTC->CNT_H;
|
|
|
+ if (RTC->RTCSTA & RTC_STA_OSC32K_SEL)
|
|
|
+ return RTC->CNT_H;
|
|
|
+ else
|
|
|
+ return ((u64)RTC->CNT_H * 65536 + rtc_residual) / 46875;
|
|
|
}
|
|
|
|
|
|
void CpuSetLocalTime(uint32_t time)
|