Rev Author Line No. Line
204 miho 1 {$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,R+,S+,V+,X-}
2 {$M 16384,0,655360}
3  
4 program PicPgr(Input,Output);
5  
6 {=============================================================}
7 {== Program pro programovani ruznych PICu ( FLASH i EPROM ) ==}
8 {=============================================================}
9  
10 uses DELAY, { casova spozdeni }
11 PP_PGMHW, { ovladani hardwaru programatoru }
12 PP_COMON, { pomocne funkce }
13 PP_DEFS, { defince procesorove zavislych parametru }
14 PP_DATA, { ulozeni dat v pameti }
15 PP_IO; { import a export dat z/do souboru }
16  
17 const ver : string[4] = '4.02'; { vzdy 4 znaky }
18 date : string[4] = '2001'; { vzdy 4 znaky }
19  
20 {=============================================================}
21 { Zmeny: }
22 { 2.01 - vychozi verze pro tuto verzi }
23 { 3.00 - predelana kompletne vrstva pro ovladani HW ( moznost }
24 { popdory vice programatoru ) }
25 { 3.01 - finalni verze s podporou programovani pres LPT port }
26 { a na programatoru ALL03 }
27 { 3.10 - pridana informace o zdoji dat pri vystupu v TXT }
28 { formatu }
29 { - cislovani radek pri vypisu chyby ve vstupnich datech }
30 { - odstranena chyba exportu dat v HEX formatu }
31 { - dodelana kontrola kontrolniho souctu HEX formatu }
32 { - znovu zprovozneny prikazy RUN, STOP, RESET }
33 { - zmena default obsahu pri konverzi z 0 na 3FFF }
34 { 3.11 - cisla ve zdrojaku nahrazeny konstantami }
35 { 4.00 - velmi rozsahle zmeny ve strukture }
36 { - prepinani parametru podle typu soucastky }
37 { - zmena Vpp z 12.0V na 13.0V }
38 { - doplneno zpozdeni po zapnuti napajeni }
39 { - zmena programovacich casu u C/F84 (20ms na 10ms) }
40 { - podpora ErrorLevel pri programovani a verifikaci }
41 { 4.01 - prvni realese verze z rady 4.x }
42 { 4.02 -doplnena podpora algoritmu EEPROM1 PEFI }
43 { Chybi: }
44 { - podpora algoritmu EPROM3 }
45 { - zpracovani prepinacu pro Cfg slovo jinych nez PIC16F }
46 { - export jen platnych dat ( HEX i TXT ) }
47 {=============================================================}
48  
49  
50 {====== Rizeni programatoru - propojeni s HW ======}
51  
52 const P:PGM_p = nil; { Inicializovana promenna }
53  
54 procedure InitHW(Port:word);
55 {== Procedura inicializuje propojeni s HW ==}
56 begin if port<4 then P:=new(PGM_LPT_p,Init(Port))
57 else P:=new(PGM_ALL_P,Init(Port));
58 if P=nil then Error('Unable Init HW',0);
59 end; {InitHW}
60  
61 const StartPICStat:boolean=false; { true po dobu programovani }
62 { Tuto promennou nastavuje procedura StartPIC a vypina procedura }
63 { StopPIC. Je tedy aktivni zejmena po dobu programovani a slouzi }
64 { pro zajisteni vypnuti programovaciho napeti v pripade }
65 { ze program skonci predcasne. }
66  
67 procedure StartPIC(Voltage:real);
68 {== Zapnuti PIC pro programovani, cteni, verifikaci ==}
69 { Voltage udava pracovni napajeci napeti }
70 begin StartPICStat:=true; { true znamena programovani }
71 {-- nastav definovany stav signalu --}
72 P^.SetReset(zero); { aktivuj reset }
73 P^.SetVcc(Voltage); { zapni napajeni }
74 P^.SetData(zero); { otevri budice }
75 P^.SetClock(zero);
76 xDelay(50); { ustaleni napeti }
77 P^.SetReset(one); { ukonci reset }
78 P^.SetVpp(13.0); { zapni Vpp }
79  
80 xDelay(50); { ustaleni napeti }
81 end; {StartPIC}
82  
83 procedure EndPIC;
84 {== Vypnuti PIC po programovani ==}
85 begin P^.SetData(tristate); { odpoj vystupy }
86 P^.SetClock(tristate);
87 P^.SetVpp(0); { odpoj Vpp }
88 P^.SetReset(zero); { aktivuj reset }
89 P^.SetVcc(0); { vypni Vcc }
90 xDelay(50); { pockej }
91 P^.SetReset(one); { a odpoj i reset ( pokud je napajeni zvenku ) }
92 { tak se to rozbehne }
93 StartPICStat:=false; { false znamena konec programovani }
94 end; {EndPIC}
95  
96 {====== Posilani prikazu a dat do a z procesoru ======}
97  
98 procedure OutCommandPIC(Command:word);
99 {== Zapise prikaz ( bez dat ) do PIC ==}
100 var i:integer;
101 begin for i:=1 to 6 do
102 begin if (Command and 1)=1 then P^.SetData(one)
103 else P^.SetData(zero);
104 P^.SetClock(one);
105 P^.SetClock(zero);
106 Command:=Command shr 1;
107 end;
108 P^.SetData(tristate); { odpoj datovy vystup }
109 end; {OutCommandPIC}
110  
111 procedure OutputDataPIC(Bits:word; Command:word; Data:word);
112 {== Zapise prikaz a data do PIC ==}
113 { Prenasi se bud 14 ( obvody 12 nebo 14 bitu core ) nebo 16 ( soucastky }
114 { s paritou pameti programu ) bitu }
115 var i:integer;
116 begin OutCommandPIC(Command);
117 P^.SetData(zero); { start bit 0 }
118 P^.SetClock(one);
119 P^.SetClock(zero);
120 for i:=1 to Bits do
121 begin if (Data and 1)=1 then P^.SetData(one) { data bit }
122 else P^.SetData(zero);
123 P^.SetClock(one);
124 P^.SetClock(zero);
125 Data:=Data shr 1;
126 end;
127 P^.SetData(zero); { stop bit 0 }
128 P^.SetClock(one);
129 P^.SetClock(zero);
130 end; {OutputDataPIC}
131  
132 function InputDataPIC(Bits:word; Command:word):word;
133 {== Posli prikaz a vrat odpoved z PIC ==}
134 { Precte zadany pocet bitu dat. Tento pocet nemusi }
135 { nutne souhlasit s poctem platnych bitu dat }
136 { ( napriklad u souvasti s delkou slova 12 bitu ). }
137 var Data:word;
138 i:integer;
139 b:word;
140 begin OutCommandPIC(Command); { zanecha DATA jako vstup }
141 Data:=0;
142 b:=1; { bitova maska }
143 P^.SetClock(one); { start bit }
144 P^.SetClock(zero);
145 for i:=1 to Bits do
146 begin P^.SetClock(one);
147 P^.SetClock(zero);
148 if P^.GetData then Data:=Data or b; { byla datova 1 }
149 b:=b shl 1; { posun masku }
150 end;
151 P^.SetClock(one); { stop bit }
152 P^.SetClock(zero);
153 InputDataPIC:=Data;
154 end; {InputDataPIC}
155  
156 {====== Programovaci prikazy pro PIC ======}
157  
158 const LoadConfiguration = $00;
159 LoadDataPM = $02;
160 ReadDataPM = $04;
161 IncrementAddress = $06;
162 BeginProgramming = $08;
163 BeginProgrammingOnlyCycle = $18; { _EE2_ - alternativa k BeginProgramming }
164 LoadDataDM = $03;
165 ReadDataDM = $05;
166 BulkErasePM = $09;
167 BulkEraseDM = $0B;
168 EndProgramming = $0E; { _EPROMx_ - jen pro EPROM verze }
169 Dis1 = $01; { _EEx_ - jen pro odstraneni code }
170 Dis2 = $07; { protection }
171  
172 {====== Zpracovani parametru ======}
173  
174 const Port : integer = -1; { Cislo portu pro komunikaci }
175 FileFormat: IO_t = _NIL_; { Format datoveho souboru }
176 CfgString : string = ''; { Sem si zapamatuji Cfg retezec }
177 { Nenastaveny ( nepouzity ) prepinac je -1 }
178  
179 {====== Zpracovani prepinacu ======}
180  
181 function TestSwitch(var s:string; sw:string):boolean;
182 {== Pokud je sw soucasti retezce s vraci true a odstrani sw z retezce s ==}
183 var i:integer;
184 begin TestSwitch:=false;
185 {-- test --}
186 i:=pos(sw+' ',s+' ');
187 if i=0 then exit; { nenalezen }
188 {-- nalezen --}
189 s:=copy(s,1,i-1)+copy(s,i+length(sw)+1,255);
190 TestSwitch:=true;
191 end; {TestSwitch}
192  
193 {====== Zpracovani Cfg parametru ======}
194  
195 procedure CfgSwitches( var s:string; Ostre:boolean;
196 var Cfg:word; var CfgStat:boolean
197 );
198 {== Zpracuje prepinace konfiguracniho slova, pokud je predano true ==}
199 { tak upravi hodnotu predaneho parametru. Uzere z retezce s }
200 { prepinace, ktere zpracovala }
201 { }
202 { s retezec s parametry, zpracovane casti se uzerou }
203 { Ostre true znamena, ze se maji data aktualizovat }
204 { Cfg config data }
205 { CfgStat priznak platnosti dat v poli Cfg }
206 { }
207 begin {-- dle typu procesoru preda konkretni procedure --}
208  
209 end; {CfgSwitches}
210  
211 procedure DisplayConfigWord(Cfg:word; Cfg_Stat:boolean);
212 begin if Cfg_Stat
213 then begin write('Config Word: ');
214 HexWord(output,Cfg);
215 writeln;
216 end
217 else begin writeln('Config Word: none');
218 end;
219 end; {DisplayConfigWord}
220  
221 procedure Switches(s:string; var Data:PicData_t);
222 {== Zpracovani prepinacu, volba typu soucastky, init datoveho objektu ==}
223 var ix:integer; { index do tabulky CfgDefAll}
224 Proc:ProcInfo_t; { informace o vybrane procesoru }
225 begin s:=UpStr(s);
226 {-- adresa portu --}
227 if TestSwitch(s,'LPT3' ) then Port := 3; { Z BIOS tabulky }
228 if TestSwitch(s,'LPT2' ) then Port := 2;
229 if TestSwitch(s,'LPT1' ) then Port := 1;
230 if TestSwitch(s,'ALL03') then Port := $2E0; { ALL03 porty }
231 {-- typ souboru --}
232 if TestSwitch(s,'HEX' ) then FileFormat:= _HEX_; { Moje cislovani filtru }
233 if TestSwitch(s,'TEXT' ) then FileFormat:= _TXT_;
234 if TestSwitch(s,'TXT' ) then FileFormat:= _TXT_;
235 {-- Default hodnoty --}
236 if Port = -1 then Port := 1;
237 if FileFormat = _NIL_ then FileFormat := _TXT_;
238 {-- typ a parametry soucastky --}
239 ProcFind(s,Proc);
240  
241 if Proc.Name=''
242 then begin (* Volba default procesoru je zamerne zaslapnuta protoze mate uzivatele
243 ProcFind('PIC16F84',Proc); { Default hodnota }
244 *)
245 end
246 else if TestSwitch(s,Proc.Name) then; { Sezer pouzitou hodnotu }
247 Data.Init(Proc);
248 {-- prislusnost soucastky do skupiny pro Cfg prepinace --}
249 ix:=CfgFindProcGroup(Proc.Cfg,CfgDefAll); { i je index do konstanty CfgDefAll }
250 {-- zpracovani prepinacu specifickych pro Cfg slovo --}
251 { s ... retezec s prepinacema, ktere jeste nebyly zpracovany }
252 { ix ... index do tabulky CfgDefAll s informacemi o prepinacich skupiny }
253 CfgString:=CfgX; { prazdny ( sama 'X' Cfg retezec }
254 if ix>0 then
255 begin CfgString:=CfgTestKeyMultiple(s,CfgDefAll[ix]);
256 if CfgString='' then Error('Konflicting Cfg switch '+GetWord(s),0);
257 end;
258 if s<>'' then Error('Unrecognized switches: '+s,0);
259 end; {Switches}
260  
261 {====== Hlavni cinnosti ( akce ) programu ======}
262  
263 procedure Help;
264 {== Vypise navod k pouziti ==}
265 var i:integer;
266 Proc:ProcInfo_t;
267 begin writeln('Usage: PICPGR <what_to_do> [<file_name>] [switches]');
268 writeln;
269 writeln('PICPGR READ <file_name> [switches]');
270 writeln('PICPGR PROGRAM <file_name> [switches]');
271 writeln('PICPGR VERIFY <file_name> [switches]');
272 writeln('PICPGR ERASE [switches]');
273 writeln('PICPGR RUN [switches]');
274 writeln('PICPGR STOP [switches]');
275 writeln('PICPGR RESET [switches]');
276 writeln('PICPGR CONVERT <file_in> <file_out> [switches]');
277 writeln;
278 writeln('Exit: 0 O.K.');
279 writeln(' 1 Generic Error');
280 writeln(' 100 Program or Verify Error');
281 writeln;
282 writeln('Switches: LPT1*, LPT2, LPT3 Development Programmer via LPT');
283 writeln('*=default ALL03 HI-LO Programmer');
284 writeln(' TXT*, HEX Data File Format');
285 writeln(' PIC16F84* Processor''s name');
286 writeln;
287 PressEnter;
288 writeln('Note: Processor groups can have their''s own set of switches');
289 writeln(' for overiding of Config Word settins.');
290 writeln;
291 writeln(' Boolean type: CP, CP_ON, CP_OFF');
292 writeln(' ( CP is CP_ON )');
293 writeln;
294 writeln(' Binary string: CP_101');
295 writeln(' ( binary digit string must');
296 writeln(' have corect length )');
297 writeln;
298 writeln('Help function:');
299 writeln;
300 writeln('PICPGR Long help - all processors)');
301 writeln('PICPGR <proc_name> Short help - particular processor');
302 writeln;
303 {-- varianta podle toho, zda mne zajima konkretni procesor --}
304 ProcFind(GetParamLine(1),Proc);
305 if Proc.Name<>''
306 then begin {-- konkretni procesor --}
307 ProcDisplayInfo(Proc); { zobraz co je vybrane }
308 i:=CfgFindProcGroup(Proc.Cfg,CfgDefAll);
309 if i>0 then
310 begin writeln(Proc.Name+' specific switches for Config Word overiding:');
311 writeln;
312 CfgDisplayHelp(CfgDefAll[i]);
313 end;
314 end
315 else begin {-- neni konkretni procesor - zobraz vsechno --}
316 PressEnter;
317 CfgDisplayHelpAll(CfgDefAll);
318 PressEnter;
319 ProcDisplayInfoAll;
320 end;
321 halt(1);
322 end; {Help}
323  
324 var Data:PicDataIo_t; { globalni promenna pro ulozeni dat }
325 { globalni je proto, protoze se neda }
326 { pouzit lokalni ( nevejde se na zasobnik ) }
327  
328 procedure ToDoRead;
329 {== Bude se cist ==}
330 { Precte obsah soucastky do pametovych bufferu }
331 var i:integer;
332 j:integer;
333 Proc:ProcInfo_t;
334 begin {-- Zpracovani parametru --}
335 if paramcount<2 then Help; { chybi jmeno souboru }
336 Switches(GetParamLine(3),Data); { typ procesoru, init Data }
337 Data.GetProcInfo(Proc); { vytahni info o procesoru }
338 if Proc.Name='' then Error('Processor type missing',0);
339 ProcDisplayInfo(Proc); { zobraz }
340 {-- Cteni --}
341 InitHW(Port);
342 writeln('Reading data from PIC ...');
343 StartPIC(5.0);
344 case Proc.Alg of
345 _EPROM1_:
346 begin {-- Read Program Memory --}
347 OutCommandPIC(IncrementAddress); {preskoc konfiguracni slovo}
348 if Proc.PM_Len>0 then
349 for i:=Proc.PM_Base to Proc.PM_Base+Proc.PM_Len-1 do
350 begin Data.StoreProc( i,
351 InputDataPIC(Proc.Bits,ReadDataPM) and Proc.PM_Mask
352 );
353 OutCommandPIC(IncrementAddress);
354 end;
355 {-- Read Config Memory --}
356 if Proc.CM_Len>0 then
357 for i:=Proc.CM_Base to Proc.CM_Base+Proc.CM_Len-1 do
358 begin Data.StoreProc( i,
359 InputDataPIC(Proc.Bits,ReadDataPM) and Proc.CM_Mask
360 );
361 OutCommandPIC(IncrementAddress);
362 end;
363 {--- Read Config Word ---}
364 EndPIC;
365 StartPIC(5.0);
366 Data.StoreProc( Proc.Cfg_Base,
367 InputDataPIC(Proc.Bits,ReadDataPM) and Proc.Cfg_Mask
368 );
369 end;
370 _EPROM2_,
371 _EE1_,
372 _EE2_ :
373 begin {-- Read Program Memory --}
374 if Proc.PM_Len>0 then
375 for i:=Proc.PM_Base to Proc.PM_Base+Proc.PM_Len-1 do
376 begin Data.StoreProc( i,
377 InputDataPIC(Proc.Bits,ReadDataPM) and Proc.PM_Mask
378 );
379 OutCommandPIC(IncrementAddress);
380 end;
381 {-- Read Data Memory --}
382 if Proc.DM_Len>0 then
383 for i:=Proc.DM_Base to Proc.DM_Base+Proc.DM_Len-1 do
384 begin Data.StoreProc( i,
385 InputDataPIC(Proc.Bits,ReadDataDM) and Proc.DM_Mask
386 );
387 OutCommandPIC(IncrementAddress);
388 end;
389 {-- Read Config Memory --}
390 OutputDataPIC(Proc.Bits,LoadConfiguration,$FFFF);
391 if Proc.CM_Len>0 then
392 for i:=Proc.CM_Base to Proc.CM_Base+Proc.CM_Len-1 do
393 begin Data.StoreProc( i,
394 InputDataPIC(Proc.Bits,ReadDataPM) and Proc.CM_Mask
395 );
396 OutCommandPIC(IncrementAddress);
397 end;
398 end;
399 _EPROM3_:
400 begin Error('Algorithm EPROM3 not yet supported',0);
401 end;
402 else Error('Algorithm not supported',0);
403 end; {case}
404 EndPIC;
405 {-- Vystup vysledku --}
406 Data.Export(paramstr(2),FileFormat,';Directly read from '+Proc.Name);
407 writeln('... Done');
408 end; {ToDoRead}
409  
410 procedure Delta(adr,data1,data2:word);
411 {== Vypis info o rozdilu ==}
412 begin HexWord(output,adr);
413 write(': ');
414 HexWord(output,data1);
415 write('-');
416 HexWord(output,data2);
417 writeln;
418 end; {Delta}
419  
420 procedure ToDoProgram;
421 {== Bude se programovat ==}
422 var i:integer;
423 err,count:integer;
424 data_wr:word; { data, ktera se maji zapsat }
425 data_rd:word; { data z kontrolniho cteni }
426 n:integer;
427 Proc:ProcInfo_t;
428 CfgStringTmp:string;
429 begin {-- Zpracovani parametru --}
430 if paramcount<2 then Help; { chybi jmeno souboru }
431 Switches(GetParamLine(3),Data); { typ procesoru, init Data }
432 Data.GetProcInfo(Proc); { vytahni info o procesoru }
433 if Proc.Name='' then Error('Processor type missing',0);
434 ProcDisplayInfo(Proc); { zobraz }
435 InitHW(Port);
436 {-- Vstup dat --}
437 Data.Import(paramstr(2),FileFormat);
438 {-- Program --}
439 writeln('Programming data to PIC ...');
440 err:=0;
441 count:=0;
442 StartPIC(5.0);
443 {-- Program Program Memory --}
444 if Proc.PM_Len>0 then
445 begin
446 if Proc.Alg=_EPROM1_ then OutCommandPIC(IncrementAddress); {preskoc konfiguracni slovo}
447 for i:=Proc.PM_Base to Proc.PM_Base+Proc.PM_Len-1 do
448 begin
449 if Data.GetStat(i) then { platna data ? }
450 begin
451 data_wr:=Data.GetData(i); { vezmi data }
452 case Proc.Alg of
453 _EPROM1_ :
454 begin
455 n:=0;
456 repeat
457 OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
458 OutCommandPIC(BeginProgramming);
459 xDelayMicro(Proc.Tprog);
460 OutCommandPIC(EndProgramming);
461 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.PM_Mask;
462 inc(n);
463 until (n>8) or (data_rd=data_wr);
464 if data_rd<>data_wr
465 then begin Delta(i,data_wr,data_rd);
466 inc(err);
467 end
468 else for n:=n*11 downto 0 do
469 begin OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
470 OutCommandPIC(BeginProgramming);
471 xDelayMicro(Proc.Tprog);
472 OutCommandPIC(EndProgramming);
473 end;
474 inc(count);
475 end;
476 _EPROM2_ :
477 begin
478 n:=0;
479 repeat
480 OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
481 OutCommandPIC(BeginProgramming);
482 xDelayMicro(Proc.Tprog);
483 OutCommandPIC(EndProgramming);
484 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.PM_Mask;
485 inc(n);
486 until (n>25) or (data_rd=data_wr);
487 if data_rd<>data_wr
488 then begin Delta(i,data_wr,data_rd);
489 inc(err);
490 end
491 else for n:=3*n downto 0 do
492 begin OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
493 OutCommandPIC(BeginProgramming);
494 xDelayMicro(Proc.Tprog);
495 OutCommandPIC(EndProgramming);
496 end;
497 inc(count);
498 end;
499 _EPROM3_ :
500 begin Error('Algorithm EPROM3 not yet supported',0);
501 end;
502 _EE1_,
503 _EE2_ :
504 begin OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
505 OutCommandPIC(BeginProgramming);
506 xDelayMicro(Proc.Tprog);
507 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.PM_Mask;
508 if data_rd <> data_wr
509 then begin Delta(i,data_wr,data_rd);
510 inc(err);
511 end;
512 inc(count);
513 end;
514 else Error('Algorithm not supported',0);
515 end; {case}
516 end; {od if testujicich platnost dat}
517 OutCommandPIC(IncrementAddress); { dalsi adresa PM }
518 end; {od cyklu for pres vsechny adresy}
519 {-- Program Data Memory --}
520 if Proc.DM_Len>0 then
521 for i:=Proc.DM_Base to Proc.DM_Base+Proc.DM_Len-1 do
522 begin
523 if Data.GetStat(i)
524 then
525 begin data_wr:=Data.GetData(i);
526 case Proc.Alg of
527 _EPROM1_,
528 _EPROM2_,
529 _EPROM3_ :
530 begin Error('Internal Error: Algorithm EPROMx does not know Data Memory',0);
531 end;
532 _EE1_,
533 _EE2_ :
534 begin OutputDataPIC(Proc.Bits,LoadDataDM,data_wr);
535 OutCommandPIC(BeginProgramming);
536 xDelayMicro(Proc.Tprog);
537 data_rd:=InputDataPIC(Proc.Bits,ReadDataDM) and Proc.DM_Mask;
538 if data_rd <> data_wr
539 then begin Delta(i,data_wr,data_rd);
540 inc(err);
541 end;
542 inc(count);
543 end;
544 else Error('Algorithm not supported',0);
545 end; {case}
546 end;
547 OutCommandPIC(IncrementAddress);
548 end;
549  
550 {-- priprav Config Word --}
551 if (CfgString<>'') and (CfgString<>CfgX)
552 then begin {-- prepinaci zmeneno Cfg slovo --}
553 writeln('Required Config Word: ',CfgString);
554 if Data.GetStat(Proc.Cfg_Base)
555 then begin {-- Cfg slovo bylo v datovem souboru --}
556 CfgStringTmp:=Word2Str(Data.GetData(Proc.Cfg_Base));
557 writeln('Config Word from data file: ',CfgStringTmp);
558 end
559 else begin {-- Cfg slovo nebylo v datovem souboru --}
560 CfgStringTmp:=CfgX; { prazdne slovo ( sama X ) }
561 end;
562 {-- sloz slovo ze souboru a z prepinacu --}
563 CfgString:=CfgOverride(CfgStringTmp,CfgString);
564 writeln('Result: ',CfgString);
565 {-- uloz slozene slovo do datoveho pole --}
566 Data.StoreProc(Proc.Cfg_Base,Str2Word(CfgString));
567 end;
568  
569 {-- Program Config Memory --}
570 {--- algoritmus _EPROM1_ prikaz Load Configuration nezna}
571 if Proc.Alg<>_EPROM1_ then OutputDataPIC(Proc.Bits,LoadConfiguration,$FFFF);
572 if Proc.CM_Len>0 then
573 for i:=Proc.CM_Base to Proc.CM_Base+Proc.CM_Len-1 do
574 begin
575 if Data.GetStat(i)
576 then
577 begin data_wr:=Data.GetData(i);
578 case Proc.Alg of
579 _EPROM1_ :
580 begin
581 n:=0;
582 repeat
583 OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
584 OutCommandPIC(BeginProgramming);
585 xDelayMicro(Proc.Tprog);
586 OutCommandPIC(EndProgramming);
587 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.CM_Mask;
588 inc(n);
589 until (n>8) or (data_wr=data_rd);
590 if data_rd <> data_wr
591 then begin Delta(i,data_wr,data_rd);
592 inc(err);
593 end
594 else for n:=11*n downto 0 do
595 begin OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
596 OutCommandPIC(BeginProgramming);
597 xDelayMicro(Proc.Tprog);
598 OutCommandPIC(EndProgramming);
599 end;
600 end;
601 _EPROM2_ :
602 begin
603 n:=0;
604 repeat
605 OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
606 OutCommandPIC(BeginProgramming);
607 xDelayMicro(Proc.Tprog);
608 OutCommandPIC(EndProgramming);
609 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.CM_Mask;
610 inc(n);
611 until (n>25) or (data_wr=data_rd);
612 if data_rd <> data_wr
613 then begin Delta(i,data_wr,data_rd);
614 inc(err);
615 end
616 else for n:=3*n downto 0 do
617 begin OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
618 OutCommandPIC(BeginProgramming);
619 xDelayMicro(Proc.Tprog);
620 OutCommandPIC(EndProgramming);
621 end;
622 end;
623 _EPROM3_ :
624 begin Error('Algorithm EPROM3 not yet supported',0);
625 end;
626 _EE1_,
627 _EE2_ :
628 begin OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
629 OutCommandPIC(BeginProgramming);
630 xDelayMicro(Proc.Tprog);
631 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.CM_Mask;
632 if i=Proc.Cfg_Base
633 then begin {-- Cfg bunka - maskuj --}
634 if ( ( data_wr xor data_rd ) and Proc.Cfg_Mask ) <> 0
635 then begin Delta(i,data_wr,data_rd);
636 inc(err);
637 end;
638 end
639 else begin if data_wr <> data_rd
640 then begin Delta(i,data_wr,data_rd);
641 inc(err);
642 end;
643 end;
644 inc(count);
645 end;
646 else Error('Algorithm not supported',0);
647 end; {case}
648 end;
649 OutCommandPIC(IncrementAddress);
650 end;
651  
652 {--- u algoritmu _EPROM1_ se konfiguracni slovo programuje zvlast}
653 if Proc.Alg=_EPROM1_ then
654 if Data.GetStat(Proc.Cfg_Base) then
655 begin
656 {--- toto zajisti prechod na konfig. bunku}
657 EndPIC;
658 StartPIC(5.0);
659 data_wr:=Data.GetData(Proc.Cfg_Base);
660 n:=0;
661 repeat
662 OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
663 OutCommandPIC(BeginProgramming);
664 xDelayMicro(Proc.Tprog);
665 OutCommandPIC(EndProgramming);
666 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.Cfg_Mask;
667 inc(n);
668 until (n>8) or (data_rd=data_wr);
669 if data_rd<>data_wr
670 then begin Delta(Proc.Cfg_Mask,data_wr,data_rd);
671 inc(err);
672 end
673 else for n:=n*11 downto 0 do
674 begin OutputDataPIC(Proc.Bits,LoadDataPM,data_wr);
675 OutCommandPIC(BeginProgramming);
676 xDelayMicro(Proc.Tprog);
677 OutCommandPIC(EndProgramming);
678 end;
679 inc(count);
680 end;
681 end;
682 EndPIC;
683 writeln('... Done');
684 write('Programmed ',count,' Data Words, ');
685 case err of
686  
687 1 : writeln(err,' diference found');
688 else writeln(err,' diferences found');
689 end;{case}
690 if err<>0 then exitcode:=100; { Jako by bylo halt(100) }
691 end; {ToDoProgram}
692  
693 procedure ToDoVerify;
694 {== Porovnani obsahu soucastky se souborem ==}
695 var i:integer;
696 data_fi:word; { data z objektu }
697 data_rd:word; { data prectena ze soucastky }
698 err,count:integer;
699 Proc:ProcInfo_t;
700 begin {-- Zpracovani parametru --}
701 if paramcount<2 then Help; { chybi jmeno souboru }
702 Switches(GetParamLine(3),Data); { typ procesoru, init Data }
703 Data.GetProcInfo(Proc); { vytahni info o procesoru }
704 if Proc.Name='' then Error('Processor type missing',0);
705 ProcDisplayInfo(Proc); { zobraz }
706 InitHW(Port);
707 {-- Vstup dat --}
708 Data.Import(paramstr(2),FileFormat);
709 {-- Verify --}
710 writeln('Verifying ...');
711 err:=0;
712 count:=0;
713 StartPIC(5.0);
714 case Proc.Alg of { toto je jen test na podporovane algoritmy }
715 _EPROM1_,
716 _EPROM2_,
717 _EE1_,
718 _EE2_ :
719 begin {-- Verify Program Memory --}
720 if Proc.PM_Len>0 then
721 for i:=Proc.PM_Base to Proc.PM_Base+Proc.PM_Len-1 do
722 begin
723 if Data.GetStat(i) { platna data }
724 then begin data_fi:=Data.GetData(i);
725 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.PM_Mask;
726 if data_fi <> data_rd
727 then begin Delta(i,data_fi,data_rd);
728 inc(err);
729 end;
730 inc(count);
731 end;
732 OutCommandPIC(IncrementAddress);
733 end;
734 {-- Verify Data Memory --}
735 if Proc.DM_Len>0 then
736 for i:=Proc.DM_Base to Proc.DM_Base+Proc.DM_Len-1 do
737 begin
738 if Data.GetStat(i) { platna data }
739 then begin data_fi:=Data.GetData(i);
740 data_rd:=InputDataPIC(Proc.Bits,ReadDataDM) and Proc.DM_Mask;
741 if data_rd <> data_fi
742 then begin Delta(i,data_fi,data_rd);
743 inc(err);
744 end;
745 inc(count);
746 end;
747 OutCommandPIC(IncrementAddress);
748 end;
749 {-- Verify Config Memory --}
750 OutputDataPIC(Proc.Bits,LoadConfiguration,$FFFF);
751 if Proc.CM_Len>0 then
752 for i:=Proc.CM_Base to Proc.CM_Base+Proc.CM_Len-1 do
753 begin
754 if Data.GetStat(i)
755 then begin data_fi:=Data.GetData(i);
756 data_rd:=InputDataPIC(Proc.Bits,ReadDataPM) and Proc.CM_Mask;
757 if i=Proc.Cfg_Base
758 then begin {-- Cfg bunka - maskuj --}
759 if ( ( data_fi xor data_rd ) and Proc.Cfg_Mask ) <> 0
760 then begin Delta(i,data_fi,data_rd);
761 inc(err);
762 end;
763 end
764 else begin if data_fi <> data_rd
765 then begin Delta(i,data_fi,data_rd);
766 inc(err);
767 end;
768 end;
769 inc(count);
770 end;
771 OutCommandPIC(IncrementAddress);
772 end;
773 end;
774 _EPROM3_:
775 begin Error('Algorithm EPROM3 not yet supported',0);
776 end;
777 else Error('Algorithm not supported',0);
778 end; {case}
779 EndPIC;
780 writeln('... Done');
781 write('Compared ',count,' Data Words, ');
782 case err of
783  
784 1 : writeln(err,' diference found');
785 else writeln(err,' diferences found');
786 end;{case}
787 if err<>0 then exitcode:=100; { Jako by bylo halt(100) }
788 end; {ToDoVerify}
789  
790 procedure ToDoErase;
791 {== Bude se pouze mazat ==}
792 var i:integer;
793 Proc:ProcInfo_t;
794 begin {-- Zpracovani parametru --}
795 Switches(GetParamLine(2),Data); { typ procesoru, init Data }
796 Data.GetProcInfo(Proc); { vytahni info o procesoru }
797 if Proc.Name='' then Error('Processor type missing',0);
798 ProcDisplayInfo(Proc); { zobraz }
799 {-- Erase --}
800 case Proc.Alg of
801 _EPROM1_,
802 _EPROM2_,
803 _EPROM3_ :
804 begin Error('Use UV light to erase EPROM processor!',0)
805 end;
806 _EE1_,
807 _EE2_ :
808 begin InitHW(Port);
809 writeln('Erasing PIC');
810 StartPIC(5.0);
811 {-- postup pro odblokovani CP soucastky --}
812 OutputDataPIC(Proc.Bits,LoadConfiguration,$FFFF);
813 for i:=1 to Proc.CM_Len-1 do OutCommandPIC(IncrementAddress);
814 OutCommandPIC(Dis1);
815 OutCommandPIC(Dis2);
816 OutCommandPIC(BeginProgramming);
817 xDelayMicro(12000);
818 OutCommandPIC(Dis1);
819 OutCommandPIC(Dis2);
820 {-- Mazani datove pameti --}
821 { Funguje na C84/F84/F877 ale postup je uveden jen }
822 { u obvodu F84. Obvod C84 potrebuje stejny postup. }
823 OutputDataPIC(Proc.Bits,LoadDataDM,$FFFF);
824 OutCommandPIC(Dis1);
825 OutCommandPIC(Dis2);
826 OutCommandPIC(BeginProgramming);
827 xDelayMicro(10000);
828 OutCommandPIC(Dis1);
829 OutCommandPIC(Dis2);
830 EndPIC;
831 end;
832 else Error('Algorithm not supported',0);
833 end; {case}
834 end; {ToDoErase}
835  
836 procedure ToDoRun;
837 {== Zapne napajeni a spusti program ==}
838 begin Switches(GetParamLine(2),Data); { typ procesoru, init Data }
839 InitHW(Port);
840 P^.SetReset(zero); { aktivuj reset }
841 P^.SetVcc(5.0); { zapni napajeni }
842 xDelay(50); { pockej na ustaleni }
843 P^.SetReset(one); { skonci reset }
844 writeln('Running ...');
845 end; {ToDoRun}
846  
847 procedure ToDoStop;
848 {== Vypne napajeni ==}
849 begin Switches(GetParamLine(2),Data); { typ procesoru, init Data }
850 InitHW(Port);
851 P^.SetReset(zero);
852 P^.SetVcc(0);
853 writeln('... Stoped');
854 end; {ToDoStop}
855  
856 procedure ToDoReset;
857 {== Provede Reset bez vypnuti napajeni ==}
858 begin Switches(GetParamLine(2),Data); { typ procesoru, init Data }
859 InitHW(Port);
860 P^.SetReset(zero);
861 P^.SetVcc(5.0);
862 xDelay(50);
863 P^.SetReset(one);
864 writeln('... Reset ...');
865 end; {ToDoReset}
866  
867 procedure ToDoConvert;
868 {== Procedura pro konverzi formatu souboru ==}
869 var s:string;
870 i:integer;
871 Proc:ProcInfo_t;
872 begin {-- Zpracovani parametru --}
873 if paramcount<3 then Help; { chybi jmeno souboru }
874 Switches(GetParamLine(4),Data); { typ procesoru, init Data }
875 Data.GetProcInfo(Proc); { vytahni info o procesoru }
876 if Proc.Name='' then Error('Processor type missing',0);
877 ProcDisplayInfo(Proc); { zobraz }
878 {-- Vstup dat --}
879 Data.Import(paramstr(2),FileFormat);
880 {-- Vystup dat --}
881 case FileFormat of
882 _HEX_ : FileFormat:=_TXT_;
883 _TXT_ : FileFormat:=_HEX_;
884 end; {case}
885 Data.Export(paramstr(3),FileFormat,';Converted from file '+paramstr(2));
886 writeln('Done');
887 end; {ToDoConvert}
888  
889 {====== Hlavni program, Entry a Exit programy ======}
890  
891 const OldExitProc:pointer=NIL; { Pro proceduru MyExitProc }
892  
893 procedure MyExitProc;
894 {== Ukoncujici procedura pro pripad predcasneho ukonceni programu ==}
895 { Tato procedura normalni nic ndela ale pokud je pri ukonceni }
896 { programu nastaven priznak aktivity programovani zavola }
897 { proceduru StopPIC. }
898 { Promenne: StartPICStat .. true znamena aktivitu pri programovani }
899 { P .. pointer na objekt zastupujici hardware }
900 far;
901 begin ExitProc:=OldExitProc;
902 if StartPICStat and (P<>nil) then EndPIC;
903 end; {MyExitProc}
904  
905 begin assign(output,''); { aby slo vystup presmerovat do souboru }
906 rewrite(output);
907 writeln;
908 writeln('PIC Development Programmer');
909 writeln('==========================');
910 writeln('(c) miho ',date,' v ',ver);
911 writeln;
912  
913 {-- test - zadny parametr --}
914 if paramcount=0 then Help;
915 {-- zaregistruj ukonceni pro pripad predcasneho skonceni programu --}
916 OldExitProc:=ExitProc;
917 ExitProc:=@MyExitProc;
918 {-- rozhodni cinnost --}
919 if UpStr(paramstr(1))='READ' then ToDoRead
920 else if UpStr(paramstr(1))='PROGRAM' then ToDoProgram
921 else if UpStr(paramstr(1))='VERIFY' then ToDoVerify
922 else if UpStr(paramstr(1))='ERASE' then ToDoErase
923 else if UpStr(paramstr(1))='RUN' then ToDoRun
924 else if UpStr(paramstr(1))='STOP' then ToDoStop
925 else if UpStr(paramstr(1))='RESET' then ToDoReset
926 else if UpStr(paramstr(1))='CONVERT' then ToDoConvert
927 else if UpStr(paramstr(1))='HELP' then Help
928 else Help;
929 Halt(exitcode); { Exitcode si nastavuji pri chybe pri programovani }
930 end. { nebo pri verifikaci. Je to treba uvest takto }
931 { explicitne jinak se provede Halt(0). }