Bootstrap

Python脚本实现通过JLink烧录Hex文件

1 安装JLink驱动程序

驱动安装包下载路径:https://www.segger.com/downloads/jlink/
在这里插入图片描述


选择对应的版本下载:
在这里插入图片描述


将下载的安装文件双击进行安装。

2 安装 pylink 包

 pip install pylink

3 查询 JLink 设备的 serial number

将JLink通过USB线插入电脑。
windows开始菜单 - J-Link Commander,打开 J-Link Commander 界面
在这里插入图片描述


使用 showemulist 命令查看 serial number.

J-Link>showemulist
J-Link[0]: Connection: USB, Serial number: 601012352, ProductName: J-Link PLUS

4 python 编程

import pylink
import sys

jlink = None
flash_progress = 0

# 初始化 J-Link 仿真器
def jlink_init():
    global jlink
    try:
        jlink = pylink.JLink()  # 创建 J-Link 对象
        if jlink is None:
            print("Failed to create J-Link object")
            return None

        # 打开 J-Link 仿真器
        jlink.open(serial_no=601012352)  # 通过命令 "showEmuList" 在 "J-Link Commander" 中获取 serial_no

        # 设置连接接口为 SWD
        jlink.set_tif(pylink.enums.JLinkInterfaces.SWD)

        # 连接目标设备
        jlink.connect('S32K314')

    except Exception as e:
        print("Failed to initialize J-Link:", e)
        jlink = None
        return None

    return jlink


# 检查连接状态
def jlink_check_status():
    if jlink is None:
        print("J-Link is not initialized")
        return
    
    open_status = jlink.opened()  # 检查 J-Link 仿真器是否打开
    if not open_status:
        print("J-Link is not opened")
    else:
        print("J-Link is opened")

    connected_status = jlink.connected()  # 检查 J-Link 仿真器是否连接
    if not connected_status:
        print("J-Link is not connected")
    else:
        print("J-Link is connected")

    target_connected_status = jlink.target_connected()  # 检查仿真器是否和目标设备连接
    if not target_connected_status:
        print("Target is not connected")
    else:
        print("Target is connected")

# 获取设备信息
def jlink_get_info():
    if jlink is None:
        print("J-Link is not initialized")
        return
    
    print("firmware_version:", jlink.firmware_version)  # 获取 J-Link 仿真器的固件版本
    print("product_name:", jlink.product_name)  # 获取 J-Link 仿真器的产品名称
    print("OEM:", jlink.oem)  # 获取 OEM 信息
    print("core_id:", jlink.core_id())  # 获取目标设备的核心 ID
    print("device_family:", jlink.device_family())  # 获取目标设备的家族信息

# 烧写文件到目标设备
def jlink_flash_file(file_path, address):
    if jlink is None:
        print("J-Link is not initialized")
        return
    
    if file_path is None or address is None:
        print("File path or address is None")
        return
    
    if address < 0x00400000 or address > 0x00500000:
        print("The flash start address is invalid.")
        return

    # 烧写文件到目标设备
    try:
        jlink.flash_file(file_path, address, on_progress)
        print("Flashing done!")
    except Exception as e:
        print("Flashing failed:", e)

def on_progress(action, progress_string, percentage):
    global flash_progress
    if action == b'Program':
        if (percentage - flash_progress) > 5:
            # print("Progress:",  progress_string)
            print("Progress:", percentage)
            flash_progress = percentage

def jlink_reset():
    if jlink is None:
        print("J-Link is not initialized")
        return
    
    jlink.reset()  # 重置目标设备
    jlink.restart()
    print("Reset done!")

# 下载文件到目标设备
def jlink_deinit(jlink):
    # 断开连接
    jlink.close()

if __name__ == '__main__':
    jlink_init()
    if jlink is None:
        print("J-Link is not initialized")
        sys.exit(1)
    
    jlink_check_status()
    jlink_get_info()

    # 烧写文件到目标设备
    file_path = "e:/01_code/test.hex"  # 注意路径只能用 "/" 而不能用 "\",否则会报错
    address = 0x00200000
    jlink_flash_file(file_path, address)

    # 重置目标设备
    jlink_reset()

    # 断开连接
    jlink_deinit(jlink)

运行效果如下:

J-Link is opened
J-Link is connected
Target is connected
firmware_version: J-Link V11 compiled Mar 14 2024 13:16:08
product_name: SEGGER J-Link
OEM: None
core_id: 1805657207
device_family: 14
The flash start address is invalid.
Reset done!
;