利用pylink下载hex/bin文件

该代码段展示了一个用Python库pylink实现SerialWireViewer(SWV)的例子,通过J-Link连接到目标CPU,使用SWD接口进行通信,配置并启动SWO实时日志记录。程序包括设备连接、重置策略设置、速度计算、内存操作以及文件烧录,并捕获串行线输出数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pylink官方文档如下:

官方文档-pylink

其中示例代码为


import pylink

try:
    import StringIO
except ImportError:
    import io as StringIO
import string
import sys
import time


def serial_wire_viewer(jlink_serial, device):
    """Implements a Serial Wire Viewer (SWV).

    A Serial Wire Viewer (SWV) allows us implement real-time logging of output
    from a connected device over Serial Wire Output (SWO).

    Args:
      jlink_serial (str): the J-Link serial number
      device (str): the target CPU

    Returns:
      Always returns ``0``.

    Raises:
      JLinkException: on error
    """
    buf = StringIO.StringIO()
    jlink = pylink.JLink(log=buf.write, detailed_log=buf.write)
    jlink.open(serial_no=jlink_serial)

    # Use Serial Wire Debug as the target interface.  Need this in order to use
    # Serial Wire Output.
    jlink.set_tif(pylink.enums.JLinkInterfaces.SWD)#设置连接方式是SWD
    jlink.connect('STAR', verbose=True)#指定芯片型号
    jlink.coresight_configure() #Prepares target and J-Link for CoreSight function usage.
    jlink.set_reset_strategy(pylink.enums.JLinkResetStrategyCortexM3.RESETPIN)
    #Sets the reset strategy for the target.
    #The reset strategy defines what happens when the target is reset.
    # Have to halt the CPU before getitng its speed.
    jlink.reset()
    jlink.halt()

    cpu_speed = jlink.cpu_speed()
    swo_speed = jlink.swo_supported_speeds(cpu_speed, 10)[0]

    # Start logging serial wire output.
    jlink.swo_start(swo_speed)
    jlink.swo_flush()

    print(jlink.core_id())
    print(jlink.device_family())
    print(jlink.target_connected())
    print(jlink.erase(start_addr,end_addr)) #成功返回0
    jlink.flash_file(file_path,addr) #刷成功返回0,失败有多种原因




    # Output the information about the program.
    sys.stdout.write('Serial Wire Viewer\n')
    sys.stdout.write('Press Ctrl-C to Exit\n')
    sys.stdout.write('Reading data from port 0:\n\n')

    # Reset the core without halting so that it runs.
    jlink.halt()
    jlink.reset(ms=10, halt=False)
    jlink.memory_write(0x0000ffff,[1])
    jlink.flash_file('./local.hex', 0x28000000)
    #jlink.flash_file('./local.bin', 0x28000000)   

    # Use the `try` loop to catch a keyboard interrupt in order to stop logging
    # serial wire output.
    try:
        while True:
            # Check for any bytes in the stream.
            num_bytes = jlink.swo_num_bytes()

            if num_bytes == 0:
                # If no bytes exist, sleep for a bit before trying again.
                time.sleep(1)
                continue

            data = jlink.swo_read_stimulus(0, num_bytes)
            sys.stdout.write(''.join(map(chr, data)))
            sys.stdout.flush()
    except KeyboardInterrupt:
        pass

    sys.stdout.write('\n')

    # Stop logging serial wire output.
    jlink.swo_stop()

    return 0


if __name__ == '__main__':
    exit(serial_wire_viewer(sys.argv[1], sys.argv[2]))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值