[453] | 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 | #define clock_t int
|
---|
| 49 | #include <time.h>
|
---|
| 50 | #include "kernel_cfg.h"
|
---|
| 51 | #include "device.h"
|
---|
| 52 | #include "monitor.h"
|
---|
| 53 |
|
---|
| 54 | #define RTC_CLOCK 32768 /* LSE_VALUE */
|
---|
| 55 |
|
---|
| 56 | static int_t date_func(int argc, char **argv);
|
---|
| 57 | static int_t time_func(int argc, char **argv);
|
---|
| 58 | static int_t intmode_func(int argc, char **argv);
|
---|
| 59 | static int_t wakeup_func(int argc, char **argv);
|
---|
| 60 | static int_t clock_func(int argc, char **argv);
|
---|
| 61 |
|
---|
| 62 | /*
|
---|
| 63 | * RTCコマンドテーブル
|
---|
| 64 | */
|
---|
| 65 | static const COMMAND_INFO rtc_command_info[] = {
|
---|
| 66 | {"DATE", date_func }, /* 日にち設定 */
|
---|
| 67 | {"TIME", time_func }, /* 時間設定 */
|
---|
| 68 | {"INTM", intmode_func }, /* INTMODE設定 */
|
---|
| 69 | {"WAKEUP", wakeup_func }, /* WAKEUP設定 */
|
---|
| 70 | {"CLOCK", clock_func } /* 時計 */
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | #define NUM_RTC_CMD (sizeof(rtc_command_info)/sizeof(COMMAND_INFO))
|
---|
| 74 |
|
---|
| 75 | static const char rtc_name[] = "RTC";
|
---|
| 76 | static const char rtc_help[] =
|
---|
| 77 | " Rtc DATE (year #) (month #) (day #)\n"
|
---|
| 78 | " TIME (hour #) (min #) (sec #)\n"
|
---|
| 79 | " INTM mode(0-4)\n"
|
---|
| 80 | " WAKEUP time(sec)\n"
|
---|
| 81 | " CLOCK DATE:TIME\n";
|
---|
| 82 |
|
---|
| 83 | static const char monthday[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
---|
| 84 |
|
---|
| 85 | static COMMAND_LINK rtc_command_link = {
|
---|
| 86 | NULL,
|
---|
| 87 | NUM_RTC_CMD,
|
---|
| 88 | rtc_name,
|
---|
| 89 | NULL,
|
---|
| 90 | rtc_help,
|
---|
| 91 | &rtc_command_info[0]
|
---|
| 92 | };
|
---|
| 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 | /*
|
---|
| 109 | * TICK EVENT
|
---|
| 110 | */
|
---|
| 111 | static void tick_event(int mode)
|
---|
| 112 | {
|
---|
| 113 | syslog_1(LOG_NOTICE, "rtc tick event(%d) !", mode);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /*
|
---|
| 117 | * WAKUP EVENT
|
---|
| 118 | */
|
---|
| 119 | static void wakeup_event(int reg)
|
---|
| 120 | {
|
---|
| 121 | syslog_1(LOG_NOTICE, "rtc wakeup event[%08x] !", reg);
|
---|
| 122 | }
|
---|
| 123 | #ifndef NOT_USE_TBP_MKTIME_GMTIME
|
---|
| 124 | /*
|
---|
| 125 | * mktime関数
|
---|
| 126 | */
|
---|
| 127 | time_t mktime(struct tm *ptm)
|
---|
| 128 | {
|
---|
| 129 | int day = ptm->tm_year *365;
|
---|
| 130 | int yday, i;
|
---|
| 131 |
|
---|
| 132 | day += (ptm->tm_year+2) / 4;
|
---|
| 133 | for(i = 1, yday = 0 ; i < ptm->tm_mon ; i++)
|
---|
| 134 | yday += monthday[i-1];
|
---|
| 135 | if(ptm->tm_mon > 2 && ((ptm->tm_year+2) % 4) == 0)
|
---|
| 136 | yday++;
|
---|
| 137 | yday += ptm->tm_mday - 1;
|
---|
| 138 | day += yday;
|
---|
| 139 | ptm->tm_wday = (day+4) % 7;
|
---|
| 140 | ptm->tm_yday = yday+1;
|
---|
| 141 | syslog_3(LOG_INFO, "yday[%d] wday[%d] day[%d]", yday+1, ptm->tm_wday, ptm->tm_yday);
|
---|
| 142 | return day * (24*60*60) + ptm->tm_hour * 60*60 + ptm->tm_min * 60 + ptm->tm_sec;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /*
|
---|
| 146 | * gmtime_r関数
|
---|
| 147 | */
|
---|
| 148 | struct tm *gmtime_r(const time_t *pt, struct tm *ptm)
|
---|
| 149 | {
|
---|
| 150 | int day = *pt / (24*60*60);
|
---|
| 151 | int time, i, yday, mday;
|
---|
| 152 |
|
---|
| 153 | time = *pt - day * 24 * 60 * 60;
|
---|
| 154 | ptm->tm_wday = (day+4) % 7;
|
---|
| 155 | for(i = 365, ptm->tm_year = 0 ; i < day ; i += 365){
|
---|
| 156 | if(((ptm->tm_year+2) % 4) == 0)
|
---|
| 157 | i++;
|
---|
| 158 | ptm->tm_year++;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | ptm->tm_yday = yday = day - i + 365 + 1;
|
---|
| 162 | for(ptm->tm_mon = 1 ; ptm->tm_mon <= 12 && yday > 0 ; ptm->tm_mon++){
|
---|
| 163 | mday = monthday[ptm->tm_mon-1];
|
---|
| 164 | if(ptm->tm_mon == 2 && ((ptm->tm_year+2) % 4) == 0)
|
---|
| 165 | mday++;
|
---|
| 166 | if(yday < mday){
|
---|
| 167 | ptm->tm_mday = yday;
|
---|
| 168 | break;
|
---|
| 169 | }
|
---|
| 170 | yday -= mday;
|
---|
| 171 | }
|
---|
| 172 | ptm->tm_hour = time / (60*60);
|
---|
| 173 | time -= ptm->tm_hour * 60 * 60;
|
---|
| 174 | ptm->tm_min = time / 60;
|
---|
| 175 | ptm->tm_sec = time % 60;
|
---|
| 176 | return ptm;
|
---|
| 177 | }
|
---|
| 178 | #endif
|
---|
| 179 | int
|
---|
| 180 | _gettimeofday (struct timeval * tp, void * tzvp)
|
---|
| 181 | {
|
---|
| 182 | struct tm timedate;
|
---|
| 183 | if (!tp) return 0;
|
---|
| 184 | rtc_get_time((struct tm2 *)&timedate);
|
---|
| 185 | uint64_t time = (uint64_t)mktime(&timedate) * (uint64_t)1000000;
|
---|
| 186 | tp->tv_sec = time / 1000000;
|
---|
| 187 | tp->tv_usec = time - (tp->tv_sec * 1000000);
|
---|
| 188 | return 0;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /*
|
---|
| 192 | * RTCコマンド設定関数
|
---|
| 193 | */
|
---|
| 194 | void rtc_info_init(intptr_t exinf)
|
---|
| 195 | {
|
---|
| 196 | setup_command(&rtc_command_link);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /*
|
---|
| 200 | * 日付設定コマンド関数
|
---|
| 201 | */
|
---|
| 202 | static int_t date_func(int argc, char **argv)
|
---|
| 203 | {
|
---|
| 204 | struct tm timedate;
|
---|
| 205 | int arg1 = 2016;
|
---|
| 206 |
|
---|
| 207 | ER ret = rtc_get_time((struct tm2 *)&timedate);
|
---|
| 208 | if (ret != E_OK){
|
---|
| 209 | printf("rtc_get_time %s\n", itron_strerror(ret));
|
---|
| 210 | }
|
---|
| 211 | if(argc > 1)
|
---|
| 212 | arg1 = atoix(argv[1]);
|
---|
| 213 | if(argc > 2)
|
---|
| 214 | timedate.tm_mon = atoix(argv[2]) - 1;
|
---|
| 215 | else
|
---|
| 216 | timedate.tm_mon = 0;
|
---|
| 217 | if(argc > 3)
|
---|
| 218 | timedate.tm_mday = atoix(argv[3]);
|
---|
| 219 | else
|
---|
| 220 | timedate.tm_mday = 1;
|
---|
| 221 | printf("%04d/%02d/%02d\n", arg1, timedate.tm_mon + 1, timedate.tm_mday);
|
---|
| 222 | timedate.tm_year = arg1-1900;
|
---|
| 223 | timedate.tm_isdst = 0;
|
---|
| 224 | mktime((struct tm *)&timedate);
|
---|
| 225 | ret = rtc_set_time((struct tm2 *)&timedate);
|
---|
| 226 | if (ret != E_OK){
|
---|
| 227 | printf("rtc_set_time %s\n", itron_strerror(ret));
|
---|
| 228 | }
|
---|
| 229 | return 0;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /*
|
---|
| 233 | * 時間設定コマンド関数
|
---|
| 234 | */
|
---|
| 235 | static int_t time_func(int argc, char **argv)
|
---|
| 236 | {
|
---|
| 237 | struct tm2 timedate;
|
---|
| 238 |
|
---|
| 239 | ER ret = rtc_get_time(&timedate);
|
---|
| 240 | if (ret != E_OK){
|
---|
| 241 | printf("rtc_get_time %s\n", itron_strerror(ret));
|
---|
| 242 | }
|
---|
| 243 | if(argc > 1)
|
---|
| 244 | timedate.tm_hour = atoix(argv[1]);
|
---|
| 245 | else
|
---|
| 246 | timedate.tm_hour = 0;
|
---|
| 247 | if(argc > 2)
|
---|
| 248 | timedate.tm_min = atoix(argv[2]);
|
---|
| 249 | else
|
---|
| 250 | timedate.tm_min = 0;
|
---|
| 251 | if(argc > 3)
|
---|
| 252 | timedate.tm_sec = atoix(argv[3]);
|
---|
| 253 | else
|
---|
| 254 | timedate.tm_sec = 0;
|
---|
| 255 | printf("%02d:%02d:%02d\n", timedate.tm_hour, timedate.tm_min, timedate.tm_sec);
|
---|
| 256 | timedate.tm_isdst = 0;
|
---|
| 257 | mktime((struct tm *)&timedate);
|
---|
| 258 | ret = rtc_set_time(&timedate);
|
---|
| 259 | if (ret != E_OK){
|
---|
| 260 | printf("rtc_set_time %s\n", itron_strerror(ret));
|
---|
| 261 | }
|
---|
| 262 | return 0;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /*
|
---|
| 266 | * TICK割込み設定
|
---|
| 267 | */
|
---|
| 268 | static int_t intmode_func(int argc, char **argv)
|
---|
| 269 | {
|
---|
| 270 | int8_t mode = -1;
|
---|
| 271 | if(argc > 1){
|
---|
| 272 | mode = atoix(argv[1]);
|
---|
| 273 | if(mode > 3)
|
---|
| 274 | printf("reset intmode(%d)\n", rtc_intmode(mode, NULL));
|
---|
| 275 | else
|
---|
| 276 | printf("setup intmode(%d)\n", rtc_intmode(mode, tick_event));
|
---|
| 277 | }
|
---|
| 278 | else
|
---|
| 279 | printf("current intmode(%d)\n", rtc_intmode(mode, NULL));
|
---|
| 280 | return 0;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /*
|
---|
| 284 | * WAKEUPイベント設定関数
|
---|
| 285 | */
|
---|
| 286 | static int_t wakeup_func(int argc, char **argv)
|
---|
| 287 | {
|
---|
| 288 | uint32_t count = 0;
|
---|
| 289 | struct tm2 timedate;
|
---|
| 290 | RTC_Alarm_t alarm_param;
|
---|
| 291 |
|
---|
| 292 | rtc_get_time(&timedate);
|
---|
| 293 | if(argc > 1){
|
---|
| 294 | count = atoix(argv[1]);
|
---|
| 295 | alarm_param.alarmmask = RTC_ALARM_SECOND | RTC_ALARM_MINUTE | RTC_ALARM_HOUR;
|
---|
| 296 | alarm_param.callback = wakeup_event;
|
---|
| 297 | timedate.tm_sec += count;
|
---|
| 298 | while(timedate.tm_sec >= 60){
|
---|
| 299 | timedate.tm_sec -= 60;
|
---|
| 300 | if(++timedate.tm_min >= 60){
|
---|
| 301 | timedate.tm_min -= 60;
|
---|
| 302 | if(++timedate.tm_hour >= 24)
|
---|
| 303 | timedate.tm_hour = 0;
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | rtc_setalarm(&alarm_param, &timedate);
|
---|
| 307 | printf("wakeup %02d:%02d:%02d !\n", timedate.tm_hour, timedate.tm_min, timedate.tm_sec);
|
---|
| 308 | }
|
---|
| 309 | else{
|
---|
| 310 | alarm_param.alarmmask = 0;
|
---|
| 311 | rtc_setalarm(&alarm_param, &timedate);
|
---|
| 312 | printf("stop wakeup event !\n");
|
---|
| 313 | }
|
---|
| 314 | return 0;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /*
|
---|
| 318 | * 時刻取出しコマンド関数
|
---|
| 319 | */
|
---|
| 320 | static int_t clock_func(int argc, char **argv)
|
---|
| 321 | {
|
---|
| 322 | struct tm2 timedate;
|
---|
| 323 |
|
---|
| 324 | ER ret = rtc_get_time(&timedate);
|
---|
| 325 | if (ret != E_OK){
|
---|
| 326 | printf("rtc_get_time %s\n", itron_strerror(ret));
|
---|
| 327 | }
|
---|
| 328 | mktime((struct tm *)&timedate);
|
---|
| 329 | printf(" %04d/%02d/%02d yday[%d]\n", timedate.tm_year+1900, timedate.tm_mon+1, timedate.tm_mday, timedate.tm_yday);
|
---|
| 330 | printf(" %02d:%02d:%02d wday[%d]\n", timedate.tm_hour, timedate.tm_min, timedate.tm_sec, timedate.tm_wday);
|
---|
| 331 | return 0;
|
---|
| 332 | }
|
---|
| 333 |
|
---|