diff options
author | Leo C | 2014-08-23 19:34:22 +0200 |
---|---|---|
committer | Leo C | 2014-08-23 19:34:22 +0200 |
commit | 61b0cfe9df810db4fbca78e5f880d61c5063f324 (patch) | |
tree | ec9b73a87afa5fdd90acc3c7309104399db104b7 /avr/cmd_date.c | |
parent | 21a24f90c5aaaaf13f91716208b32cde163c5918 (diff) | |
download | z180-stamp-61b0cfe9df810db4fbca78e5f880d61c5063f324.zip |
Add date rtc i2c
Diffstat (limited to 'avr/cmd_date.c')
-rw-r--r-- | avr/cmd_date.c | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/avr/cmd_date.c b/avr/cmd_date.c new file mode 100644 index 0000000..bc93efc --- /dev/null +++ b/avr/cmd_date.c @@ -0,0 +1,188 @@ +/* + * (C) Copyright 2001 + * Wolfgang Denk, DENX Software Engineering, wd@denx.de. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +/* + * RTC, Date & Time support: get and set date & time + */ +#include <common.h> +#include <string.h> +#include <avr/pgmspace.h> +#include <command.h> +#include <rtc.h> +#include <i2c.h> + + +static const char * const weekdays[] = { + "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", +}; + +int mk_date (const char *, struct rtc_time *); + +command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + struct rtc_time tm; + int rcode = CMD_RET_SUCCESS; + + (void) cmdtp; (void) flag; + + switch (argc) { + case 2: /* set date & time */ + if (strcmp_P(argv[1],PSTR("reset")) == 0) { + my_puts_P(PSTR("Reset RTC...\n")); + rtc_reset (); + } else { + /* initialize tm with current time */ + rcode = rtc_get (&tm); + + if(!rcode) { + /* insert new date & time */ + if (mk_date (argv[1], &tm) != 0) { + my_puts_P(PSTR("## Bad date format\n")); + break; + } + /* and write to RTC */ + rcode = rtc_set (&tm); + if(rcode) + my_puts_P(PSTR("## Set date failed\n")); + } else { + my_puts_P(PSTR("## Get date failed\n")); + } + } + /* FALL TROUGH */ + case 1: /* get date & time */ + rcode = rtc_get (&tm); + + if (rcode) { + my_puts_P(PSTR("## Get date failed\n")); + break; + } + /* TODO: flash */ + printf_P(PSTR("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n"), + tm.tm_year, tm.tm_mon, tm.tm_mday, + (tm.tm_wday<0 || tm.tm_wday>6) ? + "unknown " : weekdays[tm.tm_wday], + tm.tm_hour, tm.tm_min, tm.tm_sec); + + break; + default: + rcode = CMD_RET_USAGE; + } + + return rcode; +} + +/* + * simple conversion of two-digit string with error checking + */ +static int cnvrt2 (const char *str, int *valp) +{ + int val; + + if ((*str < '0') || (*str > '9')) + return (-1); + + val = *str - '0'; + + ++str; + + if ((*str < '0') || (*str > '9')) + return (-1); + + *valp = 10 * val + (*str - '0'); + + return (0); +} + +/* + * Convert date string: MMDDhhmm[[CC]YY][.ss] + * + * Some basic checking for valid values is done, but this will not catch + * all possible error conditions. + */ +int mk_date (const char *datestr, struct rtc_time *tmp) +{ + int len, val; + char *ptr; + + ptr = strchr (datestr,'.'); + len = strlen (datestr); + + /* Set seconds */ + if (ptr) { + int sec; + + *ptr++ = '\0'; + if ((len - (ptr - datestr)) != 2) + return (-1); + + len = strlen (datestr); + + if (cnvrt2 (ptr, &sec)) + return (-1); + + tmp->tm_sec = sec; + } else { + tmp->tm_sec = 0; + } + + if (len == 12) { /* MMDDhhmmCCYY */ + int year, century; + + if (cnvrt2 (datestr+ 8, ¢ury) || + cnvrt2 (datestr+10, &year) ) { + return (-1); + } + tmp->tm_year = 100 * century + year; + } else if (len == 10) { /* MMDDhhmmYY */ + int year, century; + + century = tmp->tm_year / 100; + if (cnvrt2 (datestr+ 8, &year)) + return (-1); + tmp->tm_year = 100 * century + year; + } + + switch (len) { + case 8: /* MMDDhhmm */ + /* fall thru */ + case 10: /* MMDDhhmmYY */ + /* fall thru */ + case 12: /* MMDDhhmmCCYY */ + if (cnvrt2 (datestr+0, &val) || + val > 12) { + break; + } + tmp->tm_mon = val; + if (cnvrt2 (datestr+2, &val) || + val > ((tmp->tm_mon==2) ? 29 : 31)) { + break; + } + tmp->tm_mday = val; + + if (cnvrt2 (datestr+4, &val) || + val > 23) { + break; + } + tmp->tm_hour = val; + + if (cnvrt2 (datestr+6, &val) || + val > 59) { + break; + } + tmp->tm_min = val; + + /* calculate day of week */ + GregorianDay (tmp); + + return (0); + default: + break; + } + + return (-1); +} + |