Skip to content

Commit

Permalink
refactor: better way to implement offset extra calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
hank9999 committed Feb 23, 2024
1 parent 27f69b2 commit 46604b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
20 changes: 7 additions & 13 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ def todo_function():
return


def check_eeprom_writeable(serial_port: serial.Serial, offset: int, extra: int) -> bool:
def check_eeprom_writeable(serial_port: serial.Serial, addr: int) -> bool:
# 读取原始数据
read_data = serial_utils.read_extra_eeprom(serial_port, offset, extra, 8)
read_data = serial_utils.read_extra_eeprom(serial_port, addr, 8)
# 写入随机数据
random_bytes = bytes([random.randint(0, 255) for _ in range(8)])
serial_utils.write_extra_eeprom(serial_port, offset, extra, random_bytes)
serial_utils.write_extra_eeprom(serial_port, addr, random_bytes)
# 读取写入的数据
read_write_data = serial_utils.read_extra_eeprom(serial_port, offset, extra, 8)
read_write_data = serial_utils.read_extra_eeprom(serial_port, addr, 8)
# 恢复原始数据
serial_utils.write_extra_eeprom(serial_port, offset, extra, read_data)
serial_utils.write_extra_eeprom(serial_port, addr, read_data)

return read_write_data == random_bytes

Expand Down Expand Up @@ -114,7 +114,6 @@ def write_data(serial_port: Serial, start_addr: int, data: Union[bytes, List[int
total_page = data_len // 128
addr = start_addr
current_step = 0
offset = 0
while addr < start_addr + data_len:
percent_float = (current_step / total_page) * 100
percent = int(percent_float)
Expand All @@ -127,9 +126,7 @@ def write_data(serial_port: Serial, start_addr: int, data: Union[bytes, List[int
if start_addr + data_len < 0x10000:
serial_utils.write_eeprom(serial_port, addr, writing_data)
else:
if addr - offset * 0x10000 >= 0x10000:
offset += 1
serial_utils.write_extra_eeprom(serial_port, offset, addr - offset * 0x10000, writing_data)
serial_utils.write_extra_eeprom(serial_port, addr, writing_data)
addr += step
current_step += 1
progress['value'] = 0
Expand Down Expand Up @@ -672,17 +669,14 @@ def backup_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressba
total_steps = (target_eeprom_offset - start_addr) // 128 # 计算总步数
current_step = 0
addr = start_addr
offset = 0

backup_data = b''

while addr < target_eeprom_offset: # 限制地址范围为start_addr到target_eeprom_offset
if target_eeprom_offset < 0x10000:
read_data = serial_utils.read_eeprom(serial_port, addr, 128)
else:
if addr - offset * 0x10000 >= 0x10000:
offset += 1
read_data = serial_utils.read_extra_eeprom(serial_port, offset, addr - offset * 0x10000, 128)
read_data = serial_utils.read_extra_eeprom(serial_port, addr, 128)
backup_data += read_data
addr += 128
current_step += 1
Expand Down
15 changes: 7 additions & 8 deletions serial_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ def read_eeprom(serial_port: serial.Serial, offset: int, length: int):
return o[8:]


def read_extra_eeprom(serial_port: serial.Serial, offset: int, extra: int, length: int):
if extra > 0xFFFF:
offset = extra >> 16
extra = extra & 0xFFFF
def read_extra_eeprom(serial_port: serial.Serial, addr: int, length: int):
offset = addr >> 16
extra = addr & 0xFFFF
extra = struct.pack("<H", extra)
extra_bytes = struct.pack("<H", extra)
read_mem = b"\x2b\x05\x08\x00" + \
struct.pack("<HBB", offset, length, 0) + \
Expand Down Expand Up @@ -135,10 +135,9 @@ def write_eeprom(serial_port: serial.Serial, offset: int, data: bytes):
raise Exception("写入前8KiB EEPROM响应错误!")


def write_extra_eeprom(serial_port: serial.Serial, offset: int, extra: int, data: bytes):
if extra > 0xFFFF:
offset = extra >> 16
extra = extra & 0xFFFF
def write_extra_eeprom(serial_port: serial.Serial, addr: int, data: bytes):
offset = addr >> 16
extra = addr & 0xFFFF
extra = struct.pack("<H", extra)
length = len(data) + len(extra)

Expand Down

0 comments on commit 46604b0

Please sign in to comment.