1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Advanced Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
8 | * Copyright (C) 2004-2010 by Embedded and Real-Time Systems Laboratory
|
---|
9 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
10 | * Copyright (C) 2012-2015 by GT Development Center RICOH COMPANY,LTD. JAPAN
|
---|
11 | * Copyright (C) 2015-2019 by TOPPERS PROJECT Educational Working Group.
|
---|
12 | *
|
---|
13 | * 上記著作権者は,以下の (1)~(4) の条件か,Free Software Foundation
|
---|
14 | * によって公表されている GNU General Public License の Version 2 に記
|
---|
15 | * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
|
---|
16 | * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
|
---|
17 | * 利用と呼ぶ)することを無償で許諾する.
|
---|
18 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
19 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
20 | * スコード中に含まれていること.
|
---|
21 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
22 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
23 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
24 | * の無保証規定を掲載すること.
|
---|
25 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
26 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
27 | * と.
|
---|
28 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
29 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
30 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
31 | * 報告すること.
|
---|
32 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
33 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
34 | *
|
---|
35 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
36 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
|
---|
37 | * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
|
---|
38 | * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
|
---|
39 | *
|
---|
40 | * @(#) $Id$
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include <kernel.h>
|
---|
44 | #include <t_syslog.h>
|
---|
45 | #include <t_stdlib.h>
|
---|
46 | #include <stdio.h>
|
---|
47 | #define _SYS__STDINT_H
|
---|
48 | #include <time.h>
|
---|
49 | #include "kernel_cfg.h"
|
---|
50 | #include "device.h"
|
---|
51 | #include "monitor.h"
|
---|
52 |
|
---|
53 | #define RTC_CLOCK 32768 /* LSE_VALUE */
|
---|
54 |
|
---|
55 | static int_t date_func(int argc, char **argv);
|
---|
56 | static int_t time_func(int argc, char **argv);
|
---|
57 | static int_t wakeup_func(int argc, char **argv);
|
---|
58 | static int_t clock_func(int argc, char **argv);
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * RTCコマンドテーブル
|
---|
62 | */
|
---|
63 | static const COMMAND_INFO rtc_command_info[] = {
|
---|
64 | {"DATE", date_func }, /* 日にち設定 */
|
---|
65 | {"TIME", time_func }, /* 時間設定 */
|
---|
66 | {"WAKEUP", wakeup_func }, /* WAKEUP設定 */
|
---|
67 | {"CLOCK", clock_func } /* 時計 */
|
---|
68 | };
|
---|
69 |
|
---|
70 | #define NUM_RTC_CMD (sizeof(rtc_command_info)/sizeof(COMMAND_INFO))
|
---|
71 |
|
---|
72 | static const char rtc_name[] = "RTC";
|
---|
73 | static const char rtc_help[] =
|
---|
74 | " Rtc DATE (year #) (month #) (day #)\n"
|
---|
75 | " TIME (hour #) (min #) (sec #)\n"
|
---|
76 | #ifdef USE_RTC_WAKEUP_CMD
|
---|
77 | " WAKEUP time(ms)\n"
|
---|
78 | #endif
|
---|
79 | " CLOCK DATE:TIME\n";
|
---|
80 |
|
---|
81 | static const char monthday[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
---|
82 |
|
---|
83 | static COMMAND_LINK rtc_command_link = {
|
---|
84 | NULL,
|
---|
85 | NUM_RTC_CMD,
|
---|
86 | rtc_name,
|
---|
87 | NULL,
|
---|
88 | rtc_help,
|
---|
89 | &rtc_command_info[0]
|
---|
90 | };
|
---|
91 |
|
---|
92 | static RTC_Wakeup_t rtc_wup;
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * ローカルatox
|
---|
96 | */
|
---|
97 | static int atoix(char *s)
|
---|
98 | {
|
---|
99 | int c, val = 0;
|
---|
100 |
|
---|
101 | while((c = *s++) != 0){
|
---|
102 | if(c >= '0' && c <= '9')
|
---|
103 | val = val * 10 + (c - '0');
|
---|
104 | }
|
---|
105 | return val;
|
---|
106 | }
|
---|
107 |
|
---|
108 | #ifdef USE_RTC_WAKEUP_CMD
|
---|
109 | /*
|
---|
110 | * WAKUP EVENT
|
---|
111 | */
|
---|
112 | static void wakeup_event(void)
|
---|
113 | {
|
---|
114 | syslog_0(LOG_NOTICE, "rtc wakeup event !");
|
---|
115 | }
|
---|
116 | #endif
|
---|
117 | #ifndef NOT_USE_TBP_MKTIME_GMTIME
|
---|
118 | /*
|
---|
119 | * mktime関数
|
---|
120 | */
|
---|
121 | time_t mktime(struct tm *ptm)
|
---|
122 | {
|
---|
123 | int day = ptm->tm_year *365;
|
---|
124 | int yday, i;
|
---|
125 |
|
---|
126 | day += (ptm->tm_year+2) / 4;
|
---|
127 | for(i = 1, yday = 0 ; i < ptm->tm_mon ; i++)
|
---|
128 | yday += monthday[i-1];
|
---|
129 | if(ptm->tm_mon > 2 && ((ptm->tm_year+2) % 4) == 0)
|
---|
130 | yday++;
|
---|
131 | yday += ptm->tm_mday - 1;
|
---|
132 | day += yday;
|
---|
133 | ptm->tm_wday = (day+4) % 7;
|
---|
134 | ptm->tm_yday = yday+1;
|
---|
135 | syslog_3(LOG_INFO, "yday[%d] wday[%d] day[%d]", yday+1, ptm->tm_wday, ptm->tm_yday);
|
---|
136 | return day * (24*60*60) + ptm->tm_hour * 60*60 + ptm->tm_min * 60 + ptm->tm_sec;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * gmtime_r関数
|
---|
141 | */
|
---|
142 | struct tm *gmtime_r(const time_t *pt, struct tm *ptm)
|
---|
143 | {
|
---|
144 | int day = *pt / (24*60*60);
|
---|
145 | int time, i, yday, mday;
|
---|
146 |
|
---|
147 | time = *pt - day * 24 * 60 * 60;
|
---|
148 | ptm->tm_wday = (day+4) % 7;
|
---|
149 | for(i = 365, ptm->tm_year = 0 ; i < day ; i += 365){
|
---|
150 | if(((ptm->tm_year+2) % 4) == 0)
|
---|
151 | i++;
|
---|
152 | ptm->tm_year++;
|
---|
153 | }
|
---|
154 |
|
---|
155 | ptm->tm_yday = yday = day - i + 365 + 1;
|
---|
156 | for(ptm->tm_mon = 1 ; ptm->tm_mon <= 12 && yday > 0 ; ptm->tm_mon++){
|
---|
157 | mday = monthday[ptm->tm_mon-1];
|
---|
158 | if(ptm->tm_mon == 2 && ((ptm->tm_year+2) % 4) == 0)
|
---|
159 | mday++;
|
---|
160 | if(yday < mday){
|
---|
161 | ptm->tm_mday = yday;
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | yday -= mday;
|
---|
165 | }
|
---|
166 | ptm->tm_hour = time / (60*60);
|
---|
167 | time -= ptm->tm_hour * 60 * 60;
|
---|
168 | ptm->tm_min = time / 60;
|
---|
169 | ptm->tm_sec = time % 60;
|
---|
170 | return ptm;
|
---|
171 | }
|
---|
172 | #endif
|
---|
173 | int
|
---|
174 | _gettimeofday (struct timeval * tp, void * tzvp)
|
---|
175 | {
|
---|
176 | struct tm timedate;
|
---|
177 | if (!tp) return 0;
|
---|
178 | rtc_get_time((struct tm2 *)&timedate);
|
---|
179 | uint64_t time = (uint64_t)mktime(&timedate) * (uint64_t)1000000;
|
---|
180 | tp->tv_sec = time / 1000000;
|
---|
181 | tp->tv_usec = time - (tp->tv_sec * 1000000);
|
---|
182 | return 0;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * RTCコマンド設定関数
|
---|
187 | */
|
---|
188 | void rtc_info_init(intptr_t exinf)
|
---|
189 | {
|
---|
190 | #ifdef USE_RTC_WAKEUP_CMD
|
---|
191 | rtc_wakeup_init(&rtc_wup, wakeup_event);
|
---|
192 | #endif
|
---|
193 | setup_command(&rtc_command_link);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * 日付設定コマンド関数
|
---|
198 | */
|
---|
199 | static int_t date_func(int argc, char **argv)
|
---|
200 | {
|
---|
201 | struct tm timedate;
|
---|
202 | int arg1 = 2016;
|
---|
203 |
|
---|
204 | ER ret = rtc_get_time((struct tm2 *)&timedate);
|
---|
205 | if (ret != E_OK){
|
---|
206 | printf("rtc_get_time %s\n", itron_strerror(ret));
|
---|
207 | }
|
---|
208 | if(argc > 1)
|
---|
209 | arg1 = atoix(argv[1]);
|
---|
210 | if(argc > 2)
|
---|
211 | timedate.tm_mon = atoix(argv[2]);
|
---|
212 | else
|
---|
213 | timedate.tm_mon = 1;
|
---|
214 | if(argc > 3)
|
---|
215 | timedate.tm_mday = atoix(argv[3]);
|
---|
216 | else
|
---|
217 | timedate.tm_mday = 1;
|
---|
218 | printf("%04d/%02d/%02d\n", arg1, timedate.tm_mon, timedate.tm_mday);
|
---|
219 | timedate.tm_year = arg1-1900;
|
---|
220 | timedate.tm_isdst = 0;
|
---|
221 | mktime((struct tm *)&timedate);
|
---|
222 | ret = rtc_set_time((struct tm2 *)&timedate);
|
---|
223 | if (ret != E_OK){
|
---|
224 | printf("rtc_set_time %s\n", itron_strerror(ret));
|
---|
225 | }
|
---|
226 | return 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * 時間設定コマンド関数
|
---|
231 | */
|
---|
232 | static int_t time_func(int argc, char **argv)
|
---|
233 | {
|
---|
234 | struct tm2 timedate;
|
---|
235 |
|
---|
236 | ER ret = rtc_get_time(&timedate);
|
---|
237 | if (ret != E_OK){
|
---|
238 | printf("rtc_get_time %s\n", itron_strerror(ret));
|
---|
239 | }
|
---|
240 | if(argc > 1)
|
---|
241 | timedate.tm_hour = atoix(argv[1]);
|
---|
242 | else
|
---|
243 | timedate.tm_hour = 0;
|
---|
244 | if(argc > 2)
|
---|
245 | timedate.tm_min = atoix(argv[2]);
|
---|
246 | else
|
---|
247 | timedate.tm_min = 0;
|
---|
248 | if(argc > 3)
|
---|
249 | timedate.tm_sec = atoix(argv[3]);
|
---|
250 | else
|
---|
251 | timedate.tm_sec = 0;
|
---|
252 | printf("%02d:%02d:%02d\n", timedate.tm_hour, timedate.tm_min, timedate.tm_sec);
|
---|
253 | timedate.tm_isdst = 0;
|
---|
254 | mktime((struct tm *)&timedate);
|
---|
255 | ret = rtc_set_time(&timedate);
|
---|
256 | if (ret != E_OK){
|
---|
257 | printf("rtc_set_time %s\n", itron_strerror(ret));
|
---|
258 | }
|
---|
259 | return 0;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * WAKEUPイベント設定関数
|
---|
264 | */
|
---|
265 | static int_t wakeup_func(int argc, char **argv)
|
---|
266 | {
|
---|
267 | uint32_t count = 0;
|
---|
268 | if(argc > 1){
|
---|
269 | count = atoix(argv[1]);
|
---|
270 | count = (count * (RTC_CLOCK >> rtc_wup.wakeuptimerPrescaler)) / 1000;
|
---|
271 | if(count <= 0xFFFF)
|
---|
272 | rtc_setup_wakeuptime(count);
|
---|
273 | else
|
---|
274 | printf("wakeup renge error(%d) !", count);
|
---|
275 | return 0;
|
---|
276 | }
|
---|
277 | else
|
---|
278 | return 1;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * 時刻取出しコマンド関数
|
---|
283 | */
|
---|
284 | static int_t clock_func(int argc, char **argv)
|
---|
285 | {
|
---|
286 | struct tm2 timedate;
|
---|
287 |
|
---|
288 | ER ret = rtc_get_time(&timedate);
|
---|
289 | if (ret != E_OK){
|
---|
290 | printf("rtc_get_time %s\n", itron_strerror(ret));
|
---|
291 | }
|
---|
292 | mktime(&timedate);
|
---|
293 | printf(" %04d/%02d/%02d yday[%d]\n", timedate.tm_year+1900, timedate.tm_mon, timedate.tm_mday, timedate.tm_yday);
|
---|
294 | printf(" %02d:%02d:%02d wday[%d]\n", timedate.tm_hour, timedate.tm_min, timedate.tm_sec, timedate.tm_wday);
|
---|
295 | return 0;
|
---|
296 | }
|
---|
297 |
|
---|