(****************************************************************************) (* Das ISA-AT89Cx051-Programmiergerät-Programm, Version 1.0 *) (* ---------------------------------------------------------------------- *) (* copyright (c) 2000 by Hubersoft Corp. All rights reserved. *) (****************************************************************************) uses crt; type Tjob=record writechip: boolean; readchip: boolean; protectchip: boolean; filename: string; end; Tflashbuffer=array[0..4999] of byte; var flash,verify:Tflashbuffer; pa, maxsize: word; flashsize: word; counter, result: word; l,lt: longint; b: boolean; job: Tjob; f: file; dir: string; (****************************************************************************) procedure writechip(bytes: word); var count: word; begin write('Programming flash ROM...'); port[pa+3]:=128; {IO Out} port[pa+2]:=1; {LED on} port[pa+1]:=0; delay(1); {alles low} port[pa+1]:=12; delay(1); {P3 erase config} port[pa+1]:=14; delay(1); {reset high} port[pa+1]:=78; delay(2); {12V on} port[pa+1]:=74; delay(20); {P3.2 low} port[pa+1]:=78; delay(1); {P3.2 high} port[pa+1]:=246; delay(1); {write config} for count:=0 to bytes-1 do begin gotoxy(26, wherey); write('(',round((count/(bytes-1))*100),'%)'); port[pa+0]:=flash[count]; {new data} port[pa+1]:=242; delay(1); {P3.2 low} port[pa+1]:=246; delay(2); {P3.2 high} port[pa+1]:=247; {XTAL high} port[pa+1]:=246; {XTAL low} end; port[pa+1]:=0; {12V off, reset low} port[pa+2]:=0; {LED off} writeln; end; (****************************************************************************) procedure readchip(bytes: word); var count: word; begin port[pa+3]:=144; {IO in} port[pa+2]:=1; {LED on} port[pa+1]:=0; delay(1); {alles low} port[pa+1]:=2; delay(1); {reset high} port[pa+1]:=166; delay(1); {read config} for count:=0 to bytes-1 do begin verify[count]:=port[pa+0]; {read data} port[pa+1]:=167; {XTAL high} port[pa+1]:=166; {XTAL low} end; port[pa+1]:=0; {reset low} port[pa+2]:=0; {LED off} end; (****************************************************************************) function adressdetect:word; var pc,w: word; b: boolean; const ad:array[0..7] of word=($200,$220,$240,$260,$280,$2A0,$2C0,$2E0); begin for pc:=0 to 7 do begin port[ad[pc]+3]:=136; b:=true; for w:=0 to 7 do begin port[ad[pc]+2]:=w*2; if port[ad[pc]+2] shr 5<>w then b:=false; end; if b then break; end; if (pc=7) and not b then adressdetect:=0 else adressdetect:=ad[pc]; end; (****************************************************************************) procedure protectchip; begin writeln('Protecting flash ROM...'); port[pa+3]:=128; {IO Out} port[pa+2]:=1; {LED on} port[pa+1]:=0; delay(1); {alles low} port[pa+1]:=188; delay(1); {P3 lock1 config} port[pa+1]:=190; delay(1); {reset high} port[pa+1]:=254; delay(2); {12V on} port[pa+1]:=250; delay(4); {P3.2 low} port[pa+1]:=254; delay(1); {P3.2 high} port[pa+1]:=94; delay(1); {P3 lock2 config} port[pa+1]:=90; delay(4); {P3.2 low} port[pa+1]:=94; delay(1); {P3.2 high} port[pa+1]:=0; delay(1); {12V off, reset low} writeln('Done.') end; (****************************************************************************) function getsignature:longint; var w: word; m,l:longint; begin port[pa+3]:=144; {IO in} port[pa+2]:=1; {LED on} port[pa+1]:=0; delay(1); {alles low} port[pa+1]:=2; delay(1); {reset high} port[pa+1]:=6; delay(1); {signature config} m:=1; l:=0; for w:=0 to 2 do begin l:=l+m*port[pa+0]; m:=m*256; port[pa+1]:=7; {XTAL high} port[pa+1]:=6; {XTAL low} end; port[pa+1]:=0; {reset low} port[pa+2]:=0; {LED off} getsignature:=l; end; (****************************************************************************) function dtohex(dezahl: word): string; var temp: string[30]; zahl, rest: word; ch: string[1]; begin zahl:=dezahl; temp:=''; repeat rest:= zahl mod 16; if rest < 10 then str(rest, ch) else ch:=chr(55+rest); temp:=ch+temp; zahl:=zahl div 16; until zahl=0; dtohex:=temp; end; (****************************************************************************) procedure readcmdline; var s: string; w,z: word; begin with job do begin filename:=''; writechip:=false; readchip:=false; protectchip:=false; end; for w:=1 to paramcount do begin s:=paramstr(w); if s[1]='-' then for z:=2 to length(s) do case upcase(s[z]) of 'W':job.writechip:=true; 'R':job.readchip:=true; 'P':job.protectchip:=true; else writeln('Error: Invalid command: ',s); halt; end else job.filename:=s; end; end; (****************************************************************************) begin writeln; writeln('ISA Flash Programmer (v1.0) - copyright (c) 2000 by Hubersoft Corp.'); if paramcount=0 then begin writeln('Usage: flash [commands] [filename]'); writeln('Commands: -w write chip'); writeln(' -r read chip'); writeln(' -p protect chip'); halt; end; (**************************************************************) pa:=adressdetect; if pa=0 then begin writeln('Fatal Error: ISA programmer card not detected!'); writeln; halt; end else writeln('Programmer card detected at ',dtohex(pa),'h.'); (**************************************************************) lt:=getsignature; if ((lt mod 65536 mod 256)<>30) or not ((lt div 256 mod 256) in [$11,$21,$41]) then begin writeln('Fatal Error: No microcontroller detected!'); halt; end else begin write('Atmel '); case (lt div 256 mod 256) of $11:begin write('AT89C1051'); maxsize:=1024; end; $21:begin write('AT89C2051'); maxsize:=2048; end; $41:begin write('AT89C4051'); maxsize:=4096; end; end; writeln(' microcontroller detected.'); end; (**************************************************************) readcmdline; if job.writechip and job.readchip then begin writeln('Error: Can''t read and write at the same time!'); halt; end; if (job.writechip or job.readchip) and (job.filename='') then begin writeln('Error: Filename is missing!'); halt; end; if not job.writechip and not job.readchip and (job.filename<>'') then begin {writeln('Error: Command is missing!');} {halt;} job.writechip:=true; {Der Jan meint, das sei besser so.} end; {Ich zwar nicht, aber bitte...} (**************************************************************) if job.writechip then begin getdir(0, dir); if dir[0]>#3 then dir:=dir+'\'; assign(f,dir+job.filename); {$I-}reset(f, 1);{$I+} if ioresult<>0 then begin writeln('Error: File not found!'); halt; end; if filesize(f)>maxsize then begin writeln('WARNING: BIN data shortened to ',maxsize, ' bytes!'); counter:=maxsize; end else counter:=filesize(f); blockread(f,flash,counter,result); flashsize:=result; close(f); writeln('Got ', result, ' bytes of data from file.'); writechip(flashsize); writeln('Verifying flash ROM...'); readchip(flashsize); b:=true; for l:=0 to flashsize-1 do if flash[l]<>verify[l] then b:=false; if b then writeln('Programming successful!') else writeln('WARNING: Programming failed!'); writeln('Done.'); end; (**************************************************************) if job.readchip then begin getdir(0, dir); if dir[0]>#3 then dir:=dir+'\'; assign(f,dir+job.filename); {$I-}rewrite(f,1);{$I+} if ioresult<>0 then begin writeln('Error: Can''t create file!'); halt; end; writeln('Reading flash ROM...'); readchip(maxsize); counter:=maxsize; blockwrite(f, verify, counter, result); close(f); writeln('Done.') end; (**************************************************************) if job.protectchip then protectchip; (**************************************************************) writeln; end.