cls
@echo off
	@ECHO *********************************************************
	@ECHO  Batch file for calibration of Atmel AVR TINY4/5/9/10/20/40
	@ECHO  oscillator through the TPI interface with AVR918 

	@ECHO  - The internal RC is calibrated to value and accuracy 
	@ECHO    given in AVR057.asm 
	@ECHO  - Programming calibration firmware is performed initially.
	@ECHO  
	@ECHO  $Name$
	@ECHO  $Revision: 3900 $                            
	@ECHO  $RCSfile$
	@ECHO  $Date: 2008-04-30 14:28:26 +0200 (on, 30 apr 2008) $
	@ECHO *********************************************************
	
	@ECHO ---------------------------------------------------------
	@ECHO.
	@ECHO   ** S T A R T   P R O G R A M M I N G **
	@ECHO.		
@ECHO ---------------------------------------------------------


@REM Fill in your cpu type and full path to the programming tool
@SET CPU=attiny4
@SET TOOL=avrosp.exe
@SET CUSTOMERCODE="user.hex"

mode com1 baud=115200 parity=n data=8

%TOOL% -d%CPU% -s -e -pf -vf -ifavr057.hex

@IF ERRORLEVEL ==1 GOTO prog_Calib_code_error

@ECHO ==========================================================
@ECHO Calibration Firmware successfully programmed to the target
@ECHO ==========================================================
@ECHO. 
@ECHO POWER CYCLE THE TARGET, RESET TPI PROGRAMMER AND PRESS ENTER!!!
@ECHO.
@pause
		
@ECHO.
@ECHO  ** Start Calibration followed by programming customer code
@ECHO  ** Specify flash byte address in -Y command to store OSCCAL value

@ECHO ---------------------------------------------------------
%TOOL% -d%CPU% -Y1FF -e -pf -if%CUSTOMERCODE% 

@IF ERRORLEVEL ==1 GOTO Calibration_error

	@ECHO.
	@ECHO *********************************************************
	@ECHO 		P R O G R A M M I N G   O K
	@ECHO *********************************************************
	@PAUSE

@GOTO END

:prog_Calib_code_error
	@ECHO.
@ECHO ---------------------------------------------------------
	@ECHO.
	@ECHO 		E R R O R		
	@ECHO Programming calibration program to AVR failed.
	@ECHO Programming aborted.
	@PAUSE
	@GOTO END
@ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


:Calibration_error
	@ECHO.
@ECHO ---------------------------------------------------------
	@ECHO.
	@ECHO 		E R R O R
	@ECHO Calibration / Programming failed.
	@ECHO Programming aborted.
	@PAUSE
	@GOTO END
@ECHO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

:END				
