'
' New VB Script File - Created 1/28/04
'
'//////////////////////
'////OVO JE SKRIPTA ZA UPIS I CITANJE S BOOTLOADEROM
'// ZA ATMEGA128  SC
'///////////////@BUCO //////////////////
'/ COM PARAMETRI DATA =115200 RESET=115200
'// PARITY=NONE   STOPBIT=1 MODE =DIRECT
'//REC TIME=150-250  BYTE DEL.=0 RESETDEL=400
'/////////////////////////////////////////////////
'// ZA KARTICE SA COMPILEROM ZA IZRADU ALGOA
'// ZA PROGRAMATOR
'////////////////////////////////////////////////

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,inw,k
Sub Main()
rln=1:inw=""
'SC.DELAY(500)
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
res=res & Right("0" & hex(sc.getbyte(i)),2)
next
end sub
sub atmbootl
dim br1,br2,tk,bnq,klj,bnw,br0
sc.Verbose=true
sc.write("02")
call READ
if res="0B" then sc.print"PROSAO"
sc.write"02AA00000000"
'sc.delay(4)
call READ
if res<>"FCAA" then
sc.write"02AA00000000"
sc.msgbox("NISTE U BOOTLOADER MODU -- PONOVITE")
exit sub
end if
br0=buttonbox("ATM128BL",3,"IZBORNIK","CITANJE","UPISIVANJE","COMPILER")
select case br0
case 1
br1=sc.buttonbox("ATM128BL",3,"CITAJ IZ -- ","FLASH","INT EEP","ZAKLJUCAJ")
select case br1
case 1
comm="A2"
maxlen=122880
rfatm
case 2
comm="A3"
maxlen=4096
rfatm
case 3
bnq=buttonbox("ATML128",3,"UPIS BLOK I KLJUC BITOVA U KARTICU","BL ZASTITA","ZAKLJUCATI SVE", "VAS MOD")
select case bnq
case 1
sc.write"02A700DF0000"  ' standardno zastita bl
case 2
bnw=sc.buttonbox("ATML128",2,"JESTE LI SIGURNI DA ZALITE POTPUNO ZAKLJUCAT KARTICU ?? ","NE","DA")
select case bnw
case 1
exit sub
case 2
sc.write"02A700DC0000"
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&"0000")
 end select
  end select
case 2
br2=sc.buttonbox("ATML128BL",2,"UPISI U-- ","FLASH","INT EEP")
select case br2
case 1
comm="A0"
maxlen=122880
'flashwr
filein
case 2
comm="A4"
maxlen=4096
'flashwr
filein
end select
case 3
Compiler
 end select
 end sub
sub  flashwr
call FileIn
if FileSize=<0 then exit sub
if Filesize>=2048 then wm=FileSize/2048
 n=0:mkf=0:tma=time:tto=0
select case comm
case  "A0"
sc.write("02A000040000")'flash 04=8Kbyte
case  "A4"
sc.write("02A400020000")' int eepr
end select
sc.verbose=true : rln=1
do while tto<1
datw=mid(ldfile,(n*2)+1,4096)
sc.write(datw)
call READ
if res="0102" then tto=1
 n=n+2048
curdiv=int(n/filesize * 100)
 Call Sc.ProgressBox("UPISAO -- "&curdiv& "  %",n,filesize, "PROCITAO % ")
loop
Call Sc.ProgressBox("", 0, 100) 'ZATVORI prbox
tmea=time
sc.msgbox("--FILE SIZE ==  "&filesize&"  BYTE ---"&"  START TIME "&tma&" END TIME "&tmea)&vbcR
sc.write"02AA00000000"
end sub
sub rfatm
tto=0:mkf=0:infile="" :tma=time:rln=2048
sc.verbose=FALSE
select case  comm
case "A2"
 sc.write"02A200400000" 'fl  8K
 maxlen=122880     '8192
case "A3"
 sc.write"02A300020000" 'int   4k A3
 maxlen=4096
end select
do while  tto<1
call READ
'sc.print("RES "&res)&vbcr
infile=infile&res
sc.write"29"
mkf=mkf+ len(res)/2
if mkf=>maxlen then rln=1 : tto=1
curdiv=int(mkf/maxlen * 100)
 Call Sc.ProgressBox("PROCITAO -- "&curdiv& "  %",mkf,maxlen, "PROCITAO % ")
loop
Call Sc.ProgressBox("", 0, 100) 'ZATVORI prbox
sc.write"02AA00000000"   ' postavlja sve u nulu
tmea=time : rln=1
sc.msgbox("--FILE SIZE ==  "&mkf&"  BYTE ---"&"  START TIME "&tma&" END TIME "&tmea)&vbcR
call FileOut
end sub
sub Filein
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)
'filesize=16368
Sc.msgbox( "VELICINA DATOTEKE JE" & FileSize )& vbCr & vbCr
 call Fs.FileSeek(InFile, 0 - (FileSize), fsoSEEK_END)
ldfile = "" : filesize=16368
 n=0:mkf=0:tma=time:tto=0
select case comm
case  "A0"
sc.write("02A000080000")'flash 08=16Kbyte
case  "A4"
sc.write("02A400020000")' int eepr
end select
sc.verbose=false : rln=1
do while tto<1
datw=""
for k=0 to 2047
datw=datw & right("0" & hex(Fs.FileGetc(InFile)),2)
next
sc.write(datw)
sc.delay(150)
 READ
if res="0102" then tto=1
 n=n+2048
curdiv=int(n/filesize * 100)
 Call Sc.ProgressBox("UPISAO -- "&curdiv& "  %",n,filesize, "PROCITAO % ")
loop
Call Sc.ProgressBox("", 0, 100) 'ZATVORI prbox
tmea=time
sc.msgbox("--FILE SIZE ==  "&filesize&"  BYTE ---"&"  START TIME "&tma&" END TIME "&tmea)&vbcR
sc.write"02AA00000000"
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 =right("0" & Hex(ThisNumber),2)
    'CurLen = Len(RetVal)
    'If CurLen < Length Then
       ' RetVal = String(Length - CurLen, "0") & RetVal
   ' End If

    HexString = RetVal
End Function
'Sub Main()
'a=1
'End Sub
sub CP1
CPX
end sub
sub Compiler
sc.write"02AC00000000"
CPX
end sub
sub CPX
dim  rt,inb,cr,ing,inj,ind,inw,inc,ct
sc.verbose=FALSE
inb=0:ing="":inj=0:ind="":inw="":inc="":ct=""
inw=sc.inputbox("unesi")
inj=len(inw)
for n=1 to inj
inb=Cstr(mid(inw,n,1))
if ing="24" then
ind=(inb)
else
ind=hex(asc(inb))
end if
if ind="24" then
 ing=ind
 else
inc=inc & ind
end if
next
sc.verbose=true
sc.print(inw)&vbcr
'sc.verbose=false
'sc.verbose=false
'ind=Chr(41)
'inj=cint("&h"&(inw))   ' hex chr =abs dec
 'inb= chr(inj) ' abs dec value= chr
 'ing=Cint("&h" &"inj")
Sc.write(inc&"20B3")
sc.delay(5)
call READ
cr=mid(res,1,2)
if len(res)/2=16 then ct=mid(res,3,2)
if cr="BF" then
 sc.msgbox("POGRESNA NAREDBA")
exit sub
end if
if ct="1B" then exit sub
CP1
end sub