示例#1
0
void ds1302_check_rtc(void)
{
    uint8_t buffer[7];

    ds1302_read_clock(buffer, 7); /* read all calendar data */

    if(buffer[0] & 0x80){ /* is the clock halted? */
        kputs("ds1302: start clock\n");
        ds1302_write_seconds(buffer[0] & 0x7F); /* start it */
    }
}
示例#2
0
uint32_t ds1302_read_rtc(void)
{
    uint32_t ret;
    uint8_t buffer[7];
    uint8_t year, month, day, hour, minute, second;

    ds1302_read_clock(buffer, 7); /* read all calendar data */

    year   = uint8_from_bcd(buffer[6]);
    month  = uint8_from_bcd(buffer[4] & 0x1F);
    day    = uint8_from_bcd(buffer[3] & 0x3F);
    if(buffer[2] & 0x80) /* AM/PM 12-hour mode */
        hour   = uint8_from_bcd(buffer[2] & 0x1F) + (buffer[2] & 0x20 ? 12 : 0);
    else /* sensible 24-hour mode */
        hour   = uint8_from_bcd(buffer[2] & 0x3F);
    minute = uint8_from_bcd(buffer[1]);
    second = uint8_from_bcd(buffer[0] & 0x7F);

    if(buffer[0] & 0x80){ /* is the clock halted? */
        kputs("ds1302: start clock\n");
        ds1302_write_seconds(second); /* start it */
    }

    if(year < 70)
        year += 100;

    /* following code is based on utc_mktime() from ELKS 
       https://github.com/jbruchon/elks/blob/master/elkscmd/sh_utils/date.c */

    /* uses zero-based month index */
    month--;

    /* calculate days from years */
    ret = multiply_8x32(year - 70, 365);

    /* count leap days in preceding years */
    ret += ((year - 69) >> 2);

    /* calculate days from months */
    ret += mktime_moffset[month];

    /* add in this year's leap day, if any */
    if (((year & 3) == 0) && (month > 1)) {
        ret ++;
    }

    /* add in days in this month */
    ret += (day - 1);

    /* convert to hours */
    ret = multiply_8x32(24, ret);
    ret += hour;

    /* convert to minutes */
    ret = multiply_8x32(60, ret);
    ret += minute;

    /* convert to seconds */
    ret = multiply_8x32(60, ret);
    ret += second;

    /* return the result */
    return ret;
}