'
'  VB Script File - Created 12/13/03
'/// ****** !! //////
' ///
''//autor : S.D//////////@buco//////////////
'''///////MINI PROGRAMMER///////////
' //BRZINA 38400 kbps, stop=1, parity=none///////////
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,li,lk
dim del,wm,ds,mxl,kk,mxt,ls,gs,bbc,intcmd,yz,mxr
dim xz,zc,lo,xr,xm
Sub Main()
del=0
 ident
'Mcu
End Sub
sub Ident
SC.VERBOSE=true
sc.write("01DA000006000000")  ' 06 ID LOWADR PIC
sc.delay(40)
READ
cfg=mid(res,5,2):kk=0
mfd=mid(res,1,2) : dev=mid(res,3,2):mkl=mid(res,5,4)
sc.msgbox("PROIZVODJAC - [ "&mfd&" ]   KODNI BROJ--[ "&dev&" ]")&vbcr
if mfd="1E" and mkl="0102" then
sc.msgbox("KARTICA JE ZAKLJUCANA")&VBCR
end if
if mfd="FF" or mfd="3F" or dev="FF" then
sc.msgbox("NEMOGUCE IDENTIFICIRATI")
kk=sc.BUTTONbox("TIP KARTICE ",2," RUCNO ","GW"," NEMA KARTICE")
end if
select case kk
case 1
mfd="05" :dev="60"
sc.write("01DA000106056000")'
case 2
sc.write("01D0000000000000")
exit sub
 end select
if mfd<>"1E" then sc.write("01D0000000000000")
Mcu
end sub
Sub READ
dim m,i,b
res=""
m=0
do while m < 1
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 Mcu
dim bf2,bf3,bf4,bf5,bf6,bf7,mdi,bc1,bc2,bc3,bc4
mdi=""
if mfd<>"1E" then
PICMODE
 end if
if mfd="1E"  then
bf2=sc.buttonbox("ATMEL",5," ATMEL MCU  OPCIJE ","PROCITAJ","UPISI","UPISI OSIG. I ZAKLJ","BRISI SVE","CITAJ OSIG. I ZAKLJ")
select case  bf2
case 1
bf3=sc.buttonbox("ATMEL",2," CITANJE FLASHA I INT EEPROMA ","FLASH","INT EEPROM")
select case bf3
case 1
call DSL
comm="02"
Mcurd
'end select
case 2
comm="03" ' eprom
call EDSL  ' eprom select routine
Mcurd
exit sub
end select
case 2
bf4=sc.buttonbox("ATMEL",2,"UPIS U FLASH I INT EEPROM","FLASH","INT EEPROM")
select case bf4
case 1
comm="10"         ' fl atm
call DSL
Filein
case 2
comm="11"        ' eep atm upis
call EDSL
Filein
end select
case 3
bf6=sc.buttonbox("ATMEL",5,"UPISI OSIGURACE I ZAKLJ"," NIZI BYTE OSIG.", "VISI BYTE OSIG","ZAKLJ BYTE","RES BL128","SET BL128")
select case bf6
case 1
xm=sc.buttonbox("ATMEL LOW FUSE ",3," UPISI "," M163-EXTO "," M128-INTO "," M128-EXTO")
select case xm
case 1
xr="01"
case 2
xr="E4"
case 3
xr="E0"
end select
sc.write"01D740ACA000"&xr&"00" 'E0=extck E4=intck at128
call  READ       'm163 bl//01 brounout and xtal
case 2
zc=sc.buttonbox("UPISI ",4,"ATMSERIE","BL163","AP163","EXO128","INTO128")
select case zc
case 1
xz="FA"
case 2
xz="FB"
case 3
xz="C0"  ' c8==dis Jtag 88 =en jtag intosc
case 4
xz="98"
end select
sc.write"01D740ACA800"&xz&"00" ' 88exto 98into  ATM128
'sc.write"01D740AC3800FF00" ' nista !!
call READ            'FA/FB 163 512b// ZA 8kBY BOOT MODE
case 3
sc.write"01D740ACE000CF00"  'e9(e0) hi atM shi
call READ               ' CF// n163 lock bit
case 4
sc.write"01D740ACA000FF00" 'rst FBL
sc.delay(600)
sc.write"01D740ACA800DF00" ' rst FBH
call READ
case 5
sc.write"01D740ACA000E000" 'set bl128  FBL
sc.delay(600)
sc.write"01D740ACA8008800" ' set bl128 FBH
call READ
end select
case 4
sc.write"01DF800000000000"  ' brisi sve
 READ
sc.msgbox("IZBRISANO")
case 5
bf7=sc.buttonbox("ATMEL",5,"CITANJE OSIGURACA I ZAKLJ BITOVA","OSIG. NIZI BYTE","OSIG. VISI BYTE","KLJUC","KALIBRACIONI BYTE","EXTFUSEBIT")
select case bf7
case 1
sc.write"01D5045000000000"  'frl
call READ
call Ispis
case 2
sc.write"01D5045808000000"   'frh
call READ
call Ispis
case 3
sc.write"01D5045800000000"   ' lr
call READ
call Ispis
case 4
sc.write"01D5043800000000"   ' rc
call  READ
call Ispis
case 5
sc.write"01D5045008000000"   ' extended fuse bits
' sc.write"01D50430000x0000"  ' sign bytes x=0,1,2
call  READ
call Ispis
end select
 end select
  end if
sc.write("01D0000000000000")
end sub
sub PICMODE
dim yu,yt,yi,yo,ye
comm="D2":del=80
yu="MICROCHIP"
if mfd="05" then sc.msgbox(" "& yu &" IC==PIC 16F84 ")
if mfd="09" then sc.msgbox(" "& yu &" IC==PIC 16F876/877")
if mfd="07" then sc.msgbox(" "& yu &" IC==PIC 16F628")
yt=sc.buttonbox("PIC ",4,"OPCIJE","CITAJ", "UPISI","BRISI","OSIGURACI")
select case yt
case 1
yi=sc.buttonbox("CITANJE",2,"OPCIJE","FLASH","INTEEPROM")
select case yi
case 1
intcmd="04"
if mfd="09" then maxlen=16384:mxl="40":mxt="08"
if mfd="05" then maxlen=2048:mxl="08": mxt="08"
if mfd="07" then maxlen=2048 :mxl="08": mxt="08"
Mcurd
case 2
intcmd="05"
if mfd="09" then maxlen=256:mxl="01":mxt="08"
if mfd="05" then maxlen=64:mxl="01": mxt="02"
if mfd="07" then maxlen=64 :mxl="01": mxt="02"
Mcurd
end select
case 2
yo=sc.buttonbox("UPIS",2,"OPCIJE","FLASH","INTEEP")
select case yo
case 1
intcmd="02"
if mfd="09" then maxlen=16384:mxl="40":mxt="08"
if mfd="05" then maxlen=2048:mxl="08":mxt="08"
Filein
case 2
intcmd="03"
if mfd="09" then maxlen=256:mxl="01":mxt="08"
if mfd="05" then maxlen=64:mxl="01":mxt="02"
Filein
end select
case 3
ye=sc.buttonbox("BRISANJE",2,"OPCIJE","BRISI IC","MAKNI CP")
select case ye
case 1
sc.write("01D2080000000000")' PRG
case 2
sc.write("01D21000003FFF00") ' cp
end select
case 4
yz=sc.buttonbox("CP--IC",4,"CFG RIJEC","PROCITAJ","UNESI CFG","CP=ON","OTKLJUCAJ")
select case yz
case 1
sc.write("01DA000007000000")
READ
li=mid(res,1,4)
lk=" "
if li="0009" or li="0310" then lk="ZAKLJUCAN"
sc.msgbox("CFG JE -- "&li&" "&lk)
case 2
li="3FF9"
li=sc.inputbox("UNESI CFG RIJEC")
sc.write("01DA000107"&li&"00") ' xtal=on ,cp=off
case 3
sc.write("01DA000107000900")
sc.msgbox("MEMORIJA ZAKLJUCANA")
case 4
sc.write("01DA0C01073FF900") ' xtal=on ,cp=off int cmd 0c == otkljucaj karticu
end select
 end select
sc.write"01D0000000000000"
end sub
sub Mcurd
sc.verbose=true
tma=time:mkf=0:InFile="":curdiv=1
if comm="03" then sc.write("01DF032000"&mxl&mxt&"00")'a
if comm="02" then sc.write("01DF021000"&mxl&mxt&"00")'at
if comm="D2" then sc.write("01D2"&intcmd&"0000"&mxt&mxl&"00")
do while  tto<1
sc.delay(55)
call READ
mkf=mkf + len(res)/2
curdiv=int(mkf/maxlen *100)
 Call Sc.ProgressBox("PROCITAO -- "&curdiv& "  %",mkf,maxlen, "PROCITAO % ")
Infile=Infile&res
sc.write"05"
if res="01" then tto=2
loop
Call Sc.ProgressBox("", 0, 100) 'ZATVORI prbox
tmea=time: mkf=mkf-1
'if comm="02" or intcmd="05" or intcmd="04" then mkf=( mkf*2)+1
sc.msgbox("--FILE SIZE ==  "&mkf&"  BYTE ---"&"  START TIME "&tma&" END TIME "&tmea)&vbcR
call FileOut
del=1
sc.write"01D0000000000000"
end sub
'//////////
sub Ispis
dim b1
b1=mid(res,1,2)
sc.msgbox("VRIJEDNOST -- ["&res&" ]")
end sub
'/////////
sub Filein
dim tst,pck,dnm,lck,ppc
dnm="default.bin":pck=0: bbc=0:ppc=0:wx2="00"
bbc=sc.buttonbox("ATMEL/PIC",3,"MOD","APLIKACIJA","BL128","BL163")
select case bbc
case 1
pck=0
case 2
pck=122880:mxl="20":wx2="F0"
case 3
pck=15360:mxl="04":wx2="1E"
end select
FileName = Fs.FileOpenDialog(FileFilter, "ODABERITE DATOTEKU ", dnm)
    If FileName <> "" Then
Set fso = CreateObject("Scripting.FileSystemObject")
   InFile = Fs.FileOpen(FileName, fsoOpenRead)
   end if
FileSize = Fs.FileSeek(InFile, -(pck), fsoSEEK_END)
Sc.msgbox( "VELICINA DATOTEKE JE" & FileSize )& vbCr & vbCr
 call Fs.FileSeek(InFile,0-(FileSize), fsoSEEK_END)
wm=Filesize/32
gs=0:curdiv=1:tma=time:tto=0'a128w2=240
if comm="D2" then sc.write("01D2"&intcmd&"0000"&mxt&mxl&"00")
if comm="11" then sc.write("01DF112000"&mxl&mxt&"00")
if comm="10" then sc.write("01DF1010"&wx2&mxl&mxt&"00")
sc.verbose=false             ' w2=30(3C) m163 bl
DO WHILE tto<1
datw=""
for  ls=0 to 31
datw=datw & right("0" & hex(Fs.FileGetc(InFile)),2)
next
SC.WRITE(datw)
sc.delay(90)
 READ
 if res="0001" then tto=2
 gs=gs+32
 curdiv=int(gs/filesize *100)
 Call Sc.ProgressBox("UPISAO  "&curdiv& "  %",gs,filesize, "UPISUJE I TESTIRA  ")
loop
Fs.FileClose(InFile)
Call Sc.ProgressBox("", 0, 100) 'ZATVORI prbox
tmea=time
if res="0001" then sc.msgbox ("PROGRAMIRANJE  "&FileName&"  USPJESNO OBAVLJENO :)"&" START TIME "&tma&" KRAJ "&tmea)
sc.write"01D0000000000000" ' iskopcava Vpp // BL
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
'///////
sub DSL      ' device select
mxt="00":del=50
select case dev
case "93"
maxlen=8192 'word
mxl="20"
case "94"
maxlen=8192*2 ' word
mxl="40"
case "97"     ' atm128
maxlen=65536 *2 ' word
mxl="00" ' mxl=200 x 256==131072 byta
mxt="01"
end select
end sub
sub EDSL
mxt="00":del=50
if dev="97" then
maxlen=4096
 mxl="10"
else
maxlen=512 ' 93,94 90s8515,atm163
mxl="02"
end if
end sub
'''