1md build\keil\Execute 2 3@echo off 4:: enter .bat folder 5cd %~dp0 6:: 批处理所在路径 7set bat_path=%0 8:: MDK $J 这里传入的是KEIL 编译器头文件路径,利用这个路径找到编译器相关工具链地址 9set tool_chain_inc=%1 10:: MDK #L 这里传入的是KEIL生成的axf文件的完整路径 11set axf_full_path=%2 12:: 获取axf文件的名字,不含后缀 13set axf_name=%~n2 14 15if %tool_chain_inc:~-1,1% == \ ( 16 :: 删除路径最后的\ 17 set tool_chain_inc=%tool_chain_inc:~,-1% 18) 19 20:: call .bin generate function 21call :binGenerate %tool_chain_inc% %axf_full_path% 22if %errorlevel% == 1 ( 23 echo Failed 1: fromelf generate .bin failed! 24 goto :EOF 25) 26 27:: call 文件复制 28call :doFileCopy %axf_full_path% 29 30:: 对hex文件进行Patch并生成patch后的.bin和.hex 31call :doFilePatch %axf_name% 32if %errorlevel% == 1 ( 33 echo Failed 2: Patch failed! 34 goto :EOF 35) 36 37exit /b %errorlevel% 38 39:: Function Definiations ------------------------------------------------ 40 41:: .bin generate function 42:binGenerate 43:: 通过头文件路径,获取工具链的根目录 44set tool_chain_root=%~dp1 45:: 获取axf的路径 46set axf_path=%~dp2 47:: 获取axf的名字 48set axf_name=%~n2 49:: echo %axf_path% 50:: echo %axf_name% 51:: echo %tool_chain_root% 52:: 执行fromelf 生成bin文件 53%tool_chain_root%bin\fromelf --bin %2 --output %axf_path%\%axf_name%.bin 54exit /b %errorlevel% 55 56:: 将axf/hex/bin文件复制到Execute文件夹下 57:doFileCopy 58:: 获取axf的名字 59set axf_name=%~n1 60copy /Y .\build\keil\Obj\%axf_name%.axf .\build\keil\Execute\%axf_name%.axf 61copy /Y .\build\keil\Obj\%axf_name%.hex .\build\keil\Execute\%axf_name%.hex 62copy /Y .\build\keil\Obj\%axf_name%.bin .\build\keil\Execute\%axf_name%.bin 63:: 根据用户的配置,可能hex不生成,不管这个,直接返回成功 64exit /b 0 65 66:: 对文件进行Patch 67:doFilePatch 68set target_name=%1 69Patcher.exe .\build\keil\Execute\%target_name%.hex 70exit /b %errorlevel% 71