Specification of rtc synchronization functions

This commit is contained in:
kurisaw 2024-12-23 15:23:19 +08:00 committed by Meco Man
parent c9ed784131
commit b23d667ba4

View File

@ -35,7 +35,6 @@
#endif #endif
static struct rt_work rtc_sync_work; static struct rt_work rtc_sync_work;
static rt_device_t source_device = RT_NULL;
static struct rt_device soft_rtc_dev; static struct rt_device soft_rtc_dev;
static rt_tick_t init_tick; static rt_tick_t init_tick;
@ -82,18 +81,6 @@ static void set_rtc_time(time_t t)
#endif #endif
} }
static void _source_device_control(int cmd, void *args)
{
if (source_device == RT_NULL)
return;
if (rt_device_open(source_device, 0) == RT_EOK)
{
rt_device_control(source_device, cmd, args);
rt_device_close(source_device);
}
}
static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
{ {
time_t *t; time_t *t;
@ -114,7 +101,6 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
{ {
t = (time_t *) args; t = (time_t *) args;
set_rtc_time(*t); set_rtc_time(*t);
_source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, t);
break; break;
} }
#ifdef RT_USING_ALARM #ifdef RT_USING_ALARM
@ -143,7 +129,6 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
rt_ktime_boottime_get_us(&_tv); rt_ktime_boottime_get_us(&_tv);
set_rtc_time(tv->tv_sec); set_rtc_time(tv->tv_sec);
init_tv.tv_usec = tv->tv_usec - _tv.tv_usec; init_tv.tv_usec = tv->tv_usec - _tv.tv_usec;
_source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(tv->tv_sec));
break; break;
} }
case RT_DEVICE_CTRL_RTC_GET_TIMESPEC: case RT_DEVICE_CTRL_RTC_GET_TIMESPEC:
@ -162,7 +147,6 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
rt_ktime_boottime_get_ns(&_ts); rt_ktime_boottime_get_ns(&_ts);
set_rtc_time(ts->tv_sec); set_rtc_time(ts->tv_sec);
init_ts.tv_nsec = ts->tv_nsec - _ts.tv_nsec; init_ts.tv_nsec = ts->tv_nsec - _ts.tv_nsec;
_source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(ts->tv_sec));
break; break;
} }
case RT_DEVICE_CTRL_RTC_GET_TIMERES: case RT_DEVICE_CTRL_RTC_GET_TIMERES:
@ -187,7 +171,6 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
rt_tick_t tick = rt_tick_get() - init_tick; rt_tick_t tick = rt_tick_get() - init_tick;
set_rtc_time(tv->tv_sec); set_rtc_time(tv->tv_sec);
init_tv.tv_usec = tv->tv_usec - ((tick % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND)); init_tv.tv_usec = tv->tv_usec - ((tick % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND));
_source_device_control(RT_DEVICE_CTRL_RTC_SET_TIME, &(tv->tv_sec));
break; break;
} }
case RT_DEVICE_CTRL_RTC_GET_TIMERES: case RT_DEVICE_CTRL_RTC_GET_TIMERES:
@ -227,7 +210,7 @@ static int rt_soft_rtc_init(void)
return 0; return 0;
} }
/* make sure only one 'rtc' device */ /* make sure only one 'rtc' device */
#if defined(RT_USING_SOFT_RTC) && defined(RT_USING_RTC) #if defined(RT_USING_SOFT_RTC) && defined(BSP_USING_ONCHIP_RTC)
#warning "Please note: Currently only one RTC device is allowed in the system, and the name is "rtc"." #warning "Please note: Currently only one RTC device is allowed in the system, and the name is "rtc"."
#endif #endif
RT_ASSERT(!rt_device_find("rtc")); RT_ASSERT(!rt_device_find("rtc"));
@ -263,8 +246,6 @@ static int rt_soft_rtc_init(void)
rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR); rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
source_device = &soft_rtc_dev;
init_ok = RT_TRUE; init_ok = RT_TRUE;
return 0; return 0;
@ -277,13 +258,7 @@ rt_err_t rt_soft_rtc_sync(void)
{ {
time_t time = 0; time_t time = 0;
if (source_device == RT_NULL) rt_device_control(&soft_rtc_dev, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
{
rt_kprintf("error: rtc source not found, please set it!!!\n");
return RT_ENOSYS;
}
_source_device_control(RT_DEVICE_CTRL_RTC_GET_TIME, &time);
set_rtc_time(time); set_rtc_time(time);
return RT_EOK; return RT_EOK;
} }
@ -299,7 +274,6 @@ rt_err_t rt_soft_rtc_set_source(const char *name)
RT_ASSERT(name != RT_NULL); RT_ASSERT(name != RT_NULL);
RT_ASSERT(rt_device_find(name)); /* make sure source is exist*/ RT_ASSERT(rt_device_find(name)); /* make sure source is exist*/
source_device = rt_device_find(name);
rt_work_init(&rtc_sync_work, rtc_sync_work_func, RT_NULL); rt_work_init(&rtc_sync_work, rtc_sync_work_func, RT_NULL);
rt_work_submit(&rtc_sync_work, rt_tick_from_millisecond(RTC_AUTO_SYNC_FIRST_DELAY * 1000)); rt_work_submit(&rtc_sync_work, rt_tick_from_millisecond(RTC_AUTO_SYNC_FIRST_DELAY * 1000));