From fbdc6d89df4c0b5891f5060ed09f5e405ef60edb Mon Sep 17 00:00:00 2001 From: Fabio Battaglia Date: Mon, 2 Sep 2024 16:34:35 +0200 Subject: [PATCH] current implementation of cxfer reading --- src/dpdumper/hl_board_utilities.py | 48 ++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/dpdumper/hl_board_utilities.py b/src/dpdumper/hl_board_utilities.py index 09762f2..b6b9163 100644 --- a/src/dpdumper/hl_board_utilities.py +++ b/src/dpdumper/hl_board_utilities.py @@ -1,11 +1,11 @@ """This module contains high level utility code to perform operations on the board""" -from typing import Generator, final, NamedTuple +from typing import Generator, List, final, NamedTuple +import logging import serial -import math -import logging +import dupicolib.utils as DPLibUtils from dupicolib.hardware_board_commands import HardwareBoardCommands from dpdumperlib.ic.ic_definition import ICDefinition @@ -59,6 +59,48 @@ class HLBoardUtilities: @classmethod def read_ic(cls, ser: serial.Serial, cmd_class: type[HardwareBoardCommands], ic: ICDefinition, check_hiz: bool = False) -> list[DataElement] | None: + read_data: list[DataElement] = [] + addr_combs: int = 1 << len(ic.address) # Calculate the number of addresses + data_width: int = -(len(ic.data) // -8) + dump_size: int = addr_combs * data_width + hi_pins: list[int] = list(set(ic.act_h_enable + ic.adapter_hi_pins)) + + data_normal: bytes | None = None + data_invert: bytes | None = None + + try: + cmd_class.set_power(True, ser) + + data_normal = cmd_class.cxfer_read(ic.address, ic.data, hi_pins, None, ser) + + # If we need to check for Hi-Z, we need to forcefully set data pins to high and then redo the dump + if check_hiz: + hi_pins = list(set(hi_pins + ic.data)) + data_invert = cmd_class.cxfer_read(ic.address, ic.data, hi_pins, None, ser) + finally: + ser.reset_input_buffer() + ser.reset_output_buffer() + cmd_class.set_power(False, ser) + + if data_normal and data_invert: + if len(data_normal) != len(data_invert): + raise IOError('Same IC read twice, bug got two different sizes!!!') + + for dn, di in zip(DPLibUtils.iter_grouper(data_normal, data_width, 0), DPLibUtils.iter_grouper(data_invert, data_width, 0)): + dni = int.from_bytes(bytes(dn)) + dii = int.from_bytes(bytes(di)) + read_data.append(DataElement(data=dni, z_mask=dni^dii)) + elif data_normal: + for dn in DPLibUtils.iter_grouper(data_normal, data_width, 0): + dni = int.from_bytes(bytes(dn)) + read_data.append(DataElement(data=dni)) + else: + raise IOError('Data array is not available!') + + return read_data[0:addr_combs] + + @classmethod + def read_ic_backup(cls, ser: serial.Serial, cmd_class: type[HardwareBoardCommands], ic: ICDefinition, check_hiz: bool = False) -> list[DataElement] | None: read_data: list[DataElement] = [] addr_combs: int = 1 << len(ic.address) # Calculate the number of addresses that this IC supports wr_responses: list[int] = []