PyLink: Python J-Link Interface

2.0.1 · active · verified Sat Apr 11

PyLink is a Python package that provides an interface to SEGGER J-Link debug probes, allowing control and interaction with embedded targets from Python. It was developed by Square to integrate J-Link functionality into Python-based test infrastructure. The library is actively maintained, with the current version being 2.0.1, and typically sees updates for new features and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a SEGGER J-Link debug probe and then connect to a target device. It then performs a basic operation, such as reading the target's IDCODE. Ensure `JLINK_SERIAL_NO` and `JLINK_TARGET_DEVICE` environment variables are set with your specific J-Link serial number and target CPU name, or replace the placeholder values. The SEGGER J-Link Software and Development Pack must be installed on your system.

import pylink
import os

# Replace with your J-Link's actual serial number (as a string).
# Consider setting JLINK_SERIAL_NO and JLINK_TARGET_DEVICE as environment variables.
jlink_serial_no = os.environ.get('JLINK_SERIAL_NO', 'YOUR_JLINK_SERIAL_HERE')
target_device = os.environ.get('JLINK_TARGET_DEVICE', 'CORTEX-M4') # Example: 'ATSAM4S2B' or 'STM32F407VG'

try:
    # Initialize JLink object
    jlink = pylink.JLink()

    # Open a connection to your J-Link probe
    jlink.open(serial_no=int(jlink_serial_no))
    print(f"Connected to J-Link: {jlink.product_name} (Serial: {jlink_serial_no})")

    # Connect to the target device
    jlink.connect(target_device, verbose=True)
    print(f"Connected to target device: {target_device}")

    # Perform a simple operation, e.g., read target IDCODE
    if jlink.connected() and jlink.target_connected():
        print(f"Target IDCODE: {hex(jlink.idcode())}")
        # Example: Reset the target
        # jlink.reset()
    else:
        print("Failed to establish full connection to target device.")

except pylink.JLinkException as e:
    print(f"J-Link operation failed: {e}")
except ValueError:
    print("Invalid J-Link serial number provided. Must be an integer.")
finally:
    # Always close the J-Link connection
    if 'jlink' in locals() and jlink.opened():
        jlink.close()
        print("J-Link connection closed.")

view raw JSON →