STM32批处理下载程序(hex)

STM32批处理下载程序

工作之余,写了个STM32批处理下载程序,方便批量烧录程序。
脚本的工作原理是:首先判断C盘根目录下是否有JFlash,如果没有,那么在C盘搜索JFlash安装位置,并在C盘根目录下创建快捷方式,方便后续调用。
因此第一次使用的该脚本的时候,会有一个查找的过程;
你也可以修改脚本,删除查找的过程,手动创建快捷方式。
脚本中增加了清空flash和读保护的设置,大家可以自行设定。

rem 1.判断是否存在JFlashARM的快捷方式
if exist C:\JFlashARM* (
    if exist C:\STM32F103RB.jflash* (
        goto download
    )
)

echo Searching JFlashARM.exe,pls wait a minute...
rem 2.系统中查找JFlashARM并获取路径
set targetPath=empty
set "targefile=JFlashARM.exe"
for %%a in (C) do (
    if exist %%a:\ (
        pushd %%a:\
        for /r %%b in (*%targefile%) do (
            if "%%~nxb" equ "%targefile%" (
                set targetPath=%%~dpb
                goto next
            )
        )
        popd
    )
)

if "%targetPath%" equ "empty" (
    echo can not find "JFlashARM.exe",pls make sure that you have installed JLINK driver!
    goto end
)

:next
set jflashPath=%targetPath%Samples\JFlash\ProjectFiles\
rem echo %targetPath%
rem echo %jflashPath%

rem 3. 创建快捷方式
set srcPath=%targetPath%JFlashARM.exe
set destPath=C:\
set lnkName=JFlashARM.exe
rem 获取桌面路径 WshShell.SpecialFolders(""DeskTop"")
mshta VBScript:Execute("Set WshShell=CreateObject(""WScript.Shell""):Set oShellLink=WshShell.CreateShortcut( ""%destPath%"" & ""\%lnkName%.lnk""):oShellLink.TargetPath=""%srcPath%"":oShellLink.Save:close")

rem copy jflash
copy "%jflashPath%STM32F103RB.jflash" C:\STM32F103RB.jflash


:download

rem 判断是否需要擦除整颗芯片
set neederase=0
choice /c:YN /m "Y:擦除整颗芯片 N:不擦除"
if %errorlevel% == 1 (
    set neederase=1
)
rem 判断是否需要锁定芯片
set needlock=0
choice /c:YN /m "Y:设置芯片读保护 N:不保护"
if %errorlevel% == 1 (
    set needlock=1
    echo 注意:设置了读保护,则下次下载程序时flash会被清空!!!
)

rem 判断是否有输入文件
set dhexfile=%1
if "%~x1" equ ".hex" (
    goto downloaddirect
)

echo 请将待下载的hex程序拖入该窗口,并按回车开始下载!!!
set /p dhexfile=

:downloaddirect
if %neederase% equ 1 (
    if %needlock% equ 1 (
        echo 开始擦除并下载程序,设置读保护...
        C:\JFlashARM.exe.lnk -openprjC:\STM32F103RB.jflash -open"%dhexfile%"  -erasechip -auto -securechip -exit
    ) else (
        echo 开始擦除并下载程序...
        C:\JFlashARM.exe.lnk -openprjC:\STM32F103RB.jflash -open"%dhexfile%"  -erasechip -auto -exit
    )
) else (
    if %needlock% equ 1 (
        echo 开始直接下载程序,设置读保护...
        C:\JFlashARM.exe.lnk -openprjC:\STM32F103RB.jflash -open"%dhexfile%"  -auto -securechip -exit
    ) else (
        echo 开始直接下载程序...
        C:\JFlashARM.exe.lnk -openprjC:\STM32F103RB.jflash -open"%dhexfile%"  -auto  -exit
    )
    
)


if %errorlevel% equ 0 (
    echo Download success!
)
if %errorlevel% neq 0 (
    echo Download failed!
)

choice /c:YN /m "Y:继续下载 N:退出"
if %errorlevel% == 1 (
    goto downloaddirect
)

:end
rem pause


猜你喜欢

转载自blog.csdn.net/llb19900510/article/details/103854593
今日推荐