summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorbernard <[email protected]>2021-07-12 00:46:38 +0000
committerGitee <[email protected]>2021-07-12 00:46:38 +0000
commitddc5c793a482fe44310bafe181802ad406fa1b29 (patch)
treeb64de33831a39079cd27acb358cfe90b90a525cb /components
parent2fda55d898826a1ee79babc4e0852ce539a3bff4 (diff)
parentecc0981d1027b2712f27b1c28d7657cb97b93ca2 (diff)
!253 更新 RTC 设备驱动框架,统一并简化 RTC 设备的注册和访问过程
Merge pull request !253 from iysheng/tmp1
Diffstat (limited to 'components')
-rw-r--r--components/drivers/include/drivers/rtc.h3
-rw-r--r--components/drivers/include/drivers/rtc_core.h45
-rw-r--r--components/drivers/rtc/SConscript4
-rw-r--r--components/drivers/rtc/rtc.c1
-rw-r--r--components/drivers/rtc/rtc_core.c123
-rw-r--r--components/libc/compilers/common/time.c9
6 files changed, 183 insertions, 2 deletions
diff --git a/components/drivers/include/drivers/rtc.h b/components/drivers/include/drivers/rtc.h
index 4845937113..3a318357da 100644
--- a/components/drivers/include/drivers/rtc.h
+++ b/components/drivers/include/drivers/rtc.h
@@ -11,6 +11,9 @@
#ifndef __RTC_H__
#define __RTC_H__
+#include <rtconfig.h>
+#include <drivers/rtc_core.h>
+
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
diff --git a/components/drivers/include/drivers/rtc_core.h b/components/drivers/include/drivers/rtc_core.h
new file mode 100644
index 0000000000..f0c7b64e94
--- /dev/null
+++ b/components/drivers/include/drivers/rtc_core.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2021-06-11 iysheng first version.
+ */
+
+#ifndef __RTC_CORE_H__
+#define __RTC_CORE_H__
+
+#include <rtthread.h>
+
+#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */
+#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */
+#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x12 /**< get microsecond time */
+#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x13 /**< set microsecond time */
+#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x14 /**< get alarm */
+#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x15 /**< set alarm */
+
+struct rt_rtc_ops
+{
+ rt_err_t (*init)(void);
+ rt_err_t (*get_secs)(void *arg);
+ rt_err_t (*set_secs)(void *arg);
+ rt_err_t (*get_alarm)(void *arg);
+ rt_err_t (*set_alarm)(void *arg);
+ rt_err_t (*get_usecs)(void *arg);
+ rt_err_t (*set_usecs)(void *arg);
+};
+
+typedef struct rt_rtc_device
+{
+ struct rt_device parent;
+ const struct rt_rtc_ops *ops;
+} rt_rtc_dev_t;
+
+rt_err_t rt_rtc_dev_register(rt_rtc_dev_t *rtc,
+ const char *name,
+ rt_uint32_t flag,
+ void *data);
+
+#endif /* __RTC_CORE_H__ */
diff --git a/components/drivers/rtc/SConscript b/components/drivers/rtc/SConscript
index e1f8f433c1..b922db70c2 100644
--- a/components/drivers/rtc/SConscript
+++ b/components/drivers/rtc/SConscript
@@ -13,7 +13,7 @@ CPPPATH = [cwd + '/../include']
group = []
if GetDepend(['RT_USING_RTC']):
- src = src + rtc
+ src = src + ['rtc.c', 'rtc_core.c']
if GetDepend(['RT_USING_ALARM']):
src = src + rtc_alarm
if GetDepend(['RT_USING_SOFT_RTC']):
@@ -21,4 +21,4 @@ if GetDepend(['RT_USING_RTC']):
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_RTC'], CPPPATH = CPPPATH)
-Return('group') \ No newline at end of file
+Return('group')
diff --git a/components/drivers/rtc/rtc.c b/components/drivers/rtc/rtc.c
index 98b60f9f95..2b9397da90 100644
--- a/components/drivers/rtc/rtc.c
+++ b/components/drivers/rtc/rtc.c
@@ -14,6 +14,7 @@
#include <sys/time.h>
#include <string.h>
#include <rtthread.h>
+#include <drivers/rtc.h>
#ifdef RT_USING_RTC
diff --git a/components/drivers/rtc/rtc_core.c b/components/drivers/rtc/rtc_core.c
new file mode 100644
index 0000000000..80bb61c8aa
--- /dev/null
+++ b/components/drivers/rtc/rtc_core.c
@@ -0,0 +1,123 @@
+/*
+ * COPYRIGHT (C) 2011-2021, Real-Thread Information Technology Ltd
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2021-06-11 iysheng first version.
+ */
+
+#include <drivers/rtc_core.h>
+
+#define TRY_DO_RTC_FUNC(rt_rtc_dev, func_name, args) \
+ rt_rtc_dev->ops->func_name ? rt_rtc_dev->ops->func_name(args) : -RT_EINVAL;
+
+/*
+ * This function initializes rtc_core
+ */
+static rt_err_t rt_rtc_core_init(struct rt_device *dev)
+{
+ rt_rtc_dev_t *rtc_core;
+
+ RT_ASSERT(dev != RT_NULL);
+ rtc_core = (rt_rtc_dev_t *)dev;
+ if (rtc_core->ops->init)
+ {
+ return (rtc_core->ops->init());
+ }
+
+ return (-RT_ENOSYS);
+}
+
+static rt_err_t rt_rtc_core_open(struct rt_device *dev, rt_uint16_t oflag)
+{
+ return (RT_EOK);
+}
+
+static rt_err_t rt_rtc_core_close(struct rt_device *dev)
+{
+ /* Add close member function in rt_rtc_ops when need,
+ * then call that function here.
+ * */
+ return (RT_EOK);
+}
+
+static rt_err_t rt_rtc_core_control(struct rt_device *dev,
+ int cmd,
+ void *args)
+{
+ rt_rtc_dev_t *rtc_core;
+ rt_err_t ret = -RT_EINVAL;
+
+ RT_ASSERT(dev != RT_NULL);
+ rtc_core = (rt_rtc_dev_t *)dev;
+
+ switch (cmd)
+ {
+ case RT_DEVICE_CTRL_RTC_GET_TIME:
+ ret = TRY_DO_RTC_FUNC(rtc_core, get_secs, args);
+ break;
+ case RT_DEVICE_CTRL_RTC_SET_TIME:
+ ret = TRY_DO_RTC_FUNC(rtc_core, set_secs, args);
+ break;
+ case RT_DEVICE_CTRL_RTC_GET_TIME_US:
+ ret = TRY_DO_RTC_FUNC(rtc_core, get_usecs, args);
+ break;
+ case RT_DEVICE_CTRL_RTC_SET_TIME_US:
+ ret = TRY_DO_RTC_FUNC(rtc_core, set_usecs, args);
+ break;
+ case RT_DEVICE_CTRL_RTC_GET_ALARM:
+ ret = TRY_DO_RTC_FUNC(rtc_core, get_alarm, args);
+ break;
+ case RT_DEVICE_CTRL_RTC_SET_ALARM:
+ ret = TRY_DO_RTC_FUNC(rtc_core, set_alarm, args);
+ break;
+ default:
+ break;
+ }
+
+ return ret;
+}
+#ifdef RT_USING_DEVICE_OPS
+const static struct rt_device_ops rtc_core_ops =
+{
+ rt_rtc_core_init,
+ rt_rtc_core_open,
+ rt_rtc_core_close,
+ RT_NULL,
+ RT_NULL,
+ rt_rtc_core_control,
+};
+#endif
+
+rt_err_t rt_rtc_dev_register(rt_rtc_dev_t *rtc,
+ const char *name,
+ rt_uint32_t flag,
+ void *data)
+{
+ struct rt_device *device;
+ RT_ASSERT(rtc != RT_NULL);
+
+ device = &(rtc->parent);
+
+ device->type = RT_Device_Class_RTC;
+ device->rx_indicate = RT_NULL;
+ device->tx_complete = RT_NULL;
+
+#ifdef RT_USING_DEVICE_OPS
+ device->ops = &rtc_core_ops;
+#else
+ device->init = rt_rtc_core_init;
+ device->open = rt_rtc_core_open;
+ device->close = rt_rtc_core_close;
+ device->read = RT_NULL;
+ device->write = RT_NULL;
+ device->control = rt_rtc_core_control;
+#endif
+ device->user_data = data;
+
+ /* register a character device */
+ return rt_device_register(device, name, flag);
+}
+
diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c
index deda18a560..e13f81c2f7 100644
--- a/components/libc/compilers/common/time.c
+++ b/components/libc/compilers/common/time.c
@@ -473,12 +473,17 @@ static int clock_time_system_init()
rt_device_t device;
time = 0;
+
+#ifdef RT_USING_RTC
device = rt_device_find("rtc");
if (device != RT_NULL)
{
/* get realtime seconds */
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
}
+#else
+ LOG_W("Cannot find a RTC device to provide time!");
+#endif
/* get tick */
tick = rt_tick_get();
@@ -591,6 +596,7 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
_timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
_timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
+#ifdef RT_USING_RTC
/* update for RTC device */
device = rt_device_find("rtc");
if (device != RT_NULL)
@@ -599,6 +605,9 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
}
else
+#else
+ LOG_W("Cannot find a RTC device to save time!");
+#endif
return -1;
return 0;