-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add python script to calculate touch IC config CRC
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |