def wr_multi(self, addr: int, buffer: List[int], size: int) -> None:
try:
chunk_size = 32 # Set this to your hardware's max message size
remaining_size = size
current_address = addr
position = 0
while remaining_size > 0:
current_chunk_size = chunk_size if remaining_size > chunk_size else remaining_size
reg_bytes = [(current_address >> 8) & 0xFF, current_address & 0xFF]
chunk = buffer[position:position + current_chunk_size]
data = bytearray(reg_bytes + chunk)
DSYS_Write(data, slave=self.i2c_address)
if DEBUG_IO:
print(f"wr_multi addr=0x{current_address:04X}, chunk_size={current_chunk_size}, data={list(data)}")
remaining_size -= current_chunk_size
current_address += current_chunk_size
position += current_chunk_size
time.sleep(0.01) # HAL_Delay(10) in ms
except Exception as e:
print(f"Error writing register 0x{addr:04X}: {e}")
def rd_byte(self, addr: int) -> int:
try:
reg_bytes = [(addr >> 8) & 0xFF, addr & 0xFF]
data = DSYS_Read(1, bytearray(reg_bytes), slave=self.i2c_address, delay_before_read=1)
if data is not None and len(data) > 0:
b = data[0]
if DEBUG_IO:
print(f"rd_byte addr=0x{addr:04X}, byte=0x{b:02X}")
return b
else:
raise Exception("Couldn't read any bytes")
except Exception as e:
print(f"Error reading register 0x{addr:04X}: {e}")
return None
def wr_byte(self, addr: int, value: int) -> None:
try:
reg_bytes = [(addr >> 8) & 0xFF, addr & 0xFF]
data = bytearray(reg_bytes + [value])
DSYS_Write(data, slave=self.i2c_address)
if DEBUG_IO:
print(f"wr_byte addr=0x{addr:04X}, byte=0x{value:02X}")
except Exception as e:
print(f"Error writing register 0x{addr:04X}: {e}")
def rd_multi(self, addr: int, buffer: List[int], size: int) -> None:
try:
# Split 16-bit register into two bytes (big-endian: high, low)
reg_bytes = [(addr >> 8) & 0xFF, addr & 0xFF]
data = DSYS_Read(size, bytearray(reg_bytes), slave=self.i2c_address, delay_before_read=1)
if data is not None:
buffer[:len(data)] = list(data)
if DEBUG_IO:
print(f"rd_multi addr=0x{addr:04X}, size={size}, read_size={len(data)}, buf_len= read=[", end="")
print_size = len(data)
if print_size > PRINT_SIZE_MAX:
print_size = PRINT_SIZE_MAX
for i in range(print_size):
if i > 0:
print(", ", end="")
print(f"{data[i]:#02x}", end="")
if print_size != len(data):
print(", ...", end="")
print("]")
else:
raise Exception("Couldn't read any bytes")
except Exception as e:
print(f"Error reading register 0x{addr:04X}: {e}")
raise