2017-04-07 09:11:36 +08:00
|
|
|
# python library to interface with panda
|
2019-10-26 07:22:42 +08:00
|
|
|
import datetime
|
2017-04-07 09:11:36 +08:00
|
|
|
import struct
|
2017-04-29 07:56:40 +08:00
|
|
|
import hashlib
|
2017-04-07 09:11:36 +08:00
|
|
|
import usb1
|
2017-07-18 06:29:31 +08:00
|
|
|
import os
|
|
|
|
import time
|
2017-08-13 09:42:24 +08:00
|
|
|
import traceback
|
2020-05-03 03:18:26 +08:00
|
|
|
import sys
|
2021-11-11 21:30:52 +08:00
|
|
|
from functools import wraps
|
2021-08-03 11:26:15 +08:00
|
|
|
from .dfu import PandaDFU, MCU_TYPE_F2, MCU_TYPE_F4, MCU_TYPE_H7 # pylint: disable=import-error
|
2020-05-30 02:52:03 +08:00
|
|
|
from .flash_release import flash_release # noqa pylint: disable=import-error
|
|
|
|
from .update import ensure_st_up_to_date # noqa pylint: disable=import-error
|
|
|
|
from .serial import PandaSerial # noqa pylint: disable=import-error
|
|
|
|
from .isotp import isotp_send, isotp_recv # pylint: disable=import-error
|
2021-08-03 11:26:15 +08:00
|
|
|
from .config import DEFAULT_FW_FN, DEFAULT_H7_FW_FN # noqa pylint: disable=import-error
|
2017-04-07 09:11:36 +08:00
|
|
|
|
CAN_FIFOMailBox to CANPacket struct + USB dynamic packet size (#739)
* Squashed commits, no cleanup
* Few fixes
* No init = garbage
* Only receive with new canpacket
* Add send with canpacket
* Revert "Add send with canpacket"
This reverts commit 7d06686ddd6d447c714b5289d31af24403d36931.
* Packet must be aligned to word, or bad performance
* Cleaner
* Fix tests
* Tests...
* MISRA 10.4
* More MISRA
* libpandasafety_py
* cffi
* even more tests...
* typo
* ...
* ...
* ...
* Slight cleanup
* MISRA 6.1
* MISRA 17.7
* Bug in bxcan + even style
* MISRA 10.1
* Revert "MISRA 10.1"
This reverts commit 404ae7fcc39556f80f528de9015702e69f4ea0a5.
* ...
* MISRA 10.1 and 10.4 suppress until next PR
* MISRA 20.1
* ...
* test_honda
* ...
* ...
* test_toyota
* test_volkswagen_mqb
* test_volkswagen_pq
* Sketchy thing...
* Revert "Sketchy thing..."
This reverts commit 3b2e5715bdc1954f7b7b3b7469ba3d0eaa06bdf9.
* remove comment
* bxcan extended address bug
* Concept, experimental dynamic usb packet size
* increase each buffer to 10240 bytes
* raise python bulk read/write limits
* ...
* Move packet size to start
* Experimental send, stream-like
* New receive test, stream-like
* cleanup
* cleanup + rebase fixes
* MISRA
* Extra receive method, stream-like, commented out
* type change
* Revert back to buffer for send, stream commented
* forgot ZLP
* lower buffer, add rx failsafe
* ... remove ZLP
* return ZLP back
* Add tx checks to panda fw
* TX stream with counter
* fix counter overflow
* 13 free slots should be enough
* limit tx usb packet
* ...
* Revert max_bulk_msg doubling
* python lib improve speed
* Stream with counter for RX, dirty, needs cleanup
* Increase chunk length to 4096 bytes
* cleanup fdcan.h
* cleanup __init__.py
* MISRA 12.1
* MISRA 10.8
* remove non-streaming usb functions
* more main.c cleanup
* MISRA 15.6
* MISRA 15.5
* MISRA 18.4 and suppress objectIndex
* handling usb pakcets > 63bytes, naming and cleanup
* Cleanup old from tests and update CANPacket_t struct
* Switch to 4 bit DLC instead of 6 bit length
* ops)
* ...
* pylint
* receive python buffer increase
* USB increase receive packet len
* tweak buffers
* No need for so high limits
* MISRA 20.1 workaround
* performance tweaks
* cleanup, dlc to data_len_code naming
* main.c naming
* comments and cleanup for main.c usb
* clean py lib
* pylint
* do not discard good rx messages on stream fail
* cleanups
* naming
* remove bitstruct lib and lower tx limit
* bitstruct lefovers
* fix bug in VW test
* remove adjusting data size and assert on wrong len
* ...
* test new memcpy before merging
* Revert "test new memcpy before merging"
This reverts commit 399465a264835061adabdd785718c4b6fc18c267.
* macros for to/fromuint8_t array
* MISRA hates me!
* tests.c include macros instead
* move CANPacket to can_definitions.h
* vw_pq python test fix
* new memcpy test, REMOVE
* check without alignment
* revert macros for uint8 arrays
* Revert "revert macros for uint8 arrays"
This reverts commit 581a9db735a42d0d68200bd270d87a8fd34e43fe.
* check assert
* Revert "check assert"
This reverts commit 9e970d029a50597a1718b2bb0260196c050fd77f.
* one more variation
* Revert "one more variation"
This reverts commit f6c0528b7ac7e125750dc0d9445c7ce97f6954b5.
* what about read performance
* Revert "what about read performance"
This reverts commit d2610f90958a816fe7f1822157a84f85e97d9249.
* check struct alignment to word
* check for aligned memcpy again
* cleanup
* add CANPacket structure diagram
* update CANPacket and add USB packet struct
* bugfix + refactoring of EP1
* move dlc_to_len to header
* missed include
* typo...
* MISRA
* fk
* lower MAX_CAN_MSGS_PER_BULK_TRANSFER
* bump CAN_PACKET_VERSION to 2
* bump python lib CAN_PACKET_VERSION to 2
* rename parse_can_buffer to unpack_can_buffer
* CANPacket_t const fields
* Revert "CANPacket_t const fields"
This reverts commit cf91c035b7706a14e317550c5f0501ae3fce7c70.
* test.c relative path
* cleanup
* move macros to safety_declarations
* Refactor pack/unpack funcs and add unittest
* usb_protocol.h
* oops
* Update .github/workflows/test.yaml
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove print from unittest
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2021-11-13 08:36:34 +08:00
|
|
|
__version__ = '0.0.10'
|
2017-06-14 09:50:47 +08:00
|
|
|
|
2017-07-25 06:16:22 +08:00
|
|
|
BASEDIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")
|
|
|
|
|
2018-01-24 10:40:53 +08:00
|
|
|
DEBUG = os.getenv("PANDADEBUG") is not None
|
|
|
|
|
CAN_FIFOMailBox to CANPacket struct + USB dynamic packet size (#739)
* Squashed commits, no cleanup
* Few fixes
* No init = garbage
* Only receive with new canpacket
* Add send with canpacket
* Revert "Add send with canpacket"
This reverts commit 7d06686ddd6d447c714b5289d31af24403d36931.
* Packet must be aligned to word, or bad performance
* Cleaner
* Fix tests
* Tests...
* MISRA 10.4
* More MISRA
* libpandasafety_py
* cffi
* even more tests...
* typo
* ...
* ...
* ...
* Slight cleanup
* MISRA 6.1
* MISRA 17.7
* Bug in bxcan + even style
* MISRA 10.1
* Revert "MISRA 10.1"
This reverts commit 404ae7fcc39556f80f528de9015702e69f4ea0a5.
* ...
* MISRA 10.1 and 10.4 suppress until next PR
* MISRA 20.1
* ...
* test_honda
* ...
* ...
* test_toyota
* test_volkswagen_mqb
* test_volkswagen_pq
* Sketchy thing...
* Revert "Sketchy thing..."
This reverts commit 3b2e5715bdc1954f7b7b3b7469ba3d0eaa06bdf9.
* remove comment
* bxcan extended address bug
* Concept, experimental dynamic usb packet size
* increase each buffer to 10240 bytes
* raise python bulk read/write limits
* ...
* Move packet size to start
* Experimental send, stream-like
* New receive test, stream-like
* cleanup
* cleanup + rebase fixes
* MISRA
* Extra receive method, stream-like, commented out
* type change
* Revert back to buffer for send, stream commented
* forgot ZLP
* lower buffer, add rx failsafe
* ... remove ZLP
* return ZLP back
* Add tx checks to panda fw
* TX stream with counter
* fix counter overflow
* 13 free slots should be enough
* limit tx usb packet
* ...
* Revert max_bulk_msg doubling
* python lib improve speed
* Stream with counter for RX, dirty, needs cleanup
* Increase chunk length to 4096 bytes
* cleanup fdcan.h
* cleanup __init__.py
* MISRA 12.1
* MISRA 10.8
* remove non-streaming usb functions
* more main.c cleanup
* MISRA 15.6
* MISRA 15.5
* MISRA 18.4 and suppress objectIndex
* handling usb pakcets > 63bytes, naming and cleanup
* Cleanup old from tests and update CANPacket_t struct
* Switch to 4 bit DLC instead of 6 bit length
* ops)
* ...
* pylint
* receive python buffer increase
* USB increase receive packet len
* tweak buffers
* No need for so high limits
* MISRA 20.1 workaround
* performance tweaks
* cleanup, dlc to data_len_code naming
* main.c naming
* comments and cleanup for main.c usb
* clean py lib
* pylint
* do not discard good rx messages on stream fail
* cleanups
* naming
* remove bitstruct lib and lower tx limit
* bitstruct lefovers
* fix bug in VW test
* remove adjusting data size and assert on wrong len
* ...
* test new memcpy before merging
* Revert "test new memcpy before merging"
This reverts commit 399465a264835061adabdd785718c4b6fc18c267.
* macros for to/fromuint8_t array
* MISRA hates me!
* tests.c include macros instead
* move CANPacket to can_definitions.h
* vw_pq python test fix
* new memcpy test, REMOVE
* check without alignment
* revert macros for uint8 arrays
* Revert "revert macros for uint8 arrays"
This reverts commit 581a9db735a42d0d68200bd270d87a8fd34e43fe.
* check assert
* Revert "check assert"
This reverts commit 9e970d029a50597a1718b2bb0260196c050fd77f.
* one more variation
* Revert "one more variation"
This reverts commit f6c0528b7ac7e125750dc0d9445c7ce97f6954b5.
* what about read performance
* Revert "what about read performance"
This reverts commit d2610f90958a816fe7f1822157a84f85e97d9249.
* check struct alignment to word
* check for aligned memcpy again
* cleanup
* add CANPacket structure diagram
* update CANPacket and add USB packet struct
* bugfix + refactoring of EP1
* move dlc_to_len to header
* missed include
* typo...
* MISRA
* fk
* lower MAX_CAN_MSGS_PER_BULK_TRANSFER
* bump CAN_PACKET_VERSION to 2
* bump python lib CAN_PACKET_VERSION to 2
* rename parse_can_buffer to unpack_can_buffer
* CANPacket_t const fields
* Revert "CANPacket_t const fields"
This reverts commit cf91c035b7706a14e317550c5f0501ae3fce7c70.
* test.c relative path
* cleanup
* move macros to safety_declarations
* Refactor pack/unpack funcs and add unittest
* usb_protocol.h
* oops
* Update .github/workflows/test.yaml
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove print from unittest
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2021-11-13 08:36:34 +08:00
|
|
|
CANPACKET_HEAD_SIZE = 0x5
|
|
|
|
DLC_TO_LEN = [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64]
|
|
|
|
LEN_TO_DLC = {length: dlc for (dlc, length) in enumerate(DLC_TO_LEN)}
|
|
|
|
|
|
|
|
def pack_can_buffer(arr):
|
|
|
|
snds = [b'']
|
|
|
|
idx = 0
|
|
|
|
for address, _, dat, bus in arr:
|
|
|
|
assert len(dat) in LEN_TO_DLC
|
2018-01-24 10:40:53 +08:00
|
|
|
if DEBUG:
|
CAN_FIFOMailBox to CANPacket struct + USB dynamic packet size (#739)
* Squashed commits, no cleanup
* Few fixes
* No init = garbage
* Only receive with new canpacket
* Add send with canpacket
* Revert "Add send with canpacket"
This reverts commit 7d06686ddd6d447c714b5289d31af24403d36931.
* Packet must be aligned to word, or bad performance
* Cleaner
* Fix tests
* Tests...
* MISRA 10.4
* More MISRA
* libpandasafety_py
* cffi
* even more tests...
* typo
* ...
* ...
* ...
* Slight cleanup
* MISRA 6.1
* MISRA 17.7
* Bug in bxcan + even style
* MISRA 10.1
* Revert "MISRA 10.1"
This reverts commit 404ae7fcc39556f80f528de9015702e69f4ea0a5.
* ...
* MISRA 10.1 and 10.4 suppress until next PR
* MISRA 20.1
* ...
* test_honda
* ...
* ...
* test_toyota
* test_volkswagen_mqb
* test_volkswagen_pq
* Sketchy thing...
* Revert "Sketchy thing..."
This reverts commit 3b2e5715bdc1954f7b7b3b7469ba3d0eaa06bdf9.
* remove comment
* bxcan extended address bug
* Concept, experimental dynamic usb packet size
* increase each buffer to 10240 bytes
* raise python bulk read/write limits
* ...
* Move packet size to start
* Experimental send, stream-like
* New receive test, stream-like
* cleanup
* cleanup + rebase fixes
* MISRA
* Extra receive method, stream-like, commented out
* type change
* Revert back to buffer for send, stream commented
* forgot ZLP
* lower buffer, add rx failsafe
* ... remove ZLP
* return ZLP back
* Add tx checks to panda fw
* TX stream with counter
* fix counter overflow
* 13 free slots should be enough
* limit tx usb packet
* ...
* Revert max_bulk_msg doubling
* python lib improve speed
* Stream with counter for RX, dirty, needs cleanup
* Increase chunk length to 4096 bytes
* cleanup fdcan.h
* cleanup __init__.py
* MISRA 12.1
* MISRA 10.8
* remove non-streaming usb functions
* more main.c cleanup
* MISRA 15.6
* MISRA 15.5
* MISRA 18.4 and suppress objectIndex
* handling usb pakcets > 63bytes, naming and cleanup
* Cleanup old from tests and update CANPacket_t struct
* Switch to 4 bit DLC instead of 6 bit length
* ops)
* ...
* pylint
* receive python buffer increase
* USB increase receive packet len
* tweak buffers
* No need for so high limits
* MISRA 20.1 workaround
* performance tweaks
* cleanup, dlc to data_len_code naming
* main.c naming
* comments and cleanup for main.c usb
* clean py lib
* pylint
* do not discard good rx messages on stream fail
* cleanups
* naming
* remove bitstruct lib and lower tx limit
* bitstruct lefovers
* fix bug in VW test
* remove adjusting data size and assert on wrong len
* ...
* test new memcpy before merging
* Revert "test new memcpy before merging"
This reverts commit 399465a264835061adabdd785718c4b6fc18c267.
* macros for to/fromuint8_t array
* MISRA hates me!
* tests.c include macros instead
* move CANPacket to can_definitions.h
* vw_pq python test fix
* new memcpy test, REMOVE
* check without alignment
* revert macros for uint8 arrays
* Revert "revert macros for uint8 arrays"
This reverts commit 581a9db735a42d0d68200bd270d87a8fd34e43fe.
* check assert
* Revert "check assert"
This reverts commit 9e970d029a50597a1718b2bb0260196c050fd77f.
* one more variation
* Revert "one more variation"
This reverts commit f6c0528b7ac7e125750dc0d9445c7ce97f6954b5.
* what about read performance
* Revert "what about read performance"
This reverts commit d2610f90958a816fe7f1822157a84f85e97d9249.
* check struct alignment to word
* check for aligned memcpy again
* cleanup
* add CANPacket structure diagram
* update CANPacket and add USB packet struct
* bugfix + refactoring of EP1
* move dlc_to_len to header
* missed include
* typo...
* MISRA
* fk
* lower MAX_CAN_MSGS_PER_BULK_TRANSFER
* bump CAN_PACKET_VERSION to 2
* bump python lib CAN_PACKET_VERSION to 2
* rename parse_can_buffer to unpack_can_buffer
* CANPacket_t const fields
* Revert "CANPacket_t const fields"
This reverts commit cf91c035b7706a14e317550c5f0501ae3fce7c70.
* test.c relative path
* cleanup
* move macros to safety_declarations
* Refactor pack/unpack funcs and add unittest
* usb_protocol.h
* oops
* Update .github/workflows/test.yaml
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove print from unittest
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2021-11-13 08:36:34 +08:00
|
|
|
print(f" W 0x{address:x}: 0x{dat.hex()}")
|
|
|
|
extended = 1 if address >= 0x800 else 0
|
|
|
|
data_len_code = LEN_TO_DLC[len(dat)]
|
|
|
|
header = bytearray(5)
|
|
|
|
word_4b = address << 3 | extended << 2
|
|
|
|
header[0] = (data_len_code << 4) | (bus << 1)
|
|
|
|
header[1] = word_4b & 0xFF
|
|
|
|
header[2] = (word_4b >> 8) & 0xFF
|
|
|
|
header[3] = (word_4b >> 16) & 0xFF
|
|
|
|
header[4] = (word_4b >> 24) & 0xFF
|
|
|
|
snds[idx] += header + dat
|
|
|
|
if len(snds[idx]) > 256: # Limit chunks to 256 bytes
|
|
|
|
snds.append(b'')
|
|
|
|
idx += 1
|
|
|
|
|
|
|
|
#Apply counter to each 64 byte packet
|
|
|
|
for idx in range(len(snds)):
|
|
|
|
tx = b''
|
|
|
|
counter = 0
|
|
|
|
for i in range (0, len(snds[idx]), 63):
|
|
|
|
tx += bytes([counter]) + snds[idx][i:i+63]
|
|
|
|
counter += 1
|
|
|
|
snds[idx] = tx
|
|
|
|
return snds
|
|
|
|
|
|
|
|
def unpack_can_buffer(dat):
|
|
|
|
ret = []
|
|
|
|
counter = 0
|
|
|
|
tail = bytearray()
|
|
|
|
for i in range(0, len(dat), 64):
|
|
|
|
if counter != dat[i]:
|
|
|
|
print("CAN: LOST RECV PACKET COUNTER")
|
|
|
|
break
|
|
|
|
counter+=1
|
|
|
|
chunk = tail + dat[i+1:i+64]
|
|
|
|
tail = bytearray()
|
|
|
|
pos = 0
|
|
|
|
while pos<len(chunk):
|
|
|
|
data_len = DLC_TO_LEN[(chunk[pos]>>4)]
|
|
|
|
pckt_len = CANPACKET_HEAD_SIZE + data_len
|
|
|
|
if pckt_len <= len(chunk[pos:]):
|
|
|
|
header = chunk[pos:pos+CANPACKET_HEAD_SIZE]
|
|
|
|
if len(header) < 5:
|
|
|
|
print("CAN: MALFORMED USB RECV PACKET")
|
|
|
|
break
|
|
|
|
bus = (header[0] >> 1) & 0x7
|
|
|
|
address = (header[4] << 24 | header[3] << 16 | header[2] << 8 | header[1]) >> 3
|
|
|
|
returned = (header[1] >> 1) & 0x1
|
|
|
|
rejected = header[1] & 0x1
|
|
|
|
data = chunk[pos + CANPACKET_HEAD_SIZE:pos + CANPACKET_HEAD_SIZE + data_len]
|
|
|
|
if returned:
|
|
|
|
bus += 128
|
|
|
|
if rejected:
|
|
|
|
bus += 192
|
|
|
|
if DEBUG:
|
|
|
|
print(f" R 0x{address:x}: 0x{data.hex()}")
|
|
|
|
ret.append((address, 0, data, bus))
|
|
|
|
pos += pckt_len
|
|
|
|
else:
|
|
|
|
tail = chunk[pos:]
|
|
|
|
break
|
2017-05-17 08:01:29 +08:00
|
|
|
return ret
|
|
|
|
|
2021-11-11 21:30:52 +08:00
|
|
|
def ensure_health_packet_version(fn):
|
|
|
|
@wraps(fn)
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
if self.health_version < self.HEALTH_PACKET_VERSION:
|
|
|
|
raise RuntimeError("Panda firmware has outdated health packet definition. Reflash panda firmware.")
|
|
|
|
elif self.health_version > self.HEALTH_PACKET_VERSION:
|
|
|
|
raise RuntimeError("Panda python library has outdated health packet definition. Update panda python library.")
|
|
|
|
return fn(self, *args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
def ensure_can_packet_version(fn):
|
|
|
|
@wraps(fn)
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
if self.can_version < self.CAN_PACKET_VERSION:
|
|
|
|
raise RuntimeError("Panda firmware has outdated CAN packet definition. Reflash panda firmware.")
|
|
|
|
elif self.can_version > self.CAN_PACKET_VERSION:
|
|
|
|
raise RuntimeError("Panda python library has outdated CAN packet definition. Update panda python library.")
|
|
|
|
return fn(self, *args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
class Panda(object):
|
2019-10-03 09:20:32 +08:00
|
|
|
|
|
|
|
# matches cereal.car.CarParams.SafetyModel
|
2019-11-21 03:56:26 +08:00
|
|
|
SAFETY_SILENT = 0
|
2019-12-21 07:51:32 +08:00
|
|
|
SAFETY_HONDA_NIDEC = 1
|
2017-10-01 10:20:33 +08:00
|
|
|
SAFETY_TOYOTA = 2
|
2019-10-03 09:20:32 +08:00
|
|
|
SAFETY_ELM327 = 3
|
|
|
|
SAFETY_GM = 4
|
2019-12-21 07:51:32 +08:00
|
|
|
SAFETY_HONDA_BOSCH_GIRAFFE = 5
|
2019-10-03 09:20:32 +08:00
|
|
|
SAFETY_FORD = 6
|
|
|
|
SAFETY_HYUNDAI = 8
|
2018-11-07 04:28:33 +08:00
|
|
|
SAFETY_CHRYSLER = 9
|
2019-10-03 09:20:32 +08:00
|
|
|
SAFETY_TESLA = 10
|
|
|
|
SAFETY_SUBARU = 11
|
|
|
|
SAFETY_MAZDA = 13
|
2020-02-27 03:11:56 +08:00
|
|
|
SAFETY_NISSAN = 14
|
2020-02-20 04:37:38 +08:00
|
|
|
SAFETY_VOLKSWAGEN_MQB = 15
|
2019-10-03 09:20:32 +08:00
|
|
|
SAFETY_ALLOUTPUT = 17
|
|
|
|
SAFETY_GM_ASCM = 18
|
2019-11-21 03:56:26 +08:00
|
|
|
SAFETY_NOOUTPUT = 19
|
2021-11-22 06:51:47 +08:00
|
|
|
SAFETY_HONDA_BOSCH = 20
|
2020-04-02 03:38:27 +08:00
|
|
|
SAFETY_VOLKSWAGEN_PQ = 21
|
2020-02-28 07:28:24 +08:00
|
|
|
SAFETY_SUBARU_LEGACY = 22
|
2020-06-13 09:03:31 +08:00
|
|
|
SAFETY_HYUNDAI_LEGACY = 23
|
2017-07-18 06:29:31 +08:00
|
|
|
|
2017-07-19 03:15:19 +08:00
|
|
|
SERIAL_DEBUG = 0
|
|
|
|
SERIAL_ESP = 1
|
|
|
|
SERIAL_LIN1 = 2
|
|
|
|
SERIAL_LIN2 = 3
|
|
|
|
|
2017-07-19 12:05:09 +08:00
|
|
|
GMLAN_CAN2 = 1
|
|
|
|
GMLAN_CAN3 = 2
|
|
|
|
|
2017-07-18 09:59:16 +08:00
|
|
|
REQUEST_IN = usb1.ENDPOINT_IN | usb1.TYPE_VENDOR | usb1.RECIPIENT_DEVICE
|
2017-07-18 09:58:04 +08:00
|
|
|
REQUEST_OUT = usb1.ENDPOINT_OUT | usb1.TYPE_VENDOR | usb1.RECIPIENT_DEVICE
|
2017-06-14 09:50:47 +08:00
|
|
|
|
2019-09-28 06:22:13 +08:00
|
|
|
HW_TYPE_UNKNOWN = b'\x00'
|
|
|
|
HW_TYPE_WHITE_PANDA = b'\x01'
|
|
|
|
HW_TYPE_GREY_PANDA = b'\x02'
|
|
|
|
HW_TYPE_BLACK_PANDA = b'\x03'
|
|
|
|
HW_TYPE_PEDAL = b'\x04'
|
2019-10-26 07:22:42 +08:00
|
|
|
HW_TYPE_UNO = b'\x05'
|
2021-07-08 15:37:14 +08:00
|
|
|
HW_TYPE_DOS = b'\x06'
|
2021-08-03 11:26:15 +08:00
|
|
|
HW_TYPE_RED_PANDA = b'\x07'
|
|
|
|
|
CAN_FIFOMailBox to CANPacket struct + USB dynamic packet size (#739)
* Squashed commits, no cleanup
* Few fixes
* No init = garbage
* Only receive with new canpacket
* Add send with canpacket
* Revert "Add send with canpacket"
This reverts commit 7d06686ddd6d447c714b5289d31af24403d36931.
* Packet must be aligned to word, or bad performance
* Cleaner
* Fix tests
* Tests...
* MISRA 10.4
* More MISRA
* libpandasafety_py
* cffi
* even more tests...
* typo
* ...
* ...
* ...
* Slight cleanup
* MISRA 6.1
* MISRA 17.7
* Bug in bxcan + even style
* MISRA 10.1
* Revert "MISRA 10.1"
This reverts commit 404ae7fcc39556f80f528de9015702e69f4ea0a5.
* ...
* MISRA 10.1 and 10.4 suppress until next PR
* MISRA 20.1
* ...
* test_honda
* ...
* ...
* test_toyota
* test_volkswagen_mqb
* test_volkswagen_pq
* Sketchy thing...
* Revert "Sketchy thing..."
This reverts commit 3b2e5715bdc1954f7b7b3b7469ba3d0eaa06bdf9.
* remove comment
* bxcan extended address bug
* Concept, experimental dynamic usb packet size
* increase each buffer to 10240 bytes
* raise python bulk read/write limits
* ...
* Move packet size to start
* Experimental send, stream-like
* New receive test, stream-like
* cleanup
* cleanup + rebase fixes
* MISRA
* Extra receive method, stream-like, commented out
* type change
* Revert back to buffer for send, stream commented
* forgot ZLP
* lower buffer, add rx failsafe
* ... remove ZLP
* return ZLP back
* Add tx checks to panda fw
* TX stream with counter
* fix counter overflow
* 13 free slots should be enough
* limit tx usb packet
* ...
* Revert max_bulk_msg doubling
* python lib improve speed
* Stream with counter for RX, dirty, needs cleanup
* Increase chunk length to 4096 bytes
* cleanup fdcan.h
* cleanup __init__.py
* MISRA 12.1
* MISRA 10.8
* remove non-streaming usb functions
* more main.c cleanup
* MISRA 15.6
* MISRA 15.5
* MISRA 18.4 and suppress objectIndex
* handling usb pakcets > 63bytes, naming and cleanup
* Cleanup old from tests and update CANPacket_t struct
* Switch to 4 bit DLC instead of 6 bit length
* ops)
* ...
* pylint
* receive python buffer increase
* USB increase receive packet len
* tweak buffers
* No need for so high limits
* MISRA 20.1 workaround
* performance tweaks
* cleanup, dlc to data_len_code naming
* main.c naming
* comments and cleanup for main.c usb
* clean py lib
* pylint
* do not discard good rx messages on stream fail
* cleanups
* naming
* remove bitstruct lib and lower tx limit
* bitstruct lefovers
* fix bug in VW test
* remove adjusting data size and assert on wrong len
* ...
* test new memcpy before merging
* Revert "test new memcpy before merging"
This reverts commit 399465a264835061adabdd785718c4b6fc18c267.
* macros for to/fromuint8_t array
* MISRA hates me!
* tests.c include macros instead
* move CANPacket to can_definitions.h
* vw_pq python test fix
* new memcpy test, REMOVE
* check without alignment
* revert macros for uint8 arrays
* Revert "revert macros for uint8 arrays"
This reverts commit 581a9db735a42d0d68200bd270d87a8fd34e43fe.
* check assert
* Revert "check assert"
This reverts commit 9e970d029a50597a1718b2bb0260196c050fd77f.
* one more variation
* Revert "one more variation"
This reverts commit f6c0528b7ac7e125750dc0d9445c7ce97f6954b5.
* what about read performance
* Revert "what about read performance"
This reverts commit d2610f90958a816fe7f1822157a84f85e97d9249.
* check struct alignment to word
* check for aligned memcpy again
* cleanup
* add CANPacket structure diagram
* update CANPacket and add USB packet struct
* bugfix + refactoring of EP1
* move dlc_to_len to header
* missed include
* typo...
* MISRA
* fk
* lower MAX_CAN_MSGS_PER_BULK_TRANSFER
* bump CAN_PACKET_VERSION to 2
* bump python lib CAN_PACKET_VERSION to 2
* rename parse_can_buffer to unpack_can_buffer
* CANPacket_t const fields
* Revert "CANPacket_t const fields"
This reverts commit cf91c035b7706a14e317550c5f0501ae3fce7c70.
* test.c relative path
* cleanup
* move macros to safety_declarations
* Refactor pack/unpack funcs and add unittest
* usb_protocol.h
* oops
* Update .github/workflows/test.yaml
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove print from unittest
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2021-11-13 08:36:34 +08:00
|
|
|
CAN_PACKET_VERSION = 2
|
2022-03-25 05:31:31 +08:00
|
|
|
HEALTH_PACKET_VERSION = 4
|
2022-01-28 09:46:24 +08:00
|
|
|
HEALTH_STRUCT = struct.Struct("<IIIIIIIIBBBBBBBHBBBHI")
|
2021-11-11 21:30:52 +08:00
|
|
|
|
2022-02-21 09:13:22 +08:00
|
|
|
F2_DEVICES = (HW_TYPE_PEDAL, )
|
|
|
|
F4_DEVICES = (HW_TYPE_WHITE_PANDA, HW_TYPE_GREY_PANDA, HW_TYPE_BLACK_PANDA, HW_TYPE_UNO, HW_TYPE_DOS)
|
|
|
|
H7_DEVICES = (HW_TYPE_RED_PANDA, )
|
2019-08-29 03:57:42 +08:00
|
|
|
|
2020-08-17 19:02:41 +08:00
|
|
|
CLOCK_SOURCE_MODE_DISABLED = 0
|
|
|
|
CLOCK_SOURCE_MODE_FREE_RUNNING = 1
|
|
|
|
|
2021-06-22 21:29:47 +08:00
|
|
|
FLAG_HONDA_ALT_BRAKE = 1
|
|
|
|
FLAG_HONDA_BOSCH_LONG = 2
|
2021-11-18 09:27:24 +08:00
|
|
|
FLAG_HONDA_NIDEC_ALT = 4
|
|
|
|
|
|
|
|
FLAG_HYUNDAI_EV_GAS = 1
|
|
|
|
FLAG_HYUNDAI_HYBRID_GAS = 2
|
2021-09-14 11:41:10 +08:00
|
|
|
FLAG_HYUNDAI_LONG = 4
|
2021-11-18 20:50:10 +08:00
|
|
|
FLAG_TESLA_POWERTRAIN = 1
|
|
|
|
FLAG_TESLA_LONG_CONTROL = 2
|
2021-06-22 21:29:47 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
def __init__(self, serial=None, claim=True):
|
2017-07-15 03:30:34 +08:00
|
|
|
self._serial = serial
|
2017-07-25 06:16:22 +08:00
|
|
|
self._handle = None
|
|
|
|
self.connect(claim)
|
2021-08-03 11:26:15 +08:00
|
|
|
self._mcu_type = self.get_mcu_type()
|
2017-07-25 06:16:22 +08:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self._handle.close()
|
|
|
|
self._handle = None
|
|
|
|
|
2017-07-30 08:21:22 +08:00
|
|
|
def connect(self, claim=True, wait=False):
|
2020-06-01 16:49:26 +08:00
|
|
|
if self._handle is not None:
|
2017-07-25 06:16:22 +08:00
|
|
|
self.close()
|
|
|
|
|
2021-12-07 06:39:28 +08:00
|
|
|
context = usb1.USBContext()
|
|
|
|
self._handle = None
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2021-12-07 06:39:28 +08:00
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
for device in context.getDeviceList(skip_on_error=True):
|
|
|
|
if device.getVendorID() == 0xbbaa and device.getProductID() in [0xddcc, 0xddee]:
|
|
|
|
try:
|
|
|
|
this_serial = device.getSerialNumber()
|
|
|
|
except Exception:
|
|
|
|
continue
|
|
|
|
if self._serial is None or this_serial == self._serial:
|
|
|
|
self._serial = this_serial
|
|
|
|
print("opening device", self._serial, hex(device.getProductID()))
|
|
|
|
self.bootstub = device.getProductID() == 0xddee
|
|
|
|
self._handle = device.open()
|
|
|
|
if sys.platform not in ["win32", "cygwin", "msys", "darwin"]:
|
|
|
|
self._handle.setAutoDetachKernelDriver(True)
|
|
|
|
if claim:
|
|
|
|
self._handle.claimInterface(0)
|
|
|
|
# self._handle.setInterfaceAltSetting(0, 0) # Issue in USB stack
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
print("exception", e)
|
|
|
|
traceback.print_exc()
|
|
|
|
if not wait or self._handle is not None:
|
|
|
|
break
|
|
|
|
context = usb1.USBContext() # New context needed so new devices show up
|
2020-06-01 16:49:26 +08:00
|
|
|
assert(self._handle is not None)
|
2021-11-11 21:30:52 +08:00
|
|
|
self.health_version, self.can_version = self.get_packets_versions()
|
2017-07-30 08:21:22 +08:00
|
|
|
print("connected")
|
2017-07-25 06:16:22 +08:00
|
|
|
|
2018-03-11 03:22:22 +08:00
|
|
|
def reset(self, enter_bootstub=False, enter_bootloader=False):
|
2017-07-25 06:16:22 +08:00
|
|
|
try:
|
2017-07-28 05:29:07 +08:00
|
|
|
if enter_bootloader:
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_IN, 0xd1, 0, 0, b'')
|
2017-07-25 06:16:22 +08:00
|
|
|
else:
|
2017-07-28 05:29:07 +08:00
|
|
|
if enter_bootstub:
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_IN, 0xd1, 1, 0, b'')
|
|
|
|
else:
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_IN, 0xd8, 0, 0, b'')
|
2017-07-25 06:16:22 +08:00
|
|
|
except Exception:
|
|
|
|
pass
|
2017-07-28 05:29:07 +08:00
|
|
|
if not enter_bootloader:
|
2018-03-11 03:22:22 +08:00
|
|
|
self.reconnect()
|
|
|
|
|
|
|
|
def reconnect(self):
|
|
|
|
self.close()
|
|
|
|
time.sleep(1.0)
|
|
|
|
success = False
|
|
|
|
# wait up to 15 seconds
|
|
|
|
for i in range(0, 15):
|
|
|
|
try:
|
|
|
|
self.connect()
|
|
|
|
success = True
|
|
|
|
break
|
|
|
|
except Exception:
|
2020-06-01 16:49:26 +08:00
|
|
|
print("reconnecting is taking %d seconds..." % (i + 1))
|
2017-08-26 03:02:26 +08:00
|
|
|
try:
|
2021-08-03 11:26:15 +08:00
|
|
|
dfu = PandaDFU(PandaDFU.st_serial_to_dfu_serial(self._serial, self._mcu_type))
|
2018-03-11 03:22:22 +08:00
|
|
|
dfu.recover()
|
2017-08-26 03:02:26 +08:00
|
|
|
except Exception:
|
2018-03-11 03:22:22 +08:00
|
|
|
pass
|
|
|
|
time.sleep(1.0)
|
|
|
|
if not success:
|
|
|
|
raise Exception("reconnect failed")
|
|
|
|
|
2021-11-11 21:30:52 +08:00
|
|
|
|
|
|
|
|
2018-03-11 03:22:22 +08:00
|
|
|
@staticmethod
|
|
|
|
def flash_static(handle, code):
|
|
|
|
# confirm flasher is present
|
|
|
|
fr = handle.controlRead(Panda.REQUEST_IN, 0xb0, 0, 0, 0xc)
|
2019-09-26 07:52:03 +08:00
|
|
|
assert fr[4:8] == b"\xde\xad\xd0\x0d"
|
2018-03-11 03:22:22 +08:00
|
|
|
|
|
|
|
# unlock flash
|
|
|
|
print("flash: unlocking")
|
|
|
|
handle.controlWrite(Panda.REQUEST_IN, 0xb1, 0, 0, b'')
|
|
|
|
|
2019-07-24 06:07:06 +08:00
|
|
|
# erase sectors 1 through 3
|
2018-03-11 03:22:22 +08:00
|
|
|
print("flash: erasing")
|
2019-07-24 06:07:06 +08:00
|
|
|
for i in range(1, 4):
|
|
|
|
handle.controlWrite(Panda.REQUEST_IN, 0xb2, i, 0, b'')
|
2018-03-11 03:22:22 +08:00
|
|
|
|
|
|
|
# flash over EP2
|
|
|
|
STEP = 0x10
|
|
|
|
print("flash: flashing")
|
|
|
|
for i in range(0, len(code), STEP):
|
2020-06-01 16:49:26 +08:00
|
|
|
handle.bulkWrite(2, code[i:i + STEP])
|
2018-03-11 03:22:22 +08:00
|
|
|
|
|
|
|
# reset
|
|
|
|
print("flash: resetting")
|
|
|
|
try:
|
|
|
|
handle.controlWrite(Panda.REQUEST_IN, 0xd8, 0, 0, b'')
|
|
|
|
except Exception:
|
|
|
|
pass
|
2017-08-23 00:30:44 +08:00
|
|
|
|
2021-03-24 21:44:32 +08:00
|
|
|
def flash(self, fn=DEFAULT_FW_FN, code=None, reconnect=True):
|
2021-08-03 11:26:15 +08:00
|
|
|
if self._mcu_type == MCU_TYPE_H7 and fn == DEFAULT_FW_FN:
|
|
|
|
fn = DEFAULT_H7_FW_FN
|
2019-03-14 11:58:00 +08:00
|
|
|
print("flash: main version is " + self.get_version())
|
2017-07-25 06:16:22 +08:00
|
|
|
if not self.bootstub:
|
2017-07-28 05:29:07 +08:00
|
|
|
self.reset(enter_bootstub=True)
|
2017-07-25 06:16:22 +08:00
|
|
|
assert(self.bootstub)
|
|
|
|
|
2017-08-23 04:36:20 +08:00
|
|
|
if code is None:
|
2019-09-26 07:52:03 +08:00
|
|
|
with open(fn, "rb") as f:
|
2017-08-23 04:36:20 +08:00
|
|
|
code = f.read()
|
2017-07-25 06:16:22 +08:00
|
|
|
|
2017-08-23 00:30:44 +08:00
|
|
|
# get version
|
2019-03-14 11:58:00 +08:00
|
|
|
print("flash: bootstub version is " + self.get_version())
|
2017-08-23 00:30:44 +08:00
|
|
|
|
2018-03-11 03:22:22 +08:00
|
|
|
# do flash
|
|
|
|
Panda.flash_static(self._handle, code)
|
2017-07-25 06:16:22 +08:00
|
|
|
|
2018-03-11 03:22:22 +08:00
|
|
|
# reconnect
|
|
|
|
if reconnect:
|
|
|
|
self.reconnect()
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2019-04-10 05:09:18 +08:00
|
|
|
def recover(self, timeout=None):
|
2020-04-14 06:12:06 +08:00
|
|
|
self.reset(enter_bootstub=True)
|
2017-08-29 01:42:23 +08:00
|
|
|
self.reset(enter_bootloader=True)
|
2019-04-10 05:09:18 +08:00
|
|
|
t_start = time.time()
|
2017-08-29 01:42:23 +08:00
|
|
|
while len(PandaDFU.list()) == 0:
|
|
|
|
print("waiting for DFU...")
|
|
|
|
time.sleep(0.1)
|
2019-04-10 05:09:18 +08:00
|
|
|
if timeout is not None and (time.time() - t_start) > timeout:
|
|
|
|
return False
|
2017-08-29 01:42:23 +08:00
|
|
|
|
2021-08-03 11:26:15 +08:00
|
|
|
dfu = PandaDFU(PandaDFU.st_serial_to_dfu_serial(self._serial, self._mcu_type))
|
2017-08-29 01:42:23 +08:00
|
|
|
dfu.recover()
|
|
|
|
|
|
|
|
# reflash after recover
|
|
|
|
self.connect(True, True)
|
|
|
|
self.flash()
|
2019-04-10 05:09:18 +08:00
|
|
|
return True
|
2017-08-29 01:42:23 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
@staticmethod
|
|
|
|
def list():
|
|
|
|
context = usb1.USBContext()
|
|
|
|
ret = []
|
2017-09-13 11:49:28 +08:00
|
|
|
try:
|
|
|
|
for device in context.getDeviceList(skip_on_error=True):
|
|
|
|
if device.getVendorID() == 0xbbaa and device.getProductID() in [0xddcc, 0xddee]:
|
|
|
|
try:
|
|
|
|
ret.append(device.getSerialNumber())
|
|
|
|
except Exception:
|
|
|
|
continue
|
|
|
|
except Exception:
|
2017-09-13 11:51:05 +08:00
|
|
|
pass
|
2017-04-07 09:11:36 +08:00
|
|
|
return ret
|
|
|
|
|
2017-07-19 03:29:16 +08:00
|
|
|
def call_control_api(self, msg):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, msg, 0, 0, b'')
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
# ******************* health *******************
|
|
|
|
|
2021-11-11 21:30:52 +08:00
|
|
|
@ensure_health_packet_version
|
2017-04-07 09:11:36 +08:00
|
|
|
def health(self):
|
2022-01-28 09:46:24 +08:00
|
|
|
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xd2, 0, 0, self.HEALTH_STRUCT.size)
|
|
|
|
a = self.HEALTH_STRUCT.unpack(dat)
|
2019-07-24 06:07:06 +08:00
|
|
|
return {
|
2019-11-22 08:47:49 +08:00
|
|
|
"uptime": a[0],
|
|
|
|
"voltage": a[1],
|
|
|
|
"current": a[2],
|
2019-12-20 10:16:31 +08:00
|
|
|
"can_rx_errs": a[3],
|
|
|
|
"can_send_errs": a[4],
|
|
|
|
"can_fwd_errs": a[5],
|
|
|
|
"gmlan_send_errs": a[6],
|
|
|
|
"faults": a[7],
|
|
|
|
"ignition_line": a[8],
|
|
|
|
"ignition_can": a[9],
|
|
|
|
"controls_allowed": a[10],
|
|
|
|
"gas_interceptor_detected": a[11],
|
|
|
|
"car_harness_status": a[12],
|
|
|
|
"usb_power_mode": a[13],
|
|
|
|
"safety_mode": a[14],
|
2021-05-03 20:59:36 +08:00
|
|
|
"safety_param": a[15],
|
|
|
|
"fault_status": a[16],
|
2021-06-05 13:07:45 +08:00
|
|
|
"power_save_enabled": a[17],
|
|
|
|
"heartbeat_lost": a[18],
|
2022-03-25 05:31:31 +08:00
|
|
|
"alternative_experience": a[19],
|
2022-01-28 09:46:24 +08:00
|
|
|
"blocked_msg_cnt": a[20],
|
2019-07-24 06:07:06 +08:00
|
|
|
}
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-04-29 07:56:40 +08:00
|
|
|
# ******************* control *******************
|
|
|
|
|
|
|
|
def enter_bootloader(self):
|
|
|
|
try:
|
2017-07-18 09:58:04 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xd1, 0, 0, b'')
|
2017-06-14 09:50:47 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2017-04-29 07:56:40 +08:00
|
|
|
|
2017-08-23 00:30:44 +08:00
|
|
|
def get_version(self):
|
2019-09-26 07:52:51 +08:00
|
|
|
return self._handle.controlRead(Panda.REQUEST_IN, 0xd6, 0, 0, 0x40).decode('utf8')
|
2017-08-23 00:30:44 +08:00
|
|
|
|
2019-12-10 02:54:48 +08:00
|
|
|
@staticmethod
|
|
|
|
def get_signature_from_firmware(fn):
|
|
|
|
f = open(fn, 'rb')
|
|
|
|
f.seek(-128, 2) # Seek from end of file
|
|
|
|
return f.read(128)
|
|
|
|
|
|
|
|
def get_signature(self):
|
|
|
|
part_1 = self._handle.controlRead(Panda.REQUEST_IN, 0xd3, 0, 0, 0x40)
|
|
|
|
part_2 = self._handle.controlRead(Panda.REQUEST_IN, 0xd4, 0, 0, 0x40)
|
2019-12-19 09:03:31 +08:00
|
|
|
return bytes(part_1 + part_2)
|
2019-12-10 02:54:48 +08:00
|
|
|
|
2019-07-24 06:07:06 +08:00
|
|
|
def get_type(self):
|
|
|
|
return self._handle.controlRead(Panda.REQUEST_IN, 0xc1, 0, 0, 0x40)
|
|
|
|
|
2021-11-11 21:30:52 +08:00
|
|
|
# Returns tuple with health packet version and CAN packet/USB packet version
|
|
|
|
def get_packets_versions(self):
|
|
|
|
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xdd, 0, 0, 2)
|
|
|
|
if dat:
|
|
|
|
a = struct.unpack("BB", dat)
|
|
|
|
return (a[0], a[1])
|
|
|
|
else:
|
|
|
|
return (0, 0)
|
|
|
|
|
2019-08-29 03:57:42 +08:00
|
|
|
def is_white(self):
|
|
|
|
return self.get_type() == Panda.HW_TYPE_WHITE_PANDA
|
|
|
|
|
2018-02-17 03:07:20 +08:00
|
|
|
def is_grey(self):
|
2019-08-29 03:57:42 +08:00
|
|
|
return self.get_type() == Panda.HW_TYPE_GREY_PANDA
|
2019-07-24 06:07:06 +08:00
|
|
|
|
|
|
|
def is_black(self):
|
2019-08-29 03:57:42 +08:00
|
|
|
return self.get_type() == Panda.HW_TYPE_BLACK_PANDA
|
2018-02-17 03:07:20 +08:00
|
|
|
|
2021-07-03 05:27:04 +08:00
|
|
|
def is_pedal(self):
|
|
|
|
return self.get_type() == Panda.HW_TYPE_PEDAL
|
|
|
|
|
2019-10-26 07:22:42 +08:00
|
|
|
def is_uno(self):
|
|
|
|
return self.get_type() == Panda.HW_TYPE_UNO
|
|
|
|
|
2021-07-08 15:36:25 +08:00
|
|
|
def is_dos(self):
|
|
|
|
return self.get_type() == Panda.HW_TYPE_DOS
|
2021-11-11 21:30:52 +08:00
|
|
|
|
2021-08-03 11:26:15 +08:00
|
|
|
def is_red(self):
|
|
|
|
return self.get_type() == Panda.HW_TYPE_RED_PANDA
|
|
|
|
|
|
|
|
def get_mcu_type(self):
|
|
|
|
hw_type = self.get_type()
|
|
|
|
if hw_type in Panda.F2_DEVICES:
|
|
|
|
return MCU_TYPE_F2
|
|
|
|
elif hw_type in Panda.F4_DEVICES:
|
|
|
|
return MCU_TYPE_F4
|
|
|
|
elif hw_type in Panda.H7_DEVICES:
|
|
|
|
return MCU_TYPE_H7
|
|
|
|
return None
|
2021-07-08 15:36:25 +08:00
|
|
|
|
2019-11-06 10:25:42 +08:00
|
|
|
def has_obd(self):
|
2021-08-03 11:26:15 +08:00
|
|
|
return (self.is_uno() or self.is_dos() or self.is_black() or self.is_red())
|
|
|
|
|
|
|
|
def has_canfd(self):
|
2022-02-21 09:13:22 +08:00
|
|
|
return self.get_type() in Panda.H7_DEVICES
|
2019-11-06 10:25:42 +08:00
|
|
|
|
2021-10-08 18:10:28 +08:00
|
|
|
def is_internal(self):
|
|
|
|
return self.get_type() in [Panda.HW_TYPE_UNO, Panda.HW_TYPE_DOS]
|
|
|
|
|
2017-04-29 07:56:40 +08:00
|
|
|
def get_serial(self):
|
2017-07-18 09:59:16 +08:00
|
|
|
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xd0, 0, 0, 0x20)
|
2017-06-14 09:50:47 +08:00
|
|
|
hashsig, calc_hash = dat[0x1c:], hashlib.sha1(dat[0:0x1c]).digest()[0:4]
|
2017-07-18 10:43:30 +08:00
|
|
|
assert(hashsig == calc_hash)
|
2020-06-01 16:49:26 +08:00
|
|
|
return [dat[0:0x10].decode("utf8"), dat[0x10:0x10 + 10].decode("utf8")]
|
2017-04-29 07:56:40 +08:00
|
|
|
|
2021-10-08 18:17:06 +08:00
|
|
|
def get_usb_serial(self):
|
|
|
|
return self._serial
|
|
|
|
|
2017-04-29 08:49:55 +08:00
|
|
|
def get_secret(self):
|
2017-07-18 09:59:16 +08:00
|
|
|
return self._handle.controlRead(Panda.REQUEST_IN, 0xd0, 1, 0, 0x10)
|
2017-04-29 08:49:55 +08:00
|
|
|
|
2017-04-26 06:16:23 +08:00
|
|
|
# ******************* configuration *******************
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-08-24 00:28:52 +08:00
|
|
|
def set_usb_power(self, on):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe6, int(on), 0, b'')
|
|
|
|
|
2019-11-22 08:47:49 +08:00
|
|
|
def set_power_save(self, power_save_enabled=0):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe7, int(power_save_enabled), 0, b'')
|
|
|
|
|
2022-03-08 06:32:14 +08:00
|
|
|
def enable_deepsleep(self):
|
2022-03-08 06:24:04 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xfb, 0, 0, b'')
|
|
|
|
|
2017-07-18 06:29:31 +08:00
|
|
|
def set_esp_power(self, on):
|
2017-07-18 09:58:04 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xd9, int(on), 0, b'')
|
2017-07-15 12:17:32 +08:00
|
|
|
|
2017-08-23 05:17:59 +08:00
|
|
|
def esp_reset(self, bootmode=0):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xda, int(bootmode), 0, b'')
|
|
|
|
time.sleep(0.2)
|
|
|
|
|
2022-02-21 11:38:08 +08:00
|
|
|
def set_safety_mode(self, mode=SAFETY_SILENT, param=0, disable_checks=True):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xdc, mode, param, b'')
|
2021-09-24 14:12:36 +08:00
|
|
|
if disable_checks:
|
2021-06-22 04:54:09 +08:00
|
|
|
self.set_heartbeat_disabled()
|
2021-09-24 14:12:36 +08:00
|
|
|
self.set_power_save(0)
|
2017-07-15 12:59:23 +08:00
|
|
|
|
2017-07-19 12:05:09 +08:00
|
|
|
def set_gmlan(self, bus=2):
|
2019-07-24 06:07:06 +08:00
|
|
|
# TODO: check panda type
|
2017-07-19 12:05:09 +08:00
|
|
|
if bus is None:
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xdb, 0, 0, b'')
|
|
|
|
elif bus in [Panda.GMLAN_CAN2, Panda.GMLAN_CAN3]:
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xdb, 1, bus, b'')
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2019-07-24 06:07:06 +08:00
|
|
|
def set_obd(self, obd):
|
|
|
|
# TODO: check panda type
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xdb, int(obd), 0, b'')
|
|
|
|
|
2017-07-15 11:25:13 +08:00
|
|
|
def set_can_loopback(self, enable):
|
2017-07-18 06:29:31 +08:00
|
|
|
# set can loopback mode for all buses
|
2017-07-18 09:58:04 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe5, int(enable), 0, b'')
|
2017-07-15 11:25:13 +08:00
|
|
|
|
2019-04-02 13:45:00 +08:00
|
|
|
def set_can_enable(self, bus_num, enable):
|
2020-09-28 22:19:09 +08:00
|
|
|
# sets the can transceiver enable pin
|
2019-04-02 13:45:00 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf4, int(bus_num), int(enable), b'')
|
|
|
|
|
2017-07-18 10:43:30 +08:00
|
|
|
def set_can_speed_kbps(self, bus, speed):
|
2020-06-01 16:49:26 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xde, bus, int(speed * 10), b'')
|
2017-07-18 10:43:30 +08:00
|
|
|
|
2021-08-03 11:26:15 +08:00
|
|
|
def set_can_data_speed_kbps(self, bus, speed):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf9, bus, int(speed * 10), b'')
|
|
|
|
|
2022-02-21 09:13:22 +08:00
|
|
|
# CAN FD and BRS status
|
2021-11-16 08:27:16 +08:00
|
|
|
def get_canfd_status(self, bus):
|
|
|
|
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xfa, bus, 0, 2)
|
|
|
|
if dat:
|
|
|
|
a = struct.unpack("BB", dat)
|
|
|
|
return (a[0], a[1])
|
|
|
|
else:
|
|
|
|
return (None, None)
|
|
|
|
|
2017-04-26 06:16:23 +08:00
|
|
|
def set_uart_baud(self, uart, rate):
|
2020-06-01 16:49:26 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe4, uart, int(rate / 300), b'')
|
2017-04-26 06:16:23 +08:00
|
|
|
|
|
|
|
def set_uart_parity(self, uart, parity):
|
|
|
|
# parity, 0=off, 1=even, 2=odd
|
2017-07-18 09:58:04 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe2, uart, parity, b'')
|
2017-04-26 06:16:23 +08:00
|
|
|
|
2017-04-26 11:23:05 +08:00
|
|
|
def set_uart_callback(self, uart, install):
|
2017-07-18 09:58:04 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe3, uart, int(install), b'')
|
2017-04-26 11:23:05 +08:00
|
|
|
|
2017-04-26 06:16:23 +08:00
|
|
|
# ******************* can *******************
|
|
|
|
|
2020-04-14 06:12:06 +08:00
|
|
|
# The panda will NAK CAN writes when there is CAN congestion.
|
2020-04-07 07:49:42 +08:00
|
|
|
# libusb will try to send it again, with a max timeout.
|
|
|
|
# Timeout is in ms. If set to 0, the timeout is infinite.
|
|
|
|
CAN_SEND_TIMEOUT_MS = 10
|
|
|
|
|
2021-11-11 21:30:52 +08:00
|
|
|
@ensure_can_packet_version
|
2020-04-07 07:49:42 +08:00
|
|
|
def can_send_many(self, arr, timeout=CAN_SEND_TIMEOUT_MS):
|
CAN_FIFOMailBox to CANPacket struct + USB dynamic packet size (#739)
* Squashed commits, no cleanup
* Few fixes
* No init = garbage
* Only receive with new canpacket
* Add send with canpacket
* Revert "Add send with canpacket"
This reverts commit 7d06686ddd6d447c714b5289d31af24403d36931.
* Packet must be aligned to word, or bad performance
* Cleaner
* Fix tests
* Tests...
* MISRA 10.4
* More MISRA
* libpandasafety_py
* cffi
* even more tests...
* typo
* ...
* ...
* ...
* Slight cleanup
* MISRA 6.1
* MISRA 17.7
* Bug in bxcan + even style
* MISRA 10.1
* Revert "MISRA 10.1"
This reverts commit 404ae7fcc39556f80f528de9015702e69f4ea0a5.
* ...
* MISRA 10.1 and 10.4 suppress until next PR
* MISRA 20.1
* ...
* test_honda
* ...
* ...
* test_toyota
* test_volkswagen_mqb
* test_volkswagen_pq
* Sketchy thing...
* Revert "Sketchy thing..."
This reverts commit 3b2e5715bdc1954f7b7b3b7469ba3d0eaa06bdf9.
* remove comment
* bxcan extended address bug
* Concept, experimental dynamic usb packet size
* increase each buffer to 10240 bytes
* raise python bulk read/write limits
* ...
* Move packet size to start
* Experimental send, stream-like
* New receive test, stream-like
* cleanup
* cleanup + rebase fixes
* MISRA
* Extra receive method, stream-like, commented out
* type change
* Revert back to buffer for send, stream commented
* forgot ZLP
* lower buffer, add rx failsafe
* ... remove ZLP
* return ZLP back
* Add tx checks to panda fw
* TX stream with counter
* fix counter overflow
* 13 free slots should be enough
* limit tx usb packet
* ...
* Revert max_bulk_msg doubling
* python lib improve speed
* Stream with counter for RX, dirty, needs cleanup
* Increase chunk length to 4096 bytes
* cleanup fdcan.h
* cleanup __init__.py
* MISRA 12.1
* MISRA 10.8
* remove non-streaming usb functions
* more main.c cleanup
* MISRA 15.6
* MISRA 15.5
* MISRA 18.4 and suppress objectIndex
* handling usb pakcets > 63bytes, naming and cleanup
* Cleanup old from tests and update CANPacket_t struct
* Switch to 4 bit DLC instead of 6 bit length
* ops)
* ...
* pylint
* receive python buffer increase
* USB increase receive packet len
* tweak buffers
* No need for so high limits
* MISRA 20.1 workaround
* performance tweaks
* cleanup, dlc to data_len_code naming
* main.c naming
* comments and cleanup for main.c usb
* clean py lib
* pylint
* do not discard good rx messages on stream fail
* cleanups
* naming
* remove bitstruct lib and lower tx limit
* bitstruct lefovers
* fix bug in VW test
* remove adjusting data size and assert on wrong len
* ...
* test new memcpy before merging
* Revert "test new memcpy before merging"
This reverts commit 399465a264835061adabdd785718c4b6fc18c267.
* macros for to/fromuint8_t array
* MISRA hates me!
* tests.c include macros instead
* move CANPacket to can_definitions.h
* vw_pq python test fix
* new memcpy test, REMOVE
* check without alignment
* revert macros for uint8 arrays
* Revert "revert macros for uint8 arrays"
This reverts commit 581a9db735a42d0d68200bd270d87a8fd34e43fe.
* check assert
* Revert "check assert"
This reverts commit 9e970d029a50597a1718b2bb0260196c050fd77f.
* one more variation
* Revert "one more variation"
This reverts commit f6c0528b7ac7e125750dc0d9445c7ce97f6954b5.
* what about read performance
* Revert "what about read performance"
This reverts commit d2610f90958a816fe7f1822157a84f85e97d9249.
* check struct alignment to word
* check for aligned memcpy again
* cleanup
* add CANPacket structure diagram
* update CANPacket and add USB packet struct
* bugfix + refactoring of EP1
* move dlc_to_len to header
* missed include
* typo...
* MISRA
* fk
* lower MAX_CAN_MSGS_PER_BULK_TRANSFER
* bump CAN_PACKET_VERSION to 2
* bump python lib CAN_PACKET_VERSION to 2
* rename parse_can_buffer to unpack_can_buffer
* CANPacket_t const fields
* Revert "CANPacket_t const fields"
This reverts commit cf91c035b7706a14e317550c5f0501ae3fce7c70.
* test.c relative path
* cleanup
* move macros to safety_declarations
* Refactor pack/unpack funcs and add unittest
* usb_protocol.h
* oops
* Update .github/workflows/test.yaml
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove print from unittest
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2021-11-13 08:36:34 +08:00
|
|
|
snds = pack_can_buffer(arr)
|
2017-06-14 09:50:47 +08:00
|
|
|
while True:
|
2017-04-07 09:11:36 +08:00
|
|
|
try:
|
2021-12-07 06:39:28 +08:00
|
|
|
for tx in snds:
|
|
|
|
while True:
|
|
|
|
bs = self._handle.bulkWrite(3, tx, timeout=timeout)
|
|
|
|
tx = tx[bs:]
|
|
|
|
if len(tx) == 0:
|
|
|
|
break
|
|
|
|
print("CAN: PARTIAL SEND MANY, RETRYING")
|
2017-04-07 09:11:36 +08:00
|
|
|
break
|
2017-06-14 09:50:47 +08:00
|
|
|
except (usb1.USBErrorIO, usb1.USBErrorOverflow):
|
|
|
|
print("CAN: BAD SEND MANY, RETRYING")
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2020-04-07 07:49:42 +08:00
|
|
|
def can_send(self, addr, dat, bus, timeout=CAN_SEND_TIMEOUT_MS):
|
|
|
|
self.can_send_many([[addr, None, dat, bus]], timeout=timeout)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2021-11-11 21:30:52 +08:00
|
|
|
@ensure_can_packet_version
|
2017-04-07 09:11:36 +08:00
|
|
|
def can_recv(self):
|
2017-06-14 09:50:47 +08:00
|
|
|
dat = bytearray()
|
|
|
|
while True:
|
2017-04-07 09:11:36 +08:00
|
|
|
try:
|
CAN_FIFOMailBox to CANPacket struct + USB dynamic packet size (#739)
* Squashed commits, no cleanup
* Few fixes
* No init = garbage
* Only receive with new canpacket
* Add send with canpacket
* Revert "Add send with canpacket"
This reverts commit 7d06686ddd6d447c714b5289d31af24403d36931.
* Packet must be aligned to word, or bad performance
* Cleaner
* Fix tests
* Tests...
* MISRA 10.4
* More MISRA
* libpandasafety_py
* cffi
* even more tests...
* typo
* ...
* ...
* ...
* Slight cleanup
* MISRA 6.1
* MISRA 17.7
* Bug in bxcan + even style
* MISRA 10.1
* Revert "MISRA 10.1"
This reverts commit 404ae7fcc39556f80f528de9015702e69f4ea0a5.
* ...
* MISRA 10.1 and 10.4 suppress until next PR
* MISRA 20.1
* ...
* test_honda
* ...
* ...
* test_toyota
* test_volkswagen_mqb
* test_volkswagen_pq
* Sketchy thing...
* Revert "Sketchy thing..."
This reverts commit 3b2e5715bdc1954f7b7b3b7469ba3d0eaa06bdf9.
* remove comment
* bxcan extended address bug
* Concept, experimental dynamic usb packet size
* increase each buffer to 10240 bytes
* raise python bulk read/write limits
* ...
* Move packet size to start
* Experimental send, stream-like
* New receive test, stream-like
* cleanup
* cleanup + rebase fixes
* MISRA
* Extra receive method, stream-like, commented out
* type change
* Revert back to buffer for send, stream commented
* forgot ZLP
* lower buffer, add rx failsafe
* ... remove ZLP
* return ZLP back
* Add tx checks to panda fw
* TX stream with counter
* fix counter overflow
* 13 free slots should be enough
* limit tx usb packet
* ...
* Revert max_bulk_msg doubling
* python lib improve speed
* Stream with counter for RX, dirty, needs cleanup
* Increase chunk length to 4096 bytes
* cleanup fdcan.h
* cleanup __init__.py
* MISRA 12.1
* MISRA 10.8
* remove non-streaming usb functions
* more main.c cleanup
* MISRA 15.6
* MISRA 15.5
* MISRA 18.4 and suppress objectIndex
* handling usb pakcets > 63bytes, naming and cleanup
* Cleanup old from tests and update CANPacket_t struct
* Switch to 4 bit DLC instead of 6 bit length
* ops)
* ...
* pylint
* receive python buffer increase
* USB increase receive packet len
* tweak buffers
* No need for so high limits
* MISRA 20.1 workaround
* performance tweaks
* cleanup, dlc to data_len_code naming
* main.c naming
* comments and cleanup for main.c usb
* clean py lib
* pylint
* do not discard good rx messages on stream fail
* cleanups
* naming
* remove bitstruct lib and lower tx limit
* bitstruct lefovers
* fix bug in VW test
* remove adjusting data size and assert on wrong len
* ...
* test new memcpy before merging
* Revert "test new memcpy before merging"
This reverts commit 399465a264835061adabdd785718c4b6fc18c267.
* macros for to/fromuint8_t array
* MISRA hates me!
* tests.c include macros instead
* move CANPacket to can_definitions.h
* vw_pq python test fix
* new memcpy test, REMOVE
* check without alignment
* revert macros for uint8 arrays
* Revert "revert macros for uint8 arrays"
This reverts commit 581a9db735a42d0d68200bd270d87a8fd34e43fe.
* check assert
* Revert "check assert"
This reverts commit 9e970d029a50597a1718b2bb0260196c050fd77f.
* one more variation
* Revert "one more variation"
This reverts commit f6c0528b7ac7e125750dc0d9445c7ce97f6954b5.
* what about read performance
* Revert "what about read performance"
This reverts commit d2610f90958a816fe7f1822157a84f85e97d9249.
* check struct alignment to word
* check for aligned memcpy again
* cleanup
* add CANPacket structure diagram
* update CANPacket and add USB packet struct
* bugfix + refactoring of EP1
* move dlc_to_len to header
* missed include
* typo...
* MISRA
* fk
* lower MAX_CAN_MSGS_PER_BULK_TRANSFER
* bump CAN_PACKET_VERSION to 2
* bump python lib CAN_PACKET_VERSION to 2
* rename parse_can_buffer to unpack_can_buffer
* CANPacket_t const fields
* Revert "CANPacket_t const fields"
This reverts commit cf91c035b7706a14e317550c5f0501ae3fce7c70.
* test.c relative path
* cleanup
* move macros to safety_declarations
* Refactor pack/unpack funcs and add unittest
* usb_protocol.h
* oops
* Update .github/workflows/test.yaml
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove print from unittest
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2021-11-13 08:36:34 +08:00
|
|
|
dat = self._handle.bulkRead(1, 16384) # Max receive batch size + 2 extra reserve frames
|
2017-04-07 09:11:36 +08:00
|
|
|
break
|
2017-06-14 09:50:47 +08:00
|
|
|
except (usb1.USBErrorIO, usb1.USBErrorOverflow):
|
|
|
|
print("CAN: BAD RECV, RETRYING")
|
2019-09-26 05:34:28 +08:00
|
|
|
time.sleep(0.1)
|
CAN_FIFOMailBox to CANPacket struct + USB dynamic packet size (#739)
* Squashed commits, no cleanup
* Few fixes
* No init = garbage
* Only receive with new canpacket
* Add send with canpacket
* Revert "Add send with canpacket"
This reverts commit 7d06686ddd6d447c714b5289d31af24403d36931.
* Packet must be aligned to word, or bad performance
* Cleaner
* Fix tests
* Tests...
* MISRA 10.4
* More MISRA
* libpandasafety_py
* cffi
* even more tests...
* typo
* ...
* ...
* ...
* Slight cleanup
* MISRA 6.1
* MISRA 17.7
* Bug in bxcan + even style
* MISRA 10.1
* Revert "MISRA 10.1"
This reverts commit 404ae7fcc39556f80f528de9015702e69f4ea0a5.
* ...
* MISRA 10.1 and 10.4 suppress until next PR
* MISRA 20.1
* ...
* test_honda
* ...
* ...
* test_toyota
* test_volkswagen_mqb
* test_volkswagen_pq
* Sketchy thing...
* Revert "Sketchy thing..."
This reverts commit 3b2e5715bdc1954f7b7b3b7469ba3d0eaa06bdf9.
* remove comment
* bxcan extended address bug
* Concept, experimental dynamic usb packet size
* increase each buffer to 10240 bytes
* raise python bulk read/write limits
* ...
* Move packet size to start
* Experimental send, stream-like
* New receive test, stream-like
* cleanup
* cleanup + rebase fixes
* MISRA
* Extra receive method, stream-like, commented out
* type change
* Revert back to buffer for send, stream commented
* forgot ZLP
* lower buffer, add rx failsafe
* ... remove ZLP
* return ZLP back
* Add tx checks to panda fw
* TX stream with counter
* fix counter overflow
* 13 free slots should be enough
* limit tx usb packet
* ...
* Revert max_bulk_msg doubling
* python lib improve speed
* Stream with counter for RX, dirty, needs cleanup
* Increase chunk length to 4096 bytes
* cleanup fdcan.h
* cleanup __init__.py
* MISRA 12.1
* MISRA 10.8
* remove non-streaming usb functions
* more main.c cleanup
* MISRA 15.6
* MISRA 15.5
* MISRA 18.4 and suppress objectIndex
* handling usb pakcets > 63bytes, naming and cleanup
* Cleanup old from tests and update CANPacket_t struct
* Switch to 4 bit DLC instead of 6 bit length
* ops)
* ...
* pylint
* receive python buffer increase
* USB increase receive packet len
* tweak buffers
* No need for so high limits
* MISRA 20.1 workaround
* performance tweaks
* cleanup, dlc to data_len_code naming
* main.c naming
* comments and cleanup for main.c usb
* clean py lib
* pylint
* do not discard good rx messages on stream fail
* cleanups
* naming
* remove bitstruct lib and lower tx limit
* bitstruct lefovers
* fix bug in VW test
* remove adjusting data size and assert on wrong len
* ...
* test new memcpy before merging
* Revert "test new memcpy before merging"
This reverts commit 399465a264835061adabdd785718c4b6fc18c267.
* macros for to/fromuint8_t array
* MISRA hates me!
* tests.c include macros instead
* move CANPacket to can_definitions.h
* vw_pq python test fix
* new memcpy test, REMOVE
* check without alignment
* revert macros for uint8 arrays
* Revert "revert macros for uint8 arrays"
This reverts commit 581a9db735a42d0d68200bd270d87a8fd34e43fe.
* check assert
* Revert "check assert"
This reverts commit 9e970d029a50597a1718b2bb0260196c050fd77f.
* one more variation
* Revert "one more variation"
This reverts commit f6c0528b7ac7e125750dc0d9445c7ce97f6954b5.
* what about read performance
* Revert "what about read performance"
This reverts commit d2610f90958a816fe7f1822157a84f85e97d9249.
* check struct alignment to word
* check for aligned memcpy again
* cleanup
* add CANPacket structure diagram
* update CANPacket and add USB packet struct
* bugfix + refactoring of EP1
* move dlc_to_len to header
* missed include
* typo...
* MISRA
* fk
* lower MAX_CAN_MSGS_PER_BULK_TRANSFER
* bump CAN_PACKET_VERSION to 2
* bump python lib CAN_PACKET_VERSION to 2
* rename parse_can_buffer to unpack_can_buffer
* CANPacket_t const fields
* Revert "CANPacket_t const fields"
This reverts commit cf91c035b7706a14e317550c5f0501ae3fce7c70.
* test.c relative path
* cleanup
* move macros to safety_declarations
* Refactor pack/unpack funcs and add unittest
* usb_protocol.h
* oops
* Update .github/workflows/test.yaml
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove print from unittest
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2021-11-13 08:36:34 +08:00
|
|
|
return unpack_can_buffer(dat)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-08-17 17:03:08 +08:00
|
|
|
def can_clear(self, bus):
|
|
|
|
"""Clears all messages from the specified internal CAN ringbuffer as
|
|
|
|
though it were drained.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
bus (int): can bus number to clear a tx queue, or 0xFFFF to clear the
|
|
|
|
global can rx queue.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf1, bus, 0, b'')
|
|
|
|
|
2018-03-10 09:40:03 +08:00
|
|
|
# ******************* isotp *******************
|
|
|
|
|
2018-03-11 02:16:53 +08:00
|
|
|
def isotp_send(self, addr, dat, bus, recvaddr=None, subaddr=None):
|
2018-03-10 09:40:03 +08:00
|
|
|
return isotp_send(self, dat, addr, bus, recvaddr, subaddr)
|
|
|
|
|
2018-03-11 02:16:53 +08:00
|
|
|
def isotp_recv(self, addr, bus=0, sendaddr=None, subaddr=None):
|
2018-03-11 02:17:57 +08:00
|
|
|
return isotp_recv(self, addr, bus, sendaddr, subaddr)
|
2018-03-10 09:40:03 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
# ******************* serial *******************
|
|
|
|
|
|
|
|
def serial_read(self, port_number):
|
2017-07-19 03:15:19 +08:00
|
|
|
ret = []
|
|
|
|
while 1:
|
|
|
|
lret = bytes(self._handle.controlRead(Panda.REQUEST_IN, 0xe0, port_number, 0, 0x40))
|
|
|
|
if len(lret) == 0:
|
|
|
|
break
|
|
|
|
ret.append(lret)
|
|
|
|
return b''.join(ret)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-04-18 16:12:04 +08:00
|
|
|
def serial_write(self, port_number, ln):
|
2018-01-20 03:04:21 +08:00
|
|
|
ret = 0
|
|
|
|
for i in range(0, len(ln), 0x20):
|
2020-06-01 16:49:26 +08:00
|
|
|
ret += self._handle.bulkWrite(2, struct.pack("B", port_number) + ln[i:i + 0x20])
|
2018-01-20 03:04:21 +08:00
|
|
|
return ret
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-08-17 17:03:08 +08:00
|
|
|
def serial_clear(self, port_number):
|
|
|
|
"""Clears all messages (tx and rx) from the specified internal uart
|
|
|
|
ringbuffer as though it were drained.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
port_number (int): port number of the uart to clear.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf2, port_number, 0, b'')
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# ******************* kline *******************
|
|
|
|
|
|
|
|
# pulse low for wakeup
|
2020-06-19 02:17:00 +08:00
|
|
|
def kline_wakeup(self, k=True, l=True):
|
|
|
|
assert k or l, "must specify k-line, l-line, or both"
|
2018-03-01 09:03:04 +08:00
|
|
|
if DEBUG:
|
|
|
|
print("kline wakeup...")
|
2020-06-19 02:17:00 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf0, 2 if k and l else int(l), 0, b'')
|
2018-03-01 09:03:04 +08:00
|
|
|
if DEBUG:
|
|
|
|
print("kline wakeup done")
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2020-07-11 05:18:24 +08:00
|
|
|
def kline_5baud(self, addr, k=True, l=True):
|
|
|
|
assert k or l, "must specify k-line, l-line, or both"
|
|
|
|
if DEBUG:
|
|
|
|
print("kline 5 baud...")
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf4, 2 if k and l else int(l), addr, b'')
|
|
|
|
if DEBUG:
|
|
|
|
print("kline 5 baud done")
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
def kline_drain(self, bus=2):
|
|
|
|
# drain buffer
|
2017-06-14 09:50:47 +08:00
|
|
|
bret = bytearray()
|
|
|
|
while True:
|
2017-07-18 09:59:16 +08:00
|
|
|
ret = self._handle.controlRead(Panda.REQUEST_IN, 0xe0, bus, 0, 0x40)
|
2017-04-07 09:11:36 +08:00
|
|
|
if len(ret) == 0:
|
|
|
|
break
|
2018-03-01 09:45:54 +08:00
|
|
|
elif DEBUG:
|
2020-06-19 02:17:00 +08:00
|
|
|
print(f"kline drain: 0x{ret.hex()}")
|
2017-06-14 09:50:47 +08:00
|
|
|
bret += ret
|
2017-06-29 05:50:00 +08:00
|
|
|
return bytes(bret)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
def kline_ll_recv(self, cnt, bus=2):
|
2017-06-14 09:50:47 +08:00
|
|
|
echo = bytearray()
|
2017-04-07 09:11:36 +08:00
|
|
|
while len(echo) != cnt:
|
2020-06-01 16:49:26 +08:00
|
|
|
ret = self._handle.controlRead(Panda.REQUEST_OUT, 0xe0, bus, 0, cnt - len(echo))
|
2018-03-01 09:03:04 +08:00
|
|
|
if DEBUG and len(ret) > 0:
|
2020-06-19 02:17:00 +08:00
|
|
|
print(f"kline recv: 0x{ret.hex()}")
|
2018-03-01 09:03:04 +08:00
|
|
|
echo += ret
|
2020-06-15 00:22:39 +08:00
|
|
|
return bytes(echo)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
def kline_send(self, x, bus=2, checksum=True):
|
|
|
|
self.kline_drain(bus=bus)
|
|
|
|
if checksum:
|
2020-06-19 02:17:00 +08:00
|
|
|
x += bytes([sum(x) % 0x100])
|
2017-04-18 22:34:56 +08:00
|
|
|
for i in range(0, len(x), 0xf):
|
2020-06-01 16:49:26 +08:00
|
|
|
ts = x[i:i + 0xf]
|
2018-03-01 09:03:04 +08:00
|
|
|
if DEBUG:
|
2020-06-19 02:17:00 +08:00
|
|
|
print(f"kline send: 0x{ts.hex()}")
|
2019-10-16 03:05:33 +08:00
|
|
|
self._handle.bulkWrite(2, bytes([bus]) + ts)
|
2017-04-07 09:11:36 +08:00
|
|
|
echo = self.kline_ll_recv(len(ts), bus=bus)
|
|
|
|
if echo != ts:
|
2020-06-19 02:17:00 +08:00
|
|
|
print(f"**** ECHO ERROR {i} ****")
|
|
|
|
print(f"0x{echo.hex()}")
|
|
|
|
print(f"0x{ts.hex()}")
|
2017-04-07 09:11:36 +08:00
|
|
|
assert echo == ts
|
|
|
|
|
2020-06-19 02:17:00 +08:00
|
|
|
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)
|
2017-04-07 09:11:36 +08:00
|
|
|
return msg
|
2019-07-24 06:07:06 +08:00
|
|
|
|
2021-12-16 09:28:57 +08:00
|
|
|
def send_heartbeat(self, engaged=True):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf3, engaged, 0, b'')
|
2019-10-26 07:22:42 +08:00
|
|
|
|
2021-06-22 04:54:09 +08:00
|
|
|
# disable heartbeat checks for use outside of openpilot
|
|
|
|
# sending a heartbeat will reenable the checks
|
|
|
|
def set_heartbeat_disabled(self):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf8, 0, 0, b'')
|
|
|
|
|
2019-10-26 07:22:42 +08:00
|
|
|
# ******************* RTC *******************
|
|
|
|
def set_datetime(self, dt):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xa1, int(dt.year), 0, b'')
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xa2, int(dt.month), 0, b'')
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xa3, int(dt.day), 0, b'')
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xa4, int(dt.isoweekday()), 0, b'')
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xa5, int(dt.hour), 0, b'')
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xa6, int(dt.minute), 0, b'')
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xa7, int(dt.second), 0, b'')
|
|
|
|
|
|
|
|
def get_datetime(self):
|
|
|
|
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xa0, 0, 0, 8)
|
|
|
|
a = struct.unpack("HBBBBBB", dat)
|
|
|
|
return datetime.datetime(a[0], a[1], a[2], a[4], a[5], a[6])
|
|
|
|
|
|
|
|
# ******************* IR *******************
|
|
|
|
def set_ir_power(self, percentage):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xb0, int(percentage), 0, b'')
|
|
|
|
|
|
|
|
# ******************* Fan ******************
|
|
|
|
def set_fan_power(self, percentage):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xb1, int(percentage), 0, b'')
|
|
|
|
|
|
|
|
def get_fan_rpm(self):
|
|
|
|
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xb2, 0, 0, 2)
|
|
|
|
a = struct.unpack("H", dat)
|
2019-11-05 09:26:37 +08:00
|
|
|
return a[0]
|
|
|
|
|
2020-06-01 16:49:26 +08:00
|
|
|
# ****************** Phone *****************
|
2019-11-05 09:26:37 +08:00
|
|
|
def set_phone_power(self, enabled):
|
2019-11-21 03:56:26 +08:00
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xb3, int(enabled), 0, b'')
|
2020-08-17 19:02:41 +08:00
|
|
|
|
|
|
|
# ************** Clock Source **************
|
|
|
|
def set_clock_source_mode(self, mode):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf5, int(mode), 0, b'')
|
2020-08-17 21:04:01 +08:00
|
|
|
|
|
|
|
# ****************** Siren *****************
|
|
|
|
def set_siren(self, enabled):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf6, int(enabled), 0, b'')
|
2021-01-05 21:51:58 +08:00
|
|
|
|
|
|
|
# ****************** Debug *****************
|
|
|
|
def set_green_led(self, enabled):
|
|
|
|
self._handle.controlWrite(Panda.REQUEST_OUT, 0xf7, int(enabled), 0, b'')
|