Skip to content

Commit 1a1ba4d

Browse files
committed
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 micropython#33
1 parent aae7847 commit 1a1ba4d

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

stm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(PY_O) $(SRC_FATFS:.
147147
all: $(BUILD) $(BUILD)/flash.dfu
148148

149149
$(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin
150-
python2 $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@
150+
python $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@
151151

152152
$(BUILD)/flash0.bin: $(BUILD)/flash.elf
153153
arm-none-eabi-objcopy -O binary -j .isr_vector $^ $@

tools/dfu.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,52 +20,52 @@ def compute_crc(data):
2020
return 0xFFFFFFFF & -zlib.crc32(data) -1
2121

2222
def parse(file,dump_images=False):
23-
print 'File: "%s"' % file
23+
print ('File: "%s"' % file)
2424
data = open(file,'rb').read()
2525
crc = compute_crc(data[:-4])
2626
prefix, data = consume('<5sBIB',data,'signature version size targets')
27-
print '%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix
27+
print ('%(signature)s v%(version)d, image size: %(size)d, targets: %(targets)d' % prefix)
2828
for t in range(prefix['targets']):
2929
tprefix, data = consume('<6sBI255s2I',data,'signature altsetting named name size elements')
3030
tprefix['num'] = t
3131
if tprefix['named']:
3232
tprefix['name'] = cstring(tprefix['name'])
3333
else:
3434
tprefix['name'] = ''
35-
print '%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix
35+
print ('%(signature)s %(num)d, alt setting: %(altsetting)s, name: "%(name)s", size: %(size)d, elements: %(elements)d' % tprefix)
3636
tsize = tprefix['size']
3737
target, data = data[:tsize], data[tsize:]
3838
for e in range(tprefix['elements']):
3939
eprefix, target = consume('<2I',target,'address size')
4040
eprefix['num'] = e
41-
print ' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix
41+
print (' %(num)d, address: 0x%(address)08x, size: %(size)d' % eprefix)
4242
esize = eprefix['size']
4343
image, target = target[:esize], target[esize:]
4444
if dump_images:
4545
out = '%s.target%d.image%d.bin' % (file,t,e)
4646
open(out,'wb').write(image)
47-
print ' DUMPED IMAGE TO "%s"' % out
47+
print (' DUMPED IMAGE TO "%s"' % out)
4848
if len(target):
49-
print "target %d: PARSE ERROR" % t
49+
print ("target %d: PARSE ERROR" % t)
5050
suffix = named(struct.unpack('<4H3sBI',data[:16]),'device product vendor dfu ufd len crc')
51-
print 'usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix
51+
print ('usb: %(vendor)04x:%(product)04x, device: 0x%(device)04x, dfu: 0x%(dfu)04x, %(ufd)s, %(len)d, 0x%(crc)08x' % suffix)
5252
if crc != suffix['crc']:
53-
print "CRC ERROR: computed crc32 is 0x%08x" % crc
53+
print ("CRC ERROR: computed crc32 is 0x%08x" % crc)
5454
data = data[16:]
5555
if data:
56-
print "PARSE ERROR"
56+
print ("PARSE ERROR")
5757

5858
def build(file,targets,device=DEFAULT_DEVICE):
59-
data = ''
59+
data = b''
6060
for t,target in enumerate(targets):
61-
tdata = ''
61+
tdata = b''
6262
for image in target:
6363
tdata += struct.pack('<2I',image['address'],len(image['data']))+image['data']
64-
tdata = struct.pack('<6sBI255s2I','Target',0,1,'ST...',len(tdata),len(target)) + tdata
64+
tdata = struct.pack('<6sBI255s2I',b'Target',0,1, b'ST...',len(tdata),len(target)) + tdata
6565
data += tdata
66-
data = struct.pack('<5sBIB','DfuSe',1,len(data)+11,len(targets)) + data
66+
data = struct.pack('<5sBIB',b'DfuSe',1,len(data)+11,len(targets)) + data
6767
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
68-
data += struct.pack('<4H3sB',0,d,v,0x011a,'UFD',16)
68+
data += struct.pack('<4H3sB',0,d,v,0x011a,b'UFD',16)
6969
crc = compute_crc(data)
7070
data += struct.pack('<I',crc)
7171
open(file,'wb').write(data)
@@ -89,15 +89,15 @@ def build(file,targets,device=DEFAULT_DEVICE):
8989
try:
9090
address,binfile = arg.split(':',1)
9191
except ValueError:
92-
print "Address:file couple '%s' invalid." % arg
92+
print ("Address:file couple '%s' invalid." % arg)
9393
sys.exit(1)
9494
try:
9595
address = int(address,0) & 0xFFFFFFFF
9696
except ValueError:
97-
print "Address %s invalid." % address
97+
print ("Address %s invalid." % address)
9898
sys.exit(1)
9999
if not os.path.isfile(binfile):
100-
print "Unreadable file '%s'." % binfile
100+
print ("Unreadable file '%s'." % binfile)
101101
sys.exit(1)
102102
target.append({ 'address': address, 'data': open(binfile,'rb').read() })
103103
outfile = args[0]
@@ -107,13 +107,13 @@ def build(file,targets,device=DEFAULT_DEVICE):
107107
try:
108108
v,d=map(lambda x: int(x,0) & 0xFFFF, device.split(':',1))
109109
except:
110-
print "Invalid device '%s'." % device
110+
print ("Invalid device '%s'." % device)
111111
sys.exit(1)
112112
build(outfile,[target],device)
113113
elif len(args)==1:
114114
infile = args[0]
115115
if not os.path.isfile(infile):
116-
print "Unreadable file '%s'." % infile
116+
print ("Unreadable file '%s'." % infile)
117117
sys.exit(1)
118118
parse(infile, dump_images=options.dump_images)
119119
else:

0 commit comments

Comments
 (0)