@echo off
setlocal

set NRFTOOLS="C:\Program Files\Nordic Semiconductor\nrf-command-line-tools\bin"
set SNR=1050240254
set PYTHON=C:\ncs\toolchains\66cdf9b75e\opt\bin\python.exe
set APP_HEX=build\blinky_pwm\zephyr\zephyr.hex
set UICR_HEX=uicr_regout0_3v3.hex
set MERGED_HEX=build\blinky_pwm\zephyr\zephyr_with_uicr.hex

echo [1/3] Merging app hex with UICR hex...
%NRFTOOLS%\mergehex.exe --merge %APP_HEX% %UICR_HEX% --output %MERGED_HEX%
if %ERRORLEVEL% neq 0 (
    echo ERROR: mergehex failed. Make sure the project is built first.
    exit /b 1
)

echo [2/3] Flashing with nrfjprog...
%NRFTOOLS%\nrfjprog.exe --program %MERGED_HEX% --sectorerase --verify --family NRF52 -s %SNR% --reset
if %ERRORLEVEL% neq 0 (
    echo ERROR: Flash failed.
    exit /b 1
)

echo [3/3] Done!
echo Power-cycle the board for REGOUT0 (3.3V) to take effect.
endlocal
