Skip to content

Commit

Permalink
add python script to calculate touch IC config CRC
Browse files Browse the repository at this point in the history
  • Loading branch information
schnommus committed Oct 25, 2023
1 parent a946f77 commit cea5422
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions gateware/drivers/touch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/python3

CY8CMBR3xxx_CONFIG_DATA_LENGTH = 126
CY8CMBR3xxx_CRC_BIT_WIDTH = 2 * 8
CY8CMBR3xxx_CRC_BIT4_MASK = 0x0F
CY8CMBR3xxx_CRC_BIT4_SHIFT = 4
CY8CMBR3xxx_CCITT16_DEFAULT_SEED = 0xffff
CY8CMBR3xxx_CCITT16_POLYNOM = 0x1021


def CY8CMBR3xxx_Calc4BitsCRC(value, remainder):
# Divide the value by polynomial, via the CRC polynomial
tableIndex = (value & CY8CMBR3xxx_CRC_BIT4_MASK) ^ (remainder >> (CY8CMBR3xxx_CRC_BIT_WIDTH - CY8CMBR3xxx_CRC_BIT4_SHIFT))
remainder = (CY8CMBR3xxx_CCITT16_POLYNOM * tableIndex) ^ (remainder << CY8CMBR3xxx_CRC_BIT4_SHIFT)
return remainder


def CY8CMBR3xxx_CalculateCrc(configuration):
seed = CY8CMBR3xxx_CCITT16_DEFAULT_SEED

# don't make count down cycle! CRC will be different!
for byteValue in configuration:
seed = CY8CMBR3xxx_Calc4BitsCRC(byteValue >> CY8CMBR3xxx_CRC_BIT4_SHIFT, seed) & 0xffff
seed = CY8CMBR3xxx_Calc4BitsCRC(byteValue, seed) & 0xffff

return seed


with open("touch-cfg.hex", "r") as f:
xs = []
for line in f.readlines():
raw = line.strip()
v = int(raw, 16)
print("hex", raw, "int", v)
xs.append(v)
print("total bytes in file", len(xs))
xs = xs[2:-2]
print("bytes to crc", len(xs))
crc = CY8CMBR3xxx_CalculateCrc(xs)
print("crc0", hex(crc & 0x00FF))
print("crc1", hex((crc & 0xFF00)>>8))

0 comments on commit cea5422

Please sign in to comment.