'
' New VB Script File - Created 12/12/03
'////OVO JE SKRIPTA ZA UPIS I CITANJE S BOOTLOADEROM
'// ZA ATMEGA163  SC
'///////////////@BUCO //////////////////
'/ COM PARAMETRI DATA =38400 RESET=38400
'// PARITY=NONE   STOPBIT=1 MODE =DIRECT
'//REC TIME=150-250  BYTE DEL.=0 RESETDEL=400
'/////////////////////////////////////////////////
'// TK=FFFFFFFFFFFFFFFF
'// MOZE SE MIJENJATI PO ZELJI //////
'////////////////////////////////////////////////

Option Explicit
 'File Open constants
 Const ForReading = 1
Const Response=1
Const fsoError = -1
Const fsoOpenRead = 0
Const fsoOpenWrite = 1
Const fsoOpenReadWrite = 2
Const FileFilter = "All Eprom Files (*.bin, *.crd,*.hex,*.txt)|*.bin;*.crd;*.hex;*.txt|Bin Files (*.bin)|*.bin|Crd Files (*.crd)|*.crd| Hex Files(*.hex)|*.hex|Text Files(*.txt)|*.txt"
'
' File Seek Constants
'
Const fsoSEEK_SET = 0  '0
Const fsoSEEK_CUR = 1
Const fsoSEEK_END = 2
dim res,dev,mfd,cfg,mkl,curdiv,maxlen,mkf
dim comm,tma,tmea,FileSize,InFile,OutFile
dim w1,w2,wx1,wx2,n,FileName,ldfile,TmpStr
dim thisbyte,ReturnValue,fso,datw,tto,bf
dim del,wm,ds,rln,lat,atr,br1,br2,tk,bnq
dim klj,bnw,br0
Sub Main()
rln=1
atmbootl
End Sub
Sub READ
dim m,i,b
res=""
m=0
do while m < rln
m=sc.bytesinbuffer
loop
sc.read(m)
for i=0 to m-1
b=sc.getbyte(i)
res=res & right("0"& hex(b),2)
next
end sub
sub atmbootl
sc.Verbose=true
tk="FFFFFFFFFFFFFFFF"
'tk=sc.inputbox("UNESITE 8 BAYTNI TK ")
sc.write"02" &tk    ' TK  test i "02"// "5A"
READ
'sc.print("NNN "&res)&vbcr
if res<>"B0"  then
sc.write"02AA0000000000"
sc.msgbox("TK NIJE DOBAR -- PAZNJA ! U SLUCAJU DA VISEKRATNO UNESETE POGRESAN TK IZBRISAT CETE LOADER")
exit sub
end if
if  res="B0" then sc.msgbox("TK JE ISPRAVAN")
br0=buttonbox("ATM163BL",4,"IZBORNIK","CITANJE","UPISIVANJE","ATR","TK")
select case br0
case 1
br1=sc.buttonbox("ATM163BL",4,"CITAJ IZ -- ","FLASH","EXT EEP","INT EEP","ZAKLJUCAJ")
select case br1
case 1
comm="A2"
maxlen=15360
rfatm
case 2
comm="A5"
maxlen=8192
rfatm
case 3
comm="A3"
maxlen=512
rfatm
case 4
bnq=buttonbox("ATML163",3,"UPIS BLOK I KLJUC BITOVA U KARTICU","BL ZASTITA","ZAKLJUCATI SVE", "VAS MOD")
select case bnq
case 1
sc.write"02A700DF000000"  ' standardno zastita bl
case 2
bnw=sc.buttonbox("ATML163",2,"JESTE LI SIGURNI DA ZALITE POTPUNO ZAKLJUCAT KARTICU ?? ","NE","DA")
select case bnw
case 1
exit sub
case 2
sc.write"02A700DC000000"
sc.msgbox("KARTICA POTPUNO ZAKLJUCANA SVE OPERACIJE IDU PREKO BL")
end select
case 3
klj="FF"
klj=sc.inputbox("UPISITE KLJ I BLB KONFIGURACIJU PO ZELJI"-- XX)
sc.write("02A700"&klj&"000000")
 end select
  end select
