Zephyr应用笔记:Python实现JLink下载程序脚本

python脚本内容:

#!/usr/bin/env python3
#
# Copyright (c) 2018 Noitom
#
# SPDX-License-Identifier: Apache-2.0

import argparse
import os
import tempfile
import subprocess

def do_jlink( args):
        lines = [ 'r'] # Reset and halt the target
        lines.append( 'loadfile {} {} '.format(args.load_path, args.load_offset))
        lines.append( 'g') # Start the CPU
        lines.append( 'q') # Close the connection and quit
         with tempfile.NamedTemporaryFile( suffix= '.jlink') as f:
                f.writelines( bytes(line + ' \n ', 'utf-8') for line in lines)
                f.flush()

                cmd = ([ 'JLinkExe'] + [ '-if', 'swd', '-speed', '4000', '-device', args.device, '-CommanderScript', f.name])
                subprocess.check_call(cmd)  
        
def args():
        parser = argparse.ArgumentParser()
        parser.add_argument( "-d", "--device", metavar= 'filename', required= True)
        parser.add_argument( "-load", "--load_path", metavar= 'filename', required= True)
        parser.add_argument( "-offset", "--load_offset", metavar= 'filename', required= True)
        args = parser.parse_args()
        do_jlink(args)

if __name__ == '__main__':
args()


Makefile内容如下:

BOARD = mimxrt1050_evk

KERNEL_BIN_NAME = image.bin

JLINK_FLASH_LOAD_ADDRESS = 0x60000000

SOURCE_DIRECTORY := $( CURDIR )
BUILD_DIRECTORY := $( CURDIR )/ $( BOARD )

IMGTOOL = $( CURDIR )/merge.py
IMGJLINK = $( CURDIR )/jlink.py
IMG_HEADER_LEN = 8192

image: check
    (mkdir -p $( BOARD ) && \
        cd $( BOARD ) && \
        cmake -DBOARD= $( BOARD ) $( SOURCE_DIRECTORY ) && \
        make -j $( nproc ))
     $( IMGTOOL ) -k $( BUILD_DIRECTORY )/zephyr/zephyr.bin \
        -i $( KERNEL_BIN_NAME ) \
        -head head.bin \
        -H $( IMG_HEADER_LEN )

flash: check
     $( IMGJLINK ) -d MIMXRT1052xxxxA \
    -load $( KERNEL_BIN_NAME ) \
    -offset $( JLINK_FLASH_LOAD_ADDRESS )
    
menuconfig: check
    cd $( BOARD ) && make menuconfig
    
clean: check
    @rm -f ${MCUBOOT_KERNEL_BIN_NAME}
    cd $( BOARD ) && make clean
    
check:
    @if [ -z " $$ZEPHYR_BASE" ]; then echo "Zephyr environment not set up"; false; fi
    @if [ -z " $( BOARD )" ]; then echo "You must specify BOARD="; false; fi

你可能感兴趣的:(zephyr,python)