Ethernut Home Hardware Firmware Tools Download Community
 
 
Search | Legals | Deutsch

X1226 Realtime Clock And Calendar


Overview

Board version 3.0 is the first Ethernut design with an on-board realtime clock chip. In addition to maintaining time and calendar date, the design with Intersil's X1226 or X1286 offers the following features:

X1226 Internals

X12x6 API

There is currently no general API for RTC chips available. Thus, applications have to use the specific API for this chip.

More details about this and all other functions of the CY2239X API are available in the Nut/OS API Documentation.

The following snippet shows how to query an SNTP time server and set the RTC accordingly.

#include <dev/x12rtc.h>

/*
 * Query a time server and optionally update the hardware clock.
 */
int QueryTimeServer(void)
{
    int rc = -1;

    {
        time_t now;
        u_long timeserver = inet_addr(MYTIMED);

        /* Query network time service and set the system time. */
        printf("Query time from %s...", MYTIMED);
        if(NutSNTPGetTime(×erver, &now) == 0) {
            puts("OK");
            rc = 0;
            stime(&now);
            /* If RTC hardware is available, update it. */
            {
                struct _tm *gmt = gmtime(&now);

                if (X12RtcSetClock(gmt)) {
                    puts("RTC update failed");
                }
            }
        }
        else {
            puts("failed");
        }
    }
    return rc;
}

The next routine makes use of the previous one. It checks wether the RTC chip is up and running. If it is, then the system time will be set from the RTC values. Otherwise the previous routine is called to update the RTC.

#include <dev/x12rtc.h>

/*
 * Try to get initial date and time from the hardware clock or a time server.
 */
int InitTimeAndDate(void)
{
    int rc = -1;

    /* Set the local time zone. */
    _timezone = MYTZ * 60L * 60L;

    /* Query RTC hardware if available. */
    {
        u_long rs;

        /* Query the status. If it fails, we do not have an RTC. */
        if (X12RtcGetStatus(&rs)) {
            puts("No hardware RTC");
            rc = QueryTimeServer();
        }
        else {
            /* RTC hardware seems to be available. Check for power failure. */
            //rs = RTC_STATUS_PF;
            if ((rs & RTC_STATUS_PF) == RTC_STATUS_PF) {
                puts("RTC power fail detected");
                rc = QueryTimeServer();
            }

            /* RTC hardware status is fine, update our system clock. */
            else {
                struct _tm gmt;

                /* Assume that RTC is running at GMT. */
                if (X12RtcGetClock(&gmt) == 0) {
                    time_t now = _mkgmtime(&gmt);

                    if (now != -1) {
                        stime(&now);
                        rc = 0;
                    }
                }
            }
        }
    }
    return rc;
}