mirror of
https://github.com/infiniteCable2/panda.git
synced 2026-02-18 17:23:52 +08:00
K line fix (#559)
* enable UART RX interrupts * update debug prints for python3 * improve kline functions and fix checksum * k-line wake-up conforming to KWP2000 fast init * fix timing * toggle k and l line together by default * k-line wakeup using timer * k and l were flipped * fix misra compliance
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
# python library to interface with panda
|
||||
import datetime
|
||||
import binascii
|
||||
import struct
|
||||
import hashlib
|
||||
import socket
|
||||
@@ -44,7 +43,7 @@ def parse_can_buffer(dat):
|
||||
address = f1 >> 21
|
||||
dddat = ddat[8:8 + (f2 & 0xF)]
|
||||
if DEBUG:
|
||||
print(" R %x: %s" % (address, binascii.hexlify(dddat)))
|
||||
print(f" R 0x{address:x}: 0x{dddat.hex()}")
|
||||
ret.append((address, f2 >> 16, dddat, (f2 >> 4) & 0xFF))
|
||||
return ret
|
||||
|
||||
@@ -489,7 +488,7 @@ class Panda(object):
|
||||
for addr, _, dat, bus in arr:
|
||||
assert len(dat) <= 8
|
||||
if DEBUG:
|
||||
print(" W %x: %s" % (addr, binascii.hexlify(dat)))
|
||||
print(f" W 0x{addr:x}: 0x{dat.hex()}")
|
||||
if addr >= 0x800:
|
||||
rir = (addr << 3) | transmit | extended
|
||||
else:
|
||||
@@ -572,10 +571,11 @@ class Panda(object):
|
||||
# ******************* kline *******************
|
||||
|
||||
# pulse low for wakeup
|
||||
def kline_wakeup(self):
|
||||
def kline_wakeup(self, k=True, l=True):
|
||||
assert k or l, "must specify k-line, l-line, or both"
|
||||
if DEBUG:
|
||||
print("kline wakeup...")
|
||||
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf0, 0, 0, b'')
|
||||
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf0, 2 if k and l else int(l), 0, b'')
|
||||
if DEBUG:
|
||||
print("kline wakeup done")
|
||||
|
||||
@@ -587,7 +587,7 @@ class Panda(object):
|
||||
if len(ret) == 0:
|
||||
break
|
||||
elif DEBUG:
|
||||
print("kline drain: " + binascii.hexlify(ret))
|
||||
print(f"kline drain: 0x{ret.hex()}")
|
||||
bret += ret
|
||||
return bytes(bret)
|
||||
|
||||
@@ -596,35 +596,31 @@ class Panda(object):
|
||||
while len(echo) != cnt:
|
||||
ret = self._handle.controlRead(Panda.REQUEST_OUT, 0xe0, bus, 0, cnt - len(echo))
|
||||
if DEBUG and len(ret) > 0:
|
||||
print("kline recv: " + binascii.hexlify(ret))
|
||||
print(f"kline recv: 0x{ret.hex()}")
|
||||
echo += ret
|
||||
return bytes(echo)
|
||||
|
||||
def kline_send(self, x, bus=2, checksum=True):
|
||||
def get_checksum(dat):
|
||||
result = 0
|
||||
result += sum(map(ord, dat)) if isinstance(b'dat', str) else sum(dat)
|
||||
result = -result
|
||||
return struct.pack("B", result % 0x100)
|
||||
|
||||
self.kline_drain(bus=bus)
|
||||
if checksum:
|
||||
x += get_checksum(x)
|
||||
x += bytes([sum(x) % 0x100])
|
||||
for i in range(0, len(x), 0xf):
|
||||
ts = x[i:i + 0xf]
|
||||
if DEBUG:
|
||||
print("kline send: " + binascii.hexlify(ts))
|
||||
print(f"kline send: 0x{ts.hex()}")
|
||||
self._handle.bulkWrite(2, bytes([bus]) + ts)
|
||||
echo = self.kline_ll_recv(len(ts), bus=bus)
|
||||
if echo != ts:
|
||||
print("**** ECHO ERROR %d ****" % i)
|
||||
print(binascii.hexlify(echo))
|
||||
print(binascii.hexlify(ts))
|
||||
print(f"**** ECHO ERROR {i} ****")
|
||||
print(f"0x{echo.hex()}")
|
||||
print(f"0x{ts.hex()}")
|
||||
assert echo == ts
|
||||
|
||||
def kline_recv(self, bus=2):
|
||||
msg = self.kline_ll_recv(2, bus=bus)
|
||||
msg += self.kline_ll_recv(ord(msg[1]) - 2, bus=bus)
|
||||
def kline_recv(self, bus=2, header_len=4):
|
||||
# read header (last byte is length)
|
||||
msg = self.kline_ll_recv(header_len, bus=bus)
|
||||
# read data (add one byte to length for checksum)
|
||||
msg += self.kline_ll_recv(msg[-1]+1, bus=bus)
|
||||
return msg
|
||||
|
||||
def send_heartbeat(self):
|
||||
|
||||
Reference in New Issue
Block a user