2021-04-13 20:11:56 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2021-04-13 armink the first version
|
2022-05-16 13:22:35 +08:00
|
|
|
* 2022-05-17 Meco Man improve and beautify
|
2021-04-13 20:11:56 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
static struct rt_device rtc_dev;
|
|
|
|
|
2022-05-16 13:22:35 +08:00
|
|
|
#ifdef BSP_USING_ALARM
|
2021-04-13 20:11:56 +08:00
|
|
|
static struct rt_rtc_wkalarm wkalarm;
|
|
|
|
static struct rt_timer alarm_time;
|
|
|
|
|
|
|
|
static void alarm_timeout(void *param)
|
|
|
|
{
|
|
|
|
rt_alarm_update(param, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm)
|
|
|
|
{
|
|
|
|
rt_tick_t next_tick;
|
|
|
|
|
|
|
|
if (palarm->enable)
|
|
|
|
{
|
|
|
|
next_tick = RT_TICK_PER_SECOND;
|
|
|
|
rt_timer_control(&alarm_time, RT_TIMER_CTRL_SET_TIME, &next_tick);
|
|
|
|
rt_timer_start(&alarm_time);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_timer_stop(&alarm_time);
|
|
|
|
}
|
|
|
|
}
|
2022-05-16 13:22:35 +08:00
|
|
|
#endif /* BSP_USING_ALARM */
|
2021-04-13 20:11:56 +08:00
|
|
|
|
2021-09-29 19:47:22 +08:00
|
|
|
static void get_rtc_timeval(struct timeval *tv)
|
|
|
|
{
|
2023-06-07 11:46:18 +08:00
|
|
|
#ifdef _WIN32
|
2021-09-29 19:47:22 +08:00
|
|
|
struct tm newtime = { 0 };
|
|
|
|
SYSTEMTIME sys_time;
|
|
|
|
|
2022-05-16 13:22:35 +08:00
|
|
|
GetSystemTime(&sys_time); /* get RTC from Windows */
|
|
|
|
|
2021-09-29 19:47:22 +08:00
|
|
|
newtime.tm_year = sys_time.wYear - 1900;
|
|
|
|
newtime.tm_mon = sys_time.wMonth - 1;
|
|
|
|
newtime.tm_mday = sys_time.wDay;
|
|
|
|
newtime.tm_hour = sys_time.wHour;
|
|
|
|
newtime.tm_min = sys_time.wMinute;
|
|
|
|
newtime.tm_sec = sys_time.wSecond;
|
|
|
|
|
|
|
|
tv->tv_sec = timegm(&newtime);
|
|
|
|
tv->tv_usec = sys_time.wMilliseconds * 1000UL;
|
2023-06-07 11:46:18 +08:00
|
|
|
#else
|
|
|
|
tv->tv_sec = 0;
|
|
|
|
tv->tv_usec = 0;
|
|
|
|
#endif
|
2021-09-29 19:47:22 +08:00
|
|
|
}
|
|
|
|
|
2022-05-17 14:19:53 +08:00
|
|
|
static rt_err_t pc_rtc_control(rt_device_t dev, int cmd, void *args)
|
2021-04-13 20:11:56 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_CTRL_RTC_GET_TIME:
|
|
|
|
{
|
2021-09-29 19:47:22 +08:00
|
|
|
struct timeval tv;
|
|
|
|
get_rtc_timeval(&tv);
|
2022-05-17 14:19:53 +08:00
|
|
|
*(time_t*) args = tv.tv_sec;
|
2021-09-29 19:47:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
|
|
|
|
{
|
|
|
|
get_rtc_timeval((struct timeval *) args);
|
2021-04-13 20:11:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-05-16 13:22:35 +08:00
|
|
|
#ifdef BSP_USING_ALARM
|
2021-04-13 20:11:56 +08:00
|
|
|
case RT_DEVICE_CTRL_RTC_SET_TIME:
|
|
|
|
{
|
|
|
|
soft_rtc_alarm_update(&wkalarm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RT_DEVICE_CTRL_RTC_GET_ALARM:
|
2022-05-16 13:22:35 +08:00
|
|
|
{
|
2021-04-13 20:11:56 +08:00
|
|
|
*((struct rt_rtc_wkalarm *)args) = wkalarm;
|
|
|
|
break;
|
2022-05-16 13:22:35 +08:00
|
|
|
}
|
2021-04-13 20:11:56 +08:00
|
|
|
case RT_DEVICE_CTRL_RTC_SET_ALARM:
|
2022-05-16 13:22:35 +08:00
|
|
|
{
|
2021-04-13 20:11:56 +08:00
|
|
|
wkalarm = *((struct rt_rtc_wkalarm *)args);
|
|
|
|
soft_rtc_alarm_update(&wkalarm);
|
|
|
|
break;
|
2022-05-16 13:22:35 +08:00
|
|
|
}
|
|
|
|
#endif /* BSP_USING_ALARM */
|
2021-04-14 20:08:22 +08:00
|
|
|
default:
|
|
|
|
return -RT_ERROR;
|
2021-04-13 20:11:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
2022-05-17 14:19:53 +08:00
|
|
|
const static struct rt_device_ops pc_rtc_ops =
|
2021-04-13 20:11:56 +08:00
|
|
|
{
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
2022-05-17 14:19:53 +08:00
|
|
|
pc_rtc_control
|
2021-04-13 20:11:56 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2022-05-17 14:19:53 +08:00
|
|
|
int rt_pc_rtc_init(void)
|
2021-04-13 20:11:56 +08:00
|
|
|
{
|
|
|
|
/* make sure only one 'rtc' device */
|
|
|
|
RT_ASSERT(!rt_device_find("rtc"));
|
|
|
|
|
2022-05-16 13:22:35 +08:00
|
|
|
#ifdef BSP_USING_ALARM
|
2021-04-13 20:11:56 +08:00
|
|
|
rt_timer_init(&alarm_time,
|
|
|
|
"alarm",
|
|
|
|
alarm_timeout,
|
|
|
|
&rtc_dev,
|
|
|
|
0,
|
|
|
|
RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rtc_dev.type = RT_Device_Class_RTC;
|
|
|
|
|
|
|
|
/* register rtc device */
|
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
2022-05-17 14:19:53 +08:00
|
|
|
rtc_dev.ops = &pc_rtc_ops;
|
2021-04-13 20:11:56 +08:00
|
|
|
#else
|
|
|
|
rtc_dev.init = RT_NULL;
|
|
|
|
rtc_dev.open = RT_NULL;
|
|
|
|
rtc_dev.close = RT_NULL;
|
|
|
|
rtc_dev.read = RT_NULL;
|
|
|
|
rtc_dev.write = RT_NULL;
|
2022-05-17 14:19:53 +08:00
|
|
|
rtc_dev.control = pc_rtc_control;
|
2021-04-13 20:11:56 +08:00
|
|
|
#endif
|
2022-05-16 13:22:35 +08:00
|
|
|
rtc_dev.user_data = RT_NULL; /* no private */
|
2021-04-13 20:11:56 +08:00
|
|
|
|
|
|
|
rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-05-17 14:19:53 +08:00
|
|
|
INIT_BOARD_EXPORT(rt_pc_rtc_init);
|