Skip to content

Commit af0960a

Browse files
rbiasinirobbederks
authored andcommitted
DFU fix (#288)
* DFU fix * fix test 2 * this should fix all the remaining jenkins test * Fixed pyenv shim not being a python file, but a sh script
1 parent 70219d7 commit af0960a

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

python/dfu.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import usb1
44
import struct
55
import time
6+
import binascii
67

78
# *** DFU mode ***
89

@@ -46,41 +47,40 @@ def list():
4647
def st_serial_to_dfu_serial(st):
4748
if st == None or st == "none":
4849
return None
49-
uid_base = struct.unpack("H"*6, st.decode("hex"))
50-
return struct.pack("!HHH", uid_base[1] + uid_base[5], uid_base[0] + uid_base[4] + 0xA, uid_base[3]).encode("hex").upper()
51-
50+
uid_base = struct.unpack("H"*6, bytes.fromhex(st))
51+
return binascii.hexlify(struct.pack("!HHH", uid_base[1] + uid_base[5], uid_base[0] + uid_base[4] + 0xA, uid_base[3])).upper().decode("utf-8")
5252

5353
def status(self):
5454
while 1:
55-
dat = str(self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
56-
if dat[1] == "\x00":
55+
dat = self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6)
56+
if dat[1] == 0:
5757
break
5858

5959
def clear_status(self):
6060
# Clear status
61-
stat = str(self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
62-
if stat[4] == "\x0a":
61+
stat = self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6)
62+
if stat[4] == 0xa:
6363
self._handle.controlRead(0x21, DFU_CLRSTATUS, 0, 0, 0)
64-
elif stat[4] == "\x09":
65-
self._handle.controlWrite(0x21, DFU_ABORT, 0, 0, "")
64+
elif stat[4] == 0x9:
65+
self._handle.controlWrite(0x21, DFU_ABORT, 0, 0, b"")
6666
self.status()
6767
stat = str(self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
6868

6969
def erase(self, address):
70-
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, "\x41" + struct.pack("I", address))
70+
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x41" + struct.pack("I", address))
7171
self.status()
7272

7373
def program(self, address, dat, block_size=None):
7474
if block_size == None:
7575
block_size = len(dat)
7676

7777
# Set Address Pointer
78-
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, "\x21" + struct.pack("I", address))
78+
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x21" + struct.pack("I", address))
7979
self.status()
8080

