@echo off setlocal :: ST-LINK CLI location: env var DTU_STLINK_CLI = full path of ST-LINK_CLI.exe. :: Explicit value is used as-is (invalid value = hard error, no silent fallback); :: when unset, fall back to PATH (where ST-LINK_CLI.exe). set "STLINK=" if defined DTU_STLINK_CLI goto stlink_check where ST-LINK_CLI.exe >nul 2>&1 if errorlevel 1 goto stlink_missing for /f "delims=" %%i in ('where ST-LINK_CLI.exe 2^>nul') do ( if not defined STLINK set "STLINK=%%i" ) if not defined STLINK goto stlink_missing goto stlink_done :stlink_check if exist "%DTU_STLINK_CLI%" goto stlink_done echo ENV_ERROR: DTU_STLINK_CLI is set but does not exist: "%DTU_STLINK_CLI%" echo Fix the variable value. No fallback to PATH or other installs is attempted. exit /b 2 :stlink_missing echo ENV_ERROR: ST-LINK_CLI.exe not found (DTU_STLINK_CLI unset and not on PATH) echo Set it to the full path of ST-LINK_CLI.exe, e.g.: echo [Environment]::SetEnvironmentVariable('DTU_STLINK_CLI', 'C:\Tools\STLink\ST-LINK_CLI.exe', 'User') exit /b 2 :stlink_done if not defined STLINK set "STLINK=%DTU_STLINK_CLI%" if exist "%STLINK%" goto stlink_ready echo RESET_FAIL: ST-LINK_CLI not found at "%STLINK%" exit /b 2 :stlink_ready if "%~1"=="" goto :reset_run if /i "%~1"=="hard" goto :hard if /i "%~1"=="halt" goto :halt if /i "%~1"=="run" goto :run if /i "%~1"=="list" goto :list if /i "%~1"=="flash" goto :flash if /i "%~1"=="cmp" goto :cmp goto :help :reset_run echo RESET: system reset and run (SWD) "%STLINK%" -c SWD -Rst -Run -Q exit /b %ERRORLEVEL% :hard echo RESET: nRST pulse 100ms (falls back to system reset if CLI lacks RM=) "%STLINK%" -c SWD RM=Hrst -HardRst PULSE=100 -Q if not errorlevel 1 exit /b 0 echo RESET_NOTE: this ST-LINK_CLI rejects RM= (v3.5.0 known gap), falling back to system reset "%STLINK%" -c SWD -Rst -Run -Q exit /b %ERRORLEVEL% :halt echo RESET: system reset and halt core (debug; resume with 'run') "%STLINK%" -c SWD -Rst -Halt exit /b %ERRORLEVEL% :run echo RUN: resume halted core "%STLINK%" -c SWD -Run exit /b %ERRORLEVEL% :list "%STLINK%" -List exit /b %ERRORLEVEL% :flash if "%~2"=="" set "FW=%~dp0STM32F407-Demo\STM32F407-Demo.hex" if not "%~2"=="" set "FW=%~2" if not exist "%FW%" ( echo FLASH_FAIL: firmware not found "%FW%" exit /b 2 ) echo FLASH: program "%FW%" then reset and run "%STLINK%" -c SWD -P "%FW%" -Rst -Run -Q exit /b %ERRORLEVEL% :cmp if "%~2"=="" set "FW=%~dp0STM32F407-Demo\STM32F407-Demo.hex" if not "%~2"=="" set "FW=%~2" if not exist "%FW%" ( echo CMP_FAIL: firmware not found "%FW%" exit /b 2 ) echo CMP: readback compare "%FW%" vs device flash echo CMP_NOTE: connecting resets the target - run this AFTER log capture, not before "%STLINK%" -c SWD -CmpFile "%FW%" 0x08000000 exit /b %ERRORLEVEL% :help echo Usage: reset.bat [command] echo. echo (none) System reset and run (restart board via SWD) echo hard nRST pulse 100ms; auto-falls back to system reset if CLI lacks RM= echo halt System reset and halt core (in-session read; auto-resumes after disconnect) echo run Resume a halted core echo list List connected ST-LINK probes echo flash [F] Program F (default: latest build hex) then reset and run echo cmp [F] Readback compare F vs device flash; "No difference found" = write landed echo (connecting resets the target: run it after log capture, not before) echo. echo Exit codes: 0=OK 1=probe or target error 2=usage or tool missing endlocal