[En-Nut-Discussion] Always getting the same time using time() call

Ole Reinhardt ole.reinhardt at embedded-it.de
Wed Jul 9 11:33:58 CEST 2008


Hi all,

I have a strange problem with an application on an ethernut3 board.

I use the X12 RTC of the board as a backup solution if no timeserver is
available. So I first try to get a valid time from a time server. If
this fails, I read back the time from the rtc.

The RTC time is updated always if I have a valid time from timeserver.

Now, the rtc detects a power fail from time to time, if I let the board
without power for some time. Thats ok...

Next time booting the board my software detects the power failure and
the internal system timer is automaticaly set to 30.11.1999 at 01:00:00

The _timezone varaiable is set to -1 * 60 * 60 before quering the
timeserver or rtc.

Up to this point everything seems ok. I can live with a wrong date, nut
now my problem: The time() call always returns the _same_ time:
30.11.1999 at 01:00:00

So what's going wrong here?

Here is my code to query the timeserver / rtc. Keep in mind that I set
the _timezone variable before.


static int QueryTimeServer(void)
{
    int rc = -1;
    {
        time_t now;

        /* Query network time service and set the system time. */
        INFO("Query time from %s...",
inet_ntoa(sysconfig->network.timeserver));
       
        if (NutSNTPGetTime(&sysconfig->network.timeserver, &now) == 0) {
            rc = 0;
            timeserver_config_failed = FALSE;
            stime(&now);
        } else {
            NutSleep(200);
            if (NutSNTPGetTime(&sysconfig->network.timeserver, &now) ==
0) {
                rc = 0;
                stime(&now);
                timeserver_config_failed = FALSE;
                /* If RTC hardware is available, update it. */
                {
                    tm *gmt = gmtime(&now);

                    if (NutRtcSetTime(gmt)) {
                        ERROR("RTC update failed");
                    }
                }
                INFO("\r\n");
            } else {
                u_long rs;
                INFO("failed\r\n");
                /* Query the status. If it fails, we do not have an RTC.
*/
                if (NutRtcGetStatus(&rs)) {
                    ERROR("No hardware RTC found! ");
                } else {
                    /* RTC hardware seems to be available. Check for
power failure. */
                    if ((rs & RTC_STATUS_PF) == RTC_STATUS_PF) {
                        ERROR("RTC power fail detected! ");
                    } else {
                        /* RTC hardware status is fine, update our
system clock. */
                        tm *gmt;
                        gmt = NutHeapAlloc(sizeof(tm));

                        /* Assume that RTC is running at GMT. */
                        if (NutRtcGetTime(gmt) == 0) {
                            
                            INFO("Time read from rtc (gmt): %02d.%02d.%
04d - %02d:%02d:%02d\r\n", gmt->tm_mday, gmt->tm_mon+1, gmt->tm_year
+1900, gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
                            
                            time_t now = _mkgmtime(gmt);

                            if (now != -1) {
                                stime(&now);
                            }
                        }
                        NutHeapFree(gmt);
                    }
                }
                
                timeserver_config_failed = TRUE;
            }
        }
    }
    return rc;
}

-- 
 _____________________________________________________________
|                                                             |
| Embedded-IT          Hard- und Softwarelösungen             |
|                                                             |
| Ole Reinhardt        Tel. / Fax:        +49 (0)271  7420433 |
| Luisenstraße 29      Mobil:             +49 (0)177  7420433 |
| 57076 Siegen         eMail:    ole.reinhardt at embedded-it.de |
| Germany              Web:         http://www.embedded-it.de |
|                      UstID / VAT:       DE198944716         |
|_____________________________________________________________|




More information about the En-Nut-Discussion mailing list