Skip to content

Commit a2a7f3c

Browse files
committed
[components][rtc] Add RTC framework V2.0 to simplify RTC registration process
1 parent 2fda55d commit a2a7f3c

File tree

7 files changed

+175
-8
lines changed

7 files changed

+175
-8
lines changed

components/drivers/include/drivers/rtc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#ifndef __RTC_H__
1212
#define __RTC_H__
1313

14+
#include <rtconfig.h>
15+
#include <drivers/rtc_core.h>
16+
1417
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
1518
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);
1619

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2021-06-11 iysheng first version.
9+
*/
10+
11+
#ifndef __RTC_CORE_H__
12+
#define __RTC_CORE_H__
13+
14+
#include <rtthread.h>
15+
16+
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */
17+
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */
18+
#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x12 /**< get microsecond time */
19+
#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x13 /**< set microsecond time */
20+
#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x14 /**< get alarm */
21+
#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x15 /**< set alarm */
22+
23+
struct rt_rtc_ops
24+
{
25+
rt_err_t (*init)(void);
26+
rt_err_t (*get_secs)(void *arg);
27+
rt_err_t (*set_secs)(void *arg);
28+
rt_err_t (*get_alarm)(void *arg);
29+
rt_err_t (*set_alarm)(void *arg);
30+
};
31+
32+
typedef struct rt_rtc_device
33+
{
34+
struct rt_device parent;
35+
const struct rt_rtc_ops *ops;
36+
} rt_rtc_dev_t;
37+
38+
rt_err_t rt_rtc_dev_register(rt_rtc_dev_t *rtc,
39+
const char *name,
40+
rt_uint32_t flag,
41+
void *data);
42+
43+
#endif /* __RTC_CORE_H__ */

components/drivers/rtc/SConscript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ CPPPATH = [cwd + '/../include']
1313
group = []
1414

1515
if GetDepend(['RT_USING_RTC']):
16-
src = src + rtc
16+
src = src + ['rtc.c', 'rtc_core.c']
1717
if GetDepend(['RT_USING_ALARM']):
1818
src = src + rtc_alarm
1919
if GetDepend(['RT_USING_SOFT_RTC']):
2020
src = src + soft_rtc
2121

2222
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_RTC'], CPPPATH = CPPPATH)
2323

24-
Return('group')
24+
Return('group')

components/drivers/rtc/rtc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <sys/time.h>
1515
#include <string.h>
1616
#include <rtthread.h>
17+
#include <drivers/rtc.h>
1718

1819
#ifdef RT_USING_RTC
1920

