1
0
Files
TEF6686_Driver/rtc.py
2026-02-18 16:22:25 +01:00

32 lines
1.0 KiB
Python

import datetime
from protocol import I2CPCClient
def sync_rtc(p: I2CPCClient):
TIMEOFFSET_ADDR = 2275
dtime = datetime.datetime.now(datetime.UTC)
utc_off = d.total_seconds() / 3600 if (d := datetime.datetime.now().astimezone().utcoffset()) else 0
if(p.version()[-1] >= 2): p.write_eeprom(TIMEOFFSET_ADDR, int(utc_off).to_bytes(1, signed=True))
else: dtime = datetime.datetime.now()
ADDRESS = 0x32
p.write_i2c(ADDRESS, b"\x1f\x40") # halt the clock
def toBCD(val: int) -> int:
return ((val // 10) << 4) | (val % 10)
data = b"\x10"
data += toBCD(dtime.second).to_bytes(1)
data += toBCD(dtime.minute).to_bytes(1)
data += toBCD(dtime.hour).to_bytes(1)
data += toBCD(1 << dtime.weekday()).to_bytes(1)
data += toBCD(dtime.day).to_bytes(1)
data += toBCD(dtime.month).to_bytes(1)
data += toBCD((1900 + dtime.year - 26) % 100).to_bytes(1)
p.write_i2c(ADDRESS, data)
p.write_i2c(ADDRESS, b"\x1f\x00") # start the clock
p = I2CPCClient("COM17")
sync_rtc(p)
p.reboot()
p.close()