uint64_t pmu_get_epoch() { //TODO: implement return 0; if (0) { //TODO: remove me (make stuff compile) bcd_to_int(1); } /* int i; int days; int years; uint64_t secs; int32_t offset; uint8_t rtc_data[PMU_RTCYR - PMU_RTCSC + 1]; uint8_t rtc_data2[PMU_RTCYR - PMU_RTCSC + 1]; do { pmu_get_regs(PMU_RTCSC, rtc_data, PMU_RTCYR - PMU_RTCSC + 1); pmu_get_regs(PMU_RTCSC, rtc_data2, PMU_RTCYR - PMU_RTCSC + 1); } while(rtc_data2[0] != rtc_data[0]); secs = bcd_to_int(rtc_data[0] & PMU_RTCSC_MASK); secs += 60 * bcd_to_int(rtc_data[PMU_RTCMN - PMU_RTCSC] & PMU_RTCMN_MASK); secs += 3600 * bcd_to_int(rtc_data[PMU_RTCHR - PMU_RTCSC] & PMU_RTCHR_MASK); days = bcd_to_int(rtc_data[PMU_RTCDT - PMU_RTCSC] & PMU_RTCDT_MASK) - 1; years = 2000 + bcd_to_int(rtc_data[PMU_RTCYR - PMU_RTCSC] & PMU_RTCYR_MASK); for(i = 1970; i < years; ++i) { if((i & 0x3) != 0) days += 365; // non-leap year else days += 366; // leap year } if((years & 0x3) != 0) days += days_in_months[(rtc_data[PMU_RTCMT - PMU_RTCSC] & PMU_RTCMT_MASK) - 1]; else days += days_in_months_leap_year[(rtc_data[PMU_RTCMT - PMU_RTCSC] & PMU_RTCMT_MASK) - 1]; secs += ((uint64_t)days) * 86400; pmu_get_regs(0x6B, (uint8_t*) &offset, sizeof(offset)); secs += offset; return secs; */ }
/* set hours and returns new latched value 0 - 23 */ time_t rtc_set_latched_hour(int hours, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int real_hours = (bcd) ? bcd_to_int(hours) : hours; /* sanity check */ if (real_hours < 0 || real_hours > 23) { return latch; } local->tm_hour = real_hours; offset_now = mktime(local); return offset_now; }
/* set month and returns new latched value 0 - 11 */ time_t rtc_set_latched_month(int month, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int real_month = (bcd) ? bcd_to_int(month) : month; /* sanity check */ if (real_month < 0 || real_month > 11) { return latch; } local->tm_mon = real_month; offset_now = mktime(local); return offset_now; }
/* set seconds and returns new latched value 0 - 59 */ time_t rtc_set_latched_second(int seconds, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int real_seconds = (bcd) ? bcd_to_int(seconds) : seconds; /* sanity check and disregard setting of leap seconds */ if (real_seconds < 0 || real_seconds > 59) { return latch; } local->tm_sec = real_seconds; offset_now = mktime(local); return offset_now; }
/* set minutes and returns new latched value 0 - 59 */ time_t rtc_set_latched_minute(int minutes, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int real_minutes = (bcd) ? bcd_to_int(minutes) : minutes; /* sanity check */ if (real_minutes < 0 || real_minutes > 59) { return latch; } local->tm_min = real_minutes; offset_now = mktime(local); return offset_now; }
/* set month and returns new offset 0 - 11 */ time_t rtc_set_month(int month, time_t offset, int bcd) { time_t now = time(NULL) + offset; struct tm *local = localtime(&now); time_t offset_now; int real_month = (bcd) ? bcd_to_int(month) : month; /* sanity check */ if (real_month < 0 || real_month > 11) { return offset; } local->tm_mon = real_month; offset_now = mktime(local); return offset + (offset_now - now); }
/* set hours and returns new offset 0 - 23 */ time_t rtc_set_hour(int hours, time_t offset, int bcd) { time_t now = time(NULL) + offset; struct tm *local = localtime(&now); time_t offset_now; int real_hours = (bcd) ? bcd_to_int(hours) : hours; /* sanity check */ if (real_hours < 0 || real_hours > 23) { return offset; } local->tm_hour = real_hours; offset_now = mktime(local); return offset + (offset_now - now); }
/* set minutes and returns new offset 0 - 59 */ time_t rtc_set_minute(int minutes, time_t offset, int bcd) { time_t now = time(NULL) + offset; struct tm *local = localtime(&now); time_t offset_now; int real_minutes = (bcd) ? bcd_to_int(minutes) : minutes; /* sanity check */ if (real_minutes < 0 || real_minutes > 59) { return offset; } local->tm_min = real_minutes; offset_now = mktime(local); return offset + (offset_now - now); }
/* set day of month and returns new latched value 1 - 31 */ time_t rtc_set_latched_day_of_month(int day, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int is_leap_year = 0; int year = local->tm_year + 1900; int real_day = (bcd) ? bcd_to_int(day) : day; /* sanity check */ if (((year % 4) == 0) && ((year % 100) != 0)) { is_leap_year = 1; } if (((year % 4) == 0) & ((year % 100) == 0) && ((year % 400) != 0)) { is_leap_year = 1; } switch (local->tm_mon) { case RTC_MONTH_JAN: case RTC_MONTH_MAR: case RTC_MONTH_MAY: case RTC_MONTH_JUL: case RTC_MONTH_AUG: case RTC_MONTH_OCT: case RTC_MONTH_DEC: if (real_day < 1 || real_day > 31) { return latch; } break; case RTC_MONTH_APR: case RTC_MONTH_JUN: case RTC_MONTH_SEP: case RTC_MONTH_NOV: if (real_day < 1 || real_day > 30) { return latch; } break; case RTC_MONTH_FEB: if (real_day < 1 || real_day > (28 + is_leap_year)) { return latch; } break; } local->tm_mday = real_day; offset_now = mktime(local); return offset_now; }
/* set century and returns new latched value 19 - 20 */ time_t rtc_set_latched_century(int century, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int real_century = (bcd) ? bcd_to_int(century) : century; /* sanity check */ if (real_century != 19 && real_century != 20) { return latch; } local->tm_year %= 100; local->tm_year += ((real_century - 19) * 100); offset_now = mktime(local); return offset_now; }
/* set years and returns new latched value 0 - 99 */ time_t rtc_set_latched_year(int year, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int real_year = (bcd) ? bcd_to_int(year) : year; /* sanity check */ if (real_year < 0 || real_year > 99) { return latch; } local->tm_year = (local->tm_year / 100) * 100; local->tm_year += real_year; offset_now = mktime(local); return offset_now; }
/* set years and returns new offset 0 - 99 */ time_t rtc_set_year(int year, time_t offset, int bcd) { time_t now = time(NULL) + offset; struct tm *local = localtime(&now); time_t offset_now; int real_year = (bcd) ? bcd_to_int(year) : year; /* sanity check */ if (real_year < 0 || real_year > 99) { return offset; } local->tm_year = (local->tm_year / 100) * 100; local->tm_year += real_year; offset_now = mktime(local); return offset + (offset_now - now); }
/* set day of month and returns new latched value 0 - 31 */ time_t rtc_set_latched_day_of_month(int day, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int is_leap_year = 0; int year = local->tm_year + 1900; int real_day = (bcd) ? bcd_to_int(day) : day; /* sanity check */ if (((year % 4) == 0) && ((year % 100) != 0)) { is_leap_year = 1; } if (((year % 4) == 0) & ((year % 100) == 0) && ((year % 400) != 0)) { is_leap_year = 1; } switch (local->tm_mon) { case 0: case 2: case 4: case 6: case 7: case 9: case 11: if (real_day < 0 || real_day > 31) { return latch; } case 3: case 5: case 8: case 10: if (real_day < 0 || real_day > 30) { return latch; } case 1: if (real_day < 0 || real_day > (28 + is_leap_year)) { return latch; } } local->tm_mday = real_day; offset_now = mktime(local); return offset_now; }
/* set hours and returns new offset 0 - 23 */ time_t rtc_set_latched_hour_am_pm(int hours, time_t latch, int bcd) { time_t now = latch; struct tm *local = localtime(&now); time_t offset_now; int real_hours = (bcd) ? bcd_to_int(hours & 0x1f) : hours & 0x1f; int pm = (hours & 0x20) >> 5; if (real_hours == 12 && !pm) { real_hours = 0; } else if (real_hours == 12 && pm) { } else { real_hours += 12; } /* sanity check */ if (real_hours < 0 || real_hours > 23) { return latch; } local->tm_hour = real_hours; offset_now = mktime(local); return offset_now; }