tw88xx_utils/tw88xx_util.py

86 lines
2.9 KiB
Python
Raw Normal View History

2022-01-09 18:20:49 +01:00
import argparse
from tw88xx import TW88xx
parser = argparse.ArgumentParser(description='TW88xx debug utility.')
parser.add_argument('command',
help='the command to execute',
2022-01-09 22:14:53 +01:00
choices=('dump_flash', 'get_id', 'dump_regs', 'dump_lut')
2022-01-09 18:20:49 +01:00
)
parser.add_argument('-f', '--flash_file',
help='file to read from / write into'
)
parser.add_argument('-i', '--interface',
help='the i2c interface to use',
choices=('devi2c', 'swd')
)
parser.add_argument('-j', '--jlink',
help='J-Link (serial) to use for SWD',
default=None
)
2022-01-09 19:53:00 +01:00
parser.add_argument('-b', '--bus',
help='/dev/i2c bus number',
2022-01-09 19:57:52 +01:00
type=int,
2022-01-09 19:53:00 +01:00
default=1
)
2022-01-09 18:20:49 +01:00
parser.add_argument('-t', '--type',
help='TX88xx type',
2022-01-09 21:52:33 +01:00
choices=('tw8836',),
2022-01-09 18:20:49 +01:00
default='tw8836'
)
args = parser.parse_args()
if args.interface == 'swd':
from i2c_interfaces import JLinkSwdI2CInterface
i2c = JLinkSwdI2CInterface(jlink_serial=args.jlink)
2022-01-09 19:58:49 +01:00
elif args.interface == 'devi2c':
2022-01-09 19:53:00 +01:00
from i2c_interfaces import DevI2CInterface
i2c = DevI2CInterface(bus=args.bus)
2022-01-09 18:20:49 +01:00
i2c.open()
if args.type == 'tw8836':
from tw8836 import TW8836
tw88xx = TW8836(i2c)
if args.command == 'get_id':
id = tw88xx.read_reg(0x000)
print(f'{id:02x}')
flash_id = tw88xx.execute_flash_cmd(cmd=[0x9f], read_len=3)
print(flash_id.hex())
elif args.command == 'dump_flash':
# tw88xx.prepare_flash_read(cmd=[0x03, 0x1a, 0x9d, 0x84], read_len=8)
tw88xx.execute_flash_cmd(cmd=[0xE9], read_len=0)
2022-01-09 19:53:00 +01:00
data = []
2022-01-09 21:52:33 +01:00
BS=4
tw88xx.prepare_flash_read(cmd_len = 4, read_len=BS)
for block_start in range(0x1a9d84, 0x1a9d84+480*480, BS):
# block = tw88xx.execute_flash_cmd(, data=[0x5a]*BS)
cmd=[0x03, (block_start >> 16) & 0xFF, (block_start >> 8) & 0xFF, block_start & 0xFF]
block = tw88xx.do_flash_cmd(cmd, read_len=BS)
2022-01-09 18:20:49 +01:00
data.extend(block)
2022-01-09 21:52:33 +01:00
print(f'{(block_start-0x1a9d84) / (480*480)*100:02.4f}% {block_start:06x}: {block.hex()}')
2022-01-09 18:20:49 +01:00
print(bytes(data).hex())
2022-01-09 21:52:33 +01:00
with open(args.flash_file,'wb') as f:
f.write(bytes(data))
2022-01-09 18:20:49 +01:00
# tw88xx.execute_flash_cmd(cmd=[0xB7], read_len=0)
# block = tw88xx.execute_flash_cmd(cmd=[0x03, 0x00, 0x00, 0x00], read_len=8)
# print(block.hex())
# block = tw88xx.execute_flash_cmd(cmd=[0x03, 0x00, 0x00, 0x08], read_len=8)
# print(block.hex())
# tw88xx.execute_flash_cmd(cmd=[0xE9], read_len=0)
2022-01-09 21:52:33 +01:00
elif args.command == 'dump_regs':
tw88xx.dump_regs()
2022-01-09 22:14:53 +01:00
elif args.command == 'dump_lut':
tbl = tw88xx.dump_lut()
if args.flash_file:
with open(args.flash_file,'w') as f:
for r in sorted(tbl):
f.write(f'{r:03x}={bytes(tbl[r]).hex()}\n')