@SET ComPort=%3
@SET BaudRate=%4

@REM -------------- AVRDUDE programmer --------------

@SET Fuse1=
@SET Fuse2=
@SET Fuse3=


@REM %6 is the flag to send fuse data instead of the program
@IF "%6"=="1" GOTO Config


:ProgFlash
@SET AVRDUDEcmd="%~dp0..\bin\avrdude.exe" -p %1 -P %ComPort% -b %BaudRate% -c %2 -U flash:w:%5
@SHIFT
@GOTO Send


:Config
@SET AVRDUDEcmd="%~dp0..\bin\avrdude.exe" -p %1 -P %ComPort% -b %BaudRate% -c %2 -u

@REM %7 is the number of fuses present
@IF "%7"=="1" GOTO ConfigOneFuse


:ConfigThreeFuses
@IF NOT "%8"=="-1" SET Fuse1=-U hfuse:w:%8:m
@IF NOT "%9"=="-1" SET Fuse2=-U lfuse:w:%9:m
@SHIFT
@IF NOT "%9"=="-1" SET Fuse3=-U efuse:w:%9:m
@GOTO Send


:ConfigOneFuse
@IF NOT "%8"=="-1" SET Fuse1=-U fuse:w:%8:m
@SHIFT
@GOTO Send


:Send
@REM Now add any additional command line parameters here
@SHIFT
@SHIFT
@SHIFT
@SHIFT
@SHIFT
@SHIFT
@SHIFT
@SHIFT
@SHIFT
@SHIFT

@REM mode %ComPort%: baud=%BaudRate% parity=n data=8 stop=1 to=on xon=off odsr=off octs=off dtr=hs rts=off idsr=off

@REM >The boards use a pulse on the RS232-DTR line to RESET the target device temporarily into programming mode. If programming begins within a short time period after the RESET, the chip is held in programming mode and the program is downloaded. If no program information is received within the time period, the device begins to run any program that is already in memory.

mode %ComPort%: baud=%BaudRate% parity=n data=8 stop=1 to=on xon=off odsr=off octs=off dtr=on rts=off idsr=off

%AVRDUDEcmd% %Fuse1% %Fuse2% %Fuse3% %0 %1  %2  %3  %4  %5  %6  %7  %8  %9
@IF %ERRORLEVEL% NEQ 0 GOTO DUDEError
@GOTO End


:DUDEError
@ECHO.
@ECHO Error returned from [avrdude]
@EXIT 1



:End
@ECHO.
@ECHO Programming successful!
@EXIT 0
