pylink

repository·master·Indexed 19 days ago

https://github.com/square/pylink

A Python interface for the SEGGER J-Link debugger that allows developers to programmatically control J-Link hardware and target devices. It provides a Pythonic wrapper around the J-Link C SDK, supporting operations such as flashing firmware, resetting devices, and accessing hardware registers. The library includes support for protocols like Serial Wire Debug (SWD), Serial Wire Output (SWO), and Real Time Transfer (RTT), and requires SEGGER J-Link Tools >= 6.0b.

Tokens
5.1K
Snippets
23
Records
50
Agent score
62%

What's inside pylink

  1. Use the JLink class to interact with the J-Link C SDK

    master

    The JLink class is the primary interface for interacting with the J-Link C SDK. It provides a Pythonic wrapper around native SDK functions.

    Note that certain methods require specific preconditions, such as having a target device connected or an emulator connected. If these conditions are not met, the library will raise an exception rather than returning error codes.

  2. Understand PyLink's C SDK bindings

    master
    PyLink interfaces with the native J-Link SDK (a C library) using Python's ctypes module. To interact with the underlying C SDK, PyLink provides native Python structure bindings and constant enumerations that map to the values returned by the C SDK. This allows you to use Pythonic objects and constants instead of raw C pointers and integers.
  3. Handle JLink exceptions

    master
    PyLink uses an object-oriented exception model instead of C-style return codes. All errors raised by the library inherit from the JLinkException base class. You should catch JLinkException (or its specific subclasses found in pylink.errors) to handle errors such as missing connections or invalid states.
  4. Understand the protocol communication model in pylink

    master

    The J-Link supports multiple communication methods to interact with a target, including Serial Wire Debug (SWD), Serial Wire Output (SWO), Memory, Coresight, and Registers.

    To facilitate communication, pylink provides protocol-specific modules. While all protocol methods require an existing JLink instance to function, the protocol modules handle the specific housekeeping and low-level communication logic required for each method automatically.

  5. Prevent concurrent access with JLock

    master
    The JLock class provides a lockfile-like interface for interacting with a specific emulator. Use JLock to prevent multiple threads or processes from creating JLink instances that attempt to interact with the same emulator simultaneously, which avoids resource contention and undefined behavior.
  6. Quickstart with PyLink

    master

    To control a J-Link using Python, import the pylink module, instantiate a JLink object, and use the open method with the device's serial number. You can then access properties like product_name to verify the connection.

    import pylink
    
    jlink = pylink.JLink()
    jlink.open(serial_no=123456789)
    print(jlink.product_name)
  7. Connect to a target CPU

    master

    After opening the emulator, use .connect(device_name) to connect to a specific target device (e.g., 'MKxxxxxxxxxx7'). Once connected, you can query device information such as .core_id() and .device_family(). Use .target_connected() to confirm the target is successfully attached.

    jlink.connect('MKxxxxxxxxxx7')
    print(jlink.core_id())
    print(jlink.device_family())
    print(jlink.target_connected()) # Returns True if target is connected
  8. Configure J-Link library path on Windows

    master

    On Windows, you can make JLinkARM.dll available to PyLink using one of these methods:

    1. Copy the DLL: Place JLinkARM.dll in the current execution directory, the Windows system directory, or the Windows directory.
    2. Update PATH: Add the SEGGER J-Link installation directory to your %PATH% environment variable.
  9. Build PyLink from source

    master

    To install the development version (master branch), clone the repository and run setup.py. For a more stable installation, it is recommended to check out a specific release branch before installing.

    To install the development branch:

    $ python setup.py install

    To install from a specific release branch:

    $ git checkout release-major.minor
    $ python setup.py install
  10. Configure J-Link library path on Linux

    master

    On Linux, you can ensure PyLink finds the J-Link library by using one of these two methods:

    Option A: Copy the library file to your libraries directory

    cp libjlinkarm.so /usr/local/lib/

    Option B: Add SEGGER's J-Link library path to your libraries path

    export LD_LIBRARY_PATH=/path/to/SEGGER/JLink:$LD_LIBRARY_PATH
    # Option A
    cp libjlinkarm.so /usr/local/lib/
    
    # Option B
    export LD_LIBRARY_PATH=/path/to/SEGGER/JLink:$LD_LIBRARY_PATH
  11. Configure J-Link library path on macOS

    master

    On macOS, you can ensure PyLink finds the J-Link library by using one of these two methods:

    Option A: Copy the library file to your libraries directory

    cp libjlinkarm.dylib /usr/local/lib/

    Option B: Add SEGGER's J-Link directory to your dynamic libraries path

    export DYLD_LIBRARY_PATH=/Applications/SEGGER/JLink:$DYLD_LIBRARY_PATH
    # Option A
    cp libjlinkarm.dylib /usr/local/lib/
    
    # Option B
    export DYLD_LIBRARY_PATH=/Applications/SEGGER/JLink:$DYLD_LIBRARY_PATH
  12. Configure SEGGER J-Link external dependencies

    master

    The library requires SEGGER J-Link tools (version >= 6.0b) to be installed on your system. While pylink attempts to find the library automatically, you may need to manually ensure the dynamic library is discoverable by your OS using one of the following methods:

    macOS

    • Option A: Copy libjlinkarm.dylib to /usr/local/lib/.
    • Option B: Add the SEGGER J-Link directory to your DYLD_LIBRARY_PATH.

    Windows

    Windows searches for DLLs in the current directory, the Windows system directory, and the Windows directory. You can:

    • Copy JLinkARM.dll to one of those directories.
    • Add the SEGGER J-Link directory to your %PATH%.

    Linux

    • Option A: Copy libjlinkarm.so to /usr/local/lib/.
    • Option B: Add the SEGGER J-Link library path to your LD_LIBRARY_PATH.
    # macOS Option A
    $ cp libjlinkarm.dylib /usr/local/lib/
    
    # macOS Option B
    $ export DYLD_LIBRARY_PATH=/Applications/SEGGER/JLink:$DYLD_LIBRARY_PATH
    
    # Linux Option A
    $ cp libjlinkarm.so /usr/local/lib/
    
    # Linux Option B
    $ export LD_LIBRARY_PATH=/path/to/SEGGER/JLink:$LD_LIBRARY_PATH