]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
RTC: Handle turn of the year correctly, detect uninitialized RTC (loss of VBAT)
authorLeo C <erbl259-lmu@yahoo.de>
Tue, 26 Jan 2016 10:29:58 +0000 (11:29 +0100)
committerLeo C <erbl259-lmu@yahoo.de>
Tue, 26 Jan 2016 10:29:58 +0000 (11:29 +0100)
avr/cmd_date.c
avr/command_tbl.c
avr/pcf8583.c
avr/z180-serv.c

index efba8584f9790e21160284b5323b3e961b096072..3e2e01680513df316623da108b4112761cf67a29 100644 (file)
@@ -129,16 +129,18 @@ 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;
+       int rc;
+       command_ret_t rcode = CMD_RET_FAILURE;
 
        (void) cmdtp; (void) flag;
 
        switch (argc) {
        case 2:                 /* set date & time */
                /* initialize t with current time */
-               rcode = rtc_get (&t);
-
-               if(!rcode) {
+               if(rtc_get(&t) < 0) {
+                       my_puts_P(PSTR("## Get date failed\n"));
+                       break;
+               } else {
                        /* insert new date & time */
                        if (mk_date (argv[1], &t) != 0) {
                                my_puts_P(PSTR("## Bad date format\n"));
@@ -150,22 +152,24 @@ command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                        gmtime_r(&time, &t);
 
                        /* and write to RTC */
-                       rcode = rtc_set (&t);
-                       if(rcode)
+                       if(rtc_set(&t) < 0) {
                                my_puts_P(PSTR("## Set date failed\n"));
-               } else {
-                       my_puts_P(PSTR("## Get date failed\n"));
+                               break;
+                       }
                }
                /* FALL TROUGH */
        case 1:                 /* get date & time */
-               rcode = rtc_get (&t);
-
-               if (rcode) {
+               rc = rtc_get(&t);
+               if (rc >= 0) {
+                       asctime_r(&t, buf);
+                       printf_P(PSTR("%s"), buf);
+                       if (rc == 1)
+                               printf_P(PSTR("  (Invalid)"));
+                       putchar('\n');
+                       rcode = CMD_RET_SUCCESS;
+               } else {
                        my_puts_P(PSTR("## Get date failed\n"));
-                       break;
                }
-               asctime_r(&t, buf);
-               printf_P(PSTR("%s\n"), buf);
                break;
 
        default:
index 768e6f05faf6c2b0671efa52602db12d4e1f4433..748c830fe00b1cbf801d84641ded36e21e0a51d5 100644 (file)
@@ -50,7 +50,7 @@ cmd_tbl_t cmd_tbl[] = {
 CMD_TBL_ITEM(
        date,   2,      1,      do_date,
        "get/set date & time",
-       "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
+       "[MMDDhhmm[[CC]YY][.ss]]\n"
        "  - without arguments: print date & time\n"
        "  - with numeric argument: set the system date & time\n"
 ),
index 169e4e937baa4ad7aee475316c6332c0ae86cb94..bdb779f0c4f1bd5137c3f4f854fa262a0c077a48 100644 (file)
@@ -48,14 +48,13 @@ static uint8_t bin2bcd (uint_fast8_t val)
 
 int rtc_get (struct tm *tmp)
 {
-       int rel = 0;
        uint8_t rtcbuf[NR_OF_REGS];
        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);
 
-       debug_rtc("Get RTC year: %u, year/date: %02x, wdays/month: %02x, "
+       debug_rtc("Get RTC year: %u, wdays/month: %02x, year/date: %02x, "
                "hour: %02x, min: %02x, sec: %02x, (stat: %02x)\n", year,
                rtcbuf[6], rtcbuf[5], rtcbuf[4], rtcbuf[3], rtcbuf[2], rtcbuf[0]);
 
@@ -64,26 +63,26 @@ int rtc_get (struct tm *tmp)
        tmp->tm_hour = bcd2bin (rtcbuf[REG_HOUR]   & 0x3F);
        tmp->tm_mday = bcd2bin (rtcbuf[REG_YRDATE] & 0x3F);
        tmp->tm_mon  = bcd2bin (rtcbuf[REG_WDMON]  & 0x1F) - 1;
-       while (year%4 < (rtcbuf[REG_YRDATE]>>6)) {
+       while (year%4 != (rtcbuf[REG_YRDATE]>>6)) {
                year++;
-               /* TODO: update RTC ram */
+               i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2);
        }
-       tmp->tm_year  = year;
+       tmp->tm_year  = year - 1900;
        tmp->tm_wday = rtcbuf[REG_WDMON] >> 5;
        tmp->tm_yday = 0;
        tmp->tm_isdst= 0;
 
-
        debug_rtc( "Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
                tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
 
-       return rel;
+       return rtcbuf[REG_CS] == 0x04 ? 0 : 1;
 }
 
 int rtc_set (struct tm *tmp)
 {
        uint8_t rtcbuf[NR_OF_REGS];
+       int16_t year = tmp->tm_year + 1900;
 
        debug_rtc("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
                tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
@@ -92,16 +91,15 @@ int rtc_set (struct tm *tmp)
        rtcbuf[REG_CS] = 0x84;
        rtcbuf[REG_CSEC] = 0x00;
        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_YRDATE] = ((year % 4) << 6) | bin2bcd(tmp->tm_mday);
        rtcbuf[REG_HOUR  ] = bin2bcd(tmp->tm_hour);
        rtcbuf[REG_MIN   ] = bin2bcd(tmp->tm_min);
        rtcbuf[REG_SEC   ] = bin2bcd(tmp->tm_sec);
 
        i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0,    1, rtcbuf, NR_OF_REGS);
-       i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &tmp->tm_year, 2);
+       i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2);
        rtcbuf[REG_CS] = 0x04;
        i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, 1);
 
-
        return 0;
 }
index 69bcd9c609286d88e248927b73b334f8ed4f6c05..a815537ea0d9fdc0b8b92cfe7d8bcf749a05f395 100644 (file)
@@ -167,7 +167,7 @@ void do_msg_get_set_time(uint8_t subf, int len, uint8_t * msg)
                /* initialize t with current time */
                rc = rtc_get (&t);
 
-               if (rc == 0) {
+               if (rc >= 0) {
                        /* insert new date & time */
                        if (mk_date_time (len, msg, &t) != 0) {
                                my_puts_P(PSTR("## set_time: Bad date format\n"));
@@ -188,19 +188,16 @@ void do_msg_get_set_time(uint8_t subf, int len, uint8_t * msg)
                /* FALL TROUGH */
        case 2:                 /* get date & time */
                rc = rtc_get (&t);
+               if (rc >= 0) {
+                       time_t time;
+                       time = mk_gmtime(&t);
+                       //mktime(&t);
+                       gmtime_r(&time, &t);
 
-               if (rc) {
+                       mk_cpm_time(&t, cpm_time);
+               } else {
                        my_puts_P(PSTR("## get_time: Get date failed\n"));
-                       break;
                }
-
-               time_t time;
-               time = mk_gmtime(&t);
-               //mktime(&t);
-               gmtime_r(&time, &t);
-
-
-               mk_cpm_time(&t, cpm_time);
                break;
        }