summaryrefslogtreecommitdiff
path: root/avr
diff options
context:
space:
mode:
Diffstat (limited to 'avr')
-rw-r--r--avr/Tupfile6
-rw-r--r--avr/cmd_date.c141
-rw-r--r--avr/date.c138
-rw-r--r--avr/pcf8583.c21
4 files changed, 80 insertions, 226 deletions
diff --git a/avr/Tupfile b/avr/Tupfile
index 1c0f7ed..6c40649 100644
--- a/avr/Tupfile
+++ b/avr/Tupfile
@@ -8,11 +8,15 @@ SRC = main.c
SRC += cli.c cli_readline.c command.c command_tbl.c
SRC += cmd_help.c cmd_date.c cmd_mem.c cmd_boot.c cmd_gpio.c cmd_misc.c
SRC += cmd_sd.c cmd_fat.c
-SRC += env.c xmalloc.c date.c con-utils.c print-utils.c getopt-min.c
+SRC += env.c xmalloc.c con-utils.c print-utils.c getopt-min.c
SRC += timer.c serial.c i2c.c pcf8583.c mmc.c
SRC += background.c z180-serv.c z80-if.c gpio.c
SRC += $(FATFS) $(TOP)/fatfs/src/option/unicode.c
+#TODO: time lib
+SRC += ../time/asctime_r.c ../time/gmtime_r.c ../time/mk_gmtime.c
+SRC += ../time/print_lz.c ../time/isLeap.c
+
SRC_Z = ../z180/hdrom.c
#TARGETS = $(PROG).elf
diff --git a/avr/cmd_date.c b/avr/cmd_date.c
index 337d720..3beebee 100644
--- a/avr/cmd_date.c
+++ b/avr/cmd_date.c
@@ -8,73 +8,13 @@
/*
* RTC, Date & Time support: get and set date & time
*/
-#include <common.h>
+#include "common.h"
#include <string.h>
-#include <command.h>
-#include <rtc.h>
-#include <i2c.h>
+#include "time.h"
+#include "rtc.h"
+#include "command.h"
-static const FLASH char * const FLASH weekdays[] = {
- FSTR("Mon"),
- FSTR("Tues"),
- FSTR("Wednes"),
- FSTR("Thurs"),
- FSTR("Fri"),
- FSTR("Satur"),
- FSTR("Sun")
-};
-
-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 */
- /* 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;
- }
- printf_P(PSTR("Date: %4d-%02d-%02d ("),
- tm.tm_year, tm.tm_mon, tm.tm_mday);
- my_puts_P( (tm.tm_wday<0 || tm.tm_wday>6) ?
- PSTR("unknown ") : weekdays[tm.tm_wday]);
- printf_P(PSTR("day) Time: %2d:%02d:%02d\n"),
- 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
*/
@@ -103,7 +43,7 @@ static int cnvrt2 (const char *str, int *valp)
* 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 mk_date (const char *datestr, struct tm *tmp)
{
int len, val;
char *ptr;
@@ -129,36 +69,36 @@ int mk_date (const char *datestr, struct rtc_time *tmp)
tmp->tm_sec = 0;
}
- if (len == 12) { /* MMDDhhmmCCYY */
+ if (len == 12) { /* MMDDhhmmCCYY */
int year, century;
if (cnvrt2 (datestr+ 8, &century) ||
cnvrt2 (datestr+10, &year) ) {
return (-1);
}
- tmp->tm_year = 100 * century + year;
+ tmp->tm_year = 100 * century + year - 1900;
} else if (len == 10) { /* MMDDhhmmYY */
int year, century;
- century = tmp->tm_year / 100;
+ century = (tmp->tm_year + 1900) / 100;
if (cnvrt2 (datestr+ 8, &year))
return (-1);
- tmp->tm_year = 100 * century + year;
+ tmp->tm_year = 100 * century + year -1900;
}
switch (len) {
- case 8: /* MMDDhhmm */
+ case 8: /* MMDDhhmm */
/* fall thru */
- case 10: /* MMDDhhmmYY */
+ case 10: /* MMDDhhmmYY */
/* fall thru */
- case 12: /* MMDDhhmmCCYY */
+ case 12: /* MMDDhhmmCCYY */
if (cnvrt2 (datestr+0, &val) ||
val > 12) {
break;
}
- tmp->tm_mon = val;
+ tmp->tm_mon = val - 1;
if (cnvrt2 (datestr+2, &val) ||
- val > ((tmp->tm_mon==2) ? 29 : 31)) {
+ val > ((tmp->tm_mon==2-1) ? 29 : 31)) {
break;
}
tmp->tm_mday = val;
@@ -175,9 +115,6 @@ int mk_date (const char *datestr, struct rtc_time *tmp)
}
tmp->tm_min = val;
- /* calculate day of week */
- GregorianDay (tmp);
-
return (0);
default:
break;
@@ -185,3 +122,53 @@ int mk_date (const char *datestr, struct rtc_time *tmp)
return (-1);
}
+
+command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ struct tm t;
+ char buf[30];
+ int rcode = CMD_RET_SUCCESS;
+
+ (void) cmdtp; (void) flag;
+
+ switch (argc) {
+ case 2: /* set date & time */
+ /* initialize t with current time */
+ rcode = rtc_get (&t);
+
+ if(!rcode) {
+ /* insert new date & time */
+ if (mk_date (argv[1], &t) != 0) {
+ my_puts_P(PSTR("## Bad date format\n"));
+ break;
+ }
+
+ time_t time;
+ time = mk_gmtime(&t);
+ gmtime_r(&time, &t);
+
+ /* and write to RTC */
+ rcode = rtc_set (&t);
+ 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 (&t);
+
+ if (rcode) {
+ my_puts_P(PSTR("## Get date failed\n"));
+ break;
+ }
+ asctime_r(&t, buf);
+ printf_P(PSTR("%s\n"), buf);
+ break;
+
+ default:
+ rcode = CMD_RET_USAGE;
+ }
+
+ return rcode;
+}
diff --git a/avr/date.c b/avr/date.c
deleted file mode 100644
index 5caee2f..0000000
--- a/avr/date.c
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- * (C) Copyright 2001
- * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * SPDX-License-Identifier: GPL-2.0+
- */
-
-/*
- * Date & Time support for RTC
- */
-
-#include <common.h>
-#include <command.h>
-#include <rtc.h>
-
-
-#define FEBRUARY 2
-#define STARTOFTIME 1970
-#define SECDAY 86400L
-#define SECYR (SECDAY * 365)
-#define leapyear(year) ((year) % 4 == 0)
-#define days_in_year(a) (leapyear(a) ? 366 : 365)
-#define days_in_month(a) (month_days[(a) - 1])
-
-
-static const FLASH int MonthOffset[] = {
- 0,31,59,90,120,151,181,212,243,273,304,334
-};
-
-/*
- * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
- */
-void GregorianDay(struct rtc_time * tm)
-{
- int leapsToDate;
- int lastYear;
- int day;
-
- lastYear=tm->tm_year-1;
-
- /*
- * Number of leap corrections to apply up to end of last year
- */
- leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
-
- /*
- * This year is a leap year if it is divisible by 4 except when it is
- * divisible by 100 unless it is divisible by 400
- *
- * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
- */
- if((tm->tm_year%4==0) &&
- ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
- (tm->tm_mon>2)) {
- /*
- * We are past Feb. 29 in a leap year
- */
- day=1;
- } else {
- day=0;
- }
-
- day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + tm->tm_mday;
-
- tm->tm_wday=day%7;
-}
-
-void to_tm(unsigned long tim, struct rtc_time * tm)
-{
- char month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
- register int i;
- register long hms, day;
-
- day = tim / SECDAY;
- hms = tim % SECDAY;
-
- /* Hours, minutes, seconds are easy */
- tm->tm_hour = hms / 3600;
- tm->tm_min = (hms % 3600) / 60;
- tm->tm_sec = (hms % 3600) % 60;
-
- /* Number of years in days */
- for (i = STARTOFTIME; day >= days_in_year(i); i++) {
- day -= days_in_year(i);
- }
- tm->tm_year = i;
-
- /* Number of months in days left */
- if (leapyear(tm->tm_year)) {
- days_in_month(FEBRUARY) = 29;
- }
- for (i = 1; day >= days_in_month(i); i++) {
- day -= days_in_month(i);
- }
- days_in_month(FEBRUARY) = 28;
- tm->tm_mon = i;
-
- /* Days are what is left over (+1) from all that. */
- tm->tm_mday = day + 1;
-
- /*
- * Determine the day of week
- */
- GregorianDay(tm);
-}
-
-/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
- * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
- * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
- *
- * [For the Julian calendar (which was used in Russia before 1917,
- * Britain & colonies before 1752, anywhere else before 1582,
- * and is still in use by some communities) leave out the
- * -year/100+year/400 terms, and add 10.]
- *
- * This algorithm was first published by Gauss (I think).
- *
- * WARNING: this function will overflow on 2106-02-07 06:28:16 on
- * machines were long is 32-bit! (However, as time_t is signed, we
- * will already get problems at other places on 2038-01-19 03:14:08)
- */
-unsigned long
-mktime (unsigned int year, unsigned int mon,
- unsigned int day, unsigned int hour,
- unsigned int min, unsigned int sec)
-{
- if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
- mon += 12; /* Puts Feb last since it has leap day */
- year -= 1;
- }
-
- return (((
- (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
- year*365 - 719499
- )*24 + hour /* now have hours */
- )*60 + min /* now have minutes */
- )*60 + sec; /* finally seconds */
-}
diff --git a/avr/pcf8583.c b/avr/pcf8583.c
index af1331d..7e9d749 100644
--- a/avr/pcf8583.c
+++ b/avr/pcf8583.c
@@ -4,10 +4,11 @@
#include "common.h"
#include <stdlib.h>
-#include "debug.h"
-#include "command.h"
+#include "time.h"
#include "rtc.h"
#include "i2c.h"
+#include "command.h"
+#include "debug.h"
#define DEBUG_RTC 0
@@ -34,17 +35,17 @@ static uint_fast8_t bcd2bin(uint8_t val)
static uint8_t bin2bcd (uint_fast8_t val)
{
div_t d = div(val, 10);
-
+
return (d.quot << 4) | d.rem;
}
-int rtc_get (struct rtc_time *tmp)
+int rtc_get (struct tm *tmp)
{
int rel = 0;
uint8_t rtcbuf[NR_OF_REGS];
- uint16_t year;
-
+ int16_t year;
+
i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, NR_OF_REGS);
i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2);
@@ -56,12 +57,12 @@ int rtc_get (struct rtc_time *tmp)
tmp->tm_min = bcd2bin (rtcbuf[REG_MIN] & 0x7F);
tmp->tm_hour = bcd2bin (rtcbuf[REG_HOUR] & 0x3F);
tmp->tm_mday = bcd2bin (rtcbuf[REG_YRDATE] & 0x3F);
- tmp->tm_mon = bcd2bin (rtcbuf[REG_WDMON] & 0x1F);
+ tmp->tm_mon = bcd2bin (rtcbuf[REG_WDMON] & 0x1F) - 1;
while (year%4 < (rtcbuf[REG_YRDATE]>>6)) {
year++;
/* TODO: update RTC ram */
}
- tmp->tm_year = year;
+ tmp->tm_year = year;
tmp->tm_wday = rtcbuf[REG_WDMON] >> 5;
tmp->tm_yday = 0;
tmp->tm_isdst= 0;
@@ -74,7 +75,7 @@ int rtc_get (struct rtc_time *tmp)
return rel;
}
-int rtc_set (struct rtc_time *tmp)
+int rtc_set (struct tm *tmp)
{
uint8_t rtcbuf[NR_OF_REGS];
@@ -84,7 +85,7 @@ int rtc_set (struct rtc_time *tmp)
rtcbuf[REG_CS] = 0x84;
rtcbuf[REG_CSEC] = 0x00;
- rtcbuf[REG_WDMON ] = bin2bcd(tmp->tm_mon) | ((tmp->tm_wday) << 5);
+ rtcbuf[REG_WDMON ] = (bin2bcd(tmp->tm_mon) + 1) | ((tmp->tm_wday) << 5);
rtcbuf[REG_YRDATE] = ((tmp->tm_year % 4) << 6) | bin2bcd(tmp->tm_mday);
rtcbuf[REG_HOUR ] = bin2bcd(tmp->tm_hour);
rtcbuf[REG_MIN ] = bin2bcd(tmp->tm_min);