1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*
* Copyright (c) 2006-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-01-30 armink the first version
*/
#include <sys/time.h>
#include <string.h>
#include <rtthread.h>
#include <rtdevice.h>
#ifdef RT_USING_KTIME
#include <ktime.h>
#endif
#ifdef RT_USING_SOFT_RTC
/* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */
#define RTC_TIME_INIT(year, month, day, hour, minute, second) \
{.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second}
#ifndef SOFT_RTC_TIME_DEFAULT
#define SOFT_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0)
#endif
#ifndef RTC_AUTO_SYNC_FIRST_DELAY
#define RTC_AUTO_SYNC_FIRST_DELAY 25
#endif
#ifndef RTC_AUTO_SYNC_PERIOD
#define RTC_AUTO_SYNC_PERIOD 3600
#endif
static struct rt_work rtc_sync_work;
static struct rt_device soft_rtc_dev;
static rt_tick_t init_tick;
static time_t init_time;
static struct timeval init_tv = {0};
#ifdef RT_USING_KTIME
static struct timespec init_ts = {0};
#endif
#ifdef RT_USING_ALARM
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);
}
}
#endif
static void set_rtc_time(time_t t)
{
init_time = t - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
#ifdef RT_USING_ALARM
soft_rtc_alarm_update(&wkalarm);
#endif
}
static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
{
time_t *t;
struct tm time_temp;
RT_ASSERT(dev != RT_NULL);
rt_memset(&time_temp, 0, sizeof(struct tm));
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
{
t = (time_t *) args;
*t = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
break;
}
case RT_DEVICE_CTRL_RTC_SET_TIME:
{
t = (time_t *) args;
set_rtc_time(*t);
break;
}
#ifdef RT_USING_ALARM
case RT_DEVICE_CTRL_RTC_GET_ALARM:
*((struct rt_rtc_wkalarm *)args) = wkalarm;
break;
case RT_DEVICE_CTRL_RTC_SET_ALARM:
wkalarm = *((struct rt_rtc_wkalarm *)args);
soft_rtc_alarm_update(&wkalarm);
break;
#endif
#ifdef RT_USING_KTIME
case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
{
struct timeval _tv;
struct timeval *tv = (struct timeval *)args;
rt_ktime_boottime_get_us(&_tv);
tv->tv_sec = init_time + _tv.tv_sec;
tv->tv_usec = init_tv.tv_usec + _tv.tv_usec;
break;
}
case RT_DEVICE_CTRL_RTC_SET_TIMEVAL:
{
struct timeval _tv;
struct timeval *tv = (struct timeval *)args;
rt_ktime_boottime_get_us(&_tv);
set_rtc_time(tv->tv_sec);
init_tv.tv_usec = tv->tv_usec - _tv.tv_usec;
break;
}
case RT_DEVICE_CTRL_RTC_GET_TIMESPEC:
{
struct timespec _ts;
struct timespec *ts = (struct timespec *)args;
rt_ktime_boottime_get_ns(&_ts);
ts->tv_sec = init_time + _ts.tv_sec;
ts->tv_nsec = init_ts.tv_nsec + _ts.tv_nsec;
break;
}
case RT_DEVICE_CTRL_RTC_SET_TIMESPEC:
{
struct timespec _ts;
struct timespec *ts = (struct timespec *)args;
rt_ktime_boottime_get_ns(&_ts);
set_rtc_time(ts->tv_sec);
init_ts.tv_nsec = ts->tv_nsec - _ts.tv_nsec;
break;
}
case RT_DEVICE_CTRL_RTC_GET_TIMERES:
{
struct timespec *ts = (struct timespec *)args;
ts->tv_sec = 0;
ts->tv_nsec = (rt_ktime_cputimer_getres() / RT_KTIME_RESMUL);
break;
}
#else
case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
{
struct timeval *tv = (struct timeval *)args;
rt_tick_t tick = rt_tick_get() - init_tick;
tv->tv_sec = init_time + tick / RT_TICK_PER_SECOND;
tv->tv_usec = init_tv.tv_usec + ((tick % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND));
break;
}
case RT_DEVICE_CTRL_RTC_SET_TIMEVAL:
{
struct timeval *tv = (struct timeval *)args;
rt_tick_t tick = rt_tick_get() - init_tick;
set_rtc_time(tv->tv_sec);
init_tv.tv_usec = tv->tv_usec - ((tick % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND));
break;
}
case RT_DEVICE_CTRL_RTC_GET_TIMERES:
{
struct timespec *ts = (struct timespec *)args;
ts->tv_sec = 0;
ts->tv_nsec = (1000UL * 1000 * 1000) / RT_TICK_PER_SECOND;
break;
}
#endif /* RT_USING_KTIME */
default:
return -RT_EINVAL;
}
return RT_EOK;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops soft_rtc_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
RT_NULL,
soft_rtc_control
};
#endif
static int rt_soft_rtc_init(void)
{
static rt_bool_t init_ok = RT_FALSE;
struct tm time_new = SOFT_RTC_TIME_DEFAULT;
if (init_ok)
{
return 0;
}
/* make sure only one 'rtc' device */
#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"."
#endif
RT_ASSERT(!rt_device_find("rtc"));
#ifdef RT_USING_ALARM
rt_timer_init(&alarm_time,
"alarm",
alarm_timeout,
&soft_rtc_dev,
0,
RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_ONE_SHOT);
#endif
init_tick = rt_tick_get();
init_time = timegm(&time_new);
soft_rtc_dev.type = RT_Device_Class_RTC;
/* register rtc device */
#ifdef RT_USING_DEVICE_OPS
soft_rtc_dev.ops = &soft_rtc_ops;
#else
soft_rtc_dev.init = RT_NULL;
soft_rtc_dev.open = RT_NULL;
soft_rtc_dev.close = RT_NULL;
soft_rtc_dev.read = RT_NULL;
soft_rtc_dev.write = RT_NULL;
soft_rtc_dev.control = soft_rtc_control;
#endif
/* no private */
soft_rtc_dev.user_data = RT_NULL;
rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
init_ok = RT_TRUE;
return 0;
}
INIT_DEVICE_EXPORT(rt_soft_rtc_init);
#ifdef RT_USING_SYSTEM_WORKQUEUE
rt_err_t rt_soft_rtc_sync(void)
{
time_t time = 0;
rt_device_control(&soft_rtc_dev, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
set_rtc_time(time);
return RT_EOK;
}
static void rtc_sync_work_func(struct rt_work *work, void *work_data)
{
rt_soft_rtc_sync();
rt_work_submit(work, rt_tick_from_millisecond(RTC_AUTO_SYNC_PERIOD * 1000));
}
rt_err_t rt_soft_rtc_set_source(const char *name)
{
RT_ASSERT(name != RT_NULL);
RT_ASSERT(rt_device_find(name)); /* make sure source is exist*/
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));
return RT_EOK;
}
#ifdef FINSH_USING_MSH
#include <finsh.h>
static void cmd_rtc_sync(int argc, char **argv)
{
struct timeval tv = {0};
struct timezone tz = {0};
time_t now = (time_t)0;
rt_soft_rtc_sync();
gettimeofday(&tv, &tz);
now = tv.tv_sec;
/* output current time */
rt_kprintf("local time: %.*s", 25, ctime(&now));
rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec);
}
MSH_CMD_EXPORT_ALIAS(cmd_rtc_sync, rtc_sync, Update time by soft rtc);
#endif
#endif /* RT_USING_SYSTEM_WORKQUEUE */
#endif /* RT_USING_SOFT_RTC */
|