Skip to content

Commit ee6eec4

Browse files
committed
RTC has cheated the MCU
1 parent 5349a26 commit ee6eec4

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

qiling/hw/timer/stm32f4xx_rtc.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
#
55

66
import ctypes
7+
78
from qiling.hw.peripheral import QlPeripheral
9+
from qiling.hw.const.stm32f4xx_rtc import RTC_TR, RTC_ISR
10+
from qiling.hw.utils.bcd import byte2bcd
11+
812

13+
class Time:
14+
def __init__(self):
15+
pass
916

1017
class STM32F4xxRtc(QlPeripheral):
1118
class Type(ctypes.Structure):
@@ -99,5 +106,38 @@ def read(self, offset: int, size: int) -> int:
99106

100107
@QlPeripheral.debug_info()
101108
def write(self, offset: int, size: int, value: int):
109+
if offset == self.struct.ISR.offset:
110+
for bitmask in [
111+
RTC_ISR.TAMP1F,
112+
RTC_ISR.TSOVF,
113+
RTC_ISR.TSF,
114+
RTC_ISR.WUTF,
115+
RTC_ISR.ALRBF,
116+
RTC_ISR.ALRAF,
117+
RTC_ISR.RSF
118+
]:
119+
if value & bitmask == 0:
120+
self.rtc.ISR &= ~bitmask
121+
122+
self.rtc.ISR = (self.rtc.ISR & ~RTC_ISR.INIT) | (value & RTC_ISR.INIT)
123+
return
124+
102125
data = (value).to_bytes(size, 'little')
103-
ctypes.memmove(ctypes.addressof(self.rtc) + offset, data, size)
126+
ctypes.memmove(ctypes.addressof(self.rtc) + offset, data, size)
127+
128+
def set_time(self, hour=0, minute=0, second=0, time_format=0):
129+
hour = (byte2bcd(hour) << 16) & (RTC_TR.HT | RTC_TR.HU)
130+
minute = (byte2bcd(minute) << 8) & (RTC_TR.MNT | RTC_TR.MNU)
131+
second = byte2bcd(second) & (RTC_TR.ST | RTC_TR.SU)
132+
time_format = (time_format << 16) & RTC_TR.PM
133+
134+
self.rtc.TR = hour | minute | second | time_format
135+
136+
def get_time(self):
137+
pass
138+
139+
def step(self):
140+
if self.rtc.ISR & RTC_ISR.INIT:
141+
self.rtc.ISR |= RTC_ISR.INITF
142+
143+
self.rtc.ISR |= RTC_ISR.RSF

qiling/hw/utils/bcd.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
7+
def byte2bcd(value):
8+
bcdhigh = 0
9+
while value >= 10:
10+
bcdhigh += 1
11+
value -= 10
12+
13+
return (bcdhigh << 4) | value

0 commit comments

Comments
 (0)