]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_date.c
Add date rtc i2c
[z180-stamp.git] / avr / cmd_date.c
1 /*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8 /*
9 * RTC, Date & Time support: get and set date & time
10 */
11 #include <common.h>
12 #include <string.h>
13 #include <avr/pgmspace.h>
14 #include <command.h>
15 #include <rtc.h>
16 #include <i2c.h>
17
18
19 static const char * const weekdays[] = {
20 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
21 };
22
23 int mk_date (const char *, struct rtc_time *);
24
25 command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
26 {
27 struct rtc_time tm;
28 int rcode = CMD_RET_SUCCESS;
29
30 (void) cmdtp; (void) flag;
31
32 switch (argc) {
33 case 2: /* set date & time */
34 if (strcmp_P(argv[1],PSTR("reset")) == 0) {
35 my_puts_P(PSTR("Reset RTC...\n"));
36 rtc_reset ();
37 } else {
38 /* initialize tm with current time */
39 rcode = rtc_get (&tm);
40
41 if(!rcode) {
42 /* insert new date & time */
43 if (mk_date (argv[1], &tm) != 0) {
44 my_puts_P(PSTR("## Bad date format\n"));
45 break;
46 }
47 /* and write to RTC */
48 rcode = rtc_set (&tm);
49 if(rcode)
50 my_puts_P(PSTR("## Set date failed\n"));
51 } else {
52 my_puts_P(PSTR("## Get date failed\n"));
53 }
54 }
55 /* FALL TROUGH */
56 case 1: /* get date & time */
57 rcode = rtc_get (&tm);
58
59 if (rcode) {
60 my_puts_P(PSTR("## Get date failed\n"));
61 break;
62 }
63 /* TODO: flash */
64 printf_P(PSTR("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n"),
65 tm.tm_year, tm.tm_mon, tm.tm_mday,
66 (tm.tm_wday<0 || tm.tm_wday>6) ?
67 "unknown " : weekdays[tm.tm_wday],
68 tm.tm_hour, tm.tm_min, tm.tm_sec);
69
70 break;
71 default:
72 rcode = CMD_RET_USAGE;
73 }
74
75 return rcode;
76 }
77
78 /*
79 * simple conversion of two-digit string with error checking
80 */
81 static int cnvrt2 (const char *str, int *valp)
82 {
83 int val;
84
85 if ((*str < '0') || (*str > '9'))
86 return (-1);
87
88 val = *str - '0';
89
90 ++str;
91
92 if ((*str < '0') || (*str > '9'))
93 return (-1);
94
95 *valp = 10 * val + (*str - '0');
96
97 return (0);
98 }
99
100 /*
101 * Convert date string: MMDDhhmm[[CC]YY][.ss]
102 *
103 * Some basic checking for valid values is done, but this will not catch
104 * all possible error conditions.
105 */
106 int mk_date (const char *datestr, struct rtc_time *tmp)
107 {
108 int len, val;
109 char *ptr;
110
111 ptr = strchr (datestr,'.');
112 len = strlen (datestr);
113
114 /* Set seconds */
115 if (ptr) {
116 int sec;
117
118 *ptr++ = '\0';
119 if ((len - (ptr - datestr)) != 2)
120 return (-1);
121
122 len = strlen (datestr);
123
124 if (cnvrt2 (ptr, &sec))
125 return (-1);
126
127 tmp->tm_sec = sec;
128 } else {
129 tmp->tm_sec = 0;
130 }
131
132 if (len == 12) { /* MMDDhhmmCCYY */
133 int year, century;
134
135 if (cnvrt2 (datestr+ 8, &century) ||
136 cnvrt2 (datestr+10, &year) ) {
137 return (-1);
138 }
139 tmp->tm_year = 100 * century + year;
140 } else if (len == 10) { /* MMDDhhmmYY */
141 int year, century;
142
143 century = tmp->tm_year / 100;
144 if (cnvrt2 (datestr+ 8, &year))
145 return (-1);
146 tmp->tm_year = 100 * century + year;
147 }
148
149 switch (len) {
150 case 8: /* MMDDhhmm */
151 /* fall thru */
152 case 10: /* MMDDhhmmYY */
153 /* fall thru */
154 case 12: /* MMDDhhmmCCYY */
155 if (cnvrt2 (datestr+0, &val) ||
156 val > 12) {
157 break;
158 }
159 tmp->tm_mon = val;
160 if (cnvrt2 (datestr+2, &val) ||
161 val > ((tmp->tm_mon==2) ? 29 : 31)) {
162 break;
163 }
164 tmp->tm_mday = val;
165
166 if (cnvrt2 (datestr+4, &val) ||
167 val > 23) {
168 break;
169 }
170 tmp->tm_hour = val;
171
172 if (cnvrt2 (datestr+6, &val) ||
173 val > 59) {
174 break;
175 }
176 tmp->tm_min = val;
177
178 /* calculate day of week */
179 GregorianDay (tmp);
180
181 return (0);
182 default:
183 break;
184 }
185
186 return (-1);
187 }
188