20 #define RTC_CMOS_PORT 0x70
21 #define RTC_DATA_PORT 0x71
22 #define RTC_CURRENT_YEAR 2021 // Needs to be updated every year!
23 #define RTC_CURRENT_CENTURY 0 // Needs to be updated every century!
25 static void callback(
struct registers* regs);
26 static bool getUpdateInProgress();
27 static uint8_t getRegister(uint8_t reg);
57 static void callback(
struct registers* regs)
62 static bool getUpdateInProgress()
68 static uint8_t getRegister(uint8_t reg)
85 uint32_t last_second = 0, last_minute = 0,
86 last_hour = 0, last_day = 0,
87 last_month = 0, last_year = 0,
88 last_century = 0, registerB = 0;
90 while (getUpdateInProgress());
92 second = getRegister(0x00);
93 minute = getRegister(0x02);
94 hour = getRegister(0x04);
95 day = getRegister(0x07);
96 month = getRegister(0x08);
97 year = getRegister(0x09);
100 while ((last_second !=
second) || (last_minute !=
minute) ||
101 (last_hour !=
hour) || (last_day !=
day) ||
102 (last_month !=
month) || (last_year !=
year) ||
114 while (getUpdateInProgress());
116 second = getRegister(0x00);
117 minute = getRegister(0x02);
118 hour = getRegister(0x04);
119 day = getRegister(0x07);
120 month = getRegister(0x08);
121 year = getRegister(0x09);
127 registerB = getRegister(0x0B);
130 if (!(registerB & 0x04)) {
144 if (!(registerB & 0x02) && (
hour & 0x80)) {
175 static uint64_t getJulianDay(uint8_t days, uint8_t months, uint16_t years)
177 int a = (14 - months) / 12;
178 int y = years + 4800 - a;
179 int m = months + 12 * a - 3;
181 int jdn = days + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
185 static uint64_t getUnixEpoch(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t days, uint8_t months, uint16_t years)
187 uint64_t jdnNow = getJulianDay(days, months, years);
188 uint64_t jdn1970 = getJulianDay(1, 1, 1970);
189 uint64_t jdnDiff = jdnNow - jdn1970;
190 return (jdnDiff * (60 * 60 * 24)) + hours * 3600 + minutes * 60 + seconds;