summaryrefslogtreecommitdiff
path: root/avr
diff options
context:
space:
mode:
Diffstat (limited to 'avr')
-rw-r--r--avr/Tupfile19
-rw-r--r--avr/cmd_date.c141
-rw-r--r--avr/cmd_fat.c203
-rw-r--r--avr/date.c138
-rw-r--r--avr/main.c13
-rw-r--r--avr/pcf8583.c21
-rw-r--r--avr/timer.c13
7 files changed, 153 insertions, 395 deletions
diff --git a/avr/Tupfile b/avr/Tupfile
index 1c0f7ed..78e9744 100644
--- a/avr/Tupfile
+++ b/avr/Tupfile
@@ -8,11 +8,19 @@ 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 += ../time/time.c ../time/fatfs_time.c
+SRC += ../time/system_time.c ../time/set_system_time.c
+
+ASRC += ../time/system_tick.S
+
SRC_Z = ../z180/hdrom.c
#TARGETS = $(PROG).elf
@@ -70,17 +78,26 @@ CFLAGS += $(INCLUDES)
CPPFLAGS += $(DEFS)
+#ASFLAGS += -Wa,-adhlns=$(<:.S=.lst),-gstabs
+ASFLAGS += -mmcu=$(MCU_TARGET) -x assembler-with-cpp $(ASFLAGS)
+
# Linker flags
LDFLAGS += -Wl,--gc-sections
LDFLAGS += -Wl,--cref
+# Assemble: create object files from assembler source files.
+#.S.o:
+# $(CC) -c $(ALL_ASFLAGS) $< -o $@
+
+!as = |> ^ AS %f^ $(CC) $(ASFLAGS) -c %f -o %o |> %B.o
!cc = |> ^ CC %f^ $(CC) $(CFLAGS) $(CPPFLAGS) $(CFLAGS_%f) -c %f -o %o |> %B.o
!LINK = |> ^ LINK %o^ $(LD) $(CFLAGS) $(LDFLAGS) -Wl,-Map=%O.map %f $(LDLIBS) -o %o |> | %O.map
!OBJCOPY= |> ^ OBJCOPY %o^ $(OBJCOPY) -Oihex %f %o |>
!OBJDUMP= |> ^ OBJDUMP %o^ $(OBJDUMP) -h -S %f > %o |> %O.lss
!SIZE = |> ^ SIZE^ $(SIZE) %f |>
+: foreach $(ASRC) |> !as |> {objs}
: foreach $(SRC) | ../z180/hdrom.h |> !cc |> {objs}
: $(SRC_Z) |> !cc -D'const=const __flash' |> {objs}
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/cmd_fat.c b/avr/cmd_fat.c
index 0232f5d..9db3b0a 100644
--- a/avr/cmd_fat.c
+++ b/avr/cmd_fat.c
@@ -8,16 +8,24 @@
#include "z80-if.h"
#include "con-utils.h"
#include "print-utils.h"
+#include "time.h"
#include "timer.h"
#include "debug.h"
#define MAX_MEMORY (1ul << 20)
+#define BUFFER_SIZE 512
+
DWORD get_fattime (void)
{
- /* TODO: */
- return 0;
+ time_t timer;
+ struct tm tm_timer;
+
+ time(&timer);
+ gmtime_r(&timer, &tm_timer);
+
+ return fatfs_time(&tm_timer);
}
@@ -121,11 +129,11 @@ command_ret_t do_fat_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg
(void) cmdtp; (void) flag; (void) argc;
FatFs = (FATFS *) malloc(sizeof (FATFS));
- buffer = (char *) malloc(512);
+ buffer = (char *) malloc(BUFFER_SIZE);
if (FatFs == NULL || buffer == NULL) {
printf_P(PSTR("fatstat: Out of Memory!\n"));
- free(FatFs);
free(buffer);
+ free(FatFs);
return CMD_RET_FAILURE;
}
@@ -177,6 +185,8 @@ command_ret_t do_fat_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const arg
}
f_mount(NULL, argv[1], 0);
+ free(buffer);
+ free(FatFs);
if (res) {
put_rc(res);
return CMD_RET_FAILURE;
@@ -223,21 +233,18 @@ command_ret_t do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
} else {
s1++; p1 += Finfo.fsize;
}
- printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s"),
+ printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu "),
(Finfo.fattrib & AM_DIR) ? 'D' : '-',
(Finfo.fattrib & AM_RDO) ? 'R' : '-',
(Finfo.fattrib & AM_HID) ? 'H' : '-',
(Finfo.fattrib & AM_SYS) ? 'S' : '-',
(Finfo.fattrib & AM_ARC) ? 'A' : '-',
(Finfo.fdate >> 9) + 1980, (Finfo.fdate >> 5) & 15, Finfo.fdate & 31,
- (Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63,
- Finfo.fsize, &(Finfo.fname[0]));
+ (Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63, Finfo.fsize);
#if _USE_LFN
- for (int i = strlen(Finfo.fname); i < 14; i++)
- putchar(' ');
- printf_P(PSTR("%s\n"), Lfname);
+ printf_P(PSTR("%s\n"), *Lfname ? Lfname : Finfo.fname);
#else
- putchar('\n');
+ printf_P(PSTR("%s\n"), Finfo.fname);
#endif
}
@@ -257,163 +264,13 @@ command_ret_t do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
/*
- * fatread - load binary file from a dos filesystem
- * <d:/path/filename> <addr> [bytes [pos]]
- */
-command_ret_t do_fat_read(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
- FATFS FatFs;
- FIL File;
- unsigned long bytes = 0x80000;
- unsigned long pos= 0;
- unsigned long bytes_read;
- uint32_t addr;
- FRESULT res;
- bool buserr = 0;
- uint32_t timer;
- uint8_t buffer[512];
-
- (void) cmdtp; (void) flag;
-
- if (argc < 3)
- return CMD_RET_USAGE;
-
- addr = strtoul(argv[2], 0, 16);
- if (argc > 3)
- bytes = strtoul(argv[3], 0, 16);
- if (argc > 4)
- pos = strtoul(argv[4], 0, 16);
-
- res = f_mount(&FatFs, argv[1], 0);
- if (!res) {
- res = f_open(&File, argv[1], FA_READ);
-
- if (!res) {
- res = f_lseek(&File, pos);
- if (!res) {
- bytes_read = 0;
- timer = get_timer(0);
- while (bytes) {
- unsigned int cnt, br;
-
- if (bytes >= sizeof buffer) {
- cnt = sizeof buffer;
- bytes -= sizeof buffer;
- } else {
- cnt = bytes; bytes = 0;
- }
- res = f_read(&File, buffer, cnt, &br);
- if (res != FR_OK)
- break;
- if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
- buserr = 1;
- break;
- }
- z80_write_block(buffer, addr, br);
- z80_bus_cmd(Release);
- addr += br;
-
- bytes_read += br;
- if (cnt != br)
- break;
- }
- timer = get_timer(timer);
- printf_P(PSTR("%lu bytes read with %lu bytes/sec.\n"),
- bytes_read, timer ? (bytes_read * 1000 / timer) : 0);
- }
- }
- f_mount(NULL, argv[1], 0);
- }
-
- if (buserr)
- my_puts_P(PSTR("Bus timeout\n"));
- if (res)
- put_rc(res);
- if (buserr || res)
- return CMD_RET_FAILURE;
-
- return CMD_RET_SUCCESS;
-}
-
-/*
- * fatwrite - write file into a dos filesystem
- * <d:/path/filename> <addr> <bytes>
- */
-command_ret_t do_fat_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
- FATFS FatFs;
- FIL File;
- unsigned long bytes;
- unsigned long bytes_written;
- uint32_t addr;
- FRESULT res;
- bool buserr = 0;
- uint32_t timer;
- uint8_t buffer[512];
-
- (void) cmdtp; (void) flag;
-
- if (argc < 4)
- return CMD_RET_USAGE;
-
- addr = strtoul(argv[2], 0, 16);
- bytes = strtoul(argv[3], 0, 16);
-
- res = f_mount(&FatFs, argv[1], 0);
- if (!res) {
- res = f_open(&File, argv[1], FA_CREATE_ALWAYS | FA_WRITE);
- if (!res) {
- bytes_written = 0;
- timer = get_timer(0);
- while (bytes) {
- unsigned int cnt, br;
-
- if (bytes >= sizeof buffer) {
- cnt = sizeof buffer;
- bytes -= sizeof buffer;
- } else {
- cnt = bytes; bytes = 0;
- }
- if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
- buserr = 1;
- break;
- }
- z80_read_block(buffer, addr, cnt);
- z80_bus_cmd(Release);
- res = f_write(&File, buffer, cnt, &br);
- if (res != FR_OK)
- break;
- addr += br;
- bytes_written += br;
- if (cnt != br)
- break;
- }
- res = f_close(&File);
- timer = get_timer(timer);
- printf_P(PSTR("%lu bytes written with %lu bytes/sec.\n"),
- bytes_written, timer ? (bytes_written * 1000 / timer) : 0);
- }
- f_mount(NULL, argv[1], 0);
- }
-
- if (buserr)
- my_puts_P(PSTR("Bus timeout\n"));
- if (res)
- put_rc(res);
- if (buserr || res)
- return CMD_RET_FAILURE;
-
- return CMD_RET_SUCCESS;
-}
-
-/*
* fatread/write - load binary file to/from a dos filesystem
* read <d:/path/filename> <addr> [bytes [pos]]
* write <d:/path/filename> <addr> <bytes>
*/
command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
- FATFS FatFs;
+ FATFS *FatFs;
FIL File;
uint32_t addr;
unsigned long bytes;
@@ -424,7 +281,7 @@ command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
FRESULT res;
bool buserr = 0;
uint32_t timer;
- uint8_t buffer[512];
+ uint8_t *buffer;
(void) cmdtp; (void) flag;
@@ -448,7 +305,16 @@ command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
if (addr + bytes > MAX_MEMORY)
bytes = MAX_MEMORY - addr;
- res = f_mount(&FatFs, argv[1], 0);
+ FatFs = (FATFS *) malloc(sizeof (FATFS));
+ buffer = (uint8_t *) malloc(BUFFER_SIZE);
+ if (FatFs == NULL || buffer == NULL) {
+ printf_P(PSTR("fatstat: Out of Memory!\n"));
+ free(FatFs);
+ free(buffer);
+ return CMD_RET_FAILURE;
+ }
+
+ res = f_mount(FatFs, argv[1], 0);
if (!res) {
res = f_open(&File, argv[1], dowrite ? FA_WRITE | FA_CREATE_ALWAYS
: FA_READ );
@@ -461,9 +327,9 @@ command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
while (bytes) {
unsigned int cnt, br;
- if (bytes >= sizeof buffer) {
- cnt = sizeof buffer;
- bytes -= sizeof buffer;
+ if (bytes >= BUFFER_SIZE) {
+ cnt = BUFFER_SIZE;
+ bytes -= BUFFER_SIZE;
} else {
cnt = bytes; bytes = 0;
}
@@ -513,6 +379,9 @@ command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
f_mount(NULL, argv[1], 0);
}
+ free(buffer);
+ free(FatFs);
+
if (buserr)
my_puts_P(PSTR("Bus timeout\n"));
if (res)
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/main.c b/avr/main.c
index 7e832bf..8acf265 100644
--- a/avr/main.c
+++ b/avr/main.c
@@ -20,6 +20,8 @@
#include "z180-serv.h"
#include "spi.h"
#include "gpio.h"
+#include "time.h"
+#include "rtc.h"
static uint8_t mcusr;
@@ -132,6 +134,16 @@ int reset_reason_is_power_on(void)
return (mcusr & _BV(PORF)) != 0;
}
+static
+void setup_system_time(void)
+{
+ struct tm rtc_time;
+
+ rtc_get(&rtc_time);
+ rtc_time.tm_isdst = 0;
+ set_system_time(mk_gmtime(&rtc_time) );
+}
+
/*--------------------------------------------------------------------------*/
/* Stored value of bootdelay, used by autoboot_command() */
@@ -252,6 +264,7 @@ int main(void)
#else
i2c_init(CONFIG_SYS_I2C_CLOCK);
#endif
+ setup_system_time();
printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n"));
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);
diff --git a/avr/timer.c b/avr/timer.c
index 7907bba..e222e68 100644
--- a/avr/timer.c
+++ b/avr/timer.c
@@ -4,6 +4,7 @@
#include "common.h"
#include <avr/interrupt.h>
#include <util/atomic.h>
+#include "time.h"
#include "timer.h"
/* timer interrupt/overflow counter */
@@ -18,7 +19,8 @@ uint32_t timestamp;
ISR(TIMER3_COMPA_vect)
{
static int_fast8_t tick_10ms;
- int_fast8_t i;
+ static int_fast8_t tick_1s;
+ int_fast8_t i, j;
extern void disk_timerproc(void);
@@ -26,11 +28,18 @@ ISR(TIMER3_COMPA_vect)
i = tick_10ms + 1;
if (i == 10) {
- i = 0;
Stat |= S_10MS_TO;
/* Drive timer procedure of low level disk I/O module */
disk_timerproc();
+
+ j = tick_1s - 1;
+ if (j == 0) {
+ system_tick();
+ j = 100;
+ }
+ tick_1s = j;
+ i = 0;
}
tick_10ms = i;
}