Automating J-Link with Python Scripts

J-Link is a multifunctional debugger and emulator launched by SEGGER, widely used in the field of embedded system development. Although J-Link supports scripts (.jlinkscript) for automating firmware programming, batch testing, and hardware status verification, it cannot process some test data.Automating J-Link with Python ScriptsDue to Python’s extensive data processing libraries, combining J-Link’s automation scripts with Python is very helpful for automated testing. Python has a dedicated library to control J-Link, namely pylink-square, which supports most J-Link commands. Let’s explore the usage of pylink.

  1. Installation
    pip install pylink-square
  • Connecting to J-Link
    jlink = pylink.JLink()# Get the serial number of J-Linkpjlist = jlink.connected_emulators()for item in pjlist:    SN = item.SerialNumberjlink.open(str(SN))jlink.set_tif(pylink.enums.JLinkInterfaces.SWD)   # Set communication interface to SWD or JTAGjlink.connect('M4')    # Set the connected chip, for some less common chips, use the core model
  • Common J-Link Operations
    jlink.set_speed(2000)    # Set communication speed to 2Mjlink.memory_read32(0x0, 1)    # Read memory, note that the returned data type is listjlink.memory_write32(reg, write_data)    # Configure register, note that write_data needs to be of list typejlink.flash_file('bin_file.bin', addr)    # Program bin file to SRAM
  • Common Errors:When using pylink, the following error often occurs:

    # Error as follows:# line 295, in init raise TypeError('Expected to be given a valid DLL.')

    Solution: Add the installation path of J-Link to the environment variables.

    Leave a Comment