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