-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprobes.c
760 lines (604 loc) · 20.3 KB
/
probes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/* ************************************************************************
*
* probing testpins
*
* (c) 2012-2019 by Markus Reschke
* based on code from Markus Frejek and Karl-Heinz Kübbeler
*
* ************************************************************************ */
/*
* local constants
*/
/* source management */
#define PROBES_C
/*
* include header files
*/
/* local includes */
#include "config.h" /* global configuration */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
/* ************************************************************************
* support functions
* ************************************************************************ */
/*
* set up probes, bitmasks for probes and test resistors
*
* requires:
* - Probe1: pin ID [0-2], mostly high level pin
* - Probe2: pin ID [0-2], mostly low level pin
* - Probe3: pin ID [0-2], mostly switch/gate pin
*/
void UpdateProbes(uint8_t Probe1, uint8_t Probe2, uint8_t Probe3)
{
/* set probe IDs */
Probes.ID_1 = Probe1;
Probes.ID_2 = Probe2;
Probes.ID_3 = Probe3;
/* set masks using bitmask tables */
Probes.Rl_1 = eeprom_read_byte(&Rl_table[Probe1]);
Probes.Rl_2 = eeprom_read_byte(&Rl_table[Probe2]);
Probes.Rl_3 = eeprom_read_byte(&Rl_table[Probe3]);
Probes.Rh_1 = eeprom_read_byte(&Rh_table[Probe1]);
Probes.Rh_2 = eeprom_read_byte(&Rh_table[Probe2]);
Probes.Rh_3 = eeprom_read_byte(&Rh_table[Probe3]);
Probes.Pin_1 = eeprom_read_byte(&Pin_table[Probe1]);
Probes.Pin_2 = eeprom_read_byte(&Pin_table[Probe2]);
Probes.Pin_3 = eeprom_read_byte(&Pin_table[Probe3]);
/* set ADC MUX input addresses */
Probes.ADC_1 = eeprom_read_byte(&ADC_table[Probe1]);
Probes.ADC_2 = eeprom_read_byte(&ADC_table[Probe2]);
Probes.ADC_3 = eeprom_read_byte(&ADC_table[Probe3]);
}
/*
* restore original probe IDs
*/
void RestoreProbes(void)
{
/* call probe update for saved IDs */
UpdateProbes(Probes.ID2_1, Probes.ID2_2, Probes.ID2_3);
}
/*
* backup current probe IDs
*/
void BackupProbes(void)
{
/* copy probe IDs */
Probes.ID2_1 = Probes.ID_1;
Probes.ID2_2 = Probes.ID_2;
Probes.ID2_3 = Probes.ID_3;
}
/*
* get ID of third probe
*
* requires:
* - Probe1: ID of first probe (0-2)
* - Probe2: ID of second probe (0-2)
*
* returns:
* - ID of third probe (0-2)
*/
uint8_t GetThirdProbe(uint8_t Probe1, uint8_t Probe2)
{
uint8_t Probe3; /* ID of third probe */
/*
* - use sum of probe IDs (order doesn't matter)
* probes #1 & #2: 0 + 1 = 1 -> probe #3: 2
* probes #1 & #3: 0 + 2 = 2 -> probe #2: 1
* probes #2 & #3: 1 + 2 = 3 -> probe #1: 0
* - third probe = 3 - (probe ID #1 + probe ID #2)
*/
Probe3 = 3;
Probe3 -= Probe1;
Probe3 -= Probe2;
return Probe3;
}
/*
* check for a short circuit between two probes
* - changes probe settings
*
* requires:
* - ID of first probe (0-2)
* - ID of second probe (0-2)
*
* returns:
* - 0 if not shorted
* - 1 if shorted
*/
uint8_t ShortedPair(uint8_t Probe1, uint8_t Probe2)
{
uint8_t Flag = 0; /* return value */
uint16_t U1; /* voltage at probe #1 in mV */
uint16_t U2; /* voltage at probe #2 in mV */
uint16_t Min; /* lower threshold */
uint16_t Max; /* upper threshold */
UpdateProbes(Probe1, Probe2, 0); /* update probes */
/*
* Set up a voltage divider between the two probes:
* - Gnd -- Rl -- probe-2 / probe-1 -- Rl -- Vcc
* - third probe: HiZ
*/
ADC_DDR = 0; /* set ADC port to HiZ */
R_PORT = Probes.Rl_1; /* pull up probe-1 via Rl */
R_DDR = Probes.Rl_1 | Probes.Rl_2; /* and pull down probe-2 via Rl */
/* read voltages */
U1 = ReadU_5ms(Probes.ADC_1);
U2 = ReadU(Probes.ADC_2);
/*
* We expect both probe voltages to be about the same and
* to be half of Vcc (allowed difference +/- 30mV).
*/
Min = (Cfg.Vcc / 2) - 30; /* lower voltage */
Max = (Cfg.Vcc / 2) + 30; /* upper voltage */
if ((U1 > Min) && (U1 < Max)) /* U1 within window */
{
if ((U2 > Min) && (U2 < Max)) /* U2 within window */
{
Flag = 1; /* about the same */
}
}
R_DDR = 0; /* reset port */
return Flag;
}
/*
* check for a short circuit between all probes
*
* returns:
* - 0 if no probes are short-circuited
* - number of probe pairs short-circuited (3 = all)
*/
uint8_t ShortedProbes(void)
{
uint8_t Flag = 0; /* return value */
/* check all possible combinations */
Flag = ShortedPair(PROBE_1, PROBE_2);
Flag += ShortedPair(PROBE_1, PROBE_3);
Flag += ShortedPair(PROBE_2, PROBE_3);
return Flag;
}
/*
* try to discharge any connected components, e.g. capacitors
* - detect batteries
* - sometimes large caps are detected as a battery
*/
void DischargeProbes(void)
{
uint8_t Counter; /* loop control */
uint8_t Limit = 40; /* sliding timeout (2s) */
uint8_t ID; /* test pin */
uint8_t DischargeMask; /* bitmask */
uint16_t U_c; /* current voltage */
uint16_t U_old[3]; /* old voltages */
/*
* set probes to a safe discharge mode (pull-down via Rh)
*/
/* set ADC port to HiZ input */
ADC_DDR = 0;
ADC_PORT = 0;
/* all probe pins: Rh and Rl pull-down */
R_PORT = 0;
R_DDR = (1 << R_RH_1) | (1 << R_RH_2) | (1 << R_RH_3) |
(1 << R_RL_1) | (1 << R_RL_2) | (1 << R_RL_3);
/* get current voltages */
U_old[0] = ReadU(TP1);
U_old[1] = ReadU(TP2);
U_old[2] = ReadU(TP3);
/*
* try to discharge probes
* - We check if the voltage decreases over time.
* - A slow discharge rate will increase the timeout to support
* large caps.
* - A very large cap will discharge too slowly and an external voltage
* maybe never :)
*/
Counter = 1;
ID = 2;
DischargeMask = 0;
while (Counter > 0)
{
ID++; /* next probe */
if (ID > 2) ID = 0; /* start with probe #1 again */
if (DischargeMask & (1 << ID)) /* skip discharged probe */
continue;
U_c = ReadU(ID); /* get voltage of probe */
if (U_c < U_old[ID]) /* voltage decreased */
{
U_old[ID] = U_c; /* update old value */
/* adapt timeout based on discharge rate */
if ((Limit - Counter) < 20)
{
/* increase timeout while preventing overflow */
if (Limit < (255 - 20)) Limit += 20;
}
Counter = 1; /* reset no-changes counter */
}
else /* voltage not decreased */
{
/* increase limit if we start at a low voltage */
if ((U_c < 10) && (Limit <= 40)) Limit = 80;
Counter++; /* increase no-changes counter */
}
if (U_c <= CAP_DISCHARGED) /* seems to be discharged */
{
DischargeMask |= (1 << ID); /* set flag */
}
else if (U_c < 800) /* extra pull-down */
{
/* it's save now to pull down probe pin directly */
ADC_DDR |= eeprom_read_byte(&Pin_table[ID]);
}
if (DischargeMask == 0b00000111) /* all probes discharged */
{
Counter = 0; /* end loop */
}
else if (Counter > Limit) /* no decrease for some time */
{
/* might be a battery or a super cap */
Check.Found = COMP_ERROR; /* report error */
Check.Type = TYPE_DISCHARGE; /* discharge problem */
Check.Probe = ID; /* save probe */
Check.U = U_c; /* save voltage */
Counter = 0; /* end loop */
}
else /* go for another round */
{
wdt_reset(); /* reset watchdog */
MilliSleep(50); /* wait for 50ms */
}
}
/* reset probes */
R_DDR = 0; /* set resistor port to input mode */
ADC_DDR = 0; /* set ADC port to input mode */
}
/*
* pull probe up/down via probe resistor for 1 or 10 ms
*
* requires:
* - mask for probe resistors
* - pull mode (bit flags):
* 0b00000000 = down
* 0b00000001 = up
* 0b00000100 = 1ms
* 0b00001000 = 10ms
*/
void PullProbe(uint8_t Mask, uint8_t Mode)
{
/* set pull mode */
if (Mode & PULL_UP) R_PORT |= Mask; /* pull-up */
else R_PORT &= ~Mask; /* pull-down */
R_DDR |= Mask; /* enable pulling */
if (Mode & PULL_1MS) wait1ms(); /* wait 1ms */
else wait10ms(); /* wait 10ms */
/* reset pulling */
R_DDR &= ~Mask; /* set to HiZ mode */
R_PORT &= ~Mask; /* set 0 */
}
/*
* lookup a voltage/ratio based factor in a table and interpolate it's value
* - value decreases with index position
*
* requires:
* - voltage (in mV) or ratio
* - table ID
*
* returns:
* - multiplicator/factor
*/
uint16_t GetFactor(uint16_t U_in, uint8_t ID)
{
uint16_t Factor; /* return value */
uint16_t U_Diff; /* voltage difference to table start */
uint16_t Fact1, Fact2; /* table entries */
uint16_t TabStart; /* table start voltage */
uint16_t TabStep; /* table step voltage */
uint16_t TabIndex; /* table entries (-2) */
uint16_t *Table; /* pointer to table */
uint8_t Index; /* table index */
uint8_t Diff; /* difference to next entry */
/*
* set up table specific stuff
*/
if (ID == TABLE_SMALL_CAP)
{
TabStart = 1000; /* table starts at 1000mV */
TabStep = 50; /* 50mV steps between entries */
TabIndex = (NUM_SMALL_CAP - 2); /* entries in table - 2 */
Table = (uint16_t *)&SmallCap_table[0]; /* pointer to table start */
}
else if (ID == TABLE_LARGE_CAP)
{
TabStart = 300; /* table starts at 1000mV */
TabStep = 25; /* 25mV steps between entries */
TabIndex = (NUM_LARGE_CAP - 2); /* entries in table - 2 */
Table = (uint16_t *)&LargeCap_table[0]; /* pointer to table start */
}
#ifdef SW_INDUCTOR
else if (ID == TABLE_INDUCTOR)
{
TabStart = 200; /* table starts at 200 */
TabStep = 25; /* steps between entries */
TabIndex = (NUM_INDUCTOR - 2); /* entries in table - 2 */
Table = (uint16_t *)&Inductor_table[0]; /* pointer to table start */
}
#endif
else
{
return 0;
}
/*
* We interpolate the table values corresponding to the given voltage/ratio.
*/
/* difference to start of table */
if (U_in >= TabStart) U_Diff = U_in - TabStart;
else U_Diff = 0;
/* calculate table index */
Index = U_Diff / TabStep; /* index (position in table) */
Diff = U_Diff % TabStep; /* difference to index */
Diff = TabStep - Diff; /* difference to next entry */
/* prevent index overflow */
if (Index > TabIndex) Index = TabIndex;
/* get values for index and next entry */
Table += Index; /* advance to index */
Fact1 = eeprom_read_word(Table);
Table++; /* next entry */
Fact2 = eeprom_read_word(Table);
/* interpolate values based on the difference */
Factor = Fact1 - Fact2;
Factor *= Diff;
Factor += TabStep / 2;
Factor /= TabStep;
Factor += Fact2;
return Factor;
}
/* ************************************************************************
* identify component
* ************************************************************************ */
/*
* probe connected component and try to identify it
*
* requires:
* - Probe1: ID of probe to be pulled up [0-2]
* - Probe2: ID of probe to be pulled down [0-2]
* - Probe3: ID of probe to be in HiZ mode [0-2]
*/
void CheckProbes(uint8_t Probe1, uint8_t Probe2, uint8_t Probe3)
{
uint8_t Flag; /* temporary value */
uint16_t U_Rl; /* voltage across Rl (load) */
uint16_t U_1; /* voltage #1 */
/* init */
if (Check.Found == COMP_ERROR) return; /* skip check on any error */
wdt_reset(); /* reset watchdog */
UpdateProbes(Probe1, Probe2, Probe3); /* update bitmasks */
/*
* We measure the current from probe 2 to ground with probe 1 pulled up
* to 5V and probe 3 in HiZ mode to determine if we got a self-conducting
* part, i.e. diode, resistor or depletion-mode FET. Rl is used as current
* shunt.
*
* In case of a FET we have to take care about the gate charge based on
* the channel type.
*/
/* set probes: Gnd -- Rl -- probe-2 / probe-1 -- Vcc */
R_PORT = 0; /* set resistor port to Gnd */
R_DDR = Probes.Rl_2; /* pull down probe-2 via Rl */
ADC_DDR = Probes.Pin_1; /* set probe-1 to output */
ADC_PORT = Probes.Pin_1; /* pull-up probe-1 directly */
/*
* For a possible n channel FET we pull down the gate for a few ms,
* assuming: probe-1 = D / probe-2 = S / probe-3 = G
*
* Hint: The pull-down of the gate will trigger a possible PUT.
*/
PullProbe(Probes.Rl_3, PULL_10MS | PULL_DOWN); /* discharge gate via Rl */
U_Rl = ReadU_5ms(Probes.ADC_2); /* get voltage at Rl */
/*
* If we got conduction we could have a p channel FET. For any
* other part U_Rl will stay the same.
*/
if (U_Rl >= 977) /* > 1.4mA */
{
/*
* For a possible p channel FET we pull up the gate for a few ms,
* assuming: probe-1 = S / probe-2 = D / probe-3 = G
*/
PullProbe(Probes.Rl_3, PULL_10MS | PULL_UP); /* discharge gate via Rl */
U_Rl = ReadU_5ms(Probes.ADC_2); /* get voltage at Rl */
}
/*
* If there's some current we could have a depletion-mode FET
* (self-conducting).
*
* Other possibilities:
* - diode or resistor
*/
if (U_Rl > 15) /* > 21µA */
{
if (Check.Done == DONE_NONE) /* not sure yet */
{
CheckDepletionModeFET(U_Rl);
}
}
/*
* If there's nearly no conduction (just a small leakage current) between
* probe-1 and probe-2 we might have a semiconductor:
* - BJT
* - enhancement mode FET or IGBT
* - Thyristor or Triac
* - or a large resistor
*/
if (U_Rl < 977) /* load current < 1.4mA (resistance > 3k) */
{
/*
* check for:
* - PNP BJT (common emitter circuit)
* - p-channel MOSFET (low side switching circuit) or IGBT
*/
if (Check.Done == DONE_NONE) /* not sure yet */
{
/*
* we assume:
* - BJT: probe-1 = E / probe-2 = C / probe-3 = B
* - FET: probe-1 = S / probe-2 = D / probe-3 = G
*/
/* set probes: Gnd -- Rl - probe-2 / probe-1 -- Vcc / probe-3 -- Rl -- Gnd */
R_DDR = Probes.Rl_2; /* enable Rl for probe-2 */
R_PORT = 0; /* pull down collector via Rl */
ADC_DDR = Probes.Pin_1; /* set probe-1 to output */
ADC_PORT = Probes.Pin_1; /* pull up emitter directly */
wait5ms();
R_DDR = Probes.Rl_2 | Probes.Rl_3; /* pull down base via Rl */
U_1 = ReadU_5ms(Probe2); /* get voltage at collector */
/*
* If DUT is conducting we might have a PNP BJT or p-channel FET.
*/
if (U_1 > 3422) /* detected current > 4.8mA */
{
/* distinguish PNP BJT from p-channel MOSFET or IGBT */
CheckTransistor(TYPE_PNP, U_Rl);
}
}
/*
* check for
* - NPN BJT (common emitter circuit)
* - Thyristor and TRIAC
* - n-channel MOSFET (high side switching circuit) or IGBT
*/
if (Check.Done == DONE_NONE) /* not sure yet */
{
/*
* we assume:
* - BJT: probe-1 = C / probe-2 = E / probe-3 = B
* - FET: probe-1 = D / probe-2 = S / probe-3 = G
* - SCR: probe-1 = A / probe-2 = C / probe-3 = G
* - TRIAC: probe-1 = MT2 / probe-2 = MT1 / probe-3 = G
*/
/* set probes: Gnd -- probe-2 / probe-1 -- Rl -- Vcc / probe-3 -- Rl -- Vcc */
ADC_DDR = Probes.Pin_2; /* set probe-2 to output mode */
ADC_PORT = 0; /* pull down probe-2 directly */
R_DDR = Probes.Rl_1 | Probes.Rl_3; /* select Rl for probe-1 & Rl for probe-3 */
R_PORT = Probes.Rl_1 | Probes.Rl_3; /* pull up collector & base via Rl */
U_1 = ReadU_5ms(Probe1); /* get voltage at collector */
/*
* If DUT is conducting we might have an NPN BJT, something similar or
* an n-channel MOSFET.
*/
if (U_1 < 1600) /* detected current > 4.8mA */
{
/* first check for Thyristor and TRIAC */
Flag = CheckThyristorTriac();
if (Flag == 0) /* no Thyristor or TRIAC */
{
/* If we've detected a TRIAC in a former run don't check for BJT. */
if (Check.Found != COMP_TRIAC)
{
/* We might have an NPN BJT, an n-channel MOSFET or IGBT. */
CheckTransistor(TYPE_NPN, U_Rl);
}
}
}
}
#ifdef SW_UJT
/*
* check for UJT
*/
if (Check.Done == DONE_NONE) /* not sure yet */
{
CheckUJT();
}
#endif
}
/*
* If there's conduction between probe-1 and probe-2 we might have a
* - diode (conducting)
* - small resistor (checked later on)
*/
else /* load current > 1.4mA (resistance < 3k) */
{
/*
* check for a PUT
*/
if (Check.Done == DONE_NONE) /* not sure yet */
{
CheckPUT();
}
/*
* We check for a diode even if we already found a component to get Vf,
* since there could be a body/protection diode of a transistor.
*/
CheckDiode();
}
/*
* Check for a resistor (or another one)
* - if no other component is found yet
* - if we've got a resistor already
*/
if ((Check.Found == COMP_NONE) ||
(Check.Found == COMP_RESISTOR))
{
CheckResistor();
}
/*
* ... otherwise run some final checks.
*/
else
{
/* verify a MOSFET */
if ((Check.Found == COMP_FET) && (Check.Type & TYPE_MOSFET))
{
VerifyMOSFET();
}
}
/* clean up */
ADC_DDR = 0; /* set ADC port to HiZ mode */
ADC_PORT = 0; /* set ADC port low */
R_DDR = 0; /* set resistor port to HiZ mode */
R_PORT = 0; /* set resistor port low */
}
/*
* logic for alternative components which might be found
*/
void CheckAlternatives(void)
{
uint8_t Flag = 0;
/*
* problematic components:
* - PNP with B-E resistor and flyback diode passes UJT test once
* - UJT might pass NPN test once
* - TRIAC passes PUT test twice, but PUT only once
*/
if (Check.AltFound != COMP_NONE) /* alternative found */
{
if (! (Check.Done & DONE_SEMI)) /* not sure about common transistor */
{
/* but sure about alternative or no common transistor found */
if ((Check.Done & DONE_ALTSEMI) || (Check.Found < COMP_BJT))
{
Flag = 1; /* choose alternative component */
}
}
}
if (Flag) /* take alternative component */
{
/* copy some data */
Check.Found = Check.AltFound;
Semi.A = AltSemi.A;
Semi.B = AltSemi.B;
Semi.C = AltSemi.C;
#ifdef SW_SYMBOLS
Check.Symbol = Check.AltSymbol;
#endif
}
/*
* add other special checks here
*/
}
/* ************************************************************************
* clean-up of local constants
* ************************************************************************ */
/* source management */
#undef PROBES_C
/* ************************************************************************
* EOF
* ************************************************************************ */