components/drivers/rtc/rtc_core.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* COPYRIGHT (C) 2011-2021, Real-Thread Information Technology Ltd
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2021-06-11 iysheng first version.
9+
*/
10+
11+
#include <drivers/rtc_core.h>
12+
13+
#define TRY_DO_RTC_FUNC(rt_rtc_dev, func_name, args) \
14+
rt_rtc_dev->ops->func_name ? rt_rtc_dev->ops->func_name(args) : -RT_EINVAL;
15+
16+
/*
17+
* This function initializes rtc_core
18+
*/
19+
static rt_err_t rt_rtc_core_init(struct rt_device *dev)
20+
{
21+
rt_rtc_dev_t *rtc_core;
22+
23+
RT_ASSERT(dev != RT_NULL);
24+
rtc_core = (rt_rtc_dev_t *)dev;
25+
if (rtc_core->ops->init)
26+
{
27+
return (rtc_core->ops->init());
28+
}
29+
30+
return (-RT_ENOSYS);
31+
}
32+
33+
static rt_err_t rt_rtc_core_open(struct rt_device *dev, rt_uint16_t oflag)
34+
{
35+
return (RT_EOK);
36+
}
37+
38+
static rt_err_t rt_rtc_core_close(struct rt_device *dev)
39+
{
40+
/* Add close member function in rt_rtc_ops when need,
41+
* then call that function here.
42+
* */
43+
return (RT_EOK);
44+
}
45+
46+
static rt_err_t rt_rtc_core_control(struct rt_device *dev,
47+
int cmd,
48+
void *args)
49+
{
50+
rt_rtc_dev_t *rtc_core;
51+
rt_err_t ret = -RT_EINVAL;
52+
53+
RT_ASSERT(dev != RT_NULL);
54+
rtc_core = (rt_rtc_dev_t *)dev;
55+
56+
switch (cmd)
57+
{
58+
case RT_DEVICE_CTRL_RTC_GET_TIME:
59+
ret = TRY_DO_RTC_FUNC(rtc_core, get_secs, args);
60+
break;
61+
case RT_DEVICE_CTRL_RTC_SET_TIME:
62+
ret = TRY_DO_RTC_FUNC(rtc_core, set_secs, args);
63+
break;
64+
case RT_DEVICE_CTRL_RTC_GET_ALARM:
65+
ret = TRY_DO_RTC_FUNC(rtc_core, get_alarm, args);
66+
break;
67+
case RT_DEVICE_CTRL_RTC_SET_ALARM:
68+
ret = TRY_DO_RTC_FUNC(rtc_core, set_alarm, args);
69+
break;
70+
default:
71+
break;
72+
}
73+
74+
return ret;
75+
}
76+
#ifdef RT_USING_DEVICE_OPS
77+
const static struct rt_device_ops rtc_core_ops =
78+
{
79+
rt_rtc_core_init,
80+
rt_rtc_core_open,
81+
rt_rtc_core_close,
82+
RT_NULL,
83+
RT_NULL,
84+
rt_rtc_core_control,
85+
};
86+
#endif
87+
88+
rt_err_t rt_rtc_dev_register(rt_rtc_dev_t *rtc,
89+
const char *name,
90+
rt_uint32_t flag,
91+
void *data)
92+
{
93+
struct rt_device *device;
94+
RT_ASSERT(rtc != RT_NULL);
95+
96+
device = &(rtc->parent);
97+
98+
device->type = RT_Device_Class_RTC;
99+
device->rx_indicate = RT_NULL;
100+
device->tx_complete = RT_NULL;
101+
102+
#ifdef RT_USING_DEVICE_OPS
103+
device->ops = &rtc_core_ops;
104+
#else
105+
device->init = rt_rtc_core_init;
106+
device->open = rt_rtc_core_open;
107+
device->close = rt_rtc_core_close;
108+
device->read = RT_NULL;
109+
device->write = RT_NULL;
110+
device->control = rt_rtc_core_control;
111+
#endif
112+
device->user_data = data;
113+
114+
/* register a character device */
115+
return rt_device_register(device, name, flag);
116+
}
117+

components/libc/compilers/common/time.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,17 @@ static int clock_time_system_init()
473473
rt_device_t device;
474474

475475
time = 0;
476+
477+
#ifdef RT_USING_RTC
476478
device = rt_device_find("rtc");
477479
if (device != RT_NULL)
478480
{
479481
/* get realtime seconds */
480482
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
481483
}
484+
#else
485+
LOG_W("Cannot find a RTC device to provide time!");
486+
#endif
482487

483488
/* get tick */
484489
tick = rt_tick_get();
@@ -591,6 +596,7 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
591596
_timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
592597
_timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
593598

599+
#ifdef RT_USING_RTC
594600
/* update for RTC device */
595601
device = rt_device_find("rtc");
596602
if (device != RT_NULL)
@@ -599,6 +605,9 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
599605
rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
600606
}
601607
else
608+
#else
609+
LOG_W("Cannot find a RTC device to save time!");
610+
#endif
602611
return -1;
603612

604613
return 0;

include/rtdef.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,12 +962,6 @@ enum rt_device_class_type
962962
#define RT_DEVICE_CTRL_BLK_AUTOREFRESH 0x13 /**< block device : enter/exit auto refresh mode */
963963
#define RT_DEVICE_CTRL_NETIF_GETMAC 0x10 /**< get mac address */
964964
#define RT_DEVICE_CTRL_MTD_FORMAT 0x10 /**< format a MTD device */
965-
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */
966-
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */
967-
#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x12 /**< get microsecond time */
968-
#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x13 /**< set microsecond time */
969-
#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x14 /**< get alarm */
970-
#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x15 /**< set alarm */
971965

972966
typedef struct rt_device *rt_device_t;
973967

0 commit comments

Comments
 (0)