Skip to content

Commit d57caa7

Browse files
authored
Merge pull request #4900 from mysterywolf/rtc
[rtc] 移除rtc_core.c/.h 将内容转移到rtc.c/.h
2 parents 56d3abe + a25667a commit d57caa7

File tree

7 files changed

+162
-236
lines changed

7 files changed

+162
-236
lines changed

bsp/efm32/drv_rtc.c

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -128,49 +128,6 @@ void rt_hw_rtc_isr(rt_device_t device)
128128
RTC->IFC = _RTC_IFC_MASK;
129129
}
130130

131-
/***************************************************************************//**
132-
* @brief
133-
* Register RTC device
134-
*
135-
* @details
136-
*
137-
* @note
138-
*
139-
* @param[in] device
140-
* Pointer to device descriptor
141-
*
142-
* @param[in] name
143-
* Device name
144-
*
145-
* @param[in] flag
146-
* Configuration flags
147-
*
148-
* @return
149-
* Error code
150-
******************************************************************************/
151-
rt_err_t rt_hw_rtc_register(
152-
rt_device_t device,
153-
const char *name,
154-
rt_uint32_t flag)
155-
{
156-
RT_ASSERT(device != RT_NULL);
157-
158-
device->type = RT_Device_Class_RTC;
159-
device->rx_indicate = RT_NULL;
160-
device->tx_complete = RT_NULL;
161-
device->init = RT_NULL;
162-
device->open = rt_rtc_open;
163-
device->close = RT_NULL;
164-
device->read = rt_rtc_read;
165-
device->write = RT_NULL;
166-
device->control = rt_rtc_control;
167-
device->user_data = RT_NULL; /* no private */
168-
169-
/* register a character device */
170-
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
171-
}
172-
173-
174131
/***************************************************************************//**
175132
* @brief
176133
* Initialize all RTC module related hardware and register RTC device to kernel
@@ -224,7 +181,18 @@ void rt_hw_rtc_init(void)
224181
}
225182

226183
/* register rtc device */
227-
rt_hw_rtc_register(&rtc, RT_RTC_NAME, EFM32_NO_DATA);
184+
rtc.type = RT_Device_Class_RTC;
185+
rtc.rx_indicate = RT_NULL;
186+
rtc.tx_complete = RT_NULL;
187+
rtc.init = RT_NULL;
188+
rtc.open = rt_rtc_open;
189+
rtc.close = RT_NULL;
190+
rtc.read = rt_rtc_read;
191+
rtc.write = RT_NULL;
192+
rtc.control = rt_rtc_control;
193+
rtc.user_data = RT_NULL; /* no private */
194+
195+
rt_device_register(&rtc, RT_RTC_NAME, RT_DEVICE_FLAG_RDWR | EFM32_NO_DATA);
228196
}
229197

230198
#endif