8181
# Program
82-
dat += "\xFF"*((block_size-len(dat)) % block_size)
83-
for i in range(0, len(dat)/block_size):
82+
dat += b"\xFF"*((block_size-len(dat)) % block_size)
83+
for i in range(0, len(dat)//block_size):
8484
ldat = dat[i*block_size:(i+1)*block_size]
8585
print("programming %d with length %d" % (i, len(ldat)))
8686
self._handle.controlWrite(0x21, DFU_DNLOAD, 2+i, 0, ldat)
@@ -105,17 +105,17 @@ def recover(self):
105105
build_st(fn)
106106
fn = os.path.join(BASEDIR, "board", fn)
107107

108-
with open(fn) as f:
108+
with open(fn, "rb") as f:
109109
code = f.read()
110110

111111
self.program_bootstub(code)
112112

113113
def reset(self):
114114
# **** Reset ****
115-
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, "\x21" + struct.pack("I", 0x8000000))
115+
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x21" + struct.pack("I", 0x8000000))
116116
self.status()
117117
try:
118-
self._handle.controlWrite(0x21, DFU_DNLOAD, 2, 0, "")
118+
self._handle.controlWrite(0x21, DFU_DNLOAD, 2, 0, b"")
119119
stat = str(self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
120120
except Exception:
121121
pass

run_automated_tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ do
1818
nmcli connection delete "$NAME"
1919
done
2020

21-
PYTHONPATH="." python $(which nosetests) -v --with-xunit --xunit-file=./$TEST_FILENAME --xunit-testsuite-name=$TESTSUITE_NAME -s $TEST_SCRIPTS
21+
PYTHONPATH="." $(which nosetests) -v --with-xunit --xunit-file=./$TEST_FILENAME --xunit-testsuite-name=$TESTSUITE_NAME -s $TEST_SCRIPTS

tests/automated/2_usb_to_can.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_can_loopback(p):
2525
p.set_can_speed_kbps(bus, 250)
2626

2727
# send a message on bus 0
28-
p.can_send(0x1aa, "message", bus)
28+
p.can_send(0x1aa, b"message", bus)
2929

3030
# confirm receive both on loopback and send receipt
3131
time.sleep(0.05)
@@ -37,7 +37,7 @@ def test_can_loopback(p):
3737

3838
# confirm data is correct
3939
assert 0x1aa == sr[0][0] == lb[0][0]
40-
assert "message" == sr[0][2] == lb[0][2]
40+
assert b"message" == sr[0][2] == lb[0][2]
4141

4242
@test_all_pandas
4343
@panda_connect_and_init
@@ -49,7 +49,7 @@ def test_safety_nooutput(p):
4949
p.set_can_loopback(True)
5050

5151
# send a message on bus 0
52-
p.can_send(0x1aa, "message", 0)
52+
p.can_send(0x1aa, b"message", 0)
5353

5454
# confirm receive nothing
5555
time.sleep(0.05)
@@ -68,7 +68,7 @@ def test_reliability(p):
6868
p.set_can_speed_kbps(0, 1000)
6969

7070
addrs = list(range(100, 100+MSG_COUNT))
71-
ts = [(j, 0, "\xaa"*8, 0) for j in addrs]
71+
ts = [(j, 0, b"\xaa"*8, 0) for j in addrs]
7272

7373
# 100 loops
7474
for i in range(LOOP_COUNT):
@@ -182,4 +182,4 @@ def test_gmlan_bad_toggle(p):
182182
def test_serial_debug(p):
183183
junk = p.serial_read(Panda.SERIAL_DEBUG)
184184
p.call_control_api(0xc0)
185-
assert(p.serial_read(Panda.SERIAL_DEBUG).startswith("can "))
185+
assert(p.serial_read(Panda.SERIAL_DEBUG).startswith(b"can "))

tests/automated/6_two_panda.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_send_recv(p_send, p_recv):
1818
assert not p_send.legacy
1919
assert not p_recv.legacy
2020

21-
p_send.can_send_many([(0x1ba, 0, "message", 0)]*2)
21+
p_send.can_send_many([(0x1ba, 0, b"message", 0)]*2)
2222
time.sleep(0.05)
2323
p_recv.can_recv()
2424
p_send.can_recv()
@@ -55,7 +55,7 @@ def test_latency(p_send, p_recv):
5555
p_recv.set_can_speed_kbps(0, 100)
5656
time.sleep(0.05)
5757

58-
p_send.can_send_many([(0x1ba, 0, "testmsg", 0)]*10)
58+
p_send.can_send_many([(0x1ba, 0, b"testmsg", 0)]*10)
5959
time.sleep(0.05)
6060
p_recv.can_recv()
6161
p_send.can_recv()
@@ -80,7 +80,7 @@ def test_latency(p_send, p_recv):
8080

8181
for i in range(num_messages):
8282
st = time.time()
83-
p_send.can_send(0x1ab, "message", bus)
83+
p_send.can_send(0x1ab, b"message", bus)
8484
r = []
8585
while len(r) < 1 and (time.time() - st) < 5:
8686
r = p_recv.can_recv()
@@ -127,7 +127,7 @@ def test_black_loopback(panda0, panda1):
127127
panda1.set_can_loopback(False)
128128

129129
# clear stuff
130-
panda0.can_send_many([(0x1ba, 0, "testmsg", 0)]*10)
130+
panda0.can_send_many([(0x1ba, 0, b"testmsg", 0)]*10)
131131
time.sleep(0.05)
132132
panda0.can_recv()
133133
panda1.can_recv()
@@ -155,7 +155,7 @@ def get_test_string():
155155
def _test_buses(send_panda, recv_panda, _test_array):
156156
for send_bus, send_obd, recv_obd, recv_buses in _test_array:
157157
print("\nSend bus:", send_bus, " Send OBD:", send_obd, " Recv OBD:", recv_obd)
158-
158+
159159
# set OBD on pandas
160160
send_panda.set_gmlan(True if send_obd else None)
161161
recv_panda.set_gmlan(True if recv_obd else None)
@@ -180,7 +180,7 @@ def _test_buses(send_panda, recv_panda, _test_array):
180180
loop_buses.append(loop[3])
181181
if len(cans_loop) == 0:
182182
print(" No loop")
183-
183+
184184
# test loop buses
185185
recv_buses.sort()
186186
loop_buses.sort()
@@ -192,4 +192,4 @@ def _test_buses(send_panda, recv_panda, _test_array):
192192
print("***************** TESTING (0 --> 1) *****************")
193193
_test_buses(panda0, panda1, test_array)
194194
print("***************** TESTING (1 --> 0) *****************")
195-
_test_buses(panda1, panda0, test_array)
195+
_test_buses(panda1, panda0, test_array)

tests/automated/helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def time_many_sends(p, bus, precv=None, msg_count=100, msg_id=None, two_pandas=F
140140
raise ValueError("Cannot have two pandas that are the same panda")
141141

142142
st = time.time()
143-
p.can_send_many([(msg_id, 0, "\xaa"*8, bus)]*msg_count)
143+
p.can_send_many([(msg_id, 0, b"\xaa"*8, bus)]*msg_count)
144144
r = []
145145
r_echo = []
146146
r_len_expected = msg_count if two_pandas else msg_count*2

0 commit comments

Comments
 (0)