Skip to content

Commit

Permalink
current implementation of cxfer reading
Browse files Browse the repository at this point in the history
  • Loading branch information
hkzlab committed Sep 2, 2024
1 parent 14f9966 commit fbdc6d8
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/dpdumper/hl_board_utilities.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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] = []
Expand Down

0 comments on commit fbdc6d8

Please sign in to comment.