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