case 2
br2=sc.buttonbox("ATML163BL",3,"UPISI U-- ","FLASH","EXT EEP","INT EEP")
select case br2
case 1
comm="A0"
maxlen=15360  '15872
filein
case 2
comm="A6"
maxlen=8192
filein
case 3
comm="A4"
maxlen=512
filein
end select
case 3
atr=sc.inputbox("UNESITE NOVI ATR ")
lat=len(atr)/2
lat=right("0" &lat,2)
sc.write("02A900000000"&lat&atr)
sc.delay(50)
READ
if res="02" then sc.msgbox(" ATR UPISAN")
case 4
sc.write("02A80000000008F1F1FFFFFFFFFFFF")
sc.delay(50)
READ
 end select
 end sub
sub rfatm
dim ffg,mxc
tto=0:mkf=0:infile="" :tma=time:rln=512
sc.verbose=true
select case  comm
case "A2"
 sc.write"02A20000000000" 'fl  16K
case "A3"
 sc.write"02A30000020000" 'int
case "A5"
 sc.write"02A50000200000" 'eep 32k==80    '32768
end select
do while  tto<1
call READ
infile=infile&res
sc.write"29"
mkf=mkf+ len(res)/2
if mkf=>maxlen then tto=2 : rln=1
curdiv=int(mkf/maxlen * 100)
 Call Sc.ProgressBox("PROCITAO -- "&curdiv& "  %",mkf,maxlen, "PROCITAO % ")
loop
Call Sc.ProgressBox("", 0, 100) 'ZATVORI prbox
sc.write"02AA0000000000"   ' postavlja sve u nulu
tmea=time:mkf=mkf
sc.msgbox("--FILE SIZE ==  "&mkf&"  BYTE ---"&"  START TIME "&tma&" END TIME "&tmea)&vbcR
call FileOut
end sub
sub Filein
dim gs,ls,m
FileName = Fs.FileOpenDialog(FileFilter, "ODABERITE DATOTEKU ", "Default.bin")
    If FileName <> "" Then
Set fso = CreateObject("Scripting.FileSystemObject")
   InFile = Fs.FileOpen(FileName, fsoOpenRead)
   end if
FileSize = Fs.FileSeek(InFile, 0, fsoSEEK_END)
Sc.msgbox( "VELICINA DATOTEKE JE" & FileSize )& vbCr & vbCr
 call Fs.FileSeek(InFile, 0 - (FileSize), fsoSEEK_END)
m=0
if FileSize<>0 then
wm=FileSize/512
else
exit sub
end if
 n=0:mkf=0:tma=time:tto=0:rln=1
 sc.verbose=false
select case comm
case  "A0"
sc.write("02A02000000000")'flash 40=16Kbyte
case  "A6"
sc.write("02A60000200000") 'ext eep 80 full/32kB
case  "A4"
sc.write("02A41000100000")' int eepr
end select
for m=0 to wm-1
ldfile=""
for n=0 to 511
ldfile=ldfile & HexString(Fs.FileGetc(InFile),2)
next
sc.write(ldfile)
sc.delay(50)
READ
if res="0102" then kraj
if res<>"01"  then
 Fs.FileClose(InFile)
 exit sub
end if
curdiv=int(m/wm * 100)
 Call Sc.ProgressBox("UPISAO -- "&curdiv& "  %",m,wm, "PROCITAO % ")
next
end sub
sub kraj
Call Sc.ProgressBox("", 0, 100) 'ZATVORI prbox
tmea=time
sc.msgbox("--FILE SIZE ==  "&filesize&"  BYTE ---"&"  START TIME "&tma&" END TIME "&tmea)&vbcR
sc.write"02AA0000000000"
Fs.FileClose(InFile)
end sub
sub fileout
dim bl
FileName = Fs.FileSaveDialog(FileFilter, "ODABERITE NAZIV NOVE DATOTEKE", "Default.hex")
    If FileName <> "" Then
Set fso = CreateObject("Scripting.FileSystemObject")
   Set bf= fso.CreateTextFile(FileName, True)
   bf.Write(""& InFile)
   bf.Close
 end if
End Sub
'///////
Function HexString(ThisNumber, Length)
    Dim RetVal
    Dim CurLen
    RetVal = Hex(ThisNumber)
    CurLen = Len(RetVal)
    If CurLen < Length Then
        RetVal = String(Length - CurLen, "0") & RetVal
    End If

    HexString = RetVal
End Function