Skip to main content

Parse ASAP2 A2L file

Project description

XCP CAN Master

A Python implementation of an XCP (Universal Measurement and Calibration Protocol) master over CAN/CAN FD using the python-can library.

Features

• Request/Response Engine: Handles CRO (Command Receive Object) to DTO (Data Transmit Object) RES/ERR communication

• Connection Management: CONNECT and GET_COMM_MODE_INFO commands to learn MAX_CTO/MAX_DTO parameters

• Calibration Operations:

• Write operations using SHORT_DOWNLOAD/DOWNLOAD + SET_MTA

• Read operations using SHORT_UPLOAD/UPLOAD

• DAQ (Data Acquisition) Support:

• DAQ list allocation and configuration

• ODT (Object Descriptor Table) entry setup

• DAQ list start/stop control

• Flexible Data Parsing: User-configurable DAQ registry for byte-aligned entry parsing

• Thread-Safe: Built-in locking for concurrent operations

• CAN FD Support: Automatic detection and switching to CAN FD when needed

Installation

  1. Install required dependencies: pip install python-can loguru

Basic Usage

import can from xcp_can_master import XcpCanMaster

Configure CAN bus

timing_fd = can.BitTimingFd( f_clock=80000000, nom_brp=1, nom_tseg1=127, nom_tseg2=32, nom_sjw=32, data_brp=1, data_tseg1=27, data_tseg2=12, data_sjw=12 )

bus = can.interface.Bus(interface='vector', channel='0', fd=True, timing=timing_fd)

Initialize XCP master

CRO_ID = 0x1BFFE600 DTO_ID = 0x1BFFE601

xcp = XcpCanMaster( bus, cro_id=CRO_ID, dto_id=DTO_ID, extended=True, is_fd=True, default_timeout=1.0 )

try: # Connect to ECU xcp.connect(mode=0x00) xcp.get_comm_mode_info()

# Read/write operations
data = (123456).to_bytes(4, "little")
xcp.write(0x00, 0x7001c000, data)

rd = xcp.read(0x00, 0x7001c000, 4)
print(f"Readback: {int.from_bytes(rd, 'little')}")

finally: xcp.stop()

API Reference

Core Methods

• connect(mode: int = 0x00, timeout: Optional[float] = None): Establish XCP connection

• disconnect(timeout: Optional[float] = None): Disconnect from ECU

• get_comm_mode_info(timeout: Optional[float] = None): Get communication parameters

• build_checksum(size: int, timeout: Optional[float] = None): Calculate checksum

Memory Access

• read(addr_ext: int, address: int, size: int, timeout: Optional[float] = None): Read memory

• write(addr_ext: int, address: int, data: bytes, timeout: Optional[float] = None): Write memory

• short_upload(), upload(), short_download(), download(): Low-level memory operations

DAQ Configuration

• free_daq(), alloc_daq(), alloc_odt(), alloc_odt_entry(): DAQ resource management

• set_daq_ptr(), write_daq(): Configure DAQ entries

• set_daq_list_mode(): Configure DAQ list behavior

• start_stop_daq_list(): Start/stop DAQ acquisition

• register_daq_entry(): Register DAQ entries for parsing

• on_daq(): Set callback for DAQ data

DAQ Example

Setup DAQ measurement

xcp.free_daq() xcp.alloc_daq(1) xcp.alloc_odt(daq_list=0, odt_num=1) xcp.alloc_odt_entry(daq_list=0, odt=0, entries=2)

Configure measurement points

xcp.set_daq_ptr(daq_list=0, odt=0, entry=0) xcp.write_daq(bit_offset=0, size=4, addr_ext=0x00, address=0x7001c000)

xcp.set_daq_ptr(daq_list=0, odt=0, entry=1) xcp.write_daq(bit_offset=0, size=4, addr_ext=0x00, address=0x20000014)

Register for parsing

xcp.register_daq_entry(odt_pid=0, entry_name="sigA", size=4, addr_ext=0x00, address=0x20000010) xcp.register_daq_entry(odt_pid=0, entry_name="sigB", size=4, addr_ext=0x00, address=0x20000014)

Set DAQ callback

def on_daq(odt_pid: int, values: Dict[str, int], raw: bytes): print(f"DAQ odt={odt_pid} values={values}")

xcp.on_daq(on_daq)

Start acquisition

xcp.set_daq_list_mode(daq_list=0, event=0, prescaler=1, priority=0, mode=0x01) xcp.start_stop_daq_list(daq_list=0, start=True)

Error Handling

The library raises RuntimeError with detailed XCP error information when commands fail. Error codes are mapped according to the XCP standard: XCP_ERROR_INFO = { 0x00: {"name": "ERR_CMD_SYNCH", "desc": "Command processor synchronization."}, 0x10: {"name": "ERR_CMD_BUSY", "desc": "Command was not executed."}, # ... more error codes }

Configuration

Constructor Parameters

• bus: Configured python-can Bus instance

• cro_id: Arbitration ID for sending CRO frames to ECU

• dto_id: Arbitration ID for receiving DTO frames from ECU

• extended: True for 29-bit CAN IDs (default: True)

• is_fd: True to use CAN FD (default: False)

• default_timeout: Default timeout for commands in seconds (default: 0.5)

• byteorder: Byte order for multi-byte values - "little" or "big" (default: "little")

Supported XCP Commands

The library implements a comprehensive subset of XCP commands including:

• Standard commands (CONNECT, DISCONNECT, GET_STATUS, etc.)

• Calibration commands (DOWNLOAD, UPLOAD, MODIFY_BITS, etc.)

• DAQ/STIM commands (SET_DAQ_PTR, WRITE_DAQ, START_STOP_DAQ_LIST, etc.)

• Page switching commands (SET_CAL_PAGE, GET_CAL_PAGE, etc.)

Requirements

• Python 3.7+

• python-can 4.0+

• loguru (for logging)

• Appropriate CAN interface drivers

License

This code is provided as-is for educational and development purposes. Adapt to your specific hardware and protocol requirements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyxcpcanmaster-0.0.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyxcpcanmaster-0.0.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file pyxcpcanmaster-0.0.1.tar.gz.

File metadata

  • Download URL: pyxcpcanmaster-0.0.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for pyxcpcanmaster-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ad2b680aa9b00478b89fa35c1c79658ba9a8b08b4a4e012886da60d8a83a4c2e
MD5 6e2d07598164a7bc46c6129e867bd0d8
BLAKE2b-256 ca9d058fb5774e3f76e69b46300bab3e9fe3a2cf1068692c8175d28d1d98c7d9

See more details on using hashes here.

File details

Details for the file pyxcpcanmaster-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyxcpcanmaster-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for pyxcpcanmaster-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93ac83c9a254c8d594b30e0b3952441f681da8410aaf797b72aa7917a8cbf587
MD5 028754ff2a18660f9827aa3a75501578
BLAKE2b-256 277fc3f62ed54d34449fc13025987b821eac04d2af60bfa1402404adf47187b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page