bsp/stm32/libraries/HAL_Drivers/drv_rtc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ static rt_err_t stm32_rtc_set_secs(void *args)
265265
static const struct rt_rtc_ops stm32_rtc_ops =
266266
{
267267
stm32_rtc_init,
268-
stm32_rtc_get_secs, /* get_secs */
269-
stm32_rtc_set_secs, /* set secs */
268+
stm32_rtc_get_secs,
269+
stm32_rtc_set_secs,
270270
RT_NULL,
271271
RT_NULL,
272272
RT_NULL,
@@ -280,7 +280,7 @@ static int rt_hw_rtc_init(void)
280280
rt_err_t result;
281281

282282
stm32_rtc_dev.ops = &stm32_rtc_ops;
283-
result = rt_rtc_dev_register(&stm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
283+
result = rt_hw_rtc_register(&stm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
284284
if (result != RT_EOK)
285285
{
286286
LOG_E("rtc register err code: %d", result);

components/drivers/include/drivers/rtc.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,43 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2012-10-10 aozima first version.
9+
* 2021-06-11 iysheng implement RTC framework V2.0
10+
* 2021-07-30 Meco Man move rtc_core.h to rtc.h
911
*/
1012

1113
#ifndef __RTC_H__
1214
#define __RTC_H__
1315

14-
#include <rtconfig.h>
15-
#include <drivers/rtc_core.h>
16+
#include <rtdef.h>
17+
18+
#define RT_DEVICE_CTRL_RTC_GET_TIME 0x10 /**< get second time */
19+
#define RT_DEVICE_CTRL_RTC_SET_TIME 0x11 /**< set second time */
20+
#define RT_DEVICE_CTRL_RTC_GET_TIME_US 0x12 /**< get microsecond time */
21+
#define RT_DEVICE_CTRL_RTC_SET_TIME_US 0x13 /**< set microsecond time */
22+
#define RT_DEVICE_CTRL_RTC_GET_ALARM 0x14 /**< get alarm */
23+
#define RT_DEVICE_CTRL_RTC_SET_ALARM 0x15 /**< set alarm */
24+
25+
struct rt_rtc_ops
26+
{
27+
rt_err_t (*init)(void);
28+
rt_err_t (*get_secs)(void *arg);
29+
rt_err_t (*set_secs)(void *arg);
30+
rt_err_t (*get_alarm)(void *arg);
31+
rt_err_t (*set_alarm)(void *arg);
32+
rt_err_t (*get_usecs)(void *arg);
33+
rt_err_t (*set_usecs)(void *arg);
34+
};
35+
36+
typedef struct rt_rtc_device
37+
{
38+
struct rt_device parent;
39+
const struct rt_rtc_ops *ops;
40+
} rt_rtc_dev_t;
41+
42+
rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
43+
const char *name,
44+
rt_uint32_t flag,
45+
void *data);
1646

1747
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day);
1848
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second);

components/drivers/include/drivers/rtc_core.h

Lines changed: 0 additions & 45 deletions
This file was deleted.

components/drivers/rtc/SConscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CPPPATH = [cwd + '/../include']
77
group = []
88

99
if GetDepend(['RT_USING_RTC']):
10-
src = src + ['rtc.c', 'rtc_core.c']
10+
src = src + ['rtc.c']
1111
if GetDepend(['RT_USING_ALARM']):
1212
src = src + ['alarm.c']
1313
if GetDepend(['RT_USING_SOFT_RTC']):

components/drivers/rtc/rtc.c

Lines changed: 114 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* 2012-04-16 aozima add scheduler lock for set_date and set_time.
1111
* 2018-02-16 armink add auto sync time by NTP
1212
* 2021-05-09 Meco Man remove NTP
13+
* 2021-06-11 iysheng implement RTC framework V2.0
14+
* 2021-07-30 Meco Man move rtc_core.c to rtc.c
1315
*/
1416

1517
#include <time.h>
@@ -20,7 +22,117 @@
2022

2123
#ifdef RT_USING_RTC
2224

25+
/*
26+
* This function initializes rtc_core
27+
*/
28+
static rt_err_t rt_rtc_init(struct rt_device *dev)
29+
{
30+
rt_rtc_dev_t *rtc_core;
31+
32+
RT_ASSERT(dev != RT_NULL);
33+
rtc_core = (rt_rtc_dev_t *)dev;
34+
if (rtc_core->ops->init)
35+
{
36+
return (rtc_core->ops->init());
37+
}
38+
39+
return -RT_ENOSYS;
40+
}
41+
42+
static rt_err_t rt_rtc_open(struct rt_device *dev, rt_uint16_t oflag)
43+
{
44+
return RT_EOK;
45+
}
2346

47+
static rt_err_t rt_rtc_close(struct rt_device *dev)
48+
{
49+
/* Add close member function in rt_rtc_ops when need,
50+
* then call that function here.
51+
* */
52+
return RT_EOK;
53+
}
54+
55+
static rt_err_t rt_rtc_control(struct rt_device *dev, int cmd, void *args)
56+
{
57+
#define TRY_DO_RTC_FUNC(rt_rtc_dev, func_name, args) \
58+
rt_rtc_dev->ops->func_name ? rt_rtc_dev->ops->func_name(args) : -RT_EINVAL;
59+
60+
rt_rtc_dev_t *rtc_device;
61+
rt_err_t ret = -RT_EINVAL;
62+
63+
RT_ASSERT(dev != RT_NULL);
64+
rtc_device = (rt_rtc_dev_t *)dev;
65+
66+
switch (cmd)
67+
{
68+
case RT_DEVICE_CTRL_RTC_GET_TIME:
69+
ret = TRY_DO_RTC_FUNC(rtc_device, get_secs, args);
70+
break;
71+
case RT_DEVICE_CTRL_RTC_SET_TIME:
72+
ret = TRY_DO_RTC_FUNC(rtc_device, set_secs, args);
73+
break;
74+
case RT_DEVICE_CTRL_RTC_GET_TIME_US:
75+
ret = TRY_DO_RTC_FUNC(rtc_device, get_usecs, args);
76+
break;
77+
case RT_DEVICE_CTRL_RTC_SET_TIME_US:
78+
ret = TRY_DO_RTC_FUNC(rtc_device, set_usecs, args);
79+
break;
80+
case RT_DEVICE_CTRL_RTC_GET_ALARM:
81+
ret = TRY_DO_RTC_FUNC(rtc_device, get_alarm, args);
82+
break;
83+
case RT_DEVICE_CTRL_RTC_SET_ALARM:
84+
ret = TRY_DO_RTC_FUNC(rtc_device, set_alarm, args);
85+
break;
86+
default:
87+
break;
88+
}
89+
90+
return ret;
91+
92+
#undef TRY_DO_RTC_FUNC
93+
}
94+
95+
#ifdef RT_USING_DEVICE_OPS
96+
const static struct rt_device_ops rtc_core_ops =
97+
{
98+
rt_rtc_init,
99+
rt_rtc_open,
100+
rt_rtc_close,
101+
RT_NULL,
102+
RT_NULL,
103+
rt_rtc_control,
104+
};
105+
#endif /* RT_USING_DEVICE_OPS */
106+
107+
rt_err_t rt_hw_rtc_register(rt_rtc_dev_t *rtc,
108+
const char *name,
109+
rt_uint32_t flag,
110+
void *data)
111+
{
112+
struct rt_device *device;
113+
RT_ASSERT(rtc != RT_NULL);
114+
115+
device = &(rtc->parent);
116+
117+
device->type = RT_Device_Class_RTC;
118+
device->rx_indicate = RT_NULL;
119+
device->tx_complete = RT_NULL;
120+
121+
#ifdef RT_USING_DEVICE_OPS
122+
device->ops = &rtc_core_ops;
123+
#else
124+
device->init = rt_rtc_init;
125+
device->open = rt_rtc_open;
126+
device->close = rt_rtc_close;
127+
device->read = RT_NULL;
128+
device->write = RT_NULL;
129+
device->control = rt_rtc_control;
130+
#endif /* RT_USING_DEVICE_OPS */
131+
device->user_data = data;
132+
133+
/* register a character device */
134+
return rt_device_register(device, name, flag);
135+
}
24136

25137
/**
26138
* Set system date(time not modify, local timezone).
@@ -30,27 +142,19 @@
30142
* @param rt_uint32_t day e.g: 31.
31143
*
32144
* @return rt_err_t if set success, return RT_EOK.
33-
*
34145
*/
35146
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
36147
{
37148
time_t now;
38-
struct tm *p_tm;
39149
struct tm tm_new;
40150
rt_device_t device;
41151
rt_err_t ret = -RT_ERROR;
42152

43153
/* get current time */
44154
now = time(RT_NULL);
45155

46-
/* lock scheduler. */
47-
rt_enter_critical();
48156
/* converts calendar time into local time. */
49-
p_tm = localtime(&now);
50-
/* copy the statically located variable */
51-
rt_memcpy(&tm_new, p_tm, sizeof(struct tm));
52-
/* unlock scheduler. */
53-
rt_exit_critical();
157+
localtime_r(&now, &tm_new);
54158

55159
/* update date. */
56160
tm_new.tm_year = year - 1900;
@@ -80,27 +184,19 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
80184
* @param rt_uint32_t second e.g: 0~59.
81185
*
82186
* @return rt_err_t if set success, return RT_EOK.
83-
*
84187
*/
85188
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
86189
{
87190
time_t now;
88-
struct tm *p_tm;
89191
struct tm tm_new;
90192
rt_device_t device;
91193
rt_err_t ret = -RT_ERROR;
92194

93195
/* get current time */
94196
now = time(RT_NULL);
95197

96-
/* lock scheduler. */
97-
rt_enter_critical();
98198
/* converts calendar time into local time. */
99-
p_tm = localtime(&now);
100-
/* copy the statically located variable */
101-
rt_memcpy(&tm_new, p_tm, sizeof(struct tm));
102-
/* unlock scheduler. */
103-
rt_exit_critical();
199+
localtime_r(&now, &tm_new);
104200

105201
/* update time. */
106202
tm_new.tm_hour = hour;

0 commit comments

Comments
 (0)