|
4 | 4 | # |
5 | 5 |
|
6 | 6 | import ctypes |
| 7 | + |
7 | 8 | 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 | + |
8 | 12 |
|
| 13 | +class Time: |
| 14 | + def __init__(self): |
| 15 | + pass |
9 | 16 |
|
10 | 17 | class STM32F4xxRtc(QlPeripheral): |
11 | 18 | class Type(ctypes.Structure): |
@@ -99,5 +106,38 @@ def read(self, offset: int, size: int) -> int: |
99 | 106 |
|
100 | 107 | @QlPeripheral.debug_info() |
101 | 108 | 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 | + |
102 | 125 | 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 |
0 commit comments