CS7000: modified "wrap" build target to generate .dfu files

CS7000
Silvano Seva 2024-10-22 19:03:26 +02:00
rodzic dab1b76967
commit 027c76477d
2 zmienionych plików z 160 dodań i 2 usunięć

Wyświetl plik

@ -777,6 +777,7 @@ gd77_loader = find_program('scripts/gd-77_firmware_loader.py', required:false, d
dfu_util = find_program('dfu-util', required:false, disabler:true)
west = find_program('west', required:false, disabler:true)
uf2conv = find_program('scripts/uf2conv.py', required:false, disabler:true)
dfu_convert = find_program('scripts/dfu-convert.py', required:false, disabler:true)
foreach t : targets
@ -871,8 +872,8 @@ foreach t : targets
output : name+'_flash',
command : [gd77_loader, '-f', '@INPUT@', '-m', t['wrap']])
# Module17 also uses dfu-tool for flashing
elif name == 'openrtx_mod17' or name == 'openrtx_cs7000'
# Module17 uses dfu-tool for flashing
elif name == 'openrtx_mod17'
# Wrap target for Module17 for consistency, same output as bin target
custom_target(name+'_wrap',
@ -890,6 +891,23 @@ foreach t : targets
'-D', '@INPUT@',
'-s', '0x08000000'])
# For CS7000 'wrap' target prepares a .dfu file
elif name == 'openrtx_cs7000'
custom_target(name+'_wrap',
output : name+'.dfu',
input : bin,
command : [dfu_convert, '-b', '0x08000000:@INPUT@', '@OUTPUT@'])
custom_target(name+'_flash',
input : bin,
output : name+'_flash',
command : [dfu_util,
'-d', '0483:df11',
'-a', '0',
'-D', '@INPUT@',
'-s', '0x08000000'])
else
wrap = custom_target(name+'_wrap',

Wyświetl plik

@ -0,0 +1,140 @@
#!/usr/bin/env python3
# Written by Antonio Galea - 2010/11/18
# Distributed under Gnu LGPL 3.0
# see http://www.gnu.org/licenses/lgpl-3.0.txt
import sys,struct,zlib,os
from optparse import OptionParser
from intelhex import IntelHex
DEFAULT_DEVICE="0x0483:0xdf11"
def named(tuple,names):
return dict(zip(names.split(),tuple))
def consume(fmt,data,names):
n = struct.calcsize(fmt)
return named(struct.unpack(fmt,data[:n]),names),data[n:]
def cstring(string):
return string.split('\0',1)[0]
def compute_crc(data):
return 0xFFFFFFFF & -zlib.crc32(data) -1
def parse(file, dump_images=False):
print ('File: "%s"' % file)
data = open(file,'rb').read()
crc = compute_crc(data[:-4])
prefix, data = consume('<5sBIB',data,'signature version size targets')
print ('%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix)
for t in range(prefix['targets']):
tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements')
tprefix['num'] = t
if not tprefix['named']:
tprefix['name'] = ''
print ('%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix)
tsize = tprefix['size']
target, data = data[:tsize], data[tsize:]
for e in range(tprefix['elements']):
eprefix, target = consume('<2I',target,'address size')
eprefix['num'] = e
print (' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix)
esize = eprefix['size']
image, target = target[:esize], target[esize:]
if dump_images:
out = '%s.target%d.image%d.bin' % (file,t,e)
open(out,'wb').write(image)
print (' DUMPED IMAGE TO "%s"' % out)
if len(target):
print ("target %d: PARSE ERROR" % t)
suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc')
print ('usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix)
if crc != suffix['crc']:
print ("CRC ERROR: computed crc32 is 0x%08x" % crc)
data = data[16:]
if data:
print ("PARSE ERROR")
def build(file, targets, device=DEFAULT_DEVICE):
data = b''
for t,target in enumerate(targets):
tdata = b''
for image in target:
tdata += struct.pack('<2I',image['address'],len(image['data']))+ image['data']
tdata = struct.pack('<6sBI255s2I',b'Target',0,1,b'ST...',len(tdata),len(target)) + tdata
data += tdata
data = struct.pack('<5sBIB',b'DfuSe',1,len(data)+11,len(targets)) + data
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
data += struct.pack('<4H3sB',0,d,v,0x011a,b'UFD',16)
crc = compute_crc(data)
data += struct.pack('<I',crc)
open(file,'wb').write(data)
if __name__=="__main__":
usage = """
%prog [-d|--dump] infile.dfu
%prog {-b|--build} address:file.bin [-b address:file.bin ...] [{-D|--device}=vendor:device] outfile.dfu
%prog {-i|--ihex} file.hex [-i file.hex ...] [{-D|--device}=vendor:device] outfile.dfu"""
parser = OptionParser(usage=usage)
parser.add_option("-b", "--build", action="append", dest="binfiles",
help="build a DFU file from given BINFILES", metavar="BINFILES")
parser.add_option("-i", "--ihex", action="append", dest="hexfiles",
help="build a DFU file from given HEXFILES", metavar="HEXFILES")
parser.add_option("-D", "--device", action="store", dest="device",
help="build for DEVICE, defaults to %s" % DEFAULT_DEVICE, metavar="DEVICE")
parser.add_option("-d", "--dump", action="store_true", dest="dump_images",
default=False, help="dump contained images to current directory")
(options, args) = parser.parse_args()
if (options.binfiles or options.hexfiles) and len(args)==1:
target = []
if options.binfiles:
for arg in options.binfiles:
try:
address,binfile = arg.split(':',1)
except ValueError:
print ("Address:file couple '%s' invalid." % arg)
sys.exit(1)
try:
address = int(address,0) & 0xFFFFFFFF
except ValueError:
print ("Address %s invalid." % address)
sys.exit(1)
if not os.path.isfile(binfile):
print ("Unreadable file '%s'." % binfile)
sys.exit(1)
target.append({ 'address': address, 'data': open(binfile,'rb').read() })
if options.hexfiles:
for hex in options.hexfiles:
ih = IntelHex(hex)
address = ih.minaddr()
data = ih.tobinstr()
try:
address = address & 0xFFFFFFFF
except ValueError:
print ("Address %s invalid." % address)
sys.exit(1)
target.append({ 'address': address, 'data': data })
outfile = args[0]
device = DEFAULT_DEVICE
if options.device:
device=options.device
try:
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
except:
print ("Invalid device '%s'." % device)
sys.exit(1)
build(outfile, [target], device)
elif len(args)==1:
infile = args[0]
if not os.path.isfile(infile):
print ("Unreadable file '%s'." % infile)
sys.exit(1)
parse(infile, dump_images=options.dump_images)
else:
parser.print_help()
sys.exit(1)