From 1a1ba4d555146fe3bf0cd32906c77f5d918423ca Mon Sep 17 00:00:00 2001 From: mux Date: Fri, 3 Jan 2014 08:51:02 +0200 Subject: [PATCH] Change dfu.py to be Python 2/3 compatible * Chane dfu.py to use Python 3 syntax to avoid dependency on Python 2. * Update Makefile to call python instead of python2 * Fix #33 --- stm/Makefile | 2 +- tools/dfu.py | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/stm/Makefile b/stm/Makefile index 6868f85ba7..cfd8f5eef1 100644 --- a/stm/Makefile +++ b/stm/Makefile @@ -147,7 +147,7 @@ OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(PY_O) $(SRC_FATFS:. all: $(BUILD) $(BUILD)/flash.dfu $(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin - python2 $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@ + python $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@ $(BUILD)/flash0.bin: $(BUILD)/flash.elf arm-none-eabi-objcopy -O binary -j .isr_vector $^ $@ diff --git a/tools/dfu.py b/tools/dfu.py index 875cc5c6e9..54b602438b 100755 --- a/tools/dfu.py +++ b/tools/dfu.py @@ -20,11 +20,11 @@ def compute_crc(data): return 0xFFFFFFFF & -zlib.crc32(data) -1 def parse(file,dump_images=False): - print 'File: "%s"' % file + 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 + 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 @@ -32,40 +32,40 @@ def parse(file,dump_images=False): tprefix['name'] = cstring(tprefix['name']) else: tprefix['name'] = '' - print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix + 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 + 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 + print (' DUMPED IMAGE TO "%s"' % out) if len(target): - print "target %d: PARSE ERROR" % t + 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 + 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 + print ("CRC ERROR: computed crc32 is 0x%08x" % crc) data = data[16:] if data: - print "PARSE ERROR" + print ("PARSE ERROR") def build(file,targets,device=DEFAULT_DEVICE): - data = '' + data = b'' for t,target in enumerate(targets): - tdata = '' + tdata = b'' for image in target: tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data'] - tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata + tdata = struct.pack('<6sBI255s2I',b'Target',0,1, b'ST...',len(tdata),len(target)) + tdata data += tdata - data = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data + 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,'UFD',16) + data += struct.pack('<4H3sB',0,d,v,0x011a,b'UFD',16) crc = compute_crc(data) data += struct.pack('